]> git.mxchange.org Git - friendica.git/blob - mod/admin.php
Merge pull request #117 from tobiasd/master
[friendica.git] / mod / admin.php
1 <?php
2  /**
3   * Friendika admin
4   */
5   
6  
7 function admin_init(&$a) {
8         if(!is_site_admin()) {
9                 notice( t('Permission denied.') . EOL);
10                 return;
11         }
12 }
13
14 function admin_post(&$a){
15         if(!is_site_admin()) {
16                 return login(false);
17         }
18         
19         // urls
20         if ($a->argc > 1){
21                 switch ($a->argv[1]){
22                         case 'site':
23                                 admin_page_site_post($a);
24                                 break;
25                         case 'users':
26                                 admin_page_users_post($a);
27                                 break;                          
28                         case 'logs':
29                                 admin_page_logs_post($a);
30                                 break;
31                 }
32         }
33
34         goaway($a->get_baseurl() . '/admin' );
35         return; // NOTREACHED   
36 }
37
38 function admin_content(&$a) {
39
40         if(!is_site_admin()) {
41                 return login(false);
42         }
43
44         /**
45          * Side bar links
46          */
47
48         // array( url, name, extra css classes )
49         $aside = Array(
50                 'site'   =>     Array($a->get_baseurl()."/admin/site/", t("Site") , "site"),
51                 'users'  =>     Array($a->get_baseurl()."/admin/users/", t("Users") , "users"),
52                 'plugins'=>     Array($a->get_baseurl()."/admin/plugins/", t("Plugins") , "plugins")
53         );
54         
55         /* get plugins admin page */
56         
57         $r = q("SELECT * FROM `hook` WHERE `hook`='plugin_admin'");
58         $aside['plugins_admin']=Array();
59         foreach ($r as $h){
60                 $plugin = explode("/",$h['file']); $plugin = $plugin[1];
61                 $aside['plugins_admin'][] = Array($a->get_baseurl()."/admin/plugins/".$plugin, $plugin, "plugin");
62         }
63                 
64         $aside['logs'] = Array($a->get_baseurl()."/admin/logs/", t("Logs"), "logs");
65
66         $t = get_markup_template("admin_aside.tpl");
67         $a->page['aside'] = replace_macros( $t, array(
68                         '$admin' => $aside, 
69                         '$h_pending' => t('User registrations waiting for confirm'),
70                         '$admurl'=> $a->get_baseurl()."/admin/"
71         ));
72
73
74
75         /**
76          * Page content
77          */
78         $o = '';
79         
80         // urls
81         if ($a->argc > 1){
82                 switch ($a->argv[1]){
83                         case 'site':
84                                 $o = admin_page_site($a);
85                                 break;
86                         case 'users':
87                                 $o = admin_page_users($a);
88                                 break;
89                         case 'plugins':
90                                 $o = admin_page_plugins($a);
91                                 break;
92                         case 'logs':
93                                 $o = admin_page_logs($a);
94                                 break;                          
95                         default:
96                                 notice( t("Item not found.") );
97                 }
98         } else {
99                 $o = admin_page_summary($a);
100         }
101         return $o;
102
103
104
105 /**
106  * Admin Summary Page
107  */
108 function admin_page_summary(&$a) {
109         $r = q("SELECT `page-flags`, COUNT(uid) as `count` FROM `user` GROUP BY `page-flags`");
110         $accounts = Array(
111                 Array( t('Normal Account'), 0),
112                 Array( t('Soapbox Account'), 0),
113                 Array( t('Community/Celebrity Account'), 0),
114                 Array( t('Automatic Friend Account'), 0)
115         );
116         $users=0;
117         foreach ($r as $u){ $accounts[$u['page-flags']][1] = $u['count']; $users+=$u['count']; }
118
119         
120         $r = q("SELECT COUNT(id) as `count` FROM `register`");
121         $pending = $r[0]['count'];
122         
123         
124         
125         
126         
127         $t = get_markup_template("admin_summary.tpl");
128         return replace_macros($t, array(
129                 '$title' => t('Administration'),
130                 '$page' => t('Summary'),
131                 '$users' => Array( t('Registered users'), $users),
132                 '$accounts' => $accounts,
133                 '$pending' => Array( t('Pending registrations'), $pending),
134                 '$version' => Array( t('Version'), FRIENDIKA_VERSION),
135                 '$build' =>  get_config('system','build'),
136                 '$plugins' => Array( t('Active plugins'), $a->plugins )
137         ));
138 }
139
140
141 /**
142  * Admin Site Page
143  */
144 function admin_page_site_post(&$a){
145         if (!x($_POST,"page_site")){
146                 return;
147         }
148
149         
150         $sitename                       =       ((x($_POST,'sitename'))                 ? notags(trim($_POST['sitename']))                      : '');
151         $banner                         =       ((x($_POST,'banner'))                   ? trim($_POST['banner'])                                        : false);
152         $language                       =       ((x($_POST,'language'))                 ? notags(trim($_POST['language']))                      : '');
153         $theme                          =       ((x($_POST,'theme'))                    ? notags(trim($_POST['theme']))                         : '');
154         $maximagesize           =       ((x($_POST,'maximagesize'))             ? intval(trim($_POST['maximagesize']))          :  0);
155         
156         
157         $register_policy        =       ((x($_POST,'register_policy'))  ? intval(trim($_POST['register_policy']))       :  0);
158         $register_text          =       ((x($_POST,'register_text'))    ? notags(trim($_POST['register_text']))         : '');  
159         
160         $allowed_sites          =       ((x($_POST,'allowed_sites'))    ? notags(trim($_POST['allowed_sites']))         : '');
161         $allowed_email          =       ((x($_POST,'allowed_email'))    ? notags(trim($_POST['allowed_email']))         : '');
162         $block_public           =       ((x($_POST,'block_public'))             ? True  :       False);
163         $force_publish          =       ((x($_POST,'publish_all'))              ? True  :       False);
164         $global_directory       =       ((x($_POST,'directory_submit_url'))     ? notags(trim($_POST['directory_submit_url']))  : '');
165         $global_search_url      =       ((x($_POST,'directory_search_url'))? notags(trim($_POST['directory_search_url']))       : '');
166         $no_multi_reg           =       ((x($_POST,'no_multi_reg'))             ? True  :       False);
167         $no_openid                      =       ((x($_POST,'no_openid'))                ? True  :       False);
168         $no_gravatar            =       ((x($_POST,'no_gravatar'))              ? True  :       False);
169         $no_regfullname         =       ((x($_POST,'no_regfullname'))   ? True  :       False);
170         $no_utf                         =       ((x($_POST,'no_utf'))                   ? True  :       False);
171         $rino_enc                       =       ((x($_POST,'rino_enc'))                 ? True  :       False);
172         $verifyssl                      =       ((x($_POST,'verifyssl'))                ? True  :       False);
173         $proxyuser                      =       ((x($_POST,'proxyuser'))                ? notags(trim($_POST['global_search_url']))     : '');
174         $proxy                          =       ((x($_POST,'proxy'))                    ? notags(trim($_POST['global_search_url']))     : '');
175         $timeout                        =       ((x($_POST,'timeout'))                  ? intval(trim($_POST['timeout']))               : 60);
176
177
178         set_config('config','sitename',$sitename);
179         if ($banner==""){
180                 // don't know why, but del_config doesn't work...
181                 q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
182                         dbesc("system"),
183                         dbesc("banner")
184                 );
185         } else {
186                 set_config('system','banner', $banner);
187         }
188         set_config('system','language', $language);
189         set_config('system','theme', $theme);
190         set_config('system','maximagesize', $maximagesize);
191         
192         set_config('config','register_policy', $register_policy);
193         set_config('config','register_text', $register_text);
194         set_config('system','allowed_sites', $allowed_sites);
195         set_config('system','allowed_email', $allowed_email);
196         set_config('system','block_public', $block_public);
197         set_config('system','publish_all', $force_publish);
198         if ($global_directory==""){
199                 // don't know why, but del_config doesn't work...
200                 q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
201                         dbesc("system"),
202                         dbesc("directory_submit_url")
203                 );
204         } else {
205                 set_config('system','directory_submit_url', $global_directory);
206         }
207         set_config('system','directory_search_url', $global_search_url);
208         set_config('system','block_extended_register', $no_multi_reg);
209         set_config('system','no_openid', $no_openid);
210         set_config('system','no_gravatar', $no_gravatar);
211         set_config('system','no_regfullname', $no_regfullname);
212         set_config('system','proxy', $no_utf);
213         set_config('system','rino_encrypt', $rino_enc);
214         set_config('system','verifyssl', $verifyssl);
215         set_config('system','proxyuser', $proxyuser);
216         set_config('system','proxy', $proxy);
217         set_config('system','curl_timeout', $timeout);
218
219
220         goaway($a->get_baseurl() . '/admin/site' );
221         return; // NOTREACHED   
222         
223 }
224  
225 function admin_page_site(&$a) {
226         
227         /* Installed langs */
228         $lang_choices = array();
229         $langs = glob('view/*/strings.php');
230         
231         if(is_array($langs) && count($langs)) {
232                 if(! in_array('view/en/strings.php',$langs))
233                         $langs[] = 'view/en/';
234                 asort($langs);
235                 foreach($langs as $l) {
236                         $t = explode("/",$l);
237                         $lang_choices[$t[1]] = $t[1];
238                 }
239         }
240         
241         /* Installed themes */
242         $theme_choices = array();
243         $files = glob('view/theme/*');
244         if($files) {
245                 foreach($files as $file) {
246                         $f = basename($file);
247                         $theme_name = ((file_exists($file . '/experimental')) ?  sprintf("%s - \x28Experimental\x29", $f) : $f);
248                         $theme_choices[$f] = $theme_name;
249                 }
250         }
251         
252         
253         /* Banner */
254         $banner = get_config('system','banner');
255         if($banner == false) 
256                 $banner = htmlspecialchars('<a href="http://project.friendika.com"><img id="logo-img" src="images/friendika-32.png" alt="logo" /></a><span id="logo-text"><a href="http://project.friendika.com">Friendika</a></span>');
257         
258         //echo "<pre>"; var_dump($lang_choices); die("</pre>");
259
260         /* Register policy */
261         $register_choices = Array(
262                 REGISTER_CLOSED => t("Closed"),
263                 REGISTER_APPROVE => t("Need approvation"),
264                 REGISTER_OPEN => t("Open")
265         ); 
266         
267         $t = get_markup_template("admin_site.tpl");
268         return replace_macros($t, array(
269                 '$title' => t('Administration'),
270                 '$page' => t('Site'),
271                 '$submit' => t('Submit'),
272                 '$baseurl' => $a->get_baseurl(),
273                                                 
274                                                                         // name, label, value, help string, extra data...
275                 '$sitename'             => array('sitename', t("Site name"), $a->config['sitename'], ""),
276                 '$banner'                       => array('banner', t("Banner/Logo"), $banner, ""),
277                 '$language'             => array('language', t("System language"), get_config('system','language'), "", $lang_choices),
278                 '$theme'                        => array('theme', t("System theme"), get_config('system','theme'), "Default system theme (which may be over-ridden by user profiles)", $theme_choices),
279
280                 '$maximagesize'         => array('maximagesize', t("Maximum image size"), get_config('system','maximagesize'), "Maximum size in bytes of uploaded images. Default is 0, which means no limits."),
281
282                 '$register_policy'      => array('register_policy', t("Register policy"), $a->config['register_policy'], "", $register_choices),
283                 '$register_text'        => array('register_text', t("Register text"), $a->config['register_text'], "Will be displayed prominently on the registration page."),
284                 '$allowed_sites'        => array('allowed_sites', t("Allowed friend domains"), get_config('system','allowed_sites'), "Comma separated list of domains which are allowed to establish friendships with this site. Wildcards are accepted. Empty to allow any domains"),
285                 '$allowed_email'        => array('allowed_email', t("Allowed email domains"), get_config('system','allowed_email'), "Comma separated list of domains which are allowed in email addresses for registrations to this site. Wildcards are accepted. Empty to allow any domains"),
286                 '$block_public'         => array('block_public', t("Block public"), get_config('system','block_public'), "Check to block public access to all otherwise public personal pages on this site unless you are currently logged in."),
287                 '$force_publish'        => array('publish_all', t("Force publish"), get_config('system','publish_all'), "Check to force all profiles on this site to be listed in the site directory."),
288                 '$global_directory'     => array('directory_submit_url', t("Global directory update URL"), get_config('system','directory_submit_url'), "URL to update the global directory. If this is not set, the global directory is completely unavailable to the application."),
289                 '$global_search_url'=> array('directory_search_url', t("Global directory search URL"), get_config('system','directory_search_url'), ""),
290                         
291                         
292                 '$no_multi_reg'         => array('no_multi_reg', t("Block multiple registrations"),  get_config('system','block_extended_register'), "Disallow users to register additional accounts for use as pages."),
293                 '$no_openid'            => array('no_openid', t("No OpenID support"), get_config('system','no_openid'), "Disable OpenID support for registration and logins."),
294                 '$no_gravatar'          => array('no_gravatar', t("No Gravatar support"), get_config('system','no_gravatar'), ""),
295                 '$no_regfullname'       => array('no_regfullname', t("No fullname check"), get_config('system','no_regfullname'), "If unchecked, force users to registrate with a space between his firsname and lastname in Full name, as an antispam measure"),
296                 '$no_utf'                       => array('no_utf', t("No UTF-8 Regular expressions"), get_config('system','proxy'), "Default is false (meaning UTF8 regular expressions are supported and working)"),
297                         
298                 '$rino_enc'                     => array('rino_enc', t("Enable Rino encrypt"), get_config('system','rino_encrypt'),""),
299                 '$verifyssl'            => array('verifyssl', t("Verify SSL"), get_config('system','verifyssl'), "If you wish, you can turn on strict certificate checking. This will mean you cannot connect (at all) to self-signed SSL sites."),
300                 '$proxyuser'            => array('proxyuser', t("Proxy user"), get_config('system','proxyuser'), ""),
301                 '$proxy'                        => array('proxy', t("Proxy URL"), get_config('system','proxy'), ""),
302                 '$timeout'                      => array('timeout', t("Network timeout"), (x(get_config('system','curl_timeout'))?get_config('system','curl_timeout'):60), "Value is in seconds. Set to 0 for unlimited (not recommended)."),
303
304                         
305         ));
306
307 }
308
309
310 /**
311  * Users admin page
312  */
313 function admin_page_users_post(&$a){
314         $users=array(); $pending=array();
315         foreach($_POST as $k=>$v){
316                 if (substr($k,0,5)=="user_") $users[] = substr($k,5,strlen($k)-5);
317                 if (substr($k,0,8)=="pending_") $users[] = substr($k,8,strlen($k)-8);
318         }
319         
320         if (x($_POST,'page_users_block')){
321                 foreach($users as $uid){
322                         q("UPDATE `user` SET `blocked`=1-`blocked` WHERE `uid`=%s",
323                                 intval( $uid )
324                         );
325                 }
326                 notice( sprintf( tt("%s user blocked", "%s users blocked", count($users)), count($users)) );
327         }
328         if (x($_POST,'page_users_delete')){
329                 require_once("include/Contact.php");
330                 foreach($users as $uid){
331                         user_remove($uid);
332                 }
333                 notice( sprintf( tt("%s user deleted", "%s users deleted", count($users)), count($users)) );
334         }
335         
336         if (x($_POST,'page_users_approve')){
337                 require_once("include/regmod.php");
338                 foreach($pending as $hash){
339                         user_allow($hash);
340                 }
341         }
342         if (x($_POST,'page_users_deny')){
343                 require_once("include/regmod.php");
344                 foreach($pending as $hash){
345                         user_deny($hash);
346                 }
347         }
348         goaway($a->get_baseurl() . '/admin/users' );
349         return; // NOTREACHED   
350 }
351  
352 function admin_page_users(&$a){
353         if ($a->argc>2) {
354                 $uid = $a->argv[3];
355                 $user = q("SELECT * FROM `user` WHERE `uid`=%d", intval($uid));
356                 if (count($user)==0){
357                         notice( 'User not found' . EOL);
358                         goaway($a->get_baseurl() . '/admin/users' );
359                         return; // NOTREACHED                                           
360                 }               
361                 switch($a->argv[2]){
362                         case "delete":{
363                                 // delete user
364                                 require_once("include/Contact.php");
365                                 user_remove($uid);
366                                 
367                                 notice( sprintf(t("User '%s' deleted"), $user[0]['username']) . EOL);
368                         }; break;
369                         case "block":{
370                                 q("UPDATE `user` SET `blocked`=%d WHERE `uid`=%s",
371                                         intval( 1-$user[0]['blocked'] ),
372                                         intval( $uid )
373                                 );
374                         }; break;
375                 }
376                 goaway($a->get_baseurl() . '/admin/users' );
377                 return; // NOTREACHED   
378                 
379         }
380         
381         /* get pending */
382         $pending = q("SELECT `register`.*, `contact`.`name`, `user`.`email`
383                                  FROM `register`
384                                  LEFT JOIN `contact` ON `register`.`uid` = `contact`.`uid`
385                                  LEFT JOIN `user` ON `register`.`uid` = `user`.`uid`;");
386         
387         /* get users */
388         $users = q("SELECT `user` . * , `contact`.`name` , `contact`.`url` , `contact`.`micro` , `lastitem`.`changed` AS `lastitem_date`
389                                 FROM (
390                                         SELECT `item`.`changed` , `item`.`uid`
391                                         FROM `item`
392                                         GROUP BY `uid`
393                                         ORDER BY `item`.`changed`
394                                 ) AS `lastitem` , `user`
395                                 LEFT JOIN `contact` ON `user`.`uid` = `contact`.`uid`
396                                 WHERE `user`.`verified` =1
397                                 AND `contact`.`self` =1
398                                 AND `lastitem`.`uid` = `user`.`uid`
399                                 ORDER BY `contact`.`name`
400                                 ");
401                                         
402         function _setup_users($e){
403                 $accounts = Array(
404                         t('Normal Account'), 
405                         t('Soapbox Account'),
406                         t('Community/Celebrity Account'),
407                         t('Automatic Friend Account')
408                 );
409                 $e['page-flags'] = $accounts[$e['page-flags']];
410                 $e['register_date'] = relative_date($e['register_date']);
411                 $e['login_date'] = relative_date($e['login_date']);
412                 $e['lastitem_date'] = relative_date($e['lastitem_date']);
413                 return $e;
414         }
415         $users = array_map("_setup_users", $users);
416         
417         $t = get_markup_template("admin_users.tpl");
418         return replace_macros($t, array(
419                 // strings //
420                 '$title' => t('Administration'),
421                 '$page' => t('Users'),
422                 '$submit' => t('Submit'),
423                 '$select_all' => t('select all'),
424                 '$h_pending' => t('User registrations waiting for confirm'),
425                 '$th_pending' => array( t('Request date'), t('Name'), t('Email') ),
426                 '$no_pending' =>  t('No registrations.'),
427                 '$approve' => t('Approve'),
428                 '$deny' => t('Deny'),
429                 '$delete' => t('Delete'),
430                 '$block' => t('Block'),
431                 '$unblock' => t('Unblock'),
432                 
433                 '$h_users' => t('Users'),
434                 '$th_users' => array( t('Name'), t('Email'), t('Register date'), t('Last login'), t('Last item'), t('Account') ),
435
436                 '$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?'),
437                 '$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?'),
438
439
440                 // values //
441                 '$baseurl' => $a->get_baseurl(),
442
443                 '$pending' => $pending,
444                 '$users' => $users,
445         ));
446         
447 }
448
449
450 /*
451  * Plugins admin page
452  */
453
454 function admin_page_plugins(&$a){
455         
456         /**
457          * Single plugin
458          */
459         if ($a->argc == 3){
460                 $plugin = $a->argv[2];
461                 if (!is_file("addon/$plugin/$plugin.php")){
462                         notice( t("Item not found.") );
463                         return;
464                 }
465                 
466                 if (x($_GET,"a") && $_GET['a']=="t"){
467                         // Toggle plugin status
468                         $idx = array_search($plugin, $a->plugins);
469                         if ($idx){
470                                 unset($a->plugins[$idx]);
471                                 uninstall_plugin($plugin);
472                         } else {
473                                 $a->plugins[] = $plugin;
474                                 install_plugin($plugin);
475                         }
476                         set_config("system","addon", implode(", ",$a->plugins));
477                         goaway($a->get_baseurl() . '/admin/plugins' );
478                         return; // NOTREACHED   
479                 }
480                 // display plugin details
481                 require_once('library/markdown.php');
482
483                 if (in_array($plugin, $a->plugins)){
484                         $status="on"; $action= t("Disable");
485                 } else {
486                         $status="off"; $action= t("Enable");
487                 }
488                 
489                 $readme=Null;
490                 if (is_file("addon/$plugin/README.md")){
491                         $readme = file_get_contents("addon/$plugin/README.md");
492                         $readme = Markdown($readme);
493                 } else if (is_file("addon/$plugin/README")){
494                         $readme = "<pre>". file_get_contents("addon/$plugin/README") ."</pre>";
495                 } 
496                 
497                 $t = get_markup_template("admin_plugins_details.tpl");
498                 return replace_macros($t, array(
499                         '$title' => t('Administration'),
500                         '$page' => t('Plugins'),
501                         '$toggle' => t('Toggle'),
502                         '$baseurl' => $a->get_baseurl(),
503                 
504                         '$plugin' => $plugin,
505                         '$status' => $status,
506                         '$action' => $action,
507                         '$info' => get_plugin_info($plugin),
508                         
509                         '$readme' => $readme
510                 ));
511         } 
512          
513          
514         
515         /**
516          * List plugins
517          */
518         
519         $plugins = array();
520         $files = glob("addon/*/");
521         if($files) {
522                 foreach($files as $file) {      
523                         if (is_dir($file)){
524                                 list($tmp, $id)=array_map("trim", explode("/",$file));
525                                 $info = get_plugin_info($id);
526                                 $plugins[] = array( $id, (in_array($id,  $a->plugins)?"on":"off") , $info);
527                         }
528                 }
529         }
530         
531         $t = get_markup_template("admin_plugins.tpl");
532         return replace_macros($t, array(
533                 '$title' => t('Administration'),
534                 '$page' => t('Plugins'),
535                 '$submit' => t('Submit'),
536                 '$baseurl' => $a->get_baseurl(),
537         
538                 '$plugins' => $plugins
539         ));
540 }
541
542
543 /**
544  * Logs admin page
545  */
546  
547 function admin_page_logs_post(&$a) {
548         if (x($_POST,"page_logs")) {
549
550                 $logfile                =       ((x($_POST,'logfile'))          ? notags(trim($_POST['logfile']))       : '');
551                 $debugging              =       ((x($_POST,'debugging'))        ? true                                                          : false);
552                 $loglevel               =       ((x($_POST,'loglevel'))         ? intval(trim($_POST['loglevel']))      : 0);
553
554                 set_config('system','logfile', $logfile);
555                 set_config('system','debugging',  $debugging);
556                 set_config('system','loglevel', $loglevel);
557
558                 
559         }
560
561         goaway($a->get_baseurl() . '/admin/logs' );
562         return; // NOTREACHED   
563 }
564  
565 function admin_page_logs(&$a){
566         
567         $log_choices = Array(
568                 LOGGER_NORMAL => 'Normal',
569                 LOGGER_TRACE => 'Trace',
570                 LOGGER_DEBUG => 'Debug',
571                 LOGGER_DATA => 'Data',
572                 LOGGER_ALL => 'All'
573         );
574         
575         $t = get_markup_template("admin_logs.tpl");
576         return replace_macros($t, array(
577                 '$title' => t('Administration'),
578                 '$page' => t('Logs'),
579                 '$submit' => t('Submit'),
580                 '$clear' => t('Clear'),
581                 '$baseurl' => $a->get_baseurl(),
582                 '$logname' =>  get_config('system','logfile'),
583                 
584                                                                         // name, label, value, help string, extra data...
585                 '$debugging'            => array('debugging', t("Debugging"),get_config('system','debugging'), ""),
586                 '$logfile'                      => array('logfile', t("Log file"), get_config('system','logfile'), "Must be writable by web server. Relative to your Friendika index.php."),
587                 '$loglevel'             => array('loglevel', t("Log level"), get_config('system','loglevel'), "", $log_choices),
588         ));
589 }
590