예제 #1
0
		public function build() {
			if(strlen(trim($_GET['validate'])) > 0) { $this->_view = $_GET['validate']; }
			
			try {
				$validator = new SchemaValidator();
				if($validator->validate($this->_output,(DOCROOT . $this->_view))) {
					$result = json_encode(array('result'=>'success', 'errors' => array()));
				} else {
					$result = json_encode(array('result'=>'failed', 'errors' => $validator->getErrors()));
				}
			} catch(Exception $e) {
				return "b";
			}
			
			return $result;
		}
예제 #2
0
 public function __construct($path_to_xml_file, $validate_xml = false)
 {
     $this->path_to_xml_file = $path_to_xml_file;
     if (!$this->path_to_xml_file) {
         $this->valid_xml = false;
     } else {
         $valid = $validate_xml ? SchemaValidator::validate($this->path_to_xml_file) : true;
         if ($valid === true) {
             $this->valid_xml = true;
         } else {
             $this->valid_xml = false;
         }
     }
 }
예제 #3
0
 /**
  * Create a schema for a given string
  *
  * @param string $stringValue
  * @return array
  */
 protected function generateStringSchema($stringValue)
 {
     $schema = ['type' => 'string'];
     $schemaValidator = new SchemaValidator();
     $detectedFormat = null;
     $detectableFormats = ['uri', 'email', 'ip-address', 'class-name', 'interface-name'];
     foreach ($detectableFormats as $testFormat) {
         $testSchema = ['type' => 'string', 'format' => $testFormat];
         $result = $schemaValidator->validate($stringValue, $testSchema);
         if ($result->hasErrors() === false) {
             $detectedFormat = $testFormat;
         }
     }
     if ($detectedFormat !== null) {
         $schema['format'] = $detectedFormat;
     }
     return $schema;
 }
 public static function eol_schema_validate($uri)
 {
     if (!$uri) {
         return false;
     }
     $valid = SchemaValidator::validate($uri);
     if ($valid !== true) {
         return array();
     }
     $errors = array();
     $warnings = array();
     $reader = new \XMLReader();
     $reader->open($uri);
     $i = 0;
     while (@$reader->read()) {
         if ($reader->nodeType == \XMLReader::ELEMENT && $reader->name == "taxon") {
             $taxon_xml = $reader->readOuterXML();
             $t = simplexml_load_string($taxon_xml, null, LIBXML_NOCDATA);
             $t_dc = $t->children("http://purl.org/dc/elements/1.1/");
             $t_dwc = $t->children("http://rs.tdwg.org/dwc/dwcore/");
             $identifier = Functions::import_decode($t_dc->identifier);
             $source_url = Functions::import_decode($t_dc->source);
             $scientific_name = Functions::import_decode($t_dwc->ScientificName);
             if (!$identifier) {
                 $warnings[$scientific_name]["taxon without dc:identifier"] = true;
             }
             if (!$source_url) {
                 $warnings[$scientific_name]["taxon without dc:source"] = true;
             }
             foreach ($t->dataObject as $d) {
                 $d_dc = $d->children("http://purl.org/dc/elements/1.1/");
                 $identifier = Functions::import_decode($d_dc->identifier);
                 /* Checking requirements*/
                 if (!$identifier) {
                     $warnings[$scientific_name]["data object without dc:identifier"] = true;
                 }
             }
             $xml->taxon[$i] = null;
             $i++;
         }
     }
     return array($errors, $warnings);
 }
예제 #5
0
 * 
 * The rule evaluating whether a name was specified contains the <blocking>
 * property, which prevents any further rules from being evaluated upon
 * failure. This can be useful for security (eg. to ensure certain
 * high-level rules are succesfully evaluated before processing sub-rules
 * which may attempt to select, modify or remove from a database).
 *
 * Of note: rules are processed in sequence, regardless of whether their
 * processing is depenedent on a previous rule having been successfully
 * passed, unless the <blocking> or <funnel> properties are used
 * intelligently.
 *
 * @author Oliver Nassar <*****@*****.**>
 */
