mirror of
https://github.com/EGroupware/egroupware.git
synced 2025-01-23 14:28:45 +01:00
merged r32187: Fix GroupDAV ctag issues
This commit is contained in:
parent
3869fcd8c3
commit
51e79b42c9
533
addressbook/inc/class.addressbook_groupdav.inc.php
Normal file
533
addressbook/inc/class.addressbook_groupdav.inc.php
Normal file
@ -0,0 +1,533 @@
|
||||
<?php
|
||||
/**
|
||||
* EGroupware: GroupDAV access: addressbook handler
|
||||
*
|
||||
* @link http://www.egroupware.org
|
||||
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
||||
* @package addressbook
|
||||
* @subpackage groupdav
|
||||
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
||||
* @copyright (c) 2007-9 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* EGroupware: GroupDAV access: addressbook handler
|
||||
*
|
||||
* Propfind now uses a groupdav_propfind_iterator with a callback to query huge addressbooks in chunk,
|
||||
* without getting into problems with memory_limit.
|
||||
*/
|
||||
class addressbook_groupdav extends groupdav_handler
|
||||
{
|
||||
/**
|
||||
* bo class of the application
|
||||
*
|
||||
* @var addressbook_bo
|
||||
*/
|
||||
var $bo;
|
||||
|
||||
var $filter_prop2cal = array(
|
||||
'UID' => 'uid',
|
||||
//'NICKNAME',
|
||||
'EMAIL' => 'email',
|
||||
'FN' => 'n_fn',
|
||||
);
|
||||
|
||||
/**
|
||||
* Charset for exporting data, as some clients ignore the headers specifying the charset
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
var $charset = 'utf-8';
|
||||
|
||||
/**
|
||||
* What attribute is used to construct the path, default id, can be uid too
|
||||
*/
|
||||
const PATH_ATTRIBUTE = 'id';
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $app 'calendar', 'addressbook' or 'infolog'
|
||||
* @param int $debug=null debug-level to set
|
||||
* @param string $base_uri=null base url of handler
|
||||
* @param string $principalURL=null pricipal url of handler
|
||||
*/
|
||||
function __construct($app,$debug=null,$base_uri=null,$principalURL=null)
|
||||
{
|
||||
parent::__construct($app,$debug,$base_uri,$principalURL);
|
||||
|
||||
$this->bo = new addressbook_bo();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the path for a contact
|
||||
*
|
||||
* @param array $contact
|
||||
* @return string
|
||||
*/
|
||||
static function get_path($contact)
|
||||
{
|
||||
return $contact[self::PATH_ATTRIBUTE].'.vcf';
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle propfind in the addressbook folder
|
||||
*
|
||||
* @param string $path
|
||||
* @param array $options
|
||||
* @param array &$files
|
||||
* @param int $user account_id
|
||||
* @param string $id=''
|
||||
* @return mixed boolean true on success, false on failure or string with http status (eg. '404 Not Found')
|
||||
*/
|
||||
function propfind($path,$options,&$files,$user,$id='')
|
||||
{
|
||||
$filter = array();
|
||||
// show addressbook of a single user?
|
||||
if ($user && $path != '/addressbook/') $filter['contact_owner'] = $user;
|
||||
// should we hide the accounts addressbook
|
||||
if ($GLOBALS['egw_info']['user']['preferences']['addressbook']['hide_accounts']) $filter['account_id'] = null;
|
||||
|
||||
// process REPORT filters or multiget href's
|
||||
if (($id || $options['root']['name'] != 'propfind') && !$this->_report_filters($options,$filter,$id))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if ($this->debug) error_log(__METHOD__."($path,".array2string($options).",,$user,$id) filter=".array2string($filter));
|
||||
|
||||
// check if we have to return the full contact data or just the etag's
|
||||
if (!($filter['address_data'] = $options['props'] == 'all' &&
|
||||
$options['root']['ns'] == groupdav::CARDDAV) && is_array($options['props']))
|
||||
{
|
||||
foreach($options['props'] as $prop)
|
||||
{
|
||||
if ($prop['name'] == 'address-data')
|
||||
{
|
||||
$filter['address_data'] = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// return iterator, calling ourself to return result in chunks
|
||||
$files['files'] = new groupdav_propfind_iterator($this,$path,$filter,$files['files']);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback for profind interator
|
||||
*
|
||||
* @param string $path
|
||||
* @param array $filter
|
||||
* @param array|boolean $start=false false=return all or array(start,num)
|
||||
* @return array with "files" array with values for keys path and props
|
||||
*/
|
||||
function &propfind_callback($path,array $filter,$start=false)
|
||||
{
|
||||
$starttime = microtime(true);
|
||||
|
||||
if (($address_data = $filter['address_data']))
|
||||
{
|
||||
$handler = self::_get_handler();
|
||||
}
|
||||
unset($filter['address_data']);
|
||||
$files = array();
|
||||
// we query etag and modified, as LDAP does not have the strong sql etag
|
||||
if (($contacts =& $this->bo->search(array(),$address_data ? false : array('id','uid','etag','modified'),'contact_id','','',False,'AND',$start,$filter)))
|
||||
{
|
||||
foreach($contacts as &$contact)
|
||||
{
|
||||
$props = array(
|
||||
HTTP_WebDAV_Server::mkprop('getetag',$this->get_etag($contact)),
|
||||
HTTP_WebDAV_Server::mkprop('getcontenttype', 'text/vcard'),
|
||||
// getlastmodified and getcontentlength are required by WebDAV and Cadaver eg. reports 404 Not found if not set
|
||||
HTTP_WebDAV_Server::mkprop('getlastmodified', $contact['modified']),
|
||||
);
|
||||
if ($address_data)
|
||||
{
|
||||
$content = $handler->getVCard($contact['id'],$this->charset,false);
|
||||
$props[] = HTTP_WebDAV_Server::mkprop('getcontentlength',bytes($content));
|
||||
$props[] = HTTP_WebDAV_Server::mkprop(groupdav::CARDDAV,'address-data',$content,true);
|
||||
}
|
||||
else
|
||||
{
|
||||
$props[] = HTTP_WebDAV_Server::mkprop('getcontentlength', ''); // expensive to calculate and no CalDAV client uses it
|
||||
}
|
||||
$files[] = array(
|
||||
'path' => $path.self::get_path($contact),
|
||||
'props' => $props,
|
||||
);
|
||||
}
|
||||
}
|
||||
if ($this->debug) error_log(__METHOD__."($path,".array2string($filter).','.array2string($start).") took ".(microtime(true) - $starttime).' to return '.count($files).' items');
|
||||
return $files;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the filters from the CalDAV REPORT request
|
||||
*
|
||||
* @param array $options
|
||||
* @param array &$cal_filters
|
||||
* @param string $id
|
||||
* @return boolean true if filter could be processed, false for requesting not here supported VTODO items
|
||||
*/
|
||||
function _report_filters($options,&$filters,$id)
|
||||
{
|
||||
if ($options['filters'])
|
||||
{
|
||||
foreach($options['filters'] as $filter)
|
||||
{
|
||||
switch($filter['name'])
|
||||
{
|
||||
case 'prop-filter':
|
||||
if ($this->debug > 1) error_log(__METHOD__."($path,...) prop-filter='{$filter['attrs']['name']}'");
|
||||
$prop_filter = $filter['attrs']['name'];
|
||||
break;
|
||||
case 'text-match':
|
||||
if ($this->debug > 1) error_log(__METHOD__."($path,...) text-match: $prop_filter='{$filter['data']}'");
|
||||
if (!isset($this->filter_prop2cal[strtoupper($prop_filter)]))
|
||||
{
|
||||
if ($this->debug) error_log(__METHOD__."($path,".str_replace(array("\n",' '),'',print_r($options,true)).",,$user) unknown property '$prop_filter' --> ignored");
|
||||
}
|
||||
else
|
||||
{
|
||||
switch($filter['attrs']['match-type'])
|
||||
{
|
||||
default:
|
||||
case 'equals':
|
||||
$filters[$this->filter_prop2cal[strtoupper($prop_filter)]] = $filter['data'];
|
||||
break;
|
||||
case 'substr': // ToDo: check RFC4790
|
||||
$filters[] = $this->filter_prop2cal[strtoupper($prop_filter)].' LIKE '.$GLOBALS['egw']->db->quote($filter['data']);
|
||||
break;
|
||||
}
|
||||
}
|
||||
unset($prop_filter);
|
||||
break;
|
||||
case 'param-filter':
|
||||
if ($this->debug) error_log(__METHOD__."($path,...) param-filter='{$filter['attrs']['name']}' not (yet) implemented!");
|
||||
break;
|
||||
default:
|
||||
if ($this->debug) error_log(__METHOD__."($path,".array2string($options).",,$user) unknown filter --> ignored");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// multiget --> fetch the url's
|
||||
if ($options['root']['name'] == 'addressbook-multiget')
|
||||
{
|
||||
$ids = array();
|
||||
foreach($options['other'] as $option)
|
||||
{
|
||||
if ($option['name'] == 'href')
|
||||
{
|
||||
$parts = explode('/',$option['data']);
|
||||
if (($id = array_pop($parts))) $ids[] = basename($id,'.vcf');
|
||||
}
|
||||
}
|
||||
if ($ids) $filters[self::PATH_ATTRIBUTE] = $ids;
|
||||
if ($this->debug) error_log(__METHOD__."($path,,,$user) addressbook-multiget: ids=".implode(',',$ids));
|
||||
}
|
||||
elseif ($id)
|
||||
{
|
||||
$filters[self::PATH_ATTRIBUTE] = basename($id,'.vcf');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle get request for an event
|
||||
*
|
||||
* @param array &$options
|
||||
* @param int $id
|
||||
* @return mixed boolean true on success, false on failure or string with http status (eg. '404 Not Found')
|
||||
*/
|
||||
function get(&$options,$id)
|
||||
{
|
||||
if (!is_array($contact = $this->_common_get_put_delete('GET',$options,$id)))
|
||||
{
|
||||
return $contact;
|
||||
}
|
||||
$handler = self::_get_handler();
|
||||
$options['data'] = $handler->getVCard($contact['id'],$this->charset,false);
|
||||
// e.g. Evolution does not understand 'text/vcard'
|
||||
$options['mimetype'] = 'text/x-vcard; charset='.$this->charset;
|
||||
header('Content-Encoding: identity');
|
||||
header('ETag: '.$this->get_etag($contact));
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle put request for an event
|
||||
*
|
||||
* @param array &$options
|
||||
* @param int $id
|
||||
* @param int $user=null account_id of owner, default null
|
||||
* @return mixed boolean true on success, false on failure or string with http status (eg. '404 Not Found')
|
||||
*/
|
||||
function put(&$options,$id,$user=null)
|
||||
{
|
||||
if ($this->debug) error_log(__METHOD__.'('.array2string($options).",$id,$user)");
|
||||
|
||||
$oldContact = $this->_common_get_put_delete('PUT',$options,$id);
|
||||
if (!is_null($oldContact) && !is_array($oldContact))
|
||||
{
|
||||
return $oldContact;
|
||||
}
|
||||
|
||||
$handler = self::_get_handler();
|
||||
$vCard = htmlspecialchars_decode($options['content']);
|
||||
$charset = null;
|
||||
if (!empty($options['content_type']))
|
||||
{
|
||||
$content_type = explode(';', $options['content_type']);
|
||||
if (count($content_type) > 1)
|
||||
{
|
||||
array_shift($content_type);
|
||||
foreach ($content_type as $attribute)
|
||||
{
|
||||
trim($attribute);
|
||||
list($key, $value) = explode('=', $attribute);
|
||||
switch (strtolower($key))
|
||||
{
|
||||
case 'charset':
|
||||
$charset = strtoupper(substr($value,1,-1));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (is_array($oldContact))
|
||||
{
|
||||
$contactId = $oldContact['id'];
|
||||
$retval = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// new entry?
|
||||
if (($foundContacts = $handler->search($vCard, null, false, $charset)))
|
||||
{
|
||||
if (($contactId = array_shift($foundContacts)) &&
|
||||
($oldContact = $this->bo->read($contactId)))
|
||||
{
|
||||
$retval = '301 Moved Permanently';
|
||||
}
|
||||
else
|
||||
{
|
||||
// to be safe
|
||||
$contactId = -1;
|
||||
$retval = '201 Created';
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// new entry
|
||||
$contactId = -1;
|
||||
$retval = '201 Created';
|
||||
}
|
||||
}
|
||||
|
||||
$contact = $handler->vcardtoegw($vCard, $charset);
|
||||
|
||||
if (is_array($contact['cat_id']))
|
||||
{
|
||||
$contact['cat_id'] = implode(',',$this->bo->find_or_add_categories($contact['cat_id'], $contactId));
|
||||
}
|
||||
elseif ($contactId > 0)
|
||||
{
|
||||
$contact['cat_id'] = $oldContact['cat_id'];
|
||||
}
|
||||
if (is_array($oldContact))
|
||||
{
|
||||
$contact['id'] = $oldContact['id'];
|
||||
// dont allow the client to overwrite certain values
|
||||
$contact['uid'] = $oldContact['uid'];
|
||||
//$contact['owner'] = $oldContact['owner'];
|
||||
$contact['private'] = $oldContact['private'];
|
||||
}
|
||||
|
||||
$contact['owner'] = $user;
|
||||
|
||||
if ($this->http_if_match) $contact['etag'] = self::etag2value($this->http_if_match);
|
||||
|
||||
if (!($save_ok = $this->bo->save($contact)))
|
||||
{
|
||||
if ($this->debug) error_log(__METHOD__."(,$id) save(".array2string($contact).") failed, Ok=$save_ok");
|
||||
if ($save_ok === 0)
|
||||
{
|
||||
return '412 Precondition Failed';
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!isset($contact['etag']))
|
||||
{
|
||||
$contact = $this->read($save_ok);
|
||||
}
|
||||
|
||||
header('ETag: '.$this->get_etag($contact));
|
||||
if ($retval !== true)
|
||||
{
|
||||
$path = preg_replace('|(.*)/[^/]*|', '\1/', $options['path']);
|
||||
header($h='Location: '.$this->base_uri.$path.self::get_path($contact));
|
||||
if ($this->debug) error_log(__METHOD__."($method,,$id) header('$h'): $retval");
|
||||
return $retval;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Query ctag for addressbook
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getctag($path,$user)
|
||||
{
|
||||
$filter = array();
|
||||
// show addressbook of a single user?
|
||||
if ($user && $path != '/addressbook/') $filter['contact_owner'] = $user;
|
||||
// should we hide the accounts addressbook
|
||||
if ($GLOBALS['egw_info']['user']['preferences']['addressbook']['hide_accounts']) $filter['account_id'] = null;
|
||||
|
||||
$result = $this->bo->search(array(),'MAX(contact_modified) AS contact_modified','','','',false,'AND',false,$filter);
|
||||
|
||||
if (empty($result))
|
||||
{
|
||||
$ctag = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
$ctag = $result[0]['contact_modified'];
|
||||
}
|
||||
|
||||
return 'EGw-'.$ctag.'-wGE';
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the privileges of the current user
|
||||
*
|
||||
* @param array $props=array() regular props by the groupdav handler
|
||||
* @return array
|
||||
*/
|
||||
static function current_user_privilege_set(array $props=array())
|
||||
{
|
||||
$props[] = HTTP_WebDAV_Server::mkprop(groupdav::DAV,'current-user-privilege-set',
|
||||
array(HTTP_WebDAV_Server::mkprop(groupdav::DAV,'privilege',
|
||||
array(
|
||||
HTTP_WebDAV_Server::mkprop(groupdav::DAV,'read',''),
|
||||
HTTP_WebDAV_Server::mkprop(groupdav::CALDAV,'read-free-busy',''),
|
||||
HTTP_WebDAV_Server::mkprop(groupdav::DAV,'read-current-user-privilege-set',''),
|
||||
HTTP_WebDAV_Server::mkprop(groupdav::DAV,'bind',''),
|
||||
HTTP_WebDAV_Server::mkprop(groupdav::DAV,'unbind',''),
|
||||
HTTP_WebDAV_Server::mkprop(groupdav::DAV,'schedule-post',''),
|
||||
HTTP_WebDAV_Server::mkprop(groupdav::DAV,'schedule-post-vevent',''),
|
||||
HTTP_WebDAV_Server::mkprop(groupdav::DAV,'schedule-respond',''),
|
||||
HTTP_WebDAV_Server::mkprop(groupdav::DAV,'schedule-respond-vevent',''),
|
||||
HTTP_WebDAV_Server::mkprop(groupdav::DAV,'schedule-deliver',''),
|
||||
HTTP_WebDAV_Server::mkprop(groupdav::DAV,'schedule-deliver-vevent',''),
|
||||
HTTP_WebDAV_Server::mkprop(groupdav::DAV,'write',''),
|
||||
HTTP_WebDAV_Server::mkprop(groupdav::DAV,'write-properties',''),
|
||||
HTTP_WebDAV_Server::mkprop(groupdav::DAV,'write-content',''),
|
||||
))));
|
||||
return $props;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add extra properties for addressbook collections
|
||||
*
|
||||
* Example for supported-report-set syntax from Apples Calendarserver:
|
||||
* <D:supported-report-set>
|
||||
* <supported-report>
|
||||
* <report>
|
||||
* <addressbook-query xmlns='urn:ietf:params:xml:ns:carddav'/>
|
||||
* </report>
|
||||
* </supported-report>
|
||||
* <supported-report>
|
||||
* <report>
|
||||
* <addressbook-multiget xmlns='urn:ietf:params:xml:ns:carddav'/>
|
||||
* </report>
|
||||
* </supported-report>
|
||||
* </D:supported-report-set>
|
||||
* @link http://www.mail-archive.com/calendarserver-users@lists.macosforge.org/msg01156.html
|
||||
*
|
||||
* @param array $props=array() regular props by the groupdav handler
|
||||
* @param string $displayname
|
||||
* @param string $base_uri=null base url of handler
|
||||
* @return array
|
||||
*/
|
||||
static function extra_properties(array $props=array(), $displayname, $base_uri=null)
|
||||
{
|
||||
// addressbook description
|
||||
$displayname = $GLOBALS['egw']->translation->convert(lang('Addressbook of') . ' ' .
|
||||
$displayname,
|
||||
$GLOBALS['egw']->translation->charset(),'utf-8');
|
||||
$props[] = HTTP_WebDAV_Server::mkprop(groupdav::CARDDAV,'addressbook-description',$displayname);
|
||||
// supported reports (required property for CardDAV)
|
||||
$props[] = HTTP_WebDAV_Server::mkprop('supported-report-set',array(
|
||||
HTTP_WebDAV_Server::mkprop('supported-report',array(
|
||||
HTTP_WebDAV_Server::mkprop('report',array(
|
||||
HTTP_WebDAV_Server::mkprop(groupdav::CARDDAV,'addressbook-query',''))))),
|
||||
HTTP_WebDAV_Server::mkprop('supported-report',array(
|
||||
HTTP_WebDAV_Server::mkprop('report',array(
|
||||
HTTP_WebDAV_Server::mkprop(groupdav::CARDDAV,'addressbook-multiget',''))))),
|
||||
));
|
||||
//$props = self::current_user_privilege_set($props);
|
||||
return $props;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the handler and set the supported fields
|
||||
*
|
||||
* @return addressbook_vcal
|
||||
*/
|
||||
private function _get_handler()
|
||||
{
|
||||
$handler = new addressbook_vcal('addressbook','text/vcard');
|
||||
$handler->setSupportedFields('GroupDAV',$this->agent);
|
||||
|
||||
return $handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle delete request for an event
|
||||
*
|
||||
* @param array &$options
|
||||
* @param int $id
|
||||
* @return mixed boolean true on success, false on failure or string with http status (eg. '404 Not Found')
|
||||
*/
|
||||
function delete(&$options,$id)
|
||||
{
|
||||
if (!is_array($contact = $this->_common_get_put_delete('DELETE',$options,$id)))
|
||||
{
|
||||
return $contact;
|
||||
}
|
||||
if (($Ok = $this->bo->delete($contact['id'],self::etag2value($this->http_if_match))) === 0)
|
||||
{
|
||||
return '412 Precondition Failed';
|
||||
}
|
||||
//return $ok;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a contact
|
||||
*
|
||||
* @param string/id $id
|
||||
* @return array/boolean array with entry, false if no read rights, null if $id does not exist
|
||||
*/
|
||||
function read($id)
|
||||
{
|
||||
return $this->bo->read(self::PATH_ATTRIBUTE == 'id' ? $id : array(self::PATH_ATTRIBUTE => $id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if user has the neccessary rights on a contact
|
||||
*
|
||||
* @param int $acl EGW_ACL_READ, EGW_ACL_EDIT or EGW_ACL_DELETE
|
||||
* @param array/int $contact contact-array or id
|
||||
* @return boolean null if entry does not exist, false if no access, true if access permitted
|
||||
*/
|
||||
function check_access($acl,$contact)
|
||||
{
|
||||
return $this->bo->check_perms($acl,$contact);
|
||||
}
|
||||
}
|
588
infolog/inc/class.infolog_groupdav.inc.php
Normal file
588
infolog/inc/class.infolog_groupdav.inc.php
Normal file
@ -0,0 +1,588 @@
|
||||
<?php
|
||||
/**
|
||||
* eGroupWare: GroupDAV access: infolog handler
|
||||
*
|
||||
* @link http://www.egroupware.org
|
||||
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
||||
* @package infolog
|
||||
* @subpackage groupdav
|
||||
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
||||
* @copyright (c) 2007-9 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* eGroupWare: GroupDAV access: infolog handler
|
||||
*/
|
||||
class infolog_groupdav extends groupdav_handler
|
||||
{
|
||||
/**
|
||||
* bo class of the application
|
||||
*
|
||||
* @var infolog_bo
|
||||
*/
|
||||
var $bo;
|
||||
|
||||
var $filter_prop2infolog = array(
|
||||
'SUMMARY' => 'info_subject',
|
||||
'UID' => 'info_uid',
|
||||
'DTSTART' => 'info_startdate',
|
||||
'DUE' => 'info_enddate',
|
||||
'DESCRIPTION' => 'info_des',
|
||||
'STATUS' => 'info_status',
|
||||
'PRIORITY' => 'info_priority',
|
||||
'LOCATION' => 'info_location',
|
||||
'COMPLETED' => 'info_datecompleted',
|
||||
);
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $app 'calendar', 'addressbook' or 'infolog'
|
||||
* @param int $debug=null debug-level to set
|
||||
* @param string $base_uri=null base url of handler
|
||||
* @param string $principalURL=null pricipal url of handler
|
||||
*/
|
||||
function __construct($app,$debug=null,$base_uri=null,$principalURL=null)
|
||||
{
|
||||
parent::__construct($app,$debug,$base_uri,$principalURL);
|
||||
|
||||
$this->bo = new infolog_bo();
|
||||
}
|
||||
|
||||
const PATH_ATTRIBUTE = 'info_id';
|
||||
|
||||
/**
|
||||
* Create the path for an event
|
||||
*
|
||||
* @param array|int $info
|
||||
* @return string
|
||||
*/
|
||||
static function get_path($info)
|
||||
{
|
||||
if (is_numeric($info) && self::PATH_ATTRIBUTE == 'info_id')
|
||||
{
|
||||
$name = $info;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!is_array($info)) $info = $this->bo->read($info);
|
||||
$name = $info[self::PATH_ATTRIBUTE];
|
||||
}
|
||||
return $name.'.ics';
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle propfind in the infolog folder
|
||||
*
|
||||
* @param string $path
|
||||
* @param array $options
|
||||
* @param array &$files
|
||||
* @param int $user account_id
|
||||
* @return mixed boolean true on success, false on failure or string with http status (eg. '404 Not Found')
|
||||
*/
|
||||
function propfind($path,$options,&$files,$user,$id='')
|
||||
{
|
||||
$myself = ($user == $GLOBALS['egw_info']['user']['account_id']);
|
||||
|
||||
if ($path == '/infolog/')
|
||||
{
|
||||
$task_filter= 'own';
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($myself)
|
||||
{
|
||||
$task_filter = 'open';
|
||||
}
|
||||
else
|
||||
{
|
||||
$task_filter = 'open-user' . $user;
|
||||
}
|
||||
}
|
||||
|
||||
// todo add a filter to limit how far back entries from the past get synced
|
||||
$filter = array(
|
||||
'info_type' => 'task',
|
||||
'filter' => $task_filter,
|
||||
);
|
||||
|
||||
// process REPORT filters or multiget href's
|
||||
if (($id || $options['root']['name'] != 'propfind') && !$this->_report_filters($options,$filter,$id))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if ($this->debug > 1)
|
||||
{
|
||||
error_log(__METHOD__."($path,,,$user,$id) filter=".
|
||||
array2string($filter));
|
||||
}
|
||||
|
||||
// check if we have to return the full calendar data or just the etag's
|
||||
if (!($filter['calendar_data'] = $options['props'] == 'all' &&
|
||||
$options['root']['ns'] == groupdav::CALDAV) && is_array($options['props']))
|
||||
{
|
||||
foreach($options['props'] as $prop)
|
||||
{
|
||||
if ($prop['name'] == 'calendar-data')
|
||||
{
|
||||
$filter['calendar_data'] = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// return iterator, calling ourself to return result in chunks
|
||||
$files['files'] = new groupdav_propfind_iterator($this,$path,$filter,$files['files']);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback for profind interator
|
||||
*
|
||||
* @param string $path
|
||||
* @param array $filter
|
||||
* @param array|boolean $start=false false=return all or array(start,num)
|
||||
* @return array with "files" array with values for keys path and props
|
||||
*/
|
||||
function &propfind_callback($path,array $filter,$start=false)
|
||||
{
|
||||
if ($this->debug) $starttime = microtime(true);
|
||||
|
||||
if (($calendar_data = $filter['calendar_data']))
|
||||
{
|
||||
$handler = self::_get_handler();
|
||||
}
|
||||
unset($filter['calendar_data']);
|
||||
$task_filter = $filter['filter'];
|
||||
unset($filter['filter']);
|
||||
|
||||
$query = array(
|
||||
'order' => 'info_datemodified',
|
||||
'sort' => 'DESC',
|
||||
'filter' => $task_filter,
|
||||
'date_format' => 'server',
|
||||
'col_filter' => $filter,
|
||||
);
|
||||
|
||||
if (!$calendar_data)
|
||||
{
|
||||
$query['cols'] = array('info_id', 'info_datemodified');
|
||||
}
|
||||
|
||||
if (is_array($start))
|
||||
{
|
||||
$query['start'] = $offset = $start[0];
|
||||
$query['num_rows'] = $start[1];
|
||||
}
|
||||
else
|
||||
{
|
||||
$offset = 0;
|
||||
}
|
||||
|
||||
$files = array();
|
||||
// ToDo: add parameter to only return id & etag
|
||||
$tasks =& $this->bo->search($query);
|
||||
if ($tasks && $offset == $query['start'])
|
||||
{
|
||||
foreach($tasks as $task)
|
||||
{
|
||||
$props = array(
|
||||
HTTP_WebDAV_Server::mkprop('getetag',$this->get_etag($task)),
|
||||
HTTP_WebDAV_Server::mkprop('getcontenttype',$this->agent != 'kde' ?
|
||||
'text/calendar; charset=utf-8; component=VTODO' : 'text/calendar'), // Konqueror (3.5) dont understand it otherwise
|
||||
// getlastmodified and getcontentlength are required by WebDAV and Cadaver eg. reports 404 Not found if not set
|
||||
HTTP_WebDAV_Server::mkprop('getlastmodified', $task['info_datemodified']),
|
||||
HTTP_WebDAV_Server::mkprop('resourcetype',''), // DAVKit requires that attribute!
|
||||
HTTP_WebDAV_Server::mkprop('getcontentlength',''),
|
||||
);
|
||||
if ($calendar_data)
|
||||
{
|
||||
$content = $handler->exportVTODO($task,'2.0','PUBLISH');
|
||||
$props[] = HTTP_WebDAV_Server::mkprop('getcontentlength',bytes($content));
|
||||
$props[] = HTTP_WebDAV_Server::mkprop(groupdav::CALDAV,'calendar-data',$content);
|
||||
}
|
||||
else
|
||||
{
|
||||
$props[] = HTTP_WebDAV_Server::mkprop('getcontentlength', ''); // expensive to calculate and no CalDAV client uses it
|
||||
}
|
||||
$files[] = array(
|
||||
'path' => $path.self::get_path($task),
|
||||
'props' => $props,
|
||||
);
|
||||
}
|
||||
}
|
||||
if ($this->debug) error_log(__METHOD__."($path) took ".(microtime(true) - $starttime).' to return '.count($files).' items');
|
||||
return $files;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the filters from the CalDAV REPORT request
|
||||
*
|
||||
* @param array $options
|
||||
* @param array &$cal_filters
|
||||
* @param string $id
|
||||
* @return boolean true if filter could be processed, false for requesting not here supported VTODO items
|
||||
*/
|
||||
function _report_filters($options,&$cal_filters,$id)
|
||||
{
|
||||
if ($options['filters'])
|
||||
{
|
||||
foreach($options['filters'] as $filter)
|
||||
{
|
||||
switch($filter['name'])
|
||||
{
|
||||
case 'comp-filter':
|
||||
if ($this->debug > 1) error_log(__METHOD__."($options[path],...) comp-filter='{$filter['attrs']['name']}'");
|
||||
|
||||
switch($filter['attrs']['name'])
|
||||
{
|
||||
case 'VTODO':
|
||||
case 'VCALENDAR':
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'prop-filter':
|
||||
if ($this->debug > 1) error_log(__METHOD__."($options[path],...) prop-filter='{$filter['attrs']['name']}'");
|
||||
$prop_filter = $filter['attrs']['name'];
|
||||
break;
|
||||
case 'text-match':
|
||||
if ($this->debug > 1) error_log(__METHOD__."($options[path],...) text-match: $prop_filter='{$filter['data']}'");
|
||||
if (!isset($this->filter_prop2infolog[strtoupper($prop_filter)]))
|
||||
{
|
||||
if ($this->debug) error_log(__METHOD__."($options[path],".array2string($options).",...) unknown property '$prop_filter' --> ignored");
|
||||
}
|
||||
else
|
||||
{
|
||||
$cal_filters[$this->filter_prop2infolog[strtoupper($prop_filter)]] = $filter['data'];
|
||||
}
|
||||
unset($prop_filter);
|
||||
break;
|
||||
case 'param-filter':
|
||||
if ($this->debug) error_log(__METHOD__."($options[path],...) param-filter='{$filter['attrs']['name']}' not (yet) implemented!");
|
||||
break;
|
||||
case 'time-range':
|
||||
if ($this->debug > 1) error_log(__FILE__ . __METHOD__."($options[path],...) time-range={$filter['attrs']['start']}-{$filter['attrs']['end']}");
|
||||
$cal_filters[] = 'info_startdate >= ' . $filter['attrs']['start'];
|
||||
$cal_filters[] = 'info_startdate <= ' . $filter['attrs']['end'];
|
||||
break;
|
||||
default:
|
||||
if ($this->debug) error_log(__METHOD__."($options[path],".array2string($options).",...) unknown filter --> ignored");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// multiget or propfind on a given id
|
||||
//error_log(__FILE__ . __METHOD__ . "multiget of propfind:");
|
||||
if ($options['root']['name'] == 'calendar-multiget' || $id)
|
||||
{
|
||||
$ids = array();
|
||||
if ($id)
|
||||
{
|
||||
if (is_numeric($id))
|
||||
{
|
||||
$cal_filters['info_id'] = $id;
|
||||
}
|
||||
else
|
||||
{
|
||||
$cal_filters['info_uid'] = basename($id,'.ics');
|
||||
}
|
||||
}
|
||||
else // fetch all given url's
|
||||
{
|
||||
foreach($options['other'] as $option)
|
||||
{
|
||||
if ($option['name'] == 'href')
|
||||
{
|
||||
$parts = explode('/',$option['data']);
|
||||
if (is_numeric($id = basename(array_pop($parts),'.ics'))) $ids[] = $id;
|
||||
}
|
||||
}
|
||||
if ($ids)
|
||||
{
|
||||
$cal_filters[] = 'info_id IN ('.implode(',',array_map(create_function('$n','return (int)$n;'),$ids)).')';
|
||||
}
|
||||
}
|
||||
if ($this->debug > 1) error_log(__METHOD__ ."($options[path],...,$id) calendar-multiget: ids=".implode(',',$ids));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Handle get request for a task / infolog entry
|
||||
*
|
||||
* @param array &$options
|
||||
* @param int $id
|
||||
* @return mixed boolean true on success, false on failure or string with http status (eg. '404 Not Found')
|
||||
*/
|
||||
function get(&$options,$id)
|
||||
{
|
||||
if (!is_array($task = $this->_common_get_put_delete('GET',$options,$id)))
|
||||
{
|
||||
return $task;
|
||||
}
|
||||
$handler = $this->_get_handler();
|
||||
$options['data'] = $handler->exportVTODO($id,'2.0','PUBLISH');
|
||||
$options['mimetype'] = 'text/calendar; charset=utf-8';
|
||||
header('Content-Encoding: identity');
|
||||
header('ETag: '.$this->get_etag($task));
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle put request for a task / infolog entry
|
||||
*
|
||||
* @param array &$options
|
||||
* @param int $id
|
||||
* @param int $user=null account_id of owner, default null
|
||||
* @return mixed boolean true on success, false on failure or string with http status (eg. '404 Not Found')
|
||||
*/
|
||||
function put(&$options,$id,$user=null)
|
||||
{
|
||||
if ($this->debug) error_log(__METHOD__."($id, $user)".print_r($options,true));
|
||||
|
||||
$oldTask = $this->_common_get_put_delete('PUT',$options,$id);
|
||||
if (!is_null($oldTask) && !is_array($oldTask))
|
||||
{
|
||||
return $oldTask;
|
||||
}
|
||||
|
||||
$handler = $this->_get_handler();
|
||||
$vTodo = htmlspecialchars_decode($options['content']);
|
||||
|
||||
if (is_array($oldTask))
|
||||
{
|
||||
$taskId = $oldTask['info_id'];
|
||||
$retval = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// new entry?
|
||||
if (($foundTasks = $handler->searchVTODO($vTodo)))
|
||||
{
|
||||
if (($taskId = array_shift($foundTasks)) &&
|
||||
($oldTask = $this->bo->read($taskId)))
|
||||
{
|
||||
$retval = '301 Moved Permanently';
|
||||
}
|
||||
else
|
||||
{
|
||||
// to be safe
|
||||
$taskId = 0;
|
||||
$retval = '201 Created';
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// new entry
|
||||
$taskId = 0;
|
||||
$retval = '201 Created';
|
||||
}
|
||||
}
|
||||
|
||||
if (!($infoId = $handler->importVTODO($vTodo, $taskId, false, $user)))
|
||||
{
|
||||
if ($this->debug) error_log(__METHOD__."(,$id) import_vtodo($options[content]) returned false");
|
||||
return '403 Forbidden';
|
||||
}
|
||||
|
||||
/*
|
||||
if (strstr($option['path'], '/infolog/') === 0)
|
||||
{
|
||||
$task_filter= 'own';
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($myself)
|
||||
{
|
||||
$task_filter = 'open';
|
||||
}
|
||||
else
|
||||
{
|
||||
$task_filter = 'user' . $user. '-open';
|
||||
}
|
||||
}
|
||||
|
||||
$query = array(
|
||||
'order' => 'info_datemodified',
|
||||
'sort' => 'DESC',
|
||||
'filter' => $task_filter,
|
||||
'date_format' => 'server',
|
||||
'col_filter' => array('info_id' => $infoId),
|
||||
);
|
||||
|
||||
if (!$this->bo->search($query))
|
||||
{
|
||||
$retval = '410 Gone';
|
||||
}
|
||||
else
|
||||
*/
|
||||
if ($infoId != $taskId)
|
||||
{
|
||||
$retval = '201 Created';
|
||||
|
||||
}
|
||||
|
||||
header('ETag: '.$this->get_etag($infoId));
|
||||
if ($retval !== true)
|
||||
{
|
||||
$path = preg_replace('|(.*)/[^/]*|', '\1/', $options['path']);
|
||||
header('Location: '.$this->base_uri.$path.self::get_path($infoId));
|
||||
return $retval;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle delete request for a task / infolog entry
|
||||
*
|
||||
* @param array &$options
|
||||
* @param int $id
|
||||
* @return mixed boolean true on success, false on failure or string with http status (eg. '404 Not Found')
|
||||
*/
|
||||
function delete(&$options,$id)
|
||||
{
|
||||
if (!is_array($task = $this->_common_get_put_delete('DELETE',$options,$id)))
|
||||
{
|
||||
return $task;
|
||||
}
|
||||
return $this->bo->delete($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read an entry
|
||||
*
|
||||
* @param string/id $id
|
||||
* @return array/boolean array with entry, false if no read rights, null if $id does not exist
|
||||
*/
|
||||
function read($id)
|
||||
{
|
||||
if (is_numeric($id)) return $this->bo->read($id,false,'server');
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if user has the neccessary rights on a task / infolog entry
|
||||
*
|
||||
* @param int $acl EGW_ACL_READ, EGW_ACL_EDIT or EGW_ACL_DELETE
|
||||
* @param array/int $task task-array or id
|
||||
* @return boolean null if entry does not exist, false if no access, true if access permitted
|
||||
*/
|
||||
function check_access($acl,$task)
|
||||
{
|
||||
if (is_null($task)) return true;
|
||||
return $this->bo->check_access($task,$acl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Query ctag for infolog
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getctag($path,$user)
|
||||
{
|
||||
$myself = ($user == $GLOBALS['egw_info']['user']['account_id']);
|
||||
|
||||
if ($path == '/infolog/')
|
||||
{
|
||||
$task_filter= 'own';
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($myself)
|
||||
{
|
||||
$task_filter = 'open';
|
||||
}
|
||||
else
|
||||
{
|
||||
$task_filter = 'open-user' . $user;
|
||||
}
|
||||
}
|
||||
|
||||
$query = array(
|
||||
'order' => 'info_datemodified',
|
||||
'sort' => 'DESC',
|
||||
'filter' => $task_filter,
|
||||
'date_format' => 'server',
|
||||
'col_filter' => array('info_type' => 'task'),
|
||||
'start' => 0,
|
||||
'num_rows' => 1,
|
||||
);
|
||||
|
||||
$result =& $this->bo->search($query);
|
||||
|
||||
if (empty($result)) return 'EGw-0-wGE';
|
||||
|
||||
$entry = array_shift($result);
|
||||
|
||||
return $this->get_etag($entry);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the etag for an infolog entry
|
||||
*
|
||||
* @param array/int $info array with infolog entry or info_id
|
||||
* @return string/boolean string with etag or false
|
||||
*/
|
||||
function get_etag($info)
|
||||
{
|
||||
if (!is_array($info))
|
||||
{
|
||||
$info = $this->bo->read($info,true,'server');
|
||||
}
|
||||
if (!is_array($info) || !isset($info['info_id']) || !isset($info['info_datemodified']))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return 'EGw-'.$info['info_id'].':'.$info['info_datemodified'].'-wGE';
|
||||
}
|
||||
|
||||
/**
|
||||
* Add extra properties for calendar collections
|
||||
*
|
||||
* @param array $props=array() regular props by the groupdav handler
|
||||
* @param string $displayname
|
||||
* @param string $base_uri=null base url of handler
|
||||
* @return array
|
||||
*/
|
||||
static function extra_properties(array $props=array(), $displayname, $base_uri=null)
|
||||
{
|
||||
// calendar description
|
||||
$displayname = $GLOBALS['egw']->translation->convert(lang('Tasks of') . ' ' .
|
||||
$displayname,
|
||||
$GLOBALS['egw']->translation->charset(),'utf-8');
|
||||
$props[] = HTTP_WebDAV_Server::mkprop(groupdav::CALDAV,'calendar-description',$displayname);
|
||||
// email of the current user, see caldav-sheduling draft
|
||||
$props[] = HTTP_WebDAV_Server::mkprop(groupdav::CALDAV,'calendar-user-address-set',array(
|
||||
HTTP_WebDAV_Server::mkprop('href','MAILTO:'.$GLOBALS['egw_info']['user']['email'])));
|
||||
// supported components, currently only VEVENT
|
||||
$props[] = HTTP_WebDAV_Server::mkprop(groupdav::CALDAV,'supported-calendar-component-set',array(
|
||||
// HTTP_WebDAV_Server::mkprop(groupdav::CALDAV,'comp',array('name' => 'VEVENT')),
|
||||
HTTP_WebDAV_Server::mkprop(groupdav::CALDAV,'comp',array('name' => 'VCALENDAR')),
|
||||
HTTP_WebDAV_Server::mkprop(groupdav::CALDAV,'comp',array('name' => 'VTIMEZONE')),
|
||||
HTTP_WebDAV_Server::mkprop(groupdav::CALDAV,'comp',array('name' => 'VTODO')),
|
||||
));
|
||||
|
||||
$props[] = HTTP_WebDAV_Server::mkprop('supported-report-set',array(
|
||||
HTTP_WebDAV_Server::mkprop('supported-report',array(
|
||||
HTTP_WebDAV_Server::mkprop('report',array(
|
||||
HTTP_WebDAV_Server::mkprop(groupdav::CALDAV,'calendar-multiget','')))))));
|
||||
|
||||
return $props;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the handler and set the supported fields
|
||||
*
|
||||
* @return infolog_ical
|
||||
*/
|
||||
private function _get_handler()
|
||||
{
|
||||
$handler = new infolog_ical();
|
||||
$handler->setSupportedFields('GroupDAV',$this->agent);
|
||||
|
||||
return $handler;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user