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>
|
2009-10-17 11:13:36 +02:00
|
|
|
* @copyright (c) 2007-9 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
|
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
|
|
|
|
*/
|
2008-05-17 14:54:26 +02:00
|
|
|
var $debug = 0;
|
2008-05-08 22:31:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* eGW's charset
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $egw_charset;
|
|
|
|
/**
|
|
|
|
* Reference to the translation class
|
|
|
|
*
|
|
|
|
* @var translation
|
|
|
|
*/
|
|
|
|
var $translation;
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
/**
|
|
|
|
* Base url of handler, need to prefix all pathes not automatic handled by HTTP_WebDAV_Server
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
2008-05-10 22:15:02 +02:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2008-05-17 14:54:26 +02:00
|
|
|
* @param string $app 'calendar', 'addressbook' or 'infolog'
|
|
|
|
* @param int $debug=null debug-level to set
|
|
|
|
* @param string $base_uri=null base url of handler
|
2008-05-10 22:15:02 +02:00
|
|
|
*/
|
2008-05-17 14:54:26 +02:00
|
|
|
function __construct($app,$debug=null,$base_uri=null)
|
2008-05-08 22:31:32 +02:00
|
|
|
{
|
2009-04-02 14:39:52 +02:00
|
|
|
//error_log(__METHOD__." called");
|
2008-05-08 22:31:32 +02:00
|
|
|
$this->app = $app;
|
2009-04-02 14:39:52 +02:00
|
|
|
#if (!is_null($debug)) $this->debug = $debug = 3;
|
2008-05-17 14:54:26 +02:00
|
|
|
$this->base_uri = is_null($base_uri) ? $base_uri : $_SERVER['SCRIPT_NAME'];
|
2008-11-03 10:36:20 +01:00
|
|
|
$this->agent = self::get_agent();
|
2009-10-17 11:13:36 +02:00
|
|
|
|
2008-05-08 22:31:32 +02:00
|
|
|
$this->translation =& $GLOBALS['egw']->translation;
|
|
|
|
$this->egw_charset = $this->translation->charset();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle propfind request for an application 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')
|
|
|
|
*/
|
|
|
|
abstract function propfind($path,$options,&$files,$user);
|
|
|
|
|
2009-10-17 11:13:36 +02:00
|
|
|
/**
|
|
|
|
* Propfind callback, if interator is used
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
function &propfind_callback(array $filter,$start,&$total) { }
|
|
|
|
|
2008-05-08 22:31:32 +02:00
|
|
|
/**
|
|
|
|
* 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 get(&$options,$id);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* @param string/int $id
|
|
|
|
* @return array/boolean array with entry, false if no read rights, null if $id does not exist
|
|
|
|
*/
|
|
|
|
abstract function read($id);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if user has the neccessary rights on an entry
|
|
|
|
*
|
|
|
|
* @param int $acl EGW_ACL_READ, EGW_ACL_EDIT or EGW_ACL_DELETE
|
|
|
|
* @param array/int $entry entry-array or id
|
|
|
|
* @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
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
static function extra_properties(array $props=array())
|
|
|
|
{
|
|
|
|
return $props;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the etag for an entry, can be reimplemented for other algorithm or field names
|
|
|
|
*
|
|
|
|
* @param array/int $event array with event or cal_id
|
|
|
|
* @return string/boolean string with etag or false
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
return '"'.$entry['id'].':'.(isset($entry['etag']) ? $entry['etag'] : $entry['modified']).'"';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert etag to the raw etag column value (without quotes, double colon and id)
|
|
|
|
*
|
|
|
|
* @param string $etag
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
static function etag2value($etag)
|
|
|
|
{
|
|
|
|
list(,$val) = explode(':',substr($etag,1,-1),2);
|
|
|
|
|
|
|
|
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
|
|
|
|
* @param int $id
|
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
|
2008-05-08 22:31:32 +02:00
|
|
|
* @return array/string entry on success, string with http-error-code on failure, null for PUT on an unknown id
|
|
|
|
*/
|
2008-05-17 14:54:26 +02:00
|
|
|
function _common_get_put_delete($method,&$options,$id,&$return_no_access=false)
|
2008-05-08 22:31:32 +02:00
|
|
|
{
|
2008-05-10 22:15:02 +02:00
|
|
|
if (!in_array($this->app,array('principals','groups')) && !$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];
|
2008-05-20 06:59:26 +02:00
|
|
|
if (!($entry = $this->read($id)) && ($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))
|
|
|
|
{
|
|
|
|
if ($this->debug) error_log(__METHOD__."($method,,$id,$return_no_access) is_null(\$entry)=".(int)is_null($entry).", set to false");
|
|
|
|
$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
|
|
|
|
if (isset($_SERVER['HTTP_IF_MATCH']) && ($this->http_if_match = $_SERVER['HTTP_IF_MATCH']) != $etag)
|
|
|
|
{
|
|
|
|
if ($this->debug) error_log(__METHOD__."($method,,$id) HTTP_IF_MATCH='$_SERVER[HTTP_IF_MATCH]', etag='$etag': 412 Precondition failed");
|
|
|
|
return '412 Precondition Failed';
|
|
|
|
}
|
|
|
|
// 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 ($method == 'GET' && isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag)
|
|
|
|
{
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the handler for the given app
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @param string $app 'calendar', 'addressbook' or 'infolog'
|
|
|
|
* @param int $debug=null debug-level to set
|
2008-05-17 14:54:26 +02:00
|
|
|
* @param string $base_uri=null base url of handler
|
2008-05-08 22:31:32 +02:00
|
|
|
* @return groupdav_handler
|
|
|
|
*/
|
2008-05-17 14:54:26 +02:00
|
|
|
static function &app_handler($app,$debug=null,$base_uri=null)
|
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
|
|
|
|
2008-05-17 14:54:26 +02:00
|
|
|
$handler_cache[$app] = new $class($app,$debug,$base_uri);
|
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(
|
|
|
|
'davkit' => 'davkit', // Apple iCal
|
|
|
|
'bionicmessage.net' => 'funambol', // funambol GroupDAV connector from bionicmessage.net
|
|
|
|
'zideone' => 'zideone', // zideone outlook plugin
|
|
|
|
'lightning' => 'lightning', // Lighting (SOGo connector for addressbook)
|
|
|
|
'khtml' => 'kde', // KDE clients
|
|
|
|
'cadaver' => 'cadaver',
|
|
|
|
) as $pattern => $name)
|
|
|
|
{
|
|
|
|
if (strpos($user_agent,$pattern) !== false)
|
|
|
|
{
|
|
|
|
$agent = $name;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$agent)
|
|
|
|
{
|
|
|
|
error_log("Unrecogniced GroupDAV client: HTTP_USER_AGENT='$_SERVER[HTTP_USER_AGENT]'!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $agent;
|
|
|
|
}
|
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
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
protected $files;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2009-10-17 11:13:36 +02:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param groupdav_handler $handler
|
|
|
|
* @param array $filter filter for propfind call
|
|
|
|
* @param array $files=null extra files/responses to return too
|
|
|
|
*/
|
|
|
|
public function __construct(groupdav_handler $handler,array $filter,array &$files=null)
|
|
|
|
{
|
2010-03-03 09:17:20 +01:00
|
|
|
if ($this->debug) error_log(__METHOD__."(,".array2string($filter).",)");
|
|
|
|
|
2009-10-17 11:13:36 +02:00
|
|
|
$this->handler = $handler;
|
|
|
|
$this->filter = $filter;
|
|
|
|
$this->files = $files;
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
$current = $this->current();
|
|
|
|
|
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;
|
|
|
|
}
|
2010-03-03 09:17:20 +01:00
|
|
|
if (is_array($this->files) && count($this->files) < self::CHUNK_SIZE) // less entries then asked --> no further available
|
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
|
|
|
|
}
|
|
|
|
// try query further files via propfind callback of handler and store result in $this->files
|
|
|
|
$this->files = $this->handler->propfind_callback($this->filter,array($this->start,self::CHUNK_SIZE));
|
|
|
|
$this->start += self::CHUNK_SIZE;
|
|
|
|
reset($this->files);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
// query first set of files via propfind callback of handler and store result in $this->files
|
|
|
|
$this->start = 0;
|
|
|
|
$this->files = $this->handler->propfind_callback($this->filter,array($this->start,self::CHUNK_SIZE));
|
|
|
|
$this->start += self::CHUNK_SIZE;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|