Skip to content

Commit b0c3f80

Browse files
committed
add timecop_time function
1 parent d319526 commit b0c3f80

File tree

3 files changed

+27
-45
lines changed

3 files changed

+27
-45
lines changed

php_timecop.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ PHP_RINIT_FUNCTION(timecop);
4242
PHP_RSHUTDOWN_FUNCTION(timecop);
4343
PHP_MINFO_FUNCTION(timecop);
4444

45-
PHP_FUNCTION(confirm_timecop_compiled); /* For testing, remove later. */
45+
PHP_FUNCTION(timecop_time);
4646

4747
/*
4848
Declare any global variables you may need between the BEGIN

timecop.c

+20-40
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "php_ini.h"
2727
#include "ext/standard/info.h"
2828
#include "php_timecop.h"
29+
#include <time.h>
2930

3031
/* If you declare any globals in php_timecop.h uncomment this:
3132
ZEND_DECLARE_MODULE_GLOBALS(timecop)
@@ -39,7 +40,7 @@ static int le_timecop;
3940
* Every user visible function must have an entry in timecop_functions[].
4041
*/
4142
const zend_function_entry timecop_functions[] = {
42-
PHP_FE(confirm_timecop_compiled, NULL) /* For testing, remove later. */
43+
PHP_FE(timecop_time, NULL)
4344
PHP_FE_END /* Must be the last line in timecop_functions[] */
4445
};
4546
/* }}} */
@@ -54,11 +55,11 @@ zend_module_entry timecop_module_entry = {
5455
timecop_functions,
5556
PHP_MINIT(timecop),
5657
PHP_MSHUTDOWN(timecop),
57-
PHP_RINIT(timecop), /* Replace with NULL if there's nothing to do at request start */
58-
PHP_RSHUTDOWN(timecop), /* Replace with NULL if there's nothing to do at request end */
58+
NULL,
59+
NULL,
5960
PHP_MINFO(timecop),
6061
#if ZEND_MODULE_API_NO >= 20010901
61-
"0.1", /* Replace with version number for your extension */
62+
"0.1",
6263
#endif
6364
STANDARD_MODULE_PROPERTIES
6465
};
@@ -111,30 +112,12 @@ PHP_MSHUTDOWN_FUNCTION(timecop)
111112
}
112113
/* }}} */
113114

114-
/* Remove if there's nothing to do at request start */
115-
/* {{{ PHP_RINIT_FUNCTION
116-
*/
117-
PHP_RINIT_FUNCTION(timecop)
118-
{
119-
return SUCCESS;
120-
}
121-
/* }}} */
122-
123-
/* Remove if there's nothing to do at request end */
124-
/* {{{ PHP_RSHUTDOWN_FUNCTION
125-
*/
126-
PHP_RSHUTDOWN_FUNCTION(timecop)
127-
{
128-
return SUCCESS;
129-
}
130-
/* }}} */
131-
132115
/* {{{ PHP_MINFO_FUNCTION
133116
*/
134117
PHP_MINFO_FUNCTION(timecop)
135118
{
136119
php_info_print_table_start();
137-
php_info_print_table_header(2, "timecop support", "enabled");
120+
php_info_print_table_header(2, "timecop", "enabled");
138121
php_info_print_table_end();
139122

140123
/* Remove comments if you have entries in php.ini
@@ -144,25 +127,22 @@ PHP_MINFO_FUNCTION(timecop)
144127
/* }}} */
145128

146129

147-
/* Remove the following function when you have succesfully modified config.m4
148-
so that your module can be compiled into PHP, it exists only for testing
149-
purposes. */
150-
151-
/* Every user-visible function in PHP should document itself in the source */
152-
/* {{{ proto string confirm_timecop_compiled(string arg)
153-
Return a string to confirm that the module is compiled in */
154-
PHP_FUNCTION(confirm_timecop_compiled)
130+
/* {{{ proto int timecop_time(void)
131+
Return timestamp for $_SERVER[request_time] */
132+
PHP_FUNCTION(timecop_time)
155133
{
156-
char *arg = NULL;
157-
int arg_len, len;
158-
char *strg;
159-
160-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
161-
return;
134+
zval **array, **request_time_long;
135+
long ret;
136+
if (zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &array) == SUCCESS &&
137+
Z_TYPE_PP(array) == IS_ARRAY &&
138+
zend_hash_find(Z_ARRVAL_PP(array), "REQUEST_TIME", sizeof("REQUEST_TIME"), (void **) &request_time_long)
139+
== SUCCESS
140+
) {
141+
ret = Z_LVAL_PP(request_time_long);
142+
} else {
143+
ret = time(NULL);
162144
}
163-
164-
len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "timecop", arg);
165-
RETURN_STRINGL(strg, len, 0);
145+
RETURN_LONG(ret);
166146
}
167147
/* }}} */
168148
/* The previous line is meant for vim and emacs, so it can correctly fold and

timecop.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
echo $func."$br\n";
1212
}
1313
echo "$br\n";
14-
$function = 'confirm_' . $module . '_compiled';
14+
$function = 'timecop_time';
1515
if (extension_loaded($module)) {
16-
$str = $function($module);
16+
echo timecop_time()."$br\n";
17+
$_SERVER['REQUEST_TIME'] = PHP_INT_MAX;
18+
echo timecop_time()."$br\n";
1719
} else {
18-
$str = "Module $module is not compiled into PHP";
20+
echo "Module $module is not compiled into PHP";
1921
}
20-
echo "$str\n";
22+
2123
?>

0 commit comments

Comments
 (0)