-
Notifications
You must be signed in to change notification settings - Fork 0
/
__.php
370 lines (296 loc) · 6.67 KB
/
__.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php
/**
* __php main
*/
/*
* Wrapper for htmlentities
*/
function __h($x) : string
{
$x = htmlentities($x, ENT_QUOTES, 'UTF-8', true);
return (string)$x;
}
/**
* Wrapper for base64 encode, url-safe
*/
function __base64_encode_url($x) : string
{
return rtrim(strtr(base64_encode($x), '+/', '-_'), '=');
}
function __base64_decode_url($x) : string
{
return base64_decode(str_pad(strtr($x, '-_', '+/'), (strlen($x) % 4), '=', STR_PAD_RIGHT));
}
/**
* Wrapper to simplify short-lived crypted data
*/
function __encrypt($d, $k=null)
{
$d = openssl_encrypt($d, 'AES-256-ECB', $k, true);
return __base64_encode_url($d);
}
function __decrypt($d, $k=null)
{
$d = __base64_decode_url($d);
return trim(openssl_decrypt($d, 'AES-256-ECB', $k, true));
}
/*
* Wrapper for cURL to set some defaults
*/
function __curl_init($url, $opt=null)
{
$req = curl_init($url);
curl_setopt($req, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
// Booleans
curl_setopt($req, CURLOPT_AUTOREFERER, true);
curl_setopt($req, CURLOPT_BINARYTRANSFER, true);
curl_setopt($req, CURLOPT_COOKIESESSION, false);
curl_setopt($req, CURLOPT_CRLF, false);
curl_setopt($req, CURLOPT_FAILONERROR, false);
curl_setopt($req, CURLOPT_FILETIME, true);
curl_setopt($req, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($req, CURLOPT_FORBID_REUSE, false);
curl_setopt($req, CURLOPT_FRESH_CONNECT, false);
curl_setopt($req, CURLOPT_HEADER, false);
curl_setopt($req, CURLOPT_NETRC, false);
curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
curl_setopt($req, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($req, CURLINFO_HEADER_OUT,true);
// curl_setopt($req, CURLOPT_BUFFERSIZE, 16384);
curl_setopt($req, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($req, CURLOPT_MAXREDIRS, 0);
// curl_setopt($req, CURLOPT_SSL_VERIFYHOST, 0);
// curl_setopt($req, CURLOPT_SSLVERSION, 3); // 2, 3 or GnuTLS
curl_setopt($req, CURLOPT_TIMEOUT, 60);
if (defined('__PHP_USERAGENT')) {
curl_setopt($req, CURLOPT_USERAGENT, __PHP_USERAGENT);
}
if (!empty($opt) && is_array($opt)) {
curl_setopt($req, $opt);
}
return $req;
}
/**
Wrapper for Date Formating
@param $f Date Format in either date() or strftime format (don't mix!)
@param $d Date/Time
@param $tz Time Zone
*/
function __date($f, $d=null, $tz=null)
{
$r = $d;
if (empty($d) && empty($tz)) {
return '-';
}
if (!empty($r)) {
// Match UNIX Timestamp (may be negative)
if (preg_match('/^\-?\d+$/', $r)) {
$r = '@' . $r;
}
}
if (!empty($tz)) {
if (is_string($tz)) {
$tz = new DateTimeZone($tz);
}
}
try {
$dt = new DateTime($r); //, $tz);
if (!empty($tz)) {
$dt->setTimezone($tz);
}
} catch (\Exception $e) {
return $r;
}
// A strftime Type Format
if (strpos($f, '%') === false) {
$r = $dt->format($f);
} else {
$tz0 = date_default_timezone_get();
if ($tz) {
date_default_timezone_set($tz);
}
$r = strftime($f, $dt->getTimestamp());
date_default_timezone_set($tz0);
}
return $r;
}
/**
* WapperJSON decode to array type
*/
function __json_decode(?string $x)
{
return json_decode($x, true, 512, JSON_INVALID_UTF8_IGNORE | JSON_OBJECT_AS_ARRAY);
}
/*
* Wapper for json_encode
*/
function __json_encode($x, int $f=0)
{
return json_encode($x, JSON_INVALID_UTF8_IGNORE | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | $f);
}
/**
* Wrapper for parse_str to not leak vars
*/
function __parse_str(string $x)
{
$r = array();
parse_str($x, $r);
return $r;
}
/**
* Sort a Keyed Array, Recursively
* @return bool
*/
function __ksort_r(&$array)
{
foreach ($array as &$value) {
if (is_array($value)) {
// If isArray and all Values are String then sort by Value
//$idx = array_keys($value) === range(0, count($value) - 1);
//if ($idx) {
__ksort_r($value);
}
}
return ksort($array);
}
/**
Array Diff by Key and Value - Recursive
@param $a0 Old Data Array
@param $a1 New Data Array
@return Array of Keys with Old and New values
*/
function __array_diff_keyval_r($a0, $a1)
{
$ret = array();
if (empty($a0)) {
$a0 = array();
}
if (empty($a1)) {
$a1 = array();
}
$key_a = array_keys($a0);
$key_b = array_keys($a1);
$key_list = array_merge($key_a, $key_b);
foreach ($key_list as $key) {
$v0 = $a0[$key];
$v1 = $a1[$key];
if ($v0 != $v1) {
if (is_array($v0) && is_array($v1)) {
$x = __array_diff_keyval_r($v0, $v1);
$ret[$key] = $x;
} else {
$ret[$key] = array(
'old' => $v0,
'new' => $v1,
);
}
}
}
return $ret;
}
/**
Makes a temp file that automatically cleans up
@param
*/
function __tempnam($pre=null)
{
$pre = trim($pre);
if (empty($pre)) {
$pre = 'app-tmp';
}
$tfn = tempnam(sys_get_temp_dir(), $pre);
$tfc = function() use ($tfn) {
unlink($tfn);
};
register_shutdown_function($tfc);
return $tfn;
}
/**
* Makes a slug from Text
*/
function __text_stub($x)
{
$x = strtolower($x);
$x = preg_replace('/[^\w\-]+/', '-', $x);
$x = preg_replace('/\-+/', '-', $x);
$x = trim($x, '-');
return $x;
}
/**
* Exit with HTML Content
*/
function __exit_html($html, $code=200)
{
while (ob_get_level()) ob_end_clean();
__exit_code($code);
header('content-type: text/html');
echo $html;
exit(0);
}
/**
* Exit with JSON Content
*/
function __exit_json($data, $code=200)
{
while (ob_get_level()) ob_end_clean();
__exit_code($code);
if (!is_string($data)) {
$data = __json_encode($data);
}
header('cache-control: no-cache');
header('content-type: application/json', true);
echo $data;
exit(0);
}
/**
* Exit with Text Content
*/
function __exit_text($text, $code=200)
{
while (ob_get_level()) ob_end_clean();
__exit_code($code);
if (!is_string($text)) {
$text = json_encode($text, JSON_INVALID_UTF8_IGNORE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
}
header('cache-control: no-cache');
header('content-type: text/plain');
echo $text;
exit(0);
}
/**
* Sets the HTTP Header Code
*/
function __exit_code($code)
{
$map_code = array(
200 => 'OK',
201 => 'Created',
204 => 'No Content',
304 => 'Not Modified',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
409 => 'Conflict',
410 => 'Gone',
500 => 'Server Error',
503 => 'Unavailable',
504 => 'Gateway Timeout',
);
$text = $map_code[$code];
$head = trim(sprintf('HTTP/1.1 %d %s', $code, $text));
header($head, true, $code);
// header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK', true, 200);
// header($_SERVER['SERVER_PROTOCOL'] . ' 403 Not Authorized', true, 403);
}
/**
* Determine which MIME type the request wants
*/
function __mime_type_want()
{
$want_list = explode(',', $_SERVER['HTTP_ACCEPT']);
return trim($want_list[0]);
}