php_xmlreader.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Rob Richards <rrichards@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include "php.h"
  23. #include "php_ini.h"
  24. #include "ext/standard/info.h"
  25. #include "php_xmlreader.h"
  26. #ifdef HAVE_DOM
  27. #include "ext/dom/xml_common.h"
  28. #endif
  29. #include <libxml/xmlreader.h>
  30. #include <libxml/uri.h>
  31. zend_class_entry *xmlreader_class_entry;
  32. static zend_object_handlers xmlreader_object_handlers;
  33. static HashTable xmlreader_prop_handlers;
  34. typedef int (*xmlreader_read_int_t)(xmlTextReaderPtr reader);
  35. typedef unsigned char *(*xmlreader_read_char_t)(xmlTextReaderPtr reader);
  36. typedef const unsigned char *(*xmlreader_read_const_char_t)(xmlTextReaderPtr reader);
  37. typedef int (*xmlreader_write_t)(xmlreader_object *obj, zval *newval TSRMLS_DC);
  38. typedef unsigned char *(*xmlreader_read_one_char_t)(xmlTextReaderPtr reader, const unsigned char *);
  39. typedef struct _xmlreader_prop_handler {
  40. xmlreader_read_int_t read_int_func;
  41. xmlreader_read_const_char_t read_char_func;
  42. xmlreader_write_t write_func;
  43. int type;
  44. } xmlreader_prop_handler;
  45. #define XMLREADER_LOAD_STRING 0
  46. #define XMLREADER_LOAD_FILE 1
  47. /* {{{ xmlreader_register_prop_handler */
  48. static void xmlreader_register_prop_handler(HashTable *prop_handler, char *name, xmlreader_read_int_t read_int_func, xmlreader_read_const_char_t read_char_func, int rettype TSRMLS_DC)
  49. {
  50. xmlreader_prop_handler hnd;
  51. hnd.read_char_func = read_char_func;
  52. hnd.read_int_func = read_int_func;
  53. hnd.type = rettype;
  54. zend_hash_add(prop_handler, name, strlen(name)+1, &hnd, sizeof(xmlreader_prop_handler), NULL);
  55. }
  56. /* }}} */
  57. /* {{{ xmlreader_property_reader */
  58. static int xmlreader_property_reader(xmlreader_object *obj, xmlreader_prop_handler *hnd, zval **retval TSRMLS_DC)
  59. {
  60. const xmlChar *retchar = NULL;
  61. int retint = 0;
  62. if (obj->ptr != NULL) {
  63. if (hnd->read_char_func) {
  64. retchar = hnd->read_char_func(obj->ptr);
  65. } else {
  66. if (hnd->read_int_func) {
  67. retint = hnd->read_int_func(obj->ptr);
  68. if (retint == -1) {
  69. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Internal libxml error returned");
  70. return FAILURE;
  71. }
  72. }
  73. }
  74. }
  75. ALLOC_ZVAL(*retval);
  76. switch (hnd->type) {
  77. case IS_STRING:
  78. if (retchar) {
  79. ZVAL_STRING(*retval, (char *) retchar, 1);
  80. } else {
  81. ZVAL_EMPTY_STRING(*retval);
  82. }
  83. break;
  84. case IS_BOOL:
  85. ZVAL_BOOL(*retval, retint);
  86. break;
  87. case IS_LONG:
  88. ZVAL_LONG(*retval, retint);
  89. break;
  90. default:
  91. ZVAL_NULL(*retval);
  92. }
  93. return SUCCESS;
  94. }
  95. /* }}} */
  96. /* {{{ xmlreader_get_property_ptr_ptr */
  97. zval **xmlreader_get_property_ptr_ptr(zval *object, zval *member, int type, const zend_literal *key TSRMLS_DC)
  98. {
  99. xmlreader_object *obj;
  100. zval tmp_member;
  101. zval **retval = NULL;
  102. xmlreader_prop_handler *hnd;
  103. zend_object_handlers *std_hnd;
  104. int ret = FAILURE;
  105. if (member->type != IS_STRING) {
  106. tmp_member = *member;
  107. zval_copy_ctor(&tmp_member);
  108. convert_to_string(&tmp_member);
  109. member = &tmp_member;
  110. }
  111. obj = (xmlreader_object *)zend_objects_get_address(object TSRMLS_CC);
  112. if (obj->prop_handler != NULL) {
  113. ret = zend_hash_find(obj->prop_handler, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, (void **) &hnd);
  114. }
  115. if (ret == FAILURE) {
  116. std_hnd = zend_get_std_object_handlers();
  117. retval = std_hnd->get_property_ptr_ptr(object, member, type, key TSRMLS_CC);
  118. }
  119. if (member == &tmp_member) {
  120. zval_dtor(member);
  121. }
  122. return retval;
  123. }
  124. /* }}} */
  125. /* {{{ xmlreader_read_property */
  126. zval *xmlreader_read_property(zval *object, zval *member, int type, const zend_literal *key TSRMLS_DC)
  127. {
  128. xmlreader_object *obj;
  129. zval tmp_member;
  130. zval *retval;
  131. xmlreader_prop_handler *hnd;
  132. zend_object_handlers *std_hnd;
  133. int ret;
  134. if (member->type != IS_STRING) {
  135. tmp_member = *member;
  136. zval_copy_ctor(&tmp_member);
  137. convert_to_string(&tmp_member);
  138. member = &tmp_member;
  139. }
  140. ret = FAILURE;
  141. obj = (xmlreader_object *)zend_objects_get_address(object TSRMLS_CC);
  142. if (obj->prop_handler != NULL) {
  143. ret = zend_hash_find(obj->prop_handler, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, (void **) &hnd);
  144. }
  145. if (ret == SUCCESS) {
  146. ret = xmlreader_property_reader(obj, hnd, &retval TSRMLS_CC);
  147. if (ret == SUCCESS) {
  148. /* ensure we're creating a temporary variable */
  149. Z_SET_REFCOUNT_P(retval, 0);
  150. } else {
  151. retval = EG(uninitialized_zval_ptr);
  152. }
  153. } else {
  154. std_hnd = zend_get_std_object_handlers();
  155. retval = std_hnd->read_property(object, member, type, key TSRMLS_CC);
  156. }
  157. if (member == &tmp_member) {
  158. zval_dtor(member);
  159. }
  160. return retval;
  161. }
  162. /* }}} */
  163. /* {{{ xmlreader_write_property */
  164. void xmlreader_write_property(zval *object, zval *member, zval *value, const zend_literal *key TSRMLS_DC)
  165. {
  166. xmlreader_object *obj;
  167. zval tmp_member;
  168. xmlreader_prop_handler *hnd;
  169. zend_object_handlers *std_hnd;
  170. int ret;
  171. if (member->type != IS_STRING) {
  172. tmp_member = *member;
  173. zval_copy_ctor(&tmp_member);
  174. convert_to_string(&tmp_member);
  175. member = &tmp_member;
  176. }
  177. ret = FAILURE;
  178. obj = (xmlreader_object *)zend_objects_get_address(object TSRMLS_CC);
  179. if (obj->prop_handler != NULL) {
  180. ret = zend_hash_find((HashTable *)obj->prop_handler, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, (void **) &hnd);
  181. }
  182. if (ret == SUCCESS) {
  183. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot write to read-only property");
  184. } else {
  185. std_hnd = zend_get_std_object_handlers();
  186. std_hnd->write_property(object, member, value, key TSRMLS_CC);
  187. }
  188. if (member == &tmp_member) {
  189. zval_dtor(member);
  190. }
  191. }
  192. /* }}} */
  193. /* {{{ _xmlreader_get_valid_file_path */
  194. /* _xmlreader_get_valid_file_path and _xmlreader_get_relaxNG should be made a
  195. common function in libxml extension as code is common to a few xml extensions */
  196. char *_xmlreader_get_valid_file_path(char *source, char *resolved_path, int resolved_path_len TSRMLS_DC) {
  197. xmlURI *uri;
  198. xmlChar *escsource;
  199. char *file_dest;
  200. int isFileUri = 0;
  201. uri = xmlCreateURI();
  202. escsource = xmlURIEscapeStr((xmlChar *)source, (xmlChar *)":");
  203. xmlParseURIReference(uri, (const char *)escsource);
  204. xmlFree(escsource);
  205. if (uri->scheme != NULL) {
  206. /* absolute file uris - libxml only supports localhost or empty host */
  207. if (strncasecmp(source, "file:///",8) == 0) {
  208. isFileUri = 1;
  209. #ifdef PHP_WIN32
  210. source += 8;
  211. #else
  212. source += 7;
  213. #endif
  214. } else if (strncasecmp(source, "file://localhost/",17) == 0) {
  215. isFileUri = 1;
  216. #ifdef PHP_WIN32
  217. source += 17;
  218. #else
  219. source += 16;
  220. #endif
  221. }
  222. }
  223. file_dest = source;
  224. if ((uri->scheme == NULL || isFileUri)) {
  225. if (!VCWD_REALPATH(source, resolved_path) && !expand_filepath(source, resolved_path TSRMLS_CC)) {
  226. xmlFreeURI(uri);
  227. return NULL;
  228. }
  229. file_dest = resolved_path;
  230. }
  231. xmlFreeURI(uri);
  232. return file_dest;
  233. }
  234. /* }}} */
  235. #ifdef LIBXML_SCHEMAS_ENABLED
  236. /* {{{ _xmlreader_get_relaxNG */
  237. static xmlRelaxNGPtr _xmlreader_get_relaxNG(char *source, int source_len, int type,
  238. xmlRelaxNGValidityErrorFunc error_func,
  239. xmlRelaxNGValidityWarningFunc warn_func TSRMLS_DC)
  240. {
  241. char *valid_file = NULL;
  242. xmlRelaxNGParserCtxtPtr parser = NULL;
  243. xmlRelaxNGPtr sptr;
  244. char resolved_path[MAXPATHLEN + 1];
  245. switch (type) {
  246. case XMLREADER_LOAD_FILE:
  247. valid_file = _xmlreader_get_valid_file_path(source, resolved_path, MAXPATHLEN TSRMLS_CC);
  248. if (!valid_file) {
  249. return NULL;
  250. }
  251. parser = xmlRelaxNGNewParserCtxt(valid_file);
  252. break;
  253. case XMLREADER_LOAD_STRING:
  254. parser = xmlRelaxNGNewMemParserCtxt(source, source_len);
  255. /* If loading from memory, we need to set the base directory for the document
  256. but it is not apparent how to do that for schema's */
  257. break;
  258. default:
  259. return NULL;
  260. }
  261. if (parser == NULL) {
  262. return NULL;
  263. }
  264. if (error_func || warn_func) {
  265. xmlRelaxNGSetParserErrors(parser,
  266. (xmlRelaxNGValidityErrorFunc) error_func,
  267. (xmlRelaxNGValidityWarningFunc) warn_func,
  268. parser);
  269. }
  270. sptr = xmlRelaxNGParse(parser);
  271. xmlRelaxNGFreeParserCtxt(parser);
  272. return sptr;
  273. }
  274. /* }}} */
  275. #endif
  276. static const zend_module_dep xmlreader_deps[] = {
  277. ZEND_MOD_REQUIRED("libxml")
  278. ZEND_MOD_END
  279. };
  280. /* {{{ xmlreader_module_entry
  281. */
  282. zend_module_entry xmlreader_module_entry = {
  283. STANDARD_MODULE_HEADER_EX, NULL,
  284. xmlreader_deps,
  285. "xmlreader",
  286. NULL,
  287. PHP_MINIT(xmlreader),
  288. PHP_MSHUTDOWN(xmlreader),
  289. NULL,
  290. NULL,
  291. PHP_MINFO(xmlreader),
  292. "0.1", /* Replace with version number for your extension */
  293. STANDARD_MODULE_PROPERTIES
  294. };
  295. /* }}} */
  296. #ifdef COMPILE_DL_XMLREADER
  297. ZEND_GET_MODULE(xmlreader)
  298. #endif
  299. /* {{{ xmlreader_objects_clone */
  300. void xmlreader_objects_clone(void *object, void **object_clone TSRMLS_DC)
  301. {
  302. /* TODO */
  303. }
  304. /* }}} */
  305. /* {{{ xmlreader_free_resources */
  306. static void xmlreader_free_resources(xmlreader_object *intern) {
  307. if (intern) {
  308. if (intern->input) {
  309. xmlFreeParserInputBuffer(intern->input);
  310. intern->input = NULL;
  311. }
  312. if (intern->ptr) {
  313. xmlFreeTextReader(intern->ptr);
  314. intern->ptr = NULL;
  315. }
  316. #ifdef LIBXML_SCHEMAS_ENABLED
  317. if (intern->schema) {
  318. xmlRelaxNGFree((xmlRelaxNGPtr) intern->schema);
  319. intern->schema = NULL;
  320. }
  321. #endif
  322. }
  323. }
  324. /* }}} */
  325. /* {{{ xmlreader_objects_free_storage */
  326. void xmlreader_objects_free_storage(void *object TSRMLS_DC)
  327. {
  328. xmlreader_object *intern = (xmlreader_object *)object;
  329. zend_object_std_dtor(&intern->std TSRMLS_CC);
  330. xmlreader_free_resources(intern);
  331. efree(object);
  332. }
  333. /* }}} */
  334. /* {{{ xmlreader_objects_new */
  335. zend_object_value xmlreader_objects_new(zend_class_entry *class_type TSRMLS_DC)
  336. {
  337. zend_object_value retval;
  338. xmlreader_object *intern;
  339. intern = emalloc(sizeof(xmlreader_object));
  340. memset(&intern->std, 0, sizeof(zend_object));
  341. intern->ptr = NULL;
  342. intern->input = NULL;
  343. intern->schema = NULL;
  344. intern->prop_handler = &xmlreader_prop_handlers;
  345. zend_object_std_init(&intern->std, class_type TSRMLS_CC);
  346. object_properties_init(&intern->std, class_type);
  347. retval.handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, (zend_objects_free_object_storage_t) xmlreader_objects_free_storage, xmlreader_objects_clone TSRMLS_CC);
  348. intern->handle = retval.handle;
  349. retval.handlers = &xmlreader_object_handlers;
  350. return retval;
  351. }
  352. /* }}} */
  353. /* {{{ php_xmlreader_string_arg */
  354. static void php_xmlreader_string_arg(INTERNAL_FUNCTION_PARAMETERS, xmlreader_read_one_char_t internal_function) {
  355. zval *id;
  356. int name_len = 0;
  357. char *retchar = NULL;
  358. xmlreader_object *intern;
  359. char *name;
  360. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) {
  361. return;
  362. }
  363. if (!name_len) {
  364. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Argument cannot be an empty string");
  365. RETURN_FALSE;
  366. }
  367. id = getThis();
  368. intern = (xmlreader_object *)zend_object_store_get_object(id TSRMLS_CC);
  369. if (intern && intern->ptr) {
  370. retchar = (char *)internal_function(intern->ptr, (const unsigned char *)name);
  371. }
  372. if (retchar) {
  373. RETVAL_STRING(retchar, 1);
  374. xmlFree(retchar);
  375. return;
  376. } else {
  377. RETVAL_NULL();
  378. }
  379. }
  380. /* }}} */
  381. /* {{{ php_xmlreader_no_arg */
  382. static void php_xmlreader_no_arg(INTERNAL_FUNCTION_PARAMETERS, xmlreader_read_int_t internal_function) {
  383. zval *id;
  384. int retval;
  385. xmlreader_object *intern;
  386. id = getThis();
  387. intern = (xmlreader_object *)zend_object_store_get_object(id TSRMLS_CC);
  388. if (intern && intern->ptr) {
  389. retval = internal_function(intern->ptr);
  390. if (retval == 1) {
  391. RETURN_TRUE;
  392. }
  393. }
  394. RETURN_FALSE;
  395. }
  396. /* }}} */
  397. #if LIBXML_VERSION >= 20620
  398. /* {{{ php_xmlreader_no_arg_string */
  399. static void php_xmlreader_no_arg_string(INTERNAL_FUNCTION_PARAMETERS, xmlreader_read_char_t internal_function) {
  400. zval *id;
  401. char *retchar = NULL;
  402. xmlreader_object *intern;
  403. id = getThis();
  404. intern = (xmlreader_object *)zend_object_store_get_object(id TSRMLS_CC);
  405. if (intern && intern->ptr) {
  406. retchar = (char *)internal_function(intern->ptr);
  407. }
  408. if (retchar) {
  409. RETVAL_STRING(retchar, 1);
  410. xmlFree(retchar);
  411. return;
  412. } else {
  413. RETVAL_EMPTY_STRING();
  414. }
  415. }
  416. /* }}} */
  417. #endif
  418. /* {{{ php_xmlreader_set_relaxng_schema */
  419. static void php_xmlreader_set_relaxng_schema(INTERNAL_FUNCTION_PARAMETERS, int type) {
  420. #ifdef LIBXML_SCHEMAS_ENABLED
  421. zval *id;
  422. int source_len = 0, retval = -1;
  423. xmlreader_object *intern;
  424. xmlRelaxNGPtr schema = NULL;
  425. char *source;
  426. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p!", &source, &source_len) == FAILURE) {
  427. return;
  428. }
  429. if (source != NULL && !source_len) {
  430. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Schema data source is required");
  431. RETURN_FALSE;
  432. }
  433. id = getThis();
  434. intern = (xmlreader_object *)zend_object_store_get_object(id TSRMLS_CC);
  435. if (intern && intern->ptr) {
  436. if (source) {
  437. schema = _xmlreader_get_relaxNG(source, source_len, type, NULL, NULL TSRMLS_CC);
  438. if (schema) {
  439. retval = xmlTextReaderRelaxNGSetSchema(intern->ptr, schema);
  440. }
  441. } else {
  442. /* unset the associated relaxNG context and schema if one exists */
  443. retval = xmlTextReaderRelaxNGSetSchema(intern->ptr, NULL);
  444. }
  445. if (retval == 0) {
  446. if (intern->schema) {
  447. xmlRelaxNGFree((xmlRelaxNGPtr) intern->schema);
  448. }
  449. intern->schema = schema;
  450. RETURN_TRUE;
  451. }
  452. }
  453. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to set schema. This must be set prior to reading or schema contains errors.");
  454. RETURN_FALSE;
  455. #else
  456. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No Schema support built into libxml.");
  457. RETURN_FALSE;
  458. #endif
  459. }
  460. /* }}} */
  461. /* {{{ proto boolean XMLReader::close()
  462. Closes xmlreader - current frees resources until xmlTextReaderClose is fixed in libxml */
  463. PHP_METHOD(xmlreader, close)
  464. {
  465. zval *id;
  466. xmlreader_object *intern;
  467. id = getThis();
  468. intern = (xmlreader_object *)zend_object_store_get_object(id TSRMLS_CC);
  469. /* libxml is segfaulting in versions up to 2.6.8 using xmlTextReaderClose so for
  470. now we will free the whole reader when close is called as it would get rebuilt on
  471. a new load anyways */
  472. xmlreader_free_resources(intern);
  473. RETURN_TRUE;
  474. }
  475. /* }}} */
  476. /* {{{ proto string XMLReader::getAttribute(string name)
  477. Get value of an attribute from current element */
  478. PHP_METHOD(xmlreader, getAttribute)
  479. {
  480. php_xmlreader_string_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextReaderGetAttribute);
  481. }
  482. /* }}} */
  483. /* {{{ proto string XMLReader::getAttributeNo(int index)
  484. Get value of an attribute at index from current element */
  485. PHP_METHOD(xmlreader, getAttributeNo)
  486. {
  487. zval *id;
  488. long attr_pos;
  489. char *retchar = NULL;
  490. xmlreader_object *intern;
  491. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &attr_pos) == FAILURE) {
  492. return;
  493. }
  494. id = getThis();
  495. intern = (xmlreader_object *)zend_object_store_get_object(id TSRMLS_CC);
  496. if (intern && intern->ptr) {
  497. retchar = (char *)xmlTextReaderGetAttributeNo(intern->ptr, attr_pos);
  498. }
  499. if (retchar) {
  500. RETVAL_STRING(retchar, 1);
  501. xmlFree(retchar);
  502. }
  503. }
  504. /* }}} */
  505. /* {{{ proto string XMLReader::getAttributeNs(string name, string namespaceURI)
  506. Get value of a attribute via name and namespace from current element */
  507. PHP_METHOD(xmlreader, getAttributeNs)
  508. {
  509. zval *id;
  510. int name_len = 0, ns_uri_len = 0;
  511. xmlreader_object *intern;
  512. char *name, *ns_uri, *retchar = NULL;
  513. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &name, &name_len, &ns_uri, &ns_uri_len) == FAILURE) {
  514. return;
  515. }
  516. if (name_len == 0 || ns_uri_len == 0) {
  517. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attribute Name and Namespace URI cannot be empty");
  518. RETURN_FALSE;
  519. }
  520. id = getThis();
  521. intern = (xmlreader_object *)zend_object_store_get_object(id TSRMLS_CC);
  522. if (intern && intern->ptr) {
  523. retchar = (char *)xmlTextReaderGetAttributeNs(intern->ptr, (xmlChar *)name, (xmlChar *)ns_uri);
  524. }
  525. if (retchar) {
  526. RETVAL_STRING(retchar, 1);
  527. xmlFree(retchar);
  528. }
  529. }
  530. /* }}} */
  531. /* {{{ proto boolean XMLReader::getParserProperty(int property)
  532. Indicates whether given property (one of the parser option constants) is set or not on parser */
  533. PHP_METHOD(xmlreader, getParserProperty)
  534. {
  535. zval *id;
  536. long property;
  537. int retval = -1;
  538. xmlreader_object *intern;
  539. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &property) == FAILURE) {
  540. return;
  541. }
  542. id = getThis();
  543. intern = (xmlreader_object *)zend_object_store_get_object(id TSRMLS_CC);
  544. if (intern && intern->ptr) {
  545. retval = xmlTextReaderGetParserProp(intern->ptr,property);
  546. }
  547. if (retval == -1) {
  548. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid parser property");
  549. RETURN_FALSE;
  550. }
  551. RETURN_BOOL(retval);
  552. }
  553. /* }}} */
  554. /* {{{ proto boolean XMLReader::isValid()
  555. Returns boolean indicating if parsed document is valid or not.
  556. Must set XMLREADER_LOADDTD or XMLREADER_VALIDATE parser option prior to the first call to read
  557. or this method will always return FALSE */
  558. PHP_METHOD(xmlreader, isValid)
  559. {
  560. php_xmlreader_no_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextReaderIsValid);
  561. }
  562. /* }}} */
  563. /* {{{ proto string XMLReader::lookupNamespace(string prefix)
  564. Return namespaceURI for associated prefix on current node */
  565. PHP_METHOD(xmlreader, lookupNamespace)
  566. {
  567. php_xmlreader_string_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextReaderLookupNamespace);
  568. }
  569. /* }}} */
  570. /* {{{ proto boolean XMLReader::moveToAttribute(string name)
  571. Positions reader at specified attribute - Returns TRUE on success and FALSE on failure */
  572. PHP_METHOD(xmlreader, moveToAttribute)
  573. {
  574. zval *id;
  575. int name_len = 0, retval;
  576. xmlreader_object *intern;
  577. char *name;
  578. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) {
  579. return;
  580. }
  581. if (name_len == 0) {
  582. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attribute Name is required");
  583. RETURN_FALSE;
  584. }
  585. id = getThis();
  586. intern = (xmlreader_object *)zend_object_store_get_object(id TSRMLS_CC);
  587. if (intern && intern->ptr) {
  588. retval = xmlTextReaderMoveToAttribute(intern->ptr, (xmlChar *)name);
  589. if (retval == 1) {
  590. RETURN_TRUE;
  591. }
  592. }
  593. RETURN_FALSE;
  594. }
  595. /* }}} */
  596. /* {{{ proto boolean XMLReader::moveToAttributeNo(int index)
  597. Positions reader at attribute at specified index.
  598. Returns TRUE on success and FALSE on failure */
  599. PHP_METHOD(xmlreader, moveToAttributeNo)
  600. {
  601. zval *id;
  602. long attr_pos;
  603. int retval;
  604. xmlreader_object *intern;
  605. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &attr_pos) == FAILURE) {
  606. return;
  607. }
  608. id = getThis();
  609. intern = (xmlreader_object *)zend_object_store_get_object(id TSRMLS_CC);
  610. if (intern && intern->ptr) {
  611. retval = xmlTextReaderMoveToAttributeNo(intern->ptr, attr_pos);
  612. if (retval == 1) {
  613. RETURN_TRUE;
  614. }
  615. }
  616. RETURN_FALSE;
  617. }
  618. /* }}} */
  619. /* {{{ proto boolean XMLReader::moveToAttributeNs(string name, string namespaceURI)
  620. Positions reader at attribute spcified by name and namespaceURI.
  621. Returns TRUE on success and FALSE on failure */
  622. PHP_METHOD(xmlreader, moveToAttributeNs)
  623. {
  624. zval *id;
  625. int name_len=0, ns_uri_len=0, retval;
  626. xmlreader_object *intern;
  627. char *name, *ns_uri;
  628. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &name, &name_len, &ns_uri, &ns_uri_len) == FAILURE) {
  629. return;
  630. }
  631. if (name_len == 0 || ns_uri_len == 0) {
  632. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attribute Name and Namespace URI cannot be empty");
  633. RETURN_FALSE;
  634. }
  635. id = getThis();
  636. intern = (xmlreader_object *)zend_object_store_get_object(id TSRMLS_CC);
  637. if (intern && intern->ptr) {
  638. retval = xmlTextReaderMoveToAttributeNs(intern->ptr, (xmlChar *)name, (xmlChar *)ns_uri);
  639. if (retval == 1) {
  640. RETURN_TRUE;
  641. }
  642. }
  643. RETURN_FALSE;
  644. }
  645. /* }}} */
  646. /* {{{ proto boolean XMLReader::moveToElement()
  647. Moves the position of the current instance to the node that contains the current Attribute node. */
  648. PHP_METHOD(xmlreader, moveToElement)
  649. {
  650. php_xmlreader_no_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextReaderMoveToElement);
  651. }
  652. /* }}} */
  653. /* {{{ proto boolean XMLReader::moveToFirstAttribute()
  654. Moves the position of the current instance to the first attribute associated with the current node. */
  655. PHP_METHOD(xmlreader, moveToFirstAttribute)
  656. {
  657. php_xmlreader_no_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextReaderMoveToFirstAttribute);
  658. }
  659. /* }}} */
  660. /* {{{ proto boolean XMLReader::moveToNextAttribute()
  661. Moves the position of the current instance to the next attribute associated with the current node. */
  662. PHP_METHOD(xmlreader, moveToNextAttribute)
  663. {
  664. php_xmlreader_no_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextReaderMoveToNextAttribute);
  665. }
  666. /* }}} */
  667. /* {{{ proto boolean XMLReader::read()
  668. Moves the position of the current instance to the next node in the stream. */
  669. PHP_METHOD(xmlreader, read)
  670. {
  671. zval *id;
  672. int retval;
  673. xmlreader_object *intern;
  674. id = getThis();
  675. intern = (xmlreader_object *)zend_object_store_get_object(id TSRMLS_CC);
  676. if (intern != NULL && intern->ptr != NULL) {
  677. retval = xmlTextReaderRead(intern->ptr);
  678. if (retval == -1) {
  679. RETURN_FALSE;
  680. } else {
  681. RETURN_BOOL(retval);
  682. }
  683. }
  684. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Load Data before trying to read");
  685. RETURN_FALSE;
  686. }
  687. /* }}} */
  688. /* {{{ proto boolean XMLReader::next([string localname])
  689. Moves the position of the current instance to the next node in the stream. */
  690. PHP_METHOD(xmlreader, next)
  691. {
  692. zval *id;
  693. int retval, name_len=0;
  694. xmlreader_object *intern;
  695. char *name = NULL;
  696. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &name, &name_len) == FAILURE) {
  697. return;
  698. }
  699. id = getThis();
  700. intern = (xmlreader_object *)zend_object_store_get_object(id TSRMLS_CC);
  701. if (intern != NULL && intern->ptr != NULL) {
  702. #if LIBXML_VERSION <= 20617
  703. /* Bug in libxml prevents a next in certain cases when positioned on end of element */
  704. if (xmlTextReaderNodeType(intern->ptr) == XML_READER_TYPE_END_ELEMENT) {
  705. retval = xmlTextReaderRead(intern->ptr);
  706. } else
  707. #endif
  708. retval = xmlTextReaderNext(intern->ptr);
  709. while (name != NULL && retval == 1) {
  710. if (xmlStrEqual(xmlTextReaderConstLocalName(intern->ptr), (xmlChar *)name)) {
  711. RETURN_TRUE;
  712. }
  713. retval = xmlTextReaderNext(intern->ptr);
  714. }
  715. if (retval == -1) {
  716. RETURN_FALSE;
  717. } else {
  718. RETURN_BOOL(retval);
  719. }
  720. }
  721. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Load Data before trying to read");
  722. RETURN_FALSE;
  723. }
  724. /* }}} */
  725. /* {{{ proto boolean XMLReader::open(string URI [, string encoding [, int options]])
  726. Sets the URI that the XMLReader will parse. */
  727. PHP_METHOD(xmlreader, open)
  728. {
  729. zval *id;
  730. int source_len = 0, encoding_len = 0;
  731. long options = 0;
  732. xmlreader_object *intern = NULL;
  733. char *source, *valid_file = NULL;
  734. char *encoding = NULL;
  735. char resolved_path[MAXPATHLEN + 1];
  736. xmlTextReaderPtr reader = NULL;
  737. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p|s!l", &source, &source_len, &encoding, &encoding_len, &options) == FAILURE) {
  738. return;
  739. }
  740. id = getThis();
  741. if (id != NULL) {
  742. if (! instanceof_function(Z_OBJCE_P(id), xmlreader_class_entry TSRMLS_CC)) {
  743. id = NULL;
  744. } else {
  745. intern = (xmlreader_object *)zend_object_store_get_object(id TSRMLS_CC);
  746. xmlreader_free_resources(intern);
  747. }
  748. }
  749. if (!source_len) {
  750. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty string supplied as input");
  751. RETURN_FALSE;
  752. }
  753. valid_file = _xmlreader_get_valid_file_path(source, resolved_path, MAXPATHLEN TSRMLS_CC);
  754. if (valid_file) {
  755. reader = xmlReaderForFile(valid_file, encoding, options);
  756. }
  757. if (reader == NULL) {
  758. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open source data");
  759. RETURN_FALSE;
  760. }
  761. if (id == NULL) {
  762. object_init_ex(return_value, xmlreader_class_entry);
  763. intern = (xmlreader_object *)zend_objects_get_address(return_value TSRMLS_CC);
  764. intern->ptr = reader;
  765. return;
  766. }
  767. intern->ptr = reader;
  768. RETURN_TRUE;
  769. }
  770. /* }}} */
  771. /* Not Yet Implemented in libxml - functions exist just not coded
  772. PHP_METHOD(xmlreader, resetState)
  773. {
  774. }
  775. */
  776. #if LIBXML_VERSION >= 20620
  777. /* {{{ proto string XMLReader::readInnerXml()
  778. Reads the contents of the current node, including child nodes and markup. */
  779. PHP_METHOD(xmlreader, readInnerXml)
  780. {
  781. php_xmlreader_no_arg_string(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextReaderReadInnerXml);
  782. }
  783. /* }}} */
  784. /* {{{ proto boolean XMLReader::readOuterXml()
  785. Reads the contents of the current node, including child nodes and markup. */
  786. PHP_METHOD(xmlreader, readOuterXml)
  787. {
  788. php_xmlreader_no_arg_string(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextReaderReadOuterXml);
  789. }
  790. /* }}} */
  791. /* {{{ proto boolean XMLReader::readString()
  792. Reads the contents of an element or a text node as a string. */
  793. PHP_METHOD(xmlreader, readString)
  794. {
  795. php_xmlreader_no_arg_string(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextReaderReadString);
  796. }
  797. /* }}} */
  798. /* {{{ proto boolean XMLReader::setSchema(string filename)
  799. Use W3C XSD schema to validate the document as it is processed. Activation is only possible before the first Read(). */
  800. PHP_METHOD(xmlreader, setSchema)
  801. {
  802. #ifdef LIBXML_SCHEMAS_ENABLED
  803. zval *id;
  804. int source_len = 0, retval = -1;
  805. xmlreader_object *intern;
  806. char *source;
  807. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p!", &source, &source_len) == FAILURE) {
  808. return;
  809. }
  810. if (source != NULL && !source_len) {
  811. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Schema data source is required");
  812. RETURN_FALSE;
  813. }
  814. id = getThis();
  815. intern = (xmlreader_object *)zend_object_store_get_object(id TSRMLS_CC);
  816. if (intern && intern->ptr) {
  817. retval = xmlTextReaderSchemaValidate(intern->ptr, source);
  818. if (retval == 0) {
  819. RETURN_TRUE;
  820. }
  821. }
  822. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to set schema. This must be set prior to reading or schema contains errors.");
  823. RETURN_FALSE;
  824. #else
  825. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No Schema support built into libxml.");
  826. RETURN_FALSE;
  827. #endif
  828. }
  829. /* }}} */
  830. #endif
  831. /* {{{ proto boolean XMLReader::setParserProperty(int property, boolean value)
  832. Sets parser property (one of the parser option constants).
  833. Properties must be set after open() or XML() and before the first read() is called */
  834. PHP_METHOD(xmlreader, setParserProperty)
  835. {
  836. zval *id;
  837. long property;
  838. int retval = -1;
  839. zend_bool value;
  840. xmlreader_object *intern;
  841. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lb", &property, &value) == FAILURE) {
  842. return;
  843. }
  844. id = getThis();
  845. intern = (xmlreader_object *)zend_object_store_get_object(id TSRMLS_CC);
  846. if (intern && intern->ptr) {
  847. retval = xmlTextReaderSetParserProp(intern->ptr,property, value);
  848. }
  849. if (retval == -1) {
  850. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid parser property");
  851. RETURN_FALSE;
  852. }
  853. RETURN_TRUE;
  854. }
  855. /* }}} */
  856. /* {{{ proto boolean XMLReader::setRelaxNGSchema(string filename)
  857. Sets the string that the XMLReader will parse. */
  858. PHP_METHOD(xmlreader, setRelaxNGSchema)
  859. {
  860. php_xmlreader_set_relaxng_schema(INTERNAL_FUNCTION_PARAM_PASSTHRU, XMLREADER_LOAD_FILE);
  861. }
  862. /* }}} */
  863. /* {{{ proto boolean XMLReader::setRelaxNGSchemaSource(string source)
  864. Sets the string that the XMLReader will parse. */
  865. PHP_METHOD(xmlreader, setRelaxNGSchemaSource)
  866. {
  867. php_xmlreader_set_relaxng_schema(INTERNAL_FUNCTION_PARAM_PASSTHRU, XMLREADER_LOAD_STRING);
  868. }
  869. /* }}} */
  870. /* TODO
  871. XMLPUBFUN int XMLCALL
  872. xmlTextReaderSetSchema (xmlTextReaderPtr reader,
  873. xmlSchemaPtr schema);
  874. */
  875. /* {{{ proto boolean XMLReader::XML(string source [, string encoding [, int options]])
  876. Sets the string that the XMLReader will parse. */
  877. PHP_METHOD(xmlreader, XML)
  878. {
  879. zval *id;
  880. int source_len = 0, encoding_len = 0;
  881. long options = 0;
  882. xmlreader_object *intern = NULL;
  883. char *source, *uri = NULL, *encoding = NULL;
  884. int resolved_path_len, ret = 0;
  885. char *directory=NULL, resolved_path[MAXPATHLEN];
  886. xmlParserInputBufferPtr inputbfr;
  887. xmlTextReaderPtr reader;
  888. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s!l", &source, &source_len, &encoding, &encoding_len, &options) == FAILURE) {
  889. return;
  890. }
  891. id = getThis();
  892. if (id != NULL && ! instanceof_function(Z_OBJCE_P(id), xmlreader_class_entry TSRMLS_CC)) {
  893. id = NULL;
  894. }
  895. if (id != NULL) {
  896. intern = (xmlreader_object *)zend_object_store_get_object(id TSRMLS_CC);
  897. xmlreader_free_resources(intern);
  898. }
  899. if (!source_len) {
  900. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty string supplied as input");
  901. RETURN_FALSE;
  902. }
  903. inputbfr = xmlParserInputBufferCreateMem(source, source_len, XML_CHAR_ENCODING_NONE);
  904. if (inputbfr != NULL) {
  905. /* Get the URI of the current script so that we can set the base directory in libxml */
  906. #if HAVE_GETCWD
  907. directory = VCWD_GETCWD(resolved_path, MAXPATHLEN);
  908. #elif HAVE_GETWD
  909. directory = VCWD_GETWD(resolved_path);
  910. #endif
  911. if (directory) {
  912. resolved_path_len = strlen(resolved_path);
  913. if (resolved_path[resolved_path_len - 1] != DEFAULT_SLASH) {
  914. resolved_path[resolved_path_len] = DEFAULT_SLASH;
  915. resolved_path[++resolved_path_len] = '\0';
  916. }
  917. uri = (char *) xmlCanonicPath((const xmlChar *) resolved_path);
  918. }
  919. reader = xmlNewTextReader(inputbfr, uri);
  920. if (reader != NULL) {
  921. #if LIBXML_VERSION >= 20628
  922. ret = xmlTextReaderSetup(reader, NULL, uri, encoding, options);
  923. #endif
  924. if (ret == 0) {
  925. if (id == NULL) {
  926. object_init_ex(return_value, xmlreader_class_entry);
  927. intern = (xmlreader_object *)zend_objects_get_address(return_value TSRMLS_CC);
  928. } else {
  929. RETVAL_TRUE;
  930. }
  931. intern->input = inputbfr;
  932. intern->ptr = reader;
  933. if (uri) {
  934. xmlFree(uri);
  935. }
  936. return;
  937. }
  938. }
  939. }
  940. if (uri) {
  941. xmlFree(uri);
  942. }
  943. if (inputbfr) {
  944. xmlFreeParserInputBuffer(inputbfr);
  945. }
  946. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to load source data");
  947. RETURN_FALSE;
  948. }
  949. /* }}} */
  950. /* {{{ proto boolean XMLReader::expand()
  951. Moves the position of the current instance to the next node in the stream. */
  952. PHP_METHOD(xmlreader, expand)
  953. {
  954. #ifdef HAVE_DOM
  955. zval *id, *basenode = NULL;
  956. int ret;
  957. xmlreader_object *intern;
  958. xmlNode *node, *nodec;
  959. xmlDocPtr docp = NULL;
  960. php_libxml_node_object *domobj = NULL;
  961. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|O!", &id, xmlreader_class_entry, &basenode, dom_node_class_entry) == FAILURE) {
  962. return;
  963. }
  964. if (basenode != NULL) {
  965. NODE_GET_OBJ(node, basenode, xmlNodePtr, domobj);
  966. docp = node->doc;
  967. }
  968. intern = (xmlreader_object *)zend_object_store_get_object(id TSRMLS_CC);
  969. if (intern && intern->ptr) {
  970. node = xmlTextReaderExpand(intern->ptr);
  971. if (node == NULL) {
  972. php_error_docref(NULL TSRMLS_CC, E_WARNING, "An Error Occurred while expanding ");
  973. RETURN_FALSE;
  974. } else {
  975. nodec = xmlDocCopyNode(node, docp, 1);
  976. if (nodec == NULL) {
  977. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot expand this node type");
  978. RETURN_FALSE;
  979. } else {
  980. DOM_RET_OBJ(nodec, &ret, (dom_object *)domobj);
  981. }
  982. }
  983. } else {
  984. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Load Data before trying to expand");
  985. RETURN_FALSE;
  986. }
  987. #else
  988. php_error(E_WARNING, "DOM support is not enabled");
  989. return;
  990. #endif
  991. }
  992. /* }}} */
  993. /* {{{ arginfo */
  994. ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_close, 0)
  995. ZEND_END_ARG_INFO()
  996. ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_getAttribute, 0)
  997. ZEND_ARG_INFO(0, name)
  998. ZEND_END_ARG_INFO()
  999. ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_getAttributeNo, 0)
  1000. ZEND_ARG_INFO(0, index)
  1001. ZEND_END_ARG_INFO()
  1002. ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_getAttributeNs, 0)
  1003. ZEND_ARG_INFO(0, name)
  1004. ZEND_ARG_INFO(0, namespaceURI)
  1005. ZEND_END_ARG_INFO()
  1006. ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_getParserProperty, 0)
  1007. ZEND_ARG_INFO(0, property)
  1008. ZEND_END_ARG_INFO()
  1009. ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_isValid, 0)
  1010. ZEND_END_ARG_INFO()
  1011. ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_lookupNamespace, 0)
  1012. ZEND_ARG_INFO(0, prefix)
  1013. ZEND_END_ARG_INFO()
  1014. ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_moveToAttribute, 0)
  1015. ZEND_ARG_INFO(0, name)
  1016. ZEND_END_ARG_INFO()
  1017. ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_moveToAttributeNo, 0)
  1018. ZEND_ARG_INFO(0, index)
  1019. ZEND_END_ARG_INFO()
  1020. ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_moveToAttributeNs, 0)
  1021. ZEND_ARG_INFO(0, name)
  1022. ZEND_ARG_INFO(0, namespaceURI)
  1023. ZEND_END_ARG_INFO()
  1024. ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_moveToElement, 0)
  1025. ZEND_END_ARG_INFO()
  1026. ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_moveToFirstAttribute, 0)
  1027. ZEND_END_ARG_INFO()
  1028. ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_moveToNextAttribute, 0)
  1029. ZEND_END_ARG_INFO()
  1030. ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_read, 0)
  1031. ZEND_END_ARG_INFO()
  1032. ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlreader_next, 0, 0, 0)
  1033. ZEND_ARG_INFO(0, localname)
  1034. ZEND_END_ARG_INFO()
  1035. ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlreader_open, 0, 0, 1)
  1036. ZEND_ARG_INFO(0, URI)
  1037. ZEND_ARG_INFO(0, encoding)
  1038. ZEND_ARG_INFO(0, options)
  1039. ZEND_END_ARG_INFO()
  1040. ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_readInnerXml, 0)
  1041. ZEND_END_ARG_INFO()
  1042. ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_readOuterXml, 0)
  1043. ZEND_END_ARG_INFO()
  1044. ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_readString, 0)
  1045. ZEND_END_ARG_INFO()
  1046. ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_setSchema, 0)
  1047. ZEND_ARG_INFO(0, filename)
  1048. ZEND_END_ARG_INFO()
  1049. ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_setParserProperty, 0)
  1050. ZEND_ARG_INFO(0, property)
  1051. ZEND_ARG_INFO(0, value)
  1052. ZEND_END_ARG_INFO()
  1053. ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_setRelaxNGSchema, 0)
  1054. ZEND_ARG_INFO(0, filename)
  1055. ZEND_END_ARG_INFO()
  1056. ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_setRelaxNGSchemaSource, 0)
  1057. ZEND_ARG_INFO(0, source)
  1058. ZEND_END_ARG_INFO()
  1059. ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlreader_XML, 0, 0, 1)
  1060. ZEND_ARG_INFO(0, source)
  1061. ZEND_ARG_INFO(0, encoding)
  1062. ZEND_ARG_INFO(0, options)
  1063. ZEND_END_ARG_INFO()
  1064. ZEND_BEGIN_ARG_INFO(arginfo_xmlreader_expand, 0)
  1065. ZEND_END_ARG_INFO()
  1066. /* }}} */
  1067. static const zend_function_entry xmlreader_functions[] = {
  1068. PHP_ME(xmlreader, close, arginfo_xmlreader_close, ZEND_ACC_PUBLIC)
  1069. PHP_ME(xmlreader, getAttribute, arginfo_xmlreader_getAttribute, ZEND_ACC_PUBLIC)
  1070. PHP_ME(xmlreader, getAttributeNo, arginfo_xmlreader_getAttributeNo, ZEND_ACC_PUBLIC)
  1071. PHP_ME(xmlreader, getAttributeNs, arginfo_xmlreader_getAttributeNs, ZEND_ACC_PUBLIC)
  1072. PHP_ME(xmlreader, getParserProperty, arginfo_xmlreader_getParserProperty, ZEND_ACC_PUBLIC)
  1073. PHP_ME(xmlreader, isValid, arginfo_xmlreader_isValid, ZEND_ACC_PUBLIC)
  1074. PHP_ME(xmlreader, lookupNamespace, arginfo_xmlreader_lookupNamespace, ZEND_ACC_PUBLIC)
  1075. PHP_ME(xmlreader, moveToAttributeNo, arginfo_xmlreader_moveToAttributeNo, ZEND_ACC_PUBLIC)
  1076. PHP_ME(xmlreader, moveToAttribute, arginfo_xmlreader_moveToAttribute, ZEND_ACC_PUBLIC)
  1077. PHP_ME(xmlreader, moveToAttributeNs, arginfo_xmlreader_moveToAttributeNs, ZEND_ACC_PUBLIC)
  1078. PHP_ME(xmlreader, moveToElement, arginfo_xmlreader_moveToElement, ZEND_ACC_PUBLIC)
  1079. PHP_ME(xmlreader, moveToFirstAttribute, arginfo_xmlreader_moveToFirstAttribute, ZEND_ACC_PUBLIC)
  1080. PHP_ME(xmlreader, moveToNextAttribute, arginfo_xmlreader_moveToNextAttribute, ZEND_ACC_PUBLIC)
  1081. PHP_ME(xmlreader, open, arginfo_xmlreader_open, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC)
  1082. PHP_ME(xmlreader, read, arginfo_xmlreader_read, ZEND_ACC_PUBLIC)
  1083. PHP_ME(xmlreader, next, arginfo_xmlreader_next, ZEND_ACC_PUBLIC)
  1084. #if LIBXML_VERSION >= 20620
  1085. PHP_ME(xmlreader, readInnerXml, arginfo_xmlreader_readInnerXml, ZEND_ACC_PUBLIC)
  1086. PHP_ME(xmlreader, readOuterXml, arginfo_xmlreader_readOuterXml, ZEND_ACC_PUBLIC)
  1087. PHP_ME(xmlreader, readString, arginfo_xmlreader_readString, ZEND_ACC_PUBLIC)
  1088. PHP_ME(xmlreader, setSchema, arginfo_xmlreader_setSchema, ZEND_ACC_PUBLIC)
  1089. #endif
  1090. /* Not Yet Implemented though defined in libxml as of 2.6.9dev
  1091. PHP_ME(xmlreader, resetState, NULL, ZEND_ACC_PUBLIC)
  1092. */
  1093. PHP_ME(xmlreader, setParserProperty, arginfo_xmlreader_setParserProperty, ZEND_ACC_PUBLIC)
  1094. PHP_ME(xmlreader, setRelaxNGSchema, arginfo_xmlreader_setRelaxNGSchema, ZEND_ACC_PUBLIC)
  1095. PHP_ME(xmlreader, setRelaxNGSchemaSource, arginfo_xmlreader_setRelaxNGSchemaSource, ZEND_ACC_PUBLIC)
  1096. PHP_ME(xmlreader, XML, arginfo_xmlreader_XML, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC)
  1097. PHP_ME(xmlreader, expand, arginfo_xmlreader_expand, ZEND_ACC_PUBLIC)
  1098. PHP_FE_END
  1099. };
  1100. /* {{{ PHP_MINIT_FUNCTION
  1101. */
  1102. PHP_MINIT_FUNCTION(xmlreader)
  1103. {
  1104. zend_class_entry ce;
  1105. memcpy(&xmlreader_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  1106. xmlreader_object_handlers.read_property = xmlreader_read_property;
  1107. xmlreader_object_handlers.write_property = xmlreader_write_property;
  1108. xmlreader_object_handlers.get_property_ptr_ptr = xmlreader_get_property_ptr_ptr;
  1109. xmlreader_object_handlers.clone_obj = NULL;
  1110. INIT_CLASS_ENTRY(ce, "XMLReader", xmlreader_functions);
  1111. ce.create_object = xmlreader_objects_new;
  1112. xmlreader_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
  1113. zend_hash_init(&xmlreader_prop_handlers, 0, NULL, NULL, 1);
  1114. xmlreader_register_prop_handler(&xmlreader_prop_handlers, "attributeCount", xmlTextReaderAttributeCount, NULL, IS_LONG TSRMLS_CC);
  1115. xmlreader_register_prop_handler(&xmlreader_prop_handlers, "baseURI", NULL, xmlTextReaderConstBaseUri, IS_STRING TSRMLS_CC);
  1116. xmlreader_register_prop_handler(&xmlreader_prop_handlers, "depth", xmlTextReaderDepth, NULL, IS_LONG TSRMLS_CC);
  1117. xmlreader_register_prop_handler(&xmlreader_prop_handlers, "hasAttributes", xmlTextReaderHasAttributes, NULL, IS_BOOL TSRMLS_CC);
  1118. xmlreader_register_prop_handler(&xmlreader_prop_handlers, "hasValue", xmlTextReaderHasValue, NULL, IS_BOOL TSRMLS_CC);
  1119. xmlreader_register_prop_handler(&xmlreader_prop_handlers, "isDefault", xmlTextReaderIsDefault, NULL, IS_BOOL TSRMLS_CC);
  1120. xmlreader_register_prop_handler(&xmlreader_prop_handlers, "isEmptyElement", xmlTextReaderIsEmptyElement, NULL, IS_BOOL TSRMLS_CC);
  1121. xmlreader_register_prop_handler(&xmlreader_prop_handlers, "localName", NULL, xmlTextReaderConstLocalName, IS_STRING TSRMLS_CC);
  1122. xmlreader_register_prop_handler(&xmlreader_prop_handlers, "name", NULL, xmlTextReaderConstName, IS_STRING TSRMLS_CC);
  1123. xmlreader_register_prop_handler(&xmlreader_prop_handlers, "namespaceURI", NULL, xmlTextReaderConstNamespaceUri, IS_STRING TSRMLS_CC);
  1124. xmlreader_register_prop_handler(&xmlreader_prop_handlers, "nodeType", xmlTextReaderNodeType, NULL, IS_LONG TSRMLS_CC);
  1125. xmlreader_register_prop_handler(&xmlreader_prop_handlers, "prefix", NULL, xmlTextReaderConstPrefix, IS_STRING TSRMLS_CC);
  1126. xmlreader_register_prop_handler(&xmlreader_prop_handlers, "value", NULL, xmlTextReaderConstValue, IS_STRING TSRMLS_CC);
  1127. xmlreader_register_prop_handler(&xmlreader_prop_handlers, "xmlLang", NULL, xmlTextReaderConstXmlLang, IS_STRING TSRMLS_CC);
  1128. /* Constants for NodeType - cannot define common types to share with dom as there are differences in these types */
  1129. REGISTER_XMLREADER_CLASS_CONST_LONG("NONE", XML_READER_TYPE_NONE);
  1130. REGISTER_XMLREADER_CLASS_CONST_LONG("ELEMENT", XML_READER_TYPE_ELEMENT);
  1131. REGISTER_XMLREADER_CLASS_CONST_LONG("ATTRIBUTE", XML_READER_TYPE_ATTRIBUTE);
  1132. REGISTER_XMLREADER_CLASS_CONST_LONG("TEXT", XML_READER_TYPE_TEXT);
  1133. REGISTER_XMLREADER_CLASS_CONST_LONG("CDATA", XML_READER_TYPE_CDATA);
  1134. REGISTER_XMLREADER_CLASS_CONST_LONG("ENTITY_REF", XML_READER_TYPE_ENTITY_REFERENCE);
  1135. REGISTER_XMLREADER_CLASS_CONST_LONG("ENTITY", XML_READER_TYPE_ENTITY);
  1136. REGISTER_XMLREADER_CLASS_CONST_LONG("PI", XML_READER_TYPE_PROCESSING_INSTRUCTION);
  1137. REGISTER_XMLREADER_CLASS_CONST_LONG("COMMENT", XML_READER_TYPE_COMMENT);
  1138. REGISTER_XMLREADER_CLASS_CONST_LONG("DOC", XML_READER_TYPE_DOCUMENT);
  1139. REGISTER_XMLREADER_CLASS_CONST_LONG("DOC_TYPE", XML_READER_TYPE_DOCUMENT_TYPE);
  1140. REGISTER_XMLREADER_CLASS_CONST_LONG("DOC_FRAGMENT", XML_READER_TYPE_DOCUMENT_FRAGMENT);
  1141. REGISTER_XMLREADER_CLASS_CONST_LONG("NOTATION", XML_READER_TYPE_NOTATION);
  1142. REGISTER_XMLREADER_CLASS_CONST_LONG("WHITESPACE", XML_READER_TYPE_WHITESPACE);
  1143. REGISTER_XMLREADER_CLASS_CONST_LONG("SIGNIFICANT_WHITESPACE", XML_READER_TYPE_SIGNIFICANT_WHITESPACE);
  1144. REGISTER_XMLREADER_CLASS_CONST_LONG("END_ELEMENT", XML_READER_TYPE_END_ELEMENT);
  1145. REGISTER_XMLREADER_CLASS_CONST_LONG("END_ENTITY", XML_READER_TYPE_END_ENTITY);
  1146. REGISTER_XMLREADER_CLASS_CONST_LONG("XML_DECLARATION", XML_READER_TYPE_XML_DECLARATION);
  1147. /* Constants for Parser options */
  1148. REGISTER_XMLREADER_CLASS_CONST_LONG("LOADDTD", XML_PARSER_LOADDTD);
  1149. REGISTER_XMLREADER_CLASS_CONST_LONG("DEFAULTATTRS", XML_PARSER_DEFAULTATTRS);
  1150. REGISTER_XMLREADER_CLASS_CONST_LONG("VALIDATE", XML_PARSER_VALIDATE);
  1151. REGISTER_XMLREADER_CLASS_CONST_LONG("SUBST_ENTITIES", XML_PARSER_SUBST_ENTITIES);
  1152. /* Constants for Errors when loading - not yet used until we implement custom error handling
  1153. REGISTER_XMLREADER_CLASS_CONST_LONG("VALIDITY_WARNING", XML_PARSER_SEVERITY_VALIDITY_WARNING, CONST_CS | CONST_PERSISTENT);
  1154. REGISTER_XMLREADER_CLASS_CONST_LONG("VALIDITY_ERROR", XML_PARSER_SEVERITY_VALIDITY_ERROR, CONST_CS | CONST_PERSISTENT);
  1155. REGISTER_XMLREADER_CLASS_CONST_LONG("WARNING", XML_PARSER_SEVERITY_WARNING, CONST_CS | CONST_PERSISTENT);
  1156. REGISTER_XMLREADER_CLASS_CONST_LONG("ERROR", XML_PARSER_SEVERITY_ERROR, CONST_CS | CONST_PERSISTENT);
  1157. */
  1158. return SUCCESS;
  1159. }
  1160. /* }}} */
  1161. /* {{{ PHP_MSHUTDOWN_FUNCTION
  1162. */
  1163. PHP_MSHUTDOWN_FUNCTION(xmlreader)
  1164. {
  1165. zend_hash_destroy(&xmlreader_prop_handlers);
  1166. return SUCCESS;
  1167. }
  1168. /* }}} */
  1169. /* {{{ PHP_MINFO_FUNCTION
  1170. */
  1171. PHP_MINFO_FUNCTION(xmlreader)
  1172. {
  1173. php_info_print_table_start();
  1174. {
  1175. php_info_print_table_row(2, "XMLReader", "enabled");
  1176. }
  1177. php_info_print_table_end();
  1178. }
  1179. /* }}} */
  1180. /*
  1181. * Local variables:
  1182. * tab-width: 4
  1183. * c-basic-offset: 4
  1184. * End:
  1185. * vim600: noet sw=4 ts=4 fdm=marker
  1186. * vim<600: noet sw=4 ts=4
  1187. */