-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathphp7-compat.php
523 lines (476 loc) · 12.7 KB
/
php7-compat.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
<?php
/**
* Backwards compatibility library for obsolete PHP functions
*
* The mysql_* functions and ereg* functions were removed in PHP 7.
* Many legacy websites contain too much legacy code to fully rewrite
* so a compatibility library is required. It should be included in the
* auto_prepend_file setting in php.ini for the site. This is the only
* way of guaranteeing it gets loaded in every file on the site without
* analysing each file individually. Note that not all mysql_*
* functions have been overridden. If more are required to maintain
* compatibility then they can be added here. Contributions welcome via
* GitHub.
*
* @author Kitson Consulting <github at kitson minus consulting dot co dot uk>
* @copyright 2019-2023 Kitson Consulting Limited
* @license https://www.apache.org/licenses/LICENSE-2.0
* @package php7-compat
* @see https://github.com./kitserve/php7-compat
*/
// Global variable to cache database link (this is used because many
// mysqli_* functions require it, but old mysql_* functions don't)
$_php7_compat_global_db_link = null;
// definitions required for old mysql_connect $client_flags
if( !defined( 'MYSQL_CLIENT_COMPRESS' ) ) define( 'MYSQL_CLIENT_COMPRESS', MYSQLI_CLIENT_COMPRESS );
if( !defined( 'MYSQL_CLIENT_IGNORE_SPACE' ) ) define( 'MYSQL_CLIENT_IGNORE_SPACE', MYSQLI_CLIENT_IGNORE_SPACE );
if( !defined( 'MYSQL_CLIENT_INTERACTIVE' ) ) define( 'MYSQL_CLIENT_INTERACTIVE', MYSQLI_CLIENT_INTERACTIVE );
if( !defined( 'MYSQL_CLIENT_SSL' ) ) define( 'MYSQL_CLIENT_SSL', MYSQLI_CLIENT_SSL );
// definitions required for old mysql_fetch_array $result_type
if( !defined( 'MYSQL_ASSOC' ) ) define( 'MYSQL_ASSOC', MYSQLI_ASSOC );
if( !defined( 'MYSQL_BOTH' ) ) define( 'MYSQL_BOTH', MYSQLI_BOTH );
if( !defined( 'MYSQL_NUM' ) ) define( 'MYSQL_NUM', MYSQLI_NUM );
if( !function_exists( 'mysql_affected_rows' ) )
{
function mysql_affected_rows( $link = null )
{
if( !$link )
{
global $_php7_compat_global_db_link;
$link = $_php7_compat_global_db_link;
}
return mysqli_affected_rows( $link );
}
}
if( !function_exists( 'mysql_close' ) )
{
function mysql_close( $link = null )
{
if( !$link )
{
global $_php7_compat_global_db_link;
$link = $_php7_compat_global_db_link;
}
// mysqli_close always returns true, and throws an exception if it wasn't able to close the connection.
// Since we're trying to mimic the behaviour of mysql_close, we need to catch exceptions and return false.
try
{
if( mysqli_close( $link ) )
{
$_php7_compat_global_db_link = null;
return true;
}
else
{
return false;
}
}
catch (\Throwable $exception)
{
return false;
}
}
}
if( !function_exists( 'mysql_connect' ) )
{
function mysql_connect( $server = '', $user = '', $password = '', $new_link = false, $client_flags = 0 )
{
global $_php7_compat_global_db_link;
if( !$server ) $server = ini_get( 'mysqli.default_host' );
if( !$user ) $user = ini_get( 'mysqli.default_user' );
if( !$password ) $password = ini_get( 'mysqli.default_pw' );
$link = mysqli_connect( $server, $user, $password );
if( !$_php7_compat_global_db_link ) $_php7_compat_global_db_link = $link;
return $link;
}
}
if( !function_exists( 'mysql_data_seek' ) )
{
function mysql_data_seek( $result, $offset )
{
return mysqli_data_seek( $result, $offset );
}
}
if( !function_exists( 'mysql_db_query' ) )
{
function mysql_db_query( $dbname, $query, $link )
{
if( !$link )
{
global $_php7_compat_global_db_link;
$link = $_php7_compat_global_db_link;
}
mysqli_select_db( $link, $dbname );
$result = mysqli_query( $link, $query );
return $result;
}
}
if( !function_exists( 'mysql_errno' ) )
{
function mysql_errno( $link = null )
{
if( !$link )
{
global $_php7_compat_global_db_link;
$link = $_php7_compat_global_db_link;
}
return mysqli_errno( $link );
}
}
if( !function_exists( 'mysql_error' ) )
{
function mysql_error( $link = null )
{
if( !$link )
{
global $_php7_compat_global_db_link;
$link = $_php7_compat_global_db_link;
}
return mysqli_error( $link );
}
}
if( !function_exists( 'mysql_escape_string' ) )
{
function mysql_escape_string( $unescaped_string, $link = null )
{
if( !$link )
{
global $_php7_compat_global_db_link;
$link = $_php7_compat_global_db_link;
}
return mysqli_real_escape_string( $link, $unescaped_string );
}
}
if( !function_exists( 'mysql_fetch_array' ) )
{
function mysql_fetch_array( $result, $result_type = MYSQLI_BOTH )
{
return mysqli_fetch_array( $result, $result_type );
}
}
if( !function_exists( 'mysql_fetch_assoc' ) )
{
function mysql_fetch_assoc( $result )
{
return mysqli_fetch_assoc( $result );
}
}
if( !function_exists( 'mysql_fetch_field' ) )
{
function mysql_fetch_field( $result, $field_offset = 0 )
{
for( $i = 0; $i < $field_offset; ++$i )
{
mysqli_fetch_field( $result );
}
return mysqli_fetch_field( $result );
}
}
if( !function_exists( 'mysql_fetch_object' ) )
{
function mysql_fetch_object( $result, $class_name = 'stdClass', $params = null )
{
if( is_array( $params ) ) return mysqli_fetch_object( $result, $class_name, $params );
else return mysqli_fetch_object( $result, $class_name );
}
}
if( !function_exists( 'mysql_fetch_row' ) )
{
function mysql_fetch_row( $result )
{
return mysqli_fetch_row( $result );
}
}
if( !function_exists( 'mysql_field_name' ) )
{
function mysql_field_name( $result, $field_offset )
{
return mysqli_fetch_field_direct( $result, $field_offset )->name;
}
}
if( !function_exists( 'mysql_field_type' ) )
{
function mysql_field_type( $result, $field_offset )
{
$type_id = mysqli_fetch_field_direct( $result, $field_offset)->type;
$types = array();
$constants = get_defined_constants( true );
foreach( $constants['mysqli'] as $c => $n )
{
if( preg_match( '/^MYSQLI_TYPE_(.*)/', $c, $m ) )
{
$types[$n] = $m[1];
}
}
return array_key_exists( $type_id, $types ) ? $types[$type_id] : null;
}
}
if( !function_exists( 'mysql_free_result' ) )
{
function mysql_free_result( $result )
{
if( $result )
{
// Note: mysqli_free_result returns void, whereas mysql_free_result returns bool
try
{
mysqli_free_result( $result );
return true;
}
catch (\Throwable $e)
{
return false;
}
}
else
{
// Note: mysql_free_result generates an E_WARNING in this situation,
// but the closest we can get to that is an E_USER_WARNING
trigger_error( 'Unable to free non-result in mysql_free_result', E_USER_WARNING );
return false;
}
}
}
if( !function_exists( 'mysql_get_server_info' ) )
{
function mysql_get_server_info( $link = null )
{
if( !$link )
{
global $_php7_compat_global_db_link;
$link = $_php7_compat_global_db_link;
}
return mysqli_get_server_info( $link );
}
}
if( !function_exists( 'mysql_insert_id' ) )
{
function mysql_insert_id( $link = null )
{
if( !$link )
{
global $_php7_compat_global_db_link;
$link = $_php7_compat_global_db_link;
}
return mysqli_insert_id( $link );
}
}
if( !function_exists( 'mysql_list_dbs' ) )
{
function mysql_list_dbs( $link = null )
{
if( !$link )
{
global $_php7_compat_global_db_link;
$link = $_php7_compat_global_db_link;
}
return mysqli_query( $link, 'SHOW DATABASES' );
}
}
if( !function_exists( 'mysql_list_fields' ) )
{
function mysql_list_fields( $dbname, $table_name, $link = null )
{
if( !$link )
{
global $_php7_compat_global_db_link;
$link = $_php7_compat_global_db_link;
}
mysqli_select_db( $link, $dbname );
return mysqli_query( $link, "SHOW COLUMNS FROM $table_name" );
}
}
if( !function_exists( 'mysql_num_fields' ) )
{
function mysql_num_fields( $result )
{
return mysqli_num_fields( $result );
}
}
if( !function_exists( 'mysql_num_rows' ) )
{
function mysql_num_rows( $result )
{
return mysqli_num_rows( $result );
}
}
if( !function_exists( 'mysql_numrows' ) )
{
function mysql_numrows( $result )
{
return mysqli_num_rows( $result );
}
}
if( !function_exists( 'mysql_pconnect' ) )
{
function mysql_pconnect( $server = '', $user = '', $password = '', $client_flags = 0 )
{
global $_php7_compat_global_db_link;
if( !$server ) $server = ini_get( 'mysqli.default_host' );
if( !$user ) $user = ini_get( 'mysqli.default_user' );
if( !$password ) $password = ini_get( 'mysqli.default_pw' );
$link = mysqli_connect( $server, $user, $password );
if( !$_php7_compat_global_db_link ) $_php7_compat_global_db_link = $link;
return $link;
}
}
if( !function_exists( 'mysql_ping' ) )
{
function mysql_ping( $link = null )
{
if( !$link )
{
global $_php7_compat_global_db_link;
$link = $_php7_compat_global_db_link;
}
return mysqli_ping( $link );
}
}
if( !function_exists( 'mysql_query' ) )
{
function mysql_query( $query, $link = null )
{
if( !$link )
{
global $_php7_compat_global_db_link;
$link = $_php7_compat_global_db_link;
}
return mysqli_query( $link, $query );
}
}
if( !function_exists( 'mysql_real_escape_string' ) )
{
function mysql_real_escape_string( $unescaped_string, $link = null )
{
if( !$link )
{
global $_php7_compat_global_db_link;
$link = $_php7_compat_global_db_link;
}
return mysqli_real_escape_string( $link, $unescaped_string );
}
}
if( !function_exists( 'mysql_result' ) )
{
function mysql_result( $result, $row, $field = 0 )
{
mysqli_data_seek( $result, $row );
$row = mysqli_fetch_array( $result, MYSQLI_BOTH );
return $row[$field];
}
}
if( !function_exists( 'mysql_select_db' ) )
{
function mysql_select_db( $database, $link = null )
{
if( !$link )
{
global $_php7_compat_global_db_link;
$link = $_php7_compat_global_db_link;
}
return mysqli_select_db( $link, $database );
}
}
if( !function_exists( 'mysql_set_charset' ) )
{
function mysql_set_charset( $database, $link = null )
{
if( !$link )
{
global $_php7_compat_global_db_link;
$link = $_php7_compat_global_db_link;
}
return mysqli_set_charset( $link, $database );
}
}
if( !function_exists( 'ereg_replace' ) )
{
function ereg_replace( $pattern, $replacement, $string )
{
return preg_replace( '/' . mb_ereg_replace( '/', '\\/', $pattern ) . '/', $replacement, $string );
}
}
if( !function_exists( 'eregi_replace' ) )
{
function eregi_replace( $pattern, $replacement, $string )
{
return preg_replace( '/' . mb_ereg_replace( '/', '\\/', $pattern ) . '/i', $replacement, $string );
}
}
if( !function_exists( 'ereg' ) )
{
function ereg( $pattern, $string, $regs = null )
{
if( $regs ) return preg_match( '/' . mb_ereg_replace( '/', '\\/', $pattern ) . '/', $string, $regs );
else return preg_match( '/' . mb_ereg_replace( '/', '\\/', $pattern ) . '/', $string );
}
}
if( !function_exists( 'eregi' ) )
{
function eregi( $pattern, $string, $regs = null )
{
if( $regs ) return preg_match( '/' . mb_ereg_replace( '/', '\\/', $pattern ) . '/i', $string, $regs );
else return preg_match( '/' . mb_ereg_replace( '/', '\\/', $pattern ) . '/i', $string );
}
}
if( !function_exists( 'split' ) )
{
function split( $pattern, $string, $limit = -1 )
{
return preg_split( '/' . mb_ereg_replace( '/', '\\/', $pattern ) . '/', $string, $limit );
}
}
if( !function_exists( 'spliti' ) )
{
function spliti( $pattern, $string, $limit = -1 )
{
return preg_split( '/' . mb_ereg_replace( '/', '\\/', $pattern ) . '/i', $string, $limit );
}
}
if( !function_exists( 'set_magic_quotes_runtime' ) )
{
function set_magic_quotes_runtime( $new_setting )
{
if( $new_setting ) return false;
else return true;
}
}
if( !function_exists( 'random_int' ) )
{
define( 'OPENSSL_RAND_MAX', mt_getrandmax() );
function random_int( $min = 0, $max = OPENSSL_RAND_MAX )
{
$range = $max - $min;
if( $range == 0 ) return $min;
$log = log( $range, 2 );
$bytes = (int) ( $log / 8 ) + 1;
$bits = (int) $log + 1;
$filter = (int) ( 1 << $bits ) - 1;
do
{
$rnd = hexdec( bin2hex( openssl_random_pseudo_bytes( $bytes, $s ) ) );
$rnd = $rnd & $filter; // discard irrelevant bits
} while( $rnd >= $range );
return $min + $rnd;
}
}
/**
* Check if a MySQL resource was successfully returned
*
* mysql_* functions generally return resources. mysqli_* functions generally return objects.
* Therefore if we wan to check whether a mysql_* function successfully returned, we can't
* use is_resource() anymore once the mysql_* wrappers are in place. Instead we should check
* with this function. It detects whether we're still running in old mysql_* mode, or if the
* functions have been replaced with wrappers around their mysqli_* counterparts.
*
* @param $resource the resource to check
* @return boolean true if resource/object, false otherwise
*/
if( !function_exists( 'is_mysql_resource' ) )
{
function is_mysql_resource( $resource )
{
return is_resource( $resource ) or is_object( $resource );
}
}
///:~