#define SCHEMA_BLOB 1
#define SCHEMA_OBJECT 2
-#ifdef xmlSchemaParserCtxtPtr
+#ifdef LIBXML_SCHEMAS_ENABLED
/* {{{ simplexml_ce_schema_validate_file()
*/
case SCHEMA_FILE:
convert_to_string_ex(&source);
parser = xmlSchemaNewParserCtxt(Z_STRVAL_P(source));
+ if (parser == NULL) {
+ php_error_docref1(NULL TSRMLS_CC, Z_STRVAL_P(source), E_WARNING, "Unable to load XML Schema file");
+ RETURN_FALSE;
+ }
sptr = xmlSchemaParse(parser);
- xmlSchemaFreeParserCtxt(parser);
break;
case SCHEMA_BLOB:
convert_to_string_ex(&source);
parser = xmlSchemaNewMemParserCtxt(Z_STRVAL_P(source), Z_STRLEN_P(source));
sptr = xmlSchemaParse(parser);
- xmlSchemaFreeParserCtxt(parser);
break;
}
+ if (sptr == NULL) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Malformed XML Schema");
+ xmlSchemaFreeParserCtxt(parser);
+ RETURN_FALSE;
+ }
+
vptr = xmlSchemaNewValidCtxt(sptr);
- is_valid = xmlSchemaValidateDoc(vptr, (xmlDocPtr) sxe->document->ptr);
- xmlSchemaFree(sptr);
- xmlSchemaFreeValidCtxt(vptr);
- xmlSchemaFreeParserCtxt(parser);
- if (is_valid) {
- RETURN_TRUE;
- } else {
+ if (vptr == NULL) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create XML Schema validation context");
+ xmlSchemaFreeParserCtxt(parser);
RETURN_FALSE;
}
+
+ switch (xmlSchemaValidateDoc(vptr, (xmlDocPtr) sxe->document->ptr)) {
+ case 0: /* validated */
+ RETVAL_TRUE;
+ break;
+ case -1: /* internal error */
+ RETVAL_FALSE;
+ break;
+ default: /* error */
+ RETVAL_TRUE;
+ break;
+ }
+
+ xmlSchemaFree(sptr);
+ xmlSchemaFreeValidCtxt(vptr);
+ xmlSchemaFreeParserCtxt(parser);
}
/* }}} */
{
if (!strcmp(method, "xsearch")) {
simplexml_ce_xpath_search(INTERNAL_FUNCTION_PARAM_PASSTHRU);
-#ifdef xmlSchemaParserCtxtPtr
+#ifdef LIBXML_SCHEMAS_ENABLED
} else if (!strcmp(method, "validate_schema_file")) {
simplexml_ce_schema_validate(INTERNAL_FUNCTION_PARAM_PASSTHRU, SCHEMA_FILE);
} else if (!strcmp(method, "validate_schema_buffer")) {
--- /dev/null
+--TEST--
+Bug #25756 (validate_schema_file() broken)
+--FILE--
+<?php
+$dir = dirname(__FILE__);
+$valid_schema_file = "$dir/bug25756.xsd";
+$invalid_schema_file = "$dir/bug25756_1.xml";
+$xml_file_1 = "$dir/bug25756_1.xml";
+$xml_file_2 = "$dir/bug25756_2.xml";
+
+$s = simplexml_load_file($xml_file_1);
+var_dump($s);
+var_dump($s->validate_schema_file($valid_schema_file));
+var_dump($s->validate_schema_file($invalid_schema_file));
+$s = simplexml_load_file($xml_file_2);
+var_dump($s);
+var_dump($s->validate_schema_file($valid_schema_file));
+?>
+--EXPECTF--
+object(simplexml_element)#1 (1) {
+ ["items"]=>
+ object(simplexml_element)#2 (1) {
+ ["item"]=>
+ array(2) {
+ [0]=>
+ object(simplexml_element)#3 (2) {
+ ["product-name"]=>
+ string(3) "abc"
+ ["quantity"]=>
+ string(3) "123"
+ }
+ [1]=>
+ object(simplexml_element)#4 (2) {
+ ["product-name"]=>
+ string(3) "def"
+ ["quantity"]=>
+ string(3) "456"
+ }
+ }
+ }
+}
+bool(true)
+
+Warning: Unknown: Malformed XML Schema in %s on line %d
+bool(false)
+object(simplexml_element)#5 (1) {
+ ["items"]=>
+ object(simplexml_element)#1 (1) {
+ ["item"]=>
+ array(2) {
+ [0]=>
+ object(simplexml_element)#6 (2) {
+ ["product-name"]=>
+ string(3) "abc"
+ ["quantity"]=>
+ string(3) "abc"
+ }
+ [1]=>
+ object(simplexml_element)#7 (2) {
+ ["product-name"]=>
+ string(3) "abc"
+ ["quantity"]=>
+ string(3) "123"
+ }
+ }
+ }
+}
+bool(false)
--- /dev/null
+<?xml version="1.0" encoding="UTF-8" ?>
+<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
+ <xsd:element name="foo" type="foo-type" />
+ <xsd:complexType name="item-type">
+ <xsd:all>
+ <xsd:element name="product-name" type="xsd:string"
+ minOccurs="1" maxOccurs="1"/>
+ <xsd:element name="quantity" type="xsd:decimal"
+ minOccurs="1" maxOccurs="1"/>
+ </xsd:all>
+ </xsd:complexType>
+ <xsd:complexType name="foo-type">
+ <xsd:sequence>
+ <xsd:element name="items" minoccurs="1" maxOccurs="1">
+ <xsd:complexType>
+ <xsd:sequence>
+ <xsd:element name="item" type="item-type"
+ minOccurs="0" maxOccurs="unbounded" />
+ </xsd:sequence>
+ </xsd:complexType>
+ </xsd:element>
+ </xsd:sequence>
+ </xsd:complexType>
+</xsd:schema>
--- /dev/null
+<?xml version="1.0" encoding="UTF-8" ?>
+<foo>
+ <items>
+ <item>
+ <product-name>abc</product-name>
+ <quantity>123</quantity>
+ </item>
+ <item>
+ <product-name>def</product-name>
+ <quantity>456</quantity>
+ </item>
+ </items>
+</foo>
--- /dev/null
+<?xml version="1.0" encoding="UTF-8" ?>
+<foo>
+ <items>
+ <item>
+ <product-name>abc</product-name>
+ <quantity>abc</quantity>
+ </item>
+ <item>
+ <product-name>abc</product-name>
+ <quantity>123</quantity>
+ </item>
+ </items>
+</foo>