// dummy data
$_POST = array('name' => '', 'email' => '', 'website' => 'http://www.olivernassar.com/', 'comment' => 'Hello World!', 'updates' => 'true');
// instantiation (could be <Schema> or <SmartSchema> instance)
require_once '../Schema.class.php';
require_once '../SchemaValidator.class.php';
$schema = new Schema('comment.json');
$validator = new SchemaValidator($schema, $_POST);
// validation
if ($validator->valid()) {
    echo 'Valid';
} else {
    echo 'Invalid<br />Offending rule(s):' . '<pre>';
    $offending = $validator->getFailedRules();
    print_r($offending);
    echo '</pre>';
}
exit(0);
 public static function validateAllowedValuesCheck($attributes, $schema, $data)
 {
     foreach ($attributes as $index => $attribute) {
         $allowed_values = isset($schema[$attribute]["ALLOWED VALUES"]) ? $schema[$attribute]["ALLOWED VALUES"] : FALSE;
         if ($allowed_values === FALSE) {
             continue;
         }
         $is_matched = FALSE;
         foreach ($allowed_values as $index => $value) {
             if ($data[$attribute] === $value) {
                 $is_matched = TRUE;
                 break;
             }
         }
         if ($is_matched === FALSE) {
             $allowed_values_string = implode(",", $allowed_values);
             $error = "Allowed values for {$attribute} are {$allowed_values_string}. " . "Cannot hold {$data[$attribute]} as content for {$attribute}";
             return SchemaValidator::errorJSON($error);
         }
     }
     return TRUE;
 }
예제 #7
0
 public static function eol_schema_validate($uri)
 {
     if (!$uri) {
         return false;
     }
     $valid = SchemaValidator::validate($uri);
     if ($valid !== true) {
         return array();
     }
     $errors = array();
     $warnings = array();
     $reader = new \XMLReader();
     $reader->open($uri);
     $i = 0;
     while (@$reader->read()) {
         if ($reader->nodeType == \XMLReader::ELEMENT && $reader->name == "taxon") {
             $taxon_xml = $reader->readOuterXML();
             $t = simplexml_load_string($taxon_xml, null, LIBXML_NOCDATA);
             $t_dc = $t->children("http://purl.org/dc/elements/1.1/");
             $t_dwc = $t->children("http://rs.tdwg.org/dwc/dwcore/");
             $identifier = Functions::import_decode($t_dc->identifier);
             $source_url = Functions::import_decode($t_dc->source);
             $scientific_name = Functions::import_decode($t_dwc->ScientificName);
             if (!$identifier) {
                 $warnings[$scientific_name]["taxon without dc:identifier"] = true;
             }
             if (!$source_url) {
                 $warnings[$scientific_name]["taxon without dc:source"] = true;
             }
             foreach ($t->dataObject as $d) {
                 $d_dc = $d->children("http://purl.org/dc/elements/1.1/");
                 $identifier = Functions::import_decode($d_dc->identifier);
                 $data_type = Functions::import_decode($d->dataType);
                 $license = Functions::import_decode($d->license);
                 $source_url = Functions::import_decode($d_dc->source);
                 $description = Functions::import_decode($d_dc->description, 0, 0);
                 $object_url = Functions::import_decode($d->mediaURL);
                 $subjects = array();
                 foreach ($d->subject as $s) {
                     $subjects[] = trim((string) $s);
                 }
                 /* Checking requirements*/
                 if (!$identifier) {
                     $warnings[$scientific_name]["data object without dc:identifier"] = true;
                 }
                 if (!$license) {
                     $warnings[$scientific_name]["data object without license"] = true;
                 }
                 //if text: must have description
                 if ($data_type == "http://purl.org/dc/dcmitype/Text" && !$description) {
                     $errors[$scientific_name]["text without dc:description"] = true;
                 }
                 //if text: must have subject
                 if ($data_type == "http://purl.org/dc/dcmitype/Text" && !$subjects) {
                     $errors[$scientific_name]["text without subject"] = true;
                 }
                 //if image, movie or sound: must have object_url
                 if ($data_type != "http://purl.org/dc/dcmitype/Text" && !$object_url) {
                     $errors[$scientific_name]["media without mediaURL"] = true;
                 }
             }
             //unset($xml->taxon[$i]);
             $xml->taxon[$i] = null;
             $i++;
             //if($i%100==0 && DEBUG) debug("Parsed taxon $i");
             //if(defined("DEBUG_PARSE_TAXON_LIMIT") && $i >= DEBUG_PARSE_TAXON_LIMIT) break;
         }
     }
     return array($errors, $warnings);
 }