OSDN Git Service

BugTrack/2247 External link cushion page
[pukiwiki/pukiwiki.git] / lib / html.php
index 9079e49..3ba8c3f 100644 (file)
@@ -1,8 +1,8 @@
 <?php
 // PukiWiki - Yet another WikiWikiWeb clone.
-// $Id: html.php,v 1.48 2005/12/18 15:19:24 henoheno Exp $
-// Copyright (C)
-//   2002-2005 PukiWiki Developers Team
+// html.php
+// Copyright
+//   2002-2018 PukiWiki Development Team
 //   2001-2002 Originally written by yu-ji
 // License: GPL v2 or (at your option) any later version
 //
 // Show page-content
 function catbody($title, $page, $body)
 {
-       global $script, $vars, $arg, $defaultpage, $whatsnew, $help_page, $hr;
+       global $vars, $arg, $defaultpage, $whatsnew, $help_page, $hr;
        global $attach_link, $related_link, $cantedit, $function_freeze;
        global $search_word_color, $_msg_word, $foot_explain, $note_hr, $head_tags;
-       global $trackback, $trackback_javascript, $referer, $javascript;
-       global $nofollow;
+       global $javascript, $nofollow;
        global $_LANG, $_LINK, $_IMAGE;
+       global $auth_type, $auth_user;
+       global $html_meta_referrer_policy;
 
        global $pkwk_dtd;     // XHTML 1.1, XHTML1.0, HTML 4.01 Transitional...
        global $page_title;   // Title of this site
@@ -24,17 +25,31 @@ function catbody($title, $page, $body)
        global $modifier;     // Site administrator's  web page
        global $modifierlink; // Site administrator's name
 
+       $script = get_base_uri();
+       $enable_login = false;
+       $enable_logout = false;
+       if (AUTH_TYPE_FORM === $auth_type || AUTH_TYPE_EXTERNAL === $auth_type ||
+               AUTH_TYPE_SAML === $auth_type) {
+               if ($auth_user) {
+                       $enable_logout = true;
+               } else {
+                       $enable_login = true;
+               }
+       } else if (AUTH_TYPE_BASIC === $auth_type) {
+               if ($auth_user) {
+                       $enable_logout = true;
+               }
+       }
        if (! file_exists(SKIN_FILE) || ! is_readable(SKIN_FILE))
                die_message('SKIN_FILE is not found');
 
        $_LINK = $_IMAGE = array();
 
-       // Add JavaScript header when ...
-       if ($trackback && $trackback_javascript) $javascript = 1; // Set something If you want
-       if (! PKWK_ALLOW_JAVASCRIPT) unset($javascript);
-
        $_page  = isset($vars['page']) ? $vars['page'] : '';
-       $r_page = rawurlencode($_page);
+       $r_page = pagename_urlencode($_page);
+       $is_edit_preview = isset($vars['preview']);
+       // Canonical URL
+       $canonical_url = get_page_uri($_page, PKWK_URI_ABSOLUTE);
 
        // Set $_LINK for skin
        $_LINK['add']      = "$script?cmd=add&amp;page=$r_page";
@@ -44,25 +59,34 @@ function catbody($title, $page, $body)
        $_LINK['edit']     = "$script?cmd=edit&amp;page=$r_page";
        $_LINK['filelist'] = "$script?cmd=filelist";
        $_LINK['freeze']   = "$script?cmd=freeze&amp;page=$r_page";
-       $_LINK['help']     = "$script?" . rawurlencode($help_page);
+       $_LINK['help']     = get_page_uri($help_page);
        $_LINK['list']     = "$script?cmd=list";
        $_LINK['new']      = "$script?plugin=newpage&amp;refer=$r_page";
        $_LINK['rdf']      = "$script?cmd=rss&amp;ver=1.0";
-       $_LINK['recent']   = "$script?" . rawurlencode($whatsnew);
-       $_LINK['refer']    = "$script?plugin=referer&amp;page=$r_page";
-       $_LINK['reload']   = "$script?$r_page";
+       $_LINK['recent']   = get_page_uri($whatsnew);
+       $_LINK['reload']   = get_page_uri($_page);
        $_LINK['rename']   = "$script?plugin=rename&amp;refer=$r_page";
        $_LINK['rss']      = "$script?cmd=rss";
        $_LINK['rss10']    = "$script?cmd=rss&amp;ver=1.0"; // Same as 'rdf'
        $_LINK['rss20']    = "$script?cmd=rss&amp;ver=2.0";
        $_LINK['search']   = "$script?cmd=search";
-       $_LINK['top']      = "$script?" . rawurlencode($defaultpage);
-       if ($trackback) {
-               $tb_id = tb_get_id($_page);
-               $_LINK['trackback'] = "$script?plugin=tb&amp;__mode=view&amp;tb_id=$tb_id";
-       }
+       $_LINK['top']      = get_page_uri($defaultpage);
        $_LINK['unfreeze'] = "$script?cmd=unfreeze&amp;page=$r_page";
        $_LINK['upload']   = "$script?plugin=attach&amp;pcmd=upload&amp;page=$r_page";
+       $_LINK['canonical_url'] = $canonical_url;
+       $login_link = "#LOGIN_ERROR"; // dummy link that is not used
+       switch ($auth_type) {
+               case AUTH_TYPE_FORM:
+                       $login_link = "$script?plugin=loginform&pcmd=login&page=$r_page";
+                       break;
+               case AUTH_TYPE_EXTERNAL:
+               case AUTH_TYPE_SAML:
+                       $login_link = get_auth_external_login_url($_page,
+                               get_page_uri($_page, PKWK_URI_ROOT));
+                       break;
+       }
+       $_LINK['login']    = htmlsc($login_link);
+       $_LINK['logout']   = "$script?plugin=loginform&amp;pcmd=logout&amp;page=$r_page";
 
        // Compat: Skins for 1.4.4 and before
        $link_add       = & $_LINK['add'];
@@ -76,7 +100,7 @@ function catbody($title, $page, $body)
        $link_whatsnew  = & $_LINK['recent'];
        $link_backup    = & $_LINK['backup'];
        $link_help      = & $_LINK['help'];
-       $link_trackback = & $_LINK['trackback'];        // New!
+       $link_trackback = ''; // Removed (compat)
        $link_rdf       = & $_LINK['rdf'];              // New!
        $link_rss       = & $_LINK['rss'];
        $link_rss10     = & $_LINK['rss10'];            // New!
@@ -85,7 +109,7 @@ function catbody($title, $page, $body)
        $link_unfreeze  = & $_LINK['unfreeze'];
        $link_upload    = & $_LINK['upload'];
        $link_template  = & $_LINK['copy'];
-       $link_refer     = & $_LINK['refer'];    // New!
+       $link_refer     = ''; // Removed (compat)
        $link_rename    = & $_LINK['rename'];
 
        // Init flags
@@ -95,10 +119,11 @@ function catbody($title, $page, $body)
 
        // Last modification date (string) of the page
        $lastmodified = $is_read ?  format_date(get_filetime($_page)) .
-               ' ' . get_pg_passage($_page, FALSE) : '';
+               get_passage_html_span($_page) : '';
 
        // List of attached files to the page
-       $attaches = ($attach_link && $is_read && exist_plugin_action('attach')) ?
+       $show_attaches = $is_read || arg_check('edit');
+       $attaches = ($attach_link && $show_attaches && exist_plugin_action('attach')) ?
                attach_filelist() : '';
 
        // List of related pages
@@ -115,80 +140,235 @@ function catbody($title, $page, $body)
        // Last modification date (UNIX timestamp) of the page
        $fmt = $is_read ? get_filetime($_page) + LOCALZONE : 0;
 
+       // Output nofollow / noindex regardless os skin file
+       if (!$is_read || $nofollow) {
+               if (!headers_sent()) {
+                       header("X-Robots-Tag: noindex,nofollow");
+               }
+       }
+
+       // Send Canonical URL for Search Engine Optimization
+       if ($is_read && !headers_sent()) {
+               header("Link: <$canonical_url>; rel=\"canonical\"");
+       }
+
        // Search words
        if ($search_word_color && isset($vars['word'])) {
-               $body = '<div class="small">' . $_msg_word . htmlspecialchars($vars['word']) .
+               $body = '<div class="small">' . $_msg_word . htmlsc($vars['word']) .
                        '</div>' . $hr . "\n" . $body;
 
                // BugTrack2/106: Only variables can be passed by reference from PHP 5.0.5
                // with array_splice(), array_flip()
-               $tmp_array = preg_split('/\s+/', $vars['word'], -1, PREG_SPLIT_NO_EMPTY);
-               $tmp_array = array_splice($tmp_array, 0, 10);
-               $words = array_flip($tmp_array);
+               $words = preg_split('/\s+/', $vars['word'], -1, PREG_SPLIT_NO_EMPTY);
+               $words = array_splice($words, 0, 10); // Max: 10 words
+               $words = array_flip($words);
 
                $keys = array();
                foreach ($words as $word=>$id) $keys[$word] = strlen($word);
                arsort($keys, SORT_NUMERIC);
                $keys = get_search_words(array_keys($keys), TRUE);
                $id = 0;
+               $patterns = '';
                foreach ($keys as $key=>$pattern) {
-                       $s_key    = htmlspecialchars($key);
-                       $pattern  = '/<textarea[^>]*>.*?<\/textarea>|<[^>]*>|(' . $pattern . ')|&[^;]+;/s';
-                       $callback = create_function(
-                               '$arr',
-                               'return (count($arr) > 1) ? \'<strong class="word' .
-                                       $id++ . '">\' . $arr[1] . \'</strong>\' : $arr[0];'
-                       );
-                       $body  = preg_replace_callback($pattern, $callback, $body);
-                       $notes = preg_replace_callback($pattern, $callback, $notes);
+                       if (strlen($patterns) > 0) {
+                               $patterns .= '|';
+                       }
+                       $patterns .= '(' . $pattern . ')';
+               }
+               if ($pattern) {
+                       $whole_pattern  = '/' .
+                               '<textarea[^>]*>.*?<\/textarea>' .      // Ignore textareas
+                               '|' . '<[^>]*>' .                       // Ignore tags
+                               '|' . '&[^;]+;' .                       // Ignore entities
+                               '|' . '(' . $patterns . ')' .           // $matches[1]: Regex for a search word
+                               '/sS';
+                       $body  = preg_replace_callback($whole_pattern, '_decorate_Nth_word', $body);
+                       $notes = preg_replace_callback($whole_pattern, '_decorate_Nth_word', $notes);
                }
        }
+       // Embed Scripting data
+       $html_scripting_data = get_html_scripting_data($_page, $is_edit_preview);
 
-       $longtaketime = getmicrotime() - MUTIME;
-       $taketime     = sprintf('%01.03f', $longtaketime);
+       // Compat: 'HTML convert time' without time about MenuBar and skin
+       $taketime = elapsedtime();
 
        require(SKIN_FILE);
 }
 
