2012-05-18 04:12:02 -04:00
< ? php
/**
* Name : public_server
2018-01-20 08:57:41 -05:00
* Description : Friendica addon with functions suitable for a public server .
2013-12-20 12:58:50 -05:00
* Version : 1.1
2012-05-18 04:12:02 -04:00
* Author : Keith Fernie < http :// friendika . me4 . it / profile / keith >
*/
2018-01-24 21:29:09 -05:00
2018-10-21 02:54:09 -04:00
use Friendica\App ;
2018-10-17 15:34:15 -04:00
use Friendica\BaseModule ;
2017-11-06 18:55:24 -05:00
use Friendica\Core\Config ;
2018-12-26 02:28:16 -05:00
use Friendica\Core\Hook ;
2018-01-22 14:03:11 -05:00
use Friendica\Core\L10n ;
2018-10-29 19:40:18 -04:00
use Friendica\Core\Logger ;
2018-10-31 10:55:15 -04:00
use Friendica\Core\Renderer ;
2018-07-20 08:20:48 -04:00
use Friendica\Database\DBA ;
2019-06-23 13:56:21 -04:00
use Friendica\Util\ConfigFileLoader ;
2018-01-26 21:39:02 -05:00
use Friendica\Util\DateTimeFormat ;
2018-11-08 11:45:19 -05:00
use Friendica\Util\Strings ;
2012-05-18 04:12:02 -04:00
2018-02-19 00:19:02 -05:00
function public_server_install ()
{
2018-12-26 02:28:16 -05:00
Hook :: register ( 'load_config' , 'addon/public_server/public_server.php' , 'public_server_load_config' );
Hook :: register ( 'register_account' , 'addon/public_server/public_server.php' , 'public_server_register_account' );
Hook :: register ( 'cron' , 'addon/public_server/public_server.php' , 'public_server_cron' );
Hook :: register ( 'enotify' , 'addon/public_server/public_server.php' , 'public_server_enotify' );
Hook :: register ( 'logged_in' , 'addon/public_server/public_server.php' , 'public_server_login' );
2012-05-18 04:12:02 -04:00
}
2018-02-19 00:19:02 -05:00
function public_server_uninstall ()
{
2018-12-26 02:28:16 -05:00
Hook :: unregister ( 'load_config' , 'addon/public_server/public_server.php' , 'public_server_load_config' );
Hook :: unregister ( 'register_account' , 'addon/public_server/public_server.php' , 'public_server_register_account' );
Hook :: unregister ( 'cron' , 'addon/public_server/public_server.php' , 'public_server_cron' );
Hook :: unregister ( 'enotify' , 'addon/public_server/public_server.php' , 'public_server_enotify' );
Hook :: unregister ( 'logged_in' , 'addon/public_server/public_server.php' , 'public_server_login' );
2012-05-18 04:12:02 -04:00
}
2019-03-24 07:54:26 -04:00
function public_server_load_config ( App $a , ConfigFileLoader $loader )
2018-06-27 23:18:46 -04:00
{
2019-02-10 14:10:59 -05:00
$a -> getConfigCache () -> load ( $loader -> loadAddonConfig ( 'public_server' ));
2018-06-27 23:18:46 -04:00
}
2018-02-19 00:19:02 -05:00
function public_server_register_account ( $a , $b )
{
2012-05-18 04:12:02 -04:00
$uid = $b ;
2018-02-18 17:20:38 -05:00
$days = Config :: get ( 'public_server' , 'expiredays' );
$days_posts = Config :: get ( 'public_server' , 'expireposts' );
if ( ! $days ) {
2012-05-18 04:12:02 -04:00
return ;
2018-02-18 17:20:38 -05:00
}
2012-05-18 04:12:02 -04:00
2018-02-18 17:20:38 -05:00
$fields = [ 'account_expires_on' => DateTimeFormat :: utc ( 'now +' . $days . ' days' ), 'expire' => $days_posts ];
2018-07-20 08:20:48 -04:00
DBA :: update ( 'user' , $fields , [ 'uid' => $uid ]);
2018-02-18 17:20:38 -05:00
}
2012-05-18 04:12:02 -04:00
2018-02-19 00:19:02 -05:00
function public_server_cron ( $a , $b )
{
2018-10-29 19:40:18 -04:00
Logger :: log ( " public_server: cron start " );
2013-07-19 17:34:54 -04:00
2018-02-19 00:19:02 -05:00
$r = q ( " SELECT * FROM `user` WHERE `account_expires_on` < UTC_TIMESTAMP() + INTERVAL 5 DAY AND
2018-02-18 17:20:38 -05:00
`account_expires_on` > '%s' AND
2018-02-19 00:19:02 -05:00
`expire_notification_sent` <= '%s' " ,
2018-10-21 02:54:09 -04:00
DBA :: NULL_DATETIME , DBA :: NULL_DATETIME );
2012-05-18 04:12:02 -04:00
2018-07-21 08:46:13 -04:00
if ( DBA :: isResult ( $r )) {
2018-02-18 17:20:38 -05:00
foreach ( $r as $rr ) {
2018-01-15 08:15:33 -05:00
notification ([
2012-05-18 04:12:02 -04:00
'uid' => $rr [ 'uid' ],
'type' => NOTIFY_SYSTEM ,
'system_type' => 'public_server_expire' ,
'language' => $rr [ 'language' ],
'to_name' => $rr [ 'username' ],
'to_email' => $rr [ 'email' ],
2018-01-22 14:03:11 -05:00
'source_name' => L10n :: t ( 'Administrator' ),
2018-10-09 14:13:22 -04:00
'source_link' => $a -> getBaseURL (),
'source_photo' => $a -> getBaseURL () . '/images/person-80.jpg' ,
2018-01-15 08:15:33 -05:00
]);
2012-05-18 04:12:02 -04:00
2018-02-18 17:20:38 -05:00
$fields = [ 'expire_notification_sent' => DateTimeFormat :: utcNow ()];
2018-07-20 08:20:48 -04:00
DBA :: update ( 'user' , $fields , [ 'uid' => $rr [ 'uid' ]]);
2012-05-18 04:12:02 -04:00
}
}
2018-02-18 17:20:38 -05:00
$nologin = Config :: get ( 'public_server' , 'nologin' , false );
if ( $nologin ) {
2018-02-19 00:19:02 -05:00
$r = q ( " SELECT `uid` FROM `user` WHERE NOT `account_expired` AND `login_date` <= '%s' AND `register_date` < UTC_TIMESTAMP() - INTERVAL %d DAY AND `account_expires_on` <= '%s' " ,
2018-10-21 02:54:09 -04:00
DBA :: NULL_DATETIME , intval ( $nologin ), DBA :: NULL_DATETIME );
2018-07-21 08:46:13 -04:00
if ( DBA :: isResult ( $r )) {
2018-02-18 17:20:38 -05:00
foreach ( $r as $rr ) {
$fields = [ 'account_expires_on' => DateTimeFormat :: utc ( 'now +6 days' )];
2018-07-20 08:20:48 -04:00
DBA :: update ( 'user' , $fields , [ 'uid' => $rr [ 'uid' ]]);
2018-02-18 17:20:38 -05:00
}
2017-11-19 17:09:54 -05:00
}
2012-05-18 04:12:02 -04:00
}
2018-02-18 17:20:38 -05:00
$flagusers = Config :: get ( 'public_server' , 'flagusers' , false );
if ( $flagusers ) {
$r = q ( " SELECT `uid` FROM `user` WHERE NOT `account_expired` AND `login_date` < UTC_TIMESTAMP() - INTERVAL %d DAY AND `account_expires_on` <= '%s' AND `page-flags` = 0 " ,
2018-10-21 02:54:09 -04:00
intval ( $flagusers ), DBA :: NULL_DATETIME );
2018-07-21 08:46:13 -04:00
if ( DBA :: isResult ( $r )) {
2018-02-18 17:20:38 -05:00
foreach ( $r as $rr ) {
$fields = [ 'account_expires_on' => DateTimeFormat :: utc ( 'now +6 days' )];
2018-07-20 08:20:48 -04:00
DBA :: update ( 'user' , $fields , [ 'uid' => $rr [ 'uid' ]]);
2018-02-18 17:20:38 -05:00
}
2012-05-18 04:12:02 -04:00
}
2018-02-18 17:20:38 -05:00
}
2012-05-18 04:12:02 -04:00
2018-02-18 17:20:38 -05:00
$flagposts = Config :: get ( 'public_server' , 'flagposts' );
$flagpostsexpire = Config :: get ( 'public_server' , 'flagpostsexpire' );
2012-05-18 04:12:02 -04:00
if ( $flagposts && $flagpostsexpire ) {
2018-02-18 17:20:38 -05:00
$r = q ( " SELECT `uid` FROM `user` WHERE NOT `account_expired` AND `login_date` < UTC_TIMESTAMP() - INTERVAL %d DAY AND `account_expires_on` <= '%s' and `expire` = 0 AND `page-flags` = 0 " ,
2018-10-21 02:54:09 -04:00
intval ( $flagposts ), DBA :: NULL_DATETIME );
2018-07-21 08:46:13 -04:00
if ( DBA :: isResult ( $r )) {
2018-02-18 17:20:38 -05:00
foreach ( $r as $rr ) {
2018-07-20 08:20:48 -04:00
DBA :: update ( 'user' , [ 'expire' => $flagpostsexpire ], [ 'uid' => $rr [ 'uid' ]]);
2018-02-18 17:20:38 -05:00
}
2012-05-18 04:12:02 -04:00
}
2018-02-18 17:20:38 -05:00
}
2012-05-18 04:12:02 -04:00
2018-10-29 19:40:18 -04:00
Logger :: log ( " public_server: cron end " );
2012-05-18 04:12:02 -04:00
}
2018-02-19 00:19:02 -05:00
function public_server_enotify ( & $a , & $b )
{
2018-11-30 09:11:56 -05:00
if ( ! empty ( $b [ 'params' ]) && $b [ 'params' ][ 'type' ] == NOTIFY_SYSTEM
&& ! empty ( $b [ 'params' ][ 'system_type' ]) && $b [ 'params' ][ 'system_type' ] === 'public_server_expire' ) {
2018-10-09 14:13:22 -04:00
$b [ 'itemlink' ] = $a -> getBaseURL ();
2018-02-18 17:20:38 -05:00
$b [ 'epreamble' ] = $b [ 'preamble' ] = L10n :: t ( 'Your account on %s will expire in a few days.' , Config :: get ( 'system' , 'sitename' ));
$b [ 'subject' ] = L10n :: t ( 'Your Friendica account is about to expire.' );
2018-07-10 08:38:36 -04:00
$b [ 'body' ] = L10n :: t ( " Hi %1 \$ s, \n \n Your account on %2 \$ s will expire in less than five days. You may keep your account by logging in at least once every 30 days " , $b [ 'params' ][ 'to_name' ], " [url= " . Config :: get ( 'system' , 'url' ) . " ] " . Config :: get ( 'config' , 'sitename' ) . " [/url] " );
2018-02-18 17:20:38 -05:00
}
2012-05-18 04:12:02 -04:00
}
2018-02-19 00:19:02 -05:00
function public_server_login ( $a , $b )
{
2018-02-18 17:20:38 -05:00
$days = Config :: get ( 'public_server' , 'expiredays' );
if ( ! $days ) {
2012-05-18 04:12:02 -04:00
return ;
2018-02-18 17:20:38 -05:00
}
$fields = [ 'account_expires_on' => DateTimeFormat :: utc ( 'now +' . $days . ' days' )];
2018-10-21 02:54:09 -04:00
$condition = [ " `uid` = ? AND `account_expires_on` > ? " , local_user (), DBA :: NULL_DATETIME ];
2018-07-20 08:20:48 -04:00
DBA :: update ( 'user' , $fields , $condition );
2012-05-18 04:12:02 -04:00
}
2013-12-20 12:58:50 -05:00
2018-02-19 00:19:02 -05:00
function public_server_addon_admin_post ( & $a )
{
2018-10-17 15:34:15 -04:00
BaseModule :: checkFormSecurityTokenRedirectOnError ( '/admin/addons/publicserver' , 'publicserver' );
2018-11-30 09:11:56 -05:00
$expiredays = ( ! empty ( $_POST [ 'expiredays' ]) ? Strings :: escapeTags ( trim ( $_POST [ 'expiredays' ])) : '' );
$expireposts = ( ! empty ( $_POST [ 'expireposts' ]) ? Strings :: escapeTags ( trim ( $_POST [ 'expireposts' ])) : '' );
$nologin = ( ! empty ( $_POST [ 'nologin' ]) ? Strings :: escapeTags ( trim ( $_POST [ 'nologin' ])) : '' );
$flagusers = ( ! empty ( $_POST [ 'flagusers' ]) ? Strings :: escapeTags ( trim ( $_POST [ 'flagusers' ])) : '' );
$flagposts = ( ! empty ( $_POST [ 'flagposts' ]) ? Strings :: escapeTags ( trim ( $_POST [ 'flagposts' ])) : '' );
$flagpostsexpire = ( ! empty ( $_POST [ 'flagpostsexpire' ]) ? Strings :: escapeTags ( trim ( $_POST [ 'flagpostsexpire' ])) : '' );
2018-02-18 17:20:38 -05:00
Config :: set ( 'public_server' , 'expiredays' , $expiredays );
Config :: set ( 'public_server' , 'expireposts' , $expireposts );
Config :: set ( 'public_server' , 'nologin' , $nologin );
Config :: set ( 'public_server' , 'flagusers' , $flagusers );
Config :: set ( 'public_server' , 'flagposts' , $flagposts );
Config :: set ( 'public_server' , 'flagpostsexpire' , $flagpostsexpire );
info ( L10n :: t ( 'Settings saved' ) . EOL );
2013-12-20 12:58:50 -05:00
}
2018-02-18 17:20:38 -05:00
2018-02-19 00:19:02 -05:00
function public_server_addon_admin ( & $a , & $o )
{
2018-10-17 15:34:15 -04:00
$token = BaseModule :: getFormSecurityToken ( " publicserver " );
2018-10-31 10:55:15 -04:00
$t = Renderer :: getMarkupTemplate ( " admin.tpl " , " addon/public_server " );
$o = Renderer :: replaceMacros ( $t , [
2018-02-18 17:20:38 -05:00
'$submit' => L10n :: t ( 'Save Settings' ),
'$form_security_token' => $token ,
'$infotext' => L10n :: t ( 'Set any of these options to 0 to deactivate it.' ),
'$expiredays' => [ " expiredays " , " Expire Days " , intval ( Config :: get ( 'public_server' , 'expiredays' )), " When an account is created on the site, it is given a hard " ],
'$expireposts' => [ " expireposts " , " Expire Posts " , intval ( Config :: get ( 'public_server' , 'expireposts' )), " Set the default days for posts to expire here " ],
'$nologin' => [ " nologin " , " No Login " , intval ( Config :: get ( 'public_server' , 'nologin' )), " Remove users who have never logged in after nologin days " ],
'$flagusers' => [ " flagusers " , " Flag users " , intval ( Config :: get ( 'public_server' , 'flagusers' )), " Remove users who last logged in over flagusers days ago " ],
'$flagposts' => [ " flagposts " , " Flag posts " , intval ( Config :: get ( 'public_server' , 'flagposts' )), " For users who last logged in over flagposts days ago set post expiry days to flagpostsexpire " ],
'$flagpostsexpire' => [ " flagpostsexpire " , " Flag posts expire " , intval ( Config :: get ( 'public_server' , 'flagpostsexpire' ))],
]);
2013-12-20 12:58:50 -05:00
}