DAViCal
DAVPrincipal.php
1<?php
12require_once('Principal.php');
13
20{
21
26 private $calendar_home_set;
27
32 private $addressbook_home_set;
33
37 private $calendar_free_busy_set;
38
42 protected $_is_group;
43
47 private $group_member_set;
48
52 private $group_membership;
53
57 private $read_proxy_for;
58
62 private $write_proxy_for;
63
67 private $read_proxy_group;
68
72 private $write_proxy_group;
73
77 private $principal_address;
78
83 private $unique_tag;
84
95 function __construct( $parameters = null ) {
96 global $session, $c;
97
98 $this->exists = null;
99
100 if ( $parameters == null ) return;
101
102 if ( is_object($parameters) ) {
103 dbg_error_log( 'principal', 'Principal: record for %s', $parameters->username );
104 parent::__construct('username',$parameters->username);
105 }
106 else if ( is_int($parameters) ) {
107 dbg_error_log( 'principal', 'Principal: %d', $parameters );
108 parent::__construct('principal_id',$parameters);
109 }
110 else if ( is_array($parameters) ) {
111 if ( ! isset($parameters['options']['allow_by_email']) ) $parameters['options']['allow_by_email'] = false;
112 if ( isset($parameters['username']) ) {
113 parent::__construct('username',$parameters['username']);
114 }
115 else if ( isset($parameters['user_no']) ) {
116 parent::__construct('user_no',$parameters['user_no']);
117 }
118 else if ( isset($parameters['principal_id']) ) {
119 parent::__construct('principal_id',$parameters['principal_id']);
120 }
121 else if ( isset($parameters['email']) ) {
122 parent::__construct('email',$parameters['email']);
123 }
124 else if ( isset($parameters['path']) ) {
125 parent::__construct('path',$parameters['path']);
126 }
127 else if ( isset($parameters['principal-property-search']) ) {
128 $username = $this->PropertySearch($parameters['principal-property-search']);
129 parent::__construct('username',$username);
130 }
131 }
132
133 if ( ! $this->exists ) return;
134
135 $this->InitialiseRecord();
136
137 }
138
139
144 function InitialiseRecord() {
145 global $c;
146
147 $this->unique_tag = '"'.md5($this->username . $this->modified).'"';
148 $this->_is_group = (isset($this->type_id) && $this->type_id == 3);
149
150 $this->principal_address = $this->url . 'principal.vcf';
151
152 $this->user_address_set = array(
153 'mailto:'.$this->email,
154 $this->url,
155// ConstructURL( '/~'.$this->username.'/', true ),
156// ConstructURL( '/__uuids__/'.$this->username.'/', true ),
157 );
158
159 if ( isset ( $c->notifications_server ) ) {
160 $this->xmpp_uri = 'xmpp:pubsub.'.$c->notifications_server['host'].'?pubsub;node=/davical-'.$this->principal_id;
161 $this->xmpp_server = $c->notifications_server['host'];
162 }
163
164 if ( $this->_is_group ) {
165 $this->group_member_set = array();
166 $qry = new AwlQuery('SELECT usr.username FROM group_member JOIN principal ON (principal_id=member_id) JOIN usr USING(user_no) WHERE usr.active=true AND group_id = :group_id ORDER BY principal.principal_id ', array( ':group_id' => $this->principal_id) );
167 if ( $qry->Exec('DAVPrincipal') && $qry->rows() > 0 ) {
168 while( $member = $qry->Fetch() ) {
169 $this->group_member_set[] = ConstructURL( '/'. $member->username . '/', true);
170 }
171 }
172 }
173
174 $this->group_membership = array();
175 $qry = new AwlQuery('SELECT usr.username FROM group_member JOIN principal ON (principal_id=group_id) JOIN usr USING(user_no) WHERE usr.active=true AND member_id = :member_id UNION SELECT usr.username FROM group_member LEFT JOIN grants ON (to_principal=group_id) JOIN principal ON (principal_id=by_principal) JOIN usr USING(user_no) WHERE usr.active=true AND member_id = :member_id and by_principal != member_id ORDER BY 1', array( ':member_id' => $this->principal_id ) );
176 if ( $qry->Exec('DAVPrincipal') && $qry->rows() > 0 ) {
177 while( $group = $qry->Fetch() ) {
178 $this->group_membership[] = ConstructURL( '/'. $group->username . '/', true);
179 }
180 }
181
182 $this->read_proxy_group = null;
183 $this->write_proxy_group = null;
184 $this->write_proxy_for = null;
185 $this->read_proxy_for = null;
186
187 dbg_error_log( 'principal', ' User: %s (%d) URL: %s, By Email: %d', $this->username, $this->user_no, $this->url, $this->by_email );
188 }
189
190
194 function FetchProxyGroups() {
195 global $c;
196
197 $this->read_proxy_group = array();
198 $this->write_proxy_group = array();
199 $this->write_proxy_for = array();
200 $this->read_proxy_for = array();
201
202 if ( isset($c->disable_caldav_proxy) && $c->disable_caldav_proxy ) return;
203
204 $write_priv = privilege_to_bits(array('write'));
205 // whom are we a proxy for? who is a proxy for us?
206 // (as per Caldav Proxy section 5.1 Paragraph 7 and 5)
207 $sql = 'SELECT principal_id, username, pprivs(:request_principal::int8,principal_id,:scan_depth::int) FROM principal JOIN usr USING(user_no) WHERE usr.active=true AND principal_id IN (SELECT * from p_has_proxy_access_to(:request_principal,:scan_depth))';
208 $params = array( ':request_principal' => $this->principal_id, ':scan_depth' => $c->permission_scan_depth );
209 $qry = new AwlQuery($sql, $params);
210 if ( $qry->Exec('DAVPrincipal') && $qry->rows() > 0 ) {
211 while( $relationship = $qry->Fetch() ) {
212 if ( (bindec($relationship->pprivs) & $write_priv) != 0 ) {
213 $this->write_proxy_for[] = ConstructURL( '/'. $relationship->username . '/', true);
214 $this->group_membership[] = ConstructURL( '/'. $relationship->username . '/calendar-proxy-write/', true);
215 }
216 else {
217 $this->read_proxy_for[] = ConstructURL( '/'. $relationship->username . '/', true);
218 $this->group_membership[] = ConstructURL( '/'. $relationship->username . '/calendar-proxy-read/', true);
219 }
220 }
221 }
222
223 /* grants_proxy_access_from_p() is too clever and doesn't return any results, so do it on foot
224 $sql = 'SELECT principal_id, username, pprivs(principal_id,:request_principal::int8,:scan_depth::int) FROM principal JOIN usr USING(user_no) WHERE usr.active=true AND principal_id IN (SELECT * from grants_proxy_access_from_p(:request_principal,:scan_depth))';
225 */
226 $sql = 'SELECT principal_id, username, pprivs(principal_id,:request_principal::int8,:scan_depth::int) FROM principal JOIN usr USING(user_no) WHERE usr.active=true AND principal_id IN (SELECT to_principal FROM grants WHERE by_principal = :request_principal AND (privileges & 5::BIT(24)) != 0::BIT(24) AND by_collection IS NULL AND to_principal != :request_principal )';
227 $qry = new AwlQuery($sql, $params ); // reuse $params assigned for earlier query
228 if ( $qry->Exec('DAVPrincipal') && $qry->rows() > 0 ) {
229 while( $relationship = $qry->Fetch() ) {
230 if ( bindec($relationship->pprivs) & $write_priv ) {
231 $this->write_proxy_group[] = ConstructURL( '/'. $relationship->username . '/', true);
232 }
233 else {
234 $this->read_proxy_group[] = ConstructURL( '/'. $relationship->username . '/', true);
235 }
236 }
237 }
238 dbg_error_log( 'principal', 'Read-proxy-for: %s', implode(',',$this->read_proxy_for) );
239 dbg_error_log( 'principal', 'Write-proxy-for: %s', implode(',',$this->write_proxy_for) );
240 dbg_error_log( 'principal', 'Read-proxy-group: %s', implode(',',$this->read_proxy_group) );
241 dbg_error_log( 'principal', 'Write-proxy-group: %s', implode(',',$this->write_proxy_group) );
242 }
243
244
248 function ReadProxyGroup() {
249 if ( !isset($this->read_proxy_group) ) $this->FetchProxyGroups();
250 return $this->read_proxy_group;
251 }
252
253
257 function WriteProxyGroup() {
258 if ( !isset($this->write_proxy_group) ) $this->FetchProxyGroups();
259 return $this->write_proxy_group;
260 }
261
262
267 function ProxyFor( $type ) {
268 if ( !isset($this->read_proxy_for) ) $this->FetchProxyGroups();
269 if ( $type == 'write' ) return $this->write_proxy_for;
270 return $this->read_proxy_for;
271 }
272
273
277 function GroupMembership() {
278 if ( !isset($this->read_proxy_group) ) $this->FetchProxyGroups();
279 return $this->group_membership;
280 }
281
282
286 function GroupMemberSet() {
287 if ( ! $this->_is_group ) return null;
288 return $this->group_member_set;
289 }
290
291
296 function IsGroup() {
297 return $this->_is_group;
298 }
299
300
305 function GetProperty( $property_id ) {
306
307 switch( $property_id ) {
308 case 'DAV::resource-id':
309 if ( $this->exists && $this->principal_id > 0 )
310 ConstructURL('/.resources/'.$this->principal_id);
311 else
312 return null;
313 break;
314 }
315
316 if ( isset($this->{$property_id}) ) {
317 if ( ! is_object($this->{$property_id}) ) return $this->{$property_id};
318 return clone($this->{$property_id});
319 }
320 return null;
321 }
322
326 public function unique_tag() {
327 if ( isset($this->unique_tag) ) return $this->unique_tag;
328
329 if ( $this->exists !== true ) $this->unique_tag = '"-1"';
330
331 return $this->unique_tag;
332 }
333
334
338 function calendar_home_set() {
339 if ( !isset($this->calendar_home_set) ) {
340 $this->calendar_home_set = array();
341 $qry = new AwlQuery('SELECT DISTINCT parent_container FROM collection WHERE is_calendar AND dav_name ~ :dav_name_start',
342 array( ':dav_name_start' => '^'.$this->dav_name));
343 if ( $qry->Exec('principal',__LINE__,__FILE__) ) {
344 if ( $qry->rows() > 0 ) {
345 while( $calendar = $qry->Fetch() ) {
346 $this->calendar_home_set[] = ConstructURL($calendar->parent_container, true);
347 }
348 }
349 else {
350 $this->calendar_home_set[] = $this->url;
351 }
352 }
353 }
354 return $this->calendar_home_set;
355 }
356
357
362 if ( !isset($this->addressbook_home_set) ) {
363 $this->addressbook_home_set = array();
364 $qry = new AwlQuery('SELECT DISTINCT parent_container FROM collection WHERE is_addressbook AND dav_name ~ :dav_name_start',
365 array( ':dav_name_start' => '^'.$this->dav_name));
366 if ( $qry->Exec('principal',__LINE__,__FILE__) ) {
367 if ( $qry->rows() > 0 ) {
368 while( $addressbook = $qry->Fetch() ) {
369 $this->addressbook_home_set[] = ConstructURL($addressbook->parent_container, true);
370 }
371 }
372 else {
373 $this->addressbook_home_set[] = $this->url;
374 }
375 }
376 }
377 return $this->addressbook_home_set;
378 }
379
380
389 if (!isset($this->calendar_free_busy_set)) {
390 $this->calendar_free_busy_set = array();
391 $qry = new AwlQuery('SELECT dav_name FROM collection WHERE is_calendar AND (schedule_transp = \'opaque\' OR schedule_transp IS NULL) AND dav_name ~ :dav_name_start ORDER BY user_no, collection_id',
392 array(':dav_name_start' => '^' . $this->dav_name));
393 if ($qry->Exec('principal', __LINE__, __FILE__)) {
394 while ($calendar = $qry->Fetch()) {
395 $this->calendar_free_busy_set[] = ConstructURL($calendar->dav_name, true);
396 }
397 }
398 }
399 return $this->calendar_free_busy_set;
400 }
401
402
406 function Privileges() {
407 global $session;
408 if ( !isset($this->privileges) ) $this->privileges = 0;
409 if ( is_string($this->privileges) ) $this->privileges = bindec( $this->privileges );
410 if ( $this->_is_group ) {
411 if ( isset($session->principal) && in_array($session->principal->url(), $this->GroupMemberSet()) ) {
412 $this->privileges |= privilege_to_bits( array('DAV::read', 'DAV::read-current-user-privilege-set') );
413 }
414 }
415 return $this->privileges;
416 }
417
418
422 function AsCollection() {
423 $dav_name = (isset($this->original_request_url) ? DeconstructURL($this->original_request_url) : $this->dav_name());
424 $collection = (object) array(
425 'collection_id' => ($this->principal_id() ? $this->principal_id() : 0),
426 'is_calendar' => false,
427 'is_addressbook' => false,
428 'is_principal' => true,
429 'type' => 'principal' . (isset($this->original_request_url) ? '_link' : ''),
430 'user_no' => ($this->user_no() ? $this->user_no() : 0),
431 'username' => $this->username(),
432 'dav_name' => $dav_name,
433 'parent_container' => '/',
434 'email' => ($this->email()? $this->email() : ''),
435 'created' => $this->created,
436 'updated' => $this->modified,
437 'dav_etag' => substr($this->unique_tag(),1,-1),
438 'resourcetypes' => $this->resourcetypes
439 );
440 $collection->dav_displayname = (isset($this->dav_displayname) ? $this->dav_displayname : (isset($this->fullname) ? $this->fullname : $collection->username));
441
442 return $collection;
443 }
444
445
446 function PropertySearch( $parameters ) {
447 throw new Exception("Unimplemented!");
448 }
449
453 function PrincipalProperty( $tag, $prop, &$reply, &$denied ) {
454 global $c;
455
456 dbg_error_log('principal',':PrincipalProperty: Principal Property "%s"', $tag );
457 switch( $tag ) {
458 case 'DAV::getcontenttype':
459 $reply->DAVElement( $prop, 'getcontenttype', 'httpd/unix-directory' );
460 break;
461
462 case 'DAV::resourcetype':
463 $reply->DAVElement( $prop, 'resourcetype', array( new XMLElement('principal'), new XMLElement('collection')) );
464 break;
465
466 case 'DAV::displayname':
467 $reply->DAVElement( $prop, 'displayname', $this->fullname );
468 break;
469
470 case 'DAV::principal-URL':
471 $reply->DAVElement( $prop, 'principal-URL', $reply->href($this->url()) );
472 break;
473
474 case 'DAV::getlastmodified':
475 $reply->DAVElement( $prop, 'getlastmodified', ISODateToHTTPDate($this->modified) );
476 break;
477
478 case 'DAV::creationdate':
479 $reply->DAVElement( $prop, 'creationdate', DateToISODate($this->created) );
480 break;
481
482 case 'DAV::getcontentlanguage':
484 $locale = (isset($c->current_locale) ? $c->current_locale : '');
485 if ( isset($this->locale) && $this->locale != '' ) $locale = $this->locale;
486 $reply->DAVElement( $prop, 'getcontentlanguage', $locale );
487 break;
488
489 case 'http://calendarserver.org/ns/:group-member-set':
490 case 'DAV::group-member-set':
492 if ( ! $this->_is_group ) return false;
493 $reply->DAVElement( $prop, 'group-member-set', $reply->href($this->group_member_set) );
494 break;
495
496 case 'http://calendarserver.org/ns/:group-membership':
497 case 'DAV::group-membership':
498 $reply->DAVElement( $prop, 'group-membership', $reply->href($this->GroupMembership()) );
499 break;
500
501 case 'urn:ietf:params:xml:ns:caldav:schedule-inbox-URL':
502 $reply->CalDAVElement($prop, 'schedule-inbox-URL', $reply->href($this->url('schedule-inbox')) );
503 break;
504
505 case 'urn:ietf:params:xml:ns:caldav:schedule-outbox-URL':
506 $reply->CalDAVElement($prop, 'schedule-outbox-URL', $reply->href($this->url('schedule-outbox')) );
507 break;
508
509 case 'urn:ietf:params:xml:ns:caldav:schedule-default-calendar-URL':
510 $reply->CalDAVElement($prop, 'schedule-default-calendar-URL', $reply->href($this->url('schedule-default-calendar')) );
511 break;
512
513 case 'http://calendarserver.org/ns/:dropbox-home-URL':
514 $reply->CalendarserverElement($prop, 'dropbox-home-URL', $reply->href($this->url('dropbox')) );
515 break;
516
517 case 'http://calendarserver.org/ns/:xmpp-server':
518 if ( ! isset( $this->xmpp_uri ) ) return false;
519 $reply->CalendarserverElement($prop, 'xmpp-server', $this->xmpp_server );
520 break;
521
522 case 'http://calendarserver.org/ns/:xmpp-uri':
523 if ( ! isset( $this->xmpp_uri ) ) return false;
524 $reply->CalendarserverElement($prop, 'xmpp-uri', $this->xmpp_uri );
525 break;
526
527 case 'urn:ietf:params:xml:ns:carddav:addressbook-home-set':
528 $reply->CardDAVElement($prop, $tag, $reply->href( $this->addressbook_home_set() ) );
529 break;
530
531 case 'urn:ietf:params:xml:ns:caldav:calendar-home-set':
532 $reply->CalDAVElement($prop, $tag, $reply->href( $this->calendar_home_set() ) );
533 break;
534
535 case 'urn:ietf:params:xml:ns:caldav:calendar-free-busy-set':
541 if ( isset($c->support_obsolete_free_busy_property) && $c->support_obsolete_free_busy_property )
542 $reply->CalDAVElement( $prop, 'calendar-free-busy-set', $reply->href( $this->calendar_free_busy_set() ) );
543 else
544 return false;
545 break;
546
547 case 'urn:ietf:params:xml:ns:caldav:calendar-user-address-set':
548 $reply->CalDAVElement($prop, 'calendar-user-address-set', $reply->href($this->user_address_set));
549 break;
550
551 case 'urn:ietf:params:xml:ns:caldav:calendar-user-type':
556 $type = 'UNKNOWN';
557 if ( isset($this->type_id) ) {
558 switch ( $this->type_id ) {
559 case 1:
560 $type = 'INDIVIDUAL';
561 break;
562 case 2:
563 $type = 'RESOURCE';
564 break;
565 case 3:
566 $type = 'GROUP';
567 break;
568 // 'ROOM' type is not supported yet
569 }
570 }
571 $reply->CalDAVElement($prop, 'calendar-user-type', $type);
572 break;
573
574 case 'DAV::owner':
575 // After a careful reading of RFC3744 we see that this must be the principal-URL of the owner
576 $reply->DAVElement( $prop, 'owner', $reply->href( $this->url ) );
577 break;
578
579 // Empty tag responses.
580 case 'DAV::alternate-URI-set':
581 $reply->DAVElement( $prop, $reply->Tag($tag));
582 break;
583
584 case 'SOME-DENIED-PROPERTY':
585 $denied[] = $reply->Tag($tag);
586 break;
587
588 default:
589 return false;
590 break;
591 }
592
593 return true;
594 }
595
596
606 function RenderAsXML( $properties, &$reply, $props_only = false ) {
607 dbg_error_log('principal',':RenderAsXML: Principal "%s"', $this->username );
608
609 $prop = new XMLElement('prop');
610 $denied = array();
611 $not_found = array();
612 foreach( $properties AS $k => $tag ) {
613 if ( ! $this->PrincipalProperty( $tag, $prop, $reply, $denied ) ) {
614 dbg_error_log( 'principal', 'Request for unsupported property "%s" of principal "%s".', $tag, $this->username );
615 $not_found[] = $reply->Tag($tag);
616 }
617 }
618
619 if ( $props_only ) return $prop;
620
621 $status = new XMLElement('status', 'HTTP/1.1 200 OK' );
622
623 $propstat = new XMLElement( 'propstat', array( $prop, $status) );
624 $href = $reply->href($this->url );
625
626 $elements = array($href,$propstat);
627
628 if ( count($denied) > 0 ) {
629 $status = new XMLElement('status', 'HTTP/1.1 403 Forbidden' );
630 $noprop = new XMLElement('prop');
631 foreach( $denied AS $k => $v ) {
632 $noprop->NewElement( $v );
633 }
634 $elements[] = new XMLElement( 'propstat', array( $noprop, $status) );
635 }
636
637 if ( count($not_found) > 0 ) {
638 $status = new XMLElement('status', 'HTTP/1.1 404 Not Found' );
639 $noprop = new XMLElement('prop');
640 foreach( $not_found AS $k => $v ) {
641 $noprop->NewElement( $v );
642 }
643 $elements[] = new XMLElement( 'propstat', array( $noprop, $status) );
644 }
645
646 $response = new XMLElement( 'response', $elements );
647
648 return $response;
649 }
650
651}
__construct( $parameters=null)
PrincipalProperty( $tag, $prop, &$reply, &$denied)
RenderAsXML( $properties, &$reply, $props_only=false)
ProxyFor( $type)
GetProperty( $property_id)
url($type='principal', $internal=false)
Definition: Principal.php:467
principal_id()
Definition: Principal.php:361