> -----Original Message-----
> D.Kingma wrote:
>
> > Hi Rob / Chregu,
> >
> > As a first (small) step to implementing xml-security specs
> (dig-sign,
> > encryption) I created a patch against HEAD to expose
> (exclusive-)C14n
> > functionality from libxml2. I allows you to canonize a single node
> > (with it's children) or a whole document, with or without comments,
> > exclusive or not. I also created a small test file to show it's
> > functionality. Could you please review the patch or else
> commit it (if
> > it's ok with everybody)?
>
> You have to use text file (.txt) in this list. Or provide a
> link to the patch.
>
> --Pierre
Once again, this time as a txt file.
Regards,
David
--TEST--
DOMDocument canonize function
--FILE--
<?php
$txt2 = "<foo:Root xmlns:bar=\"http://example.org/bar\"
xmlns:baz=\"http://example.org/baz\" xmlns:foo=\"http://example.org/foo\"
xmlns=\"http://example.org/\" xml:lang=\"en-ie\">
<bar:Something xmlns:bar=\"http://example.org/bar\">
<foo:Something>
<bar:Something>
<foo:Something>
<!-- Here some comment -->
<baz:Something />
</foo:Something>
</bar:Something>
</foo:Something>
</bar:Something>
</foo:Root>";
$doc = new DOMDocument('1.0');
$doc->loadXML($txt2);
/*DOMDocument::canonize([boolean withComment, boolean exclusive, string
inclusive_ns_prefixe, DOMNOde node]) */
var_dump($doc->canonize(true,true,"foo",$doc->documentElement->childNodes->item(1)));
?>
--EXPECT--
string(325) "<bar:Something xmlns:bar="http://example.org/bar"
xmlns:foo="http://example.org/foo">
<foo:Something>
<bar:Something>
<foo:Something>
<!-- Here some comment -->
<baz:Something></baz:Something>
</foo:Something>
</bar:Something>
</foo:Something>
</bar:Something>"
Index: ext/dom/dom_fe.h
===================================================================
RCS file: /repository/php-src/ext/dom/dom_fe.h,v
retrieving revision 1.13
diff -u -r1.13 dom_fe.h
--- ext/dom/dom_fe.h 3 Oct 2004 09:55:29 -0000 1.13
+++ ext/dom/dom_fe.h 16 Feb 2005 18:31:17 -0000
@@ -126,6 +126,7 @@
/* convienience methods */
PHP_METHOD(domdocument, load);
PHP_FUNCTION(dom_document_save);
+PHP_FUNCTION(dom_document_canonize);
PHP_METHOD(domdocument, loadXML);
PHP_FUNCTION(dom_document_savexml);
PHP_FUNCTION(dom_document_validate);
Index: ext/dom/document.c
===================================================================
RCS file: /repository/php-src/ext/dom/document.c,v
retrieving revision 1.64
diff -u -r1.64 document.c
--- ext/dom/document.c 8 Jan 2005 13:24:39 -0000 1.64
+++ ext/dom/document.c 17 Feb 2005 00:36:19 -0000
@@ -30,6 +30,7 @@
#ifdef LIBXML_SCHEMAS_ENABLED
#include <libxml/relaxng.h>
#include <libxml/xmlschemas.h>
+#include <libxml/c14n.h>
#endif
typedef struct _idsIterator idsIterator;
@@ -68,6 +69,7 @@
PHP_FALIAS(renameNode, dom_document_rename_node, NULL)
PHP_ME(domdocument, load, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC)
PHP_FALIAS(save, dom_document_save, NULL)
+ PHP_FALIAS(canonize, dom_document_canonize, NULL)
PHP_ME(domdocument, loadXML, NULL,
ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC)
PHP_FALIAS(saveXML, dom_document_savexml, NULL)
PHP_ME(domdocument, __construct, NULL, ZEND_ACC_PUBLIC)
@@ -1653,6 +1655,81 @@
}
/* }}} end dom_document_save */
+int php_dom_document_canonize_visible(xmlNodePtr super, xmlNodePtr node,
xmlNodePtr parent)
+{
+ if(super == NULL) {
+ return(1);
+ }
+ if(super == parent) return (1);
+ while(node != NULL && node != super && node->parent != NULL){
+ node = node->parent;
+ }
+ if(node == super){
+ return (1);
+ }else
+ return (0);
+}
+
+/* {{{ proto string dom_document_canonize([boolean bcomment, boolean
bexclusive, string inclusive_ns_prefix, node n]);
+*/
+PHP_FUNCTION(dom_document_canonize)
+{
+ zval *id, *node_set = NULL, *nodep = NULL;
+ zend_bool bcomments = TRUE, bexclusive = FALSE;
+ char *str_inclusive_ns = NULL;
+ dom_object *intern = NULL, *nodeobj = NULL;
+ xmlDoc *docp;
+ xmlNode *node;
+ xmlChar *mem = NULL, *ns_prefix;
+ int str_inclusive_ns_len, ret;
+ /* Buffer */
+ xmlOutputBufferPtr buff = NULL;
+
+ if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(),
"O|bbsO", &id, dom_document_class_entry, &bcomments,
+ &bexclusive, &str_inclusive_ns, &str_inclusive_ns_len,
&nodep, dom_node_class_entry) == FAILURE) {
+ return;
+ }
+
+ DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
+
+ if(nodep != NULL){
+ DOM_GET_OBJ(node, nodep, xmlNodePtr, nodeobj);
+ }
+
+ buff = xmlAllocOutputBuffer(NULL);
+ if (!buff) {
+ php_error(E_WARNING, "Could not fetch buffer");
+ RETURN_FALSE;
+ }
+
+ ns_prefix = xmlStrdup((const xmlChar *) str_inclusive_ns);
+
+ ret = xmlC14NExecute (docp, //xmlDocPtr
doc,
+ (xmlC14NIsVisibleCallback)
php_dom_document_canonize_visible, //xmlC14NIsVisibleCallback
is_visible_callback,
+ node,
//void * user_data,
+ (int) bexclusive,
// int exclusive,
+ &ns_prefix,
// xmlChar ** inclusive_ns_prefixes,
+ (int) bcomments,
//int with_comments,
+ buff);
//xmlOutputBufferPtr buf)
+
+ xmlOutputBufferFlush(buff);
+ mem = xmlStrndup(buff->buffer->content, buff->buffer->use);
+ (void)xmlOutputBufferClose(buff);
+
+ if (ret < 0) {
+ php_error(E_WARNING, "Error canonizing");
+ RETURN_FALSE;
+ }
+
+ if (!mem) {
+ php_error(E_WARNING, "Error dumping buffer");
+ RETURN_FALSE;
+ }
+ RETVAL_STRING(mem, 1);
+ xmlFree(mem);
+}
+/* }}} end dom_document_canonize */
+
/* {{{ proto string dom_document_savexml([node n]);
URL: http://www.w3.org/TR/DOM-Level-3-LS/load-save.html#LS-DocumentLS-saveXML
Since: DOM Level 3
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php