Loading...
Searching...
No Matches
DAVPrincipal.php
<?php
require_once('Principal.php');
class DAVPrincipal extends Principal
{
private $calendar_home_set;
private $addressbook_home_set;
private $calendar_free_busy_set;
protected $_is_group;
private $group_member_set;
private $group_membership;
private $read_proxy_for;
private $write_proxy_for;
private $read_proxy_group;
private $write_proxy_group;
private $principal_address;
private $unique_tag;
private $user_address_set;
function __construct( $parameters = null ) {
global $session, $c;
$this->exists = null;
if ( $parameters == null ) return;
if ( is_object($parameters) ) {
dbg_error_log( 'principal', 'Principal: record for %s', $parameters->username );
parent::__construct('username',$parameters->username);
}
else if ( is_int($parameters) ) {
dbg_error_log( 'principal', 'Principal: %d', $parameters );
parent::__construct('principal_id',$parameters);
}
else if ( is_array($parameters) ) {
if ( ! isset($parameters['options']['allow_by_email']) ) $parameters['options']['allow_by_email'] = false;
if ( isset($parameters['username']) ) {
parent::__construct('username',$parameters['username']);
}
else if ( isset($parameters['user_no']) ) {
parent::__construct('user_no',$parameters['user_no']);
}
else if ( isset($parameters['principal_id']) ) {
parent::__construct('principal_id',$parameters['principal_id']);
}
else if ( isset($parameters['email']) ) {
parent::__construct('email',$parameters['email']);
}
else if ( isset($parameters['path']) ) {
parent::__construct('path',$parameters['path']);
}
else if ( isset($parameters['principal-property-search']) ) {
$username = $this->PropertySearch($parameters['principal-property-search']);
parent::__construct('username',$username);
}
}
if ( ! $this->exists ) return;
$this->InitialiseRecord();
}
function InitialiseRecord() {
global $c;
$this->unique_tag = '"'.md5($this->username . $this->modified).'"';
$this->_is_group = (isset($this->type_id) && $this->type_id == 3);
$this->principal_address = $this->url . 'principal.vcf';
$this->user_address_set = array(
'mailto:'.$this->email,
$this->url,
// ConstructURL( '/~'.$this->username.'/', true ),
// ConstructURL( '/__uuids__/'.$this->username.'/', true ),
);
if ( isset ( $c->notifications_server ) ) {
$this->xmpp_uri = 'xmpp:pubsub.'.$c->notifications_server['host'].'?pubsub;node=/davical-'.$this->principal_id;
$this->xmpp_server = $c->notifications_server['host'];
}
if ( $this->_is_group ) {
$this->group_member_set = array();
$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) );
if ( $qry->Exec('DAVPrincipal') && $qry->rows() > 0 ) {
while( $member = $qry->Fetch() ) {
$this->group_member_set[] = ConstructURL( '/'. $member->username . '/', true);
}
}
}
$this->group_membership = array();
$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 ) );
if ( $qry->Exec('DAVPrincipal') && $qry->rows() > 0 ) {
while( $group = $qry->Fetch() ) {
$this->group_membership[] = ConstructURL( '/'. $group->username . '/', true);
}
}
$this->read_proxy_group = null;
$this->write_proxy_group = null;
$this->write_proxy_for = null;
$this->read_proxy_for = null;
dbg_error_log( 'principal', ' User: %s (%d) URL: %s, By Email: %d', $this->username, $this->user_no, $this->url, $this->by_email );
}
function FetchProxyGroups() {
global $c;
$this->read_proxy_group = array();
$this->write_proxy_group = array();
$this->write_proxy_for = array();
$this->read_proxy_for = array();
if ( isset($c->disable_caldav_proxy) && $c->disable_caldav_proxy ) return;
$write_priv = privilege_to_bits(array('write'));
// whom are we a proxy for? who is a proxy for us?
// (as per Caldav Proxy section 5.1 Paragraph 7 and 5)
$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))';
if ( isset($c->strict_result_ordering) && $c->strict_result_ordering ) $sql .= " ORDER BY username";
$params = array( ':request_principal' => $this->principal_id, ':scan_depth' => $c->permission_scan_depth );
$qry = new AwlQuery($sql, $params);
if ( $qry->Exec('DAVPrincipal') && $qry->rows() > 0 ) {
while( $relationship = $qry->Fetch() ) {
if ( (bindec($relationship->pprivs) & $write_priv) != 0 ) {
$this->write_proxy_for[] = ConstructURL( '/'. $relationship->username . '/', true);
$this->group_membership[] = ConstructURL( '/'. $relationship->username . '/calendar-proxy-write/', true);
}
else {
$this->read_proxy_for[] = ConstructURL( '/'. $relationship->username . '/', true);
$this->group_membership[] = ConstructURL( '/'. $relationship->username . '/calendar-proxy-read/', true);
}
}
}
/* grants_proxy_access_from_p() is too clever and doesn't return any results, so do it on foot
$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))';
*/
$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 )';
if ( isset($c->strict_result_ordering) && $c->strict_result_ordering ) $sql .= " ORDER BY username";
$qry = new AwlQuery($sql, $params ); // reuse $params assigned for earlier query
if ( $qry->Exec('DAVPrincipal') && $qry->rows() > 0 ) {
while( $relationship = $qry->Fetch() ) {
if ( bindec($relationship->pprivs) & $write_priv ) {
$this->write_proxy_group[] = ConstructURL( '/'. $relationship->username . '/', true);
}
else {
$this->read_proxy_group[] = ConstructURL( '/'. $relationship->username . '/', true);
}
}
}
dbg_error_log( 'principal', 'Read-proxy-for: %s', implode(',',$this->read_proxy_for) );
dbg_error_log( 'principal', 'Write-proxy-for: %s', implode(',',$this->write_proxy_for) );
dbg_error_log( 'principal', 'Read-proxy-group: %s', implode(',',$this->read_proxy_group) );
dbg_error_log( 'principal', 'Write-proxy-group: %s', implode(',',$this->write_proxy_group) );
}
function ReadProxyGroup() {
if ( !isset($this->read_proxy_group) ) $this->FetchProxyGroups();
return $this->read_proxy_group;
}
function WriteProxyGroup() {
if ( !isset($this->write_proxy_group) ) $this->FetchProxyGroups();
return $this->write_proxy_group;
}
function ProxyFor( $type ) {
if ( !isset($this->read_proxy_for) ) $this->FetchProxyGroups();
if ( $type == 'write' ) return $this->write_proxy_for;
return $this->read_proxy_for;
}
function GroupMembership() {
if ( !isset($this->read_proxy_group) ) $this->FetchProxyGroups();
return $this->group_membership;
}
function GroupMemberSet() {
if ( ! $this->_is_group ) return null;
return $this->group_member_set;
}
function IsGroup() {
return $this->_is_group;
}
function GetProperty( $property_id ) {
switch( $property_id ) {
case 'DAV::resource-id':
if ( $this->exists && $this->principal_id > 0 )
ConstructURL('/.resources/'.$this->principal_id);
else
return null;
break;
}
if ( isset($this->{$property_id}) ) {
if ( ! is_object($this->{$property_id}) ) return $this->{$property_id};
return clone($this->{$property_id});
}
return null;
}
public function unique_tag() {
if ( isset($this->unique_tag) ) return $this->unique_tag;
if ( $this->exists !== true ) $this->unique_tag = '"-1"';
return $this->unique_tag;
}
function calendar_home_set() {
if ( !isset($this->calendar_home_set) ) {
$this->calendar_home_set = array();
$qry = new AwlQuery('SELECT DISTINCT parent_container FROM collection WHERE is_calendar AND dav_name ~ :dav_name_start',
array( ':dav_name_start' => '^'.$this->dav_name));
if ( $qry->Exec('principal',__LINE__,__FILE__) ) {
if ( $qry->rows() > 0 ) {
while( $calendar = $qry->Fetch() ) {
$this->calendar_home_set[] = ConstructURL($calendar->parent_container, true);
}
}
else {
$this->calendar_home_set[] = $this->url;
}
}
}
return $this->calendar_home_set;
}
function addressbook_home_set() {
if ( !isset($this->addressbook_home_set) ) {
$this->addressbook_home_set = array();
$qry = new AwlQuery('SELECT DISTINCT parent_container FROM collection WHERE is_addressbook AND dav_name ~ :dav_name_start',
array( ':dav_name_start' => '^'.$this->dav_name));
if ( $qry->Exec('principal',__LINE__,__FILE__) ) {
if ( $qry->rows() > 0 ) {
while( $addressbook = $qry->Fetch() ) {
$this->addressbook_home_set[] = ConstructURL($addressbook->parent_container, true);
}
}
else {
$this->addressbook_home_set[] = $this->url;
}
}
}
return $this->addressbook_home_set;
}
function calendar_free_busy_set() {
if (!isset($this->calendar_free_busy_set)) {
$this->calendar_free_busy_set = array();
$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',
array(':dav_name_start' => '^' . $this->dav_name));
if ($qry->Exec('principal', __LINE__, __FILE__)) {
while ($calendar = $qry->Fetch()) {
$this->calendar_free_busy_set[] = ConstructURL($calendar->dav_name, true);
}
}
}
return $this->calendar_free_busy_set;
}
function Privileges() {
global $session;
if ( !isset($this->privileges) ) $this->privileges = 0;
if ( is_string($this->privileges) ) $this->privileges = bindec( $this->privileges );
if ( $this->_is_group ) {
if ( isset($session->principal) && in_array($session->principal->url(), $this->GroupMemberSet()) ) {
$this->privileges |= privilege_to_bits( array('DAV::read', 'DAV::read-current-user-privilege-set') );
}
}
return $this->privileges;
}
function AsCollection() {
$dav_name = (isset($this->original_request_url) ? DeconstructURL($this->original_request_url) : $this->dav_name());
$collection = (object) array(
'collection_id' => ($this->principal_id() ? $this->principal_id() : 0),
'is_calendar' => false,
'is_addressbook' => false,
'is_principal' => true,
'type' => 'principal' . (isset($this->original_request_url) ? '_link' : ''),
'user_no' => ($this->user_no() ? $this->user_no() : 0),
'username' => $this->username(),
'dav_name' => $dav_name,
'parent_container' => '/',
'email' => ($this->email()? $this->email() : ''),
'created' => $this->created,
'updated' => $this->modified,
'dav_etag' => substr($this->unique_tag(),1,-1),
'resourcetypes' => $this->resourcetypes
);
$collection->dav_displayname = (isset($this->dav_displayname) ? $this->dav_displayname : (isset($this->fullname) ? $this->fullname : $collection->username));
return $collection;
}
function PropertySearch( $parameters ) {
throw new Exception("Unimplemented!");
}
function PrincipalProperty( $tag, $prop, &$reply, &$denied ) {
global $c;
dbg_error_log('principal',':PrincipalProperty: Principal Property "%s"', $tag );
switch( $tag ) {
case 'DAV::getcontenttype':
$reply->DAVElement( $prop, 'getcontenttype', 'httpd/unix-directory' );
break;
case 'DAV::resourcetype':
$reply->DAVElement( $prop, 'resourcetype', array( new XMLElement('principal'), new XMLElement('collection')) );
break;
case 'DAV::displayname':
$reply->DAVElement( $prop, 'displayname', $this->fullname );
break;
case 'DAV::principal-URL':
$reply->DAVElement( $prop, 'principal-URL', $reply->href($this->url()) );
break;
case 'DAV::getlastmodified':
$reply->DAVElement( $prop, 'getlastmodified', ISODateToHTTPDate($this->modified) );
break;
case 'DAV::creationdate':
$reply->DAVElement( $prop, 'creationdate', DateToISODate($this->created) );
break;
case 'DAV::getcontentlanguage':
$locale = (isset($c->current_locale) ? $c->current_locale : '');
if ( isset($this->locale) && $this->locale != '' ) $locale = $this->locale;
$reply->DAVElement( $prop, 'getcontentlanguage', $locale );
break;
case 'http://calendarserver.org/ns/:group-member-set':
case 'DAV::group-member-set':
if ( ! $this->_is_group ) return false;
$reply->DAVElement( $prop, 'group-member-set', $reply->href($this->group_member_set) );
break;
case 'http://calendarserver.org/ns/:group-membership':
case 'DAV::group-membership':
$reply->DAVElement( $prop, 'group-membership', $reply->href($this->GroupMembership()) );
break;
case 'urn:ietf:params:xml:ns:caldav:schedule-inbox-URL':
$reply->CalDAVElement($prop, 'schedule-inbox-URL', $reply->href($this->url('schedule-inbox')) );
break;
case 'urn:ietf:params:xml:ns:caldav:schedule-outbox-URL':
$reply->CalDAVElement($prop, 'schedule-outbox-URL', $reply->href($this->url('schedule-outbox')) );
break;
case 'urn:ietf:params:xml:ns:caldav:schedule-default-calendar-URL':
$reply->CalDAVElement($prop, 'schedule-default-calendar-URL', $reply->href($this->url('schedule-default-calendar')) );
break;
case 'http://calendarserver.org/ns/:dropbox-home-URL':
$reply->CalendarserverElement($prop, 'dropbox-home-URL', $reply->href($this->url('dropbox')) );
break;
case 'http://calendarserver.org/ns/:xmpp-server':
if ( ! isset( $this->xmpp_uri ) ) return false;
$reply->CalendarserverElement($prop, 'xmpp-server', $this->xmpp_server );
break;
case 'http://calendarserver.org/ns/:xmpp-uri':
if ( ! isset( $this->xmpp_uri ) ) return false;
$reply->CalendarserverElement($prop, 'xmpp-uri', $this->xmpp_uri );
break;
case 'urn:ietf:params:xml:ns:carddav:addressbook-home-set':
$reply->CardDAVElement($prop, $tag, $reply->href( $this->addressbook_home_set() ) );
break;
case 'urn:ietf:params:xml:ns:caldav:calendar-home-set':
$reply->CalDAVElement($prop, $tag, $reply->href( $this->calendar_home_set() ) );
break;
case 'urn:ietf:params:xml:ns:caldav:calendar-free-busy-set':
if ( isset($c->support_obsolete_free_busy_property) && $c->support_obsolete_free_busy_property )
$reply->CalDAVElement( $prop, 'calendar-free-busy-set', $reply->href( $this->calendar_free_busy_set() ) );
else
return false;
break;
case 'urn:ietf:params:xml:ns:caldav:calendar-user-address-set':
$reply->CalDAVElement($prop, 'calendar-user-address-set', $reply->href($this->user_address_set));
break;
case 'urn:ietf:params:xml:ns:caldav:calendar-user-type':
$type = 'UNKNOWN';
if ( isset($this->type_label) )
$type = $this->type_label;
$reply->CalDAVElement($prop, 'calendar-user-type', $type);
break;
case 'DAV::owner':
// After a careful reading of RFC3744 we see that this must be the principal-URL of the owner
$reply->DAVElement( $prop, 'owner', $reply->href( $this->url ) );
break;
// Empty tag responses.
case 'DAV::alternate-URI-set':
$reply->DAVElement( $prop, $reply->Tag($tag));
break;
case 'SOME-DENIED-PROPERTY':
$denied[] = $reply->Tag($tag);
break;
default:
return false;
break;
}
return true;
}
function RenderAsXML( $properties, &$reply, $props_only = false ) {
dbg_error_log('principal',':RenderAsXML: Principal "%s"', $this->username );
$prop = new XMLElement('prop');
$denied = array();
$not_found = array();
foreach( $properties AS $k => $tag ) {
if ( ! $this->PrincipalProperty( $tag, $prop, $reply, $denied ) ) {
dbg_error_log( 'principal', 'Request for unsupported property "%s" of principal "%s".', $tag, $this->username );
$not_found[] = $reply->Tag($tag);
}
}
if ( $props_only ) return $prop;
$status = new XMLElement('status', 'HTTP/1.1 200 OK' );
$propstat = new XMLElement( 'propstat', array( $prop, $status) );
$href = $reply->href($this->url );
$elements = array($href,$propstat);
if ( count($denied) > 0 ) {
$status = new XMLElement('status', 'HTTP/1.1 403 Forbidden' );
$noprop = new XMLElement('prop');
foreach( $denied AS $k => $v ) {
$noprop->NewElement( $v );
}
$elements[] = new XMLElement( 'propstat', array( $noprop, $status) );
}
if ( count($not_found) > 0 ) {
$status = new XMLElement('status', 'HTTP/1.1 404 Not Found' );
$noprop = new XMLElement('prop');
foreach( $not_found AS $k => $v ) {
$noprop->NewElement( $v );
}
$elements[] = new XMLElement( 'propstat', array( $noprop, $status) );
}
$response = new XMLElement( 'response', $elements );
return $response;
}
}
DAVPrincipal
Definition DAVPrincipal.php:20
DAVPrincipal\__construct
__construct( $parameters=null)
Definition DAVPrincipal.php:97
DAVPrincipal\AsCollection
AsCollection()
Definition DAVPrincipal.php:428
DAVPrincipal\InitialiseRecord
InitialiseRecord()
Definition DAVPrincipal.php:146
DAVPrincipal\PrincipalProperty
PrincipalProperty( $tag, $prop, &$reply, &$denied)
Definition DAVPrincipal.php:459
DAVPrincipal\GroupMembership
GroupMembership()
Definition DAVPrincipal.php:283
DAVPrincipal\addressbook_home_set
addressbook_home_set()
Definition DAVPrincipal.php:367
DAVPrincipal\calendar_free_busy_set
calendar_free_busy_set()
Definition DAVPrincipal.php:394
DAVPrincipal\Privileges
Privileges()
Definition DAVPrincipal.php:412
DAVPrincipal\FetchProxyGroups
FetchProxyGroups()
Definition DAVPrincipal.php:196
DAVPrincipal\unique_tag
unique_tag()
Definition DAVPrincipal.php:332
DAVPrincipal\IsGroup
IsGroup()
Definition DAVPrincipal.php:302
DAVPrincipal\RenderAsXML
RenderAsXML( $properties, &$reply, $props_only=false)
Definition DAVPrincipal.php:600
DAVPrincipal\ProxyFor
ProxyFor( $type)
Definition DAVPrincipal.php:273
DAVPrincipal\GroupMemberSet
GroupMemberSet()
Definition DAVPrincipal.php:292
DAVPrincipal\ReadProxyGroup
ReadProxyGroup()
Definition DAVPrincipal.php:254
DAVPrincipal\calendar_home_set
calendar_home_set()
Definition DAVPrincipal.php:344
DAVPrincipal\WriteProxyGroup
WriteProxyGroup()
Definition DAVPrincipal.php:263
DAVPrincipal\GetProperty
GetProperty( $property_id)
Definition DAVPrincipal.php:311
Principal
Definition Principal.php:19
Principal\email
email()
Definition Principal.php:374
Principal\$username
$username
Definition Principal.php:49
Principal\user_no
user_no()
Definition Principal.php:356
Principal\principal_id
principal_id()
Definition Principal.php:365
Principal\dav_name
dav_name()
Definition Principal.php:383
Principal\username
username()
Definition Principal.php:336