PageRenderTime 23ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/html.php

https://github.com/miya5n/pukiwiki
PHP | 538 lines | 409 code | 72 blank | 57 comment | 70 complexity | 3b3b32de8408e4b329c79884857b5e3c MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. // PukiWiki - Yet another WikiWikiWeb clone.
  3. // $Id: html.php,v 1.66 2011/01/25 15:01:01 henoheno Exp $
  4. // Copyright (C)
  5. // 2002-2007 PukiWiki Developers Team
  6. // 2001-2002 Originally written by yu-ji
  7. // License: GPL v2 or (at your option) any later version
  8. //
  9. // HTML-publishing related functions
  10. // Show page-content
  11. function catbody($title, $page, $body)
  12. {
  13. global $script, $vars, $arg, $defaultpage, $whatsnew, $help_page, $hr;
  14. global $attach_link, $related_link, $function_freeze;
  15. global $search_word_color, $_msg_word, $foot_explain, $note_hr, $head_tags;
  16. global $javascript, $nofollow;
  17. global $_LANG, $_LINK, $_IMAGE;
  18. global $pkwk_dtd; // XHTML 1.1, XHTML1.0, HTML 4.01 Transitional...
  19. global $page_title; // Title of this site
  20. global $do_backup; // Do backup or not
  21. global $modifier; // Site administrator's web page
  22. global $modifierlink; // Site administrator's name
  23. if (! file_exists(SKIN_FILE) || ! is_readable(SKIN_FILE))
  24. die_message('SKIN_FILE is not found');
  25. $_LINK = $_IMAGE = array();
  26. // Add JavaScript header when ...
  27. if (! PKWK_ALLOW_JAVASCRIPT) unset($javascript);
  28. $_page = isset($vars['page']) ? $vars['page'] : '';
  29. $r_page = rawurlencode($_page);
  30. // Set $_LINK for skin
  31. $_LINK['add'] = "$script?cmd=add&amp;page=$r_page";
  32. $_LINK['backup'] = "$script?cmd=backup&amp;page=$r_page";
  33. $_LINK['copy'] = "$script?plugin=template&amp;refer=$r_page";
  34. $_LINK['diff'] = "$script?cmd=diff&amp;page=$r_page";
  35. $_LINK['edit'] = "$script?cmd=edit&amp;page=$r_page";
  36. $_LINK['filelist'] = "$script?cmd=filelist";
  37. $_LINK['freeze'] = "$script?cmd=freeze&amp;page=$r_page";
  38. $_LINK['help'] = "$script?" . rawurlencode($help_page);
  39. $_LINK['list'] = "$script?cmd=list";
  40. $_LINK['new'] = "$script?plugin=newpage&amp;refer=$r_page";
  41. $_LINK['rdf'] = "$script?cmd=rss&amp;ver=1.0";
  42. $_LINK['recent'] = "$script?" . rawurlencode($whatsnew);
  43. $_LINK['reload'] = "$script?$r_page";
  44. $_LINK['rename'] = "$script?plugin=rename&amp;refer=$r_page";
  45. $_LINK['rss'] = "$script?cmd=rss";
  46. $_LINK['rss10'] = "$script?cmd=rss&amp;ver=1.0"; // Same as 'rdf'
  47. $_LINK['rss20'] = "$script?cmd=rss&amp;ver=2.0";
  48. $_LINK['search'] = "$script?cmd=search";
  49. $_LINK['top'] = "$script?" . rawurlencode($defaultpage);
  50. $_LINK['unfreeze'] = "$script?cmd=unfreeze&amp;page=$r_page";
  51. $_LINK['upload'] = "$script?plugin=attach&amp;pcmd=upload&amp;page=$r_page";
  52. // Compat: Skins for 1.4.4 and before
  53. $link_add = & $_LINK['add'];
  54. $link_new = & $_LINK['new']; // New!
  55. $link_edit = & $_LINK['edit'];
  56. $link_diff = & $_LINK['diff'];
  57. $link_top = & $_LINK['top'];
  58. $link_list = & $_LINK['list'];
  59. $link_filelist = & $_LINK['filelist'];
  60. $link_search = & $_LINK['search'];
  61. $link_whatsnew = & $_LINK['recent'];
  62. $link_backup = & $_LINK['backup'];
  63. $link_help = & $_LINK['help'];
  64. $link_trackback = ''; // Removed (compat)
  65. $link_rdf = & $_LINK['rdf']; // New!
  66. $link_rss = & $_LINK['rss'];
  67. $link_rss10 = & $_LINK['rss10']; // New!
  68. $link_rss20 = & $_LINK['rss20']; // New!
  69. $link_freeze = & $_LINK['freeze'];
  70. $link_unfreeze = & $_LINK['unfreeze'];
  71. $link_upload = & $_LINK['upload'];
  72. $link_template = & $_LINK['copy'];
  73. $link_refer = ''; // Removed (compat)
  74. $link_rename = & $_LINK['rename'];
  75. // Init flags
  76. $is_page = (is_pagename($_page) && ! arg_check('backup') && ! is_cantedit($_page));
  77. $is_read = (arg_check('read') && is_page($_page));
  78. $is_freeze = is_freeze($_page);
  79. // Last modification date (string) of the page
  80. $lastmodified = $is_read ? format_date(get_filetime($_page)) .
  81. ' ' . get_pg_passage($_page, FALSE) : '';
  82. // List of attached files to the page
  83. $attaches = ($attach_link && $is_read && exist_plugin_action('attach')) ?
  84. attach_filelist() : '';
  85. // List of related pages
  86. $related = ($related_link && $is_read) ? make_related($_page) : '';
  87. // List of footnotes
  88. ksort($foot_explain, SORT_NUMERIC);
  89. $notes = ! empty($foot_explain) ? $note_hr . join("\n", $foot_explain) : '';
  90. // Tags will be inserted into <head></head>
  91. $head_tag = ! empty($head_tags) ? join("\n", $head_tags) ."\n" : '';
  92. // 1.3.x compat
  93. // Last modification date (UNIX timestamp) of the page
  94. $fmt = $is_read ? get_filetime($_page) + LOCALZONE : 0;
  95. // Search words
  96. if ($search_word_color && isset($vars['word'])) {
  97. $body = '<div class="small">' . $_msg_word . htmlsc($vars['word']) .
  98. '</div>' . $hr . "\n" . $body;
  99. // BugTrack2/106: Only variables can be passed by reference from PHP 5.0.5
  100. // with array_splice(), array_flip()
  101. $words = preg_split('/\s+/', $vars['word'], -1, PREG_SPLIT_NO_EMPTY);
  102. $words = array_splice($words, 0, 10); // Max: 10 words
  103. $words = array_flip($words);
  104. $keys = array();
  105. foreach ($words as $word=>$id) $keys[$word] = strlen($word);
  106. arsort($keys, SORT_NUMERIC);
  107. $keys = get_search_words(array_keys($keys), TRUE);
  108. $id = 0;
  109. foreach ($keys as $key=>$pattern) {
  110. $s_key = htmlsc($key);
  111. $pattern = '/' .
  112. '<textarea[^>]*>.*?<\/textarea>' . // Ignore textareas
  113. '|' . '<[^>]*>' . // Ignore tags
  114. '|' . '&[^;]+;' . // Ignore entities
  115. '|' . '(' . $pattern . ')' . // $matches[1]: Regex for a search word
  116. '/sS';
  117. $decorate_Nth_word = create_function(
  118. '$matches',
  119. 'return (isset($matches[1])) ? ' .
  120. '\'<strong class="word' .
  121. $id .
  122. '">\' . $matches[1] . \'</strong>\' : ' .
  123. '$matches[0];'
  124. );
  125. $body = preg_replace_callback($pattern, $decorate_Nth_word, $body);
  126. $notes = preg_replace_callback($pattern, $decorate_Nth_word, $notes);
  127. ++$id;
  128. }
  129. }
  130. // Compat: 'HTML convert time' without time about MenuBar and skin
  131. $taketime = elapsedtime();
  132. require(SKIN_FILE);
  133. }
  134. // Show 'edit' form
  135. function edit_form($page, $postdata, $digest = FALSE, $b_template = TRUE)
  136. {
  137. global $script, $vars, $rows, $cols, $hr, $function_freeze;
  138. global $_btn_preview, $_btn_repreview, $_btn_update, $_btn_cancel, $_msg_help;
  139. global $_btn_template, $_btn_load, $load_template_func;
  140. global $notimeupdate;
  141. // Newly generate $digest or not
  142. if ($digest === FALSE) $digest = md5(get_source($page, TRUE, TRUE));
  143. $refer = $template = '';
  144. // Add plugin
  145. $addtag = $add_top = '';
  146. if(isset($vars['add'])) {
  147. global $_btn_addtop;
  148. $addtag = '<input type="hidden" name="add" value="true" />';
  149. $add_top = isset($vars['add_top']) ? ' checked="checked"' : '';
  150. $add_top = '<input type="checkbox" name="add_top" ' .
  151. 'id="_edit_form_add_top" value="true"' . $add_top . ' />' . "\n" .
  152. ' <label for="_edit_form_add_top">' .
  153. '<span class="small">' . $_btn_addtop . '</span>' .
  154. '</label>';
  155. }
  156. if($load_template_func && $b_template) {
  157. $pages = array();
  158. foreach(get_existpages() as $_page) {
  159. if (is_cantedit($_page) || check_non_list($_page))
  160. continue;
  161. $s_page = htmlsc($_page);
  162. $pages[$_page] = ' <option value="' . $s_page . '">' .
  163. $s_page . '</option>';
  164. }
  165. ksort($pages, SORT_STRING);
  166. $s_pages = join("\n", $pages);
  167. $template = <<<EOD
  168. <select name="template_page">
  169. <option value="">-- $_btn_template --</option>
  170. $s_pages
  171. </select>
  172. <input type="submit" name="template" value="$_btn_load" accesskey="r" />
  173. <br />
  174. EOD;
  175. if (isset($vars['refer']) && $vars['refer'] != '')
  176. $refer = '[[' . strip_bracket($vars['refer']) . ']]' . "\n\n";
  177. }
  178. $r_page = rawurlencode($page);
  179. $s_page = htmlsc($page);
  180. $s_digest = htmlsc($digest);
  181. $s_postdata = htmlsc($refer . $postdata);
  182. $s_original = isset($vars['original']) ? htmlsc($vars['original']) : $s_postdata;
  183. $b_preview = isset($vars['preview']); // TRUE when preview
  184. $btn_preview = $b_preview ? $_btn_repreview : $_btn_preview;
  185. // Checkbox 'do not change timestamp'
  186. $add_notimestamp = '';
  187. if ($notimeupdate != 0) {
  188. global $_btn_notchangetimestamp;
  189. $checked_time = isset($vars['notimestamp']) ? ' checked="checked"' : '';
  190. // Only for administrator
  191. if ($notimeupdate == 2) {
  192. $add_notimestamp = ' ' .
  193. '<input type="password" name="pass" size="12" />' . "\n";
  194. }
  195. $add_notimestamp = '<input type="checkbox" name="notimestamp" ' .
  196. 'id="_edit_form_notimestamp" value="true"' . $checked_time . ' />' . "\n" .
  197. ' ' . '<label for="_edit_form_notimestamp"><span class="small">' .
  198. $_btn_notchangetimestamp . '</span></label>' . "\n" .
  199. $add_notimestamp .
  200. '&nbsp;';
  201. }
  202. // 'margin-bottom', 'float:left', and 'margin-top'
  203. // are for layout of 'cancel button'
  204. $body = <<<EOD
  205. <div class="edit_form">
  206. <form action="$script" method="post" style="margin-bottom:0px;">
  207. $template
  208. $addtag
  209. <input type="hidden" name="cmd" value="edit" />
  210. <input type="hidden" name="page" value="$s_page" />
  211. <input type="hidden" name="digest" value="$s_digest" />
  212. <textarea name="msg" rows="$rows" cols="$cols">$s_postdata</textarea>
  213. <br />
  214. <div style="float:left;">
  215. <input type="submit" name="preview" value="$btn_preview" accesskey="p" />
  216. <input type="submit" name="write" value="$_btn_update" accesskey="s" />
  217. $add_top
  218. $add_notimestamp
  219. </div>
  220. <textarea name="original" rows="1" cols="1" style="display:none">$s_original</textarea>
  221. </form>
  222. <form action="$script" method="post" style="margin-top:0px;">
  223. <input type="hidden" name="cmd" value="edit" />
  224. <input type="hidden" name="page" value="$s_page" />
  225. <input type="submit" name="cancel" value="$_btn_cancel" accesskey="c" />
  226. </form>
  227. </div>
  228. EOD;
  229. if (isset($vars['help'])) {
  230. $body .= $hr . catrule();
  231. } else {
  232. $body .= '<ul><li><a href="' .
  233. $script . '?cmd=edit&amp;help=true&amp;page=' . $r_page .
  234. '">' . $_msg_help . '</a></li></ul>';
  235. }
  236. return $body;
  237. }
  238. // Related pages
  239. function make_related($page, $tag = '')
  240. {
  241. global $script, $vars, $rule_related_str, $related_str;
  242. global $_ul_left_margin, $_ul_margin, $_list_pad_str;
  243. $links = links_get_related($page);
  244. if ($tag) {
  245. ksort($links, SORT_STRING); // Page name, alphabetical order
  246. } else {
  247. arsort($links, SORT_NUMERIC); // Last modified date, newer
  248. }
  249. $_links = array();
  250. foreach ($links as $page=>$lastmod) {
  251. if (check_non_list($page)) continue;
  252. $r_page = rawurlencode($page);
  253. $s_page = htmlsc($page);
  254. $passage = get_passage($lastmod);
  255. $_links[] = $tag ?
  256. '<a href="' . $script . '?' . $r_page . '" title="' .
  257. $s_page . ' ' . $passage . '">' . $s_page . '</a>' :
  258. '<a href="' . $script . '?' . $r_page . '">' .
  259. $s_page . '</a>' . $passage;
  260. }
  261. if (empty($_links)) return ''; // Nothing
  262. if ($tag == 'p') { // From the line-head
  263. $margin = $_ul_left_margin + $_ul_margin;
  264. $style = sprintf($_list_pad_str, 1, $margin, $margin);
  265. $retval = "\n" . '<ul' . $style . '>' . "\n" .
  266. '<li>' . join($rule_related_str, $_links) . '</li>' . "\n" .
  267. '</ul>' . "\n";
  268. } else if ($tag) {
  269. $retval = join($rule_related_str, $_links);
  270. } else {
  271. $retval = join($related_str, $_links);
  272. }
  273. return $retval;
  274. }
  275. // User-defined rules (convert without replacing source)
  276. function make_line_rules($str)
  277. {
  278. global $line_rules;
  279. static $pattern, $replace;
  280. if (! isset($pattern)) {
  281. $pattern = array_map(create_function('$a',
  282. 'return \'/\' . $a . \'/\';'), array_keys($line_rules));
  283. $replace = array_values($line_rules);
  284. unset($line_rules);
  285. }
  286. return preg_replace($pattern, $replace, $str);
  287. }
  288. // Remove all HTML tags(or just anchor tags), and WikiName-speific decorations
  289. function strip_htmltag($str, $all = TRUE)
  290. {
  291. global $_symbol_noexists;
  292. static $noexists_pattern;
  293. if (! isset($noexists_pattern))
  294. $noexists_pattern = '#<span class="noexists">([^<]*)<a[^>]+>' .
  295. preg_quote($_symbol_noexists, '#') . '</a></span>#';
  296. // Strip Dagnling-Link decoration (Tags and "$_symbol_noexists")
  297. $str = preg_replace($noexists_pattern, '$1', $str);
  298. if ($all) {
  299. // All other HTML tags
  300. return preg_replace('#<[^>]+>#', '', $str);
  301. } else {
  302. // All other anchor-tags only
  303. return preg_replace('#<a[^>]+>|</a>#i', '', $str);
  304. }
  305. }
  306. // Remove AutoLink marker with AutLink itself
  307. function strip_autolink($str)
  308. {
  309. return preg_replace('#<!--autolink--><a [^>]+>|</a><!--/autolink-->#', '', $str);
  310. }
  311. // Make a backlink. searching-link of the page name, by the page name, for the page name
  312. function make_search($page)
  313. {
  314. global $script;
  315. $s_page = htmlsc($page);
  316. $r_page = rawurlencode($page);
  317. return '<a href="' . $script . '?plugin=related&amp;page=' . $r_page .
  318. '">' . $s_page . '</a> ';
  319. }
  320. // Make heading string (remove heading-related decorations from Wiki text)
  321. function make_heading(& $str, $strip = TRUE)
  322. {
  323. global $NotePattern;
  324. // Cut fixed-heading anchors
  325. $id = '';
  326. $matches = array();
  327. if (preg_match('/^(\*{0,3})(.*?)\[#([A-Za-z][\w-]+)\](.*?)$/m', $str, $matches)) {
  328. $str = $matches[2] . $matches[4];
  329. $id = & $matches[3];
  330. } else {
  331. $str = preg_replace('/^\*{0,3}/', '', $str);
  332. }
  333. // Cut footnotes and tags
  334. if ($strip === TRUE)
  335. $str = strip_htmltag(make_link(preg_replace($NotePattern, '', $str)));
  336. return $id;
  337. }
  338. // Separate a page-name(or URL or null string) and an anchor
  339. // (last one standing) without sharp
  340. function anchor_explode($page, $strict_editable = FALSE)
  341. {
  342. $pos = strrpos($page, '#');
  343. if ($pos === FALSE) return array($page, '', FALSE);
  344. // Ignore the last sharp letter
  345. if ($pos + 1 == strlen($page)) {
  346. $pos = strpos(substr($page, $pos + 1), '#');
  347. if ($pos === FALSE) return array($page, '', FALSE);
  348. }
  349. $s_page = substr($page, 0, $pos);
  350. $anchor = substr($page, $pos + 1);
  351. if($strict_editable === TRUE && preg_match('/^[a-z][a-f0-9]{7}$/', $anchor)) {
  352. return array ($s_page, $anchor, TRUE); // Seems fixed-anchor
  353. } else {
  354. return array ($s_page, $anchor, FALSE);
  355. }
  356. }
  357. // Check HTTP header()s were sent already, or
  358. // there're blank lines or something out of php blocks
  359. function pkwk_headers_sent()
  360. {
  361. if (PKWK_OPTIMISE) return;
  362. $file = $line = '';
  363. if (version_compare(PHP_VERSION, '4.3.0', '>=')) {
  364. if (headers_sent($file, $line))
  365. die('Headers already sent at ' .
  366. htmlsc($file) .
  367. ' line ' . $line . '.');
  368. } else {
  369. if (headers_sent())
  370. die('Headers already sent.');
  371. }
  372. }
  373. // Output common HTTP headers
  374. function pkwk_common_headers()
  375. {
  376. if (! PKWK_OPTIMISE) pkwk_headers_sent();
  377. if(defined('PKWK_ZLIB_LOADABLE_MODULE')) {
  378. $matches = array();
  379. if(ini_get('zlib.output_compression') &&
  380. preg_match('/\b(gzip|deflate)\b/i', $_SERVER['HTTP_ACCEPT_ENCODING'], $matches)) {
  381. // Bug #29350 output_compression compresses everything _without header_ as loadable module
  382. // http://bugs.php.net/bug.php?id=29350
  383. header('Content-Encoding: ' . $matches[1]);
  384. header('Vary: Accept-Encoding');
  385. }
  386. }
  387. }
  388. // DTD definitions
  389. define('PKWK_DTD_XHTML_1_1', 17); // Strict only
  390. define('PKWK_DTD_XHTML_1_0', 16); // Strict
  391. define('PKWK_DTD_XHTML_1_0_STRICT', 16);
  392. define('PKWK_DTD_XHTML_1_0_TRANSITIONAL', 15);
  393. define('PKWK_DTD_XHTML_1_0_FRAMESET', 14);
  394. define('PKWK_DTD_HTML_4_01', 3); // Strict
  395. define('PKWK_DTD_HTML_4_01_STRICT', 3);
  396. define('PKWK_DTD_HTML_4_01_TRANSITIONAL', 2);
  397. define('PKWK_DTD_HTML_4_01_FRAMESET', 1);
  398. define('PKWK_DTD_TYPE_XHTML', 1);
  399. define('PKWK_DTD_TYPE_HTML', 0);
  400. // Output HTML DTD, <html> start tag. Return content-type.
  401. function pkwk_output_dtd($pkwk_dtd = PKWK_DTD_XHTML_1_1, $charset = CONTENT_CHARSET)
  402. {
  403. static $called;
  404. if (isset($called)) die('pkwk_output_dtd() already called. Why?');
  405. $called = TRUE;
  406. $type = PKWK_DTD_TYPE_XHTML;
  407. $option = '';
  408. switch($pkwk_dtd){
  409. case PKWK_DTD_XHTML_1_1 :
  410. $version = '1.1' ;
  411. $dtd = 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd';
  412. break;
  413. case PKWK_DTD_XHTML_1_0_STRICT :
  414. $version = '1.0' ;
  415. $option = 'Strict';
  416. $dtd = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd';
  417. break;
  418. case PKWK_DTD_XHTML_1_0_TRANSITIONAL:
  419. $version = '1.0' ;
  420. $option = 'Transitional';
  421. $dtd = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd';
  422. break;
  423. case PKWK_DTD_HTML_4_01_STRICT :
  424. $type = PKWK_DTD_TYPE_HTML;
  425. $version = '4.01';
  426. $dtd = 'http://www.w3.org/TR/html4/strict.dtd';
  427. break;
  428. case PKWK_DTD_HTML_4_01_TRANSITIONAL:
  429. $type = PKWK_DTD_TYPE_HTML;
  430. $version = '4.01';
  431. $option = 'Transitional';
  432. $dtd = 'http://www.w3.org/TR/html4/loose.dtd';
  433. break;
  434. default: die('DTD not specified or invalid DTD');
  435. break;
  436. }
  437. $charset = htmlsc($charset);
  438. // Output XML or not
  439. if ($type == PKWK_DTD_TYPE_XHTML) echo '<?xml version="1.0" encoding="' . $charset . '" ?>' . "\n";
  440. // Output doctype
  441. echo '<!DOCTYPE html PUBLIC "-//W3C//DTD ' .
  442. ($type == PKWK_DTD_TYPE_XHTML ? 'XHTML' : 'HTML') . ' ' .
  443. $version .
  444. ($option != '' ? ' ' . $option : '') .
  445. '//EN" "' .
  446. $dtd .
  447. '">' . "\n";
  448. // Output <html> start tag
  449. echo '<html';
  450. if ($type == PKWK_DTD_TYPE_XHTML) {
  451. echo ' xmlns="http://www.w3.org/1999/xhtml"'; // dir="ltr" /* LeftToRight */
  452. echo ' xml:lang="' . LANG . '"';
  453. if ($version == '1.0') echo ' lang="' . LANG . '"'; // Only XHTML 1.0
  454. } else {
  455. echo ' lang="' . LANG . '"'; // HTML
  456. }
  457. echo '>' . "\n"; // <html>
  458. // Return content-type (with MIME type)
  459. if ($type == PKWK_DTD_TYPE_XHTML) {
  460. // NOTE: XHTML 1.1 browser will ignore http-equiv
  461. return '<meta http-equiv="content-type" content="application/xhtml+xml; charset=' . $charset . '" />' . "\n";
  462. } else {
  463. return '<meta http-equiv="content-type" content="text/html; charset=' . $charset . '" />' . "\n";
  464. }
  465. }
  466. ?>