domimplementation.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include "php.h"
  21. #if defined(HAVE_LIBXML) && defined(HAVE_DOM)
  22. #include "php_dom.h"
  23. /*
  24. * class DOMImplementation
  25. *
  26. * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-102161490
  27. * Since:
  28. */
  29. /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-5CED94D7
  30. Since:
  31. */
  32. PHP_METHOD(DOMImplementation, hasFeature)
  33. {
  34. zend_string *feature, *version;
  35. if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS", &feature, &version) == FAILURE) {
  36. RETURN_THROWS();
  37. }
  38. RETURN_BOOL(dom_has_feature(feature, version));
  39. }
  40. /* }}} end dom_domimplementation_has_feature */
  41. /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Level-2-Core-DOM-createDocType
  42. Since: DOM Level 2
  43. */
  44. PHP_METHOD(DOMImplementation, createDocumentType)
  45. {
  46. xmlDtd *doctype;
  47. int ret;
  48. size_t name_len = 0, publicid_len = 0, systemid_len = 0;
  49. char *name = NULL, *publicid = NULL, *systemid = NULL;
  50. xmlChar *pch1 = NULL, *pch2 = NULL, *localname = NULL;
  51. xmlURIPtr uri;
  52. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|ss", &name, &name_len, &publicid, &publicid_len, &systemid, &systemid_len) == FAILURE) {
  53. RETURN_THROWS();
  54. }
  55. if (name_len == 0) {
  56. zend_argument_value_error(1, "cannot be empty");
  57. RETURN_THROWS();
  58. }
  59. if (publicid_len > 0) {
  60. pch1 = (xmlChar *) publicid;
  61. }
  62. if (systemid_len > 0) {
  63. pch2 = (xmlChar *) systemid;
  64. }
  65. if (strstr(name, "%00")) {
  66. php_error_docref(NULL, E_WARNING, "URI must not contain percent-encoded NUL bytes");
  67. RETURN_FALSE;
  68. }
  69. uri = xmlParseURI(name);
  70. if (uri != NULL && uri->opaque != NULL) {
  71. localname = xmlStrdup((xmlChar *) uri->opaque);
  72. if (xmlStrchr(localname, (xmlChar) ':') != NULL) {
  73. php_dom_throw_error(NAMESPACE_ERR, 1);
  74. xmlFreeURI(uri);
  75. xmlFree(localname);
  76. RETURN_FALSE;
  77. }
  78. } else {
  79. localname = xmlStrdup((xmlChar *) name);
  80. }
  81. /* TODO: Test that localname has no invalid chars
  82. php_dom_throw_error(INVALID_CHARACTER_ERR,);
  83. */
  84. if (uri) {
  85. xmlFreeURI(uri);
  86. }
  87. doctype = xmlCreateIntSubset(NULL, localname, pch1, pch2);
  88. xmlFree(localname);
  89. if (doctype == NULL) {
  90. php_error_docref(NULL, E_WARNING, "Unable to create DocumentType");
  91. RETURN_FALSE;
  92. }
  93. DOM_RET_OBJ((xmlNodePtr) doctype, &ret, NULL);
  94. }
  95. /* }}} end dom_domimplementation_create_document_type */
  96. /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Level-2-Core-DOM-createDocument
  97. Since: DOM Level 2
  98. */
  99. PHP_METHOD(DOMImplementation, createDocument)
  100. {
  101. zval *node = NULL;
  102. xmlDoc *docp;
  103. xmlNode *nodep;
  104. xmlDtdPtr doctype = NULL;
  105. xmlNsPtr nsptr = NULL;
  106. int ret, errorcode = 0;
  107. size_t uri_len = 0, name_len = 0;
  108. char *uri = NULL, *name = NULL;
  109. char *prefix = NULL, *localname = NULL;
  110. dom_object *doctobj;
  111. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!sO!", &uri, &uri_len, &name, &name_len, &node, dom_documenttype_class_entry) == FAILURE) {
  112. RETURN_THROWS();
  113. }
  114. if (node != NULL) {
  115. DOM_GET_OBJ(doctype, node, xmlDtdPtr, doctobj);
  116. if (doctype->type == XML_DOCUMENT_TYPE_NODE) {
  117. zend_argument_value_error(3, "is an invalid DocumentType object");
  118. RETURN_THROWS();
  119. }
  120. if (doctype->doc != NULL) {
  121. php_dom_throw_error(WRONG_DOCUMENT_ERR, 1);
  122. RETURN_THROWS();
  123. }
  124. } else {
  125. doctobj = NULL;
  126. }
  127. if (name_len > 0) {
  128. errorcode = dom_check_qname(name, &localname, &prefix, 1, name_len);
  129. if (errorcode == 0 && uri_len > 0
  130. && ((nsptr = xmlNewNs(NULL, (xmlChar *) uri, (xmlChar *) prefix)) == NULL)
  131. ) {
  132. errorcode = NAMESPACE_ERR;
  133. }
  134. }
  135. if (prefix != NULL) {
  136. xmlFree(prefix);
  137. }
  138. if (errorcode != 0) {
  139. if (localname != NULL) {
  140. xmlFree(localname);
  141. }
  142. php_dom_throw_error(errorcode, 1);
  143. RETURN_THROWS();
  144. }
  145. /* currently letting libxml2 set the version string */
  146. docp = xmlNewDoc(NULL);
  147. if (!docp) {
  148. if (localname != NULL) {
  149. xmlFree(localname);
  150. }
  151. RETURN_FALSE;
  152. }
  153. if (doctype != NULL) {
  154. docp->intSubset = doctype;
  155. doctype->parent = docp;
  156. doctype->doc = docp;
  157. docp->children = (xmlNodePtr) doctype;
  158. docp->last = (xmlNodePtr) doctype;
  159. }
  160. if (localname != NULL) {
  161. nodep = xmlNewDocNode(docp, nsptr, (xmlChar *) localname, NULL);
  162. if (!nodep) {
  163. if (doctype != NULL) {
  164. docp->intSubset = NULL;
  165. doctype->parent = NULL;
  166. doctype->doc = NULL;
  167. docp->children = NULL;
  168. docp->last = NULL;
  169. }
  170. xmlFreeDoc(docp);
  171. xmlFree(localname);
  172. /* Need some better type of error here */
  173. php_dom_throw_error(PHP_ERR, 1);
  174. RETURN_THROWS();
  175. }
  176. nodep->nsDef = nsptr;
  177. xmlDocSetRootElement(docp, nodep);
  178. xmlFree(localname);
  179. }
  180. DOM_RET_OBJ((xmlNodePtr) docp, &ret, NULL);
  181. if (doctobj != NULL) {
  182. doctobj->document = ((dom_object *)((php_libxml_node_ptr *)docp->_private)->_private)->document;
  183. php_libxml_increment_doc_ref((php_libxml_node_object *)doctobj, docp);
  184. }
  185. }
  186. /* }}} end dom_domimplementation_create_document */
  187. /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#DOMImplementation3-getFeature
  188. Since: DOM Level 3
  189. */
  190. PHP_METHOD(DOMImplementation, getFeature)
  191. {
  192. size_t feature_len, version_len;
  193. char *feature, *version;
  194. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &feature, &feature_len, &version, &version_len) == FAILURE) {
  195. RETURN_THROWS();
  196. }
  197. DOM_NOT_IMPLEMENTED();
  198. }
  199. /* }}} end dom_domimplementation_get_feature */
  200. #endif