2012-06-03 18:19:28 +00:00
< ? php
2018-01-01 02:04:02 +00:00
2018-01-17 00:51:12 +00:00
use Friendica\Core\Addon ;
2018-01-22 19:03:11 +00:00
use Friendica\Core\L10n ;
2018-01-01 02:04:02 +00:00
use Friendica\Module\Login ;
2017-11-22 23:10:01 +00:00
use Friendica\Util\Emailer ;
2012-06-03 18:19:28 +00:00
require_once ( 'include/security.php' );
function dav_install ()
{
2018-01-17 00:51:12 +00:00
Addon :: registerHook ( 'event_created' , 'addon/dav/dav.php' , 'dav_event_created_hook' );
Addon :: registerHook ( 'event_updated' , 'addon/dav/dav.php' , 'dav_event_updated_hook' );
Addon :: registerHook ( 'profile_tabs' , 'addon/dav/dav.php' , 'dav_profile_tabs_hook' );
Addon :: registerHook ( 'cron' , 'addon/dav/dav.php' , 'dav_cron' );
2012-06-03 18:19:28 +00:00
}
function dav_uninstall ()
{
2018-01-17 00:51:12 +00:00
Addon :: unregisterHook ( 'event_created' , 'addon/dav/dav.php' , 'dav_event_created_hook' );
Addon :: unregisterHook ( 'event_updated' , 'addon/dav/dav.php' , 'dav_event_updated_hook' );
Addon :: unregisterHook ( 'profile_tabs' , 'addon/dav/dav.php' , 'dav_profile_tabs_hook' );
Addon :: unregisterHook ( 'cron' , 'addon/dav/dav.php' , 'dav_cron' );
2012-06-03 18:19:28 +00:00
}
function dav_module ()
{
}
2012-06-10 16:29:31 +00:00
function dav_include_files ()
{
2012-08-04 08:46:11 +00:00
require_once ( __DIR__ . " /../SabreDAV/lib/Sabre/autoload.php " );
2012-06-10 16:29:31 +00:00
2012-08-11 08:07:19 +00:00
require_once ( __DIR__ . " /../sabre-vobject/lib/Sabre/VObject/Node.php " );
require_once ( __DIR__ . " /../sabre-vobject/lib/Sabre/VObject/Element.php " );
require_once ( __DIR__ . " /../sabre-vobject/lib/Sabre/VObject/Component.php " );
require_once ( __DIR__ . " /../sabre-vobject/lib/Sabre/VObject/DateTimeParser.php " );
require_once ( __DIR__ . " /../sabre-vobject/lib/Sabre/VObject/ElementList.php " );
require_once ( __DIR__ . " /../sabre-vobject/lib/Sabre/VObject/FreeBusyGenerator.php " );
require_once ( __DIR__ . " /../sabre-vobject/lib/Sabre/VObject/Parameter.php " );
require_once ( __DIR__ . " /../sabre-vobject/lib/Sabre/VObject/ParseException.php " );
require_once ( __DIR__ . " /../sabre-vobject/lib/Sabre/VObject/Property.php " );
require_once ( __DIR__ . " /../sabre-vobject/lib/Sabre/VObject/Reader.php " );
require_once ( __DIR__ . " /../sabre-vobject/lib/Sabre/VObject/RecurrenceIterator.php " );
require_once ( __DIR__ . " /../sabre-vobject/lib/Sabre/VObject/TimeZoneUtil.php " );
require_once ( __DIR__ . " /../sabre-vobject/lib/Sabre/VObject/Version.php " );
require_once ( __DIR__ . " /../sabre-vobject/lib/Sabre/VObject/Property/DateTime.php " );
require_once ( __DIR__ . " /../sabre-vobject/lib/Sabre/VObject/Property/MultiDateTime.php " );
require_once ( __DIR__ . " /../sabre-vobject/lib/Sabre/VObject/Component/VAlarm.php " );
require_once ( __DIR__ . " /../sabre-vobject/lib/Sabre/VObject/Component/VCalendar.php " );
require_once ( __DIR__ . " /../sabre-vobject/lib/Sabre/VObject/Component/VEvent.php " );
require_once ( __DIR__ . " /../sabre-vobject/lib/Sabre/VObject/Component/VJournal.php " );
require_once ( __DIR__ . " /../sabre-vobject/lib/Sabre/VObject/Component/VTodo.php " );
2012-08-04 08:46:11 +00:00
require_once ( __DIR__ . " /../common/calendar.fnk.php " );
require_once ( __DIR__ . " /../common/calendar_rendering.fnk.php " );
2012-08-02 20:26:52 +00:00
2012-08-04 08:46:11 +00:00
require_once ( __DIR__ . " /../common/dav_caldav_backend_common.inc.php " );
require_once ( __DIR__ . " /../common/dav_caldav_backend_private.inc.php " );
require_once ( __DIR__ . " /../common/dav_caldav_backend_virtual.inc.php " );
require_once ( __DIR__ . " /../common/dav_caldav_root.inc.php " );
require_once ( __DIR__ . " /../common/dav_user_calendars.inc.php " );
require_once ( __DIR__ . " /../common/dav_caldav_calendar_virtual.inc.php " );
require_once ( __DIR__ . " /../common/dav_caldav_calendar_private.inc.php " );
2012-08-02 20:26:52 +00:00
2012-08-04 08:46:11 +00:00
require_once ( __DIR__ . " /../common/dav_carddav_root.inc.php " );
require_once ( __DIR__ . " /../common/dav_carddav_backend_common.inc.php " );
require_once ( __DIR__ . " /../common/dav_carddav_backend_virtual.inc.php " );
require_once ( __DIR__ . " /../common/dav_carddav_backend_private.inc.php " );
require_once ( __DIR__ . " /../common/dav_user_addressbooks.inc.php " );
2012-08-02 20:26:52 +00:00
2012-08-04 08:46:11 +00:00
require_once ( __DIR__ . " /../common/wdcal_configuration.php " );
require_once ( __DIR__ . " /../common/wdcal_backend.inc.php " );
2012-06-10 16:29:31 +00:00
require_once ( __DIR__ . " /dav_friendica_principal.inc.php " );
require_once ( __DIR__ . " /dav_friendica_auth.inc.php " );
2012-07-08 17:12:58 +00:00
require_once ( __DIR__ . " /dav_carddav_backend_virtual_friendica.inc.php " );
require_once ( __DIR__ . " /dav_caldav_backend_virtual_friendica.inc.php " );
2012-06-10 16:29:31 +00:00
require_once ( __DIR__ . " /FriendicaACLPlugin.inc.php " );
2012-08-04 08:46:11 +00:00
require_once ( __DIR__ . " /../common/wdcal_edit.inc.php " );
2012-06-10 16:29:31 +00:00
require_once ( __DIR__ . " /calendar.friendica.fnk.php " );
require_once ( __DIR__ . " /layout.fnk.php " );
2012-06-06 07:05:23 +00:00
}
2012-06-03 18:19:28 +00:00
/**
* @ param App $a
*/
function dav_init ( & $a )
{
/*
* Recommended settings :
* ALTER TABLE `photo` ADD INDEX ( `contact-id` )
*/
2012-08-02 20:26:52 +00:00
ini_set ( " display_errors " , 1 );
error_reporting ( E_ALL );
2012-06-06 07:05:23 +00:00
dav_include_files ();
2012-06-10 16:29:31 +00:00
2012-07-28 10:58:12 +00:00
if ( false ) {
2012-06-03 18:19:28 +00:00
dbg ( true );
error_reporting ( E_ALL );
ini_set ( " display_errors " , 1 );
}
wdcal_create_std_calendars ();
2012-08-02 20:26:52 +00:00
wdcal_create_std_addressbooks ();
2012-07-12 19:22:11 +00:00
wdcal_addRequiredHeaders ();
2012-06-03 18:19:28 +00:00
if ( $a -> argc >= 2 && $a -> argv [ 1 ] == " wdcal " ) {
if ( $a -> argc >= 3 && $a -> argv [ 2 ] == " feed " ) {
2012-06-04 17:18:20 +00:00
wdcal_print_feed ( $a -> get_baseurl () . " /dav/wdcal/ " );
2012-06-03 18:19:28 +00:00
killme ();
}
return ;
}
2012-07-08 17:12:58 +00:00
if ( $a -> argc >= 2 && $a -> argv [ 1 ] == " getExceptionDates " ) {
echo wdcal_getEditPage_exception_selector ();
killme ();
}
2012-06-03 18:19:28 +00:00
if ( $a -> argc >= 2 && $a -> argv [ 1 ] == " settings " ) {
return ;
}
2012-07-08 17:12:58 +00:00
if ( isset ( $_REQUEST [ " test " ])) {
renderAllCalDavEntries ();
}
2012-06-03 18:19:28 +00:00
2012-08-11 08:07:19 +00:00
$server = dav_create_server ();
2012-07-30 19:07:20 +00:00
2012-06-03 18:19:28 +00:00
$browser = new Sabre_DAV_Browser_Plugin ();
$server -> addPlugin ( $browser );
2012-07-30 19:07:20 +00:00
2012-06-03 18:19:28 +00:00
$server -> exec ();
killme ();
}
/**
* @ return string
*/
function dav_content ()
{
$a = get_app ();
if ( ! isset ( $a -> user [ " uid " ]) || $a -> user [ " uid " ] == 0 ) {
2018-01-01 02:04:02 +00:00
return Login :: form ();
2012-06-03 18:19:28 +00:00
}
$x = " " ;
2012-07-28 09:10:48 +00:00
try {
if ( $a -> argv [ 1 ] == " settings " ) {
return wdcal_getSettingsPage ( $a );
} elseif ( $a -> argv [ 1 ] == " wdcal " ) {
if ( isset ( $a -> argv [ 2 ]) && strlen ( $a -> argv [ 2 ]) > 0 ) {
if ( $a -> argv [ 2 ] == " new " ) {
$o = " " ;
if ( isset ( $_REQUEST [ " save " ])) {
2015-08-17 06:03:24 +00:00
check_form_security_token_redirectOnErr ( " /dav/wdcal/ " , " caledit " );
2012-07-28 09:10:48 +00:00
$ret = wdcal_postEditPage ( " new " , " " , $a -> user [ " uid " ], $a -> timezone , $a -> get_baseurl () . " /dav/wdcal/ " );
if ( $ret [ " ok " ]) notice ( $ret [ " msg " ]);
else info ( $ret [ " msg " ]);
2018-01-01 02:04:02 +00:00
goaway ( 'dav/wdcal/' );
2012-07-28 09:10:48 +00:00
}
$o .= wdcal_getNewPage ();
return $o ;
} else {
$calendar_id = IntVal ( $a -> argv [ 2 ]);
if ( isset ( $a -> argv [ 3 ]) && $a -> argv [ 3 ] == " ics-export " ) {
wdcal_print_user_ics ( $calendar_id );
} elseif ( isset ( $a -> argv [ 3 ]) && $a -> argv [ 3 ] == " ics-import " ) {
return wdcal_import_user_ics ( $calendar_id );
} elseif ( isset ( $a -> argv [ 3 ]) && $a -> argv [ 3 ] > 0 ) {
if ( isset ( $a -> argv [ 4 ]) && $a -> argv [ 4 ] == " edit " ) {
$o = " " ;
if ( isset ( $_REQUEST [ " save " ])) {
2015-08-17 06:03:24 +00:00
check_form_security_token_redirectOnErr ( " /dav/wdcal/ " , " caledit " );
2012-07-28 09:10:48 +00:00
$ret = wdcal_postEditPage ( $a -> argv [ 3 ], $a -> user [ " uid " ], $a -> timezone , $a -> get_baseurl () . " /dav/wdcal/ " );
if ( $ret [ " ok " ]) notice ( $ret [ " msg " ]);
else info ( $ret [ " msg " ]);
2018-01-01 02:04:02 +00:00
goaway ( 'dav/wdcal/' );
2012-07-28 09:10:48 +00:00
}
$o .= wdcal_getEditPage ( $calendar_id , $a -> argv [ 3 ]);
return $o ;
} else {
return wdcal_getDetailPage ( $calendar_id , $a -> argv [ 3 ]);
2012-07-08 17:12:58 +00:00
}
} else {
2012-07-28 09:10:48 +00:00
// @TODO Edit Calendar
2012-06-10 16:29:31 +00:00
}
}
2012-07-28 09:10:48 +00:00
} else {
$server = dav_create_server ( true , true , false );
$cals = dav_get_current_user_calendars ( $server , DAV_ACL_READ );
2018-01-15 13:15:33 +00:00
$x = wdcal_printCalendar ( $cals , [], $a -> get_baseurl () . " /dav/wdcal/feed/ " , " week " , 0 , 200 );
2012-06-03 18:19:28 +00:00
}
}
2012-07-28 09:10:48 +00:00
} catch ( DAVVersionMismatchException $e ) {
2018-01-22 19:03:11 +00:00
$x = L10n :: t ( " The current version of this addon has not been set up correctly. Please contact the system administrator of your installation of friendica to fix this. " );
2012-06-03 18:19:28 +00:00
}
return $x ;
}
/**
* @ param App $a
* @ param object $b
*/
function dav_event_created_hook ( & $a , & $b )
{
2012-06-06 07:05:23 +00:00
dav_include_files ();
2012-06-03 18:19:28 +00:00
// @TODO Updating the cache instead of completely invalidating and rebuilding it
2012-07-08 17:12:58 +00:00
Sabre_CalDAV_Backend_Friendica :: invalidateCache ( $a -> user [ " uid " ], CALDAV_FRIENDICA_CONTACTS );
Sabre_CalDAV_Backend_Friendica :: invalidateCache ( $a -> user [ " uid " ], CALDAV_FRIENDICA_MINE );
2012-06-03 18:19:28 +00:00
}
/**
* @ param App $a
* @ param object $b
*/
function dav_event_updated_hook ( & $a , & $b )
{
2012-06-06 07:05:23 +00:00
dav_include_files ();
2012-06-03 18:19:28 +00:00
// @TODO Updating the cache instead of completely invalidating and rebuilding it
2012-07-08 17:12:58 +00:00
Sabre_CalDAV_Backend_Friendica :: invalidateCache ( $a -> user [ " uid " ], CALDAV_FRIENDICA_CONTACTS );
Sabre_CalDAV_Backend_Friendica :: invalidateCache ( $a -> user [ " uid " ], CALDAV_FRIENDICA_MINE );
2012-06-03 18:19:28 +00:00
}
/**
* @ param App $a
* @ param object $b
*/
function dav_profile_tabs_hook ( & $a , & $b )
{
2018-01-15 13:15:33 +00:00
$b [ " tabs " ][] = [
2018-01-22 19:03:11 +00:00
" label " => L10n :: t ( 'Calendar' ),
2012-06-10 16:29:31 +00:00
" url " => $a -> get_baseurl () . " /dav/wdcal/ " ,
" sel " => " " ,
2018-01-22 19:03:11 +00:00
" title " => L10n :: t ( 'Extended calendar with CalDAV-support' ),
2018-01-15 13:15:33 +00:00
];
2012-06-03 18:19:28 +00:00
}
2012-07-27 18:35:14 +00:00
/**
* @ param App $a
* @ param object $b
*/
function dav_cron ( & $a , & $b )
{
dav_include_files ();
$r = q ( " SELECT * FROM %s%snotifications WHERE `notified` = 0 AND `alert_date` <= NOW() " , CALDAV_SQL_DB , CALDAV_SQL_PREFIX );
2015-01-20 00:05:38 +00:00
if ( is_array ( $r )) {
2012-07-27 18:35:14 +00:00
foreach ( $r as $not ) {
q ( " UPDATE %s%snotifications SET `notified` = 1 WHERE `id` = %d " , CALDAV_SQL_DB , CALDAV_SQL_PREFIX , $not [ " id " ]);
2012-07-28 09:10:48 +00:00
$event = q ( " SELECT * FROM %s%sjqcalendar WHERE `calendarobject_id` = %d " , CALDAV_SQL_DB , CALDAV_SQL_PREFIX , $not [ " calendarobject_id " ]);
$calendar = q ( " SELECT * FROM %s%scalendars WHERE `id` = %d " , CALDAV_SQL_DB , CALDAV_SQL_PREFIX , $not [ " calendar_id " ]);
2018-01-15 13:15:33 +00:00
$users = [];
2012-07-27 18:35:14 +00:00
if ( count ( $calendar ) != 1 || count ( $event ) == 0 ) continue ;
switch ( $calendar [ 0 ][ " namespace " ]) {
case CALDAV_NAMESPACE_PRIVATE :
$user = q ( " SELECT * FROM user WHERE `uid` = %d AND `blocked` = 0 " , $calendar [ 0 ][ " namespace_id " ]);
if ( count ( $user ) != 1 ) continue ;
$users [] = $user [ 0 ];
break ;
}
switch ( $not [ " action " ]) {
2012-07-28 09:10:48 +00:00
case " email " :
case " display " : // @TODO implement "Display"
2012-07-27 18:35:14 +00:00
foreach ( $users as $user ) {
2018-01-15 13:15:33 +00:00
$find = [ " %to% " , " %event% " , " %url% " ];
$repl = [ $user [ " username " ], $event [ 0 ][ " Summary " ], $a -> get_baseurl () . " /dav/wdcal/ " . $calendar [ 0 ][ " id " ] . " / " . $not [ " calendarobject_id " ] . " / " ];
2012-07-27 18:35:14 +00:00
$text_text = str_replace ( $find , $repl , " Hi %to%! \n \n The event \" %event% \" is about to begin: \n %url% " );
$text_html = str_replace ( $find , $repl , " Hi %to%!<br> \n <br> \n The event \" %event% \" is about to begin:<br> \n <a href=' " . " %url% " . " '>%url%</a> " );
2018-01-15 13:15:33 +00:00
$params = [
2012-07-28 09:10:48 +00:00
'fromName' => FRIENDICA_PLATFORM ,
2018-01-22 19:03:11 +00:00
'fromEmail' => L10n :: t ( 'noreply' ) . '@' . $a -> get_hostname (),
'replyTo' => L10n :: t ( 'noreply' ) . '@' . $a -> get_hostname (),
2012-07-28 09:10:48 +00:00
'toEmail' => $user [ " email " ],
2018-01-22 19:03:11 +00:00
'messageSubject' => L10n :: t ( " Notification: " . $event [ 0 ][ " Summary " ]),
2012-07-28 09:10:48 +00:00
'htmlVersion' => $text_html ,
'textVersion' => $text_text ,
2012-07-27 18:35:14 +00:00
'additionalMailHeader' => " " ,
2018-01-15 13:15:33 +00:00
];
2015-03-23 11:20:21 +00:00
Emailer :: send ( $params );
2012-07-27 18:35:14 +00:00
}
break ;
}
}
2015-01-20 00:05:38 +00:00
}
2012-07-27 18:35:14 +00:00
}
2012-06-03 18:19:28 +00:00
/**
* @ param App $a
* @ param null | object $o
*/
2018-01-20 13:57:41 +00:00
function dav_addon_admin_post ( & $a = null , & $o = null )
2012-06-03 18:19:28 +00:00
{
2018-01-20 13:57:41 +00:00
check_form_security_token_redirectOnErr ( '/admin/addons/dav' , 'dav_admin_save' );
2012-06-03 18:19:28 +00:00
2012-07-08 17:12:58 +00:00
dav_include_files ();
2012-06-03 18:19:28 +00:00
require_once ( __DIR__ . " /database-init.inc.php " );
if ( isset ( $_REQUEST [ " install " ])) {
$errs = dav_create_tables ();
2018-01-22 19:03:11 +00:00
if ( count ( $errs ) == 0 ) info ( L10n :: t ( 'The database tables have been installed.' ) . EOL );
else notice ( L10n :: t ( " An error occurred during the installation. " ) . EOL );
2012-06-03 18:19:28 +00:00
}
2012-07-08 17:12:58 +00:00
if ( isset ( $_REQUEST [ " upgrade " ])) {
$errs = dav_upgrade_tables ();
2012-07-28 09:10:48 +00:00
if ( count ( $errs ) == 0 ) {
renderAllCalDavEntries ();
2018-01-22 19:03:11 +00:00
info ( L10n :: t ( 'The database tables have been updated.' ) . EOL );
} else notice ( L10n :: t ( " An error occurred during the update. " ) . EOL );
2012-07-08 17:12:58 +00:00
}
2012-06-03 18:19:28 +00:00
}
/**
* @ param App $a
2012-07-08 17:12:58 +00:00
* @ param string $o
2012-06-03 18:19:28 +00:00
*/
2018-01-20 13:57:41 +00:00
function dav_addon_admin ( & $a , & $o )
2012-06-03 18:19:28 +00:00
{
2012-07-08 17:12:58 +00:00
dav_include_files ();
2012-06-03 18:19:28 +00:00
require_once ( __DIR__ . " /database-init.inc.php " );
$dbstatus = dav_check_tables ();
$o = '<input type="hidden" name="form_security_token" value="' . get_form_security_token ( " dav_admin_save " ) . '">' ;
2018-01-22 19:03:11 +00:00
$o .= '<i>' . L10n :: t ( " No system-wide settings yet. " ) . '</i><br><br>' ;
2012-06-03 18:19:28 +00:00
2018-01-22 19:03:11 +00:00
$o .= '<h3>' . L10n :: t ( 'Database status' ) . '</h3>' ;
2012-06-03 18:19:28 +00:00
switch ( $dbstatus ) {
case 0 :
2018-01-22 19:03:11 +00:00
$o .= L10n :: t ( 'Installed' );
2012-06-03 18:19:28 +00:00
break ;
case 1 :
2012-08-12 09:31:34 +00:00
case 2 :
2018-01-22 19:03:11 +00:00
$o .= " <strong> " . L10n :: t ( 'Upgrade needed' ) . " </strong><br> " . L10n :: t ( " Please back up all calendar data (the tables beginning with dav_*) before proceeding. While all calendar events <i>should</i> be converted to the new database structure, it's always safe to have a backup. Below, you can have a look at the database-queries that will be made when pressing the 'update'-button. " ) . " <br><br><input type='submit' name='upgrade' value=' " . L10n :: t ( 'Upgrade' ) . " '> " ;
2012-06-03 18:19:28 +00:00
break ;
case - 1 :
2018-01-22 19:03:11 +00:00
$o .= L10n :: t ( 'Not installed' ) . " <br><br><input type='submit' name='install' value=' " . L10n :: t ( 'Install' ) . " '> " ;
2012-06-03 18:19:28 +00:00
break ;
2012-07-28 09:10:48 +00:00
case - 2 :
default :
2018-01-22 19:03:11 +00:00
$o .= L10n :: t ( 'Unknown' ) . " <br><br> " . L10n :: t ( " Something really went wrong. I cannot recover from this state automatically, sorry. Please go to the database backend, back up the data, and delete all tables beginning with 'dav_' manually. Afterwards, this installation routine should be able to reinitialize the tables automatically. " );
2012-07-28 09:10:48 +00:00
break ;
2012-06-03 18:19:28 +00:00
}
$o .= " <br><br> " ;
2018-01-22 19:03:11 +00:00
$o .= " <h3> " . L10n :: t ( " Troubleshooting " ) . " </h3> " ;
$o .= " <h4> " . L10n :: t ( " Manual creation of the database tables: " ) . " </h4> " ;
$o .= " <a href='#' onClick=' \$ ( \" #sqlstatements \" ).show(); return false;'> " . L10n :: t ( " Show SQL-statements " ) . " </a><blockquote style='display: none;' id='sqlstatements'><pre> " ;
2012-07-28 09:10:48 +00:00
switch ( $dbstatus ) {
2012-08-12 09:31:34 +00:00
case 1 : case 2 :
$tables = dav_get_update_statements ( $dbstatus );
2012-07-28 09:10:48 +00:00
foreach ( $tables as $t ) $o .= escape_tags ( $t . " ; \n \n " );
break ;
default :
$tables = dav_get_create_statements ();
foreach ( $tables as $t ) $o .= escape_tags ( $t . " ; \n \n " );
break ;
}
2012-06-03 18:19:28 +00:00
$o .= " </pre></blockquote> " ;
2012-06-04 17:18:20 +00:00
}