2008-05-08 22:31:32 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2009-10-17 11:13:36 +02:00
|
|
|
* EGroupware: GroupDAV access: abstract baseclass for groupdav/caldav/carddav handlers
|
2008-05-08 22:31:32 +02:00
|
|
|
*
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package api
|
|
|
|
* @subpackage groupdav
|
|
|
|
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
2012-02-10 09:58:08 +01:00
|
|
|
* @copyright (c) 2007-12 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
2008-05-08 22:31:32 +02:00
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2009-10-17 11:13:36 +02:00
|
|
|
* EGroupware: GroupDAV access: abstract baseclass for groupdav/caldav/carddav handlers
|
2012-02-21 21:05:35 +01:00
|
|
|
*
|
|
|
|
* Permanent error_log() calls should use $this->groupdav->log($str) instead, to be send to PHP error_log()
|
|
|
|
* and our request-log (prefixed with "### " after request and response, like exceptions).
|
2012-10-16 13:11:29 +02:00
|
|
|
*
|
|
|
|
* @ToDo: If precondition for PUT, see https://tools.ietf.org/html/rfc6578#section-5
|
2008-05-08 22:31:32 +02:00
|
|
|
*/
|
|
|
|
abstract class groupdav_handler
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Debug level: 0 = nothing, 1 = function calls, 2 = more info, eg. complete $_SERVER array
|
|
|
|
*
|
|
|
|
* The debug messages are send to the apache error_log
|
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
*/
|
2011-04-10 17:05:47 +02:00
|
|
|
var $debug = 0;
|
2008-05-08 22:31:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* eGW's charset
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $egw_charset;
|
2010-03-07 00:06:43 +01:00
|
|
|
/**
|
|
|
|
* Reference to the accounts class
|
|
|
|
*
|
|
|
|
* @var accounts
|
|
|
|
*/
|
|
|
|
var $accounts;
|
2011-11-06 10:40:33 +01:00
|
|
|
/**
|
|
|
|
* Reference to the ACL class
|
|
|
|
*
|
|
|
|
* @var acl
|
|
|
|
*/
|
|
|
|
var $acl;
|
2008-05-08 22:31:32 +02:00
|
|
|
/**
|
|
|
|
* Translates method names into ACL bits
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $method2acl = array(
|
|
|
|
'GET' => EGW_ACL_READ,
|
|
|
|
'PUT' => EGW_ACL_EDIT,
|
|
|
|
'DELETE' => EGW_ACL_DELETE,
|
|
|
|
);
|
|
|
|
/**
|
|
|
|
* eGW application responsible for the handler
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $app;
|
2008-05-17 14:54:26 +02:00
|
|
|
/**
|
2011-11-06 10:40:33 +01:00
|
|
|
* Calling groupdav object
|
2008-05-17 14:54:26 +02:00
|
|
|
*
|
2011-11-06 10:40:33 +01:00
|
|
|
* @var groupdav
|
2008-05-17 14:54:26 +02:00
|
|
|
*/
|
2011-11-06 10:40:33 +01:00
|
|
|
var $groupdav;
|
2010-03-07 00:06:43 +01:00
|
|
|
/**
|
2011-11-06 10:40:33 +01:00
|
|
|
* Base url of handler, need to prefix all pathes not automatic handled by HTTP_WebDAV_Server
|
2010-03-07 00:06:43 +01:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-11-06 10:40:33 +01:00
|
|
|
var $base_uri;
|
2008-05-08 22:31:32 +02:00
|
|
|
/**
|
|
|
|
* HTTP_IF_MATCH / etag of current request / last call to _common_get_put_delete() method
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $http_if_match;
|
2008-11-03 10:36:20 +01:00
|
|
|
/**
|
|
|
|
* Identified user agent
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $agent;
|
2008-05-08 22:31:32 +02:00
|
|
|
|
2011-04-05 22:39:13 +02:00
|
|
|
/**
|
|
|
|
* Extension to append to url/path
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
static $path_extension = '.ics';
|
|
|
|
|
2011-11-06 10:40:33 +01:00
|
|
|
/**
|
|
|
|
* Which attribute to use to contruct name part of url/path
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
static $path_attr = 'id';
|
|
|
|
|
2008-05-10 22:15:02 +02:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2008-05-17 14:54:26 +02:00
|
|
|
* @param string $app 'calendar', 'addressbook' or 'infolog'
|
2011-11-06 10:40:33 +01:00
|
|
|
* @param groupdav $groupdav calling class
|
2008-05-10 22:15:02 +02:00
|
|
|
*/
|
2011-11-06 10:40:33 +01:00
|
|
|
function __construct($app, groupdav $groupdav)
|
2008-05-08 22:31:32 +02:00
|
|
|
{
|
|
|
|
$this->app = $app;
|
2011-11-06 10:40:33 +01:00
|
|
|
if (!is_null($parent->debug)) $this->debug = $groupdav->debug;
|
|
|
|
$this->base_uri = $groupdav->base_uri;
|
|
|
|
$this->groupdav = $groupdav;
|
2010-03-07 00:06:43 +01:00
|
|
|
|
2008-11-03 10:36:20 +01:00
|
|
|
$this->agent = self::get_agent();
|
2009-10-17 11:13:36 +02:00
|
|
|
|
2010-09-25 11:08:37 +02:00
|
|
|
$this->egw_charset = translation::charset();
|
2011-11-06 10:40:33 +01:00
|
|
|
|
|
|
|
$this->accounts = $GLOBALS['egw']->accounts;
|
|
|
|
$this->acl = $GLOBALS['egw']->acl;
|
2008-05-08 22:31:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle propfind request for an application folder
|
|
|
|
*
|
|
|
|
* @param string $path
|
2012-07-15 11:15:57 +02:00
|
|
|
* @param array &$options
|
2008-05-08 22:31:32 +02:00
|
|
|
* @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')
|
|
|
|
*/
|
2012-07-15 11:15:57 +02:00
|
|
|
abstract function propfind($path,&$options,&$files,$user);
|
2008-05-08 22:31:32 +02:00
|
|
|
|
2009-10-17 11:13:36 +02:00
|
|
|
/**
|
|
|
|
* Propfind callback, if interator is used
|
|
|
|
*
|
2010-03-07 00:06:43 +01:00
|
|
|
* @param string $path
|
2009-10-17 11:13:36 +02:00
|
|
|
* @param array $filter
|
|
|
|
* @param array|boolean $start false=return all or array(start,num)
|
|
|
|
* @param int &$total
|
|
|
|
* @return array with "files" array with values for keys path and props
|
|
|
|
*/
|
2010-03-07 00:06:43 +01:00
|
|
|
function &propfind_callback($path, array $filter,$start,&$total) { }
|
2009-10-17 11:13:36 +02:00
|
|
|
|
2008-05-08 22:31:32 +02:00
|
|
|
/**
|
|
|
|
* Handle get request for an applications entry
|
|
|
|
*
|
|
|
|
* @param array &$options
|
|
|
|
* @param int $id
|
2011-03-05 11:21:32 +01:00
|
|
|
* @param int $user=null account_id
|
2008-05-08 22:31:32 +02:00
|
|
|
* @return mixed boolean true on success, false on failure or string with http status (eg. '404 Not Found')
|
|
|
|
*/
|
2011-03-05 11:21:32 +01:00
|
|
|
abstract function get(&$options,$id,$user=null);
|
2008-05-08 22:31:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle get request for an applications 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')
|
|
|
|
*/
|
|
|
|
abstract function put(&$options,$id,$user=null);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle get request for an applications 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')
|
|
|
|
*/
|
|
|
|
abstract function delete(&$options,$id);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read an entry
|
|
|
|
*
|
2012-02-10 12:44:52 +01:00
|
|
|
* @param string|int $id
|
|
|
|
* @param string $path=null implementation can use it, used in call from _common_get_put_delete
|
|
|
|
* @return array|boolean array with entry, false if no read rights, null if $id does not exist
|
2008-05-08 22:31:32 +02:00
|
|
|
*/
|
2012-02-10 12:44:52 +01:00
|
|
|
abstract function read($id /*,$path=null*/);
|
2008-05-08 22:31:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if user has the neccessary rights on an entry
|
|
|
|
*
|
|
|
|
* @param int $acl EGW_ACL_READ, EGW_ACL_EDIT or EGW_ACL_DELETE
|
2012-02-10 12:44:52 +01:00
|
|
|
* @param array|int $entry entry-array or id
|
2008-05-08 22:31:32 +02:00
|
|
|
* @return boolean null if entry does not exist, false if no access, true if access permitted
|
|
|
|
*/
|
|
|
|
abstract function check_access($acl,$entry);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add extra properties for collections
|
|
|
|
*
|
|
|
|
* @param array $props=array() regular props by the groupdav handler
|
2010-03-07 00:06:43 +01:00
|
|
|
* @param string $displayname
|
|
|
|
* @param string $base_uri=null base url of handler
|
2011-11-06 10:40:33 +01:00
|
|
|
* @param int $user=null account_id of owner of collection
|
2008-05-08 22:31:32 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
2011-11-06 10:40:33 +01:00
|
|
|
public function extra_properties(array $props=array(), $displayname, $base_uri=null, $user=null)
|
2008-05-08 22:31:32 +02:00
|
|
|
{
|
|
|
|
return $props;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the etag for an entry, can be reimplemented for other algorithm or field names
|
|
|
|
*
|
2012-02-10 12:44:52 +01:00
|
|
|
* @param array|int $event array with event or cal_id
|
|
|
|
* @return string|boolean string with etag or false
|
2008-05-08 22:31:32 +02:00
|
|
|
*/
|
|
|
|
function get_etag($entry)
|
|
|
|
{
|
|
|
|
if (!is_array($entry))
|
|
|
|
{
|
|
|
|
$entry = $this->read($entry);
|
|
|
|
}
|
|
|
|
if (!is_array($entry) || !isset($entry['id']) || !(isset($entry['modified']) || isset($entry['etag'])))
|
|
|
|
{
|
2009-04-02 14:39:52 +02:00
|
|
|
// error_log(__METHOD__."(".array2string($entry).") Cant create etag!");
|
2008-05-08 22:31:32 +02:00
|
|
|
return false;
|
|
|
|
}
|
2011-11-06 10:40:33 +01:00
|
|
|
return $entry['id'].':'.(isset($entry['etag']) ? $entry['etag'] : $entry['modified']);
|
2008-05-08 22:31:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert etag to the raw etag column value (without quotes, double colon and id)
|
|
|
|
*
|
|
|
|
* @param string $etag
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
static function etag2value($etag)
|
|
|
|
{
|
2011-11-08 22:10:37 +01:00
|
|
|
list(,$val) = explode(':',$etag,2);
|
2008-05-08 22:31:32 +02:00
|
|
|
|
|
|
|
return $val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle common stuff for get, put and delete requests:
|
|
|
|
* - application rights
|
|
|
|
* - entry level acl, incl. edit and delete rights
|
|
|
|
* - etag handling for precondition failed and not modified
|
|
|
|
*
|
|
|
|
* @param string $method GET, PUT, DELETE
|
|
|
|
* @param array &$options
|
2011-04-05 22:39:13 +02:00
|
|
|
* @param int|string &$id on return self::$path_extension got removed
|
2008-05-17 14:54:26 +02:00
|
|
|
* @param boolean &$return_no_access=false if set to true on call, instead of '403 Forbidden' the entry is returned and $return_no_access===false
|
2011-11-06 10:40:33 +01:00
|
|
|
* @param boolean $ignore_if_match=false if true, ignore If-Match precondition
|
2012-10-16 14:17:32 +02:00
|
|
|
* @param string &$etag on return of array with entry, as calculating etag is expensive for recurring events (extra DB query)
|
2010-03-07 00:06:43 +01:00
|
|
|
* @return array|string entry on success, string with http-error-code on failure, null for PUT on an unknown id
|
2008-05-08 22:31:32 +02:00
|
|
|
*/
|
2012-10-16 14:17:32 +02:00
|
|
|
function _common_get_put_delete($method,&$options,&$id,&$return_no_access=false,$ignore_if_match=false,&$etag=null)
|
2008-05-08 22:31:32 +02:00
|
|
|
{
|
2011-04-05 22:39:13 +02:00
|
|
|
if (self::$path_extension) $id = basename($id,self::$path_extension);
|
|
|
|
|
2010-10-20 01:30:16 +02:00
|
|
|
if ($this->app != 'principals' && !$GLOBALS['egw_info']['user']['apps'][$this->app])
|
2008-05-08 22:31:32 +02:00
|
|
|
{
|
2008-05-10 22:15:02 +02:00
|
|
|
if ($this->debug) error_log(__METHOD__."($method,,$id) 403 Forbidden: no app rights for '$this->app'");
|
|
|
|
return '403 Forbidden'; // no app rights
|
2008-05-08 22:31:32 +02:00
|
|
|
}
|
|
|
|
$extra_acl = $this->method2acl[$method];
|
2012-02-10 12:44:52 +01:00
|
|
|
if (!($entry = $this->read($id, $options['path'])) && ($method != 'PUT' || $entry === false) ||
|
2008-05-08 22:31:32 +02:00
|
|
|
($extra_acl != EGW_ACL_READ && $this->check_access($extra_acl,$entry) === false))
|
|
|
|
{
|
2008-05-17 14:54:26 +02:00
|
|
|
if ($return_no_access && !is_null($entry))
|
|
|
|
{
|
2010-10-31 08:56:29 +01:00
|
|
|
if ($this->debug) error_log(__METHOD__."($method,,$id,$return_no_access) \$entry=".array2string($entry).", \$return_no_access set to false");
|
2008-05-17 14:54:26 +02:00
|
|
|
$return_no_access = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ($this->debug) error_log(__METHOD__."($method,,$id) 403 Forbidden/404 Not Found: read($id)==".($entry===false?'false':'null'));
|
|
|
|
return !is_null($entry) ? '403 Forbidden' : '404 Not Found';
|
|
|
|
}
|
2008-05-08 22:31:32 +02:00
|
|
|
}
|
|
|
|
if ($entry)
|
|
|
|
{
|
|
|
|
$etag = $this->get_etag($entry);
|
|
|
|
// If the clients sends an "If-Match" header ($_SERVER['HTTP_IF_MATCH']) we check with the current etag
|
|
|
|
// of the calendar --> on failure we return 412 Precondition failed, to not overwrite the modifications
|
2011-11-06 10:40:33 +01:00
|
|
|
if (isset($_SERVER['HTTP_IF_MATCH']) && !$ignore_if_match)
|
2008-05-08 22:31:32 +02:00
|
|
|
{
|
2011-11-06 10:40:33 +01:00
|
|
|
$this->http_if_match = $_SERVER['HTTP_IF_MATCH'];
|
|
|
|
// strip of quotes around etag, if they exist, that way we allow etag with and without quotes
|
|
|
|
if ($this->http_if_match[0] == '"') $this->http_if_match = substr($this->http_if_match, 1, -1);
|
|
|
|
|
|
|
|
if ($this->http_if_match !== $etag)
|
2010-06-14 09:38:41 +02:00
|
|
|
{
|
2012-02-10 12:44:52 +01:00
|
|
|
if ($this->debug) error_log(__METHOD__."($method,path=$options[path],$id) HTTP_IF_MATCH='$_SERVER[HTTP_IF_MATCH]', etag='$etag': 412 Precondition failed".array2string($entry));
|
2010-06-14 09:38:41 +02:00
|
|
|
return '412 Precondition Failed';
|
|
|
|
}
|
2008-05-08 22:31:32 +02:00
|
|
|
}
|
2010-06-14 09:38:41 +02:00
|
|
|
if (isset($_SERVER['HTTP_IF_NONE_MATCH']))
|
2008-05-08 22:31:32 +02:00
|
|
|
{
|
2011-11-06 10:40:33 +01:00
|
|
|
$if_none_match = $_SERVER['HTTP_IF_NONE_MATCH'];
|
|
|
|
// strip of quotes around etag, if they exist, that way we allow etag with and without quotes
|
|
|
|
if ($if_none_match[0] == '"') $if_none_match = substr($if_none_match, 1, -1);
|
|
|
|
|
|
|
|
// if an IF_NONE_MATCH is given, check if we need to send a new export, or the current one is still up-to-date
|
|
|
|
if (in_array($method, array('GET','HEAD')) && $etag === $if_none_match)
|
|
|
|
{
|
|
|
|
if ($this->debug) error_log(__METHOD__."($method,,$id) HTTP_IF_NONE_MATCH='$_SERVER[HTTP_IF_NONE_MATCH]', etag='$etag': 304 Not Modified");
|
|
|
|
return '304 Not Modified';
|
|
|
|
}
|
|
|
|
if ($method == 'PUT' && ($if_none_match == '*' || $if_none_match == $etag))
|
|
|
|
{
|
|
|
|
if ($this->debug) error_log(__METHOD__."($method,,$id) HTTP_IF_NONE_MATCH='$_SERVER[HTTP_IF_NONE_MATCH]', etag='$etag': 412 Precondition failed");
|
|
|
|
return '412 Precondition Failed';
|
|
|
|
}
|
2008-05-08 22:31:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the handler for the given app
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @param string $app 'calendar', 'addressbook' or 'infolog'
|
2011-11-06 10:40:33 +01:00
|
|
|
* @param groupdav $groupdav calling class
|
2008-05-08 22:31:32 +02:00
|
|
|
* @return groupdav_handler
|
|
|
|
*/
|
2011-11-06 10:40:33 +01:00
|
|
|
static function app_handler($app, $groupdav)
|
2008-05-08 22:31:32 +02:00
|
|
|
{
|
|
|
|
static $handler_cache = array();
|
|
|
|
|
|
|
|
if (!array_key_exists($app,$handler_cache))
|
|
|
|
{
|
|
|
|
$class = $app.'_groupdav';
|
2008-05-10 22:15:02 +02:00
|
|
|
if (!class_exists($class) && !class_exists($class = 'groupdav_'.$app)) return null;
|
2008-05-08 22:31:32 +02:00
|
|
|
|
2011-11-06 10:40:33 +01:00
|
|
|
$handler_cache[$app] = new $class($app, $groupdav);
|
2008-05-08 22:31:32 +02:00
|
|
|
}
|
2010-03-07 00:06:43 +01:00
|
|
|
|
2011-11-06 10:40:33 +01:00
|
|
|
if ($debug) error_log(__METHOD__."('$app')");
|
2010-03-07 00:06:43 +01:00
|
|
|
|
2008-05-08 22:31:32 +02:00
|
|
|
return $handler_cache[$app];
|
|
|
|
}
|
2008-11-03 10:36:20 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Identify know GroupDAV agents by HTTP_USER_AGENT header
|
|
|
|
*
|
|
|
|
* @return string|boolean agent name or false
|
|
|
|
*/
|
|
|
|
static function get_agent()
|
|
|
|
{
|
|
|
|
static $agent;
|
|
|
|
|
|
|
|
if (is_null($agent))
|
|
|
|
{
|
|
|
|
$agent = false;
|
|
|
|
// identify the agent (GroupDAV client) from the HTTP_USER_AGENT header
|
|
|
|
$user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
|
|
|
|
foreach(array(
|
2012-02-29 17:46:42 +01:00
|
|
|
'carddav-sync' => 'carddav-sync', // dmfs.org CardDAV client for Android: CardDAV-Sync (Android) (like iOS/5.0.1 (9A405) dataaccessd/1.0) gzip
|
2010-09-25 11:08:37 +02:00
|
|
|
'iphone' => 'iphone', // Apple iPhone iCal
|
2011-08-02 15:01:21 +02:00
|
|
|
'davkit' => 'davkit', // Apple iCal 10.6
|
|
|
|
'coredav' => 'coredav', // Apple iCal 10.7
|
2011-11-08 22:10:37 +01:00
|
|
|
'calendarstore' => 'calendarstore', // Apple iCal 5.0.1 under OS X 10.7.2
|
2012-09-26 10:06:48 +02:00
|
|
|
'calendaragent/' => 'calendaragent', // Apple iCal OS X 10.8*: Mac OS X/10.8.2 (12C54) CalendarAgent/55
|
2010-09-25 11:08:37 +02:00
|
|
|
'dataaccess' => 'dataaccess', // Apple addressbook iPhone
|
2011-08-02 15:01:21 +02:00
|
|
|
'cfnetwork' => 'cfnetwork', // Apple Addressbook 10.6/7
|
2012-09-26 10:06:48 +02:00
|
|
|
'addressbook/' => 'cfnetwork', // Apple Addressbook OS X 10.8*: Mac OS X/10.8.2 (12C54) AddressBook/1167
|
2008-11-03 10:36:20 +01:00
|
|
|
'bionicmessage.net' => 'funambol', // funambol GroupDAV connector from bionicmessage.net
|
|
|
|
'zideone' => 'zideone', // zideone outlook plugin
|
2012-02-10 12:46:57 +01:00
|
|
|
'lightning' => 'lightning', // Lighting (incl. SOGo connector for addressbook)
|
2010-10-31 08:56:29 +01:00
|
|
|
'webkit' => 'webkit', // Webkit Browser (also reports KHTML!)
|
2011-09-26 12:21:25 +02:00
|
|
|
'akonadi' => 'akonadi', // new KDE PIM framework (also reports KHTML!)
|
2008-11-03 10:36:20 +01:00
|
|
|
'khtml' => 'kde', // KDE clients
|
2010-10-31 08:56:29 +01:00
|
|
|
'neon' => 'neon',
|
|
|
|
'ical4ol' => 'ical4ol', // iCal4OL client
|
2011-03-07 15:00:37 +01:00
|
|
|
'evolution' => 'evolution', // Evolution
|
2012-02-10 12:46:57 +01:00
|
|
|
'thunderbird' => 'thunderbird', // SOGo connector for addressbook, no Lightning installed
|
2008-11-03 10:36:20 +01:00
|
|
|
) as $pattern => $name)
|
|
|
|
{
|
|
|
|
if (strpos($user_agent,$pattern) !== false)
|
|
|
|
{
|
|
|
|
$agent = $name;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$agent)
|
|
|
|
{
|
2010-07-08 11:41:06 +02:00
|
|
|
//error_log("Unrecogniced GroupDAV client: HTTP_USER_AGENT='$_SERVER[HTTP_USER_AGENT]'!");
|
2008-11-03 10:36:20 +01:00
|
|
|
}
|
2010-04-21 19:44:36 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
switch ($agent)
|
|
|
|
{
|
|
|
|
case 'cfnetwork':
|
|
|
|
if (preg_match('/address%20book\/([0-9.]+)/', $user_agent, $matches))
|
|
|
|
{
|
|
|
|
if ((int)$matches[1] < 868) $agent .= '_old';
|
|
|
|
}
|
2010-10-31 08:56:29 +01:00
|
|
|
break;
|
2011-09-26 12:21:25 +02:00
|
|
|
case 'kde':
|
|
|
|
// Akonadi (new KDE Pim framework) unfortunately has same user-agent as old kde
|
|
|
|
// we can only assume KDE 4.7+ uses Akonadi native resource, while below this was not available
|
|
|
|
// Unfortunately the old pre-Akonadi GroupDAV resource can still be used, but we have no way of detecting it
|
|
|
|
if (preg_match('/KHTML\/([0-9.]+)/', $_SERVER['HTTP_USER_AGENT'], $matches) && (float)$matches[1] >= 4.7)
|
|
|
|
{
|
|
|
|
$agent = 'akonadi';
|
|
|
|
}
|
|
|
|
break;
|
2010-04-21 19:44:36 +02:00
|
|
|
}
|
|
|
|
}
|
2008-11-03 10:36:20 +01:00
|
|
|
}
|
2010-10-31 08:56:29 +01:00
|
|
|
|
2010-08-15 08:42:05 +02:00
|
|
|
if ($debug) error_log(__METHOD__."GroupDAV client: $agent");
|
2010-10-31 08:56:29 +01:00
|
|
|
|
2008-11-03 10:36:20 +01:00
|
|
|
return $agent;
|
|
|
|
}
|
2011-11-06 10:40:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return priviledges for current user, default is read and read-current-user-privilege-set
|
|
|
|
*
|
|
|
|
* Priviledges are for the collection, not the resources / entries!
|
|
|
|
*
|
|
|
|
* @param string $path path of collection
|
|
|
|
* @param int $user=null owner of the collection, default current user
|
|
|
|
* @return array with privileges
|
|
|
|
*/
|
|
|
|
public function current_user_privileges($path, $user=null)
|
|
|
|
{
|
|
|
|
static $grants;
|
|
|
|
if (is_null($grants))
|
|
|
|
{
|
|
|
|
$grants = $this->acl->get_grants($this->app, $this->app != 'addressbook');
|
|
|
|
}
|
|
|
|
$priviledes = array('read-current-user-privilege-set' => 'read-current-user-privilege-set');
|
|
|
|
|
|
|
|
if (!$user || $grants[$user] & EGW_ACL_READ)
|
|
|
|
{
|
|
|
|
$priviledes['read'] = 'read';
|
2012-02-10 12:01:24 +01:00
|
|
|
// allows on all calendars/addressbooks to write properties, as we store them on a per-user basis
|
|
|
|
// and only allow to modify explicit named properties in CalDAV, CardDAV or Calendarserver name-space
|
|
|
|
$priviledes['write-properties'] = 'write-properties';
|
2011-11-06 10:40:33 +01:00
|
|
|
}
|
|
|
|
if (!$user || $grants[$user] & EGW_ACL_ADD)
|
|
|
|
{
|
|
|
|
$priviledes['bind'] = 'bind'; // PUT for new resources
|
|
|
|
}
|
|
|
|
if (!$user || $grants[$user] & EGW_ACL_EDIT)
|
|
|
|
{
|
|
|
|
$priviledes['write-content'] = 'write-content'; // otherwise iOS calendar does not allow to add events
|
|
|
|
}
|
|
|
|
if (!$user || $grants[$user] & EGW_ACL_DELETE)
|
|
|
|
{
|
|
|
|
$priviledes['unbind'] = 'unbind'; // DELETE
|
|
|
|
}
|
|
|
|
// copy/move of existing resources might require write-properties, thought we do not support an explicit PROPATCH
|
|
|
|
return $priviledes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the path/name for an entry
|
|
|
|
*
|
|
|
|
* @param array $entry
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function get_path($entry)
|
|
|
|
{
|
|
|
|
return $entry[self::$path_attr].self::$path_extension;
|
|
|
|
}
|
|
|
|
|
2012-02-10 12:28:56 +01:00
|
|
|
/**
|
|
|
|
* Send response-headers for a PUT (or POST with add-member query parameter)
|
|
|
|
*
|
|
|
|
* @param int|array $entry id or array of new created entry
|
|
|
|
* @param string $path
|
|
|
|
* @param int|string $retval
|
|
|
|
* @param boolean $path_attr_is_name=true true: path_attr is ca(l|rd)dav_name, false: id (GroupDAV needs Location header)
|
|
|
|
*/
|
|
|
|
function put_response_headers($entry, $path, $retval, $path_attr_is_name=true)
|
|
|
|
{
|
|
|
|
// we should not return an etag here, as EGroupware never stores ical/vcard byte-by-byte
|
2012-02-10 12:46:57 +01:00
|
|
|
// as SOGO Connector requires ETag header to recognice as successful PUT, we are sending them again for it
|
2012-02-20 12:34:15 +01:00
|
|
|
// --> as all clients dislike not getting an ETag for a PUT, we sending it again even not storing byte-by-byte
|
|
|
|
//if (get_class($this) == 'addressbook_groupdav' && in_array(self::get_agent(),array('thunderbird','lightning')))
|
2012-02-10 12:46:57 +01:00
|
|
|
{
|
|
|
|
header('ETag: "'.$this->get_etag($entry).'"');
|
|
|
|
}
|
2012-02-10 12:28:56 +01:00
|
|
|
// send Location header only if we dont use caldav_name as path-attribute or
|
|
|
|
if ($retval !== true && (!$path_attr_is_name ||
|
|
|
|
// POST with add-member query parameter
|
|
|
|
$_SERVER['REQUEST_METHOD'] == 'POST' && isset($_GET['add-member'])))
|
|
|
|
{
|
|
|
|
$path = preg_replace('|(.*)/[^/]*|', '\1/', $path);
|
|
|
|
header('Location: '.$this->base_uri.$path.$this->get_path($entry));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-10 12:27:26 +01:00
|
|
|
/**
|
|
|
|
* Return calendars/addressbooks shared from other users with the current one
|
|
|
|
*
|
|
|
|
* return array account_id => account_lid pairs
|
|
|
|
*/
|
|
|
|
function get_shared()
|
|
|
|
{
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return appliction specific settings
|
|
|
|
*
|
2012-02-14 18:39:40 +01:00
|
|
|
* @param array $hook_data
|
|
|
|
* @return array of array with settings
|
2012-02-10 12:27:26 +01:00
|
|
|
*/
|
2012-02-14 18:39:40 +01:00
|
|
|
static function get_settings($hook_data)
|
2012-02-10 12:27:26 +01:00
|
|
|
{
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2011-11-06 10:40:33 +01:00
|
|
|
/**
|
|
|
|
* Add a resource
|
|
|
|
*
|
|
|
|
* @param string $path path of collection, NOT entry!
|
|
|
|
* @param array $entry
|
|
|
|
* @param array $props
|
|
|
|
* @return array with values for keys 'path' and 'props'
|
|
|
|
*/
|
|
|
|
public function add_resource($path, array $entry, array $props)
|
|
|
|
{
|
|
|
|
foreach(array(
|
|
|
|
'getetag' => $this->get_etag($entry),
|
|
|
|
'getcontenttype' => 'text/calendar',
|
|
|
|
'getlastmodified' => $entry['modified'],
|
|
|
|
'displayname' => $entry['title'],
|
|
|
|
) as $name => $value)
|
|
|
|
{
|
|
|
|
if (!isset($props[$name]))
|
|
|
|
{
|
|
|
|
$props[$name] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if requested add privileges
|
|
|
|
$privileges = array('read', 'read-current-user-privilege-set');
|
|
|
|
if ($this->groupdav->prop_requested('current-user-privilege-set') === true && !isset($props['current-user-privilege-set']))
|
|
|
|
{
|
|
|
|
if ($this->check_access(EGW_ACL_EDIT, $entry))
|
|
|
|
{
|
|
|
|
$privileges[] = 'write-content';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($this->groupdav->prop_requested('owner') === true && !isset($props['owner']) &&
|
|
|
|
($account_lid = $this->accounts->name2id($entry['owner'])))
|
|
|
|
{
|
|
|
|
$type = $this->accounts->get_type($entry['owner']) == 'u' ? 'users' : 'groups';
|
|
|
|
$props['owner'] = HTTP_WebDAV_Server::mkprop('href', $this->base_uri.'/principals/'.$type.'/'.$account_lid.'/');
|
|
|
|
}
|
|
|
|
// we urldecode here, as HTTP_WebDAV_Server uses a minimal (#?%) urlencoding for incomming pathes and urlencodes pathes in propfind
|
|
|
|
return $this->groupdav->add_resource($path.urldecode($this->get_path($entry)), $props, $privileges);
|
|
|
|
}
|
2012-10-16 13:08:33 +02:00
|
|
|
|
2012-10-16 14:17:32 +02:00
|
|
|
/**
|
|
|
|
* Return base uri, making sure it's either a full uri (incl. protocoll and host) or just a path
|
|
|
|
*
|
|
|
|
* base_uri of WebDAV class can be both, depending on EGroupware config
|
|
|
|
*
|
|
|
|
* @param boolean $full_uri=true
|
|
|
|
* @return string eg. https://domain.com/egroupware/groupdav.php
|
|
|
|
*/
|
|
|
|
public function base_uri($full_uri=true)
|
|
|
|
{
|
|
|
|
static $uri;
|
|
|
|
static $path;
|
|
|
|
|
|
|
|
if (!isset($uri))
|
|
|
|
{
|
|
|
|
$uri = $path = $this->groupdav->base_uri;
|
|
|
|
if ($uri[0] == '/')
|
|
|
|
{
|
|
|
|
$uri = ($_SERVER["HTTPS"] === "on" ? "https:" : "http:") .'//' . $_SERVER['HTTP_HOST'] . $uri;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$path = parse_url($uri, PHP_URL_PATH);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $full_uri ? $uri : $path;
|
|
|
|
}
|
|
|
|
|
2012-10-16 13:11:29 +02:00
|
|
|
/**
|
|
|
|
* sync-token to be filled by propfind_callback and returned by get_sync_token method
|
|
|
|
*/
|
|
|
|
protected $sync_collection_token;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Query sync-token from a just run sync-collection report
|
|
|
|
*
|
|
|
|
* Modified time is taken from value filled by propfind_callback in sync_collection_token.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param int $user parameter necessary to call getctag, if no $token specified
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_sync_collection_token($path, $user=null)
|
|
|
|
{
|
|
|
|
return $this->get_sync_token($path, $user, $this->sync_collection_token);
|
|
|
|
}
|
|
|
|
|
2012-10-16 13:08:33 +02:00
|
|
|
/**
|
|
|
|
* Query sync-token
|
|
|
|
*
|
|
|
|
* We use ctag / max. modification time as sync-token. As garnularity is 1sec, we can never be sure,
|
|
|
|
* if there are more modifications to come in the current second.
|
|
|
|
*
|
|
|
|
* Therefor we are never returning current time, but 1sec less!
|
|
|
|
*
|
2012-10-16 13:11:29 +02:00
|
|
|
* Modified time is either taken from value filled by propfind_callback in $this->sync_token or
|
|
|
|
* by call to getctag();
|
|
|
|
*
|
2012-10-16 13:08:33 +02:00
|
|
|
* @param string $path
|
2012-10-16 13:11:29 +02:00
|
|
|
* @param int $user parameter necessary to call getctag, if no $token specified
|
|
|
|
* @param int $token=null modification time, default call getctag($path, $user) to fetch it
|
2012-10-16 13:08:33 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-10-16 13:11:29 +02:00
|
|
|
public function get_sync_token($path, $user, $token=null)
|
2012-10-16 13:08:33 +02:00
|
|
|
{
|
2012-10-16 13:11:29 +02:00
|
|
|
if (!isset($token)) $token = $this->getctag($path, $user);
|
2012-10-16 13:08:33 +02:00
|
|
|
|
2012-10-16 13:11:29 +02:00
|
|
|
// never return current time, as more modifications might happen due to second granularity --> return 1sec less
|
|
|
|
if ($token >= (int)$GLOBALS['egw_info']['flags']['page_start_time'])
|
|
|
|
{
|
|
|
|
$token = (int)$GLOBALS['egw_info']['flags']['page_start_time'] - 1;
|
|
|
|
}
|
|
|
|
return $this->base_uri().$path.$token;
|
2012-10-16 13:08:33 +02:00
|
|
|
}
|
2009-04-02 14:39:52 +02:00
|
|
|
}
|
|
|
|
|
2009-10-17 11:13:36 +02:00
|
|
|
/**
|
|
|
|
* Iterator for propfinds using propfind callback of a groupdav_handler to query results in chunks
|
|
|
|
*
|
|
|
|
* The propfind method just computes a filter and then returns an instance of this iterator instead of the files:
|
|
|
|
*
|
|
|
|
* function propfind($path,$options,&$files,$user,$id='')
|
|
|
|
* {
|
|
|
|
* $filter = array();
|
|
|
|
* // compute filter from path, options, ...
|
|
|
|
*
|
|
|
|
* $files['files'] = new groupdav_propfind_iterator($this,$filter,$files['files']);
|
|
|
|
*
|
|
|
|
* return true;
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
class groupdav_propfind_iterator implements Iterator
|
|
|
|
{
|
2010-03-07 00:06:43 +01:00
|
|
|
/**
|
|
|
|
* current path
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $path;
|
|
|
|
|
2009-10-17 11:13:36 +02:00
|
|
|
/**
|
|
|
|
* Handler to call for entries
|
|
|
|
*
|
|
|
|
* @var groupdav_handler
|
|
|
|
*/
|
|
|
|
protected $handler;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter of propfind call
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $filter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extra responses to return too
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-03-07 00:06:43 +01:00
|
|
|
protected $common_files;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* current chunk
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2009-10-17 11:13:36 +02:00
|
|
|
protected $files;
|
|
|
|
|
2010-03-07 00:06:43 +01:00
|
|
|
|
2009-10-17 11:13:36 +02:00
|
|
|
/**
|
|
|
|
* Start value for callback
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $start=0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of entries queried from callback in one call
|
|
|
|
*
|
|
|
|
*/
|
2009-10-19 09:47:06 +02:00
|
|
|
const CHUNK_SIZE = 500;
|
2009-10-17 11:13:36 +02:00
|
|
|
|
2010-03-03 09:17:20 +01:00
|
|
|
/**
|
|
|
|
* Log calls via error_log()
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
public $debug = false;
|
|
|
|
|
2010-03-07 00:06:43 +01:00
|
|
|
/**
|
|
|
|
|
2009-10-17 11:13:36 +02:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param groupdav_handler $handler
|
|
|
|
* @param array $filter filter for propfind call
|
2011-04-05 17:32:20 +02:00
|
|
|
* @param array $files=array() extra files/responses to return too
|
2009-10-17 11:13:36 +02:00
|
|
|
*/
|
2011-04-05 17:32:20 +02:00
|
|
|
public function __construct(groupdav_handler $handler, $path, array $filter,array &$files=array())
|
2009-10-17 11:13:36 +02:00
|
|
|
{
|
2010-03-07 00:06:43 +01:00
|
|
|
if ($this->debug) error_log(__METHOD__."('$path', ".array2string($filter).",)");
|
|
|
|
$this->path = $path;
|
2009-10-17 11:13:36 +02:00
|
|
|
$this->handler = $handler;
|
|
|
|
$this->filter = $filter;
|
2011-04-05 17:32:20 +02:00
|
|
|
$this->files = $this->common_files = $files;
|
2009-10-17 11:13:36 +02:00
|
|
|
reset($this->files);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the current element
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function current()
|
|
|
|
{
|
2010-03-03 09:17:20 +01:00
|
|
|
if ($this->debug) error_log(__METHOD__."() returning ".array2string(current($this->files)));
|
2009-10-17 11:13:36 +02:00
|
|
|
return current($this->files);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the key of the current element
|
|
|
|
*
|
|
|
|
* @return int|string
|
|
|
|
*/
|
|
|
|
public function key()
|
|
|
|
{
|
2010-03-07 00:06:43 +01:00
|
|
|
$current = current($this->files);
|
2009-10-17 11:13:36 +02:00
|
|
|
|
2010-03-03 09:17:20 +01:00
|
|
|
if ($this->debug) error_log(__METHOD__."() returning ".array2string($current['path']));
|
2009-10-17 11:13:36 +02:00
|
|
|
return $current['path']; // we return path as key
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move forward to next element (called after each foreach loop)
|
|
|
|
*/
|
|
|
|
public function next()
|
|
|
|
{
|
|
|
|
if (next($this->files) !== false)
|
|
|
|
{
|
2010-03-03 09:17:20 +01:00
|
|
|
if ($this->debug) error_log(__METHOD__."() returning TRUE");
|
2009-10-17 11:13:36 +02:00
|
|
|
return true;
|
|
|
|
}
|
2011-04-05 17:32:20 +02:00
|
|
|
// check if previous query gave less then CHUNK_SIZE entries --> we're done
|
|
|
|
if ($this->start && count($this->files) < self::CHUNK_SIZE)
|
|
|
|
{
|
|
|
|
if ($this->debug) error_log(__METHOD__."() returning FALSE (no more entries)");
|
|
|
|
return false;
|
|
|
|
}
|
2010-03-15 10:55:16 +01:00
|
|
|
// try query further files via propfind callback of handler and store result in $this->files
|
|
|
|
$this->files = $this->handler->propfind_callback($this->path,$this->filter,array($this->start,self::CHUNK_SIZE));
|
|
|
|
if (!is_array($this->files) || !($entries = count($this->files)))
|
2009-10-17 11:13:36 +02:00
|
|
|
{
|
2010-03-03 09:17:20 +01:00
|
|
|
if ($this->debug) error_log(__METHOD__."() returning FALSE (no more entries)");
|
2009-10-17 11:13:36 +02:00
|
|
|
return false; // no further entries
|
|
|
|
}
|
2011-04-05 17:32:20 +02:00
|
|
|
$this->start += self::CHUNK_SIZE;
|
2009-10-17 11:13:36 +02:00
|
|
|
reset($this->files);
|
|
|
|
|
2011-04-05 17:32:20 +02:00
|
|
|
if ($this->debug) error_log(__METHOD__."() this->start=$this->start, entries=$entries, count(this->files)=".count($this->files)." returning ".array2string(current($this->files) !== false));
|
2010-03-07 00:06:43 +01:00
|
|
|
|
2009-10-17 11:13:36 +02:00
|
|
|
return current($this->files) !== false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rewind the Iterator to the first element (called at beginning of foreach loop)
|
|
|
|
*/
|
|
|
|
public function rewind()
|
|
|
|
{
|
2010-03-03 09:17:20 +01:00
|
|
|
if ($this->debug) error_log(__METHOD__."()");
|
2009-10-17 11:13:36 +02:00
|
|
|
|
2010-03-03 09:17:20 +01:00
|
|
|
$this->start = 0;
|
2011-04-05 17:32:20 +02:00
|
|
|
$this->files = $this->common_files;
|
2011-04-05 22:39:13 +02:00
|
|
|
if (!$this->files) $this->next(); // otherwise valid will return false and nothing get returned
|
2010-03-03 09:17:20 +01:00
|
|
|
reset($this->files);
|
2009-10-17 11:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if current position is valid
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function valid ()
|
|
|
|
{
|
2010-03-03 09:17:20 +01:00
|
|
|
if ($this->debug) error_log(__METHOD__."() returning ".array2string(current($this->files) !== false));
|
2009-10-17 11:13:36 +02:00
|
|
|
return current($this->files) !== false;
|
|
|
|
}
|
|
|
|
}
|