OSDN Git Service

BugTrack/2247 External link cushion page
[pukiwiki/pukiwiki.git] / lib / html.php
index 92972b6..3ba8c3f 100644 (file)
@@ -2,7 +2,7 @@
 // PukiWiki - Yet another WikiWikiWeb clone.
 // html.php
 // Copyright
-//   2002-2017 PukiWiki Development Team
+//   2002-2018 PukiWiki Development Team
 //   2001-2002 Originally written by yu-ji
 // License: GPL v2 or (at your option) any later version
 //
@@ -17,6 +17,7 @@ function catbody($title, $page, $body)
        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
@@ -46,7 +47,7 @@ function catbody($title, $page, $body)
 
        $_page  = isset($vars['page']) ? $vars['page'] : '';
        $r_page = pagename_urlencode($_page);
-
+       $is_edit_preview = isset($vars['preview']);
        // Canonical URL
        $canonical_url = get_page_uri($_page, PKWK_URI_ABSOLUTE);
 
@@ -80,7 +81,8 @@ function catbody($title, $page, $body)
                        break;
                case AUTH_TYPE_EXTERNAL:
                case AUTH_TYPE_SAML:
-                       $login_link = get_auth_external_login_url($_page, $_LINK['reload']);
+                       $login_link = get_auth_external_login_url($_page,
+                               get_page_uri($_page, PKWK_URI_ROOT));
                        break;
        }
        $_LINK['login']    = htmlsc($login_link);
@@ -117,7 +119,7 @@ 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
        $show_attaches = $is_read || arg_check('edit');
@@ -166,30 +168,26 @@ function catbody($title, $page, $body)
                arsort($keys, SORT_NUMERIC);
                $keys = get_search_words(array_keys($keys), TRUE);
                $id = 0;
+               $patterns = '';
                foreach ($keys as $key=>$pattern) {
-                       $s_key    = htmlsc($key);
-                       $pattern  = '/' .
+                       if (strlen($patterns) > 0) {
+                               $patterns .= '|';
+                       }
+                       $patterns .= '(' . $pattern . ')';
+               }
+               if ($pattern) {
+                       $whole_pattern  = '/' .
                                '<textarea[^>]*>.*?<\/textarea>' .      // Ignore textareas
                                '|' . '<[^>]*>' .                       // Ignore tags
                                '|' . '&[^;]+;' .                       // Ignore entities
-                               '|' . '(' . $pattern . ')' .            // $matches[1]: Regex for a search word
+                               '|' . '(' . $patterns . ')' .           // $matches[1]: Regex for a search word
                                '/sS';
-                       $decorate_Nth_word = create_function(
-                               '$matches',
-                               'return (isset($matches[1])) ? ' .
-                                       '\'<strong class="word' .
-                                               $id .
-                                       '">\' . $matches[1] . \'</strong>\' : ' .
-                                       '$matches[0];'
-                       );
-                       $body  = preg_replace_callback($pattern, $decorate_Nth_word, $body);
-                       $notes = preg_replace_callback($pattern, $decorate_Nth_word, $notes);
-                       ++$id;
+                       $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();
+       $html_scripting_data = get_html_scripting_data($_page, $is_edit_preview);
 
        // Compat: 'HTML convert time' without time about MenuBar and skin
        $taketime = elapsedtime();
@@ -197,12 +195,38 @@ function catbody($title, $page, $body)
        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()
+function get_html_scripting_data($page, $in_editing)
 {
-       global $ticket_link_sites;
+       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 '';
        }
@@ -223,31 +247,81 @@ EOS;
                'base_uri_pathname' => get_base_uri(PKWK_URI_ROOT),
                'base_uri_absolute' => get_base_uri(PKWK_URI_ABSOLUTE)
        );
