Revision | ebefc4e7bfd62d78f53c666ee0a92f2088d67b9c (tree) |
---|---|
Time | 2017-09-02 12:50:22 |
Author | umorigu <umorigu@gmai...> |
Commiter | umorigu |
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/)
@@ -8,6 +8,14 @@ | ||
8 | 8 | // |
9 | 9 | // General functions |
10 | 10 | |
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 | + | |
11 | 19 | function pkwk_log($message) |
12 | 20 | { |
13 | 21 | $log_filepath = 'log/error.log.php'; |
@@ -692,40 +700,77 @@ function get_autolink_pattern_sub(& $pages, $start, $end, $pos) | ||
692 | 700 | } |
693 | 701 | |
694 | 702 | // 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) | |
696 | 710 | { |
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()'); | |
716 | 720 | } |
721 | +} | |
717 | 722 | |
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()'); | |
726 | 773 | } |
727 | - | |
728 | - return $script; | |
729 | 774 | } |
730 | 775 | |
731 | 776 | /** |
@@ -134,9 +134,12 @@ $weeklabels = $_msg_week; | ||
134 | 134 | // INI_FILE: Init $script |
135 | 135 | |
136 | 136 | if (isset($script)) { |
137 | - get_script_uri($script); // Init manually | |
137 | + // Init manually | |
138 | + pkwk_script_uri_base(PKWK_URI_ABSOLUTE, true, $script); | |
138 | 139 | } 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; | |
140 | 143 | } |
141 | 144 | |
142 | 145 | // INI_FILE: Auth settings |