2012-06-03 18:19:28 +00:00
< ? php
2012-07-08 17:12:58 +00:00
$a = get_app ();
$uri = parse_url ( $a -> get_baseurl ());
2012-06-04 17:21:54 +00:00
$path = " / " ;
2012-07-08 17:12:58 +00:00
if ( isset ( $uri [ " path " ]) && strlen ( $uri [ " path " ]) > 1 ) {
2012-06-04 17:18:20 +00:00
$path = $uri [ " path " ] . " / " ;
}
2012-06-03 18:19:28 +00:00
define ( " CALDAV_SQL_DB " , " " );
define ( " CALDAV_SQL_PREFIX " , " dav_ " );
2012-06-04 17:18:20 +00:00
define ( " CALDAV_URL_PREFIX " , $path . " dav/ " );
2012-07-30 19:07:20 +00:00
define ( " DAV_APPNAME " , " Friendica " );
2012-06-03 18:19:28 +00:00
define ( " CALDAV_NAMESPACE_PRIVATE " , 1 );
2012-07-08 17:12:58 +00:00
define ( " CALDAV_FRIENDICA_MINE " , " friendica-mine " );
define ( " CALDAV_FRIENDICA_CONTACTS " , " friendica-contacts " );
2018-01-15 13:15:33 +00:00
$GLOBALS [ " CALDAV_PRIVATE_SYSTEM_CALENDARS " ] = [ CALDAV_FRIENDICA_MINE , CALDAV_FRIENDICA_CONTACTS ];
$GLOBALS [ " CALDAV_PRIVATE_SYSTEM_BACKENDS " ] = [ " Sabre_CalDAV_Backend_Friendica " ];
2012-06-03 18:19:28 +00:00
2012-08-02 20:26:52 +00:00
define ( " CARDDAV_NAMESPACE_PRIVATE " , 1 );
define ( " CARDDAV_FRIENDICA_CONTACT " , " friendica " );
2018-01-15 13:15:33 +00:00
$GLOBALS [ " CARDDAV_PRIVATE_SYSTEM_ADDRESSBOOKS " ] = [ CARDDAV_FRIENDICA_CONTACT ];
$GLOBALS [ " CARDDAV_PRIVATE_SYSTEM_BACKENDS " ] = [ " Sabre_CardDAV_Backend_Friendica " ];
2012-08-02 20:26:52 +00:00
$GLOBALS [ " CALDAV_ACL_PLUGIN_CLASS " ] = " Sabre_DAVACL_Plugin_Friendica " ;
2012-06-03 18:19:28 +00:00
2012-07-08 17:12:58 +00:00
define ( " CALDAV_MAX_YEAR " , date ( " Y " ) + 5 );
2012-06-03 18:19:28 +00:00
2012-06-10 16:29:31 +00:00
/**
* @ return int
*/
2012-07-08 17:12:58 +00:00
function getCurMicrotime ()
{
list ( $usec , $sec ) = explode ( " " , microtime ());
return sprintf ( " %14.0f " , $sec * 10000 + $usec * 10000 );
2012-06-03 18:19:28 +00:00
} // function getCurMicrotime
2012-06-10 16:29:31 +00:00
/**
*
*/
2012-07-08 17:12:58 +00:00
function debug_time ()
{
$cur = getCurMicrotime ();
if ( $GLOBALS [ " debug_time_last " ] > 0 ) {
echo " Zeit: " . ( $cur - $GLOBALS [ " debug_time_last " ]) . " <br> \n " ;
}
$GLOBALS [ " debug_time_last " ] = $cur ;
2012-06-03 18:19:28 +00:00
}
/**
* @ param string $username
* @ return int | null
*/
function dav_compat_username2id ( $username = " " )
{
2012-07-08 17:12:58 +00:00
$x = q ( " SELECT `uid` FROM `user` WHERE `nickname`='%s' AND `account_removed` = 0 AND `account_expired` = 0 " , dbesc ( $username ));
2012-06-03 18:19:28 +00:00
if ( count ( $x ) == 1 ) return $x [ 0 ][ " uid " ];
return null ;
}
/**
* @ param int $id
* @ return string
*/
function dav_compat_id2username ( $id = 0 )
{
2012-07-08 17:12:58 +00:00
$x = q ( " SELECT `nickname` FROM `user` WHERE `uid` = %i AND `account_removed` = 0 AND `account_expired` = 0 " , IntVal ( $id ));
2012-06-03 18:19:28 +00:00
if ( count ( $x ) == 1 ) return $x [ 0 ][ " nickname " ];
return " " ;
}
/**
* @ return int
*/
2012-07-08 17:12:58 +00:00
function dav_compat_get_curr_user_id ()
{
2012-06-03 18:19:28 +00:00
$a = get_app ();
return IntVal ( $a -> user [ " uid " ]);
}
/**
* @ param string $principalUri
* @ return int | null
*/
function dav_compat_principal2uid ( $principalUri = " " )
{
if ( strlen ( $principalUri ) == 0 ) return null ;
if ( $principalUri [ 0 ] == " / " ) $principalUri = substr ( $principalUri , 1 );
if ( strpos ( $principalUri , " principals/users/ " ) !== 0 ) return null ;
$username = substr ( $principalUri , strlen ( " principals/users/ " ));
return dav_compat_username2id ( $username );
}
2012-07-08 17:12:58 +00:00
/**
* @ param string $principalUri
* @ return array | null
*/
function dav_compat_principal2namespace ( $principalUri = " " )
{
if ( strlen ( $principalUri ) == 0 ) return null ;
if ( $principalUri [ 0 ] == " / " ) $principalUri = substr ( $principalUri , 1 );
if ( strpos ( $principalUri , " principals/users/ " ) !== 0 ) return null ;
$username = substr ( $principalUri , strlen ( " principals/users/ " ));
2018-01-15 13:15:33 +00:00
return [ " namespace " => CALDAV_NAMESPACE_PRIVATE , " namespace_id " => dav_compat_username2id ( $username )];
2012-07-08 17:12:58 +00:00
}
2012-07-28 09:10:48 +00:00
/**
* @ return string
*/
function dav_compat_currentUserPrincipal ()
{
2012-07-08 17:12:58 +00:00
$a = get_app ();
return " principals/users/ " . strtolower ( $a -> user [ " nickname " ]);
}
2012-06-10 16:29:31 +00:00
/**
* @ param string $name
* @ return null | string
*/
2012-07-08 17:12:58 +00:00
function dav_compat_getRequestVar ( $name = " " )
{
if ( isset ( $_REQUEST [ $name ])) return $_REQUEST [ $name ];
2012-06-10 16:29:31 +00:00
else return null ;
}
2012-06-03 18:19:28 +00:00
/**
* @ param $text
2012-06-10 16:29:31 +00:00
* @ return null | string
2012-06-03 18:19:28 +00:00
*/
2012-06-10 16:29:31 +00:00
function dav_compat_parse_text_serverside ( $text )
2012-06-03 18:19:28 +00:00
{
2012-06-10 16:29:31 +00:00
return dav_compat_getRequestVar ( $text );
}
/**
* @ param string $uri
*/
2012-07-08 17:12:58 +00:00
function dav_compat_redirect ( $uri = " " )
{
2012-06-10 16:29:31 +00:00
goaway ( $uri );
2012-06-03 18:19:28 +00:00
}
2012-07-08 17:12:58 +00:00
/**
* @ return null | int
*/
function dav_compat_get_max_private_calendars ()
{
return null ;
}
2012-07-30 19:07:20 +00:00
/**
* @ return string
*/
function dav_compat_get_hostname () {
$a = get_app ();
return $a -> get_hostname ();
}
2012-06-03 18:19:28 +00:00
/**
* @ param int $namespace
* @ param int $namespace_id
2012-07-08 17:12:58 +00:00
* @ param string $uri
* @ param array $calendar
* @ return Sabre_CalDAV_Backend_Common
2012-06-03 18:19:28 +00:00
* @ throws Exception
*/
2012-07-08 17:12:58 +00:00
function wdcal_calendar_factory ( $namespace , $namespace_id , $uri , $calendar = null )
2012-06-03 18:19:28 +00:00
{
switch ( $namespace ) {
case CALDAV_NAMESPACE_PRIVATE :
2012-07-08 17:12:58 +00:00
if ( $uri == CALDAV_FRIENDICA_MINE || $uri == CALDAV_FRIENDICA_CONTACTS ) return Sabre_CalDAV_Backend_Friendica :: getInstance ();
else return Sabre_CalDAV_Backend_Private :: getInstance ();
2012-06-03 18:19:28 +00:00
}
throw new Exception ( " Calendar Namespace not found " );
}
2012-07-08 17:12:58 +00:00
/**
* @ param int $calendar_id
* @ return Sabre_CalDAV_Backend_Common
* @ throws Exception
*/
2012-07-28 09:10:48 +00:00
function wdcal_calendar_factory_by_id ( $calendar_id )
{
2012-07-08 17:12:58 +00:00
$calendar = Sabre_CalDAV_Backend_Common :: loadCalendarById ( $calendar_id );
return wdcal_calendar_factory ( $calendar [ " namespace " ], $calendar [ " namespace_id " ], $calendar [ " uri " ], $calendar );
}
2012-06-03 18:19:28 +00:00
/**
2012-07-28 09:10:48 +00:00
* @ param int $user_id
* @ param bool $withcheck
* @ return array
2012-06-03 18:19:28 +00:00
*/
2012-07-28 09:10:48 +00:00
function wdcal_create_std_calendars_get_statements ( $user_id , $withcheck = true )
2012-06-03 18:19:28 +00:00
{
2018-01-15 13:15:33 +00:00
$stms = [];
2012-06-03 18:19:28 +00:00
$a = get_app ();
2018-01-15 13:15:33 +00:00
$uris = [
2012-07-08 17:12:58 +00:00
'private' => t ( " Private Calendar " ),
CALDAV_FRIENDICA_MINE => t ( " Friendica Events: Mine " ),
CALDAV_FRIENDICA_CONTACTS => t ( " Friendica Events: Contacts " ),
2018-01-15 13:15:33 +00:00
];
2012-07-08 17:12:58 +00:00
foreach ( $uris as $uri => $name ) {
$cals = q ( " SELECT * FROM %s%scalendars WHERE `namespace` = %d AND `namespace_id` = %d AND `uri` = '%s' " ,
2012-07-28 09:10:48 +00:00
CALDAV_SQL_DB , CALDAV_SQL_PREFIX , CALDAV_NAMESPACE_PRIVATE , IntVal ( $user_id ), dbesc ( $uri ));
if ( count ( $cals ) == 0 || ! $withcheck ) $stms [] =
sprintf ( " INSERT INTO %s%scalendars (`namespace`, `namespace_id`, `displayname`, `timezone`, `ctag`, `uri`, `has_vevent`, `has_vtodo`)
VALUES ( % d , % d , '%s' , '%s' , 1 , '%s' , 1 , 0 ) " ,
CALDAV_SQL_DB , CALDAV_SQL_PREFIX , CALDAV_NAMESPACE_PRIVATE , IntVal ( $user_id ), dbesc ( $name ), dbesc ( $a -> timezone ), dbesc ( $uri ));
2012-06-03 18:19:28 +00:00
}
2012-07-28 09:10:48 +00:00
return $stms ;
}
/**
*/
function wdcal_create_std_calendars ()
{
$a = get_app ();
if ( ! local_user ()) return ;
$privates = q ( " SELECT COUNT(*) num FROM %s%scalendars WHERE `namespace` = %d AND `namespace_id` = %d " , CALDAV_SQL_DB , CALDAV_SQL_PREFIX , CALDAV_NAMESPACE_PRIVATE , IntVal ( $a -> user [ " uid " ]));
if ( $privates [ 0 ][ " num " ] > 0 ) return ;
2012-07-14 17:02:21 +00:00
2012-07-28 09:10:48 +00:00
$stms = wdcal_create_std_calendars_get_statements ( $a -> user [ " uid " ]);
foreach ( $stms as $stmt ) q ( $stmt );
2012-06-03 18:19:28 +00:00
}
2012-08-02 20:26:52 +00:00
/**
* @ param int $user_id
* @ param bool $withcheck
* @ return array
*/
function wdcal_create_std_addressbooks_get_statements ( $user_id , $withcheck = true )
{
2018-01-15 13:15:33 +00:00
$stms = [];
2012-08-02 20:26:52 +00:00
$a = get_app ();
2018-01-15 13:15:33 +00:00
$uris = [
2012-08-02 20:26:52 +00:00
'private' => t ( " Private Addresses " ),
CARDDAV_FRIENDICA_CONTACT => t ( " Friendica Contacts " ),
2018-01-15 13:15:33 +00:00
];
2012-08-02 20:26:52 +00:00
foreach ( $uris as $uri => $name ) {
$cals = q ( " SELECT * FROM %s%saddressbooks WHERE `namespace` = %d AND `namespace_id` = %d AND `uri` = '%s' " ,
CALDAV_SQL_DB , CALDAV_SQL_PREFIX , CALDAV_NAMESPACE_PRIVATE , IntVal ( $user_id ), dbesc ( $uri ));
if ( count ( $cals ) == 0 || ! $withcheck ) $stms [] =
sprintf ( " INSERT INTO %s%saddressbooks (`namespace`, `namespace_id`, `displayname`, `ctag`, `uri`)
VALUES ( % d , % d , '%s' , 1 , '%s' ) " ,
CALDAV_SQL_DB , CALDAV_SQL_PREFIX , CARDDAV_NAMESPACE_PRIVATE , IntVal ( $user_id ), dbesc ( $name ), dbesc ( $uri ));
}
return $stms ;
}
/**
*/
function wdcal_create_std_addressbooks ()
{
$a = get_app ();
if ( ! local_user ()) return ;
$privates = q ( " SELECT COUNT(*) num FROM %s%addressbooks WHERE `namespace` = %d AND `namespace_id` = %d " , CALDAV_SQL_DB , CALDAV_SQL_PREFIX , CARDDAV_NAMESPACE_PRIVATE , IntVal ( $a -> user [ " uid " ]));
if ( $privates [ 0 ][ " num " ] > 0 ) return ;
$stms = wdcal_create_std_addressbooks_get_statements ( $a -> user [ " uid " ]);
foreach ( $stms as $stmt ) q ( $stmt );
}