From 4d1f936a8b9251fc1d71a393254766ce7eff8a8d Mon Sep 17 00:00:00 2001 From: Andrew McMillan Date: Fri, 14 Oct 2011 11:08:17 +1300 Subject: [PATCH] Fix tools.php to allow importing of a directory of calendars again. --- htdocs/tools.php | 52 +++++++++++++++++++----------------- inc/caldav-PUT-functions.php | 2 +- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/htdocs/tools.php b/htdocs/tools.php index 3aaa6307..a6fbc3cd 100644 --- a/htdocs/tools.php +++ b/htdocs/tools.php @@ -17,6 +17,9 @@ require_once("DataEntry.php"); require_once("interactive-page.php"); require_once("classBrowser.php"); +require_once("caldav-PUT-functions.php"); +include_once('check_UTF8.php'); + if ( !$session->AllowedTo("Admin" ) ) exit; @@ -43,7 +46,7 @@ class Tools { } } - function renderSyncLDAP(){ + static function renderSyncLDAP(){ $html = '
'; $html .= '

'.translate('Sync LDAP with DAViCal') .'

'; @@ -71,18 +74,19 @@ class Tools { return $html; } - function renderImportFromDirectory(){ + static function renderImportFromDirectory(){ $html = '
'; $html .= '

'.translate('Import all .ics files of a directory') .'

'; - - $data = (object) array('directory_path' => '/path/to/your/ics/files','calendar_path' => 'home'); + $html .= '

'.translate('This process will import each file in a directory named "username.ics" and create a user and calendar for each file to import.') .'

'; + + $data = (object) array('directory_path' => '/path/to/your/ics/files','calendar_path' => 'calendar'); $ef = new EntryForm( $_SERVER['REQUEST_URI'],$data , true,true ); $html .= "\n"; $html .= $ef->StartForm( array("autocomplete" => "off" ) ); $html .= $ef->DataEntryLine( translate("path to store your ics"), "%s", "text", "calendar_path", array( "size" => 20, - "title" => translate("Set the path to store your ics e.g. 'home' will be referenced as /caldav.php/me/home/"), + "title" => translate("Set the path to store your ics e.g. 'calendar' will be referenced as /caldav.php/username/calendar/"), "help" => translate("WARNING: all events in this path will be deleted before inserting allof the ics file") ) , '' ); @@ -98,9 +102,9 @@ class Tools { return $html; } - function importFromDirectory(){ + static function importFromDirectory(){ global $c; - if(!isset($_POST["calendar_path"])){ + if(empty($_POST["calendar_path"])){ dbg_error_log( "importFromDirectory", "calendar path not given"); return ; } @@ -108,7 +112,7 @@ class Tools { if ( substr($path_ics,-1,1) != '/' ) $path_ics .= '/'; // ensure that we target a collection if ( substr($path_ics,0,1) != '/' ) $path_ics = '/'.$path_ics; // ensure that we target a collection - if(!isset($_POST["directory_path"])){ + if(empty($_POST["directory_path"])){ dbg_error_log( "importFromDirectory", "directory path not given"); return ; } @@ -119,6 +123,7 @@ class Tools { return ; } if ($handle = opendir($dir)) { + $c->readonly_webdav_collections = false; // Override this setting so we can create collections/events on import. while (false !== ($file = readdir($handle))) { if ($file == "." || $file == ".." || substr($file,-4) != '.ics') continue; if ( !is_readable($dir.'/'.$file) ) { @@ -130,24 +135,23 @@ class Tools { if ( $ics != '' ) { - include_once('check_UTF8.php'); - if ( check_string($ics) ) { - $username = substr($file,0,-4); - $path = "/".$username.$path_ics; - dbg_error_log( "importFromDirectory", "importing to $path"); - $c->readonly_webdav_collections = false; // Override this setting so we can create collections/events on import. - require_once("caldav-PUT-functions.php"); - if ( $principal = new Principal('username',$username) ) { - $user_no = $principal->user_no(); - } - if ( controlRequestContainer($username, $user_no, $path, false) === -1) - continue; - import_collection($ics,$user_no,$path,1); - $c->messages[] = sprintf(translate('all events of user %s were deleted and replaced by those from file %s'),substr($file,0,-4),$dir.'/'.$file); + if ( ! check_string($ics) ) { + $c->messages[] = sprintf(translate('The file "%s" is not UTF-8 encoded, please check error for more details'),$dir.'/'.$file); + continue; } - else { - $c->messages[] = sprintf(translate('the file %s is not UTF-8 encoded, please check error for more details'),$dir.'/'.$file); + $username = substr($file,0,-4); + $principal = new Principal('username',$username); + if ( !$principal->Exists() ) { + $c->messages[] = sprintf(translate('The principal "%s" does not exist'),$username); + continue; } + $path = "/".$username.$path_ics; + $user_no = $principal->user_no(); + if ( controlRequestContainer($username, $user_no, $path, false) === -1) + continue; + dbg_error_log( "importFromDirectory", "importing to $path"); + import_collection($ics,$user_no,$path,1); + $c->messages[] = sprintf(translate('All events of user "%s" were deleted and replaced by those from file %s'),substr($file,0,-4),$dir.'/'.$file); } } closedir($handle); diff --git a/inc/caldav-PUT-functions.php b/inc/caldav-PUT-functions.php index 37825de9..993be38b 100644 --- a/inc/caldav-PUT-functions.php +++ b/inc/caldav-PUT-functions.php @@ -317,7 +317,7 @@ function handle_schedule_reply ( vCalendar $ical ) { */ function do_scheduling_requests( vCalendar $resource, $create ) { global $request, $c; - if ( isset($c->enable_auto_schedule) && !$c->enable_auto_schedule ) return; + if ( !isset($request) || (isset($c->enable_auto_schedule) && !$c->enable_auto_schedule) ) return; if ( ! is_object($resource) ) { dbg_error_log( 'PUT', 'do_scheduling_requests called with non-object parameter (%s)', gettype($resource) );