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