dolibarr  17.0.4
agendaexport.php
Go to the documentation of this file.
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 
31 if (!defined('NOTOKENRENEWAL')) {
32  define('NOTOKENRENEWAL', '1');
33 }
34 if (!defined('NOREQUIREMENU')) {
35  define('NOREQUIREMENU', '1'); // If there is no menu to show
36 }
37 if (!defined('NOREQUIREHTML')) {
38  define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
39 }
40 if (!defined('NOREQUIREAJAX')) {
41  define('NOREQUIREAJAX', '1');
42 }
43 if (!defined('NOLOGIN')) {
44  define("NOLOGIN", 1); // This means this output page does not require to be logged.
45 }
46 if (!defined('NOCSRFCHECK')) {
47  define("NOCSRFCHECK", 1); // We accept to go on this page from external web site.
48 }
49 if (!defined('NOIPCHECK')) {
50  define('NOIPCHECK', '1'); // Do not check IP defined into conf $dolibarr_main_restrict_ip
51 }
52 
53 
54 // It's a wrapper, so empty header
55 
61 function llxHeaderVierge()
62 {
63  print '<html><title>Export agenda cal</title><body>';
64 }
70 function llxFooterVierge()
71 {
72  print '</body></html>';
73 }
74 
75 // For MultiCompany module.
76 // Do not use GETPOST here, function is not defined and define must be done before including main.inc.php
77 $entity = (!empty($_GET['entity']) ? (int) $_GET['entity'] : (!empty($_POST['entity']) ? (int) $_POST['entity'] : 1));
78 if (is_numeric($entity)) {
79  define("DOLENTITY", $entity);
80 }
81 
82 // Load Dolibarr environment
83 require '../../main.inc.php';
84 require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
85 
86 // Security check
87 if (empty($conf->agenda->enabled)) {
88  httponly_accessforbidden('Module Agenda not enabled');
89 }
90 
91 // Not older than
92 if (empty($conf->global->MAIN_AGENDA_EXPORT_PAST_DELAY)) {
93  $conf->global->MAIN_AGENDA_EXPORT_PAST_DELAY = 100; // default limit
94 }
95 
96 // Define format, type and filter
97 $format = 'ical';
98 $type = 'event';
99 if (GETPOST("format", 'alpha')) {
100  $format = GETPOST("format", 'alpha');
101 }
102 if (GETPOST("type", 'alpha')) {
103  $type = GETPOST("type", 'alpha');
104 }
105 
106 $filters = array();
107 if (GETPOST("year", 'int')) {
108  $filters['year'] = GETPOST("year", 'int');
109 }
110 if (GETPOST("id", 'int')) {
111  $filters['id'] = GETPOST("id", 'int');
112 }
113 if (GETPOST("idfrom", 'int')) {
114  $filters['idfrom'] = GETPOST("idfrom", 'int');
115 }
116 if (GETPOST("idto", 'int')) {
117  $filters['idto'] = GETPOST("idto", 'int');
118 }
119 if (GETPOST("project", 'alpha')) {
120  $filters['project'] = GETPOST("project", 'alpha');
121 }
122 if (GETPOST("logina", 'alpha')) {
123  $filters['logina'] = GETPOST("logina", 'alpha');
124 }
125 if (GETPOST("logint", 'alpha')) {
126  $filters['logint'] = GETPOST("logint", 'alpha');
127 }
128 if (GETPOST("notactiontype", 'alpha')) {
129  $filters['notactiontype'] = GETPOST("notactiontype", 'alpha');
130 }
131 if (GETPOST("actiontype", 'alpha')) {
132  $filters['actiontype'] = GETPOST("actiontype", 'alpha');
133 }
134 if (GETPOST("notolderthan", 'int')) {
135  $filters['notolderthan'] = GETPOST("notolderthan", "int");
136 } else {
137  $filters['notolderthan'] = $conf->global->MAIN_AGENDA_EXPORT_PAST_DELAY;
138 }
139 if (GETPOST("module", 'alpha')) {
140  $filters['module'] = GETPOST("module", 'alpha');
141 }
142 if (GETPOST("status", 'int')) {
143  $filters['status'] = GETPOST("status", 'int');
144 }
145 // Check config
146 if (empty($conf->global->MAIN_AGENDA_XCAL_EXPORTKEY)) {
147  $user->getrights();
148 
149  llxHeaderVierge();
150  print '<div class="error">Module Agenda was not configured properly.</div>';
151  llxFooterVierge();
152  exit;
153 }
154 
155 // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array of hooks
156 $hookmanager->initHooks(array('agendaexport'));
157 
158 $reshook = $hookmanager->executeHooks('doActions', $filters); // Note that $action and $object may have been modified by some
159 if ($reshook < 0) {
160  llxHeaderVierge();
161  if (!empty($hookmanager->errors) && is_array($hookmanager->errors)) {
162  print '<div class="error">'.implode('<br>', $hookmanager->errors).'</div>';
163  } else {
164  print '<div class="error">'.$hookmanager->error.'</div>';
165  }
166  llxFooterVierge();
167 } elseif (empty($reshook)) {
168  // Check exportkey
169  if (empty($_GET["exportkey"]) || $conf->global->MAIN_AGENDA_XCAL_EXPORTKEY != $_GET["exportkey"]) {
170  $user->getrights();
171 
172  llxHeaderVierge();
173  print '<div class="error">Bad value for key.</div>';
174  llxFooterVierge();
175  exit;
176  }
177 }
178 
179 
180 // Define filename with prefix on filters predica (each predica set must have on cache file)
181 $shortfilename = 'dolibarrcalendar';
182 $filename = $shortfilename;
183 // Complete long filename
184 foreach ($filters as $key => $value) {
185  //if ($key == 'notolderthan') $filename.='-notolderthan'.$value; This filter key is already added before and does not need to be in filename
186  if ($key == 'year') {
187  $filename .= '-year'.$value;
188  }
189  if ($key == 'id') {
190  $filename .= '-id'.$value;
191  }
192  if ($key == 'idfrom') {
193  $filename .= '-idfrom'.$value;
194  }
195  if ($key == 'idto') {
196  $filename .= '-idto'.$value;
197  }
198  if ($key == 'project') {
199  $filename .= '-project'.$value;
200  }
201  if ($key == 'logina') {
202  $filename .= '-logina'.$value; // Author
203  }
204  if ($key == 'logint') {
205  $filename .= '-logint'.$value; // Assigned to
206  }
207  if ($key == 'notactiontype') {
208  $filename .= '-notactiontype'.$value;
209  }
210  if ($key == 'actiontype') {
211  $filename .= '-actiontype'.$value;
212  }
213  if ($key == 'module') {
214  $filename .= '-module'.$value;
215  }
216  if ($key == 'status') {
217  $filename .= '-status'.$value;
218  }
219 }
220 // Add extension
221 if ($format == 'vcal') {
222  $shortfilename .= '.vcs'; $filename .= '.vcs';
223 }
224 if ($format == 'ical') {
225  $shortfilename .= '.ics'; $filename .= '.ics';
226 }
227 if ($format == 'rss') {
228  $shortfilename .= '.rss'; $filename .= '.rss';
229 }
230 if ($shortfilename == 'dolibarrcalendar') {
231  $langs->load("errors");
232  llxHeaderVierge();
233  print '<div class="error">'.$langs->trans("ErrorWrongValueForParameterX", 'format').'</div>';
234  llxFooterVierge();
235  exit;
236 }
237 
238 $agenda = new ActionComm($db);
239 
240 $cachedelay = 0;
241 if (!empty($conf->global->MAIN_AGENDA_EXPORT_CACHE)) {
242  $cachedelay = $conf->global->MAIN_AGENDA_EXPORT_CACHE;
243 }
244 
245 $exportholidays = GETPOST('includeholidays', 'int');
246 
247 // Build file
248 if ($format == 'ical' || $format == 'vcal') {
249  $result = $agenda->build_exportfile($format, $type, $cachedelay, $filename, $filters, $exportholidays);
250  if ($result >= 0) {
251  $attachment = true;
252  if (isset($_GET["attachment"])) {
253  $attachment = $_GET["attachment"];
254  }
255  //$attachment = false;
256  $contenttype = 'text/calendar';
257  if (isset($_GET["contenttype"])) {
258  $contenttype = $_GET["contenttype"];
259  }
260  //$contenttype='text/plain';
261  $outputencoding = 'UTF-8';
262 
263  if ($contenttype) {
264  header('Content-Type: '.$contenttype.($outputencoding ? '; charset='.$outputencoding : ''));
265  }
266  if ($attachment) {
267  header('Content-Disposition: attachment; filename="'.$shortfilename.'"');
268  }
269 
270  if ($cachedelay) {
271  header('Cache-Control: max-age='.$cachedelay.', private, must-revalidate');
272  } else {
273  header('Cache-Control: private, must-revalidate');
274  }
275 
276  // Clean parameters
277  $outputfile = $conf->agenda->dir_temp.'/'.$filename;
278  $result = readfile($outputfile);
279  if (!$result) {
280  print 'File '.$outputfile.' was empty.';
281  }
282 
283  //header("Location: ".DOL_URL_ROOT.'/document.php?modulepart=agenda&file='.urlencode($filename));
284  exit;
285  } else {
286  print 'Error '.$agenda->error;
287 
288  exit;
289  }
290 }
291 
292 if ($format == 'rss') {
293  $result = $agenda->build_exportfile($format, $type, $cachedelay, $filename, $filters, $exportholidays);
294  if ($result >= 0) {
295  $attachment = false;
296  if (isset($_GET["attachment"])) {
297  $attachment = $_GET["attachment"];
298  }
299  //$attachment = false;
300  $contenttype = 'application/rss+xml';
301  if (isset($_GET["contenttype"])) {
302  $contenttype = $_GET["contenttype"];
303  }
304  //$contenttype='text/plain';
305  $outputencoding = 'UTF-8';
306 
307  if ($contenttype) {
308  header('Content-Type: '.$contenttype.($outputencoding ? '; charset='.$outputencoding : ''));
309  }
310  if ($attachment) {
311  header('Content-Disposition: attachment; filename="'.$filename.'"');
312  }
313 
314  // Ajout directives pour resoudre bug IE
315  //header('Cache-Control: Public, must-revalidate');
316  //header('Pragma: public');
317  if ($cachedelay) {
318  header('Cache-Control: max-age='.$cachedelay.', private, must-revalidate');
319  } else {
320  header('Cache-Control: private, must-revalidate');
321  }
322 
323  // Clean parameters
324  $outputfile = $conf->agenda->dir_temp.'/'.$filename;
325  $result = readfile($outputfile);
326  if (!$result) {
327  print 'File '.$outputfile.' was empty.';
328  }
329 
330  // header("Location: ".DOL_URL_ROOT.'/document.php?modulepart=agenda&file='.urlencode($filename));
331  exit;
332  } else {
333  print 'Error '.$agenda->error;
334 
335  exit;
336  }
337 }
338 
339 
341 print '<div class="error">'.$agenda->error.'</div>';
llxFooterVierge()
Footer function.
if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) if(!defined('NOLOGIN')) if(!defined('NOCSRFCHECK')) if(!defined('NOIPCHECK')) llxHeaderVierge()
Header function.
Class to manage agenda events (actions)
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
httponly_accessforbidden($message=1, $http_response_code=403, $stringalreadysanitized=0)
Show a message to say access is forbidden and stop program.