Passed
Push — 1.0.0-dev ( b68981...00dab9 )
by nguereza
05:23
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
			$baseUrl = get_config('base_url');
53
			$frontController = get_config('front_controller');
54
			$url = $baseUrl;
55
			if($frontController){
56
				$url .= $frontController . '/';
57
			}
58
			if(($suffix = get_config('url_suffix')) && $path){
59
				if(strpos($path, '?') !== false){
60
					$query = explode('?', $path);
61
					$query[0] = str_ireplace($suffix, '', $query[0]);
62
					$query[0] = rtrim($query[0], '/');
63
					$query[0] .= $suffix;
64
					$path = implode('?', $query);
65
				}
66
				else{
67
					$path .= $suffix;
68
				}
69
			}
70
			return $url . $path;
71
		}
72
73
		/**
74
		 * Return the current site URL
75
		 * @return string
76
		 */
77
		public static function current(){
78
			$current = '/';
79
			$requestUri = get_instance()->request->requestUri();
80
			if($requestUri){
81
				$current = $requestUri;
82
			}
83
			return static::domain() . $current;
84
		}
85
86
		/**
87
		 * Generate a friendly  text to use in link (slugs)
88
		 * @param  string  $str       the title or text to use to get the friendly text
89
		 * @param  string  $separator the caracters separator
90
		 * @param  boolean $lowercase whether to set the final text to lowe case or not
91
		 * @return string the friendly generated text
92
		 */
93
		public static function title($str = null, $separator = '-', $lowercase = true){
94
			$str = trim($str);
95
			$from = array('ç','À','Á','Â','Ã','Ä','Å','à','á','â','ã','ä','å','Ò','Ó','Ô','Õ','Ö','Ø','ò','ó','ô','õ','ö','ø','È','É','Ê','Ë','è','é','ê','ë','Ç','ç','Ì','Í','Î','Ï','ì','í','î','ï','Ù','Ú','Û','Ü','ù','ú','û','ü','ÿ','Ñ','ñ');
96
			$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');
97
			$str = str_replace($from, $to, $str);
98
			$str = preg_replace('#([^a-z0-9]+)#i', $separator, $str);
99
			$str = str_replace('--', $separator, $str);
100
			//if after process we get something like one-two-three-, need truncate the last separator "-"
101
			if(substr($str, -1) == $separator){
102
				$str = substr($str, 0, -1);
103
			}
104
			if($lowercase){
105
				$str = strtolower($str);
106
			}
107
			return $str;
108
		}
109
110
		/**
111
		 * Get the current application domain with protocol
112
		 * @return string the domain name
113
		 */
114
		public static function domain(){
115
			$obj = & get_instance();
116
			$domain = 'localhost';
117
			$port = $obj->request->server('SERVER_PORT');
118
			$protocol = 'http';
119
			if(is_https()){
120
				$protocol = 'https';
121
			}
122
123
			$domainserverVars = array(
124
				'HTTP_HOST',
125
				'SERVER_NAME',
126
				'SERVER_ADDR'
127
			);
128
129
			foreach ($domainserverVars as $var) {
130
				$value = $obj->request->server($var);
131
				if($value){
132
					$domain = $value;
133
					break;
134
				}
135
			}
136
			
137
			if($port && ((is_https() && $port != 443) || (!is_https() && $port != 80))){
138
				//some server use SSL but the port doesn't equal 443 sometime is 80 if is the case put the port at this end
139
				//of the domain like https://my.domain.com:787
140
				if(is_https() && $port != 80){
141
					$domain .= ':'.$port;
142
				}
143
			}
144
			return $protocol.'://'.$domain;
145
		}
146
147
		/**
148
		 * Get the current request query string
149
		 * @return string
150
		 */
151
		public static function queryString(){
152
			return get_instance()->request->server('QUERY_STRING');
153
		}
154
	}
155