-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.php
157 lines (125 loc) · 4.16 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
<?php
/**
* Define the user's "~/.valet" path.
*/
define('VALET_HOME_PATH', posix_getpwuid(posix_geteuid())['dir'] . '/.valet');
define('VALET_STATIC_PREFIX', '41c270e4-5535-4daa-b23e-c269744c2f45');
/**
* 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 = urldecode(
explode("?", $_SERVER['REQUEST_URI'])[0]
);
$siteName = basename(
// Filter host to support xip.io feature
$_SERVER['HTTP_HOST'],
'.' . $valetConfig['domain']
);
if (strpos($siteName, 'www.') === 0) {
$siteName = substr($siteName, 4);
}
/**
* Determine a possible rewrite.
*/
if (isset($valetConfig['rewrites'])) {
foreach ($valetConfig['rewrites'] as $site => $rewrites) {
foreach ($rewrites as $rewrite) {
if ($rewrite == $siteName) {
$siteName = $site;
break;
}
}
}
}
/**
* Determine the fully qualified path to the site.
*/
$valetSitePath = apcu_fetch('valet_site_path' . $siteName);
$domain = array_slice(explode('.', $siteName), -1)[0];
if (!$valetSitePath) {
$siteCount = 0;
$valetPaths = count($valetConfig['paths']);
foreach ($valetConfig['paths'] as $path) {
if (is_dir($path . '/' . $siteName)) {
$valetSitePath = $path . '/' . $siteName;
break;
}
if (is_dir($path . '/' . $domain)) {
$valetSitePath = $path . '/' . $domain;
break;
}
$siteCount += count(glob(htmlspecialchars($path) . '/*', GLOB_ONLYDIR));
}
if (!$valetSitePath) {
http_response_code(404);
// 404 variables
$valetCustomConfig = $valetConfig;
unset($valetCustomConfig['domain']);
unset($valetCustomConfig['paths']);
$logo = __DIR__ . '/cli/templates/assets/images/logo.svg';
$ssl = __DIR__ . '/cli/templates/assets/images/ssl.svg';
$certificatePath = htmlspecialchars(VALET_HOME_PATH . '/Certificates/');
$certKey = '.crt';
$globalCertificates = glob($certificatePath . '*' . $certKey);
$certificates = [];
foreach ($globalCertificates as $certificate) {
$key = str_replace($certificatePath, '', $certificate);
$key = str_replace($certKey, '', $key);
$certificates[$key] = $certificate;
}
$requestedSite = htmlspecialchars($siteName . '.' . $valetConfig['domain']);
$requestedSiteName = htmlspecialchars($siteName);
require __DIR__ . '/cli/templates/404.php';
exit;
}
$valetSitePath = realpath($valetSitePath);
apcu_add('valet_site_path' . $siteName, $valetSitePath, 3600);
}
/**
* Find the appropriate Valet driver for the request.
*/
$valetDriver = null;
require __DIR__ . '/cli/drivers/require.php';
$valetDriver = ValetDriver::assign($valetSitePath, $siteName, $uri);
if (!$valetDriver) {
http_response_code(404);
echo 'Could not find suitable driver for your project.';
exit;
}
/**
* ngrok uses the X-Original-Host to store the forwarded hostname.
*/
if (isset($_SERVER['HTTP_X_ORIGINAL_HOST']) && !isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
$_SERVER['HTTP_X_FORWARDED_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) {
http_response_code(404);
echo 'Did not get front controller from driver. Please return a front controller to be executed.';
exit;
}
chdir(dirname($frontControllerPath));
unset($domain, $path, $siteName, $uri, $valetConfig, $valetDriver, $valetSitePath);
require $frontControllerPath;