12require_once(
'Principal.php');
26 private $calendar_home_set;
32 private $addressbook_home_set;
37 private $calendar_free_busy_set;
47 private $group_member_set;
52 private $group_membership;
57 private $read_proxy_for;
62 private $write_proxy_for;
67 private $read_proxy_group;
72 private $write_proxy_group;
77 private $principal_address;
85 private $user_address_set;
100 $this->exists =
null;
102 if ( $parameters ==
null )
return;
104 if ( is_object($parameters) ) {
105 dbg_error_log(
'principal',
'Principal: record for %s', $parameters->username );
106 parent::__construct(
'username',$parameters->username);
108 else if ( is_int($parameters) ) {
109 dbg_error_log(
'principal',
'Principal: %d', $parameters );
110 parent::__construct(
'principal_id',$parameters);
112 else if ( is_array($parameters) ) {
113 if ( ! isset($parameters[
'options'][
'allow_by_email']) ) $parameters[
'options'][
'allow_by_email'] =
false;
114 if ( isset($parameters[
'username']) ) {
115 parent::__construct(
'username',$parameters[
'username']);
117 else if ( isset($parameters[
'user_no']) ) {
118 parent::__construct(
'user_no',$parameters[
'user_no']);
120 else if ( isset($parameters[
'principal_id']) ) {
121 parent::__construct(
'principal_id',$parameters[
'principal_id']);
123 else if ( isset($parameters[
'email']) ) {
124 parent::__construct(
'email',$parameters[
'email']);
126 else if ( isset($parameters[
'path']) ) {
127 parent::__construct(
'path',$parameters[
'path']);
129 else if ( isset($parameters[
'principal-property-search']) ) {
130 $username = $this->PropertySearch($parameters[
'principal-property-search']);
131 parent::__construct(
'username',
$username);
135 if ( ! $this->exists )
return;
150 $this->_is_group = (isset($this->type_id) && $this->type_id == 3);
152 $this->principal_address = $this->
url .
'principal.vcf';
154 $this->user_address_set = array(
155 'mailto:'.$this->
email,
161 if ( isset ( $c->notifications_server ) ) {
162 $this->xmpp_uri =
'xmpp:pubsub.'.$c->notifications_server[
'host'].
'?pubsub;node=/davical-'.$this->principal_id;
163 $this->xmpp_server = $c->notifications_server[
'host'];
166 if ( $this->_is_group ) {
167 $this->group_member_set = array();
168 $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) );
169 if ( $qry->Exec(
'DAVPrincipal') && $qry->rows() > 0 ) {
170 while( $member = $qry->Fetch() ) {
171 $this->group_member_set[] = ConstructURL(
'/'. $member->username .
'/',
true);
176 $this->group_membership = array();
177 $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 ) );
178 if ( $qry->Exec(
'DAVPrincipal') && $qry->rows() > 0 ) {
179 while( $group = $qry->Fetch() ) {
180 $this->group_membership[] = ConstructURL(
'/'. $group->username .
'/',
true);
184 $this->read_proxy_group =
null;
185 $this->write_proxy_group =
null;
186 $this->write_proxy_for =
null;
187 $this->read_proxy_for =
null;
189 dbg_error_log(
'principal',
' User: %s (%d) URL: %s, By Email: %d', $this->
username, $this->
user_no, $this->
url, $this->by_email );
199 $this->read_proxy_group = array();
200 $this->write_proxy_group = array();
201 $this->write_proxy_for = array();
202 $this->read_proxy_for = array();
204 if ( isset($c->disable_caldav_proxy) && $c->disable_caldav_proxy )
return;
206 $write_priv = privilege_to_bits(array(
'write'));
209 $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))';
210 if ( isset($c->strict_result_ordering) && $c->strict_result_ordering ) $sql .=
" ORDER BY username";
212 $params = array(
':request_principal' => $this->
principal_id,
':scan_depth' => $c->permission_scan_depth );
213 $qry =
new AwlQuery($sql, $params);
214 if ( $qry->Exec(
'DAVPrincipal') && $qry->rows() > 0 ) {
215 while( $relationship = $qry->Fetch() ) {
216 if ( (bindec($relationship->pprivs) & $write_priv) != 0 ) {
217 $this->write_proxy_for[] = ConstructURL(
'/'. $relationship->username .
'/',
true);
218 $this->group_membership[] = ConstructURL(
'/'. $relationship->username .
'/calendar-proxy-write/',
true);
221 $this->read_proxy_for[] = ConstructURL(
'/'. $relationship->username .
'/',
true);
222 $this->group_membership[] = ConstructURL(
'/'. $relationship->username .
'/calendar-proxy-read/',
true);
230 $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 )';
231 if ( isset($c->strict_result_ordering) && $c->strict_result_ordering ) $sql .=
" ORDER BY username";
233 $qry =
new AwlQuery($sql, $params );
234 if ( $qry->Exec(
'DAVPrincipal') && $qry->rows() > 0 ) {
235 while( $relationship = $qry->Fetch() ) {
236 if ( bindec($relationship->pprivs) & $write_priv ) {
237 $this->write_proxy_group[] = ConstructURL(
'/'. $relationship->username .
'/',
true);
240 $this->read_proxy_group[] = ConstructURL(
'/'. $relationship->username .
'/',
true);
244 dbg_error_log(
'principal',
'Read-proxy-for: %s', implode(
',',$this->read_proxy_for) );
245 dbg_error_log(
'principal',
'Write-proxy-for: %s', implode(
',',$this->write_proxy_for) );
246 dbg_error_log(
'principal',
'Read-proxy-group: %s', implode(
',',$this->read_proxy_group) );
247 dbg_error_log(
'principal',
'Write-proxy-group: %s', implode(
',',$this->write_proxy_group) );
256 return $this->read_proxy_group;
265 return $this->write_proxy_group;
275 if ( $type ==
'write' )
return $this->write_proxy_for;
276 return $this->read_proxy_for;
285 return $this->group_membership;
293 if ( ! $this->_is_group )
return null;
294 return $this->group_member_set;
303 return $this->_is_group;
313 switch( $property_id ) {
314 case 'DAV::resource-id':
322 if ( isset($this->{$property_id}) ) {
323 if ( ! is_object($this->{$property_id}) )
return $this->{$property_id};
324 return clone($this->{$property_id});
333 if ( isset($this->
unique_tag) )
return $this->unique_tag;
335 if ( $this->exists !==
true ) $this->
unique_tag =
'"-1"';
337 return $this->unique_tag;
347 $qry =
new AwlQuery(
'SELECT DISTINCT parent_container FROM collection WHERE is_calendar AND dav_name ~ :dav_name_start',
348 array(
':dav_name_start' =>
'^'.$this->
dav_name));
349 if ( $qry->Exec(
'principal',__LINE__,__FILE__) ) {
350 if ( $qry->rows() > 0 ) {
351 while( $calendar = $qry->Fetch() ) {
360 return $this->calendar_home_set;
370 $qry =
new AwlQuery(
'SELECT DISTINCT parent_container FROM collection WHERE is_addressbook AND dav_name ~ :dav_name_start',
371 array(
':dav_name_start' =>
'^'.$this->
dav_name));
372 if ( $qry->Exec(
'principal',__LINE__,__FILE__) ) {
373 if ( $qry->rows() > 0 ) {
374 while( $addressbook = $qry->Fetch() ) {
383 return $this->addressbook_home_set;
397 $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',
398 array(
':dav_name_start' =>
'^' . $this->
dav_name));
399 if ($qry->Exec(
'principal', __LINE__, __FILE__)) {
400 while ($calendar = $qry->Fetch()) {
405 return $this->calendar_free_busy_set;
414 if ( !isset($this->privileges) ) $this->privileges = 0;
415 if ( is_string($this->privileges) ) $this->privileges = bindec( $this->privileges );
416 if ( $this->_is_group ) {
417 if ( isset($session->principal) && in_array($session->principal->url(), $this->GroupMemberSet()) ) {
418 $this->privileges |= privilege_to_bits( array(
'DAV::read',
'DAV::read-current-user-privilege-set') );
421 return $this->privileges;
429 $dav_name = (isset($this->original_request_url) ? DeconstructURL($this->original_request_url) : $this->
dav_name());
430 $collection = (object) array(
432 'is_calendar' =>
false,
433 'is_addressbook' =>
false,
434 'is_principal' =>
true,
435 'type' =>
'principal' . (isset($this->original_request_url) ?
'_link' :
''),
438 'dav_name' => $dav_name,
439 'parent_container' =>
'/',
440 'email' => ($this->
email()? $this->
email() :
''),
441 'created' => $this->created,
442 'updated' => $this->modified,
443 'dav_etag' => substr($this->
unique_tag(),1,-1),
444 'resourcetypes' => $this->resourcetypes
446 $collection->dav_displayname = (isset($this->dav_displayname) ? $this->dav_displayname : (isset($this->fullname) ? $this->fullname : $collection->username));
452 function PropertySearch( $parameters ) {
453 throw new Exception(
"Unimplemented!");
462 dbg_error_log(
'principal',
':PrincipalProperty: Principal Property "%s"', $tag );
464 case 'DAV::getcontenttype':
465 $reply->DAVElement( $prop,
'getcontenttype',
'httpd/unix-directory' );
468 case 'DAV::resourcetype':
469 $reply->DAVElement( $prop,
'resourcetype', array(
new XMLElement(
'principal'),
new XMLElement(
'collection')) );
472 case 'DAV::displayname':
473 $reply->DAVElement( $prop,
'displayname', $this->fullname );
476 case 'DAV::principal-URL':
477 $reply->DAVElement( $prop,
'principal-URL', $reply->href($this->url()) );
480 case 'DAV::getlastmodified':
481 $reply->DAVElement( $prop,
'getlastmodified', ISODateToHTTPDate($this->modified) );
484 case 'DAV::creationdate':
485 $reply->DAVElement( $prop,
'creationdate', DateToISODate($this->created) );
488 case 'DAV::getcontentlanguage':
490 $locale = (isset($c->current_locale) ? $c->current_locale :
'');
491 if ( isset($this->locale) && $this->locale !=
'' ) $locale = $this->locale;
492 $reply->DAVElement( $prop,
'getcontentlanguage', $locale );
495 case 'http://calendarserver.org/ns/:group-member-set':
496 case 'DAV::group-member-set':
498 if ( ! $this->_is_group )
return false;
499 $reply->DAVElement( $prop,
'group-member-set', $reply->href($this->group_member_set) );
502 case 'http://calendarserver.org/ns/:group-membership':
503 case 'DAV::group-membership':
504 $reply->DAVElement( $prop,
'group-membership', $reply->href($this->GroupMembership()) );
507 case 'urn:ietf:params:xml:ns:caldav:schedule-inbox-URL':
508 $reply->CalDAVElement($prop,
'schedule-inbox-URL', $reply->href($this->url(
'schedule-inbox')) );
511 case 'urn:ietf:params:xml:ns:caldav:schedule-outbox-URL':
512 $reply->CalDAVElement($prop,
'schedule-outbox-URL', $reply->href($this->url(
'schedule-outbox')) );
515 case 'urn:ietf:params:xml:ns:caldav:schedule-default-calendar-URL':
516 $reply->CalDAVElement($prop,
'schedule-default-calendar-URL', $reply->href($this->url(
'schedule-default-calendar')) );
519 case 'http://calendarserver.org/ns/:dropbox-home-URL':
520 $reply->CalendarserverElement($prop,
'dropbox-home-URL', $reply->href($this->url(
'dropbox')) );
523 case 'http://calendarserver.org/ns/:xmpp-server':
524 if ( ! isset( $this->xmpp_uri ) )
return false;
525 $reply->CalendarserverElement($prop,
'xmpp-server', $this->xmpp_server );
528 case 'http://calendarserver.org/ns/:xmpp-uri':
529 if ( ! isset( $this->xmpp_uri ) )
return false;
530 $reply->CalendarserverElement($prop,
'xmpp-uri', $this->xmpp_uri );
533 case 'urn:ietf:params:xml:ns:carddav:addressbook-home-set':
534 $reply->CardDAVElement($prop, $tag, $reply->href( $this->addressbook_home_set() ) );
537 case 'urn:ietf:params:xml:ns:caldav:calendar-home-set':
538 $reply->CalDAVElement($prop, $tag, $reply->href( $this->calendar_home_set() ) );
541 case 'urn:ietf:params:xml:ns:caldav:calendar-free-busy-set':
547 if ( isset($c->support_obsolete_free_busy_property) && $c->support_obsolete_free_busy_property )
548 $reply->CalDAVElement( $prop,
'calendar-free-busy-set', $reply->href( $this->calendar_free_busy_set() ) );
553 case 'urn:ietf:params:xml:ns:caldav:calendar-user-address-set':
554 $reply->CalDAVElement($prop,
'calendar-user-address-set', $reply->href($this->user_address_set));
557 case 'urn:ietf:params:xml:ns:caldav:calendar-user-type':
563 if ( isset($this->type_id) ) {
564 switch ( $this->type_id ) {
566 $type =
'INDIVIDUAL';
577 $reply->CalDAVElement($prop,
'calendar-user-type', $type);
582 $reply->DAVElement( $prop,
'owner', $reply->href( $this->url ) );
586 case 'DAV::alternate-URI-set':
587 $reply->DAVElement( $prop, $reply->Tag($tag));
590 case 'SOME-DENIED-PROPERTY':
591 $denied[] = $reply->Tag($tag);
612 function RenderAsXML( $properties, &$reply, $props_only =
false ) {
613 dbg_error_log(
'principal',
':RenderAsXML: Principal "%s"', $this->
username );
615 $prop =
new XMLElement(
'prop');
617 $not_found = array();
618 foreach( $properties AS $k => $tag ) {
620 dbg_error_log(
'principal',
'Request for unsupported property "%s" of principal "%s".', $tag, $this->
username );
621 $not_found[] = $reply->Tag($tag);
625 if ( $props_only )
return $prop;
627 $status =
new XMLElement(
'status',
'HTTP/1.1 200 OK' );
629 $propstat =
new XMLElement(
'propstat', array( $prop, $status) );
630 $href = $reply->href($this->
url );
632 $elements = array($href,$propstat);
634 if ( count($denied) > 0 ) {
635 $status =
new XMLElement(
'status',
'HTTP/1.1 403 Forbidden' );
636 $noprop =
new XMLElement(
'prop');
637 foreach( $denied AS $k => $v ) {
638 $noprop->NewElement( $v );
640 $elements[] =
new XMLElement(
'propstat', array( $noprop, $status) );
643 if ( count($not_found) > 0 ) {
644 $status =
new XMLElement(
'status',
'HTTP/1.1 404 Not Found' );
645 $noprop =
new XMLElement(
'prop');
646 foreach( $not_found AS $k => $v ) {
647 $noprop->NewElement( $v );
649 $elements[] =
new XMLElement(
'propstat', array( $noprop, $status) );
652 $response =
new XMLElement(
'response', $elements );
__construct( $parameters=null)
PrincipalProperty( $tag, $prop, &$reply, &$denied)
RenderAsXML( $properties, &$reply, $props_only=false)
GetProperty( $property_id)
url($type='principal', $internal=false)