]>
git.datanom.net - webcal.git/blob - caldav/caldavresource.class.php
4c1214ef5d3956ae771ac5ec6a174adf93c45513
13 public function __construct($value) {
15 case 'VEVENT': $this->value
= self
::VEVENT
; break;
16 case 'VTODO': $this->value
= self
::VTODO
; break;
17 case 'VJOURNAL': $this->value
= self
::VJOURNAL
; break;
18 case 'VFREEBUSY': $this->value
= self
::VFREEBUSY
; break;
19 case 'VTIMEZONE': $this->value
= self
::VTIMEZONE
; break;
20 case 'VALARM': $this->value
= self
::VALARM
; break;
26 case self
::VALARM
: $this->value
= $value; break;
27 default: throw new Exception ("$value: Invalid VTYPE");
31 private function __clone() {}
33 public function ordinal() {
37 public function __toString() {
38 switch ($this->value
) {
39 case self
::VEVENT
: return 'VEVENT'; break;
40 case self
::VTODO
: return 'VTODO'; break;
41 case self
::VJOURNAL
: return 'VJOURNAL'; break;
42 case self
::VFREEBUSY
: return 'VFREEBUSY'; break;
43 case self
::VTIMEZONE
: return 'VTIMEZONE'; break;
44 case self
::VALARM
: return 'VALARM'; break;
50 abstract class CaldavRessource
51 implements ArrayAccess
, IteratorAggregate
{
55 function __construct($url, $uid = '', $pwd = '', $cal = '') {
57 throw new Exception("Missing URL");
58 $this->client
= new CalDAVClient($url, $uid, $pwd, $cal);
62 * abstract functions to be implemented by sub classes
64 abstract function update($url, $etag = NULL);
65 abstract function newComponent($c_type);
66 abstract function getComponents($start, $end);
67 abstract function delete($url, $etag = NULL);
69 protected function callServer($method, $param = array()) {
71 $msg = "Unknown error";
73 if (! is_array($param))
74 throw new Exception("Parameters must be inclosed in an array");
75 switch (strtolower($method)) {
77 if (count($param) != 2) {
79 $msg = "Expected 2 parameters";
82 if ($this->isDateTime($param[0]) &&
83 $this->isDateTime($param[1])) {
84 $res = $this->client
->GetEvents($param[0], $param[1]);
88 $msg = "[${param[0]},${param[1]}]: Invalid DateTime";
93 if (count($param) != 1) {
95 $msg = "Expected 1 parameter";
98 $res = $this->client
->GetEntryByUid($param[0]);
102 if (count($param) < 2 ||
count($param) > 3) {
104 $msg = "Syntax: URL, CalDAV_resource[, ETag]";
107 if (count($param) == 2)
108 $res = $this->client
->DoPUTRequest($param[0], $param[1]);
110 $res = $this->client
->DoPUTRequest($param[0], $param[1], $param[2]);
114 if (count($param) < 1 ||
count($param) > 2) {
116 $msg = "Syntax: URL[, ETag]";
119 if (count($param) == 1)
120 $res = $this->client
->DoDELETERequest($param[0]);
122 $res = $this->client
->DoDELETERequest($param[0], $param[1]);
126 throw new Exception("$method: Unknown method");
129 throw new Exception($msg);
134 static function isDateTime($var) {
135 return (preg_match("/^([0-9]{8})T([0-9]{6})Z?$/", $var) > 0);
139 * Returned date-time will always be in UTC
141 static function timestamp2ICal($ts, $localtime = TRUE) {
144 throw new Exception("$ts: invalid timestamp");
146 $date = date('Ymd', $ts);
147 $time = date('His', $ts);
148 $res = sprintf("%sT%s", $date, $time);
151 $date = gmdate('Ymd', $ts);
152 $time = gmdate('His', $ts);
153 $res = sprintf("%sT%sZ", $date, $time);
158 static function iCal2Timestamp($ical) {
159 if (! self
::isDateTime($ical)) {
160 // test for badly formed all-day event
162 $res = preg_match("/^([0-9]{4})([0-9]{2})([0-9]{2})$/",
165 throw new Exception("$ical: invalid CalDAV Date-Time");
167 $timepart = array('00', '00', '00');
168 $parts = array_merge($parts, $timepart);
172 $date = "([0-9]{4})([0-9]{2})([0-9]{2})";
173 $time = "([0-9]{2})([0-9]{2})([0-9]{2})";
174 preg_match("/^${date}T${time}(Z?)$/", $ical, $parts);
176 if (count($parts) == 8 && ! empty($parts[7]))
177 return gmmktime($parts[4], $parts[5], $parts[6],
178 $parts[2], $parts[3], $parts[1]);
180 return mktime($parts[4], $parts[5], $parts[6],
181 $parts[2], $parts[3], $parts[1]);
184 private static function down_hour($date) {
185 //print "$date<br/>";
186 if (! self
::isDateTime($date)) {
187 // test for badly formed all-day event
188 $res = preg_match("/^([0-9]{4})([0-9]{2})([0-9]{2})$/",
191 throw new Exception("$date: invalid CalDAV Date-Time");
194 $timepart = array('T', '00', '00', '00');
195 $parts = array_merge($parts, $timepart);
196 return implode('', $parts);
200 $a = explode('T', $date);
201 $a[1] = substr_replace($a[1], '0000', 2);
202 return $a[0].'T'.$a[1];
206 static function fix_allday_event(&$date_a, &$date_b) {
207 //print "$date_a : $date_b<br/>";
208 if ($date_a == $date_b) {
209 if (! self
::isDateTime($date_a) && ! self
::isDateTime($date_b)) {
210 $res1 = preg_match("/^([0-9]{4})([0-9]{2})([0-9]{2})$/",
212 $res2 = preg_match("/^([0-9]{4})([0-9]{2})([0-9]{2})$/",
214 if ($res1 == 0 ||
$res2 == 0)
215 throw new Exception("$date_a, $date_b: invalid CalDAV Date-Time");
217 $date_a .= "T000000";
218 $date_b .= "T235959";
222 preg_match("/^([0-9]{4}[0-9]{2}[0-9]{2})T([0-9]{6})$/",
224 preg_match("/^([0-9]{4}[0-9]{2}[0-9]{2})T([0-9]{6})$/",
226 $date_a = $part_a[1]."T000000";
227 $date_b = $part_b[1]."T235959";
228 //print "$date_a : $date_b<br/>";
230 //print "$date_a : $date_b<br/>";
234 static function datecmp($date_a, $date_b) {
235 $date_a = self
::iCal2Timestamp($date_a);
236 $date_b = self
::iCal2Timestamp(self
::down_hour($date_b));
237 if ($date_a < $date_b)
239 else if ($date_a > $date_b)
246 private static function intcmpstr($a_str, $b_str) {
249 //print "$a:$b<br/>";
258 static function cmpdate($date_a, $date_b) {
259 $datepart = explode('T', $date_a);
261 $datepart = explode('T', $date_b);
263 $y_cmp = self
::intcmpstr(substr($d_a, 0, 4), substr($d_b, 0, 4));
265 $m_cmp = self
::intcmpstr(substr($d_a, 4, 2), substr($d_b, 4, 2));
267 return self
::intcmpstr(substr($d_a, 6, 2), substr($d_b, 6, 2));
274 static function cmptime($time_a, $time_b) {
275 $timepart = explode('T', $time_a);
277 $timepart = explode('T', $time_b);
279 //print "$t_a:$t_b<br/>";
280 $h_cmp = self
::intcmpstr(substr($t_a, 0, 2), substr($t_b, 0, 2));
282 $m_cmp = self
::intcmpstr(substr($t_a, 2, 2), substr($t_b, 2, 2));
284 return self
::intcmpstr(substr($t_a, 4, 2), substr($t_b, 4, 2));
291 static function allDayEvent($time_a, $time_b) {
292 //echo $time_a.':'.$time_b.'<br/>';
293 $a = explode('T', $time_a);
295 array_push($a, '0000');
296 $b = explode('T', $time_b);
298 array_push($b, '0000');
299 $t = strtotime($time_b) - 3600;
300 $t = date("Ymd\THm", $t);
301 return (self
::cmpdate($time_a, $t) == 0 &&
302 $a[1] == '0000' && $b[1] == '0000');
This page took 0.191819 seconds and 6 git commands to generate.