aboutsummaryrefslogtreecommitdiffstats
path: root/functions/calendar_functions.php
blob: f4e7a8177be237487ac78c017705564b591557b3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php

// This function returns a list of all calendars that the current user
// has access to. Basically, all local calendars found in the calendar
// directory, plus any webcals listed in the configuration file, but
// excluding blacklisted calendars and locked calendars which the user,
// if logged in, does not have access to.
//
// $username		= The username. Empty if no username provided.
// $password		= The password. Empty if no password provided.
// $cal_filename	= The calendar name without .ics.
// $admin			= True if this is an administrative request, in
//					  which case all local calendars only will be
//					  returned.
function availableCalendars($username, $password, $cal_filename, $admin = false) {
	// Import globals.
	global $allow_webcals, $allow_login, $calendar_path, $recursive_path, $support_ical, $locked_map, $apache_map, $lang, $_SERVER, $phpiCal_config;

	// Create the list of available calendars.
	$calendars = array();

	// Grab any HTTP authentication.
	unset($http_user);
	if ((isset($_SERVER['PHP_AUTH_USER'])) && ($allow_login == 'yes')) {
		$http_user = $_SERVER['PHP_AUTH_USER'];
	}

	// Grab the list of unlocked calendars.
	$unlocked_cals = array();
	if (isset($locked_map["$username:$password"])) {
		$unlocked_cals = $locked_map["$username:$password"];
	}
	
	// Make a local copy of the requested calendars.
	if (!is_array($cal_filename))
		$cal_filename_local = array($cal_filename);
	else
		$cal_filename_local = $cal_filename;

	// Create the list of available calendars.
	$calendars = array();
	
	// This array keeps track of paths we need to search.
	$search_paths = array($phpiCal_config->calendar_path);
		
	// Add web calendars.
	if ($cal_filename_local[0] == $phpiCal_config->ALL_CALENDARS_COMBINED || $admin)	{
		if (!isset($http_user) && !$admin) {
			foreach ($phpiCal_config->list_webcals as $file) {
				// Make sure the URL ends with .ics.
				if (!preg_match("/.ics$/i", $file)) continue;
				
				// Add this calendar.
				array_push($calendars, $file);
			}
		}
	}
	
	// Set some booleans that will dictate our search.
	$find_all = ($cal_filename_local[0] == $phpiCal_config->ALL_CALENDARS_COMBINED || $admin);
	
	// Process all search paths.
	while (!empty($search_paths)) {
		// Read the next search path.
		$search_path = array_pop($search_paths);
			
		// This array keeps track of filenames we need to look at.
		$files = array();
		
		// Build the list of files we need to check.
		//
		// We do a full directory search if we are supposed to find all
		// calendars, the calendar we're looking for may be in a
		// subdirectory, or we are supporting the iCal repository format.
		// The latter is necessary because the calendar name cannot be
		// used to identify the calendar filename.
		if ($find_all || $recursive_path == 'yes' || $support_ical == 'yes') {
			// Open the directory.
			$dir_handle = @opendir($search_path)
				or die(error(sprintf($lang['l_error_path'], $search_path), implode(',', $cal_filename)));
			if ($dir_handle === false)
				die(error(sprintf($lang['l_error_path'], $search_path), implode(',', $cal_filename)));
				
			// Add each file in the directory that does not begin with a dot.
			while (false !== ($file = readdir($dir_handle))) {
				// Make sure this is not a dot file.
				if (preg_match("/^\./", $file)) continue;
				array_push($files, "$search_path/$file");
			}
		} else {
			// The file process block below expects actual filenames. So
			// we have to append '.ics' to the passed in calendar names.
			foreach ($cal_filename_local as $filename) {
				array_push($files, "$search_path/$filename".".ics");
			}
		}
		
		// Process files.
		foreach ($files as $file) {
			// Push directories onto the search paths if recursive paths is
			// turned on.
			if (is_dir($file)) {
				if ($recursive_path == 'yes') {
					array_push($search_paths, $file);
				}
				continue;
			}
			
			// Make sure the file is real.
			if (!is_file($file)) continue;
			
			// Make sure the file ends in .ics.
			if (!preg_match("/^.*\.ics$/i", $file)) continue;
			
			// Make sure this is not a blacklisted calendar.
			$cal_name = getCalendarName($file);
			if (in_array($cal_name, $phpiCal_config->blacklisted_cals)) continue;
			
			// If HTTP authenticated, make sure this calendar is available
			// to the user.
			if (isset($http_user)) {
				if (!in_array($cal_name, $phpiCal_config->apache_map[$http_user])) continue;
			}
		
			// Make sure this calendar is not locked.
			if (!$admin &&
				in_array($cal_name, $phpiCal_config->locked_cals) &&
				!in_array($cal_name, $phpiCal_config->unlocked_cals))
			{
				continue;
			}
			
			// Add this calendar if we're looking for it, and remove it's name
			// from the local list because we've found it.
			if ($find_all || in_array($cal_name, $cal_filename_local)) {
				array_push($calendars, $file);
				$cal_filename_local = array_diff($cal_filename_local, array($cal_name));
				
				// If the local list is empty, we're done.
				if (empty($cal_filename_local))
					break 2;
			}
		}
	}

	// Return the sorted calendar list.
	natcasesort($calendars);
	return $calendars;
}

// This function returns the result of the availableCalendars function
// but only includes the calendar names.
//
// $username		= The username. Empty if no username provided.
// $password		= The password. Empty if no password provided.
// $cal_filename	= The calendar name without .ics.
// $admin			= True if this is an administrative request, in
//					  which case all local calendars only will be
//					  returned.
function availableCalendarNames($username, $password, $cal_filename, $admin = false) {
	// Grab the available calendar paths.
	$calendars = availableCalendars($username, $password, $cal_filename, $admin);

	// Strip the paths off the calendars.
	foreach (array_keys($calendars) as $key) {
		$calendars[$key] = getCalendarName($key);
	}
	
	// Return the sorted calendar names.
	natcasesort($calendars);
	return $calendars;
}

