Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
guolifu committed Jan 4, 2023
0 parents commit e19e6c0
Show file tree
Hide file tree
Showing 14 changed files with 934 additions and 0 deletions.
36 changes: 36 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
.idea
.deps
*.lo
*.la
.libs
acinclude.m4
aclocal.m4
autom4te.cache
build
config.guess
config.h
config.h.in
config.log
config.nice
config.status
config.sub
configure
configure.in
include
install-sh
libtool
ltmain.sh
Makefile
Makefile.fragments
Makefile.global
Makefile.objects
missing
mkinstalldirs
modules
run-tests.php
tests/*/*.diff
tests/*/*.out
tests/*/*.php
tests/*/*.exp
tests/*/*.log
tests/*/*.sh
1 change: 1 addition & 0 deletions CREDITS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
evil
Empty file added EXPERIMENTAL
Empty file.
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# evil-php
什么?PHP环境运行太过稳定,无事可做?千篇一律的日常开发毫无挑战?想体验不知何时发生未知BUG的恐惧?

把本项目引入你们的项目吧,你们的项目会有但不仅限于如下的神奇效果:

* `var_dump` 在周末时,会有10%的概率返回`NULL`
* `in_array` 当数组长度可以被7整除时,永远返回`false`
* `array_map` 在周日时,方法的结果总是会丢失最后一个元素。
* `array_filter` 返回值有50%的概率丢失最后一个元素。
* `sleep` 总是会比预期时间慢1秒才触发。
* `strtotime` 的结果总是会慢7分钟。
* `json_encode` 会把I(大写字母I)变成l(小写字母L)。
* `substr` 有50%几率返回空字符串。
* `mb_strlen` 有50%几率返回0。
* ...

## Install
### Make Install evil-php
```sh
$ /path/to/phpize
$ ./configure --with-php-config=/path/to/php-config
$ make && make install
```
### evil.ini
```conf
[evil]
extension=evil.so
```
### example
```php
<?php

var_dump(mb_strlen("123"));

var_dump(substr("abc", 0, 1));

var_dump(json_encode(["a" => "III", "I" => 2]));

var_dump(strtotime("2023-1-1"));

var_dump(in_array(1, [1, 2, 3, 4, 5, 6, 7, 10, 11, 12, "qwe" => "123"]));

var_dump(in_array(1, [1, 2, 3, 4, 5, 6, 7]));

var_dump(array_map(function ($v1, $v2) {
return $v1 + $v2;
}, [1, 2, 3, 4], [1, 2, 3, 4]));

var_dump(array_filter([1, 2, 3, 4, 5, 6, 7, 10, 11, 12, "qwe" => "123"], function ($v) {
return $v > 10 || is_string($v);
}));

function user_sleep()
{
for ($i = 0; $i < 2; $i++) {
sleep(1);
echo strtotime(date("Y:m:d H:i:s")) . PHP_EOL;
}
}

user_sleep();
```
### 严正声明⚠️

**本包仅供PHP扩展爱好者学习交流使用哦,包的作者不参与注入,因引入本包造成的损失本包作者概不负责哦。**
63 changes: 63 additions & 0 deletions config.m4
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
dnl $Id$
dnl config.m4 for extension evil

dnl Comments in this file start with the string 'dnl'.
dnl Remove where necessary. This file will not work
dnl without editing.

dnl If your extension references something external, use with:

PHP_ARG_WITH(evil, for evil support,
Make sure that the comment is aligned:
[ --with-evil Include evil support])

dnl Otherwise use enable:

PHP_ARG_ENABLE(evil, whether to enable evil support,
Make sure that the comment is aligned:
[ --enable-evil Enable evil support])

if test "$PHP_EVIL" != "no"; then
dnl Write more examples of tests here...

dnl # --with-evil -> check with-path
dnl SEARCH_PATH="/usr/local /usr" # you might want to change this
dnl SEARCH_FOR="/include/evil.h" # you most likely want to change this
dnl if test -r $PHP_EVIL/$SEARCH_FOR; then # path given as parameter
dnl EVIL_DIR=$PHP_EVIL
dnl else # search default path list
dnl AC_MSG_CHECKING([for evil files in default path])
dnl for i in $SEARCH_PATH ; do
dnl if test -r $i/$SEARCH_FOR; then
dnl EVIL_DIR=$i
dnl AC_MSG_RESULT(found in $i)
dnl fi
dnl done
dnl fi
dnl
dnl if test -z "$EVIL_DIR"; then
dnl AC_MSG_RESULT([not found])
dnl AC_MSG_ERROR([Please reinstall the evil distribution])
dnl fi

