xref: /PHP-8.0/ext/dom/documenttype.c (revision 62b1d2cb)
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    | http://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 
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21 
22 #include "php.h"
23 #if defined(HAVE_LIBXML) && defined(HAVE_DOM)
24 #include "php_dom.h"
25 
26 /* {{{ name	string
27 readonly=yes
28 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1844763134
29 Since:
30 */
dom_documenttype_name_read(dom_object * obj,zval * retval)31 int dom_documenttype_name_read(dom_object *obj, zval *retval)
32 {
33 	xmlDtdPtr dtdptr = (xmlDtdPtr) dom_object_get_node(obj);
34 
35 	if (dtdptr == NULL) {
36 		php_dom_throw_error(INVALID_STATE_ERR, 0);
37 		return FAILURE;
38 	}
39 
40 	ZVAL_STRING(retval, dtdptr->name ? (char *) (dtdptr->name) : "");
41 
42 	return SUCCESS;
43 }
44 
45 /* }}} */
46 
47 /* {{{ entities	DOMNamedNodeMap
48 readonly=yes
49 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1788794630
50 Since:
51 */
dom_documenttype_entities_read(dom_object * obj,zval * retval)52 int dom_documenttype_entities_read(dom_object *obj, zval *retval)
53 {
54 	xmlDtdPtr doctypep = (xmlDtdPtr) dom_object_get_node(obj);
55 	xmlHashTable *entityht;
56 	dom_object *intern;
57 
58 	if (doctypep == NULL) {
59 		php_dom_throw_error(INVALID_STATE_ERR, 0);
60 		return FAILURE;
61 	}
62 
63 	php_dom_create_iterator(retval, DOM_NAMEDNODEMAP);
64 
65 	entityht = (xmlHashTable *) doctypep->entities;
66 
67 	intern = Z_DOMOBJ_P(retval);
68 	dom_namednode_iter(obj, XML_ENTITY_NODE, intern, entityht, NULL, NULL);
69 
70 	return SUCCESS;
71 }
72 
73 /* }}} */
74 
75 /* {{{ notations	DOMNamedNodeMap
76 readonly=yes
77 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-D46829EF
78 Since:
79 */
dom_documenttype_notations_read(dom_object * obj,zval * retval)80 int dom_documenttype_notations_read(dom_object *obj, zval *retval)
81 {
82 	xmlDtdPtr doctypep = (xmlDtdPtr) dom_object_get_node(obj);
83 	xmlHashTable *notationht;
84 	dom_object *intern;
85 
86 	if (doctypep == NULL) {
87 		php_dom_throw_error(INVALID_STATE_ERR, 0);
88 		return FAILURE;
89 	}
90 
91 	php_dom_create_iterator(retval, DOM_NAMEDNODEMAP);
92 
93 	notationht = (xmlHashTable *) doctypep->notations;
94 
95 	intern = Z_DOMOBJ_P(retval);
96 	dom_namednode_iter(obj, XML_NOTATION_NODE, intern, notationht, NULL, NULL);
97 
98 	return SUCCESS;
99 }
100 
101 /* }}} */
102 
103 /* {{{ publicId	string
104 readonly=yes
105 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-Core-DocType-publicId
106 Since: DOM Level 2
107 */
dom_documenttype_public_id_read(dom_object * obj,zval * retval)108 int dom_documenttype_public_id_read(dom_object *obj, zval *retval)
109 {
110 	xmlDtdPtr dtdptr = (xmlDtdPtr) dom_object_get_node(obj);
111 
112 	if (dtdptr == NULL) {
113 		php_dom_throw_error(INVALID_STATE_ERR, 0);
114 		return FAILURE;
115 	}
116 
117 	if (dtdptr->ExternalID) {
118 		ZVAL_STRING(retval, (char *) (dtdptr->ExternalID));
119 	} else {
120 		ZVAL_EMPTY_STRING(retval);
121 	}
122 	return SUCCESS;
123 
124 }
125 
126 /* }}} */
127 
128 /* {{{ systemId	string
129 readonly=yes
130 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-Core-DocType-systemId
131 Since: DOM Level 2
132 */
dom_documenttype_system_id_read(dom_object * obj,zval * retval)133 int dom_documenttype_system_id_read(dom_object *obj, zval *retval)
134 {
135 	xmlDtdPtr dtdptr = (xmlDtdPtr) dom_object_get_node(obj);
136 
137 	if (dtdptr == NULL) {
138 		php_dom_throw_error(INVALID_STATE_ERR, 0);
139 		return FAILURE;
140 	}
141 
142 	if (dtdptr->SystemID) {
143 		ZVAL_STRING(retval, (char *) (dtdptr->SystemID));
144 	} else {
145 		ZVAL_EMPTY_STRING(retval);
146 	}
147 	return SUCCESS;
148 }
149 
150 /* }}} */
151 
152 /* {{{ internalSubset	string
153 readonly=yes
154 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-Core-DocType-internalSubset
155 Since: DOM Level 2
156 */
dom_documenttype_internal_subset_read(dom_object * obj,zval * retval)157 int dom_documenttype_internal_subset_read(dom_object *obj, zval *retval)
158 {
159 	xmlDtdPtr dtdptr = (xmlDtdPtr) dom_object_get_node(obj);
160 	xmlDtdPtr intsubset;
161 
162 	if (dtdptr == NULL) {
163 		php_dom_throw_error(INVALID_STATE_ERR, 0);
164 		return FAILURE;
165 	}
166 
167 	if (dtdptr->doc != NULL && ((intsubset = xmlGetIntSubset(dtdptr->doc)) != NULL)) {
168 		smart_str ret_buf = {0};
169 		xmlNodePtr cur = intsubset->children;
170 
171 		while (cur != NULL) {
172 			xmlOutputBuffer *buff = xmlAllocOutputBuffer(NULL);
173 
174 			if (buff != NULL) {
175 				xmlNodeDumpOutput (buff, NULL, cur, 0, 0, NULL);
176 				xmlOutputBufferFlush(buff);
177 
178 #ifdef LIBXML2_NEW_BUFFER
179 				smart_str_appendl(&ret_buf, (const char *) xmlOutputBufferGetContent(buff), xmlOutputBufferGetSize(buff));
180 #else
181 				smart_str_appendl(&ret_buf, (char *) buff->buffer->content, buff->buffer->use);
182 #endif
183 
184 				(void)xmlOutputBufferClose(buff);
185 			}
186 
187 			cur = cur->next;
188 		}
189 
190 		if (ret_buf.s) {
191 			smart_str_0(&ret_buf);
192 			ZVAL_NEW_STR(retval, ret_buf.s);
193 			return SUCCESS;
194 		}
195 	}
196 
197 	ZVAL_NULL(retval);
198 
199 	return SUCCESS;
200 
201 }
202 
203 /* }}} */
204 
205 #endif
206