-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.php
130 lines (102 loc) · 3.29 KB
/
utils.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
<?php
function pre_dump($val) {
echo '<pre>';
var_dump($val);
echo '</pre>';
}
function check($var) {
return isset($var) and !empty($var);
}
function sec($var) {
return htmlspecialchars($var);
}
function http_response($body, $code = 200) {
header('Content-Type: application/json');
http_response_code($code);
echo $body;
exit();
}
function http_json_response($json, $code = 200) {
http_response(json_encode($json), $code);
}
function http_message($message, $key = 'message', $code = 200) {
$arr = array($key => $message);
http_json_response($arr, $code);
}
function http_error($code, $message) {
http_message($message, 'error', $code);
}
function is_primitive($value) {
$value_type = gettype($value);
return $value_type == 'NULL' ||
$value_type == 'boolean' ||
$value_type == 'integer' ||
$value_type == 'double' ||
$value_type == 'string';
}
function is_number_like($value) {
$value_type = gettype($value);
return in_array($value_type, ['integer', 'double']);
}
function is_keyable($value) {
return in_array(gettype($value), ['integer', 'string']);
}
function http_success($message) {
http_message($message, 'message', 200);
}
function check_key_json($key, $arr, $parse = false) {
if (array_key_exists($key, $arr))
return $parse ? sec($arr[$key]) : $arr[$key];
return false;
}
function array_assoc($arr) {
if (array() === $arr || !is_array($arr)) return false;
return array_keys($arr) !== range(0, count($arr) - 1);
}
function array_sequential($arr) {
return !array_assoc($arr);
}
function stringifier($obj, $depth = 1) {
if ($depth == 0) return json_encode($obj);
$res = "{";
$formed = array();
foreach (array_keys($obj) as $key) {
array_push($formed, '"' . strval($key) . '":' . stringifier($obj[$key], $depth - 1));
}
$res .= implode(",", $formed);
$res .= "}";
return $res;
}
function cors() {
// Allow from any origin
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
}
function remove_dots($path) {
$root = ($path[0] === '/') ? '/' : '';
$segments = explode('/', trim($path, '/'));
$ret = array();
foreach ($segments as $segment) {
if ($segment == '.' || strlen($segment) === 0) continue;
if ($segment == '..') array_pop($ret);
else array_push($ret, $segment);
}
return $root . implode('/', $ret);
}
if (!function_exists('str_ends_with')) {
function str_ends_with(string $haystack, string $needle): bool {
$needle_len = strlen($needle);
return $needle_len === 0 || 0 === substr_compare($haystack, $needle, -$needle_len);
}
}