php_spl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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. ZVAL_UNDEF(&result);
  240. zend_execute(new_op_array, &result);
  241. destroy_op_array(new_op_array);
  242. efree(new_op_array);
  243. if (!EG(exception)) {
  244. zval_ptr_dtor(&result);
  245. }
  246. zend_destroy_file_handle(&file_handle);
  247. zend_string_release(class_file);
  248. return zend_hash_exists(EG(class_table), lc_name);
  249. }
  250. }
  251. zend_destroy_file_handle(&file_handle);
  252. zend_string_release(class_file);
  253. return 0;
  254. } /* }}} */
  255. /* {{{ Default autoloader implementation */
  256. PHP_FUNCTION(spl_autoload)
  257. {
  258. int pos_len, pos1_len;
  259. char *pos, *pos1;
  260. zend_string *class_name, *lc_name, *file_exts = NULL;
  261. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|S!", &class_name, &file_exts) == FAILURE) {
  262. RETURN_THROWS();
  263. }
  264. if (!file_exts) {
  265. file_exts = spl_autoload_extensions;
  266. }
  267. if (file_exts == NULL) { /* autoload_extensions is not initialized, set to defaults */
  268. pos = SPL_DEFAULT_FILE_EXTENSIONS;
  269. pos_len = sizeof(SPL_DEFAULT_FILE_EXTENSIONS) - 1;
  270. } else {
  271. pos = ZSTR_VAL(file_exts);
  272. pos_len = (int)ZSTR_LEN(file_exts);
  273. }
  274. lc_name = zend_string_tolower(class_name);
  275. while (pos && *pos && !EG(exception)) {
  276. pos1 = strchr(pos, ',');
  277. if (pos1) {
  278. pos1_len = (int)(pos1 - pos);
  279. } else {
  280. pos1_len = pos_len;
  281. }
  282. if (spl_autoload(class_name, lc_name, pos, pos1_len)) {
  283. break; /* loaded */
  284. }
  285. pos = pos1 ? pos1 + 1 : NULL;
  286. pos_len = pos1? pos_len - pos1_len - 1 : 0;
  287. }
  288. zend_string_release(lc_name);
  289. } /* }}} */
  290. /* {{{ Register and return default file extensions for spl_autoload */
  291. PHP_FUNCTION(spl_autoload_extensions)
  292. {
  293. zend_string *file_exts = NULL;
  294. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S!", &file_exts) == FAILURE) {
  295. RETURN_THROWS();
  296. }
  297. if (file_exts) {
  298. if (spl_autoload_extensions) {
  299. zend_string_release_ex(spl_autoload_extensions, 0);
  300. }
  301. spl_autoload_extensions = zend_string_copy(file_exts);
  302. }
  303. if (spl_autoload_extensions == NULL) {
  304. RETURN_STRINGL(SPL_DEFAULT_FILE_EXTENSIONS, sizeof(SPL_DEFAULT_FILE_EXTENSIONS) - 1);
  305. } else {
  306. zend_string_addref(spl_autoload_extensions);
  307. RETURN_STR(spl_autoload_extensions);
  308. }
  309. } /* }}} */
  310. typedef struct {
  311. zend_function *func_ptr;
  312. zend_object *obj;
  313. zend_object *closure;
  314. zend_class_entry *ce;
  315. } autoload_func_info;
  316. static void autoload_func_info_destroy(autoload_func_info *alfi) {
  317. if (alfi->obj) {
  318. zend_object_release(alfi->obj);
  319. }
  320. if (alfi->func_ptr &&
  321. UNEXPECTED(alfi->func_ptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
  322. zend_string_release_ex(alfi->func_ptr->common.function_name, 0);
  323. zend_free_trampoline(alfi->func_ptr);
  324. }
  325. if (alfi->closure) {
  326. zend_object_release(alfi->closure);
  327. }
  328. efree(alfi);
  329. }
  330. static void autoload_func_info_zval_dtor(zval *element)
  331. {
  332. autoload_func_info_destroy(Z_PTR_P(element));
  333. }
  334. static autoload_func_info *autoload_func_info_from_fci(
  335. zend_fcall_info *fci, zend_fcall_info_cache *fcc) {
  336. autoload_func_info *alfi = emalloc(sizeof(autoload_func_info));
  337. alfi->ce = fcc->calling_scope;
  338. alfi->func_ptr = fcc->function_handler;
  339. alfi->obj = fcc->object;
  340. if (alfi->obj) {
  341. GC_ADDREF(alfi->obj);
  342. }
  343. if (Z_TYPE(fci->function_name) == IS_OBJECT) {
  344. alfi->closure = Z_OBJ(fci->function_name);
  345. GC_ADDREF(alfi->closure);
  346. } else {
  347. alfi->closure = NULL;
  348. }
  349. return alfi;
  350. }
  351. static bool autoload_func_info_equals(
  352. const autoload_func_info *alfi1, const autoload_func_info *alfi2) {
  353. return alfi1->func_ptr == alfi2->func_ptr
  354. && alfi1->obj == alfi2->obj
  355. && alfi1->ce == alfi2->ce
  356. && alfi1->closure == alfi2->closure;
  357. }
  358. static zend_class_entry *spl_perform_autoload(zend_string *class_name, zend_string *lc_name) {
  359. if (!spl_autoload_functions) {
  360. return NULL;
  361. }
  362. /* We don't use ZEND_HASH_FOREACH here,
  363. * because autoloaders may be added/removed during autoloading. */
  364. HashPosition pos;
  365. zend_hash_internal_pointer_reset_ex(spl_autoload_functions, &pos);
  366. while (1) {
  367. autoload_func_info *alfi =
  368. zend_hash_get_current_data_ptr_ex(spl_autoload_functions, &pos);
  369. if (!alfi) {
  370. break;
  371. }
  372. zend_function *func = alfi->func_ptr;
  373. if (UNEXPECTED(func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
  374. func = emalloc(sizeof(zend_op_array));
  375. memcpy(func, alfi->func_ptr, sizeof(zend_op_array));
  376. zend_string_addref(func->op_array.function_name);
  377. }
  378. zval param;
  379. ZVAL_STR(&param, class_name);
  380. zend_call_known_function(func, alfi->obj, alfi->ce, NULL, 1, &param, NULL);
  381. if (EG(exception)) {
  382. break;
  383. }
  384. if (ZSTR_HAS_CE_CACHE(class_name) && ZSTR_GET_CE_CACHE(class_name)) {
  385. return (zend_class_entry*)ZSTR_GET_CE_CACHE(class_name);
  386. } else {
  387. zend_class_entry *ce = zend_hash_find_ptr(EG(class_table), lc_name);
  388. if (ce) {
  389. return ce;
  390. }
  391. }
  392. zend_hash_move_forward_ex(spl_autoload_functions, &pos);
  393. }
  394. return NULL;
  395. }
  396. /* {{{ Try all registered autoload function to load the requested class */
  397. PHP_FUNCTION(spl_autoload_call)
  398. {
  399. zend_string *class_name;
  400. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &class_name) == FAILURE) {
  401. RETURN_THROWS();
  402. }
  403. zend_string *lc_name = zend_string_tolower(class_name);
  404. spl_perform_autoload(class_name, lc_name);
  405. zend_string_release(lc_name);
  406. } /* }}} */
  407. #define HT_MOVE_TAIL_TO_HEAD(ht) \
  408. do { \
  409. Bucket tmp = (ht)->arData[(ht)->nNumUsed-1]; \
  410. memmove((ht)->arData + 1, (ht)->arData, \
  411. sizeof(Bucket) * ((ht)->nNumUsed - 1)); \
  412. (ht)->arData[0] = tmp; \
  413. zend_hash_rehash(ht); \
  414. } while (0)
  415. static Bucket *spl_find_registered_function(autoload_func_info *find_alfi) {
  416. if (!spl_autoload_functions) {
  417. return NULL;
  418. }
  419. autoload_func_info *alfi;
  420. ZEND_HASH_FOREACH_PTR(spl_autoload_functions, alfi) {
  421. if (autoload_func_info_equals(alfi, find_alfi)) {
  422. return _p;
  423. }
  424. } ZEND_HASH_FOREACH_END();
  425. return NULL;
  426. }
  427. /* {{{ Register given function as autoloader */
  428. PHP_FUNCTION(spl_autoload_register)
  429. {
  430. bool do_throw = 1;
  431. bool prepend = 0;
  432. zend_fcall_info fci = {0};
  433. zend_fcall_info_cache fcc;
  434. autoload_func_info *alfi;
  435. ZEND_PARSE_PARAMETERS_START(0, 3)
  436. Z_PARAM_OPTIONAL
  437. Z_PARAM_FUNC_OR_NULL(fci, fcc)
  438. Z_PARAM_BOOL(do_throw)
  439. Z_PARAM_BOOL(prepend)
  440. ZEND_PARSE_PARAMETERS_END();
  441. if (!do_throw) {
  442. php_error_docref(NULL, E_NOTICE, "Argument #2 ($do_throw) has been ignored, "
  443. "spl_autoload_register() will always throw");
  444. }
  445. if (!spl_autoload_functions) {
  446. ALLOC_HASHTABLE(spl_autoload_functions);
  447. zend_hash_init(spl_autoload_functions, 1, NULL, autoload_func_info_zval_dtor, 0);
  448. /* Initialize as non-packed hash table for prepend functionality. */
  449. zend_hash_real_init_mixed(spl_autoload_functions);
  450. }
  451. /* If first arg is not null */
  452. if (ZEND_FCI_INITIALIZED(fci)) {
  453. if (!fcc.function_handler) {
  454. /* Call trampoline has been cleared by zpp. Refetch it, because we want to deal
  455. * with it outselves. It is important that it is not refetched on every call,
  456. * because calls may occur from different scopes. */
  457. zend_is_callable_ex(&fci.function_name, NULL, 0, NULL, &fcc, NULL);
  458. }
  459. if (fcc.function_handler->type == ZEND_INTERNAL_FUNCTION &&
  460. fcc.function_handler->internal_function.handler == zif_spl_autoload_call) {
  461. zend_argument_value_error(1, "must not be the spl_autoload_call() function");
  462. RETURN_THROWS();
  463. }
  464. alfi = autoload_func_info_from_fci(&fci, &fcc);
  465. if (UNEXPECTED(alfi->func_ptr == &EG(trampoline))) {
  466. zend_function *copy = emalloc(sizeof(zend_op_array));
  467. memcpy(copy, alfi->func_ptr, sizeof(zend_op_array));
  468. alfi->func_ptr->common.function_name = NULL;
  469. alfi->func_ptr = copy;
  470. }
  471. } else {
  472. alfi = emalloc(sizeof(autoload_func_info));
  473. alfi->func_ptr = zend_hash_str_find_ptr(
  474. CG(function_table), "spl_autoload", sizeof("spl_autoload") - 1);
  475. alfi->obj = NULL;
  476. alfi->ce = NULL;
  477. alfi->closure = NULL;
  478. }
  479. if (spl_find_registered_function(alfi)) {
  480. autoload_func_info_destroy(alfi);
  481. RETURN_TRUE;
  482. }
  483. zend_hash_next_index_insert_ptr(spl_autoload_functions, alfi);
  484. if (prepend && spl_autoload_functions->nNumOfElements > 1) {
  485. /* Move the newly created element to the head of the hashtable */
  486. HT_MOVE_TAIL_TO_HEAD(spl_autoload_functions);
  487. }
  488. RETURN_TRUE;
  489. } /* }}} */
  490. /* {{{ Unregister given function as autoloader */
  491. PHP_FUNCTION(spl_autoload_unregister)
  492. {
  493. zend_fcall_info fci;
  494. zend_fcall_info_cache fcc;
  495. ZEND_PARSE_PARAMETERS_START(1, 1)
  496. Z_PARAM_FUNC(fci, fcc)
  497. ZEND_PARSE_PARAMETERS_END();
  498. if (fcc.function_handler && zend_string_equals_literal(
  499. fcc.function_handler->common.function_name, "spl_autoload_call")) {
  500. /* Don't destroy the hash table, as we might be iterating over it right now. */
  501. zend_hash_clean(spl_autoload_functions);
  502. RETURN_TRUE;
  503. }
  504. autoload_func_info *alfi = autoload_func_info_from_fci(&fci, &fcc);
  505. Bucket *p = spl_find_registered_function(alfi);
  506. autoload_func_info_destroy(alfi);
  507. if (p) {
  508. zend_hash_del_bucket(spl_autoload_functions, p);
  509. RETURN_TRUE;
  510. }
  511. RETURN_FALSE;
  512. } /* }}} */
  513. /* {{{ Return all registered autoloader functions */
  514. PHP_FUNCTION(spl_autoload_functions)
  515. {
  516. autoload_func_info *alfi;
  517. if (zend_parse_parameters_none() == FAILURE) {
  518. RETURN_THROWS();
  519. }
  520. array_init(return_value);
  521. if (spl_autoload_functions) {
  522. ZEND_HASH_FOREACH_PTR(spl_autoload_functions, alfi) {
  523. if (alfi->closure) {
  524. GC_ADDREF(alfi->closure);
  525. add_next_index_object(return_value, alfi->closure);
  526. } else if (alfi->func_ptr->common.scope) {
  527. zval tmp;
  528. array_init(&tmp);
  529. if (alfi->obj) {
  530. GC_ADDREF(alfi->obj);
  531. add_next_index_object(&tmp, alfi->obj);
  532. } else {
  533. add_next_index_str(&tmp, zend_string_copy(alfi->ce->name));
  534. }
  535. add_next_index_str(&tmp, zend_string_copy(alfi->func_ptr->common.function_name));
  536. add_next_index_zval(return_value, &tmp);
  537. } else {
  538. add_next_index_str(return_value, zend_string_copy(alfi->func_ptr->common.function_name));
  539. }
  540. } ZEND_HASH_FOREACH_END();
  541. }
  542. } /* }}} */
  543. /* {{{ Return hash id for given object */
  544. PHP_FUNCTION(spl_object_hash)
  545. {
  546. zend_object *obj;
  547. ZEND_PARSE_PARAMETERS_START(1, 1)
  548. Z_PARAM_OBJ(obj)
  549. ZEND_PARSE_PARAMETERS_END();
  550. RETURN_NEW_STR(php_spl_object_hash(obj));
  551. }
  552. /* }}} */
  553. /* {{{ Returns the integer object handle for the given object */
  554. PHP_FUNCTION(spl_object_id)
  555. {
  556. zend_object *obj;
  557. ZEND_PARSE_PARAMETERS_START(1, 1)
  558. Z_PARAM_OBJ(obj)
  559. ZEND_PARSE_PARAMETERS_END();
  560. RETURN_LONG((zend_long)obj->handle);
  561. }
  562. /* }}} */
  563. PHPAPI zend_string *php_spl_object_hash(zend_object *obj) /* {{{*/
  564. {
  565. return strpprintf(32, "%016zx0000000000000000", (intptr_t)obj->handle);
  566. }
  567. /* }}} */
  568. static void spl_build_class_list_string(zval *entry, char **list) /* {{{ */
  569. {
  570. char *res;
  571. spprintf(&res, 0, "%s, %s", *list, Z_STRVAL_P(entry));
  572. efree(*list);
  573. *list = res;
  574. } /* }}} */
  575. /* {{{ PHP_MINFO(spl) */
  576. PHP_MINFO_FUNCTION(spl)
  577. {
  578. zval list, *zv;
  579. char *strg;
  580. php_info_print_table_start();
  581. php_info_print_table_header(2, "SPL support", "enabled");
  582. array_init(&list);
  583. SPL_LIST_CLASSES(&list, 0, 1, ZEND_ACC_INTERFACE)
  584. strg = estrdup("");
  585. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(&list), zv) {
  586. spl_build_class_list_string(zv, &strg);
  587. } ZEND_HASH_FOREACH_END();
  588. zend_array_destroy(Z_ARR(list));
  589. php_info_print_table_row(2, "Interfaces", strg + 2);
  590. efree(strg);
  591. array_init(&list);
  592. SPL_LIST_CLASSES(&list, 0, -1, ZEND_ACC_INTERFACE)
  593. strg = estrdup("");
  594. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(&list), zv) {
  595. spl_build_class_list_string(zv, &strg);
  596. } ZEND_HASH_FOREACH_END();
  597. zend_array_destroy(Z_ARR(list));
  598. php_info_print_table_row(2, "Classes", strg + 2);
  599. efree(strg);
  600. php_info_print_table_end();
  601. }
  602. /* }}} */
  603. /* {{{ PHP_MINIT_FUNCTION(spl) */
  604. PHP_MINIT_FUNCTION(spl)
  605. {
  606. zend_autoload = spl_perform_autoload;
  607. PHP_MINIT(spl_exceptions)(INIT_FUNC_ARGS_PASSTHRU);
  608. PHP_MINIT(spl_iterators)(INIT_FUNC_ARGS_PASSTHRU);
  609. PHP_MINIT(spl_array)(INIT_FUNC_ARGS_PASSTHRU);
  610. PHP_MINIT(spl_directory)(INIT_FUNC_ARGS_PASSTHRU);
  611. PHP_MINIT(spl_dllist)(INIT_FUNC_ARGS_PASSTHRU);
  612. PHP_MINIT(spl_heap)(INIT_FUNC_ARGS_PASSTHRU);
  613. PHP_MINIT(spl_fixedarray)(INIT_FUNC_ARGS_PASSTHRU);
  614. PHP_MINIT(spl_observer)(INIT_FUNC_ARGS_PASSTHRU);
  615. return SUCCESS;
  616. }
  617. /* }}} */
  618. PHP_RINIT_FUNCTION(spl) /* {{{ */
  619. {
  620. spl_autoload_extensions = NULL;
  621. spl_autoload_functions = NULL;
  622. return SUCCESS;
  623. } /* }}} */
  624. PHP_RSHUTDOWN_FUNCTION(spl) /* {{{ */
  625. {
  626. if (spl_autoload_extensions) {
  627. zend_string_release_ex(spl_autoload_extensions, 0);
  628. spl_autoload_extensions = NULL;
  629. }
  630. if (spl_autoload_functions) {
  631. zend_hash_destroy(spl_autoload_functions);
  632. FREE_HASHTABLE(spl_autoload_functions);
  633. spl_autoload_functions = NULL;
  634. }
  635. return SUCCESS;
  636. } /* }}} */
  637. static const zend_module_dep spl_deps[] = {
  638. ZEND_MOD_REQUIRED("json")
  639. ZEND_MOD_END
  640. };
  641. zend_module_entry spl_module_entry = {
  642. STANDARD_MODULE_HEADER_EX, NULL,
  643. spl_deps,
  644. "SPL",
  645. ext_functions,
  646. PHP_MINIT(spl),
  647. NULL,
  648. PHP_RINIT(spl),
  649. PHP_RSHUTDOWN(spl),
  650. PHP_MINFO(spl),
  651. PHP_SPL_VERSION,
  652. STANDARD_MODULE_PROPERTIES
  653. };