xref: /PHP-8.4/ext/dom/documenttype.c (revision 11accb5c)
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    | https://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 #include "dom_properties.h"
26 
27 /* {{{ name	string
28 readonly=yes
29 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1844763134
30 Since:
31 */
dom_documenttype_name_read(dom_object * obj,zval * retval)32 zend_result dom_documenttype_name_read(dom_object *obj, zval *retval)
33 {
34 	DOM_PROP_NODE(xmlDtdPtr, dtdptr, obj);
35 	ZVAL_STRING(retval, dtdptr->name ? (char *) (dtdptr->name) : "");
36 	return SUCCESS;
37 }
38 
39 /* }}} */
40 
41 /* {{{ entities	DOMNamedNodeMap
42 readonly=yes
43 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1788794630
44 Since:
45 */
dom_documenttype_entities_read(dom_object * obj,zval * retval)46 zend_result dom_documenttype_entities_read(dom_object *obj, zval *retval)
47 {
48 	DOM_PROP_NODE(xmlDtdPtr, dtdptr, obj);
49 
50 	php_dom_create_iterator(retval, DOM_DTD_NAMEDNODEMAP, php_dom_follow_spec_intern(obj));
51 
52 	xmlHashTable *entityht = (xmlHashTable *) dtdptr->entities;
53 
54 	dom_object *intern = Z_DOMOBJ_P(retval);
55 	dom_namednode_iter(obj, XML_ENTITY_NODE, intern, entityht, NULL, 0, NULL, 0);
56 
57 	return SUCCESS;
58 }
59 
60 /* }}} */
61 
62 /* {{{ notations	DOMNamedNodeMap
63 readonly=yes
64 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-D46829EF
65 Since:
66 */
dom_documenttype_notations_read(dom_object * obj,zval * retval)67 zend_result dom_documenttype_notations_read(dom_object *obj, zval *retval)
68 {
69 	DOM_PROP_NODE(xmlDtdPtr, dtdptr, obj);
70 
71 	php_dom_create_iterator(retval, DOM_DTD_NAMEDNODEMAP, php_dom_follow_spec_intern(obj));
72 
73 	xmlHashTable *notationht = (xmlHashTable *) dtdptr->notations;
74 
75 	dom_object *intern = Z_DOMOBJ_P(retval);
76 	dom_namednode_iter(obj, XML_NOTATION_NODE, intern, notationht, NULL, 0, NULL, 0);
77 
78 	return SUCCESS;
79 }
80 
81 /* }}} */
82 
83 /* {{{ publicId	string
84 readonly=yes
85 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-Core-DocType-publicId
86 Since: DOM Level 2
87 */
dom_documenttype_public_id_read(dom_object * obj,zval * retval)88 zend_result dom_documenttype_public_id_read(dom_object *obj, zval *retval)
89 {
90 	DOM_PROP_NODE(xmlDtdPtr, dtdptr, obj);
91 
92 	if (dtdptr->ExternalID) {
93 		ZVAL_STRING(retval, (char *) (dtdptr->ExternalID));
94 	} else {
95 		ZVAL_EMPTY_STRING(retval);
96 	}
97 
98 	return SUCCESS;
99 }
100 
101 /* }}} */
102 
103 /* {{{ systemId	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-systemId
106 Since: DOM Level 2
107 */
dom_documenttype_system_id_read(dom_object * obj,zval * retval)108 zend_result dom_documenttype_system_id_read(dom_object *obj, zval *retval)
109 {
110 	DOM_PROP_NODE(xmlDtdPtr, dtdptr, obj);
111 
112 	if (dtdptr->SystemID) {
113 		ZVAL_STRING(retval, (char *) (dtdptr->SystemID));
114 	} else {
115 		ZVAL_EMPTY_STRING(retval);
116 	}
117 
118 	return SUCCESS;
119 }
120 
121 /* }}} */
122 
123 /* {{{ internalSubset	string
124 readonly=yes
125 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-Core-DocType-internalSubset
126 Since: DOM Level 2
127 */
dom_documenttype_internal_subset_read(dom_object * obj,zval * retval)128 zend_result dom_documenttype_internal_subset_read(dom_object *obj, zval *retval)
129 {
130 	DOM_PROP_NODE(xmlDtdPtr, dtdptr, obj);
131 
132 	xmlDtdPtr intsubset;
133 	if (dtdptr->doc != NULL && ((intsubset = xmlGetIntSubset(dtdptr->doc)) != NULL)) {
134 		smart_str ret_buf = {0};
135 		xmlNodePtr cur = intsubset->children;
136 
137 		while (cur != NULL) {
138 			xmlOutputBuffer *buff = xmlAllocOutputBuffer(NULL);
139 
140 			if (buff != NULL) {
141 				xmlNodeDumpOutput (buff, NULL, cur, 0, 0, NULL);
142 				xmlOutputBufferFlush(buff);
143 
144 				smart_str_appendl(&ret_buf, (const char *) xmlOutputBufferGetContent(buff), xmlOutputBufferGetSize(buff));
145 
146 				(void)xmlOutputBufferClose(buff);
147 			}
148 
149 			cur = cur->next;
150 		}
151 
152 		if (ret_buf.s) {
153 			ZVAL_NEW_STR(retval, smart_str_extract(&ret_buf));
154 			return SUCCESS;
155 		}
156 	}
157 
158 	ZVAL_NULL(retval);
159 
160 	return SUCCESS;
161 }
162 
163 /* }}} */
164 
165 #endif
166