domimplementation.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include "php.h"
  23. #if HAVE_LIBXML && HAVE_DOM
  24. #include "php_dom.h"
  25. /* {{{ arginfo */
  26. ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_implementation_get_feature, 0, 0, 2)
  27. ZEND_ARG_INFO(0, feature)
  28. ZEND_ARG_INFO(0, version)
  29. ZEND_END_ARG_INFO();
  30. ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_implementation_has_feature, 0, 0, 0)
  31. ZEND_END_ARG_INFO();
  32. ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_implementation_create_documenttype, 0, 0, 3)
  33. ZEND_ARG_INFO(0, qualifiedName)
  34. ZEND_ARG_INFO(0, publicId)
  35. ZEND_ARG_INFO(0, systemId)
  36. ZEND_END_ARG_INFO();
  37. ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_implementation_create_document, 0, 0, 3)
  38. ZEND_ARG_INFO(0, namespaceURI)
  39. ZEND_ARG_INFO(0, qualifiedName)
  40. ZEND_ARG_OBJ_INFO(0, docType, DOMDocumentType, 0)
  41. ZEND_END_ARG_INFO();
  42. /* }}} */
  43. /*
  44. * class DOMImplementation
  45. *
  46. * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-102161490
  47. * Since:
  48. */
  49. const zend_function_entry php_dom_domimplementation_class_functions[] = {
  50. PHP_ME(domimplementation, getFeature, arginfo_dom_implementation_get_feature, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC)
  51. PHP_ME(domimplementation, hasFeature, arginfo_dom_implementation_has_feature, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC)
  52. PHP_ME(domimplementation, createDocumentType, arginfo_dom_implementation_create_documenttype, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC)
  53. PHP_ME(domimplementation, createDocument, arginfo_dom_implementation_create_document, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC)
  54. PHP_FE_END
  55. };
  56. /* {{{ proto bool dom_domimplementation_has_feature(string feature, string version);
  57. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-5CED94D7
  58. Since:
  59. */
  60. PHP_METHOD(domimplementation, hasFeature)
  61. {
  62. size_t feature_len, version_len;
  63. char *feature, *version;
  64. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &feature, &feature_len, &version, &version_len) == FAILURE) {
  65. return;
  66. }
  67. if (dom_has_feature(feature, version)) {
  68. RETURN_TRUE;
  69. } else {
  70. RETURN_FALSE;
  71. }
  72. }
  73. /* }}} end dom_domimplementation_has_feature */
  74. /* {{{ proto DOMDocumentType dom_domimplementation_create_document_type(string qualifiedName, string publicId, string systemId);
  75. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Level-2-Core-DOM-createDocType
  76. Since: DOM Level 2
  77. */
  78. PHP_METHOD(domimplementation, createDocumentType)
  79. {
  80. xmlDtd *doctype;
  81. int ret;
  82. size_t name_len = 0, publicid_len = 0, systemid_len = 0;
  83. char *name = NULL, *publicid = NULL, *systemid = NULL;
  84. xmlChar *pch1 = NULL, *pch2 = NULL, *localname = NULL;
  85. xmlURIPtr uri;
  86. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sss", &name, &name_len, &publicid, &publicid_len, &systemid, &systemid_len) == FAILURE) {
  87. return;
  88. }
  89. if (name_len == 0) {
  90. php_error_docref(NULL, E_WARNING, "qualifiedName is required");
  91. RETURN_FALSE;
  92. }
  93. if (publicid_len > 0) {
  94. pch1 = (xmlChar *) publicid;
  95. }
  96. if (systemid_len > 0) {
  97. pch2 = (xmlChar *) systemid;
  98. }
  99. uri = xmlParseURI(name);
  100. if (uri != NULL && uri->opaque != NULL) {
  101. localname = xmlStrdup((xmlChar *) uri->opaque);
  102. if (xmlStrchr(localname, (xmlChar) ':') != NULL) {
  103. php_dom_throw_error(NAMESPACE_ERR, 1);
  104. xmlFreeURI(uri);
  105. xmlFree(localname);
  106. RETURN_FALSE;
  107. }
  108. } else {
  109. localname = xmlStrdup((xmlChar *) name);
  110. }
  111. /* TODO: Test that localname has no invalid chars
  112. php_dom_throw_error(INVALID_CHARACTER_ERR,);
  113. */
  114. if (uri) {
  115. xmlFreeURI(uri);
  116. }
  117. doctype = xmlCreateIntSubset(NULL, localname, pch1, pch2);
  118. xmlFree(localname);
  119. if (doctype == NULL) {
  120. php_error_docref(NULL, E_WARNING, "Unable to create DocumentType");
  121. RETURN_FALSE;
  122. }
  123. DOM_RET_OBJ((xmlNodePtr) doctype, &ret, NULL);
  124. }
  125. /* }}} end dom_domimplementation_create_document_type */
  126. /* {{{ proto DOMDocument dom_domimplementation_create_document(string namespaceURI, string qualifiedName, DOMDocumentType doctype);
  127. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Level-2-Core-DOM-createDocument
  128. Since: DOM Level 2
  129. */
  130. PHP_METHOD(domimplementation, createDocument)
  131. {
  132. zval *node = NULL;
  133. xmlDoc *docp;
  134. xmlNode *nodep;
  135. xmlDtdPtr doctype = NULL;
  136. xmlNsPtr nsptr = NULL;
  137. int ret, errorcode = 0;
  138. size_t uri_len = 0, name_len = 0;
  139. char *uri = NULL, *name = NULL;
  140. char *prefix = NULL, *localname = NULL;
  141. dom_object *doctobj;
  142. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|ssO", &uri, &uri_len, &name, &name_len, &node, dom_documenttype_class_entry) == FAILURE) {
  143. return;
  144. }
  145. if (node != NULL) {
  146. DOM_GET_OBJ(doctype, node, xmlDtdPtr, doctobj);
  147. if (doctype->type == XML_DOCUMENT_TYPE_NODE) {
  148. php_error_docref(NULL, E_WARNING, "Invalid DocumentType object");
  149. RETURN_FALSE;
  150. }
  151. if (doctype->doc != NULL) {
  152. php_dom_throw_error(WRONG_DOCUMENT_ERR, 1);
  153. RETURN_FALSE;
  154. }
  155. } else {
  156. doctobj = NULL;
  157. }
  158. if (name_len > 0) {
  159. errorcode = dom_check_qname(name, &localname, &prefix, 1, name_len);
  160. if (errorcode == 0 && uri_len > 0
  161. && ((nsptr = xmlNewNs(NULL, (xmlChar *) uri, (xmlChar *) prefix)) == NULL)
  162. ) {
  163. errorcode = NAMESPACE_ERR;
  164. }
  165. }
  166. if (prefix != NULL) {
  167. xmlFree(prefix);
  168. }
  169. if (errorcode != 0) {
  170. if (localname != NULL) {
  171. xmlFree(localname);
  172. }
  173. php_dom_throw_error(errorcode, 1);
  174. RETURN_FALSE;
  175. }
  176. /* currently letting libxml2 set the version string */
  177. docp = xmlNewDoc(NULL);
  178. if (!docp) {
  179. if (localname != NULL) {
  180. xmlFree(localname);
  181. }
  182. RETURN_FALSE;
  183. }
  184. if (doctype != NULL) {
  185. docp->intSubset = doctype;
  186. doctype->parent = docp;
  187. doctype->doc = docp;
  188. docp->children = (xmlNodePtr) doctype;
  189. docp->last = (xmlNodePtr) doctype;
  190. }
  191. if (localname != NULL) {
  192. nodep = xmlNewDocNode(docp, nsptr, (xmlChar *) localname, NULL);
  193. if (!nodep) {
  194. if (doctype != NULL) {
  195. docp->intSubset = NULL;
  196. doctype->parent = NULL;
  197. doctype->doc = NULL;
  198. docp->children = NULL;
  199. docp->last = NULL;
  200. }
  201. xmlFreeDoc(docp);
  202. xmlFree(localname);
  203. /* Need some type of error here */
  204. php_error_docref(NULL, E_WARNING, "Unexpected Error");
  205. RETURN_FALSE;
  206. }
  207. nodep->nsDef = nsptr;
  208. xmlDocSetRootElement(docp, nodep);
  209. xmlFree(localname);
  210. }
  211. DOM_RET_OBJ((xmlNodePtr) docp, &ret, NULL);
  212. if (doctobj != NULL) {
  213. doctobj->document = ((dom_object *)((php_libxml_node_ptr *)docp->_private)->_private)->document;
  214. php_libxml_increment_doc_ref((php_libxml_node_object *)doctobj, docp);
  215. }
  216. }
  217. /* }}} end dom_domimplementation_create_document */
  218. /* {{{ proto DOMNode dom_domimplementation_get_feature(string feature, string version);
  219. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#DOMImplementation3-getFeature
  220. Since: DOM Level 3
  221. */
  222. PHP_METHOD(domimplementation, getFeature)
  223. {
  224. DOM_NOT_IMPLEMENTED();
  225. }
  226. /* }}} end dom_domimplementation_get_feature */
  227. #endif
  228. /*
  229. * Local variables:
  230. * tab-width: 4
  231. * c-basic-offset: 4
  232. * End:
  233. * vim600: noet sw=4 ts=4 fdm=marker
  234. * vim<600: noet sw=4 ts=4
  235. */