|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Wrapping function By Dimas Lanjaka |
|
5
|
|
|
* @author Dimas Lanjaka <[email protected]> |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* An example CORS-compliant method. It will allow any GET, POST, or OPTIONS requests from any origin. |
|
10
|
|
|
* |
|
11
|
|
|
* In a production environment, you probably want to be more restrictive, but this gives you the general idea of what is involved. For the nitty-gritty low-down, read: |
|
12
|
|
|
* - https://developer.mozilla.org/en/HTTP_access_control |
|
13
|
|
|
* - https://fetch.spec.whatwg.org/#http-cors-protocol |
|
14
|
|
|
* @return void |
|
15
|
|
|
*/ |
|
16
|
|
|
function cors() |
|
17
|
|
|
{ |
|
18
|
|
|
if (!headers_sent()) return; |
|
19
|
|
|
|
|
20
|
|
|
// Allow from any origin |
|
21
|
|
|
if (isset($_SERVER['HTTP_ORIGIN'])) { |
|
22
|
|
|
// Decide if the origin in $_SERVER['HTTP_ORIGIN'] is one |
|
23
|
|
|
// you want to allow, and if so: |
|
24
|
|
|
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}"); |
|
25
|
|
|
header('Access-Control-Allow-Credentials: true'); |
|
26
|
|
|
header('Access-Control-Max-Age: 86400'); // cache for 1 day |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
// Access-Control headers are received during OPTIONS requests |
|
30
|
|
|
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { |
|
31
|
|
|
|
|
32
|
|
|
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) { |
|
33
|
|
|
// may also be using PUT, PATCH, HEAD etc |
|
34
|
|
|
header("Access-Control-Allow-Methods: GET, POST, OPTIONS"); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) { |
|
38
|
|
|
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}"); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
exit(0); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
//echo "You have CORS!"; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Check if string start with needle string |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $haystack |
|
51
|
|
|
* @param string $needle |
|
52
|
|
|
* @return boolean |
|
53
|
|
|
*/ |
|
54
|
|
|
function startsWith($haystack, $needle) |
|
55
|
|
|
{ |
|
56
|
|
|
$length = strlen($needle); |
|
57
|
|
|
return substr($haystack, 0, $length) === $needle; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Check if string ends with needle string |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $haystack |
|
64
|
|
|
* @param string $needle |
|
65
|
|
|
* @return boolean |
|
66
|
|
|
*/ |
|
67
|
|
|
function endsWith($haystack, $needle) |
|
68
|
|
|
{ |
|
69
|
|
|
$length = strlen($needle); |
|
70
|
|
|
if (!$length) { |
|
71
|
|
|
return true; |
|
72
|
|
|
} |
|
73
|
|
|
return substr($haystack, -$length) === $needle; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Convert string or number or float to number/float value |
|
78
|
|
|
* |
|
79
|
|
|
* @param int|float|string $val |
|
80
|
|
|
* @return int|float|null |
|
81
|
|
|
*/ |
|
82
|
|
|
function toNumber($val) |
|
83
|
|
|
{ |
|
84
|
|
|
if (is_string($val)) { |
|
85
|
|
|
if (preg_match('/^\d*\.?\d*$/m', $val, $matches)) { |
|
86
|
|
|
$val = $matches[0]; |
|
87
|
|
|
} else if (preg_match('/^\d*\.?\d*/m', $val, $matches)) { |
|
88
|
|
|
$val = $matches[0]; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
if (is_numeric($val)) { |
|
92
|
|
|
$int = (int)$val; |
|
93
|
|
|
$float = (float)$val; |
|
94
|
|
|
|
|
95
|
|
|
$val = ($int == $float) ? $int : $float; |
|
96
|
|
|
return $val; |
|
97
|
|
|
} else { |
|
98
|
|
|
trigger_error("Cannot cast $val to a number", E_USER_WARNING); |
|
99
|
|
|
return null; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Convert path to url |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $file |
|
107
|
|
|
* @param string $Protocol |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
|
|
function path2url($file, $Protocol = 'http://') |
|
111
|
|
|
{ |
|
112
|
|
|
$file = realpath($file); |
|
113
|
|
|
//var_dump($file); |
|
114
|
|
|
if ($file) { |
|
115
|
|
|
return $Protocol . $_SERVER['HTTP_HOST'] . fixurl(str_replace(fixpath($_SERVER['DOCUMENT_ROOT']), '', fixpath($file))); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
//echo debug_backtrace()[1]['function']; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get URL Origin by Path |
|
122
|
|
|
*/ |
|
123
|
|
|
function getOrigin($path) |
|
124
|
|
|
{ |
|
125
|
|
|
return (isset($_SERVER['HTTPS']) && 'on' === $_SERVER['HTTPS'] ? 'https' : 'http') . "://$_SERVER[HTTP_HOST]$path"; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Fix url |
|
130
|
|
|
* |
|
131
|
|
|
* @param string $url |
|
132
|
|
|
* @return string fixed url |
|
133
|
|
|
*/ |
|
134
|
|
|
function fixurl(string $url) |
|
135
|
|
|
{ |
|
136
|
|
|
$remove_backslash = preg_replace('/[\/\\\\]{2,100}/m', '/', $url); |
|
137
|
|
|
return str_replace("\\", "/", $remove_backslash); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Fix path separator based OS |
|
142
|
|
|
* |
|
143
|
|
|
* @param string $subject |
|
144
|
|
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
|
|
function fixpath(string $subject) |
|
147
|
|
|
{ |
|
148
|
|
|
$replace = (DIRECTORY_SEPARATOR === '\\') |
|
149
|
|
|
? str_replace('/', '\\', $subject) |
|
150
|
|
|
: str_replace('\\', '/', $subject); |
|
151
|
|
|
return preg_replace('/[\/\\\\]{2,100}/m', DIRECTORY_SEPARATOR, $replace); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Write to file |
|
156
|
|
|
* |
|
157
|
|
|
* @param string $file |
|
158
|
|
|
* @param string $content |
|
159
|
|
|
* @param boolean $append |
|
160
|
|
|
* @return void |
|
161
|
|
|
*/ |
|
162
|
|
|
function write($file, $content, bool $append = false) |
|
163
|
|
|
{ |
|
164
|
|
|
if (!file_exists(dirname($file))) { |
|
165
|
|
|
mkdir(dirname($file)); |
|
166
|
|
|
} |
|
167
|
|
|
if (file_exists($file)) delete($file); |
|
168
|
|
|
file_put_contents($file, $content, ($append ? FILE_APPEND : 0)); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Delete file or directory. |
|
173
|
|
|
* Unset array or object. |
|
174
|
|
|
* @param mixed $object |
|
175
|
|
|
* @return void |
|
176
|
|
|
*/ |
|
177
|
|
|
function delete($object) |
|
178
|
|
|
{ |
|
179
|
|
|
if (is_file($object) || is_dir($object)) { |
|
180
|
|
|
unlink($object); |
|
181
|
|
|
} else if (is_array($object) || is_object($object)) { |
|
182
|
|
|
unset($object); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Get All included files |
|
188
|
|
|
* |
|
189
|
|
|
* @return string|false|void |
|
190
|
|
|
*/ |
|
191
|
|
|
function get_includes() |
|
192
|
|
|
{ |
|
193
|
|
|
$included = array_values(array_filter(array_map(function ($arr) { |
|
194
|
|
|
if (is_string($arr)) { |
|
195
|
|
|
if (strpos($arr, 'vendor')) { |
|
196
|
|
|
return ''; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
return $arr; |
|
201
|
|
|
}, get_included_files()))); |
|
202
|
|
|
$included[] = 'total included files: ' . count(get_included_files()); |
|
203
|
|
|
$inc = \JSON\json::json($included, false, false); |
|
204
|
|
|
|
|
205
|
|
|
return $inc; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* check session name exist ($_SESSION) |
|
210
|
|
|
*/ |
|
211
|
|
|
function is_session(string $sessionName) |
|
212
|
|
|
{ |
|
213
|
|
|
return isset($_SESSION[$sessionName]); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Get session value by name ($_SESSION) |
|
218
|
|
|
*/ |
|
219
|
|
|
function get_session(string $sessionName) |
|
220
|
|
|
{ |
|
221
|
|
|
if (is_session($sessionName)) { |
|
222
|
|
|
return $_SESSION[$sessionName]; |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Set session |
|
228
|
|
|
*/ |
|
229
|
|
|
function set_session(string $name, $value) |
|
230
|
|
|
{ |
|
231
|
|
|
$_SESSION[$name] = $value; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Parse URL deeper. |
|
236
|
|
|
*/ |
|
237
|
|
|
function parse_url2($url, $encoded = false) |
|
238
|
|
|
{ |
|
239
|
|
|
if ($encoded) { |
|
240
|
|
|
$url = urldecode($url); |
|
241
|
|
|
} |
|
242
|
|
|
$url = html_entity_decode($url); |
|
243
|
|
|
$parts = parse_url($url); |
|
244
|
|
|
if (isset($parts['query'])) { |
|
245
|
|
|
parse_str($parts['query'], $query); |
|
246
|
|
|
$parts['original_query'] = $parts['query']; |
|
247
|
|
|
$parts['query'] = $query; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
return array_merge($parts); |
|
251
|
|
|
} |
|
252
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.