-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRoutes.php
233 lines (172 loc) · 5.9 KB
/
Routes.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
<?php
namespace BlueRaster\PowerBIAuthProxy;
use BlueRaster\PowerBIAuthProxy\Exceptions\MissingConfigException;
require_once(__DIR__.'/boot.php');
class Routes{
private $auth_proxy;
public $segments;
public $argument_string;
public $query_string;
public $path;
private $method;
private static $mime_set = false;
public static $routes = [];
public $current_route;
public $patterns= [
'proxy' => '.*?arcgis\\/rest.*?$',
'proxy_sharing' => '.*?sharing\\/rest.*?$',
'proxy_dashboard' => '^\\/apps\\/opsdashboard\\/.*?$',
'proxy_other' => '.*?ESRI.*?',
];
public function _route($patterns = []){
$this->path = rtrim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
$this->query_string = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
$this->patterns = array_merge($this->get_patterns(), $this->patterns, $patterns);
$matched = false;
$method = null;
$current_route = null;
foreach($this->patterns as $possible_method => $pattern){
$matched = !!preg_match('/'.$pattern.'/i', $this->path, $matches);
if($matched) {
if($pattern instanceof Route){
$current_route = $pattern;
$current_route->router = $this;
$this->current_route = $current_route;
$method = $current_route->callback;
}
else if(is_string($possible_method)) {
$this->current_route = new Route($pattern);
$this->current_route->router = $this;
$method = $possible_method;
$current_route = $this->current_route;
}
break;
}
}
if($matched){
$this->auth_proxy = Auth::get_instance();
$this->segments = array_filter(explode('/', @$matches[1]));
$method = $this->method = $method ?? array_shift($this->segments);
$this->argument_string = empty($this->segments) ? null : implode('/', $this->segments);
$response = false;
$this->call_gates();
if(is_callable($method)){
$args = array_filter(array_merge($this->segments, [$this]));
$response = call_user_func_array($method, $args);
}
else if(method_exists($current_route, $method)){
$args = array_filter(array_merge($this->segments, [$this]));
$response = call_user_func_array([$current_route, $method], $args);
}
else if(method_exists($this, $method)){
$response = call_user_func_array([$this, $method], $this->segments);
}
else if(preg_replace('/[^a-z]/', '', $method) === 'proxy'){
$response = call_user_func_array([$this, 'proxy'], $this->segments);
}
if($response !== false){
self::set_mime(count($this->segments) ? $this->segments[0] : $this->path);
if(is_array($response)) echo collect($response);
else echo $response;
exit();
}
}
}
public function call_gates(){
$gates = array_map(function($v){
if(is_callable($v)){
return $v($this);
}
else if(method_exists($this->current_route, $v)){
return $this->current_route->{$v}($this);
}
else if(is_callable(Auth::config($v))){
return Auth::config($v)($this);
}
}, $this->current_route->gates);
$passes = count($gates) === count(array_filter($gates));
if(!$passes){
if(!Auth::config('auth_proxy_gate')){
throw new MissingConfigException("The default gate: 'auth_proxy_gate' is missing. This should be set in the configuration for your framework as a Closure.");
}
return UserProxy::abort();
}
}
private function get_patterns(){
$patterns = static::$routes;
$i = 0;
$continuing = true;
while($continuing){
$test = env("PATTERNS_$i");
if($test){
$patterns[] = $test;
}
else{
$continuing = false;
}
$i++;
}
return $patterns;
}
public static function route(){
try{
$router = new self;
$router->_route();
}
catch(\Exception $e){
if($router->current_route->isNullable()){
self::set_mime($router->path);
die('');
}
else if(method_exists($e, 'handle')){
$e->handle();
}
else{
throw $e;
}
}
}
private static function set_mime($filename = null){
if(self::$mime_set) return;
self::$mime_set = true;
$ok = preg_match('/^.*?\.(js|css|html|pbf)$/', $filename, $match);
if(!$ok) $mime = 'application/json';
else{
$mimes = [
'js' => 'application/javascript',
'css' => 'text/css',
'html' => 'text/html',
'pbf' => 'application/octet-stream'
];
$mime = $mimes[$match[1]];
}
header("Content-Type: $mime");
}
private function proxy($endpoint = null){
if($endpoint)
$esri_endpoint = $endpoint;
else
$esri_endpoint = Auth::config('esri_endpoint', 'https://services7.arcgis.com');
$query = $_GET;
$query['token'] = $this->auth_proxy->getEsriEmbedToken();
ksort($query);
$url = $esri_endpoint . $this->path . '?' . http_build_query($query);
self::set_mime('.' . @$query['f'] ?? 'html');
header('Access-Control-Allow-Origin: *');
return guzzle_get_contents($url);
}
private function proxy_sharing(){
return $this->proxy(Auth::config('esri_dashboard_endpoint', 'https://www.arcgis.com'));
}
private function proxy_dashboard(){
self::set_mime($this->path);
$content = $this->proxy(Auth::config('esri_dashboard_endpoint', 'https://www.arcgis.com'));
return $content;
}
private function proxy_other(){
$esri_endpoint = env('ESRI_ENDPOINT', 'https://services7.arcgis.com');
$url = $esri_endpoint . $this->path;
self::set_mime($this->path);
return guzzle_get_contents("$url");
}
}