-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.php
293 lines (256 loc) · 9.68 KB
/
server.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
/**
* Define the user's "~/.valet" path.
*/
define('VALET_HOME_PATH', posix_getpwuid(fileowner(__FILE__))['dir'] . '/.valet');
define('VALET_STATIC_PREFIX', '41c270e4-5535-4daa-b23e-c269744c2f45');
/**
* Show the Valet 404 "Not Found" page.
*/
function show_valet_404()
{
http_response_code(404);
require __DIR__ . '/cli/templates/404.html';
exit;
}
/**
* Show the Valet 404 "Not Found" page.
*/
function show_valet_index($valetConfig)
{
require __DIR__ . '/cli/templates/index.php';
exit;
}
/**
* Show the Valet 404 "Not Found" page.
*
* @param string $directory
* @param string $siteName
* @param string $uri
* @param array $ignoredPaths
*/
function show_project_directory($directory, $siteName, $uri, $ignoredPaths = ['.'])
{
if (!substr($uri, 0, -1)) {
$ignoredPaths[] = '..';
}
require __DIR__ . '/cli/templates/directory.php';
exit;
}
/**
* Show available sites.
*
* @param array $valetConfig
*/
function show_available_sites($valetConfig)
{
$availableSites = [];
foreach ($valetConfig['paths'] as $path) {
foreach (glob($path . '/*', GLOB_ONLYDIR) as $dirPath) {
$slug = valet_path_to_slug($dirPath);
$availableSites[$slug] = ucfirst(str_replace('-', ' ', $slug));
}
}
require __DIR__ . '/cli/templates/sites.php';
exit;
}
/**
* @param $domain string Domain to filter
*
* @return string Filtered domain (without xip.io feature)
*/
function valet_support_xip_io($domain)
{
if (substr($domain, -7) === '.xip.io') {
// support only ip v4 for now
$domainPart = explode('.', $domain);
if (count($domainPart) > 6) {
$domain = implode('.', array_reverse(array_slice(array_reverse($domainPart), 6)));
}
}
if (strpos($domain, ':') !== false) {
$domain = explode(':', $domain)[0];
}
return $domain;
}
/**
* Convert absolute path to slug.
*
* @param string $path
*
* @return string Slug version of last folder name
*/
function valet_path_to_slug($path)
{
$replace = [
'<' => '', '>' => '', ''' => '', '&' => '',
'"' => '', 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'Ae',
'Ä' => 'A', 'Å' => 'A', 'Ā' => 'A', 'Ą' => 'A', 'Ă' => 'A', 'Æ' => 'Ae',
'Ç' => 'C', 'Ć' => 'C', 'Č' => 'C', 'Ĉ' => 'C', 'Ċ' => 'C', 'Ď' => 'D', 'Đ' => 'D',
'Ð' => 'D', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ē' => 'E',
'Ę' => 'E', 'Ě' => 'E', 'Ĕ' => 'E', 'Ė' => 'E', 'Ĝ' => 'G', 'Ğ' => 'G',
'Ġ' => 'G', 'Ģ' => 'G', 'Ĥ' => 'H', 'Ħ' => 'H', 'Ì' => 'I', 'Í' => 'I',
'Î' => 'I', 'Ï' => 'I', 'Ī' => 'I', 'Ĩ' => 'I', 'Ĭ' => 'I', 'Į' => 'I',
'İ' => 'I', 'IJ' => 'IJ', 'Ĵ' => 'J', 'Ķ' => 'K', 'Ł' => 'K', 'Ľ' => 'K',
'Ĺ' => 'K', 'Ļ' => 'K', 'Ŀ' => 'K', 'Ñ' => 'N', 'Ń' => 'N', 'Ň' => 'N',
'Ņ' => 'N', 'Ŋ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O',
'Ö' => 'Oe', 'Ö' => 'Oe', 'Ø' => 'O', 'Ō' => 'O', 'Ő' => 'O', 'Ŏ' => 'O',
'Œ' => 'OE', 'Ŕ' => 'R', 'Ř' => 'R', 'Ŗ' => 'R', 'Ś' => 'S', 'Š' => 'S',
'Ş' => 'S', 'Ŝ' => 'S', 'Ș' => 'S', 'Ť' => 'T', 'Ţ' => 'T', 'Ŧ' => 'T',
'Ț' => 'T', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'Ue', 'Ū' => 'U',
'Ü' => 'Ue', 'Ů' => 'U', 'Ű' => 'U', 'Ŭ' => 'U', 'Ũ' => 'U', 'Ų' => 'U',
'Ŵ' => 'W', 'Ý' => 'Y', 'Ŷ' => 'Y', 'Ÿ' => 'Y', 'Ź' => 'Z', 'Ž' => 'Z',
'Ż' => 'Z', 'Þ' => 'T', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a',
'ä' => 'ae', 'ä' => 'ae', 'å' => 'a', 'ā' => 'a', 'ą' => 'a', 'ă' => 'a',
'æ' => 'ae', 'ç' => 'c', 'ć' => 'c', 'č' => 'c', 'ĉ' => 'c', 'ċ' => 'c',
'ď' => 'd', 'đ' => 'd', 'ð' => 'd', 'è' => 'e', 'é' => 'e', 'ê' => 'e',
'ë' => 'e', 'ē' => 'e', 'ę' => 'e', 'ě' => 'e', 'ĕ' => 'e', 'ė' => 'e',
'ƒ' => 'f', 'ĝ' => 'g', 'ğ' => 'g', 'ġ' => 'g', 'ģ' => 'g', 'ĥ' => 'h',
'ħ' => 'h', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ī' => 'i',
'ĩ' => 'i', 'ĭ' => 'i', 'į' => 'i', 'ı' => 'i', 'ij' => 'ij', 'ĵ' => 'j',
'ķ' => 'k', 'ĸ' => 'k', 'ł' => 'l', 'ľ' => 'l', 'ĺ' => 'l', 'ļ' => 'l',
'ŀ' => 'l', 'ñ' => 'n', 'ń' => 'n', 'ň' => 'n', 'ņ' => 'n', 'ʼn' => 'n',
'ŋ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'oe',
'ö' => 'oe', 'ø' => 'o', 'ō' => 'o', 'ő' => 'o', 'ŏ' => 'o', 'œ' => 'oe',
'ŕ' => 'r', 'ř' => 'r', 'ŗ' => 'r', 'š' => 's', 'ù' => 'u', 'ú' => 'u',
'û' => 'u', 'ü' => 'ue', 'ū' => 'u', 'ü' => 'ue', 'ů' => 'u', 'ű' => 'u',
'ŭ' => 'u', 'ũ' => 'u', 'ų' => 'u', 'ŵ' => 'w', 'ý' => 'y', 'ÿ' => 'y',
'ŷ' => 'y', 'ž' => 'z', 'ż' => 'z', 'ź' => 'z', 'þ' => 't', 'ß' => 'ss',
'ſ' => 'ss', 'ый' => 'iy', 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G',
'Д' => 'D', 'Е' => 'E', 'Ё' => 'YO', 'Ж' => 'ZH', 'З' => 'Z', 'И' => 'I',
'Й' => 'Y', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', 'О' => 'O',
'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U', 'Ф' => 'F',
'Х' => 'H', 'Ц' => 'C', 'Ч' => 'CH', 'Ш' => 'SH', 'Щ' => 'SCH', 'Ъ' => '',
'Ы' => 'Y', 'Ь' => '', 'Э' => 'E', 'Ю' => 'YU', 'Я' => 'YA', 'а' => 'a',
'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'yo',
'ж' => 'zh', 'з' => 'z', 'и' => 'i', 'й' => 'y', 'к' => 'k', 'л' => 'l',
'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's',
'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c', 'ч' => 'ch',
'ш' => 'sh', 'щ' => 'sch', 'ъ' => '', 'ы' => 'y', 'ь' => '', 'э' => 'e',
'ю' => 'yu', 'я' => 'ya',
];
// make a human readable string
$slug = strtr(basename($path), $replace);
// replace non letter or digits by -
$slug = preg_replace('~[^\\pL\d.]+~u', '-', $slug);
// trim
$slug = trim($slug, '-');
// remove unwanted characters
$slug = preg_replace('~[^-\w.]+~', '', $slug);
return strtolower($slug);
}
/**
* Load the Valet configuration.
*/
$valetConfig = json_decode(
file_get_contents(VALET_HOME_PATH . '/config.json'),
true
);
/**
* Parse the URI and site / host for the incoming request.
*/
$uri = rawurldecode(
explode('?', $_SERVER['REQUEST_URI'])[0]
);
$siteName = basename(
// Filter host to support xip.io feature
valet_support_xip_io(explode(':', strtolower($_SERVER['HTTP_HOST']))[0]),
'.' . $valetConfig['domain']
);
if (strpos($siteName, 'www.') === 0) {
$siteName = substr($siteName, 4);
}
/**
* Validate if request is from Remote IP.
* */
if ($_SERVER['SERVER_ADDR'] !== '127.0.0.1') {
if (strpos($uri, '/') === 0) {
$urlParam = substr($uri, 1, strlen($uri));
if (substr($urlParam, -1) === '/') {
$urlParam = substr($urlParam, 0, -1);
}
if (strtolower($urlParam) === 'valet-sites') {
$urlParams = parse_url($_SERVER['REQUEST_URI']);
if (isset($urlParams['query'])) {
parse_str($urlParams['query'], $parameters);
if ($parameters['use']) {
setcookie('valet_remote_path', $parameters['use'], 0);
header('Location: /');
exit;
}
}
show_available_sites($valetConfig);
exit;
}
}
if (!isset($_COOKIE['valet_remote_path'])) {
header('Location: /valet-sites');
exit;
}
$siteName = $_COOKIE['valet_remote_path'];
}
/**
* Determine the fully qualified path to the site.
*/
$valetSitePath = null;
foreach ($valetConfig['paths'] as $path) {
$domain = ($pos = strrpos($siteName, '.')) !== false
? substr($siteName, $pos + 1)
: null;
foreach (glob($path . '/*', GLOB_ONLYDIR) as $dirPath) {
$slug = valet_path_to_slug($dirPath);
if ($slug == $siteName || $slug == $domain) {
$valetSitePath = $dirPath;
break 2;
}
}
}
if (in_array($_SERVER['HTTP_HOST'], ['localhost', '127.0.0.1'])) {
show_valet_index($valetConfig);
}
if (is_null($valetSitePath)) {
show_valet_404();
}
/**
* Find the appropriate Valet driver for the request.
*/
$valetDriver = null;
require __DIR__ . '/cli/drivers/require.php';
$valetDriver = ValetDriver::assign($valetSitePath, $siteName, $uri);
if (!$valetDriver) {
show_valet_404();
}
/**
* Overwrite the HTTP host for Ngrok.
*/
if (isset($_SERVER['HTTP_X_ORIGINAL_HOST'])) {
$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_ORIGINAL_HOST'];
}
/**
* Allow driver to mutate incoming URL.
*/
$uri = $valetDriver->mutateUri($uri);
/**
* Determine if the incoming request is for a static file.
*/
$isPhpFile = pathinfo($uri, PATHINFO_EXTENSION) === 'php';
if ($uri !== '/' && !$isPhpFile && $staticFilePath = $valetDriver->isStaticFile($valetSitePath, $siteName, $uri)) {
return $valetDriver->serveStaticFile($staticFilePath, $valetSitePath, $siteName, $uri);
}
/**
* Attempt to dispatch to a front controller.
*/
$frontControllerPath = $valetDriver->frontControllerPath(
$valetSitePath,
$siteName,
$uri
);
if (!$frontControllerPath) {
if (is_dir($valetSitePath . $uri)) {
show_project_directory($valetSitePath, $siteName, $uri);
} else {
show_valet_404();
}
}
chdir(dirname($frontControllerPath));
require $frontControllerPath;