From d341ba0cc7a1a491b9f370b5f866682e78f9ac21 Mon Sep 17 00:00:00 2001 From: simon golpe Date: Tue, 6 Feb 2024 10:49:34 +0100 Subject: [PATCH] avoid duplicated code --- SnomDeskphone/module.php | 74 ++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 41 deletions(-) diff --git a/SnomDeskphone/module.php b/SnomDeskphone/module.php index 23f70da..fbf4e6c 100644 --- a/SnomDeskphone/module.php +++ b/SnomDeskphone/module.php @@ -191,16 +191,8 @@ public function fkeysAreUnique(array $fkeys_settings): bool public function GetConfigurationForm(): string { $data = json_decode(file_get_contents(__DIR__ . "/form.json"), true); - $device_info = array( - "is snom phone" => false, - "mac address" => '00:04:13:', - "phone model" => '', - ); $phone_ip = $this->ReadPropertyString("PhoneIP"); - - if ($phone_ip and Sys_Ping($phone_ip, 2000)) { - $device_info = $this->getDeviceInformation(); - } + $device_info = $this->getDeviceInformation(); $data["elements"][3]["value"] = $device_info['mac address']; $data["elements"][4]["value"] = $device_info['phone model']; @@ -266,39 +258,36 @@ public function GetConfigurationForm(): string public function getDeviceInformation(): array { - $mac_address = $this->getMacAddress(); + $phoneIp = $this->ReadPropertyString("PhoneIP"); + $deviceInfo = array( + "is snom phone" => false, + "mac address" => '00:04:13:', + "phone model" => '', + ); - if (str_contains($mac_address, '00:04:13:') or str_contains($mac_address, '0:4:13:')) { // for MacOS '0:4:13:' - $phone_ip = $this->ReadPropertyString("PhoneIP"); - $protocol = $this->ReadPropertyString("Protocol"); - $url = "$protocol://$phone_ip/settings.xml"; - $response = $this->httpGetRequest($url, headerOutput: false); - $phone_settings_xml = @simplexml_load_string($response); - - if ($phone_settings_xml) { - $phone_model = (string) $phone_settings_xml->{'phone-settings'}->phone_type[0]; - $this->SetValue('PhoneModel', $phone_model); - $this->SetValue('PhoneMac', $mac_address); - - return array( - "is snom phone" => true, - "mac address" => $mac_address, - "phone model" => $phone_model, - ); - } else { - return array( - "is snom phone" => true, - "mac address" => '00:04:13:', - "phone model" => '', - ); + if ($phoneIp and Sys_Ping($phoneIp, 2000)) { + $macAddress = $this->getMacAddress(); + + if (str_contains($macAddress, '00:04:13:') or str_contains($macAddress, '0:4:13:')) { // for MacOS '0:4:13:' + $phoneIp = $this->ReadPropertyString("PhoneIP"); + $protocol = $this->ReadPropertyString("Protocol"); + $url = "$protocol://$phoneIp/settings.xml"; + $response = $this->httpGetRequest($url, return_message: true, headerOutput: false); + $phoneSettings = @simplexml_load_string($response); + + if ($phoneSettings) { + $phoneModel = (string) $phoneSettings->{'phone-settings'}->phone_type[0]; + $this->SetValue('PhoneModel', $phoneModel); + $this->SetValue('PhoneMac', $macAddress); + $deviceInfo["is snom phone"] = true; + $deviceInfo["mac address"] = $macAddress; + $deviceInfo["phone model"] = $phoneModel; + } else { + $deviceInfo["is snom phone"] = true; + } } - } else { - return array( - "is snom phone" => false, - "mac address" => '00:04:13:', - "phone model" => '', - ); } + return $deviceInfo; } public function getMacAddress(): string @@ -382,7 +371,7 @@ public function httpGetRequest(string $url, bool $return_message = false, bool $ $message = "Login failed"; } } else { - $message = "$http_code $response"; + $message = ""; } break; case 303: @@ -406,7 +395,10 @@ public function httpGetRequest(string $url, bool $return_message = false, bool $ curl_close($handler); - return $message; + if ($message) { + return $message; + } + return $response; } curl_close($handler);