1
+ − 1
<?php
+ − 2
/*
+ − 3
* Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
+ − 4
* Version 1.0 (Banshee)
+ − 5
* pageprocess.php - intelligent retrieval of pages
+ − 6
* Copyright (C) 2006-2007 Dan Fuhry
+ − 7
*
+ − 8
* This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
+ − 9
* as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
+ − 10
*
+ − 11
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
+ − 12
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
+ − 13
*/
+ − 14
+ − 15
/**
+ − 16
* Class to handle fetching page text (possibly from a cache) and formatting it.
+ − 17
* @package Enano
+ − 18
* @subpackage UI
+ − 19
* @copyright 2007 Dan Fuhry
+ − 20
* @license GNU General Public License <http://www.gnu.org/licenses/gpl.html>
+ − 21
*/
+ − 22
+ − 23
class PageProcessor
+ − 24
{
+ − 25
+ − 26
/**
+ − 27
* Page ID and namespace of the page handled by this instance
+ − 28
* @var string
+ − 29
*/
+ − 30
+ − 31
var $page_id;
+ − 32
var $namespace;
+ − 33
+ − 34
/**
+ − 35
* Tracks if the page we're loading exists in the database or not.
+ − 36
* @var bool
+ − 37
*/
+ − 38
+ − 39
var $page_exists = false;
+ − 40
+ − 41
/**
+ − 42
* Permissions!
+ − 43
* @var object
+ − 44
*/
+ − 45
+ − 46
var $perms = null;
+ − 47
+ − 48
/**
+ − 49
* Switch to track if redirects are allowed. Defaults to true.
+ − 50
* @var bool
+ − 51
*/
+ − 52
+ − 53
var $allow_redir = true;
+ − 54
+ − 55
/**
+ − 56
* If this is set to true, this will call the header and footer funcs on $template when render() is called.
+ − 57
* @var bool
+ − 58
*/
+ − 59
+ − 60
var $send_headers = false;
+ − 61
+ − 62
/**
+ − 63
* Cache the fetched text so we don't fetch it from the DB twice.
+ − 64
* @var string
+ − 65
*/
+ − 66
+ − 67
var $text_cache = '';
+ − 68
+ − 69
/**
+ − 70
* Debugging information to track errors. You can set enable to false to disable sending debug information.
+ − 71
* @var array
+ − 72
*/
+ − 73
+ − 74
var $debug = array(
+ − 75
'enable' => true,
+ − 76
'works' => false
+ − 77
);
+ − 78
+ − 79
/**
+ − 80
* Constructor.
+ − 81
* @param string The page ID (urlname) of the page
+ − 82
* @param string The namespace of the page
+ − 83
*/
+ − 84
+ − 85
function __construct( $page_id, $namespace )
+ − 86
{
+ − 87
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 88
+ − 89
// See if we can get some debug info
+ − 90
if ( function_exists('debug_backtrace') && $this->debug['enable'] )
+ − 91
{
+ − 92
$this->debug['works'] = true;
+ − 93
$this->debug['backtrace'] = enano_debug_print_backtrace(true);
+ − 94
}
+ − 95
+ − 96
// First things first - check page existence and permissions
+ − 97
+ − 98
if ( !isset($paths->nslist[$namespace]) )
+ − 99
{
+ − 100
$this->send_error('The namespace "' . htmlspecialchars($namespace) . '" does not exist.');
+ − 101
}
+ − 102
+ − 103
$this->_setup( $page_id, $namespace );
+ − 104
+ − 105
}
+ − 106
+ − 107
/**
+ − 108
* The main method to send the page content. Also responsible for checking permissions.
+ − 109
*/
+ − 110
+ − 111
function send()
+ − 112
{
+ − 113
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 114
if ( !$this->perms->get_permissions('read') )
+ − 115
{
+ − 116
$this->err_access_denied();
+ − 117
return false;
+ − 118
}
+ − 119
if ( $this->namespace == 'Special' || $this->namespace == 'Admin' )
+ − 120
{
+ − 121
if ( !$this->page_exists )
+ − 122
{
+ − 123
redirect( makeUrl(getConfig('main_page')), 'Can\'t find special page', 'The special or administration page you requested does not exist. You will now be transferred to the main page.', 2 );
+ − 124
}
+ − 125
$func_name = "page_{$this->namespace}_{$this->page_id}";
+ − 126
if ( function_exists($func_name) )
+ − 127
{
+ − 128
return @call_user_func($func_name);
+ − 129
}
+ − 130
else
+ − 131
{
+ − 132
$title = 'Page backend not found';
+ − 133
$message = "The administration page you are looking for was properly registered using the page API, but the backend function
+ − 134
(<tt>$fname</tt>) was not found. If this is a plugin page, then this is almost certainly a bug with the plugin.";
+ − 135
+ − 136
if ( $this->send_headers )
+ − 137
{
+ − 138
$template->tpl_strings['PAGE_NAME'] = $title;
+ − 139
$template->header();
+ − 140
echo "<p>$message</p>";
+ − 141
$template->footer();
+ − 142
}
+ − 143
else
+ − 144
{
+ − 145
echo "<h2>$title</h2>
+ − 146
<p>$message</p>";
+ − 147
}
+ − 148
return false;
+ − 149
}
+ − 150
}
+ − 151
else if ( ( $this->namespace == 'Template' || $this->namespace == 'System' ) && $this->page_exists )
+ − 152
{
+ − 153
$this->header();
+ − 154
+ − 155
$text = $this->fetch_text();
+ − 156
$text = preg_replace('/<noinclude>(.*?)<\/noinclude>/is', '\\1', $text);
+ − 157
$text = preg_replace('/<nodisplay>(.*?)<\/nodisplay>/is', '', $text);
+ − 158
+ − 159
$text = RenderMan::render( $text );
+ − 160
+ − 161
echo $text;
+ − 162
+ − 163
$this->footer();
+ − 164
+ − 165
}
+ − 166
else if ( !$this->page_exists )
+ − 167
{
+ − 168
// Perhaps this is hooked?
+ − 169
ob_start();
+ − 170
+ − 171
$code = $plugins->setHook('page_not_found');
+ − 172
foreach ( $code as $cmd )
+ − 173
{
+ − 174
eval($cmd);
+ − 175
}
+ − 176
+ − 177
$ob = ob_get_contents();
+ − 178
+ − 179
if ( empty($ob) )
+ − 180
{
+ − 181
$this->err_page_not_existent();
+ − 182
}
+ − 183
}
9
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 184
else // if ( in_array($this->namespace, array('Article', 'User', 'Project', 'Help', 'File', 'Category')) && $this->page_exists )
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 185
{
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 186
// Send as regular page
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 187
$text = $this->fetch_text();
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 188
if ( $text == 'err_no_text_rows' )
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 189
{
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 190
$this->err_no_rows();
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 191
return false;
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 192
}
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 193
else
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 194
{
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 195
$this->render();
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 196
}
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 197
}
1
+ − 198
4
+ − 199
1
+ − 200
}
+ − 201
+ − 202
/**
+ − 203
* Sets internal variables.
+ − 204
* @access private
+ − 205
*/
+ − 206
+ − 207
function _setup($page_id, $namespace)
+ − 208
{
+ − 209
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 210
+ − 211
$page_id_cleaned = sanitize_page_id($page_id);
+ − 212
+ − 213
$this->page_id = $page_id_cleaned;
+ − 214
$this->namespace = $namespace;
+ − 215
+ − 216
$this->perms = $session->fetch_page_acl( $page_id, $namespace );
+ − 217
+ − 218
// Exception for Admin: pages
+ − 219
if ( $this->namespace == 'Admin' )
+ − 220
{
+ − 221
$fname = "page_Admin_{$this->page_id}";
+ − 222
}
+ − 223
+ − 224
// Does the page "exist"?
4
+ − 225
if ( $paths->cpage['urlname_nons'] == $page_id && $paths->namespace == $namespace && !$paths->page_exists && ( $this->namespace != 'Admin' || ($this->namespace == 'Admin' && !function_exists($fname) ) ) )
1
+ − 226
{
+ − 227
$this->page_exists = false;
+ − 228
}
+ − 229
else if ( !isset( $paths->pages[ $paths->nslist[$namespace] . $page_id ] ) && ( $this->namespace == 'Admin' && !function_exists($fname) ) )
+ − 230
{
+ − 231
$this->page_exists = false;
+ − 232
}
+ − 233
else
+ − 234
{
+ − 235
$this->page_exists = true;
+ − 236
}
+ − 237
}
+ − 238
+ − 239
/**
+ − 240
* Renders it all in one go, and echoes it out. This assumes that the text is in the DB.
+ − 241
* @access private
+ − 242
*/
+ − 243
+ − 244
function render()
+ − 245
{
+ − 246
$text = $this->fetch_text();
+ − 247
+ − 248
$this->header();
9
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 249
if ( $this->send_headers )
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 250
{
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 251
display_page_headers();
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 252
}
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 253
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 254
$text = '?>' . RenderMan::render($text);
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 255
// echo('<pre>'.htmlspecialchars($text).'</pre>');
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 256
eval ( $text );
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 257
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 258
if ( $this->send_headers )
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 259
{
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 260
display_page_footers();
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 261
}
1e61232606d6
Following fixes: admin theme supports <button> tag now, PageProcessor can eval now, and SpecialAdmin.php plugin can no longer be disabled
dan@fuhry
diff
changeset
+ − 262
1
+ − 263
$this->footer();
+ − 264
}
+ − 265
+ − 266
/**
+ − 267
* Sends the page header, dependent on, of course, whether we're supposed to.
+ − 268
*/
+ − 269
+ − 270
function header()
+ − 271
{
+ − 272
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 273
if ( $this->send_headers )
+ − 274
$template->header();
+ − 275
}
+ − 276
+ − 277
/**
+ − 278
* Sends the page footer, dependent on, of course, whether we're supposed to.
+ − 279
*/
+ − 280
+ − 281
function footer()
+ − 282
{
+ − 283
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 284
if ( $this->send_headers )
+ − 285
$template->footer();
+ − 286
}
+ − 287
+ − 288
/**
+ − 289
* Fetches the raw, unfiltered page text.
+ − 290
* @access public
+ − 291
*/
+ − 292
+ − 293
function fetch_text()
+ − 294
{
+ − 295
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 296
+ − 297
if ( !empty($this->text_cache) )
+ − 298
{
+ − 299
return $this->text_cache;
+ − 300
}
+ − 301
+ − 302
$q = $db->sql_query('SELECT page_text, char_tag FROM '.table_prefix.'page_text WHERE page_id=\'' . $this->page_id . '\' AND namespace=\'' . $this->namespace . '\';');
+ − 303
if ( !$q )
+ − 304
{
+ − 305
$this->send_error('Error during SQL query.', true);
+ − 306
}
+ − 307
if ( $db->numrows() < 1 )
+ − 308
{
+ − 309
$this->page_exists = false;
+ − 310
return 'err_no_text_rows';
+ − 311
}
+ − 312
+ − 313
$row = $db->fetchrow();
+ − 314
$db->free_result();
+ − 315
+ − 316
if ( !empty($row['char_tag']) )
+ − 317
{
+ − 318
// This page text entry uses the old text-escaping format
+ − 319
$from = array(
+ − 320
"{APOS:{$row['char_tag']}}",
+ − 321
"{QUOT:{$row['char_tag']}}",
+ − 322
"{SLASH:{$row['char_tag']}}"
+ − 323
);
+ − 324
$to = array("'", '"', '\\');
+ − 325
$row['page_text'] = str_replace($from, $to, $row['page_text']);
+ − 326
}
+ − 327
+ − 328
$this->text_cache = $row['page_text'];
+ − 329
+ − 330
return $row['page_text'];
+ − 331
+ − 332
}
+ − 333
+ − 334
/**
+ − 335
* Send the error message to the user that the access to this page is denied.
+ − 336
* @access private
+ − 337
*/
+ − 338
+ − 339
function err_access_denied()
+ − 340
{
+ − 341
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 342
+ − 343
$ob = '';
+ − 344
$template->tpl_strings['PAGE_NAME'] = 'Access denied';
+ − 345
+ − 346
if ( $this->send_headers )
+ − 347
{
+ − 348
$ob .= $template->getHeader();
+ − 349
}
+ − 350
+ − 351
$ob .= '<div class="error-box"><b>Access to this page is denied.</b><br />This may be because you are not logged in or you have not met certain criteria for viewing this page.</div>';
+ − 352
+ − 353
if ( $this->send_headers )
+ − 354
{
+ − 355
$ob .= $template->getFooter();
+ − 356
}
+ − 357
echo $ob;
+ − 358
}
+ − 359
+ − 360
/**
+ − 361
* Send the error message to the user complaining that there weren't any rows.
+ − 362
* @access private
+ − 363
*/
+ − 364
+ − 365
function err_no_rows()
+ − 366
{
+ − 367
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 368
+ − 369
$title = 'No text rows';
+ − 370
$message = 'While the page\'s existence was verified, there were no rows in the database that matched the query for the text. This may indicate a bug with the software; ask the webmaster for more information. The offending query was:<pre>' . $db->latest_query . '</pre>';
+ − 371
if ( $this->send_headers )
+ − 372
{
+ − 373
$template->tpl_strings['PAGE_NAME'] = $title;
+ − 374
$template->header();
+ − 375
echo "<p>$message</p>";
+ − 376
$template->footer();
+ − 377
}
+ − 378
else
+ − 379
{
+ − 380
echo "<h2>$title</h2>
+ − 381
<p>$message</p>";
+ − 382
}
+ − 383
}
+ − 384
+ − 385
/**
+ − 386
* Tell the user the page doesn't exist, and present them with their options.
+ − 387
* @access private
+ − 388
*/
+ − 389
+ − 390
function err_page_not_existent()
+ − 391
{
+ − 392
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 393
+ − 394
$this->header();
+ − 395
header('HTTP/1.1 404 Not Found');
+ − 396
echo '<h3>There is no page with this title yet.</h3>
+ − 397
<p>You have requested a page that doesn\'t exist yet.';
+ − 398
if ( $session->get_permissions('create_page') )
+ − 399
{
+ − 400
echo ' You can <a href="'.makeUrlNS($this->namespace, $this->page_id, 'do=edit', true).'" onclick="ajaxEditor(); return false;">create this page</a>, or return to the <a href="'.makeUrl(getConfig('main_page')).'">homepage</a>.';
+ − 401
}
+ − 402
else
+ − 403
{
+ − 404
echo ' Return to the <a href="'.makeUrl(getConfig('main_page')).'">homepage</a>.</p>';
+ − 405
}
+ − 406
if ( $session->get_permissions('history_rollback') )
+ − 407
{
+ − 408
$e = $db->sql_query('SELECT * FROM ' . table_prefix . 'logs WHERE action=\'delete\' AND page_id=\'' . $this->page_id . '\' AND namespace=\'' . $this->namespace . '\' ORDER BY time_id DESC;');
+ − 409
if ( !$e )
+ − 410
{
+ − 411
$db->_die('The deletion log could not be selected.');
+ − 412
}
+ − 413
if ( $db->numrows() > 0 )
+ − 414
{
+ − 415
$r = $db->fetchrow();
+ − 416
echo '<p>This page also appears to have some log entries in the database - it seems that it was deleted on ' . $r['date_string'] . '. You can probably <a href="'.makeUrl($paths->page, 'do=rollback&id='.$r['time_id']).'" onclick="ajaxRollback(\''.$r['time_id'].'\'); return false;">roll back</a> the deletion.</p>';
+ − 417
}
+ − 418
$db->free_result();
+ − 419
}
+ − 420
echo '<p>
+ − 421
HTTP Error: 404 Not Found
+ − 422
</p>';
+ − 423
$this->footer();
+ − 424
}
+ − 425
+ − 426
/**
+ − 427
* PHP 4 constructor.
+ − 428
* @see PageProcessor::__construct()
+ − 429
*/
+ − 430
+ − 431
function PageProcessor( $page_id, $namespace )
+ − 432
{
+ − 433
$this->__construct($page_id, $namespace);
+ − 434
}
+ − 435
+ − 436
/**
+ − 437
* Send an error message and die
+ − 438
* @var string Error message
+ − 439
* @var bool If true, send DBAL's debugging information as well
+ − 440
*/
+ − 441
+ − 442
function send_error($message, $sql = false)
+ − 443
{
+ − 444
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 445
+ − 446
$content = "<p>$message</p>";
+ − 447
$template->tpl_strings['PAGE_NAME'] = 'General error in page fetcher';
+ − 448
+ − 449
if ( $this->debug['works'] )
+ − 450
{
+ − 451
$content .= $this->debug['backtrace'];
+ − 452
}
+ − 453
+ − 454
header('HTTP/1.1 500 Internal Server Error');
+ − 455
+ − 456
$template->header();
+ − 457
echo $content;
+ − 458
$template->footer();
+ − 459
+ − 460
$db->close();
+ − 461
+ − 462
exit;
+ − 463
+ − 464
}
+ − 465
+ − 466
} // class PageProcessor
+ − 467
+ − 468
?>