12 require_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;
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;
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,
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 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);
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);
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 );
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'));
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);
217 $this->read_proxy_for[] = ConstructURL(
'/'. $relationship->username .
'/',
true);
218 $this->group_membership[] = ConstructURL(
'/'. $relationship->username .
'/calendar-proxy-read/',
true);
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 );
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);
234 $this->read_proxy_group[] = ConstructURL(
'/'. $relationship->username .
'/',
true);
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) );
250 return $this->read_proxy_group;
259 return $this->write_proxy_group;
269 if ( $type ==
'write' )
return $this->write_proxy_for;
270 return $this->read_proxy_for;
279 return $this->group_membership;
287 if ( ! $this->_is_group )
return null;
288 return $this->group_member_set;
297 return $this->_is_group;
307 switch( $property_id ) {
308 case 'DAV::resource-id':
316 if ( isset($this->{$property_id}) ) {
317 if ( ! is_object($this->{$property_id}) )
return $this->{$property_id};
318 return clone($this->{$property_id});
327 if ( isset($this->
unique_tag) )
return $this->unique_tag;
329 if ( $this->exists !==
true ) $this->
unique_tag =
'"-1"';
331 return $this->unique_tag;
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() ) {
354 return $this->calendar_home_set;
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() ) {
377 return $this->addressbook_home_set;
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()) {
399 return $this->calendar_free_busy_set;
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') );
415 return $this->privileges;
423 $dav_name = (isset($this->original_request_url) ? DeconstructURL($this->original_request_url) : $this->
dav_name());
424 $collection = (object) array(
426 'is_calendar' =>
false,
427 'is_addressbook' =>
false,
428 'is_principal' =>
true,
429 'type' =>
'principal' . (isset($this->original_request_url) ?
'_link' :
''),
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
440 $collection->dav_displayname = (isset($this->dav_displayname) ? $this->dav_displayname : (isset($this->fullname) ? $this->fullname : $collection->username));
446 function PropertySearch( $parameters ) {
447 throw new Exception(
"Unimplemented!");
456 dbg_error_log(
'principal',
':PrincipalProperty: Principal Property "%s"', $tag );
458 case 'DAV::getcontenttype':
459 $reply->DAVElement( $prop,
'getcontenttype',
'httpd/unix-directory' );
462 case 'DAV::resourcetype':
463 $reply->DAVElement( $prop,
'resourcetype', array(
new XMLElement(
'principal'),
new XMLElement(
'collection')) );
466 case 'DAV::displayname':
467 $reply->DAVElement( $prop,
'displayname', $this->fullname );
470 case 'DAV::principal-URL':
471 $reply->DAVElement( $prop,
'principal-URL', $reply->href($this->url()) );
474 case 'DAV::getlastmodified':
475 $reply->DAVElement( $prop,
'getlastmodified', ISODateToHTTPDate($this->modified) );
478 case 'DAV::creationdate':
479 $reply->DAVElement( $prop,
'creationdate', DateToISODate($this->created) );
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 );
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) );
496 case 'http://calendarserver.org/ns/:group-membership':
497 case 'DAV::group-membership':
498 $reply->DAVElement( $prop,
'group-membership', $reply->href($this->GroupMembership()) );
501 case 'urn:ietf:params:xml:ns:caldav:schedule-inbox-URL':
502 $reply->CalDAVElement($prop,
'schedule-inbox-URL', $reply->href($this->url(
'schedule-inbox')) );
505 case 'urn:ietf:params:xml:ns:caldav:schedule-outbox-URL':
506 $reply->CalDAVElement($prop,
'schedule-outbox-URL', $reply->href($this->url(
'schedule-outbox')) );
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')) );
513 case 'http://calendarserver.org/ns/:dropbox-home-URL':
514 $reply->CalendarserverElement($prop,
'dropbox-home-URL', $reply->href($this->url(
'dropbox')) );
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 );
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 );
527 case 'urn:ietf:params:xml:ns:carddav:addressbook-home-set':
528 $reply->CardDAVElement($prop, $tag, $reply->href( $this->addressbook_home_set() ) );
531 case 'urn:ietf:params:xml:ns:caldav:calendar-home-set':
532 $reply->CalDAVElement($prop, $tag, $reply->href( $this->calendar_home_set() ) );
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() ) );
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));
551 case 'urn:ietf:params:xml:ns:caldav:calendar-user-type':
557 if ( isset($this->type_id) ) {
558 switch ( $this->type_id ) {
560 $type =
'INDIVIDUAL';
571 $reply->CalDAVElement($prop,
'calendar-user-type', $type);
576 $reply->DAVElement( $prop,
'owner', $reply->href( $this->url ) );
580 case 'DAV::alternate-URI-set':
581 $reply->DAVElement( $prop, $reply->Tag($tag));
584 case 'SOME-DENIED-PROPERTY':
585 $denied[] = $reply->Tag($tag);
606 function RenderAsXML( $properties, &$reply, $props_only =
false ) {
607 dbg_error_log(
'principal',
':RenderAsXML: Principal "%s"', $this->
username );
609 $prop =
new XMLElement(
'prop');
611 $not_found = array();
612 foreach( $properties AS $k => $tag ) {
614 dbg_error_log(
'principal',
'Request for unsupported property "%s" of principal "%s".', $tag, $this->
username );
615 $not_found[] = $reply->Tag($tag);
619 if ( $props_only )
return $prop;
621 $status =
new XMLElement(
'status',
'HTTP/1.1 200 OK' );
623 $propstat =
new XMLElement(
'propstat', array( $prop, $status) );
624 $href = $reply->href($this->
url );
626 $elements = array($href,$propstat);
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 );
634 $elements[] =
new XMLElement(
'propstat', array( $noprop, $status) );
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 );
643 $elements[] =
new XMLElement(
'propstat', array( $noprop, $status) );
646 $response =
new XMLElement(
'response', $elements );