root/devel/mod/pages/lib.php

Revision 1607, 23.9 kB (checked in by misja, 7 months ago)

Assorted patches, thanks rho

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