source: contrib/davical/inc/CalDAVPrincipal.php @ 3733

Revision 3733, 24.5 KB checked in by gabriel.malheiros, 13 years ago (diff)

Ticket #1541 - <Davical customizado para o Expresso.Utiliza Caldav e CardDav?>

Line 
1<?php
2/**
3* An object representing a DAV 'Principal'
4*
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
10*/
11
12/**
13* @var $_CalDAVPrincipalCache
14* A global variable holding a cache of any DAV Principals which are
15* read from the DB.
16*/
17$_CalDAVPrincipalCache = (object) array( 'p' => array(), 'u' => array() );
18
19
20/**
21* A class for things to do with a DAV Principal
22*
23* @package   davical
24*/
25class CalDAVPrincipal
26{
27  /**
28  * @var The home URL of the principal
29  */
30  var $url;
31
32  /**
33  * @var Identifies whether this principal exists in the DB yet
34  */
35  private $exists;
36
37  /**
38  * @var RFC4791: Identifies the URL(s) of any WebDAV collections that contain
39  * calendar collections owned by the associated principal resource.
40  */
41  private $calendar_home_set;
42
43  /**
44  * @var CardDAV: Identifies the URL(s) of any WebDAV collections that contain
45  * addressbook collections owned by the associated principal resource.
46  */
47  private $addressbook_home_set;
48
49  /**
50  * @var Obsolete: Identifies the URL(s) of any calendars participating in free/busy
51  */
52  private $calendar_free_busy_set;
53
54  /**
55  * @var draft-desruisseaux-caldav-sched-03: Identify the URL of the scheduling
56  * Inbox collection owned by the associated principal resource.
57  */
58  var $schedule_inbox_url;
59
60  /**
61  * @var draft-desruisseaux-caldav-sched-03: Identify the URL of the scheduling
62  * Outbox collection owned by the associated principal resource.
63  */
64  var $schedule_outbox_url;
65
66  /**
67  * @var Whether or not we are using an e-mail address based URL.
68  */
69  var $by_email;
70
71  /**
72  * @var RFC3744: The principals that are direct members of this group.
73  */
74  protected $_is_group;
75
76  /**
77  * @var RFC3744: The principals that are direct members of this group.
78  */
79  protected $group_member_set;
80
81  /**
82  * @var RFC3744: The groups in which the principal is directly a member.
83  */
84  protected $group_membership;
85
86  /**
87   * @var caldav-cu-proxy-02: The principals which this one has read permissions on.
88   */
89  protected $read_proxy_for;
90
91  /**
92   * @var caldav-cu-proxy-02: The principals which this one has read-write prmissions for.
93   */
94  protected $write_proxy_for;
95
96   /**
97   * @var caldav-cu-proxy-02: The principals which have read permissions on this one.
98   */
99  protected $read_proxy_group;
100
101  /**
102   * @var caldav-cu-proxy-02: The principals which have write permissions on this one.
103   */
104  protected $write_proxy_group;
105
106  /**
107   * @var CardDAV: The URL to an addressbook entry for this principal
108   */
109  protected $principal_address;
110
111  /**
112   * @var The username for this principal
113   */
114  protected $username;
115
116  /**
117   * @var The dav_name for this principal - a partial path
118   */
119  protected $dav_name;
120
121  /**
122  * Constructor
123  * @param mixed $parameters If null, an empty Principal is created.  If it
124  *              is an integer then that ID is read (if possible).  If it is
125  *              an array then the Principal matching the supplied elements
126  *              is read.  If it is an object then it is expected to be a 'usr'
127  *              record that was read elsewhere.
128  *
129  * @return boolean Whether we actually read data from the DB to initialise the record.
130  */
131  function __construct( $parameters = null ) {
132    global $session, $c;
133
134    $this->exists = null;
135    $this->url = null;
136
137    if ( $parameters == null ) return false;
138    $this->by_email = false;
139    if ( is_object($parameters) ) {
140      dbg_error_log( 'principal', 'Principal: record for %s', $parameters->username );
141      $usr = $parameters;
142    }
143    else if ( is_int($parameters) ) {
144      dbg_error_log( 'principal', 'Principal: %d', $parameters );
145      $usr = getUserByID($parameters);
146      $this->user_no = $parameters['user_no'];
147    }
148    else if ( is_array($parameters) ) {
149      if ( ! isset($parameters['options']['allow_by_email']) ) $parameters['options']['allow_by_email'] = false;
150      if ( isset($parameters['username']) ) {
151        $usr = getUserByName($parameters['username']);
152        $this->username = $parameters['username'];
153      }
154      else if ( isset($parameters['user_no']) ) {
155        $usr = getUserByID($parameters['user_no']);
156        $this->user_no = $parameters['user_no'];
157      }
158      else if ( isset($parameters['email']) && $parameters['options']['allow_by_email'] ) {
159        if ( $username = $this->UsernameFromEMail($parameters['email']) ) {
160          $usr = getUserByName($username);
161          $this->username = $username;
162        }
163      }
164      else if ( isset($parameters['path']) ) {
165        dbg_error_log( 'principal', 'Finding Principal from path: "%s", options.allow_by_email: "%s"', $parameters['path'], $parameters['options']['allow_by_email'] );
166        if ( $username = $this->UsernameFromPath($parameters['path'], $parameters['options']) ) {
167          $usr = getUserByName($username);
168          $this->username = $username;
169        }
170      }
171      else if ( isset($parameters['principal-property-search']) ) {
172        $usr = $this->PropertySearch($parameters['principal-property-search']);
173      }
174    }
175    if ( !isset($usr) || !is_object($usr) ) {
176      $this->exists = false;
177      return false;
178    }
179    $this->exists = true;
180    $this->InitialiseRecord($usr);
181
182    if ( is_array($parameters) && !isset($parameters['username']) && !isset($parameters['user_no'])
183                 && isset($parameters['path']) && preg_match('{^/(~|principals/)}', $parameters['path']) ) {
184      // Force it to match
185      $this->url = $parameters['path'];
186      $this->dav_name = $parameters['path'];
187    }
188  }
189
190
191  /**
192  * Initialise the Principal object from a $usr record from the DB.
193  * @param object $usr The usr record from the DB.
194  */
195  function InitialiseRecord($usr) {
196    global $c;
197    foreach( $usr AS $k => $v ) {
198      $this->{$k} = $v;
199    }
200    if ( !isset($this->modified) ) $this->modified = $this->updated;
201    if ( !isset($this->created) )  $this->created  = $this->joined;
202
203    $this->dav_etag = md5($this->username . $this->updated);
204
205    $this->_is_group = (isset($usr->type_id) && $usr->type_id == 3);
206
207    $this->principal_url = ConstructURL( '/'.$this->username.'/', true );
208    $this->url = $this->principal_url;
209
210    $this->principal_address = $this->principal_url . 'principal.vcf';
211
212    $this->user_address_set = array(
213       'mailto:'.$this->email,
214       ConstructURL( '/'.$this->username.'/', true ),
215//       ConstructURL( '/~'.$this->username.'/', true ),
216//       ConstructURL( '/__uuids__/'.$this->username.'/', true ),
217    );
218    $this->schedule_inbox_url = sprintf( '%s.in/', $this->url);
219    $this->schedule_outbox_url = sprintf( '%s.out/', $this->url);
220    $this->dropbox_url = sprintf( '%s.drop/', $this->url);
221    $this->notifications_url = sprintf( '%s.notify/', $this->url);
222
223    if ( isset ( $c->notifications_server ) ) {
224      $this->xmpp_uri = 'xmpp:pubsub.'.$c->notifications_server['host'].'?pubsub;node=/davical-'.$this->principal_id;
225      $this->xmpp_server = $c->notifications_server['host'];
226    }
227
228    if ( $this->_is_group ) {
229      $this->group_member_set = array();
230      $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) );
231      if ( $qry->Exec('CalDAVPrincipal') && $qry->rows() > 0 ) {
232        while( $member = $qry->Fetch() ) {
233          $this->group_member_set[] = ConstructURL( '/'. $member->username . '/', true);
234        }
235      }
236    }
237
238    $this->group_membership = array();
239    $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 ) );
240    if ( $qry->Exec('CalDAVPrincipal') && $qry->rows() > 0 ) {
241      while( $group = $qry->Fetch() ) {
242        $this->group_membership[] = ConstructURL( '/'. $group->username . '/', true);
243      }
244    }
245
246    $this->read_proxy_group = null;
247    $this->write_proxy_group = null;
248    $this->write_proxy_for = null;
249    $this->read_proxy_for = null;
250
251    dbg_error_log( 'principal', ' User: %s (%d) URL: %s, Home: %s, By Email: %d', $this->username, $this->user_no, $this->url, $this->by_email );
252  }
253
254
255  /**
256  * Split this out so we do it as infrequently as possible, given the cost.
257  */
258  function FetchProxyGroups() {
259    global $c;
260
261    $this->read_proxy_group = array();
262    $this->write_proxy_group = array();
263    $this->write_proxy_for = array();
264    $this->read_proxy_for = array();
265
266    if ( !isset($c->disable_caldav_proxy) || $c->disable_caldav_proxy === false ) {
267
268      $write_priv = privilege_to_bits(array('write'));
269      // whom are we a proxy for? who is a proxy for us?
270      // (as per Caldav Proxy section 5.1 Paragraph 7 and 5)
271      $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))';
272      $params = array( ':request_principal' => $this->principal_id, ':scan_depth' => $c->permission_scan_depth );
273      $qry = new AwlQuery($sql, $params);
274      if ( $qry->Exec('CalDAVPrincipal') && $qry->rows() > 0 ) {
275        while( $relationship = $qry->Fetch() ) {
276          if ( (bindec($relationship->pprivs) & $write_priv) != 0 ) {
277            $this->write_proxy_for[] = ConstructURL( '/'. $relationship->username . '/', true);
278            $this->group_membership[] = ConstructURL( '/'. $relationship->username . '/calendar-proxy-write/', true);
279          }
280          else {
281            $this->read_proxy_for[] = ConstructURL( '/'. $relationship->username . '/', true);
282            $this->group_membership[] = ConstructURL( '/'. $relationship->username . '/calendar-proxy-read/', true);
283          }
284        }
285      }
286
287      $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))';
288      $qry = new AwlQuery($sql, $params ); // reuse $params assigned for earlier query
289      if ( $qry->Exec('CalDAVPrincipal') && $qry->rows() > 0 ) {
290        while( $relationship = $qry->Fetch() ) {
291          if ( bindec($relationship->pprivs) & $write_priv ) {
292            $this->write_proxy_group[] = ConstructURL( '/'. $relationship->username . '/', true);
293          }
294          else {
295            $this->read_proxy_group[] = ConstructURL( '/'. $relationship->username . '/', true);
296          }
297        }
298      }
299//      @dbg_error_log( 'principal', 'Read-proxy-for:    %s', implode(',',$this->read_proxy_for) );
300//      @dbg_error_log( 'principal', 'Write-proxy-for:   %s', implode(',',$this->write_proxy_for) );
301//      @dbg_error_log( 'principal', 'Read-proxy-group:  %s', implode(',',$this->read_proxy_group) );
302//      @dbg_error_log( 'principal', 'Write-proxy-group: %s', implode(',',$this->write_proxy_group) );
303    }
304  }
305
306
307  /**
308  * Accessor for the read proxy group
309  */
310  function ReadProxyGroup() {
311    if ( !isset($this->read_proxy_group) ) $this->FetchProxyGroups();
312    return $this->read_proxy_group;
313  }
314
315
316  /**
317  * Accessor for the write proxy group
318  */
319  function WriteProxyGroup() {
320    if ( !isset($this->write_proxy_group) ) $this->FetchProxyGroups();
321    return $this->write_proxy_group;
322  }
323
324
325  /**
326  * Accessor for read or write proxy
327  * @param string read/write - which sort of proxy list is requested.
328  */
329  function ProxyFor( $type ) {
330    if ( !isset($this->read_proxy_for) ) $this->FetchProxyGroups();
331    if ( $type == 'write' ) return $this->write_proxy_for;
332    return $this->read_proxy_for;
333  }
334
335
336  /**
337  * Accessor for the group membership - the groups this principal is a member of
338  */
339  function GroupMembership() {
340    if ( !isset($this->read_proxy_group) ) $this->FetchProxyGroups();
341    return $this->group_membership;
342  }
343
344
345  /**
346  * Accessor for the group member set - the members of this group
347  */
348  function GroupMemberSet() {
349    if ( ! $this->_is_group ) return null;
350    return $this->group_member_set;
351  }
352
353
354  /**
355  * Work out the username, based on elements of the path.
356  * @param string $path The path to be used.
357  * @param array $options The request options, controlling whether e-mail paths are allowed.
358  */
359  function UsernameFromPath( $path, $options = null ) {
360    global $session, $c;
361
362    if ( $path == '/' || $path == '' ) {
363      dbg_error_log( 'principal', 'No useful path split possible' );
364      return $session->username;
365    }
366
367    $path_split = explode('/', $path );
368    @dbg_error_log( 'principal', 'Path split into at least /// %s /// %s /// %s', $path_split[1], $path_split[2], $path_split[3] );
369
370    $username = $path_split[1];
371    if ( $path_split[1] == 'principals' && isset($path_split[3]) ) $username = $path_split[3];
372    if ( substr($username,0,1) == '~' ) $username = substr($username,1);
373
374    if ( isset($options['allow_by_email']) && $options['allow_by_email'] && preg_match( '#^(\S+@\S+[.]\S+)$#', $username) ) {
375      $username = $this->UsernameFromEMail($username);
376    }
377    return $username;
378  }
379
380
381  /**
382  * Work out the username, based on the given e-mail
383  * @param string $email The email address to be used.
384  */
385  function UsernameFromEMail( $email ) {
386    @dbg_error_log( 'principal', 'Retrieving username from e-mail address "%s" ', $email );
387    $qry = new AwlQuery('SELECT username FROM usr WHERE email = :email', array( ':email' => $email ) );
388    if ( $qry->Exec('principal') && $user = $qry->Fetch() ) {
389      $username = $user->username;
390      $this->by_email = true;
391      return $username;
392    }
393    return null;
394  }
395
396
397  /**
398  * Does this principal exist?
399  * @return boolean Whether or not it exists.
400  */
401  function Exists() {
402    return $this->exists;
403  }
404
405
406  /**
407  * Is this a group principal?
408  * @return boolean Whether this is a group principal
409  */
410  function IsGroup() {
411    return $this->_is_group;
412  }
413
414
415  /**
416  * Return the username
417  * @return string The username
418  */
419  function username() {
420    return (isset($this->username)?$this->username:'username not set');
421  }
422
423
424  /**
425  * Return the partial path representing this principal
426  * @return string The dav_name
427  */
428  function dav_name() {
429    if ( !isset($this->dav_name) ) {
430      if ( !isset($this->username) ) $this->dav_name = '';
431      else $this->dav_name = '/'.$this->username.'/';
432    }
433    return $this->dav_name;
434  }
435
436
437  /**
438  * Get the calendar_home_set, as lazily as possible
439  */
440  function calendar_home_set() {
441    if ( !isset($this->calendar_home_set) ) {
442      $this->calendar_home_set = array();
443/*      $qry = new AwlQuery('SELECT DISTINCT parent_container FROM collection WHERE is_calendar AND user_no = :user_no', array( ':user_no' => $this->user_no));
444      if ( $qry->Exec('principal',__LINE__,__FILE__) ) {
445        if ( $qry->rows() > 0 ) {
446          while( $calendar = $qry->Fetch() ) {
447            $this->calendar_home_set[] = ConstructURL($calendar->parent_container, true);
448          }
449        }
450        else {*/
451          $this->calendar_home_set[] = $this->principal_url;
452//         }
453//       }
454    }
455    return $this->calendar_home_set;
456  }
457
458
459  /**
460  * Get the addressbook_home_set, as lazily as possible
461  */
462  function addressbook_home_set() {
463    if ( !isset($this->addressbook_home_set) ) {
464     $this->addressbook_home_set = array();
465/*      $qry = new AwlQuery('SELECT DISTINCT parent_container FROM collection WHERE is_addressbook AND user_no = :user_no', array( ':user_no' => $this->user_no));
466      if ( $qry->Exec('principal',__LINE__,__FILE__) ) {
467        if ( $qry->rows() > 0 ) {
468          while( $addressbook = $qry->Fetch() ) {
469            $this->addressbook_home_set[] = ConstructURL($addressbook->parent_container, true);
470          }
471        }
472        else {*/
473          $this->addressbook_home_set[] = $this->principal_url;
474//         }
475//       }
476    }
477    return $this->addressbook_home_set;
478  }
479
480
481  /**
482  * Get the calendar_free_busy_set, as lazily as possible
483  */
484  function calendar_free_busy_set() {
485    if ( !isset($this->calendar_free_busy_set) ) {
486      /**
487      * calendar-free-busy-set has been dropped from draft 5 of the scheduling extensions for CalDAV
488      * in favour of
489      */
490      $this->calendar_free_busy_set = array();
491      $qry = new AwlQuery('SELECT dav_name FROM collection WHERE user_no = :user_no AND is_calendar AND (schedule_transp = \'opaque\' OR schedule_transp IS NULL) ORDER BY user_no, collection_id',
492                        array( ':user_no' => $this->user_no) );
493      if ( $qry->Exec('principal',__LINE__,__FILE__) ) {
494        while( $calendar = $qry->Fetch() ) {
495          $this->calendar_free_busy_set[] = ConstructURL($calendar->dav_name, true);
496        }
497      }
498    }
499    return $this->calendar_free_busy_set;
500  }
501
502  /**
503  * Return the privileges bits for the current session user to this resource
504  */
505  function Privileges() {
506    if ( !isset($this->privileges) ) $this->privileges = 0;
507    if ( is_string($this->privileges) ) $this->privileges = bindec( $this->privileges );
508    return $this->privileges;
509  }
510
511
512  /**
513  * Returns a representation of the principal as a collection
514  */
515  function AsCollection() {
516    $collection = (object) array(
517                            'collection_id' => (isset($this->principal_id) ? $this->principal_id : 0),
518                            'is_calendar' => false,
519                            'is_addressbook' => false,
520                            'is_principal' => true,
521                            'type'     => 'principal' . (substr($this->dav_name(), 0, 12) == '/principals/'?'_link':''),
522                            'user_no'  => (isset($this->user_no)  ? $this->user_no : 0),
523                            'username' => $this->username(),
524                            'dav_name' => $this->dav_name,
525                            'parent_container' => '/',
526                            'email'    => (isset($this->email)    ? $this->email : ''),
527                            'created'  => (isset($this->created)  ? $this->created : date('Ymd\THis')),
528                            'updated'  => (isset($this->updated)  ? $this->updated : date('Ymd\THis'))
529                  );
530    $collection->dav_etag = (isset($this->dav_etag) ? $this->dav_etag : md5($collection->username . $collection->updated));
531    $collection->dav_displayname =  (isset($this->dav_displayname) ? $this->dav_displayname : (isset($this->fullname) ? $this->fullname : $collection->username));
532
533    return $collection;
534  }
535
536  /**
537  * Returns properties which are specific to this principal
538  */
539  function PrincipalProperty( $tag, $prop, &$reply, &$denied ) {
540
541    dbg_error_log('principal',': RenderAsXML: Principal Property "%s"', $tag );
542    switch( $tag ) {
543      case 'DAV::getcontenttype':
544        $prop->NewElement('getcontenttype', 'httpd/unix-directory' );
545        break;
546
547      case 'DAV::resourcetype':
548        $prop->NewElement('resourcetype', array( new XMLElement('principal'), new XMLElement('collection')) );
549        break;
550
551      case 'DAV::displayname':
552        $prop->NewElement('displayname', $this->fullname );
553        break;
554
555      case 'DAV::principal-URL':
556        $prop->NewElement('principal-URL', $reply->href($this->principal_url) );
557        break;
558
559      case 'DAV::getlastmodified':
560        $prop->NewElement('getlastmodified', ISODateToHTTPDate($this->modified) );
561        break;
562
563      case 'DAV::creationdate':
564        $prop->NewElement('creationdate', DateToISODate($this->created) );
565        break;
566
567      case 'DAV::getcontentlanguage':
568        /** Use the principal's locale by preference, otherwise system default */
569        $locale = (isset($c->current_locale) ? $c->current_locale : '');
570        if ( isset($this->locale) && $this->locale != '' ) $locale = $this->locale;
571        $prop->NewElement('getcontentlanguage', $locale );
572        break;
573
574      case 'DAV::group-member-set':
575        if ( ! $this->_is_group ) return false;
576        $prop->NewElement('group-member-set', $reply->href($this->group_member_set) );
577        break;
578
579      case 'DAV::group-membership':
580        $prop->NewElement('group-membership', $reply->href($this->GroupMembership()) );
581        break;
582
583      case 'urn:ietf:params:xml:ns:caldav:schedule-inbox-URL':
584        $reply->CalDAVElement($prop, 'schedule-inbox-URL', $reply->href($this->schedule_inbox_url) );
585        break;
586
587      case 'urn:ietf:params:xml:ns:caldav:schedule-outbox-URL':
588        $reply->CalDAVElement($prop, 'schedule-outbox-URL', $reply->href($this->schedule_outbox_url) );
589        break;
590
591      case 'http://calendarserver.org/ns/:dropbox-home-URL':
592        $reply->CalendarserverElement($prop, 'dropbox-home-URL', $reply->href($this->dropbox_url) );
593        break;
594
595      case 'http://calendarserver.org/ns/:xmpp-server':
596        if ( ! isset( $this->xmpp_uri ) ) return false;
597        $reply->CalendarserverElement($prop, 'xmpp-server', $this->xmpp_server );
598        break;
599
600      case 'http://calendarserver.org/ns/:xmpp-uri':
601        if ( ! isset( $this->xmpp_uri ) ) return false;
602        $reply->CalendarserverElement($prop, 'xmpp-uri', $this->xmpp_uri );
603        break;
604
605      case 'urn:ietf:params:xml:ns:carddav:addressbook-home-set':
606        $reply->NSElement($prop, $tag, $reply->href( $this->addressbook_home_set() ) );
607        break;
608
609      case 'urn:ietf:params:xml:ns:caldav:calendar-home-set':
610        $reply->NSElement($prop, $tag, $reply->href( $this->calendar_home_set() ) );
611        break;
612
613      case 'urn:ietf:params:xml:ns:caldav:calendar-free-busy-set':
614        $reply->CalDAVElement( $prop, 'calendar-free-busy-set', $reply->href( $this->calendar_free_busy_set() ) );
615        break;
616
617      case 'urn:ietf:params:xml:ns:caldav:calendar-user-address-set':
618        $reply->CalDAVElement($prop, 'calendar-user-address-set', $reply->href($this->user_address_set) );
619        break;
620
621      case 'DAV::owner':
622        // After a careful reading of RFC3744 we see that this must be the principal-URL of the owner
623        $reply->DAVElement( $prop, 'owner', $reply->href( $this->principal_url ) );
624        break;
625
626      case 'DAV::principal-collection-set':
627        $reply->DAVElement( $prop, 'principal-collection-set', $reply->href( ConstructURL('/') ) );
628        break;
629
630      // Empty tag responses.
631      case 'DAV::alternate-URI-set':
632        $prop->NewElement( $reply->Tag($tag));
633        break;
634
635      case 'SOME-DENIED-PROPERTY':  /** @todo indicating the style for future expansion */
636        $denied[] = $reply->Tag($tag);
637        break;
638
639      default:
640        return false;
641        break;
642    }
643
644    return true;
645  }
646
647
648  /**
649  * Render XML for a single Principal (user) from the DB
650  *
651  * @param array $properties The requested properties for this principal
652  * @param reference $reply A reference to the XMLDocument being used for the reply
653  * @param boolean $props_only Default false.  If true will only return the fragment with the properties, not a full response fragment.
654  *
655  * @return string An XML fragment with the requested properties for this principal
656  */
657  function RenderAsXML( $properties, &$reply, $props_only = false ) {
658    global $request;
659
660    dbg_error_log('principal',': RenderAsXML: Principal "%s"', $this->username );
661
662    $prop = new XMLElement('prop');
663    $denied = array();
664    $not_found = array();
665    foreach( $properties AS $k => $tag ) {
666      if ( ! $this->PrincipalProperty( $tag, $prop, $reply, $denied ) ) {
667        dbg_error_log( 'principal', 'Request for unsupported property "%s" of principal "%s".', $tag, $this->username );
668        $not_found[] = $reply->Tag($tag);
669      }
670    }
671
672    if ( $props_only ) return $prop;
673
674    $status = new XMLElement('status', 'HTTP/1.1 200 OK' );
675
676    $propstat = new XMLElement( 'propstat', array( $prop, $status) );
677    $href = $reply->href($this->url );
678
679    $elements = array($href,$propstat);
680
681    if ( count($denied) > 0 ) {
682      $status = new XMLElement('status', 'HTTP/1.1 403 Forbidden' );
683      $noprop = new XMLElement('prop');
684      foreach( $denied AS $k => $v ) {
685        $noprop->NewElement( $v );
686      }
687      $elements[] = new XMLElement( 'propstat', array( $noprop, $status) );
688    }
689
690    if ( count($not_found) > 0 ) {
691      $status = new XMLElement('status', 'HTTP/1.1 404 Not Found' );
692      $noprop = new XMLElement('prop');
693      foreach( $not_found AS $k => $v ) {
694        $noprop->NewElement( $v );
695      }
696      $elements[] = new XMLElement( 'propstat', array( $noprop, $status) );
697    }
698
699    $response = new XMLElement( 'response', $elements );
700
701    return $response;
702  }
703
704}
Note: See TracBrowser for help on using the repository browser.