dnl # --with-evil -> add include path
dnl PHP_ADD_INCLUDE($EVIL_DIR/include)

dnl # --with-evil -> check for lib and symbol presence
dnl LIBNAME=evil # you may want to change this
dnl LIBSYMBOL=evil # you most likely want to change this

dnl PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,
dnl [
dnl PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $EVIL_DIR/$PHP_LIBDIR, EVIL_SHARED_LIBADD)
dnl AC_DEFINE(HAVE_EVILLIB,1,[ ])
dnl ],[
dnl AC_MSG_ERROR([wrong evil lib version or lib not found])
dnl ],[
dnl -L$EVIL_DIR/$PHP_LIBDIR -lm
dnl ])
dnl
dnl PHP_SUBST(EVIL_SHARED_LIBADD)

PHP_NEW_EXTENSION(evil, evil.c, $ext_shared)
fi
13 changes: 13 additions & 0 deletions config.w32
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// $Id$
// vim:ft=javascript

// If your extension references something external, use ARG_WITH
// ARG_WITH("evil", "for evil support", "no");

// Otherwise, use ARG_ENABLE
// ARG_ENABLE("evil", "enable evil support", "no");

if (PHP_EVIL != "no") {
EXTENSION("evil", "evil.c");
}

92 changes: 92 additions & 0 deletions evil.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Copyright (c) 2022-2023
* Author carl.guo
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_evil.h"
#include "tools/common.c"
#include "tools/override_func.c"

static int le_evil;

/**
* evil
* @param type
* @param module_number
* @return
*/
PHP_MINIT_FUNCTION (evil)
{
zend_hash_init(&overrideFunctions, 0, NULL, NULL, 1);
/* array */
evilRewriteFunc(PHP_FN(evil_in_array), "in_array" TSRMLS_CC);
evilRewriteFunc(PHP_FN(evil_array_map), "array_map" TSRMLS_CC);
evilRewriteFunc(PHP_FN(evil_array_filter), "array_filter" TSRMLS_CC);
/* string */
evilRewriteFunc(PHP_FN(evil_json_encode), "json_encode" TSRMLS_CC);
evilRewriteFunc(PHP_FN(evil_substr), "substr" TSRMLS_CC);
evilRewriteFunc(PHP_FN(evil_mb_strlen), "mb_strlen" TSRMLS_CC);
/* tools */
evilRewriteFunc(PHP_FN(evil_var_dump), "var_dump" TSRMLS_CC);
evilRewriteFunc(PHP_FN(evil_sleep), "sleep" TSRMLS_CC);
evilRewriteFunc(PHP_FN(evil_strtotime), "strtotime" TSRMLS_CC);

return SUCCESS;
}

PHP_MSHUTDOWN_FUNCTION (evil)
{
/* uncomment this line if you have INI entries
UNREGISTER_INI_ENTRIES();
*/
return SUCCESS;
}

PHP_RINIT_FUNCTION (evil)
{
return SUCCESS;
}

PHP_RSHUTDOWN_FUNCTION (evil)
{
return SUCCESS;
}

PHP_MINFO_FUNCTION (evil)
{
php_info_print_table_start();
php_info_print_table_header(2, "evil support", "enabled");
php_info_print_table_end();

/* Remove comments if you have entries in php.ini
DISPLAY_INI_ENTRIES();
*/
}

const zend_function_entry evil_functions[] = {
PHP_FE_END
};

zend_module_entry evil_module_entry = {
STANDARD_MODULE_HEADER,
"evil",
evil_functions,
PHP_MINIT(evil),
PHP_MSHUTDOWN(evil),
PHP_RINIT(evil),
PHP_RSHUTDOWN(evil),
PHP_MINFO(evil),
PHP_EVIL_VERSION,
STANDARD_MODULE_PROPERTIES
};

#ifdef COMPILE_DL_EVIL
ZEND_GET_MODULE(evil)
#endif
21 changes: 21 additions & 0 deletions evil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
$br = (php_sapi_name() == "cli")? "":"<br>";

if(!extension_loaded('evil')) {
dl('evil.' . PHP_SHLIB_SUFFIX);
}
$module = 'evil';
$functions = get_extension_funcs($module);
echo "Functions available in the test extension:$br\n";
foreach($functions as $func) {
echo $func."$br\n";
}
echo "$br\n";
$function = 'confirm_' . $module . '_compiled';
if (extension_loaded($module)) {
$str = $function($module);
} else {
$str = "Module $module is not compiled into PHP";
}
echo "$str\n";
?>
Loading

0 comments on commit e19e6c0

Please sign in to comment.