Also need to change collection_id...
[davical.git] / scripts / load_calendar.php
blob12e38c143d6bb5cdbc37eff1cbfe5efcd668c909
1 #!/usr/bin/env php
2 <?php
3 /**
4 * Script to load a calendar from an ICS file from the command line.
5 */
7 if ( $argc < 5 ) {
9 $basename = $argv[0];
10 echo <<<USAGE
11 Usage:
13 $basename davical.example.com replace /username/calendar/ calendarfile.ics
15 Where:
16 'davical.example.com' is the hostname of your DAViCal server.
17 'replace' is either 'replace' or 'append'
18 '/username/calendar/' is the (sub-)path of the calendar to be updated.
19 'calendarfile.ics' is the iCalendar file to be loaded.
21 This script can be used to load events or whole calendars from an external
22 iCalendar file.
24 If the mode is 'replace' and the target calendar does not exist then it will
25 be created. For appending the calendar must always exist.
27 USAGE;
28 exit(1);
31 $_SERVER['SERVER_NAME'] = $argv[1];
32 $mode = $argv[2];
33 $target = $argv[3];
34 $source = $argv[4];
36 if ( ! is_readable($source) ) {
37 printf( "The iCalendar source file '%s' was not found.\n", $source );
38 exit(1);
40 $ics = trim(file_get_contents($source));
41 if ( strlen($ics) < 29 ) {
42 printf( "The iCalendar source file '%s' was missing or invalid.\n", $source );
43 exit(1);
47 $script_file = __FILE__;
48 chdir(preg_replace('{/scripts/[^/]+.php$}','/htdocs',$script_file));
50 require_once("./always.php");
51 require_once('caldav-PUT-functions.php');
52 require_once('check_UTF8.php');
53 $c->readonly_webdav_collections = false; // Override any active default.
55 dbg_error_log('load-collection',':Write: Loaded %d bytes from %s', strlen($ics), $source );
56 if ( !check_string($ics) ) {
57 $ics = force_utf8($ics);
58 if ( !check_string($ics) ) {
59 printf( "The source file '%s' contains some non-UTF-8 characters.\n", $source );
60 exit(1);
64 class FakeSession {
66 var $user_no;
67 var $principal_id;
68 var $username;
69 var $email;
70 var $dav_name;
71 var $principal;
72 var $logged_in;
74 function __construct($user_no = null) {
75 if ( empty($user_no) ) {
76 $this->user_no = -1;
77 $this->principal_id = -1;
78 $this->logged_in = false;
79 return;
82 $this->user_no = $user_no;
83 $principal = new Principal('user_no',$user_no);
84 // Assign each field in the selected record to the object
85 foreach( $principal AS $k => $v ) {
86 $this->{$k} = $v;
88 $this->username = $principal->username();
89 $this->principal_id = $principal->principal_id();
90 $this->email = $principal->email();
91 $this->dav_name = $principal->dav_name();
92 $this->principal = $principal;
94 $this->logged_in = true;
98 function AllowedTo($do_something) {
99 return $this->logged_in;
102 $session = new FakeSession();
104 $dest = new DAVResource($target);
105 $session = new FakeSession($dest->user_no());
106 if ( $mode == 'append' && ! $dest->Exists() ) {
107 printf( "The target '%s' does not exist.\n", $target );
108 exit(1);
111 if ( ! $dest->IsCollection() ) {
112 printf( "The target '%s' is not a collection.\n", $target );
113 exit(1);
116 $user_no = $dest->user_no();
117 $username = $session->username;
118 param_to_global('mode');
119 include_once('caldav-PUT-functions.php');
120 controlRequestContainer( $session->username, $dest->user_no(), $target, false, ($dest->IsPublic() ? true : false));
121 import_collection( $ics, $dest->user_no(), $target, $session->user_no, ($mode == 'append') );
122 printf(translate("Calendar '%s' was loaded from file.\n"), $target);