Fix tools.php to allow importing of a directory of calendars again.

This commit is contained in:
Andrew McMillan 2011-10-14 11:08:17 +13:00
parent 2fd4a95285
commit 4d1f936a8b
2 changed files with 29 additions and 25 deletions

View File

@ -17,6 +17,9 @@ require_once("DataEntry.php");
require_once("interactive-page.php"); require_once("interactive-page.php");
require_once("classBrowser.php"); require_once("classBrowser.php");
require_once("caldav-PUT-functions.php");
include_once('check_UTF8.php');
if ( !$session->AllowedTo("Admin" ) ) if ( !$session->AllowedTo("Admin" ) )
exit; exit;
@ -43,7 +46,7 @@ class Tools {
} }
} }
function renderSyncLDAP(){ static function renderSyncLDAP(){
$html = '<div id="entryform">'; $html = '<div id="entryform">';
$html .= '<h1>'.translate('Sync LDAP with DAViCal') .'</h1>'; $html .= '<h1>'.translate('Sync LDAP with DAViCal') .'</h1>';
@ -71,18 +74,19 @@ class Tools {
return $html; return $html;
} }
function renderImportFromDirectory(){ static function renderImportFromDirectory(){
$html = '<div id="entryform">'; $html = '<div id="entryform">';
$html .= '<h1>'.translate('Import all .ics files of a directory') .'</h1>'; $html .= '<h1>'.translate('Import all .ics files of a directory') .'</h1>';
$html .= '<p>'.translate('This process will import each file in a directory named "username.ics" and create a user and calendar for each file to import.') .'</p>';
$data = (object) array('directory_path' => '/path/to/your/ics/files','calendar_path' => 'home');
$data = (object) array('directory_path' => '/path/to/your/ics/files','calendar_path' => 'calendar');
$ef = new EntryForm( $_SERVER['REQUEST_URI'],$data , true,true ); $ef = new EntryForm( $_SERVER['REQUEST_URI'],$data , true,true );
$html .= "<table width=\"100%\" class=\"data\">\n"; $html .= "<table width=\"100%\" class=\"data\">\n";
$html .= $ef->StartForm( array("autocomplete" => "off" ) ); $html .= $ef->StartForm( array("autocomplete" => "off" ) );
$html .= $ef->DataEntryLine( translate("path to store your ics"), "%s", "text", "calendar_path", $html .= $ef->DataEntryLine( translate("path to store your ics"), "%s", "text", "calendar_path",
array( "size" => 20, 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("<b>WARNING: all events in this path will be deleted before inserting allof the ics file</b>") "help" => translate("<b>WARNING: all events in this path will be deleted before inserting allof the ics file</b>")
) )
, '' ); , '' );
@ -98,9 +102,9 @@ class Tools {
return $html; return $html;
} }
function importFromDirectory(){ static function importFromDirectory(){
global $c; global $c;
if(!isset($_POST["calendar_path"])){ if(empty($_POST["calendar_path"])){
dbg_error_log( "importFromDirectory", "calendar path not given"); dbg_error_log( "importFromDirectory", "calendar path not given");
return ; 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,-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 ( 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"); dbg_error_log( "importFromDirectory", "directory path not given");
return ; return ;
} }
@ -119,6 +123,7 @@ class Tools {
return ; return ;
} }
if ($handle = opendir($dir)) { 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))) { while (false !== ($file = readdir($handle))) {
if ($file == "." || $file == ".." || substr($file,-4) != '.ics') continue; if ($file == "." || $file == ".." || substr($file,-4) != '.ics') continue;
if ( !is_readable($dir.'/'.$file) ) { if ( !is_readable($dir.'/'.$file) ) {
@ -130,24 +135,23 @@ class Tools {
if ( $ics != '' ) { if ( $ics != '' ) {
include_once('check_UTF8.php'); if ( ! check_string($ics) ) {
if ( check_string($ics) ) { $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); continue;
$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);
} }
else { $username = substr($file,0,-4);
$c->messages[] = sprintf(translate('the file %s is not UTF-8 encoded, please check error for more details'),$dir.'/'.$file); $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); closedir($handle);

View File

@ -317,7 +317,7 @@ function handle_schedule_reply ( vCalendar $ical ) {
*/ */
function do_scheduling_requests( vCalendar $resource, $create ) { function do_scheduling_requests( vCalendar $resource, $create ) {
global $request, $c; 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) ) { if ( ! is_object($resource) ) {
dbg_error_log( 'PUT', 'do_scheduling_requests called with non-object parameter (%s)', gettype($resource) ); dbg_error_log( 'PUT', 'do_scheduling_requests called with non-object parameter (%s)', gettype($resource) );