root/devel/mod/elggadmin/lib/elggadmin.inc.php

Revision 1612, 19.9 kB (checked in by misja, 5 months ago)

Applied attachment:ticket:382:elggadmin.diff, fixes #382

  • Property svn:eol-style set to native
Line 
1 <?php
2 /**
3  * Elgg administrator plugin
4  *
5  * @copyright Copyright (c) 2007 Pro Soft Resources Inc. http://www.prosoftpeople.com
6  * @author Rolando Espinoza La Fuente <rho@prosoftpeople.com>
7  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8  */
9
10 function elggadmin_actions() {
11     // do actions
12     $page = elggadmin_currentpage();
13
14     if (pages_is_submitted()) {
15         elggadmin_save($page);
16     }
17 }
18
19 function elggadmin_page($page_name=null) {
20     // this must go somewhere else!
21     global $metatags;
22     $metatags .= "\n<style type=\"text/css\">\n";
23     $metatags .= file_get_contents(dirname(__FILE__).'/../elggadmin.css');
24     $metatags .= "</style>";
25
26     if ($page_name == 'theme') {
27         $page = elggadmin_page_theme();
28         elggadmin_currentpage('theme');
29     } elseif ($page_name == 'frontpage') {
30         $page = elggadmin_page_frontpage();
31         elggadmin_currentpage('frontpage');
32     } elseif ($page_name == 'logs') {
33         $page = elggadmin_page_logs();
34         elggadmin_currentpage('logs');
35     } else {
36         $page = elggadmin_page_config();
37         elggadmin_currentpage('config');
38     }
39
40     // add global title
41     $page->title = __gettext('Administration') . ' :: ' . $page->title;
42
43     return $page;
44
45 }
46
47 function elggadmin_page_before($c=null, $args=null) {
48     require_login('admin');
49     if (!defined('context')) { context('elggadmin'); }
50     $page = (isset($args[1])) ? $args[1] : 'config';
51     elggadmin_currentpage($page);
52 }
53
54 function elggadmin_sidebar() {
55     $output = '';
56     if (elggadmin_currentpage() == 'frontpage' || elggadmin_currentpage() == 'theme') {
57         $output .= "<h2>" . __gettext("Special keywords") . "</h2>";
58         $output .= "<p>" . __gettext("You can insert these into your pageshell for special functionality:") . "</p>";
59         $output .= "<p><b>{{url}}</b> " . __gettext("The address of your site.") . "</p>";
60         $output .= "<p><b>{{sitename}}</b> " . __gettext("The name of your site.") . "</p>";
61         $output .= "<p><b>{{tagline}}</b> " . __gettext("Your site's tagline.") . "</p>";
62         $output .= "<p><b>{{username}}</b> " . __gettext("The current user's username.") . "</p>";
63         $output .= "<p><b>{{userfullname}}</b> " . __gettext("The current user's full name.") . "</p>";
64         $output .= "<p><b>{{populartags}}</b> " . __gettext("A list of the most popular tags.") . "</p>";
65         $output .= "<p><b>{{randomusers}}</b> " . __gettext("A list of random users who have filled in their profiles, if some exist.") . "</p>";
66         $output .= "<p><b>{{people:interests:foo:5}}</b> " . __gettext("Lists five people interested in 'foo' in a horizontal table.") . "</p>";
67         $output .= "<p><b>{{toptags:town}}</b> " . __gettext("Lists the top tags of type 'town' (or select weblog, file or the profile field of your choice).") . "</p>";
68     }
69
70     return $output;
71 }
72
73 function elggadmin_save($page) {
74     global $CFG;
75
76     $success = false;
77
78     // do not do anything if form key is incorrect
79     if (!elggform_key_check(optional_param('form_key'), 'elgg' . $page)) return;
80
81     $action = optional_param('action');
82
83     switch ($page) {
84         case 'config':
85             switch ($action) {
86
87             case 'elggadmin:config:restore':
88                 break;
89             case 'elggadmin:config':
90                 $defcfg = elggadmin_get_defconfig();
91                 $allowed = array();
92                 foreach ($defcfg as $name => $obj) {
93                     if (!isset($obj->noteditable)) {
94                         $allowed[$name] = $obj;
95                     }
96                 }
97
98                 $config = optional_param('config');
99                 if (!empty($config)) {
100                     foreach ($config as $name => $value) {
101                         $value = trim($value);
102                         if (array_key_exists($name, $allowed) && (!isset($CFG->$name) || $value != $CFG->$name)) {
103                             if (isset($allowed[$name]->important) && $value == '') {
104                                 elgg_messages_add(sprintf(__gettext('%s could not be empty'), $allowed[$name]->name));
105                             } else {
106                                 if (!isset($allowed[$name]->type)) {
107                                     $allowed[$name]->type = null;
108                                 }
109                                 $allowempty = false;
110
111                                 switch ($allowed[$name]->type) {
112                                     case 'bool':
113                                     case 'boolean':
114                                         $value = (bool)$value;
115                                         if (!$value) {
116                                             $value = '';
117                                         }
118                                         $allowempty = true;
119                                         break;
120                                     case 'int':
121                                     case 'integer':
122                                     case 'debug':
123                                         if (empty($value)) {
124                                             $value = 0;
125                                         } else {
126                                             $value = (int)$value;
127                                         }
128                                         $value .= '';
129                                         $allowempty = true;
130                                         break;
131                                     case 'access':
132                                         $accessvals = array(
133                                             'PUBLIC',
134                                             'LOGGED_IN',
135                                             'PRIVATE',
136                                             );
137                                         if (!in_array($value, $accessvals)) {
138                                             $value = $CFG->default_value;
139                                         }
140                                         break;
141                                     case 'language':
142                                         if (!preg_match('!^[a-z]{2,3}(_[A-Z]{2,3})?$!', $value)) {
143                                             $value = $CFG->defaultlocale;
144                                         }
145                                         break;
146                                 }
147
148                                 if (empty($value) && !$allowempty) {
149                                     unset_config($name);
150                                 } else {
151                                     set_config($name, $value);
152                                 }
153                                 elgg_messages_add(sprintf(__gettext('%s option updated'), $allowed[$name]->name));
154                                 $success = true;
155                             }
156                         }
157                     }
158
159                 }
160                 break;
161             default:
162                 break;
163             }
164             break;
165         case 'theme':
166         case 'frontpage':
167             if ($page == 'theme') {
168                 $templates = array('css', 'pageshell');
169             } else {
170                 $templates = array('frontpage_loggedin', 'frontpage_loggedout');
171             }
172             foreach ($templates as $tpl) {
173                 $content = optional_param($tpl, null, null);
174                 if (empty($content)) {
175                     elgg_messages_add(__gettext('Your input could not be empty'));
176                 } else {
177                     if (elggadmin_savetpl($tpl, $content)) {
178                         elgg_messages_add(sprintf(__gettext('%s updated'), $tpl));
179                         $success = true;
180                     } else {
181                         elgg_messages_add(sprintf(__gettext('%s could not be updated'), $tpl));
182                     }
183                 }
184             }
185             break;
186         default:
187             break;
188     }
189
190     if ($success) {
191         header_redirect(get_url(null, 'elggadmin::'.$page));
192     }
193 }
194
195 function elggadmin_currentpage($page_name=null) {
196     static $name;
197
198     if (!isset($name) && !empty($page_name)) {
199         $name = $page_name;
200     } else {
201         if (!isset($name)) {
202             $name = null;
203         }
204     }
205
206     return $name;
207 }
208
209 function elggadmin_page_frontpage() {
210     
211     $page = new StdClass;
212     $page->title = __gettext('Front page');
213     $page->body = elggadmin_tpltextarea('frontpage_loggedout', __gettext('Front page (when logged out)'));
214     $page->body .= elggadmin_tpltextarea('frontpage_loggedin', __gettext('Front page (when logged in)'));
215     $page->body .= pages_html_input('hidden', array('name'=>'action','value'=>'elggadmin:frontpage'));
216     $page->body = pages_html_form('elggfrontpage', $page->body);
217
218     return $page;
219 }
220
221 function elggadmin_page_logs() {
222     global $CFG;
223
224     $action = optional_param('action');
225     if ($action == 'elggadmin:logs:clear') {
226         elggadmin_writefile($CFG->dataroot.'errors.log',"");
227         header_redirect(get_url(null,'elggadmin::logs'), __gettext('Error log cleared'));
228     }
229
230     $logs = elggadmin_tailfile($CFG->dataroot.'errors.log', 50);
231
232     $clear = '&raquo; ' . pages_html_a(get_url_query(1, 'elggadmin::logs', 'action=elggadmin:logs:clear'), __gettext('Clear error log'));
233
234     $page = new StdClass;
235     $page->title = __gettext('Error log');
236     $page->body = pages_html_wrap('p', $clear);
237     $page->body .= pages_html_wrap('textarea', $logs, array('wrap'=>'off', 'readonly'=>'readonly'));
238
239     return $page;
240 }
241
242 function elggadmin_page_theme() {
243     $page = new StdClass;
244     $page->title = __gettext('Site theme');
245     $page->body .= elggadmin_tpltextarea('pageshell', __gettext('Main pageshell'));
246     $page->body .= elggadmin_tpltextarea('css', __gettext('CSS styles'));
247     $page->body .= pages_html_input('hidden', array('name'=>'action','value'=>'elggadmin:theme'));
248     // wrap into form
249     $page->body = pages_html_form('elggtheme', $page->body);
250
251     return $page;
252 }
253
254 function elggadmin_config_restore() {
255     $rs = null;
256
257     $config = elggadmin_get_defconfig();
258     foreach ($config as $name => $obj) {
259         $rs = delete_records('datalists', 'name', $name);
260     }
261
262     return $rs;
263 }
264
265 function elggadmin_page_config() {
266
267     // restore!
268     if (optional_param('action') == 'elggadmin:config:restore') {
269         if (require_confirm(__gettext(__gettext('Are you sure to restore default configuration?')))) {
270             if (elggadmin_config_restore()) {
271                 elgg_messages_add(__gettext('Your configuration has been restored to default values'));
272             }
273             header_redirect(get_url(null, 'elggadmin::config'));
274         }
275     }
276
277     $show_all = optional_param('view');
278
279     $_config = elggadmin_get_defconfig();
280     $page = new StdClass;
281     $page->title = __gettext('Configuration manager');
282     $page->body = null;
283
284     if (empty($show_all)) {
285         $view_all = '&raquo; ' . pages_html_a(get_url_query(1, 'elggadmin::', 'view=all'), __gettext('View all options'));
286         $page->body .= pages_html_wrap('div', pages_html_wrap('label', $view_all), array('class'=>''));
287     } else {
288         $restore = '&raquo; ' . pages_html_a(get_url_query(1, 'elggadmin::', 'action=elggadmin:config:restore'), __gettext('Restore default values'));
289         $page->body .= pages_html_wrap('div', pages_html_wrap('label', $restore), array('class'=>''));
290     }
291
292     $note = __gettext('Note: some fields are disabled because the value is forced by your <code>config.php</code>.');
293     $note .= __gettext('To change you must hand edit your <code>config.php</code>.');
294     $page->body .= pages_html_wrap('p', $note);
295
296     foreach ($_config as $c => $obj) {
297         if ((isset($obj->noteditable) || isset($obj->hidden)) && !$show_all) {
298             continue;
299         }
300
301         $name = htmlspecialchars($obj->name, ENT_COMPAT, 'utf-8');
302         if (isset($obj->important)) {
303             $name .= ': *';
304         } else {
305             $name .= ': &nbsp;';
306         }
307
308         $class = 'form-item ' . (isset($obj->important) ? ' important' : '');
309         $desc = (isset($obj->description)) ? $obj->description : '&nbsp;';
310
311         $input = pages_html_wrap('label', $name, array('class'=>'input-label'));
312         $input .= elggadmin_config_input($c, $obj);
313         $input .= pages_html_wrap('span', $desc);
314
315         $page->body .= pages_html_wrap('div', $input, array('class'=>$class));
316     }
317
318     $page->body .= pages_html_input('hidden', array('name'=>'action', 'value'=>'elggadmin:config'));
319     $page->body = pages_html_form('elggconfig', $page->body);
320
321     return $page;
322 }
323
324 function elggadmin_config_input($c, $obj) {
325     global $CFG;
326
327     // override with current values
328     $value = (isset($CFG->$c)) ? $CFG->$c : null;
329
330     $input_name = "config[$c]";
331
332     $attrs =  array();
333     $attrs['name'] = $input_name;
334     $attrs['value'] = $value;
335     $attrs['class'] = 'input';
336
337     if (isset($obj->noteditable)) {
338         $attrs['disabled'] = 'disabled';
339         $attrs['class'] .= ' input-disabled';
340     }
341
342     if (!isset($obj->type)) {
343         $obj->type = null;
344     }
345     switch ($obj->type) {
346         case 'bool':
347         case 'boolean':
348             $yes = __gettext('Yes');
349             $no = __gettext('No');
350             $yesattrs = unserialize(serialize($attrs));
351             $noattrs = unserialize(serialize($attrs));
352             $yesattrs['value'] = 1;
353             $noattrs['value'] = 0;
354
355             if ((bool)$value) {
356                 $yesattrs['checked'] = 'checked';
357             } else {
358                 $noattrs['checked'] = 'checked';
359             }
360
361             $result = pages_html_wrap('label', pages_html_input('radio', $yesattrs) . ' ' . $yes);
362             $result .= pages_html_wrap('label', pages_html_input('radio', $noattrs) . ' ' . $no);
363             $result = pages_html_wrap('div', $result, array('class'=>'input-text'));
364             break;               
365         case 'int':
366         case 'integer':
367             $attrs['class'] = ' input-numeric';
368             $result = pages_html_input('text', $attrs);
369             break;
370         case 'access':
371             unset($attrs['name']);
372             unset($attrs['value']);
373             $options = array();
374             $_opts = array(
375                 'Private' => 'PRIVATE',
376                 'Logged in' => 'LOGGED_IN',
377                 'Public' => 'PUBLIC',
378                 );
379             foreach ($_opts as $label=>$access) {
380                 $obj = new StdClass;
381                 $obj->label = $label;
382                 $obj->value = $access;
383                 if ($value == $access) {
384                     $obj->selected = true;
385                 }
386                 $options[] = $obj;
387             }
388             $result = pages_html_select($input_name, $options, $attrs);
389             break;
390         case 'debug':
391             unset($attrs['name']);
392             unset($attrs['value']);
393             $_opts = array(
394                 'Hide all errors' => '0',
395                 'Only warnings' => '7',
396                 'Show all errors' => '2047',
397                 );
398
399             $options = array();
400             foreach ($_opts as $label=>$access) {
401                 $obj = new StdClass;
402                 $obj->label = $label;
403                 $obj->value = $access;
404                 if ($value == $access) {
405                     $obj->selected = true;
406                 }
407                 $options[] = $obj;
408             }
409             $result = pages_html_select($input_name, $options, $attrs);
410             break;
411         case 'language':
412             if (empty($CFG->languages_installed)) {
413                 $result = __gettext('No languages installed');
414             } else {
415                 ksort($CFG->languages_installed);
416                 $options = array();
417                 foreach ($CFG->languages_installed as $code => $lang) {
418                     $obj = new StdClass;
419                     $obj->label = $lang;
420                     $obj->value = $code;
421                     if ($code == $value) {
422                         $obj->selected = true;
423                     }
424                     $options[] = $obj;
425                 }
426             }
427             $result = pages_html_select($input_name, $options, $attrs);
428             break;
429         case 'template':
430             global $CFG;
431             // get all list of templates/themes
432             $themes = get_list_of_plugins($CFG->templatesroot);
433             // at least should exists Default_Template
434             $options = array();
435             foreach ($themes as $theme) {
436                 $obj = new StdClass;
437                 $obj->label = templates_file_to_shortname($theme);
438