annotate plugins/libcalendaring/lib/Sabre/VObject/Splitter/ICalendar.php @ 4:888e774ee983

libcalendar plugin as distributed
author Charlie Root
date Sat, 13 Jan 2018 08:57:56 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1 <?php
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
2
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
3 namespace Sabre\VObject\Splitter;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
4
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
5 use Sabre\VObject;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
6
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
7 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
8 * Splitter
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
9 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
10 * This class is responsible for splitting up iCalendar objects.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
11 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
12 * This class expects a single VCALENDAR object with one or more
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
13 * calendar-objects inside. Objects with identical UID's will be combined into
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
14 * a single object.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
15 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
16 * @copyright Copyright (C) 2007-2013 fruux GmbH (https://fruux.com/).
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
17 * @author Dominik Tobschall
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
18 * @author Armin Hackmann
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
19 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
20 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
21 class ICalendar implements SplitterInterface {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
22
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
23 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
24 * Timezones
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
25 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
26 * @var array
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
27 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
28 protected $vtimezones = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
29
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
30 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
31 * iCalendar objects
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
32 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
33 * @var array
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
34 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
35 protected $objects = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
36
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
37 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
38 * Constructor
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
39 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
40 * The splitter should receive an readable file stream as it's input.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
41 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
42 * @param resource $input
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
43 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
44 public function __construct($input) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
45
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
46 $data = VObject\Reader::read(stream_get_contents($input));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
47 $vtimezones = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
48 $components = array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
49
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
50 foreach($data->children as $component) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
51 if (!$component instanceof VObject\Component) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
52 continue;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
53 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
54
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
55 // Get all timezones
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
56 if ($component->name === 'VTIMEZONE') {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
57 $this->vtimezones[(string)$component->TZID] = $component;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
58 continue;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
59 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
60
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
61 // Get component UID for recurring Events search
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
62 if($component->UID) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
63 $uid = (string)$component->UID;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
64 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
65 // Generating a random UID
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
66 $uid = sha1(microtime()) . '-vobjectimport';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
67 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
68
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
69 // Take care of recurring events
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
70 if (!array_key_exists($uid, $this->objects)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
71 $this->objects[$uid] = VObject\Component::create('VCALENDAR');
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
72 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
73
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
74 $this->objects[$uid]->add(clone $component);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
75 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
76
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
77 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
78
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
79 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
80 * Every time getNext() is called, a new object will be parsed, until we
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
81 * hit the end of the stream.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
82 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
83 * When the end is reached, null will be returned.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
84 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
85 * @return Sabre\VObject\Component|null
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
86 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
87 public function getNext() {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
88
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
89 if($object=array_shift($this->objects)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
90
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
91 // create our baseobject
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
92 $object->version = '2.0';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
93 $object->prodid = '-//Sabre//Sabre VObject ' . VObject\Version::VERSION . '//EN';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
94 $object->calscale = 'GREGORIAN';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
95
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
96 // add vtimezone information to obj (if we have it)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
97 foreach ($this->vtimezones as $vtimezone) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
98 $object->add($vtimezone);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
99 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
100
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
101 return $object;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
102
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
103 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
104
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
105 return null;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
106
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
107 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
108
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
109 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
110
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
111 }