2012-05-18 10:12:02 +02:00
< ? php
/**
* Name : public_server
2018-01-20 14:57:41 +01:00
* Description : Friendica addon with functions suitable for a public server .
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-01-17 01:51:12 +01:00
use Friendica\Core\Addon ;
2017-11-07 00:55:24 +01:00
use Friendica\Core\Config ;
2018-01-22 20:03:11 +01:00
use Friendica\Core\L10n ;
2018-02-19 14:56:47 +01:00
use Friendica\Database\DBM ;
2018-01-27 03:39:02 +01:00
use Friendica\Util\DateTimeFormat ;
2012-05-18 10:12:02 +02:00
2018-02-19 06:19:02 +01:00
function public_server_install ()
{
2018-06-28 05:18:46 +02:00
Addon :: registerHook ( 'load_config' , 'addon/public_server/public_server.php' , 'public_server_load_config' );
2018-01-17 01:51:12 +01:00
Addon :: registerHook ( 'register_account' , 'addon/public_server/public_server.php' , 'public_server_register_account' );
Addon :: registerHook ( 'cron' , 'addon/public_server/public_server.php' , 'public_server_cron' );
2018-01-22 20:03:11 +01:00
Addon :: registerHook ( 'enotify' , 'addon/public_server/public_server.php' , 'public_server_enotify' );
2018-01-17 01:51:12 +01:00
Addon :: registerHook ( 'logged_in' , 'addon/public_server/public_server.php' , 'public_server_login' );
2012-05-18 10:12:02 +02:00
}
2018-02-19 06:19:02 +01:00
function public_server_uninstall ()
{
2018-06-28 05:18:46 +02:00
Addon :: unregisterHook ( 'load_config' , 'addon/public_server/public_server.php' , 'public_server_load_config' );
2018-01-17 01:51:12 +01:00
Addon :: unregisterHook ( 'register_account' , 'addon/public_server/public_server.php' , 'public_server_register_account' );
Addon :: unregisterHook ( 'cron' , 'addon/public_server/public_server.php' , 'public_server_cron' );
2018-01-22 20:03:11 +01:00
Addon :: unregisterHook ( 'enotify' , 'addon/public_server/public_server.php' , 'public_server_enotify' );
2018-01-17 01:51:12 +01:00
Addon :: unregisterHook ( 'logged_in' , 'addon/public_server/public_server.php' , 'public_server_login' );
2012-05-18 10:12:02 +02:00
}
2018-06-28 05:18:46 +02:00
function public_server_load_config ( \Friendica\App $a )
{
$a -> loadConfigFile ( __DIR__ . '/config/public_server.ini.php' );
}
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 ;
2018-02-18 23:20:38 +01:00
$days = Config :: get ( 'public_server' , 'expiredays' );
$days_posts = Config :: get ( 'public_server' , 'expireposts' );
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 ];
dba :: update ( 'user' , $fields , [ 'uid' => $uid ]);
}
2012-05-18 10:12:02 +02:00
2018-02-19 06:19:02 +01:00
function public_server_cron ( $a , $b )
{
2013-07-19 23:34:54 +02:00
logger ( " public_server: cron start " );
2012-05-18 10:12:02 +02:00
require_once ( 'include/enotify.php' );
2018-02-19 06:19:02 +01:00
$r = q ( " SELECT * FROM `user` WHERE `account_expires_on` < UTC_TIMESTAMP() + INTERVAL 5 DAY AND
2018-02-18 23:20:38 +01:00
`account_expires_on` > '%s' AND
2018-02-19 06:19:02 +01:00
`expire_notification_sent` <= '%s' " ,
2018-02-18 23:20:38 +01:00
dbesc ( NULL_DATE ), dbesc ( NULL_DATE ));
2012-05-18 10:12:02 +02:00
2018-02-18 23:20:38 +01:00
if ( DBM :: is_result ( $r )) {
foreach ( $r as $rr ) {
2018-01-15 14:15:33 +01:00
notification ([
2012-05-18 10:12:02 +02: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 20:03:11 +01:00
'source_name' => L10n :: t ( 'Administrator' ),
2012-05-18 10:12:02 +02:00
'source_link' => $a -> get_baseurl (),
'source_photo' => $a -> get_baseurl () . '/images/person-80.jpg' ,
2018-01-15 14:15:33 +01:00
]);
2012-05-18 10:12:02 +02:00
2018-02-18 23:20:38 +01:00
$fields = [ 'expire_notification_sent' => DateTimeFormat :: utcNow ()];
dba :: update ( 'user' , $fields , [ 'uid' => $rr [ 'uid' ]]);
2012-05-18 10:12:02 +02:00
}
}
2018-02-18 23:20:38 +01:00
$nologin = Config :: get ( 'public_server' , 'nologin' , false );
if ( $nologin ) {
2018-02-19 06:19:02 +01: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-02-18 23:20:38 +01:00
dbesc ( NULL_DATE ), intval ( $nologin ), dbesc ( NULL_DATE ));
if ( DBM :: is_result ( $r )) {
foreach ( $r 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
$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 " ,
intval ( $flagusers ), dbesc ( NULL_DATE ));
if ( DBM :: is_result ( $r )) {
foreach ( $r 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
2018-02-18 23:20:38 +01:00
$flagposts = Config :: get ( 'public_server' , 'flagposts' );
$flagpostsexpire = Config :: get ( 'public_server' , 'flagpostsexpire' );
2012-05-18 10:12:02 +02:00
if ( $flagposts && $flagpostsexpire ) {
2018-02-18 23:20:38 +01: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 " ,
intval ( $flagposts ), dbesc ( NULL_DATE ));
if ( DBM :: is_result ( $r )) {
foreach ( $r 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
2013-07-19 23:34:54 +02:00
logger ( " 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 )
{
2018-02-18 23:20:38 +01:00
if ( x ( $b , 'params' ) && $b [ 'params' ][ 'type' ] == NOTIFY_SYSTEM
2012-05-18 10:12:02 +02:00
&& x ( $b [ 'params' ], 'system_type' ) && $b [ 'params' ][ 'system_type' ] === 'public_server_expire' ) {
2018-02-18 23:20:38 +01:00
$b [ 'itemlink' ] = $a -> get_baseurl ();
$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 14:38:36 +02: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 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 )
{
2018-02-18 23:20:38 +01:00
$days = Config :: get ( 'public_server' , 'expiredays' );
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' )];
$condition = [ " `uid` = ? AND `account_expires_on` > ? " , local_user (), NULL_DATE ];
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-02-18 23:20:38 +01:00
check_form_security_token_redirectOnErr ( '/admin/addons/publicserver' , 'publicserver' );
$expiredays = ( x ( $_POST , 'expiredays' ) ? notags ( trim ( $_POST [ 'expiredays' ])) : '' );
$expireposts = ( x ( $_POST , 'expireposts' ) ? notags ( trim ( $_POST [ 'expireposts' ])) : '' );
$nologin = ( x ( $_POST , 'nologin' ) ? notags ( trim ( $_POST [ 'nologin' ])) : '' );
$flagusers = ( x ( $_POST , 'flagusers' ) ? notags ( trim ( $_POST [ 'flagusers' ])) : '' );
$flagposts = ( x ( $_POST , 'flagposts' ) ? notags ( trim ( $_POST [ 'flagposts' ])) : '' );
$flagpostsexpire = ( x ( $_POST , 'flagpostsexpire' ) ? notags ( trim ( $_POST [ 'flagpostsexpire' ])) : '' );
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 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-02-18 23:20:38 +01:00
$token = get_form_security_token ( " publicserver " );
$t = get_markup_template ( " admin.tpl " , " addon/public_server " );
$o = replace_macros ( $t , [
'$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 18:58:50 +01:00
}