From cc3e8057d85415e23a2026c4bc2485c61f1ef5ec Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Sun, 13 Oct 2019 20:04:22 +0200 Subject: [PATCH] added getConfiguredScriptServers() --- lam/lib/config.inc | 49 +++++++++++++++++++++------ lam/lib/remote.inc | 59 +++++++++++++++++++++++++++++++++ lam/tests/lib/LAMConfigTest.php | 24 +++++++++++++- 3 files changed, 121 insertions(+), 11 deletions(-) diff --git a/lam/lib/config.inc b/lam/lib/config.inc index 9d4c3109..16e27067 100644 --- a/lam/lib/config.inc +++ b/lam/lib/config.inc @@ -1,5 +1,6 @@ scriptServer; } + /** + * Returns the list of configureed servers. + * + * @return RemoteServerConfiguration[] servers + */ + public function getConfiguredScriptServers() { + $servers = array(); + if (empty($this->scriptServer)) { + return $servers; + } + $serverChunks = explode(';', $this->scriptServer); + foreach ($serverChunks as $serverChunk) { + if (empty($serverChunk)) { + continue; + } + $serverSettings = explode(':', $serverChunk); + $serverName = $serverSettings[0]; + $serverLabel = isset($serverSettings[1]) ? $serverSettings[1] : null; + $serverHomedirPrefix = isset($serverSettings[2]) ? $serverSettings[2] : null; + $servers[] = new RemoteServerConfiguration($serverName, $serverLabel, $serverHomedirPrefix); + } + return $servers; + } + /** * Sets the servers of the external script * @@ -1470,21 +1495,25 @@ class LAMConfig { // Explode name and IP, if a name exists if (preg_match("/:/", $arr_value)) { $arr_value_explode = explode(":", $arr_value); - $ip = $arr_value_explode[1]; $servername = $arr_value_explode[0]; + $label = $arr_value_explode[1]; + $homedirPrefix = isset($arr_value_explode[2]) ? $arr_value_explode[2] : ''; } else { - $ip = $arr_value; - $servername = ""; + $servername = $arr_value; + $label = ''; + $homedirPrefix = ''; } - if (isset($ip) && is_string($ip) && preg_match("/^[a-z0-9-]+(\\.[a-z0-9-]+)*(,[0-9]+)?$/i", $ip)) { - // Check if the IP has a server name - if (!empty($servername)) { - $valid_ips[] = $servername.":".$ip; + if (isset($servername) && is_string($servername) && preg_match("/^[a-z0-9-]+(\\.[a-z0-9-]+)*(,[0-9]+)?$/i", $servername)) { + $serverData = array($servername); + if (!empty($label)) { + $serverData[] = $label; } - else { - $valid_ips[] = $ip; + if (!empty($homedirPrefix)) { + $serverData[] = $homedirPrefix; } + $serverChunk = implode(':', $serverData); + $valid_ips[] = $serverChunk; } else { // wrong format @@ -1492,7 +1521,7 @@ class LAMConfig { } } // Check that the array is not empty - if ($array_string > 0) { + if (!empty($array_string)) { $this->scriptServer = implode(";", $valid_ips); return true; } diff --git a/lam/lib/remote.inc b/lam/lib/remote.inc index 9190d7fd..eb71aa14 100644 --- a/lam/lib/remote.inc +++ b/lam/lib/remote.inc @@ -174,4 +174,63 @@ class Remote { } +/** + * Server configuration. + * + * @author Roland Gruber + */ +class RemoteServerConfiguration { + + private $server; + private $label; + private $homeDirPrefix; + + /** + * Constructor + * + * @param string $server server DNS name + * @param string $label label for GUI + * @param string $homeDirPrefix prefix for home directories + */ + public function __construct($server, $label, $homeDirPrefix) { + $this->server = $server; + $this->label = $label; + if (empty($label)) { + $this->label = $server; + } + $this->homeDirPrefix = $homeDirPrefix; + if (empty($homeDirPrefix)) { + $this->homeDirPrefix = ''; + } + } + + /** + * Returns the server's DNS name. + * + * @return string server name + */ + public function getServer() { + return $this->server; + } + + /** + * Returns a descriptive label. + * + * @return string label + */ + public function getLabel() { + return $this->label; + } + + /** + * Returns the prefix for user home directories. + * + * @return string prefix for user home directories + */ + public function getHomeDirPrefix() { + return $this->homeDirPrefix; + } + +} + ?> diff --git a/lam/tests/lib/LAMConfigTest.php b/lam/tests/lib/LAMConfigTest.php index 58e94bf5..6d0c2738 100644 --- a/lam/tests/lib/LAMConfigTest.php +++ b/lam/tests/lib/LAMConfigTest.php @@ -279,12 +279,34 @@ class LAMConfigTest extends PHPUnit_Framework_TestCase { public function testscriptServers() { $this->assertFalse($this->lAMConfig->set_scriptServers(';;..')); $val = 'server;server'; - $this->lAMConfig->set_scriptServers($val); + $this->assertTrue($this->lAMConfig->set_scriptServers($val)); $this->assertEquals($val, $this->lAMConfig->get_scriptServers()); $this->doSave(); $this->assertEquals($val, $this->lAMConfig->get_scriptServers()); } + /** + * Tests LAMConfig->getConfiguredScriptServers(). + */ + public function testGetConfiguredScriptServers() { + $val = 'server1;server2:label2;server3:label3:/prefix'; + $this->assertTrue($this->lAMConfig->set_scriptServers($val)); + $servers = $this->lAMConfig->getConfiguredScriptServers(); + $this->assertEquals(3, sizeof($servers)); + $this->doSave(); + $servers = $this->lAMConfig->getConfiguredScriptServers(); + $this->assertEquals(3, sizeof($servers)); + $this->assertEquals('server1', $servers[0]->getServer()); + $this->assertEquals('server2', $servers[1]->getServer()); + $this->assertEquals('server3', $servers[2]->getServer()); + $this->assertEquals('server1', $servers[0]->getLabel()); + $this->assertEquals('label2', $servers[1]->getLabel()); + $this->assertEquals('label3', $servers[2]->getLabel()); + $this->assertEquals('', $servers[0]->getHomeDirPrefix()); + $this->assertEquals('', $servers[1]->getHomeDirPrefix()); + $this->assertEquals('/prefix', $servers[2]->getHomeDirPrefix()); + } + /** * Tests LAMConfig->get_scriptRights() and LAMConfig->set_scriptRights() */