-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevsuite.php
More file actions
162 lines (132 loc) · 4.07 KB
/
devsuite.php
File metadata and controls
162 lines (132 loc) · 4.07 KB
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
<?php
/**
* Define the user's "~/.config/devsuite" path.
*/
define('DEVSUITE_HOME_PATH', str_replace('\\', '/', $_SERVER['HOME'] . '/.config/devsuite'));
define('DEVSUITE_STATIC_PREFIX', '41c270e4-5535-4daa-b23e-c269744c2f45');
define('CFDEVSUITE', 1);
/**
* Show the Valet 404 "Not Found" page.
*/
function showDevSuite404() {
http_response_code(404);
require __DIR__ . '/system/data/devsuite/template/404.html';
exit;
}
/**
* You may use wildcard DNS providers xip.io or nip.io as a tool for testing your site via an IP address.
* It's simple to use: First determine the IP address of your local computer (like 192.168.0.10).
* Then simply use http://project.your-ip.xip.io - ie: http://laravel.192.168.0.10.xip.io.
*
* @param mixed $domain
*/
function devSuiteSupportWildcardDns($domain) {
if (in_array(substr($domain, -7), ['.xip.io', '.nip.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;
}
/**
* Load the Valet configuration.
*/
$devSuiteConfig = json_decode(
file_get_contents(DEVSUITE_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 wildcard dns feature
devSuiteSupportWildcardDns($_SERVER['HTTP_HOST']),
'.' . $devSuiteConfig['tld']
);
if (strpos($siteName, 'www.') === 0) {
$siteName = substr($siteName, 4);
}
/**
* Determine the fully qualified path to the site.
*/
$devSuiteSitePath = null;
$domain = array_slice(explode('.', $siteName), -1)[0];
foreach ($devSuiteConfig['paths'] as $path) {
if (is_dir($path . '/' . $siteName)) {
$devSuiteSitePath = $path . '/' . $siteName;
break;
}
if (is_dir($path . '/' . $domain)) {
$devSuiteSitePath = $path . '/' . $domain;
break;
}
}
if (is_null($devSuiteSitePath)) {
showDevSuite404();
}
$devSuiteSitePath = realpath($devSuiteSitePath);
/**
* Find the appropriate Valet driver for the request.
*/
$devSuiteDriver = null;
$driverFiles = [
__DIR__ . '/system/libraries/CDevSuite/Trait/ConsoleTrait.php',
__DIR__ . '/system/libraries/CDevSuite/Trait/WindowsTrait.php',
__DIR__ . '/system/libraries/CDevSuite/Trait/MacTrait.php',
__DIR__ . '/system/libraries/CDevSuite/Trait/LinuxTrait.php',
__DIR__ . '/system/libraries/CDevSuite.php',
__DIR__ . '/system/libraries/CDevSuite/DevSuiteDriver.php',
__DIR__ . '/system/libraries/CDevSuite/Driver/BasicDevSuiteDriver.php',
];
foreach ($driverFiles as $file) {
require $file;
}
$devSuiteDriver = CDevSuite_DevSuiteDriver::assign($devSuiteSitePath, $siteName, $uri);
if (!$devSuiteDriver) {
showDevSuite404();
}
/*
* 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 = $devSuiteDriver->mutateUri($uri);
/**
* Determine if the incoming request is for a static file.
*/
$isPhpFile = pathinfo($uri, PATHINFO_EXTENSION) === 'php';
if ($uri !== '/' && !$isPhpFile && $staticFilePath = $devSuiteDriver->isStaticFile($devSuiteSitePath, $siteName, $uri)) {
return $devSuiteDriver->serveStaticFile($staticFilePath, $devSuiteSitePath, $siteName, $uri);
}
/**
* Attempt to load server environment variables.
*/
$devSuiteDriver->loadServerEnvironmentVariables(
$devSuiteSitePath,
$siteName
);
/**
* Attempt to dispatch to a front controller.
*/
$frontControllerPath = $devSuiteDriver->frontControllerPath(
$devSuiteSitePath,
$siteName,
$uri
);
if (!$frontControllerPath) {
showDevSuite404();
}
chdir(dirname($frontControllerPath));
require $frontControllerPath;