con = &$con; $this->xml = $xml; $schema = dbSchema::init($this->con); $this->tables = $schema->getTables(); unset($schema); } public function replace($str, $rep) { $this->xml = str_replace($str, $rep, $this->xml); } public function execute($version = 0) { $this->test_version = $version; $x = @simplexml_load_string($this->xml); if (!$x) { throw new Exception('Unable to load XML file.'); } $this->parseNode($x); } protected function parseNode($node) { foreach ($node->children() as $n) { switch (dom_import_simplexml($n)->nodeName) { case 'test': $this->performTest($n); break; case 'action': $this->performAction($n); break; } } } protected function performTest($node) { /* Test like: ... */ if (isset($node['type']) && (string) $node['type'] == 'table') { $test['result'] = in_array($node['name'], $this->tables); $test['label'] = 'Table %s does not exists'; $test['string'] = (string) $node['name']; $xtest = $node; } /* Test syntax: ... */ elseif (isset($node['type']) && (string) $node['type'] == 'column') { $c = explode('.', $node['name']); if (count($c) != 2) { return false; } list($table, $col) = $c; $rs = $this->con->getColumns($table); $test['result'] = isset($rs[$col]); $test['label'] = 'Column %s does not exists'; $test['string'] = (string) $node['name']; $xtest = $node; } /* Test syntax: ... */ elseif (isset($node['type']) && (string) $node['type'] == 'version') { $comp = isset($node['comp']) ? $node['comp'] : '>'; $test['result'] = version_compare($node['name'], $this->test_version, $comp); $test['label'] = 'Version %s is too low'; $test['string'] = (string) $node['name']; $xtest = $node; } # End tests if (isset($xtest)) { if ($xtest['eq'] == 'neq') { $test['result'] = !$test['result']; } if (isset($xtest['alert'])) { $test['alert'] = (boolean) (integer) $xtest['alert']; } else { $test['alert'] = false; } if (isset($xtest['label'])) { $test['label'] = (string) $xtest['label']; } if (isset($xtest['string'])) { $test['string'] = (string) $xtest['string']; } unset($xtest); # Test false if (!$test['result']) { if ($test['alert']) { throw new Exception(sprintf($test['label'], $test['string'])); } } # Test true else { $this->parseNode($node); } } else { return false; } } protected function performAction($node) { $req = trim((string) $node); if ($req) { try { $this->con->execute($req); } catch (Exception $e) { if ($node['silent'] != 1) { throw $e; } } } } }