4
4
*
5
5
* @author Josantonius - [email protected]
6
6
* @author David Carr - [email protected]
7
- * @copyright Copyright (c) 2017 JST PHP Framework
7
+ * @copyright Copyright (c) 2017
8
8
* @license https://opensource.org/licenses/MIT - The MIT License (MIT)
9
9
* @link https://github.com/Josantonius/PHP-Url
10
10
* @since 1.0.0
11
11
*/
12
12
13
13
namespace Josantonius \Url ;
14
14
15
- # use Josantonius\Url\Exception\UrlException;
16
-
17
15
/**
18
16
* Url handler.
19
17
*
20
18
* @since 1.0.0
21
19
*/
22
20
class Url {
23
21
24
- /**
25
- * Directory separator.
26
- *
27
- * @since 1.1.2
28
- *
29
- * @var string
30
- */
31
- const DS = DIRECTORY_SEPARATOR ;
32
-
33
22
/**
34
23
* Get url from the current page.
35
24
*
@@ -49,7 +38,7 @@ public static function getCurrentPage() {
49
38
50
39
$ uri = self ::getUri ();
51
40
52
- return $ protocol . ': ' . self :: DS . self :: DS . $ host . $ port . $ uri ;
41
+ return $ protocol . ':// ' . $ host . $ port . $ uri ;
53
42
}
54
43
55
44
/**
@@ -65,9 +54,9 @@ public static function getBaseUrl() {
65
54
66
55
$ url = self ::addBackslash (self ::getCurrentPage ());
67
56
68
- if ($ uri !== self :: DS ) {
57
+ if ($ uri !== ' / ' ) {
69
58
70
- $ url = trim (str_replace ($ uri , '' , $ url ), self :: DS );
59
+ $ url = trim (str_replace ($ uri , '' , $ url ), ' / ' );
71
60
}
72
61
73
62
return self ::addBackslash ($ url );
@@ -78,13 +67,20 @@ public static function getBaseUrl() {
78
67
*
79
68
* @since 1.0.0
80
69
*
70
+ * @param string $url
71
+ *
81
72
* @return string → http|https
82
73
*/
83
- public static function getProtocol () {
74
+ public static function getProtocol ($ url = false ) {
75
+
76
+ if ($ url ) {
77
+
78
+ return (preg_match ('/^https/ ' , $ url )) ? 'https ' : 'http ' ;
79
+ }
84
80
85
81
$ protocol = strtolower ($ _SERVER ['SERVER_PROTOCOL ' ]);
86
82
87
- $ protocol = substr ($ protocol , 0 , strpos ($ protocol , self :: DS ));
83
+ $ protocol = substr ($ protocol , 0 , strpos ($ protocol , ' / ' ));
88
84
89
85
$ ssl = (!empty ($ _SERVER ['HTTPS ' ]) && $ _SERVER ['HTTPS ' ] == 'on ' );
90
86
@@ -96,21 +92,32 @@ public static function getProtocol() {
96
92
*
97
93
* @since 1.0.0
98
94
*
95
+ * @param string $url
96
+ *
99
97
* @return boolean
100
98
*/
101
- public static function isSSL () {
99
+ public static function isSSL ($ url = false ) {
102
100
103
- return (self ::getProtocol () === 'https ' );
101
+ return (self ::getProtocol ($ url ) === 'https ' );
104
102
}
105
103
106
104
/**
107
105
* Get the server name.
108
106
*
109
107
* @since 1.0.0
110
108
*
111
- * @return string → server name
109
+ * @param string $url
110
+ *
111
+ * @return string|false → server name
112
112
*/
113
- public static function getDomain () {
113
+ public static function getDomain ($ url = false ) {
114
+
115
+ if ($ url ) {
116
+
117
+ preg_match ('/([\w]+[.]){1,}[a-z]+/ ' , $ url , $ matches );
118
+
119
+ return isset ($ matches [0 ]) ? $ matches [0 ] : false ;
120
+ }
114
121
115
122
return $ _SERVER ['SERVER_NAME ' ];
116
123
}
@@ -138,9 +145,9 @@ public static function getUriMethods() {
138
145
139
146
$ root = str_replace ($ _SERVER ["DOCUMENT_ROOT " ], '' , getcwd ());
140
147
141
- $ subfolder = trim ($ root , self :: DS );
148
+ $ subfolder = trim ($ root , ' / ' );
142
149
143
- return trim (str_replace ($ subfolder , '' , self ::getUri ()), self :: DS );
150
+ return trim (str_replace ($ subfolder , '' , self ::getUri ()), ' / ' );
144
151
}
145
152
146
153
/**
@@ -171,11 +178,11 @@ public static function addBackslash($uri, $position = 'end') {
171
178
172
179
case 'top ' :
173
180
174
- return (substr ($ uri , 1 ) === self :: DS ) ? $ uri : self :: DS . $ uri ;
181
+ return (substr ($ uri , 1 ) === ' / ' ) ? $ uri : ' / ' . $ uri ;
175
182
176
183
case 'end ' :
177
184
178
- return (substr ($ uri , -1 ) === self :: DS ) ? $ uri : $ uri. self :: DS ;
185
+ return (substr ($ uri , -1 ) === ' / ' ) ? $ uri : $ uri . ' / ' ;
179
186
180
187
case 'both ' :
181
188
@@ -240,7 +247,6 @@ public static function autoLink($url, $custom = null) {
240
247
/**
241
248
* This function converts and url segment to an safe one.
242
249
* For example: `test name @132` will be converted to `test-name--123`.
243
- * Replace every character that isn't an letter or an number to an dash sign.
244
250
* It will also return all letters in lowercase
245
251
*
246
252
* @since 1.0.0
@@ -272,7 +278,7 @@ public static function segment($uri = null) {
272
278
273
279
$ uri = (!is_null ($ uri )) ? $ uri : $ _SERVER ['REQUEST_URI ' ];
274
280
275
- return explode (self :: DS , trim ($ uri , self :: DS ));
281
+ return explode (' / ' , trim ($ uri , ' / ' ));
276
282
}
277
283
278
284
/**
0 commit comments