|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00004 // // 00005 // NOTICE OF COPYRIGHT // 00006 // // 00007 // Moodle - Modular Object-Oriented Dynamic Learning Environment // 00008 // http://moodle.com // 00009 // // 00010 // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com // 00011 // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com // 00012 // // 00013 // This program is free software; you can redistribute it and/or modify // 00014 // it under the terms of the GNU General Public License as published by // 00015 // the Free Software Foundation; either version 2 of the License, or // 00016 // (at your option) any later version. // 00017 // // 00018 // This program is distributed in the hope that it will be useful, // 00019 // but WITHOUT ANY WARRANTY; without even the implied warranty of // 00020 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 00021 // GNU General Public License for more details: // 00022 // // 00023 // http://www.gnu.org/copyleft/gpl.html // 00024 // // 00026 00028 00029 class xmldb_file extends xmldb_object { 00030 00031 var $path; 00032 var $schema; 00033 var $dtd; 00034 var $xmldb_structure; 00035 00039 function __construct($path) { 00040 parent::__construct($path); 00041 $this->path = $path; 00042 $this->xmldb_structure = NULL; 00043 } 00044 00048 function fileExists() { 00049 if (file_exists($this->path) && is_readable($this->path)) { 00050 return true; 00051 } 00052 return false; 00053 } 00054 00058 function fileWriteable() { 00059 if (is_writeable(dirname($this->path))) { 00060 return true; 00061 } 00062 return false; 00063 } 00064 00065 function &getStructure() { 00066 return $this->xmldb_structure; 00067 } 00068 00074 function validateXMLStructure() { 00075 00077 $parser = new DOMDocument(); 00078 $contents = file_get_contents($this->path); 00079 if (strpos($contents, '<STATEMENTS>')) { 00080 //delete the removed STATEMENTS section, it would not validate 00081 $contents = preg_replace('|<STATEMENTS>.*</STATEMENTS>|s', '', $contents); 00082 } 00083 00084 // Let's capture errors 00085 $olderrormode = libxml_use_internal_errors(true); 00086 00087 // Clear XML error flag so that we don't incorrectly report failure 00088 // when a previous xml parse failed 00089 libxml_clear_errors(); 00090 00091 $parser->loadXML($contents); 00093 if (!empty($this->schema) && file_exists($this->schema)) { 00094 $parser->schemaValidate($this->schema); 00095 } 00097 $errors = libxml_get_errors(); 00098 00099 // Stop capturing errors 00100 libxml_use_internal_errors($olderrormode); 00101 00103 if (!empty($errors)) { 00105 $structure = new xmldb_structure($this->path); 00107 $structure->errormsg = 'XML Error: '; 00108 foreach ($errors as $error) { 00109 $structure->errormsg .= sprintf("%s at line %d. ", 00110 trim($error->message, "\n\r\t ."), 00111 $error->line); 00112 } 00114 $this->xmldb_structure = $structure; 00116 return false; 00117 } 00118 00119 return true; 00120 } 00121 00125 function loadXMLStructure() { 00126 if ($this->fileExists()) { 00128 if (!$this->validateXMLStructure()) { 00129 return false; 00130 } 00131 $contents = file_get_contents($this->path); 00132 if (strpos($contents, '<STATEMENTS>')) { 00133 //delete the removed STATEMENTS section, it would not validate 00134 $contents = preg_replace('|<STATEMENTS>.*</STATEMENTS>|s', '', $contents); 00135 debugging('STATEMENTS section is not supported any more, please use db/install.php or db/log.php'); 00136 } 00139 $xmlarr = xmlize($contents); 00141 $this->xmldb_structure = $this->arr2xmldb_structure($xmlarr); 00143 if ($this->xmldb_structure->isLoaded()) { 00144 $this->loaded = true; 00145 return true; 00146 } else { 00147 return false; 00148 } 00149 } 00150 return true; 00151 } 00152 00156 function arr2xmldb_structure ($xmlarr) { 00157 $structure = new xmldb_structure($this->path); 00158 $structure->arr2xmldb_structure($xmlarr); 00159 return $structure; 00160 } 00161 00165 function setDTD($path) { 00166 $this->dtd = $path; 00167 } 00168 00172 function setSchema($path) { 00173 $this->schema = $path; 00174 } 00175 00179 function saveXMLFile() { 00180 00181 $structure =& $this->getStructure(); 00182 00183 $result = file_put_contents($this->path, $structure->xmlOutput()); 00184 00185 return $result; 00186 } 00187 }