From 8039a2f6e5a1050bbeeddb746cd6515cf268da51 Mon Sep 17 00:00:00 2001 From: Thomas Bruederli Date: Thu, 17 Oct 2013 14:32:50 +0200 Subject: [PATCH] Use new ical parser iterator to import large files and avoid htp connection timeouts --- plugins/calendar/calendar.php | 23 ++++++++++++----------- plugins/calendar/calendar_ui.js | 7 ++++++- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/plugins/calendar/calendar.php b/plugins/calendar/calendar.php index de4645ef..a31bec7b 100644 --- a/plugins/calendar/calendar.php +++ b/plugins/calendar/calendar.php @@ -968,7 +968,7 @@ class calendar extends rcube_plugin rcube_upload_progress(); } - $calendar = get_input_value('calendar', RCUBE_INPUT_GPC); + @set_time_limit(0); // process uploaded file if there is no error $err = $_FILES['_data']['error']; @@ -976,22 +976,23 @@ class calendar extends rcube_plugin if (!$err && $_FILES['_data']['tmp_name']) { $calendar = get_input_value('calendar', RCUBE_INPUT_GPC); $rangestart = $_REQUEST['_range'] ? date_create("now -" . intval($_REQUEST['_range']) . " months") : 0; - $count = $errors = 0; + $user_email = $this->rc->user->get_username(); - try { - $events = $this->get_ical()->import_from_file($_FILES['_data']['tmp_name'], 'UTF-8', true); - } - catch (Exception $e) { - $errors = 1; - $msg = $e->getMessage(); - $events = array(); - } + $ical = $this->get_ical(); + $errors = !$ical->fopen($_FILES['_data']['tmp_name']); + $count = $i = 0; + foreach ($ical as $event) { + // keep the browser connection alive on long import jobs + if (++$i > 100 && $i % 100 == 0) { + echo ""; + ob_flush(); + } - foreach ($events as $event) { // TODO: correctly handle recurring events which start before $rangestart if ($event['end'] < $rangestart && (!$event['recurrence'] || ($event['recurrence']['until'] && $event['recurrence']['until'] < $rangestart))) continue; + $event['_owner'] = $user_email; $event['calendar'] = $calendar; if ($this->driver->new_event($event)) { $count++; diff --git a/plugins/calendar/calendar_ui.js b/plugins/calendar/calendar_ui.js index d6272535..21aedc1d 100644 --- a/plugins/calendar/calendar_ui.js +++ b/plugins/calendar/calendar_ui.js @@ -1970,10 +1970,15 @@ function rcube_calendar_ui(settings) rcmail.display_message(rcmail.get_label('importerror', 'calendar'), 'error'); }); - // display upload indicator + // display upload indicator (with extended timeout) + var timeout = rcmail.env.request_timeout; + rcmail.env.request_timeout = 600; me.import_succeeded = null; me.saving_lock = rcmail.set_busy(true, 'uploading'); $('.ui-dialog-buttonpane button', $dialog.parent()).button('disable'); + + // restore settings + rcmail.env.request_timeout = timeout; } };