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