From b2ac1fb593e357b4e523a9ac10115ec71e957c00 Mon Sep 17 00:00:00 2001 From: "Michael Kaufmann (d00p)" Date: Mon, 19 Feb 2018 13:58:39 +0100 Subject: [PATCH] add FpmDaemons ApiCommand Signed-off-by: Michael Kaufmann (d00p) --- admin_phpsettings.php | 186 +++--------- lib/classes/api/commands/class.FpmDaemons.php | 277 ++++++++++++++++++ .../api/commands/class.PhpSettings.php | 2 +- 3 files changed, 317 insertions(+), 148 deletions(-) create mode 100644 lib/classes/api/commands/class.FpmDaemons.php diff --git a/admin_phpsettings.php b/admin_phpsettings.php index 12b6b853..6eee297a 100644 --- a/admin_phpsettings.php +++ b/admin_phpsettings.php @@ -171,38 +171,26 @@ if ($page == 'overview') { } elseif ($page == 'fpmdaemons') { if ($action == '') { + + try { + $json_result = FpmDaemons::getLocal($userinfo)->list(); + } catch (Exception $e) { + dynamic_error($e->getMessage()); + } + $result = json_decode($json_result, true)['data']; $tablecontent = ''; $count = 0; - $result = Database::query("SELECT * FROM `" . TABLE_PANEL_FPMDAEMONS . "` ORDER BY `description` ASC"); - - while ($row = $result->fetch(PDO::FETCH_ASSOC)) { - - $query_params = array( - 'id' => $row['id'] - ); - - $query = "SELECT * FROM `" . TABLE_PANEL_PHPCONFIGS . "` WHERE `fpmsettingid` = :id"; - - $configresult_stmt = Database::prepare($query); - Database::pexecute($configresult_stmt, $query_params); - - $configs = ''; - if (Database::num_rows() > 0) { - while ($row2 = $configresult_stmt->fetch(PDO::FETCH_ASSOC)) { - $configs .= $row2['description'] . '
'; + if (isset($result['count']) && $result['count'] > 0) { + foreach ($result['list'] as $row) { + $configs = ""; + foreach ($row['configs'] as $configused) { + $configs .= $configused . "
"; } + $count++; + eval("\$tablecontent.=\"" . getTemplate("phpconfig/fpmdaemons_overview") . "\";"); } - - if ($configs == '') { - $configs = $lng['admin']['phpsettings']['notused']; - } - - $count ++; - eval("\$tablecontent.=\"" . getTemplate("phpconfig/fpmdaemons_overview") . "\";"); } - - $log->logAction(ADM_ACTION, LOG_INFO, "fpm daemons setting overview has been viewed by '" . $userinfo['loginname'] . "'"); eval("echo \"" . getTemplate("phpconfig/fpmdaemons") . "\";"); } @@ -211,53 +199,11 @@ if ($page == 'overview') { if ((int) $userinfo['change_serversettings'] == 1) { if (isset($_POST['send']) && $_POST['send'] == 'send') { - $description = validate($_POST['description'], 'description'); - $reload_cmd = validate($_POST['reload_cmd'], 'reload_cmd'); - $config_dir = validate($_POST['config_dir'], 'config_dir'); - $pm = $_POST['pm']; - $max_children = isset($_POST['max_children']) ? (int) $_POST['max_children'] : 0; - $start_servers = isset($_POST['start_servers']) ? (int) $_POST['start_servers'] : 0; - $min_spare_servers = isset($_POST['min_spare_servers']) ? (int) $_POST['min_spare_servers'] : 0; - $max_spare_servers = isset($_POST['max_spare_servers']) ? (int) $_POST['max_spare_servers'] : 0; - $max_requests = isset($_POST['max_requests']) ? (int) $_POST['max_requests'] : 0; - $idle_timeout = isset($_POST['idle_timeout']) ? (int) $_POST['idle_timeout'] : 0; - $limit_extensions = validate($_POST['limit_extensions'], 'limit_extensions', '/^(\.[a-z]([a-z0-9]+)\ ?)+$/'); - - if (strlen($description) == 0 || strlen($description) > 50) { - standard_error('descriptioninvalid'); + try { + FpmDaemons::getLocal($userinfo, $_POST)->add(); + } catch (Exception $e) { + dynamic_error($e->getMessage()); } - - $ins_stmt = Database::prepare(" - INSERT INTO `" . TABLE_PANEL_FPMDAEMONS . "` SET - `description` = :desc, - `reload_cmd` = :reload_cmd, - `config_dir` = :config_dir, - `pm` = :pm, - `max_children` = :max_children, - `start_servers` = :start_servers, - `min_spare_servers` = :min_spare_servers, - `max_spare_servers` = :max_spare_servers, - `max_requests` = :max_requests, - `idle_timeout` = :idle_timeout, - `limit_extensions` = :limit_extensions - "); - $ins_data = array( - 'desc' => $description, - 'reload_cmd' => $reload_cmd, - 'config_dir' => makeCorrectDir($config_dir), - 'pm' => $pm, - 'max_children' => $max_children, - 'start_servers' => $start_servers, - 'min_spare_servers' => $min_spare_servers, - 'max_spare_servers' => $max_spare_servers, - 'max_requests' => $max_requests, - 'idle_timeout' => $idle_timeout, - 'limit_extensions' => $limit_extensions - ); - Database::pexecute($ins_stmt, $ins_data); - - inserttask('1'); - $log->logAction(ADM_ACTION, LOG_INFO, "fpm-daemon setting with description '" . $description . "' has been created by '" . $userinfo['loginname'] . "'"); redirectTo($filename, array( 'page' => $page, 's' => $s @@ -283,11 +229,12 @@ if ($page == 'overview') { if ($action == 'delete') { - $result_stmt = Database::prepare(" - SELECT * FROM `" . TABLE_PANEL_FPMDAEMONS . "` WHERE `id` = :id"); - $result = Database::pexecute_first($result_stmt, array( - 'id' => $id - )); + try { + $json_result = FpmDaemons::getLocal($userinfo, array('id' => $id))->get(); + } catch (Exception $e) { + dynamic_error($e->getMessage()); + } + $result = json_decode($json_result, true)['data']; if ($id == 1) { standard_error('cannotdeletedefaultphpconfig'); @@ -295,24 +242,12 @@ if ($page == 'overview') { if ($result['id'] != 0 && $result['id'] == $id && (int) $userinfo['change_serversettings'] == 1 && $id != 1) // cannot delete the default php.config { - if (isset($_POST['send']) && $_POST['send'] == 'send') { - // set default fpm daemon config for all php-config that use this config that is to be deleted - $upd_stmt = Database::prepare(" - UPDATE `" . TABLE_PANEL_PHPCONFIGS . "` SET - `fpmsettingid` = '1' WHERE `fpmsettingid` = :id"); - Database::pexecute($upd_stmt, array( - 'id' => $id - )); - - $del_stmt = Database::prepare(" - DELETE FROM `" . TABLE_PANEL_FPMDAEMONS . "` WHERE `id` = :id"); - Database::pexecute($del_stmt, array( - 'id' => $id - )); - - inserttask('1'); - $log->logAction(ADM_ACTION, LOG_INFO, "fpm-daemon setting with id #" . (int) $id . " has been deleted by '" . $userinfo['loginname'] . "'"); + try { + FpmDaemons::getLocal($userinfo, $_POST)->delete(); + } catch (Exception $e) { + dynamic_error($e->getMessage()); + } redirectTo($filename, array( 'page' => $page, 's' => $s @@ -331,64 +266,21 @@ if ($page == 'overview') { if ($action == 'edit') { - $result_stmt = Database::prepare(" - SELECT * FROM `" . TABLE_PANEL_FPMDAEMONS . "` WHERE `id` = :id"); - $result = Database::pexecute_first($result_stmt, array( - 'id' => $id - )); + try { + $json_result = FpmDaemons::getLocal($userinfo, array('id' => $id))->get(); + } catch (Exception $e) { + dynamic_error($e->getMessage()); + } + $result = json_decode($json_result, true)['data']; if ($result['id'] != 0 && $result['id'] == $id && (int) $userinfo['change_serversettings'] == 1) { if (isset($_POST['send']) && $_POST['send'] == 'send') { - $description = validate($_POST['description'], 'description'); - $reload_cmd = validate($_POST['reload_cmd'], 'reload_cmd'); - $config_dir = validate($_POST['config_dir'], 'config_dir'); - $pm = $_POST['pm']; - $max_children = isset($_POST['max_children']) ? (int) $_POST['max_children'] : $result['max_children']; - $start_servers = isset($_POST['start_servers']) ? (int) $_POST['start_servers'] : $result['start_servers']; - $min_spare_servers = isset($_POST['min_spare_servers']) ? (int) $_POST['min_spare_servers'] : $result['min_spare_servers']; - $max_spare_servers = isset($_POST['max_spare_servers']) ? (int) $_POST['max_spare_servers'] : $result['max_spare_servers']; - $max_requests = isset($_POST['max_requests']) ? (int) $_POST['max_requests'] : $result['max_requests']; - $idle_timeout = isset($_POST['idle_timeout']) ? (int) $_POST['idle_timeout'] : $result['idle_timeout']; - $limit_extensions = validate($_POST['limit_extensions'], 'limit_extensions', '/^(\.[a-z]([a-z0-9]+)\ ?)+$/'); - - if (strlen($description) == 0 || strlen($description) > 50) { - standard_error('descriptioninvalid'); + try { + FpmDaemons::getLocal($userinfo, $_POST)->update(); + } catch (Exception $e) { + dynamic_error($e->getMessage()); } - - $upd_stmt = Database::prepare(" - UPDATE `" . TABLE_PANEL_FPMDAEMONS . "` SET - `description` = :desc, - `reload_cmd` = :reload_cmd, - `config_dir` = :config_dir, - `pm` = :pm, - `max_children` = :max_children, - `start_servers` = :start_servers, - `min_spare_servers` = :min_spare_servers, - `max_spare_servers` = :max_spare_servers, - `max_requests` = :max_requests, - `idle_timeout` = :idle_timeout, - `limit_extensions` = :limit_extensions - WHERE `id` = :id - "); - $upd_data = array( - 'desc' => $description, - 'reload_cmd' => $reload_cmd, - 'config_dir' => makeCorrectDir($config_dir), - 'pm' => $pm, - 'max_children' => $max_children, - 'start_servers' => $start_servers, - 'min_spare_servers' => $min_spare_servers, - 'max_spare_servers' => $max_spare_servers, - 'max_requests' => $max_requests, - 'idle_timeout' => $idle_timeout, - 'limit_extensions' => $limit_extensions, - 'id' => $id - ); - Database::pexecute($upd_stmt, $upd_data); - - inserttask('1'); - $log->logAction(ADM_ACTION, LOG_INFO, "fpm-daemon setting with description '" . $description . "' has been changed by '" . $userinfo['loginname'] . "'"); redirectTo($filename, array( 'page' => $page, 's' => $s diff --git a/lib/classes/api/commands/class.FpmDaemons.php b/lib/classes/api/commands/class.FpmDaemons.php new file mode 100644 index 00000000..9d906593 --- /dev/null +++ b/lib/classes/api/commands/class.FpmDaemons.php @@ -0,0 +1,277 @@ + (2010-) + * @license GPLv2 http://files.froxlor.org/misc/COPYING.txt + * @package Panel + * + */ +class FpmDaemons extends ApiCommand implements ResourceEntity +{ + + /** + * lists all php-config entries + * + * @return array count|list + */ + public function list() + { + if ($this->isAdmin()) { + $this->logger()->logAction(ADM_ACTION, LOG_NOTICE, "[API] list fpm-daemons"); + + $result = Database::query(" + SELECT * FROM `" . TABLE_PANEL_FPMDAEMONS . "` ORDER BY `description` ASC + "); + + $fpmdaemons = array(); + while ($row = $result->fetch(PDO::FETCH_ASSOC)) { + + $query_params = array( + 'id' => $row['id'] + ); + + $query = "SELECT * FROM `" . TABLE_PANEL_PHPCONFIGS . "` WHERE `fpmsettingid` = :id"; + + $configresult_stmt = Database::prepare($query); + Database::pexecute($configresult_stmt, $query_params, true, true); + + $configs = array(); + if (Database::num_rows() > 0) { + while ($row2 = $configresult_stmt->fetch(PDO::FETCH_ASSOC)) { + $configs[] = $row2['description']; + } + } + + if (empty($configs)) { + $configs[] = $lng['admin']['phpsettings']['notused']; + } + + $row['configs'] = $configs; + $fpmdaemons[] = $row; + } + + return $this->response(200, "successfull", array( + 'count' => count($fpmdaemons), + 'list' => $fpmdaemons + )); + } + throw new Exception("Not allowed to execute given command.", 403); + } + + public function get() + { + if ($this->isAdmin()) { + $id = $this->getParam('id'); + + $result_stmt = Database::prepare(" + SELECT * FROM `" . TABLE_PANEL_FPMDAEMONS . "` WHERE `id` = :id + "); + $result = Database::pexecute_first($result_stmt, array( + 'id' => $id + ), true, true); + if ($result) { + return $this->response(200, "successfull", $result); + } + throw new Exception("fpm-daemon with id #" . $id . " could not be found", 404); + } + throw new Exception("Not allowed to execute given command.", 403); + } + + public function add() + { + if ($this->isAdmin() && $this->getUserDetail('change_serversettings') == 1) { + + // required parameter + $description = $this->getParam('description'); + $reload_cmd = $this->getParam('reload_cmd'); + $config_dir = $this->getParam('config_dir'); + + // parameters + $pm = $this->getParam('pm', true, 'static'); + $max_children = $this->getParam('max_children', true, 0); + $start_servers = $this->getParam('start_servers', true, 0); + $min_spare_servers = $this->getParam('min_spare_servers', true, 0); + $max_spare_servers = $this->getParam('max_spare_servers', true, 0); + $max_requests = $this->getParam('max_requests', true, 0); + $idle_timeout = $this->getParam('idle_timeout', true, 0); + $limit_extensions = $this->getParam('limit_extensions', true, ''); + + // validation + $description = validate($description, 'description', '', '', array(), true); + $reload_cmd = validate($reload_cmd, 'reload_cmd', '', '', array(), true); + $config_dir = validate($config_dir, 'config_dir', '', '', array(), true); + if (! in_array($pm, array( + 'static', + 'dynamic', + 'ondemand' + ))) { + throw new ErrorException("Unknown process manager", 406); + } + $limit_extensions = validate($limit_extensions, 'limit_extensions', '/^(\.[a-z]([a-z0-9]+)\ ?)+$/', '', array(), true); + + if (strlen($description) == 0 || strlen($description) > 50) { + standard_error('descriptioninvalid', '', true); + } + + $ins_stmt = Database::prepare(" + INSERT INTO `" . TABLE_PANEL_FPMDAEMONS . "` SET + `description` = :desc, + `reload_cmd` = :reload_cmd, + `config_dir` = :config_dir, + `pm` = :pm, + `max_children` = :max_children, + `start_servers` = :start_servers, + `min_spare_servers` = :min_spare_servers, + `max_spare_servers` = :max_spare_servers, + `max_requests` = :max_requests, + `idle_timeout` = :idle_timeout, + `limit_extensions` = :limit_extensions + "); + $ins_data = array( + 'desc' => $description, + 'reload_cmd' => $reload_cmd, + 'config_dir' => makeCorrectDir($config_dir), + 'pm' => $pm, + 'max_children' => $max_children, + 'start_servers' => $start_servers, + 'min_spare_servers' => $min_spare_servers, + 'max_spare_servers' => $max_spare_servers, + 'max_requests' => $max_requests, + 'idle_timeout' => $idle_timeout, + 'limit_extensions' => $limit_extensions + ); + Database::pexecute($ins_stmt, $ins_data); + $ins_data['id'] = Database::lastInsertId(); + + inserttask('1'); + $this->logger()->logAction(ADM_ACTION, LOG_INFO, "[API] fpm-daemon with description '" . $description . "' has been created by '" . $this->getUserDetail('loginname') . "'"); + return $this->response(200, "successfull", $ins_data); + } + throw new Exception("Not allowed to execute given command.", 403); + } + + public function update() + { + if ($this->isAdmin() && $this->getUserDetail('change_serversettings') == 1) { + + // required parameter + $id = $this->getParam('id'); + + $json_result = PhpSettings::getLocal($this->getUserData(), array( + 'id' => $id + ))->get(); + $result = json_decode($json_result, true)['data']; + + // parameters + $description = $this->getParam('description', true, $result['description']); + $reload_cmd = $this->getParam('reload_cmd', true, $result['reload_cmd']); + $config_dir = $this->getParam('config_dir', true, $result['config_dir']); + $pm = $this->getParam('pm', true, $result['pm']); + $max_children = $this->getParam('max_children', true, $result['max_children']); + $start_servers = $this->getParam('start_servers', true, $result['start_servers']); + $min_spare_servers = $this->getParam('min_spare_servers', true, $result['min_spare_servers']); + $max_spare_servers = $this->getParam('max_spare_servers', true, $result['max_spare_servers']); + $max_requests = $this->getParam('max_requests', true, $result['max_requests']); + $idle_timeout = $this->getParam('idle_timeout', true, $result['idle_timeout']); + $limit_extensions = $this->getParam('limit_extensions', true, $result['limit_extensions']); + + // validation + $description = validate($description, 'description', '', '', array(), true); + $reload_cmd = validate($reload_cmd, 'reload_cmd', '', '', array(), true); + $config_dir = validate($config_dir, 'config_dir', '', '', array(), true); + if (! in_array($pm, array( + 'static', + 'dynamic', + 'ondemand' + ))) { + throw new ErrorException("Unknown process manager", 406); + } + $limit_extensions = validate($limit_extensions, 'limit_extensions', '/^(\.[a-z]([a-z0-9]+)\ ?)+$/', '', array(), true); + + if (strlen($description) == 0 || strlen($description) > 50) { + standard_error('descriptioninvalid', '', true); + } + + $upd_stmt = Database::prepare(" + UPDATE `" . TABLE_PANEL_FPMDAEMONS . "` SET + `description` = :desc, + `reload_cmd` = :reload_cmd, + `config_dir` = :config_dir, + `pm` = :pm, + `max_children` = :max_children, + `start_servers` = :start_servers, + `min_spare_servers` = :min_spare_servers, + `max_spare_servers` = :max_spare_servers, + `max_requests` = :max_requests, + `idle_timeout` = :idle_timeout, + `limit_extensions` = :limit_extensions + WHERE `id` = :id + "); + $upd_data = array( + 'desc' => $description, + 'reload_cmd' => $reload_cmd, + 'config_dir' => makeCorrectDir($config_dir), + 'pm' => $pm, + 'max_children' => $max_children, + 'start_servers' => $start_servers, + 'min_spare_servers' => $min_spare_servers, + 'max_spare_servers' => $max_spare_servers, + 'max_requests' => $max_requests, + 'idle_timeout' => $idle_timeout, + 'limit_extensions' => $limit_extensions, + 'id' => $id + ); + Database::pexecute($upd_stmt, $upd_data, true, true); + + inserttask('1'); + $this->logger()->logAction(ADM_ACTION, LOG_INFO, "[API] fpm-daemon with description '" . $description . "' has been updated by '" . $this->getUserDetail('loginname') . "'"); + return $this->response(200, "successfull", $upd_data); + } + throw new Exception("Not allowed to execute given command.", 403); + } + + public function delete() + { + if ($this->isAdmin() && $this->getUserDetail('change_serversettings') == 1) { + $id = $this->getParam('id'); + + if ($id == 1) { + standard_error('cannotdeletedefaultphpconfig', '', true); + } + + $json_result = FpmDaemons::getLocal($this->getUserData(), array( + 'id' => $id + ))->get(); + $result = json_decode($json_result, true)['data']; + + // set default fpm daemon config for all php-config that use this config that is to be deleted + $upd_stmt = Database::prepare(" + UPDATE `" . TABLE_PANEL_PHPCONFIGS . "` SET + `fpmsettingid` = '1' WHERE `fpmsettingid` = :id + "); + Database::pexecute($upd_stmt, array( + 'id' => $id + ), true, true); + + $del_stmt = Database::prepare(" + DELETE FROM `" . TABLE_PANEL_FPMDAEMONS . "` WHERE `id` = :id + "); + Database::pexecute($del_stmt, array( + 'id' => $id + ), true, true); + + inserttask('1'); + $this->logger()->logAction(ADM_ACTION, LOG_INFO, "[API] fpm-daemon setting '" . $result['description'] . "' has been deleted by '" . $this->getUserDetail('loginname') . "'"); + return $this->response(200, "successfull", $result); + } + throw new Exception("Not allowed to execute given command.", 403); + } +} diff --git a/lib/classes/api/commands/class.PhpSettings.php b/lib/classes/api/commands/class.PhpSettings.php index f987c1cd..686da052 100644 --- a/lib/classes/api/commands/class.PhpSettings.php +++ b/lib/classes/api/commands/class.PhpSettings.php @@ -356,7 +356,7 @@ class PhpSettings extends ApiCommand implements ResourceEntity ), true, true); inserttask('1'); - $this->logger()->logAction(ADM_ACTION, LOG_INFO, "[API] php setting '" . $result['description'] . " has been deleted by '" . $this->getUserDetail('loginname') . "'"); + $this->logger()->logAction(ADM_ACTION, LOG_INFO, "[API] php setting '" . $result['description'] . "' has been deleted by '" . $this->getUserDetail('loginname') . "'"); return $this->response(200, "successfull", $result); } throw new Exception("Not allowed to execute given command.", 403);