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