2012-05-18 10:12:02 +02:00
< ? php
/**
* Name : public_server
2020-02-23 06:13:30 +01:00
* Description : Friendica addon with functions suitable for a public server . WARNING : This addon is currently not well maintained . It may produce unexpected results . Use with caution !
2013-12-20 18:58:50 +01:00
* Version : 1.1
2012-05-18 10:12:02 +02:00
* Author : Keith Fernie < http :// friendika . me4 . it / profile / keith >
*/
2018-01-25 03:29:09 +01:00
2018-10-21 08:54:09 +02:00
use Friendica\App ;
2018-10-17 21:34:15 +02:00
use Friendica\BaseModule ;
2018-12-26 08:28:16 +01:00
use Friendica\Core\Hook ;
2018-10-30 00:40:18 +01:00
use Friendica\Core\Logger ;
2018-10-31 15:55:15 +01:00
use Friendica\Core\Renderer ;
2018-07-20 14:20:48 +02:00
use Friendica\Database\DBA ;
2019-12-30 03:55:10 +01:00
use Friendica\DI ;
2021-01-23 21:36:36 +01:00
use Friendica\Model\Notification ;
2021-10-07 07:53:16 +02:00
use Friendica\Model\User ;
2019-06-23 19:56:21 +02:00
use Friendica\Util\ConfigFileLoader ;
2018-01-27 03:39:02 +01:00
use Friendica\Util\DateTimeFormat ;
2018-11-08 17:45:19 +01:00
use Friendica\Util\Strings ;
2012-05-18 10:12:02 +02:00
2018-02-19 06:19:02 +01:00
function public_server_install ()
{
2018-12-26 08:28:16 +01: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 10:12:02 +02:00
}
2019-03-24 12:54:26 +01:00
function public_server_load_config ( App $a , ConfigFileLoader $loader )
2018-06-28 05:18:46 +02:00
{
2019-02-10 20:10:59 +01:00
$a -> getConfigCache () -> load ( $loader -> loadAddonConfig ( 'public_server' ));
2018-06-28 05:18:46 +02:00
}
2018-02-19 06:19:02 +01:00
function public_server_register_account ( $a , $b )
{
2012-05-18 10:12:02 +02:00
$uid = $b ;
2020-01-19 21:21:12 +01:00
$days = DI :: config () -> get ( 'public_server' , 'expiredays' );
$days_posts = DI :: config () -> get ( 'public_server' , 'expireposts' );
2018-02-18 23:20:38 +01:00
if ( ! $days ) {
2012-05-18 10:12:02 +02:00
return ;
2018-02-18 23:20:38 +01:00
}
2012-05-18 10:12:02 +02:00
2018-02-18 23:20:38 +01:00
$fields = [ 'account_expires_on' => DateTimeFormat :: utc ( 'now +' . $days . ' days' ), 'expire' => $days_posts ];
2018-07-20 14:20:48 +02:00
DBA :: update ( 'user' , $fields , [ 'uid' => $uid ]);
2018-02-18 23:20:38 +01:00
}
2012-05-18 10:12:02 +02:00
2018-02-19 06:19:02 +01:00
function public_server_cron ( $a , $b )
{
2021-10-21 08:04:27 +02:00
Logger :: notice ( " public_server: cron start " );
2013-07-19 23:34:54 +02:00
2021-10-07 07:53:16 +02:00
$users = DBA :: selectToArray ( 'user' , [], [ " `account_expires_on` < UTC_TIMESTAMP() + INTERVAL ? DAY AND
`account_expires_on` > ? AND `expire_notification_sent` <= ? " , 5, DBA::NULL_DATETIME, DBA::NULL_DATETIME]);
foreach ( $users as $rr ) {
2021-10-19 21:53:10 +02:00
DI :: notify () -> createFromArray ([
2021-10-07 07:53:16 +02:00
'type' => Notification\Type :: SYSTEM ,
'uid' => $rr [ 'uid' ],
'system_type' => 'public_server_expire' ,
'source_name' => DI :: l10n () -> t ( 'Administrator' ),
'source_link' => DI :: baseUrl () -> get (),
'source_photo' => DI :: baseUrl () -> get () . '/images/person-80.jpg' ,
]);
2012-05-18 10:12:02 +02:00
2021-10-07 07:53:16 +02:00
$fields = [ 'expire_notification_sent' => DateTimeFormat :: utcNow ()];
DBA :: update ( 'user' , $fields , [ 'uid' => $rr [ 'uid' ]]);
2012-05-18 10:12:02 +02:00
}
2020-01-19 21:21:12 +01:00
$nologin = DI :: config () -> get ( 'public_server' , 'nologin' , false );
2018-02-18 23:20:38 +01:00
if ( $nologin ) {
2021-10-07 07:53:16 +02:00
$users = DBA :: selectToArray ( 'user' , [], [ " NOT `account_expired` AND `login_date` <= ? AND `register_date` < UTC_TIMESTAMP() - INTERVAL ? DAY AND `account_expires_on` <= ? " ,
DBA :: NULL_DATETIME , $nologin , DBA :: NULL_DATETIME ]);
foreach ( $users as $rr ) {
$fields = [ 'account_expires_on' => DateTimeFormat :: utc ( 'now +6 days' )];
DBA :: update ( 'user' , $fields , [ 'uid' => $rr [ 'uid' ]]);
2017-11-19 23:09:54 +01:00
}
2012-05-18 10:12:02 +02:00
}
2018-02-18 23:20:38 +01:00
2020-01-19 21:21:12 +01:00
$flagusers = DI :: config () -> get ( 'public_server' , 'flagusers' , false );
2018-02-18 23:20:38 +01:00
if ( $flagusers ) {
2021-10-07 07:53:16 +02:00
$users = DBA :: selectToArray ( 'user' , [], [ " NOT `account_expired` AND `login_date` < UTC_TIMESTAMP() - INTERVAL ? DAY AND `account_expires_on` <= ? AND `page-flags` = ? " ,
$flagusers , DBA :: NULL_DATETIME , User :: PAGE_FLAGS_NORMAL ]);
foreach ( $users as $rr ) {
$fields = [ 'account_expires_on' => DateTimeFormat :: utc ( 'now +6 days' )];
DBA :: update ( 'user' , $fields , [ 'uid' => $rr [ 'uid' ]]);
2012-05-18 10:12:02 +02:00
}
2018-02-18 23:20:38 +01:00
}
2012-05-18 10:12:02 +02:00
2020-01-19 21:21:12 +01:00
$flagposts = DI :: config () -> get ( 'public_server' , 'flagposts' );
$flagpostsexpire = DI :: config () -> get ( 'public_server' , 'flagpostsexpire' );
2012-05-18 10:12:02 +02:00
if ( $flagposts && $flagpostsexpire ) {
2021-10-07 07:53:16 +02:00
$users = DBA :: selectToArray ( 'user' , [], [ " NOT `account_expired` AND `login_date` < UTC_TIMESTAMP() - INTERVAL ? DAY AND `account_expires_on` <= ? AND NOT `expire` AND `page-flags` = ? " ,
$flagposts , DBA :: NULL_DATETIME , User :: PAGE_FLAGS_NORMAL ]);
foreach ( $users as $rr ) {
DBA :: update ( 'user' , [ 'expire' => $flagpostsexpire ], [ 'uid' => $rr [ 'uid' ]]);
2012-05-18 10:12:02 +02:00
}
2018-02-18 23:20:38 +01:00
}
2012-05-18 10:12:02 +02:00
2021-10-21 08:04:27 +02:00
Logger :: notice ( " public_server: cron end " );
2012-05-18 10:12:02 +02:00
}
2018-02-19 06:19:02 +01:00
function public_server_enotify ( & $a , & $b )
{
2021-02-03 23:29:09 +01:00
if ( ! empty ( $b [ 'params' ]) && $b [ 'params' ][ 'type' ] == Notification\Type :: SYSTEM
2018-11-30 15:11:56 +01:00
&& ! empty ( $b [ 'params' ][ 'system_type' ]) && $b [ 'params' ][ 'system_type' ] === 'public_server_expire' ) {
2019-12-30 03:55:10 +01:00
$b [ 'itemlink' ] = DI :: baseUrl () -> get ();
2020-01-19 21:21:12 +01:00
$b [ 'epreamble' ] = $b [ 'preamble' ] = DI :: l10n () -> t ( 'Your account on %s will expire in a few days.' , DI :: config () -> get ( 'system' , 'sitename' ));
2020-01-18 20:52:33 +01:00
$b [ 'subject' ] = DI :: l10n () -> t ( 'Your Friendica account is about to expire.' );
2020-01-19 21:21:12 +01:00
$b [ 'body' ] = DI :: 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= " . DI :: config () -> get ( 'system' , 'url' ) . " ] " . DI :: config () -> get ( 'config' , 'sitename' ) . " [/url] " );
2018-02-18 23:20:38 +01:00
}
2012-05-18 10:12:02 +02:00
}
2018-02-19 06:19:02 +01:00
function public_server_login ( $a , $b )
{
2020-01-19 21:21:12 +01:00
$days = DI :: config () -> get ( 'public_server' , 'expiredays' );
2018-02-18 23:20:38 +01:00
if ( ! $days ) {
2012-05-18 10:12:02 +02:00
return ;
2018-02-18 23:20:38 +01:00
}
$fields = [ 'account_expires_on' => DateTimeFormat :: utc ( 'now +' . $days . ' days' )];
2018-10-21 08:54:09 +02:00
$condition = [ " `uid` = ? AND `account_expires_on` > ? " , local_user (), DBA :: NULL_DATETIME ];
2018-07-20 14:20:48 +02:00
DBA :: update ( 'user' , $fields , $condition );
2012-05-18 10:12:02 +02:00
}
2013-12-20 18:58:50 +01:00
2018-02-19 06:19:02 +01:00
function public_server_addon_admin_post ( & $a )
{
2018-10-17 21:34:15 +02:00
BaseModule :: checkFormSecurityTokenRedirectOnError ( '/admin/addons/publicserver' , 'publicserver' );
2018-11-30 15:11:56 +01: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' ])) : '' );
2020-01-19 21:21:52 +01:00
DI :: config () -> set ( 'public_server' , 'expiredays' , $expiredays );
DI :: config () -> set ( 'public_server' , 'expireposts' , $expireposts );
DI :: config () -> set ( 'public_server' , 'nologin' , $nologin );
DI :: config () -> set ( 'public_server' , 'flagusers' , $flagusers );
DI :: config () -> set ( 'public_server' , 'flagposts' , $flagposts );
DI :: config () -> set ( 'public_server' , 'flagpostsexpire' , $flagpostsexpire );
2013-12-20 18:58:50 +01:00
}
2018-02-18 23:20:38 +01:00
2018-02-19 06:19:02 +01:00
function public_server_addon_admin ( & $a , & $o )
{
2018-10-17 21:34:15 +02:00
$token = BaseModule :: getFormSecurityToken ( " publicserver " );
2018-10-31 15:55:15 +01:00
$t = Renderer :: getMarkupTemplate ( " admin.tpl " , " addon/public_server " );
$o = Renderer :: replaceMacros ( $t , [
2020-01-18 20:52:33 +01:00
'$submit' => DI :: l10n () -> t ( 'Save Settings' ),
2018-02-18 23:20:38 +01:00
'$form_security_token' => $token ,
2020-01-18 20:52:33 +01:00
'$infotext' => DI :: l10n () -> t ( 'Set any of these options to 0 to deactivate it.' ),
2020-01-19 21:21:12 +01:00
'$expiredays' => [ " expiredays " , " Expire Days " , intval ( DI :: config () -> get ( 'public_server' , 'expiredays' )), " When an account is created on the site, it is given a hard " ],
'$expireposts' => [ " expireposts " , " Expire Posts " , intval ( DI :: config () -> get ( 'public_server' , 'expireposts' )), " Set the default days for posts to expire here " ],
'$nologin' => [ " nologin " , " No Login " , intval ( DI :: config () -> get ( 'public_server' , 'nologin' )), " Remove users who have never logged in after nologin days " ],
'$flagusers' => [ " flagusers " , " Flag users " , intval ( DI :: config () -> get ( 'public_server' , 'flagusers' )), " Remove users who last logged in over flagusers days ago " ],
'$flagposts' => [ " flagposts " , " Flag posts " , intval ( DI :: 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 ( DI :: config () -> get ( 'public_server' , 'flagpostsexpire' ))],
2018-02-18 23:20:38 +01:00
]);
2013-12-20 18:58:50 +01:00
}