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