Passed
Push — 1.0.0-dev ( 632010...3e5cd9 )
by nguereza
02:59
created

Url::base_url()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
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 GNU GPL License (GPL)
9
     *
10
     * Copyright (C) 2017 Tony NGUEREZA
11
     *
12
     * This program is free software; you can redistribute it and/or
13
     * modify it under the terms of the GNU General Public License
14
     * as published by the Free Software Foundation; either version 3
15
     * of the License, or (at your option) any later version.
16
     *
17
     * This program is distributed in the hope that it will be useful,
18
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
     * GNU General Public License for more details.
21
     *
22
     * You should have received a copy of the GNU General Public License
23
     * along with this program; if not, write to the Free Software
24
     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25
     */
26
27
28
    class Url {
29
30
        /**
31
         * Return the link using base_url config without front controller "index.php"
32
         * @param  string $path the link path or full URL
33
         * @return string the full link URL
34
         */
35
        public static function base_url($path = '') {
36
            if (is_url($path)) {
37
                return $path;
38
            }
39
            return get_config('base_url') . $path;
40
        }
41
42
        /**
43
         * Return the link using base_url config with front controller "index.php"
44
         * @param  string $path the link path or full URL
45
         * @return string the full link URL
46
         */
47
        public static function site_url($path = '') {
48
            if (is_url($path)) {
49
                return $path;
50
            }
51
            $path = rtrim($path, '/');
52
            $url = get_config('base_url');
53
            $frontController = get_config('front_controller');
54
            if ($frontController) {
55
                $url .= $frontController . '/';
56
            }
57
            $path = self::addSuffixInPath($path);
58
            return $url . $path;
59
        }
60
61
        /**
62
         * Return the current site URL
63
         * @return string
64
         */
65
        public static function current() {
66
            $current = '/';
67
            $requestUri = get_instance()->request->requestUri();
68
            if ($requestUri) {
69
                $current = $requestUri;
70
            }
71
            return static::domain() . $current;
72
        }
73
74
        /**
75
         * Generate a friendly  text to use in link (slugs)
76
         * @param  string  $str       the title or text to use to get the friendly text
77
         * @param  string  $separator the caracters separator
78
         * @param  boolean $lowercase whether to set the final text to lowe case or not
79
         * @return string the friendly generated text
80
         */
81
        public static function title($str = null, $separator = '-', $lowercase = true) {
82
            $str = trim($str);
83
            $from = array('ç', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'à', 'á', 'â', 'ã', 'ä', 'å', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'È', 'É', 'Ê', 'Ë', 'è', 'é', 'ê', 'ë', 'Ç', 'ç', 'Ì', 'Í', 'Î', 'Ï', 'ì', 'í', 'î', 'ï', 'Ù', 'Ú', 'Û', 'Ü', 'ù', 'ú', 'û', 'ü', 'ÿ', 'Ñ', 'ñ');
84
            $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');
85
            $str = str_replace($from, $to, $str);
86
            $str = preg_replace('#([^a-z0-9]+)#i', $separator, $str);
87
            $str = str_replace('--', $separator, $str);
88
            //if after process we get something like one-two-three-, need truncate the last separator "-"
89
            if (substr($str, -1) == $separator) {
90
                $str = substr($str, 0, -1);
91
            }
92
            if ($lowercase) {
93
                $str = strtolower($str);
94
            }
95
            return $str;
96
        }
97
98
        /**
99
         * Get the current application domain with protocol
100
         * @return string the domain name
101
         */
102
        public static function domain() {
103
            $domain = 'localhost';
104
            $port = get_instance()->request->server('SERVER_PORT');
105
            $protocol = 'http';
106
            if (is_https()) {
107
                $protocol = 'https';
108
            }
109
110
            $domainserverVars = array(
111
                'HTTP_HOST',
112
                'SERVER_NAME',
113
                'SERVER_ADDR'
114
            );
115
116
            foreach ($domainserverVars as $var) {
117
                $value = get_instance()->request->server($var);
118
                if ($value) {
119
                    $domain = $value;
120
                    break;
121
                }
122
            }
123
			
124
            if ($port && ((is_https() && $port != 443) || (!is_https() && $port != 80))) {
125
                $domain .= ':' . $port;
126
            }
127
            return $protocol . '://' . $domain;
128
        }
129
130
        /**
131
         * Get the current request query string
132
         * @return string
133
         */
134
        public static function queryString() {
135
            return get_instance()->request->server('QUERY_STRING');
136
        }
137
138
        /**
139
         * Add configured suffixe in the path
140
         * @param string $path the path
141
         *
142
         * @return string the final path after add suffix if configured
143
         * otherwise the same value will be returned
144
         */
145
        protected static function addSuffixInPath($path){
146
            $suffix = get_config('url_suffix');
147
            if ($suffix && $path) {
148
                if (strpos($path, '?') !== false) {
149
                    $query = explode('?', $path);
150
                    $query[0] = str_ireplace($suffix, '', $query[0]);
151
                    $query[0] = rtrim($query[0], '/');
152
                    $query[0] .= $suffix;
153
                    $path = implode('?', $query);
154
                } else {
155
                    $path .= $suffix;
156
                }
157
            }
158
            return $path;
159
        }
160
161
    }
162