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

Revision 1607, 26.3 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 functions
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_default($owner=-1) {
13     if ($owner > 0) {
14         $page = user_flag_get('pages_default', $owner);
15         if (empty($pagename)) $page = __gettext('Main');
16     } else {
17         $obj = get_config('pages_default');
18         $page = intval($obj->value);
19     }
20
21     return $page;
22 }
23
24 function pages_actions() {
25     global $CFG, $messages;
26
27     $action = optional_param('action');
28     $do_action = optional_param('do');
29     $owner = (int)page_owner();
30     $owner = ($owner > 0) ? $owner : -1;
31
32     if ((defined('context') && context == 'pages' || defined('pages_external')) && $action == 'pages:edit' && $do_action == 'edit' && permissions_check('pages::edit', $owner)) {
33         $submit = optional_param('submit');
34         // get page id
35         $page_id = optional_param('page');
36
37         if ($submit == __gettext('Save page')) {
38             // default values
39             $page = pages_input_override(null, $owner);
40             // set owner
41
42             // if noerrors
43             if (empty($messages)) {
44                 if ($page->name == 'New page' || $page->title == 'New page') {
45                     $messages[] = __gettext('You cannot use "New page" as page title or menu title.');
46                 }
47                 
48                 // name record exists?
49                 if ($test = get_record_sql("SELECT ident FROM {$CFG->prefix}pages WHERE name = ? AND owner=?", array($page->name, $owner))) {
50                     if (empty($page->ident) || $test->ident != $page->ident) {
51                         $messages[] = __gettext('Menu title not available, please choose another.');
52                     } else {
53                         // update current record uri
54                         if (empty($page->uri)) {
55                             $page->uri = pages_build_uri($page->name);
56                         }
57
58                         if (empty($page->uri)) {
59                             $messages[] = __gettext('Menu title not available, please choose another.');
60                         } elseif ($test = get_record_sql("SELECT ident FROM {$CFG->prefix}pages WHERE uri = ? AND owner=?", array($page->uri, $owner))) {
61                             if (empty($page->ident) || $test->ident != $page->ident) {
62                                 $messages[] = __gettext('Menu title not available, please choose another.');
63                             }
64                         }
65                     }
66                 } else {
67                     // new uri
68                     if (empty($page->uri)) {
69                         $page->uri = pages_build_uri($page->name);
70                     }
71
72                     if (empty($page->uri)) {
73                         $messages[] = __gettext('Menu title not available, please choose another.');
74                     } elseif ($test = get_record_sql("SELECT ident FROM {$CFG->prefix}pages WHERE uri = ? AND owner=?", array($page->uri, $owner))) {
75                         if (empty($page->ident) || $test->ident != $page->ident) {
76                             $messages[] = __gettext('Menu title not available, please choose another.');
77                         }
78                     }
79                 }
80
81                 if ($page->parent > 0 && !pages_exists((int)$page->parent, $owner)) {
82                     $messages[] = __gettext('Invalid parent menu.');
83                 }
84             }
85
86             if (empty($messages)) {
87                 // set page owner if empty
88                 if (empty($page->owner)) {
89                     $page->owner = $owner;
90                 }
91
92                 // let's insert the data
93                 if (empty($page->ident)) {
94                     $rs = insert_record('pages', $page);
95                     if ($rs) {
96                         $messages[] = __gettext('Page created successful');
97                         $page->ident = intval($rs);
98                     } else {
99                         $messages[] = __gettext('Error creating new page. Please try again.');
100                         $page->ident = null;
101                     }
102                 } else {
103                     // last chance to verify integrity
104                     if (!pages_exists((int)$page->ident, $owner)) {
105                         $messages[] = __gettext('Error on update. That page does not exist.');
106                         $rs = true;
107                     } else {
108                         $oldparent = get_field('pages', 'parent', 'ident', $page->ident);
109                         $rs = update_record('pages', $page);
110                         if ($rs) {
111                             $messages[] = __gettext('Page updated successful');
112
113                             // check parent menu changes
114                             // If it's top menu, update childs
115                             if ($page->parent != $oldparent && $oldparent == 0) {
116                                 // set childs' parent to 0 (top menu)
117                                 execute_sql("UPDATE {$CFG->prefix}pages SET parent=0 WHERE parent={$page->ident} AND owner={$owner}", false);
118                             }
119                         } else {
120                             $messages[] = __gettext('Error updating the page. Please try again.');
121                         }
122                     }
123                 }
124
125                 // data inserted/updated?
126                 if ($rs) {
127                     // make default?
128                     $default = optional_param('page-default');
129                     if ($default) {
130                         user_flag_set('pages_default', $page->ident, $owner);
131                     }
132
133                     pages_header_redirect(pages_url(intval($page->ident), 'pages::page', $owner));
134                 }
135             }
136         }
137
138         if ($submit == __gettext('Delete')) {
139             // page exists?
140             if ($_page = get_record('pages', 'uri', $page_id, 'owner', $owner)) {
141                 // delete
142                 delete_records('pages', 'ident', $_page->ident);
143
144                 // update childs
145                 execute_sql("UPDATE {$CFG->prefix}pages SET parent=0 WHERE parent={$_page->ident} AND owner={$owner}", false);
146
147                 $messages[] = sprintf(__gettext('Page %s deleted successful.'), $page_id);
148                 // redirect
149                 pages_header_redirect(pages_url($owner, 'pages::'));
150             } else {
151                 trigger_error(__FUNCTION__.": Trying to delete an non-existent page (page id: $page_id, owner: $owner)", E_USER_NOTICE);
152             }
153         }
154     }
155 }
156
157 function pages_input_override($page=null, $owner=-1, $verbose=true) {
158     global $CFG, $messages;
159
160     $do_action = optional_param('do');
161
162     if ($do_action == 'edit') {
163         $submit = optional_param('submit');
164
165         $title = trim(optional_param('page-title'));
166         if (pages_php_allowed()) {
167             $content = trim(optional_param('page-content', null, null));
168         } else {
169             $content = trim(optional_param('page-content'));
170         }
171         $name = trim(optional_param('page-name'));
172         $parent = optional_param('menu-parent', 0, PARAM_INT);
173         $weight = optional_param('menu-weight', 0, PARAM_INT);
174         $uri = str_replace(' ', '', trim(optional_param('page-uri')));
175         $access = optional_param('page-access');
176
177         // try to get from uri query
178         if (empty($page)) {
179             $page_name = optional_param('page');
180             if (!empty($page_name)) {
181                 //$page = get_record_sql("SELECT * FROM {$CFG->prefix}pages WHERE uri=? ", array($page_name));
182                 $page = get_record('pages', 'uri', $page_name, 'owner', $owner);
183                 if (empty($page)) {
184                     $new_page_title = str_replace('_', ' ', $page_name);
185                     // new page
186                     $page = new StdClass;
187                     $page->title = $new_page_title;
188                     $page->content = pages_html_wrap('p', $new_page_title . __gettext(' Content'));
189                     $page->name = $new_page_title;
190                 }
191             } else {
192                 //  new page?
193                 //  always there should be page param
194                 $messages[] = __gettext('Page name empty.');
195             }
196         }
197
198         // not null
199         if (!empty($page)) {
200             if (!empty($title)) {
201                 $page->title = htmlspecialchars_decode($title, ENT_QUOTES);
202             } elseif ($verbose) {
203                 $messages[] = __gettext('Your title was empty.');
204             }
205
206             if (!empty($content)) {
207                 //$page->content = htmlspecialchars_decode($content, ENT_COMPAT);
208                 $page->content = $content;
209             } elseif ($verbose) {
210                 $messages[] = __gettext('Your body was empty.');
211             }
212
213             if (!empty($name)) {
214                 $textlib = textlib_get_instance();
215                 $page->name = htmlspecialchars_decode($textlib->substr($name, 0, 127), ENT_QUOTES);
216             } elseif ($verbose) {
217                 $messages[] = __gettext('Your menu title was empty.');
218             }
219
220             if (in_array($submit, array(__gettext('Save page'), __gettext('Preview')))) {
221                 $page->parent = $parent;
222                 $page->weight = $weight;
223                 $page->access = $access;
224             }
225
226             $page->parent = !isset($page->parent) ? $parent : $page->parent;
227             $page->weight = !isset($page->weight) ? $weight : $page->weight;
228             $page->access = !isset($page->access) ? $CFG->default_access : $page->access;
229
230             if (!empty($uri)) {
231                 $page->uri = $uri;
232             }
233
234             //workaround
235             if ($owner == -1 && pages_is_frontpage($page->name)) {
236                 $page->parent = -1;
237                 $page->access = 'PUBLIC';
238                 $page->owner = -1;
239             }
240         }
241     }
242
243     return $page;
244 }
245
246 function pages_create_page($page) {
247     global $messages;
248     if (!is_object($page)) {
249         trigger_error(__FUNCTION__.": invalid argument (page: is not an object)", E_USER_ERROR);
250     }
251
252     if (empty($page->title) || empty($page->content)) {
253         trigger_error(__FUNCTION__.": invalid argument (page title or content empty)", E_USER_ERROR);
254     }
255
256     if (empty($page->name)) {
257         $page->name = $page->title;
258     }
259
260     if (empty($page->uri)) {
261         $page->uri = pages_build_uri($page->name);
262     }
263
264     $rs = insert_record('pages', $page);
265     if ($rs) {
266         $page->ident = $rs;
267         return $page;
268     } else {
269         return false;
270     }
271 }
272
273 function pages_get_page($page_name, $owner=-1) {
274
275     if (empty($page_name)) {
276         if ($owner > 0) {
277             $page_id = (int) user_flag_get('pages_default', $owner);
278         } else {
279             $obj = get_config('pages_default');
280             $page_id = intval($obj->value);
281         }
282         //TODO: do header redirect
283         if (pages_exists($page_id, $owner)) {
284             $page_url = pages_url($page_id, 'pages::page', $owner);
285         } else {
286             $page_url = pages_url(__gettext('Main'), 'pages::edit', $owner);
287         }
288
289         pages_header_redirect($page_url, 301);
290     } else {
291         // remove trailing slash
292         $page_name = preg_replace('|(/)$|', '', $page_name);
293     }
294
295     $page = get_record('pages', 'uri', $page_name, 'owner', $owner);
296
297     //FIXME: auto correct parent
298     if ($page->ident == $page->parent) {
299         $_page = new StdClass;
300         $_page->ident = $page->ident;
301         $_page->parent = 0;
302         update_record('pages', $_page);
303         unset($_page);
304     }
305
306     if (isset($page->ident) && permissions_check('pages::access', $page->ident) || permissions_check('pages::edit', $owner)) {
307         if (isset($page->content)) {
308             pages_current_page($page);
309         } else {
310             // check for legacy content pages
311             if ($owner == -1) {
312                 $page = pages_get_legacy($page_name);
313             }
314
315             if (empty($page)) {
316                 // Page not found
317                 $page = pages_page_not_found();
318             }
319         }
320     } else {
321         if (isset($page->ident)) { // page exists?
322             $page = pages_page_denied();
323         } else {
324             // check for legacy content pages
325             if ($owner == -1) {
326                 $page = pages_get_legacy($page_name);
327             }
328
329             if (empty($page)) {
330                 // Page not found
331                 $page = pages_page_not_found();
332             }
333         }
334     }
335
336     if (!empty($page->title)) $page->title = pages_process_content($page->title);
337     if (!empty($page->content)) $page->content = pages_process_content($page->content);
338
339     return $page;
340 }
341
342 function pages_get_legacy($page_name) {
343     global $CFG, $PAGE;
344
345     if (isset($PAGE->pages->old_compat[$page_name])) {
346         $page = new StdClass;
347         $page->title = $PAGE->pages->old_compat[$page_name]['title'];
348         $page->content = run($PAGE->pages->old_compat[$page_name]['function']);
349         // strip content title
350         $page->content = preg_replace('|^<h1>(.+)</h1>|', '', $page->content);
351         $page->name = $page->title;
352     } else {
353         $page = null;
354     }
355
356     return $page;
357 }
358
359 function pages_process_content($content) {
360     // execute php code
361     if (pages_php_allowed()) {
362         $content = pages_eval($content);
363     }
364
365     // parse elgg template keywords
366     if (PAGES_PARSE_KEYWORDS) {
367         // wrap into templates_draw
368         global $template;
369         $tmp_name = 'pages:' . mt_rand();
370         $template[$tmp_name] = $content;
371
372         // replace all keywords
373         $content = templates_draw(array('context' => $tmp_name));
374
375         // clear template
376         unset($template[$tmp_name]);
377     }
378
379     // process with blog filters
380     if (PAGES_BLOG_TEXTPROC) {
381         $content = run('weblogs:text:process', $content);
382     }
383
384     return $content;
385 }
386
387 function pages_is_frontpage($page_name) {
388     if ($page_name == 'frontpage_loggedin' || $page_name == 'frontpage_loggedout') {
389         return true;
390     } else {
391         return false;
392     }
393 }
394
395 function pages_is_editing() {
396     global $PAGE;
397
398     return isset($PAGE->pages->editing);
399 }
400
401 function pages_edit_page($page_name, $owner=-1) {
402     global $CFG, $PAGE;
403
404     $PAGE->pages->editing = true;
405
406     if (pages_enabled() && !empty($page_name) && permissions_check('pages::edit', $owner)) {
407         if ($owner == -1 && pages_is_frontpage($page_name)) {
408             $is_frontpage = true;
409         } else {
410             $is_frontpage = false;
411         }
412
413         if (!$page = get_record('pages', 'uri', $page_name, 'owner', $owner)) {
414             // try to edit legacy content
415             if ($owner == -1) {
416                 $page = pages_get_legacy($page_name);
417             }
418         } else {
419             pages_current_page($page);
420         }
421
422         $page = pages_input_override($page, $owner, false);
423
424         if (isset($page->ident)) {
425             $page->ident = intval($page->ident);
426         }
427
428         //if frontpage force some value
429         if ($is_frontpage) {
430             $page->name = $page_name;
431             $page->parent = -1; //hidden
432             $page->access = 'PUBLIC'; //force public
433         }
434
435         $title_enc = htmlspecialchars($page->title, ENT_QUOTES, 'utf-8');
436         //$page->content = htmlspecialchars(stripslashes($page->content), ENT_COMPAT, 'utf-8');
437         $page->content = trim($page->content);
438         $page->name = htmlspecialchars($page->name, ENT_QUOTES, 'utf-8');
439
440         $input_title = pages_html_wrap('label', __gettext('Title:'), array('for' => 'page-title'));
441         $input_title .= pages_html_input(
442                         'text',
443                         array(
444                             'id' => 'page-title',
445                             'name' => 'page-title',
446                             'value' => $title_enc,
447                             'maxlength' => 127,
448                             'style' => 'width:100%;',
449                             )
450                         );
451
452         $input_content = pages_html_wrap('label', __gettext('Content:'), array('for' => 'page-content'));
453         $input_content .= pages_html_wrap(
454