/
Router.php
152 lines (134 loc) · 3.44 KB
/
Router.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/*
* @author PhileCMS
* @link https://philecms.github.io
* @license http://opensource.org/licenses/MIT
*/
namespace Phile\Core;
/**
* this Router class is responsible for Phile's basic URL management
*/
class Router
{
/**
* @var array with $_SERVER environment
*/
protected $server;
/**
* @param array $server $_SERVER environment
*/
public function __construct(array $server = [])
{
if (empty($server)) {
$server = $_SERVER;
}
$this->server = $server;
}
/**
* get request-URL relative to Phile base-URL
*
* @return string relative URL e.g. `index`, `sub/`, `sub/page`
*/
public function getCurrentUrl()
{
$url = $this->server['REQUEST_URI'];
// remove query string
list($url) = explode('?', $url);
// resolve root-relative URL-path
$baseUrl = $this->getBaseUrl();
$basePath = $this->getUrlPath($baseUrl);
if (!empty($basePath) && strpos($url, $basePath) === 0) {
$url = substr($url, strlen($basePath));
}
$url = ltrim($url, '/');
$url = rawurldecode($url);
return $url;
}
/**
* Get base-URL of the Phile installation
*
* @return string `scheme://host/path/phile-root`
*/
public function getBaseUrl()
{
$baseUrl = Container::getInstance()->get('Phile_Config')->get('base_url');
if (!empty($baseUrl)) {
return $baseUrl;
}
$url = '';
if (isset($this->server['PHP_SELF'])) {
$url = preg_replace('/index\.php(.*)?$/', '', (string)$this->server['PHP_SELF']);
}
$protocol = $this->getProtocol();
if (!empty($protocol)) {
$host = $this->server['HTTP_HOST'];
$url = $protocol . '://' . $host . $url;
}
$url = rtrim($url, '/');
return $url;
}
/**
* get the URL for a page-Id
*
* e.g. `sub/index` --> `http://host/phile-root/sub`
*
* @param string $pageId
* @param bool $base return a full or root-relative URL
* @return string URL
*/
public function urlForPage($pageId, $base = true)
{
$url = $pageId;
if ($base) {
$url = $this->url($url);
}
return $url;
}
/**
* converts Phile-root relative URL to full URL
*
* e.g. `foo/bar.ext` --> `http://host/phile-root/foo/bar.ext`
*
* @param string $url
* @return string
*/
public function url($url)
{
return $this->getBaseUrl() . '/' . ltrim($url, '/');
}
/**
* get the HTTP-protocol
*
* @return string|null
*/
public function getProtocol(): ?string
{
if (empty($this->server['HTTP_HOST'])) {
return null;
}
$protocol = 'http';
if (isset($this->server['HTTPS']) && strtolower($this->server['HTTPS']) !== 'off') {
$protocol = 'https';
}
return $protocol;
}
/**
* get path of an URL
*
* `scheme://host/path/sub` --> `/path/sub`
*
* @param string $url
* @return string
*/
protected function getUrlPath($url)
{
$path = '';
if (strpos($url, '://') !== false) {
$parsed = parse_url($url);
if (isset($parsed['path'])) {
$path = $parsed['path'];
}
}
return $path;
}
}