2008-05-08 22:31:32 +02:00
< ? php
/**
2016-04-02 10:40:34 +02:00
* EGroupware : CalDAV / CardDAV / GroupDAV access : Addressbook handler
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 addressbook
2016-04-02 12:44:17 +02:00
* @ subpackage carddav
2008-05-08 22:31:32 +02:00
* @ author Ralf Becker < RalfBecker - AT - outdoor - training . de >
2016-03-06 14:45:15 +01:00
* @ copyright ( c ) 2007 - 16 by Ralf Becker < RalfBecker - AT - outdoor - training . de >
2008-05-08 22:31:32 +02:00
* @ version $Id $
*/
2016-03-06 14:45:15 +01:00
use EGroupware\Api ;
2016-04-29 12:41:53 +02:00
use EGroupware\Api\Acl ;
2021-09-15 18:45:32 +02:00
use EGroupware\Api\Contacts\JsContact ;
2016-03-06 14:45:15 +01:00
2008-05-08 22:31:32 +02:00
/**
2016-04-02 12:44:17 +02:00
* CalDAV / CardDAV / GroupDAV access : Addressbook handler
2009-10-17 11:13:36 +02:00
*
2016-04-02 12:44:17 +02:00
* Propfind now uses a Api\CalDAV\PropfindIterator with a callback to query huge addressbooks in chunk ,
2009-10-17 11:13:36 +02:00
* without getting into problems with memory_limit .
2011-09-28 11:47:47 +02:00
*
2016-04-02 12:44:17 +02:00
* Permanent error_log () calls should use $this -> caldav -> log ( $str ) instead , to be send to PHP error_log ()
2012-02-21 21:04:45 +01:00
* and our request - log ( prefixed with " ### " after request and response , like exceptions ) .
2008-05-08 22:31:32 +02:00
*/
2016-04-02 12:44:17 +02:00
class addressbook_groupdav extends Api\CalDAV\Handler
2008-05-08 22:31:32 +02:00
{
/**
* bo class of the application
*
2016-04-02 12:44:17 +02:00
* @ var Api\Contacts
2008-05-08 22:31:32 +02:00
*/
var $bo ;
var $filter_prop2cal = array (
'UID' => 'uid' ,
//'NICKNAME',
'EMAIL' => 'email' ,
'FN' => 'n_fn' ,
2011-10-05 08:50:26 +02:00
'ORG' => 'org_name' ,
2008-05-08 22:31:32 +02:00
);
/**
* Charset for exporting data , as some clients ignore the headers specifying the charset
*
* @ var string
*/
var $charset = 'utf-8' ;
2009-08-07 09:15:37 +02:00
2012-02-09 21:09:49 +01:00
/**
* 'addressbook_home_set' preference already exploded as array
*
* A = all available addressbooks
* G = primary group
* D = distribution lists as groups
* O = sync all in one ( /< username >/ addressbook / )
2023-10-19 20:34:38 +02:00
* or numerical account_id , but not user itself
2012-02-09 21:09:49 +01:00
*
* @ var array
*/
var $home_set_pref ;
2021-09-20 16:01:22 +02:00
/**
* Prefix for JsCardGroup id
*/
const JS_CARDGROUP_ID_PREFIX = 'list-' ;
2023-02-15 12:40:12 +01:00
/**
* Contains IDs for multiget REPORT to be able to report missing ones
*
* @ var string []
*/
var $requested_multiget_ids ;
2008-05-17 15:11:46 +02:00
/**
* Constructor
*
* @ param string $app 'calendar' , 'addressbook' or 'infolog'
2016-04-02 12:44:17 +02:00
* @ param Api\CalDAV $caldav calling class
2008-05-17 15:11:46 +02:00
*/
2016-04-02 12:44:17 +02:00
function __construct ( $app , Api\CalDAV $caldav )
2008-05-08 22:31:32 +02:00
{
2016-04-02 12:44:17 +02:00
parent :: __construct ( $app , $caldav );
2008-05-08 22:31:32 +02:00
2016-04-02 12:44:17 +02:00
$this -> bo = new Api\Contacts ();
2011-04-05 22:39:13 +02:00
2021-09-16 20:53:43 +02:00
if ( Api\CalDAV :: isJSON ())
{
self :: $path_attr = 'id' ;
self :: $path_extension = '' ;
}
2011-04-05 22:39:13 +02:00
// since 1.9.007 we allow clients to specify the URL when creating a new contact, as specified by CardDAV
2012-02-29 10:58:15 +01:00
// LDAP does NOT have a carddav_name attribute --> stick with id mapped to LDAP attribute uid
2021-09-16 20:53:43 +02:00
elseif ( version_compare ( $GLOBALS [ 'egw_info' ][ 'apps' ][ 'api' ][ 'version' ], '1.9.007' , '<' ) ||
2013-06-29 13:41:55 +02:00
$this -> bo -> contact_repository != 'sql' ||
$this -> bo -> account_repository != 'sql' && strpos ( $_SERVER [ 'REQUEST_URI' ] . '/' , '/addressbook-accounts/' ) !== false )
2011-04-05 22:39:13 +02:00
{
2016-04-02 12:44:17 +02:00
self :: $path_extension = '.vcf' ;
2011-04-05 22:39:13 +02:00
}
else
{
2016-04-02 12:44:17 +02:00
self :: $path_attr = 'carddav_name' ;
self :: $path_extension = '' ;
2011-04-05 22:39:13 +02:00
}
2012-02-29 10:58:15 +01:00
if ( $this -> debug ) error_log ( __METHOD__ . " () contact_repository= { $this -> bo -> contact_repository } , account_repository= { $this -> bo -> account_repository } , REQUEST_URI= $_SERVER[REQUEST_URI] --> path_attr= " . self :: $path_attr . " , path_extension= " . self :: $path_extension );
2021-11-05 14:43:32 +01:00
$this -> home_set_pref = $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'groupdav' ][ 'addressbook-home-set' ] ? ? [];
if ( ! is_array ( $this -> home_set_pref ))
{
$this -> home_set_pref = $this -> home_set_pref ? explode ( ',' , $this -> home_set_pref ) : array ();
}
2012-02-09 21:09:49 +01:00
// silently switch "Sync all into one" preference on for OS X addressbook, as it only supports one AB
// this restores behavior before Lion (10.7), where AB synced all ABs contained in addressbook-home-set
if ( substr ( self :: get_agent (), 0 , 9 ) == 'cfnetwork' && ! in_array ( 'O' , $this -> home_set_pref ))
{
$this -> home_set_pref [] = 'O' ;
}
2008-05-17 15:11:46 +02:00
}
2008-05-08 22:31:32 +02:00
/**
* Handle propfind in the addressbook folder
*
* @ param string $path
2012-06-27 22:08:56 +02:00
* @ param array & $options
2008-05-08 22:31:32 +02:00
* @ param array & $files
* @ param int $user account_id
2016-04-02 10:40:34 +02:00
* @ param string $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' )
*/
2012-06-27 22:08:56 +02:00
function propfind ( $path , & $options , & $files , $user , $id = '' )
2008-05-08 22:31:32 +02:00
{
2008-05-19 10:01:28 +02:00
$filter = array ();
2012-02-04 22:47:28 +01:00
// If "Sync selected addressbooks into one" is set
2012-02-09 21:09:49 +01:00
if ( $user && $user == $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ] && in_array ( 'O' , $this -> home_set_pref ))
2012-02-04 22:47:28 +01:00
{
2012-02-29 10:58:15 +01:00
$filter [ 'owner' ] = array_keys ( $this -> get_shared ( true )); // true: ignore all-in-one pref
$filter [ 'owner' ][] = $user ;
2012-02-04 22:47:28 +01:00
}
2008-05-19 10:01:28 +02:00
// show addressbook of a single user?
2012-02-04 22:47:28 +01:00
elseif ( $user && $path != '/addressbook/' || $user === 0 )
{
2012-02-29 10:58:15 +01:00
$filter [ 'owner' ] = $user ;
2012-02-04 22:47:28 +01:00
}
2008-05-19 10:01:28 +02:00
// should we hide the accounts addressbook
2017-12-01 14:58:44 +01:00
if ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'addressbook' ][ 'hide_accounts' ] === '1' ) $filter [ 'account_id' ] = null ;
2008-05-08 22:31:32 +02:00
// process REPORT filters or multiget href's
2016-04-02 10:40:34 +02:00
$nresults = null ;
2011-10-05 08:50:26 +02:00
if (( $id || $options [ 'root' ][ 'name' ] != 'propfind' ) && ! $this -> _report_filters ( $options , $filter , $id , $nresults ))
2008-05-08 22:31:32 +02:00
{
return false ;
}
2011-10-04 14:16:03 +02:00
if ( $id ) $path = dirname ( $path ) . '/' ; // carddav_name get's added anyway in the callback
2008-05-17 15:11:46 +02:00
if ( $this -> debug ) error_log ( __METHOD__ . " ( $path , " . array2string ( $options ) . " ,, $user , $id ) filter= " . array2string ( $filter ));
2010-03-07 00:06:43 +01:00
// check if we have to return the full contact data or just the etag's
if ( ! ( $filter [ 'address_data' ] = $options [ 'props' ] == 'all' &&
2016-04-02 12:44:17 +02:00
$options [ 'root' ][ 'ns' ] == Api\CalDAV :: CARDDAV ) && is_array ( $options [ 'props' ]))
2008-05-08 22:31:32 +02:00
{
foreach ( $options [ 'props' ] as $prop )
{
if ( $prop [ 'name' ] == 'address-data' )
{
2009-10-17 11:13:36 +02:00
$filter [ 'address_data' ] = true ;
2008-05-08 22:31:32 +02:00
break ;
}
}
}
2012-09-23 22:19:35 +02:00
// rfc 6578 sync-collection report: filter for sync-token is already set in _report_filters
2024-05-13 19:18:44 +02:00
if ( $options [ 'root' ][ 'name' ] === 'sync-collection' )
2012-09-23 22:19:35 +02:00
{
2012-09-26 16:30:47 +02:00
// callback to query sync-token, after propfind_callbacks / iterator is run and
// stored max. modification-time in $this->sync_collection_token
$files [ 'sync-token' ] = array ( $this , 'get_sync_collection_token' );
$files [ 'sync-token-params' ] = array ( $path , $user );
2024-05-13 19:18:44 +02:00
$this -> sync_collection_token = $this -> more_results = null ;
2014-02-20 20:26:02 +01:00
$filter [ 'order' ] = 'contact_modified ASC' ; // return oldest modifications first
$filter [ 'sync-collection' ] = true ;
2012-09-23 22:19:35 +02:00
}
2024-05-13 19:18:44 +02:00
if ( isset ( $nresults ) && $options [ 'root' ][ 'name' ] === 'sync-collection' )
2011-10-05 08:50:26 +02:00
{
2023-02-15 12:40:12 +01:00
$files [ 'files' ] = $this -> propfind_generator ( $path , $filter , $files [ 'files' ], ( int ) $nresults );
2011-10-05 08:50:26 +02:00
}
else
{
2021-09-16 20:53:43 +02:00
// return iterator, calling ourselves to return result in chunks
2023-02-15 12:40:12 +01:00
$files [ 'files' ] = $this -> propfind_generator ( $path , $filter , $files [ 'files' ]);
2011-10-05 08:50:26 +02:00
}
2009-10-17 11:13:36 +02:00
return true ;
}
/**
2023-02-15 12:40:12 +01:00
* Chunk - size for DB queries of profind_generator
2009-10-17 11:13:36 +02:00
*/
2023-02-15 12:41:33 +01:00
const CHUNK_SIZE = 500 ;
2021-03-28 20:48:55 +02:00
/**
2023-02-15 12:40:12 +01:00
* Generator for propfind with ability to skip reporting not found ids
2021-03-28 20:48:55 +02:00
*
* @ param string $path
* @ param array & $filter
2023-02-15 12:40:12 +01:00
* @ param array $extra extra resources like the collection itself
* @ param int | null $nresults option limit of number of results to report
2021-03-28 20:48:55 +02:00
* @ param boolean $report_not_found_multiget_ids = true
2023-02-15 19:50:28 +01:00
* @ return Generator < array with values for keys path and props >
2023-02-15 12:40:12 +01:00
* @ ToDo also use CHUNK_SIZE when querying lists
2021-03-28 20:48:55 +02:00
*/
2023-02-15 19:50:28 +01:00
function propfind_generator ( $path , array & $filter , array $extra = [], $nresults = null , $report_not_found_multiget_ids = true )
2009-10-17 11:13:36 +02:00
{
2015-04-18 15:20:56 +02:00
//error_log(__METHOD__."('$path', ".array2string($filter).", ".array2string($start).", $report_not_found_multiget_ids)");
2009-10-17 11:13:36 +02:00
$starttime = microtime ( true );
2013-08-22 18:39:21 +02:00
$filter_in = $filter ;
2009-10-17 11:13:36 +02:00
2023-02-15 12:40:12 +01:00
// yield extra resources like the root itself
$yielded = 0 ;
foreach ( $extra as $resource )
{
if ( ++ $yielded && isset ( $nresults ) && $yielded > $nresults )
{
2024-05-13 19:18:44 +02:00
$this -> sync_collection_token = $resource [ 'modified' ];
$this -> more_results = true ;
2023-02-15 12:40:12 +01:00
return ;
}
yield $resource ;
}
2009-10-17 11:13:36 +02:00
if (( $address_data = $filter [ 'address_data' ]))
2008-05-08 22:31:32 +02:00
{
$handler = self :: _get_handler ();
}
2009-10-17 11:13:36 +02:00
unset ( $filter [ 'address_data' ]);
2012-02-04 22:47:28 +01:00
2011-10-05 08:50:26 +02:00
if ( isset ( $filter [ 'order' ]))
{
$order = $filter [ 'order' ];
unset ( $filter [ 'order' ]);
}
else
{
$order = 'egw_addressbook.contact_id' ;
}
2012-09-26 16:30:47 +02:00
// detect sync-collection report
2014-02-20 20:26:02 +01:00
$sync_collection_report = $filter [ 'sync-collection' ];
2014-02-20 20:38:21 +01:00
unset ( $filter [ 'sync-collection' ]);
2012-09-26 16:30:47 +02:00
2023-02-15 12:40:12 +01:00
// stop output buffering switched on to log the response, if we should return more than 200 entries
if ( ! empty ( $this -> requested_multiget_ids ) && ob_get_level () && count ( $this -> requested_multiget_ids ) > 200 )
2013-10-01 13:38:34 +02:00
{
2023-02-15 19:50:28 +01:00
$this -> caldav -> log ( " ### " . count ( $this -> requested_multiget_ids ) . " resources requested in multiget REPORT --> turning logging off to allow streaming of the response " );
2023-02-15 12:40:12 +01:00
ob_end_flush ();
2013-10-01 13:38:34 +02:00
}
2013-09-25 12:27:41 +02:00
2009-10-17 11:13:36 +02:00
$files = array ();
2008-05-17 15:11:46 +02:00
// we query etag and modified, as LDAP does not have the strong sql etag
2012-01-31 01:37:01 +01:00
$cols = array ( 'id' , 'uid' , 'etag' , 'modified' , 'n_fn' );
2011-04-05 22:39:13 +02:00
if ( ! in_array ( self :: $path_attr , $cols )) $cols [] = self :: $path_attr ;
2012-09-23 22:19:35 +02:00
// we need tid for sync-collection report
if ( array_key_exists ( 'tid' , $filter ) && ! isset ( $filter [ 'tid' ]) && ! in_array ( 'tid' , $cols )) $cols [] = 'tid' ;
2023-10-19 20:34:38 +02:00
$search = $filter [ 'search' ] ? ? [];
unset ( $filter [ 'search' ]);
for ( $chunk = 0 ; ( $contacts =& $this -> bo -> search ( $search , $cols , $order , '' , '' , False , 'AND' ,
2023-02-15 12:40:12 +01:00
[ $chunk * self :: CHUNK_SIZE , self :: CHUNK_SIZE ], $filter )); ++ $chunk )
2008-05-08 22:31:32 +02:00
{
2020-10-16 21:34:26 +02:00
// filter[tid] === null also returns no longer shared contacts, to remove them from devices, we need to mark them here as deleted
// to do so we need to read not deleted sharing info of potential candidates (not deleted and no regular access), as search does NOT
$id2key = [];
foreach ( $contacts as $key => & $contact )
{
if ( $contact [ 'tid' ] !== Api\Contacts :: DELETED_TYPE &&
// check for (deleted) shared access
( ! isset ( $filter [ 'owner' ]) || ! in_array ( $contact [ 'owner' ], ( array ) $filter [ 'owner' ])) &&
! $this -> bo -> check_perms ( Acl :: READ , $contact , false , $this -> user , 0 ))
{
$id2key [ $contact [ 'id' ]] = $key ;
}
}
if ( $id2key )
{
foreach ( $this -> bo -> read_shared ( array_keys ( $id2key ), false ) as $id => $shared )
{
$contacts [ $id2key [ $id ]][ 'shared' ] = $shared ;
if ( ! $this -> bo -> check_perms ( Acl :: READ , $contact , false , $this -> user ))
{
$contacts [ $id2key [ $id ]][ 'tid' ] = Api\Contacts :: DELETED_TYPE ;
}
}
}
2021-09-16 20:53:43 +02:00
$is_jscontact = Api\CalDAV :: isJSON ();
2009-10-16 10:01:28 +02:00
foreach ( $contacts as & $contact )
2008-05-08 22:31:32 +02:00
{
2013-09-25 12:27:41 +02:00
// remove contact from requested multiget ids, to be able to report not found urls
2023-02-15 12:40:12 +01:00
if ( ! empty ( $this -> requested_multiget_ids ) && ( $k = array_search ( $contact [ self :: $path_attr ], $this -> requested_multiget_ids )) !== false )
2013-09-25 12:27:41 +02:00
{
2023-02-15 12:40:12 +01:00
unset ( $this -> requested_multiget_ids [ $k ]);
2013-09-25 12:27:41 +02:00
}
2024-05-13 19:18:44 +02:00
if ( ++ $yielded && isset ( $nresults ) && $yielded > $nresults )
{
$this -> sync_collection_token = $contact [ 'modified' ];
$this -> more_results = true ;
return ;
}
2012-09-23 22:19:35 +02:00
// sync-collection report: deleted entry need to be reported without properties
2016-04-29 12:41:53 +02:00
if ( $contact [ 'tid' ] == Api\Contacts :: DELETED_TYPE )
2012-09-23 22:19:35 +02:00
{
2023-02-15 19:50:28 +01:00
yield [ 'path' => $path . urldecode ( $this -> get_path ( $contact ))];
2012-09-23 22:19:35 +02:00
continue ;
}
2009-04-02 14:31:44 +02:00
$props = array (
2021-09-16 20:53:43 +02:00
'getcontenttype' => Api\CalDAV :: mkprop ( 'getcontenttype' , $is_jscontact ? JsContact :: MIME_TYPE_JSCARD : 'text/vcard' ),
2012-01-31 01:37:01 +01:00
'getlastmodified' => $contact [ 'modified' ],
'displayname' => $contact [ 'n_fn' ],
2008-05-08 22:31:32 +02:00
);
2009-04-02 14:31:44 +02:00
if ( $address_data )
2008-05-08 22:31:32 +02:00
{
2021-09-16 20:53:43 +02:00
$content = $is_jscontact ? JsContact :: getJsCard ( $contact [ 'id' ], false ) :
$handler -> getVCard ( $contact [ 'id' ], $this -> charset , false );
2022-01-17 20:35:31 +01:00
$props [ 'getcontentlength' ] = bytes ( is_array ( $content ) ? json_encode ( $content ) : $content );
2021-09-16 20:53:43 +02:00
$props [ 'address-data' ] = Api\CalDAV :: mkprop ( Api\CalDAV :: CARDDAV , 'address-data' , $content );
2008-05-20 11:02:16 +02:00
}
2023-02-15 12:40:12 +01:00
yield $this -> add_resource ( $path , $contact , $props );
2008-05-08 22:31:32 +02:00
}
2012-09-26 16:30:47 +02:00
// sync-collection report --> return modified of last contact as sync-token
if ( $sync_collection_report )
{
$this -> sync_collection_token = $contact [ 'modified' ];
}
2012-09-23 22:19:35 +02:00
}
2023-02-15 12:40:12 +01:00
// add accounts after contacts, if enabled and stored in different repository
if ( $this -> bo -> so_accounts && is_array ( $filter [ 'owner' ]) && in_array ( '0' , $filter [ 'owner' ]))
2012-01-31 10:57:59 +01:00
{
2023-02-15 12:40:12 +01:00
$accounts_filter = $filter_in ;
$accounts_filter [ 'owner' ] = '0' ;
if ( $sync_collection_report ) $token_was = $this -> sync_collection_token ;
self :: $path_attr = 'id' ;
self :: $path_extension = '.vcf' ;
foreach ( $this -> propfind_generator ( $path , $accounts_filter , [], $nresults , false ) as $resource )
2012-02-01 01:31:24 +01:00
{
2023-02-15 12:40:12 +01:00
if ( ++ $yielded && isset ( $nresults ) && $yielded > $nresults )
2013-08-22 18:39:21 +02:00
{
2024-05-13 19:18:44 +02:00
$this -> sync_collection_token = Api\DateTime :: user2server ( $resource [ 'modified' ]) - 1 ;
$this -> more_results = true ;
2023-02-15 12:40:12 +01:00
return ;
2013-08-22 18:39:21 +02:00
}
2023-02-15 12:40:12 +01:00
yield $resource ;
2012-02-01 01:31:24 +01:00
}
2023-02-15 12:40:12 +01:00
self :: $path_attr = 'carddav_name' ;
self :: $path_extension = '' ;
if ( $sync_collection_report && $token_was > $this -> sync_collection_token )
2012-01-31 10:57:59 +01:00
{
2023-02-15 12:40:12 +01:00
$this -> sync_collection_token = $token_was ;
}
}
// add groups after contacts, but only if enabled and NOT for '/addressbook/' (!isset($filter['owner'])
if ( in_array ( 'D' , $this -> home_set_pref ) && ( string ) $filter [ 'owner' ] !== '0' )
{
$where = array (
'list_owner' => isset ( $filter [ 'owner' ]) ? $filter [ 'owner' ] : array_keys ( $this -> bo -> grants )
);
// add sync-token to support sync-collection report
if ( $sync_collection_report )
{
list (, $sync_token ) = explode ( '>' , $filter [ 0 ]);
if (( int ) $sync_token ) $where [] = 'list_modified>' . $GLOBALS [ 'egw' ] -> db -> from_unixtime (( int ) $sync_token );
}
if ( isset ( $filter [ self :: $path_attr ])) // multiget report?
{
$where [ 'list_' . self :: $path_attr ] = $filter [ self :: $path_attr ];
}
//error_log(__METHOD__."() filter=".array2string($filter).", do_groups=".in_array('D',$this->home_set_pref).", where=".array2string($where));
if (( $lists = $this -> bo -> read_lists ( $where , 'contact_uid' , $where [ 'list_owner' ]))) // limit to contacts in same AB!
{
foreach ( $lists as $list )
2013-08-22 18:39:21 +02:00
{
2023-02-15 12:40:12 +01:00
$list [ self :: $path_attr ] = $is_jscontact ? self :: JS_CARDGROUP_ID_PREFIX . $list [ 'list_id' ] : $list [ 'list_carddav_name' ];
$etag = $list [ 'list_id' ] . ':' . $list [ 'list_etag' ];
// for all-in-one addressbook, add selected ABs to etag
if ( isset ( $filter [ 'owner' ]) && is_array ( $filter [ 'owner' ]))
2012-01-31 10:57:59 +01:00
{
2023-02-15 12:40:12 +01:00
$etag .= ':' . implode ( '-' , $filter [ 'owner' ]);
}
$props = array (
'getcontenttype' => Api\CalDAV :: mkprop ( 'getcontenttype' , $is_jscontact ? JsContact :: MIME_TYPE_JSCARDGROUP : 'text/vcard' ),
'getlastmodified' => Api\DateTime :: to ( $list [ 'list_modified' ], 'ts' ),
'displayname' => $list [ 'list_name' ],
'getetag' => '"' . $etag . '"' ,
);
if ( $address_data )
{
$content = $is_jscontact ? JsContact :: getJsCardGroup ( $list , false ) : $handler -> getGroupVCard ( $list );
$props [ 'getcontentlength' ] = bytes ( is_array ( $content ) ? json_encode ( $content ) : $content );
$props [ 'address-data' ] = Api\CalDAV :: mkprop ( Api\CalDAV :: CARDDAV , 'address-data' , $content );
}
if ( ++ $yielded && isset ( $nresults ) && $yielded > $nresults )
{
2024-05-13 19:18:44 +02:00
$this -> sync_collection_token = $GLOBALS [ 'egw' ] -> db -> from_timestamp ( $list [ 'list_modified' ]) - 1 ;
$this -> more_results = true ;
2023-02-15 12:40:12 +01:00
return ;
}
yield $this -> add_resource ( $path , $list , $props );
2012-09-26 16:30:47 +02:00
2023-02-15 12:40:12 +01:00
// remove list from requested multiget ids, to be able to report not found urls
if ( ! empty ( $this -> requested_multiget_ids ) && ( $k = array_search ( $list [ self :: $path_attr ], $this -> requested_multiget_ids )) !== false )
{
unset ( $this -> requested_multiget_ids [ $k ]);
}
2013-09-25 12:27:41 +02:00
2023-02-15 12:40:12 +01:00
if ( $sync_collection_report && $this -> sync_collection_token < ( $ts = $GLOBALS [ 'egw' ] -> db -> from_timestamp ( $list [ 'list_modified' ])))
{
$this -> sync_collection_token = $ts ;
2012-09-26 16:30:47 +02:00
}
2012-01-31 10:57:59 +01:00
}
}
2023-02-15 12:40:12 +01:00
}
// report not found multiget urls
if ( $report_not_found_multiget_ids && ! empty ( $this -> requested_multiget_ids ))
{
foreach ( $this -> requested_multiget_ids as $id )
2013-09-25 12:27:41 +02:00
{
2023-02-15 12:40:12 +01:00
if ( ++ $yielded && isset ( $nresults ) && $yielded > $nresults )
2015-04-15 12:00:25 +02:00
{
2024-05-13 19:18:44 +02:00
-- $this -> sync_collection_token ;
$this -> more_results = true ;
2023-02-15 12:40:12 +01:00
return ;
2015-04-15 12:00:25 +02:00
}
2023-02-15 12:40:12 +01:00
yield [ 'path' => $path . $id . self :: $path_extension ];
2013-09-25 12:27:41 +02:00
}
}
2023-02-15 12:40:12 +01:00
if ( $this -> debug )
{
error_log ( __METHOD__ . " ( $path , filter= " . json_encode ( $filter ) . ', extra=' . json_encode ( $extra ) .
" , nresults= $nresults , report_not_found= $report_not_found_multiget_ids ) took " .
( microtime ( true ) - $starttime ) . " to return $yielded resources " );
}
2008-05-08 22:31:32 +02:00
}
/**
* Process the filters from the CalDAV REPORT request
*
* @ param array $options
2023-10-19 20:34:38 +02:00
* @ param array & $filters
2008-05-08 22:31:32 +02:00
* @ param string $id
2011-10-05 08:50:26 +02:00
* @ param int & $nresult on return limit for number or results or unchanged / null
* @ return boolean true if filter could be processed
2008-05-08 22:31:32 +02:00
*/
2023-10-19 20:34:38 +02:00
function _report_filters ( $options , & $filters , $id , & $nresults )
2008-05-08 22:31:32 +02:00
{
2023-10-19 20:34:38 +02:00
// in case of JSON/REST API pass filters to report
if ( Api\CalDAV :: isJSON () && ! empty ( $options [ 'filters' ]) && is_array ( $options [ 'filters' ]))
{
$filters += $options [ 'filters' ]; // using += to no allow overwriting existing filters
}
elseif ( ! empty ( $options [ 'filters' ]))
2008-05-08 22:31:32 +02:00
{
2011-10-05 08:50:26 +02:00
/* Example of a complex filter used by Mac Addressbook
< B : filter test = " anyof " >
< B : prop - filter name = " FN " test = " allof " >
< B : text - match collation = " i;unicode-casemap " match - type = " contains " > becker </ B : text - match >
< B : text - match collation = " i;unicode-casemap " match - type = " contains " > ralf </ B : text - match >
</ B : prop - filter >
< B : prop - filter name = " EMAIL " test = " allof " >
< B : text - match collation = " i;unicode-casemap " match - type = " contains " > becker </ B : text - match >
< B : text - match collation = " i;unicode-casemap " match - type = " contains " > ralf </ B : text - match >
</ B : prop - filter >
< B : prop - filter name = " NICKNAME " test = " allof " >
< B : text - match collation = " i;unicode-casemap " match - type = " contains " > becker </ B : text - match >
< B : text - match collation = " i;unicode-casemap " match - type = " contains " > ralf </ B : text - match >
</ B : prop - filter >
</ B : filter >
*/
$filter_test = isset ( $options [ 'filters' ][ 'attrs' ]) && isset ( $options [ 'filters' ][ 'attrs' ][ 'test' ]) ?
$options [ 'filters' ][ 'attrs' ][ 'test' ] : 'anyof' ;
$prop_filters = array ();
2016-04-02 10:40:34 +02:00
$matches = $prop_test = $column = null ;
2011-10-05 08:50:26 +02:00
foreach ( $options [ 'filters' ] as $n => $filter )
2008-05-08 22:31:32 +02:00
{
2011-10-05 08:50:26 +02:00
if ( ! is_int ( $n )) continue ; // eg. attributes of filter xml element
switch (( string ) $filter [ 'name' ])
2008-05-08 22:31:32 +02:00
{
2011-10-05 08:50:26 +02:00
case 'param-filter' :
2016-04-02 12:44:17 +02:00
$this -> caldav -> log ( __METHOD__ . " (...) param-filter=' { $filter [ 'attrs' ][ 'name' ] } ' not (yet) implemented! " );
2011-10-05 08:50:26 +02:00
break ;
case 'prop-filter' : // can be multiple prop-filter, see example
if ( $matches ) $prop_filters [] = implode ( $prop_test == 'allof' ? ' AND ' : ' OR ' , $matches );
$matches = array ();
$prop_filter = strtoupper ( $filter [ 'attrs' ][ 'name' ]);
$prop_test = isset ( $filter [ 'attrs' ][ 'test' ]) ? $filter [ 'attrs' ][ 'test' ] : 'anyof' ;
if ( $this -> debug > 1 ) error_log ( __METHOD__ . " (...) prop-filter=' $prop_filter ', test=' $prop_test ' " );
2008-05-08 22:31:32 +02:00
break ;
2011-10-05 08:50:26 +02:00
case 'is-not-defined' :
$matches [] = '(' . $column . " ='' OR " . $column . ' IS NULL)' ;
break ;
case 'text-match' : // prop-filter can have multiple text-match, see example
if ( ! isset ( $this -> filter_prop2cal [ $prop_filter ])) // eg. not existing NICKNAME in EGroupware
2008-05-08 22:31:32 +02:00
{
2011-10-05 08:50:26 +02:00
if ( $this -> debug || $prop_filter != 'NICKNAME' ) error_log ( __METHOD__ . " (...) text-match: $prop_filter { $filter [ 'attrs' ][ 'match-type' ] } ' { $filter [ 'data' ] } ' unknown property ' $prop_filter ' --> ignored " );
$column = false ; // to ignore following data too
2008-05-08 22:31:32 +02:00
}
else
{
2021-09-16 20:53:43 +02:00
switch ( $filter [ 'attrs' ][ 'collation' ]) // todo: which other collations allowed, we are always unicode
2008-05-08 22:31:32 +02:00
{
2011-10-05 08:50:26 +02:00
case 'i;unicode-casemap' :
2008-05-08 22:31:32 +02:00
default :
2016-04-29 12:41:53 +02:00
$comp = ' ' . $GLOBALS [ 'egw' ] -> db -> capabilities [ Api\Db :: CAPABILITY_CASE_INSENSITIV_LIKE ] . ' ' ;
2008-05-08 22:31:32 +02:00
break ;
}
2011-10-05 08:50:26 +02:00
$column = $this -> filter_prop2cal [ strtoupper ( $prop_filter )];
if ( strpos ( $column , '_' ) === false ) $column = 'contact_' . $column ;
if ( ! isset ( $filters [ 'order' ])) $filters [ 'order' ] = $column ;
$match_type = $filter [ 'attrs' ][ 'match-type' ];
$negate_condition = isset ( $filter [ 'attrs' ][ 'negate-condition' ]) && $filter [ 'attrs' ][ 'negate-condition' ] == 'yes' ;
2008-05-08 22:31:32 +02:00
}
break ;
2011-10-05 08:50:26 +02:00
case '' : // data of text-match element
if ( isset ( $filter [ 'data' ]) && isset ( $column ))
{
if ( $column ) // false for properties not known to EGroupware
{
$value = str_replace ( array ( '%' , '_' ), array ( '\\%' , '\\_' ), $filter [ 'data' ]);
switch ( $match_type )
{
case 'equals' :
$sql_filter = $column . $comp . $GLOBALS [ 'egw' ] -> db -> quote ( $value );
break ;
default :
case 'contains' :
$sql_filter = $column . $comp . $GLOBALS [ 'egw' ] -> db -> quote ( '%' . $value . '%' );
break ;
case 'starts-with' :
$sql_filter = $column . $comp . $GLOBALS [ 'egw' ] -> db -> quote ( $value . '%' );
break ;
case 'ends-with' :
$sql_filter = $column . $comp . $GLOBALS [ 'egw' ] -> db -> quote ( '%' . $value );
break ;
}
$matches [] = ( $negate_condition ? 'NOT ' : '' ) . $sql_filter ;
if ( $this -> debug > 1 ) error_log ( __METHOD__ . " (...) text-match: $prop_filter $match_type ' ' { $filter [ 'data' ] } ' " );
}
unset ( $column );
break ;
}
// fall through
2008-05-08 22:31:32 +02:00
default :
2016-04-02 12:44:17 +02:00
$this -> caldav -> log ( __METHOD__ . " ( " . array2string ( $options ) . " ,, $id ) unknown filter= " . array2string ( $filter ) . ' --> ignored' );
2008-05-08 22:31:32 +02:00
break ;
}
}
2011-10-05 08:50:26 +02:00
if ( $matches ) $prop_filters [] = implode ( $prop_test == 'allof' ? ' AND ' : ' OR ' , $matches );
if ( $prop_filters )
{
$filters [] = $filter = '((' . implode ( $filter_test == 'allof' ? ') AND (' : ') OR (' , $prop_filters ) . '))' ;
2016-04-02 10:40:34 +02:00
if ( $this -> debug ) error_log ( __METHOD__ . " (path= $options[path] , ...) sql-filter: $filter " );
2011-10-05 08:50:26 +02:00
}
}
// parse limit from $options['other']
/* Example limit
< B : limit >
< B : nresults > 10 </ B : nresults >
</ B : limit >
*/
2012-01-30 06:11:05 +01:00
foreach (( array ) $options [ 'other' ] as $option )
2011-10-05 08:50:26 +02:00
{
switch ( $option [ 'name' ])
{
case 'nresults' :
$nresults = ( int ) $option [ 'data' ];
//error_log(__METHOD__."(...) options[other]=".array2string($options['other'])." --> nresults=$nresults");
break ;
case 'limit' :
break ;
2011-11-10 07:57:54 +01:00
case 'href' :
break ; // from addressbook-multiget, handled below
2012-09-23 22:19:35 +02:00
// rfc 6578 sync-report
case 'sync-token' :
if ( ! empty ( $option [ 'data' ]))
{
$parts = explode ( '/' , $option [ 'data' ]);
$sync_token = array_pop ( $parts );
$filters [] = 'contact_modified>' . ( int ) $sync_token ;
$filters [ 'tid' ] = null ; // to return deleted entries too
}
break ;
case 'sync-level' :
2012-09-24 12:26:29 +02:00
if ( $option [ 'data' ] != '1' )
{
2016-04-02 12:44:17 +02:00
$this -> caldav -> log ( __METHOD__ . " (...) only sync-level { $option [ 'data' ] } requested, but only 1 supported! options[other]= " . array2string ( $options [ 'other' ]));
2012-09-24 12:26:29 +02:00
}
2012-09-23 22:19:35 +02:00
break ;
2011-10-05 08:50:26 +02:00
default :
2016-04-02 12:44:17 +02:00
$this -> caldav -> log ( __METHOD__ . " (...) unknown xml tag ' { $option [ 'name' ] } ': options[other]= " . array2string ( $options [ 'other' ]));
2011-10-05 08:50:26 +02:00
break ;
}
2008-05-08 22:31:32 +02:00
}
// multiget --> fetch the url's
2023-02-15 12:40:12 +01:00
$this -> requested_multiget_ids = null ;
2008-05-08 22:31:32 +02:00
if ( $options [ 'root' ][ 'name' ] == 'addressbook-multiget' )
{
2023-02-15 12:40:12 +01:00
$this -> requested_multiget_ids = [];
2008-05-08 22:31:32 +02:00
foreach ( $options [ 'other' ] as $option )
{
if ( $option [ 'name' ] == 'href' )
{
$parts = explode ( '/' , $option [ 'data' ]);
2014-02-20 16:11:27 +01:00
if (( $id = urldecode ( array_pop ( $parts ))))
2011-04-05 22:39:13 +02:00
{
2023-02-15 12:40:12 +01:00
$this -> requested_multiget_ids [] = self :: $path_extension ? basename ( $id , self :: $path_extension ) : $id ;
2011-04-05 22:39:13 +02:00
}
2008-05-08 22:31:32 +02:00
}
}
2023-02-15 12:40:12 +01:00
if ( $this -> requested_multiget_ids ) $filters [ self :: $path_attr ] = $this -> requested_multiget_ids ;
if ( $this -> debug ) error_log ( __METHOD__ . " (...) addressbook-multiget: ids= " . implode ( ',' , $this -> requested_multiget_ids ));
2008-05-08 22:31:32 +02:00
}
elseif ( $id )
{
2016-04-02 12:44:17 +02:00
$filters [ self :: $path_attr ] = self :: $path_extension ? basename ( $id , self :: $path_extension ) : $id ;
2008-05-08 22:31:32 +02:00
}
2012-09-23 22:19:35 +02:00
//error_log(__METHOD__."() options[other]=".array2string($options['other'])." --> filters=".array2string($filters));
2008-05-08 22:31:32 +02:00
return true ;
}
/**
* Handle get request for an event
*
* @ param array & $options
* @ param int $id
2016-04-02 10:40:34 +02: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
function get ( & $options , $id , $user = null )
2008-05-08 22:31:32 +02:00
{
2016-04-02 10:40:34 +02:00
unset ( $user ); // not used, but required by function signature
2008-05-08 22:31:32 +02:00
if ( ! is_array ( $contact = $this -> _common_get_put_delete ( 'GET' , $options , $id )))
{
return $contact ;
}
2021-09-15 18:45:32 +02:00
// jsContact or vCard
2024-02-06 15:39:12 +01:00
if (( $type = Api\CalDAV :: isJSON ( $_SERVER [ 'HTTP_ACCEPT' ])) || ( $type = Api\CalDAV :: isJSON ()))
2021-09-15 18:45:32 +02:00
{
2021-09-19 11:09:44 +02:00
$options [ 'data' ] = $contact [ 'list_id' ] ? JsContact :: getJsCardGroup ( $contact , $type ) :
JsContact :: getJsCard ( $contact , $type );
2021-09-17 20:15:36 +02:00
$options [ 'mimetype' ] = ( $contact [ 'list_id' ] ? JsContact :: MIME_TYPE_JSCARDGROUP :
JsContact :: MIME_TYPE_JSCARD ) . ';charset=utf-8' ;
2021-09-15 18:45:32 +02:00
}
else
{
$handler = self :: _get_handler ();
$options [ 'data' ] = $contact [ 'list_id' ] ? $handler -> getGroupVCard ( $contact ) :
$handler -> getVCard ( $contact [ 'id' ], $this -> charset , false );
// e.g. Evolution does not understand 'text/vcard'
$options [ 'mimetype' ] = 'text/x-vcard; charset=' . $this -> charset ;
}
2008-05-08 22:31:32 +02:00
header ( 'Content-Encoding: identity' );
2011-10-05 10:15:24 +02:00
header ( 'ETag: "' . $this -> get_etag ( $contact ) . '"' );
2008-05-08 22:31:32 +02:00
return true ;
}
/**
2011-10-05 08:50:26 +02:00
* Handle put request for a contact
2008-05-08 22:31:32 +02:00
*
* @ param array & $options
* @ param int $id
2016-04-02 10:40:34 +02:00
* @ param int $user = null account_id of owner , default null
* @ param string $prefix = null user prefix from path ( eg . / ralf from / ralf / addressbook )
2021-09-25 12:20:31 +02:00
* @ param string $method = 'PUT' also called for POST and PATCH
* @ param string $content_type = null
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' )
*/
2021-09-25 12:20:31 +02:00
function put ( & $options , $id , $user = null , $prefix = null , string $method = 'PUT' , string $content_type = null )
2008-05-08 22:31:32 +02:00
{
2010-03-07 00:06:43 +01:00
if ( $this -> debug ) error_log ( __METHOD__ . '(' . array2string ( $options ) . " , $id , $user ) " );
2021-09-25 12:20:31 +02:00
$oldContact = $this -> _common_get_put_delete ( $method , $options , $id );
2010-03-07 00:06:43 +01:00
if ( ! is_null ( $oldContact ) && ! is_array ( $oldContact ))
2008-05-08 22:31:32 +02:00
{
2012-01-31 20:47:52 +01:00
if ( $this -> debug ) error_log ( __METHOD__ . " (,' $id ', $user , ' $prefix ') returning " . array2string ( $oldContact ));
2010-03-07 00:06:43 +01:00
return $oldContact ;
2008-05-08 22:31:32 +02:00
}
2010-03-07 00:06:43 +01:00
2021-09-20 16:01:22 +02:00
$type = null ;
if (( $is_json = Api\CalDAV :: isJSON ( $type )))
2021-09-15 18:45:32 +02:00
{
2021-09-20 16:01:22 +02:00
if ( strpos ( $type , JsContact :: MIME_TYPE_JSCARD ) === false && strpos ( $type , JsContact :: MIME_TYPE_JSCARDGROUP ) === false )
{
if ( ! empty ( $id ))
{
$type = strpos ( $id , self :: JS_CARDGROUP_ID_PREFIX ) === 0 ? JsContact :: MIME_TYPE_JSCARDGROUP : JsContact :: MIME_TYPE_JSCARD ;
}
else
{
$json = json_decode ( $options [ 'content' ], true );
$type = is_array ( $json [ 'members' ]) ? JsContact :: MIME_TYPE_JSCARDGROUP : JsContact :: MIME_TYPE_JSCARD ;
}
}
$contact = $type === JsContact :: MIME_TYPE_JSCARD ?
2021-09-25 12:20:31 +02:00
JsContact :: parseJsCard ( $options [ 'content' ], $oldContact ? : [], $content_type , $method ) :
JsContact :: parseJsCardGroup ( $options [ 'content' ]);
2021-09-20 16:01:22 +02:00
if ( ! empty ( $id ) && strpos ( $id , self :: JS_CARDGROUP_ID_PREFIX ) === 0 )
{
$id = substr ( $id , strlen ( self :: JS_CARDGROUP_ID_PREFIX ));
}
elseif ( empty ( $id ))
{
2023-07-21 17:41:37 +02:00
$contact [ 'carddav_name' ] = $contact [ 'uid' ] . '.vcf' ;
2021-09-20 16:01:22 +02:00
$contact [ 'owner' ] = $user ;
}
2021-09-17 20:15:36 +02:00
/* uncomment to return parsed data for testing
2021-09-15 18:45:32 +02:00
header ( 'Content-Type: application/json' );
echo json_encode ( $contact , JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES );
return " 200 Ok " ;
2021-09-17 20:15:36 +02:00
*/
2021-09-15 18:45:32 +02:00
}
else
2010-06-14 09:45:25 +02:00
{
2021-09-15 18:45:32 +02:00
$handler = self :: _get_handler ();
// Fix for Apple Addressbook
$vCard = preg_replace ( '/item\d\.(ADR|TEL|EMAIL|URL)/' , '\1' ,
htmlspecialchars_decode ( $options [ 'content' ]));
$charset = null ;
if ( ! empty ( $options [ 'content_type' ]))
2010-06-14 09:45:25 +02:00
{
2021-09-15 18:45:32 +02:00
$content_type = explode ( ';' , $options [ 'content_type' ]);
if ( count ( $content_type ) > 1 )
2010-06-14 09:45:25 +02:00
{
2021-09-15 18:45:32 +02:00
array_shift ( $content_type );
foreach ( $content_type as $attribute )
2010-06-14 09:45:25 +02:00
{
2021-09-15 18:45:32 +02:00
trim ( $attribute );
list ( $key , $value ) = explode ( '=' , $attribute );
2021-10-11 09:50:46 +02:00
// check if value is enclosed in quotes
if ( in_array ( $value [ 0 ], [ '"' , " ' " ], true ) && $value [ 0 ] === substr ( $value , - 1 ))
{
$value = substr ( $value , 1 , - 1 );
}
2021-09-15 18:45:32 +02:00
switch ( strtolower ( $key ))
{
case 'charset' :
2021-10-11 09:50:46 +02:00
$charset = strtoupper ( $value );
break ;
2021-09-15 18:45:32 +02:00
}
2010-06-14 09:45:25 +02:00
}
}
2010-12-02 20:42:03 +01:00
}
2008-05-17 15:11:46 +02:00
2021-09-15 18:45:32 +02:00
$contact = $handler -> vcardtoegw ( $vCard , $charset );
}
2011-04-05 22:39:13 +02:00
if ( is_array ( $oldContact ) || ( $oldContact = $this -> bo -> read ( array ( 'contact_uid' => $contact [ 'uid' ]))))
2010-03-07 00:06:43 +01:00
{
$contactId = $oldContact [ 'id' ];
$retval = true ;
}
else
{
2011-04-05 22:39:13 +02:00
// new entry
$contactId = - 1 ;
$retval = '201 Created' ;
2010-03-07 00:06:43 +01:00
}
2021-09-20 16:01:22 +02:00
$is_group = isset ( $type ) && $type === JsContact :: MIME_TYPE_JSCARDGROUP || $contact [ '##X-ADDRESSBOOKSERVER-KIND' ] === 'group' ;
2012-01-31 10:57:59 +01:00
if ( $oldContact && $is_group !== isset ( $oldContact [ 'list_id' ]))
{
2016-04-29 12:41:53 +02:00
throw new Api\Exception\AssertionFailed ( __METHOD__ . " (,' $id ', $user ,' $prefix ') can contact into group or visa-versa! " );
2012-01-31 10:57:59 +01:00
}
2010-03-07 00:06:43 +01:00
2012-01-31 10:57:59 +01:00
if ( ! $is_group && is_array ( $contact [ 'cat_id' ]))
2010-03-07 00:06:43 +01:00
{
2010-06-14 09:45:25 +02:00
$contact [ 'cat_id' ] = implode ( ',' , $this -> bo -> find_or_add_categories ( $contact [ 'cat_id' ], $contactId ));
2010-03-07 00:06:43 +01:00
}
elseif ( $contactId > 0 )
2008-05-08 22:31:32 +02:00
{
2021-04-30 17:26:00 +02:00
switch ( Api\CalDAV\Handler :: get_agent ())
{
case 'davx5' :
// DAVx5 does not always give us our categories back (Seems to depend on client)
if ( is_null ( $contact [ 'cat_id' ]) && $oldContact [ 'cat_id' ])
{
$contact [ 'cat_id' ] = $oldContact [ 'cat_id' ];
}
break ;
}
2010-03-07 00:06:43 +01:00
}
if ( is_array ( $oldContact ))
{
$contact [ 'id' ] = $oldContact [ 'id' ];
2008-05-17 15:11:46 +02:00
// dont allow the client to overwrite certain values
2010-03-07 00:06:43 +01:00
$contact [ 'uid' ] = $oldContact [ 'uid' ];
2010-10-20 17:47:30 +02:00
$contact [ 'owner' ] = $oldContact [ 'owner' ];
2010-03-07 00:06:43 +01:00
$contact [ 'private' ] = $oldContact [ 'private' ];
2011-04-05 22:39:13 +02:00
$contact [ 'carddav_name' ] = $oldContact [ 'carddav_name' ];
2012-02-10 22:29:50 +01:00
$contact [ 'tid' ] = $oldContact [ 'tid' ];
$contact [ 'creator' ] = $oldContact [ 'creator' ];
2012-07-31 18:18:26 +02:00
$contact [ 'created' ] = $oldContact [ 'created' ];
2012-02-29 10:58:15 +01:00
$contact [ 'account_id' ] = $oldContact [ 'account_id' ];
2011-04-05 22:39:13 +02:00
}
else
{
2021-09-28 11:33:50 +02:00
$contact [ 'carddav_name' ] = ( ! empty ( $id ) ? basename ( $id , '.vcf' ) : $contact [ 'uid' ]) . '.vcf' ;
2012-02-15 14:04:27 +01:00
// only set owner, if user is explicitly specified in URL (check via prefix, NOT for /addressbook/) or sync-all-in-one!)
2021-09-28 11:33:50 +02:00
if ( $prefix && ( $is_json || ! in_array ( 'O' , $this -> home_set_pref )) && $user )
2012-02-15 14:04:27 +01:00
{
$contact [ 'owner' ] = $user ;
}
2016-04-29 12:41:53 +02:00
// check if default addressbook is synced and not Api\Accounts, if not use (always synced) personal addressbook
2015-07-23 19:40:48 +02:00
elseif ( ! $this -> bo -> default_addressbook || ! in_array ( $this -> bo -> default_addressbook , $this -> home_set_pref ))
2012-02-15 14:04:27 +01:00
{
$contact [ 'owner' ] = $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ];
}
else
{
$contact [ 'owner' ] = $this -> bo -> default_addressbook ;
$contact [ 'private' ] = $this -> bo -> default_private ;
}
// check if user has add rights for addressbook
// done here again, as _common_get_put_delete knows nothing about default addressbooks...
2016-04-29 12:41:53 +02:00
if ( ! ( $this -> bo -> grants [ $contact [ 'owner' ]] & Acl :: ADD ))
2010-10-20 17:47:30 +02:00
{
2012-01-31 20:47:52 +01:00
if ( $this -> debug ) error_log ( __METHOD__ . " (,' $id ', $user , ' $prefix ') returning '403 Forbidden' " );
2010-10-20 17:47:30 +02:00
return '403 Forbidden' ;
}
}
2008-05-17 15:11:46 +02:00
if ( $this -> http_if_match ) $contact [ 'etag' ] = self :: etag2value ( $this -> http_if_match );
2008-05-08 22:31:32 +02:00
2021-09-17 20:15:36 +02:00
// ignore photo for JSON/REST, it's not yet supported
2021-09-20 16:01:22 +02:00
$contact [ 'photo_unchanged' ] = $is_json ; //false; // photo needs saving
2012-02-01 01:59:25 +01:00
if ( ! ( $save_ok = $is_group ? $this -> save_group ( $contact , $oldContact ) : $this -> bo -> save ( $contact )))
2008-05-08 22:31:32 +02:00
{
2008-05-20 11:02:16 +02:00
if ( $this -> debug ) error_log ( __METHOD__ . " (, $id ) save( " . array2string ( $contact ) . " ) failed, Ok= $save_ok " );
if ( $save_ok === 0 )
2008-05-08 22:31:32 +02:00
{
2015-10-16 09:50:43 +02:00
// honor Prefer: return=representation for 412 too (no need for client to explicitly reload)
$this -> check_return_representation ( $options , $id , $user );
2008-05-08 22:31:32 +02:00
return '412 Precondition Failed' ;
}
2010-10-20 18:16:03 +02:00
return '403 Forbidden' ; // happens when writing new entries in AB's without ADD rights
2008-05-08 22:31:32 +02:00
}
2010-03-07 00:06:43 +01:00
2014-11-30 10:24:23 +01:00
if ( empty ( $contact [ 'etag' ]) || empty ( $contact [ 'cardav_name' ]))
2008-05-17 15:11:46 +02:00
{
2013-06-23 10:57:21 +02:00
if ( $is_group )
{
if (( $contact = $this -> bo -> read_list ( $save_ok )))
{
2021-09-20 16:01:22 +02:00
// re-read group to get correct etag (not duplicate etag code here)
2018-04-12 13:01:56 +02:00
$contact = $this -> read ( $contact [ 'list_' . self :: $path_attr ], $options [ 'path' ]);
2013-06-23 10:57:21 +02:00
}
}
else
{
$contact = $this -> bo -> read ( $save_ok );
}
//error_log(__METHOD__."(, $id, '$user') read(_list)($save_ok) returned ".array2string($contact));
2008-05-17 15:11:46 +02:00
}
2008-05-08 22:31:32 +02:00
2021-09-15 18:45:32 +02:00
// send evtl. necessary response headers: Location, etag, ...
2021-09-20 16:01:22 +02:00
$this -> put_response_headers ( $contact , $options [ 'path' ], $retval ,
// JSON uses 'id', while CardDAV uses carddav_name !== 'id'
( self :: $path_attr !== 'id' ) === ! $is_json , null ,
$is_group && $is_json ? self :: JS_CARDGROUP_ID_PREFIX : '' );
2011-04-05 22:39:13 +02:00
2012-01-31 20:47:52 +01:00
if ( $this -> debug > 1 ) error_log ( __METHOD__ . " (,' $id ', $user , ' $prefix ') returning " . array2string ( $retval ));
2011-04-05 22:39:13 +02:00
return $retval ;
2008-05-08 22:31:32 +02:00
}
2012-01-31 10:57:59 +01:00
/**
2021-09-20 16:01:22 +02:00
* Save distribution - list / group
2012-01-31 10:57:59 +01:00
*
* @ param array $contact
* @ param array | false $oldContact
2013-06-20 09:50:12 +02:00
* @ return int | boolean $list_id or false on error
2012-01-31 10:57:59 +01:00
*/
2012-02-18 11:22:59 +01:00
function save_group ( array & $contact , $oldContact = null )
2012-01-31 10:57:59 +01:00
{
$data = array ( 'list_name' => $contact [ 'n_fn' ]);
2012-02-20 12:25:41 +01:00
if ( ! isset ( $contact [ 'owner' ])) $contact [ 'owner' ] = $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ];
foreach ( array ( 'id' , 'carddav_name' , 'uid' , 'owner' ) as $name )
2012-01-31 10:57:59 +01:00
{
2013-08-02 18:31:54 +02:00
$data [ 'list_' . $name ] = $contact [ $name ];
2012-01-31 10:57:59 +01:00
}
2012-02-01 01:59:25 +01:00
//error_log(__METHOD__.'('.array2string($contact).', '.array2string($oldContact).') data='.array2string($data));
2018-04-12 13:01:56 +02:00
if (( $list_id = $this -> bo -> add_list ( empty ( $contact [ self :: $path_attr ]) ? null : array ( 'list_' . self :: $path_attr => $contact [ self :: $path_attr ]),
2012-01-31 10:57:59 +01:00
$contact [ 'owner' ], null , $data )))
{
2012-02-01 01:59:25 +01:00
// update members given in $contact['##X-ADDRESSBOOKSERVER-MEMBER']
2021-09-20 16:01:22 +02:00
$new_members = $contact [ 'members' ] ? : $contact [ '##X-ADDRESSBOOKSERVER-MEMBER' ];
if ( is_string ( $new_members ) && $new_members [ 1 ] === ':' && ( $n = unserialize ( $new_members )))
2012-01-31 10:57:59 +01:00
{
$new_members = $n [ 'values' ];
}
else
{
2021-09-20 16:01:22 +02:00
$new_members = ( array ) $new_members ;
2012-01-31 10:57:59 +01:00
}
2016-04-02 10:40:34 +02:00
foreach ( $new_members as & $uid )
{
2021-09-20 16:01:22 +02:00
if ( substr ( $uid , 0 , 9 ) === 'urn:uuid:' )
{
$uid = substr ( $uid , 9 ); // cut off "urn:uuid:" prefix
}
2016-04-02 10:40:34 +02:00
}
2012-01-31 10:57:59 +01:00
if ( $oldContact )
{
2012-02-01 01:59:25 +01:00
$to_add = array_diff ( $new_members , $oldContact [ 'members' ]);
$to_delete = array_diff ( $oldContact [ 'members' ], $new_members );
2012-01-31 10:57:59 +01:00
}
else
{
$to_add = $new_members ;
}
2012-02-01 01:59:25 +01:00
//error_log('to_add='.array2string($to_add).', to_delete='.array2string($to_delete));
2012-01-31 10:57:59 +01:00
if ( $to_add || $to_delete )
{
$to_add_ids = $to_delete_ids = array ();
$filter = array ( 'uid' => $to_delete ? array_merge ( $to_add , $to_delete ) : $to_add );
2013-08-06 12:18:12 +02:00
if (( $contacts =& $this -> bo -> search ( array (), array ( 'id' , 'uid' ), '' , '' , '' , False , 'AND' , false , $filter )))
2012-01-31 10:57:59 +01:00
{
2012-02-18 11:22:59 +01:00
foreach ( $contacts as $c )
2012-01-31 10:57:59 +01:00
{
2012-02-18 11:22:59 +01:00
if ( $to_delete && in_array ( $c [ 'uid' ], $to_delete ))
2012-01-31 10:57:59 +01:00
{
2012-02-18 11:22:59 +01:00
$to_delete_ids [] = $c [ 'id' ];
2012-01-31 10:57:59 +01:00
}
else
{
2012-02-18 11:22:59 +01:00
$to_add_ids [] = $c [ 'id' ];
2012-01-31 10:57:59 +01:00
}
}
}
2012-02-01 01:59:25 +01:00
//error_log('to_add_ids='.array2string($to_add_ids).', to_delete_ids='.array2string($to_delete_ids));
2012-01-31 10:57:59 +01:00
if ( $to_add_ids ) $this -> bo -> add2list ( $to_add_ids , $list_id , array ());
if ( $to_delete_ids ) $this -> bo -> remove_from_list ( $to_delete_ids , $list_id );
}
2013-08-02 18:31:54 +02:00
// reread as update of list-members updates etag and modified
2018-04-12 13:01:56 +02:00
if (( $contact = $this -> bo -> read_list ( $list_id )))
{
2021-09-20 16:01:22 +02:00
// re-read group to get correct etag (not duplicate etag code here)
2018-04-12 13:01:56 +02:00
$contact = $this -> read ( $contact [ 'list_' . self :: $path_attr ]);
}
2012-01-31 10:57:59 +01:00
}
2012-02-18 11:22:59 +01:00
if ( $this -> debug > 1 ) error_log ( __METHOD__ . '(' . array2string ( $contact ) . ', ' . array2string ( $oldContact ) . ') on return contact=' . array2string ( $data ) . ' returning ' . array2string ( $list_id ));
2013-08-02 18:31:54 +02:00
return $list_id ;
2012-01-31 10:57:59 +01:00
}
2010-01-06 00:25:17 +01:00
/**
* Query ctag for addressbook
2010-03-07 00:06:43 +01:00
*
2012-02-09 21:09:49 +01:00
* @ param string $path
* @ param int $user
2010-01-06 00:25:17 +01:00
* @ return string
*/
public function getctag ( $path , $user )
{
2012-09-23 22:19:35 +02:00
static $ctags = array (); // a little per request caching, in case ctag and sync-token is both requested
if ( isset ( $ctags [ $path ])) return $ctags [ $path ];
2012-09-26 16:30:47 +02:00
$user_in = $user ;
2010-12-02 20:42:03 +01:00
// not showing addressbook of a single user?
2012-02-29 10:58:15 +01:00
if ( is_null ( $user ) || $user === '' || $path == '/addressbook/' ) $user = null ;
2010-12-02 20:42:03 +01:00
2012-02-09 21:09:49 +01:00
// If "Sync selected addressbooks into one" is set --> ctag need to take selected AB's into account too
if ( $user && $user == $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ] && in_array ( 'O' , $this -> home_set_pref ))
{
$user = array_merge (( array ) $user , array_keys ( $this -> get_shared ( true ))); // true: ignore all-in-one pref
2013-08-22 18:39:21 +02:00
// include accounts ctag, if accounts stored different from contacts (eg.in LDAP or ADS)
if ( $this -> bo -> so_accounts && in_array ( '0' , $user ))
{
$accounts_ctag = $this -> bo -> get_ctag ( '0' );
}
2012-02-09 21:09:49 +01:00
}
$ctag = $this -> bo -> get_ctag ( $user );
2013-08-22 18:39:21 +02:00
2013-08-02 18:31:54 +02:00
// include lists-ctag, if enabled
if ( in_array ( 'D' , $this -> home_set_pref ))
2012-02-09 21:09:49 +01:00
{
$lists_ctag = $this -> bo -> lists_ctag ( $user );
}
2012-09-26 16:30:47 +02:00
//error_log(__METHOD__."('$path', ".array2string($user_in).") --> user=".array2string($user)." --> ctag=$ctag=".date('Y-m-d H:i:s',$ctag).", lists_ctag=".($lists_ctag ? $lists_ctag.'='.date('Y-m-d H:i:s',$lists_ctag) : '').' returning '.max($ctag,$lists_ctag));
2016-04-02 10:40:34 +02:00
unset ( $user_in );
2013-08-22 18:39:21 +02:00
return $ctags [ $path ] = max ( $ctag , $accounts_ctag , $lists_ctag );
2012-09-23 22:19:35 +02:00
}
2010-01-07 03:04:09 +01:00
/**
* Add extra properties for addressbook collections
*
2010-01-07 05:24:45 +01:00
* 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
2010-03-07 00:06:43 +01:00
*
2016-04-29 12:41:53 +02:00
* @ param array $props = array () regular props by the Api\CalDAV handler
2010-03-07 00:06:43 +01:00
* @ param string $displayname
2016-04-02 10:40:34 +02:00
* @ param string $base_uri = null base url of handler
* @ param int $user = null account_id of owner of collection
2010-01-07 03:04:09 +01:00
* @ return array
*/
2016-04-02 10:40:34 +02:00
public function extra_properties ( array $props , $displayname , $base_uri = null , $user = null )
2010-01-07 03:04:09 +01:00
{
2016-04-02 10:40:34 +02:00
unset ( $displayname , $base_uri , $user ); // not used, but required by function signature
2012-01-30 06:11:05 +01:00
if ( ! isset ( $props [ 'addressbook-description' ]))
{
// default addressbook description: can be overwritten via PROPPATCH, in which case it's already set
2016-04-02 12:44:17 +02:00
$props [ 'addressbook-description' ] = Api\CalDAV :: mkprop ( Api\CalDAV :: CARDDAV , 'addressbook-description' , $props [ 'displayname' ]);
2012-01-30 06:11:05 +01:00
}
2012-01-30 20:47:34 +01:00
// setting an max image size, so iOS scales the images before transmitting them
2014-01-28 12:24:47 +01:00
// we currently scale down to width of 240px, which tests shown to be ~20k
2016-04-02 12:44:17 +02:00
$props [ 'max-image-size' ] = Api\CalDAV :: mkprop ( Api\CalDAV :: CARDDAV , 'max-image-size' , 24 * 1024 );
2012-01-30 20:47:34 +01:00
2010-01-07 03:04:09 +01:00
// supported reports (required property for CardDAV)
2012-09-24 12:26:29 +02:00
$props [ 'supported-report-set' ] = array (
2016-04-02 12:44:17 +02:00
'addressbook-query' => Api\CalDAV :: mkprop ( 'supported-report' , array (
Api\CalDAV :: mkprop ( 'report' , array (
Api\CalDAV :: mkprop ( Api\CalDAV :: CARDDAV , 'addressbook-query' , '' ))))),
'addressbook-multiget' => Api\CalDAV :: mkprop ( 'supported-report' , array (
Api\CalDAV :: mkprop ( 'report' , array (
Api\CalDAV :: mkprop ( Api\CalDAV :: CARDDAV , 'addressbook-multiget' , '' ))))),
2021-05-18 14:02:51 +02:00
'sync-collection' => Api\CalDAV :: mkprop ( 'supported-report' , array (
Api\CalDAV :: mkprop ( 'report' , array (
Api\CalDAV :: mkprop ( 'sync-collection' , '' ))))),
2012-09-24 12:26:29 +02:00
);
2010-01-07 03:04:09 +01:00
return $props ;
}
2008-05-08 22:31:32 +02:00
/**
* Get the handler and set the supported fields
*
2008-05-10 14:02:49 +02:00
* @ return addressbook_vcal
2008-05-08 22:31:32 +02:00
*/
private function _get_handler ()
{
2012-02-01 20:52:19 +01:00
$handler = new addressbook_vcal ( 'addressbook' , 'text/vcard' );
2013-02-19 20:53:20 +01:00
$supportedFields = $handler -> supportedFields ;
2012-02-01 20:52:19 +01:00
// Apple iOS or OS X addressbook
2012-02-10 22:29:50 +01:00
if ( $this -> agent == 'cfnetwork' || $this -> agent == 'dataaccess' )
2010-09-25 17:19:48 +02:00
{
2012-06-17 14:28:16 +02:00
$databaseFields = $handler -> databaseFields ;
2012-02-01 20:52:19 +01:00
// use just CELL and IPHONE, CELL;WORK and CELL;HOME are NOT understood
//'TEL;CELL;WORK' => array('tel_cell'),
//'TEL;CELL;HOME' => array('tel_cell_private'),
2013-02-19 20:53:20 +01:00
$supportedFields [ 'TEL;CELL' ] = array ( 'tel_cell' );
unset ( $supportedFields [ 'TEL;CELL;WORK' ]);
$supportedFields [ 'TEL;IPHONE' ] = array ( 'tel_cell_private' );
unset ( $supportedFields [ 'TEL;CELL;HOME' ]);
2012-06-17 14:28:16 +02:00
$databaseFields [ 'X-ABSHOWAS' ] = $supportedFields [ 'X-ABSHOWAS' ] = array ( 'fileas_type' ); // Horde vCard class uses uppercase prop-names!
2012-07-31 18:18:26 +02:00
2012-02-14 16:14:41 +01:00
// Apple Addressbook pre Lion (OS X 10.7) messes up CLASS and CATEGORIES (Lion cant set them but leaves them alone)
2016-04-02 10:40:34 +02:00
$matches = null ;
2012-07-31 18:18:26 +02:00
if ( preg_match ( '|CFNetwork/([0-9]+)|i' , $_SERVER [ 'HTTP_USER_AGENT' ], $matches ) && $matches [ 1 ] < 520 ||
// iOS 5.1.1 does not display CLASS or CATEGORY, but wrongly escapes multiple, comma-separated categories
// and appends CLASS: PUBLIC to an empty NOTE: field --> leaving them out for iOS
$this -> agent == 'dataaccess' )
2012-02-14 16:14:41 +01:00
{
unset ( $supportedFields [ 'CLASS' ]);
2012-06-17 14:28:16 +02:00
unset ( $databaseFields [ 'CLASS' ]);
2012-02-14 16:14:41 +01:00
unset ( $supportedFields [ 'CATEGORIES' ]);
2012-06-17 14:28:16 +02:00
unset ( $databaseFields [ 'CATEGORIES' ]);
2012-07-31 18:18:26 +02:00
}
if ( preg_match ( '|CFNetwork/([0-9]+)|i' , $_SERVER [ 'HTTP_USER_AGENT' ], $matches ) && $matches [ 1 ] < 520 )
{
2012-02-14 16:14:41 +01:00
// gd cant parse or resize images stored from snow leopard addressbook: gd-jpeg:
// - JPEG library reports unrecoverable error
// - Passed data is not in 'JPEG' format
// - Couldn't create GD Image Stream out of Data
// FF (10), Safari (5.1.3) and Chrome (17) cant display it either --> ignore images
unset ( $supportedFields [ 'PHOTO' ]);
2012-06-17 14:28:16 +02:00
unset ( $databaseFields [ 'PHOTO' ]);
2012-02-14 16:14:41 +01:00
}
2012-06-17 14:28:16 +02:00
$handler -> setDatabaseFields ( $databaseFields );
2012-02-01 20:52:19 +01:00
}
2013-02-19 20:53:20 +01:00
$handler -> setSupportedFields ( 'GroupDAV' , $this -> agent , $supportedFields );
2008-05-08 22:31:32 +02:00
return $handler ;
}
/**
* Handle delete request for an event
*
* @ param array & $options
* @ param int $id
2018-10-09 13:14:36 +02:00
* @ param int $user account_id of collection owner
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' )
*/
2018-10-09 13:14:36 +02:00
function delete ( & $options , $id , $user )
2008-05-08 22:31:32 +02:00
{
2018-10-09 13:14:36 +02:00
unset ( $user ); // not used, but required by function signature
2008-05-17 15:11:46 +02:00
if ( ! is_array ( $contact = $this -> _common_get_put_delete ( 'DELETE' , $options , $id )))
2008-05-08 22:31:32 +02:00
{
2008-05-17 15:11:46 +02:00
return $contact ;
2008-05-08 22:31:32 +02:00
}
2012-10-01 19:23:44 +02:00
if ( isset ( $contact [ 'list_id' ]))
{
$ok = $this -> bo -> delete_list ( $contact [ 'list_id' ]) !== false ;
}
elseif (( $ok = $this -> bo -> delete ( $contact [ 'id' ], self :: etag2value ( $this -> http_if_match ))) === 0 )
2008-05-08 22:31:32 +02:00
{
return '412 Precondition Failed' ;
}
2012-10-01 19:23:44 +02:00
return $ok ;
2008-05-08 22:31:32 +02:00
}
/**
* Read a contact
*
2011-10-04 16:18:35 +02:00
* We have to make sure to not return or even consider in read deleted contacts , as the might have
* the same UID and / or carddav_name as not deleted contacts and would block access to valid entries
*
2012-02-09 21:09:49 +01:00
* @ param string | int $id
2016-04-02 10:40:34 +02:00
* @ param string $path = null
2012-02-09 21:09:49 +01:00
* @ 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-09 21:09:49 +01:00
function read ( $id , $path = null )
2008-05-08 22:31:32 +02:00
{
2016-04-02 10:40:34 +02:00
static $non_deleted_tids = null ;
2011-10-04 16:18:35 +02:00
if ( is_null ( $non_deleted_tids ))
{
2016-04-02 10:40:34 +02:00
$tids = $this -> bo -> content_types ;
unset ( $tids [ Api\Contacts :: DELETED_TYPE ]);
$non_deleted_tids = array_keys ( $tids );
2011-10-04 16:18:35 +02:00
}
2021-09-17 20:15:36 +02:00
$keys = [ 'tid' => $non_deleted_tids ];
2021-09-19 11:09:44 +02:00
2021-09-17 20:15:36 +02:00
// with REST/JSON we only use our id, but DELETE request has neither Accept nor Content-Type header to detect JSON request
2021-09-20 16:01:22 +02:00
if ( preg_match ( '/^(' . self :: JS_CARDGROUP_ID_PREFIX . ')?(\d+)$/' , $id , $matches ))
2021-09-17 20:15:36 +02:00
{
2021-09-19 11:09:44 +02:00
if ( ! empty ( $matches [ 1 ]))
{
$keys = [ 'list_id' => $matches [ 2 ]];
}
else
{
$keys [ 'id' ] = $id ;
}
2021-09-17 20:15:36 +02:00
}
2021-09-25 13:04:49 +02:00
// json with uid
2021-12-16 18:53:04 +01:00
elseif ( empty ( self :: $path_extension ) && self :: $path_attr === 'id' && ( string ) $id !== ( string )( int ) $id )
2021-09-25 13:04:49 +02:00
{
$keys [ 'uid' ] = $id ;
}
2021-09-17 20:15:36 +02:00
else
{
$keys [ self :: $path_attr ] = $id ;
}
2021-09-19 11:09:44 +02:00
$contact = isset ( $keys [ 'list_id' ]) ? false : $this -> bo -> read ( $keys );
2011-06-19 11:01:15 +02:00
2013-08-22 18:39:21 +02:00
// if contact not found and accounts stored NOT like contacts, try reading it without path-extension as id
if ( is_null ( $contact ) && $this -> bo -> so_accounts && ( $c = $this -> bo -> read ( $test = basename ( $id , '.vcf' ))))
{
$contact = $c ;
}
2012-01-31 10:57:59 +01:00
// see if we have a distribution-list / group with that id
2012-02-01 01:31:24 +01:00
// bo->read_list(..., true) limits returned uid to same owner's addressbook, as iOS and OS X addressbooks
// only understands/shows that and if return more, save_lists would delete the others ones on update!
2012-02-09 21:09:49 +01:00
$limit_in_ab = true ;
list (, $account_lid , $app ) = explode ( '/' , $path ); // eg. /<username>/addressbook/<id>
// /<username>/addressbook/ with home_set_prefs containing 'O'=all-in-one contains selected ab's
if ( $account_lid == $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_lid' ] && $app == 'addressbook' && in_array ( 'O' , $this -> home_set_pref ))
{
$limit_in_ab = array_keys ( $this -> get_shared ( true ));
$limit_in_ab [] = $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ];
}
/* we are currently not syncing distribution - lists / groups to / addressbook / as
2021-09-19 11:09:44 +02:00
* Apple clients use that only as directory gateway */
elseif ( Api\CalDAV :: isJSON () && $account_lid == 'addressbook' ) // /addressbook/ contains all readably contacts
2012-02-09 21:09:49 +01:00
{
$limit_in_ab = array_keys ( $this -> bo -> grants );
2021-09-19 11:09:44 +02:00
}
if ( ! $contact && ( $contact = $this -> bo -> read_lists ( isset ( $keys [ 'list_id' ]) ? $keys :
[ 'list_' . self :: $path_attr => $id ], 'contact_uid' , $limit_in_ab )))
2012-01-31 10:57:59 +01:00
{
$contact = array_shift ( $contact );
$contact [ 'n_fn' ] = $contact [ 'n_family' ] = $contact [ 'list_name' ];
2012-02-01 01:59:25 +01:00
foreach ( array ( 'owner' , 'id' , 'carddav_name' , 'modified' , 'modifier' , 'created' , 'creator' , 'etag' , 'uid' ) as $name )
2012-01-31 10:57:59 +01:00
{
$contact [ $name ] = $contact [ 'list_' . $name ];
}
2012-02-09 21:09:49 +01:00
// if NOT limited to containing AB ($limit_in_ab === true), add that limit to etag
if ( $limit_in_ab !== true )
{
$contact [ 'etag' ] .= ':' . implode ( '-' , $limit_in_ab );
}
2012-01-31 10:57:59 +01:00
}
2012-01-31 20:47:52 +01:00
elseif ( $contact === array ()) // not found from read_lists()
{
$contact = null ;
}
2012-01-31 10:57:59 +01:00
2016-03-06 14:45:15 +01:00
if ( $contact && $contact [ 'tid' ] == Api\Contacts :: DELETED_TYPE )
2011-06-19 11:01:15 +02:00
{
$contact = null ; // handle deleted events, as not existing (404 Not Found)
}
2012-01-31 20:47:52 +01:00
if ( $this -> debug > 1 ) error_log ( __METHOD__ . " (' $id ') returning " . array2string ( $contact ));
2011-06-19 11:01:15 +02:00
return $contact ;
2008-05-08 22:31:32 +02:00
}
/**
* Check if user has the neccessary rights on a contact
*
2016-04-29 12:41:53 +02:00
* @ param int $acl Acl :: READ , Acl :: EDIT or Acl :: DELETE
2012-02-09 21:09:49 +01:00
* @ param array | int $contact contact - 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
*/
function check_access ( $acl , $contact )
{
2013-01-22 14:19:07 +01:00
return $this -> bo -> check_perms ( $acl , $contact , true ); // true = deny to delete accounts
2008-05-08 22:31:32 +02:00
}
2012-02-04 02:03:56 +01:00
2013-01-22 09:37:58 +01:00
/**
* Get grants of current user and app
*
* Reimplemented to account for static LDAP ACL and accounts ( owner = 0 )
*
2024-07-12 08:16:33 +02:00
* @ param ? string $user the user whose grants for the current user are requested , or null for all
* @ return array user - id => Api\Acl :: ADD | Api\Acl :: READ | Api\Acl :: EDIT | Api\Acl :: DELETE pairs
2013-01-22 09:37:58 +01:00
*/
2024-07-12 08:16:33 +02:00
public function get_grants ( string $user = null )
2013-01-22 09:37:58 +01:00
{
2015-07-23 19:40:48 +02:00
$grants = $this -> bo -> get_grants ( $this -> bo -> user );
// remove add and delete grants for accounts (for admins too)
2024-07-12 08:16:33 +02:00
// as accounts can not be created as contacts, they e.g. need further data
// and admins might not recognize they delete an account incl. its data
2015-07-23 19:40:48 +02:00
if ( isset ( $grants [ 0 ])) $grants [ 0 ] &= ~ ( EGW_ACL_ADD | EGW_ACL_DELETE );
return $grants ;
2013-01-22 09:37:58 +01:00
}
2012-02-04 02:03:56 +01:00
/**
* Return calendars / addressbooks shared from other users with the current one
*
2016-04-02 10:40:34 +02:00
* @ param boolean $ignore_all_in_one = false if true , return selected addressbooks and not array () for all - in - one
2012-02-04 22:47:28 +01:00
* @ return array account_id => account_lid pairs
2012-02-04 02:03:56 +01:00
*/
2012-02-04 22:47:28 +01:00
function get_shared ( $ignore_all_in_one = false )
2012-02-04 02:03:56 +01:00
{
$shared = array ();
2012-02-04 22:47:28 +01:00
// if "Sync all selected addressbook into one" is set --> no (additional) shared addressbooks
2012-02-09 21:09:49 +01:00
if ( ! $ignore_all_in_one && in_array ( 'O' , $this -> home_set_pref )) return array ();
2012-02-04 22:47:28 +01:00
2012-02-04 02:03:56 +01:00
// replace symbolic id's with real nummeric id's
foreach ( array (
'G' => $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_primary_group' ],
'U' => '0' ,
) as $sym => $id )
{
2012-02-09 21:09:49 +01:00
if (( $key = array_search ( $sym , $this -> home_set_pref )) !== false )
2012-02-04 02:03:56 +01:00
{
2012-02-09 21:09:49 +01:00
$this -> home_set_pref [ $key ] = $id ;
2012-02-04 02:03:56 +01:00
}
}
2016-04-29 12:41:53 +02:00
foreach ( array_keys ( $this -> bo -> get_addressbooks ( Acl :: READ )) as $id )
2012-02-04 02:03:56 +01:00
{
2017-12-01 14:58:44 +01:00
if (( $id || $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'addressbook' ][ 'hide_accounts' ] !== '1' ) &&
2012-02-04 22:47:28 +01:00
$GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ] != $id && // no current user and no accounts, if disabled in ab prefs
2012-02-09 21:09:49 +01:00
( in_array ( 'A' , $this -> home_set_pref ) || in_array (( string ) $id , $this -> home_set_pref )) &&
2012-02-04 02:03:56 +01:00
is_numeric ( $id ) && ( $owner = $id ? $this -> accounts -> id2name ( $id ) : 'accounts' ))
{
2012-09-27 17:46:08 +02:00
$shared [ $id ] = 'addressbook-' . $owner ;
2012-02-04 02:03:56 +01:00
}
}
return $shared ;
}
2012-02-04 02:24:34 +01:00
2016-04-27 15:27:22 +02:00
/**
* Hook to add properties to CardDAV root
*
* OS X 10.11 . 4 addressbook does a propfind for " addressbook-home-set " and " directory-gateway "
* in the root and does not continue without it .
*
* @ param array $data
*/
public static function groupdav_root_props ( array $data )
{
$data [ 'props' ][ 'addressbook-home-set' ] = Api\CalDAV :: mkprop ( Api\CalDAV :: CARDDAV , 'addressbook-home-set' , array (
Api\CalDAV :: mkprop ( 'href' , $data [ 'caldav' ] -> base_uri . '/' . $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_lid' ] . '/' )));
2016-04-27 15:45:46 +02:00
$data [ 'props' ][ 'principal-address' ] = Api\CalDAV :: mkprop ( Api\CalDAV :: CARDDAV , 'principal-address' ,
2017-12-01 14:58:44 +01:00
$GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'addressbook' ][ 'hide_accounts' ] === '1' ? '' : array (
2016-04-27 15:45:46 +02:00
Api\CalDAV :: mkprop ( 'href' , $data [ 'caldav' ] -> base_uri . '/addressbook-accounts/' . $GLOBALS [ 'egw_info' ][ 'user' ][ 'person_id' ] . '.vcf' )));
2016-04-27 15:27:22 +02:00
$data [ 'props' ][ 'directory-gateway' ] = Api\CalDAV :: mkprop ( Api\CalDAV :: CARDDAV , 'directory-gateway' , array (
Api\CalDAV :: mkprop ( 'href' , $data [ 'caldav' ] -> base_uri . '/addressbook/' )));
}
2012-02-04 02:24:34 +01:00
/**
* Return appliction specific settings
*
2014-07-23 16:00:20 +02:00
* @ param array $hook_data values for keys 'location' , 'type' and 'account_id'
2012-02-14 18:38:45 +01:00
* @ return array of array with settings
2012-02-04 02:24:34 +01:00
*/
2012-02-14 18:38:45 +01:00
static function get_settings ( $hook_data )
2012-02-04 02:24:34 +01:00
{
$addressbooks = array (
'A' => lang ( 'All' ),
'G' => lang ( 'Primary Group' ),
'U' => lang ( 'Accounts' ),
2012-02-04 22:47:28 +01:00
'O' => lang ( 'Sync all selected into one' ),
'D' => lang ( 'Distribution lists as groups' )
2014-07-23 16:00:20 +02:00
);
2023-04-27 16:19:00 +02:00
if ( ! isset ( $hook_data [ 'setup' ]))
2014-07-23 16:00:20 +02:00
{
2023-04-27 16:19:00 +02:00
$user = in_array ( $hook_data [ 'type' ], array ( 'user' , 'group' )) ? $hook_data [ 'account_id' ] :
// for default or forced prefs show current users addressbooks
$GLOBALS [ 'egw_info' ][ 'user' ][ 'account_id' ];
2016-04-29 12:41:53 +02:00
$addressbook_bo = new Api\Contacts ();
$addressbooks += $addressbook_bo -> get_addressbooks ( Acl :: READ , null , $user );
2023-04-27 16:19:00 +02:00
if ( $user > 0 || ! in_array ( $hook_data [ 'type' ], array ( 'user' , 'group' )))
{
unset ( $addressbooks [ $user ]); // personal AB is always synced, and don't show if for default and forced prefs
}
2014-07-23 16:00:20 +02:00
unset ( $addressbooks [ $user . 'p' ]); // ignore (optional) private addressbook for now
}
2012-02-04 02:24:34 +01:00
2014-02-20 14:53:05 +01:00
// allow to force no other addressbooks
2014-07-23 16:00:20 +02:00
if ( $hook_data [ 'type' ] === 'forced' )
2014-02-20 14:53:05 +01:00
{
$addressbooks [ 'N' ] = lang ( 'None' );
}
2023-04-27 16:19:00 +02:00
// rewriting owner=0 to 'U', as 0 gets always selected by prefs
2014-07-23 16:00:20 +02:00
// not removing it for default or forced prefs based on current users pref
if ( ! isset ( $addressbooks [ 0 ]) && ( in_array ( $hook_data [ 'type' ], array ( 'user' , 'group' )) ||
2017-12-01 14:58:44 +01:00
$GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'addressbook' ][ 'hide_accounts' ] === '1' ))
2012-02-04 02:24:34 +01:00
{
unset ( $addressbooks [ 'U' ]);
}
else
{
unset ( $addressbooks [ 0 ]);
}
$settings = array ();
$settings [ 'addressbook-home-set' ] = array (
'type' => 'multiselect' ,
'label' => 'Addressbooks to sync in addition to personal addressbook' ,
'name' => 'addressbook-home-set' ,
2014-07-13 11:01:09 +02:00
'help' => lang ( 'Only supported by a few fully conformant clients (eg. from Apple). If you have to enter a URL, it will most likely not be supported!' ) .
2012-02-04 22:47:28 +01:00
'<br/>' . lang ( 'They will be sub-folders in users home (%1 attribute).' , 'CardDAV "addressbook-home-set"' ) .
'<br/>' . lang ( 'Select "%1", if your client does not support multiple addressbooks.' , lang ( 'Sync all selected into one' )) .
'<br/>' . lang ( 'Select "%1", if your client support groups, eg. OS X or iOS addressbook.' , lang ( 'Distribution lists as groups' )),
2012-02-04 02:24:34 +01:00
'values' => $addressbooks ,
'xmlrpc' => True ,
'admin' => False ,
);
return $settings ;
}
2023-02-13 22:41:57 +01:00
}