forked from hnw/php-timecop
-
Notifications
You must be signed in to change notification settings - Fork 5
/
timecop_php7.c
1670 lines (1431 loc) · 49.6 KB
/
timecop_php7.c
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
/*
MIT License
Copyright (c) 2012-2017 Yoshio HANAWA
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_timecop.h"
#ifdef ZFS
#include "TSRM.h"
#endif
ZEND_DECLARE_MODULE_GLOBALS(timecop)
static void timecop_globals_ctor(zend_timecop_globals *globals) {
/* Initialize your global struct */
globals->func_override = 1;
globals->sync_request_time = 1;
ZVAL_NULL(&globals->orig_request_time);
globals->timecop_mode = TIMECOP_MODE_REALTIME;
globals->freezed_time.sec = 0;
globals->freezed_time.usec = 0;
globals->travel_origin.sec = 0;
globals->travel_origin.usec = 0;
globals->travel_offset.sec = 0;
globals->travel_offset.usec = 0;
globals->scaling_factor = 1;
globals->ce_DateTimeZone = NULL;
globals->ce_DateTimeInterface = NULL;
globals->ce_DateTime = NULL;
globals->ce_TimecopDateTime = NULL;
globals->ce_DateTimeImmutable = NULL;
globals->ce_TimecopDateTimeImmutable = NULL;
}
static const struct timecop_override_func_entry timecop_override_func_table[] = {
TIMECOP_OFE("time"),
TIMECOP_OFE("mktime"),
TIMECOP_OFE("gmmktime"),
TIMECOP_OFE("date"),
TIMECOP_OFE("gmdate"),
TIMECOP_OFE("idate"),
TIMECOP_OFE("getdate"),
TIMECOP_OFE("localtime"),
TIMECOP_OFE("strtotime"),
TIMECOP_OFE("strftime"),
TIMECOP_OFE("gmstrftime"),
#ifdef HAVE_GETTIMEOFDAY
TIMECOP_OFE("microtime"),
TIMECOP_OFE("gettimeofday"),
#endif
TIMECOP_OFE("unixtojd"),
TIMECOP_OFE("date_create"),
TIMECOP_OFE("date_create_from_format"),
TIMECOP_OFE("date_create_immutable"),
TIMECOP_OFE("date_create_immutable_from_format"),
{NULL, NULL, NULL}
};
static const struct timecop_override_class_entry timecop_override_class_table[] = {
TIMECOP_OCE("datetime", "__construct"),
TIMECOP_OCE("datetime", "createfromformat"),
TIMECOP_OCE("datetimeimmutable", "__construct"),
TIMECOP_OCE("datetimeimmutable", "createfromformat"),
{NULL, NULL, NULL, NULL}
};
#if PHP_VERSION_ID >= 80000
#include "timecop_php8_arginfo.h"
#else
#include "timecop_php7_arginfo.h"
#endif
/* {{{ timecop_functions[] */
const zend_function_entry timecop_functions[] = {
PHP_FE(timecop_freeze, arginfo_timecop_freeze)
PHP_FE(timecop_travel, arginfo_timecop_travel)
PHP_FE(timecop_scale, arginfo_timecop_scale)
PHP_FE(timecop_return, arginfo_timecop_return)
PHP_FE(timecop_time, arginfo_timecop_time)
PHP_FE(timecop_mktime, arginfo_timecop_mktime)
PHP_FE(timecop_gmmktime, arginfo_timecop_gmmktime)
PHP_FE(timecop_date, arginfo_timecop_date)
PHP_FE(timecop_gmdate, arginfo_timecop_gmdate)
PHP_FE(timecop_idate, arginfo_timecop_idate)
PHP_FE(timecop_getdate, arginfo_timecop_getdate)
PHP_FE(timecop_localtime, arginfo_timecop_localtime)
PHP_FE(timecop_strtotime, arginfo_timecop_strtotime)
#if PHP_VERSION_ID >= 80100
PHP_DEP_FE(timecop_strftime, arginfo_timecop_strftime)
PHP_DEP_FE(timecop_gmstrftime, arginfo_timecop_gmstrftime)
#else
PHP_FE(timecop_strftime, arginfo_timecop_strftime)
PHP_FE(timecop_gmstrftime, arginfo_timecop_gmstrftime)
#endif
#ifdef HAVE_GETTIMEOFDAY
PHP_FE(timecop_microtime, arginfo_timecop_microtime)
PHP_FE(timecop_gettimeofday, arginfo_timecop_gettimeofday)
#endif
PHP_FE(timecop_unixtojd, arginfo_timecop_unixtojd)
PHP_FE(timecop_date_create, arginfo_timecop_date_create)
PHP_FE(timecop_date_create_from_format, arginfo_timecop_date_create_from_format)
PHP_FE(timecop_date_create_immutable, arginfo_timecop_date_create_immutable)
PHP_FE(timecop_date_create_immutable_from_format, arginfo_timecop_date_create_immutable_from_format)
{NULL, NULL, NULL}
};
/* }}} */
/* declare method parameters, */
/* each method can have its own parameters and visibility */
static zend_function_entry timecop_funcs_timecop[] = {
PHP_ME_MAPPING(freeze, timecop_freeze, arginfo_timecop_travel, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME_MAPPING(travel, timecop_travel, arginfo_timecop_travel, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME_MAPPING(scale, timecop_scale, arginfo_timecop_scale, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME_MAPPING(return, timecop_return, arginfo_timecop_return, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
{NULL, NULL, NULL}
};
static zend_function_entry timecop_funcs_date[] = {
PHP_ME(TimecopDateTime, __construct, arginfo_class_TimecopDateTime___construct,
ZEND_ACC_CTOR | ZEND_ACC_PUBLIC)
PHP_ME_MAPPING(createFromFormat, timecop_date_create_from_format, arginfo_class_TimecopDateTime_createFromFormat,
ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
{NULL, NULL, NULL}
};
static zend_function_entry timecop_funcs_orig_date[] = {
PHP_ME(TimecopOrigDateTime, __construct, arginfo_class_TimecopDateTime___construct,
ZEND_ACC_CTOR | ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
static zend_function_entry timecop_funcs_immutable[] = {
PHP_ME(TimecopDateTimeImmutable, __construct, arginfo_class_TimecopDateTimeImmutable___construct,
ZEND_ACC_CTOR | ZEND_ACC_PUBLIC)
PHP_ME_MAPPING(createFromFormat, timecop_date_create_immutable_from_format, arginfo_class_TimecopDateTimeImmutable_createFromFormat,
ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
{NULL, NULL, NULL}
};
static zend_function_entry timecop_funcs_orig_immutable[] = {
PHP_ME(TimecopOrigDateTimeImmutable, __construct, arginfo_class_TimecopDateTimeImmutable___construct,
ZEND_ACC_CTOR | ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
#define MKTIME_NUM_ARGS 6
#define TIMECOP_CALL_FUNCTION(func_name, index_to_fill_timestamp) \
_timecop_call_function(INTERNAL_FUNCTION_PARAM_PASSTHRU, ORIG_FUNC_NAME(func_name), index_to_fill_timestamp);
#define TIMECOP_CALL_MKTIME(mktime_func_name, date_func_name) \
_timecop_call_mktime(INTERNAL_FUNCTION_PARAM_PASSTHRU, ORIG_FUNC_NAME(mktime_func_name), ORIG_FUNC_NAME(date_func_name));
static void timecop_globals_ctor(zend_timecop_globals *globals);
static int register_timecop_classes();
static int timecop_func_override();
static int timecop_class_override();
static int timecop_func_override_clear();
static int timecop_class_override_clear();
static int update_request_time(zend_long unixtime);
static int restore_request_time();
static int fill_mktime_params(zval *fill_params, const char *date_function_name, int from);
static int get_formatted_mock_time(zval *time, zval *timezone_obj, zval *retval_time, zval *retval_timezone);
static long get_mock_fraction(zval *time, zval *timezone_obj);
static void _timecop_call_function(INTERNAL_FUNCTION_PARAMETERS, const char *function_name, int index_to_fill_timestamp);
static void _timecop_call_mktime(INTERNAL_FUNCTION_PARAMETERS, const char *mktime_function_name, const char *date_function_name);
static int get_mock_timeval(tc_timeval *fixed, const tc_timeval *now);
static inline zend_long mock_timestamp();
static int get_timeval_from_datetime(tc_timeval *tp, zval *dt);
static int get_current_time(tc_timeval *now);
static void _timecop_orig_datetime_constructor(INTERNAL_FUNCTION_PARAMETERS, int immutable);
static inline void _timecop_date_create(INTERNAL_FUNCTION_PARAMETERS, int immutable);
static inline void _timecop_datetime_constructor(INTERNAL_FUNCTION_PARAMETERS, int immutable);
static void _timecop_datetime_constructor_ex(INTERNAL_FUNCTION_PARAMETERS, zval *obj, int immutable);
static inline zval* _call_php_method_with_0_params(zval *object_pp, zend_class_entry *obj_ce, const char *method_name, zval *retval_ptr);
static inline zval* _call_php_method_with_1_params(zval *object_pp, zend_class_entry *obj_ce, const char *method_name, zval *retval_ptr, zval *arg1);
static inline zval* _call_php_method_with_2_params(zval *object_pp, zend_class_entry *obj_ce, const char *method_name, zval *retval_ptr, zval *arg1, zval *arg2);
static inline zval* _call_php_method(zval *object_pp, zend_class_entry *obj_ce, const char *method_name, zval *retval_ptr, int param_count, zval* arg1, zval* arg2);
static void _call_php_function_with_3_params(const char *function_name, zval *retval_ptr, zval *arg1, zval *arg2, zval *arg3);
static inline void _call_php_function_with_params(const char *function_name, zval *retval_ptr, uint32_t param_count, zval params[]);
static const zend_module_dep timecop_module_deps[] = {
ZEND_MOD_REQUIRED("Date")
ZEND_MOD_END
};
/* {{{ timecop_module_entry
*/
zend_module_entry timecop_module_entry = {
STANDARD_MODULE_HEADER_EX,
NULL,
timecop_module_deps,
"timecop",
timecop_functions,
PHP_MINIT(timecop),
PHP_MSHUTDOWN(timecop),
PHP_RINIT(timecop),
PHP_RSHUTDOWN(timecop),
PHP_MINFO(timecop),
#if ZEND_MODULE_API_NO >= 20010901
PHP_TIMECOP_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_TIMECOP
# ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE();
# endif
ZEND_GET_MODULE(timecop)
#endif
/* {{{ PHP_INI
*/
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("timecop.func_override", "1",
PHP_INI_SYSTEM, OnUpdateLong, func_override, zend_timecop_globals, timecop_globals)
STD_PHP_INI_ENTRY("timecop.sync_request_time", "1",
PHP_INI_SYSTEM, OnUpdateLong, sync_request_time, zend_timecop_globals, timecop_globals)
PHP_INI_END()
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(timecop)
{
ZEND_INIT_MODULE_GLOBALS(timecop, timecop_globals_ctor, NULL);
REGISTER_INI_ENTRIES();
register_timecop_classes();
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(timecop)
{
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RINIT_FUNCTION(timecop) */
PHP_RINIT_FUNCTION(timecop)
{
#if defined(COMPILE_DL_TIMECOP) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
if (TIMECOP_G(func_override)) {
if (SUCCESS != timecop_func_override() ||
SUCCESS != timecop_class_override()) {
return FAILURE;
}
}
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RSHUTDOWN_FUNCTION(timecop) */
PHP_RSHUTDOWN_FUNCTION(timecop)
{
if (TIMECOP_G(func_override)) {
timecop_func_override_clear();
timecop_class_override_clear();
}
if (Z_TYPE(TIMECOP_G(orig_request_time)) == IS_NULL) {
restore_request_time();
}
TIMECOP_G(timecop_mode) = TIMECOP_MODE_REALTIME;
TIMECOP_G(scaling_factor) = 1;
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(timecop)
{
php_info_print_table_start();
php_info_print_table_header(2, "timecop", "enabled");
php_info_print_table_row(2, "Version", PHP_TIMECOP_VERSION);
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* }}} */
static int register_timecop_classes()
{
zend_class_entry ce;
zend_class_entry *tmp, *date_ce, *timezone_ce, *immutable_ce, *interface_ce;
date_ce = zend_hash_str_find_ptr(CG(class_table), "datetime", sizeof("datetime")-1);
if (date_ce == NULL) {
/* DateTime must be initialized before */
php_error_docref("https://github.com/hnw/php-timecop", E_WARNING,
"timecop couldn't find class %s.", "DateTime");
return SUCCESS;
}
timezone_ce = zend_hash_str_find_ptr(CG(class_table), "datetimezone", sizeof("datetimezone")-1);
if (timezone_ce == NULL) {
/* DateTime must be initialized before */
php_error_docref("https://github.com/hnw/php-timecop", E_WARNING,
"timecop couldn't find class %s.", "DateTimeZone");
return SUCCESS;
}
immutable_ce = zend_hash_str_find_ptr(CG(class_table), "datetimeimmutable", sizeof("datetimeimmutable")-1);
if (immutable_ce == NULL) {
/* DateTimeImmutable must be initialized before */
php_error_docref("https://github.com/hnw/php-timecop", E_WARNING,
"timecop couldn't find class %s.", "DateTimeImmutable");
return SUCCESS;
}
interface_ce = zend_hash_str_find_ptr(CG(class_table), "datetimeinterface", sizeof("datetimeinterface")-1);
if (interface_ce == NULL) {
/* DateTimeInterface must be initialized before */
php_error_docref("https://github.com/hnw/php-timecop", E_WARNING,
"timecop couldn't find interface %s.", "DateTimeInterface");
return SUCCESS;
}
INIT_CLASS_ENTRY(ce, "Timecop", timecop_funcs_timecop);
zend_register_internal_class(&ce);
TIMECOP_G(ce_DateTimeZone) = timezone_ce;
TIMECOP_G(ce_DateTimeInterface) = interface_ce;
/* replace DateTime */
INIT_CLASS_ENTRY(ce, "TimecopDateTime", timecop_funcs_date);
tmp = zend_register_internal_class_ex(&ce, date_ce);
tmp->create_object = date_ce->create_object;
TIMECOP_G(ce_DateTime) = date_ce;
TIMECOP_G(ce_TimecopDateTime) = tmp;
INIT_CLASS_ENTRY(ce, "TimecopOrigDateTime", timecop_funcs_orig_date);
tmp = zend_register_internal_class_ex(&ce, date_ce);
tmp->create_object = date_ce->create_object;
/* replace DateTimeImmutable */
INIT_CLASS_ENTRY(ce, "TimecopDateTimeImmutable", timecop_funcs_immutable);
tmp = zend_register_internal_class_ex(&ce, immutable_ce);
tmp->create_object = immutable_ce->create_object;
TIMECOP_G(ce_DateTimeImmutable) = immutable_ce;
TIMECOP_G(ce_TimecopDateTimeImmutable) = tmp;
INIT_CLASS_ENTRY(ce, "TimecopOrigDateTimeImmutable", timecop_funcs_orig_immutable);
tmp = zend_register_internal_class_ex(&ce, immutable_ce);
tmp->create_object = immutable_ce->create_object;
return SUCCESS;
}
static int timecop_func_override()
{
const struct timecop_override_func_entry *p;
zend_function *zf_orig, *zf_ovrd, *zf_save;
p = &(timecop_override_func_table[0]);
while (p->orig_func != NULL) {
zf_orig = zend_hash_str_find_ptr(CG(function_table), p->orig_func, strlen(p->orig_func));
if (zf_orig == NULL) {
/* Do nothing. Because some functions are introduced by optional extensions. */
p++;
continue;
}
zf_ovrd = zend_hash_str_find_ptr(CG(function_table), p->ovrd_func, strlen(p->ovrd_func));
if (zf_ovrd == NULL) {
php_error_docref("https://github.com/hnw/php-timecop", E_WARNING,
"timecop couldn't find function %s.", p->ovrd_func);
p++;
continue;
}
zf_save = zend_hash_str_find_ptr(CG(function_table), p->save_func, strlen(p->save_func));
if (zf_save != NULL) {
php_error_docref("https://github.com/hnw/php-timecop", E_WARNING,
"timecop couldn't create function %s because already exists.",
p->save_func);
p++;
continue;
}
TIMECOP_ASSERT(zf_orig->type == ZEND_INTERNAL_FUNCTION);
TIMECOP_ASSERT(zf_ovrd->type == ZEND_INTERNAL_FUNCTION);
zend_hash_str_add_mem(CG(function_table), p->save_func, strlen(p->save_func),
zf_orig, sizeof(zend_internal_function));
function_add_ref(zf_orig);
FIX_FUNCTION_ARG_INFO_DTOR(zf_orig);
zend_hash_str_update_mem(CG(function_table), p->orig_func, strlen(p->orig_func),
zf_ovrd, sizeof(zend_internal_function));
function_add_ref(zf_ovrd);
p++;
}
return SUCCESS;
}
static int timecop_class_override()
{
const struct timecop_override_class_entry *p;
zend_class_entry *ce_orig, *ce_ovrd;
zend_function *zf_orig, *zf_ovrd, *zf_save, *zf_new;
p = &(timecop_override_class_table[0]);
while (p->orig_class != NULL) {
ce_orig = zend_hash_str_find_ptr(CG(class_table), p->orig_class, strlen(p->orig_class));
if (ce_orig == NULL) {
php_error_docref("https://github.com/hnw/php-timecop", E_WARNING,
"timecop couldn't find class %s.", p->orig_class);
p++;
continue;
}
ce_ovrd = zend_hash_str_find_ptr(CG(class_table), p->ovrd_class, strlen(p->ovrd_class));
if (ce_ovrd == NULL) {
php_error_docref("https://github.com/hnw/php-timecop", E_WARNING,
"timecop couldn't find class %s.", p->ovrd_class);
p++;
continue;
}
zf_orig = zend_hash_str_find_ptr(&ce_orig->function_table,
p->orig_method, strlen(p->orig_method));
if (zf_orig == NULL) {
php_error_docref("https://github.com/hnw/php-timecop", E_WARNING,
"timecop couldn't find method %s::%s.",
p->orig_class, p->orig_method);
p++;
continue;
}
zf_ovrd = zend_hash_str_find_ptr(&ce_ovrd->function_table,
p->orig_method, strlen(p->orig_method));
if (zf_ovrd == NULL) {
php_error_docref("https://github.com/hnw/php-timecop", E_WARNING,
"timecop couldn't find method %s::%s.",
p->ovrd_class, p->orig_method);
p++;
continue;
}
zf_save = zend_hash_str_find_ptr(&ce_orig->function_table,
p->save_method, strlen(p->save_method));
if (zf_save != NULL) {
php_error_docref("https://github.com/hnw/php-timecop", E_WARNING,
"timecop couldn't create method %s::%s because already exists.",
p->orig_class, p->save_method);
p++;
continue;
}
TIMECOP_ASSERT(zf_orig->type == ZEND_INTERNAL_FUNCTION);
TIMECOP_ASSERT(ce_orig->type & ZEND_INTERNAL_CLASS);
TIMECOP_ASSERT(zf_ovrd->type == ZEND_INTERNAL_FUNCTION);
TIMECOP_ASSERT(ce_ovrd->type & ZEND_INTERNAL_CLASS);
zend_hash_str_add_mem(&ce_orig->function_table,
p->save_method, strlen(p->save_method),
zf_orig, sizeof(zend_internal_function));
function_add_ref(zf_orig);
zf_new = zend_hash_str_update_mem(&ce_orig->function_table,
p->orig_method, strlen(p->orig_method),
zf_ovrd, sizeof(zend_internal_function));
function_add_ref(zf_ovrd);
TIMECOP_ASSERT(zf_new != NULL);
TIMECOP_ASSERT(zf_new != zf_orig);
if (strcmp(p->orig_method, "__construct") == 0) {
ce_orig->constructor = zf_new;
}
p++;
}
return SUCCESS;
}
/* clear function overriding. */
static int timecop_func_override_clear()
{
const struct timecop_override_func_entry *p;
zend_function *zf_orig, *zf_ovld;
p = &(timecop_override_func_table[0]);
while (p->orig_func != NULL) {
zf_orig = zend_hash_str_find_ptr(CG(function_table),
p->save_func, strlen(p->save_func));
zf_ovld = zend_hash_str_find_ptr(CG(function_table),
p->orig_func, strlen(p->orig_func));
if (zf_orig == NULL || zf_ovld == NULL) {
p++;
continue;
}
FIX_FUNCTION_ARG_INFO_DTOR(zf_ovld);
zend_hash_str_update_mem(CG(function_table), p->orig_func, strlen(p->orig_func),
zf_orig, sizeof(zend_internal_function));
function_add_ref(zf_orig);
FIX_FUNCTION_ARG_INFO_DTOR(zf_orig);
zend_hash_str_del(CG(function_table), p->save_func, strlen(p->save_func));
p++;
}
return SUCCESS;
}
static int timecop_class_override_clear()
{
const struct timecop_override_class_entry *p;
zend_class_entry *ce_orig;
zend_function *zf_orig;
p = &(timecop_override_class_table[0]);
while (p->orig_class != NULL) {
ce_orig = zend_hash_str_find_ptr(CG(class_table),
p->orig_class, strlen(p->orig_class));
if (ce_orig == NULL) {
php_error_docref("https://github.com/hnw/php-timecop", E_WARNING,
"timecop couldn't find class %s.", p->orig_class);
p++;
continue;
}
zf_orig = zend_hash_str_find_ptr(&ce_orig->function_table,
p->save_method, strlen(p->save_method));
if (zf_orig == NULL) {
php_error_docref("https://github.com/hnw/php-timecop", E_WARNING,
"timecop couldn't find method %s::%s.",
p->orig_class, p->save_method);
p++;
continue;
}
zend_hash_str_update_mem(&ce_orig->function_table, p->orig_method, strlen(p->orig_method),
zf_orig, sizeof(zend_internal_function));
function_add_ref(zf_orig);
zend_hash_str_del(&ce_orig->function_table, p->save_method, strlen(p->save_method));
if (strcmp(p->orig_method, "__construct") == 0) {
ce_orig->constructor = zf_orig;
}
p++;
}
return SUCCESS;
}
static int update_request_time(zend_long unixtime)
{
zval *server_vars, *request_time, tmp;
server_vars = zend_hash_str_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER")-1);
if (server_vars != NULL && Z_TYPE_P(server_vars) == IS_ARRAY) {
request_time = zend_hash_str_find(Z_ARRVAL_P(server_vars), "REQUEST_TIME", sizeof("REQUEST_TIME")-1);
if (request_time != NULL) {
if (Z_TYPE(TIMECOP_G(orig_request_time)) == IS_NULL) {
ZVAL_COPY_VALUE(&TIMECOP_G(orig_request_time), request_time);
}
}
ZVAL_LONG(&tmp, unixtime);
zend_hash_str_update(Z_ARRVAL_P(server_vars),
"REQUEST_TIME", sizeof("REQUEST_TIME")-1,
&tmp);
}
return SUCCESS;
}
static int restore_request_time()
{
zval *server_vars;
server_vars = zend_hash_str_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER")-1);
if (Z_TYPE(TIMECOP_G(orig_request_time)) != IS_NULL &&
server_vars != NULL && Z_TYPE_P(server_vars) == IS_ARRAY) {
zend_hash_str_update(Z_ARRVAL_P(server_vars),
"REQUEST_TIME", sizeof("REQUEST_TIME")-1,
&TIMECOP_G(orig_request_time));
ZVAL_NULL(&TIMECOP_G(orig_request_time));
}
return SUCCESS;
}
static int fill_mktime_params(zval *fill_params, const char *date_function_name, int from)
{
char *formats[MKTIME_NUM_ARGS] = {"H", "i", "s", "n", "j", "Y"};
zval format, timestamp;
int i;
ZVAL_LONG(×tamp, mock_timestamp());
for (i = from; i < MKTIME_NUM_ARGS; i++) {
ZVAL_STRING(&format, formats[i]);
call_php_function_with_2_params(date_function_name, &fill_params[i], &format, ×tamp);
zval_ptr_dtor(&format);
}
return MKTIME_NUM_ARGS;
}
/*
* get_formatted_mock_time() : return formatted mock time like "2000-12-30 01:02:03.456000"
*
* pseudo code:
*
* function get_formatted_mock_time($time, $timezone_obj) {
* if ($time === null || $time === false || $time === "") {
* $time = "now";
* }
* $now = get_mock_timeval();
* if ($timezone_obj) {
* // save default timezone
* $zonename = $timezone_obj->getName()
* $orig_zonename = date_default_timezone_get();
* date_default_timezone_set($zonename);
* }
* $fixed_sec = strtotime($time, $now->sec);
* if ($timezone_obj && $orig_zonename) {
* // restore default timezone
* date_default_timezone_set($orig_zonename);
* }
* if ($fixed_sec === FALSE) {
* return false;
* }
* $fixed_usec = get_mock_fraction($time, $timezone_obj);
* if ($fixed_usec === -1) {
* $fixed_usec = $now->usec;
* }
* $dt = date_create($time, $timezone_obj);
* $dt->setTimestamp($fixed_sec);
* $format = sprintf("Y-m-d H:i:s.%06d", $fixed_usec);
* $formatted_time = $dt->format($format);
* return $formatted_time;
* }
*/
static int get_formatted_mock_time(zval *time, zval *timezone_obj, zval *retval_time, zval *retval_timezone)
{
zval fixed_sec, orig_zonename;
zval now_timestamp, str_now;
tc_timeval now;
zend_long fixed_usec;
if (TIMECOP_G(timecop_mode) == TIMECOP_MODE_REALTIME) {
ZVAL_FALSE(retval_time);
ZVAL_NULL(retval_timezone);
return -1;
}
if (time == NULL || Z_TYPE_P(time) == IS_NULL ||
Z_TYPE_P(time) == IS_FALSE ||
(Z_TYPE_P(time) == IS_STRING && Z_STRLEN_P(time) == 0)) {
ZVAL_STRING(&str_now, "now");
time = &str_now;
}
get_mock_timeval(&now, NULL);
// @todo Restore removed timezone handling code? https://github.com/kiddivouchers/php-timecop/pull/6
ZVAL_LONG(&now_timestamp, now.sec);
call_php_function_with_2_params(ORIG_FUNC_NAME("strtotime"), &fixed_sec, time, &now_timestamp);
if (Z_TYPE(fixed_sec) == IS_FALSE) {
ZVAL_FALSE(retval_time);
ZVAL_NULL(retval_timezone);
return -1;
}
fixed_usec = get_mock_fraction(time, timezone_obj);
if (fixed_usec == -1) {
fixed_usec = now.usec;
}
{
zval dt;
zval format_str;
char buf[64];
call_php_function_with_2_params(ORIG_FUNC_NAME("date_create"), &dt, time, timezone_obj);
if (Z_TYPE(dt) == IS_FALSE) {
ZVAL_FALSE(retval_time);
ZVAL_NULL(retval_timezone);
return -1;
}
sprintf(buf, "Y-m-d H:i:s.%06ld", fixed_usec);
ZVAL_STRING(&format_str, buf);
call_php_method_with_1_params(&dt, TIMECOP_G(ce_DateTime), "settimestamp", NULL, &fixed_sec);
call_php_method_with_0_params(&dt, TIMECOP_G(ce_DateTime), "gettimezone", retval_timezone);
call_php_method_with_1_params(&dt, TIMECOP_G(ce_DateTime), "format", retval_time, &format_str);
zval_ptr_dtor(&fixed_sec);
zval_ptr_dtor(&format_str);
zval_ptr_dtor(&dt);
}
if (time == &str_now) {
zval_ptr_dtor(&str_now);
}
return 0;
}
/*
* get_mock_fraction()
*
* pseudo code:
*
* function get_mock_fraction($time, $timezone_obj) {
* $dt1 = date_create($time, $timezone_obj);
* usleep(1);
* $dt2 = date_create($time, $timezone_obj);
* $usec1 = $dt1->format("u");
* $usec2 = $dt2->format("u");
* if ($usec1 === $usec2) {
* $fixed_usec = $usec1;
* } else {
* $fixed_usec = -1;
* }
* return $fixed_usec;
* }
*/
static long get_mock_fraction(zval *time, zval *timezone_obj TSRMLS_DC)
{
zval dt1, dt2, usec1, usec2;
zend_long fixed_usec;
zval u_str, sleep_usec;
call_php_function_with_2_params(ORIG_FUNC_NAME("date_create"), &dt1, time, timezone_obj);
if (Z_TYPE(dt1) == IS_FALSE) {
return -1;
}
ZVAL_LONG(&sleep_usec, 1);
call_php_function_with_1_params("usleep", NULL, &sleep_usec);
call_php_function_with_2_params(ORIG_FUNC_NAME("date_create"), &dt2, time, timezone_obj);
if (Z_TYPE(dt2) == IS_FALSE) {
zval_ptr_dtor(&dt1);
return -1;
}
ZVAL_STRING(&u_str, "u");
call_php_method_with_1_params(&dt1, TIMECOP_G(ce_DateTime), "format", &usec1, &u_str);
call_php_method_with_1_params(&dt2, TIMECOP_G(ce_DateTime), "format", &usec2, &u_str);
convert_to_long(&usec1);
convert_to_long(&usec2);
if (Z_LVAL(usec1) == Z_LVAL(usec2)) {
fixed_usec = Z_LVAL(usec1);
} else {
fixed_usec = -1;
}
zval_ptr_dtor(&dt1);
zval_ptr_dtor(&dt2);
zval_ptr_dtor(&u_str);
return fixed_usec;
}
static void _timecop_call_function(INTERNAL_FUNCTION_PARAMETERS, const char *function_name, int index_to_fill_timestamp)
{
zval *params;
uint32_t param_count;
param_count = MAX(ZEND_NUM_ARGS(), index_to_fill_timestamp+1);
params = (zval *)safe_emalloc(param_count, sizeof(zval), 0);
if (zend_get_parameters_array_ex(ZEND_NUM_ARGS(), params) == FAILURE) {
efree(params);
return;
}
param_count = ZEND_NUM_ARGS();
if (param_count == index_to_fill_timestamp) {
ZVAL_LONG(¶ms[param_count], mock_timestamp());
param_count++;
}
_call_php_function_with_params(function_name, return_value, param_count, params);
efree(params);
}
/* {{{ _timecop_call_mktime - timecop_(gm)mktime helper */
static void _timecop_call_mktime(INTERNAL_FUNCTION_PARAMETERS, const char *mktime_function_name, const char *date_function_name)
{
zval *params;
uint32_t param_count;
int i;
param_count = MAX(ZEND_NUM_ARGS(), MKTIME_NUM_ARGS);
params = (zval *)safe_emalloc(param_count, sizeof(zval), 0);
if (zend_get_parameters_array_ex(ZEND_NUM_ARGS(), params) == FAILURE) {
efree(params);
zend_throw_error(NULL, "Cannot get arguments for calling");
return;
}
param_count = ZEND_NUM_ARGS();
if (param_count < MKTIME_NUM_ARGS) {
fill_mktime_params(params, date_function_name, param_count);
param_count = MKTIME_NUM_ARGS;
}
if (ZEND_NUM_ARGS() == 0) {
php_error_docref(NULL, E_DEPRECATED, "You should be using the time() function instead");
}
_call_php_function_with_params(mktime_function_name, return_value, param_count, params);
for (i = ZEND_NUM_ARGS(); i < MKTIME_NUM_ARGS; i++) {
zval_ptr_dtor(¶ms[i]);
}
efree(params);
}
/* }}} */
/* {{{ proto int timecop_freeze(long timestamp)
Time travel to specified timestamp and freeze */
PHP_FUNCTION(timecop_freeze)
{
zval *dt;
zend_long timestamp;
tc_timeval freezed_tv;
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "O", &dt, TIMECOP_G(ce_DateTimeInterface)) != FAILURE) {
get_timeval_from_datetime(&freezed_tv, dt);
} else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "l", ×tamp) != FAILURE) {
freezed_tv.sec = timestamp;
freezed_tv.usec = 0;
} else {
php_error_docref(NULL, E_WARNING, "This function accepts either (DateTimeInterface) OR (int) as arguments.");
RETURN_FALSE;
}
TIMECOP_G(timecop_mode) = TIMECOP_MODE_FREEZE;
TIMECOP_G(freezed_time) = freezed_tv;
if (TIMECOP_G(sync_request_time)){
update_request_time(freezed_tv.sec);
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto int timecop_travel(long timestamp)
Time travel to specified timestamp */
PHP_FUNCTION(timecop_travel)
{
zval *dt;
zend_long timestamp;
tc_timeval now, mock_tv;
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "O", &dt, TIMECOP_G(ce_DateTimeInterface)) != FAILURE) {
get_timeval_from_datetime(&mock_tv, dt);
} else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "l", ×tamp) != FAILURE) {
mock_tv.sec = timestamp;
mock_tv.usec = 0;
} else {
php_error_docref(NULL, E_WARNING, "This function accepts either (DateTimeInterface) OR (int) as arguments.");
RETURN_FALSE;
}
TIMECOP_G(timecop_mode) = TIMECOP_MODE_TRAVEL;
get_current_time(&now);
tc_timeval_sub(&TIMECOP_G(travel_offset), &mock_tv, &now);
TIMECOP_G(travel_origin) = now;
if (TIMECOP_G(sync_request_time)){
update_request_time(mock_tv.sec);
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto int timecop_scale(long scale)
Accelerate time with specified scale */
PHP_FUNCTION(timecop_scale)
{
zend_long scale;
tc_timeval now, mock_time;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &scale) == FAILURE) {
RETURN_FALSE;
}
if (scale < 0) {
RETURN_FALSE;
}
get_current_time(&now);
get_mock_timeval(&mock_time, &now);
TIMECOP_G(timecop_mode) = TIMECOP_MODE_TRAVEL;
TIMECOP_G(travel_origin) = now;
tc_timeval_sub(&TIMECOP_G(travel_offset), &mock_time, &now);
TIMECOP_G(scaling_factor) = scale;
if (TIMECOP_G(sync_request_time)){
update_request_time(mock_time.sec);
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto int timecop_return(void)
Return to Time travel to specified timestamp */
PHP_FUNCTION(timecop_return)
{
TIMECOP_G(timecop_mode) = TIMECOP_MODE_REALTIME;
if (TIMECOP_G(sync_request_time)){
restore_request_time();
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto int timecop_time(void)
Return virtual timestamp */
PHP_FUNCTION(timecop_time)
{
RETURN_LONG(mock_timestamp());
}
/* }}} */
/* {{{ proto int timecop_mktime([int hour [, int min [, int sec [, int mon [, int day [, int year]]]]]])
Get UNIX timestamp for a date */
PHP_FUNCTION(timecop_mktime)
{
zend_long hou = 0, min = 0, sec = 0, mon = 0, day = 0, yea = 0;
zend_bool min_is_null = 1, sec_is_null = 1, mon_is_null = 1, day_is_null = 1, yea_is_null = 1;
#if PHP_VERSION_ID >= 80000
ZEND_PARSE_PARAMETERS_START(1, 6)
Z_PARAM_LONG(hou)
Z_PARAM_OPTIONAL
Z_PARAM_LONG_OR_NULL(min, min_is_null)
Z_PARAM_LONG_OR_NULL(sec, sec_is_null)
Z_PARAM_LONG_OR_NULL(mon, mon_is_null)
Z_PARAM_LONG_OR_NULL(day, day_is_null)
Z_PARAM_LONG_OR_NULL(yea, yea_is_null)
ZEND_PARSE_PARAMETERS_END();