document closed Debian bug
[davical.git] / inc / DAVPrincipal.php
blobb5f8c9ef01e033e694e8a336ff05c3a1ea8b9edb
1 <?php
2 /**
3 * An object representing a DAV 'Principal'
5 * @package davical
6 * @subpackage Principal
7 * @author Andrew McMillan <andrew@mcmillan.net.nz>
8 * @copyright Catalyst .Net Ltd, Morphoss Ltd <http://www.morhposs.com/>
9 * @license http://gnu.org/copyleft/gpl.html GNU GPL v2 or later
12 require_once('Principal.php');
14 /**
15 * A class for things to do with a DAV Principal
17 * @package davical
19 class DAVPrincipal extends Principal
22 /**
23 * @var RFC4791: Identifies the URL(s) of any WebDAV collections that contain
24 * calendar collections owned by the associated principal resource.
26 private $calendar_home_set;
28 /**
29 * @var CardDAV: Identifies the URL(s) of any WebDAV collections that contain
30 * addressbook collections owned by the associated principal resource.
32 private $addressbook_home_set;
34 /**
35 * @var Obsolete: Identifies the URL(s) of any calendars participating in free/busy
37 private $calendar_free_busy_set;
39 /**
40 * @var RFC3744: The principals that are direct members of this group.
42 protected $_is_group;
44 /**
45 * @var RFC3744: The principals that are direct members of this group.
47 private $group_member_set;
49 /**
50 * @var RFC3744: The groups in which the principal is directly a member.
52 private $group_membership;
54 /**
55 * @var caldav-cu-proxy-02: The principals which this one has read permissions on.
57 private $read_proxy_for;
59 /**
60 * @var caldav-cu-proxy-02: The principals which this one has read-write prmissions for.
62 private $write_proxy_for;
64 /**
65 * @var caldav-cu-proxy-02: The principals which have read permissions on this one.
67 private $read_proxy_group;
69 /**
70 * @var caldav-cu-proxy-02: The principals which have write permissions on this one.
72 private $write_proxy_group;
74 /**
75 * @var CardDAV: The URL to an addressbook entry for this principal
77 private $principal_address;
79 /**
80 * A unique tag which will change if this principal changes
81 * @var string
83 private $unique_tag;
85 /**
86 * Constructor
87 * @param mixed $parameters If null, an empty Principal is created. If it
88 * is an integer then that ID is read (if possible). If it is
89 * an array then the Principal matching the supplied elements
90 * is read. If it is an object then it is expected to be a 'usr'
91 * record that was read elsewhere.
93 * @return boolean Whether we actually read data from the DB to initialise the record.
95 function __construct( $parameters = null ) {
96 global $session, $c;
98 $this->exists = null;
100 if ( $parameters == null ) return;
102 if ( is_object($parameters) ) {
103 dbg_error_log( 'principal', 'Principal: record for %s', $parameters->username );
104 parent::__construct('username',$parameters->username);
106 else if ( is_int($parameters) ) {
107 dbg_error_log( 'principal', 'Principal: %d', $parameters );
108 parent::__construct('principal_id',$parameters);
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']);
115 else if ( isset($parameters['user_no']) ) {
116 parent::__construct('user_no',$parameters['user_no']);
118 else if ( isset($parameters['principal_id']) ) {
119 parent::__construct('principal_id',$parameters['principal_id']);
121 else if ( isset($parameters['email']) ) {
122 parent::__construct('email',$parameters['email']);
124 else if ( isset($parameters['path']) ) {
125 parent::__construct('path',$parameters['path']);
127 else if ( isset($parameters['principal-property-search']) ) {
128 $username = $this->PropertySearch($parameters['principal-property-search']);
129 parent::__construct('username',$username);
133 if ( ! $this->exists ) return;
135 $this->InitialiseRecord();
141 * Initialise the Principal object from a $usr record from the DB.
142 * @param object $usr The usr record from the DB.
144 function InitialiseRecord() {
145 global $c;
147 $this->unique_tag = '"'.md5($this->username . $this->modified).'"';
148 $this->_is_group = (isset($this->type_id) && $this->type_id == 3);
150 $this->principal_address = $this->url . 'principal.vcf';
152 $this->user_address_set = array(
153 'mailto:'.$this->email,
154 $this->url,
155 // ConstructURL( '/~'.$this->username.'/', true ),
156 // ConstructURL( '/__uuids__/'.$this->username.'/', true ),
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'];
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 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);
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 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 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);
182 $this->read_proxy_group = null;
183 $this->write_proxy_group = null;
184 $this->write_proxy_for = null;
185 $this->read_proxy_for = null;
187 dbg_error_log( 'principal', ' User: %s (%d) URL: %s, By Email: %d', $this->username, $this->user_no, $this->url, $this->by_email );
192 * Split this out so we do it as infrequently as possible, given the cost.
194 function FetchProxyGroups() {
195 global $c;
197 $this->read_proxy_group = array();
198 $this->write_proxy_group = array();
199 $this->write_proxy_for = array();
200 $this->read_proxy_for = array();
202 if ( isset($c->disable_caldav_proxy) && $c->disable_caldav_proxy ) return;
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 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);
216 else {
217 $this->read_proxy_for[] = ConstructURL( '/'. $relationship->username . '/', true);
218 $this->group_membership[] = ConstructURL( '/'. $relationship->username . '/calendar-proxy-read/', true);
223 $sql = 'SELECT principal_id, username, pprivs(:request_principal::int8,principal_id,:scan_depth::int) FROM principal JOIN usr USING(user_no) WHERE principal_id IN (SELECT * from grants_proxy_access_from_p(:request_principal,:scan_depth))';
224 $qry = new AwlQuery($sql, $params ); // reuse $params assigned for earlier query
225 if ( $qry->Exec('DAVPrincipal') && $qry->rows() > 0 ) {
226 while( $relationship = $qry->Fetch() ) {
227 if ( bindec($relationship->pprivs) & $write_priv ) {
228 $this->write_proxy_group[] = ConstructURL( '/'. $relationship->username . '/', true);
230 else {
231 $this->read_proxy_group[] = ConstructURL( '/'. $relationship->username . '/', true);
235 // @dbg_error_log( 'principal', 'Read-proxy-for: %s', implode(',',$this->read_proxy_for) );
236 // @dbg_error_log( 'principal', 'Write-proxy-for: %s', implode(',',$this->write_proxy_for) );
237 // @dbg_error_log( 'principal', 'Read-proxy-group: %s', implode(',',$this->read_proxy_group) );
238 // @dbg_error_log( 'principal', 'Write-proxy-group: %s', implode(',',$this->write_proxy_group) );
243 * Accessor for the read proxy group
245 function ReadProxyGroup() {
246 if ( !isset($this->read_proxy_group) ) $this->FetchProxyGroups();
247 return $this->read_proxy_group;
252 * Accessor for the write proxy group
254 function WriteProxyGroup() {
255 if ( !isset($this->write_proxy_group) ) $this->FetchProxyGroups();
256 return $this->write_proxy_group;
261 * Accessor for read or write proxy
262 * @param string read/write - which sort of proxy list is requested.
264 function ProxyFor( $type ) {
265 if ( !isset($this->read_proxy_for) ) $this->FetchProxyGroups();
266 if ( $type == 'write' ) return $this->write_proxy_for;
267 return $this->read_proxy_for;
272 * Accessor for the group membership - the groups this principal is a member of
274 function GroupMembership() {
275 if ( !isset($this->read_proxy_group) ) $this->FetchProxyGroups();
276 return $this->group_membership;
281 * Accessor for the group member set - the members of this group
283 function GroupMemberSet() {
284 if ( ! $this->_is_group ) return null;
285 return $this->group_member_set;
290 * Is this a group principal?
291 * @return boolean Whether this is a group principal
293 function IsGroup() {
294 return $this->_is_group;
299 * Return an arbitrary property
300 * @return string The name of the arbitrary property
302 function GetProperty( $property_id ) {
304 switch( $property_id ) {
305 case 'DAV::resource-id':
306 if ( $this->exists && $this->principal_id > 0 )
307 ConstructURL('/.resources/'.$this->principal_id);
308 else
309 return null;
310 break;
313 if ( isset($this->{$property_id}) ) {
314 if ( ! is_object($this->{$property_id}) ) return $this->{$property_id};
315 return clone($this->{$property_id});
317 return null;
321 * Returns the unique_tag (ETag or getctag) for this resource
323 public function unique_tag() {
324 if ( isset($this->unique_tag) ) return $this->unique_tag;
326 if ( $this->exists !== true ) $this->unique_tag = '"-1"';
328 return $this->unique_tag;
333 * Get the calendar_home_set, as lazily as possible
335 function calendar_home_set() {
336 if ( !isset($this->calendar_home_set) ) {
337 $this->calendar_home_set = array();
338 $qry = new AwlQuery('SELECT DISTINCT parent_container FROM collection WHERE is_calendar AND dav_name ~ :dav_name_start',
339 array( ':dav_name_start' => '^'.$this->dav_name));
340 if ( $qry->Exec('principal',__LINE__,__FILE__) ) {
341 if ( $qry->rows() > 0 ) {
342 while( $calendar = $qry->Fetch() ) {
343 $this->calendar_home_set[] = ConstructURL($calendar->parent_container, true);
346 else {
347 $this->calendar_home_set[] = $this->url;
351 return $this->calendar_home_set;
356 * Get the addressbook_home_set, as lazily as possible
358 function addressbook_home_set() {
359 if ( !isset($this->addressbook_home_set) ) {
360 $this->addressbook_home_set = array();
361 $qry = new AwlQuery('SELECT DISTINCT parent_container FROM collection WHERE is_addressbook AND dav_name ~ :dav_name_start',
362 array( ':dav_name_start' => '^'.$this->dav_name));
363 if ( $qry->Exec('principal',__LINE__,__FILE__) ) {
364 if ( $qry->rows() > 0 ) {
365 while( $addressbook = $qry->Fetch() ) {
366 $this->addressbook_home_set[] = ConstructURL($addressbook->parent_container, true);
369 else {
370 $this->addressbook_home_set[] = $this->url;
374 return $this->addressbook_home_set;
379 * Get the calendar_free_busy_set, as lazily as possible
381 function calendar_free_busy_set() {
382 if ( !isset($this->calendar_free_busy_set) ) {
384 * calendar-free-busy-set has been dropped from draft 5 of the scheduling extensions for CalDAV
385 * in favour of ???
387 $this->calendar_free_busy_set = array();
388 $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',
389 array( ':dav_name_start' => '^'.$this->dav_name));
390 if ( $qry->Exec('principal',__LINE__,__FILE__) ) {
391 while( $calendar = $qry->Fetch() ) {
392 $this->calendar_free_busy_set[] = ConstructURL($calendar->dav_name, true);
396 return $this->calendar_free_busy_set;
401 * Return the privileges bits for the current session user to this resource
403 function Privileges() {
404 global $session;
405 if ( !isset($this->privileges) ) $this->privileges = 0;
406 if ( is_string($this->privileges) ) $this->privileges = bindec( $this->privileges );
407 if ( $this->_is_group ) {
408 if ( isset($session->principal) && in_array($session->principal->url(), $this->GroupMemberSet()) ) {
409 $this->privileges |= privilege_to_bits( array('DAV::read', 'DAV::read-current-user-privilege-set') );
412 return $this->privileges;
417 * Returns a representation of the principal as a collection
419 function AsCollection() {
420 $dav_name = (isset($this->original_request_url) ? DeconstructURL($this->original_request_url) : $this->dav_name());
421 $collection = (object) array(
422 'collection_id' => ($this->principal_id() ? $this->principal_id() : 0),
423 'is_calendar' => false,
424 'is_addressbook' => false,
425 'is_principal' => true,
426 'type' => 'principal' . (isset($this->original_request_url) ? '_link' : ''),
427 'user_no' => ($this->user_no() ? $this->user_no() : 0),
428 'username' => $this->username(),
429 'dav_name' => $dav_name,
430 'parent_container' => '/',
431 'email' => ($this->email()? $this->email() : ''),
432 'created' => $this->created,
433 'updated' => $this->modified,
434 'dav_etag' => substr($this->unique_tag(),1,-1),
435 'resourcetypes' => $this->resourcetypes
437 $collection->dav_displayname = (isset($this->dav_displayname) ? $this->dav_displayname : (isset($this->fullname) ? $this->fullname : $collection->username));
439 return $collection;
443 function PropertySearch( $parameters ) {
444 throw new Exception("Unimplemented!");
448 * Returns properties which are specific to this principal
450 function PrincipalProperty( $tag, $prop, &$reply, &$denied ) {
452 dbg_error_log('principal',': RenderAsXML: Principal Property "%s"', $tag );
453 switch( $tag ) {
454 case 'DAV::getcontenttype':
455 $reply->DAVElement( $prop, 'getcontenttype', 'httpd/unix-directory' );
456 break;
458 case 'DAV::resourcetype':
459 $reply->DAVElement( $prop, 'resourcetype', array( new XMLElement('principal'), new XMLElement('collection')) );
460 break;
462 case 'DAV::displayname':
463 $reply->DAVElement( $prop, 'displayname', $this->fullname );
464 break;
466 case 'DAV::principal-URL':
467 $reply->DAVElement( $prop, 'principal-URL', $reply->href($this->url()) );
468 break;
470 case 'DAV::getlastmodified':
471 $reply->DAVElement( $prop, 'getlastmodified', ISODateToHTTPDate($this->modified) );
472 break;
474 case 'DAV::creationdate':
475 $reply->DAVElement( $prop, 'creationdate', DateToISODate($this->created) );
476 break;
478 case 'DAV::getcontentlanguage':
479 /** Use the principal's locale by preference, otherwise system default */
480 $locale = (isset($c->current_locale) ? $c->current_locale : '');
481 if ( isset($this->locale) && $this->locale != '' ) $locale = $this->locale;
482 $reply->DAVElement( $prop, 'getcontentlanguage', $locale );
483 break;
485 case 'http://calendarserver.org/ns/:group-member-set':
486 case 'DAV::group-member-set':
487 if ( ! $this->_is_group ) return false;
488 $reply->DAVElement( $prop, 'group-member-set', $reply->href($this->group_member_set) );
489 break;
491 case 'http://calendarserver.org/ns/:group-membership':
492 case 'DAV::group-membership':
493 $reply->DAVElement( $prop, 'group-membership', $reply->href($this->GroupMembership()) );
494 break;
496 case 'urn:ietf:params:xml:ns:caldav:schedule-inbox-URL':
497 $reply->CalDAVElement($prop, 'schedule-inbox-URL', $reply->href($this->url('schedule-inbox')) );
498 break;
500 case 'urn:ietf:params:xml:ns:caldav:schedule-outbox-URL':
501 $reply->CalDAVElement($prop, 'schedule-outbox-URL', $reply->href($this->url('schedule-outbox')) );
502 break;
504 case 'urn:ietf:params:xml:ns:caldav:schedule-default-calendar-URL':
505 $reply->CalDAVElement($prop, 'schedule-default-calendar-URL', $reply->href($this->url('schedule-default-calendar')) );
506 break;
508 case 'http://calendarserver.org/ns/:dropbox-home-URL':
509 $reply->CalendarserverElement($prop, 'dropbox-home-URL', $reply->href($this->url('dropbox')) );
510 break;
512 case 'http://calendarserver.org/ns/:xmpp-server':
513 if ( ! isset( $this->xmpp_uri ) ) return false;
514 $reply->CalendarserverElement($prop, 'xmpp-server', $this->xmpp_server );
515 break;
517 case 'http://calendarserver.org/ns/:xmpp-uri':
518 if ( ! isset( $this->xmpp_uri ) ) return false;
519 $reply->CalendarserverElement($prop, 'xmpp-uri', $this->xmpp_uri );
520 break;
522 case 'urn:ietf:params:xml:ns:carddav:addressbook-home-set':
523 $reply->CardDAVElement($prop, $tag, $reply->href( $this->addressbook_home_set() ) );
524 break;
526 case 'urn:ietf:params:xml:ns:caldav:calendar-home-set':
527 $reply->CalDAVElement($prop, $tag, $reply->href( $this->calendar_home_set() ) );
528 break;
530 case 'urn:ietf:params:xml:ns:caldav:calendar-free-busy-set':
531 $reply->CalDAVElement( $prop, 'calendar-free-busy-set', $reply->href( $this->calendar_free_busy_set() ) );
532 break;
534 case 'urn:ietf:params:xml:ns:caldav:calendar-user-address-set':
535 $reply->CalDAVElement($prop, 'calendar-user-address-set', $reply->href($this->user_address_set) );
536 break;
538 case 'DAV::owner':
539 // After a careful reading of RFC3744 we see that this must be the principal-URL of the owner
540 $reply->DAVElement( $prop, 'owner', $reply->href( $this->url ) );
541 break;
543 // Empty tag responses.
544 case 'DAV::alternate-URI-set':
545 $reply->DAVElement( $prop, $reply->Tag($tag));
546 break;
548 case 'SOME-DENIED-PROPERTY': /** @todo indicating the style for future expansion */
549 $denied[] = $reply->Tag($tag);
550 break;
552 default:
553 return false;
554 break;
557 return true;
562 * Render XML for a single Principal (user) from the DB
564 * @param array $properties The requested properties for this principal
565 * @param reference $reply A reference to the XMLDocument being used for the reply
566 * @param boolean $props_only Default false. If true will only return the fragment with the properties, not a full response fragment.
568 * @return string An XML fragment with the requested properties for this principal
570 function RenderAsXML( $properties, &$reply, $props_only = false ) {
571 global $request;
573 dbg_error_log('principal',': RenderAsXML: Principal "%s"', $this->username );
575 $prop = new XMLElement('prop');
576 $denied = array();
577 $not_found = array();
578 foreach( $properties AS $k => $tag ) {
579 if ( ! $this->PrincipalProperty( $tag, $prop, $reply, $denied ) ) {
580 dbg_error_log( 'principal', 'Request for unsupported property "%s" of principal "%s".', $tag, $this->username );
581 $not_found[] = $reply->Tag($tag);
585 if ( $props_only ) return $prop;
587 $status = new XMLElement('status', 'HTTP/1.1 200 OK' );
589 $propstat = new XMLElement( 'propstat', array( $prop, $status) );
590 $href = $reply->href($this->url );
592 $elements = array($href,$propstat);
594 if ( count($denied) > 0 ) {
595 $status = new XMLElement('status', 'HTTP/1.1 403 Forbidden' );
596 $noprop = new XMLElement('prop');
597 foreach( $denied AS $k => $v ) {
598 $noprop->NewElement( $v );
600 $elements[] = new XMLElement( 'propstat', array( $noprop, $status) );
603 if ( count($not_found) > 0 ) {
604 $status = new XMLElement('status', 'HTTP/1.1 404 Not Found' );
605 $noprop = new XMLElement('prop');
606 foreach( $not_found AS $k => $v ) {
607 $noprop->NewElement( $v );
609 $elements[] = new XMLElement( 'propstat', array( $noprop, $status) );
612 $response = new XMLElement( 'response', $elements );
614 return $response;