2012-05-18 04:12:02 -04:00
< ? php
/**
* Name : public_server
2020-02-23 00:13:30 -05: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 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 ;
2018-12-26 02:28:16 -05:00
use Friendica\Core\Hook ;
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-12-29 21:55:10 -05:00
use Friendica\DI ;
2021-01-23 15:36:36 -05:00
use Friendica\Model\Notification ;
2021-10-07 01:53:16 -04:00
use Friendica\Model\User ;
2023-01-01 14:21:56 -05:00
use Friendica\Core\Config\Util\ConfigFileManager ;
2018-01-26 21:39:02 -05:00
use Friendica\Util\DateTimeFormat ;
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
}
2023-01-01 14:21:56 -05:00
function public_server_load_config ( App $a , ConfigFileManager $loader )
2018-06-27 23:18:46 -04:00
{
2022-11-20 12:44:53 -05:00
$a -> getConfigCache () -> load ( $loader -> loadAddonConfig ( 'public_server' ), \Friendica\Core\Config\ValueObject\Cache :: SOURCE_STATIC );
2018-06-27 23:18:46 -04:00
}
2022-06-23 01:16:22 -04:00
function public_server_register_account ( App $a , $b )
2018-02-19 00:19:02 -05:00
{
2012-05-18 04:12:02 -04:00
$uid = $b ;
2020-01-19 15:21:12 -05:00
$days = DI :: config () -> get ( 'public_server' , 'expiredays' );
$days_posts = DI :: config () -> get ( 'public_server' , 'expireposts' );
2018-02-18 17:20:38 -05:00
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
2022-06-23 01:16:22 -04:00
function public_server_cron ( App $a , $b )
2018-02-19 00:19:02 -05:00
{
2021-10-21 02:04:27 -04:00
Logger :: notice ( " public_server: cron start " );
2013-07-19 17:34:54 -04:00
2021-12-03 07:35:02 -05:00
$users = DBA :: selectToArray ( 'user' , [], [ " `account_expires_on` > ? AND `account_expires_on` < ?
AND `expire_notification_sent` <= ? " , DBA::NULL_DATETIME, DateTimeFormat::utc('now + 5 days'), DBA::NULL_DATETIME]);
2021-10-07 01:53:16 -04:00
foreach ( $users as $rr ) {
2021-10-19 15:53:10 -04:00
DI :: notify () -> createFromArray ([
2021-10-07 01:53:16 -04:00
'type' => Notification\Type :: SYSTEM ,
2023-01-09 09:22:30 -05:00
'event' => 'SYSTEM_PUBLIC_SERVER_EXPIRATION' ,
2021-10-07 01:53:16 -04:00
'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 04:12:02 -04:00
2021-10-07 01:53:16 -04:00
$fields = [ 'expire_notification_sent' => DateTimeFormat :: utcNow ()];
DBA :: update ( 'user' , $fields , [ 'uid' => $rr [ 'uid' ]]);
2012-05-18 04:12:02 -04:00
}
2020-01-19 15:21:12 -05:00
$nologin = DI :: config () -> get ( 'public_server' , 'nologin' , false );
2018-02-18 17:20:38 -05:00
if ( $nologin ) {
2021-12-03 07:35:02 -05:00
$users = DBA :: selectToArray ( 'user' , [], [ " NOT `account_expired` AND `login_date` <= ? AND `register_date` < ? AND `account_expires_on` <= ? " ,
DBA :: NULL_DATETIME , DateTimeFormat :: utc ( 'now - ' . ( int ) $nologin . ' days' ), DBA :: NULL_DATETIME ]);
2021-10-07 01:53:16 -04:00
foreach ( $users as $rr ) {
$fields = [ 'account_expires_on' => DateTimeFormat :: utc ( 'now +6 days' )];
DBA :: update ( 'user' , $fields , [ 'uid' => $rr [ 'uid' ]]);
2017-11-19 17:09:54 -05:00
}
2012-05-18 04:12:02 -04:00
}
2018-02-18 17:20:38 -05:00
2020-01-19 15:21:12 -05:00
$flagusers = DI :: config () -> get ( 'public_server' , 'flagusers' , false );
2018-02-18 17:20:38 -05:00
if ( $flagusers ) {
2021-12-03 07:35:02 -05:00
$users = DBA :: selectToArray ( 'user' , [], [ " NOT `account_expired` AND `login_date` < ? AND `account_expires_on` <= ? AND `page-flags` = ? " ,
DateTimeFormat :: utc ( 'now - ' . ( int ) $flagusers . ' days' ), DBA :: NULL_DATETIME , User :: PAGE_FLAGS_NORMAL ]);
2021-10-07 01:53:16 -04:00
foreach ( $users as $rr ) {
$fields = [ 'account_expires_on' => DateTimeFormat :: utc ( 'now +6 days' )];
DBA :: update ( 'user' , $fields , [ 'uid' => $rr [ 'uid' ]]);
2012-05-18 04:12:02 -04:00
}
2018-02-18 17:20:38 -05:00
}
2012-05-18 04:12:02 -04:00
2020-01-19 15:21:12 -05:00
$flagposts = DI :: config () -> get ( 'public_server' , 'flagposts' );
$flagpostsexpire = DI :: config () -> get ( 'public_server' , 'flagpostsexpire' );
2012-05-18 04:12:02 -04:00
if ( $flagposts && $flagpostsexpire ) {
2021-12-03 07:35:02 -05:00
$users = DBA :: selectToArray ( 'user' , [], [ " NOT `account_expired` AND `login_date` < ? AND `account_expires_on` <= ? AND NOT `expire` AND `page-flags` = ? " ,
DateTimeFormat :: utc ( 'now - ' . ( int ) $flagposts . ' days' ), DBA :: NULL_DATETIME , User :: PAGE_FLAGS_NORMAL ]);
2021-10-07 01:53:16 -04:00
foreach ( $users as $rr ) {
DBA :: update ( 'user' , [ 'expire' => $flagpostsexpire ], [ 'uid' => $rr [ 'uid' ]]);
2012-05-18 04:12:02 -04:00
}
2018-02-18 17:20:38 -05:00
}
2012-05-18 04:12:02 -04:00
2021-10-21 02:04:27 -04:00
Logger :: notice ( " public_server: cron end " );
2012-05-18 04:12:02 -04:00
}
2022-06-23 01:16:22 -04:00
function public_server_enotify ( App $a , array & $b )
2018-02-19 00:19:02 -05:00
{
2021-02-03 17:29:09 -05:00
if ( ! empty ( $b [ 'params' ]) && $b [ 'params' ][ 'type' ] == Notification\Type :: SYSTEM
2018-11-30 09:11:56 -05:00
&& ! empty ( $b [ 'params' ][ 'system_type' ]) && $b [ 'params' ][ 'system_type' ] === 'public_server_expire' ) {
2019-12-29 21:55:10 -05:00
$b [ 'itemlink' ] = DI :: baseUrl () -> get ();
2020-01-19 15:21:12 -05: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 14:52:33 -05:00
$b [ 'subject' ] = DI :: l10n () -> t ( 'Your Friendica account is about to expire.' );
2020-01-19 15:21:12 -05: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 17:20:38 -05:00
}
2012-05-18 04:12:02 -04:00
}
2022-06-23 01:16:22 -04:00
function public_server_login ( App $a , $b )
2018-02-19 00:19:02 -05:00
{
2020-01-19 15:21:12 -05:00
$days = DI :: config () -> get ( 'public_server' , 'expiredays' );
2018-02-18 17:20:38 -05:00
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' )];
2022-10-20 17:51:49 -04:00
$condition = [ " `uid` = ? AND `account_expires_on` > ? " , DI :: userSession () -> getLocalUserId (), 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
2022-06-23 01:16:22 -04:00
function public_server_addon_admin_post ( App $a )
2018-02-19 00:19:02 -05:00
{
2018-10-17 15:34:15 -04:00
BaseModule :: checkFormSecurityTokenRedirectOnError ( '/admin/addons/publicserver' , 'publicserver' );
2022-06-30 07:32:13 -04:00
DI :: config () -> set ( 'public_server' , 'expiredays' , trim ( $_POST [ 'expiredays' ] ? ? '' ));
DI :: config () -> set ( 'public_server' , 'expireposts' , trim ( $_POST [ 'expireposts' ] ? ? '' ));
DI :: config () -> set ( 'public_server' , 'nologin' , trim ( $_POST [ 'nologin' ] ? ? '' ));
DI :: config () -> set ( 'public_server' , 'flagusers' , trim ( $_POST [ 'flagusers' ] ? ? '' ));
DI :: config () -> set ( 'public_server' , 'flagposts' , trim ( $_POST [ 'flagposts' ] ? ? '' ));
DI :: config () -> set ( 'public_server' , 'flagpostsexpire' , trim ( $_POST [ 'flagpostsexpire' ] ? ? '' ));
2013-12-20 12:58:50 -05:00
}
2018-02-18 17:20:38 -05:00
2022-06-30 07:32:13 -04:00
function public_server_addon_admin ( App $a , string & $o )
2018-02-19 00:19:02 -05:00
{
2022-06-30 07:32:13 -04:00
$token = BaseModule :: getFormSecurityToken ( 'publicserver' );
$t = Renderer :: getMarkupTemplate ( 'admin.tpl' , 'addon/public_server' );
2018-10-31 10:55:15 -04:00
$o = Renderer :: replaceMacros ( $t , [
2020-01-18 14:52:33 -05:00
'$submit' => DI :: l10n () -> t ( 'Save Settings' ),
2018-02-18 17:20:38 -05:00
'$form_security_token' => $token ,
2020-01-18 14:52:33 -05:00
'$infotext' => DI :: l10n () -> t ( 'Set any of these options to 0 to deactivate it.' ),
2022-06-30 07:32:13 -04: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 " ],
2020-01-19 15:21:12 -05:00
'$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 17:20:38 -05:00
]);
2013-12-20 12:58:50 -05:00
}