]> git.mxchange.org Git - friendica.git/blob - mod/admin.php
store key with contact record
[friendica.git] / mod / admin.php
1 <?php
2
3  /**
4   * Friendika admin
5   */
6 require_once("include/remoteupdate.php");
7  
8 function admin_init(&$a) {
9         if(!is_site_admin()) {
10                 notice( t('Permission denied.') . EOL);
11                 return;
12         }
13 }
14
15 function admin_post(&$a){
16         if(!is_site_admin()) {
17                 return login(false);
18         }
19         
20         // urls
21         if ($a->argc > 1){
22                 switch ($a->argv[1]){
23                         case 'site':
24                                 admin_page_site_post($a);
25                                 break;
26                         case 'users':
27                                 admin_page_users_post($a);
28                                 break;
29                         case 'plugins':
30                                 if ($a->argc > 2 && 
31                                         is_file("addon/".$a->argv[2]."/".$a->argv[2].".php")){
32                                                 @include_once("addon/".$a->argv[2]."/".$a->argv[2].".php");
33                                                 if(function_exists($a->argv[2].'_plugin_admin_post')) {
34                                                         $func = $a->argv[2].'_plugin_admin_post';
35                                                         $func($a);
36                                                 }
37                                 }
38                                 goaway($a->get_baseurl() . '/admin/plugins/' . $a->argv[2] );
39                                 return; // NOTREACHED
40                                 break;
41                         case 'logs':
42                                 admin_page_logs_post($a);
43                                 break;
44                         case 'update':
45                                 admin_page_remoteupdate_post($a);
46                                 break;
47                 }
48         }
49
50         goaway($a->get_baseurl() . '/admin' );
51         return; // NOTREACHED   
52 }
53
54 function admin_content(&$a) {
55
56         if(!is_site_admin()) {
57                 return login(false);
58         }
59
60         /**
61          * Side bar links
62          */
63
64         // array( url, name, extra css classes )
65         $aside = Array(
66                 'site'   =>     Array($a->get_baseurl()."/admin/site/", t("Site") , "site"),
67                 'users'  =>     Array($a->get_baseurl()."/admin/users/", t("Users") , "users"),
68                 'plugins'=>     Array($a->get_baseurl()."/admin/plugins/", t("Plugins") , "plugins"),
69                 'update' =>     Array($a->get_baseurl()."/admin/update/", t("Update") , "update")
70         );
71         
72         /* get plugins admin page */
73         
74         $r = q("SELECT * FROM `addon` WHERE `plugin_admin`=1");
75         $aside['plugins_admin']=Array();
76         foreach ($r as $h){
77                 $plugin =$h['name'];
78                 $aside['plugins_admin'][] = Array($a->get_baseurl()."/admin/plugins/".$plugin, $plugin, "plugin");
79                 // temp plugins with admin
80                 $a->plugins_admin[] = $plugin;
81         }
82                 
83         $aside['logs'] = Array($a->get_baseurl()."/admin/logs/", t("Logs"), "logs");
84
85         $t = get_markup_template("admin_aside.tpl");
86         $a->page['aside'] = replace_macros( $t, array(
87                         '$admin' => $aside, 
88                         '$h_pending' => t('User registrations waiting for confirmation'),
89                         '$admurl'=> $a->get_baseurl()."/admin/"
90         ));
91
92
93
94         /**
95          * Page content
96          */
97         $o = '';
98         
99         // urls
100         if ($a->argc > 1){
101                 switch ($a->argv[1]){
102                         case 'site':
103                                 $o = admin_page_site($a);
104                                 break;
105                         case 'users':
106                                 $o = admin_page_users($a);
107                                 break;
108                         case 'plugins':
109                                 $o = admin_page_plugins($a);
110                                 break;
111                         case 'logs':
112                                 $o = admin_page_logs($a);
113                                 break;
114                         case 'update':
115                                 $o = admin_page_remoteupdate($a);
116                                 break;
117                         default:
118                                 notice( t("Item not found.") );
119                 }
120         } else {
121                 $o = admin_page_summary($a);
122         }
123         return $o;
124
125
126
127 /**
128  * Admin Summary Page
129  */
130 function admin_page_summary(&$a) {
131         $r = q("SELECT `page-flags`, COUNT(uid) as `count` FROM `user` GROUP BY `page-flags`");
132         $accounts = Array(
133                 Array( t('Normal Account'), 0),
134                 Array( t('Soapbox Account'), 0),
135                 Array( t('Community/Celebrity Account'), 0),
136                 Array( t('Automatic Friend Account'), 0)
137         );
138         $users=0;
139         foreach ($r as $u){ $accounts[$u['page-flags']][1] = $u['count']; $users+=$u['count']; }
140
141         
142         $r = q("SELECT COUNT(id) as `count` FROM `register`");
143         $pending = $r[0]['count'];
144         
145         
146         
147         
148         
149         $t = get_markup_template("admin_summary.tpl");
150         return replace_macros($t, array(
151                 '$title' => t('Administration'),
152                 '$page' => t('Summary'),
153                 '$users' => Array( t('Registered users'), $users),
154                 '$accounts' => $accounts,
155                 '$pending' => Array( t('Pending registrations'), $pending),
156                 '$version' => Array( t('Version'), FRIENDIKA_VERSION),
157                 '$build' =>  get_config('system','build'),
158                 '$plugins' => Array( t('Active plugins'), $a->plugins )
159         ));
160 }
161
162
163 /**
164  * Admin Site Page
165  */
166 function admin_page_site_post(&$a){
167         if (!x($_POST,"page_site")){
168                 return;
169         }
170
171         
172         $sitename                       =       ((x($_POST,'sitename'))                 ? notags(trim($_POST['sitename']))                      : '');
173         $banner                         =       ((x($_POST,'banner'))                   ? trim($_POST['banner'])                                        : false);
174         $language                       =       ((x($_POST,'language'))                 ? notags(trim($_POST['language']))                      : '');
175         $theme                          =       ((x($_POST,'theme'))                    ? notags(trim($_POST['theme']))                         : '');
176         $maximagesize           =       ((x($_POST,'maximagesize'))             ? intval(trim($_POST['maximagesize']))          :  0);
177         
178         
179         $register_policy        =       ((x($_POST,'register_policy'))  ? intval(trim($_POST['register_policy']))       :  0);
180         $register_text          =       ((x($_POST,'register_text'))    ? notags(trim($_POST['register_text']))         : '');  
181         
182         $allowed_sites          =       ((x($_POST,'allowed_sites'))    ? notags(trim($_POST['allowed_sites']))         : '');
183         $allowed_email          =       ((x($_POST,'allowed_email'))    ? notags(trim($_POST['allowed_email']))         : '');
184         $block_public           =       ((x($_POST,'block_public'))             ? True  :       False);
185         $force_publish          =       ((x($_POST,'publish_all'))              ? True  :       False);
186         $global_directory       =       ((x($_POST,'directory_submit_url'))     ? notags(trim($_POST['directory_submit_url']))  : '');
187         $no_multi_reg           =       ((x($_POST,'no_multi_reg'))             ? True  :       False);
188         $no_openid                      =       !((x($_POST,'no_openid'))               ? True  :       False);
189         $no_gravatar            =       !((x($_POST,'no_gravatar'))             ? True  :       False);
190         $no_regfullname         =       !((x($_POST,'no_regfullname'))  ? True  :       False);
191         $no_utf                         =       !((x($_POST,'no_utf'))                  ? True  :       False);
192         $no_community_page      =       !((x($_POST,'no_community_page'))       ? True  :       False);
193
194         $verifyssl                      =       ((x($_POST,'verifyssl'))                ? True  :       False);
195         $proxyuser                      =       ((x($_POST,'proxyuser'))                ? notags(trim($_POST['proxyuser']))     : '');
196         $proxy                          =       ((x($_POST,'proxy'))                    ? notags(trim($_POST['proxy'])) : '');
197         $timeout                        =       ((x($_POST,'timeout'))                  ? intval(trim($_POST['timeout']))               : 60);
198         $dfrn_only          =   ((x($_POST,'dfrn_only'))            ? True      :       False);
199     $ostatus_disabled   =   !((x($_POST,'ostatus_disabled')) ? True  :   False);
200
201
202         set_config('config','sitename',$sitename);
203         if ($banner==""){
204                 // don't know why, but del_config doesn't work...
205                 q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
206                         dbesc("system"),
207                         dbesc("banner")
208                 );
209         } else {
210                 set_config('system','banner', $banner);
211         }
212         set_config('system','language', $language);
213         set_config('system','theme', $theme);
214         set_config('system','maximagesize', $maximagesize);
215         
216         set_config('config','register_policy', $register_policy);
217         set_config('config','register_text', $register_text);
218         set_config('system','allowed_sites', $allowed_sites);
219         set_config('system','allowed_email', $allowed_email);
220         set_config('system','block_public', $block_public);
221         set_config('system','publish_all', $force_publish);
222         if ($global_directory==""){
223                 // don't know why, but del_config doesn't work...
224                 q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
225                         dbesc("system"),
226                         dbesc("directory_submit_url")
227                 );
228         } else {
229                 set_config('system','directory_submit_url', $global_directory);
230         }
231         set_config('system','directory_search_url', $global_search_url);
232         set_config('system','block_extended_register', $no_multi_reg);
233         set_config('system','no_openid', $no_openid);
234         set_config('system','no_gravatar', $no_gravatar);
235         set_config('system','no_regfullname', $no_regfullname);
236         set_config('system','no_community_page', $no_community_page);
237         set_config('system','no_utf', $no_utf);
238         set_config('system','verifyssl', $verifyssl);
239         set_config('system','proxyuser', $proxyuser);
240         set_config('system','proxy', $proxy);
241         set_config('system','curl_timeout', $timeout);
242         set_config('system','dfrn_only', $dfrn_only);
243         set_config('system','ostatus_disabled', $ostatus_disabled);
244
245         info( t('Site settings updated.') . EOL);
246         goaway($a->get_baseurl() . '/admin/site' );
247         return; // NOTREACHED   
248         
249 }
250  
251 function admin_page_site(&$a) {
252         
253         /* Installed langs */
254         $lang_choices = array();
255         $langs = glob('view/*/strings.php');
256         
257         if(is_array($langs) && count($langs)) {
258                 if(! in_array('view/en/strings.php',$langs))
259                         $langs[] = 'view/en/';
260                 asort($langs);
261                 foreach($langs as $l) {
262                         $t = explode("/",$l);
263                         $lang_choices[$t[1]] = $t[1];
264                 }
265         }
266         
267         /* Installed themes */
268         $theme_choices = array();
269         $files = glob('view/theme/*');
270         if($files) {
271                 foreach($files as $file) {
272                         $f = basename($file);
273                         $theme_name = ((file_exists($file . '/experimental')) ?  sprintf("%s - \x28Experimental\x29", $f) : $f);
274                         $theme_choices[$f] = $theme_name;
275                 }
276         }
277         
278         
279         /* Banner */
280         $banner = get_config('system','banner');
281         if($banner == false) 
282                 $banner = '<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>';
283         $banner = htmlspecialchars($banner);
284         
285         //echo "<pre>"; var_dump($lang_choices); die("</pre>");
286
287         /* Register policy */
288         $register_choices = Array(
289                 REGISTER_CLOSED => t("Closed"),
290                 REGISTER_APPROVE => t("Requires approval"),
291                 REGISTER_OPEN => t("Open")
292         ); 
293         
294         $t = get_markup_template("admin_site.tpl");
295         return replace_macros($t, array(
296                 '$title' => t('Administration'),
297                 '$page' => t('Site'),
298                 '$submit' => t('Submit'),
299                 '$registration' => t('Registration'),
300                 '$upload' => t('File upload'),
301                 '$corporate' => t('Policies'),
302                 '$advanced' => t('Advanced'),
303                 
304                 '$baseurl' => $a->get_baseurl(),
305                                                                         // name, label, value, help string, extra data...
306                 '$sitename'             => array('sitename', t("Site name"), htmlentities($a->config['sitename'], ENT_QUOTES), ""),
307                 '$banner'                       => array('banner', t("Banner/Logo"), $banner, ""),
308                 '$language'             => array('language', t("System language"), get_config('system','language'), "", $lang_choices),
309                 '$theme'                        => array('theme', t("System theme"), get_config('system','theme'), "Default system theme (which may be over-ridden by user profiles)", $theme_choices),
310
311                 '$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."),
312
313                 '$register_policy'      => array('register_policy', t("Register policy"), $a->config['register_policy'], "", $register_choices),
314                 '$register_text'        => array('register_text', t("Register text"), htmlentities($a->config['register_text'], ENT_QUOTES), "Will be displayed prominently on the registration page."),
315                 '$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"),
316                 '$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"),
317                 '$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."),
318                 '$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."),
319                 '$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."),
320                         
321                 '$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."),
322                 '$no_openid'            => array('no_openid', t("OpenID support"), !get_config('system','no_openid'), "OpenID support for registration and logins."),
323                 '$no_gravatar'          => array('no_gravatar', t("Gravatar support"), !get_config('system','no_gravatar'), "Search new user's photo on Gravatar."),
324                 '$no_regfullname'       => array('no_regfullname', t("Fullname check"), !get_config('system','no_regfullname'), "Force users to register with a space between firstname and lastname in Full name, as an antispam measure"),
325                 '$no_utf'                       => array('no_utf', t("UTF-8 Regular expressions"), !get_config('system','no_utf'), "Use PHP UTF8 regular expressions"),
326                 '$no_community_page' => array('no_community_page', t("Show Community Page"), !get_config('system','no_community_page'), "Display a Community page showing all recent public postings on this site."),
327                 '$ostatus_disabled' => array('ostatus_disabled', t("Enable OStatus support"), !get_config('system','ostatus_disable'), "Provide built-in OStatus \x28identi.ca, status.net, etc.\x29 compatibility. All communications in OStatus are public, so privacy warnings will be occasionally displayed."),    
328                 '$dfrn_only'        => array('dfrn_only', t('Only allow Friendika contacts'), get_config('system','dfrn_only'), "All contacts must use Friendika protocols. All other built-in communication protocols disabled."),
329                 '$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."),
330                 '$proxyuser'            => array('proxyuser', t("Proxy user"), get_config('system','proxyuser'), ""),
331                 '$proxy'                        => array('proxy', t("Proxy URL"), get_config('system','proxy'), ""),
332                 '$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)."),
333
334                         
335         ));
336
337 }
338
339
340 /**
341  * Users admin page
342  */
343 function admin_page_users_post(&$a){
344         $pending = ( x(£_POST, 'pending') ? $_POST['pending'] : Array() );
345         $users = ( x($_POST, 'user') ? $_POST['user'] : Array() );
346         
347         if (x($_POST,'page_users_block')){
348                 foreach($users as $uid){
349                         q("UPDATE `user` SET `blocked`=1-`blocked` WHERE `uid`=%s",
350                                 intval( $uid )
351                         );
352                 }
353                 notice( sprintf( tt("%s user blocked", "%s users blocked/unblocked", count($users)), count($users)) );
354         }
355         if (x($_POST,'page_users_delete')){
356                 require_once("include/Contact.php");
357                 foreach($users as $uid){
358                         user_remove($uid);
359                 }
360                 notice( sprintf( tt("%s user deleted", "%s users deleted", count($users)), count($users)) );
361         }
362         
363         if (x($_POST,'page_users_approve')){
364                 require_once("mod/regmod.php");
365                 foreach($pending as $hash){
366                         user_allow($hash);
367                 }
368         }
369         if (x($_POST,'page_users_deny')){
370                 require_once("mod/regmod.php");
371                 foreach($pending as $hash){
372                         user_deny($hash);
373                 }
374         }
375         goaway($a->get_baseurl() . '/admin/users' );
376         return; // NOTREACHED   
377 }
378  
379 function admin_page_users(&$a){
380         if ($a->argc>2) {
381                 $uid = $a->argv[3];
382                 $user = q("SELECT * FROM `user` WHERE `uid`=%d", intval($uid));
383                 if (count($user)==0){
384                         notice( 'User not found' . EOL);
385                         goaway($a->get_baseurl() . '/admin/users' );
386                         return; // NOTREACHED                                           
387                 }               
388                 switch($a->argv[2]){
389                         case "delete":{
390                                 // delete user
391                                 require_once("include/Contact.php");
392                                 user_remove($uid);
393                                 
394                                 notice( sprintf(t("User '%s' deleted"), $user[0]['username']) . EOL);
395                         }; break;
396                         case "block":{
397                                 q("UPDATE `user` SET `blocked`=%d WHERE `uid`=%s",
398                                         intval( 1-$user[0]['blocked'] ),
399                                         intval( $uid )
400                                 );
401                                 notice( sprintf( ($user[0]['blocked']?t("User '%s' unblocked"):t("User '%s' blocked")) , $user[0]['username']) . EOL);
402                         }; break;
403                 }
404                 goaway($a->get_baseurl() . '/admin/users' );
405                 return; // NOTREACHED   
406                 
407         }
408         
409         /* get pending */
410         $pending = q("SELECT `register`.*, `contact`.`name`, `user`.`email`
411                                  FROM `register`
412                                  LEFT JOIN `contact` ON `register`.`uid` = `contact`.`uid`
413                                  LEFT JOIN `user` ON `register`.`uid` = `user`.`uid`;");
414         
415         /* get users */
416
417         $total = q("SELECT count(*) as total FROM `user` where 1");
418         if(count($total)) {
419                 $a->set_pager_total($total[0]['total']);
420                 $a->set_pager_itemspage(100);
421         }
422
423         $users = q("SELECT `user` . * , `contact`.`name` , `contact`.`url` , `contact`.`micro`, `lastitem`.`lastitem_date`
424                                 FROM
425                                         (SELECT MAX(`item`.`changed`) as `lastitem_date`, `item`.`uid`
426                                         FROM `item`
427                                         WHERE `item`.`type` = 'wall'
428                                         GROUP BY `item`.`uid`) AS `lastitem`
429                                                  RIGHT OUTER JOIN `user` ON `user`.`uid` = `lastitem`.`uid`,
430                                            `contact`
431                                 WHERE
432                                            `user`.`uid` = `contact`.`uid`
433                                                 AND `user`.`verified` =1
434                                         AND `contact`.`self` =1
435                                 ORDER BY `contact`.`name` LIMIT %d, %d
436                                 ",
437                                 intval($a->pager['start']),
438                                 intval($a->pager['itemspage'])
439                                 );
440                                         
441         function _setup_users($e){
442                 $accounts = Array(
443                         t('Normal Account'), 
444                         t('Soapbox Account'),
445                         t('Community/Celebrity Account'),
446                         t('Automatic Friend Account')
447                 );
448                 $e['page-flags'] = $accounts[$e['page-flags']];
449                 $e['register_date'] = relative_date($e['register_date']);
450                 $e['login_date'] = relative_date($e['login_date']);
451                 $e['lastitem_date'] = relative_date($e['lastitem_date']);
452                 return $e;
453         }
454         $users = array_map("_setup_users", $users);
455         
456         $t = get_markup_template("admin_users.tpl");
457         $o = replace_macros($t, array(
458                 // strings //
459                 '$title' => t('Administration'),
460                 '$page' => t('Users'),
461                 '$submit' => t('Submit'),
462                 '$select_all' => t('select all'),
463                 '$h_pending' => t('User registrations waiting for confirm'),
464                 '$th_pending' => array( t('Request date'), t('Name'), t('Email') ),
465                 '$no_pending' =>  t('No registrations.'),
466                 '$approve' => t('Approve'),
467                 '$deny' => t('Deny'),
468                 '$delete' => t('Delete'),
469                 '$block' => t('Block'),
470                 '$unblock' => t('Unblock'),
471                 
472                 '$h_users' => t('Users'),
473                 '$th_users' => array( t('Name'), t('Email'), t('Register date'), t('Last login'), t('Last item'),  t('Account') ),
474
475                 '$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?'),
476                 '$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?'),
477
478
479                 // values //
480                 '$baseurl' => $a->get_baseurl(),
481
482                 '$pending' => $pending,
483                 '$users' => $users,
484         ));
485         $o .= paginate($a);
486         return $o;
487 }
488
489
490 /*
491  * Plugins admin page
492  */
493
494 function admin_page_plugins(&$a){
495         
496         /**
497          * Single plugin
498          */
499         if ($a->argc == 3){
500                 $plugin = $a->argv[2];
501                 if (!is_file("addon/$plugin/$plugin.php")){
502                         notice( t("Item not found.") );
503                         return;
504                 }
505                 
506                 if (x($_GET,"a") && $_GET['a']=="t"){
507                         // Toggle plugin status
508                         $idx = array_search($plugin, $a->plugins);
509                         if ($idx){
510                                 unset($a->plugins[$idx]);
511                                 uninstall_plugin($plugin);
512                                 info( sprintf( t("Plugin %s disabled."), $plugin ) );
513                         } else {
514                                 $a->plugins[] = $plugin;
515                                 install_plugin($plugin);
516                                 info( sprintf( t("Plugin %s enabled."), $plugin ) );
517                         }
518                         set_config("system","addon", implode(", ",$a->plugins));
519                         goaway($a->get_baseurl() . '/admin/plugins' );
520                         return; // NOTREACHED   
521                 }
522                 // display plugin details
523                 require_once('library/markdown.php');
524
525                 if (in_array($plugin, $a->plugins)){
526                         $status="on"; $action= t("Disable");
527                 } else {
528                         $status="off"; $action= t("Enable");
529                 }
530                 
531                 $readme=Null;
532                 if (is_file("addon/$plugin/README.md")){
533                         $readme = file_get_contents("addon/$plugin/README.md");
534                         $readme = Markdown($readme);
535                 } else if (is_file("addon/$plugin/README")){
536                         $readme = "<pre>". file_get_contents("addon/$plugin/README") ."</pre>";
537                 } 
538                 
539                 $admin_form="";
540                 if (in_array($plugin, $a->plugins_admin)){
541                         @require_once("addon/$plugin/$plugin.php");
542                         $func = $plugin.'_plugin_admin';
543                         $func($a, $admin_form);
544                 }
545                 
546                 $t = get_markup_template("admin_plugins_details.tpl");
547                 return replace_macros($t, array(
548                         '$title' => t('Administration'),
549                         '$page' => t('Plugins'),
550                         '$toggle' => t('Toggle'),
551                         '$settings' => t('Settings'),
552                         '$baseurl' => $a->get_baseurl(),
553                 
554                         '$plugin' => $plugin,
555                         '$status' => $status,
556                         '$action' => $action,
557                         '$info' => get_plugin_info($plugin),
558                 
559                         '$admin_form' => $admin_form,
560                         
561                         '$readme' => $readme
562                 ));
563         } 
564          
565          
566         
567         /**
568          * List plugins
569          */
570         
571         $plugins = array();
572         $files = glob("addon/*/");
573         if($files) {
574                 foreach($files as $file) {      
575                         if (is_dir($file)){
576                                 list($tmp, $id)=array_map("trim", explode("/",$file));
577                                 $info = get_plugin_info($id);
578                                 $plugins[] = array( $id, (in_array($id,  $a->plugins)?"on":"off") , $info);
579                         }
580                 }
581         }
582         
583         $t = get_markup_template("admin_plugins.tpl");
584         return replace_macros($t, array(
585                 '$title' => t('Administration'),
586                 '$page' => t('Plugins'),
587                 '$submit' => t('Submit'),
588                 '$baseurl' => $a->get_baseurl(),
589         
590                 '$plugins' => $plugins
591         ));
592 }
593
594
595 /**
596  * Logs admin page
597  */
598  
599 function admin_page_logs_post(&$a) {
600         if (x($_POST,"page_logs")) {
601
602                 $logfile                =       ((x($_POST,'logfile'))          ? notags(trim($_POST['logfile']))       : '');
603                 $debugging              =       ((x($_POST,'debugging'))        ? true                                                          : false);
604                 $loglevel               =       ((x($_POST,'loglevel'))         ? intval(trim($_POST['loglevel']))      : 0);
605
606                 set_config('system','logfile', $logfile);
607                 set_config('system','debugging',  $debugging);
608                 set_config('system','loglevel', $loglevel);
609
610                 
611         }
612
613         info( t("Log settings updated.") );
614         goaway($a->get_baseurl() . '/admin/logs' );
615         return; // NOTREACHED   
616 }
617  
618 function admin_page_logs(&$a){
619         
620         $log_choices = Array(
621                 LOGGER_NORMAL => 'Normal',
622                 LOGGER_TRACE => 'Trace',
623                 LOGGER_DEBUG => 'Debug',
624                 LOGGER_DATA => 'Data',
625                 LOGGER_ALL => 'All'
626         );
627         
628         $t = get_markup_template("admin_logs.tpl");
629
630         $f = get_config('system','logfile');
631         $size = filesize($f);
632         if($size > 5000000)
633                 $size = 5000000;
634
635         $data = '';
636         $fp = fopen($f,'r');
637         if($fp) {
638                 $seek = fseek($fp,0-$size,SEEK_END);
639                 if($seek === 0) {
640                         fgets($fp); // throw away the first partial line
641                         $data = escape_tags(fread($fp,$size));
642                         while(! feof($fp))
643                                 $data .= escape_tags(fread($fp,4096));
644                 }
645                 fclose($fp);
646         }
647
648
649         return replace_macros($t, array(
650                 '$title' => t('Administration'),
651                 '$page' => t('Logs'),
652                 '$submit' => t('Submit'),
653                 '$clear' => t('Clear'),
654                 '$data' => $data,
655                 '$baseurl' => $a->get_baseurl(),
656                 '$logname' =>  get_config('system','logfile'),
657                 
658                                                                         // name, label, value, help string, extra data...
659                 '$debugging'            => array('debugging', t("Debugging"),get_config('system','debugging'), ""),
660                 '$logfile'                      => array('logfile', t("Log file"), get_config('system','logfile'), t("Must be writable by web server. Relative to your Friendika index.php.")),
661                 '$loglevel'             => array('loglevel', t("Log level"), get_config('system','loglevel'), "", $log_choices),
662         ));
663 }
664
665 function admin_page_remoteupdate_post(&$a) {
666         // this function should be called via ajax post
667         if(!is_site_admin()) {
668                 return login(false);
669         }
670
671         
672         if (x($_POST,'remotefile') && $_POST['remotefile']!=""){
673                 $remotefile = $_POST['remotefile'];
674                 $ftpdata = (x($_POST['ftphost'])?$_POST:false);
675                 doUpdate($remotefile, $ftpdata);
676         } else {
677                 echo "No remote file to download. Abort!";
678         }
679
680         killme();
681 }
682
683 function admin_page_remoteupdate(&$a) {
684         if(!is_site_admin()) {
685                 return login(false);
686         }
687
688         $canwrite = canWeWrite();
689         $canftp = function_exists('ftp_connect');
690         
691         $needupdate = true;
692         $u = checkUpdate();
693         if (!is_array($u)){
694                 $needupdate = false;
695                 $u = array('','','');
696         }
697         
698         $tpl = get_markup_template("admin_remoteupdate.tpl");
699         return replace_macros($tpl, array(
700                 '$baseurl' => $a->get_baseurl(),
701                 '$submit' => t("Update now"),
702                 '$close' => t("Close"),
703                 '$localversion' => FRIENDIKA_VERSION,
704                 '$remoteversion' => $u[1],
705                 '$needupdate' => $needupdate,
706                 '$canwrite' => $canwrite,
707                 '$canftp'       => $canftp,
708                 '$ftphost'      => array('ftphost', t("FTP Host"), '',''),
709                 '$ftppath'      => array('ftppath', t("FTP Path"), '/',''),
710                 '$ftpuser'      => array('ftpuser', t("FTP User"), '',''),
711                 '$ftppwd'       => array('ftppwd', t("FTP Password"), '',''),
712                 '$remotefile'=>array('remotefile','', $u['2'],'')
713         ));
714         
715 }