agendaexport.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. /* Copyright (C) 2008-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/public/agenda/agendaexport.php
  19. * \ingroup agenda
  20. * \brief Page to export agenda
  21. * http://127.0.0.1/dolibarr/public/agenda/agendaexport.php?format=vcal&exportkey=cle
  22. * http://127.0.0.1/dolibarr/public/agenda/agendaexport.php?format=ical&type=event&exportkey=cle
  23. * http://127.0.0.1/dolibarr/public/agenda/agendaexport.php?format=rss&exportkey=cle
  24. * Other parameters into url are:
  25. * &notolderthan=99
  26. * &year=2015
  27. * &id=..., &idfrom=..., &idto=...
  28. */
  29. if (!defined('NOTOKENRENEWAL')) {
  30. define('NOTOKENRENEWAL', '1');
  31. }
  32. if (!defined('NOREQUIREMENU')) {
  33. define('NOREQUIREMENU', '1'); // If there is no menu to show
  34. }
  35. if (!defined('NOREQUIREHTML')) {
  36. define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
  37. }
  38. if (!defined('NOREQUIREAJAX')) {
  39. define('NOREQUIREAJAX', '1');
  40. }
  41. if (!defined('NOLOGIN')) {
  42. define("NOLOGIN", 1); // This means this output page does not require to be logged.
  43. }
  44. if (!defined('NOCSRFCHECK')) {
  45. define("NOCSRFCHECK", 1); // We accept to go on this page from external web site.
  46. }
  47. if (!defined('NOIPCHECK')) {
  48. define('NOIPCHECK', '1'); // Do not check IP defined into conf $dolibarr_main_restrict_ip
  49. }
  50. // It's a wrapper, so empty header
  51. /**
  52. * Header function
  53. *
  54. * @return void
  55. */
  56. function llxHeaderVierge()
  57. {
  58. print '<html><title>Export agenda cal</title><body>';
  59. }
  60. /**
  61. * Footer function
  62. *
  63. * @return void
  64. */
  65. function llxFooterVierge()
  66. {
  67. print '</body></html>';
  68. }
  69. // For MultiCompany module.
  70. // Do not use GETPOST here, function is not defined and define must be done before including main.inc.php
  71. $entity = (!empty($_GET['entity']) ? (int) $_GET['entity'] : (!empty($_POST['entity']) ? (int) $_POST['entity'] : 1));
  72. if (is_numeric($entity)) {
  73. define("DOLENTITY", $entity);
  74. }
  75. // Load Dolibarr environment
  76. require '../../main.inc.php';
  77. require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
  78. // Security check
  79. if (empty($conf->agenda->enabled)) {
  80. httponly_accessforbidden('Module Agenda not enabled');
  81. }
  82. // Not older than
  83. if (!isset($conf->global->MAIN_AGENDA_EXPORT_PAST_DELAY)) {
  84. $conf->global->MAIN_AGENDA_EXPORT_PAST_DELAY = 100; // default limit
  85. }
  86. // Define format, type and filter
  87. $format = 'ical';
  88. $type = 'event';
  89. if (GETPOST("format", 'alpha')) {
  90. $format = GETPOST("format", 'alpha');
  91. }
  92. if (GETPOST("type", 'alpha')) {
  93. $type = GETPOST("type", 'alpha');
  94. }
  95. $filters = array();
  96. if (GETPOST("year", 'int')) {
  97. $filters['year'] = GETPOST("year", 'int');
  98. }
  99. if (GETPOST("id", 'int')) {
  100. $filters['id'] = GETPOST("id", 'int');
  101. }
  102. if (GETPOST("idfrom", 'int')) {
  103. $filters['idfrom'] = GETPOST("idfrom", 'int');
  104. }
  105. if (GETPOST("idto", 'int')) {
  106. $filters['idto'] = GETPOST("idto", 'int');
  107. }
  108. if (GETPOST("project", 'alpha')) {
  109. $filters['project'] = GETPOST("project", 'alpha');
  110. }
  111. if (GETPOST("logina", 'alpha')) {
  112. $filters['logina'] = GETPOST("logina", 'alpha');
  113. }
  114. if (GETPOST("logint", 'alpha')) {
  115. $filters['logint'] = GETPOST("logint", 'alpha');
  116. }
  117. if (GETPOST("notactiontype", 'alpha')) {
  118. $filters['notactiontype'] = GETPOST("notactiontype", 'alpha');
  119. }
  120. if (GETPOST("actiontype", 'alpha')) {
  121. $filters['actiontype'] = GETPOST("actiontype", 'alpha');
  122. }
  123. if (GETPOST("notolderthan", 'int')) {
  124. $filters['notolderthan'] = GETPOST("notolderthan", "int");
  125. } else {
  126. $filters['notolderthan'] = $conf->global->MAIN_AGENDA_EXPORT_PAST_DELAY;
  127. }
  128. if (GETPOST("module", 'alpha')) {
  129. $filters['module'] = GETPOST("module", 'alpha');
  130. }
  131. if (GETPOST("status", 'int')) {
  132. $filters['status'] = GETPOST("status", 'int');
  133. }
  134. // Check config
  135. if (empty($conf->global->MAIN_AGENDA_XCAL_EXPORTKEY)) {
  136. $user->getrights();
  137. llxHeaderVierge();
  138. print '<div class="error">Module Agenda was not configured properly.</div>';
  139. llxFooterVierge();
  140. exit;
  141. }
  142. // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array of hooks
  143. $hookmanager->initHooks(array('agendaexport'));
  144. $reshook = $hookmanager->executeHooks('doActions', $filters); // Note that $action and $object may have been modified by some
  145. if ($reshook < 0) {
  146. llxHeaderVierge();
  147. if (!empty($hookmanager->errors) && is_array($hookmanager->errors)) {
  148. print '<div class="error">'.implode('<br>', $hookmanager->errors).'</div>';
  149. } else {
  150. print '<div class="error">'.$hookmanager->error.'</div>';
  151. }
  152. llxFooterVierge();
  153. } elseif (empty($reshook)) {
  154. // Check exportkey
  155. if (empty($_GET["exportkey"]) || $conf->global->MAIN_AGENDA_XCAL_EXPORTKEY != $_GET["exportkey"]) {
  156. $user->getrights();
  157. llxHeaderVierge();
  158. print '<div class="error">Bad value for key.</div>';
  159. llxFooterVierge();
  160. exit;
  161. }
  162. }
  163. // Define filename with prefix on filters predica (each predica set must have on cache file)
  164. $shortfilename = 'dolibarrcalendar';
  165. $filename = $shortfilename;
  166. // Complete long filename
  167. foreach ($filters as $key => $value) {
  168. //if ($key == 'notolderthan') $filename.='-notolderthan'.$value; This filter key is already added before and does not need to be in filename
  169. if ($key == 'year') {
  170. $filename .= '-year'.$value;
  171. }
  172. if ($key == 'id') {
  173. $filename .= '-id'.$value;
  174. }
  175. if ($key == 'idfrom') {
  176. $filename .= '-idfrom'.$value;
  177. }
  178. if ($key == 'idto') {
  179. $filename .= '-idto'.$value;
  180. }
  181. if ($key == 'project') {
  182. $filename .= '-project'.$value;
  183. }
  184. if ($key == 'logina') {
  185. $filename .= '-logina'.$value; // Author
  186. }
  187. if ($key == 'logint') {
  188. $filename .= '-logint'.$value; // Assigned to
  189. }
  190. if ($key == 'notactiontype') {
  191. $filename .= '-notactiontype'.$value;
  192. }
  193. if ($key == 'actiontype') {
  194. $filename .= '-actiontype'.$value;
  195. }
  196. if ($key == 'module') {
  197. $filename .= '-module'.$value;
  198. }
  199. if ($key == 'status') {
  200. $filename .= '-status'.$value;
  201. }
  202. }
  203. // Add extension
  204. if ($format == 'vcal') {
  205. $shortfilename .= '.vcs'; $filename .= '.vcs';
  206. }
  207. if ($format == 'ical') {
  208. $shortfilename .= '.ics'; $filename .= '.ics';
  209. }
  210. if ($format == 'rss') {
  211. $shortfilename .= '.rss'; $filename .= '.rss';
  212. }
  213. if ($shortfilename == 'dolibarrcalendar') {
  214. $langs->load("errors");
  215. llxHeaderVierge();
  216. print '<div class="error">'.$langs->trans("ErrorWrongValueForParameterX", 'format').'</div>';
  217. llxFooterVierge();
  218. exit;
  219. }
  220. $agenda = new ActionComm($db);
  221. $cachedelay = 0;
  222. if (!empty($conf->global->MAIN_AGENDA_EXPORT_CACHE)) {
  223. $cachedelay = $conf->global->MAIN_AGENDA_EXPORT_CACHE;
  224. }
  225. $exportholidays = GETPOST('includeholidays', 'int');
  226. // Build file
  227. if ($format == 'ical' || $format == 'vcal') {
  228. $result = $agenda->build_exportfile($format, $type, $cachedelay, $filename, $filters, $exportholidays);
  229. if ($result >= 0) {
  230. $attachment = true;
  231. if (isset($_GET["attachment"])) {
  232. $attachment = $_GET["attachment"];
  233. }
  234. //$attachment = false;
  235. $contenttype = 'text/calendar';
  236. if (isset($_GET["contenttype"])) {
  237. $contenttype = $_GET["contenttype"];
  238. }
  239. //$contenttype='text/plain';
  240. $outputencoding = 'UTF-8';
  241. if ($contenttype) {
  242. header('Content-Type: '.$contenttype.($outputencoding ? '; charset='.$outputencoding : ''));
  243. }
  244. if ($attachment) {
  245. header('Content-Disposition: attachment; filename="'.$shortfilename.'"');
  246. }
  247. if ($cachedelay) {
  248. header('Cache-Control: max-age='.$cachedelay.', private, must-revalidate');
  249. } else {
  250. header('Cache-Control: private, must-revalidate');
  251. }
  252. // Clean parameters
  253. $outputfile = $conf->agenda->dir_temp.'/'.$filename;
  254. $result = readfile($outputfile);
  255. if (!$result) {
  256. print 'File '.$outputfile.' was empty.';
  257. }
  258. //header("Location: ".DOL_URL_ROOT.'/document.php?modulepart=agenda&file='.urlencode($filename));
  259. exit;
  260. } else {
  261. print 'Error '.$agenda->error;
  262. exit;
  263. }
  264. }
  265. if ($format == 'rss') {
  266. $result = $agenda->build_exportfile($format, $type, $cachedelay, $filename, $filters, $exportholidays);
  267. if ($result >= 0) {
  268. $attachment = false;
  269. if (isset($_GET["attachment"])) {
  270. $attachment = $_GET["attachment"];
  271. }
  272. //$attachment = false;
  273. $contenttype = 'application/rss+xml';
  274. if (isset($_GET["contenttype"])) {
  275. $contenttype = $_GET["contenttype"];
  276. }
  277. //$contenttype='text/plain';
  278. $outputencoding = 'UTF-8';
  279. if ($contenttype) {
  280. header('Content-Type: '.$contenttype.($outputencoding ? '; charset='.$outputencoding : ''));
  281. }
  282. if ($attachment) {
  283. header('Content-Disposition: attachment; filename="'.$filename.'"');
  284. }
  285. // Ajout directives pour resoudre bug IE
  286. //header('Cache-Control: Public, must-revalidate');
  287. //header('Pragma: public');
  288. if ($cachedelay) {
  289. header('Cache-Control: max-age='.$cachedelay.', private, must-revalidate');
  290. } else {
  291. header('Cache-Control: private, must-revalidate');
  292. }
  293. // Clean parameters
  294. $outputfile = $conf->agenda->dir_temp.'/'.$filename;
  295. $result = readfile($outputfile);
  296. if (!$result) {
  297. print 'File '.$outputfile.' was empty.';
  298. }
  299. // header("Location: ".DOL_URL_ROOT.'/document.php?modulepart=agenda&file='.urlencode($filename));
  300. exit;
  301. } else {
  302. print 'Error '.$agenda->error;
  303. exit;
  304. }
  305. }
  306. llxHeaderVierge();
  307. print '<div class="error">'.$agenda->error.'</div>';
  308. llxFooterVierge();