-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathphp-legacy-compat-strict.php
More file actions
1996 lines (1739 loc) · 58 KB
/
php-legacy-compat-strict.php
File metadata and controls
1996 lines (1739 loc) · 58 KB
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Strict legacy compatibility layer for PHP 8.0 through PHP 8.3.
*
* Author: Ivijan-Stefan Stipic
* Version: 1.0.0
* Repository: https://github.com/InfinitumForm/php-legacy-compat
*
* This file is designed for early bootstrap loading in legacy WordPress themes,
* plugins, mu-plugins, and custom PHP applications that still need removed
* core behavior, but want a more conservative compatibility surface.
*
* Scope:
* - Recreate removed functions where userland can do so honestly.
* - Provide clearly prefixed migration helpers for stricter PHP 8 behavior.
* - Offer opt-in diagnostics and soft error handling without side effects.
*
* Non-goals:
* - Do not fake removed engine state such as magic quotes.
* - Do not recreate removed extensions such as ext/mysql or ext/mcrypt.
* - Do not suppress warnings globally unless the project explicitly opts in.
*/
if (defined('LEGACY_COMPAT_BOOTSTRAPPED')) {
return;
}
define('LEGACY_COMPAT_BOOTSTRAPPED', true);
define('LEGACY_COMPAT_MODE', 'strict');
$GLOBALS['legacy_compat_runtime'] = [
'mode' => LEGACY_COMPAT_MODE,
'source' => __FILE__,
'polyfills' => [],
'helpers' => [],
'limitations' => [],
];
/**
* Register a loaded polyfill or helper in the diagnostics registry.
*
* Historical note:
* - This is an internal utility for the compatibility layer itself.
* - It does not map to a removed PHP core function.
*
* Compatibility type: internal utility.
*
* Return behavior:
* - Returns void.
*
* Limitations:
* - The registry exists only for the lifetime of the current request.
*/
function legacy_compat_internal_register($bucket, $name)
{
if (!isset($GLOBALS['legacy_compat_runtime'][$bucket])) {
$GLOBALS['legacy_compat_runtime'][$bucket] = [];
}
if (!in_array($name, $GLOBALS['legacy_compat_runtime'][$bucket], true)) {
$GLOBALS['legacy_compat_runtime'][$bucket][] = $name;
}
}
/**
* Record a notable limitation or non-goal in the diagnostics registry.
*
* Historical note:
* - This is an internal utility for the compatibility layer itself.
* - It does not map to a removed PHP core function.
*
* Compatibility type: internal utility.
*
* Return behavior:
* - Returns void.
*
* Limitations:
* - Messages are informational only and are not emitted automatically.
*/
function legacy_compat_internal_note_limitation($message)
{
if (!in_array($message, $GLOBALS['legacy_compat_runtime']['limitations'], true)) {
$GLOBALS['legacy_compat_runtime']['limitations'][] = $message;
}
}
/**
* Convert a legacy-compatible scalar or stringable value to string.
*
* Historical note:
* - This helper centralizes the loose scalar-to-string coercion that many
* internal PHP functions used before PHP 8 hardened type handling.
* - It does not map to a removed PHP core function.
*
* Compatibility type: internal utility.
*
* Return behavior:
* - Returns a string for strings, scalars, null when allowed, and objects that
* implement __toString().
* - Returns null when the value cannot be represented honestly as a string.
*
* Limitations:
* - Arrays and non-stringable objects are rejected instead of being converted.
*/
function legacy_compat_internal_stringify($value, $nullAsEmpty = true)
{
if ($value === null) {
return $nullAsEmpty ? '' : null;
}
if (is_string($value)) {
return $value;
}
if (is_scalar($value)) {
return (string) $value;
}
if (is_object($value) && method_exists($value, '__toString')) {
return (string) $value;
}
return null;
}
/**
* Convert an array or Traversable value to a concrete array.
*
* Historical note:
* - This helper centralizes iterable normalization for the migration helpers.
* - It does not map to a removed PHP core function.
*
* Compatibility type: internal utility.
*
* Return behavior:
* - Returns an array when the input is already an array or Traversable.
* - Returns null when the input is not iterable.
*
* Limitations:
* - Traversable values are materialized into memory.
*/
function legacy_compat_internal_iterable_to_array($value, $preserveKeys = true)
{
if (is_array($value)) {
return $value;
}
if ($value instanceof Traversable) {
return iterator_to_array($value, $preserveKeys);
}
return null;
}
/**
* Escape the delimiter used by the internal POSIX-to-PCRE wrapper.
*
* Historical note:
* - Removed ereg-style functions accepted undelimited POSIX patterns.
* - This helper only prepares those patterns for PCRE wrapping.
*
* Compatibility type: internal utility.
*
* Return behavior:
* - Returns the escaped pattern string.
*
* Limitations:
* - The escaping is intentionally simple because it is only used to wrap a
* raw legacy pattern, not to rewrite POSIX syntax into a different grammar.
*/
function legacy_compat_internal_escape_regex_delimiter($pattern, $delimiter)
{
return str_replace($delimiter, '\\' . $delimiter, $pattern);
}
/**
* Wrap a legacy POSIX regular expression in a PCRE delimiter.
*
* Historical note:
* - ereg(), eregi(), split(), and their related APIs used POSIX regex syntax.
* - PHP removed that engine in PHP 7.0, so userland can only approximate it
* through PCRE.
*
* Compatibility type: internal utility.
*
* Return behavior:
* - Returns a PCRE pattern string when the input is non-empty.
* - Returns false when the pattern cannot be prepared honestly.
*
* Limitations:
* - Obscure POSIX-only edge cases can behave differently under PCRE.
*/
function legacy_compat_internal_posix_pattern($pattern, $caseInsensitive = false)
{
if (!is_string($pattern) || $pattern === '') {
return false;
}
$delimiter = '~';
$modifiers = $caseInsensitive ? 'i' : '';
return $delimiter
. legacy_compat_internal_escape_regex_delimiter($pattern, $delimiter)
. $delimiter
. $modifiers;
}
/**
* Compute a strftime-style week number for Sunday- or Monday-based calendars.
*
* Historical note:
* - strftime() and gmstrftime() expose week number tokens that date() does not
* reproduce directly in the same form.
* - This helper exists solely to support the prefixed replacement helpers.
*
* Compatibility type: internal utility.
*
* Return behavior:
* - Returns a two-digit week number string.
*
* Limitations:
* - The calculation intentionally mirrors common strftime semantics for %U and
* %W, but exact libc parity can still differ across platforms.
*/
function legacy_compat_internal_week_number($timestamp, $gmt, $startDay)
{
$formatter = $gmt ? 'gmdate' : 'date';
$year = (int) $formatter('Y', $timestamp);
$dayOfYear = (int) $formatter('z', $timestamp);
$jan1Timestamp = $gmt
? gmmktime(0, 0, 0, 1, 1, $year)
: mktime(0, 0, 0, 1, 1, $year);
if ($startDay === 1) {
$jan1Weekday = ((int) $formatter('N', $jan1Timestamp)) - 1;
} else {
$jan1Weekday = (int) $formatter('w', $jan1Timestamp);
}
$firstWeekStart = (7 - $jan1Weekday) % 7;
if ($dayOfYear < $firstWeekStart) {
return '00';
}
return sprintf('%02d', intdiv($dayOfYear - $firstWeekStart, 7) + 1);
}
/**
* Approximate deprecated strftime-style formatting with date()/gmdate().
*
* Historical note:
* - strftime() and gmstrftime() were deprecated in PHP 8.1 because they depend
* on platform locale APIs that vary by system.
* - This helper provides a stable replacement for common legacy tokens.
*
* Compatibility type: compatibility approximation.
*
* Return behavior:
* - Returns a formatted string.
*
* Limitations:
* - Locale-sensitive textual tokens fall back to English date()/gmdate()
* output on PHP 8.1+ because userland cannot reproduce libc locale behavior
* exactly without delegating back to the deprecated native APIs.
*/
function legacy_compat_internal_format_strftime($format, $timestamp, $gmt)
{
$formatter = $gmt ? 'gmdate' : 'date';
$result = '';
$length = strlen($format);
for ($index = 0; $index < $length; $index++) {
$character = $format[$index];
if ($character !== '%') {
$result .= $character;
continue;
}
$index++;
if ($index >= $length) {
$result .= '%';
break;
}
$token = $format[$index];
switch ($token) {
case '%':
$result .= '%';
break;
case 'a':
$result .= $formatter('D', $timestamp);
break;
case 'A':
$result .= $formatter('l', $timestamp);
break;
case 'b':
case 'h':
$result .= $formatter('M', $timestamp);
break;
case 'B':
$result .= $formatter('F', $timestamp);
break;
case 'c':
$result .= $formatter('D M d H:i:s Y', $timestamp);
break;
case 'C':
$result .= sprintf('%02d', (int) floor(((int) $formatter('Y', $timestamp)) / 100));
break;
case 'd':
$result .= $formatter('d', $timestamp);
break;
case 'D':
$result .= $formatter('m/d/y', $timestamp);
break;
case 'e':
$result .= str_pad($formatter('j', $timestamp), 2, ' ', STR_PAD_LEFT);
break;
case 'F':
$result .= $formatter('Y-m-d', $timestamp);
break;
case 'g':
$result .= substr($formatter('o', $timestamp), -2);
break;
case 'G':
$result .= $formatter('o', $timestamp);
break;
case 'H':
$result .= $formatter('H', $timestamp);
break;
case 'I':
$result .= $formatter('h', $timestamp);
break;
case 'j':
$result .= sprintf('%03d', ((int) $formatter('z', $timestamp)) + 1);
break;
case 'k':
$result .= str_pad($formatter('G', $timestamp), 2, ' ', STR_PAD_LEFT);
break;
case 'l':
$result .= str_pad($formatter('g', $timestamp), 2, ' ', STR_PAD_LEFT);
break;
case 'm':
$result .= $formatter('m', $timestamp);
break;
case 'M':
$result .= $formatter('i', $timestamp);
break;
case 'n':
$result .= "\n";
break;
case 'p':
$result .= $formatter('A', $timestamp);
break;
case 'P':
$result .= strtolower($formatter('A', $timestamp));
break;
case 'r':
$result .= $formatter('h:i:s A', $timestamp);
break;
case 'R':
$result .= $formatter('H:i', $timestamp);
break;
case 's':
$result .= (string) $timestamp;
break;
case 'S':
$result .= $formatter('s', $timestamp);
break;
case 't':
$result .= "\t";
break;
case 'T':
$result .= $formatter('H:i:s', $timestamp);
break;
case 'u':
$result .= $formatter('N', $timestamp);
break;
case 'U':
$result .= legacy_compat_internal_week_number($timestamp, $gmt, 0);
break;
case 'V':
$result .= $formatter('W', $timestamp);
break;
case 'w':
$result .= $formatter('w', $timestamp);
break;
case 'W':
$result .= legacy_compat_internal_week_number($timestamp, $gmt, 1);
break;
case 'x':
$result .= $formatter('m/d/y', $timestamp);
break;
case 'X':
$result .= $formatter('H:i:s', $timestamp);
break;
case 'y':
$result .= $formatter('y', $timestamp);
break;
case 'Y':
$result .= $formatter('Y', $timestamp);
break;
case 'z':
$result .= $formatter('O', $timestamp);
break;
case 'Z':
$result .= $formatter('T', $timestamp);
break;
default:
$result .= '%' . $token;
break;
}
}
return $result;
}
/**
* Match an error message against a user-provided list of regular expressions.
*
* Historical note:
* - This helper supports the opt-in soft error handler.
* - It does not map to a removed PHP core function.
*
* Compatibility type: internal utility.
*
* Return behavior:
* - Returns true when a pattern matches the message.
* - Returns false when no pattern matches.
*
* Limitations:
* - Invalid user-provided patterns are ignored after preg_match() reports them.
*/
function legacy_compat_internal_message_matches_any($message, array $patterns)
{
foreach ($patterns as $pattern) {
if (!is_string($pattern) || $pattern === '') {
continue;
}
if (preg_match($pattern, $message) === 1) {
return true;
}
}
return false;
}
legacy_compat_internal_note_limitation(
'Removed extensions such as ext/mysql, ext/mcrypt, and legacy crypto APIs are intentionally not recreated.'
);
legacy_compat_internal_note_limitation(
'Only one compatibility file should be loaded per request; universal and strict variants are not meant to be stacked.'
);
legacy_compat_internal_note_limitation(
'money_format() is intentionally omitted in strict mode because locale-equivalent output cannot be guaranteed.'
);
/*
|--------------------------------------------------------------------------
| Removed In PHP 7.x
|--------------------------------------------------------------------------
*/
/**
* Call a user method on a specific object.
*
* Historically this function proxied a method call on an object instance.
* It was deprecated in PHP 4.1.0 and removed in PHP 7.0.
*
* Compatibility type: true polyfill.
*
* Return behavior:
* - Returns the underlying method return value.
*
* Limitations:
* - None for normal callable object methods.
*/
if (!function_exists('call_user_method')) {
function call_user_method($methodName, &$object, ...$arguments)
{
return call_user_func_array([$object, $methodName], $arguments);
}
legacy_compat_internal_register('polyfills', 'call_user_method');
}
/**
* Call a user method with an array of parameters.
*
* Historically this function proxied a method call using an argument array.
* It was deprecated in PHP 4.1.0 and removed in PHP 7.0.
*
* Compatibility type: true polyfill.
*
* Return behavior:
* - Returns the underlying method return value.
*
* Limitations:
* - Non-array parameter lists are normalized to an empty array or single-item
* array because call_user_func_array() requires an array.
*/
if (!function_exists('call_user_method_array')) {
function call_user_method_array($methodName, &$object, $parameters)
{
if ($parameters === null) {
$parameters = [];
} elseif (!is_array($parameters)) {
$parameters = [$parameters];
}
return call_user_func_array([$object, $methodName], $parameters);
}
legacy_compat_internal_register('polyfills', 'call_user_method_array');
}
/**
* Match a POSIX regular expression.
*
* Historically ereg() searched a string with the POSIX regex engine and
* returned the matched length or false. It was deprecated in PHP 5.3 and
* removed in PHP 7.0.
*
* Compatibility type: compatibility approximation.
*
* Return behavior:
* - Returns the byte length of the full match when found.
* - Returns false when there is no match or the pattern cannot be compiled.
*
* Limitations:
* - The removed POSIX regex engine no longer exists, so this implementation
* wraps the legacy pattern for PCRE use. Obscure POSIX edge cases can differ.
*/
if (!function_exists('ereg')) {
function ereg($pattern, $string, &$registers = null)
{
$compiledPattern = legacy_compat_internal_posix_pattern($pattern, false);
$subject = legacy_compat_internal_stringify($string);
if ($compiledPattern === false || $subject === null) {
if (func_num_args() > 2) {
$registers = [];
}
return false;
}
$matches = [];
$result = preg_match($compiledPattern, $subject, $matches);
if ($result !== 1) {
if (func_num_args() > 2) {
$registers = [];
}
return false;
}
if (func_num_args() > 2) {
$registers = $matches;
}
return strlen($matches[0]);
}
legacy_compat_internal_register('polyfills', 'ereg');
}
/**
* Match a case-insensitive POSIX regular expression.
*
* Historically eregi() was the case-insensitive companion to ereg(). It was
* deprecated in PHP 5.3 and removed in PHP 7.0.
*
* Compatibility type: compatibility approximation.
*
* Return behavior:
* - Returns the byte length of the full match when found.
* - Returns false when there is no match or the pattern cannot be compiled.
*
* Limitations:
* - The removed POSIX regex engine no longer exists, so this implementation
* wraps the legacy pattern for PCRE use with the i modifier.
*/
if (!function_exists('eregi')) {
function eregi($pattern, $string, &$registers = null)
{
$compiledPattern = legacy_compat_internal_posix_pattern($pattern, true);
$subject = legacy_compat_internal_stringify($string);
if ($compiledPattern === false || $subject === null) {
if (func_num_args() > 2) {
$registers = [];
}
return false;
}
$matches = [];
$result = preg_match($compiledPattern, $subject, $matches);
if ($result !== 1) {
if (func_num_args() > 2) {
$registers = [];
}
return false;
}
if (func_num_args() > 2) {
$registers = $matches;
}
return strlen($matches[0]);
}
legacy_compat_internal_register('polyfills', 'eregi');
}
/**
* Replace text matched by a POSIX regular expression.
*
* Historically ereg_replace() performed POSIX-regex substitution. It was
* deprecated in PHP 5.3 and removed in PHP 7.0.
*
* Compatibility type: compatibility approximation.
*
* Return behavior:
* - Returns the replaced string.
* - Returns false when the pattern cannot be compiled or the subject cannot be
* represented honestly as a string.
*
* Limitations:
* - The removed POSIX regex engine no longer exists, so this implementation
* delegates to preg_replace() with a wrapped PCRE pattern.
*/
if (!function_exists('ereg_replace')) {
function ereg_replace($pattern, $replacement, $string)
{
$compiledPattern = legacy_compat_internal_posix_pattern($pattern, false);
$subject = legacy_compat_internal_stringify($string);
$replacement = legacy_compat_internal_stringify($replacement);
if ($compiledPattern === false || $subject === null || $replacement === null) {
return false;
}
return preg_replace($compiledPattern, $replacement, $subject);
}
legacy_compat_internal_register('polyfills', 'ereg_replace');
}
/**
* Replace text matched by a case-insensitive POSIX regular expression.
*
* Historically eregi_replace() performed case-insensitive POSIX-regex
* substitution. It was deprecated in PHP 5.3 and removed in PHP 7.0.
*
* Compatibility type: compatibility approximation.
*
* Return behavior:
* - Returns the replaced string.
* - Returns false when the pattern cannot be compiled or the subject cannot be
* represented honestly as a string.
*
* Limitations:
* - The removed POSIX regex engine no longer exists, so this implementation
* delegates to preg_replace() with a wrapped PCRE pattern.
*/
if (!function_exists('eregi_replace')) {
function eregi_replace($pattern, $replacement, $string)
{
$compiledPattern = legacy_compat_internal_posix_pattern($pattern, true);
$subject = legacy_compat_internal_stringify($string);
$replacement = legacy_compat_internal_stringify($replacement);
if ($compiledPattern === false || $subject === null || $replacement === null) {
return false;
}
return preg_replace($compiledPattern, $replacement, $subject);
}
legacy_compat_internal_register('polyfills', 'eregi_replace');
}
/**
* Split a string by a POSIX regular expression.
*
* Historically split() tokenized a string with the POSIX regex engine. It was
* deprecated in PHP 5.3 and removed in PHP 7.0.
*
* Compatibility type: compatibility approximation.
*
* Return behavior:
* - Returns an array of string segments.
* - Returns false when the pattern cannot be compiled or the subject cannot be
* represented honestly as a string.
*
* Limitations:
* - The removed POSIX regex engine no longer exists, so this implementation
* delegates to preg_split() with a wrapped PCRE pattern.
*/
if (!function_exists('split')) {
function split($pattern, $string, $limit = -1)
{
$compiledPattern = legacy_compat_internal_posix_pattern($pattern, false);
$subject = legacy_compat_internal_stringify($string);
if ($compiledPattern === false || $subject === null) {
return false;
}
$limit = (int) $limit;
if ($limit === 0) {
$limit = 1;
}
return preg_split($compiledPattern, $subject, $limit);
}
legacy_compat_internal_register('polyfills', 'split');
}
/**
* Split a string by a case-insensitive POSIX regular expression.
*
* Historically spliti() tokenized a string with a case-insensitive POSIX regex.
* It was deprecated in PHP 5.3 and removed in PHP 7.0.
*
* Compatibility type: compatibility approximation.
*
* Return behavior:
* - Returns an array of string segments.
* - Returns false when the pattern cannot be compiled or the subject cannot be
* represented honestly as a string.
*
* Limitations:
* - The removed POSIX regex engine no longer exists, so this implementation
* delegates to preg_split() with a wrapped PCRE pattern.
*/
if (!function_exists('spliti')) {
function spliti($pattern, $string, $limit = -1)
{
$compiledPattern = legacy_compat_internal_posix_pattern($pattern, true);
$subject = legacy_compat_internal_stringify($string);
if ($compiledPattern === false || $subject === null) {
return false;
}
$limit = (int) $limit;
if ($limit === 0) {
$limit = 1;
}
return preg_split($compiledPattern, $subject, $limit);
}
legacy_compat_internal_register('polyfills', 'spliti');
}
legacy_compat_internal_note_limitation(
'POSIX regular-expression polyfills are implemented through PCRE because the original regex engine was removed in PHP 7.0.'
);
/**
* Build a case-insensitive ASCII-ish regular expression fragment.
*
* Historically sql_regcase() converted alphabetic characters into bracket
* expressions such as [Aa]. It was deprecated in PHP 5.3 and removed in 7.0.
*
* Compatibility type: safe fallback.
*
* Return behavior:
* - Returns the transformed pattern string.
*
* Limitations:
* - The historical function was byte-oriented and locale-sensitive. This
* fallback remains byte-oriented and is most faithful for Latin-1 style text.
*/
if (!function_exists('sql_regcase')) {
function sql_regcase($string)
{
$subject = legacy_compat_internal_stringify($string);
if ($subject === null) {
return false;
}
$result = '';
$length = strlen($subject);
for ($index = 0; $index < $length; $index++) {
$character = $subject[$index];
if (ctype_alpha($character)) {
$upper = strtoupper($character);
$lower = strtolower($character);
$result .= ($upper === $lower)
? $character
: '[' . $upper . $lower . ']';
continue;
}
$result .= $character;
}
return $result;
}
legacy_compat_internal_register('polyfills', 'sql_regcase');
}
/**
* Register one or more globals with the active session.
*
* Historically session_register() exposed register_globals-era session usage by
* binding global variables into $_SESSION. It was deprecated in PHP 5.3 and
* removed in PHP 5.4.
*
* Compatibility type: partial polyfill.
*
* Return behavior:
* - Returns true when every provided name was registered in the active session.
* - Returns false when there is no active session or an invalid name is given.
*
* Limitations:
* - Modern PHP no longer has register_globals, so this polyfill can only mirror
* the $_SESSION binding behavior inside the current request.
* - Strict mode does not auto-start a session because that would introduce a
* side effect outside the immediate compatibility contract.
*/
if (!function_exists('session_register')) {
function session_register(...$names)
{
if (!function_exists('session_status') || session_status() !== PHP_SESSION_ACTIVE) {
return false;
}
if (!isset($_SESSION) || !is_array($_SESSION)) {
$_SESSION = [];
}
$success = true;
foreach ($names as $name) {
if (!is_string($name) || $name === '') {
$success = false;
continue;
}
if (!array_key_exists($name, $GLOBALS)) {
$GLOBALS[$name] = null;
}
$_SESSION[$name] =& $GLOBALS[$name];
}
return $success;
}
legacy_compat_internal_register('polyfills', 'session_register');
}
/**
* Unregister a session variable by name.
*
* Historically session_unregister() removed a registered session variable. It
* was deprecated in PHP 5.3 and removed in PHP 5.4.
*
* Compatibility type: partial polyfill.
*
* Return behavior:
* - Returns true when the named key existed and was removed from $_SESSION.
* - Returns false when there is no active session or the key was absent.
*
* Limitations:
* - The associated global variable is intentionally left untouched because the
* removed register_globals engine behavior no longer exists.
* - Strict mode does not auto-start a session because that would introduce a
* side effect outside the immediate compatibility contract.
*/
if (!function_exists('session_unregister')) {
function session_unregister($name)
{
if (!function_exists('session_status') || session_status() !== PHP_SESSION_ACTIVE) {
return false;
}
if (!is_string($name) || $name === '' || !isset($_SESSION) || !is_array($_SESSION)) {
return false;
}
if (!array_key_exists($name, $_SESSION)) {
return false;
}
unset($_SESSION[$name]);
return true;
}
legacy_compat_internal_register('polyfills', 'session_unregister');
}
/**
* Check whether a named session variable is registered.
*
* Historically session_is_registered() reported whether a session key had been
* registered through the older session APIs. It was deprecated in PHP 5.3 and
* removed in PHP 5.4.
*
* Compatibility type: partial polyfill.
*
* Return behavior:
* - Returns true when the named key exists in the active session.
* - Returns false otherwise.
*
* Limitations:
* - Modern PHP no longer tracks "registered" session globals separately, so
* this polyfill checks key existence in $_SESSION directly.
* - Strict mode does not auto-start a session because that would introduce a
* side effect outside the immediate compatibility contract.
*/
if (!function_exists('session_is_registered')) {
function session_is_registered($name)
{
if (!function_exists('session_status') || session_status() !== PHP_SESSION_ACTIVE) {
return false;
}
if (!is_string($name) || $name === '' || !isset($_SESSION) || !is_array($_SESSION)) {
return false;
}
return array_key_exists($name, $_SESSION);
}
legacy_compat_internal_register('polyfills', 'session_is_registered');
}
legacy_compat_internal_note_limitation(
'session_register() polyfills only the $_SESSION binding behavior because register_globals was removed from the engine; strict mode requires an already active session.'
);
/**
* Create a dynamically named function from argument and body strings.
*
* Historically create_function() compiled runtime PHP code and returned a
* generated function name string. It was deprecated in PHP 7.2 and removed in
* PHP 8.0.
*
* Compatibility type: true polyfill.
*
* Return behavior:
* - Returns the generated function name string on success.
* - Returns false when the generated function cannot be compiled.
*
* Limitations:
* - eval() is required because the historical API itself compiled runtime code.
* - Generated functions remain defined until the request ends, just like the
* original implementation.
*/
if (!function_exists('create_function')) {
function create_function($arguments, $code)
{
if (!is_string($arguments) || !is_string($code)) {
return false;
}
static $counter = 0;
do {
$counter++;
$functionName = '__legacy_create_function_' . $counter;
} while (function_exists($functionName));
$source = 'function ' . $functionName . '(' . $arguments . ') {' . "\n" . $code . "\n" . '}';
try {
eval($source);
} catch (ParseError $exception) {
return false;
}
return function_exists($functionName) ? $functionName : false;
}
legacy_compat_internal_register('polyfills', 'create_function');
legacy_compat_internal_note_limitation(
'create_function() uses eval() because the historical API compiled code at runtime; only load trusted legacy code.'
);