// This function returns the calendar name for the specified calendar
// path.
//
// $cal_path	= The path to the calendar file.
function getCalendarName($cal_path) {
	global $support_ical;
		
	// If iCal is supported, check the directory for an Info.plist.
	if ($support_ical == 'yes') {
		// Look for the Info.plist file.
		$plist_filename = dirname($cal_path)."/Info.plist";
		if (is_file($plist_filename)) {
			// Read the Info.plist.
			$handle = fopen($plist_filename, 'r');
			$contents = fread($handle, filesize($plist_filename));
			fclose($handle);
						
			// Pull out the calendar name.
			$num_matches = preg_match("/<key>Title<\/key>\s*?<string>(.+?)<\/string>/i", $contents, $matches);
			if ($num_matches > 0)
				return $matches[1];
		}
	}
	
	// At this point, just pull the name off the file.
	return str_replace(".ics", '', basename($cal_path));
}

// This function prints out the calendars available to the user, for
// selection. Should be enclosed within a <select>...</select>, which
// is not printed out by this function.
//
// $cals	= The calendars (entire path, e.g. from availableCalendars).
function display_ical_list($cals, $pick=FALSE) {
	global $cal, $current_view, $getdate, $calendar_lang, $all_cal_comb_lang, $cal_filelist, $cal_displaynames, $phpiCal_config;
	// Print each calendar option.
	$return = '';
	foreach ($cals as $cal_tmp) {
		// Format the calendar path for display.
		//
		// Only display the calendar name, replace all instances of "32" with " ",
		// and remove the .ics suffix.
		$cal_displayname_tmp = getCalendarName($cal_tmp);
		$cal_displayname_tmp = str_replace("32", " ", $cal_displayname_tmp);
		#overwrite the display name if we already have a real name
		if (is_numeric(array_search($cal_tmp, $cal_filelist))){
			$cal_displayname_tmp = $cal_displaynames[array_search($cal_tmp,$cal_filelist)];
		}else{
			# pull the name from the $cal_tmp file
			$cal_tmp = str_replace('webcal://','http://',$cal_tmp);

			$ifile = @fopen($cal_tmp, "r");
			if ($ifile == FALSE) exit(error($lang['l_error_cantopen'], $cal_tmp));
			while (!feof($ifile)) {
				$line = fgets($ifile, 1024);
				$line = trim($line);
				if (ereg ("([^:]+):(.*)", $line, $regs)){
					$field = $regs[1];
					$data = $regs[2];
					$property = $field;
					$prop_pos = strpos($property,';');
					if ($prop_pos !== false) $property = substr($property,0,$prop_pos);
					$property = strtoupper($property);
					if ($property == "X-WR-CALNAME"){
						$cal_displayname_tmp = $data;
						break;
					}
				}	
				#stop reading if we find an event or timezone before there's a name
				if ($line == "BEGIN:VTIMEZONE" ||$line == "BEGIN:VEVENT") break;
			}

		}

		// If this is a webcal, add 'Webcal' to the display name.
		if (preg_match("/^(https?|webcal):\/\//i", $cal_tmp)) {
			$cal_displayname_tmp .= " Webcal";
		}

		// Otherwise, remove all the path information, since that should
		// not be used to identify local calendars. Also add the calendar
		// label to the display name.
		else {
			// Strip path and .ics suffix.
			$cal_tmp = getCalendarName($cal_tmp);

			// Add calendar label.
			$cal_displayname_tmp .= " $calendar_lang";
		}

		// Encode the calendar path.
		$cal_encoded_tmp = urlencode($cal_tmp);

		// Display the option.
		//
		// The submitted calendar will be encoded, and always use http://
		// if it is a webcal. So that is how we perform the comparison when
		// trying to figure out if this is the selected calendar.
		if($pick) {
			if (in_array($cal_encoded_tmp, explode(",", $cal)) || count($cals) == count(explode(",", $cal))) {
					$return .= "<option value=\"$cal_encoded_tmp\" selected=\"selected\">$cal_displayname_tmp</option>\n";
			} else {
					$return .= "<option value=\"$cal_encoded_tmp\">$cal_displayname_tmp</option>\n";	
			}
		} else {
		$cal_httpPrefix_tmp = str_replace('webcal://', 'http://', $cal_tmp);
		if ($cal_httpPrefix_tmp == urldecode($cal)) {
			$return .= "<option value=\"$current_view.php?cal=$cal_encoded_tmp&amp;getdate=$getdate\" selected=\"selected\">$cal_displayname_tmp</option>";
		} else {
			$return .= "<option value=\"$current_view.php?cal=$cal_encoded_tmp&amp;getdate=$getdate\">$cal_displayname_tmp</option>";	
		}
	 }			
	}			

	// option to open all (non-web) calenders together
	if (!$pick) {
		if ($cal == $phpiCal_config->ALL_CALENDARS_COMBINED) {
			$return .=  "<option value=\"$current_view.php?cal=$ALL_CALENDARS_COMBINED&amp;getdate=$getdate\" selected=\"selected\">$all_cal_comb_lang</option>";
		} else {
			$return .=  "<option value=\"$current_view.php?cal=$ALL_CALENDARS_COMBINED&amp;getdate=$getdate\">$all_cal_comb_lang</option>";
		}
	}
	return $return;
}

© 2014-2024 Faster IT GmbH | imprint | privacy policy