php_spl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Marcus Boerger <helly@php.net> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19. #include "php.h"
  20. #include "php_ini.h"
  21. #include "php_main.h"
  22. #include "ext/standard/info.h"
  23. #include "php_spl.h"
  24. #include "php_spl_arginfo.h"
  25. #include "spl_functions.h"
  26. #include "spl_engine.h"
  27. #include "spl_array.h"
  28. #include "spl_directory.h"
  29. #include "spl_iterators.h"
  30. #include "spl_exceptions.h"
  31. #include "spl_observer.h"
  32. #include "spl_dllist.h"
  33. #include "spl_fixedarray.h"
  34. #include "spl_heap.h"
  35. #include "zend_exceptions.h"
  36. #include "zend_interfaces.h"
  37. #include "ext/standard/php_mt_rand.h"
  38. #include "main/snprintf.h"
  39. #ifdef COMPILE_DL_SPL
  40. ZEND_GET_MODULE(spl)
  41. #endif
  42. ZEND_TLS zend_string *spl_autoload_extensions;
  43. ZEND_TLS HashTable *spl_autoload_functions;
  44. #define SPL_DEFAULT_FILE_EXTENSIONS ".inc,.php"
  45. static zend_class_entry * spl_find_ce_by_name(zend_string *name, bool autoload)
  46. {
  47. zend_class_entry *ce;
  48. if (!autoload) {
  49. zend_string *lc_name = zend_string_tolower(name);
  50. ce = zend_hash_find_ptr(EG(class_table), lc_name);
  51. zend_string_release(lc_name);
  52. } else {
  53. ce = zend_lookup_class(name);
  54. }
  55. if (ce == NULL) {
  56. php_error_docref(NULL, E_WARNING, "Class %s does not exist%s", ZSTR_VAL(name), autoload ? " and could not be loaded" : "");
  57. return NULL;
  58. }
  59. return ce;
  60. }
  61. /* {{{ Return an array containing the names of all parent classes */
  62. PHP_FUNCTION(class_parents)
  63. {
  64. zval *obj;
  65. zend_class_entry *parent_class, *ce;
  66. bool autoload = 1;
  67. if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &obj, &autoload) == FAILURE) {
  68. RETURN_THROWS();
  69. }
  70. if (Z_TYPE_P(obj) != IS_OBJECT && Z_TYPE_P(obj) != IS_STRING) {
  71. zend_argument_type_error(1, "must be of type object|string, %s given", zend_zval_type_name(obj));
  72. RETURN_THROWS();
  73. }
  74. if (Z_TYPE_P(obj) == IS_STRING) {
  75. if (NULL == (ce = spl_find_ce_by_name(Z_STR_P(obj), autoload))) {
  76. RETURN_FALSE;
  77. }
  78. } else {
  79. ce = Z_OBJCE_P(obj);
  80. }
  81. array_init(return_value);
  82. parent_class = ce->parent;
  83. while (parent_class) {
  84. spl_add_class_name(return_value, parent_class, 0, 0);
  85. parent_class = parent_class->parent;
  86. }
  87. }
  88. /* }}} */
  89. /* {{{ Return all classes and interfaces implemented by SPL */
  90. PHP_FUNCTION(class_implements)
  91. {
  92. zval *obj;
  93. bool autoload = 1;
  94. zend_class_entry *ce;
  95. if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &obj, &autoload) == FAILURE) {
  96. RETURN_THROWS();
  97. }
  98. if (Z_TYPE_P(obj) != IS_OBJECT && Z_TYPE_P(obj) != IS_STRING) {
  99. zend_argument_type_error(1, "must be of type object|string, %s given", zend_zval_type_name(obj));
  100. RETURN_THROWS();
  101. }
  102. if (Z_TYPE_P(obj) == IS_STRING) {
  103. if (NULL == (ce = spl_find_ce_by_name(Z_STR_P(obj), autoload))) {
  104. RETURN_FALSE;
  105. }
  106. } else {
  107. ce = Z_OBJCE_P(obj);
  108. }
  109. array_init(return_value);
  110. spl_add_interfaces(return_value, ce, 1, ZEND_ACC_INTERFACE);
  111. }
  112. /* }}} */
  113. /* {{{ Return all traits used by a class. */
  114. PHP_FUNCTION(class_uses)
  115. {
  116. zval *obj;
  117. bool autoload = 1;
  118. zend_class_entry *ce;
  119. if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &obj, &autoload) == FAILURE) {
  120. RETURN_THROWS();
  121. }
  122. if (Z_TYPE_P(obj) != IS_OBJECT && Z_TYPE_P(obj) != IS_STRING) {
  123. zend_argument_type_error(1, "must be of type object|string, %s given", zend_zval_type_name(obj));
  124. RETURN_THROWS();
  125. }
  126. if (Z_TYPE_P(obj) == IS_STRING) {
  127. if (NULL == (ce = spl_find_ce_by_name(Z_STR_P(obj), autoload))) {
  128. RETURN_FALSE;
  129. }
  130. } else {
  131. ce = Z_OBJCE_P(obj);
  132. }
  133. array_init(return_value);
  134. spl_add_traits(return_value, ce, 1, ZEND_ACC_TRAIT);
  135. }
  136. /* }}} */
  137. #define SPL_ADD_CLASS(class_name, z_list, sub, allow, ce_flags) \
  138. spl_add_classes(spl_ce_ ## class_name, z_list, sub, allow, ce_flags)
  139. #define SPL_LIST_CLASSES(z_list, sub, allow, ce_flags) \
  140. SPL_ADD_CLASS(AppendIterator, z_list, sub, allow, ce_flags); \
  141. SPL_ADD_CLASS(ArrayIterator, z_list, sub, allow, ce_flags); \
  142. SPL_ADD_CLASS(ArrayObject, z_list, sub, allow, ce_flags); \
  143. SPL_ADD_CLASS(BadFunctionCallException, z_list, sub, allow, ce_flags); \
  144. SPL_ADD_CLASS(BadMethodCallException, z_list, sub, allow, ce_flags); \
  145. SPL_ADD_CLASS(CachingIterator, z_list, sub, allow, ce_flags); \
  146. SPL_ADD_CLASS(CallbackFilterIterator, z_list, sub, allow, ce_flags); \
  147. SPL_ADD_CLASS(DirectoryIterator, z_list, sub, allow, ce_flags); \
  148. SPL_ADD_CLASS(DomainException, z_list, sub, allow, ce_flags); \
  149. SPL_ADD_CLASS(EmptyIterator, z_list, sub, allow, ce_flags); \
  150. SPL_ADD_CLASS(FilesystemIterator, z_list, sub, allow, ce_flags); \
  151. SPL_ADD_CLASS(FilterIterator, z_list, sub, allow, ce_flags); \
  152. SPL_ADD_CLASS(GlobIterator, z_list, sub, allow, ce_flags); \
  153. SPL_ADD_CLASS(InfiniteIterator, z_list, sub, allow, ce_flags); \
  154. SPL_ADD_CLASS(InvalidArgumentException, z_list, sub, allow, ce_flags); \
  155. SPL_ADD_CLASS(IteratorIterator, z_list, sub, allow, ce_flags); \
  156. SPL_ADD_CLASS(LengthException, z_list, sub, allow, ce_flags); \
  157. SPL_ADD_CLASS(LimitIterator, z_list, sub, allow, ce_flags); \
  158. SPL_ADD_CLASS(LogicException, z_list, sub, allow, ce_flags); \
  159. SPL_ADD_CLASS(MultipleIterator, z_list, sub, allow, ce_flags); \
  160. SPL_ADD_CLASS(NoRewindIterator, z_list, sub, allow, ce_flags); \
  161. SPL_ADD_CLASS(OuterIterator, z_list, sub, allow, ce_flags); \
  162. SPL_ADD_CLASS(OutOfBoundsException, z_list, sub, allow, ce_flags); \
  163. SPL_ADD_CLASS(OutOfRangeException, z_list, sub, allow, ce_flags); \
  164. SPL_ADD_CLASS(OverflowException, z_list, sub, allow, ce_flags); \
  165. SPL_ADD_CLASS(ParentIterator, z_list, sub, allow, ce_flags); \
  166. SPL_ADD_CLASS(RangeException, z_list, sub, allow, ce_flags); \
  167. SPL_ADD_CLASS(RecursiveArrayIterator, z_list, sub, allow, ce_flags); \
  168. SPL_ADD_CLASS(RecursiveCachingIterator, z_list, sub, allow, ce_flags); \
  169. SPL_ADD_CLASS(RecursiveCallbackFilterIterator, z_list, sub, allow, ce_flags); \
  170. SPL_ADD_CLASS(RecursiveDirectoryIterator, z_list, sub, allow, ce_flags); \
  171. SPL_ADD_CLASS(RecursiveFilterIterator, z_list, sub, allow, ce_flags); \
  172. SPL_ADD_CLASS(RecursiveIterator, z_list, sub, allow, ce_flags); \
  173. SPL_ADD_CLASS(RecursiveIteratorIterator, z_list, sub, allow, ce_flags); \
  174. SPL_ADD_CLASS(RecursiveRegexIterator, z_list, sub, allow, ce_flags); \
  175. SPL_ADD_CLASS(RecursiveTreeIterator, z_list, sub, allow, ce_flags); \
  176. SPL_ADD_CLASS(RegexIterator, z_list, sub, allow, ce_flags); \
  177. SPL_ADD_CLASS(RuntimeException, z_list, sub, allow, ce_flags); \
  178. SPL_ADD_CLASS(SeekableIterator, z_list, sub, allow, ce_flags); \
  179. SPL_ADD_CLASS(SplDoublyLinkedList, z_list, sub, allow, ce_flags); \
  180. SPL_ADD_CLASS(SplFileInfo, z_list, sub, allow, ce_flags); \
  181. SPL_ADD_CLASS(SplFileObject, z_list, sub, allow, ce_flags); \
  182. SPL_ADD_CLASS(SplFixedArray, z_list, sub, allow, ce_flags); \
  183. SPL_ADD_CLASS(SplHeap, z_list, sub, allow, ce_flags); \
  184. SPL_ADD_CLASS(SplMinHeap, z_list, sub, allow, ce_flags); \
  185. SPL_ADD_CLASS(SplMaxHeap, z_list, sub, allow, ce_flags); \
  186. SPL_ADD_CLASS(SplObjectStorage, z_list, sub, allow, ce_flags); \
  187. SPL_ADD_CLASS(SplObserver, z_list, sub, allow, ce_flags); \
  188. SPL_ADD_CLASS(SplPriorityQueue, z_list, sub, allow, ce_flags); \
  189. SPL_ADD_CLASS(SplQueue, z_list, sub, allow, ce_flags); \
  190. SPL_ADD_CLASS(SplStack, z_list, sub, allow, ce_flags); \
  191. SPL_ADD_CLASS(SplSubject, z_list, sub, allow, ce_flags); \
  192. SPL_ADD_CLASS(SplTempFileObject, z_list, sub, allow, ce_flags); \
  193. SPL_ADD_CLASS(UnderflowException, z_list, sub, allow, ce_flags); \
  194. SPL_ADD_CLASS(UnexpectedValueException, z_list, sub, allow, ce_flags); \
  195. /* {{{ Return an array containing the names of all clsses and interfaces defined in SPL */
  196. PHP_FUNCTION(spl_classes)
  197. {
  198. if (zend_parse_parameters_none() == FAILURE) {
  199. RETURN_THROWS();
  200. }
  201. array_init(return_value);
  202. SPL_LIST_CLASSES(return_value, 0, 0, 0)
  203. }
  204. /* }}} */
  205. static int spl_autoload(zend_string *class_name, zend_string *lc_name, const char *ext, int ext_len) /* {{{ */
  206. {
  207. zend_string *class_file;
  208. zval dummy;
  209. zend_file_handle file_handle;
  210. zend_op_array *new_op_array;
  211. zval result;
  212. int ret;
  213. class_file = zend_strpprintf(0, "%s%.*s", ZSTR_VAL(lc_name), ext_len, ext);
  214. #if DEFAULT_SLASH != '\\'
  215. {
  216. char *ptr = ZSTR_VAL(class_file);
  217. char *end = ptr + ZSTR_LEN(class_file);
  218. while ((ptr = memchr(ptr, '\\', (end - ptr))) != NULL) {
  219. *ptr = DEFAULT_SLASH;
  220. }
  221. }
  222. #endif
  223. zend_stream_init_filename_ex(&file_handle, class_file);
  224. ret = php_stream_open_for_zend_ex(&file_handle, USE_PATH|STREAM_OPEN_FOR_INCLUDE);
  225. if (ret == SUCCESS) {
  226. zend_string *opened_path;
  227. if (!file_handle.opened_path) {
  228. file_handle.opened_path = zend_string_copy(class_file);
  229. }
  230. opened_path = zend_string_copy(file_handle.opened_path);
  231. ZVAL_NULL(&dummy);
  232. if (zend_hash_add(&EG(included_files), opened_path, &dummy)) {
  233. new_op_array = zend_compile_file(&file_handle, ZEND_REQUIRE);
  234. } else {
  235. new_op_array = NULL;
  236. }
  237. zend_string_release_ex(opened_path, 0);
  238. if (new_op_array) {
  239. uint32_t orig_jit_trace_num = EG(jit_trace_num);
  240. ZVAL_UNDEF(&result);
  241. zend_execute(new_op_array, &result);
  242. EG(jit_trace_num) = orig_jit_trace_num;
  243. destroy_op_array(new_op_array);
  244. efree(new_op_array);
  245. if (!EG(exception)) {
  246. zval_ptr_dtor(&result);
  247. }
  248. zend_destroy_file_handle(&file_handle);
  249. zend_string_release(class_file);
  250. return zend_hash_exists(EG(class_table), lc_name);
  251. }
  252. }
  253. zend_destroy_file_handle(&file_handle);
  254. zend_string_release(class_file);
  255. return 0;
  256. } /* }}} */
  257. /* {{{ Default autoloader implementation */
  258. PHP_FUNCTION(spl_autoload)
  259. {
  260. int pos_len, pos1_len;
  261. char *pos, *pos1;
  262. zend_string *class_name, *lc_name, *file_exts = NULL;
  263. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|S!", &class_name, &file_exts) == FAILURE) {
  264. RETURN_THROWS();
  265. }
  266. if (!file_exts) {
  267. file_exts = spl_autoload_extensions;
  268. }
  269. if (file_exts == NULL) { /* autoload_extensions is not initialized, set to defaults */
  270. pos = SPL_DEFAULT_FILE_EXTENSIONS;
  271. pos_len = sizeof(SPL_DEFAULT_FILE_EXTENSIONS) - 1;
  272. } else {
  273. pos = ZSTR_VAL(file_exts);
  274. pos_len = (int)ZSTR_LEN(file_exts);
  275. }
  276. lc_name = zend_string_tolower(class_name);
  277. while (pos && *pos && !EG(exception)) {
  278. pos1 = strchr(pos, ',');
  279. if (pos1) {
  280. pos1_len = (int)(pos1 - pos);
  281. } else {
  282. pos1_len = pos_len;
  283. }
  284. if (spl_autoload(class_name, lc_name, pos, pos1_len)) {
  285. break; /* loaded */
  286. }
  287. pos = pos1 ? pos1 + 1 : NULL;
  288. pos_len = pos1? pos_len - pos1_len - 1 : 0;
  289. }
  290. zend_string_release(lc_name);
  291. } /* }}} */
  292. /* {{{ Register and return default file extensions for spl_autoload */
  293. PHP_FUNCTION(spl_autoload_extensions)
  294. {
  295. zend_string *file_exts = NULL;
  296. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S!", &file_exts) == FAILURE) {
  297. RETURN_THROWS();
  298. }
  299. if (file_exts) {
  300. if (spl_autoload_extensions) {
  301. zend_string_release_ex(spl_autoload_extensions, 0);
  302. }
  303. spl_autoload_extensions = zend_string_copy(file_exts);
  304. }
  305. if (spl_autoload_extensions == NULL) {
  306. RETURN_STRINGL(SPL_DEFAULT_FILE_EXTENSIONS, sizeof(SPL_DEFAULT_FILE_EXTENSIONS) - 1);
  307. } else {
  308. zend_string_addref(spl_autoload_extensions);
  309. RETURN_STR(spl_autoload_extensions);
  310. }
  311. } /* }}} */
  312. typedef struct {
  313. zend_function *func_ptr;
  314. zend_object *obj;
  315. zend_object *closure;
  316. zend_class_entry *ce;
  317. } autoload_func_info;
  318. static void autoload_func_info_destroy(autoload_func_info *alfi) {
  319. if (alfi->obj) {
  320. zend_object_release(alfi->obj);
  321. }
  322. if (alfi->func_ptr &&
  323. UNEXPECTED(alfi->func_ptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
  324. zend_string_release_ex(alfi->func_ptr->common.function_name, 0);
  325. zend_free_trampoline(alfi->func_ptr);
  326. }
  327. if (alfi->closure) {
  328. zend_object_release(alfi->closure);
  329. }
  330. efree(alfi);
  331. }
  332. static void autoload_func_info_zval_dtor(zval *element)
  333. {
  334. autoload_func_info_destroy(Z_PTR_P(element));
  335. }
  336. static autoload_func_info *autoload_func_info_from_fci(
  337. zend_fcall_info *fci, zend_fcall_info_cache *fcc) {
  338. autoload_func_info *alfi = emalloc(sizeof(autoload_func_info));
  339. alfi->ce = fcc->calling_scope;
  340. alfi->func_ptr = fcc->function_handler;
  341. alfi->obj = fcc->object;
  342. if (alfi->obj) {
  343. GC_ADDREF(alfi->obj);
  344. }
  345. if (Z_TYPE(fci->function_name) == IS_OBJECT) {
  346. alfi->closure = Z_OBJ(fci->function_name);
  347. GC_ADDREF(alfi->closure);
  348. } else {
  349. alfi->closure = NULL;
  350. }
  351. return alfi;
  352. }
  353. static bool autoload_func_info_equals(
  354. const autoload_func_info *alfi1, const autoload_func_info *alfi2) {
  355. return alfi1->func_ptr == alfi2->func_ptr
  356. && alfi1->obj == alfi2->obj
  357. && alfi1->ce == alfi2->ce
  358. && alfi1->closure == alfi2->closure;
  359. }
  360. static zend_class_entry *spl_perform_autoload(zend_string *class_name, zend_string *lc_name) {
  361. if (!spl_autoload_functions) {
  362. return NULL;
  363. }
  364. /* We don't use ZEND_HASH_FOREACH here,
  365. * because autoloaders may be added/removed during autoloading. */
  366. HashPosition pos;
  367. zend_hash_internal_pointer_reset_ex(spl_autoload_functions, &pos);
  368. while (1) {
  369. autoload_func_info *alfi =
  370. zend_hash_get_current_data_ptr_ex(spl_autoload_functions, &pos);
  371. if (!alfi) {
  372. break;
  373. }
  374. zend_function *func = alfi->func_ptr;
  375. if (UNEXPECTED(func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
  376. func = emalloc(sizeof(zend_op_array));
  377. memcpy(func, alfi->func_ptr, sizeof(zend_op_array));
  378. zend_string_addref(func->op_array.function_name);
  379. }
  380. zval param;
  381. ZVAL_STR(&param, class_name);
  382. zend_call_known_function(func, alfi->obj, alfi->ce, NULL, 1, &param, NULL);
  383. if (EG(exception)) {
  384. break;
  385. }
  386. if (ZSTR_HAS_CE_CACHE(class_name) && ZSTR_GET_CE_CACHE(class_name)) {
  387. return (zend_class_entry*)ZSTR_GET_CE_CACHE(class_name);
  388. } else {
  389. zend_class_entry *ce = zend_hash_find_ptr(EG(class_table), lc_name);
  390. if (ce) {
  391. return ce;
  392. }
  393. }
  394. zend_hash_move_forward_ex(spl_autoload_functions, &pos);
  395. }
  396. return NULL;
  397. }
  398. /* {{{ Try all registered autoload function to load the requested class */
  399. PHP_FUNCTION(spl_autoload_call)
  400. {
  401. zend_string *class_name;
  402. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &class_name) == FAILURE) {
  403. RETURN_THROWS();
  404. }
  405. zend_string *lc_name = zend_string_tolower(class_name);
  406. spl_perform_autoload(class_name, lc_name);
  407. zend_string_release(lc_name);
  408. } /* }}} */
  409. #define HT_MOVE_TAIL_TO_HEAD(ht) \
  410. do { \
  411. Bucket tmp = (ht)->arData[(ht)->nNumUsed-1]; \
  412. memmove((ht)->arData + 1, (ht)->arData, \
  413. sizeof(Bucket) * ((ht)->nNumUsed - 1)); \
  414. (ht)->arData[0] = tmp; \
  415. zend_hash_rehash(ht); \
  416. } while (0)
  417. static Bucket *spl_find_registered_function(autoload_func_info *find_alfi) {
  418. if (!spl_autoload_functions) {
  419. return NULL;
  420. }
  421. autoload_func_info *alfi;
  422. ZEND_HASH_FOREACH_PTR(spl_autoload_functions, alfi) {
  423. if (autoload_func_info_equals(alfi, find_alfi)) {
  424. return _p;
  425. }
  426. } ZEND_HASH_FOREACH_END();
  427. return NULL;
  428. }
  429. /* {{{ Register given function as autoloader */
  430. PHP_FUNCTION(spl_autoload_register)
  431. {
  432. bool do_throw = 1;
  433. bool prepend = 0;
  434. zend_fcall_info fci = {0};
  435. zend_fcall_info_cache fcc;
  436. autoload_func_info *alfi;
  437. ZEND_PARSE_PARAMETERS_START(0, 3)
  438. Z_PARAM_OPTIONAL
  439. Z_PARAM_FUNC_OR_NULL(fci, fcc)
  440. Z_PARAM_BOOL(do_throw)
  441. Z_PARAM_BOOL(prepend)
  442. ZEND_PARSE_PARAMETERS_END();
  443. if (!do_throw) {
  444. php_error_docref(NULL, E_NOTICE, "Argument #2 ($do_throw) has been ignored, "
  445. "spl_autoload_register() will always throw");
  446. }
  447. if (!spl_autoload_functions) {
  448. ALLOC_HASHTABLE(spl_autoload_functions);
  449. zend_hash_init(spl_autoload_functions, 1, NULL, autoload_func_info_zval_dtor, 0);
  450. /* Initialize as non-packed hash table for prepend functionality. */
  451. zend_hash_real_init_mixed(spl_autoload_functions);
  452. }
  453. /* If first arg is not null */
  454. if (ZEND_FCI_INITIALIZED(fci)) {
  455. if (!fcc.function_handler) {
  456. /* Call trampoline has been cleared by zpp. Refetch it, because we want to deal
  457. * with it outselves. It is important that it is not refetched on every call,
  458. * because calls may occur from different scopes. */
  459. zend_is_callable_ex(&fci.function_name, NULL, 0, NULL, &fcc, NULL);
  460. }
  461. if (fcc.function_handler->type == ZEND_INTERNAL_FUNCTION &&
  462. fcc.function_handler->internal_function.handler == zif_spl_autoload_call) {
  463. zend_argument_value_error(1, "must not be the spl_autoload_call() function");
  464. RETURN_THROWS();
  465. }
  466. alfi = autoload_func_info_from_fci(&fci, &fcc);
  467. if (UNEXPECTED(alfi->func_ptr == &EG(trampoline))) {
  468. zend_function *copy = emalloc(sizeof(zend_op_array));
  469. memcpy(copy, alfi->func_ptr, sizeof(zend_op_array));
  470. alfi->func_ptr->common.function_name = NULL;
  471. alfi->func_ptr = copy;
  472. }
  473. } else {
  474. alfi = emalloc(sizeof(autoload_func_info));
  475. alfi->func_ptr = zend_hash_str_find_ptr(
  476. CG(function_table), "spl_autoload", sizeof("spl_autoload") - 1);
  477. alfi->obj = NULL;
  478. alfi->ce = NULL;
  479. alfi->closure = NULL;
  480. }
  481. if (spl_find_registered_function(alfi)) {
  482. autoload_func_info_destroy(alfi);
  483. RETURN_TRUE;
  484. }
  485. zend_hash_next_index_insert_ptr(spl_autoload_functions, alfi);
  486. if (prepend && spl_autoload_functions->nNumOfElements > 1) {
  487. /* Move the newly created element to the head of the hashtable */
  488. HT_MOVE_TAIL_TO_HEAD(spl_autoload_functions);
  489. }
  490. RETURN_TRUE;
  491. } /* }}} */
  492. /* {{{ Unregister given function as autoloader */
  493. PHP_FUNCTION(spl_autoload_unregister)
  494. {
  495. zend_fcall_info fci;
  496. zend_fcall_info_cache fcc;
  497. ZEND_PARSE_PARAMETERS_START(1, 1)
  498. Z_PARAM_FUNC(fci, fcc)
  499. ZEND_PARSE_PARAMETERS_END();
  500. if (fcc.function_handler && zend_string_equals_literal(
  501. fcc.function_handler->common.function_name, "spl_autoload_call")) {
  502. /* Don't destroy the hash table, as we might be iterating over it right now. */
  503. zend_hash_clean(spl_autoload_functions);
  504. RETURN_TRUE;
  505. }
  506. autoload_func_info *alfi = autoload_func_info_from_fci(&fci, &fcc);
  507. Bucket *p = spl_find_registered_function(alfi);
  508. autoload_func_info_destroy(alfi);
  509. if (p) {
  510. zend_hash_del_bucket(spl_autoload_functions, p);
  511. RETURN_TRUE;
  512. }
  513. RETURN_FALSE;
  514. } /* }}} */
  515. /* {{{ Return all registered autoloader functions */
  516. PHP_FUNCTION(spl_autoload_functions)
  517. {
  518. autoload_func_info *alfi;
  519. if (zend_parse_parameters_none() == FAILURE) {
  520. RETURN_THROWS();
  521. }
  522. array_init(return_value);
  523. if (spl_autoload_functions) {
  524. ZEND_HASH_FOREACH_PTR(spl_autoload_functions, alfi) {
  525. if (alfi->closure) {
  526. GC_ADDREF(alfi->closure);
  527. add_next_index_object(return_value, alfi->closure);
  528. } else if (alfi->func_ptr->common.scope) {
  529. zval tmp;
  530. array_init(&tmp);
  531. if (alfi->obj) {
  532. GC_ADDREF(alfi->obj);
  533. add_next_index_object(&tmp, alfi->obj);
  534. } else {
  535. add_next_index_str(&tmp, zend_string_copy(alfi->ce->name));
  536. }
  537. add_next_index_str(&tmp, zend_string_copy(alfi->func_ptr->common.function_name));
  538. add_next_index_zval(return_value, &tmp);
  539. } else {
  540. add_next_index_str(return_value, zend_string_copy(alfi->func_ptr->common.function_name));
  541. }
  542. } ZEND_HASH_FOREACH_END();
  543. }
  544. } /* }}} */
  545. /* {{{ Return hash id for given object */
  546. PHP_FUNCTION(spl_object_hash)
  547. {
  548. zend_object *obj;
  549. ZEND_PARSE_PARAMETERS_START(1, 1)
  550. Z_PARAM_OBJ(obj)
  551. ZEND_PARSE_PARAMETERS_END();
  552. RETURN_NEW_STR(php_spl_object_hash(obj));
  553. }
  554. /* }}} */
  555. /* {{{ Returns the integer object handle for the given object */
  556. PHP_FUNCTION(spl_object_id)
  557. {
  558. zend_object *obj;
  559. ZEND_PARSE_PARAMETERS_START(1, 1)
  560. Z_PARAM_OBJ(obj)
  561. ZEND_PARSE_PARAMETERS_END();
  562. RETURN_LONG((zend_long)obj->handle);
  563. }
  564. /* }}} */
  565. PHPAPI zend_string *php_spl_object_hash(zend_object *obj) /* {{{*/
  566. {
  567. return strpprintf(32, "%016zx0000000000000000", (intptr_t)obj->handle);
  568. }
  569. /* }}} */
  570. static void spl_build_class_list_string(zval *entry, char **list) /* {{{ */
  571. {
  572. char *res;
  573. spprintf(&res, 0, "%s, %s", *list, Z_STRVAL_P(entry));
  574. efree(*list);
  575. *list = res;
  576. } /* }}} */
  577. /* {{{ PHP_MINFO(spl) */
  578. PHP_MINFO_FUNCTION(spl)
  579. {
  580. zval list, *zv;
  581. char *strg;
  582. php_info_print_table_start();
  583. php_info_print_table_header(2, "SPL support", "enabled");
  584. array_init(&list);
  585. SPL_LIST_CLASSES(&list, 0, 1, ZEND_ACC_INTERFACE)
  586. strg = estrdup("");
  587. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(&list), zv) {
  588. spl_build_class_list_string(zv, &strg);
  589. } ZEND_HASH_FOREACH_END();
  590. zend_array_destroy(Z_ARR(list));
  591. php_info_print_table_row(2, "Interfaces", strg + 2);
  592. efree(strg);
  593. array_init(&list);
  594. SPL_LIST_CLASSES(&list, 0, -1, ZEND_ACC_INTERFACE)
  595. strg = estrdup("");
  596. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(&list), zv) {
  597. spl_build_class_list_string(zv, &strg);
  598. } ZEND_HASH_FOREACH_END();
  599. zend_array_destroy(Z_ARR(list));
  600. php_info_print_table_row(2, "Classes", strg + 2);
  601. efree(strg);
  602. php_info_print_table_end();
  603. }
  604. /* }}} */
  605. /* {{{ PHP_MINIT_FUNCTION(spl) */
  606. PHP_MINIT_FUNCTION(spl)
  607. {
  608. zend_autoload = spl_perform_autoload;
  609. PHP_MINIT(spl_exceptions)(INIT_FUNC_ARGS_PASSTHRU);
  610. PHP_MINIT(spl_iterators)(INIT_FUNC_ARGS_PASSTHRU);
  611. PHP_MINIT(spl_array)(INIT_FUNC_ARGS_PASSTHRU);
  612. PHP_MINIT(spl_directory)(INIT_FUNC_ARGS_PASSTHRU);
  613. PHP_MINIT(spl_dllist)(INIT_FUNC_ARGS_PASSTHRU);
  614. PHP_MINIT(spl_heap)(INIT_FUNC_ARGS_PASSTHRU);
  615. PHP_MINIT(spl_fixedarray)(INIT_FUNC_ARGS_PASSTHRU);
  616. PHP_MINIT(spl_observer)(INIT_FUNC_ARGS_PASSTHRU);
  617. return SUCCESS;
  618. }
  619. /* }}} */
  620. PHP_RINIT_FUNCTION(spl) /* {{{ */
  621. {
  622. spl_autoload_extensions = NULL;
  623. spl_autoload_functions = NULL;
  624. return SUCCESS;
  625. } /* }}} */
  626. PHP_RSHUTDOWN_FUNCTION(spl) /* {{{ */
  627. {
  628. if (spl_autoload_extensions) {
  629. zend_string_release_ex(spl_autoload_extensions, 0);
  630. spl_autoload_extensions = NULL;
  631. }
  632. if (spl_autoload_functions) {
  633. zend_hash_destroy(spl_autoload_functions);
  634. FREE_HASHTABLE(spl_autoload_functions);
  635. spl_autoload_functions = NULL;
  636. }
  637. return SUCCESS;
  638. } /* }}} */
  639. static const zend_module_dep spl_deps[] = {
  640. ZEND_MOD_REQUIRED("json")
  641. ZEND_MOD_END
  642. };
  643. zend_module_entry spl_module_entry = {
  644. STANDARD_MODULE_HEADER_EX, NULL,
  645. spl_deps,
  646. "SPL",
  647. ext_functions,
  648. PHP_MINIT(spl),
  649. NULL,
  650. PHP_RINIT(spl),
  651. PHP_RSHUTDOWN(spl),
  652. PHP_MINFO(spl),
  653. PHP_SPL_VERSION,
  654. STANDARD_MODULE_PROPERTIES
  655. };