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