-       $props_json = htmlsc(json_encode($props, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
+       $h_props = htmlsc_json($props);
        $site_props = <<<EOS
-<div data-key="site-props" data-value="$props_json"></div>
+<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
-       $text = '';
+       $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;
                }
-               $site_info_json = htmlsc(json_encode($s, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
-               $text .= <<<EOS
-  <span class="ticketlink-site" data-site="$site_info_json"></span>
-EOS;
-               $text .= "\n";
+               array_push($filtered_ticket_link_sites, $s);
        }
+       $h_ticket_link_sites = htmlsc_json($filtered_ticket_link_sites);
        $ticketlink_data = <<<EOS
-<div class="ticketlink-def">
-$text
-</div>
+<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;
@@ -256,19 +330,17 @@ EOS;
 // Show 'edit' form
 function edit_form($page, $postdata, $digest = FALSE, $b_template = TRUE)
 {
-       global $vars, $rows, $cols, $hr, $function_freeze;
+       global $vars, $rows, $cols;
        global $_btn_preview, $_btn_repreview, $_btn_update, $_btn_cancel, $_msg_help;
-       global $whatsnew, $_btn_template, $_btn_load, $load_template_func;
+       global $_btn_template, $_btn_load, $load_template_func;
        global $notimeupdate;
-       global $_title_list, $_label_template_pages;
+       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 = '';
        // Add plugin
        $addtag = $add_top = '';
        if(isset($vars['add'])) {
@@ -281,57 +353,22 @@ function edit_form($page, $postdata, $digest = FALSE, $b_template = TRUE)
                                '<span class="small">' . $_btn_addtop . '</span>' .
                        '</label>';
        }
-
        if($load_template_func && $b_template) {
-               $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();
-               foreach(get_existpages() as $_page) {
-                       if ($_page == $whatsnew || check_non_list($_page) ||
-                               !is_page_readable($_page))
-                               continue;
-                       if (preg_match('/template/i', $_page)) {
-                               $tpage_names[] = $_page;
-                       } else {
-                               if (count($page_names) >= $page_max) continue;
-                               $page_names[] = $_page;
-                       }
-               }
-               $tpage_names2 = array_values(array_unique($tpage_names));
-               natcasesort($tpage_names2);
-               natcasesort($page_names);
+               $template_page_list = get_template_page_list();
                $tpages = array(); // Template pages
-               $npages = array(); // Normal pages
-               foreach($tpage_names2 as $p) {
+               foreach($template_page_list as $p) {
                        $ps = htmlsc($p);
                        $tpages[] = '   <option value="' . $ps . '">' . $ps . '</option>';
                }
-               foreach($page_names as $p) {
-                       $ps = htmlsc($p);
-                       $npages[] = '   <option value="' . $ps . '">' . $ps . '</option>';
-               }
-               if (count($page_names) === $page_max) {
-                       $npages[] = '   <option value="">...</option>';
+               if (count($template_page_list) > 0) {
+                       $s_tpages = join("\n", $tpages);
+               } else {
+                       $s_tpages = '   <option value="">(no template pages)</option>';
                }
-               $s_tpages  = join("\n", $tpages);
-               $s_npages  = join("\n", $npages);
                $template = <<<EOD
   <select name="template_page">
    <option value="">-- $_btn_template --</option>
-   <optgroup label="$_label_template_pages">
 $s_tpages
-   </optgroup>
-   <optgroup label="$_title_list">
-$s_npages
-   </optgroup>
   </select>
   <input type="submit" name="template" value="$_btn_load" accesskey="r" />
   <br />
@@ -369,14 +406,18 @@ EOD;
 
        // '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
 <div class="edit_form">
- <form action="$script" method="post" style="margin-bottom:0px;">
+ <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 />
   <div style="float:left;">
@@ -387,7 +428,7 @@ $template
   </div>
   <textarea name="original" rows="1" cols="1" style="display:none">$s_original</textarea>
  </form>
- <form action="$script" method="post" style="margin-top:0px;">
+ <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" />
@@ -401,6 +442,47 @@ EOD;
        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 {
+               $target_pages = $page_list;
+       }
+       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 = '')
 {
@@ -419,15 +501,18 @@ function make_related($page, $tag = '')
        $_links = array();
        foreach ($links as $page=>$lastmod) {
                if (check_non_list($page)) continue;
-
                $page_uri = get_page_uri($page);
                $s_page   = htmlsc($page);
-               $passage  = get_passage($lastmod);
-               $_links[] = $tag ?
-                       '<a href="' . $page_uri . '" title="' .
-                       $s_page . ' ' . $passage . '">' . $s_page . '</a>' :
-                       '<a href="' . $page_uri . '">' .
-                       $s_page . '</a>' . $passage;
+               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
 
@@ -445,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)
 {
@@ -452,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);
        }
@@ -564,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') &&