Files
webgui/plugins/dynamix/include/FileUpload.php
2020-05-01 18:57:21 +02:00

101 lines
3.7 KiB
PHP

<?PHP
/* Copyright 2005-2020, Lime Technology
* Copyright 2012-2020, Bergware International.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
?>
<?
$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
$file = rawurldecode($_POST['filename']);
$temp = "/var/tmp";
$tmp = '/tmp/plugins';
$plugins = '/var/log/plugins';
$boot = "/boot/config/plugins";
$safepaths = ["$boot/dynamix"];
$safeexts = ['.png'];
switch ($_POST['cmd'] ?? 'load') {
case 'load':
if (isset($_POST['filedata'])) {
exec("rm -f $temp/*.png");
$result = file_put_contents("$temp/".basename($file),base64_decode(str_replace(['data:image/png;base64,',' '],['','+'],$_POST['filedata'])));
}
break;
case 'save':
$path = $_POST['path'];
foreach ($safepaths as $safepath) {
if (strpos(dirname("$path/{$_POST['output']}"),$safepath)===0 && in_array(substr(basename($_POST['output']),-4),$safeexts)) {
exec("mkdir -p ".escapeshellarg(realpath($path)));
$result = @rename("$temp/".basename($file), "$path/{$_POST['output']}");
break;
}
}
break;
case 'delete':
$path = $_POST['path'];
foreach ($safepaths as $safepath) {
if (strpos(realpath("$path/$file"), $safepath) === 0 && in_array(substr(realpath("$path/$file"), -4), $safeexts)) {
exec("rm -f ".escapeshellarg(realpath("$path/$file")));
$result = true;
break;
}
}
break;
case 'add':
$file = basename($file);
$path = "$docroot/languages/$file";
$save = "/tmp/dynamix.$file.lang.zip";
exec("mkdir -p $path");
if ($result = file_put_contents($save,base64_decode(preg_replace('/^data:.*;base64,/','',$_POST['filedata'])))) {
@unlink("$docroot/webGui/javascript/translate.$file.js");
foreach (glob("$path/*.dot",GLOB_NOSORT) as $dot_file) unlink($dot_file);
exec("unzip -qqjLo -d $path $save", $dummy, $err);
if ($err > 1) {
unlink("$boot/dynamix/$file.lang.zip");
exec("rm -rf $path");
$result = false;
break;
}
[$home,$name] = explode(' (',urldecode($_POST['name']));
$name = rtrim($name,')'); $i = 0;
$place = "$plugins/dynamix.$file.xml";
$child = ['LanguageURL','Language','LanguageLocal','Author','Name','TemplateURL','Version','Icon','Description','Changes'];
$value = [$save,$name,$home,$_SERVER['HTTP_HOST'],"$name translation",$place,date('Y.m.d',time()),'','',''];
// create a corresponding XML file
$xml = new SimpleXMLElement('<Language/>');
foreach ($child as $key) $xml->addChild($key,$value[$i++]);
// saved as file (not link)
$xml->asXML($place);
// return list of installed language packs
$installed = [];
foreach (glob("$docroot/languages/*",GLOB_ONLYDIR) as $dir) $installed[] = basename($dir);
exit(implode(',',$installed));
}
break;
case 'rm':
$file = basename($file);
$path = "$docroot/languages/$file";
if ($result = is_dir($path)) {
exec("rm -rf $path");
@unlink("$docroot/webGui/javascript/translate.$file.js");
@unlink("$boot/dynamix.$file.xml");
@unlink("$plugins/dynamix.$file.xml");
@unlink("$tmp/dynamix.$file.xml");
@unlink("$boot/dynamix/$file.lang.zip");
@unlink("/tmp/dynamix.$file.lang.zip");
// return list of installed language packs
$installed = [];
foreach (glob("$docroot/languages/*",GLOB_ONLYDIR) as $dir) $installed[] = basename($dir);
exit(implode(',',$installed));
}
break;
}
exit($result ? 'OK 200' : 'Internal Error 500');
?>