+function _decorate_Nth_word($matches)
+{
+       // $matches[0]: including both words to skip and to decorate
+       // $matches[1]: word to decorate
+       // $matches[2+]: indicates which keyword to decorate
+       $index = -1;
+       for ($i = 2; $i < count($matches); $i++) {
+               if (isset($matches[$i]) && $matches[$i]) {
+                       $index = $i - 2;
+                       break;
+               }
+       }
+       if (isset($matches[1])) {
+               // wordN highlight class: N=0...n
+               return '<strong class="word' . $index . '">' .
+                       $matches[0] . '</strong>';
+       }
+       return $matches[0];
+}
+
+/**
+ * Get data used by JavaScript modules
+ *
+ * @param $page page name
+ * @param $in_editing true if preview in editing
+ */
+function get_html_scripting_data($page, $in_editing)
+{
+       global $ticket_link_sites, $plugin;
+       global $external_link_cushion_page, $external_link_cushion;
+       global $topicpath_title;
+       global $ticket_jira_default_site;
+       if (!isset($ticket_link_sites) || !is_array($ticket_link_sites)) {
+               return '';
+       }
+       $is_utf8 = (bool)defined('PKWK_UTF8_ENABLE');
+       // Require: PHP 5.4+
+       $json_enabled = defined('JSON_UNESCAPED_UNICODE');
+       if (!$json_enabled) {
+               $empty_data = <<<EOS
+<div id="pukiwiki-site-properties" style="display:none;">
+</div>
+EOS;
+               return $empty_data;
+       }
+       // Site basic Properties
+       $props = array(
+               'is_utf8' => $is_utf8,
+               'json_enabled' => $json_enabled,
+               'base_uri_pathname' => get_base_uri(PKWK_URI_ROOT),
+               'base_uri_absolute' => get_base_uri(PKWK_URI_ABSOLUTE)
+       );
+       $h_props = htmlsc_json($props);
+       $site_props = <<<EOS
+<input type="hidden" class="site-props" value="$h_props" />
+EOS;
+       $h_plugin = htmlsc($plugin);
+       $plugin_prop = <<<EOS
+<input type="hidden" class="plugin-name" value="$h_plugin" />
+EOS;
+       // Page name
+       $h_page_name = htmlsc($page);
+       $page_name_data = <<<EOS
+<input type="hidden" class="page-name" value="$h_page_name" />
+EOS;
+       // Page is editing (preview)
+       $in_editing_value = ($plugin === 'edit' && $in_editing) ? 'true' : 'false';
+       $page_edit_data = <<<EOS
+<input type="hidden" class="page-in-edit" value="$in_editing_value" />
+EOS;
+       // AutoTicketLink
+       $filtered_ticket_link_sites = array();
+       foreach ($ticket_link_sites as $s) {
+               if (!preg_match('/^([a-zA-Z0-9]+)([\.\-][a-zA-Z0-9]+)*$/', $s['key'])) {
+                       continue;
+               }
+               array_push($filtered_ticket_link_sites, $s);
+       }
+       $h_ticket_link_sites = htmlsc_json($filtered_ticket_link_sites);
+       $ticketlink_data = <<<EOS
+<input type="hidden" class="ticketlink-def" value="$h_ticket_link_sites" />
+EOS;
+       // AutoTicketLink - JIRA
+       $ticket_jira_projects = get_ticketlink_jira_projects();
+       $ticketlink_jira_data = '';
+       if (count($ticket_jira_projects) > 0) {
+               $h_ticket_jira_projects = htmlsc_json($ticket_jira_projects);
+               $ticketlink_jira_data = <<<EOS
+<input type="hidden" class="ticketlink-jira-def" value="$h_ticket_jira_projects" />
+EOS;
+       }
+       $ticketlink_jira_default_data = '';
+       if (isset($ticket_jira_default_site) && is_array($ticket_jira_default_site)) {
+               $h_ticket_jira_default_site = htmlsc_json($ticket_jira_default_site);
+               $ticketlink_jira_default_data = <<<EOS
+<input type="hidden" class="ticketlink-jira-default-def" value="$h_ticket_jira_default_site" />
+EOS;
+       }
+       // External link cushion page
+       $external_link_cushion_data = '';
+       if ($external_link_cushion_page) {
+               $h_cushion = htmlsc_json($external_link_cushion);
+               $external_link_cushion_data = <<<EOS
+<input type="hidden" class="external-link-cushion" value="$h_cushion" />
+EOS;
+       }
+       // Topicpath title
+       $topicpath_data = '';
+       if ($topicpath_title && exist_plugin('topicpath')) {
+               $parents = plugin_topicpath_parent_links($page);
+               $h_topicpath = htmlsc(json_encode($parents,
+               JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
+               $topicpath_data = <<<EOS
+<input type="hidden" class="topicpath-links" value="$h_topicpath" />
+EOS;
+       }
+       $data = <<<EOS
+<div id="pukiwiki-site-properties" style="display:none;">
+$site_props
+$plugin_prop
+$page_name_data
+$page_edit_data
+$ticketlink_data
+$ticketlink_jira_data
+$ticketlink_jira_default_data
+$external_link_cushion_data
+$topicpath_data
+</div>
+EOS;
+       return $data;
+}
+
 // Show 'edit' form
 function edit_form($page, $postdata, $digest = FALSE, $b_template = TRUE)
 {
-       global $script, $vars, $rows, $cols, $hr, $function_freeze;
-       global $_btn_addtop, $_btn_preview, $_btn_repreview, $_btn_update, $_btn_cancel,
-               $_msg_help, $_btn_notchangetimestamp;
-       global $whatsnew, $_btn_template, $_btn_load, $load_template_func;
+       global $vars, $rows, $cols;
+       global $_btn_preview, $_btn_repreview, $_btn_update, $_btn_cancel, $_msg_help;
+       global $_btn_template, $_btn_load, $load_template_func;
        global $notimeupdate;
+       global $_msg_edit_cancel_confirm, $_msg_edit_unloadbefore_message;
+       global $rule_page;
 
+       $script = get_base_uri();
        // Newly generate $digest or not
        if ($digest === FALSE) $digest = md5(join('', get_source($page)));
-
-       $refer = $template = $addtag = $add_top = '';
-
-       $checked_top  = isset($vars['add_top'])     ? ' checked="checked"' : '';
-       $checked_time = isset($vars['notimestamp']) ? ' checked="checked"' : '';
-
+       $refer = $template = '';
+       // Add plugin
+       $addtag = $add_top = '';
        if(isset($vars['add'])) {
-               $addtag  = '<input type="hidden" name="add" value="true" />';
-               $add_top = '<input type="checkbox" name="add_top" value="true"' .
-                       $checked_top . ' /><span class="small">' .
-                       $_btn_addtop . '</span>';
+               global $_btn_addtop;
+               $addtag  = '<input type="hidden" name="add"    value="true" />';
+               $add_top = isset($vars['add_top']) ? ' checked="checked"' : '';
+               $add_top = '<input type="checkbox" name="add_top" ' .
+                       'id="_edit_form_add_top" value="true"' . $add_top . ' />' . "\n" .
+                       '  <label for="_edit_form_add_top">' .
+                               '<span class="small">' . $_btn_addtop . '</span>' .
+                       '</label>';
        }
-
        if($load_template_func && $b_template) {
-               $pages  = array();
-               foreach(get_existpages() as $_page) {
-                       if ($_page == $whatsnew || check_non_list($_page))
-                               continue;
-                       $s_page = htmlspecialchars($_page);
-                       $pages[$_page] = '   <option value="' . $s_page . '">' .
-                               $s_page . '</option>';
+               $template_page_list = get_template_page_list();
+               $tpages = array(); // Template pages
+               foreach($template_page_list as $p) {
+                       $ps = htmlsc($p);
+                       $tpages[] = '   <option value="' . $ps . '">' . $ps . '</option>';
+               }
+               if (count($template_page_list) > 0) {
+                       $s_tpages = join("\n", $tpages);
+               } else {
+                       $s_tpages = '   <option value="">(no template pages)</option>';
                }
-               ksort($pages);
-               $s_pages  = join("\n", $pages);
                $template = <<<EOD
   <select name="template_page">
    <option value="">-- $_btn_template --</option>
-$s_pages
+$s_tpages
   </select>
   <input type="submit" name="template" value="$_btn_load" accesskey="r" />
   <br />
@@ -199,92 +379,145 @@ EOD;
        }
 
        $r_page      = rawurlencode($page);
-       $s_page      = htmlspecialchars($page);
-       $s_digest    = htmlspecialchars($digest);
-       $s_postdata  = htmlspecialchars($refer . $postdata);
-       $s_original  = isset($vars['original']) ? htmlspecialchars($vars['original']) : $s_postdata;
+       $s_page      = htmlsc($page);
+       $s_digest    = htmlsc($digest);
+       $s_postdata  = htmlsc($refer . $postdata);
+       $s_original  = isset($vars['original']) ? htmlsc($vars['original']) : $s_postdata;
        $b_preview   = isset($vars['preview']); // TRUE when preview
        $btn_preview = $b_preview ? $_btn_repreview : $_btn_preview;
 
+       // Checkbox 'do not change timestamp'
        $add_notimestamp = '';
-       if ( $notimeupdate != 0 ) {
-               // enable 'do not change timestamp'
-               $add_notimestamp = <<<EOD
-  <input type="checkbox" name="notimestamp" id="_edit_form_notimestamp" value="true"$checked_time />
-  <label for="_edit_form_notimestamp"><span class="small">$_btn_notchangetimestamp</span></label>
-EOD;
-               if ( $notimeupdate == 2 ) {
-                       // enable only administrator
-                       $add_notimestamp .= <<<EOD
-  <input type="password" name="pass" size="12" />
-EOD;
+       if ($notimeupdate != 0) {
+               global $_btn_notchangetimestamp;
+               $checked_time = isset($vars['notimestamp']) ? ' checked="checked"' : '';
+               // Only for administrator
+               if ($notimeupdate == 2) {
+                       $add_notimestamp = '   ' .
+                               '<input type="password" name="pass" size="12" />' . "\n";
                }
-               $add_notimestamp .= '&nbsp;';
+               $add_notimestamp = '<input type="checkbox" name="notimestamp" ' .
+                       'id="_edit_form_notimestamp" value="true"' . $checked_time . ' />' . "\n" .
+                       '   ' . '<label for="_edit_form_notimestamp"><span class="small">' .
+                       $_btn_notchangetimestamp . '</span></label>' . "\n" .
+                       $add_notimestamp .
+                       '&nbsp;';
        }
 
+       // 'margin-bottom', 'float:left', and 'margin-top'
+       // are for layout of 'cancel button'
+       $h_msg_edit_cancel_confirm = htmlsc($_msg_edit_cancel_confirm);
+       $h_msg_edit_unloadbefore_message = htmlsc($_msg_edit_unloadbefore_message);
        $body = <<<EOD
-<form action="$script" method="post">
- <div class="edit_form">
+<div class="edit_form">
+ <form action="$script" method="post" class="_plugin_edit_edit_form" style="margin-bottom:0px;">
 $template
   $addtag
   <input type="hidden" name="cmd"    value="edit" />
   <input type="hidden" name="page"   value="$s_page" />
   <input type="hidden" name="digest" value="$s_digest" />
+  <input type="hidden" id="_msg_edit_cancel_confirm" value="$h_msg_edit_cancel_confirm" />
+  <input type="hidden" id="_msg_edit_unloadbefore_message" value="$h_msg_edit_unloadbefore_message" />
   <textarea name="msg" rows="$rows" cols="$cols">$s_postdata</textarea>
   <br />
-  <input type="submit" name="preview" value="$btn_preview" accesskey="p" />
-  <input type="submit" name="write"   value="$_btn_update" accesskey="s" />
-  $add_top
-  $add_notimestamp
-  <input type="submit" name="cancel"  value="$_btn_cancel" accesskey="c" />
+  <div style="float:left;">
+   <input type="submit" name="preview" value="$btn_preview" accesskey="p" />
+   <input type="submit" name="write"   value="$_btn_update" accesskey="s" />
+   $add_top
+   $add_notimestamp
+  </div>
   <textarea name="original" rows="1" cols="1" style="display:none">$s_original</textarea>
- </div>
-</form>
+ </form>
+ <form action="$script" method="post" class="_plugin_edit_cancel" style="margin-top:0px;">
+  <input type="hidden" name="cmd"    value="edit" />
+  <input type="hidden" name="page"   value="$s_page" />
+  <input type="submit" name="cancel" value="$_btn_cancel" accesskey="c" />
+ </form>
+</div>
 EOD;
 
-       if (isset($vars['help'])) {
-               $body .= $hr . catrule();
+       $body .= '<ul><li><a href="' .
+               get_page_uri($rule_page) .
+               '" target="_blank">' . $_msg_help . '</a></li></ul>';
+       return $body;
+}
+
+/**
+ * Get template page list.
+ */
+function get_template_page_list()
+{
+       global $whatsnew;
+       $tpage_names = array(); // Pages marked as template
+       $template_page = ':config/Templates';
+       $page_max = 100;
+       foreach(get_source($template_page) as $_templates) {
+               $m = array();
+               if (! preg_match('#\-\s*\[\[([^\[\]]+)\]\]#', $_templates, $m)) continue;
+               $tpage = preg_replace('#^./#', "$template_page/", $m[1]);
+               if (! is_page($tpage)) continue;
+               $tpage_names[] = $tpage;
+       }
+       $page_names = array();
+       $page_list = get_existpages();
+       if (count($page_list) > $page_max) {
+               // Extract only template name pages
+               $target_pages = array();
+               foreach ($page_list as $_page) {
+                       if (preg_match('/template/i', $_page)) {
+                               $target_pages[] = $_page;
+                       }
+               }
        } else {
-               $body .= '<ul><li><a href="' .
-                       $script . '?cmd=edit&amp;help=true&amp;page=' . $r_page .
-                       '">' . $_msg_help . '</a></li></ul>';
+               $target_pages = $page_list;
        }
-
-       return $body;
+       foreach ($target_pages as $_page) {
+               if ($_page == $whatsnew || check_non_list($_page) ||
+                       !is_page_readable($_page)) {
+                       continue;
+               }
+               $tpage_names[] = $_page;
+       }
+       $tempalte_page_list = array_values(array_unique($tpage_names));
+       natcasesort($tempalte_page_list);
+       return $tempalte_page_list;
 }
 
 // Related pages
 function make_related($page, $tag = '')
 {
-       global $script, $vars, $rule_related_str, $related_str;
-       global $_ul_left_margin, $_ul_margin, $_list_pad_str;
+       global $vars, $rule_related_str, $related_str;
 
+       $script = get_base_uri();
+       prepare_links_related($page);
        $links = links_get_related($page);
 
        if ($tag) {
-               ksort($links);
+               ksort($links, SORT_STRING);             // Page name, alphabetical order
        } else {
-               arsort($links);
+               arsort($links, SORT_NUMERIC);   // Last modified date, newer
        }
 
        $_links = array();
        foreach ($links as $page=>$lastmod) {
                if (check_non_list($page)) continue;
-
-               $r_page   = rawurlencode($page);
-               $s_page   = htmlspecialchars($page);
-               $passage  = get_passage($lastmod);
-               $_links[] = $tag ?
-                       '<a href="' . $script . '?' . $r_page . '" title="' .
-                       $s_page . ' ' . $passage . '">' . $s_page . '</a>' :
-                       '<a href="' . $script . '?' . $r_page . '">' .
-                       $s_page . '</a>' . $passage;
+               $page_uri = get_page_uri($page);
+               $s_page   = htmlsc($page);
+               if ($tag) {
+                       $attrs = get_page_link_a_attrs($page);
+                       $_links[] = '<a href="' . $page_uri . '" class="' .
+                               $attrs['class'] . '" data-mtime="' . $attrs['data_mtime'] .
+                               '">' . $s_page . '</a>';
+               } else {
+                       $mtime_span = get_passage_mtime_html_span($lastmod + LOCALZONE);
+                       $_links[] = '<a href="' . $page_uri . '">' .
+                       $s_page . '</a>' . $mtime_span;
+               }
        }
        if (empty($_links)) return ''; // Nothing
 
        if ($tag == 'p') { // From the line-head
-               $margin = $_ul_left_margin + $_ul_margin;
-               $style  = sprintf($_list_pad_str, 1, $margin, $margin);
+               $style  = sprintf(pkwk_list_attrs_template(), 1, 1);
                $retval =  "\n" . '<ul' . $style . '>' . "\n" .
                        '<li>' . join($rule_related_str, $_links) . '</li>' . "\n" .
                        '</ul>' . "\n";
@@ -297,6 +530,11 @@ function make_related($page, $tag = '')
        return $retval;
 }
 
+function _convert_line_rule_to_regex($a)
+{
+       return '/' . $a . '/';
+}
+
 // User-defined rules (convert without replacing source)
 function make_line_rules($str)
 {
@@ -304,8 +542,7 @@ function make_line_rules($str)
        static $pattern, $replace;
 
        if (! isset($pattern)) {
-               $pattern = array_map(create_function('$a',
-                       'return \'/\' . $a . \'/\';'), array_keys($line_rules));
+               $pattern = array_map('_convert_line_rule_to_regex', array_keys($line_rules));
                $replace = array_values($line_rules);
                unset($line_rules);
        }
@@ -344,12 +581,9 @@ function strip_autolink($str)
 // Make a backlink. searching-link of the page name, by the page name, for the page name
 function make_search($page)
 {
-       global $script;
-
-       $s_page = htmlspecialchars($page);
+       $s_page = htmlsc($page);
        $r_page = rawurlencode($page);
-
-       return '<a href="' . $script . '?plugin=related&amp;page=' . $r_page .
+       return '<a href="' . get_base_uri() . '?plugin=related&amp;page=' . $r_page .
                '">' . $s_page . '</a> ';
 }
 
@@ -408,7 +642,7 @@ function pkwk_headers_sent()
        if (version_compare(PHP_VERSION, '4.3.0', '>=')) {
                if (headers_sent($file, $line))
                    die('Headers already sent at ' .
-                       htmlspecialchars($file) .
+                       htmlsc($file) .
                        ' line ' . $line . '.');
        } else {
                if (headers_sent())
@@ -419,8 +653,11 @@ function pkwk_headers_sent()
 // Output common HTTP headers
 function pkwk_common_headers()
 {
+       global $http_response_custom_headers;
        if (! PKWK_OPTIMISE) pkwk_headers_sent();
-
+       foreach ($http_response_custom_headers as $header) {
+               header($header);
+       }
        if(defined('PKWK_ZLIB_LOADABLE_MODULE')) {
                $matches = array();
                if(ini_get('zlib.output_compression') &&
@@ -488,7 +725,7 @@ function pkwk_output_dtd($pkwk_dtd = PKWK_DTD_XHTML_1_1, $charset = CONTENT_CHAR
                break;
        }
 
-       $charset = htmlspecialchars($charset);
+       $charset = htmlsc($charset);
 
        // Output XML or not
        if ($type == PKWK_DTD_TYPE_XHTML) echo '<?xml version="1.0" encoding="' . $charset . '" ?>' . "\n";
@@ -521,4 +758,10 @@ function pkwk_output_dtd($pkwk_dtd = PKWK_DTD_XHTML_1_1, $charset = CONTENT_CHAR
                return '<meta http-equiv="content-type" content="text/html; charset=' . $charset . '" />' . "\n";
        }
 }
-?>
+
+/**
+ * Get template of List (ul, ol, dl) attributes
+ */
+function pkwk_list_attrs_template() {
+       return ' class="list%d list-indent%d"';
+}