• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Commit MetaInfo

Revisionebefc4e7bfd62d78f53c666ee0a92f2088d67b9c (tree)
Time2017-09-02 12:50:22
Authorumorigu <umorigu@gmai...>
Commiterumorigu

Log Message

BugTrack/2213 Use relative path for link URI

Add URI type enum.

* PKWK_URI_RELATIVE : relative path (ex: ./, index.php )
* PKWK_URI_ROOT : Root relative path (ex: /wiki/)
* PKWK_URI_ABSOLUTE : Absolute URI (ex: http://example.com/wiki/)

Change Summary

Incremental Difference

--- a/lib/func.php
+++ b/lib/func.php
@@ -8,6 +8,14 @@
88 //
99 // General functions
1010
11+// URI type enum
12+/** Relative path. */
13+define('PKWK_URI_RELATIVE', 0);
14+/** Root relative URI. */
15+define('PKWK_URI_ROOT', 1);
16+/** Absolute URI. */
17+define('PKWK_URI_ABSOLUTE', 2);
18+
1119 function pkwk_log($message)
1220 {
1321 $log_filepath = 'log/error.log.php';
@@ -692,40 +700,77 @@ function get_autolink_pattern_sub(& $pages, $start, $end, $pos)
692700 }
693701
694702 // Get absolute-URI of this script
695-function get_script_uri($init_uri = '')
703+/**
704+ * Get propery URI of this script
705+ *
706+ * @param $uri_type relative or absolute potion
707+ * PKWK_URI_RELATIVE, PKWK_URI_ROOT or PKWK_URI_ABSOLUTE
708+ */
709+function get_script_uri($uri_type = PKWK_URI_RELATIVE)
696710 {
697- global $script_directory_index;
698- static $script;
699-
700- if ($init_uri == '') {
701- // Get
702- if (isset($script)) return $script;
703-
704- // Set automatically
705- $msg = 'get_script_uri() failed: Please set $script at INI_FILE manually';
706- $script = guess_script_absolute_uri();
707- if (! is_url($script, TRUE) && php_sapi_name() == 'cgi')
708- die_message($msg);
709- unset($msg);
710-
711- } else {
712- // Set manually
713- if (isset($script)) die_message('$script: Already init');
714- if (! is_url($init_uri, TRUE)) die_message('$script: Invalid URI');
715- $script = $init_uri;
711+ switch ($uri_type) {
712+ case PKWK_URI_RELATIVE:
713+ return pkwk_script_uri_base(PKWK_URI_RELATIVE);
714+ case PKWK_URI_ROOT:
715+ return pkwk_script_uri_base(PKWK_URI_ROOT);
716+ case PKWK_URI_ABSOLUTE:
717+ return pkwk_script_uri_base(PKWK_URI_ABSOLUTE);
718+ default:
719+ die_message('Invalid uri_type in get_script_uri()');
716720 }
721+}
717722
718- // Cut filename or not
719- if (isset($script_directory_index)) {
720- if (! file_exists($script_directory_index))
721- die_message('Directory index file not found: ' .
722- htmlsc($script_directory_index));
723- $matches = array();
724- if (preg_match('#^(.+/)' . preg_quote($script_directory_index, '#') . '$#',
725- $script, $matches)) $script = $matches[1];
723+/**
724+ * Get or initialize Script URI
725+ *
726+ * @param $uri_type relative or absolute potion
727+ * PKWK_URI_RELATIVE, PKWK_URI_ROOT or PKWK_URI_ABSOLUTE
728+ * @param $initialize true if you initialize URI
729+ * @param $uri_set URI set manually
730+ */
731+ function pkwk_script_uri_base($uri_type, $initialize, $uri_set) {
732+ global $script_directory_index;
733+ static $initialized = false;
734+ static $uri_absolute, $uri_root, $uri_relative;
735+ //$script_directory_index = 'index.php';
736+ if (isset($initialize) && $initialize) {
737+ if (isset($uri_set)) {
738+ $uri_absolute = $uri_set;
739+ } else {
740+ $uri_absolute = guess_script_absolute_uri();
741+ // Support $script_directory_index (cut 'index.php')
742+ if (isset($script_directory_index)) {
743+ $slash_index = '/' . $script_directory_index;
744+ $len = strlen($slash_index);
745+ if (substr($uri_absolute, -1 * $len) === $slash_index) {
746+ $uri_absolute = substr($uri_absolute, 0, strlen($uri_absolute) - $len + 1);
747+ }
748+ }
749+ }
750+ $elements = parse_url($uri_absolute);
751+ $uri_root = $elements['path'];
752+ if (substr($uri_root, -1) === '/') {
753+ $uri_relative = './';
754+ } else {
755+ $pos = mb_strrpos($uri_root, '/');
756+ if ($pos >= 0) {
757+ $uri_relative = substr($uri_root, $pos + 1);
758+ } else {
759+ $uri_relative = $uri_root;
760+ }
761+ }
762+ $initialized = true;
763+ }
764+ switch ($uri_type) {
765+ case PKWK_URI_RELATIVE:
766+ return $uri_relative;
767+ case PKWK_URI_ROOT:
768+ return $uri_root;
769+ case PKWK_URI_ABSOLUTE:
770+ return $uri_absolute;
771+ default:
772+ die_message('Invalid uri_type in pkwk_script_uri_base()');
726773 }
727-
728- return $script;
729774 }
730775
731776 /**
--- a/lib/init.php
+++ b/lib/init.php
@@ -134,9 +134,12 @@ $weeklabels = $_msg_week;
134134 // INI_FILE: Init $script
135135
136136 if (isset($script)) {
137- get_script_uri($script); // Init manually
137+ // Init manually
138+ pkwk_script_uri_base(PKWK_URI_ABSOLUTE, true, $script);
138139 } else {
139- $script = get_script_uri(); // Init automatically
140+ // Init automatically
141+ $__uri_absolute = pkwk_script_uri_base(PKWK_URI_ABSOLUTE, true);
142+ $script = $__uri_absolute;
140143 }
141144
142145 // INI_FILE: Auth settings