dolibarr  13.0.2
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')) define('NOTOKENRENEWAL', '1');
32 if (!defined('NOREQUIREMENU')) define('NOREQUIREMENU', '1'); // If there is no menu to show
33 if (!defined('NOREQUIREHTML')) define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
34 if (!defined('NOREQUIREAJAX')) define('NOREQUIREAJAX', '1');
35 if (!defined('NOLOGIN')) define("NOLOGIN", 1); // This means this output page does not require to be logged.
36 if (!defined('NOCSRFCHECK')) define("NOCSRFCHECK", 1); // We accept to go on this page from external web site.
37 if (!defined('NOIPCHECK')) define('NOIPCHECK', '1'); // Do not check IP defined into conf $dolibarr_main_restrict_ip
38 
39 
40 // It's a wrapper, so empty header
41 
47 function llxHeaderVierge()
48 {
49  print '<html><title>Export agenda cal</title><body>';
50 }
56 function llxFooterVierge()
57 {
58  print '</body></html>';
59 }
60 
61 // For MultiCompany module.
62 // Do not use GETPOST here, function is not defined and define must be done before including main.inc.php
63 $entity = (!empty($_GET['entity']) ? (int) $_GET['entity'] : (!empty($_POST['entity']) ? (int) $_POST['entity'] : 1));
64 if (is_numeric($entity)) define("DOLENTITY", $entity);
65 
66 require '../../main.inc.php';
67 require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
68 
69 // Security check
70 if (empty($conf->agenda->enabled)) accessforbidden('', 0, 0, 1);
71 
72 // Not older than
73 if (!isset($conf->global->MAIN_AGENDA_EXPORT_PAST_DELAY)) $conf->global->MAIN_AGENDA_EXPORT_PAST_DELAY = 100; // default limit
74 
75 // Define format, type and filter
76 $format = 'ical';
77 $type = 'event';
78 if (GETPOST("format", 'alpha')) $format = GETPOST("format", 'apha');
79 if (GETPOST("type", 'apha')) $type = GETPOST("type", 'alpha');
80 
81 $filters = array();
82 if (GETPOST("year", 'int')) $filters['year'] = GETPOST("year", 'int');
83 if (GETPOST("id", 'int')) $filters['id'] = GETPOST("id", 'int');
84 if (GETPOST("idfrom", 'int')) $filters['idfrom'] = GETPOST("idfrom", 'int');
85 if (GETPOST("idto", 'int')) $filters['idto'] = GETPOST("idto", 'int');
86 if (GETPOST("project", 'apha')) $filters['project'] = GETPOST("project", 'apha');
87 if (GETPOST("logina", 'apha')) $filters['logina'] = GETPOST("logina", 'apha');
88 if (GETPOST("logint", 'apha')) $filters['logint'] = GETPOST("logint", 'apha');
89 if (GETPOST("notactiontype", 'apha')) $filters['notactiontype'] = GETPOST("notactiontype", 'apha');
90 if (GETPOST("actiontype", 'apha')) $filters['actiontype'] = GETPOST("actiontype", 'apha');
91 if (GETPOST("notolderthan", 'int')) $filters['notolderthan'] = GETPOST("notolderthan", "int");
92 else $filters['notolderthan'] = $conf->global->MAIN_AGENDA_EXPORT_PAST_DELAY;
93 
94 // Check config
95 if (empty($conf->global->MAIN_AGENDA_XCAL_EXPORTKEY))
96 {
97  $user->getrights();
98 
100  print '<div class="error">Module Agenda was not configured properly.</div>';
101  llxFooterVierge();
102  exit;
103 }
104 
105 // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array of hooks
106 $hookmanager->initHooks(array('agendaexport'));
107 
108 $reshook = $hookmanager->executeHooks('doActions', $filters); // Note that $action and $object may have been modified by some
109 if ($reshook < 0) {
110  llxHeaderVierge();
111  if (!empty($hookmanager->errors) && is_array($hookmanager->errors)) {
112  print '<div class="error">'.implode('<br/>', $hookmanager->errors).'</div>';
113  } else {
114  print '<div class="error">'.$hookmanager->error.'</div>';
115  }
116  llxFooterVierge();
117 } elseif (empty($reshook)) {
118  // Check exportkey
119  if (empty($_GET["exportkey"]) || $conf->global->MAIN_AGENDA_XCAL_EXPORTKEY != $_GET["exportkey"]) {
120  $user->getrights();
121 
122  llxHeaderVierge();
123  print '<div class="error">Bad value for key.</div>';
124  llxFooterVierge();
125  exit;
126  }
127 }
128 
129 
130 // Define filename with prefix on filters predica (each predica set must have on cache file)
131 $shortfilename = 'dolibarrcalendar';
132 $filename = $shortfilename;
133 // Complete long filename
134 foreach ($filters as $key => $value)
135 {
136  //if ($key == 'notolderthan') $filename.='-notolderthan'.$value; This filter key is already added before and does not need to be in filename
137  if ($key == 'year') $filename .= '-year'.$value;
138  if ($key == 'id') $filename .= '-id'.$value;
139  if ($key == 'idfrom') $filename .= '-idfrom'.$value;
140  if ($key == 'idto') $filename .= '-idto'.$value;
141  if ($key == 'project') $filename .= '-project'.$value;
142  if ($key == 'logina') $filename .= '-logina'.$value; // Author
143  if ($key == 'logint') $filename .= '-logint'.$value; // Assigned to
144  if ($key == 'notactiontype') $filename .= '-notactiontype'.$value;
145 }
146 // Add extension
147 if ($format == 'vcal') { $shortfilename .= '.vcs'; $filename .= '.vcs'; }
148 if ($format == 'ical') { $shortfilename .= '.ics'; $filename .= '.ics'; }
149 if ($format == 'rss') { $shortfilename .= '.rss'; $filename .= '.rss'; }
150 
151 if ($shortfilename == 'dolibarrcalendar')
152 {
153  $langs->load("main");
154  $langs->load("errors");
155  llxHeaderVierge();
156  print '<div class="error">'.$langs->trans("ErrorWrongValueForParameterX", 'format').'</div>';
157  llxFooterVierge();
158  exit;
159 }
160 
161 $agenda = new ActionComm($db);
162 
163 $cachedelay = 0;
164 if (!empty($conf->global->MAIN_AGENDA_EXPORT_CACHE)) $cachedelay = $conf->global->MAIN_AGENDA_EXPORT_CACHE;
165 
166 $exportholidays = GETPOST('includeholidays', 'int');
167 
168 // Build file
169 if ($format == 'ical' || $format == 'vcal')
170 {
171  $result = $agenda->build_exportfile($format, $type, $cachedelay, $filename, $filters, $exportholidays);
172  if ($result >= 0)
173  {
174  $attachment = true;
175  if (isset($_GET["attachment"])) $attachment = $_GET["attachment"];
176  //$attachment = false;
177  $contenttype = 'text/calendar';
178  if (isset($_GET["contenttype"])) $contenttype = $_GET["contenttype"];
179  //$contenttype='text/plain';
180  $outputencoding = 'UTF-8';
181 
182  if ($contenttype) header('Content-Type: '.$contenttype.($outputencoding ? '; charset='.$outputencoding : ''));
183  if ($attachment) header('Content-Disposition: attachment; filename="'.$shortfilename.'"');
184 
185  if ($cachedelay) header('Cache-Control: max-age='.$cachedelay.', private, must-revalidate');
186  else header('Cache-Control: private, must-revalidate');
187 
188  // Clean parameters
189  $outputfile = $conf->agenda->dir_temp.'/'.$filename;
190  $result = readfile($outputfile);
191  if (!$result) print 'File '.$outputfile.' was empty.';
192 
193  //header("Location: ".DOL_URL_ROOT.'/document.php?modulepart=agenda&file='.urlencode($filename));
194  exit;
195  } else {
196  print 'Error '.$agenda->error;
197 
198  exit;
199  }
200 }
201 
202 if ($format == 'rss')
203 {
204  $result = $agenda->build_exportfile($format, $type, $cachedelay, $filename, $filters, $exportholidays);
205  if ($result >= 0)
206  {
207  $attachment = false;
208  if (isset($_GET["attachment"])) $attachment = $_GET["attachment"];
209  //$attachment = false;
210  $contenttype = 'application/rss+xml';
211  if (isset($_GET["contenttype"])) $contenttype = $_GET["contenttype"];
212  //$contenttype='text/plain';
213  $outputencoding = 'UTF-8';
214 
215  if ($contenttype) header('Content-Type: '.$contenttype.($outputencoding ? '; charset='.$outputencoding : ''));
216  if ($attachment) header('Content-Disposition: attachment; filename="'.$filename.'"');
217 
218  // Ajout directives pour resoudre bug IE
219  //header('Cache-Control: Public, must-revalidate');
220  //header('Pragma: public');
221  if ($cachedelay) header('Cache-Control: max-age='.$cachedelay.', private, must-revalidate');
222  else header('Cache-Control: private, must-revalidate');
223 
224  // Clean parameters
225  $outputfile = $conf->agenda->dir_temp.'/'.$filename;
226  $result = readfile($outputfile);
227  if (!$result) print 'File '.$outputfile.' was empty.';
228 
229  // header("Location: ".DOL_URL_ROOT.'/document.php?modulepart=agenda&file='.urlencode($filename));
230  exit;
231  } else {
232  print 'Error '.$agenda->error;
233 
234  exit;
235  }
236 }
237 
238 
240 print '<div class="error">'.$agenda->error.'</div>';
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Class to manage agenda events (actions)
accessforbidden($message= '', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
llxHeaderVierge()
Header function.
print
Draft customers invoices.
Definition: index.php:89
llxFooterVierge()
Footer function.
if(!defined('CSRFCHECK_WITH_TOKEN')) define('CSRFCHECK_WITH_TOKEN'
Draft customers invoices.