1<?php
2# require php 5
3$php_started = getmicrotime();
4# define BASE
5if (!defined('BASE')) define('BASE', './');
6if (phpversion() < '5.1'){
7	die (phpversion()." detected. php 5.1 or higher required for this version.\n\n" );
8}
9include_once(BASE.'functions/init/sanitize.php');
10include_once(BASE.'functions/init/set_error_reporting.php');
11include_once(BASE.'functions/init/configs.php');
12include_once(BASE.'functions/init/cpaths.php');
13include_once(BASE.'functions/init/date_range.php');
14include_once(BASE.'error.php');
15include_once(BASE.'functions/calendar_functions.php');
16include_once(BASE.'functions/userauth_functions.php');
17
18// Grab the action (login or logout).
19$action = '';
20if (isset($_GET['action']))			$action = $_GET['action'];
21else if (isset($_POST['action']))	$action = $_POST['action'];
22
23// Login and/or logout.
24list($username, $password, $invalid_login) = user_login();
25if ($action != 'login') $invalid_login = false;
26if ($action == 'logout' || $invalid_login) {
27	list($username, $password) = user_logout();
28}
29
30if (ini_get('max_execution_time') < 60) {
31	@ini_set('max_execution_time', '60');
32}
33
34// Pull the calendars off the GET line if provided. The $cal_filename
35// is always an array, because this makes it easier to deal with below.
36$cal_filenames = array();
37if (isset($_GET['cal']) && $_GET['cal'] !='') {
38	// If the cal value is not an array, split it into an array on
39	// commas.
40	if (!is_array($_GET['cal']))
41		$_GET['cal'] = explode(',', $_GET['cal']);
42
43	// Grab the calendar filenames off the cal value array.
44	$cal_filenames = $_GET['cal'];
45} elseif ($phpiCal_config->default_cal != '') {
46	$cal_filenames = explode(',',$phpiCal_config->default_cal);
47} else {
48	$cal_filenames[0] = $phpiCal_config->ALL_CALENDARS_COMBINED;
49}
50
51$all_calendars_combined = availableCalendars($username, $password, $phpiCal_config->ALL_CALENDARS_COMBINED);
52
53//load cal_filenames if $ALL_CALENDARS_COMBINED
54if ($cal_filenames[0] == $phpiCal_config->ALL_CALENDARS_COMBINED){
55	$cal_filenames = $all_calendars_combined;
56}
57// Separate the calendar identifiers into web calendars and local
58// calendars.
59$web_cals = array();
60$local_cals = array();
61foreach ($cal_filenames as $cal_filename) {
62	# substitute for md5-obscured list_webcals
63	foreach ($list_webcals as $tmp_cal){
64		if($cal_filename == md5($phpiCal_config->salt.$tmp_cal)) $cal_filename = $tmp_cal;
65	}
66	// If the calendar identifier begins with a web protocol, this is a web
67	// calendar.
68	$cal_filename = urldecode($cal_filename); #need to decode for substr statements to identify webcals
69	$cal_filename = str_replace(' ','%20', $cal_filename); #need to reencode blank spaces for matching with $list_webcals
70	if (substr($cal_filename, 0, 7) == 'http://' ||
71		substr($cal_filename, 0, 8) == 'https://' ||
72		substr($cal_filename, 0, 9) == 'webcal://')
73	{
74		#jump sends cal url without .ics extension.  Add it if needed.
75	#	if (substr($cal_filename, -4) != ".ics") $cal_filename .= ".ics";
76		$web_cals[] = $cal_filename;
77	}
78
79	// Otherwise it is a local calendar.
80	else {
81		// Check blacklisted.
82		if (in_array($cal_filename, $blacklisted_cals)  && $cal_filename !='') {
83			exit(error($lang['l_error_restrictedcal'], $cal_filename));
84		}
85		$local_cals[] = urldecode(str_replace(".ics", '', basename($cal_filename)));
86	}
87}
88
89// We will build the calendar display name as we go. The final display
90// name will get constructed at the end.
91$cal_displaynames = array();
92
93// This is our list of final calendars.
94$cal_filelist = array();
95
96// This is our list of URL encoded calendars.
97$cals = array();
98
99// Process the web calendars.
100foreach ($web_cals as $web_cal) {
101	// Make some protocol alternatives, and set our real identifier to the
102	// HTTP protocol.
103	$cal_webcalPrefix = str_replace(array('http://', 'https://'), 'webcal://', $web_cal);
104	$cal_httpPrefix = str_replace(array('webcal://', 'https://'), 'http://', $web_cal);
105	$cal_httpsPrefix = str_replace(array('http://', 'webcal://'), 'https://', $web_cal);
106
107	// We can only include this web calendar if we allow all web calendars
108	// (as defined by $allow_webcals) or if the web calendar shows up in the
109	// list of web calendars defined in config.inc.php.
110	if ($phpiCal_config->allow_webcals != 'yes' &&
111		!in_array($cal_webcalPrefix, $list_webcals) &&
112		!in_array($cal_httpPrefix, $list_webcals) &&
113		!in_array($cal_httpsPrefix, $list_webcals))
114	{
115		exit(error($lang['l_error_remotecal'], $web_cal));
116	}
117
118	// Pull the display name off the URL.
119#	$cal_displaynames[] = substr(str_replace('32', ' ', basename($web_cal)), 0, -4);
120	$cal_displaynames[] = substr(basename($web_cal), 0, -4);
121
122	if(in_array($web_cal, $list_webcals)){
123		$web_cal = md5($phpiCal_config->salt.$web_cal);
124	}
125	$cals[] = urlencode($web_cal);
126	$subscribe_path = $cal_webcalPrefix;
127
128	// Add the webcal to the available calendars.
129	$cal_filelist[] = $cal_httpPrefix;
130}
131
132// Process the local calendars.
133if (count($local_cals) > 0) {
134	$local_cals = availableCalendars($username, $password, $local_cals);
135	foreach ($local_cals as $local_cal) {
136		$cal_displaynames[] = str_replace('32', ' ', getCalendarName($local_cal));
137	}
138	$cal_filelist = array_merge($cal_filelist, $local_cals);
139	$cals = array_merge($cals, array_map("urlencode", array_map("getCalendarName", $local_cals)));
140
141	// Set the download and subscribe paths from the config, if there is
142	// only one calendar being displayed and those paths are defined.
143	if (count($local_cals) == 1) {
144		$filename = $local_cals[0];
145		$add_cpath = '';
146		if (isset($cpath) && $cpath !='') $add_cpath = "$cpath/";
147
148		if (($phpiCal_config->download_uri == '') && (preg_match('/(^\/|\.\.\/)/', $filename) == 0)) {
149			$subscribe_path = 'webcal://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']).'/'.$filename;
150			$download_filename = $filename;
151		} elseif ($phpiCal_config->download_uri != '') {
152			$newurl = eregi_replace("^(http://)", "", $phpiCal_config->download_uri);
153				$subscribe_path = 'webcal://'.$newurl.'/'.$add_cpath.basename($filename);
154				$download_filename = $phpiCal_config->download_uri.'/'.$add_cpath.basename($filename);
155		} else {
156			$subscribe_path = $add_cpath;
157			$download_filename = $add_cpath;
158		}
159	}
160}
161
162// We should only allow a download filename and subscribe path if there is
163// only one calendar being displayed.
164if (count($cal_filelist) > 1) {
165	$subscribe_path = '';
166	$download_filename = '';
167}
168
169// Build the final cal list. This is a comma separated list of the
170// url-encoded calendar names and web calendar URLs.
171$cal = implode(',', $cals);
172
173// Build the final display name used for template substitution.
174asort($cal_displaynames);
175$cal_displayname = implode(', ', $cal_displaynames);
176
177$rss_powered = ($phpiCal_config->enable_rss == 'yes') ? 'yes' : '';
178
179function getmicrotime() {
180	list($usec, $sec) = explode(' ',microtime());
181	return ((float)$usec + (float)$sec);
182}
183
184$uid_list = array();
185#uncomment for diagnostics
186#echo "after init.inc.ics<pre>";
187#echo "cals";print_r($cals);echo"\n\n";
188#echo "cal_filenames";print_r($cal_filenames);echo"\n\n";
189#echo "web_cals";print_r($web_cals);echo"\n\n";
190#echo "local_cals";print_r($local_cals);echo"\n\n";
191#echo "cal_filelist";print_r($cal_filelist);
192#echo "cal_displaynames";print_r($cal_displaynames);
193#echo "</pre><hr>";
194
195?>