5 * Description: Friendica plugin/addon with functions suitable for a public server.
7 * Author: Keith Fernie <http://friendika.me4.it/profile/keith>
10 use Friendica\Core\Config;
11 use Friendica\Model\User;
14 function public_server_install() {
16 register_hook('register_account', 'addon/public_server/public_server.php', 'public_server_register_account');
17 register_hook('cron', 'addon/public_server/public_server.php', 'public_server_cron');
18 register_hook('enotify','addon/public_server/public_server.php', 'public_server_enotify');
19 register_hook('logged_in', 'addon/public_server/public_server.php', 'public_server_login');
23 function public_server_uninstall() {
25 unregister_hook('register_account', 'addon/public_server/public_server.php', 'public_server_register_account');
26 unregister_hook('cron', 'addon/public_server/public_server.php', 'public_server_cron');
27 unregister_hook('enotify','addon/public_server/public_server.php', 'public_server_enotify');
28 unregister_hook('logged_in', 'addon/public_server/public_server.php', 'public_server_login');
31 function public_server_register_account($a,$b) {
35 $days = Config::get('public_server','expiredays');
36 $days_posts = Config::get('public_server','expireposts');
40 $r = q("UPDATE user set account_expires_on = '%s', expire = %d where uid = %d",
41 dbesc(datetime_convert('UTC','UTC','now +' . $days . ' days')),
49 function public_server_cron($a,$b) {
50 logger("public_server: cron start");
52 require_once('include/enotify.php');
53 $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
54 expire_notification_sent = '0000-00-00 00:00:00' ");
60 'type' => NOTIFY_SYSTEM,
61 'system_type' => 'public_server_expire',
62 'language' => $rr['language'],
63 'to_name' => $rr['username'],
64 'to_email' => $rr['email'],
65 'source_name' => t('Administrator'),
66 'source_link' => $a->get_baseurl(),
67 'source_photo' => $a->get_baseurl() . '/images/person-80.jpg',
70 q("update user set expire_notification_sent = '%s' where uid = %d",
71 dbesc(datetime_convert()),
77 $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'");
80 User::remove($rr['uid']);
83 $nologin = Config::get('public_server','nologin');
85 $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));
88 q("update user set account_expires_on = '%s' where uid = %d",
89 dbesc(datetime_convert('UTC','UTC','now +' . '6 days')),
96 $flagusers = Config::get('public_server','flagusers');
98 $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));
101 q("update user set account_expires_on = '%s' where uid = %d",
102 dbesc(datetime_convert('UTC','UTC','now +' . '6 days')),
108 $flagposts = Config::get('public_server','flagposts');
109 $flagpostsexpire = Config::get('public_server','flagpostsexpire');
110 if ($flagposts && $flagpostsexpire) {
111 $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));
114 q("update user set expire = %d where uid = %d",
115 intval($flagpostsexpire),
121 logger("public_server: cron end");
125 function public_server_enotify(&$a, &$b) {
126 if (x($b, 'params') && $b['params']['type'] == NOTIFY_SYSTEM
127 && x($b['params'], 'system_type') && $b['params']['system_type'] === 'public_server_expire') {
128 $b['itemlink'] = $a->get_baseurl();
129 $b['epreamble'] = $b['preamble'] = sprintf( t('Your account on %s will expire in a few days.'), Config::get('system','sitename'));
130 $b['subject'] = t('Your Friendica account is about to expire.');
131 $b['body'] = sprintf( t("Hi %1\$s,\n\nYour 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]");
135 function public_server_login($a,$b) {
136 $days = Config::get('public_server','expiredays');
139 $r = q("UPDATE user set account_expires_on = '%s' where uid = %d and account_expires_on > '0000-00-00 00:00:00'",
140 dbesc(datetime_convert('UTC','UTC','now +' . $days . ' days')),
145 function public_server_plugin_admin_post ( &$a ) {
146 check_form_security_token_redirectOnErr('/admin/plugins/publicserver', 'publicserver');
147 $expiredays = (( x($_POST, 'expiredays') ) ? notags(trim($_POST['expiredays'] )) : '');
148 $expireposts = (( x($_POST, 'expireposts') ) ? notags(trim($_POST['expireposts'] )) : '');
149 $nologin = (( x($_POST, 'nologin') ) ? notags(trim($_POST['nologin'] )) : '');
150 $flagusers = (( x($_POST, 'flagusers') ) ? notags(trim($_POST['flagusers'] )) : '');
151 $flagposts = (( x($_POST, 'flagposts') ) ? notags(trim($_POST['flagposts'] )) : '');
152 $flagpostsexpire = (( x($_POST, 'flagpostsexpire') ) ? notags(trim($_POST['flagpostsexpire'] )) : '');
153 Config::set( 'public_server','expiredays',$expiredays );
154 Config::set( 'public_server','expireposts',$expireposts );
155 Config::set( 'public_server','nologin',$nologin );
156 Config::set( 'public_server','flagusers',$flagusers);
157 Config::set( 'public_server','flagposts',$flagposts );
158 Config::set( 'public_server','flagpostsexpire',$flagpostsexpire );
159 info( t('Settings saved').EOL );
161 function public_server_plugin_admin ( &$a, &$o) {
162 $token = get_form_security_token("publicserver");
163 $t = get_markup_template( "admin.tpl", "addon/public_server");
164 $o = replace_macros($t, array(
165 '$submit' => t('Save Settings'),
166 '$form_security_token' => $token,
167 '$infotext' => t('Set any of these options to 0 to deactivate it.'),
168 '$expiredays' => Array( "expiredays","Expire Days", intval(Config::get('public_server', 'expiredays')), "When an account is created on the site, it is given a hard "),
169 '$expireposts' => Array( "expireposts", "Expire Posts", intval(Config::get('public_server','expireposts')), "Set the default days for posts to expire here"),
170 '$nologin' => Array( "nologin", "No Login", intval(Config::get('public_server','nologin')), "Remove users who have never logged in after nologin days "),
171 '$flagusers' => Array( "flagusers", "Flag users", intval(Config::get('public_server','flagusers')), "Remove users who last logged in over flagusers days ago"),
172 '$flagposts' => Array( "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 "),
173 '$flagpostsexpire' => Array( "flagpostsexpire", "Flag posts expire", intval(Config::get('public_server','flagpostsexpire'))),