1
|
|
|
<?php |
2
|
|
|
namespace humanity; |
3
|
|
|
|
4
|
|
|
class Site { |
5
|
|
|
|
6
|
|
|
private static $js; |
7
|
|
|
|
8
|
|
|
public function __construct(){ |
9
|
|
|
|
10
|
|
|
$config = (new Config)->get(); |
11
|
|
|
|
12
|
|
|
if(!isset($_SERVER['HTTP_ACCEPT'])) $_SERVER['HTTP_ACCEPT'] = '*/*'; |
13
|
|
|
$acceptSrc = explode(',',$_SERVER['HTTP_ACCEPT']); |
14
|
|
|
foreach($acceptSrc as $key=>$value){ |
15
|
|
|
$value = explode('/',$value); |
16
|
|
|
$value[1] = explode(';',$value[1]); |
17
|
|
|
$name = array_shift($value[1]); |
18
|
|
|
$data = []; |
19
|
|
|
foreach($value[1] as $k=>$v){ |
20
|
|
|
$v = explode('=',$v); |
21
|
|
|
$data[$v[0]] = $v[1]; |
22
|
|
|
} |
23
|
|
|
$accept[$value[0]][$name] = $data; |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
unset($acceptSrc,$key,$value,$name,$data,$k,$v); |
27
|
|
|
|
28
|
|
|
$requestUri = parse_url($_SERVER['REQUEST_URI']); |
29
|
|
|
if($requestUri['path'] == '/javascript.js'){ |
30
|
|
|
header('Content-Type: application/javascript'); |
31
|
|
|
|
32
|
|
|
$tsstring = 'Sun, 03 Jan 2016 11:22:20 GMT'; |
33
|
|
|
$etag = md5($tsstring); |
34
|
|
|
$if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false; |
35
|
|
|
$if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? $_SERVER['HTTP_IF_NONE_MATCH'] : false; |
36
|
|
|
if ((($if_none_match && $if_none_match == $etag) || (!$if_none_match)) && |
37
|
|
|
($if_modified_since && $if_modified_since == $tsstring)) |
38
|
|
|
{ |
39
|
|
|
header('HTTP/1.1 304 Not Modified'); |
40
|
|
|
exit(); |
41
|
|
|
} |
42
|
|
|
else |
43
|
|
|
{ |
44
|
|
|
header("Last-Modified: $tsstring"); |
45
|
|
|
header("ETag: $etag"); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if(isset($requestUri['query'])){ |
49
|
|
|
$query = explode(':',$requestUri['query']); |
50
|
|
|
(new Content)->js($query); |
51
|
|
|
} |
52
|
|
|
exit; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
header('Access-Control-Allow-Origin: *'); |
56
|
|
|
header('Access-Control-Max-Age: 31556926'); |
57
|
|
|
header('Access-Control-Allow-Credentials: true'); |
58
|
|
|
header('Access-Control-Allow-Methods: POST, GET, DELETE, PUT'); |
59
|
|
|
|
60
|
|
|
if(isset($accept['application']['view'])){ |
61
|
|
|
header('Content-Type: text/html'); |
62
|
|
|
(new RestApi)->view(); |
63
|
|
|
} else if(isset($accept['application']['widget'])){ |
64
|
|
|
header('Content-Type: text/html'); |
65
|
|
|
(new RestApi)->widget(); |
66
|
|
|
} else if(isset($accept['application']['apps'])){ |
67
|
|
|
header('Content-Type: application/json'); |
68
|
|
|
$query = json_decode(file_get_contents('php://input'),1); |
69
|
|
|
$result = []; |
70
|
|
|
foreach($query as $method=>$params){ |
71
|
|
|
$stableParams = []; |
72
|
|
|
$file = $config['core']['apps'].'/'.$method.'.php'; |
73
|
|
|
$func = require_once($file); |
74
|
|
|
$reflection = new \ReflectionFunction($func); |
75
|
|
|
foreach($reflection->getParameters() as $key=>$value){ |
76
|
|
|
if(isset($params[$value->name])) $stableParams[$value->name] = $params[$value->name]; |
77
|
|
|
else $stableParams[$value->name] = null; |
78
|
|
|
} |
79
|
|
|
$name = explode('/',$method); |
80
|
|
|
$count = count($name)-1; |
81
|
|
|
$app = new Application; |
82
|
|
|
foreach($name as $key=>$var){ |
83
|
|
|
if($count == $key) $app = call_user_method_array($var,$app,$stableParams); |
84
|
|
|
else $app = $app->{$var}; |
85
|
|
|
} |
86
|
|
|
$result[$method] = $app; |
87
|
|
|
} |
88
|
|
|
die(json_encode($result)); |
89
|
|
|
} else { |
90
|
|
|
(new Content)->page(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
?> |
96
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.