<?php
error_reporting(E_ALL|E_STRICT);
function libxml_create_error(LibXMLError $error)
{
$return = '<li><strong style="color:';
switch ($error->level)
{
case LIBXML_ERR_WARNING:
$return .= 'yellow;">Warning';
break;
case LIBXML_ERR_ERROR:
$return .= 'orange;">Error';
break;
case LIBXML_ERR_FATAL:
$return .= 'red;">Fatal Error';
break;
}
$return .= '</strong> on line <strong>'.$error->line.'</strong>:'.PHP_EOL;
return $return.trim($error->message).' <i>(#'.$error->code.')</i></li>'.PHP_EOL;
}
function libxml_display_errors()
{
echo '<div class="box"><h2>XML Errors:</h2>';
echo '<ol>';
foreach (libxml_get_errors() as $error)
print libxml_create_error($error);
echo '</ol></div>';
libxml_clear_errors();
}
libxml_use_internal_errors(true); // Enable user error handling
abstract class Upload
{
protected $file = array();
protected $infos = array();
public function __construct(Array $file)
{
if(isset($file))
{
if ($file["error"] > 0)
$this->info('<span style="color:red;">Error: '.$file["error"].'</span>');
elseif($file["size"] > 1024 * 1024) // 1 MB
$this->info('<span style="color:red;">Your file exceeds maximum filesize of 1 MB.</span>');
else
$this->file = $file;
}
}
abstract public function save();
protected function info($str)
{
$this->infos[] = $str;
}
protected function getInfos()
{
return $this->infos;
}
public function printInfos()
{
echo '<div class="box"><h2>Information:</h2>';
echo '<ul>';
foreach($this->infos as $info)
echo '<li>'.$info.'</li>';
echo '</ul></div>';
}
protected function backup($source)
{
if(is_file($source))
{
$backupdir = preg_replace('#(\/)*$#', '', dirname($_SERVER['SCRIPT_FILENAME'])).'/backup/';
if(!is_dir($backupdir))
{
if(!mkdir($backupdir))
return false;
chmod($backupdir, 0777);
}
$backupfile = $backupdir.$this->file['name'].'-'.date('Ymd-Gis').'.bak';
if(copy($source, $backupfile))
{
chmod($backupfile, 0777);
$this->info('<span style="color:green;">Old "'. $source . '" was backuped</span>');
return true;
}
else
$this->info('<span style="color:red;">Failed to backup ' . $source . '</span>');
}
else
$this->info('<span style="color:red;">Source ' . $source . ' does not exist.</span>');
return false;
}
}
abstract class XMLUpload extends Upload
{
protected $schemalocation;
protected function validate()
{
if($this->file["type"] != 'text/xml')
{
echo '<span style="color:red;">You did not submit an XML file.</span>';
return false;
}
else
{
$xml = $this->file["tmp_name"];
$dom = new DomDocument();
$dom->load($xml);
// Autoload schema
//$schema = $dom->documentElement->getAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'noNamespaceSchemaLocation');
return @$dom->schemaValidate($this->schemalocation);
}
}
}
class FilesystemXMLUpload extends XMLUpload
{
protected $schemalocation = '../xmlfs1.0/schema.xsd';
public function save()
{
$valid = $this->validate();
$this->info($this->file["name"] . ' <strong style="color:' . ($valid ? 'green;">is' : 'red;">is not') . '</strong> valid.');
if($valid)
{
$uri = preg_replace('#(\\'.DIRECTORY_SEPARATOR.')*$#', '', $_SERVER['DOCUMENT_ROOT']).DIRECTORY_SEPARATOR.$this->file["name"];
if($this->backup($uri) && move_uploaded_file($this->file["tmp_name"], $uri)) {
$this->info('New file was uploaded.');
return true;
}
else
$this->info('An <span style="color:red;">error</span> occured while moving file.');
}
else
$this->info('Nothing was saved. Please improve your file first.');
if(!$valid)
libxml_display_errors();
return false;
}
}
class TranslationXMLUpload extends XMLUpload
{
protected $schemalocation = '../translation1.0/schema.xsd';
public function save()
{
$valid = $this->validate();
$this->info($this->file["name"] . ' <strong style="color:' . ($valid ? 'green;">is' : 'red;">is not') . '</strong> valid.');
if($valid)
{
$uri = preg_replace('#(\\'.DIRECTORY_SEPARATOR.')*$#', '', $_SERVER['DOCUMENT_ROOT']).DIRECTORY_SEPARATOR.'mocovi'.DIRECTORY_SEPARATOR.'translations'.DIRECTORY_SEPARATOR.$this->file["name"];
die($uri);
if($this->backup($uri) && move_uploaded_file($this->file["tmp_name"], $uri))
{
$this->info('New file was uploaded.');
return true;
}
else
$this->info('An <span style="color:red;">error</span> occured while moving file.');
}
else
$this->info('Nothing was saved. Please improve your file first.');
if(!$valid)
libxml_display_errors();
return false;
}
}