Passed
Push — 1.0.0-dev ( 8851fa...9b3671 )
by nguereza
02:38
created

Url::site_url()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 12
rs 9.9666
c 1
b 0
f 0
1
<?php
2
    defined('ROOT_PATH') or exit('Access denied');
3
    /**
4
     * TNH Framework
5
     *
6
     * A simple PHP framework using HMVC architecture
7
     *
8
     * This content is released under the MIT License (MIT)
9
     *
10
     * Copyright (c) 2017 TNH Framework
11
     *
12
     * Permission is hereby granted, free of charge, to any person obtaining a copy
13
     * of this software and associated documentation files (the "Software"), to deal
14
     * in the Software without restriction, including without limitation the rights
15
     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
     * copies of the Software, and to permit persons to whom the Software is
17
     * furnished to do so, subject to the following conditions:
18
     *
19
     * The above copyright notice and this permission notice shall be included in all
20
     * copies or substantial portions of the Software.
21
     *
22
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
     * SOFTWARE.
29
     */
30
31
    class Url extends BaseClass{
32
33
        /**
34
         * Construct new instance
35
         */
36
        public function __construct() {
37
            parent::__construct();
38
        }
39
        
40
        /**
41
         * Return the link using "base_url" config without front controller "index.php"
42
         * @param  string $path the link path or full URL
43
         * @return string the full link URL
44
         */
45
        public function mainUrl($path = '') {
46
            if (is_url($path)) {
47
                return $path;
48
            }
49
            return get_config('base_url') . $path;
50
        }
51
52
        /**
53
         * Return the link using "base_url" config with front controller "index.php"
54
         * @param  string $path the link path or full URL
55
         * @return string the full link URL
56
         */
57
        public function appUrl($path = '') {
58
            if (is_url($path)) {
59
                return $path;
60
            }
61
            $path = rtrim($path, '/');
62
            $url = get_config('base_url');
63
            $frontController = get_config('front_controller');
64
            if ($frontController) {
65
                $url .= $frontController . '/';
66
            }
67
            $path = $this->addSuffixInPath($path);
68
            return $url . $path;
69
        }
70
71
        /**
72
         * Return the current site URL
73
         * @return string
74
         */
75
        public function current() {
76
            $current = '/';
77
            $requestUri = get_instance()->request->requestUri();
78
            if ($requestUri) {
79
                $current = $requestUri;
80
            }
81
            return $this->domain() . $current;
82
        }
83
84
        /**
85
         * Generate a friendly  text to use in link (slugs)
86
         * @param  string  $str       the title or text to use to get the friendly text
87
         * @param  string  $separator the caracters separator
88
         * @param  boolean $lowercase whether to set the final text to lowe case or not
89
         * @return string the friendly generated text
90
         */
91
        public function title($str = null, $separator = '-', $lowercase = true) {
92
            $str = trim($str);
93
            $from = array('ç', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'à', 'á', 'â', 'ã', 'ä', 'å', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'È', 'É', 'Ê', 'Ë', 'è', 'é', 'ê', 'ë', 'Ç', 'ç', 'Ì', 'Í', 'Î', 'Ï', 'ì', 'í', 'î', 'ï', 'Ù', 'Ú', 'Û', 'Ü', 'ù', 'ú', 'û', 'ü', 'ÿ', 'Ñ', 'ñ');
94
            $to = array('c', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'y', 'n', 'n');
95
            $str = str_replace($from, $to, $str);
96
            $str = preg_replace('#([^a-z0-9]+)#i', $separator, $str);
97
            $str = str_replace('--', $separator, $str);
98
            //if after process we get something like one-two-three-, need truncate the last separator "-"
99
            if (substr($str, -1) == $separator) {
100
                $str = substr($str, 0, -1);
101
            }
102
            if ($lowercase) {
103
                $str = strtolower($str);
104
            }
105
            return $str;
106
        }
107
108
        /**
109
         * Get the current application domain with protocol
110
         * @return string the domain name
111
         */
112
        public function domain() {
113
            $domain = 'localhost';
114
            $port = get_instance()->request->server('SERVER_PORT');
115
            $protocol = 'http';
116
            if (is_https()) {
117
                $protocol = 'https';
118
            }
119
120
            $domainserverVars = array(
121
                'HTTP_HOST',
122
                'SERVER_NAME',
123
                'SERVER_ADDR'
124
            );
125
            foreach ($domainserverVars as $var) {
126
                $value = get_instance()->request->server($var);
127
                if ($value) {
128
                    $domain = $value;
129
                    break;
130
                }
131
            }
132
			
133
            if ($port && ((is_https() && $port != 443) || (!is_https() && $port != 80))) {
134
                $domain .= ':' . $port;
135
            }
136
            return $protocol . '://' . $domain;
137
        }
138
139
        /**
140
         * Get the current request query string
141
         * @return string
142
         */
143
        public function queryString() {
144
            return get_instance()->request->server('QUERY_STRING');
145
        }
146
147
        /**
148
         * Add configured suffixe in the path
149
         * @param string $path the path
150
         *
151
         * @return string the final path after add suffix if configured
152
         * otherwise the same value will be returned
153
         */
154
        protected function addSuffixInPath($path){
155
            $suffix = get_config('url_suffix');
156
            if ($suffix && $path) {
157
                if (strpos($path, '?') !== false) {
158
                    $query = explode('?', $path);
159
                    $query[0] = str_ireplace($suffix, '', $query[0]);
160
                    $query[0] = rtrim($query[0], '/');
161
                    $query[0] .= $suffix;
162
                    $path = implode('?', $query);
163
                } else {
164
                    $path .= $suffix;
165
                }
166
            }
167
            return $path;
168
        }
169
170
    }
171