2012-05-18 08:12:02 +00:00
< ? php
/**
* Name : public_server
2018-01-20 13:57:41 +00:00
* Description : Friendica addon with functions suitable for a public server .
2013-12-20 17:58:50 +00:00
* Version : 1.1
2012-05-18 08:12:02 +00:00
* Author : Keith Fernie < http :// friendika . me4 . it / profile / keith >
*/
2018-01-25 02:29:09 +00:00
2018-01-17 00:51:12 +00:00
use Friendica\Core\Addon ;
2017-11-06 23:55:24 +00:00
use Friendica\Core\Config ;
2018-01-22 19:03:11 +00:00
use Friendica\Core\L10n ;
2017-11-19 22:09:54 +00:00
use Friendica\Model\User ;
2018-01-25 02:29:09 +00:00
use Friendica\Util\Temporal ;
2012-05-18 08:12:02 +00:00
function public_server_install () {
2018-01-17 00:51:12 +00: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 19:03:11 +00:00
Addon :: registerHook ( 'enotify' , 'addon/public_server/public_server.php' , 'public_server_enotify' );
2018-01-17 00:51:12 +00:00
Addon :: registerHook ( 'logged_in' , 'addon/public_server/public_server.php' , 'public_server_login' );
2012-05-18 08:12:02 +00:00
}
function public_server_uninstall () {
2018-01-17 00:51:12 +00: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 19:03:11 +00:00
Addon :: unregisterHook ( 'enotify' , 'addon/public_server/public_server.php' , 'public_server_enotify' );
2018-01-17 00:51:12 +00:00
Addon :: unregisterHook ( 'logged_in' , 'addon/public_server/public_server.php' , 'public_server_login' );
2012-05-18 08:12:02 +00:00
}
function public_server_register_account ( $a , $b ) {
$uid = $b ;
2017-11-06 23:55:24 +00:00
$days = Config :: get ( 'public_server' , 'expiredays' );
$days_posts = Config :: get ( 'public_server' , 'expireposts' );
2012-05-18 08:12:02 +00:00
if ( ! $days )
return ;
2014-03-16 18:13:57 +00:00
$r = q ( " UPDATE user set account_expires_on = '%s', expire = %d where uid = %d " ,
2018-01-25 02:29:09 +00:00
dbesc ( Temporal :: convert ( 'now +' . $days . ' days' )),
2012-05-18 08:12:02 +00:00
intval ( $days_posts ),
intval ( $uid )
);
};
2013-07-19 21:34:54 +00:00
2012-05-18 08:12:02 +00:00
function public_server_cron ( $a , $b ) {
2013-07-19 21:34:54 +00:00
logger ( " public_server: cron start " );
2012-05-18 08:12:02 +00:00
require_once ( 'include/enotify.php' );
$r = q ( " select * from user where account_expires_on < UTC_TIMESTAMP() + INTERVAL 5 DAY and account_expires_on > '0000-00-00 00:00:00' and
expire_notification_sent = '0000-00-00 00:00:00' " );
if ( count ( $r )) {
foreach ( $r as $rr ) {
2018-01-15 13:15:33 +00:00
notification ([
2012-05-18 08:12:02 +00: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 19:03:11 +00:00
'source_name' => L10n :: t ( 'Administrator' ),
2012-05-18 08:12:02 +00:00
'source_link' => $a -> get_baseurl (),
'source_photo' => $a -> get_baseurl () . '/images/person-80.jpg' ,
2018-01-15 13:15:33 +00:00
]);
2012-05-18 08:12:02 +00:00
2014-03-16 18:13:57 +00:00
q ( " update user set expire_notification_sent = '%s' where uid = %d " ,
2018-01-26 04:28:13 +00:00
dbesc ( Temporal :: utcNow ()),
2012-05-18 08:12:02 +00:00
intval ( $rr [ 'uid' ])
);
}
}
$r = q ( " select * from user where account_expired = 1 and account_expires_on < UTC_TIMESTAMP() - INTERVAL 5 DAY and account_expires_on > '0000-00-00 00:00:00' " );
if ( count ( $r )) {
2017-11-19 22:09:54 +00:00
foreach ( $r as $rr ) {
User :: remove ( $rr [ 'uid' ]);
}
2012-05-18 08:12:02 +00:00
}
2017-11-06 23:55:24 +00:00
$nologin = Config :: get ( 'public_server' , 'nologin' );
2012-05-18 08:12:02 +00:00
if ( $nologin ) {
$r = q ( " select uid from user where account_expired = 0 and login_date = '0000-00-00 00:00:00' and register_date < UTC_TIMESTAMP() - INTERVAL %d DAY and account_expires_on = '0000-00-00 00:00:00' " , intval ( $nologin ));
if ( count ( $r )) {
foreach ( $r as $rr )
2014-03-16 18:13:57 +00:00
q ( " update user set account_expires_on = '%s' where uid = %d " ,
2018-01-25 02:29:09 +00:00
dbesc ( Temporal :: convert ( 'now +' . '6 days' )),
2012-05-18 08:12:02 +00:00
intval ( $rr [ 'uid' ])
);
}
}
2017-11-06 23:55:24 +00:00
$flagusers = Config :: get ( 'public_server' , 'flagusers' );
2012-05-18 08:12:02 +00:00
if ( $flagusers ) {
$r = q ( " select uid from user where account_expired = 0 and login_date < UTC_TIMESTAMP() - INTERVAL %d DAY and account_expires_on = '0000-00-00 00:00:00' and `page-flags` = 0 " , intval ( $flagusers ));
if ( count ( $r )) {
foreach ( $r as $rr )
2014-03-16 18:13:57 +00:00
q ( " update user set account_expires_on = '%s' where uid = %d " ,
2018-01-25 02:29:09 +00:00
dbesc ( Temporal :: convert ( 'now +' . '6 days' )),
2012-05-18 08:12:02 +00:00
intval ( $rr [ 'uid' ])
);
}
}
2017-11-06 23:55:24 +00:00
$flagposts = Config :: get ( 'public_server' , 'flagposts' );
$flagpostsexpire = Config :: get ( 'public_server' , 'flagpostsexpire' );
2012-05-18 08:12:02 +00:00
if ( $flagposts && $flagpostsexpire ) {
2012-05-19 18:44:22 +00:00
$r = q ( " select uid from user where account_expired = 0 and login_date < UTC_TIMESTAMP() - INTERVAL %d DAY and account_expires_on = '0000-00-00 00:00:00' and expire = 0 and `page-flags` = 0 " , intval ( $flagposts ));
2012-05-18 08:12:02 +00:00
if ( count ( $r )) {
foreach ( $r as $rr )
2014-03-16 18:13:57 +00:00
q ( " update user set expire = %d where uid = %d " ,
2012-05-18 08:12:02 +00:00
intval ( $flagpostsexpire ),
intval ( $rr [ 'uid' ])
);
}
}
2013-07-19 21:34:54 +00:00
logger ( " public_server: cron end " );
2012-05-18 08:12:02 +00:00
}
function public_server_enotify ( & $a , & $b ) {
2017-11-19 22:09:54 +00:00
if ( x ( $b , 'params' ) && $b [ 'params' ][ 'type' ] == NOTIFY_SYSTEM
2012-05-18 08:12:02 +00:00
&& x ( $b [ 'params' ], 'system_type' ) && $b [ 'params' ][ 'system_type' ] === 'public_server_expire' ) {
$b [ 'itemlink' ] = $a -> get_baseurl ();
2018-01-24 03:59:20 +00:00
$b [ 'epreamble' ] = $b [ 'preamble' ] = L10n :: t ( 'Your account on %s will expire in a few days.' , Config :: get ( 'system' , 'sitename' ));
2018-01-22 19:03:11 +00:00
$b [ 'subject' ] = L10n :: t ( 'Your Friendica account is about to expire.' );
2018-01-24 03:59:20 +00: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= " . $app -> config [ " system " ][ " url " ] . " ] " . $app -> config [ " sitename " ] . " [/url] " );
2012-05-18 08:12:02 +00:00
}
}
function public_server_login ( $a , $b ) {
2017-11-06 23:55:24 +00:00
$days = Config :: get ( 'public_server' , 'expiredays' );
2012-05-18 08:12:02 +00:00
if ( ! $days )
return ;
2014-03-16 18:13:57 +00:00
$r = q ( " UPDATE user set account_expires_on = '%s' where uid = %d and account_expires_on > '0000-00-00 00:00:00' " ,
2018-01-25 02:29:09 +00:00
dbesc ( Temporal :: convert ( 'now +' . $days . ' days' )),
2012-05-18 08:12:02 +00:00
local_user ()
);
}
2013-12-20 17:58:50 +00:00
2018-01-20 13:57:41 +00:00
function public_server_addon_admin_post ( & $a ) {
check_form_security_token_redirectOnErr ( '/admin/addons/publicserver' , 'publicserver' );
2013-12-20 18:58:27 +00:00
$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' ] )) : '' );
2017-11-06 23:55:24 +00: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 );
2018-01-22 19:03:11 +00:00
info ( L10n :: t ( 'Settings saved' ) . EOL );
2013-12-20 17:58:50 +00:00
}
2018-01-20 13:57:41 +00:00
function public_server_addon_admin ( & $a , & $o ) {
2013-12-20 17:58:50 +00:00
$token = get_form_security_token ( " publicserver " );
$t = get_markup_template ( " admin.tpl " , " addon/public_server " );
2018-01-15 13:15:33 +00:00
$o = replace_macros ( $t , [
2018-01-22 19:03:11 +00:00
'$submit' => L10n :: t ( 'Save Settings' ),
2013-12-20 17:58:50 +00:00
'$form_security_token' => $token ,
2018-01-22 19:03:11 +00:00
'$infotext' => L10n :: t ( 'Set any of these options to 0 to deactivate it.' ),
2018-01-15 13:15:33 +00:00
'$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 17:58:50 +00:00
}