1
+ − 1
<?php
+ − 2
/*
+ − 3
* Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
21
663fcf528726
Updated all version numbers back to Banshee; a few preliminary steps towards full UTF-8 support in page URLs
Dan
diff
changeset
+ − 4
* Version 1.0 (Banshee)
1
+ − 5
* render.php - handles fetching pages and parsing them into HTML
+ − 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
class RenderMan {
+ − 16
+ − 17
function strToPageID($string)
+ − 18
{
+ − 19
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 20
$k = array_keys($paths->nslist);
+ − 21
for($i=0;$i<sizeof($paths->nslist);$i++)
+ − 22
{
+ − 23
$ln = strlen($paths->nslist[$k[$i]]);
+ − 24
if(substr($string, 0, $ln) == $paths->nslist[$k[$i]])
+ − 25
{
+ − 26
$ns = $k[$i];
+ − 27
$pg = substr($string, strlen($paths->nslist[$ns]), strlen($string));
+ − 28
}
+ − 29
}
+ − 30
return Array($pg, $ns);
+ − 31
}
+ − 32
+ − 33
function getPage($page_id, $namespace, $wiki = 1, $smilies = true, $filter_links = true, $redir = true, $render = true)
+ − 34
{
+ − 35
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 36
dc_here('render: page requested<br />ID/namespace: '."$page_id, $namespace<br />Wiki mode: $wiki<br />Smilies: ".(string)$smilies."<br />Allow redirects: ".(string)$redir);
+ − 37
+ − 38
$perms =& $session;
+ − 39
+ − 40
if ( $page_id != $paths->cpage['urlname_nons'] || $namespace != $paths->namespace )
+ − 41
{
+ − 42
unset($perms);
+ − 43
unset($perms); // PHP <5.1.5 Zend bug
+ − 44
$perms = $session->fetch_page_acl($page_id, $namespace);
+ − 45
}
+ − 46
+ − 47
if(!$perms->get_permissions('read'))
+ − 48
return 'Access denied ('.$paths->nslist[$namespace].$page_id.')';
+ − 49
+ − 50
if($wiki == 0 || $render == false)
+ − 51
{
+ − 52
if(!$perms->get_permissions('view_source'))
+ − 53
{
+ − 54
return 'Access denied ('.$paths->nslist[$namespace].$page_id.')';
+ − 55
}
+ − 56
}
+ − 57
+ − 58
$q = $db->sql_query('SELECT page_text,char_tag FROM '.table_prefix.'page_text WHERE page_id=\''.$db->escape($page_id).'\' AND namespace=\''.$db->escape($namespace).'\';');
+ − 59
if ( !$q )
+ − 60
{
+ − 61
$db->_die('Method called was: RenderMan::getPage(\''.$page_id.'\', \''.$namespace.'\');.');
+ − 62
}
+ − 63
if ( $db->numrows() < 1 )
+ − 64
{
+ − 65
return false;
+ − 66
}
+ − 67
$row = $db->fetchrow();
+ − 68
$db->free_result();
+ − 69
+ − 70
$message = $row['page_text'];
+ − 71
$chartag = $row['char_tag'];
+ − 72
unset($row); // Free some memory
+ − 73
+ − 74
if ( preg_match('#^\#redirect \[\[(.+?)\]\]#', $message, $m) && $redir && !isset($_GET['redirect']) || ( isset($_GET['redirect']) && $_GET['redirect'] != 'no' ) )
+ − 75
{
+ − 76
dc_here('render: looks like a redirect page to me...');
+ − 77
$old = $paths->cpage;
+ − 78
$a = RenderMan::strToPageID($m[1]);
+ − 79
$a[0] = str_replace(' ', '_', $a[0]);
+ − 80
+ − 81
$pageid = str_replace(' ', '_', $paths->nslist[$a[1]] . $a[0]);
+ − 82
$paths->page = $pageid;
+ − 83
$paths->cpage = $paths->pages[$pageid];
+ − 84
//die('<pre>'.print_r($paths->cpage,true).'</pre>');
+ − 85
+ − 86
dc_here('render: wreckin\' $template, and reloading the theme vars to match the new page<br />This might get messy!');
+ − 87
+ − 88
unset($template);
+ − 89
unset($GLOBALS['template']);
+ − 90
+ − 91
$GLOBALS['template'] = new template();
+ − 92
global $template;
+ − 93
+ − 94
$template->template(); // Tear down and rebuild the template parser
+ − 95
$template->load_theme($session->theme, $session->style);
+ − 96
+ − 97
$data = '<div><small>(Redirected from <a href="'.makeUrlNS($old['namespace'], $old['urlname_nons'], 'redirect=no', true).'">'.$old['name'].'</a>)</small></div>'.RenderMan::getPage($a[0], $a[1], $wiki, $smilies, $filter_links, false /* Enforces a maximum of one redirect */);
+ − 98
+ − 99
return $data;
+ − 100
}
+ − 101
else if(preg_match('#^\#redirect \[\[(.+?)\]\]#', $message, $m) && isset($_GET['redirect']) && $_GET['redirect'] == 'no')
+ − 102
{
+ − 103
dc_here('render: looks like a redirect page to me...');
+ − 104
dc_here('render: skipping redirect as requested on URI');
+ − 105
preg_match('#^\#redirect \[\[(.+)\]\]#', $message, $m);
+ − 106
$m[1] = str_replace(' ', '_', $m[1]);
+ − 107
$message = preg_replace('#\#redirect \[\[(.+)\]\]#', '<nowiki><div class="mdg-infobox"><table border="0" width="100%" cellspacing="0" cellpadding="0"><tr><td valign="top"><img alt="Cute wet-floor icon" src="'.scriptPath.'/images/redirector.png" /></td><td valign="top" style="padding-left: 10px;"><b>This page is a <i>redirector</i>.</b><br />This means that this page will not show its own content by default. Instead it will display the contents of the page it redirects to.<br /><br />To create a redirect page, make the <i>first characters</i> in the page content <tt>#redirect [[Page_ID]]</tt>. For more information, see the Enano <a href="http://enanocms.org/Help:Wiki_formatting">Wiki formatting guide</a>.<br /><br />This page redirects to <a href="'.makeUrl($m[1]).'">'.$paths->pages[$m[1]]['name'].'</a>.</td></tr></table></div><br /><hr style="margin-left: 1em; width: 200px;" /></nowiki>', $message);
+ − 108
}
+ − 109
$session->disallow_password_grab();
+ − 110
dc_here('render: alright, got the text, formatting...');
+ − 111
return ($render) ? RenderMan::render($message, $wiki, $smilies, $filter_links) : $message;
+ − 112
}
+ − 113
+ − 114
function getTemplate($id, $parms)
+ − 115
{
+ − 116
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 117
dc_here('render: template requested: '.$id);
+ − 118
if(!isset($paths->pages[$paths->nslist['Template'].$id]))
+ − 119
{
+ − 120
return '[['.$paths->nslist['Template'].$id.']]';
+ − 121
}
+ − 122
if(isset($paths->template_cache[$id]))
+ − 123
{
+ − 124
$text = $paths->template_cache[$id];
+ − 125
}
+ − 126
else
+ − 127
{
+ − 128
$text = RenderMan::getPage($id, 'Template', 0, true, true, 0);
+ − 129
$paths->template_cache[$id] = $text;
+ − 130
}
+ − 131
+ − 132
$text = preg_replace('/<noinclude>(.*?)<\/noinclude>/is', '', $text);
+ − 133
$text = preg_replace('/<nodisplay>(.*?)<\/nodisplay>/is', '\\1', $text);
+ − 134
+ − 135
preg_match_all('#\(_([0-9]+)_\)#', $text, $matchlist);
+ − 136
+ − 137
foreach($matchlist[1] as $m)
+ − 138
{
+ − 139
if(isset($parms[((int)$m)+1]))
+ − 140
{
+ − 141
$p = $parms[((int)$m)+1];
+ − 142
}
+ − 143
else
+ − 144
{
+ − 145
$p = '<b>Notice:</b> RenderMan::getTemplate(): Parameter '.$m.' is not set';
+ − 146
}
+ − 147
$text = str_replace('(_'.$m.'_)', $p, $text);
+ − 148
}
+ − 149
$text = RenderMan::include_templates($text);
+ − 150
return $text;
+ − 151
}
+ − 152
+ − 153
function fetch_template_text($id)
+ − 154
{
+ − 155
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 156
dc_here('render: template raw data requested: '.$id);
+ − 157
if(!isset($paths->pages[$paths->nslist['Template'].$id]))
+ − 158
{
+ − 159
return '[['.$paths->nslist['Template'].$id.']]';
+ − 160
}
+ − 161
if(isset($paths->template_cache[$id]))
+ − 162
{
+ − 163
$text = $paths->template_cache[$id];
+ − 164
}
+ − 165
else
+ − 166
{
+ − 167
$text = RenderMan::getPage($id, 'Template', 0, false, false, false, false);
+ − 168
$paths->template_cache[$id] = $text;
+ − 169
}
+ − 170
+ − 171
if ( is_string($text) )
+ − 172
{
+ − 173
$text = preg_replace('/<noinclude>(.*?)<\/noinclude>/is', '', $text);
+ − 174
$text = preg_replace('/<nodisplay>(.*?)<\/nodisplay>/is', '\\1', $text);
+ − 175
}
+ − 176
+ − 177
return $text;
+ − 178
}
+ − 179
+ − 180
function render($text, $wiki = 1, $smilies = true, $filter_links = true)
+ − 181
{
+ − 182
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 183
if($smilies)
+ − 184
{
+ − 185
$text = RenderMan::smilieyize($text);
+ − 186
}
+ − 187
if($wiki == 1)
+ − 188
{
+ − 189
$text = RenderMan::next_gen_wiki_format($text);
+ − 190
}
+ − 191
elseif($wiki == 2)
+ − 192
{
+ − 193
$text = $template->tplWikiFormat($text);
+ − 194
}
+ − 195
return $text;
+ − 196
}
+ − 197
+ − 198
function PlainTextRender($text, $wiki = 1, $smilies = false, $filter_links = true)
+ − 199
{
+ − 200
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 201
if($smilies)
+ − 202
{
+ − 203
$text = RenderMan::smilieyize($text);
+ − 204
}
+ − 205
if($wiki == 1)
+ − 206
{
+ − 207
$text = RenderMan::next_gen_wiki_format($text, true);
+ − 208
}
+ − 209
elseif($wiki == 2)
+ − 210
{
+ − 211
$text = $template->tplWikiFormat($text);
+ − 212
}
+ − 213
return $text;
+ − 214
}
+ − 215
+ − 216
function next_gen_wiki_format($text, $plaintext = false, $filter_links = true, $do_params = false)
+ − 217
{
+ − 218
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 219
$random_id = md5( time() . mt_rand() );
+ − 220
+ − 221
// Strip out <nowiki> sections and PHP code
+ − 222
+ − 223
$php = preg_match_all('#<\?php(.*?)\?>#is', $text, $phpsec);
+ − 224
+ − 225
for($i=0;$i<sizeof($phpsec[1]);$i++)
+ − 226
{
+ − 227
$text = str_replace('<?php'.$phpsec[1][$i].'?>', '{PHP:'.$random_id.':'.$i.'}', $text);
+ − 228
}
+ − 229
+ − 230
$nw = preg_match_all('#<nowiki>(.*?)<\/nowiki>#is', $text, $nowiki);
+ − 231
+ − 232
for($i=0;$i<sizeof($nowiki[1]);$i++)
+ − 233
{
+ − 234
$text = str_replace('<nowiki>'.$nowiki[1][$i].'</nowiki>', '{NOWIKI:'.$random_id.':'.$i.'}', $text);
+ − 235
}
+ − 236
+ − 237
$text = preg_replace('/<noinclude>(.*?)<\/noinclude>/is', '\\1', $text);
+ − 238
if ( $paths->namespace == 'Template' )
+ − 239
{
+ − 240
$text = preg_replace('/<nodisplay>(.*?)<\/nodisplay>/is', '', $text);
+ − 241
}
+ − 242
+ − 243
if ( !$plaintext )
+ − 244
{
+ − 245
// Process images
37
+ − 246
$text = RenderMan::process_image_tags($text, $taglist);
1
+ − 247
}
+ − 248
+ − 249
if($do_params)
+ − 250
{
+ − 251
preg_match_all('#\(_([0-9]+)_\)#', $text, $matchlist);
+ − 252
foreach($matchlist[1] as $m)
+ − 253
{
+ − 254
$text = str_replace('(_'.$m.'_)', $paths->getParam((int)$m), $text);
+ − 255
}
+ − 256
}
+ − 257
+ − 258
$text = RenderMan::include_templates($text);
+ − 259
+ − 260
$text = process_tables($text);
+ − 261
+ − 262
$wiki =& Text_Wiki::singleton('Mediawiki');
+ − 263
if($plaintext)
+ − 264
{
+ − 265
$wiki->setRenderConf('Plain', 'wikilink', 'view_url', contentPath);
+ − 266
$result = $wiki->transform($text, 'Plain');
+ − 267
}
+ − 268
else
+ − 269
{
+ − 270
$wiki->setRenderConf('Xhtml', 'wikilink', 'view_url', contentPath);
+ − 271
$wiki->setRenderConf('Xhtml', 'Url', 'css_descr', 'external');
+ − 272
$result = $wiki->transform($text, 'Xhtml');
+ − 273
}
+ − 274
37
+ − 275
if ( !$plaintext )
+ − 276
{
+ − 277
$result = RenderMan::process_imgtags_stage2($result, $taglist);
+ − 278
}
+ − 279
1
+ − 280
// Reinsert <nowiki> sections
+ − 281
for($i=0;$i<$nw;$i++)
+ − 282
{
+ − 283
$result = str_replace('{NOWIKI:'.$random_id.':'.$i.'}', $nowiki[1][$i], $result);
+ − 284
}
+ − 285
+ − 286
// Reinsert PHP
+ − 287
for($i=0;$i<$php;$i++)
+ − 288
{
+ − 289
$result = str_replace('{PHP:'.$random_id.':'.$i.'}', '<?php'.$phpsec[1][$i].'?>', $result);
+ − 290
}
+ − 291
+ − 292
return $result;
+ − 293
+ − 294
}
+ − 295
+ − 296
function wikiFormat($message, $filter_links = true, $do_params = false, $plaintext = false) {
+ − 297
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 298
+ − 299
return RenderMan::next_gen_wiki_format($message, $plaintext, $filter_links, $do_params);
+ − 300
+ − 301
$random_id = md5( time() . mt_rand() );
+ − 302
+ − 303
// Strip out <nowiki> sections
+ − 304
$nw = preg_match_all('#<nowiki>(.*?)<\/nowiki>#is', $message, $nowiki);
+ − 305
+ − 306
if(!$plaintext)
+ − 307
{
+ − 308
+ − 309
//return '<pre>'.print_r($nowiki,true).'</pre>';
+ − 310
+ − 311
for($i=0;$i<sizeof($nowiki[1]);$i++)
+ − 312
{
+ − 313
$message = str_replace('<nowiki>'.$nowiki[1][$i].'</nowiki>', '{NOWIKI:'.$random_id.':'.$i.'}', $message);
+ − 314
}
+ − 315
+ − 316
$message = preg_replace('/<noinclude>(.*?)<\/noinclude>/is', '\\1', $message);
+ − 317
+ − 318
//return '<pre>'.htmlspecialchars($message).'</pre>';
+ − 319
35
+ − 320
$message = RenderMan::process_image_tags($message);
1
+ − 321
+ − 322
}
+ − 323
+ − 324
if($do_params)
+ − 325
{
+ − 326
preg_match_all('#\(_([0-9]+)_\)#', $message, $matchlist);
+ − 327
foreach($matchlist[1] as $m)
+ − 328
{
+ − 329
$message = str_replace('(_'.$m.'_)', $paths->getParam((int)$m), $message);
+ − 330
}
+ − 331
}
+ − 332
+ − 333
$message = RenderMan::include_templates($message);
+ − 334
+ − 335
// Reinsert <nowiki> sections
+ − 336
for($i=0;$i<$nw;$i++)
+ − 337
{
+ − 338
$message = str_replace('{NOWIKI:'.$random_id.':'.$i.'}', '<nowiki>'.$nowiki[1][$i].'</nowiki>', $message);
+ − 339
}
+ − 340
+ − 341
$message = process_tables($message);
+ − 342
//if($message2 != $message) return '<pre>'.htmlspecialchars($message2).'</pre>';
+ − 343
//$message = str_replace(array('<table>', '</table>'), array('<nowiki><table>', '</table></nowiki>'), $message);
+ − 344
+ − 345
$wiki =& Text_Wiki::singleton('Mediawiki');
+ − 346
if($plaintext)
+ − 347
{
+ − 348
$wiki->setRenderConf('Plain', 'wikilink', 'view_url', contentPath);
+ − 349
$result = $wiki->transform($message, 'Plain');
+ − 350
} else {
+ − 351
$wiki->setRenderConf('Xhtml', 'wikilink', 'view_url', contentPath);
+ − 352
$wiki->setRenderConf('Xhtml', 'Url', 'css_descr', 'external');
+ − 353
$result = $wiki->transform($message, 'Xhtml');
+ − 354
}
+ − 355
+ − 356
// HTML fixes
+ − 357
$result = preg_replace('#<tr>([\s]*?)<\/tr>#is', '', $result);
+ − 358
$result = preg_replace('#<p>([\s]*?)<\/p>#is', '', $result);
+ − 359
$result = preg_replace('#<br />([\s]*?)<table#is', '<table', $result);
+ − 360
$result = str_replace("<pre><code>\n", "<pre><code>", $result);
+ − 361
$result = preg_replace("/<p><table([^>]*?)><\/p>/", "<table\\1>", $result);
+ − 362
$result = str_replace("<br />\n</td>", "\n</td>", $result);
+ − 363
$result = str_replace("<p><tr>", "<tr>", $result);
+ − 364
$result = str_replace("<tr><br />", "<tr>", $result);
+ − 365
$result = str_replace("</tr><br />", "</tr>", $result);
+ − 366
$result = str_replace("</table></p>", "</table>", $result);
+ − 367
$result = str_replace("</table><br />", "</table>", $result);
+ − 368
$result = preg_replace('/<\/table>$/', "</table><br /><br />", $result);
+ − 369
+ − 370
$result = str_replace('<nowiki>', '<nowiki>', $result);
+ − 371
$result = str_replace('</nowiki>', '</nowiki>', $result);
+ − 372
+ − 373
return $result;
+ − 374
}
+ − 375
+ − 376
function destroy_javascript($message, $_php = false)
+ − 377
{
+ − 378
$message = preg_replace('#<(script|object|applet|embed|iframe|frame|form|input|select)(.*?)>#is', '<\\1\\2>', $message);
+ − 379
$message = preg_replace('#</(script|object|applet|embed|iframe|frame|form|input|select)(.*?)>#is', '</\\1\\2>', $message);
+ − 380
$message = preg_replace('#(javascript|script|activex|chrome|about|applet):#is', '\\1:', $message);
+ − 381
if ( $_php )
+ − 382
{
+ − 383
// Left in only for compatibility
+ − 384
$message = preg_replace('#<(.*?)>#is', '<\\1>', $message);
+ − 385
$message = preg_replace('#<(.*?)>#is', '<\\1>', $message);
+ − 386
$message = preg_replace('#<(\?|\?php|%)(.*?)(\?|%)>#is', '<\\1\\2\\3>', $message);
+ − 387
// strip <a href="foo" onclick="bar();">-type attacks
+ − 388
$message = preg_replace('#<([a-zA-Z:\-]+) (.*?)on([A-Za-z]*)=(.*?)>#is', '<\\1\\2on\\3=\\4>', $message);
+ − 389
}
+ − 390
return $message;
+ − 391
}
+ − 392
+ − 393
function strip_php($message)
+ − 394
{
+ − 395
return RenderMan::destroy_javascript($message, true);
+ − 396
}
+ − 397
+ − 398
function sanitize_html($text)
+ − 399
{
+ − 400
$text = htmlspecialchars($text);
+ − 401
$allowed_tags = Array('b', 'i', 'u', 'pre', 'code', 'tt', 'br', 'p', 'nowiki', '!--([^.]+)--');
+ − 402
foreach($allowed_tags as $t)
+ − 403
{
+ − 404
$text = preg_replace('#<'.$t.'>(.*?)</'.$t.'>#is', '<'.$t.'>\\1</'.$t.'>', $text);
+ − 405
$text = preg_replace('#<'.$t.' />#is', '<'.$t.' />', $text);
+ − 406
$text = preg_replace('#<'.$t.'>#is', '<'.$t.'>', $text);
+ − 407
}
+ − 408
return $text;
+ − 409
}
+ − 410
+ − 411
/* *
+ − 412
* Replaces template inclusions with the templates
+ − 413
* @param string $message The text to format
+ − 414
* @return string
+ − 415
* /
+ − 416
+ − 417
function old_include_templates($message)
+ − 418
{
+ − 419
$random_id = md5( time() . mt_rand() );
+ − 420
preg_match_all('#\{\{(.+?)\}\}#s', $message, $matchlist);
+ − 421
foreach($matchlist[1] as $m)
+ − 422
{
+ − 423
$mn = $m;
+ − 424
// Strip out wikilinks and re-add them after the explosion (because of the "|")
+ − 425
preg_match_all('#\[\[(.+?)\]\]#i', $m, $linklist);
+ − 426
//echo '<pre>'.print_r($linklist, true).'</pre>';
+ − 427
for($i=0;$i<sizeof($linklist[1]);$i++)
+ − 428
{
+ − 429
$mn = str_replace('[['.$linklist[1][$i].']]', '{WIKILINK:'.$random_id.':'.$i.'}', $mn);
+ − 430
}
+ − 431
+ − 432
$ar = explode('|', $mn);
+ − 433
+ − 434
for($j=0;$j<sizeof($ar);$j++)
+ − 435
{
+ − 436
for($i=0;$i<sizeof($linklist[1]);$i++)
+ − 437
{
+ − 438
$ar[$j] = str_replace('{WIKILINK:'.$random_id.':'.$i.'}', '[['.$linklist[1][$i].']]', $ar[$j]);
+ − 439
}
+ − 440
}
+ − 441
+ − 442
$tp = $ar[0];
+ − 443
unset($ar[0]);
+ − 444
$tp = str_replace(' ', '_', $tp);
+ − 445
$message = str_replace('{{'.$m.'}}', RenderMan::getTemplate($tp, $ar), $message);
+ − 446
}
+ − 447
return $message;
+ − 448
}
+ − 449
*/
+ − 450
+ − 451
/**
+ − 452
* Parses a partial template tag in wikitext, and return an array with the parameters.
+ − 453
* @param string The portion of the template tag that contains the parameters. Example:
+ − 454
* <code>
+ − 455
* foo = lorem ipsum
+ − 456
* bar = dolor sit amet
+ − 457
* </code>
+ − 458
* @return array Example:
+ − 459
* [foo] => lorem ipsum
+ − 460
* [bar] => dolor sit amet
+ − 461
*/
+ − 462
+ − 463
function parse_template_vars($input)
+ − 464
{
+ − 465
$input = explode("\n", trim( $input ));
+ − 466
$parms = Array();
+ − 467
$current_line = '';
+ − 468
$current_parm = '';
+ − 469
foreach ( $input as $num => $line )
+ − 470
{
+ − 471
if ( preg_match('/^([ ]*?)([A-z0-9_]+?)([ ]*?)=([ ]*?)(.+?)$/i', $line, $matches) )
+ − 472
{
+ − 473
$parm =& $matches[2];
+ − 474
$text =& $matches[5];
+ − 475
if ( $parm == $current_parm )
+ − 476
{
+ − 477
$current_line .= $text;
+ − 478
}
+ − 479
else
+ − 480
{
+ − 481
// New parameter
+ − 482
if ( $current_parm != '' )
+ − 483
$parms[$current_parm] = $current_line;
+ − 484
$current_line = $text;
+ − 485
$current_parm = $parm;
+ − 486
}
+ − 487
}
+ − 488
else if ( $num == 0 )
+ − 489
{
+ − 490
// Syntax error
+ − 491
return false;
+ − 492
}
+ − 493
else
+ − 494
{
+ − 495
$current_line .= "\n$line";
+ − 496
}
+ − 497
}
+ − 498
if ( !empty($current_parm) && !empty($current_line) )
+ − 499
{
+ − 500
$parms[$current_parm] = $current_line;
+ − 501
}
+ − 502
return $parms;
+ − 503
}
+ − 504
+ − 505
/**
+ − 506
* Processes all template tags within a block of wikitext.
+ − 507
* @param string The text to process
+ − 508
* @return string Formatted text
+ − 509
* @example
+ − 510
* <code>
+ − 511
$text = '{{Template
+ − 512
parm1 = Foo
+ − 513
parm2 = Bar
+ − 514
}}';
+ − 515
$text = include_templates($text);
+ − 516
* </code>
+ − 517
*/
+ − 518
+ − 519
function include_templates($text)
+ − 520
{
+ − 521
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 522
$template_regex = "/\{\{([A-z0-9_-]+?)((\n([ ]*?)[A-z0-9]+([ ]*?)=([ ]*?)(.+?))*)\}\}/is";
+ − 523
if ( $count = preg_match_all($template_regex, $text, $matches) )
+ − 524
{
+ − 525
for ( $i = 0; $i < $count; $i++ )
+ − 526
{
+ − 527
$parmsection = trim($matches[2][$i]);
+ − 528
if ( !empty($parmsection) )
+ − 529
{
+ − 530
$parms = RenderMan::parse_template_vars($parmsection);
+ − 531
foreach ( $parms as $j => $parm )
+ − 532
{
+ − 533
$parms[$j] = $parm;
+ − 534
}
+ − 535
}
+ − 536
else
+ − 537
{
+ − 538
$parms = Array();
+ − 539
}
+ − 540
if ( $tpl_code = RenderMan::fetch_template_text($matches[1][$i]) )
+ − 541
{
+ − 542
$parser = $template->makeParserText($tpl_code);
+ − 543
$parser->assign_vars($parms);
+ − 544
$text = str_replace($matches[0][$i], $parser->run(), $text);
+ − 545
}
+ − 546
}
+ − 547
}
+ − 548
return $text;
+ − 549
}
+ − 550
+ − 551
/**
+ − 552
* Preprocesses an HTML text string prior to being sent to MySQL.
+ − 553
* @param string $text
+ − 554
* @param bool $strip_all_php - if true, strips all PHP regardless of user permissions. Else, strips PHP only if user level < USER_LEVEL_ADMIN.
+ − 555
*/
+ − 556
function preprocess_text($text, $strip_all_php = true, $sqlescape = true)
+ − 557
{
+ − 558
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 559
$random_id = md5( time() . mt_rand() );
+ − 560
+ − 561
$can_do_php = ( $session->get_permissions('php_in_pages') && !$strip_all_php );
+ − 562
+ − 563
if ( !$can_do_php )
+ − 564
{
24
+ − 565
$text = sanitize_html($text, true);
1
+ − 566
// If we can't do PHP, we can't do Javascript either.
+ − 567
$text = RenderMan::destroy_javascript($text);
+ − 568
}
+ − 569
+ − 570
// Strip out <nowiki> sections and PHP code
+ − 571
+ − 572
$php = preg_match_all('#(<|<)\?php(.*?)\?(>|>)#is', $text, $phpsec);
+ − 573
+ − 574
//die('<pre>'.htmlspecialchars(print_r($phpsec, true))."\n".htmlspecialchars(print_r($text, true)).'</pre>');
+ − 575
+ − 576
for($i=0;$i<sizeof($phpsec[1]);$i++)
+ − 577
{
+ − 578
$text = str_replace($phpsec[0][$i], '{PHP:'.$random_id.':'.$i.'}', $text);
+ − 579
}
+ − 580
+ − 581
$nw = preg_match_all('#<nowiki>(.*?)<\/nowiki>#is', $text, $nowiki);
+ − 582
+ − 583
for($i=0;$i<sizeof($nowiki[1]);$i++)
+ − 584
{
+ − 585
$text = str_replace('<nowiki>'.$nowiki[1][$i].'</nowiki>', '{NOWIKI:'.$random_id.':'.$i.'}', $text);
+ − 586
}
+ − 587
+ − 588
$text = str_replace('~~~~~', date('G:i, j F Y (T)'), $text);
+ − 589
$text = str_replace('~~~~', "[[User:$session->username|$session->username]] ".date('G:i, j F Y (T)'), $text);
+ − 590
$text = str_replace('~~~', "[[User:$session->username|$session->username]] ", $text);
+ − 591
+ − 592
// Reinsert <nowiki> sections
+ − 593
for($i=0;$i<$nw;$i++)
+ − 594
{
+ − 595
$text = str_replace('{NOWIKI:'.$random_id.':'.$i.'}', '<nowiki>'.$nowiki[1][$i].'</nowiki>', $text);
+ − 596
}
+ − 597
// Reinsert PHP
+ − 598
for($i=0;$i<$php;$i++)
+ − 599
{
+ − 600
$phsec = ''.$phpsec[1][$i].'?php'.$phpsec[2][$i].'?'.$phpsec[3][$i].'';
+ − 601
if ( $strip_all_php )
+ − 602
$phsec = htmlspecialchars($phsec);
+ − 603
$text = str_replace('{PHP:'.$random_id.':'.$i.'}', $phsec, $text);
+ − 604
}
+ − 605
+ − 606
$text = ( $sqlescape ) ? $db->escape($text) : $text;
+ − 607
+ − 608
return $text;
+ − 609
}
+ − 610
+ − 611
function smilieyize($text, $complete_urls = false)
+ − 612
{
+ − 613
+ − 614
$random_id = md5( time() . mt_rand() );
+ − 615
+ − 616
// Smileys array - eventually this will be fetched from the database by
+ − 617
// RenderMan::initSmileys during initialization, but it will all be hardcoded for beta 2
+ − 618
+ − 619
$smileys = Array(
+ − 620
'O:-)' => 'face-angel.png',
+ − 621
'O:)' => 'face-angel.png',
+ − 622
'O=)' => 'face-angel.png',
+ − 623
':-)' => 'face-smile.png',
+ − 624
':)' => 'face-smile.png',
+ − 625
'=)' => 'face-smile-big.png',
+ − 626
':-(' => 'face-sad.png',
+ − 627
':(' => 'face-sad.png',
+ − 628
';(' => 'face-sad.png',
+ − 629
':-O' => 'face-surprise.png',
+ − 630
';-)' => 'face-wink.png',
+ − 631
';)' => 'face-wink.png',
+ − 632
'8-)' => 'face-glasses.png',
+ − 633
'8)' => 'face-glasses.png',
+ − 634
':-D' => 'face-grin.png',
+ − 635
':D' => 'face-grin.png',
+ − 636
'=D' => 'face-grin.png',
+ − 637
':-*' => 'face-kiss.png',
+ − 638
':*' => 'face-kiss.png',
+ − 639
'=*' => 'face-kiss.png',
+ − 640
':\'(' => 'face-crying.png',
+ − 641
':-|' => 'face-plain.png',
+ − 642
':-\\' => 'face-plain.png',
+ − 643
':-/' => 'face-plain.png',
+ − 644
':joke:' => 'face-plain.png',
+ − 645
']:->' => 'face-devil-grin.png',
+ − 646
':kiss:' => 'face-kiss.png',
+ − 647
':-P' => 'face-tongue-out.png',
+ − 648
':P' => 'face-tongue-out.png',
+ − 649
':-p' => 'face-tongue-out.png',
+ − 650
':p' => 'face-tongue-out.png',
+ − 651
':-X' => 'face-sick.png',
+ − 652
':X' => 'face-sick.png',
+ − 653
':sick:' => 'face-sick.png',
+ − 654
':-]' => 'face-oops.png',
+ − 655
':]' => 'face-oops.png',
+ − 656
':oops:' => 'face-oops.png',
+ − 657
':-[' => 'face-embarassed.png',
+ − 658
':[' => 'face-embarassed.png'
+ − 659
);
+ − 660
/*
+ − 661
$keys = array_keys($smileys);
+ − 662
foreach($keys as $k)
+ − 663
{
+ − 664
$regex1 = '#([\W]+)'.preg_quote($k).'([\s\n\r\.]+)#s';
+ − 665
$regex2 = '\\1<img alt="'.$k.'" title="'.$k.'" src="'.scriptPath.'/images/smilies/'.$smileys[$k].'" style="border: 0;" />\\2';
+ − 666
$text = preg_replace($regex1, $regex2, $text);
+ − 667
}
+ − 668
*/
+ − 669
+ − 670
// Strip out <nowiki> sections
+ − 671
//return '<pre>'.htmlspecialchars($text).'</pre>';
+ − 672
$nw = preg_match_all('#<nowiki>(.*?)<\/nowiki>#is', $text, $nowiki);
+ − 673
+ − 674
for($i=0;$i<sizeof($nowiki[1]);$i++)
+ − 675
{
+ − 676
$text = str_replace('<nowiki>'.$nowiki[1][$i].'</nowiki>', '{NOWIKI:'.$random_id.':'.$i.'}', $text);
+ − 677
}
+ − 678
+ − 679
$keys = array_keys($smileys);
+ − 680
foreach($keys as $k)
+ − 681
{
+ − 682
$t = str_hex($k);
+ − 683
$t = explode(' ', $t);
+ − 684
$s = '';
+ − 685
foreach($t as $b)
+ − 686
{
+ − 687
$s.='&#x'.$b.';';
+ − 688
}
+ − 689
$pfx = ( $complete_urls ) ? 'http' . ( isset($_SERVER['HTTPS']) ? 's' : '' ) . '://'.$_SERVER['HTTP_HOST'] : '';
+ − 690
$text = str_replace(' '.$k, ' <nowiki><img title="'.$s.'" alt="'.$s.'" src="'.$pfx.scriptPath.'/images/smilies/'.$smileys[$k].'" style="border: 0;" /></nowiki>', $text);
+ − 691
}
+ − 692
//*/
+ − 693
+ − 694
// Reinsert <nowiki> sections
+ − 695
for($i=0;$i<$nw;$i++)
+ − 696
{
+ − 697
$text = str_replace('{NOWIKI:'.$random_id.':'.$i.'}', '<nowiki>'.$nowiki[1][$i].'</nowiki>', $text);
+ − 698
}
+ − 699
+ − 700
return $text;
+ − 701
}
+ − 702
+ − 703
/*
+ − 704
* **** DEPRECATED ****
+ − 705
* Replaces some critical characters in a string with MySQL-safe equivalents
+ − 706
* @param $text string the text to escape
+ − 707
* @return array key 0 is the escaped text, key 1 is the character tag
+ − 708
* /
+ − 709
+ − 710
function escape_page_text($text)
+ − 711
{
+ − 712
$char_tag = md5(microtime() . mt_rand());
+ − 713
$text = str_replace("'", "{APOS:$char_tag}", $text);
+ − 714
$text = str_replace('"', "{QUOT:$char_tag}", $text);
+ − 715
$text = str_replace("\\", "{SLASH:$char_tag}", $text);
+ − 716
return Array($text, $char_tag);
+ − 717
}
+ − 718
*/
+ − 719
+ − 720
/* **** DEPRECATED ****
+ − 721
* Reverses the result of RenderMan::escape_page_text().
+ − 722
* @param $text string the text to unescape
+ − 723
* @param $char_tag string the character tag
+ − 724
* @return string
+ − 725
* /
+ − 726
+ − 727
function unescape_page_text($text, $char_tag)
+ − 728
{
+ − 729
$text = str_replace("{APOS:$char_tag}", "'", $text);
+ − 730
$text = str_replace("{QUOT:$char_tag}", '"', $text);
+ − 731
$text = str_replace("{SLASH:$char_tag}", "\\", $text);
+ − 732
return $text;
+ − 733
}
+ − 734
*/
+ − 735
+ − 736
/**
+ − 737
* Generates a summary of the differences between two texts, and formats it as XHTML.
+ − 738
* @param $str1 string the first block of text
+ − 739
* @param $str2 string the second block of text
+ − 740
* @return string
+ − 741
*/
+ − 742
function diff($str1, $str2)
+ − 743
{
+ − 744
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 745
$str1 = explode("\n", $str1);
+ − 746
$str2 = explode("\n", $str2);
+ − 747
$diff = new Diff($str1, $str2);
+ − 748
$renderer = new TableDiffFormatter();
+ − 749
return '<table class="diff">'.$renderer->format($diff).'</table>';
+ − 750
}
+ − 751
35
+ − 752
/**
+ − 753
* Changes wikitext image tags to HTML.
+ − 754
* @param string The wikitext to process
37
+ − 755
* @param array Will be overwritten with the list of HTML tags (the system uses tokens for TextWiki compatibility)
35
+ − 756
* @return string
+ − 757
*/
+ − 758
37
+ − 759
function process_image_tags($text, &$taglist)
35
+ − 760
{
+ − 761
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 762
37
+ − 763
$s_delim = "\xFF";
+ − 764
$f_delim = "\xFF";
+ − 765
$taglist = array();
+ − 766
35
+ − 767
// Wicked huh?
+ − 768
$regex = '/\[\[:' . $paths->nslist['File'] . '([\w\s0-9_\(\)!@%\^\+\|\.-]+?)((\|thumb)|(\|([0-9]+)x([0-9]+)))?(\|left|\|right)?(\|(.+))?\]\]/i';
+ − 769
+ − 770
preg_match_all($regex, $text, $matches);
+ − 771
+ − 772
foreach ( $matches[0] as $i => $match )
+ − 773
{
+ − 774
+ − 775
$full_tag =& $matches[0][$i];
+ − 776
$filename =& $matches[1][$i];
+ − 777
$scale_type =& $matches[2][$i];
+ − 778
$width =& $matches[5][$i];
+ − 779
$height =& $matches[6][$i];
+ − 780
$clear =& $matches[7][$i];
+ − 781
$caption =& $matches[8][$i];
+ − 782
+ − 783
if ( !isPage( $paths->nslist['File'] . $filename ) )
+ − 784
{
+ − 785
continue;
+ − 786
}
+ − 787
+ − 788
if ( $scale_type == '|thumb' )
+ − 789
{
+ − 790
$r_width = 225;
+ − 791
$r_height = 225;
+ − 792
+ − 793
$url = makeUrlNS('Special', 'DownloadFile/' . $filename, 'preview&width=' . $r_width . '&height=' . $r_height, true);
+ − 794
}
+ − 795
else if ( !empty($width) && !empty($height) )
+ − 796
{
+ − 797
$r_width = $width;
+ − 798
$r_height = $height;
+ − 799
+ − 800
$url = makeUrlNS('Special', 'DownloadFile/' . $filename, 'preview&width=' . $r_width . '&height=' . $r_height, true);
+ − 801
}
+ − 802
else
+ − 803
{
+ − 804
$url = makeUrlNS('Special', 'DownloadFile/' . $filename);
+ − 805
}
+ − 806
+ − 807
$img_tag = '<img src="' . $url . '" ';
+ − 808
+ − 809
if ( isset($r_width) && isset($r_height) && $scale_type != '|thumb' )
+ − 810
{
+ − 811
$img_tag .= 'width="' . $r_width . '" height="' . $r_height . '" ';
+ − 812
}
+ − 813
+ − 814
$img_tag .= 'style="border-width: 0px; background-color: white;" ';
+ − 815
+ − 816
$img_tag .= '/>';
+ − 817
+ − 818
$complete_tag = '';
+ − 819
+ − 820
if ( !empty($scale_type) )
+ − 821
{
+ − 822
$complete_tag .= '<div class="thumbnail" ';
+ − 823
$clear_text = '';
+ − 824
if ( !empty($clear) )
+ − 825
{
+ − 826
$side = ( $clear == '|left' ) ? 'left' : 'right';
+ − 827
$opposite = ( $clear == '|left' ) ? 'right' : 'left';
37
+ − 828
$clear_text .= "float: $side; margin-$opposite: 20px;";
35
+ − 829
$complete_tag .= 'style="' . $clear_text . '" ';
+ − 830
}
+ − 831
$complete_tag .= '>';
+ − 832
+ − 833
$complete_tag .= '<a href="' . makeUrlNS('File', $filename) . '" style="display: block;">';
+ − 834
$complete_tag .= $img_tag;
+ − 835
$complete_tag .= '</a>';
+ − 836
+ − 837
$mag_button = '<a href="' . makeUrlNS('File', $filename) . '" style="display: block; float: right; clear: right; margin: 0 0 10px 10px;"><img alt="[ + ]" src="' . scriptPath . '/images/thumbnail.png" style="border-width: 0px;" /></a>';
+ − 838
+ − 839
if ( !empty($caption) )
+ − 840
{
+ − 841
$cap = substr($caption, 1);
+ − 842
$complete_tag .= $mag_button . $cap;
+ − 843
}
+ − 844
+ − 845
$complete_tag .= '</div>';
+ − 846
}
+ − 847
else
+ − 848
{
+ − 849
$complete_tag .= '<a href="' . makeUrlNS('File', $filename) . '" style="display: block;">';
+ − 850
$complete_tag .= $img_tag;
+ − 851
$complete_tag .= '</a>';
+ − 852
}
+ − 853
37
+ − 854
$complete_tag .= "\n\n";
+ − 855
$taglist[$i] = $complete_tag;
35
+ − 856
37
+ − 857
$pos = strpos($text, $full_tag);
35
+ − 858
+ − 859
while(true)
+ − 860
{
+ − 861
$check1 = substr($text, $pos, 3);
+ − 862
$check2 = substr($text, $pos, 1);
+ − 863
if ( $check1 == '<p>' || $pos == 0 || $check2 == "\n" )
+ − 864
{
+ − 865
// die('found at pos '.$pos);
+ − 866
break;
+ − 867
}
+ − 868
$pos--;
+ − 869
}
+ − 870
37
+ − 871
$repl = "{$s_delim}e_img_{$i}{$f_delim}";
+ − 872
$text = substr($text, 0, $pos) . $repl . substr($text, $pos);
35
+ − 873
+ − 874
$text = str_replace($full_tag, '', $text);
+ − 875
+ − 876
unset($full_tag, $filename, $scale_type, $width, $height, $clear, $caption, $r_width, $r_height);
+ − 877
+ − 878
}
+ − 879
+ − 880
return $text;
+ − 881
}
+ − 882
37
+ − 883
/**
+ − 884
* Finalizes processing of image tags.
+ − 885
* @param string The preprocessed text
+ − 886
* @param array The list of image tags created by RenderMan::process_image_tags()
+ − 887
*/
+ − 888
+ − 889
function process_imgtags_stage2($text, $taglist)
+ − 890
{
+ − 891
$s_delim = "\xFF";
+ − 892
$f_delim = "\xFF";
+ − 893
foreach ( $taglist as $i => $tag )
+ − 894
{
+ − 895
$repl = "{$s_delim}e_img_{$i}{$f_delim}";
+ − 896
$text = str_replace($repl, $tag, $text);
+ − 897
}
+ − 898
return $text;
+ − 899
}
+ − 900
1
+ − 901
}
+ − 902
+ − 903
?>