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