From c269cd5c5c020d9285978b1cc11d4c4c4528fd61 Mon Sep 17 00:00:00 2001 From: Michael Kaufmann Date: Sun, 25 Nov 2018 11:17:56 +0100 Subject: [PATCH] added unit-tests for version-check; implemented settings import/export in API; minor variable declarations Signed-off-by: Michael Kaufmann --- admin_index.php | 48 ++---- admin_settings.php | 11 +- lib/classes/api/commands/class.Customers.php | 1 + lib/classes/api/commands/class.Froxlor.php | 155 ++++++++++++------- lib/classes/api/commands/class.Ftps.php | 1 + lib/classes/api/commands/class.Mysqls.php | 1 + phpunit.xml | 1 + templates/Sparkle/admin/index/index.tpl | 15 +- tests/Froxlor/FroxlorTest.php | 20 +++ 9 files changed, 146 insertions(+), 107 deletions(-) create mode 100644 tests/Froxlor/FroxlorTest.php diff --git a/admin_index.php b/admin_index.php index 973a703f..660d2539 100644 --- a/admin_index.php +++ b/admin_index.php @@ -86,46 +86,22 @@ if ($page == 'overview') { if ((isset($_GET['lookfornewversion']) && $_GET['lookfornewversion'] == 'yes') || (isset($lookfornewversion) && $lookfornewversion == 'yes') ) { - if (function_exists('curl_version')) { - $update_check_uri = 'http://version.froxlor.org/Froxlor/api/' . $version; - $latestversion = HttpClient::urlGet($update_check_uri); - $latestversion = explode('|', $latestversion); - - if (is_array($latestversion) - && count($latestversion) >= 1 - ) { - $_version = $latestversion[0]; - $_message = isset($latestversion[1]) ? $latestversion[1] : ''; - $_link = isset($latestversion[2]) ? $latestversion[2] : htmlspecialchars($filename . '?s=' . urlencode($s) . '&page=' . urlencode($page) . '&lookfornewversion=yes'); - - // add the branding so debian guys are not gettings confused - // about their version-number - $lookfornewversion_lable = $_version.$branding; - $lookfornewversion_link = $_link; - $lookfornewversion_addinfo = $_message; - - // not numeric -> error-message - if (!preg_match('/^((\d+\\.)(\d+\\.)(\d+\\.)?(\d+)?(\-(svn|dev|rc)(\d+))?)$/', $_version)) { - // check for customized version to not output - // "There is a newer version of froxlor" besides the error-message - $isnewerversion = 2; - } elseif (version_compare2($version, $_version) == -1) { - $isnewerversion = 1; - } else { - $isnewerversion = 0; - } - } else { - redirectTo($update_check_uri.'/pretty', NULL, false); - } - } else { - $lookfornewversion_lable = "Version-check not available due to missing php-curl extension"; - $lookfornewversion_link = htmlspecialchars($filename . '?s=' . urlencode($s) . '&page=' . urlencode($page) . '&lookfornewversion=yes'); - $lookfornewversion_addinfo = ''; - $isnewerversion = 0; + try { + $json_result = Froxlor::getLocal($userinfo)->checkUpdate(); + } catch (Exception $e) { + dynamic_error($e->getMessage()); } + $result = json_decode($json_result, true)['data']; + + $lookfornewversion_lable = $result['version']; + $lookfornewversion_link = $result['link']; + $lookfornewversion_message = $result['message']; + $lookfornewversion_addinfo = $result['additional_info']; + $isnewerversion = $result['isnewerversion']; } else { $lookfornewversion_lable = $lng['admin']['lookfornewversion']['clickhere']; $lookfornewversion_link = htmlspecialchars($filename . '?s=' . urlencode($s) . '&page=' . urlencode($page) . '&lookfornewversion=yes'); + $lookfornewversion_message = ''; $lookfornewversion_addinfo = ''; $isnewerversion = 0; } diff --git a/admin_settings.php b/admin_settings.php index 8bc17a13..5098ab45 100644 --- a/admin_settings.php +++ b/admin_settings.php @@ -300,7 +300,8 @@ elseif ($page == 'importexport' && $userinfo['change_serversettings'] == '1') if (isset($_GET['action']) && $_GET['action'] == "export") { // export try { - $json_export = SImExporter::export(); + $json_result = Froxlor::getLocal($userinfo)->exportSettings(); + $json_export = json_decode($json_result, true)['data']; } catch(Exception $e) { dynamic_error($e->getMessage()); } @@ -315,16 +316,10 @@ elseif ($page == 'importexport' && $userinfo['change_serversettings'] == '1') if (isset($_FILES["import_file"]["tmp_name"])) { $imp_content = file_get_contents($_FILES["import_file"]["tmp_name"]); try { - SImExporter::import($imp_content); + Froxlor::getLocal($userinfo, array('json_str' => $imp_content))->importSettings(); } catch(Exception $e) { dynamic_error($e->getMessage()); } - inserttask('1'); - inserttask('10'); - // Using nameserver, insert a task which rebuilds the server config - inserttask('4'); - // cron.d file - inserttask('99'); standard_success('settingsimported', '', array('filename' => 'admin_settings.php')); } dynamic_error("Upload failed"); diff --git a/lib/classes/api/commands/class.Customers.php b/lib/classes/api/commands/class.Customers.php index 75e3dd32..0b0d0894 100644 --- a/lib/classes/api/commands/class.Customers.php +++ b/lib/classes/api/commands/class.Customers.php @@ -692,6 +692,7 @@ class Customers extends ApiCommand implements ResourceEntity ), 'mails', 'createcustomer_mailbody', $replace_arr, $this->lng['mails']['createcustomer']['mailbody']); $_mailerror = false; + $mailerr_msg = ""; try { $this->mailer()->Subject = $mail_subject; $this->mailer()->AltBody = $mail_body; diff --git a/lib/classes/api/commands/class.Froxlor.php b/lib/classes/api/commands/class.Froxlor.php index 0135cd19..554c6276 100644 --- a/lib/classes/api/commands/class.Froxlor.php +++ b/lib/classes/api/commands/class.Froxlor.php @@ -27,79 +27,123 @@ class Froxlor extends ApiCommand */ public function checkUpdate() { + define('UPDATE_URI', "https://version.froxlor.org/Froxlor/api/" . $this->version); + if ($this->isAdmin() && $this->getUserDetail('change_serversettings')) { - // log our actions - $this->logger()->logAction(ADM_ACTION, LOG_NOTICE, "[API] checking for updates"); + if (function_exists('curl_version')) { + // log our actions + $this->logger()->logAction(ADM_ACTION, LOG_NOTICE, "[API] checking for updates"); - // check for new version - define('UPDATE_URI', "https://version.froxlor.org/Froxlor/api/" . $this->version); - $latestversion = HttpClient::urlGet(UPDATE_URI); - $latestversion = explode('|', $latestversion); + // check for new version + $latestversion = HttpClient::urlGet(UPDATE_URI); + $latestversion = explode('|', $latestversion); - if (is_array($latestversion) && count($latestversion) >= 1) { - $_version = $latestversion[0]; - $_message = isset($latestversion[1]) ? $latestversion[1] : ''; - $_link = isset($latestversion[2]) ? $latestversion[2] : ''; + if (is_array($latestversion) && count($latestversion) >= 1) { + $_version = $latestversion[0]; + $_message = isset($latestversion[1]) ? $latestversion[1] : ''; + $_link = isset($latestversion[2]) ? $latestversion[2] : ''; - // add the branding so debian guys are not gettings confused - // about their version-number - $version_label = $_version . $this->branding; - $version_link = $_link; - $message_addinfo = $_message; + // add the branding so debian guys are not gettings confused + // about their version-number + $version_label = $_version . $this->branding; + $version_link = $_link; + $message_addinfo = $_message; - // not numeric -> error-message - if (! preg_match('/^((\d+\\.)(\d+\\.)(\d+\\.)?(\d+)?(\-(svn|dev|rc)(\d+))?)$/', $_version)) { - // check for customized version to not output - // "There is a newer version of froxlor" besides the error-message - $isnewerversion = - 1; - } elseif (version_compare2($this->version, $_version) == - 1) { - // there is a newer version - yay - $isnewerversion = 1; - } else { - // nothing new - $isnewerversion = 0; + // not numeric -> error-message + if (! preg_match('/^((\d+\\.)(\d+\\.)(\d+\\.)?(\d+)?(\-(svn|dev|rc)(\d+))?)$/', $_version)) { + // check for customized version to not output + // "There is a newer version of froxlor" besides the error-message + $isnewerversion = - 1; + } elseif (version_compare2($this->version, $_version) == - 1) { + // there is a newer version - yay + $isnewerversion = 1; + } else { + // nothing new + $isnewerversion = 0; + } + + // anzeige über version-status mit ggfls. formular + // zum update schritt #1 -> download + if ($isnewerversion == 1) { + $text = 'There is a newer version available: "' . $_version . '" (Your current version is: ' . $this->version . ')'; + return $this->response(200, "successfull", array( + 'isnewerversion' => $isnewerversion, + 'version' => $_version, + 'message' => $text, + 'link' => $version_link, + 'additional_info' => $message_addinfo + )); + } elseif ($isnewerversion == 0) { + // all good + return $this->response(200, "successfull", array( + 'isnewerversion' => $isnewerversion, + 'version' => $version_label, + 'message' => "", + 'link' => $version_link, + 'additional_info' => $message_addinfo + )); + } else { + standard_error('customized_version', '', true); + } } + } + return $this->response(300, "successfull", array( + 'isnewerversion' => 0, + 'version' => $this->version.$this->branding, + 'message' => 'Version-check not available due to missing php-curl extension', + 'link' => UPDATE_URI.'/pretty', + 'additional_info' => "" + )); + } + throw new Exception("Not allowed to execute given command.", 403); + } - // anzeige über version-status mit ggfls. formular - // zum update schritt #1 -> download - if ($isnewerversion == 1) { - $text = 'There is a newer version available: "' . $_version . '" (Your current version is: ' . $version_label . ')'; - return $this->response(200, "successfull", array( - 'message' => $text, - 'link' => $version_link, - 'additional_info' => $message_addinfo - )); - } elseif ($isnewerversion == 0) { - // all good - standard_success('noupdatesavail', '', array(), true); - } else { - standard_error('customized_version', '', true); - } + /** + * import settings + * + * @param string $json_str + * content of exported froxlor-settings json file + * + * @access admin + * @throws Exception + * @return bool + */ + public function importSettings() + { + if ($this->isAdmin() && $this->getUserDetail('change_serversettings')) { + $json_str = $this->getParam('json_str'); + $this->logger()->logAction(ADM_ACTION, LOG_NOTICE, "User " . $this->getUserDetail('loginname') . " imported settings"); + try { + SImExporter::import($json_str); + inserttask('1'); + inserttask('10'); + // Using nameserver, insert a task which rebuilds the server config + inserttask('4'); + // cron.d file + inserttask('99'); + return $this->response(200, "successfull", true); + } catch (Exception $e) { + throw new Exception($e->getMessage(), 406); } } throw new Exception("Not allowed to execute given command.", 403); } /** + * export settings * - * @todo import settings - * - * @access admin - */ - public function importSettings() - { - throw new Exception("Not available yet.", 501); - } - - /** - * - * @todo export settings - * * @access admin + * @throws Exception + * @return string json-string */ public function exportSettings() { - throw new Exception("Not available yet.", 501); + if ($this->isAdmin() && $this->getUserDetail('change_serversettings')) { + $this->logger()->logAction(ADM_ACTION, LOG_NOTICE, "User " . $this->getUserDetail('loginname') . " exported settings"); + $json_export = SImExporter::export(); + return $this->response(200, "successfull", $json_export); + } + throw new Exception("Not allowed to execute given command.", 403); } /** @@ -279,6 +323,7 @@ class Froxlor extends ApiCommand $result = array(); $result['params'] = array(); $param_desc = false; + $r = array(); foreach ($clines as $c) { $c = trim($c); // check param-section diff --git a/lib/classes/api/commands/class.Ftps.php b/lib/classes/api/commands/class.Ftps.php index ae4c9e95..4cab9039 100644 --- a/lib/classes/api/commands/class.Ftps.php +++ b/lib/classes/api/commands/class.Ftps.php @@ -195,6 +195,7 @@ class Ftps extends ApiCommand implements ResourceEntity $mail_body = $this->getMailTemplate($customer, 'mails', 'new_ftpaccount_by_customer_mailbody', $replace_arr, $this->lng['mails']['new_ftpaccount_by_customer']['mailbody']); $_mailerror = false; + $mailerr_msg = ""; try { $this->mailer()->Subject = $mail_subject; $this->mailer()->AltBody = $mail_body; diff --git a/lib/classes/api/commands/class.Mysqls.php b/lib/classes/api/commands/class.Mysqls.php index 996c8d80..b174d437 100644 --- a/lib/classes/api/commands/class.Mysqls.php +++ b/lib/classes/api/commands/class.Mysqls.php @@ -138,6 +138,7 @@ class Mysqls extends ApiCommand implements ResourceEntity $mail_body = $this->getMailTemplate($userinfo, 'mails', 'new_database_by_customer_mailbody', $replace_arr, $this->lng['mails']['new_database_by_customer']['mailbody']); $_mailerror = false; + $mailerr_msg = ""; try { $this->mailer()->Subject = $mail_subject; $this->mailer()->AltBody = $mail_body; diff --git a/phpunit.xml b/phpunit.xml index 02370269..97f6c4cd 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -9,6 +9,7 @@ tests/Global + tests/Froxlor tests/Admins tests/Customers tests/IpsAndPorts diff --git a/templates/Sparkle/admin/index/index.tpl b/templates/Sparkle/admin/index/index.tpl index 0e8fa3de..02a5b6fe 100644 --- a/templates/Sparkle/admin/index/index.tpl +++ b/templates/Sparkle/admin/index/index.tpl @@ -275,15 +275,15 @@ $header $lookfornewversion_lable - + - {$lng['admin']['newerversionavailable']} + $lookfornewversion_message + + + + + $lookfornewversion_addinfo - - - $lookfornewversion_addinfo - - @@ -292,4 +292,3 @@ $header $footer - diff --git a/tests/Froxlor/FroxlorTest.php b/tests/Froxlor/FroxlorTest.php new file mode 100644 index 00000000..f6539354 --- /dev/null +++ b/tests/Froxlor/FroxlorTest.php @@ -0,0 +1,20 @@ +checkUpdate(); + $result = json_decode($json_result, true)['data']; + $this->assertEquals(0, $result['isnewerversion']); + $this->assertEquals("You already have the latest version of Froxlor installed.", $result['additional_info']); + } +}