added unit-tests for version-check; implemented settings import/export in API; minor variable declarations

Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann
2018-11-25 11:17:56 +01:00
parent f6e0298d25
commit c269cd5c5c
9 changed files with 146 additions and 107 deletions

View File

@@ -86,46 +86,22 @@ if ($page == 'overview') {
if ((isset($_GET['lookfornewversion']) && $_GET['lookfornewversion'] == 'yes') if ((isset($_GET['lookfornewversion']) && $_GET['lookfornewversion'] == 'yes')
|| (isset($lookfornewversion) && $lookfornewversion == 'yes') || (isset($lookfornewversion) && $lookfornewversion == 'yes')
) { ) {
if (function_exists('curl_version')) { try {
$update_check_uri = 'http://version.froxlor.org/Froxlor/api/' . $version; $json_result = Froxlor::getLocal($userinfo)->checkUpdate();
$latestversion = HttpClient::urlGet($update_check_uri); } catch (Exception $e) {
$latestversion = explode('|', $latestversion); dynamic_error($e->getMessage());
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;
} }
$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 { } else {
$lookfornewversion_lable = $lng['admin']['lookfornewversion']['clickhere']; $lookfornewversion_lable = $lng['admin']['lookfornewversion']['clickhere'];
$lookfornewversion_link = htmlspecialchars($filename . '?s=' . urlencode($s) . '&page=' . urlencode($page) . '&lookfornewversion=yes'); $lookfornewversion_link = htmlspecialchars($filename . '?s=' . urlencode($s) . '&page=' . urlencode($page) . '&lookfornewversion=yes');
$lookfornewversion_message = '';
$lookfornewversion_addinfo = ''; $lookfornewversion_addinfo = '';
$isnewerversion = 0; $isnewerversion = 0;
} }

View File

@@ -300,7 +300,8 @@ elseif ($page == 'importexport' && $userinfo['change_serversettings'] == '1')
if (isset($_GET['action']) && $_GET['action'] == "export") { if (isset($_GET['action']) && $_GET['action'] == "export") {
// export // export
try { try {
$json_export = SImExporter::export(); $json_result = Froxlor::getLocal($userinfo)->exportSettings();
$json_export = json_decode($json_result, true)['data'];
} catch(Exception $e) { } catch(Exception $e) {
dynamic_error($e->getMessage()); dynamic_error($e->getMessage());
} }
@@ -315,16 +316,10 @@ elseif ($page == 'importexport' && $userinfo['change_serversettings'] == '1')
if (isset($_FILES["import_file"]["tmp_name"])) { if (isset($_FILES["import_file"]["tmp_name"])) {
$imp_content = file_get_contents($_FILES["import_file"]["tmp_name"]); $imp_content = file_get_contents($_FILES["import_file"]["tmp_name"]);
try { try {
SImExporter::import($imp_content); Froxlor::getLocal($userinfo, array('json_str' => $imp_content))->importSettings();
} catch(Exception $e) { } catch(Exception $e) {
dynamic_error($e->getMessage()); 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')); standard_success('settingsimported', '', array('filename' => 'admin_settings.php'));
} }
dynamic_error("Upload failed"); dynamic_error("Upload failed");

View File

@@ -692,6 +692,7 @@ class Customers extends ApiCommand implements ResourceEntity
), 'mails', 'createcustomer_mailbody', $replace_arr, $this->lng['mails']['createcustomer']['mailbody']); ), 'mails', 'createcustomer_mailbody', $replace_arr, $this->lng['mails']['createcustomer']['mailbody']);
$_mailerror = false; $_mailerror = false;
$mailerr_msg = "";
try { try {
$this->mailer()->Subject = $mail_subject; $this->mailer()->Subject = $mail_subject;
$this->mailer()->AltBody = $mail_body; $this->mailer()->AltBody = $mail_body;

View File

@@ -27,79 +27,123 @@ class Froxlor extends ApiCommand
*/ */
public function checkUpdate() public function checkUpdate()
{ {
define('UPDATE_URI', "https://version.froxlor.org/Froxlor/api/" . $this->version);
if ($this->isAdmin() && $this->getUserDetail('change_serversettings')) { if ($this->isAdmin() && $this->getUserDetail('change_serversettings')) {
// log our actions if (function_exists('curl_version')) {
$this->logger()->logAction(ADM_ACTION, LOG_NOTICE, "[API] checking for updates"); // log our actions
$this->logger()->logAction(ADM_ACTION, LOG_NOTICE, "[API] checking for updates");
// check for new version // check for new version
define('UPDATE_URI', "https://version.froxlor.org/Froxlor/api/" . $this->version); $latestversion = HttpClient::urlGet(UPDATE_URI);
$latestversion = HttpClient::urlGet(UPDATE_URI); $latestversion = explode('|', $latestversion);
$latestversion = explode('|', $latestversion);
if (is_array($latestversion) && count($latestversion) >= 1) { if (is_array($latestversion) && count($latestversion) >= 1) {
$_version = $latestversion[0]; $_version = $latestversion[0];
$_message = isset($latestversion[1]) ? $latestversion[1] : ''; $_message = isset($latestversion[1]) ? $latestversion[1] : '';
$_link = isset($latestversion[2]) ? $latestversion[2] : ''; $_link = isset($latestversion[2]) ? $latestversion[2] : '';
// add the branding so debian guys are not gettings confused // add the branding so debian guys are not gettings confused
// about their version-number // about their version-number
$version_label = $_version . $this->branding; $version_label = $_version . $this->branding;
$version_link = $_link; $version_link = $_link;
$message_addinfo = $_message; $message_addinfo = $_message;
// not numeric -> error-message // not numeric -> error-message
if (! preg_match('/^((\d+\\.)(\d+\\.)(\d+\\.)?(\d+)?(\-(svn|dev|rc)(\d+))?)$/', $_version)) { if (! preg_match('/^((\d+\\.)(\d+\\.)(\d+\\.)?(\d+)?(\-(svn|dev|rc)(\d+))?)$/', $_version)) {
// check for customized version to not output // check for customized version to not output
// "There is a newer version of froxlor" besides the error-message // "There is a newer version of froxlor" besides the error-message
$isnewerversion = - 1; $isnewerversion = - 1;
} elseif (version_compare2($this->version, $_version) == - 1) { } elseif (version_compare2($this->version, $_version) == - 1) {
// there is a newer version - yay // there is a newer version - yay
$isnewerversion = 1; $isnewerversion = 1;
} else { } else {
// nothing new // nothing new
$isnewerversion = 0; $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 * import settings
if ($isnewerversion == 1) { *
$text = 'There is a newer version available: "' . $_version . '" (Your current version is: ' . $version_label . ')'; * @param string $json_str
return $this->response(200, "successfull", array( * content of exported froxlor-settings json file
'message' => $text, *
'link' => $version_link, * @access admin
'additional_info' => $message_addinfo * @throws Exception
)); * @return bool
} elseif ($isnewerversion == 0) { */
// all good public function importSettings()
standard_success('noupdatesavail', '', array(), true); {
} else { if ($this->isAdmin() && $this->getUserDetail('change_serversettings')) {
standard_error('customized_version', '', true); $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); 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 * @access admin
* @throws Exception
* @return string json-string
*/ */
public function exportSettings() 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 = array();
$result['params'] = array(); $result['params'] = array();
$param_desc = false; $param_desc = false;
$r = array();
foreach ($clines as $c) { foreach ($clines as $c) {
$c = trim($c); $c = trim($c);
// check param-section // check param-section

View File

@@ -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']); $mail_body = $this->getMailTemplate($customer, 'mails', 'new_ftpaccount_by_customer_mailbody', $replace_arr, $this->lng['mails']['new_ftpaccount_by_customer']['mailbody']);
$_mailerror = false; $_mailerror = false;
$mailerr_msg = "";
try { try {
$this->mailer()->Subject = $mail_subject; $this->mailer()->Subject = $mail_subject;
$this->mailer()->AltBody = $mail_body; $this->mailer()->AltBody = $mail_body;

View File

@@ -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']); $mail_body = $this->getMailTemplate($userinfo, 'mails', 'new_database_by_customer_mailbody', $replace_arr, $this->lng['mails']['new_database_by_customer']['mailbody']);
$_mailerror = false; $_mailerror = false;
$mailerr_msg = "";
try { try {
$this->mailer()->Subject = $mail_subject; $this->mailer()->Subject = $mail_subject;
$this->mailer()->AltBody = $mail_body; $this->mailer()->AltBody = $mail_body;

View File

@@ -9,6 +9,7 @@
<testsuite name="froxlor"> <testsuite name="froxlor">
<!-- we need to specify the order of the tests for dependency-reasons --> <!-- we need to specify the order of the tests for dependency-reasons -->
<directory>tests/Global</directory> <directory>tests/Global</directory>
<directory>tests/Froxlor</directory>
<directory>tests/Admins</directory> <directory>tests/Admins</directory>
<directory>tests/Customers</directory> <directory>tests/Customers</directory>
<directory>tests/IpsAndPorts</directory> <directory>tests/IpsAndPorts</directory>

View File

@@ -275,15 +275,15 @@ $header
<td><a href="$lookfornewversion_link">$lookfornewversion_lable</a></td> <td><a href="$lookfornewversion_link">$lookfornewversion_lable</a></td>
</if> </if>
</tr> </tr>
<if $isnewerversion == 1 > <if $lookfornewversion_message != ''>
<tr> <tr>
<td colspan="2"><strong>{$lng['admin']['newerversionavailable']}</strong></td> <td colspan="2"><strong>$lookfornewversion_message</strong></td>
</tr>
</if>
<if $lookfornewversion_addinfo != ''>
<tr>
<td colspan="2">$lookfornewversion_addinfo</td>
</tr> </tr>
<if $lookfornewversion_addinfo != ''>
<tr>
<td colspan="2">$lookfornewversion_addinfo</td>
</tr>
</if>
</if> </if>
</tbody> </tbody>
</table> </table>
@@ -292,4 +292,3 @@ $header
</article> </article>
$footer $footer

View File

@@ -0,0 +1,20 @@
<?php
use PHPUnit\Framework\TestCase;
/**
*
* @covers Froxlor
*/
class FroxlorTest extends TestCase
{
public function testFroxlorcheckUpdate()
{
global $admin_userdata;
$json_result = Froxlor::getLocal($admin_userdata)->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']);
}
}