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