]> git.mxchange.org Git - friendica.git/blob - mod/admin.php
7386dc5a3c0a1e3d93b5f3b732aff73d6d62a0bf
[friendica.git] / mod / admin.php
1 <?php
2
3  /**
4   * Friendica admin
5   */
6 require_once("include/remoteupdate.php");
7  
8 function admin_post(&$a){
9
10
11         if(!is_site_admin()) {
12                 return;
13         }
14
15         // do not allow a page manager to access the admin panel at all.
16
17         if(x($_SESSION,'submanage') && intval($_SESSION['submanage']))
18                 return;
19         
20
21
22         // urls
23         if ($a->argc > 1){
24                 switch ($a->argv[1]){
25                         case 'site':
26                                 admin_page_site_post($a);
27                                 break;
28                         case 'users':
29                                 admin_page_users_post($a);
30                                 break;
31                         case 'plugins':
32                                 if ($a->argc > 2 && 
33                                         is_file("addon/".$a->argv[2]."/".$a->argv[2].".php")){
34                                                 @include_once("addon/".$a->argv[2]."/".$a->argv[2].".php");
35                                                 if(function_exists($a->argv[2].'_plugin_admin_post')) {
36                                                         $func = $a->argv[2].'_plugin_admin_post';
37                                                         $func($a);
38                                                 }
39                                 }
40                                 goaway($a->get_baseurl(true) . '/admin/plugins/' . $a->argv[2] );
41                                 return; // NOTREACHED
42                                 break;
43                         case 'themes':
44                                 $theme = $a->argv[2];
45                                 if (is_file("view/theme/$theme/config.php")){
46                                         require_once("view/theme/$theme/config.php");
47                                         if (function_exists("theme_admin_post")){
48                                                 theme_admin_post($a);
49                                         }
50                                 }
51                                 info(t('Theme settings updated.'));
52                                 if(is_ajax()) return;
53                                 
54                                 goaway($a->get_baseurl(true) . '/admin/themes/' . $theme );
55                                 return;
56                                 break;
57                         case 'logs':
58                                 admin_page_logs_post($a);
59                                 break;
60                         case 'update':
61                                 admin_page_remoteupdate_post($a);
62                                 break;
63                 }
64         }
65
66         goaway($a->get_baseurl(true) . '/admin' );
67         return; // NOTREACHED   
68 }
69
70 function admin_content(&$a) {
71
72         if(!is_site_admin()) {
73                 return login(false);
74         }
75
76         if(x($_SESSION,'submanage') && intval($_SESSION['submanage']))
77                 return;
78
79         /**
80          * Side bar links
81          */
82
83         // array( url, name, extra css classes )
84         $aside = Array(
85                 'site'   =>     Array($a->get_baseurl(true)."/admin/site/", t("Site") , "site"),
86                 'users'  =>     Array($a->get_baseurl(true)."/admin/users/", t("Users") , "users"),
87                 'plugins'=>     Array($a->get_baseurl(true)."/admin/plugins/", t("Plugins") , "plugins"),
88                 'themes' =>     Array($a->get_baseurl(true)."/admin/themes/", t("Themes") , "themes"),
89                 'update' =>     Array($a->get_baseurl(true)."/admin/update/", t("Update") , "update")
90         );
91         
92         /* get plugins admin page */
93         
94         $r = q("SELECT * FROM `addon` WHERE `plugin_admin`=1");
95         $aside['plugins_admin']=Array();
96         foreach ($r as $h){
97                 $plugin =$h['name'];
98                 $aside['plugins_admin'][] = Array($a->get_baseurl(true)."/admin/plugins/".$plugin, $plugin, "plugin");
99                 // temp plugins with admin
100                 $a->plugins_admin[] = $plugin;
101         }
102                 
103         $aside['logs'] = Array($a->get_baseurl(true)."/admin/logs/", t("Logs"), "logs");
104
105         $t = get_markup_template("admin_aside.tpl");
106         $a->page['aside'] = replace_macros( $t, array(
107                         '$admin' => $aside, 
108                         '$h_pending' => t('User registrations waiting for confirmation'),
109                         '$admurl'=> $a->get_baseurl(true)."/admin/"
110         ));
111
112
113
114         /**
115          * Page content
116          */
117         $o = '';
118         
119         // urls
120         if ($a->argc > 1){
121                 switch ($a->argv[1]){
122                         case 'site':
123                                 $o = admin_page_site($a);
124                                 break;
125                         case 'users':
126                                 $o = admin_page_users($a);
127                                 break;
128                         case 'plugins':
129                                 $o = admin_page_plugins($a);
130                                 break;
131                         case 'themes':
132                                 $o = admin_page_themes($a);
133                                 break;
134                         case 'logs':
135                                 $o = admin_page_logs($a);
136                                 break;
137                         case 'update':
138                                 $o = admin_page_remoteupdate($a);
139                                 break;
140                         default:
141                                 notice( t("Item not found.") );
142                 }
143         } else {
144                 $o = admin_page_summary($a);
145         }
146         
147         if(is_ajax()) {
148                 echo $o; 
149                 killme();
150         } else {
151                 return $o;
152         }
153
154
155
156 /**
157  * Admin Summary Page
158  */
159 function admin_page_summary(&$a) {
160         $r = q("SELECT `page-flags`, COUNT(uid) as `count` FROM `user` GROUP BY `page-flags`");
161         $accounts = Array(
162                 Array( t('Normal Account'), 0),
163                 Array( t('Soapbox Account'), 0),
164                 Array( t('Community/Celebrity Account'), 0),
165                 Array( t('Automatic Friend Account'), 0)
166         );
167         $users=0;
168         foreach ($r as $u){ $accounts[$u['page-flags']][1] = $u['count']; $users+= $u['count']; }
169
170         logger('accounts: ' . print_r($accounts,true));
171
172         $r = q("SELECT COUNT(id) as `count` FROM `register`");
173         $pending = $r[0]['count'];
174                 
175         $t = get_markup_template("admin_summary.tpl");
176         return replace_macros($t, array(
177                 '$title' => t('Administration'),
178                 '$page' => t('Summary'),
179                 '$users' => Array( t('Registered users'), $users),
180                 '$accounts' => $accounts,
181                 '$pending' => Array( t('Pending registrations'), $pending),
182                 '$version' => Array( t('Version'), FRIENDICA_VERSION),
183                 '$build' =>  get_config('system','build'),
184                 '$plugins' => Array( t('Active plugins'), $a->plugins )
185         ));
186 }
187
188
189 /**
190  * Admin Site Page
191  */
192 function admin_page_site_post(&$a){
193         if (!x($_POST,"page_site")){
194                 return;
195         }
196
197         $sitename                       =       ((x($_POST,'sitename'))                 ? notags(trim($_POST['sitename']))                      : '');
198         $banner                         =       ((x($_POST,'banner'))                   ? trim($_POST['banner'])                                        : false);
199         $language                       =       ((x($_POST,'language'))                 ? notags(trim($_POST['language']))                      : '');
200         $theme                          =       ((x($_POST,'theme'))                    ? notags(trim($_POST['theme']))                         : '');
201         $maximagesize           =       ((x($_POST,'maximagesize'))             ? intval(trim($_POST['maximagesize']))          :  0);
202         
203         
204         $register_policy        =       ((x($_POST,'register_policy'))  ? intval(trim($_POST['register_policy']))       :  0);
205         $abandon_days       =   ((x($_POST,'abandon_days'))         ? intval(trim($_POST['abandon_days']))          :  0);
206
207         $register_text          =       ((x($_POST,'register_text'))    ? notags(trim($_POST['register_text']))         : '');  
208         
209         $allowed_sites          =       ((x($_POST,'allowed_sites'))    ? notags(trim($_POST['allowed_sites']))         : '');
210         $allowed_email          =       ((x($_POST,'allowed_email'))    ? notags(trim($_POST['allowed_email']))         : '');
211         $block_public           =       ((x($_POST,'block_public'))             ? True  :       False);
212         $force_publish          =       ((x($_POST,'publish_all'))              ? True  :       False);
213         $global_directory       =       ((x($_POST,'directory_submit_url'))     ? notags(trim($_POST['directory_submit_url']))  : '');
214         $no_multi_reg           =       ((x($_POST,'no_multi_reg'))             ? True  :       False);
215         $no_openid                      =       !((x($_POST,'no_openid'))               ? True  :       False);
216         $no_regfullname         =       !((x($_POST,'no_regfullname'))  ? True  :       False);
217         $no_utf                         =       !((x($_POST,'no_utf'))                  ? True  :       False);
218         $no_community_page      =       !((x($_POST,'no_community_page'))       ? True  :       False);
219
220         $verifyssl                      =       ((x($_POST,'verifyssl'))                ? True  :       False);
221         $proxyuser                      =       ((x($_POST,'proxyuser'))                ? notags(trim($_POST['proxyuser']))     : '');
222         $proxy                          =       ((x($_POST,'proxy'))                    ? notags(trim($_POST['proxy'])) : '');
223         $timeout                        =       ((x($_POST,'timeout'))                  ? intval(trim($_POST['timeout']))               : 60);
224         $dfrn_only          =   ((x($_POST,'dfrn_only'))            ? True      :       False);
225         $ostatus_disabled   =   !((x($_POST,'ostatus_disabled')) ? True  :   False);
226         $diaspora_enabled   =   ((x($_POST,'diaspora_enabled')) ? True   :  False);
227         $ssl_policy         =   ((x($_POST,'ssl_policy')) ? intval($_POST['ssl_policy']) : 0);
228
229         if($ssl_policy != intval(get_config('system','ssl_policy'))) {
230                 if($ssl_policy == SSL_POLICY_FULL) {
231                         q("update `contact` set 
232                                 `url`     = replace(`url`    , 'http:' , 'https:'),
233                                 `photo`   = replace(`photo`  , 'http:' , 'https:'),
234                                 `thumb`   = replace(`thumb`  , 'http:' , 'https:'),
235                                 `micro`   = replace(`micro`  , 'http:' , 'https:'),
236                                 `request` = replace(`request`, 'http:' , 'https:'),
237                                 `notify`  = replace(`notify` , 'http:' , 'https:'),
238                                 `poll`    = replace(`poll`   , 'http:' , 'https:'),
239                                 `confirm` = replace(`confirm`, 'http:' , 'https:'),
240                                 `poco`    = replace(`poco`   , 'http:' , 'https:')
241                                 where `self` = 1"
242                         );
243                         q("update `profile` set 
244                                 `photo`   = replace(`photo`  , 'http:' , 'https:'),
245                                 `thumb`   = replace(`thumb`  , 'http:' , 'https:')
246                                 where 1 "
247                         );
248                 }
249                 elseif($ssl_policy == SSL_POLICY_SELFSIGN) {
250                         q("update `contact` set 
251                                 `url`     = replace(`url`    , 'https:' , 'http:'),
252                                 `photo`   = replace(`photo`  , 'https:' , 'http:'),
253                                 `thumb`   = replace(`thumb`  , 'https:' , 'http:'),
254                                 `micro`   = replace(`micro`  , 'https:' , 'http:'),
255                                 `request` = replace(`request`, 'https:' , 'http:'),
256                                 `notify`  = replace(`notify` , 'https:' , 'http:'),
257                                 `poll`    = replace(`poll`   , 'https:' , 'http:'),
258                                 `confirm` = replace(`confirm`, 'https:' , 'http:'),
259                                 `poco`    = replace(`poco`   , 'https:' , 'http:')
260                                 where `self` = 1"
261                         );
262                         q("update `profile` set 
263                                 `photo`   = replace(`photo`  , 'https:' , 'http:'),
264                                 `thumb`   = replace(`thumb`  , 'https:' , 'http:')
265                                 where 1 "
266                         );
267                 }
268         }
269         set_config('system','ssl_policy',$ssl_policy);
270
271         set_config('config','sitename',$sitename);
272         if ($banner==""){
273                 // don't know why, but del_config doesn't work...
274                 q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
275                         dbesc("system"),
276                         dbesc("banner")
277                 );
278         } else {
279                 set_config('system','banner', $banner);
280         }
281         set_config('system','language', $language);
282         set_config('system','theme', $theme);
283         set_config('system','maximagesize', $maximagesize);
284         
285         set_config('config','register_policy', $register_policy);
286         set_config('system','account_abandon_days', $abandon_days);
287         set_config('config','register_text', $register_text);
288         set_config('system','allowed_sites', $allowed_sites);
289         set_config('system','allowed_email', $allowed_email);
290         set_config('system','block_public', $block_public);
291         set_config('system','publish_all', $force_publish);
292         if ($global_directory==""){
293                 // don't know why, but del_config doesn't work...
294                 q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
295                         dbesc("system"),
296                         dbesc("directory_submit_url")
297                 );
298         } else {
299                 set_config('system','directory_submit_url', $global_directory);
300         }
301         set_config('system','directory_search_url', $global_search_url);
302         set_config('system','block_extended_register', $no_multi_reg);
303         set_config('system','no_openid', $no_openid);
304         set_config('system','no_regfullname', $no_regfullname);
305         set_config('system','no_community_page', $no_community_page);
306         set_config('system','no_utf', $no_utf);
307         set_config('system','verifyssl', $verifyssl);
308         set_config('system','proxyuser', $proxyuser);
309         set_config('system','proxy', $proxy);
310         set_config('system','curl_timeout', $timeout);
311         set_config('system','dfrn_only', $dfrn_only);
312         set_config('system','ostatus_disabled', $ostatus_disabled);
313         set_config('system','diaspora_enabled', $diaspora_enabled);
314
315         info( t('Site settings updated.') . EOL);
316         goaway($a->get_baseurl(true) . '/admin/site' );
317         return; // NOTREACHED   
318         
319 }
320  
321 function admin_page_site(&$a) {
322         
323         /* Installed langs */
324         $lang_choices = array();
325         $langs = glob('view/*/strings.php');
326         
327         if(is_array($langs) && count($langs)) {
328                 if(! in_array('view/en/strings.php',$langs))
329                         $langs[] = 'view/en/';
330                 asort($langs);
331                 foreach($langs as $l) {
332                         $t = explode("/",$l);
333                         $lang_choices[$t[1]] = $t[1];
334                 }
335         }
336         
337         /* Installed themes */
338         $theme_choices = array();
339         $files = glob('view/theme/*');
340         if($files) {
341                 foreach($files as $file) {
342                         $f = basename($file);
343                         $theme_name = ((file_exists($file . '/experimental')) ?  sprintf("%s - \x28Experimental\x29", $f) : $f);
344                         $theme_choices[$f] = $theme_name;
345                 }
346         }
347         
348         
349         /* Banner */
350         $banner = get_config('system','banner');
351         if($banner == false) 
352                 $banner = '<a href="http://friendica.com"><img id="logo-img" src="images/friendica-32.png" alt="logo" /></a><span id="logo-text"><a href="http://friendica.com">Friendica</a></span>';
353         $banner = htmlspecialchars($banner);
354         
355         //echo "<pre>"; var_dump($lang_choices); die("</pre>");
356
357         /* Register policy */
358         $register_choices = Array(
359                 REGISTER_CLOSED => t("Closed"),
360                 REGISTER_APPROVE => t("Requires approval"),
361                 REGISTER_OPEN => t("Open")
362         ); 
363
364         $ssl_choices = array(
365                 SSL_POLICY_NONE => t("No SSL policy, links will track page SSL state"),
366                 SSL_POLICY_FULL => t("Force all links to use SSL"),
367                 SSL_POLICY_SELFSIGN => t("Self-signed certificate, use SSL for local links only (discouraged)")
368         );
369
370         $t = get_markup_template("admin_site.tpl");
371         return replace_macros($t, array(
372                 '$title' => t('Administration'),
373                 '$page' => t('Site'),
374                 '$submit' => t('Submit'),
375                 '$registration' => t('Registration'),
376                 '$upload' => t('File upload'),
377                 '$corporate' => t('Policies'),
378                 '$advanced' => t('Advanced'),
379                 
380                 '$baseurl' => $a->get_baseurl(true),
381                                                                         // name, label, value, help string, extra data...
382                 '$sitename'             => array('sitename', t("Site name"), htmlentities($a->config['sitename'], ENT_QUOTES), ""),
383                 '$banner'                       => array('banner', t("Banner/Logo"), $banner, ""),
384                 '$language'             => array('language', t("System language"), get_config('system','language'), "", $lang_choices),
385                 '$theme'                        => array('theme', t("System theme"), get_config('system','theme'), t("Default system theme - may be over-ridden by user profiles - <a href='#' id='cnftheme'>change theme settings</a>"), $theme_choices),
386                 '$ssl_policy'       => array('ssl_policy', t("SSL link policy"), (string) intval(get_config('system','ssl_policy')), t("Determines whether generated links should be forced to use SSL"), $ssl_choices),
387                 '$maximagesize'         => array('maximagesize', t("Maximum image size"), get_config('system','maximagesize'), t("Maximum size in bytes of uploaded images. Default is 0, which means no limits.")),
388
389                 '$register_policy'      => array('register_policy', t("Register policy"), $a->config['register_policy'], "", $register_choices),
390                 '$register_text'        => array('register_text', t("Register text"), htmlentities($a->config['register_text'], ENT_QUOTES), t("Will be displayed prominently on the registration page.")),
391                 '$abandon_days'     => array('abandon_days', t('Accounts abandoned after x days'), get_config('system','account_abandon_days'), t('Will not waste system resources polling external sites for abandonded accounts. Enter 0 for no time limit.')),
392                 '$allowed_sites'        => array('allowed_sites', t("Allowed friend domains"), get_config('system','allowed_sites'), t("Comma separated list of domains which are allowed to establish friendships with this site. Wildcards are accepted. Empty to allow any domains")),
393                 '$allowed_email'        => array('allowed_email', t("Allowed email domains"), get_config('system','allowed_email'), t("Comma separated list of domains which are allowed in email addresses for registrations to this site. Wildcards are accepted. Empty to allow any domains")),
394                 '$block_public'         => array('block_public', t("Block public"), get_config('system','block_public'), t("Check to block public access to all otherwise public personal pages on this site unless you are currently logged in.")),
395                 '$force_publish'        => array('publish_all', t("Force publish"), get_config('system','publish_all'), t("Check to force all profiles on this site to be listed in the site directory.")),
396                 '$global_directory'     => array('directory_submit_url', t("Global directory update URL"), get_config('system','directory_submit_url'), t("URL to update the global directory. If this is not set, the global directory is completely unavailable to the application.")),
397                         
398                 '$no_multi_reg'         => array('no_multi_reg', t("Block multiple registrations"),  get_config('system','block_extended_register'), t("Disallow users to register additional accounts for use as pages.")),
399                 '$no_openid'            => array('no_openid', t("OpenID support"), !get_config('system','no_openid'), t("OpenID support for registration and logins.")),
400                 '$no_regfullname'       => array('no_regfullname', t("Fullname check"), !get_config('system','no_regfullname'), t("Force users to register with a space between firstname and lastname in Full name, as an antispam measure")),
401                 '$no_utf'                       => array('no_utf', t("UTF-8 Regular expressions"), !get_config('system','no_utf'), t("Use PHP UTF8 regular expressions")),
402                 '$no_community_page' => array('no_community_page', t("Show Community Page"), !get_config('system','no_community_page'), t("Display a Community page showing all recent public postings on this site.")),
403                 '$ostatus_disabled' => array('ostatus_disabled', t("Enable OStatus support"), !get_config('system','ostatus_disable'), t("Provide built-in OStatus \x28identi.ca, status.net, etc.\x29 compatibility. All communications in OStatus are public, so privacy warnings will be occasionally displayed.")), 
404                 '$diaspora_enabled' => array('diaspora_enabled', t("Enable Diaspora support"), get_config('system','diaspora_enabled'), t("Provide built-in Diaspora network compatibility.")), 
405                 '$dfrn_only'        => array('dfrn_only', t('Only allow Friendica contacts'), get_config('system','dfrn_only'), t("All contacts must use Friendica protocols. All other built-in communication protocols disabled.")),
406                 '$verifyssl'            => array('verifyssl', t("Verify SSL"), get_config('system','verifyssl'), t("If you wish, you can turn on strict certificate checking. This will mean you cannot connect (at all) to self-signed SSL sites.")),
407                 '$proxyuser'            => array('proxyuser', t("Proxy user"), get_config('system','proxyuser'), ""),
408                 '$proxy'                        => array('proxy', t("Proxy URL"), get_config('system','proxy'), ""),
409                 '$timeout'                      => array('timeout', t("Network timeout"), (x(get_config('system','curl_timeout'))?get_config('system','curl_timeout'):60), t("Value is in seconds. Set to 0 for unlimited (not recommended).")),
410
411                         
412         ));
413
414 }
415
416
417 /**
418  * Users admin page
419  */
420 function admin_page_users_post(&$a){
421         $pending = ( x($_POST, 'pending') ? $_POST['pending'] : Array() );
422         $users = ( x($_POST, 'user') ? $_POST['user'] : Array() );
423         
424         if (x($_POST,'page_users_block')){
425                 foreach($users as $uid){
426                         q("UPDATE `user` SET `blocked`=1-`blocked` WHERE `uid`=%s",
427                                 intval( $uid )
428                         );
429                 }
430                 notice( sprintf( tt("%s user blocked/unblocked", "%s users blocked/unblocked", count($users)), count($users)) );
431         }
432         if (x($_POST,'page_users_delete')){
433                 require_once("include/Contact.php");
434                 foreach($users as $uid){
435                         user_remove($uid);
436                 }
437                 notice( sprintf( tt("%s user deleted", "%s users deleted", count($users)), count($users)) );
438         }
439         
440         if (x($_POST,'page_users_approve')){
441                 require_once("mod/regmod.php");
442                 foreach($pending as $hash){
443                         user_allow($hash);
444                 }
445         }
446         if (x($_POST,'page_users_deny')){
447                 require_once("mod/regmod.php");
448                 foreach($pending as $hash){
449                         user_deny($hash);
450                 }
451         }
452         goaway($a->get_baseurl(true) . '/admin/users' );
453         return; // NOTREACHED   
454 }
455  
456 function admin_page_users(&$a){
457         if ($a->argc>2) {
458                 $uid = $a->argv[3];
459                 $user = q("SELECT * FROM `user` WHERE `uid`=%d", intval($uid));
460                 if (count($user)==0){
461                         notice( 'User not found' . EOL);
462                         goaway($a->get_baseurl(true) . '/admin/users' );
463                         return; // NOTREACHED                                           
464                 }               
465                 switch($a->argv[2]){
466                         case "delete":{
467                                 // delete user
468                                 require_once("include/Contact.php");
469                                 user_remove($uid);
470                                 
471                                 notice( sprintf(t("User '%s' deleted"), $user[0]['username']) . EOL);
472                         }; break;
473                         case "block":{
474                                 q("UPDATE `user` SET `blocked`=%d WHERE `uid`=%s",
475                                         intval( 1-$user[0]['blocked'] ),
476                                         intval( $uid )
477                                 );
478                                 notice( sprintf( ($user[0]['blocked']?t("User '%s' unblocked"):t("User '%s' blocked")) , $user[0]['username']) . EOL);
479                         }; break;
480                 }
481                 goaway($a->get_baseurl(true) . '/admin/users' );
482                 return; // NOTREACHED   
483                 
484         }
485         
486         /* get pending */
487         $pending = q("SELECT `register`.*, `contact`.`name`, `user`.`email`
488                                  FROM `register`
489                                  LEFT JOIN `contact` ON `register`.`uid` = `contact`.`uid`
490                                  LEFT JOIN `user` ON `register`.`uid` = `user`.`uid`;");
491         
492         
493         /* get users */
494
495         $total = q("SELECT count(*) as total FROM `user` where 1");
496         if(count($total)) {
497                 $a->set_pager_total($total[0]['total']);
498                 $a->set_pager_itemspage(100);
499         }
500         
501         
502         $users = q("SELECT `user` . * , `contact`.`name` , `contact`.`url` , `contact`.`micro`, `lastitem`.`lastitem_date`
503                                 FROM
504                                         (SELECT MAX(`item`.`changed`) as `lastitem_date`, `item`.`uid`
505                                         FROM `item`
506                                         WHERE `item`.`type` = 'wall'
507                                         GROUP BY `item`.`uid`) AS `lastitem`
508                                                  RIGHT OUTER JOIN `user` ON `user`.`uid` = `lastitem`.`uid`,
509                                            `contact`
510                                 WHERE
511                                            `user`.`uid` = `contact`.`uid`
512                                                 AND `user`.`verified` =1
513                                         AND `contact`.`self` =1
514                                 ORDER BY `contact`.`name` LIMIT %d, %d
515                                 ",
516                                 intval($a->pager['start']),
517                                 intval($a->pager['itemspage'])
518                                 );
519                                         
520         function _setup_users($e){
521                 $accounts = Array(
522                         t('Normal Account'), 
523                         t('Soapbox Account'),
524                         t('Community/Celebrity Account'),
525                         t('Automatic Friend Account')
526                 );
527                 $e['page-flags'] = $accounts[$e['page-flags']];
528                 $e['register_date'] = relative_date($e['register_date']);
529                 $e['login_date'] = relative_date($e['login_date']);
530                 $e['lastitem_date'] = relative_date($e['lastitem_date']);
531                 return $e;
532         }
533         $users = array_map("_setup_users", $users);
534         
535         
536         $t = get_markup_template("admin_users.tpl");
537         $o = replace_macros($t, array(
538                 // strings //
539                 '$title' => t('Administration'),
540                 '$page' => t('Users'),
541                 '$submit' => t('Submit'),
542                 '$select_all' => t('select all'),
543                 '$h_pending' => t('User registrations waiting for confirm'),
544                 '$th_pending' => array( t('Request date'), t('Name'), t('Email') ),
545                 '$no_pending' =>  t('No registrations.'),
546                 '$approve' => t('Approve'),
547                 '$deny' => t('Deny'),
548                 '$delete' => t('Delete'),
549                 '$block' => t('Block'),
550                 '$unblock' => t('Unblock'),
551                 
552                 '$h_users' => t('Users'),
553                 '$th_users' => array( t('Name'), t('Email'), t('Register date'), t('Last login'), t('Last item'),  t('Account') ),
554
555                 '$confirm_delete_multi' => t('Selected users will be deleted!\n\nEverything these users had posted on this site will be permanently deleted!\n\nAre you sure?'),
556                 '$confirm_delete' => t('The user {0} will be deleted!\n\nEverything this user has posted on this site will be permanently deleted!\n\nAre you sure?'),
557
558
559                 // values //
560                 '$baseurl' => $a->get_baseurl(true),
561
562                 '$pending' => $pending,
563                 '$users' => $users,
564         ));
565         $o .= paginate($a);
566         return $o;
567 }
568
569
570 /*
571  * Plugins admin page
572  */
573
574 function admin_page_plugins(&$a){
575         
576         /**
577          * Single plugin
578          */
579         if ($a->argc == 3){
580                 $plugin = $a->argv[2];
581                 if (!is_file("addon/$plugin/$plugin.php")){
582                         notice( t("Item not found.") );
583                         return;
584                 }
585                 
586                 if (x($_GET,"a") && $_GET['a']=="t"){
587                         // Toggle plugin status
588                         $idx = array_search($plugin, $a->plugins);
589                         if ($idx !== false){
590                                 unset($a->plugins[$idx]);
591                                 uninstall_plugin($plugin);
592                                 info( sprintf( t("Plugin %s disabled."), $plugin ) );
593                         } else {
594                                 $a->plugins[] = $plugin;
595                                 install_plugin($plugin);
596                                 info( sprintf( t("Plugin %s enabled."), $plugin ) );
597                         }
598                         set_config("system","addon", implode(", ",$a->plugins));
599                         goaway($a->get_baseurl(true) . '/admin/plugins' );
600                         return; // NOTREACHED   
601                 }
602                 // display plugin details
603                 require_once('library/markdown.php');
604
605                 if (in_array($plugin, $a->plugins)){
606                         $status="on"; $action= t("Disable");
607                 } else {
608                         $status="off"; $action= t("Enable");
609                 }
610                 
611                 $readme=Null;
612                 if (is_file("addon/$plugin/README.md")){
613                         $readme = file_get_contents("addon/$plugin/README.md");
614                         $readme = Markdown($readme);
615                 } else if (is_file("addon/$plugin/README")){
616                         $readme = "<pre>". file_get_contents("addon/$plugin/README") ."</pre>";
617                 } 
618                 
619                 $admin_form="";
620                 if (is_array($a->plugins_admin) && in_array($plugin, $a->plugins_admin)){
621                         @require_once("addon/$plugin/$plugin.php");
622                         $func = $plugin.'_plugin_admin';
623                         $func($a, $admin_form);
624                 }
625                 
626                 $t = get_markup_template("admin_plugins_details.tpl");
627                 return replace_macros($t, array(
628                         '$title' => t('Administration'),
629                         '$page' => t('Plugins'),
630                         '$toggle' => t('Toggle'),
631                         '$settings' => t('Settings'),
632                         '$baseurl' => $a->get_baseurl(true),
633                 
634                         '$plugin' => $plugin,
635                         '$status' => $status,
636                         '$action' => $action,
637                         '$info' => get_plugin_info($plugin),
638                         '$str_author' => t('Author: '),
639                         '$str_maintainer' => t('Maintainer: '),                 
640                 
641                         '$admin_form' => $admin_form,
642                         '$function' => 'plugins',
643                         '$screenshot' => '',
644                         '$readme' => $readme
645                 ));
646         } 
647          
648          
649         
650         /**
651          * List plugins
652          */
653         
654         $plugins = array();
655         $files = glob("addon/*/");
656         if($files) {
657                 foreach($files as $file) {      
658                         if (is_dir($file)){
659                                 list($tmp, $id)=array_map("trim", explode("/",$file));
660                                 $info = get_plugin_info($id);
661                                 $plugins[] = array( $id, (in_array($id,  $a->plugins)?"on":"off") , $info);
662                         }
663                 }
664         }
665         
666         $t = get_markup_template("admin_plugins.tpl");
667         return replace_macros($t, array(
668                 '$title' => t('Administration'),
669                 '$page' => t('Plugins'),
670                 '$submit' => t('Submit'),
671                 '$baseurl' => $a->get_baseurl(true),
672                 '$function' => 'plugins',       
673                 '$plugins' => $plugins
674         ));
675 }
676
677 function toggle_theme(&$themes,$th,&$result) {
678         for($x = 0; $x < count($themes); $x ++) {
679                 if($themes[$x]['name'] === $th) {
680                         if($themes[$x]['allowed']) {
681                                 $themes[$x]['allowed'] = 0;
682                                 $result = 0;
683                         }
684                         else {
685                                 $themes[$x]['allowed'] = 1;
686                                 $result = 1;
687                         }
688                 }
689         }
690 }
691
692 function theme_status($themes,$th) {
693         for($x = 0; $x < count($themes); $x ++) {
694                 if($themes[$x]['name'] === $th) {
695                         if($themes[$x]['allowed']) {
696                                 return 1;
697                         }
698                         else {
699                                 return 0;
700                         }
701                 }
702         }
703         return 0;
704 }
705         
706
707
708 function rebuild_theme_table($themes) {
709         $o = '';
710         if(count($themes)) {
711                 foreach($themes as $th) {
712                         if($th['allowed']) {
713                                 if(strlen($o))
714                                         $o .= ',';
715                                 $o .= $th['name'];
716                         }
717                 }
718         }
719         return $o;
720 }
721
722         
723 /*
724  * Themes admin page
725  */
726
727 function admin_page_themes(&$a){
728         
729         $allowed_themes_str = get_config('system','allowed_themes');
730         $allowed_themes_raw = explode(',',$allowed_themes_str);
731         $allowed_themes = array();
732         if(count($allowed_themes_raw))
733                 foreach($allowed_themes_raw as $x)
734                         if(strlen(trim($x)))
735                                 $allowed_themes[] = trim($x);
736
737         $themes = array();
738     $files = glob('view/theme/*');
739     if($files) {
740         foreach($files as $file) {
741             $f = basename($file);
742             $is_experimental = intval(file_exists($file . '/experimental'));
743                         $is_unsupported = 1-(intval(file_exists($file . '/unsupported')));
744                         $is_allowed = intval(in_array($f,$allowed_themes));
745                         $themes[] = array('name' => $f, 'experimental' => $is_experimental, 'supported' => $is_supported, 'allowed' => $is_allowed);
746         }
747     }
748
749         if(! count($themes)) {
750                 notice( t('No themes found.'));
751                 return;
752         }
753
754         /**
755          * Single theme
756          */
757
758         if ($a->argc == 3){
759                 $theme = $a->argv[2];
760                 if(! is_dir("view/theme/$theme")){
761                         notice( t("Item not found.") );
762                         return;
763                 }
764                 
765                 if (x($_GET,"a") && $_GET['a']=="t"){
766
767                         // Toggle theme status
768
769                         toggle_theme($themes,$theme,$result);
770                         $s = rebuild_theme_table($themes);
771                         if($result)
772                                 info( sprintf('Theme %s enabled.',$theme));
773                         else
774                                 info( sprintf('Theme %s disabled.',$theme));
775
776                         set_config('system','allowed_themes',$s);
777                         goaway($a->get_baseurl(true) . '/admin/themes' );
778                         return; // NOTREACHED   
779                 }
780
781                 // display theme details
782                 require_once('library/markdown.php');
783
784                 if (theme_status($themes,$theme)) {
785                         $status="on"; $action= t("Disable");
786                 } else {
787                         $status="off"; $action= t("Enable");
788                 }
789                 
790                 $readme=Null;
791                 if (is_file("view/theme/$theme/README.md")){
792                         $readme = file_get_contents("view/theme/$theme/README.md");
793                         $readme = Markdown($readme);
794                 } else if (is_file("view/theme/$theme/README")){
795                         $readme = "<pre>". file_get_contents("view/theme/$theme/README") ."</pre>";
796                 } 
797                 
798                 $admin_form="";
799                 if (is_file("view/theme/$theme/config.php")){
800                         require_once("view/theme/$theme/config.php");
801                         if(function_exists("theme_admin")){
802                                 $admin_form = theme_admin($a);
803                         }
804                         
805                 }
806                 
807
808                 $screenshot = array( get_theme_screenshot($theme), t('Screenshot'));
809                 if(! stristr($screenshot[0],$theme))
810                         $screenshot = null;             
811
812                 $t = get_markup_template("admin_plugins_details.tpl");
813                 return replace_macros($t, array(
814                         '$title' => t('Administration'),
815                         '$page' => t('Themes'),
816                         '$toggle' => t('Toggle'),
817                         '$settings' => t('Settings'),
818                         '$baseurl' => $a->get_baseurl(true),
819                 
820                         '$plugin' => $theme,
821                         '$status' => $status,
822                         '$action' => $action,
823                         '$info' => get_theme_info($theme),
824                         '$function' => 'themes',
825                         '$admin_form' => $admin_form,
826                         '$str_author' => t('Author: '),
827                         '$str_maintainer' => t('Maintainer: '),
828                         '$screenshot' => $screenshot,
829                         '$readme' => $readme
830                 ));
831         } 
832          
833          
834         
835         /**
836          * List themes
837          */
838         
839         $xthemes = array();
840         if($themes) {
841                 foreach($themes as $th) {
842                         $xthemes[] = array($th['name'],(($th['allowed']) ? "on" : "off"), get_theme_info($th['name']));
843                 }
844         }
845         
846         $t = get_markup_template("admin_plugins.tpl");
847         return replace_macros($t, array(
848                 '$title' => t('Administration'),
849                 '$page' => t('Themes'),
850                 '$submit' => t('Submit'),
851                 '$baseurl' => $a->get_baseurl(true),
852                 '$function' => 'themes',
853                 '$plugins' => $xthemes,
854                 '$experimental' => t('[Experimental]'),
855                 '$unsupported' => t('[Unsupported]')
856         ));
857 }
858
859
860 /**
861  * Logs admin page
862  */
863  
864 function admin_page_logs_post(&$a) {
865         if (x($_POST,"page_logs")) {
866
867                 $logfile                =       ((x($_POST,'logfile'))          ? notags(trim($_POST['logfile']))       : '');
868                 $debugging              =       ((x($_POST,'debugging'))        ? true                                                          : false);
869                 $loglevel               =       ((x($_POST,'loglevel'))         ? intval(trim($_POST['loglevel']))      : 0);
870
871                 set_config('system','logfile', $logfile);
872                 set_config('system','debugging',  $debugging);
873                 set_config('system','loglevel', $loglevel);
874
875                 
876         }
877
878         info( t("Log settings updated.") );
879         goaway($a->get_baseurl(true) . '/admin/logs' );
880         return; // NOTREACHED   
881 }
882  
883 function admin_page_logs(&$a){
884         
885         $log_choices = Array(
886                 LOGGER_NORMAL => 'Normal',
887                 LOGGER_TRACE => 'Trace',
888                 LOGGER_DEBUG => 'Debug',
889                 LOGGER_DATA => 'Data',
890                 LOGGER_ALL => 'All'
891         );
892         
893         $t = get_markup_template("admin_logs.tpl");
894
895         $f = get_config('system','logfile');
896
897         $data = '';
898
899         if(!file_exists($f)) {
900                 $data = t("Error trying to open <strong>$f</strong> log file.\r\n<br/>Check to see if file $f exist and is 
901 readable.");
902         }
903         else {
904                 $fp = fopen($f, 'r');
905                 if(!$fp) {
906                         $data = t("Couldn't open <strong>$f</strong> log file.\r\n<br/>Check to see if file $f is readable.");
907                 }
908                 else {
909                         $fstat = fstat($fp);
910                         $size = $fstat['size'];
911                         if($size != 0)
912                         {
913                                 if($size > 5000000 || $size < 0)
914                                         $size = 5000000;
915                                 $seek = fseek($fp,0-$size,SEEK_END);
916                                 if($seek === 0) {
917                                         fgets($fp); // throw away the first partial line
918                                         $data = escape_tags(fread($fp,$size));
919                                         while(! feof($fp))
920                                                 $data .= escape_tags(fread($fp,4096));
921                                 }
922                         }
923                         fclose($fp);
924                 }
925         }                       
926
927         return replace_macros($t, array(
928                 '$title' => t('Administration'),
929                 '$page' => t('Logs'),
930                 '$submit' => t('Submit'),
931                 '$clear' => t('Clear'),
932                 '$data' => $data,
933                 '$baseurl' => $a->get_baseurl(true),
934                 '$logname' =>  get_config('system','logfile'),
935                 
936                                                                         // name, label, value, help string, extra data...
937                 '$debugging'            => array('debugging', t("Debugging"),get_config('system','debugging'), ""),
938                 '$logfile'                      => array('logfile', t("Log file"), get_config('system','logfile'), t("Must be writable by web server. Relative to your Friendica top-level directory.")),
939                 '$loglevel'             => array('loglevel', t("Log level"), get_config('system','loglevel'), "", $log_choices),
940         ));
941 }
942
943 function admin_page_remoteupdate_post(&$a) {
944         // this function should be called via ajax post
945         if(!is_site_admin()) {
946                 return;
947         }
948
949         
950         if (x($_POST,'remotefile') && $_POST['remotefile']!=""){
951                 $remotefile = $_POST['remotefile'];
952                 $ftpdata = (x($_POST['ftphost'])?$_POST:false);
953                 doUpdate($remotefile, $ftpdata);
954         } else {
955                 echo "No remote file to download. Abort!";
956         }
957
958         killme();
959 }
960
961 function admin_page_remoteupdate(&$a) {
962         if(!is_site_admin()) {
963                 return login(false);
964         }
965
966         $canwrite = canWeWrite();
967         $canftp = function_exists('ftp_connect');
968         
969         $needupdate = true;
970         $u = checkUpdate();
971         if (!is_array($u)){
972                 $needupdate = false;
973                 $u = array('','','');
974         }
975         
976         $tpl = get_markup_template("admin_remoteupdate.tpl");
977         return replace_macros($tpl, array(
978                 '$baseurl' => $a->get_baseurl(true),
979                 '$submit' => t("Update now"),
980                 '$close' => t("Close"),
981                 '$localversion' => FRIENDICA_VERSION,
982                 '$remoteversion' => $u[1],
983                 '$needupdate' => $needupdate,
984                 '$canwrite' => $canwrite,
985                 '$canftp'       => $canftp,
986                 '$ftphost'      => array('ftphost', t("FTP Host"), '',''),
987                 '$ftppath'      => array('ftppath', t("FTP Path"), '/',''),
988                 '$ftpuser'      => array('ftpuser', t("FTP User"), '',''),
989                 '$ftppwd'       => array('ftppwd', t("FTP Password"), '',''),
990                 '$remotefile'=>array('remotefile','', $u['2'],'')
991         ));
992         
993 }