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