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