xref: /PHP-7.3/ext/dom/processinginstruction.c (revision 8d3f8ca1)
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 /* {{{ arginfo */
30 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_processinginstruction_construct, 0, 0, 1)
31 	ZEND_ARG_INFO(0, name)
32 	ZEND_ARG_INFO(0, value)
33 ZEND_END_ARG_INFO();
34 /* }}} */
35 
36 /*
37 * class DOMProcessingInstruction extends DOMNode
38 *
39 * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-1004215813
40 * Since:
41 */
42 
43 const zend_function_entry php_dom_processinginstruction_class_functions[] = {
44 	PHP_ME(domprocessinginstruction, __construct, arginfo_dom_processinginstruction_construct, ZEND_ACC_PUBLIC)
45 	PHP_FE_END
46 };
47 
48 /* {{{ proto DOMProcessingInstruction::__construct(string name, [string value]); */
PHP_METHOD(domprocessinginstruction,__construct)49 PHP_METHOD(domprocessinginstruction, __construct)
50 {
51 	zval *id = getThis();
52 	xmlNodePtr nodep = NULL, oldnode = NULL;
53 	dom_object *intern;
54 	char *name, *value = NULL;
55 	size_t name_len, value_len;
56 	int name_valid;
57 
58 	if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s|s", &name, &name_len, &value, &value_len) == FAILURE) {
59 		return;
60 	}
61 
62 	name_valid = xmlValidateName((xmlChar *) name, 0);
63 	if (name_valid != 0) {
64 		php_dom_throw_error(INVALID_CHARACTER_ERR, 1);
65 		RETURN_FALSE;
66 	}
67 
68 	nodep = xmlNewPI((xmlChar *) name, (xmlChar *) value);
69 
70 	if (!nodep) {
71 		php_dom_throw_error(INVALID_STATE_ERR, 1);
72 		RETURN_FALSE;
73 	}
74 
75 	intern = Z_DOMOBJ_P(id);
76 	oldnode = dom_object_get_node(intern);
77 	if (oldnode != NULL) {
78 		php_libxml_node_free_resource(oldnode );
79 	}
80 	php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern);
81 }
82 /* }}} end DOMProcessingInstruction::__construct */
83 
84 /* {{{ target	string
85 readonly=yes
86 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-1478689192
87 Since:
88 */
dom_processinginstruction_target_read(dom_object * obj,zval * retval)89 int dom_processinginstruction_target_read(dom_object *obj, zval *retval)
90 {
91 	xmlNodePtr nodep = dom_object_get_node(obj);
92 
93 	if (nodep == NULL) {
94 		php_dom_throw_error(INVALID_STATE_ERR, 0);
95 		return FAILURE;
96 	}
97 
98 	ZVAL_STRING(retval, (char *) (nodep->name));
99 
100 	return SUCCESS;
101 }
102 
103 /* }}} */
104 
105 /* {{{ data	string
106 readonly=no
107 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-837822393
108 Since:
109 */
dom_processinginstruction_data_read(dom_object * obj,zval * retval)110 int dom_processinginstruction_data_read(dom_object *obj, zval *retval)
111 {
112 	xmlNodePtr nodep;
113 	xmlChar *content;
114 
115 	nodep = dom_object_get_node(obj);
116 
117 	if (nodep == NULL) {
118 		php_dom_throw_error(INVALID_STATE_ERR, 0);
119 		return FAILURE;
120 	}
121 
122 	if ((content = xmlNodeGetContent(nodep)) != NULL) {
123 		ZVAL_STRING(retval, (char *) content);
124 		xmlFree(content);
125 	} else {
126 		ZVAL_EMPTY_STRING(retval);
127 	}
128 
129 	return SUCCESS;
130 }
131 
dom_processinginstruction_data_write(dom_object * obj,zval * newval)132 int dom_processinginstruction_data_write(dom_object *obj, zval *newval)
133 {
134 	xmlNode *nodep = dom_object_get_node(obj);
135 	zend_string *str;
136 
137 	if (nodep == NULL) {
138 		php_dom_throw_error(INVALID_STATE_ERR, 0);
139 		return FAILURE;
140 	}
141 
142 	str = zval_get_string(newval);
143 
144 	xmlNodeSetContentLen(nodep, (xmlChar *) ZSTR_VAL(str), ZSTR_LEN(str) + 1);
145 
146 	zend_string_release_ex(str, 0);
147 	return SUCCESS;
148 }
149 
150 /* }}} */
151 
152 #endif
153 
154 /*
155  * Local variables:
156  * tab-width: 4
157  * c-basic-offset: 4
158  * End:
159  * vim600: noet sw=4 ts=4 fdm=marker
160  * vim<600: noet sw=4 ts=4
161  */
162