Skip to content

Commit 2983cba

Browse files
author
Jason Oster
committed
Additional performance improvement: Don't re-parse ICS files to get the calendar name
1 parent 88abd08 commit 2983cba

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

functions/calendar_functions.php

+37-15
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function availableCalendars($username, $password, $cal_filename, $admin = false)
8585
// The file process block below expects actual filenames. So
8686
// we have to append '.ics' to the passed in calendar names.
8787
foreach ($cal_filename_local as $filename) {
88-
array_push($files, "$search_path/$filename".".ics");
88+
array_push($files, "$search_path/$filename.ics");
8989
}
9090
}
9191

@@ -175,7 +175,13 @@ function getCalendarName($cal_path) {
175175
}
176176

177177
// At this point, just pull the name off the file.
178-
return str_replace(".ics", '', basename($cal_path));
178+
$name = str_replace(".ics", '', basename($cal_path));
179+
if ((substr($cal_path, 0, 7) == 'http://') ||
180+
(substr($cal_path, 0, 8) == 'https://') ||
181+
(substr($cal_path, 0, 9) == 'webcal://')) {
182+
$name = urldecode($name);
183+
}
184+
return $name;
179185
}
180186

181187
// This function prints out the calendars available to the user, for
@@ -184,7 +190,7 @@ function getCalendarName($cal_path) {
184190
//
185191
// $cals = The calendars (entire path, e.g. from availableCalendars).
186192
function display_ical_list($cals, $pick=FALSE) {
187-
global $cal, $current_view, $getdate, $lang, $calendar_lang, $all_cal_comb_lang, $cal_filelist, $cal_displaynames, $list_webcals, $phpiCal_config;
193+
global $cal, $current_view, $getdate, $lang, $calendar_lang, $all_cal_comb_lang, $cal_filelist, $cal_displaynames, $list_webcals, $phpiCal_config, $master_array;
188194
// Print each calendar option.
189195
$return = '';
190196

@@ -194,18 +200,28 @@ function display_ical_list($cals, $pick=FALSE) {
194200
foreach ($cals as $cal_tmp) {
195201
// Format the calendar path for display.
196202
//
197-
// Only display the calendar name, replace all instances of "32" with " ",
198-
// and remove the .ics suffix.
199-
$cal_displayname_tmp = getCalendarName($cal_tmp);
200-
#$cal_displayname_tmp = str_replace("32", " ", $cal_displayname_tmp);
201-
#overwrite the display name if we already have a real name
202-
if (is_numeric(array_search($cal_tmp, $cal_filelist))){
203-
$cal_displayname_tmp = $cal_displaynames[array_search($cal_tmp,$cal_filelist)];
204-
}else{
203+
$cal_displayname_tmp = '';
204+
$search_idx = array_search($cal_tmp, $cal_filelist);
205+
if (is_numeric($search_idx)) {
206+
$cal_displayname_tmp = $cal_displaynames[$search_idx];
207+
}
208+
else if ((substr($cal_tmp, 0, 7) == 'http://') ||
209+
(substr($cal_tmp, 0, 8) == 'https://') ||
210+
(substr($cal_tmp, 0, 9) == 'webcal://')) {
211+
$cal_tmp2 = str_replace('webcal://', 'http://', $cal_tmp);
212+
$cal_tmp2 = str_replace('https://', 'http://', $cal_tmp2);
213+
$num = 1;
214+
foreach ($master_array[-4] as $master_cal) {
215+
if ($master_cal['filename'] == $cal_tmp2) {
216+
$cal_displayname_tmp = $master_array[-3][$num];
217+
break;
218+
}
219+
$num++;
220+
}
221+
}
222+
else {
205223
# pull the name from the $cal_tmp file
206-
$cal_tmp2 = str_replace('webcal://','http://',$cal_tmp);
207-
208-
$ifile = @fopen($cal_tmp2, "r");
224+
$ifile = @fopen($cal_tmp, 'r');
209225
if ($ifile == FALSE) exit(error($lang['l_error_cantopen'], $cal_tmp));
210226
while (!feof($ifile)) {
211227
$line = fgets($ifile, 1024);
@@ -225,7 +241,13 @@ function display_ical_list($cals, $pick=FALSE) {
225241
#stop reading if we find an event or timezone before there's a name
226242
if ($line == "BEGIN:VTIMEZONE" ||$line == "BEGIN:VEVENT") break;
227243
}
228-
244+
@fclose($ifile);
245+
}
246+
if (empty($cal_displayname_tmp)) {
247+
// Only display the calendar name, replace all instances of "32" with " ",
248+
// and remove the .ics suffix.
249+
$cal_displayname_tmp = getCalendarName($cal_tmp);
250+
#$cal_displayname_tmp = str_replace('32', ' ', $cal_displayname_tmp);
229251
}
230252

231253
// If this is a webcal, add 'Webcal' to the display name.

0 commit comments

Comments
 (0)