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-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 " );
$GLOBALS [ " CALDAV_PRIVATE_SYSTEM_CALENDARS " ] = array ( CALDAV_FRIENDICA_MINE , CALDAV_FRIENDICA_CONTACTS );
2012-06-03 18:19:28 +00:00
define ( " CARDDAV_NAMESPACE_COMMUNITYCONTACTS " , 1 );
define ( " CARDDAV_NAMESPACE_PHONECONTACTS " , 2 );
2012-07-08 17:12:58 +00:00
define ( " CALDAV_DB_VERSION " , 2 );
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/ " ));
return array ( " namespace " => CALDAV_NAMESPACE_PRIVATE , " namespace_id " => dav_compat_username2id ( $username ));
}
function dav_compat_currentUserPrincipal () {
$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-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
*/
function wdcal_calendar_factory_by_id ( $calendar_id ) {
$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
/**
*/
function wdcal_create_std_calendars ()
{
$a = get_app ();
if ( ! local_user ()) return ;
2012-07-14 17:02:21 +00:00
$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-08 17:12:58 +00:00
$uris = array (
'private' => t ( " Private Calendar " ),
CALDAV_FRIENDICA_MINE => t ( " Friendica Events: Mine " ),
CALDAV_FRIENDICA_CONTACTS => t ( " Friendica Events: Contacts " ),
);
foreach ( $uris as $uri => $name ) {
$cals = q ( " SELECT * FROM %s%scalendars WHERE `namespace` = %d AND `namespace_id` = %d AND `uri` = '%s' " ,
CALDAV_SQL_DB , CALDAV_SQL_PREFIX , CALDAV_NAMESPACE_PRIVATE , $a -> user [ " uid " ], dbesc ( $uri ));
if ( count ( $cals ) == 0 ) {
2012-07-14 17:02:21 +00:00
q ( " INSERT INTO %s%scalendars (`namespace`, `namespace_id`, `displayname`, `timezone`, `ctag`, `uri`, `has_vevent`, `has_vtodo`) VALUES (%d, %d, '%s', '%s', 1, '%s', 1, 0) " ,
2012-07-08 17:12:58 +00:00
CALDAV_SQL_DB , CALDAV_SQL_PREFIX , CALDAV_NAMESPACE_PRIVATE , IntVal ( $a -> user [ " uid " ]), dbesc ( $name ), dbesc ( $a -> timezone ), dbesc ( $uri )
);
2012-06-03 18:19:28 +00:00
}
}
2012-07-14 17:02:21 +00:00
2012-06-03 18:19:28 +00:00
}