diff options
author | Simon Rettberg | 2014-05-15 18:28:24 +0200 |
---|---|---|
committer | Simon Rettberg | 2014-05-15 18:28:24 +0200 |
commit | 63c0cf521f8097b0dadaf1228176dc38c7d897f6 (patch) | |
tree | 83f5da6dc130ac7db575b0eee41ed6c7a2f994fb /modules | |
parent | Fix handle leak in downloading, better error reporting on failed downloads, a... (diff) | |
download | slx-admin-63c0cf521f8097b0dadaf1228176dc38c7d897f6.tar.gz slx-admin-63c0cf521f8097b0dadaf1228176dc38c7d897f6.tar.xz slx-admin-63c0cf521f8097b0dadaf1228176dc38c7d897f6.zip |
Working on config.tgz composition through config modules
Diffstat (limited to 'modules')
-rw-r--r-- | modules/sysconfig.inc.php | 81 | ||||
-rw-r--r-- | modules/sysconfig/addmodule.inc.php | 196 | ||||
-rw-r--r-- | modules/syslog.inc.php | 35 |
3 files changed, 255 insertions, 57 deletions
diff --git a/modules/sysconfig.inc.php b/modules/sysconfig.inc.php index c883eb68..faa26787 100644 --- a/modules/sysconfig.inc.php +++ b/modules/sysconfig.inc.php @@ -1,37 +1,29 @@ <?php +/* +@include_once('Archive/Tar.php'); +if (!class_exists('Archive_Tar')) { + Message::addError('Broken php installation: pear extension Archive_Tar missing!'); + Util::redirect('?do=main'); +} + */ + User::load(); -if (isset($_POST['action']) && $_POST['action'] === 'upload') { - if (!Util::verifyToken()) { - Util::redirect('?do=sysconfig'); - } - if (!User::hasPermission('superadmin')) { - Message::addError('no-permission'); - Util::redirect('?do=sysconfig'); - } - if (!isset($_FILES['customtgz'])) { - Message::addError('missing-file'); - Util::redirect('?do=sysconfig'); - } - $dest = $_FILES['customtgz']['name']; - $dest = preg_replace('/[^a-z0-9\-_]/', '', $dest); - $dest = substr($dest, 0, 30); - if (substr($dest, -3) === 'tgz') $dest = substr($dest, 0, -3); - $dest .= '.tgz'; - # TODO: Validate its a (compressed) tar? - if (move_uploaded_file($_FILES['customtgz']['tmp_name'], CONFIG_TGZ_LIST_DIR . '/' . $dest)) { - Message::addSuccess('upload-complete', $dest); - } else { - Message::addError('upload-failed', $dest); - } - Util::redirect('?do=sysconfig'); +// Read request vars +$action = Request::any('action', 'list'); +$step = Request::any('step', 0); +$nextStep = $step + 1; + +// Action: "addmodule" (upload new module) +if ($action === 'addmodule') { + require_once 'modules/sysconfig/addmodule.inc.php'; + $handler = AddModule_Base::get($step); + $handler->preprocess(); } -if (isset($_REQUEST['action']) && $_REQUEST['action'] === 'activate') { - if (!Util::verifyToken()) { - Util::redirect('?do=sysconfig'); - } +// Action "activate" (set sysconfig as active) +if ($action === 'activate') { if (!User::hasPermission('superadmin')) { Message::addError('no-permission'); Util::redirect('?do=sysconfig'); @@ -55,15 +47,19 @@ if (isset($_REQUEST['action']) && $_REQUEST['action'] === 'activate') { Util::redirect('?do=sysconfig'); } +/** + * Render module; called by main script when this module page should render + * its content. + */ function render_module() { - if (!isset($_REQUEST['action'])) $_REQUEST['action'] = 'list'; - switch ($_REQUEST['action']) { - case 'remotelist': - list_remote_configs(); + global $action, $handler; + switch ($action) { + case 'addmodule': + $handler->render(); break; case 'list': - list_configs(); + rr_list_configs(); break; default: Message::addError('invalid-action', $_REQUEST['action']); @@ -71,25 +67,23 @@ function render_module() } } -function list_configs() +function rr_list_configs() { if (!User::hasPermission('superadmin')) { Message::addError('no-permission'); return; } - $current = '<none>'; - if (file_exists(CONFIG_HTTP_DIR . '/default/config.tgz')) $current = realpath(CONFIG_HTTP_DIR . '/default/config.tgz'); - $files = array(); - foreach (glob(CONFIG_TGZ_LIST_DIR . '/*.tgz') as $file) { - $files[] = array( - 'file' => basename($file), - 'current' => ($current === realpath($file)) + $res = Database::simpleQuery("SELECT title FROM configtgz_module ORDER BY title ASC"); + $modules = array(); + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + $modules[] = array( + 'module' => $row['title'] ); } - Render::addTemplate('page-tgz-list', array('files' => $files, 'token' => Session::get('token'))); + Render::addTemplate('page-sysconfig-main', array('modules' => $modules, 'token' => Session::get('token'))); } -function list_remote_configs() +function rr_list_remote_configs() { if (!User::hasPermission('superadmin')) { Message::addError('no-permission'); @@ -111,4 +105,3 @@ function list_remote_configs() } Render::addTemplate('page-remote-tgz-list', array('files' => $list)); } - diff --git a/modules/sysconfig/addmodule.inc.php b/modules/sysconfig/addmodule.inc.php new file mode 100644 index 00000000..aca2f762 --- /dev/null +++ b/modules/sysconfig/addmodule.inc.php @@ -0,0 +1,196 @@ +<?php + +/** + * Addmodule subpage base - makes sure + * we have the two required methods preprocess and render + */ +abstract class AddModule_Base +{ + + /** + * + * @param type $step + * @return \AddModule_Base + */ + public static function get($step) + { + switch ($step) { + case 0: // Upload form + return new AddModule_UploadForm(); + case 1: // Handle config module uploading + return new AddModule_ProcessUpload(); + case 2: // ? + return new AddModule_CompressModule(); + } + Message::addError('invalid-action', $step); + Util::redirect('?do=sysconfig'); + } + + protected function tmError() + { + Message::addError('taskmanager-error'); + Util::redirect('?do=sysconfig'); + } + + protected function taskError($status) + { + if (isset($status['data']['error'])) { + $error = $status['data']['error']; + } elseif (isset($status['statusCode'])) { + $error = $status['statusCode']; + } else { + $error = 'Unbekannter Taskmanager-Fehler'; // TODO: No text + } + Message::addError('task-error', $error); + Util::redirect('?do=sysconfig'); + } + + /** + * Called before any HTML rendering happens, so you can + * pepare stuff, validate input, and optionally redirect + * early if something is wrong, or you received post + * data etc. + */ + public function preprocess() + { + // void + } + + /** + * Do page rendering + */ + public function render() + { + // void + } + +} + +class AddModule_UploadForm extends AddModule_Base +{ + + public function render() + { + global $nextStep; + Session::set('mod_temp', false); + Render::addDialog('Eigenes Modul hinzufügen', false, 'sysconfig/custom-upload', array('step' => $nextStep)); + } + +} + +/** + * Some file has just been uploaded. Try to store it, then try to unpack/analyze it. + * If this succeeds, we proceed to the next step where we present the user our findings + * and ask what to do with this. + */ +class AddModule_ProcessUpload extends AddModule_Base +{ + + private $taskId = false; + + public function preprocess() + { + if (!isset($_FILES['modulefile'])) { + Message::addError('missing-file'); + return; + } + if ($_FILES['modulefile']['error'] != UPLOAD_ERR_OK) { + Message::addError('upload-failed', $_FILE['modulefile']['name']); + return; + } + $tempfile = $_FILES['modulefile']['tmp_name'] . '.tmp'; + if (!move_uploaded_file($_FILES['modulefile']['tmp_name'], $tempfile)) { + Message:addError('error-write', $tempfile); + return; + } + $this->taskId = 'tgzmod' . mt_rand() . '-' . microtime(true); + Taskmanager::submit('ListArchive', array( + 'id' => $this->taskId, + 'file' => $tempfile + ), true); + Session::set('mod_temp', $tempfile); + } + + public function render() + { + $status = Taskmanager::waitComplete($this->taskId); + Taskmanager::release($this->taskId); + $tempfile = Session::get('mod_temp'); + if (!isset($status['statusCode'])) { + unlink($tempfile); + $this->tmError(); + } + if ($status['statusCode'] != TASK_FINISHED) { + unlink($tempfile); + $this->taskError($status); + } + // Sort files for better display + $dirs = array(); + foreach ($status['data']['entries'] as $file) { + if ($file['isdir']) continue; + $dirs[dirname($file['name'])][] = $file; + } + ksort($dirs); + $list = array(); + foreach ($dirs as $dir => $files) { + $list[] = array( + 'name' => $dir, + 'isdir' => true + ); + sort($files); + foreach ($files as $file) { + $file['size'] = Util::readableFileSize($file['size']); + $list[] = $file; + } + } + global $nextStep; + Render::addDialog('Eigenes Modul hinzufügen', false, 'sysconfig/custom-fileselect', array( + 'step' => $nextStep, + 'files' => $list, + )); + Session::save(); + } + +} + +class AddModule_CompressModule extends AddModule_Base +{ + + private $taskId = false; + + public function preprocess() + { + $title = Request::post('title'); + $tempfile = Session::get('mod_temp'); + if (empty($title) || empty($tempfile) || !file_exists($tempfile)) { + Message::addError('empty-field'); + return; + } + // Recompress using task manager + $this->taskId = 'tgzmod' . mt_rand() . '-' . microtime(true); + $destFile = CONFIG_TGZ_LIST_DIR . '/modules/mod-' . preg_replace('/[^a-z0-9_\-]+/is', '_', $title) . '-' . microtime(true) . '.tgz'; + Taskmanager::submit('RecompressArchive', array( + 'id' => $this->taskId, + 'inputFiles' => array($tempfile), + 'outputFile' => $destFile + ), true); + $status = Taskmanager::waitComplete($this->taskId); + unlink($tempfile); + if (!isset($status['statusCode'])) { + $this->tmError(); + } + if ($status['statusCode'] != TASK_FINISHED) { + $this->taskError($status); + } + // Seems ok, create entry in DB + $ret = Database::exec("INSERT INTO configtgz_module (title, moduletype, filename, contents) VALUES (:title, 'custom', :file, '')", + array('title' => $title, 'file' => $destFile)); + if ($ret === false) { + unlink($destFile); + Util::traceError("Could not insert module into Database"); + } + Message::addSuccess('module-added'); + Util::redirect('?do=sysconfig'); + } + +} diff --git a/modules/syslog.inc.php b/modules/syslog.inc.php index f8d99de3..62c94edf 100644 --- a/modules/syslog.inc.php +++ b/modules/syslog.inc.php @@ -12,26 +12,35 @@ function render_module() { Render::setTitle('Client Log'); - $filter = ''; - $not = ''; - if (isset($_POST['filter'])) $filter = $_POST['filter']; + if (isset($_GET['filter'])) { + $filter = $_GET['filter']; + $not = isset($_GET['not']) ? 'NOT' : ''; + } elseif (isset($_POST['filter'])) { + $filter = $_POST['filter']; + $not = isset($_POST['not']) ? 'NOT' : ''; + Session::set('log_filter', $filter); + Session::set('log_not', $not); + Session::save(); + } else { + $filter = Session::get('log_filter'); + $not = Session::get('log_not') ? 'NOT' : ''; + } if (!empty($filter)) { - $parts = explode(',', $filter); - $opt = array(); - foreach ($parts as $part) { - $part = preg_replace('/[^a-z0-9_\-]/', '', trim($part)); - if (empty($part) || in_array($part, $opt)) continue; - $opt[] = "'$part'"; + $filterList = explode(',', $filter); + $whereClause = array(); + foreach ($filterList as $filterItem) { + $filterItem = preg_replace('/[^a-z0-9_\-]/', '', trim($filterItem)); + if (empty($filterItem) || in_array($filterItem, $whereClause)) continue; + $whereClause[] = "'$filterItem'"; } - if (isset($_POST['not'])) $not = 'NOT'; - if (!empty($opt)) $opt = ' WHERE logtypeid ' . $not . ' IN (' . implode(', ', $opt) . ')'; + if (!empty($whereClause)) $whereClause = ' WHERE logtypeid ' . $not . ' IN (' . implode(', ', $whereClause) . ')'; } - if (!isset($opt) || empty($opt)) $opt = ''; + if (!isset($whereClause) || empty($whereClause)) $whereClause = ''; $today = date('d.m.Y'); $yesterday = date('d.m.Y', time() - 86400); $lines = array(); - $paginate = new Paginate("SELECT logid, dateline, logtypeid, clientip, description, extra FROM clientlog $opt ORDER BY logid DESC", 50); + $paginate = new Paginate("SELECT logid, dateline, logtypeid, clientip, description, extra FROM clientlog $whereClause ORDER BY logid DESC", 50); $res = $paginate->exec(); while ($row = $res->fetch(PDO::FETCH_ASSOC)) { $day = date('d.m.Y', $row['dateline']); |