dom_iterators.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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: Christian Stocker <chregu@php.net> |
  14. | Rob Richards <rrichards@php.net> |
  15. +----------------------------------------------------------------------+
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include "php.h"
  21. #if defined(HAVE_LIBXML) && defined(HAVE_DOM)
  22. #include "php_dom.h"
  23. #include "dom_ce.h"
  24. typedef struct _nodeIterator nodeIterator;
  25. struct _nodeIterator {
  26. int cur;
  27. int index;
  28. xmlNode *node;
  29. };
  30. typedef struct _notationIterator notationIterator;
  31. struct _notationIterator {
  32. int cur;
  33. int index;
  34. xmlNotation *notation;
  35. };
  36. #if LIBXML_VERSION >= 20908
  37. static void itemHashScanner (void *payload, void *data, const xmlChar *name) /* {{{ */
  38. #else
  39. static void itemHashScanner (void *payload, void *data, xmlChar *name)
  40. #endif
  41. {
  42. nodeIterator *priv = (nodeIterator *)data;
  43. if(priv->cur < priv->index) {
  44. priv->cur++;
  45. } else {
  46. if(priv->node == NULL) {
  47. priv->node = (xmlNode *)payload;
  48. }
  49. }
  50. }
  51. /* }}} */
  52. xmlNodePtr create_notation(const xmlChar *name, const xmlChar *ExternalID, const xmlChar *SystemID) /* {{{ */
  53. {
  54. xmlEntityPtr ret;
  55. ret = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
  56. memset(ret, 0, sizeof(xmlEntity));
  57. ret->type = XML_NOTATION_NODE;
  58. ret->name = xmlStrdup(name);
  59. ret->ExternalID = xmlStrdup(ExternalID);
  60. ret->SystemID = xmlStrdup(SystemID);
  61. ret->length = 0;
  62. ret->content = NULL;
  63. ret->URI = NULL;
  64. ret->orig = NULL;
  65. ret->children = NULL;
  66. ret->parent = NULL;
  67. ret->doc = NULL;
  68. ret->_private = NULL;
  69. ret->last = NULL;
  70. ret->prev = NULL;
  71. return((xmlNodePtr) ret);
  72. }
  73. /* }}} */
  74. xmlNode *php_dom_libxml_hash_iter(xmlHashTable *ht, int index) /* {{{ */
  75. {
  76. xmlNode *nodep = NULL;
  77. nodeIterator *iter;
  78. int htsize;
  79. if ((htsize = xmlHashSize(ht)) > 0 && index < htsize) {
  80. iter = emalloc(sizeof(nodeIterator));
  81. iter->cur = 0;
  82. iter->index = index;
  83. iter->node = NULL;
  84. xmlHashScan(ht, itemHashScanner, iter);
  85. nodep = iter->node;
  86. efree(iter);
  87. return nodep;
  88. } else {
  89. return NULL;
  90. }
  91. }
  92. /* }}} */
  93. xmlNode *php_dom_libxml_notation_iter(xmlHashTable *ht, int index) /* {{{ */
  94. {
  95. notationIterator *iter;
  96. xmlNotation *notep = NULL;
  97. int htsize;
  98. if ((htsize = xmlHashSize(ht)) > 0 && index < htsize) {
  99. iter = emalloc(sizeof(notationIterator));
  100. iter->cur = 0;
  101. iter->index = index;
  102. iter->notation = NULL;
  103. xmlHashScan(ht, itemHashScanner, iter);
  104. notep = iter->notation;
  105. efree(iter);
  106. return create_notation(notep->name, notep->PublicID, notep->SystemID);
  107. } else {
  108. return NULL;
  109. }
  110. }
  111. /* }}} */
  112. static void php_dom_iterator_dtor(zend_object_iterator *iter) /* {{{ */
  113. {
  114. php_dom_iterator *iterator = (php_dom_iterator *)iter;
  115. zval_ptr_dtor(&iterator->intern.data);
  116. zval_ptr_dtor(&iterator->curobj);
  117. }
  118. /* }}} */
  119. static int php_dom_iterator_valid(zend_object_iterator *iter) /* {{{ */
  120. {
  121. php_dom_iterator *iterator = (php_dom_iterator *)iter;
  122. if (Z_TYPE(iterator->curobj) != IS_UNDEF) {
  123. return SUCCESS;
  124. } else {
  125. return FAILURE;
  126. }
  127. }
  128. /* }}} */
  129. zval *php_dom_iterator_current_data(zend_object_iterator *iter) /* {{{ */
  130. {
  131. php_dom_iterator *iterator = (php_dom_iterator *)iter;
  132. return &iterator->curobj;
  133. }
  134. /* }}} */
  135. static void php_dom_iterator_current_key(zend_object_iterator *iter, zval *key) /* {{{ */
  136. {
  137. php_dom_iterator *iterator = (php_dom_iterator *)iter;
  138. zval *object = &iterator->intern.data;
  139. if (instanceof_function(Z_OBJCE_P(object), dom_nodelist_class_entry)) {
  140. ZVAL_LONG(key, iter->index);
  141. } else {
  142. dom_object *intern = Z_DOMOBJ_P(&iterator->curobj);
  143. if (intern != NULL && intern->ptr != NULL) {
  144. xmlNodePtr curnode = (xmlNodePtr)((php_libxml_node_ptr *)intern->ptr)->node;
  145. ZVAL_STRINGL(key, (char *) curnode->name, xmlStrlen(curnode->name));
  146. } else {
  147. ZVAL_NULL(key);
  148. }
  149. }
  150. }
  151. /* }}} */
  152. static void php_dom_iterator_move_forward(zend_object_iterator *iter) /* {{{ */
  153. {
  154. zval *object;
  155. xmlNodePtr curnode = NULL, basenode;
  156. dom_object *intern;
  157. dom_object *nnmap;
  158. dom_nnodemap_object *objmap;
  159. int previndex=0;
  160. HashTable *nodeht;
  161. zval *entry;
  162. bool do_curobj_undef = 1;
  163. php_dom_iterator *iterator = (php_dom_iterator *)iter;
  164. object = &iterator->intern.data;
  165. nnmap = Z_DOMOBJ_P(object);
  166. objmap = (dom_nnodemap_object *)nnmap->ptr;
  167. intern = Z_DOMOBJ_P(&iterator->curobj);
  168. if (intern != NULL && intern->ptr != NULL) {
  169. if (objmap->nodetype != XML_ENTITY_NODE &&
  170. objmap->nodetype != XML_NOTATION_NODE) {
  171. if (objmap->nodetype == DOM_NODESET) {
  172. nodeht = HASH_OF(&objmap->baseobj_zv);
  173. zend_hash_move_forward_ex(nodeht, &iterator->pos);
  174. if ((entry = zend_hash_get_current_data_ex(nodeht, &iterator->pos))) {
  175. zval_ptr_dtor(&iterator->curobj);
  176. ZVAL_UNDEF(&iterator->curobj);
  177. ZVAL_COPY(&iterator->curobj, entry);
  178. do_curobj_undef = 0;
  179. }
  180. } else {
  181. curnode = (xmlNodePtr)((php_libxml_node_ptr *)intern->ptr)->node;
  182. if (objmap->nodetype == XML_ATTRIBUTE_NODE ||
  183. objmap->nodetype == XML_ELEMENT_NODE) {
  184. curnode = curnode->next;
  185. } else {
  186. /* Nav the tree evey time as this is LIVE */
  187. basenode = dom_object_get_node(objmap->baseobj);
  188. if (basenode && (basenode->type == XML_DOCUMENT_NODE ||
  189. basenode->type == XML_HTML_DOCUMENT_NODE)) {
  190. basenode = xmlDocGetRootElement((xmlDoc *) basenode);
  191. } else if (basenode) {
  192. basenode = basenode->children;
  193. } else {
  194. goto err;
  195. }
  196. curnode = dom_get_elements_by_tag_name_ns_raw(
  197. basenode, (char *) objmap->ns, (char *) objmap->local, &previndex, iter->index);
  198. }
  199. }
  200. } else {
  201. if (objmap->nodetype == XML_ENTITY_NODE) {
  202. curnode = php_dom_libxml_hash_iter(objmap->ht, iter->index);
  203. } else {
  204. curnode = php_dom_libxml_notation_iter(objmap->ht, iter->index);
  205. }
  206. }
  207. }
  208. err:
  209. if (do_curobj_undef) {
  210. zval_ptr_dtor(&iterator->curobj);
  211. ZVAL_UNDEF(&iterator->curobj);
  212. }
  213. if (curnode) {
  214. php_dom_create_object(curnode, &iterator->curobj, objmap->baseobj);
  215. }
  216. }
  217. /* }}} */
  218. static const zend_object_iterator_funcs php_dom_iterator_funcs = {
  219. php_dom_iterator_dtor,
  220. php_dom_iterator_valid,
  221. php_dom_iterator_current_data,
  222. php_dom_iterator_current_key,
  223. php_dom_iterator_move_forward,
  224. NULL,
  225. NULL,
  226. NULL, /* get_gc */
  227. };
  228. zend_object_iterator *php_dom_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
  229. {
  230. dom_object *intern;
  231. dom_nnodemap_object *objmap;
  232. xmlNodePtr nodep, curnode=NULL;
  233. int curindex = 0;
  234. HashTable *nodeht;
  235. zval *entry;
  236. php_dom_iterator *iterator;
  237. if (by_ref) {
  238. zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
  239. return NULL;
  240. }
  241. iterator = emalloc(sizeof(php_dom_iterator));
  242. zend_iterator_init(&iterator->intern);
  243. ZVAL_OBJ_COPY(&iterator->intern.data, Z_OBJ_P(object));
  244. iterator->intern.funcs = &php_dom_iterator_funcs;
  245. ZVAL_UNDEF(&iterator->curobj);
  246. intern = Z_DOMOBJ_P(object);
  247. objmap = (dom_nnodemap_object *)intern->ptr;
  248. if (objmap != NULL) {
  249. if (objmap->nodetype != XML_ENTITY_NODE &&
  250. objmap->nodetype != XML_NOTATION_NODE) {
  251. if (objmap->nodetype == DOM_NODESET) {
  252. nodeht = HASH_OF(&objmap->baseobj_zv);
  253. zend_hash_internal_pointer_reset_ex(nodeht, &iterator->pos);
  254. if ((entry = zend_hash_get_current_data_ex(nodeht, &iterator->pos))) {
  255. ZVAL_COPY(&iterator->curobj, entry);
  256. }
  257. } else {
  258. nodep = (xmlNode *)dom_object_get_node(objmap->baseobj);
  259. if (!nodep) {
  260. goto err;
  261. }
  262. if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
  263. if (objmap->nodetype == XML_ATTRIBUTE_NODE) {
  264. curnode = (xmlNodePtr) nodep->properties;
  265. } else {
  266. curnode = (xmlNodePtr) nodep->children;
  267. }
  268. } else {
  269. if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
  270. nodep = xmlDocGetRootElement((xmlDoc *) nodep);
  271. } else {
  272. nodep = nodep->children;
  273. }
  274. curnode = dom_get_elements_by_tag_name_ns_raw(
  275. nodep, (char *) objmap->ns, (char *) objmap->local, &curindex, 0);
  276. }
  277. }
  278. } else {
  279. if (objmap->nodetype == XML_ENTITY_NODE) {
  280. curnode = php_dom_libxml_hash_iter(objmap->ht, 0);
  281. } else {
  282. curnode = php_dom_libxml_notation_iter(objmap->ht, 0);
  283. }
  284. }
  285. }
  286. err:
  287. if (curnode) {
  288. php_dom_create_object(curnode, &iterator->curobj, objmap->baseobj);
  289. }
  290. return &iterator->intern;
  291. }
  292. /* }}} */
  293. #endif