xref: /PHP-7.4/ext/dom/processinginstruction.c (revision 457392fa)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 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 	xmlNodePtr nodep = NULL, oldnode = NULL;
52 	dom_object *intern;
53 	char *name, *value = NULL;
54 	size_t name_len, value_len;
55 	int name_valid;
56 
57 	if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s|s", &name, &name_len, &value, &value_len) == FAILURE) {
58 		return;
59 	}
60 
61 	name_valid = xmlValidateName((xmlChar *) name, 0);
62 	if (name_valid != 0) {
63 		php_dom_throw_error(INVALID_CHARACTER_ERR, 1);
64 		RETURN_FALSE;
65 	}
66 
67 	nodep = xmlNewPI((xmlChar *) name, (xmlChar *) value);
68 
69 	if (!nodep) {
70 		php_dom_throw_error(INVALID_STATE_ERR, 1);
71 		RETURN_FALSE;
72 	}
73 
74 	intern = Z_DOMOBJ_P(ZEND_THIS);
75 	oldnode = dom_object_get_node(intern);
76 	if (oldnode != NULL) {
77 		php_libxml_node_free_resource(oldnode );
78 	}
79 	php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern);
80 }
81 /* }}} end DOMProcessingInstruction::__construct */
82 
83 /* {{{ target	string
84 readonly=yes
85 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-1478689192
86 Since:
87 */
dom_processinginstruction_target_read(dom_object * obj,zval * retval)88 int dom_processinginstruction_target_read(dom_object *obj, zval *retval)
89 {
90 	xmlNodePtr nodep = dom_object_get_node(obj);
91 
92 	if (nodep == NULL) {
93 		php_dom_throw_error(INVALID_STATE_ERR, 0);
94 		return FAILURE;
95 	}
96 
97 	ZVAL_STRING(retval, (char *) (nodep->name));
98 
99 	return SUCCESS;
100 }
101 
102 /* }}} */
103 
104 /* {{{ data	string
105 readonly=no
106 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-837822393
107 Since:
108 */
dom_processinginstruction_data_read(dom_object * obj,zval * retval)109 int dom_processinginstruction_data_read(dom_object *obj, zval *retval)
110 {
111 	xmlNodePtr nodep;
112 	xmlChar *content;
113 
114 	nodep = dom_object_get_node(obj);
115 
116 	if (nodep == NULL) {
117 		php_dom_throw_error(INVALID_STATE_ERR, 0);
118 		return FAILURE;
119 	}
120 
121 	if ((content = xmlNodeGetContent(nodep)) != NULL) {
122 		ZVAL_STRING(retval, (char *) content);
123 		xmlFree(content);
124 	} else {
125 		ZVAL_EMPTY_STRING(retval);
126 	}
127 
128 	return SUCCESS;
129 }
130 
dom_processinginstruction_data_write(dom_object * obj,zval * newval)131 int dom_processinginstruction_data_write(dom_object *obj, zval *newval)
132 {
133 	xmlNode *nodep = dom_object_get_node(obj);
134 	zend_string *str;
135 
136 	if (nodep == NULL) {
137 		php_dom_throw_error(INVALID_STATE_ERR, 0);
138 		return FAILURE;
139 	}
140 
141 	str = zval_try_get_string(newval);
142 	if (UNEXPECTED(!str)) {
143 		return FAILURE;
144 	}
145 
146 	xmlNodeSetContentLen(nodep, (xmlChar *) ZSTR_VAL(str), ZSTR_LEN(str) + 1);
147 
148 	zend_string_release_ex(str, 0);
149 	return SUCCESS;
150 }
151 
152 /* }}} */
153 
154 #endif
155