array ( 'method' => 'Unpacking method', 'download' => 'Download file', 'unzip_it' => 'Unzip it', 'delete_it' => 'Delete it', 'zip_file' => 'zip file', 'zip_files' => 'zip files', 'msg_found_files' => 'Found %s in this directory.', 'msg_files_not_found' => 'There is no zip file in this directory.', 'msg_not_zip_file' => 'This %s is not a zip file.', 'msg_error_while_unzip' => 'Error while unzipping file %s.', 'msg_unzip_success' => 'File %s has been unziped.', 'msg_cannot_delete' => 'This file %s cannot be deleted.', 'msg_error_while_delete' => 'Error while deleting file %s.', 'msg_delete_success' => 'File %s has been deleted.', 'msg_missing_token' => 'Missing token.', 'msg_invalid_token' => 'Invalid token.', 'msg_warning_files_overwrite' => 'All unzipped files will be overwritten if they already exist.', 'msg_warning_file_delete' => 'File will be deleted permanently.', 'msg_warning_script_delete' => 'This script file will be deleted permanently.', 'msg_remind_to_delete' => 'Remember to delete this script when you are done.', 'msg_are_you_sure' => 'Are you sure?', 'msg_confirm_your_action' => 'Confirm your action.', 'msg_action_proceed' => 'Yes, proceed it', 'msg_action_close' => 'No, close it', )]; // === Helpers === // /** * TranslateHelper * * Helps manage translations. * * @author Robert Wierzchowski * @version 1.2.0 */ class TranslateHelper { private static $language; private static $defaultlanguage = 'en'; private static $availableLanguages = ['en', 'pl', 'de', 'es', 'ru']; private static $translationsFileName = 'src/lang/translations.php'; private static $translations; private function __construct() {} private static function findTranslation($key) { return (! empty(self::$translations[self::$language][$key])) ? self::$translations[self::$language][$key] : str_replace('_', ' ', ucfirst($key)); } private static function setLanguageFromGet($name) { if (! empty($_GET[$name])) { self::$language = (in_array($_GET[$name], self::$availableLanguages)) ? $_GET[$name] : self::$defaultlanguage; } } private static function setLanguageFromUri() { $args = explode('/', $_SERVER['REQUEST_URI']); $cleanArgs = array_filter($args, function ($value) { return $value !== ''; }); $lastArg = array_pop($cleanArgs); if (in_array($lastArg, self::$availableLanguages)) { self::$language = $lastArg; } } private static function setLanguageFromBrowser() { self::$availableLanguages = array_flip(self::$availableLanguages); $languagesWeight = []; preg_match_all('~([\w-]+)(?:[^,\d]+([\d.]+))?~', strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']), $matches, PREG_SET_ORDER); foreach ($matches as $match) { list($a, $b) = explode('-', $match[1]) + ['', '']; $value = isset($match[2]) ? (float) $match[2] : 1.0; if (isset(self::$availableLanguages[$match[1]])) { $languagesWeight[$match[1]] = $value; continue; } if (isset(self::$availableLanguages[$a])) { $languagesWeight[$a] = $value - 0.1; } } if ($languagesWeight) { arsort($languagesWeight); self::$language = key($languagesWeight); } } private static function setLanguage() { self::setLanguageFromGet('lang'); if (empty(self::$language)) { self::setLanguageFromUri(); } if (empty(self::$language)) { self::setLanguageFromBrowser(); } if (empty(self::$language)) { self::$language = self::$defaultlanguage; } if (! empty(LANGUAGE)) { self::$language = LANGUAGE; } } private static function readTranslations($fileName) { if (! file_exists($fileName)) { return false; } $extension = pathinfo($fileName, PATHINFO_EXTENSION); if ($extension != 'php') { return false; } return (include $fileName); } private static function setTranslations() { self::$translations = (! empty(TRANSLATIONS)) ? TRANSLATIONS : self::readTranslations(self::$translationsFileName); } public static function getTranslation($key) { self::setLanguage(); self::setTranslations(); return self::findTranslation($key); } public static function getLanguage() { self::setLanguage(); return self::$language; } } /** * Get translation from translate helper function */ function _t($key, ...$args) { $translation = TranslateHelper::getTranslation($key); $result = vsprintf($translation, $args); return $result; } /** * Get language from translate helper function */ function _lang() { return TranslateHelper::getLanguage(); } /** * Alias of htmlspecialchars helper function */ function _h($text) { return htmlspecialchars($text, ENT_COMPAT); } // === Classes === // /** * UnZipper * * Unzip zip files. One file server side simple unzipper with UI. * * @author Robert Wierzchowski * @version 1.2.0 */ class UnZipper { private $title = 'UnZipper'; private $dir = './'; private $zips = []; private $alertMessage; private $alertStatus; private $token; private $output; private $methods = [ 'zipArchive' => 'ZipArchive', 'execUnzip' => 'exec unzip', 'systemUnzip' => 'system unzip', ]; public function __construct() { $this->checkIfUnZip(); $this->checkIfDeleteFile(); $this->setToken(); $this->findZips(); $this->countZips(); } private function checkIfUnZip() { if (! empty($_POST['zipfile']) && $this->verifyToken($_POST['token'])) { $this->unZip($_POST['zipfile'], $_POST['method']); } } private function checkIfDeleteFile() { if (! empty($_POST['delfile']) && $this->verifyToken($_POST['token'])) { $this->deleteFile($_POST['delfile']); } } private function findZips() { $fileNames = scandir($this->dir); foreach ($fileNames as $fileName) { if ($this->checkZipFile($fileName)) { $this->zips[] = $fileName; } } } private function countZips() { if ($this->alertMessage) { return false; } $count = count($this->zips); if ($count) { $zipFile = ($count == 1) ? _t('zip_file') : _t('zip_files'); $this->alertMessage = _t('msg_found_files', '' . $count . ' ' . $zipFile . ''); $this->alertStatus = 'info'; } else { $this->alertMessage = _t('msg_files_not_found'); $this->alertStatus = 'warning'; } } private function checkZipFile($fileName) { if (! file_exists($fileName)) { return false; } $extension = pathinfo($fileName, PATHINFO_EXTENSION); if ($extension != 'zip') { return false; } return true; } private function unZip($zip, $method = null) { if (! $this->checkZipFile($zip) || ! in_array($zip, scandir($this->dir))) { $this->alertMessage = _t('msg_not_zip_file', '' . $zip . ''); $this->alertStatus = 'danger'; return false; } switch ($method) { case 'execUnzip': $unzipResult = $this->execUnzip($zip); break; case 'systemUnzip': $unzipResult = $this->systemUnzip($zip); break; default: $unzipResult = $this->unZipArchive($zip); } if (! $unzipResult) { $this->alertMessage = _t('msg_error_while_unzip', '' . $zip . ''); $this->alertStatus = 'danger'; return false; } $this->alertMessage = _t('msg_unzip_success', '' . $zip . ''); $this->alertStatus = 'success'; return true; } private function unZipArchive($fileName) { $dirPath = pathinfo(realpath($fileName), PATHINFO_DIRNAME); $zip = new ZipArchive; if ($zip->open($fileName) !== true) { return false; } $zip->extractTo($dirPath); $zip->close(); return true; } private function execUnzip($fileName) { if (! exec('unzip -o ' . $fileName, $output)) { return false; } $this->output = implode('
', $output); return true; } private function systemUnzip($fileName) { ob_start(); if (! system("unzip -o {$fileName}")) { return false; } $this->output = nl2br(ob_get_contents()); ob_end_clean(); return true; } private function deleteFile($fileName) { if (! $this->checkZipFile($fileName) && $fileName != basename(__FILE__)) { $this->alertMessage = _t('msg_cannot_delete', '' . $fileName . ''); $this->alertStatus = 'danger'; return false; } if (! unlink($fileName)) { $this->alertMessage = _t('msg_error_while_delete', '' . $fileName . ''); $this->alertStatus = 'danger'; return false; } $this->alertMessage = _t('msg_delete_success', '' . $fileName . ''); $this->alertStatus = 'success'; return true; } private function verifyToken($inputToken) { if (empty($inputToken)) { $this->alertMessage = _t('msg_missing_token'); $this->alertStatus = 'danger'; return false; } if (! hash_equals($_SESSION['token'], $inputToken)) { $this->alertMessage = _t('msg_invalid_token'); $this->alertStatus = 'danger'; return false; } return true; } private function setToken() { $_SESSION['token'] = md5(uniqid(rand(), true)); // PHP 7 only: bin2hex(random_bytes(32)); $this->token = $_SESSION['token']; } public function getToken() { return $this->token; } public function getZips() { return $this->zips; } public function getMessage() { return $this->alertMessage; } public function getStatus() { return $this->alertStatus; } public function getOutput() { return $this->output; } public function getScriptPath() { return $_SERVER['REQUEST_URI'] ; } public function getTitle() { return $this->title; } public function getMethods() { return $this->methods; } } // === Instances === // $unZipper = new UnZipper(); // === Template === // ?> <?=$unZipper->getTitle()?>
getMessage()): ?>
getMessage()?>
getZips()): ?>
: getMethods() as $methodKey => $methodName): ?>
    getZips() as $key => $zip): ?>
getOutput()): ?>
getOutput()?>