root/releases/0.9rc1/mod/pages/lib.php

Revision 1420, 24.2 kB (checked in by rho, 1 year ago)

updated elggadmin

  • don't "print continue" on database modify
  • save sysadminemail on db
  • fixed issue that overwrite debug
  • improved elggadmin form to support types of inputs

Signed-off: Rolando Espinoza La fuente <rho@prosoftpeople.com>

Line 
1 <?php
2 /**
3  * Custom Pages plugin main file
4  * $id$
5  *
6  * @copyright Copyright (c) 2007 Pro Soft Resources Inc. http://www.prosoftpeople.com
7  * @author Rolando Espinoza La Fuente <rho@prosoftpeople.com>
8  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9  * @todo documentation
10  */
11
12 function pages_init() {
13     global $CFG, $function, $PAGE;
14
15     define('pages_allow_php_admin', true);
16     define('pages_allow_php_user', false);
17     define('pages_parse_keywords', true);
18     define('pages_blog_textproc', false); // breaks content with br's
19
20     pages_dbsetup();
21
22     //DEPRECATED: legacy pages
23     /*
24     $PAGE->pages->old_compat = array(
25         'about.php' => array(
26             'title' => sprintf(__gettext('About %s'), $CFG->sitename),
27             'function' => 'content:about',
28             ),
29         'faq.php' => array(
30             'title' => sprintf(__gettext('%s FAQ'), $CFG->sitename),
31             'function' => 'content:faq',
32             ),
33         'privacy.php' => array(
34             'title' => sprintf(__gettext('%s Privacy Police'), $CFG->sitename),
35             'function' => 'content:privacy',
36             ),
37         'terms.php' => array(
38             'title' => sprintf(__gettext('%s Terms & Conditions'), $CFG->sitename),
39             'function' => 'content:terms',
40             ),
41         'run_your_own.php' => array(
42             'title' => sprintf(__gettext('Running your own %s'), $CFG->sitename),
43             'function' => 'content:run_your_own',
44             ),
45         );
46
47     $function["pages:frontpage_loggedin"][] = dirname(__FILE__) . "/lib/function_frontpage_loggedin.php";
48     $function["pages:frontpage_loggedout"][] = dirname(__FILE__) . "/lib/function_frontpage_loggedout.php";
49
50     $PAGE->pages->old_compat['frontpage_loggedin'] = array(
51         'title' => sprintf(__gettext('Welcome {{username}}')),
52         'function' => 'pages:frontpage_loggedin',
53         );
54     $PAGE->pages->old_compat['frontpage_loggedout'] = array(
55         'title' => sprintf(__gettext('Welcome Guest')),
56         'function' => 'pages:frontpage_loggedout',
57         );
58      */
59 }
60
61 function pages_pagesetup() {
62     // backward compatibilty
63     global $CFG, $PAGE;
64
65     // menu keyword
66     $CFG->templates->variables_substitute['pagesmenu'][] = 'pages_tplkw_menu';
67     $CFG->templates->variables_substitute['page'][] = 'pages_tplkw_page';
68     $CFG->templates->variables_substitute['sysadminemail'][] = 'pages_tplkw_sysadminemail';
69
70     if (defined('context') && context == 'pages') {
71         if (permissions_check('pages::edit', page_owner())) {
72             $page_name = optional_param('page');
73             $do_action = optional_param('do');
74
75             if ($do_action != 'edit') {
76                 pages_submenu_add('pages:edit', __gettext('Edit this page'), pages_url($page_name, 'pages::edit', page_owner()), 0);
77             }
78         }
79     }
80
81     if (isloggedin()) {
82         pages_menu_add('pages', __gettext('Your Pages'), get_url($_SESSION['userid'], 'pages::'));
83     }
84
85     // not show main site pages on sidebar
86     sidebar_add(25, 'pages_sidebar', null, true, __gettext('Your pages'));
87 }
88
89 function pages_php_allowed() {
90     if (page_owner() == -1
91         || pages_allow_php_user
92         || (pages_allow_php_admin && user_flag_get('admin', page_owner())))
93     {
94         return true;
95     } else {
96         return false;
97     }
98 }
99
100 function pages_permissions_check($type, $ident) {
101     $result = false;
102
103     if (isadmin()) {
104         return true;
105     }
106
107     switch ($type) {
108         case 'pages::edit':
109             if ($ident > 0) {
110                 $result = run('permissions:check', 'profile');
111             } else {
112                 $result = false;
113             }
114             break;
115         case 'pages::access':
116             $access = get_field('pages', 'access', 'ident', $ident);
117             $result = run('users:access_level_check', $access);
118             break;
119     }
120
121     return $result;
122 }
123
124 function pages_dbsetup() {
125     global $CFG, $METATABLES;
126
127     if (!in_array($CFG->prefix . 'pages', $METATABLES)) {
128         $dbscript = $CFG->dirroot . 'mod/pages/db/' . $CFG->dbtype . '.sql';
129
130         if (is_readable($dbscript)) {
131             modify_database($dbscript);
132
133             // pages functions
134             require_once(dirname(__FILE__) . '/lib/pages.inc.php');
135
136             // insert first page
137             $page = new StdClass;
138             $page->name = __gettext('About');
139             $page->title = __gettext('About') . " {{sitename}}";
140             $page->content = @file_get_contents(dirname(__FILE__).'/legacy/content_about.html');
141
142             $page = pages_create_page($page);
143
144             if ($page) {
145                 set_config('pages_default', $page->ident);
146
147                 $_page = new StdClass;
148                 $_page->uri = 'privacy.php'; //backward compatibility
149                 $_page->title = __gettext('Privacy Policy');
150                 $_page->content = @file_get_contents(dirname(__FILE__).'/legacy/content_privacy.html');
151                 $_page->parent = $page->ident;
152                 $_page = pages_create_page($_page);
153
154                 $_page = new StdClass;
155                 $_page->uri = 'terms.php'; //backward compatibility
156                 $_page->title = __gettext('Terms and Conditions');
157                 $_page->content = @file_get_contents(dirname(__FILE__).'/legacy/content_terms.html');
158                 $_page->parent = $page->ident;
159                 $_page = pages_create_page($_page);
160             }
161
162             $page = new StdClass;
163             $page->name = __gettext('FAQ');
164             $page->title = __gettext('Frequently Asked Questions');
165             $page->content = @file_get_contents(dirname(__FILE__).'/legacy/content_faq.html');
166
167             $page = pages_create_page($page);
168
169             //reload system
170             header_redirect($CFG->wwwroot);
171
172         } else {
173             // not supported!
174             error('Error: your database (' . $CFG->dbtype . ') is not supported by elgg pages plugin.');
175         }
176
177         print_continue('');
178         exit();
179     }
180 }
181
182 function pages_url($id=null, $type=null, $owner=-1) {
183     global $CFG;
184     if (!is_numeric($owner)) {
185         trigger_error(__FUNCTION__.": invalid argument (owner: $owner)", E_USER_ERROR);
186     }
187
188     $url = '#'; //safe value
189
190     if (empty($id)) {
191         $id = pages_default($owner);
192     }
193
194     if (empty($type)) {
195         $type = 'pages::page';
196     }
197
198
199     switch ($type) {
200         case 'pages::':
201             //id is user ident
202             if ($id > 0) {
203                 $url = get_url($id, 'profile::');
204                 if (!empty($url)) $url .= 'pages/'; // url for user's pages
205             } else {
206                 $url = $CFG->wwwroot . 'content/'; // url for site pages
207             }
208             break;
209         case 'pages::page':
210             if (is_string($id)) {
211                 // get from uri
212                 $page_name = $id;
213             } elseif (is_int($id)) {
214                 $page_name = get_field('pages', 'uri', 'ident', $id, 'owner', $owner);
215                 if (empty($page_name)) {
216                     trigger_error(__FUNCTION__.": page does not exists (page id: $id, owner: $owner)", E_USER_WARNING);
217                     return null;
218                 }
219             } else {
220                 trigger_error(__FUNCTION__.": invalid argument id.", E_USER_ERROR);
221             }
222
223             $url = get_url($owner, 'pages::') . $page_name;
224             break;
225         case 'pages::edit':
226             $url = pages_url($id, 'pages::page', $owner) . '?do=edit';
227             break;
228     }
229
230     return $url;
231 }
232
233 function pages_tplkw_menu($vars) {
234     if (isset($vars[1])) {
235         $owner = intval($vars[1]);
236     } else {
237         $owner = page_owner();
238     }
239     // force only keyword for site pages
240     $owner = -1;
241
242     return pages_html_menu(pages_get_mainmenu($owner));
243 }
244
245 function pages_tplkw_page($vars) {
246     global $messages;
247     $output = '';
248
249     if (!isset($vars[1])) {
250         $msg = "{{page}} keyword error, must provied a page name. e.g. {{page:Main}}";
251         if (page_owner() == $_SESSION['userid']) {
252             // show error to page owner
253             $messages[] = $msg;
254         }
255         trigger_error($msg, E_USER_WARNING);
256     } else {
257         if (isset($vars[2])) {
258             $page_id = $vars[2];
259             $page_name = pages_build_uri($page_id);
260
261             $username = $vars[1];
262             // main site content
263             if ($username == 'content') {
264                 $user_id = -1;
265             } else {
266                 $user_id = (int)user_info_username('ident', $username);
267             }
268
269             if (empty($user_id)) {
270                 $msg = "{{page}} keyword error, invalid username: {$username}";
271                 if (page_owner() == $_SESSION['userid']) {
272                     // show error to page owner
273                     $messages[] = $msg;
274                 }
275                 trigger_error($msg, E_USER_WARNING);
276             } else {
277                 $page_url = pages_url($page_name, 'pages::page', $user_id);
278             }
279         } else {
280             $page_id = $vars[1];
281             $page_name = pages_build_uri($page_id);
282             $page_url = pages_url($page_name, 'pages::page', page_owner());
283         }
284
285         // return html link
286         if (isset($page_url)) {
287             $output pages_html_a($page_url, $page_id);
288         }
289     }
290
291     return $output;
292 }
293
294 function pages_tplkw_sysadminemail() {
295     global $CFG;
296     return $CFG->sysadminemail;
297 }
298
299 function pages_frontpage($logged=false) {
300     require_once(dirname(__FILE__) . '/lib/pages.inc.php');
301     if ($logged) {
302         $context = 'frontpage_loggedin';
303         
304         if (isadmin()) {
305             pages_submenu_add('pages:edit_in', __gettext('Edit frontpage_loggedin'), pages_url('frontpage_loggedin', 'pages::edit', -1));
306             pages_submenu_add('pages:edit_out', __gettext('Edit frontpage_loggedout'), pages_url('frontpage_loggedout', 'pages::edit', -1));
307         }
308     } else {
309         $context = 'frontpage_loggedout';
310     }
311     return pages_get_page($context, -1);
312 }
313
314 function pages_sidebar() {
315     global $CFG, $page_owner;
316
317     $owner = page_owner();
318
319     $menu_elements = pages_get_mainmenu($owner);
320
321     if ($owner > 0) {
322         if ($_SESSION['userid'] == $owner) {
323             $title = __gettext('Your pages');
324         }else {
325             //$title = sprintf(__gettext("%s's pages"), htmlspecialchars(user_name($owner), ENT_COMPAT, 'utf-8'));
326             $title = __gettext("Pages");
327         }
328     } else {
329         $title = $CFG->sitename;
330     }
331     
332     $body = templates_draw(array(
333         'context' => 'sidebarholder',
334         'title' => $title,
335         'body' => pages_html_menu($menu_elements),
336         ));
337
338     return $body;
339 }
340
341 function pages_get_mainmenu($owner=-1) {
342     global $CFG;
343     if (!is_numeric($owner)) {
344         trigger_error(__FUNCTION__.": invalid argument (owner: $owner)");
345     }
346
347     $elements = get_records_sql("SELECT ident,parent,name,uri,weight,owner FROM {$CFG->prefix}pages WHERE owner=? AND parent>=0 ORDER BY parent,weight,name", array($owner));
348
349     $menu = array();
350     if (is_array($elements)) {
351         foreach ($elements as $e) {
352             if ($e->parent == 0) {
353                 if (!isset($menu[$e->weight])) {
354                     $menu[$e->ident] = array();
355                 }
356                 $menu[$e->ident][0] = $e;
357             } else {
358                 if (!isset($menu[$e->parent][1])) {
359                     $menu[$e->parent][1] = array();
360                 }
361                 $menu[$e->parent][1][] = $e;
362             }
363         }
364     }
365
366     return $menu;
367 }
368
369 function pages_html_menu($elements) {
370     $menu = '';
371     global $page_owner;
372
373     if (is_array($elements)) {
374         $current_page = pages_current_page();
375         $current_uri = !empty($current_page->uri) ? $current_page->uri : '';
376
377         foreach ($elements as $p => $li) {
378             if (is_array($li)) {
379                 $parent = $li[0];
380                 $childs = isset($li[1]) ? $li[1] : null;
381
382                 // set owner
383                 if (!isset($owner)) $owner = $parent->owner;
384
385                 if ($parent->uri == $current_uri) {
386                     $parent_link = pages_html_a(pages_url($parent->uri, 'pages::page', $parent->owner), $parent->name, array('class' => 'selected'));
387                 } else {
388                     $parent_link = pages_html_a(pages_url($parent->uri, 'pages::page', $parent->owner), $parent->name);
389                 }
390
391                 if (is_array($childs)) {
392                     $submenu = '';
393                     foreach ($childs as $child) {
394                         if ($child->uri == $current_uri) {
395                             $_li = pages_html_a(pages_url($child->uri, 'pages::page', $child->owner), $child->name, array('class' => 'selected'));
396                         } else {
397                             $_li = pages_html_a(pages_url($child->uri, 'pages::page', $child->owner), $child->name);
398                         }
399                         $submenu .= pages_html_wrap('li', $_li, array('class' => 'menu-item'));
400                     }
401
402                     $menu .= pages_html_wrap('li', $parent_link . pages_html_wrap('ul', $submenu), array('class' => 'menu-parent'));
403                 } else {
404                     $menu .= pages_html_wrap('li', $parent_link, array('class' => 'menu-item'));
405                 }
406             } else {
407             }
408         }
409     }
410
411     // workaround
412     if (!isset($owner)) $owner = empty($page_owner) ? page_owner() : $page_owner;
413
414     if (empty($menu)) {
415         $main_link = pages_html_a(pages_url(__gettext('Main'), 'pages::page', $owner), __gettext('Main'));
416         $menu .= pages_html_wrap('li', $main_link, array('class' => 'menu-item'));
417     }
418
419     // "add new" page link
420     if (permissions_check('pages::edit',$owner)) {
421         $new_page_link = pages_html_wrap('small', __gettext('New page'));
422         $menu .= pages_html_wrap('li', pages_html_wrap('a', $new_page_link, array('href'=>pages_url('New_page', 'pages::edit', $owner), 'title'=>__gettext('New page')), array('class' => 'menu-item')));
423     }
424
425     if (!empty($menu)) {
426         $menu = pages_html_wrap('ul', $menu);
427     }
428
429     return $menu;
430 }
431
432 function pages_current_page($page=null) {
433     global $PAGE;
434
435     if (!is_null($page)) {
436         $PAGE->pages->current = $page;
437     }
438
439     if (!isset($PAGE->pages->current)) {
440         $PAGE->pages->current = null;
441     }
442
443     return $PAGE->pages->current;
444 }
445
446 function pages_html_wrap($tag, $content=null, $attrs=null) {
447     if (is_array($attrs)) {
448         $extra = pages_html_array2attrs($attrs);
449     } else {
450         $extra = '';
451     }
452
453     if (!empty($content)) {
454         $result = "<{$tag}{$extra}>{$content}</{$tag}>\n";
455     } else {
456         $result = "\n<{$tag} {$extra} />\n";
457     }
458
459     return $result;
460 }
461
462 function pages_html_a($url, $title, $attrs=null) {
463     // Default attrs
464     $extra = array(
465         'title' => $title,
466         'href' => $url,
467         );
468
469     if (is_array($attrs)) {
470         $extra = array_merge($extra, $attrs);
471     }
472
473     return pages_html_wrap('a', htmlspecialchars($title, ENT_QUOTES, 'utf-8'), $extra);
474 }
475
476 function pages_html_input($type, $attrs=null) {
477     if (!isset($attrs)) $attrs = array();
478
479     $attrs = array_merge(array('type' => $type), $attrs);
480
481     return pages_html_wrap('input', null, $attrs);
482 }
483
484 function pages_html_form($name, $body, $attrs=null) {
485     if (!isset($attrs)) {$attrs = array();}
486
487     if (!isset($attrs['id'])) { $attrs['id'] = $name; }
488     if (!isset($attrs['name'])) { $attrs['name'] = $name; }
489     if (!isset($attrs['method'])) { $attrs['method'] = 'post'; }
490     if (!isset($attrs['action'])) { $attrs['action'] = ''; }
491
492     if (isset($attrs['buttons'])) {
493         $buttons = $attrs['buttons'];
494     } else {
495         $buttons = pages_html_input('submit', array('value' => __gettext('Submit')));
496     }
497
498