domimplementation.c 7.9 KB

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