/
shim.php
422 lines (357 loc) · 11.1 KB
/
shim.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
<?php
/**
* shim.php
*
* PHP version 5
*
* @category Retro
* @package Retro
* @author Michael Meyer (mmeyer2k) <m.meyer2k@gmail.com>
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
* @link https://github.com/mmeyer2k/retro
*/
if (!function_exists('mysql_escape')) {
/**
* Mimics mysql_real_escape_string but without the need for
* a database connection.
*
* @param string $str
* @return string
*/
function mysql_escape($str)
{
if (is_array($str)) {
return array_map(__METHOD__, $str);
}
if (!empty($str) && is_string($str)) {
return str_replace(array('\\', "\0", "\n", "\r", "'", '"', "\x1a"), array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $str);
}
return $str;
}
}
/*
* Function to convert array to csv string
*/
if (!function_exists('str_putcsv')) {
/**
*
* @param string $input
* @param string $delimiter
* @param string $enclosure
* @param string $escape_char
* @return string
*/
function str_putcsv($input, $delimiter = ',', $enclosure = '"', $escape_char = '\\')
{
// Open a memory "file" for read/write...
$fp = fopen('php://temp', 'r+');
// ... write the $input array to the "file" using fputcsv()...
foreach ($input as $i) {
fputcsv($fp, $i, $delimiter, $enclosure, $escape_char);
}
// ... rewind the "file" so we can read what we just wrote...
rewind($fp);
// ... read the temp stream into a variable...
$data = stream_get_contents($fp);
// ... close the "file"...
fclose($fp);
// ... and return the $data to the caller, with the trailing newline from fgets() removed.
return rtrim($data, "\n");
}
}
/**
* Shim to allow for hex2bin in less than PHP 5.4.
*
* @link https://stackoverflow.com/questions/17963289/hex2bin-is-not-working-when-i-am-trying-to-use-it-to-convert-encoded-data-to-bin
*/
if (!function_exists('hex2bin')) {
/**
* Convery hex to binary.
*
* @param string $hexstr Hexidecimal string to convert to binary.
*
* @return string
*/
function hex2bin($hexstr)
{
$n = strlen($hexstr);
$sbin = '';
$i = 0;
while ($i < $n) {
$a = substr($hexstr, $i, 2);
$c = pack('H*', $a);
if ($i == 0) {
$sbin = $c;
} else {
$sbin.= $c;
}
$i+=2;
}
return $sbin;
}
}
/**
* This file is part of the array_column library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) 2013 Ben Ramsey <http://benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/
if (!function_exists('array_column')) {
/**
* Returns the values from a single column of the input array, identified by
* the $columnKey.
*
* Optionally, you may provide an $indexKey to index the values in the returned
* array by the values from the $indexKey column in the input array.
*
* @param array $input A multi-dimensional array (record set) from which
* to pull a column of values.
* @param mixed $columnKey The column of values to return. This value may
* be the integer key of the column you wish to
* retrieve, or it may be the string key name for an
* associative array.
* @param mixed $indexKey (Optional.) The column to use as the index/keys for
* the returned array. This value may be the integer key
* of the column, or it may be the string key name.
*
* @return array
*/
function array_column($input = null, $columnKey = null, $indexKey = null)
{
// Using func_get_args() in order to check for proper number of
// parameters and trigger errors exactly as the built-in array_column()
// does in PHP 5.5.
$argc = func_num_args();
$params = func_get_args();
if ($argc < 2) {
trigger_error("array_column() expects at least 2 parameters, {$argc} given", E_USER_WARNING);
return null;
}
if (!is_array($params[0])) {
trigger_error('array_column() expects parameter 1 to be array, ' . gettype($params[0]) . ' given', E_USER_WARNING);
return null;
}
if (!is_int($params[1]) && !is_float($params[1]) && !is_string($params[1]) && $params[1] !== null && !(is_object($params[1]) && method_exists($params[1], '__toString'))
) {
trigger_error('array_column(): The column key should be either a string or an integer', E_USER_WARNING);
return false;
}
if (isset($params[2]) && !is_int($params[2]) && !is_float($params[2]) && !is_string($params[2]) && !(is_object($params[2]) && method_exists($params[2], '__toString'))
) {
trigger_error('array_column(): The index key should be either a string or an integer', E_USER_WARNING);
return false;
}
$paramsInput = $params[0];
$paramsColumnKey = ($params[1] !== null) ? (string) $params[1] : null;
$paramsIndexKey = null;
if (isset($params[2])) {
if (is_float($params[2]) || is_int($params[2])) {
$paramsIndexKey = (int) $params[2];
} else {
$paramsIndexKey = (string) $params[2];
}
}
$resultArray = array();
foreach ($paramsInput as $row) {
$key = $value = null;
$keySet = $valueSet = false;
if ($paramsIndexKey !== null && array_key_exists($paramsIndexKey, $row)) {
$keySet = true;
$key = (string) $row[$paramsIndexKey];
}
if ($paramsColumnKey === null) {
$valueSet = true;
$value = $row;
} elseif (is_array($row) && array_key_exists($paramsColumnKey, $row)) {
$valueSet = true;
$value = $row[$paramsColumnKey];
}
if ($valueSet) {
if ($keySet) {
$resultArray[$key] = $value;
} else {
$resultArray[] = $value;
}
}
}
return $resultArray;
}
}
if (!function_exists('xml2object')) {
/**
*
* @param type $xml
* @return type
*/
function xml2object($xml)
{
return simplexml_load_string($xml);
}
}
if (!function_exists('xml2array')) {
/**
*
* @param type $xml
* @return type
*/
function xml2array($xml)
{
return obj2arr(xml2object($xml));
}
}
if (!function_exists('arr2obj')) {
function arr2obj($arr)
{
return json_decode(json_encode($arr), false);
}
}
if (!function_exists('base32_encode')) {
/**
* Encode a string in base 32
*
* @param string $str
* @return string
*/
function base32_encode($str)
{
$_b32table = '1bcd2fgh3jklmn4pqrstavwxyz567890';
$n = strlen($str) * 8 / 5;
$arr = str_split($str, 1);
$m = '';
foreach ($arr as $c) {
$m .= str_pad(decbin(ord($c)), 8, '0', STR_PAD_LEFT);
}
$p = ceil(strlen($m) / 5) * 5;
$m = str_pad($m, $p, '0', STR_PAD_RIGHT);
$newstr = '';
for ($i = 0; $i < $n; $i++) {
$newstr .= $_b32table[bindec(substr($m, $i * 5, 5))];
}
return $newstr;
}
}
if (!function_exists('base32_decode')) {
/**
* Decode a base32 encoded string
*
* @param string $str
* @return string
*/
function base32_decode($str)
{
$str = strtolower($str);
$_b32table = '1bcd2fgh3jklmn4pqrstavwxyz567890';
$n = strlen($str) * 5 / 8;
$arr = str_split($str, 1);
$m = '';
$split = str_split($_b32table);
foreach ($arr as $c) {
$m .= str_pad(decbin(array_search($c, $split)), 5, '0', STR_PAD_LEFT);
}
$oldstr = '';
$floor = floor($n);
for ($i = 0; $i < $floor; $i++) {
$oldstr .= chr(bindec(substr($m, $i * 8, 8)));
}
return $oldstr;
}
}
if (!function_exists('obj2arr')) {
/**
* Convert object to array.
*
* @param object|array $obj Object to convert to array.
*
* @return array
*/
function obj2arr($obj)
{
if (is_array($obj)) {
foreach ($obj as $i => $o) {
$obj[$i] = obj2arr($o);
}
return $obj;
} elseif (is_object($obj)) {
return json_decode(json_encode($obj), true);
}
return $obj;
}
}
if (!function_exists('mysqltime')) {
/**
* Create a mysql DATETIME string.
*
* @param int|null $timestamp PHP timestamp to convert to MySQL DATETIME format
*
* @return array
*/
function mysqltime($timestamp = null)
{
if ($timestamp === null) {
$timestamp = time();
}
return date('Y-m-d H:i:s', $timestamp);
}
}
if (!function_exists('mysqldate')) {
/**
* Create a mysql DATE string.
*
* @param int|null $timestamp PHP timestamp to convert to MySQL DATE format
*
* @return array
*/
function mysqldate($timestamp = null)
{
if ($timestamp === null) {
$timestamp = time();
}
return date('Y-m-d', $timestamp);
}
}
if (!function_exists('base62_encode')) {
/**
* Encode string via base 62 encoding
*
* @param string $data
* @return string
*/
function base62_encode($data)
{
$outstring = '';
$l = strlen($data);
for ($i = 0; $i < $l; $i += 8) {
$chunk = substr($data, $i, 8);
$outlen = ceil((strlen($chunk) * 8) / 6); //8bit/char in, 6bits/char out, round up
$x = bin2hex($chunk); //gmp won't convert from binary, so go via hex
$w = gmp_strval(gmp_init(ltrim($x, '0'), 16), 62); //gmp doesn't like leading 0s
$pad = str_pad($w, $outlen, '0', STR_PAD_LEFT);
$outstring .= $pad;
}
return $outstring;
}
}
if (!function_exists('base62_decode')) {
/**
* Decode base 62 encoded string
*
* @param string $data
* @return string
*/
function base62_decode($data)
{
$outstring = '';
$l = strlen($data);
for ($i = 0; $i < $l; $i += 11) {
$chunk = substr($data, $i, 11);
$outlen = floor((strlen($chunk) * 6) / 8); //6bit/char in, 8bits/char out, round down
$y = gmp_strval(gmp_init(ltrim($chunk, '0'), 62), 16); //gmp doesn't like leading 0s
$pad = str_pad($y, $outlen * 2, '0', STR_PAD_LEFT); //double output length as as we're going via hex (4bits/char)
$outstring .= pack('H*', $pad); //same as hex2bin
}
return $outstring;
}
}