File: load_calendar.php

package info (click to toggle)
davical 1.1.12-2.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,020 kB
  • sloc: php: 19,361; sql: 4,514; perl: 3,524; sh: 554; javascript: 162; makefile: 85
file content (123 lines) | stat: -rwxr-xr-x 3,302 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env php
<?php
/**
 * Script to load a calendar from an ICS file from the command line.
 */

if ( $argc < 5 ) {

  $basename = $argv[0];
  echo <<<USAGE
Usage:

	$basename davical.example.com replace /username/calendar/ calendarfile.ics

Where:
 'davical.example.com' is the hostname of your DAViCal server.
 'replace' is either 'replace' or 'append'
 '/username/calendar/' is the (sub-)path of the calendar to be updated.
 'calendarfile.ics' is the iCalendar file to be loaded.

This script can be used to load events or whole calendars from an external
iCalendar file.

If the mode is 'replace' and the target calendar does not exist then it will
be created.  For appending the calendar must always exist.

USAGE;
  exit(1);
}

$_SERVER['SERVER_NAME'] = $argv[1];
$mode = $argv[2];
$target = $argv[3];
$source = $argv[4];

if ( ! is_readable($source) ) {
  printf( "The iCalendar source file '%s' was not found.\n", $source );
  exit(1);
}
$ics = trim(file_get_contents($source));
if ( strlen($ics) < 29 ) {
  printf( "The iCalendar source file '%s' was missing or invalid.\n", $source );
  exit(1);
}


$script_file = __FILE__;
chdir(preg_replace('{/scripts/[^/]+.php$}','/htdocs',$script_file));

require_once("./always.php");
require_once('caldav-PUT-functions.php');
require_once('check_UTF8.php');
$c->readonly_webdav_collections = false; // Override any active default.

dbg_error_log('load-collection',':Write: Loaded %d bytes from %s', strlen($ics), $source );
if ( !check_string($ics) ) {
  $ics = force_utf8($ics);
  if ( !check_string($ics) ) {
    printf( "The source file '%s' contains some non-UTF-8 characters.\n", $source );
    exit(1);
  }
}

class FakeSession {

  var $user_no;
  var $principal_id;
  var $username;
  var $email;
  var $dav_name;
  var $principal;
  var $logged_in;

  function __construct($user_no = null) {
    if ( empty($user_no) ) {
      $this->user_no = -1;
      $this->principal_id = -1;
      $this->logged_in = false;
      return;
    }

    $this->user_no = $user_no;
    $principal = new Principal('user_no',$user_no);
    // Assign each field in the selected record to the object
    foreach( $principal AS $k => $v ) {
      $this->{$k} = $v;
    }
    $this->username = $principal->username();
    $this->principal_id = $principal->principal_id();
    $this->email = $principal->email();
    $this->dav_name = $principal->dav_name();
    $this->principal = $principal;
    
    $this->logged_in = true;

  }

  function AllowedTo($do_something) {
    return $this->logged_in;
  }
}
$session = new FakeSession();

$dest = new DAVResource($target);
$session = new FakeSession($dest->user_no());
if ( $mode == 'append' && ! $dest->Exists() ) {
  printf( "The target '%s' does not exist.\n", $target );
  exit(1);
}

if ( ! $dest->IsCollection() ) {
  printf( "The target '%s' is not a collection.\n", $target );
  exit(1);
}

$user_no = $dest->user_no();
$username = $session->username;
param_to_global('mode');
include_once('caldav-PUT-functions.php');
controlRequestContainer( $session->username, $dest->user_no(), $target, false, ($dest->IsPublic() ? true : false));
import_collection( $ics, $dest->user_no(), $target, $session->user_no, ($mode == 'append') );
printf(translate('Calendar "%s" was loaded from file.') . "\n", $target);