/* {{{ proto void DOMAttr::__construct(string name, [string value]); */ PHP_METHOD(domattr, __construct) { zval *id; xmlAttrPtr nodep = NULL; xmlNodePtr oldnode = NULL; dom_object *intern; char *name, *value = NULL; int name_len, value_len, name_valid; zend_error_handling error_handling; zend_replace_error_handling(EH_THROW, dom_domexception_class_entry, &error_handling TSRMLS_CC); if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|s", &id, dom_attr_class_entry, &name, &name_len, &value, &value_len) == FAILURE) { zend_restore_error_handling(&error_handling TSRMLS_CC); return; } zend_restore_error_handling(&error_handling TSRMLS_CC); intern = (dom_object *)zend_object_store_get_object(id TSRMLS_CC); name_valid = xmlValidateName((xmlChar *) name, 0); if (name_valid != 0) { php_dom_throw_error(INVALID_CHARACTER_ERR, 1 TSRMLS_CC); RETURN_FALSE; } nodep = xmlNewProp(NULL, (xmlChar *) name, value); if (!nodep) { php_dom_throw_error(INVALID_STATE_ERR, 1 TSRMLS_CC); RETURN_FALSE; } if (intern != NULL) { oldnode = dom_object_get_node(intern); if (oldnode != NULL) { php_libxml_node_free_resource(oldnode TSRMLS_CC); } php_libxml_increment_node_ptr((php_libxml_node_object *)intern, (xmlNodePtr)nodep, (void *)intern TSRMLS_CC); } }
/* {{{ ownerElement DOMElement readonly=yes URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Attr-ownerElement Since: DOM Level 2 */ int dom_attr_owner_element_read(dom_object *obj, zval *retval) { xmlNodePtr nodep, nodeparent; nodep = dom_object_get_node(obj); if (nodep == NULL) { php_dom_throw_error(INVALID_STATE_ERR, 0); return FAILURE; } nodeparent = nodep->parent; if (!nodeparent) { ZVAL_NULL(retval); return SUCCESS; } php_dom_create_object(nodeparent, retval, obj); return SUCCESS; }
/* {{{ data string readonly=no URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-837822393 Since: */ int dom_processinginstruction_data_read(dom_object *obj, zval *retval) { xmlNodePtr nodep; xmlChar *content; nodep = dom_object_get_node(obj); if (nodep == NULL) { php_dom_throw_error(INVALID_STATE_ERR, 0); return FAILURE; } if ((content = xmlNodeGetContent(nodep)) != NULL) { ZVAL_STRING(retval, (char *) content); xmlFree(content); } else { ZVAL_EMPTY_STRING(retval); } return SUCCESS; }
/* {{{ textContent string readonly=no URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Node3-textContent Since: DOM Level 3 */ int dom_node_text_content_read(dom_object *obj, zval *retval) { xmlNode *nodep = dom_object_get_node(obj); char *str = NULL; if (nodep == NULL) { php_dom_throw_error(INVALID_STATE_ERR, 0); return FAILURE; } str = (char *) xmlNodeGetContent(nodep); if (str != NULL) { ZVAL_STRING(retval, str); xmlFree(str); } else { ZVAL_EMPTY_STRING(retval); } return SUCCESS; }
int dom_attr_value_write(dom_object *obj, zval *newval) { zend_string *str; xmlAttrPtr attrp = (xmlAttrPtr) dom_object_get_node(obj); if (attrp == NULL) { php_dom_throw_error(INVALID_STATE_ERR, 0); return FAILURE; } if (attrp->children) { node_list_unlink(attrp->children); } str = zval_get_string(newval); xmlNodeSetContentLen((xmlNodePtr) attrp, (xmlChar *) ZSTR_VAL(str), ZSTR_LEN(str) + 1); zend_string_release(str); return SUCCESS; }
/* {{{ proto void DOMEntityReference::__construct(string name); */ PHP_METHOD(domentityreference, __construct) { zval *id; xmlNode *node; xmlNodePtr oldnode = NULL; dom_object *intern; char *name; size_t name_len, name_valid; zend_error_handling error_handling; zend_replace_error_handling(EH_THROW, dom_domexception_class_entry, &error_handling); if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &id, dom_entityreference_class_entry, &name, &name_len) == FAILURE) { zend_restore_error_handling(&error_handling); return; } zend_restore_error_handling(&error_handling); name_valid = xmlValidateName((xmlChar *) name, 0); if (name_valid != 0) { php_dom_throw_error(INVALID_CHARACTER_ERR, 1); RETURN_FALSE; } node = xmlNewReference(NULL, (xmlChar *) name); if (!node) { php_dom_throw_error(INVALID_STATE_ERR, 1); RETURN_FALSE; } intern = Z_DOMOBJ_P(id); if (intern != NULL) { oldnode = dom_object_get_node(intern); if (oldnode != NULL) { php_libxml_node_free_resource(oldnode ); } php_libxml_increment_node_ptr((php_libxml_node_object *)intern, node, (void *)intern); } }
/* {{{ length long readonly=yes URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-7D61178C Since: */ int dom_characterdata_length_read(dom_object *obj, zval *retval) { xmlNodePtr nodep = dom_object_get_node(obj); xmlChar *content; long length = 0; if (nodep == NULL) { php_dom_throw_error(INVALID_STATE_ERR, 0); return FAILURE; } content = xmlNodeGetContent(nodep); if (content) { length = xmlUTF8Strlen(content); xmlFree(content); } ZVAL_LONG(retval, length); return SUCCESS; }
/* {{{ nodeValue string readonly=no URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-F68D080 Since: */ int dom_node_node_value_read(dom_object *obj, zval *retval) { xmlNode *nodep = dom_object_get_node(obj); char *str = NULL; if (nodep == NULL) { php_dom_throw_error(INVALID_STATE_ERR, 0); return FAILURE; } /* Access to Element node is implemented as a convience method */ switch (nodep->type) { case XML_ATTRIBUTE_NODE: case XML_TEXT_NODE: case XML_ELEMENT_NODE: case XML_COMMENT_NODE: case XML_CDATA_SECTION_NODE: case XML_PI_NODE: str = (char *) xmlNodeGetContent(nodep); break; case XML_NAMESPACE_DECL: str = (char *) xmlNodeGetContent(nodep->children); break; default: str = NULL; break; } if(str != NULL) { ZVAL_STRING(retval, str); xmlFree(str); } else { ZVAL_NULL(retval); } return SUCCESS; }
/* {{{ lastChild DomNode readonly=yes URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-61AD09FB Since: */ int dom_node_last_child_read(dom_object *obj, zval *retval) { xmlNode *nodep, *last = NULL; nodep = dom_object_get_node(obj); if (nodep == NULL) { php_dom_throw_error(INVALID_STATE_ERR, 0); return FAILURE; } if (dom_node_children_valid(nodep) == SUCCESS) { last = nodep->last; } if (!last) { ZVAL_NULL(retval); return SUCCESS; } php_dom_create_object(last, retval, obj); return SUCCESS; }
/* {{{ ownerDocument DomDocument readonly=yes URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-node-ownerDoc Since: */ int dom_node_owner_document_read(dom_object *obj, zval *retval) { xmlNode *nodep = dom_object_get_node(obj); xmlDocPtr docp; if (nodep == NULL) { php_dom_throw_error(INVALID_STATE_ERR, 0); return FAILURE; } if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) { ZVAL_NULL(retval); return SUCCESS; } docp = nodep->doc; if (!docp) { return FAILURE; } php_dom_create_object((xmlNodePtr) docp, retval, obj); return SUCCESS; }
php_libxml_increment_node_ptr((php_libxml_node_object *)intern, (xmlNodePtr)nodep, (void *)intern TSRMLS_CC); } } /* }}} end DOMAttr::__construct */ /* {{{ name string readonly=yes URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-1112119403 Since: */ int dom_attr_name_read(dom_object *obj, zval **retval TSRMLS_DC) { xmlAttrPtr attrp; attrp = (xmlAttrPtr) dom_object_get_node(obj); if (attrp == NULL) { php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); return FAILURE; } ALLOC_ZVAL(*retval); ZVAL_STRING(*retval, (char *) (attrp->name), 1); return SUCCESS; } /* }}} */ /* {{{ specified boolean
/* {{{ proto void DOMElement::__construct(string name, [string value], [string uri]); */ PHP_METHOD(domelement, __construct) { zval *id; xmlNodePtr nodep = NULL, oldnode = NULL; dom_object *intern; char *name, *value = NULL, *uri = NULL; char *localname = NULL, *prefix = NULL; int errorcode = 0, uri_len = 0; int name_len, value_len = 0, name_valid; xmlNsPtr nsptr = NULL; zend_error_handling error_handling; zend_replace_error_handling(EH_THROW, dom_domexception_class_entry, &error_handling TSRMLS_CC); if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|s!s", &id, dom_element_class_entry, &name, &name_len, &value, &value_len, &uri, &uri_len) == FAILURE) { zend_restore_error_handling(&error_handling TSRMLS_CC); return; } zend_restore_error_handling(&error_handling TSRMLS_CC); name_valid = xmlValidateName((xmlChar *) name, 0); if (name_valid != 0) { php_dom_throw_error(INVALID_CHARACTER_ERR, 1 TSRMLS_CC); RETURN_FALSE; } /* Namespace logic is separate and only when uri passed in to insure no BC breakage */ if (uri_len > 0) { errorcode = dom_check_qname(name, &localname, &prefix, uri_len, name_len); if (errorcode == 0) { nodep = xmlNewNode (NULL, (xmlChar *)localname); if (nodep != NULL && uri != NULL) { nsptr = dom_get_ns(nodep, uri, &errorcode, prefix); xmlSetNs(nodep, nsptr); } } xmlFree(localname); if (prefix != NULL) { xmlFree(prefix); } if (errorcode != 0) { if (nodep != NULL) { xmlFreeNode(nodep); } php_dom_throw_error(errorcode, 1 TSRMLS_CC); RETURN_FALSE; } } else { /* If you don't pass a namespace uri, then you can't set a prefix */ localname = xmlSplitQName2((xmlChar *)name, (xmlChar **) &prefix); if (prefix != NULL) { xmlFree(localname); xmlFree(prefix); php_dom_throw_error(NAMESPACE_ERR, 1 TSRMLS_CC); RETURN_FALSE; } nodep = xmlNewNode(NULL, (xmlChar *) name); } if (!nodep) { php_dom_throw_error(INVALID_STATE_ERR, 1 TSRMLS_CC); RETURN_FALSE; } if (value_len > 0) { xmlNodeSetContentLen(nodep, (xmlChar *) value, value_len); } intern = (dom_object *)zend_object_store_get_object(id TSRMLS_CC); if (intern != NULL) { oldnode = dom_object_get_node(intern); if (oldnode != NULL) { php_libxml_node_free_resource(oldnode TSRMLS_CC); } php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern TSRMLS_CC); } }
} } /* }}} end DOMElement::__construct */ /* {{{ tagName string readonly=yes URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-104682815 Since: */ int dom_element_tag_name_read(dom_object *obj, zval **retval TSRMLS_DC) { xmlNodePtr nodep; xmlNsPtr ns; xmlChar *qname; nodep = dom_object_get_node(obj); if (nodep == NULL) { php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); return FAILURE; } ALLOC_ZVAL(*retval); ns = nodep->ns; if (ns != NULL && ns->prefix) { qname = xmlStrdup(ns->prefix); qname = xmlStrcat(qname, (xmlChar *)":"); qname = xmlStrcat(qname, nodep->name); ZVAL_STRING(*retval, (char *)qname, 1); xmlFree(qname); } else {
*/ const zend_function_entry php_dom_entity_class_functions[] = { PHP_FE_END }; /* {{{ publicId string readonly=yes URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-D7303025 Since: */ int dom_entity_public_id_read(dom_object *obj, zval **retval TSRMLS_DC) { xmlEntity *nodep; nodep = (xmlEntity *) dom_object_get_node(obj); if (nodep == NULL) { php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); return FAILURE; } ALLOC_ZVAL(*retval); if (nodep->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { ZVAL_NULL(*retval); } else { ZVAL_STRING(*retval, (char *) (nodep->ExternalID), 1); } return SUCCESS; }
*/ const zend_function_entry php_dom_documenttype_class_functions[] = { {NULL, NULL, NULL} }; /* {{{ name string readonly=yes URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1844763134 Since: */ int dom_documenttype_name_read(dom_object *obj, zval **retval TSRMLS_DC) { xmlDtdPtr dtdptr; dtdptr = (xmlDtdPtr) dom_object_get_node(obj); if (dtdptr == NULL) { php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); return FAILURE; } ALLOC_ZVAL(*retval); ZVAL_STRING(*retval, (char *) (dtdptr->name), 1); return SUCCESS; } /* }}} */ /* {{{ entities DOMNamedNodeMap
static void xsl_ext_function_php(xmlXPathParserContextPtr ctxt, int nargs, int type) /* {{{ */ { xsltTransformContextPtr tctxt; zval *args; zval retval; int result, i; int error = 0; zend_fcall_info fci; zval handler; xmlXPathObjectPtr obj; char *str; xsl_object *intern; zend_string *callable = NULL; if (! zend_is_executing()) { xsltGenericError(xsltGenericErrorContext, "xsltExtFunctionTest: Function called from outside of PHP\n"); error = 1; } else { tctxt = xsltXPathGetTransformContext(ctxt); if (tctxt == NULL) { xsltGenericError(xsltGenericErrorContext, "xsltExtFunctionTest: failed to get the transformation context\n"); error = 1; } else { intern = (xsl_object*)tctxt->_private; if (intern == NULL) { xsltGenericError(xsltGenericErrorContext, "xsltExtFunctionTest: failed to get the internal object\n"); error = 1; } else if (intern->registerPhpFunctions == 0) { xsltGenericError(xsltGenericErrorContext, "xsltExtFunctionTest: PHP Object did not register PHP functions\n"); error = 1; } } } if (error == 1) { for (i = nargs - 1; i >= 0; i--) { obj = valuePop(ctxt); xmlXPathFreeObject(obj); } return; } fci.param_count = nargs - 1; if (fci.param_count > 0) { args = safe_emalloc(fci.param_count, sizeof(zval), 0); } /* Reverse order to pop values off ctxt stack */ for (i = nargs - 2; i >= 0; i--) { obj = valuePop(ctxt); switch (obj->type) { case XPATH_STRING: ZVAL_STRING(&args[i], (char *)obj->stringval); break; case XPATH_BOOLEAN: ZVAL_BOOL(&args[i], obj->boolval); break; case XPATH_NUMBER: ZVAL_DOUBLE(&args[i], obj->floatval); break; case XPATH_NODESET: if (type == 1) { str = (char*)xmlXPathCastToString(obj); ZVAL_STRING(&args[i], str); xmlFree(str); } else if (type == 2) { int j; dom_object *domintern = (dom_object *)intern->doc; array_init(&args[i]); if (obj->nodesetval && obj->nodesetval->nodeNr > 0) { for (j = 0; j < obj->nodesetval->nodeNr; j++) { xmlNodePtr node = obj->nodesetval->nodeTab[j]; zval child; /* not sure, if we need this... it's copied from xpath.c */ if (node->type == XML_NAMESPACE_DECL) { xmlNsPtr curns; xmlNodePtr nsparent; nsparent = node->_private; curns = xmlNewNs(NULL, node->name, NULL); if (node->children) { curns->prefix = xmlStrdup((char *)node->children); } if (node->children) { node = xmlNewDocNode(node->doc, NULL, (char *) node->children, node->name); } else { node = xmlNewDocNode(node->doc, NULL, (const xmlChar *) "xmlns", node->name); } node->type = XML_NAMESPACE_DECL; node->parent = nsparent; node->ns = curns; } else { node = xmlDocCopyNodeList(domintern->document->ptr, node); } php_dom_create_object(node, &child, domintern); add_next_index_zval(&args[i], &child); } } } break; default: str = (char *) xmlXPathCastToString(obj); ZVAL_STRING(&args[i], str); xmlFree(str); } xmlXPathFreeObject(obj); } fci.size = sizeof(fci); fci.function_table = EG(function_table); if (fci.param_count > 0) { fci.params = args; } else { fci.params = NULL; } obj = valuePop(ctxt); if (obj->stringval == NULL) { php_error_docref(NULL, E_WARNING, "Handler name must be a string"); xmlXPathFreeObject(obj); valuePush(ctxt, xmlXPathNewString((const xmlChar *) "")); if (fci.param_count > 0) { for (i = 0; i < nargs - 1; i++) { zval_ptr_dtor(&args[i]); } efree(args); } return; } ZVAL_STRING(&handler, (char *) obj->stringval); xmlXPathFreeObject(obj); ZVAL_COPY_VALUE(&fci.function_name, &handler); fci.symbol_table = NULL; fci.object = NULL; fci.retval = &retval; fci.no_separation = 0; /*fci.function_handler_cache = &function_ptr;*/ if (!zend_make_callable(&handler, &callable)) { php_error_docref(NULL, E_WARNING, "Unable to call handler %s()", ZSTR_VAL(callable)); valuePush(ctxt, xmlXPathNewString((const xmlChar *) "")); } else if ( intern->registerPhpFunctions == 2 && zend_hash_exists(intern->registered_phpfunctions, callable) == 0) { php_error_docref(NULL, E_WARNING, "Not allowed to call handler '%s()'", ZSTR_VAL(callable)); /* Push an empty string, so that we at least have an xslt result... */ valuePush(ctxt, xmlXPathNewString((const xmlChar *) "")); } else { result = zend_call_function(&fci, NULL); if (result == FAILURE) { if (Z_TYPE(handler) == IS_STRING) { php_error_docref(NULL, E_WARNING, "Unable to call handler %s()", Z_STRVAL(handler)); valuePush(ctxt, xmlXPathNewString((const xmlChar *) "")); } /* retval is == NULL, when an exception occurred, don't report anything, because PHP itself will handle that */ } else if (Z_ISUNDEF(retval)) { } else { if (Z_TYPE(retval) == IS_OBJECT && instanceof_function(Z_OBJCE(retval), dom_node_class_entry)) { xmlNode *nodep; dom_object *obj; if (intern->node_list == NULL) { ALLOC_HASHTABLE(intern->node_list); zend_hash_init(intern->node_list, 0, NULL, ZVAL_PTR_DTOR, 0); } Z_ADDREF(retval); zend_hash_next_index_insert(intern->node_list, &retval); obj = Z_DOMOBJ_P(&retval); nodep = dom_object_get_node(obj); valuePush(ctxt, xmlXPathNewNodeSet(nodep)); } else if (Z_TYPE(retval) == IS_TRUE || Z_TYPE(retval) == IS_FALSE) { valuePush(ctxt, xmlXPathNewBoolean(Z_LVAL(retval))); } else if (Z_TYPE(retval) == IS_OBJECT) { php_error_docref(NULL, E_WARNING, "A PHP Object cannot be converted to a XPath-string"); valuePush(ctxt, xmlXPathNewString((const xmlChar *) "")); } else { convert_to_string_ex(&retval); valuePush(ctxt, xmlXPathNewString((xmlChar *) Z_STRVAL(retval))); } zval_ptr_dtor(&retval); } } zend_string_release(callable); zval_ptr_dtor(&handler); if (fci.param_count > 0) { for (i = 0; i < nargs - 1; i++) { zval_ptr_dtor(&args[i]); } efree(args); } }
static void dom_xpath_ext_function_php(xmlXPathParserContextPtr ctxt, int nargs, int type) /* {{{ */ { zval retval; int result, i; int error = 0; zend_fcall_info fci; xmlXPathObjectPtr obj; char *str; zend_string *callable = NULL; dom_xpath_object *intern; if (! zend_is_executing()) { xmlGenericError(xmlGenericErrorContext, "xmlExtFunctionTest: Function called from outside of PHP\n"); error = 1; } else { intern = (dom_xpath_object *) ctxt->context->userData; if (intern == NULL) { xmlGenericError(xmlGenericErrorContext, "xmlExtFunctionTest: failed to get the internal object\n"); error = 1; } else if (intern->registerPhpFunctions == 0) { xmlGenericError(xmlGenericErrorContext, "xmlExtFunctionTest: PHP Object did not register PHP functions\n"); error = 1; } } if (error == 1) { for (i = nargs - 1; i >= 0; i--) { obj = valuePop(ctxt); xmlXPathFreeObject(obj); } return; } fci.param_count = nargs - 1; if (fci.param_count > 0) { fci.params = safe_emalloc(fci.param_count, sizeof(zval), 0); } /* Reverse order to pop values off ctxt stack */ for (i = nargs - 2; i >= 0; i--) { obj = valuePop(ctxt); switch (obj->type) { case XPATH_STRING: ZVAL_STRING(&fci.params[i], (char *)obj->stringval); break; case XPATH_BOOLEAN: ZVAL_BOOL(&fci.params[i], obj->boolval); break; case XPATH_NUMBER: ZVAL_DOUBLE(&fci.params[i], obj->floatval); break; case XPATH_NODESET: if (type == 1) { str = (char *)xmlXPathCastToString(obj); ZVAL_STRING(&fci.params[i], str); xmlFree(str); } else if (type == 2) { int j; array_init(&fci.params[i]); if (obj->nodesetval && obj->nodesetval->nodeNr > 0) { for (j = 0; j < obj->nodesetval->nodeNr; j++) { xmlNodePtr node = obj->nodesetval->nodeTab[j]; zval child; /* not sure, if we need this... it's copied from xpath.c */ if (node->type == XML_NAMESPACE_DECL) { xmlNsPtr curns; xmlNodePtr nsparent; nsparent = node->_private; curns = xmlNewNs(NULL, node->name, NULL); if (node->children) { curns->prefix = xmlStrdup((xmlChar *) node->children); } if (node->children) { node = xmlNewDocNode(node->doc, NULL, (xmlChar *) node->children, node->name); } else { node = xmlNewDocNode(node->doc, NULL, (xmlChar *) "xmlns", node->name); } node->type = XML_NAMESPACE_DECL; node->parent = nsparent; node->ns = curns; } php_dom_create_object(node, &child, &intern->dom); add_next_index_zval(&fci.params[i], &child); } } } break; default: ZVAL_STRING(&fci.params[i], (char *)xmlXPathCastToString(obj)); } xmlXPathFreeObject(obj); } fci.size = sizeof(fci); fci.function_table = EG(function_table); obj = valuePop(ctxt); if (obj->stringval == NULL) { php_error_docref(NULL, E_WARNING, "Handler name must be a string"); xmlXPathFreeObject(obj); if (fci.param_count > 0) { for (i = 0; i < nargs - 1; i++) { zval_ptr_dtor(&fci.params[i]); } efree(fci.params); } return; } ZVAL_STRING(&fci.function_name, (char *) obj->stringval); xmlXPathFreeObject(obj); fci.symbol_table = NULL; fci.object = NULL; fci.retval = &retval; fci.no_separation = 0; if (!zend_make_callable(&fci.function_name, &callable)) { php_error_docref(NULL, E_WARNING, "Unable to call handler %s()", callable->val); } else if (intern->registerPhpFunctions == 2 && zend_hash_exists(intern->registered_phpfunctions, callable) == 0) { php_error_docref(NULL, E_WARNING, "Not allowed to call handler '%s()'.", callable->val); /* Push an empty string, so that we at least have an xslt result... */ valuePush(ctxt, xmlXPathNewString((xmlChar *)"")); } else { result = zend_call_function(&fci, NULL); if (result == SUCCESS && Z_TYPE(retval) != IS_UNDEF) { if (Z_TYPE(retval) == IS_OBJECT && instanceof_function(Z_OBJCE(retval), dom_node_class_entry)) { xmlNode *nodep; dom_object *obj; if (intern->node_list == NULL) { ALLOC_HASHTABLE(intern->node_list); zend_hash_init(intern->node_list, 0, NULL, ZVAL_PTR_DTOR, 0); } GC_REFCOUNT(&retval)++; zend_hash_next_index_insert(intern->node_list, &retval); obj = Z_DOMOBJ_P(&retval); nodep = dom_object_get_node(obj); valuePush(ctxt, xmlXPathNewNodeSet(nodep)); } else if (Z_TYPE(retval) == IS_FALSE || Z_TYPE(retval) == IS_TRUE) { valuePush(ctxt, xmlXPathNewBoolean(Z_TYPE(retval) == IS_TRUE)); } else if (Z_TYPE(retval) == IS_OBJECT) { php_error_docref(NULL, E_WARNING, "A PHP Object cannot be converted to a XPath-string"); valuePush(ctxt, xmlXPathNewString((xmlChar *)"")); } else { zend_string *str = zval_get_string(&retval); valuePush(ctxt, xmlXPathNewString((xmlChar *) str->val)); zend_string_release(str); } zval_ptr_dtor(&retval); } } zend_string_release(callable); zval_dtor(&fci.function_name); if (fci.param_count > 0) { for (i = 0; i < nargs - 1; i++) { zval_ptr_dtor(&fci.params[i]); } efree(fci.params); } }
static void php_dom_iterator_move_forward(zend_object_iterator *iter) /* {{{ */ { zval *object; xmlNodePtr curnode = NULL, basenode; dom_object *intern; dom_object *nnmap; dom_nnodemap_object *objmap; int previndex=0; HashTable *nodeht; zval *entry; zend_bool do_curobj_undef = 1; php_dom_iterator *iterator = (php_dom_iterator *)iter; object = &iterator->intern.data; nnmap = Z_DOMOBJ_P(object); objmap = (dom_nnodemap_object *)nnmap->ptr; intern = Z_DOMOBJ_P(&iterator->curobj); if (intern != NULL && intern->ptr != NULL) { if (objmap->nodetype != XML_ENTITY_NODE && objmap->nodetype != XML_NOTATION_NODE) { if (objmap->nodetype == DOM_NODESET) { nodeht = HASH_OF(&objmap->baseobj_zv); zend_hash_move_forward_ex(nodeht, &iterator->pos); if ((entry = zend_hash_get_current_data_ex(nodeht, &iterator->pos))) { zval_ptr_dtor(&iterator->curobj); ZVAL_UNDEF(&iterator->curobj); ZVAL_COPY(&iterator->curobj, entry); do_curobj_undef = 0; } } else { curnode = (xmlNodePtr)((php_libxml_node_ptr *)intern->ptr)->node; if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) { curnode = curnode->next; } else { /* Nav the tree evey time as this is LIVE */ basenode = dom_object_get_node(objmap->baseobj); if (basenode && (basenode->type == XML_DOCUMENT_NODE || basenode->type == XML_HTML_DOCUMENT_NODE)) { basenode = xmlDocGetRootElement((xmlDoc *) basenode); } else if (basenode) { basenode = basenode->children; } else { goto err; } curnode = dom_get_elements_by_tag_name_ns_raw( basenode, (char *) objmap->ns, (char *) objmap->local, &previndex, iter->index); } } } else { if (objmap->nodetype == XML_ENTITY_NODE) { curnode = php_dom_libxml_hash_iter(objmap->ht, iter->index); } else { curnode = php_dom_libxml_notation_iter(objmap->ht, iter->index); } } } err: if (do_curobj_undef) { zval_ptr_dtor(&iterator->curobj); ZVAL_UNDEF(&iterator->curobj); } if (curnode) { php_dom_create_object(curnode, &iterator->curobj, objmap->baseobj); } }
int dom_node_prefix_write(dom_object *obj, zval *newval) { zend_string *str; xmlNode *nodep, *nsnode = NULL; xmlNsPtr ns = NULL, curns; char *strURI; char *prefix; nodep = dom_object_get_node(obj); if (nodep == NULL) { php_dom_throw_error(INVALID_STATE_ERR, 0); return FAILURE; } switch (nodep->type) { case XML_ELEMENT_NODE: nsnode = nodep; case XML_ATTRIBUTE_NODE: if (nsnode == NULL) { nsnode = nodep->parent; if (nsnode == NULL) { nsnode = xmlDocGetRootElement(nodep->doc); } } str = zval_get_string(newval); prefix = ZSTR_VAL(str); if (nsnode && nodep->ns != NULL && !xmlStrEqual(nodep->ns->prefix, (xmlChar *)prefix)) { strURI = (char *) nodep->ns->href; if (strURI == NULL || (!strcmp(prefix, "xml") && strcmp(strURI, (char *) XML_XML_NAMESPACE)) || (nodep->type == XML_ATTRIBUTE_NODE && !strcmp(prefix, "xmlns") && strcmp(strURI, (char *) DOM_XMLNS_NAMESPACE)) || (nodep->type == XML_ATTRIBUTE_NODE && !strcmp((char *) nodep->name, "xmlns"))) { ns = NULL; } else { curns = nsnode->nsDef; while (curns != NULL) { if (xmlStrEqual((xmlChar *)prefix, curns->prefix) && xmlStrEqual(nodep->ns->href, curns->href)) { ns = curns; break; } curns = curns->next; } if (ns == NULL) { ns = xmlNewNs(nsnode, nodep->ns->href, (xmlChar *)prefix); } } if (ns == NULL) { zend_string_release_ex(str, 0); php_dom_throw_error(NAMESPACE_ERR, dom_get_strict_error(obj->document)); return FAILURE; } xmlSetNs(nodep, ns); } zend_string_release_ex(str, 0); break; default: break; } return SUCCESS; }
zend_object_iterator *php_dom_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */ { dom_object *intern; dom_nnodemap_object *objmap; xmlNodePtr nodep, curnode=NULL; int curindex = 0; HashTable *nodeht; zval *entry; php_dom_iterator *iterator; if (by_ref) { zend_error(E_ERROR, "An iterator cannot be used with foreach by reference"); } iterator = emalloc(sizeof(php_dom_iterator)); zend_iterator_init(&iterator->intern); ZVAL_COPY(&iterator->intern.data, object); iterator->intern.funcs = &php_dom_iterator_funcs; ZVAL_UNDEF(&iterator->curobj); intern = Z_DOMOBJ_P(object); objmap = (dom_nnodemap_object *)intern->ptr; if (objmap != NULL) { if (objmap->nodetype != XML_ENTITY_NODE && objmap->nodetype != XML_NOTATION_NODE) { if (objmap->nodetype == DOM_NODESET) { nodeht = HASH_OF(&objmap->baseobj_zv); zend_hash_internal_pointer_reset_ex(nodeht, &iterator->pos); if ((entry = zend_hash_get_current_data_ex(nodeht, &iterator->pos))) { ZVAL_COPY(&iterator->curobj, entry); } } else { nodep = (xmlNode *)dom_object_get_node(objmap->baseobj); if (!nodep) { goto err; } if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) { if (objmap->nodetype == XML_ATTRIBUTE_NODE) { curnode = (xmlNodePtr) nodep->properties; } else { curnode = (xmlNodePtr) nodep->children; } } else { if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) { nodep = xmlDocGetRootElement((xmlDoc *) nodep); } else { nodep = nodep->children; } curnode = dom_get_elements_by_tag_name_ns_raw( nodep, (char *) objmap->ns, (char *) objmap->local, &curindex, 0); } } } else { if (objmap->nodetype == XML_ENTITY_NODE) { curnode = php_dom_libxml_hash_iter(objmap->ht, 0); } else { curnode = php_dom_libxml_notation_iter(objmap->ht, 0); } } } err: if (curnode) { php_dom_create_object(curnode, &iterator->curobj, objmap->baseobj); } return &iterator->intern; }
/* {{{ nodeName string readonly=yes URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-F68D095 Since: */ int dom_node_node_name_read(dom_object *obj, zval *retval) { xmlNode *nodep; xmlNsPtr ns; char *str = NULL; xmlChar *qname = NULL; nodep = dom_object_get_node(obj); if (nodep == NULL) { php_dom_throw_error(INVALID_STATE_ERR, 0); return FAILURE; } switch (nodep->type) { case XML_ATTRIBUTE_NODE: case XML_ELEMENT_NODE: ns = nodep->ns; if (ns != NULL && ns->prefix) { qname = xmlStrdup(ns->prefix); qname = xmlStrcat(qname, (xmlChar *) ":"); qname = xmlStrcat(qname, nodep->name); str = (char *) qname; } else { str = (char *) nodep->name; } break; case XML_NAMESPACE_DECL: ns = nodep->ns; if (ns != NULL && ns->prefix) { qname = xmlStrdup((xmlChar *) "xmlns"); qname = xmlStrcat(qname, (xmlChar *) ":"); qname = xmlStrcat(qname, nodep->name); str = (char *) qname; } else { str = (char *) nodep->name; } break; case XML_DOCUMENT_TYPE_NODE: case XML_DTD_NODE: case XML_PI_NODE: case XML_ENTITY_DECL: case XML_ENTITY_REF_NODE: case XML_NOTATION_NODE: str = (char *) nodep->name; break; case XML_CDATA_SECTION_NODE: str = "#cdata-section"; break; case XML_COMMENT_NODE: str = "#comment"; break; case XML_HTML_DOCUMENT_NODE: case XML_DOCUMENT_NODE: str = "#document"; break; case XML_DOCUMENT_FRAG_NODE: str = "#document-fragment"; break; case XML_TEXT_NODE: str = "#text"; break; default: php_error_docref(NULL, E_WARNING, "Invalid Node Type"); } if (str != NULL) { ZVAL_STRING(retval, str); } else { ZVAL_EMPTY_STRING(retval); } if (qname != NULL) { xmlFree(qname); } return SUCCESS; }
PHP_FALIAS(substringData, dom_characterdata_substring_data, arginfo_dom_characterdata_substring_data) PHP_FALIAS(appendData, dom_characterdata_append_data, arginfo_dom_characterdata_append_data) PHP_FALIAS(insertData, dom_characterdata_insert_data, arginfo_dom_characterdata_insert_data) PHP_FALIAS(deleteData, dom_characterdata_delete_data, arginfo_dom_characterdata_delete_data) PHP_FALIAS(replaceData, dom_characterdata_replace_data, arginfo_dom_characterdata_replace_data) PHP_FE_END }; /* {{{ data string readonly=no URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-72AB8359 Since: */ int dom_characterdata_data_read(dom_object *obj, zval *retval TSRMLS_DC) { xmlNodePtr nodep = dom_object_get_node(obj); xmlChar *content; if (nodep == NULL) { php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC); return FAILURE; } if ((content = xmlNodeGetContent(nodep)) != NULL) { ZVAL_STRING(retval, (char *) content); xmlFree(content); } else { ZVAL_EMPTY_STRING(retval); } return SUCCESS;
/* {{{ proto void DOMElement::__construct(string name, [string value], [string uri]) */ PHP_METHOD(domelement, __construct) { zval *id = getThis(); xmlNodePtr nodep = NULL, oldnode = NULL; dom_object *intern; char *name, *value = NULL, *uri = NULL; char *localname = NULL, *prefix = NULL; int errorcode = 0; size_t name_len, value_len = 0, uri_len = 0; int name_valid; xmlNsPtr nsptr = NULL; if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s|s!s", &name, &name_len, &value, &value_len, &uri, &uri_len) == FAILURE) { return; } name_valid = xmlValidateName((xmlChar *) name, 0); if (name_valid != 0) { php_dom_throw_error(INVALID_CHARACTER_ERR, 1); RETURN_FALSE; } /* Namespace logic is separate and only when uri passed in to insure no BC breakage */ if (uri_len > 0) { errorcode = dom_check_qname(name, &localname, &prefix, uri_len, name_len); if (errorcode == 0) { nodep = xmlNewNode (NULL, (xmlChar *)localname); if (nodep != NULL && uri != NULL) { nsptr = dom_get_ns(nodep, uri, &errorcode, prefix); xmlSetNs(nodep, nsptr); } } xmlFree(localname); if (prefix != NULL) { xmlFree(prefix); } if (errorcode != 0) { if (nodep != NULL) { xmlFreeNode(nodep); } php_dom_throw_error(errorcode, 1); RETURN_FALSE; } } else { /* If you don't pass a namespace uri, then you can't set a prefix */ localname = (char *) xmlSplitQName2((xmlChar *) name, (xmlChar **) &prefix); if (prefix != NULL) { xmlFree(localname); xmlFree(prefix); php_dom_throw_error(NAMESPACE_ERR, 1); RETURN_FALSE; } nodep = xmlNewNode(NULL, (xmlChar *) name); } if (!nodep) { php_dom_throw_error(INVALID_STATE_ERR, 1); RETURN_FALSE; } if (value_len > 0) { xmlNodeSetContentLen(nodep, (xmlChar *) value, value_len); } intern = Z_DOMOBJ_P(id); oldnode = dom_object_get_node(intern); if (oldnode != NULL) { php_libxml_node_free_resource(oldnode ); } php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern); }
static void dom_xpath_ext_function_php(xmlXPathParserContextPtr ctxt, int nargs, int type) /* {{{ */ { zval **args = NULL; zval *retval; int result, i, ret; int error = 0; zend_fcall_info fci; zval handler; xmlXPathObjectPtr obj; char *str; char *callable = NULL; dom_xpath_object *intern; TSRMLS_FETCH(); if (! zend_is_executing(TSRMLS_C)) { xmlGenericError(xmlGenericErrorContext, "xmlExtFunctionTest: Function called from outside of PHP\n"); error = 1; } else { intern = (dom_xpath_object *) ctxt->context->userData; if (intern == NULL) { xmlGenericError(xmlGenericErrorContext, "xmlExtFunctionTest: failed to get the internal object\n"); error = 1; } else if (intern->registerPhpFunctions == 0) { xmlGenericError(xmlGenericErrorContext, "xmlExtFunctionTest: PHP Object did not register PHP functions\n"); error = 1; } } if (error == 1) { for (i = nargs - 1; i >= 0; i--) { obj = valuePop(ctxt); xmlXPathFreeObject(obj); } return; } fci.param_count = nargs - 1; if (fci.param_count > 0) { fci.params = safe_emalloc(fci.param_count, sizeof(zval**), 0); args = safe_emalloc(fci.param_count, sizeof(zval *), 0); } /* Reverse order to pop values off ctxt stack */ for (i = nargs - 2; i >= 0; i--) { obj = valuePop(ctxt); MAKE_STD_ZVAL(args[i]); switch (obj->type) { case XPATH_STRING: ZVAL_STRING(args[i], (char *)obj->stringval, 1); break; case XPATH_BOOLEAN: ZVAL_BOOL(args[i], obj->boolval); break; case XPATH_NUMBER: ZVAL_DOUBLE(args[i], obj->floatval); break; case XPATH_NODESET: if (type == 1) { str = (char *)xmlXPathCastToString(obj); ZVAL_STRING(args[i], str, 1); xmlFree(str); } else if (type == 2) { int j; array_init(args[i]); if (obj->nodesetval && obj->nodesetval->nodeNr > 0) { for (j = 0; j < obj->nodesetval->nodeNr; j++) { xmlNodePtr node = obj->nodesetval->nodeTab[j]; zval *child; MAKE_STD_ZVAL(child); /* not sure, if we need this... it's copied from xpath.c */ if (node->type == XML_NAMESPACE_DECL) { xmlNsPtr curns; xmlNodePtr nsparent; nsparent = node->_private; curns = xmlNewNs(NULL, node->name, NULL); if (node->children) { curns->prefix = xmlStrdup((xmlChar *) node->children); } if (node->children) { node = xmlNewDocNode(node->doc, NULL, (xmlChar *) node->children, node->name); } else { node = xmlNewDocNode(node->doc, NULL, (xmlChar *) "xmlns", node->name); } node->type = XML_NAMESPACE_DECL; node->parent = nsparent; node->ns = curns; } child = php_dom_create_object(node, &ret, child, (dom_object *)intern TSRMLS_CC); add_next_index_zval(args[i], child); } } } break; default: ZVAL_STRING(args[i], (char *)xmlXPathCastToString(obj), 1); } xmlXPathFreeObject(obj); fci.params[i] = &args[i]; } fci.size = sizeof(fci); fci.function_table = EG(function_table); obj = valuePop(ctxt); if (obj->stringval == NULL) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Handler name must be a string"); xmlXPathFreeObject(obj); if (fci.param_count > 0) { for (i = 0; i < nargs - 1; i++) { zval_ptr_dtor(&args[i]); } efree(args); efree(fci.params); } return; } INIT_PZVAL(&handler); ZVAL_STRING(&handler, obj->stringval, 1); xmlXPathFreeObject(obj); fci.function_name = &handler; fci.symbol_table = NULL; fci.object_ptr = NULL; fci.retval_ptr_ptr = &retval; fci.no_separation = 0; if (!zend_make_callable(&handler, &callable TSRMLS_CC)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to call handler %s()", callable); } else if ( intern->registerPhpFunctions == 2 && zend_hash_exists(intern->registered_phpfunctions, callable, strlen(callable) + 1) == 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not allowed to call handler '%s()'.", callable); /* Push an empty string, so that we at least have an xslt result... */ valuePush(ctxt, xmlXPathNewString((xmlChar *)"")); } else { result = zend_call_function(&fci, NULL TSRMLS_CC); if (result == FAILURE) { if (Z_TYPE(handler) == IS_STRING) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to call handler %s()", Z_STRVAL_P(&handler)); } /* retval is == NULL, when an exception occurred, don't report anything, because PHP itself will handle that */ } else if (retval == NULL) { } else { if (retval->type == IS_OBJECT && instanceof_function( Z_OBJCE_P(retval), dom_node_class_entry TSRMLS_CC)) { xmlNode *nodep; dom_object *obj; if (intern->node_list == NULL) { ALLOC_HASHTABLE(intern->node_list); zend_hash_init(intern->node_list, 0, NULL, ZVAL_PTR_DTOR, 0); } zval_add_ref(&retval); zend_hash_next_index_insert(intern->node_list, &retval, sizeof(zval *), NULL); obj = (dom_object *)zend_object_store_get_object(retval TSRMLS_CC); nodep = dom_object_get_node(obj); valuePush(ctxt, xmlXPathNewNodeSet(nodep)); } else if (retval->type == IS_BOOL) { valuePush(ctxt, xmlXPathNewBoolean(retval->value.lval)); } else if (retval->type == IS_OBJECT) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "A PHP Object cannot be converted to a XPath-string"); valuePush(ctxt, xmlXPathNewString((xmlChar *)"")); } else { convert_to_string_ex(&retval); valuePush(ctxt, xmlXPathNewString( Z_STRVAL_P(retval))); } zval_ptr_dtor(&retval); } } efree(callable); zval_dtor(&handler); if (fci.param_count > 0) { for (i = 0; i < nargs - 1; i++) { zval_ptr_dtor(&args[i]); } efree(args); efree(fci.params); } }