Skip to content

Commit

Permalink
add timecop_time function
Browse files Browse the repository at this point in the history
  • Loading branch information
hnw committed Jun 19, 2012
1 parent d319526 commit b0c3f80
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 45 deletions.
2 changes: 1 addition & 1 deletion php_timecop.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ PHP_RINIT_FUNCTION(timecop);
PHP_RSHUTDOWN_FUNCTION(timecop);
PHP_MINFO_FUNCTION(timecop);

PHP_FUNCTION(confirm_timecop_compiled); /* For testing, remove later. */
PHP_FUNCTION(timecop_time);

/*
Declare any global variables you may need between the BEGIN
Expand Down
60 changes: 20 additions & 40 deletions timecop.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_timecop.h"
#include <time.h>

/* If you declare any globals in php_timecop.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(timecop)
Expand All @@ -39,7 +40,7 @@ static int le_timecop;
* Every user visible function must have an entry in timecop_functions[].
*/
const zend_function_entry timecop_functions[] = {
PHP_FE(confirm_timecop_compiled, NULL) /* For testing, remove later. */
PHP_FE(timecop_time, NULL)
PHP_FE_END /* Must be the last line in timecop_functions[] */
};
/* }}} */
Expand All @@ -54,11 +55,11 @@ zend_module_entry timecop_module_entry = {
timecop_functions,
PHP_MINIT(timecop),
PHP_MSHUTDOWN(timecop),
PHP_RINIT(timecop), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(timecop), /* Replace with NULL if there's nothing to do at request end */
NULL,
NULL,
PHP_MINFO(timecop),
#if ZEND_MODULE_API_NO >= 20010901
"0.1", /* Replace with version number for your extension */
"0.1",
#endif
STANDARD_MODULE_PROPERTIES
};
Expand Down Expand Up @@ -111,30 +112,12 @@ PHP_MSHUTDOWN_FUNCTION(timecop)
}
/* }}} */

/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(timecop)
{
return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(timecop)
{
return SUCCESS;
}
/* }}} */

/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(timecop)
{
php_info_print_table_start();
php_info_print_table_header(2, "timecop support", "enabled");
php_info_print_table_header(2, "timecop", "enabled");
php_info_print_table_end();

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


/* Remove the following function when you have succesfully modified config.m4
so that your module can be compiled into PHP, it exists only for testing
purposes. */

/* Every user-visible function in PHP should document itself in the source */
/* {{{ proto string confirm_timecop_compiled(string arg)
Return a string to confirm that the module is compiled in */
PHP_FUNCTION(confirm_timecop_compiled)
/* {{{ proto int timecop_time(void)
Return timestamp for $_SERVER[request_time] */
PHP_FUNCTION(timecop_time)
{
char *arg = NULL;
int arg_len, len;
char *strg;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
return;
zval **array, **request_time_long;
long ret;
if (zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &array) == SUCCESS &&
Z_TYPE_PP(array) == IS_ARRAY &&
zend_hash_find(Z_ARRVAL_PP(array), "REQUEST_TIME", sizeof("REQUEST_TIME"), (void **) &request_time_long)
== SUCCESS
) {
ret = Z_LVAL_PP(request_time_long);
} else {
ret = time(NULL);
}

len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "timecop", arg);
RETURN_STRINGL(strg, len, 0);
RETURN_LONG(ret);
}
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and
Expand Down
10 changes: 6 additions & 4 deletions timecop.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
echo $func."$br\n";
}
echo "$br\n";
$function = 'confirm_' . $module . '_compiled';
$function = 'timecop_time';
if (extension_loaded($module)) {
$str = $function($module);
echo timecop_time()."$br\n";
$_SERVER['REQUEST_TIME'] = PHP_INT_MAX;
echo timecop_time()."$br\n";
} else {
$str = "Module $module is not compiled into PHP";
echo "Module $module is not compiled into PHP";
}
echo "$str\n";

?>

0 comments on commit b0c3f80

Please sign in to comment.