PageRenderTime 63ms CodeModel.GetById 2ms RepoModel.GetById 1ms app.codeStats 1ms

/Zend/zend_builtin_functions.c

http://github.com/php/php-src
C | 2237 lines | 1673 code | 305 blank | 259 comment | 496 complexity | c3cdf266208da601fe59a570619d5ad0 MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Andi Gutmans <andi@php.net> |
  16. | Zeev Suraski <zeev@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #include "zend.h"
  20. #include "zend_API.h"
  21. #include "zend_gc.h"
  22. #include "zend_builtin_functions.h"
  23. #include "zend_constants.h"
  24. #include "zend_ini.h"
  25. #include "zend_exceptions.h"
  26. #include "zend_extensions.h"
  27. #include "zend_closures.h"
  28. #include "zend_generators.h"
  29. #include "zend_builtin_functions_arginfo.h"
  30. /* }}} */
  31. ZEND_MINIT_FUNCTION(core) { /* {{{ */
  32. zend_class_entry class_entry;
  33. INIT_CLASS_ENTRY(class_entry, "stdClass", NULL);
  34. zend_standard_class_def = zend_register_internal_class(&class_entry);
  35. zend_register_default_classes();
  36. return SUCCESS;
  37. }
  38. /* }}} */
  39. zend_module_entry zend_builtin_module = { /* {{{ */
  40. STANDARD_MODULE_HEADER,
  41. "Core",
  42. ext_functions,
  43. ZEND_MINIT(core),
  44. NULL,
  45. NULL,
  46. NULL,
  47. NULL,
  48. ZEND_VERSION,
  49. STANDARD_MODULE_PROPERTIES
  50. };
  51. /* }}} */
  52. int zend_startup_builtin_functions(void) /* {{{ */
  53. {
  54. zend_builtin_module.module_number = 0;
  55. zend_builtin_module.type = MODULE_PERSISTENT;
  56. return (EG(current_module) = zend_register_module_ex(&zend_builtin_module)) == NULL ? FAILURE : SUCCESS;
  57. }
  58. /* }}} */
  59. /* {{{ proto string zend_version(void)
  60. Get the version of the Zend Engine */
  61. ZEND_FUNCTION(zend_version)
  62. {
  63. ZEND_PARSE_PARAMETERS_NONE();
  64. RETURN_STRINGL(ZEND_VERSION, sizeof(ZEND_VERSION)-1);
  65. }
  66. /* }}} */
  67. /* {{{ proto int gc_mem_caches(void)
  68. Reclaims memory used by MM caches.
  69. Returns number of freed bytes */
  70. ZEND_FUNCTION(gc_mem_caches)
  71. {
  72. ZEND_PARSE_PARAMETERS_NONE();
  73. RETURN_LONG(zend_mm_gc(zend_mm_get_heap()));
  74. }
  75. /* }}} */
  76. /* {{{ proto int gc_collect_cycles(void)
  77. Forces collection of any existing garbage cycles.
  78. Returns number of freed zvals */
  79. ZEND_FUNCTION(gc_collect_cycles)
  80. {
  81. ZEND_PARSE_PARAMETERS_NONE();
  82. RETURN_LONG(gc_collect_cycles());
  83. }
  84. /* }}} */
  85. /* {{{ proto void gc_enabled(void)
  86. Returns status of the circular reference collector */
  87. ZEND_FUNCTION(gc_enabled)
  88. {
  89. ZEND_PARSE_PARAMETERS_NONE();
  90. RETURN_BOOL(gc_enabled());
  91. }
  92. /* }}} */
  93. /* {{{ proto void gc_enable(void)
  94. Activates the circular reference collector */
  95. ZEND_FUNCTION(gc_enable)
  96. {
  97. zend_string *key;
  98. ZEND_PARSE_PARAMETERS_NONE();
  99. key = zend_string_init("zend.enable_gc", sizeof("zend.enable_gc")-1, 0);
  100. zend_alter_ini_entry_chars(key, "1", sizeof("1")-1, ZEND_INI_USER, ZEND_INI_STAGE_RUNTIME);
  101. zend_string_release_ex(key, 0);
  102. }
  103. /* }}} */
  104. /* {{{ proto void gc_disable(void)
  105. Deactivates the circular reference collector */
  106. ZEND_FUNCTION(gc_disable)
  107. {
  108. zend_string *key;
  109. ZEND_PARSE_PARAMETERS_NONE();
  110. key = zend_string_init("zend.enable_gc", sizeof("zend.enable_gc")-1, 0);
  111. zend_alter_ini_entry_chars(key, "0", sizeof("0")-1, ZEND_INI_USER, ZEND_INI_STAGE_RUNTIME);
  112. zend_string_release_ex(key, 0);
  113. }
  114. /* }}} */
  115. /* {{{ proto array gc_status(void)
  116. Returns current GC statistics */
  117. ZEND_FUNCTION(gc_status)
  118. {
  119. zend_gc_status status;
  120. ZEND_PARSE_PARAMETERS_NONE();
  121. zend_gc_get_status(&status);
  122. array_init_size(return_value, 3);
  123. add_assoc_long_ex(return_value, "runs", sizeof("runs")-1, (long)status.runs);
  124. add_assoc_long_ex(return_value, "collected", sizeof("collected")-1, (long)status.collected);
  125. add_assoc_long_ex(return_value, "threshold", sizeof("threshold")-1, (long)status.threshold);
  126. add_assoc_long_ex(return_value, "roots", sizeof("roots")-1, (long)status.num_roots);
  127. }
  128. /* }}} */
  129. /* {{{ proto int func_num_args(void)
  130. Get the number of arguments that were passed to the function */
  131. ZEND_FUNCTION(func_num_args)
  132. {
  133. zend_execute_data *ex = EX(prev_execute_data);
  134. ZEND_PARSE_PARAMETERS_NONE();
  135. if (ZEND_CALL_INFO(ex) & ZEND_CALL_CODE) {
  136. zend_error(E_WARNING, "func_num_args(): Called from the global scope - no function context");
  137. RETURN_LONG(-1);
  138. }
  139. if (zend_forbid_dynamic_call("func_num_args()") == FAILURE) {
  140. RETURN_LONG(-1);
  141. }
  142. RETURN_LONG(ZEND_CALL_NUM_ARGS(ex));
  143. }
  144. /* }}} */
  145. /* {{{ proto mixed func_get_arg(int arg_num)
  146. Get the $arg_num'th argument that was passed to the function */
  147. ZEND_FUNCTION(func_get_arg)
  148. {
  149. uint32_t arg_count, first_extra_arg;
  150. zval *arg;
  151. zend_long requested_offset;
  152. zend_execute_data *ex;
  153. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &requested_offset) == FAILURE) {
  154. RETURN_THROWS();
  155. }
  156. if (requested_offset < 0) {
  157. zend_argument_value_error(1, "must be greater than or equal to 0");
  158. RETURN_THROWS();
  159. }
  160. ex = EX(prev_execute_data);
  161. if (ZEND_CALL_INFO(ex) & ZEND_CALL_CODE) {
  162. zend_throw_error(NULL, "func_get_arg() cannot be called from the global scope");
  163. RETURN_THROWS();
  164. }
  165. if (zend_forbid_dynamic_call("func_get_arg()") == FAILURE) {
  166. RETURN_THROWS();
  167. }
  168. arg_count = ZEND_CALL_NUM_ARGS(ex);
  169. if ((zend_ulong)requested_offset >= arg_count) {
  170. zend_throw_error(NULL, "func_get_arg(): Argument " ZEND_LONG_FMT " not passed to function", requested_offset);
  171. RETURN_THROWS();
  172. }
  173. first_extra_arg = ex->func->op_array.num_args;
  174. if ((zend_ulong)requested_offset >= first_extra_arg && (ZEND_CALL_NUM_ARGS(ex) > first_extra_arg)) {
  175. arg = ZEND_CALL_VAR_NUM(ex, ex->func->op_array.last_var + ex->func->op_array.T) + (requested_offset - first_extra_arg);
  176. } else {
  177. arg = ZEND_CALL_ARG(ex, requested_offset + 1);
  178. }
  179. if (EXPECTED(!Z_ISUNDEF_P(arg))) {
  180. ZVAL_COPY_DEREF(return_value, arg);
  181. }
  182. }
  183. /* }}} */
  184. /* {{{ proto array func_get_args()
  185. Get an array of the arguments that were passed to the function */
  186. ZEND_FUNCTION(func_get_args)
  187. {
  188. zval *p, *q;
  189. uint32_t arg_count, first_extra_arg;
  190. uint32_t i;
  191. zend_execute_data *ex = EX(prev_execute_data);
  192. if (ZEND_CALL_INFO(ex) & ZEND_CALL_CODE) {
  193. zend_throw_error(NULL, "func_get_args() cannot be called from the global scope");
  194. RETURN_THROWS();
  195. }
  196. if (zend_forbid_dynamic_call("func_get_args()") == FAILURE) {
  197. RETURN_THROWS();
  198. }
  199. arg_count = ZEND_CALL_NUM_ARGS(ex);
  200. if (arg_count) {
  201. array_init_size(return_value, arg_count);
  202. first_extra_arg = ex->func->op_array.num_args;
  203. zend_hash_real_init_packed(Z_ARRVAL_P(return_value));
  204. ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(return_value)) {
  205. i = 0;
  206. p = ZEND_CALL_ARG(ex, 1);
  207. if (arg_count > first_extra_arg) {
  208. while (i < first_extra_arg) {
  209. q = p;
  210. if (EXPECTED(Z_TYPE_INFO_P(q) != IS_UNDEF)) {
  211. ZVAL_DEREF(q);
  212. if (Z_OPT_REFCOUNTED_P(q)) {
  213. Z_ADDREF_P(q);
  214. }
  215. ZEND_HASH_FILL_SET(q);
  216. } else {
  217. ZEND_HASH_FILL_SET_NULL();
  218. }
  219. ZEND_HASH_FILL_NEXT();
  220. p++;
  221. i++;
  222. }
  223. p = ZEND_CALL_VAR_NUM(ex, ex->func->op_array.last_var + ex->func->op_array.T);
  224. }
  225. while (i < arg_count) {
  226. q = p;
  227. if (EXPECTED(Z_TYPE_INFO_P(q) != IS_UNDEF)) {
  228. ZVAL_DEREF(q);
  229. if (Z_OPT_REFCOUNTED_P(q)) {
  230. Z_ADDREF_P(q);
  231. }
  232. ZEND_HASH_FILL_SET(q);
  233. } else {
  234. ZEND_HASH_FILL_SET_NULL();
  235. }
  236. ZEND_HASH_FILL_NEXT();
  237. p++;
  238. i++;
  239. }
  240. } ZEND_HASH_FILL_END();
  241. Z_ARRVAL_P(return_value)->nNumOfElements = arg_count;
  242. } else {
  243. RETURN_EMPTY_ARRAY();
  244. }
  245. }
  246. /* }}} */
  247. /* {{{ proto int strlen(string str)
  248. Get string length
  249. Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
  250. ZEND_FUNCTION(strlen)
  251. {
  252. zend_string *s;
  253. ZEND_PARSE_PARAMETERS_START(1, 1)
  254. Z_PARAM_STR(s)
  255. ZEND_PARSE_PARAMETERS_END();
  256. RETVAL_LONG(ZSTR_LEN(s));
  257. }
  258. /* }}} */
  259. /* {{{ proto int strcmp(string str1, string str2)
  260. Binary safe string comparison */
  261. ZEND_FUNCTION(strcmp)
  262. {
  263. zend_string *s1, *s2;
  264. ZEND_PARSE_PARAMETERS_START(2, 2)
  265. Z_PARAM_STR(s1)
  266. Z_PARAM_STR(s2)
  267. ZEND_PARSE_PARAMETERS_END();
  268. RETURN_LONG(zend_binary_strcmp(ZSTR_VAL(s1), ZSTR_LEN(s1), ZSTR_VAL(s2), ZSTR_LEN(s2)));
  269. }
  270. /* }}} */
  271. /* {{{ proto int strncmp(string str1, string str2, int len)
  272. Binary safe string comparison */
  273. ZEND_FUNCTION(strncmp)
  274. {
  275. zend_string *s1, *s2;
  276. zend_long len;
  277. ZEND_PARSE_PARAMETERS_START(3, 3)
  278. Z_PARAM_STR(s1)
  279. Z_PARAM_STR(s2)
  280. Z_PARAM_LONG(len)
  281. ZEND_PARSE_PARAMETERS_END();
  282. if (len < 0) {
  283. zend_argument_value_error(3, "must be greater than or equal to 0");
  284. RETURN_THROWS();
  285. }
  286. RETURN_LONG(zend_binary_strncmp(ZSTR_VAL(s1), ZSTR_LEN(s1), ZSTR_VAL(s2), ZSTR_LEN(s2), len));
  287. }
  288. /* }}} */
  289. /* {{{ proto int strcasecmp(string str1, string str2)
  290. Binary safe case-insensitive string comparison */
  291. ZEND_FUNCTION(strcasecmp)
  292. {
  293. zend_string *s1, *s2;
  294. ZEND_PARSE_PARAMETERS_START(2, 2)
  295. Z_PARAM_STR(s1)
  296. Z_PARAM_STR(s2)
  297. ZEND_PARSE_PARAMETERS_END();
  298. RETURN_LONG(zend_binary_strcasecmp(ZSTR_VAL(s1), ZSTR_LEN(s1), ZSTR_VAL(s2), ZSTR_LEN(s2)));
  299. }
  300. /* }}} */
  301. /* {{{ proto int strncasecmp(string str1, string str2, int len)
  302. Binary safe string comparison */
  303. ZEND_FUNCTION(strncasecmp)
  304. {
  305. zend_string *s1, *s2;
  306. zend_long len;
  307. ZEND_PARSE_PARAMETERS_START(3, 3)
  308. Z_PARAM_STR(s1)
  309. Z_PARAM_STR(s2)
  310. Z_PARAM_LONG(len)
  311. ZEND_PARSE_PARAMETERS_END();
  312. if (len < 0) {
  313. zend_argument_value_error(3, "must be greater than or equal to 0");
  314. RETURN_THROWS();
  315. }
  316. RETURN_LONG(zend_binary_strncasecmp(ZSTR_VAL(s1), ZSTR_LEN(s1), ZSTR_VAL(s2), ZSTR_LEN(s2), len));
  317. }
  318. /* }}} */
  319. /* {{{ proto int error_reporting([int new_error_level])
  320. Return the current error_reporting level, and if an argument was passed - change to the new level */
  321. ZEND_FUNCTION(error_reporting)
  322. {
  323. zval *err = NULL;
  324. int old_error_reporting;
  325. ZEND_PARSE_PARAMETERS_START(0, 1)
  326. Z_PARAM_OPTIONAL
  327. Z_PARAM_ZVAL(err)
  328. ZEND_PARSE_PARAMETERS_END();
  329. old_error_reporting = EG(error_reporting);
  330. if (ZEND_NUM_ARGS() != 0) {
  331. zend_string *new_val = zval_try_get_string(err);
  332. if (UNEXPECTED(!new_val)) {
  333. RETURN_THROWS();
  334. }
  335. do {
  336. zend_ini_entry *p = EG(error_reporting_ini_entry);
  337. if (!p) {
  338. zval *zv = zend_hash_find_ex(EG(ini_directives), ZSTR_KNOWN(ZEND_STR_ERROR_REPORTING), 1);
  339. if (zv) {
  340. p = EG(error_reporting_ini_entry) = (zend_ini_entry*)Z_PTR_P(zv);
  341. } else {
  342. break;
  343. }
  344. }
  345. if (!p->modified) {
  346. if (!EG(modified_ini_directives)) {
  347. ALLOC_HASHTABLE(EG(modified_ini_directives));
  348. zend_hash_init(EG(modified_ini_directives), 8, NULL, NULL, 0);
  349. }
  350. if (EXPECTED(zend_hash_add_ptr(EG(modified_ini_directives), ZSTR_KNOWN(ZEND_STR_ERROR_REPORTING), p) != NULL)) {
  351. p->orig_value = p->value;
  352. p->orig_modifiable = p->modifiable;
  353. p->modified = 1;
  354. }
  355. } else if (p->orig_value != p->value) {
  356. zend_string_release_ex(p->value, 0);
  357. }
  358. p->value = new_val;
  359. if (Z_TYPE_P(err) == IS_LONG) {
  360. EG(error_reporting) = Z_LVAL_P(err);
  361. } else {
  362. EG(error_reporting) = atoi(ZSTR_VAL(p->value));
  363. }
  364. } while (0);
  365. }
  366. RETVAL_LONG(old_error_reporting);
  367. }
  368. /* }}} */
  369. static int validate_constant_array(HashTable *ht) /* {{{ */
  370. {
  371. int ret = 1;
  372. zval *val;
  373. GC_PROTECT_RECURSION(ht);
  374. ZEND_HASH_FOREACH_VAL_IND(ht, val) {
  375. ZVAL_DEREF(val);
  376. if (Z_REFCOUNTED_P(val)) {
  377. if (Z_TYPE_P(val) == IS_ARRAY) {
  378. if (Z_REFCOUNTED_P(val)) {
  379. if (Z_IS_RECURSIVE_P(val)) {
  380. zend_error(E_WARNING, "Constants cannot be recursive arrays");
  381. ret = 0;
  382. break;
  383. } else if (!validate_constant_array(Z_ARRVAL_P(val))) {
  384. ret = 0;
  385. break;
  386. }
  387. }
  388. } else if (Z_TYPE_P(val) != IS_STRING && Z_TYPE_P(val) != IS_RESOURCE) {
  389. zend_error(E_WARNING, "Constants may only evaluate to scalar values, arrays or resources");
  390. ret = 0;
  391. break;
  392. }
  393. }
  394. } ZEND_HASH_FOREACH_END();
  395. GC_UNPROTECT_RECURSION(ht);
  396. return ret;
  397. }
  398. /* }}} */
  399. static void copy_constant_array(zval *dst, zval *src) /* {{{ */
  400. {
  401. zend_string *key;
  402. zend_ulong idx;
  403. zval *new_val, *val;
  404. array_init_size(dst, zend_hash_num_elements(Z_ARRVAL_P(src)));
  405. ZEND_HASH_FOREACH_KEY_VAL_IND(Z_ARRVAL_P(src), idx, key, val) {
  406. /* constant arrays can't contain references */
  407. ZVAL_DEREF(val);
  408. if (key) {
  409. new_val = zend_hash_add_new(Z_ARRVAL_P(dst), key, val);
  410. } else {
  411. new_val = zend_hash_index_add_new(Z_ARRVAL_P(dst), idx, val);
  412. }
  413. if (Z_TYPE_P(val) == IS_ARRAY) {
  414. if (Z_REFCOUNTED_P(val)) {
  415. copy_constant_array(new_val, val);
  416. }
  417. } else {
  418. Z_TRY_ADDREF_P(val);
  419. }
  420. } ZEND_HASH_FOREACH_END();
  421. }
  422. /* }}} */
  423. /* {{{ proto bool define(string constant_name, mixed value[, bool case_insensitive])
  424. Define a new constant */
  425. ZEND_FUNCTION(define)
  426. {
  427. zend_string *name;
  428. zval *val, val_free;
  429. zend_bool non_cs = 0;
  430. zend_constant c;
  431. ZEND_PARSE_PARAMETERS_START(2, 3)
  432. Z_PARAM_STR(name)
  433. Z_PARAM_ZVAL(val)
  434. Z_PARAM_OPTIONAL
  435. Z_PARAM_BOOL(non_cs)
  436. ZEND_PARSE_PARAMETERS_END();
  437. if (zend_memnstr(ZSTR_VAL(name), "::", sizeof("::") - 1, ZSTR_VAL(name) + ZSTR_LEN(name))) {
  438. zend_error(E_WARNING, "Class constants cannot be defined or redefined");
  439. RETURN_FALSE;
  440. }
  441. if (non_cs) {
  442. zend_error(E_WARNING,
  443. "define(): Declaration of case-insensitive constants is no longer supported");
  444. RETURN_FALSE;
  445. }
  446. ZVAL_UNDEF(&val_free);
  447. switch (Z_TYPE_P(val)) {
  448. case IS_LONG:
  449. case IS_DOUBLE:
  450. case IS_STRING:
  451. case IS_FALSE:
  452. case IS_TRUE:
  453. case IS_NULL:
  454. case IS_RESOURCE:
  455. break;
  456. case IS_ARRAY:
  457. if (Z_REFCOUNTED_P(val)) {
  458. if (!validate_constant_array(Z_ARRVAL_P(val))) {
  459. RETURN_FALSE;
  460. } else {
  461. copy_constant_array(&c.value, val);
  462. goto register_constant;
  463. }
  464. }
  465. break;
  466. case IS_OBJECT:
  467. if (Z_OBJ_HT_P(val)->cast_object(Z_OBJ_P(val), &val_free, IS_STRING) == SUCCESS) {
  468. val = &val_free;
  469. break;
  470. }
  471. /* no break */
  472. default:
  473. zend_error(E_WARNING, "Constants may only evaluate to scalar values, arrays or resources");
  474. zval_ptr_dtor(&val_free);
  475. RETURN_FALSE;
  476. }
  477. ZVAL_COPY(&c.value, val);
  478. zval_ptr_dtor(&val_free);
  479. register_constant:
  480. /* non persistent */
  481. ZEND_CONSTANT_SET_FLAGS(&c, CONST_CS, PHP_USER_CONSTANT);
  482. c.name = zend_string_copy(name);
  483. if (zend_register_constant(&c) == SUCCESS) {
  484. RETURN_TRUE;
  485. } else {
  486. RETURN_FALSE;
  487. }
  488. }
  489. /* }}} */
  490. /* {{{ proto bool defined(string constant_name)
  491. Check whether a constant exists
  492. Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
  493. ZEND_FUNCTION(defined)
  494. {
  495. zend_string *name;
  496. ZEND_PARSE_PARAMETERS_START(1, 1)
  497. Z_PARAM_STR(name)
  498. ZEND_PARSE_PARAMETERS_END();
  499. if (zend_get_constant_ex(name, zend_get_executed_scope(), ZEND_FETCH_CLASS_SILENT)) {
  500. RETURN_TRUE;
  501. } else {
  502. RETURN_FALSE;
  503. }
  504. }
  505. /* }}} */
  506. /* {{{ proto string get_class([object object])
  507. Retrieves the class name */
  508. ZEND_FUNCTION(get_class)
  509. {
  510. zval *obj = NULL;
  511. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|o", &obj) == FAILURE) {
  512. RETURN_THROWS();
  513. }
  514. if (!obj) {
  515. zend_class_entry *scope = zend_get_executed_scope();
  516. if (scope) {
  517. RETURN_STR_COPY(scope->name);
  518. } else {
  519. zend_throw_error(NULL, "get_class() without arguments must be called from within a class");
  520. RETURN_THROWS();
  521. }
  522. }
  523. RETURN_STR_COPY(Z_OBJCE_P(obj)->name);
  524. }
  525. /* }}} */
  526. /* {{{ proto string get_called_class()
  527. Retrieves the "Late Static Binding" class name */
  528. ZEND_FUNCTION(get_called_class)
  529. {
  530. zend_class_entry *called_scope;
  531. ZEND_PARSE_PARAMETERS_NONE();
  532. called_scope = zend_get_called_scope(execute_data);
  533. if (!called_scope) {
  534. zend_throw_error(NULL, "get_called_class() must be called from within a class");
  535. RETURN_THROWS();
  536. }
  537. RETURN_STR_COPY(called_scope->name);
  538. }
  539. /* }}} */
  540. /* {{{ proto mixed get_parent_class([mixed object])
  541. Retrieves the parent class name for object or class or current scope or false if not in a scope. */
  542. ZEND_FUNCTION(get_parent_class)
  543. {
  544. zval *arg;
  545. zend_class_entry *ce = NULL;
  546. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|z", &arg) == FAILURE) {
  547. RETURN_THROWS();
  548. }
  549. if (!ZEND_NUM_ARGS()) {
  550. ce = zend_get_executed_scope();
  551. if (ce && ce->parent) {
  552. RETURN_STR_COPY(ce->parent->name);
  553. } else {
  554. RETURN_FALSE;
  555. }
  556. }
  557. if (Z_TYPE_P(arg) == IS_OBJECT) {
  558. ce = Z_OBJ_P(arg)->ce;
  559. } else if (Z_TYPE_P(arg) == IS_STRING) {
  560. ce = zend_lookup_class(Z_STR_P(arg));
  561. }
  562. if (ce && ce->parent) {
  563. RETURN_STR_COPY(ce->parent->name);
  564. } else {
  565. RETURN_FALSE;
  566. }
  567. }
  568. /* }}} */
  569. static void is_a_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool only_subclass) /* {{{ */
  570. {
  571. zval *obj;
  572. zend_string *class_name;
  573. zend_class_entry *instance_ce;
  574. zend_class_entry *ce;
  575. zend_bool allow_string = only_subclass;
  576. zend_bool retval;
  577. ZEND_PARSE_PARAMETERS_START(2, 3)
  578. Z_PARAM_ZVAL(obj)
  579. Z_PARAM_STR(class_name)
  580. Z_PARAM_OPTIONAL
  581. Z_PARAM_BOOL(allow_string)
  582. ZEND_PARSE_PARAMETERS_END();
  583. /*
  584. * allow_string - is_a default is no, is_subclass_of is yes.
  585. * if it's allowed, then the autoloader will be called if the class does not exist.
  586. * default behaviour is different, as 'is_a' used to be used to test mixed return values
  587. * and there is no easy way to deprecate this.
  588. */
  589. if (allow_string && Z_TYPE_P(obj) == IS_STRING) {
  590. instance_ce = zend_lookup_class(Z_STR_P(obj));
  591. if (!instance_ce) {
  592. RETURN_FALSE;
  593. }
  594. } else if (Z_TYPE_P(obj) == IS_OBJECT) {
  595. instance_ce = Z_OBJCE_P(obj);
  596. } else {
  597. RETURN_FALSE;
  598. }
  599. if (!only_subclass && EXPECTED(zend_string_equals(instance_ce->name, class_name))) {
  600. retval = 1;
  601. } else {
  602. ce = zend_lookup_class_ex(class_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
  603. if (!ce) {
  604. retval = 0;
  605. } else {
  606. if (only_subclass && instance_ce == ce) {
  607. retval = 0;
  608. } else {
  609. retval = instanceof_function(instance_ce, ce);
  610. }
  611. }
  612. }
  613. RETURN_BOOL(retval);
  614. }
  615. /* }}} */
  616. /* {{{ proto bool is_subclass_of(mixed object_or_string, string class_name [, bool allow_string])
  617. Returns true if the object has this class as one of its parents */
  618. ZEND_FUNCTION(is_subclass_of)
  619. {
  620. is_a_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  621. }
  622. /* }}} */
  623. /* {{{ proto bool is_a(mixed object_or_string, string class_name [, bool allow_string])
  624. Returns true if the first argument is an object and is this class or has this class as one of its parents, */
  625. ZEND_FUNCTION(is_a)
  626. {
  627. is_a_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  628. }
  629. /* }}} */
  630. /* {{{ add_class_vars */
  631. static void add_class_vars(zend_class_entry *scope, zend_class_entry *ce, int statics, zval *return_value)
  632. {
  633. zend_property_info *prop_info;
  634. zval *prop, prop_copy;
  635. zend_string *key;
  636. ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->properties_info, key, prop_info) {
  637. if (((prop_info->flags & ZEND_ACC_PROTECTED) &&
  638. !zend_check_protected(prop_info->ce, scope)) ||
  639. ((prop_info->flags & ZEND_ACC_PRIVATE) &&
  640. prop_info->ce != scope)) {
  641. continue;
  642. }
  643. prop = NULL;
  644. if (statics && (prop_info->flags & ZEND_ACC_STATIC) != 0) {
  645. prop = &ce->default_static_members_table[prop_info->offset];
  646. ZVAL_DEINDIRECT(prop);
  647. } else if (!statics && (prop_info->flags & ZEND_ACC_STATIC) == 0) {
  648. prop = &ce->default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
  649. }
  650. if (!prop) {
  651. continue;
  652. }
  653. if (Z_ISUNDEF_P(prop)) {
  654. /* Return uninitialized typed properties as a null value */
  655. ZVAL_NULL(&prop_copy);
  656. } else {
  657. /* copy: enforce read only access */
  658. ZVAL_COPY_OR_DUP(&prop_copy, prop);
  659. }
  660. prop = &prop_copy;
  661. /* this is necessary to make it able to work with default array
  662. * properties, returned to user */
  663. if (Z_OPT_TYPE_P(prop) == IS_CONSTANT_AST) {
  664. if (UNEXPECTED(zval_update_constant_ex(prop, NULL) != SUCCESS)) {
  665. return;
  666. }
  667. }
  668. zend_hash_add_new(Z_ARRVAL_P(return_value), key, prop);
  669. } ZEND_HASH_FOREACH_END();
  670. }
  671. /* }}} */
  672. /* {{{ proto array get_class_vars(string class_name)
  673. Returns an array of default properties of the class. */
  674. ZEND_FUNCTION(get_class_vars)
  675. {
  676. zend_string *class_name;
  677. zend_class_entry *ce, *scope;
  678. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &class_name) == FAILURE) {
  679. RETURN_THROWS();
  680. }
  681. ce = zend_lookup_class(class_name);
  682. if (!ce) {
  683. RETURN_FALSE;
  684. } else {
  685. array_init(return_value);
  686. if (UNEXPECTED(!(ce->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
  687. if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
  688. return;
  689. }
  690. }
  691. scope = zend_get_executed_scope();
  692. add_class_vars(scope, ce, 0, return_value);
  693. add_class_vars(scope, ce, 1, return_value);
  694. }
  695. }
  696. /* }}} */
  697. /* {{{ proto array get_object_vars(object obj)
  698. Returns an array of object properties */
  699. ZEND_FUNCTION(get_object_vars)
  700. {
  701. zval *obj;
  702. zval *value;
  703. HashTable *properties;
  704. zend_string *key;
  705. zend_object *zobj;
  706. zend_ulong num_key;
  707. ZEND_PARSE_PARAMETERS_START(1, 1)
  708. Z_PARAM_OBJECT(obj)
  709. ZEND_PARSE_PARAMETERS_END();
  710. zobj = Z_OBJ_P(obj);
  711. properties = zobj->handlers->get_properties(zobj);
  712. if (properties == NULL) {
  713. RETURN_EMPTY_ARRAY();
  714. }
  715. if (!zobj->ce->default_properties_count && properties == zobj->properties && !GC_IS_RECURSIVE(properties)) {
  716. /* fast copy */
  717. if (EXPECTED(zobj->handlers == &std_object_handlers)) {
  718. RETURN_ARR(zend_proptable_to_symtable(properties, 0));
  719. }
  720. RETURN_ARR(zend_proptable_to_symtable(properties, 1));
  721. } else {
  722. array_init_size(return_value, zend_hash_num_elements(properties));
  723. ZEND_HASH_FOREACH_KEY_VAL(properties, num_key, key, value) {
  724. zend_bool is_dynamic = 1;
  725. if (Z_TYPE_P(value) == IS_INDIRECT) {
  726. value = Z_INDIRECT_P(value);
  727. if (UNEXPECTED(Z_ISUNDEF_P(value))) {
  728. continue;
  729. }
  730. is_dynamic = 0;
  731. }
  732. if (key && zend_check_property_access(zobj, key, is_dynamic) == FAILURE) {
  733. continue;
  734. }
  735. if (Z_ISREF_P(value) && Z_REFCOUNT_P(value) == 1) {
  736. value = Z_REFVAL_P(value);
  737. }
  738. Z_TRY_ADDREF_P(value);
  739. if (UNEXPECTED(!key)) {
  740. /* This case is only possible due to loopholes, e.g. ArrayObject */
  741. zend_hash_index_add(Z_ARRVAL_P(return_value), num_key, value);
  742. } else if (!is_dynamic && ZSTR_VAL(key)[0] == 0) {
  743. const char *prop_name, *class_name;
  744. size_t prop_len;
  745. zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_len);
  746. /* We assume here that a mangled property name is never
  747. * numeric. This is probably a safe assumption, but
  748. * theoretically someone might write an extension with
  749. * private, numeric properties. Well, too bad.
  750. */
  751. zend_hash_str_add_new(Z_ARRVAL_P(return_value), prop_name, prop_len, value);
  752. } else {
  753. zend_symtable_add_new(Z_ARRVAL_P(return_value), key, value);
  754. }
  755. } ZEND_HASH_FOREACH_END();
  756. }
  757. }
  758. /* }}} */
  759. /* {{{ proto array get_mangled_object_vars(object obj)
  760. Returns an array of mangled object properties. Does not respect property visibility. */
  761. ZEND_FUNCTION(get_mangled_object_vars)
  762. {
  763. zval *obj;
  764. HashTable *properties;
  765. ZEND_PARSE_PARAMETERS_START(1, 1)
  766. Z_PARAM_OBJECT(obj)
  767. ZEND_PARSE_PARAMETERS_END();
  768. properties = Z_OBJ_HT_P(obj)->get_properties(Z_OBJ_P(obj));
  769. if (!properties) {
  770. ZVAL_EMPTY_ARRAY(return_value);
  771. return;
  772. }
  773. properties = zend_proptable_to_symtable(properties,
  774. (Z_OBJCE_P(obj)->default_properties_count ||
  775. Z_OBJ_P(obj)->handlers != &std_object_handlers ||
  776. GC_IS_RECURSIVE(properties)));
  777. RETURN_ARR(properties);
  778. }
  779. /* }}} */
  780. static int same_name(zend_string *key, zend_string *name) /* {{{ */
  781. {
  782. zend_string *lcname;
  783. int ret;
  784. if (key == name) {
  785. return 1;
  786. }
  787. if (ZSTR_LEN(key) != ZSTR_LEN(name)) {
  788. return 0;
  789. }
  790. lcname = zend_string_tolower(name);
  791. ret = memcmp(ZSTR_VAL(lcname), ZSTR_VAL(key), ZSTR_LEN(key)) == 0;
  792. zend_string_release_ex(lcname, 0);
  793. return ret;
  794. }
  795. /* }}} */
  796. /* {{{ proto array get_class_methods(mixed class)
  797. Returns an array of method names for class or class instance. */
  798. ZEND_FUNCTION(get_class_methods)
  799. {
  800. zval *klass;
  801. zval method_name;
  802. zend_class_entry *ce = NULL;
  803. zend_class_entry *scope;
  804. zend_function *mptr;
  805. if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &klass) == FAILURE) {
  806. RETURN_THROWS();
  807. }
  808. if (Z_TYPE_P(klass) == IS_OBJECT) {
  809. ce = Z_OBJCE_P(klass);
  810. } else if (Z_TYPE_P(klass) == IS_STRING) {
  811. ce = zend_lookup_class(Z_STR_P(klass));
  812. }
  813. if (!ce) {
  814. RETURN_NULL();
  815. }
  816. array_init(return_value);
  817. scope = zend_get_executed_scope();
  818. ZEND_HASH_FOREACH_PTR(&ce->function_table, mptr) {
  819. if ((mptr->common.fn_flags & ZEND_ACC_PUBLIC)
  820. || (scope &&
  821. (((mptr->common.fn_flags & ZEND_ACC_PROTECTED) &&
  822. zend_check_protected(mptr->common.scope, scope))
  823. || ((mptr->common.fn_flags & ZEND_ACC_PRIVATE) &&
  824. scope == mptr->common.scope)))
  825. ) {
  826. ZVAL_STR_COPY(&method_name, mptr->common.function_name);
  827. zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &method_name);
  828. }
  829. } ZEND_HASH_FOREACH_END();
  830. }
  831. /* }}} */
  832. /* {{{ proto bool method_exists(object object, string method)
  833. Checks if the class method exists */
  834. ZEND_FUNCTION(method_exists)
  835. {
  836. zval *klass;
  837. zend_string *method_name;
  838. zend_string *lcname;
  839. zend_class_entry *ce;
  840. zend_function *func;
  841. ZEND_PARSE_PARAMETERS_START(2, 2)
  842. Z_PARAM_ZVAL(klass)
  843. Z_PARAM_STR(method_name)
  844. ZEND_PARSE_PARAMETERS_END();
  845. if (Z_TYPE_P(klass) == IS_OBJECT) {
  846. ce = Z_OBJCE_P(klass);
  847. } else if (Z_TYPE_P(klass) == IS_STRING) {
  848. if ((ce = zend_lookup_class(Z_STR_P(klass))) == NULL) {
  849. RETURN_FALSE;
  850. }
  851. } else {
  852. zend_argument_type_error(1, "must be of type object|string, %s given", zend_zval_type_name(klass));
  853. RETURN_THROWS();
  854. }
  855. lcname = zend_string_tolower(method_name);
  856. func = zend_hash_find_ptr(&ce->function_table, lcname);
  857. zend_string_release_ex(lcname, 0);
  858. if (func) {
  859. /* Exclude shadow properties when checking a method on a specific class. Include
  860. * them when checking an object, as method_exists() generally ignores visibility.
  861. * TODO: Should we use EG(scope) for the object case instead? */
  862. RETURN_BOOL(Z_TYPE_P(klass) == IS_OBJECT
  863. || !(func->common.fn_flags & ZEND_ACC_PRIVATE) || func->common.scope == ce);
  864. }
  865. if (Z_TYPE_P(klass) == IS_OBJECT) {
  866. zend_object *obj = Z_OBJ_P(klass);
  867. func = Z_OBJ_HT_P(klass)->get_method(&obj, method_name, NULL);
  868. if (func != NULL) {
  869. if (func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
  870. /* Returns true to the fake Closure's __invoke */
  871. RETVAL_BOOL(func->common.scope == zend_ce_closure
  872. && zend_string_equals_literal(method_name, ZEND_INVOKE_FUNC_NAME));
  873. zend_string_release_ex(func->common.function_name, 0);
  874. zend_free_trampoline(func);
  875. return;
  876. }
  877. RETURN_TRUE;
  878. }
  879. }
  880. RETURN_FALSE;
  881. }
  882. /* }}} */
  883. /* {{{ proto bool property_exists(mixed object_or_class, string property_name)
  884. Checks if the object or class has a property */
  885. ZEND_FUNCTION(property_exists)
  886. {
  887. zval *object;
  888. zend_string *property;
  889. zend_class_entry *ce;
  890. zend_property_info *property_info;
  891. if (zend_parse_parameters(ZEND_NUM_ARGS(), "zS", &object, &property) == FAILURE) {
  892. RETURN_THROWS();
  893. }
  894. if (Z_TYPE_P(object) == IS_STRING) {
  895. ce = zend_lookup_class(Z_STR_P(object));
  896. if (!ce) {
  897. RETURN_FALSE;
  898. }
  899. } else if (Z_TYPE_P(object) == IS_OBJECT) {
  900. ce = Z_OBJCE_P(object);
  901. } else {
  902. zend_argument_type_error(1, "must be of type object|string, %s given", zend_zval_type_name(object));
  903. RETURN_THROWS();
  904. }
  905. property_info = zend_hash_find_ptr(&ce->properties_info, property);
  906. if (property_info != NULL
  907. && (!(property_info->flags & ZEND_ACC_PRIVATE)
  908. || property_info->ce == ce)) {
  909. RETURN_TRUE;
  910. }
  911. if (Z_TYPE_P(object) == IS_OBJECT &&
  912. Z_OBJ_HANDLER_P(object, has_property)(Z_OBJ_P(object), property, 2, NULL)) {
  913. RETURN_TRUE;
  914. }
  915. RETURN_FALSE;
  916. }
  917. /* }}} */
  918. static inline void class_exists_impl(INTERNAL_FUNCTION_PARAMETERS, int flags, int skip_flags) /* {{{ */
  919. {
  920. zend_string *name;
  921. zend_string *lcname;
  922. zend_class_entry *ce;
  923. zend_bool autoload = 1;
  924. ZEND_PARSE_PARAMETERS_START(1, 2)
  925. Z_PARAM_STR(name)
  926. Z_PARAM_OPTIONAL
  927. Z_PARAM_BOOL(autoload)
  928. ZEND_PARSE_PARAMETERS_END();
  929. if (!autoload) {
  930. if (ZSTR_VAL(name)[0] == '\\') {
  931. /* Ignore leading "\" */
  932. lcname = zend_string_alloc(ZSTR_LEN(name) - 1, 0);
  933. zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1);
  934. } else {
  935. lcname = zend_string_tolower(name);
  936. }
  937. ce = zend_hash_find_ptr(EG(class_table), lcname);
  938. zend_string_release_ex(lcname, 0);
  939. } else {
  940. ce = zend_lookup_class(name);
  941. }
  942. if (ce) {
  943. RETURN_BOOL(((ce->ce_flags & flags) == flags) && !(ce->ce_flags & skip_flags));
  944. } else {
  945. RETURN_FALSE;
  946. }
  947. }
  948. /* {{{ */
  949. /* {{{ proto bool class_exists(string classname [, bool autoload])
  950. Checks if the class exists */
  951. ZEND_FUNCTION(class_exists)
  952. {
  953. class_exists_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_LINKED, ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT);
  954. }
  955. /* }}} */
  956. /* {{{ proto bool interface_exists(string classname [, bool autoload])
  957. Checks if the class exists */
  958. ZEND_FUNCTION(interface_exists)
  959. {
  960. class_exists_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_LINKED|ZEND_ACC_INTERFACE, 0);
  961. }
  962. /* }}} */
  963. /* {{{ proto bool trait_exists(string traitname [, bool autoload])
  964. Checks if the trait exists */
  965. ZEND_FUNCTION(trait_exists)
  966. {
  967. class_exists_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_TRAIT, 0);
  968. }
  969. /* }}} */
  970. /* {{{ proto bool function_exists(string function_name)
  971. Checks if the function exists */
  972. ZEND_FUNCTION(function_exists)
  973. {
  974. zend_string *name;
  975. zend_bool exists;
  976. zend_string *lcname;
  977. ZEND_PARSE_PARAMETERS_START(1, 1)
  978. Z_PARAM_STR(name)
  979. ZEND_PARSE_PARAMETERS_END();
  980. if (ZSTR_VAL(name)[0] == '\\') {
  981. /* Ignore leading "\" */
  982. lcname = zend_string_alloc(ZSTR_LEN(name) - 1, 0);
  983. zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1);
  984. } else {
  985. lcname = zend_string_tolower(name);
  986. }
  987. exists = zend_hash_exists(EG(function_table), lcname);
  988. zend_string_release_ex(lcname, 0);
  989. RETURN_BOOL(exists);
  990. }
  991. /* }}} */
  992. /* {{{ proto bool class_alias(string user_class_name , string alias_name [, bool autoload])
  993. Creates an alias for user defined class */
  994. ZEND_FUNCTION(class_alias)
  995. {
  996. zend_string *class_name;
  997. char *alias_name;
  998. zend_class_entry *ce;
  999. size_t alias_name_len;
  1000. zend_bool autoload = 1;
  1001. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ss|b", &class_name, &alias_name, &alias_name_len, &autoload) == FAILURE) {
  1002. RETURN_THROWS();
  1003. }
  1004. ce = zend_lookup_class_ex(class_name, NULL, !autoload ? ZEND_FETCH_CLASS_NO_AUTOLOAD : 0);
  1005. if (ce) {
  1006. if (ce->type == ZEND_USER_CLASS) {
  1007. if (zend_register_class_alias_ex(alias_name, alias_name_len, ce, 0) == SUCCESS) {
  1008. RETURN_TRUE;
  1009. } else {
  1010. zend_error(E_WARNING, "Cannot declare %s %s, because the name is already in use", zend_get_object_type(ce), alias_name);
  1011. RETURN_FALSE;
  1012. }
  1013. } else {
  1014. zend_error(E_WARNING, "First argument of class_alias() must be a name of user defined class");
  1015. RETURN_FALSE;
  1016. }
  1017. } else {
  1018. zend_error(E_WARNING, "Class '%s' not found", ZSTR_VAL(class_name));
  1019. RETURN_FALSE;
  1020. }
  1021. }
  1022. /* }}} */
  1023. /* {{{ proto array get_included_files(void)
  1024. Returns an array with the file names that were include_once()'d */
  1025. ZEND_FUNCTION(get_included_files)
  1026. {
  1027. zend_string *entry;
  1028. ZEND_PARSE_PARAMETERS_NONE();
  1029. array_init(return_value);
  1030. ZEND_HASH_FOREACH_STR_KEY(&EG(included_files), entry) {
  1031. if (entry) {
  1032. add_next_index_str(return_value, zend_string_copy(entry));
  1033. }
  1034. } ZEND_HASH_FOREACH_END();
  1035. }
  1036. /* }}} */
  1037. /* {{{ proto bool trigger_error(string message [, int error_type])
  1038. Generates a user-level error/warning/notice message */
  1039. ZEND_FUNCTION(trigger_error)
  1040. {
  1041. zend_long error_type = E_USER_NOTICE;
  1042. char *message;
  1043. size_t message_len;
  1044. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &message, &message_len, &error_type) == FAILURE) {
  1045. RETURN_THROWS();
  1046. }
  1047. switch (error_type) {
  1048. case E_USER_ERROR:
  1049. case E_USER_WARNING:
  1050. case E_USER_NOTICE:
  1051. case E_USER_DEPRECATED:
  1052. break;
  1053. default:
  1054. zend_argument_value_error(2, "must be one of E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE,"
  1055. " or E_USER_DEPRECATED");
  1056. RETURN_THROWS();
  1057. break;
  1058. }
  1059. zend_error((int)error_type, "%s", message);
  1060. // TODO Change to void
  1061. RETURN_TRUE;
  1062. }
  1063. /* }}} */
  1064. /* {{{ proto string set_error_handler(callable error_handler [, int error_types])
  1065. Sets a user-defined error handler function. Returns the previously defined error handler, or false on error */
  1066. ZEND_FUNCTION(set_error_handler)
  1067. {
  1068. zval *error_handler;
  1069. zend_long error_type = E_ALL;
  1070. if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|l", &error_handler, &error_type) == FAILURE) {
  1071. RETURN_THROWS();
  1072. }
  1073. if (Z_TYPE_P(error_handler) != IS_NULL) { /* NULL == unset */
  1074. if (!zend_is_callable(error_handler, 0, NULL)) {
  1075. zend_string *error_handler_name = zend_get_callable_name(error_handler);
  1076. zend_argument_type_error(1, "must be a valid callback");
  1077. zend_string_release_ex(error_handler_name, 0);
  1078. RETURN_THROWS();
  1079. }
  1080. }
  1081. if (Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
  1082. ZVAL_COPY(return_value, &EG(user_error_handler));
  1083. }
  1084. zend_stack_push(&EG(user_error_handlers_error_reporting), &EG(user_error_handler_error_reporting));
  1085. zend_stack_push(&EG(user_error_handlers), &EG(user_error_handler));
  1086. if (Z_TYPE_P(error_handler) == IS_NULL) { /* unset user-defined handler */
  1087. ZVAL_UNDEF(&EG(user_error_handler));
  1088. return;
  1089. }
  1090. ZVAL_COPY(&EG(user_error_handler), error_handler);
  1091. EG(user_error_handler_error_reporting) = (int)error_type;
  1092. }
  1093. /* }}} */
  1094. /* {{{ proto void restore_error_handler(void)
  1095. Restores the previously defined error handler function */
  1096. ZEND_FUNCTION(restore_error_handler)
  1097. {
  1098. ZEND_PARSE_PARAMETERS_NONE();
  1099. if (Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
  1100. zval zeh;
  1101. ZVAL_COPY_VALUE(&zeh, &EG(user_error_handler));
  1102. ZVAL_UNDEF(&EG(user_error_handler));
  1103. zval_ptr_dtor(&zeh);
  1104. }
  1105. if (zend_stack_is_empty(&EG(user_error_handlers))) {
  1106. ZVAL_UNDEF(&EG(user_error_handler));
  1107. } else {
  1108. zval *tmp;
  1109. EG(user_error_handler_error_reporting) = zend_stack_int_top(&EG(user_error_handlers_error_reporting));
  1110. zend_stack_del_top(&EG(user_error_handlers_error_reporting));
  1111. tmp = zend_stack_top(&EG(user_error_handlers));
  1112. ZVAL_COPY_VALUE(&EG(user_error_handler), tmp);
  1113. zend_stack_del_top(&EG(user_error_handlers));
  1114. }
  1115. // TODO Change to void
  1116. RETURN_TRUE;
  1117. }
  1118. /* }}} */
  1119. /* {{{ proto mixed set_exception_handler(callable exception_handler)
  1120. Sets a user-defined exception handler function. Returns the previously defined exception handler, or false on error */
  1121. ZEND_FUNCTION(set_exception_handler)
  1122. {
  1123. zval *exception_handler;
  1124. if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &exception_handler) == FAILURE) {
  1125. RETURN_THROWS();
  1126. }
  1127. if (Z_TYPE_P(exception_handler) != IS_NULL) { /* NULL == unset */
  1128. if (!zend_is_callable(exception_handler, 0, NULL)) {
  1129. zend_string *exception_handler_name = zend_get_callable_name(exception_handler);
  1130. zend_argument_type_error(1, "must be a valid callback");
  1131. zend_string_release_ex(exception_handler_name, 0);
  1132. RETURN_THROWS();
  1133. }
  1134. }
  1135. if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
  1136. ZVAL_COPY(return_value, &EG(user_exception_handler));
  1137. }
  1138. zend_stack_push(&EG(user_exception_handlers), &EG(user_exception_handler));
  1139. if (Z_TYPE_P(exception_handler) == IS_NULL) { /* unset user-defined handler */
  1140. ZVAL_UNDEF(&EG(user_exception_handler));
  1141. return;
  1142. }
  1143. ZVAL_COPY(&EG(user_exception_handler), exception_handler);
  1144. }
  1145. /* }}} */
  1146. /* {{{ proto void restore_exception_handler(void)
  1147. Restores the previously defined exception handler function */
  1148. ZEND_FUNCTION(restore_exception_handler)
  1149. {
  1150. ZEND_PARSE_PARAMETERS_NONE();
  1151. if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
  1152. zval_ptr_dtor(&EG(user_exception_handler));
  1153. }
  1154. if (zend_stack_is_empty(&EG(user_exception_handlers))) {
  1155. ZVAL_UNDEF(&EG(user_exception_handler));
  1156. } else {
  1157. zval *tmp = zend_stack_top(&EG(user_exception_handlers));
  1158. ZVAL_COPY_VALUE(&EG(user_exception_handler), tmp);
  1159. zend_stack_del_top(&EG(user_exception_handlers));
  1160. }
  1161. // TODO Change to void
  1162. RETURN_TRUE;
  1163. }
  1164. /* }}} */
  1165. static void copy_class_or_interface_name(zval *array, zend_string *key, zend_class_entry *ce) /* {{{ */
  1166. {
  1167. if ((ce->refcount == 1 && !(ce->ce_flags & ZEND_ACC_IMMUTABLE)) ||
  1168. same_name(key, ce->name)) {
  1169. key = ce->name;
  1170. }
  1171. add_next_index_str(array, zend_string_copy(key));
  1172. }
  1173. /* }}} */
  1174. static inline void get_declared_class_impl(INTERNAL_FUNCTION_PARAMETERS, int flags, int skip_flags) /* {{{ */
  1175. {
  1176. zend_string *key;
  1177. zend_class_entry *ce;
  1178. ZEND_PARSE_PARAMETERS_NONE();
  1179. array_init(return_value);
  1180. ZEND_HASH_FOREACH_STR_KEY_PTR(EG(class_table), key, ce) {
  1181. if (key
  1182. && ZSTR_VAL(key)[0] != 0
  1183. && (ce->ce_flags & flags)
  1184. && !(ce->ce_flags & skip_flags)) {
  1185. copy_class_or_interface_name(return_value, key, ce);
  1186. }
  1187. } ZEND_HASH_FOREACH_END();
  1188. }
  1189. /* {{{ */
  1190. /* {{{ proto array get_declared_traits()
  1191. Returns an array of all declared traits. */
  1192. ZEND_FUNCTION(get_declared_traits)
  1193. {
  1194. get_declared_class_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_TRAIT, 0);
  1195. }
  1196. /* }}} */
  1197. /* {{{ proto array get_declared_classes()
  1198. Returns an array of all declared classes. */
  1199. ZEND_FUNCTION(get_declared_classes)
  1200. {
  1201. get_declared_class_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_LINKED, ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT);
  1202. }
  1203. /* }}} */
  1204. /* {{{ proto array get_declared_interfaces()
  1205. Returns an array of all declared interfaces. */
  1206. ZEND_FUNCTION(get_declared_interfaces)
  1207. {
  1208. get_declared_class_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_INTERFACE, 0);
  1209. }
  1210. /* }}} */
  1211. /* {{{ proto array get_defined_functions(void)
  1212. Returns an array of all defined functions */
  1213. ZEND_FUNCTION(get_defined_functions)
  1214. {
  1215. zval internal, user;
  1216. zend_string *key;
  1217. zend_function *func;
  1218. zend_bool exclude_disabled = 1;
  1219. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &exclude_disabled) == FAILURE) {
  1220. RETURN_THROWS();
  1221. }
  1222. if (exclude_disabled == 0) {
  1223. zend_error(E_DEPRECATED,
  1224. "get_defined_functions(): Setting $exclude_disabled to false has no effect");
  1225. }
  1226. array_init(&internal);
  1227. array_init(&user);
  1228. array_init(return_value);
  1229. ZEND_HASH_FOREACH_STR_KEY_PTR(EG(function_table), key, func) {
  1230. if (key && ZSTR_VAL(key)[0] != 0) {
  1231. if (func->type == ZEND_INTERNAL_FUNCTION) {
  1232. add_next_index_str(&internal, zend_string_copy(key));
  1233. } else if (func->type == ZEND_USER_FUNCTION) {
  1234. add_next_index_str(&user, zend_string_copy(key));
  1235. }
  1236. }
  1237. } ZEND_HASH_FOREACH_END();
  1238. zend_hash_str_add_new(Z_ARRVAL_P(return_value), "internal", sizeof("internal")-1, &internal);
  1239. zend_hash_str_add_new(Z_ARRVAL_P(return_value), "user", sizeof("user")-1, &user);
  1240. }
  1241. /* }}} */
  1242. /* {{{ proto array get_defined_vars(void)
  1243. Returns an associative array of names and values of all currently defined variable names (variables in the current scope) */
  1244. ZEND_FUNCTION(get_defined_vars)
  1245. {
  1246. zend_array *symbol_table;
  1247. ZEND_PARSE_PARAMETERS_NONE();
  1248. if (zend_forbid_dynamic_call("get_defined_vars()") == FAILURE) {
  1249. return;
  1250. }
  1251. symbol_table = zend_rebuild_symbol_table();
  1252. if (UNEXPECTED(symbol_table == NULL)) {
  1253. RETURN_EMPTY_ARRAY();
  1254. }
  1255. RETURN_ARR(zend_array_dup(symbol_table));
  1256. }
  1257. /* }}} */
  1258. #if ZEND_DEBUG && defined(ZTS)
  1259. ZEND_FUNCTION(zend_thread_id)
  1260. {
  1261. ZEND_PARSE_PARAMETERS_NONE();
  1262. RETURN_LONG((zend_long)tsrm_thread_id());
  1263. }
  1264. #endif
  1265. /* {{{ proto string get_resource_type(resource res)
  1266. Get the resource type name for a given resource */
  1267. ZEND_FUNCTION(get_resource_type)
  1268. {
  1269. const char *resource_type;
  1270. zval *z_resource_type;
  1271. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &z_resource_type) == FAILURE) {
  1272. RETURN_THROWS();
  1273. }
  1274. resource_type = zend_rsrc_list_get_rsrc_type(Z_RES_P(z_resource_type));
  1275. if (resource_type) {
  1276. RETURN_STRING(resource_type);
  1277. } else {
  1278. RETURN_STRING("Unknown");
  1279. }
  1280. }
  1281. /* }}} */
  1282. /* {{{ proto int get_resource_id(resource res)
  1283. Get the resource ID for a given resource */
  1284. ZEND_FUNCTION(get_resource_id)
  1285. {
  1286. zval *resource;
  1287. ZEND_PARSE_PARAMETERS_START(1, 1)
  1288. Z_PARAM_RESOURCE(resource)
  1289. ZEND_PARSE_PARAMETERS_END();
  1290. RETURN_LONG(Z_RES_HANDLE_P(resource));
  1291. }
  1292. /* }}} */
  1293. /* {{{ proto array get_resources([string resource_type])
  1294. Get an array with all active resources */
  1295. ZEND_FUNCTION(get_resources)
  1296. {
  1297. zend_string *type = NULL;
  1298. zend_string *key;
  1299. zend_ulong index;
  1300. zval *val;
  1301. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S", &type) == FAILURE) {
  1302. RETURN_THROWS();
  1303. }
  1304. if (!type) {
  1305. array_init(return_value);
  1306. ZEND_HASH_FOREACH_KEY_VAL(&EG(regular_list), index, key, val) {
  1307. if (!key) {
  1308. Z_ADDREF_P(val);
  1309. zend_hash_index_add_new(Z_ARRVAL_P(return_value), index, val);
  1310. }
  1311. } ZEND_HASH_FOREACH_END();
  1312. } else if (zend_string_equals_literal(type, "Unknown")) {
  1313. array_init(return_value);
  1314. ZEND_HASH_FOREACH_KEY_VAL(&EG(regular_list), index, key, val) {
  1315. if (!key && Z_RES_TYPE_P(val) <= 0) {
  1316. Z_ADDREF_P(val);
  1317. zend_hash_index_add_new(Z_ARRVAL_P(return_value), index, val);
  1318. }
  1319. } ZEND_HASH_FOREACH_END();
  1320. } else {
  1321. int id = zend_fetch_list_dtor_id(ZSTR_VAL(type));
  1322. if (id <= 0) {
  1323. zend_error(E_WARNING, "get_resources(): Unknown resource type '%s'", ZSTR_VAL(type));
  1324. RETURN_FALSE;
  1325. }
  1326. array_init(return_value);
  1327. ZEND_HASH_FOREACH_KEY_VAL(&EG(regular_list), index, key, val) {
  1328. if (!key && Z_RES_TYPE_P(val) == id) {
  1329. Z_ADDREF_P(val);
  1330. zend_hash_index_add_new(Z_ARRVAL_P(return_value), index, val);
  1331. }
  1332. } ZEND_HASH_FOREACH_END();
  1333. }
  1334. }
  1335. /* }}} */
  1336. static void add_zendext_info(zend_extension *ext, void *arg) /* {{{ */
  1337. {
  1338. zval *name_array = (zval *)arg;
  1339. add_next_index_string(name_array, ext->name);
  1340. }
  1341. /* }}} */
  1342. /* {{{ proto array get_loaded_extensions([bool zend_extensions])
  1343. Return an array containing names of loaded extensions */
  1344. ZEND_FUNCTION(get_loaded_extensions)
  1345. {
  1346. zend_bool zendext = 0;
  1347. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &zendext) == FAILURE) {
  1348. RETURN_THROWS();
  1349. }
  1350. array_init(return_value);
  1351. if (zendext) {
  1352. zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) add_zendext_info, return_value);
  1353. } else {
  1354. zend_module_entry *module;
  1355. ZEND_HASH_FOREACH_PTR(&module_registry, module) {
  1356. add_next_index_string(return_value, module->name);
  1357. } ZEND_HASH_FOREACH_END();
  1358. }
  1359. }
  1360. /* }}} */
  1361. /* {{{ proto array get_defined_constants([bool categorize])
  1362. Return an array containing the names and values of all defined constants */
  1363. ZEND_FUNCTION(get_defined_constants)
  1364. {
  1365. zend_bool categorize = 0;
  1366. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &categorize) == FAILURE) {
  1367. RETURN_THROWS();
  1368. }
  1369. array_init(return_value);
  1370. if (categorize) {
  1371. zend_constant *val;
  1372. int module_number;
  1373. zval *modules, const_val;
  1374. char **module_names;
  1375. zend_module_entry *module;
  1376. int i = 1;
  1377. modules = ecalloc(zend_hash_num_elements(&module_registry) + 2, sizeof(zval));
  1378. module_names = emalloc((zend_hash_num_elements(&module_registry) + 2) * sizeof(char *));
  1379. module_names[0] = "internal";
  1380. ZEND_HASH_FOREACH_PTR(&module_registry, module) {
  1381. module_names[module->module_number] = (char *)module->name;
  1382. i++;
  1383. } ZEND_HASH_FOREACH_END();
  1384. module_names[i] = "user";
  1385. ZEND_HASH_FOREACH_PTR(EG(zend_constants), val) {
  1386. if (!val->name) {
  1387. /* skip special constants */
  1388. continue;
  1389. }
  1390. if (ZEND_CONSTANT_MODULE_NUMBER(val) == PHP_USER_CONSTANT) {
  1391. module_number = i;
  1392. } else if (ZEND_CONSTANT_MODULE_NUMBER(val) > i) {
  1393. /* should not happen */
  1394. continue;
  1395. } else {
  1396. module_number = ZEND_CONSTANT_MODULE_NUMBER(val);
  1397. }
  1398. if (Z_TYPE(modules[module_number]) == IS_UNDEF) {
  1399. array_init(&modules[module_number]);
  1400. add_assoc_zval(return_value, module_names[module_number], &modules[module_number]);
  1401. }
  1402. ZVAL_COPY_OR_DUP(&const_val, &val->value);
  1403. zend_hash_add_new(Z_ARRVAL(modules[module_number]), val->name, &const_val);
  1404. } ZEND_HASH_FOREACH_END();
  1405. efree(module_names);
  1406. efree(modules);
  1407. } else {
  1408. zend_constant *constant;
  1409. zval const_val;
  1410. ZEND_HASH_FOREACH_PTR(EG(zend_constants), constant) {
  1411. if (!constant->name) {
  1412. /* skip special constants */
  1413. continue;
  1414. }
  1415. ZVAL_COPY_OR_DUP(&const_val, &constant->value);
  1416. zend_hash_add_new(Z_ARRVAL_P(return_value), constant->name, &const_val);
  1417. } ZEND_HASH_FOREACH_END();
  1418. }
  1419. }
  1420. /* }}} */
  1421. static void debug_backtrace_get_args(zend_execute_data *call, zval *arg_array) /* {{{ */
  1422. {
  1423. uint32_t num_args = ZEND_CALL_NUM_ARGS(call);
  1424. if (num_args) {
  1425. uint32_t i = 0;
  1426. zval *p = ZEND_CALL_ARG(call, 1);
  1427. array_init_size(arg_array, num_args);
  1428. zend_hash_real_init_packed(Z_ARRVAL_P(arg_array));
  1429. ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(arg_array)) {
  1430. if (call->func->type == ZEND_USER_FUNCTION) {
  1431. uint32_t first_extra_arg = MIN(num_args, call->func->op_array.num_args);
  1432. if (UNEXPECTED(ZEND_CALL_INFO(call) & ZEND_CALL_HAS_SYMBOL_TABLE)) {
  1433. /* In case of attached symbol_table, values on stack may be invalid
  1434. * and we have to access them through symbol_table
  1435. * See: https://bugs.php.net/bug.php?id=73156
  1436. */
  1437. zend_string *arg_name;
  1438. zval *arg;
  1439. while (i < first_extra_arg) {
  1440. arg_name = call->func->op_array.vars[i];
  1441. arg = zend_hash_find_ex_ind(call->symbol_table, arg_name, 1);
  1442. if (arg) {
  1443. if (Z_OPT_REFCOUNTED_P(arg)) {
  1444. Z_ADDREF_P(arg);
  1445. }
  1446. ZEND_HASH_FILL_SET(arg);
  1447. } else {
  1448. ZEND_HASH_FILL_SET_NULL();
  1449. }
  1450. ZEND_HASH_FILL_NEXT();
  1451. i++;
  1452. }
  1453. } else {
  1454. while (i < first_extra_arg) {
  1455. if (EXPECTED(Z_TYPE_INFO_P(p) != IS_UNDEF)) {
  1456. if (Z_OPT_REFCOUNTED_P(p)) {
  1457. Z_ADDREF_P(p);
  1458. }
  1459. ZEND_HASH_FILL_SET(p);
  1460. } else {
  1461. ZEND_HASH_FILL_SET_NULL();
  1462. }
  1463. ZEND_HASH_FILL_NEXT();
  1464. p++;
  1465. i++;
  1466. }
  1467. }
  1468. p = ZEND_CALL_VAR_NUM(call, call->func->op_array.last_var + call->func->op_array.T);
  1469. }
  1470. while (i < num_args) {
  1471. if (EXPECTED(Z_TYPE_INFO_P(p) != IS_UNDEF)) {
  1472. if (Z_OPT_REFCOUNTED_P(p)) {
  1473. Z_ADDREF_P(p);
  1474. }
  1475. ZEND_HASH_FILL_SET(p);
  1476. } else {
  1477. ZEND_HASH_FILL_SET_NULL();
  1478. }
  1479. ZEND_HASH_FILL_NEXT();
  1480. p++;
  1481. i++;
  1482. }
  1483. } ZEND_HASH_FILL_END();
  1484. Z_ARRVAL_P(arg_array)->nNumOfElements = num_args;
  1485. } else {
  1486. ZVAL_EMPTY_ARRAY(arg_array);
  1487. }
  1488. }
  1489. /* }}} */
  1490. void debug_print_backtrace_args(zval *arg_array) /* {{{ */
  1491. {
  1492. zval *tmp;
  1493. int i = 0;
  1494. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(arg_array), tmp) {
  1495. if (i++) {
  1496. ZEND_PUTS(", ");
  1497. }
  1498. zend_print_flat_zval_r(tmp);
  1499. } ZEND_HASH_FOREACH_END();
  1500. }
  1501. /* }}} */
  1502. static inline zend_bool skip_internal_handler(zend_execute_data *skip) /* {{{ */
  1503. {
  1504. return !(skip->func && ZEND_USER_CODE(skip->func->common.type))
  1505. && skip->prev_execute_data
  1506. && skip->prev_execute_data->func
  1507. && ZEND_USER_CODE(skip->prev_execute_data->func->common.type)
  1508. && skip->prev_execute_data->opline->opcode != ZEND_DO_FCALL
  1509. && skip->prev_execute_data->opline->opcode != ZEND_DO_ICALL
  1510. && skip->prev_execute_data->opline->opcode != ZEND_DO_UCALL
  1511. && skip->prev_execute_data->opline->opcode != ZEND_DO_FCALL_BY_NAME
  1512. && skip->prev_execute_data->opline->opcode != ZEND_INCLUDE_OR_EVAL;
  1513. }
  1514. /* {{{ */
  1515. /* {{{ proto void debug_print_backtrace([int options[, int limit]]) */
  1516. ZEND_FUNCTION(debug_print_backtrace)
  1517. {
  1518. zend_execute_data *call, *ptr, *skip;
  1519. zend_object *object;
  1520. int lineno, frameno = 0;
  1521. zend_function *func;
  1522. const char *function_name;
  1523. const char *filename;
  1524. zend_string *class_name = NULL;
  1525. char *call_type;
  1526. const char *include_filename = NULL;
  1527. zval arg_array;
  1528. int indent = 0;
  1529. zend_long options = 0;
  1530. zend_long limit = 0;
  1531. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|ll", &options, &limit) == FAILURE) {
  1532. RETURN_THROWS();
  1533. }
  1534. ZVAL_UNDEF(&arg_array);
  1535. ptr = EX(prev_execute_data);
  1536. /* skip debug_backtrace() */
  1537. call = ptr;
  1538. ptr = ptr->prev_execute_data;
  1539. while (ptr && (limit == 0 || frameno < limit)) {
  1540. frameno++;
  1541. class_name = NULL;
  1542. call_type = NULL;
  1543. ZVAL_UNDEF(&arg_array);
  1544. ptr = zend_generator_check_placeholder_frame(ptr);
  1545. skip = ptr;
  1546. /* skip internal handler */
  1547. if (skip_internal_handler(skip)) {
  1548. skip = skip->prev_execute_data;
  1549. }
  1550. if (skip->func && ZEND_USER_CODE(skip->func->common.type)) {
  1551. filename = ZSTR_VAL(skip->func->op_array.filename);
  1552. if (skip->opline->opcode == ZEND_HANDLE_EXCEPTION) {
  1553. if (EG(opline_before_exception)) {
  1554. lineno = EG(opline_before_exception)->lineno;
  1555. } else {
  1556. lineno = skip->func->op_array.line_end;
  1557. }
  1558. } else {
  1559. lineno = skip->opline->lineno;
  1560. }
  1561. } else {
  1562. filename = NULL;
  1563. lineno = 0;
  1564. }
  1565. /* $this may be passed into regular internal functions */
  1566. object = (Z_TYPE(call->This) == IS_OBJECT) ? Z_OBJ(call->This) : NULL;
  1567. if (call->func) {
  1568. func = call->func;
  1569. if (func->common.function_name) {
  1570. function_name = ZSTR_VAL(func->common.function_name);
  1571. } else {
  1572. function_name = NULL;
  1573. }
  1574. } else {
  1575. func = NULL;
  1576. function_name = NULL;
  1577. }
  1578. if (function_name) {
  1579. if (object) {
  1580. if (func->common.scope) {
  1581. class_name = func->common.scope->name;
  1582. } else if (object->handlers->get_class_name == zend_std_get_class_name) {
  1583. class_name = object->ce->name;
  1584. } else {
  1585. class_name = object->handlers->get_class_name(object);
  1586. }
  1587. call_type = "->";
  1588. } else if (func->common.scope) {
  1589. class_name = func->common.scope->name;
  1590. call_type = "::";
  1591. } else {
  1592. class_name = NULL;
  1593. call_type = NULL;
  1594. }
  1595. if (func->type != ZEND_EVAL_CODE) {
  1596. if ((options & DEBUG_BACKTRACE_IGNORE_ARGS) == 0) {
  1597. debug_backtrace_get_args(call, &arg_array);
  1598. }
  1599. }
  1600. } else {
  1601. /* i know this is kinda ugly, but i'm trying to avoid extra cycles in the main execution loop */
  1602. zend_bool build_filename_arg = 1;
  1603. if (!ptr->func || !ZEND_USER_CODE(ptr->func->common.type) || ptr->opline->opcode != ZEND_INCLUDE_OR_EVAL) {
  1604. /* can happen when calling eval from a custom sapi */
  1605. function_name = "unknown";
  1606. build_filename_arg = 0;
  1607. } else
  1608. switch (ptr->opline->extended_value) {
  1609. case ZEND_EVAL:
  1610. function_name = "eval";
  1611. build_filename_arg = 0;
  1612. break;
  1613. case ZEND_INCLUDE:
  1614. function_name = "include";
  1615. break;
  1616. case ZEND_REQUIRE:
  1617. function_name = "require";
  1618. break;
  1619. case ZEND_INCLUDE_ONCE:
  1620. function_name = "include_once";
  1621. break;
  1622. case ZEND_REQUIRE_ONCE:
  1623. function_name = "require_once";
  1624. break;
  1625. default:
  1626. /* this can actually happen if you use debug_backtrace() in your error_handler and
  1627. * you're in the top-scope */
  1628. function_name = "unknown";
  1629. build_filename_arg = 0;
  1630. break;
  1631. }
  1632. if (build_filename_arg && include_filename) {
  1633. array_init(&arg_array);
  1634. add_next_index_string(&arg_array, (char*)include_filename);
  1635. }
  1636. call_type = NULL;
  1637. }
  1638. zend_printf("#%-2d ", indent);
  1639. if (class_name) {
  1640. ZEND_PUTS(ZSTR_VAL(class_name));
  1641. ZEND_PUTS(call_type);
  1642. if (object
  1643. && !func->common.scope
  1644. && object->handlers->get_class_name != zend_std_get_class_name) {
  1645. zend_string_release_ex(class_name, 0);
  1646. }
  1647. }
  1648. zend_printf("%s(", function_name);
  1649. if (Z_TYPE(arg_array) != IS_UNDEF) {
  1650. debug_print_backtrace_args(&arg_array);
  1651. zval_ptr_dtor(&arg_array);
  1652. }
  1653. if (filename) {
  1654. zend_printf(") called at [%s:%d]\n", filename, lineno);
  1655. } else {
  1656. zend_execute_data *prev_call = skip;
  1657. zend_execute_data *prev = skip->prev_execute_data;
  1658. while (prev) {
  1659. if (prev_call &&
  1660. prev_call->func &&
  1661. !ZEND_USER_CODE(prev_call->func->common.type)) {
  1662. prev = NULL;
  1663. break;
  1664. }
  1665. if (prev->func && ZEND_USER_CODE(prev->func->common.type)) {
  1666. zend_printf(") called at [%s:%d]\n", ZSTR_VAL(prev->func->op_array.filename), prev->opline->lineno);
  1667. break;
  1668. }
  1669. prev_call = prev;
  1670. prev = prev->prev_execute_data;
  1671. }
  1672. if (!prev) {
  1673. ZEND_PUTS(")\n");
  1674. }
  1675. }
  1676. include_filename = filename;
  1677. call = skip;
  1678. ptr = skip->prev_execute_data;
  1679. ++indent;
  1680. }
  1681. }
  1682. /* }}} */
  1683. ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int options, int limit) /* {{{ */
  1684. {
  1685. zend_execute_data *ptr, *skip, *call = NULL;
  1686. zend_object *object;
  1687. int lineno, frameno = 0;
  1688. zend_function *func;
  1689. zend_string *function_name;
  1690. zend_string *filename;
  1691. zend_string *include_filename = NULL;
  1692. zval stack_frame, tmp;
  1693. array_init(return_value);
  1694. if (!(ptr = EG(current_execute_data))) {
  1695. return;
  1696. }
  1697. if (!ptr->func || !ZEND_USER_CODE(ptr->func->common.type)) {
  1698. call = ptr;
  1699. ptr = ptr->prev_execute_data;
  1700. }
  1701. if (ptr) {
  1702. if (skip_last) {
  1703. /* skip debug_backtrace() */
  1704. call = ptr;
  1705. ptr = ptr->prev_execute_data;
  1706. } else {
  1707. /* skip "new Exception()" */
  1708. if (ptr->func && ZEND_USER_CODE(ptr->func->common.type) && (ptr->opline->opcode == ZEND_NEW)) {
  1709. call = ptr;
  1710. ptr = ptr->prev_execute_data;
  1711. }
  1712. }
  1713. if (!call) {
  1714. call = ptr;
  1715. ptr = ptr->prev_execute_data;
  1716. }
  1717. }
  1718. while (ptr && (limit == 0 || frameno < limit)) {
  1719. frameno++;
  1720. array_init(&stack_frame);
  1721. ptr = zend_generator_check_placeholder_frame(ptr);
  1722. skip = ptr;
  1723. /* skip internal handler */
  1724. if (skip_internal_handler(skip)) {
  1725. skip = skip->prev_execute_data;
  1726. }
  1727. if (skip->func && ZEND_USER_CODE(skip->func->common.type)) {
  1728. filename = skip->func->op_array.filename;
  1729. if (skip->opline->opcode == ZEND_HANDLE_EXCEPTION) {
  1730. if (EG(opline_before_exception)) {
  1731. lineno = EG(opline_before_exception)->lineno;
  1732. } else {
  1733. lineno = skip->func->op_array.line_end;
  1734. }
  1735. } else {
  1736. lineno = skip->opline->lineno;
  1737. }
  1738. ZVAL_STR_COPY(&tmp, filename);
  1739. zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
  1740. ZVAL_LONG(&tmp, lineno);
  1741. zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
  1742. /* try to fetch args only if an FCALL was just made - elsewise we're in the middle of a function
  1743. * and debug_baktrace() might have been called by the error_handler. in this case we don't
  1744. * want to pop anything of the argument-stack */
  1745. } else {
  1746. zend_execute_data *prev_call = skip;
  1747. zend_execute_data *prev = skip->prev_execute_data;
  1748. while (prev) {
  1749. if (prev_call &&
  1750. prev_call->func &&
  1751. !ZEND_USER_CODE(prev_call->func->common.type) &&
  1752. !(prev_call->func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
  1753. break;
  1754. }
  1755. if (prev->func && ZEND_USER_CODE(prev->func->common.type)) {
  1756. ZVAL_STR_COPY(&tmp, prev->func->op_array.filename);
  1757. zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
  1758. ZVAL_LONG(&tmp, prev->opline->lineno);
  1759. zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
  1760. break;
  1761. }
  1762. prev_call = prev;
  1763. prev = prev->prev_execute_data;
  1764. }
  1765. filename = NULL;
  1766. }
  1767. /* $this may be passed into regular internal functions */
  1768. object = (call && (Z_TYPE(call->This) == IS_OBJECT)) ? Z_OBJ(call->This) : NULL;
  1769. if (call && call->func) {
  1770. func = call->func;
  1771. function_name = func->common.function_name;
  1772. } else {
  1773. func = NULL;
  1774. function_name = NULL;
  1775. }
  1776. if (function_name) {
  1777. ZVAL_STR_COPY(&tmp, function_name);
  1778. zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_FUNCTION), &tmp);
  1779. if (object) {
  1780. if (func->common.scope) {
  1781. ZVAL_STR_COPY(&tmp, func->common.scope->name);
  1782. } else if (object->handlers->get_class_name == zend_std_get_class_name) {
  1783. ZVAL_STR_COPY(&tmp, object->ce->name);
  1784. } else {
  1785. ZVAL_STR(&tmp, object->handlers->get_class_name(object));
  1786. }
  1787. zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_CLASS), &tmp);
  1788. if ((options & DEBUG_BACKTRACE_PROVIDE_OBJECT) != 0) {
  1789. ZVAL_OBJ(&tmp, object);
  1790. zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_OBJECT), &tmp);
  1791. Z_ADDREF(tmp);
  1792. }
  1793. ZVAL_INTERNED_STR(&tmp, ZSTR_KNOWN(ZEND_STR_OBJECT_OPERATOR));
  1794. zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_TYPE), &tmp);
  1795. } else if (func->common.scope) {
  1796. ZVAL_STR_COPY(&tmp, func->common.scope->name);
  1797. zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_CLASS), &tmp);
  1798. ZVAL_INTERNED_STR(&tmp, ZSTR_KNOWN(ZEND_STR_PAAMAYIM_NEKUDOTAYIM));
  1799. zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_TYPE), &tmp);
  1800. }
  1801. if ((options & DEBUG_BACKTRACE_IGNORE_ARGS) == 0 &&
  1802. func->type != ZEND_EVAL_CODE) {
  1803. debug_backtrace_get_args(call, &tmp);
  1804. zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_ARGS), &tmp);
  1805. }
  1806. } else {
  1807. /* i know this is kinda ugly, but i'm trying to avoid extra cycles in the main execution loop */
  1808. zend_bool build_filename_arg = 1;
  1809. zend_string *pseudo_function_name;
  1810. if (!ptr->func || !ZEND_USER_CODE(ptr->func->common.type) || ptr->opline->opcode != ZEND_INCLUDE_OR_EVAL) {
  1811. /* can happen when calling eval from a custom sapi */
  1812. pseudo_function_name = ZSTR_KNOWN(ZEND_STR_UNKNOWN);
  1813. build_filename_arg = 0;
  1814. } else
  1815. switch (ptr->opline->extended_value) {
  1816. case ZEND_EVAL:
  1817. pseudo_function_name = ZSTR_KNOWN(ZEND_STR_EVAL);
  1818. build_filename_arg = 0;
  1819. break;
  1820. case ZEND_INCLUDE:
  1821. pseudo_function_name = ZSTR_KNOWN(ZEND_STR_INCLUDE);
  1822. break;
  1823. case ZEND_REQUIRE:
  1824. pseudo_function_name = ZSTR_KNOWN(ZEND_STR_REQUIRE);
  1825. break;
  1826. case ZEND_INCLUDE_ONCE:
  1827. pseudo_function_name = ZSTR_KNOWN(ZEND_STR_INCLUDE_ONCE);
  1828. break;
  1829. case ZEND_REQUIRE_ONCE:
  1830. pseudo_function_name = ZSTR_KNOWN(ZEND_STR_REQUIRE_ONCE);
  1831. break;
  1832. default:
  1833. /* this can actually happen if you use debug_backtrace() in your error_handler and
  1834. * you're in the top-scope */
  1835. pseudo_function_name = ZSTR_KNOWN(ZEND_STR_UNKNOWN);
  1836. build_filename_arg = 0;
  1837. break;
  1838. }
  1839. if (build_filename_arg && include_filename) {
  1840. zval arg_array;
  1841. array_init(&arg_array);
  1842. /* include_filename always points to the last filename of the last last called-function.
  1843. if we have called include in the frame above - this is the file we have included.
  1844. */
  1845. ZVAL_STR_COPY(&tmp, include_filename);
  1846. zend_hash_next_index_insert_new(Z_ARRVAL(arg_array), &tmp);
  1847. zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_ARGS), &arg_array);
  1848. }
  1849. ZVAL_INTERNED_STR(&tmp, pseudo_function_name);
  1850. zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_FUNCTION), &tmp);
  1851. }
  1852. zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &stack_frame);
  1853. include_filename = filename;
  1854. call = skip;
  1855. ptr = skip->prev_execute_data;
  1856. }
  1857. }
  1858. /* }}} */
  1859. /* {{{ proto array debug_backtrace([int options[, int limit]])
  1860. Return backtrace as array */
  1861. ZEND_FUNCTION(debug_backtrace)
  1862. {
  1863. zend_long options = DEBUG_BACKTRACE_PROVIDE_OBJECT;
  1864. zend_long limit = 0;
  1865. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|ll", &options, &limit) == FAILURE) {
  1866. RETURN_THROWS();
  1867. }
  1868. zend_fetch_debug_backtrace(return_value, 1, options, limit);
  1869. }
  1870. /* }}} */
  1871. /* {{{ proto bool extension_loaded(string extension_name)
  1872. Returns true if the named extension is loaded */
  1873. ZEND_FUNCTION(extension_loaded)
  1874. {
  1875. zend_string *extension_name;
  1876. zend_string *lcname;
  1877. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &extension_name) == FAILURE) {
  1878. RETURN_THROWS();
  1879. }
  1880. lcname = zend_string_tolower(extension_name);
  1881. if (zend_hash_exists(&module_registry, lcname)) {
  1882. RETVAL_TRUE;
  1883. } else {
  1884. RETVAL_FALSE;
  1885. }
  1886. zend_string_release_ex(lcname, 0);
  1887. }
  1888. /* }}} */
  1889. /* {{{ proto array get_extension_funcs(string extension_name)
  1890. Returns an array with the names of functions belonging to the named extension */
  1891. ZEND_FUNCTION(get_extension_funcs)
  1892. {
  1893. zend_string *extension_name;
  1894. zend_string *lcname;
  1895. int array;
  1896. zend_module_entry *module;
  1897. zend_function *zif;
  1898. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &extension_name) == FAILURE) {
  1899. RETURN_THROWS();
  1900. }
  1901. if (strncasecmp(ZSTR_VAL(extension_name), "zend", sizeof("zend"))) {
  1902. lcname = zend_string_tolower(extension_name);
  1903. module = zend_hash_find_ptr(&module_registry, lcname);
  1904. zend_string_release_ex(lcname, 0);
  1905. } else {
  1906. module = zend_hash_str_find_ptr(&module_registry, "core", sizeof("core") - 1);
  1907. }
  1908. if (!module) {
  1909. RETURN_FALSE;
  1910. }
  1911. if (module->functions) {
  1912. /* avoid BC break, if functions list is empty, will return an empty array */
  1913. array_init(return_value);
  1914. array = 1;
  1915. } else {
  1916. array = 0;
  1917. }
  1918. ZEND_HASH_FOREACH_PTR(CG(function_table), zif) {
  1919. if (zif->common.type == ZEND_INTERNAL_FUNCTION
  1920. && zif->internal_function.module == module) {
  1921. if (!array) {
  1922. array_init(return_value);
  1923. array = 1;
  1924. }
  1925. add_next_index_str(return_value, zend_string_copy(zif->common.function_name));
  1926. }
  1927. } ZEND_HASH_FOREACH_END();
  1928. if (!array) {
  1929. RETURN_FALSE;
  1930. }
  1931. }
  1932. /* }}} */