OSDN Git Service

BugTrack/2213 Use Absolute URI as Canonical URL
[pukiwiki/pukiwiki.git] / lib / html.php
index d6eb24d..d128972 100644 (file)
@@ -2,7 +2,7 @@
 // PukiWiki - Yet another WikiWikiWeb clone.
 // html.php
 // Copyright
-//   2002-2016 PukiWiki Development Team
+//   2002-2017 PukiWiki Development Team
 //   2001-2002 Originally written by yu-ji
 // License: GPL v2 or (at your option) any later version
 //
@@ -11,7 +11,7 @@
 // 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 $javascript, $nofollow;
@@ -24,9 +24,11 @@ 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) {
+       if (AUTH_TYPE_FORM === $auth_type || AUTH_TYPE_EXTERNAL === $auth_type ||
+               AUTH_TYPE_SAML === $auth_type) {
                if ($auth_user) {
                        $enable_logout = true;
                } else {
@@ -42,12 +44,16 @@ function catbody($title, $page, $body)
 
        $_LINK = $_IMAGE = array();
 
-       // Add JavaScript header when ...
-       if (! PKWK_ALLOW_JAVASCRIPT) unset($javascript);
-
        $_page  = isset($vars['page']) ? $vars['page'] : '';
        $r_page = pagename_urlencode($_page);
 
+       // Canonical URL
+       if ($_page === $defaultpage) {
+               $canonical_url = get_base_uri(PKWK_URI_ABSOLUTE);
+       } else {
+               $canonical_url = get_page_uri($_page, PKWK_URI_ABSOLUTE);
+       }
+
        // Set $_LINK for skin
        $_LINK['add']      = "$script?cmd=add&page=$r_page";
        $_LINK['backup']   = "$script?cmd=backup&page=$r_page";
@@ -61,7 +67,7 @@ function catbody($title, $page, $body)
        $_LINK['new']      = "$script?plugin=newpage&refer=$r_page";
        $_LINK['rdf']      = "$script?cmd=rss&ver=1.0";
        $_LINK['recent']   = "$script?" . pagename_urlencode($whatsnew);
-       $_LINK['reload']   = "$script?$r_page";
+       $_LINK['reload']   = $canonical_url;
        $_LINK['rename']   = "$script?plugin=rename&refer=$r_page";
        $_LINK['rss']      = "$script?cmd=rss";
        $_LINK['rss10']    = "$script?cmd=rss&ver=1.0"; // Same as 'rdf'
@@ -76,6 +82,7 @@ function catbody($title, $page, $body)
                        $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, $_LINK['reload']);
                        break;
        }
@@ -140,6 +147,11 @@ function catbody($title, $page, $body)
                }
        }
 
+       // 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 . htmlsc($vars['word']) .
@@ -178,21 +190,58 @@ function catbody($title, $page, $body)
                }
        }
 
+       // Embed Scripting data
+       $html_scripting_data = get_html_scripting_data();
+
        // Compat: 'HTML convert time' without time about MenuBar and skin
        $taketime = elapsedtime();
 
        require(SKIN_FILE);
 }
 
+/**
+ * Get data used by JavaScript modules
+ */
+function get_html_scripting_data()
+{
+       global $ticket_link_sites;
+       if (!isset($ticket_link_sites) || !is_array($ticket_link_sites)) {
+               return '';
+       }
+       // Require: PHP 5.4+
+       if (!defined('JSON_UNESCAPED_UNICODE')) {
+               return '';
+       };
+       $text = '';
+       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="pukiwiki-ticketlink-site" data-site="$site_info_json"></span>
+EOS;
+               $text .= "\n";
+       }
+       $data = <<<EOS
+<div class="pukiwiki-ticketlink-def" style="display:none;">
+$text
+</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 $vars, $rows, $cols, $hr, $function_freeze;
        global $_btn_preview, $_btn_repreview, $_btn_update, $_btn_cancel, $_msg_help;
        global $whatsnew, $_btn_template, $_btn_load, $load_template_func;
        global $notimeupdate;
        global $_title_list, $_label_template_pages;
+       global $rule_page;
 
+       $script = get_base_uri();
        // Newly generate $digest or not
        if ($digest === FALSE) $digest = md5(join('', get_source($page)));
 
@@ -224,7 +273,8 @@ function edit_form($page, $postdata, $digest = FALSE, $b_template = TRUE)
                }
                $page_names = array();
                foreach(get_existpages() as $_page) {
-                       if ($_page == $whatsnew || check_non_list($_page))
+                       if ($_page == $whatsnew || check_non_list($_page) ||
+                               !is_page_readable($_page))
                                continue;
                        if (preg_match('/template/i', $_page)) {
                                $tpage_names[] = $_page;
@@ -323,23 +373,19 @@ $template
 </div>
 EOD;
 
-       if (isset($vars['help'])) {
-               $body .= $hr . catrule();
-       } else {
-               $body .= '<ul><li><a href="' .
-                       $script . '?cmd=edit&amp;help=true&amp;page=' . $r_page .
-                       '">' . $_msg_help . '</a></li></ul>';
-       }
-
+       $body .= '<ul><li><a href="' .
+               $script . '?' . pagename_urlencode($rule_page) .
+               '" target="_blank">' . $_msg_help . '</a></li></ul>';
        return $body;
 }
 
 // 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) {
@@ -364,8 +410,7 @@ function make_related($page, $tag = '')
        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";
@@ -425,12 +470,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 = 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> ';
 }
 
@@ -602,3 +644,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"';
+}