Last active
October 5, 2022 05:31
-
-
Save Commifreak/6e17c379805dcca66d3c293999eac8e0 to your computer and use it in GitHub Desktop.
Alcatel WBM API Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: Robin | |
* Date: 09.04.2018 | |
* Time: 14:23 | |
*/ | |
class HTTPAPI | |
{ | |
/** | |
* @var string IP of the OXE Web-UI | |
*/ | |
public $oxeIP = "172.28.0.10"; | |
/** | |
* @var bool Use API 1.0? | |
*/ | |
public $useApiVer1dot0 = true; | |
/** | |
* @var string Username - defaults to mtcl | |
*/ | |
public $username = "mtcl"; | |
public $password = ""; | |
/** | |
* @var bool If the Web-UI has no valid cert, this should be set to true | |
*/ | |
public $ignoreSSLErrors = false; | |
public $lastError; | |
public $node = 101; | |
private $loggedIn = false; | |
private $ch = false; | |
private $_token; | |
//NOE_B_IP_8058s | |
private static $_sets = [ | |
'NOE_B' => [ | |
'setName' => '4029', | |
'progKeysPerPage' => 4, | |
'progKeyMaxEntry' => 72, | |
'ip' => false, | |
], | |
'NOE_C' => [ | |
'setName' => '4039', | |
'progKeysPerPage' => 8, | |
'progKeyMaxEntry' => 72, | |
'ip' => false, | |
], | |
'NOE_B_IP_8028s' => [ | |
'setName' => '8028s', | |
'progKeysPerPage' => 4, | |
'progKeyMaxEntry' => 68, | |
'ip' => true, | |
], | |
'NOE_B_8029' => [ | |
'setName' => '8029s', | |
'progKeysPerPage' => 4, | |
'progKeyMaxEntry' => 68, | |
'ip' => false, | |
], | |
'NOE_B_IP_8058s' => [ | |
'setName' => '8058s', | |
'progKeysPerPage' => 4, | |
'progKeyMaxEntry' => 68, | |
'ip' => true, | |
], | |
'NOE_C_IP_8038' => [ | |
'setName' => '8038', | |
'progKeysPerPage' => 8, | |
'progKeyMaxEntry' => 72, | |
'ip' => true, | |
], | |
'NOE_C_COLOR_IP_8068s' => [ | |
'setName' => '8068s', | |
'progKeysPerPage' => 8, | |
'progKeyMaxEntry' => 72, | |
'ip' => true, | |
], | |
'NOE_C_COLOR_IP_8078s' => [ | |
'setName' => '8078s', | |
'progKeysPerPage' => 8, | |
'progKeyMaxEntry' => 72, | |
'ip' => true, | |
], | |
'UA_VVLE_3G' => ['setName' => '4004', 'ip' => false], | |
'ANALOG' => ['setName' => 'Analog', 'ip' => false], | |
'GAP_HANDSET' => ['setName' => 'Handtelefon', 'ip' => false], | |
'SIP_Extension' => [ | |
'setName' => 'SIPExt', | |
'progKeysPerPage' => 1, | |
'progKeyMaxEntry' => 10, | |
'ip' => true, | |
], | |
]; | |
private static $_forwardTypes = [ | |
'NO_RV' => 'Keine Rufumleitung', | |
'RV_OC_OR_NON_REP' => 'Rufumleitung bei belegt und bei keiner Antwort', | |
'RV_IMD' => 'Rufumleitung sofort', | |
'RV_SU_OCC' => 'Rufumleitung sofort bei besetzt', | |
'RV_SU_NON_REP' => 'Rufumleitung wenn keine Antwort', | |
]; | |
/** | |
* @param $action Action to do | |
* @param array $attributes The Attributes to GET back | |
* @param array $parameters Parameters to send with their values | |
* @param array $additionalHeaders | |
* @param bool $returnHeaders | |
* | |
* @return array|bool|mixed | |
*/ | |
private function _send($action, $attributes = [], $parameters = [], $additionalHeaders = [], $returnHeaders = false) | |
{ | |
$headers = []; | |
// Default cURL config | |
$this->ch = curl_init(); | |
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true); | |
if ($this->useApiVer1dot0) { | |
$apiVer = "1.0"; | |
} | |
if (!empty($attributes)) { | |
$attributes = "?attributes=" . implode(',', $attributes); | |
} else { | |
$attributes = ''; | |
} | |
curl_setopt($this->ch, CURLOPT_URL, | |
"https://" . $this->oxeIP . "/api/mgt/" . $apiVer . "/" . $action . $attributes); | |
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true); | |
if ($this->ignoreSSLErrors) { | |
curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false); | |
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false); | |
} | |
if ($returnHeaders) { | |
curl_setopt($this->ch, CURLOPT_HEADER, true); | |
} | |
if (!empty($this->_token)) { | |
$headers[] = 'Authorization: Bearer ' . $this->_token; | |
} | |
if (!empty($additionalHeaders)) { | |
$headers = array_merge($headers, $additionalHeaders); | |
} | |
if (!empty($parameters)) { | |
// Build up PUT request! | |
$data_json = json_encode($parameters, JSON_UNESCAPED_UNICODE); | |
$headers = array_merge($headers, | |
['Content-Type: application/json', 'Content-Length: ' . strlen($data_json)]); | |
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'PUT'); | |
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $data_json); | |
} | |
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers); | |
$response = curl_exec($this->ch); | |
$http_code = curl_getinfo($this->ch, CURLINFO_HTTP_CODE); | |
if ($action != 'login' && $http_code != 200) { | |
$this->lastError = $response; | |
return false; | |
} | |
if ($response === false) { | |
$this->lastError = curl_error($this->ch); | |
return false; | |
} | |
if ($returnHeaders) { | |
$header_size = curl_getinfo($this->ch, CURLINFO_HEADER_SIZE); | |
$header = substr($response, 0, $header_size); | |
$body = substr($response, $header_size); | |
return [$response, $body, $header]; | |
} | |
return empty($response) && $response !== false ? true : $response; | |
} | |
public function debug() | |
{ | |
return [ | |
'token' => $this->_token, | |
'lastErr' => $this->lastError, | |
'ch' => $this->ch, | |
]; | |
} | |
public function login() | |
{ | |
if (!$this->loggedIn) { | |
$response = $this->_send('login', false, false, | |
['Authorization: Basic ' . base64_encode($this->username . ':' . $this->password)]); | |
if ($response) { | |
$this->_token = json_decode($response, true)['token']; | |
return true; | |
} else { | |
$this->lastError = "Login to TK-API failed because: " . $this->lastError; | |
return false; | |
} | |
} else { | |
return true; | |
} | |
} | |
public function signOff($dirNum) | |
{ | |
$data = $this->_send('Node/' . $this->node . '/Subscriber/' . $dirNum); | |
if ($data) { | |
$data = json_decode($data); | |
if (isset(self::$_sets[$data->Station_Type]) && self::$_sets[$data->Station_Type]['ip'] == true) { | |
// Method: TSC IP Subscriber: Delete Ethernet address! | |
$data = $this->_send('Node/' . $this->node . '/Subscriber/' . $dirNum . '/Tsc_IP_subscriber/' . $dirNum, | |
false, ['Ethernet_Address' => '']); | |
if ($data) { | |
return true; | |
} else { | |
return false; | |
} | |
} else { | |
$this->lastError = 'Not supported!'; | |
} | |
} else { | |
return false; | |
} | |
} | |
public function getSetType($dirNum) | |
{ | |
$data = $this->getSubscriberData($dirNum); | |
if ($data) { | |
if (array_key_exists($data->Station_Type, self::$_sets)) { | |
return self::$_sets[$data->Station_Type]; | |
} else { | |
return false; | |
} | |
} | |
} | |
/** | |
* @param $type string internal name of the set | |
* | |
* @return string Nice name or internal name (when not found) | |
*/ | |
public static function getSetName($type) | |
{ | |
if (array_key_exists($type, self::$_sets)) { | |
return self::$_sets[$type]['setName']; | |
} else { | |
return $type; | |
} | |
} | |
public function getProgKeys($dirNum) | |
{ | |
$data = $this->_send('Node/' . $this->node . '/Subscriber/' . $dirNum . '/Subscriber_Keyboard/', | |
['UTF8_Mnemonic1', 'Content', 'Type', 'Locked', 'Directory_Number']); | |
if ($data) { | |
return $data; | |
} | |
} | |
public function getProgKey($dirNum, $key) | |
{ | |
$data = $this->_send('Node/' . $this->node . '/Subscriber/' . $dirNum . '/Subscriber_Keyboard/' . $key, | |
['UTF8_Mnemonic1', 'Content', 'Type', 'Locked']); | |
if ($data) { | |
return $data; | |
} | |
} | |
public function setProgKey($dirNum, $key, $content, $name) | |
{ | |
if (empty($key)) { | |
// SOnderlösung SIP Extensions | |
return true; | |
} | |
if (empty($name) || $name == 'null' || empty($content) || $content == 'null') { | |
$data = $this->_send('Node/' . $this->node . '/Subscriber/' . $dirNum . '/Subscriber_Keyboard/' . $key, | |
false, ['Type' => 'Not_Assigned']); | |
} else { | |
$data = $this->_send('Node/' . $this->node . '/Subscriber/' . $dirNum . '/Subscriber_Keyboard/' . $key, | |
false, [ | |
'Type' => 'Programmed', | |
'UTF8_Mnemonic1' => $name, | |
'Key_NOE_Mnemonic1' => $name, | |
'Content' => $content, | |
]); | |
} | |
if ($data) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
/** | |
* @param $dirNum | |
* @param $key | |
* @param $directoryNumber | |
* @param $name | |
* @param string $ringingMode No_Ring, Short_Ring, Short_Ring_Without_overring, Long_Ring, | |
* Long_Ring_Without_overring | |
* | |
* @return bool | |
*/ | |
public function setStationSupervision($dirNum, $key, $directoryNumber, $name, $ringingMode = 'No_Ring') | |
{ | |
if (empty($name) || $name == 'null' || empty($directoryNumber) || $directoryNumber == 'null') { | |
$data = $this->_send('Node/' . $this->node . '/Subscriber/' . $dirNum . '/Subscriber_Keyboard/' . $key, | |
false, ['Type' => 'Not_Assigned']); | |
} else { | |
$data = $this->_send('Node/' . $this->node . '/Subscriber/' . $dirNum . '/Subscriber_Keyboard/' . $key, | |
false, [ | |
'Type' => 'Station_Supervision', | |
'UTF8_Mnemonic1' => $name, | |
'Key_NOE_Mnemonic1' => $name, | |
'Directory_Number' => $directoryNumber, | |
'Ringing_Mode' => $ringingMode, | |
]); | |
} | |
if ($data) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public function getSubscriberData($dirNum) | |
{ | |
$data = $this->_send('Node/' . $this->node . '/Subscriber/' . $dirNum); | |
if ($data) { | |
$data = json_decode($data); | |
return $data; | |
} else { | |
return false; | |
} | |
} | |
public function setSubscriberData($dirNum, $parameters) | |
{ | |
$data = $this->_send('Node/' . $this->node . '/Subscriber/' . $dirNum, false, $parameters); | |
if ($data) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public function getSubscriberList($attributes, $json = true) | |
{ | |
if (empty($attributes)) { | |
return false; | |
} | |
$data = $this->_send('Node/' . $this->node . '/Subscriber/', $attributes); | |
if ($data) { | |
$data = $json ? $data : json_decode($data); | |
return $data; | |
} else { | |
return false; | |
} | |
} | |
public function getAdvancedConfigurationTscIP($dirNum, $attributes = [], $json = true) | |
{ | |
$data = $this->_send('Node/' . $this->node . '/Subscriber/' . $dirNum . '/Tsc_IP_subscriber/' . $dirNum, | |
$attributes); | |
if ($data) { | |
$data = $json ? $data : json_decode($data); | |
return $data; | |
} else { | |
return false; | |
} | |
} | |
public function getAttendantTscIP($dirNum, $attributes = [], $json = true) | |
{ | |
$data = $this->_send('Node/' . $this->node . '/Attendant/1/Attendant_Console/' . $dirNum . '/Tsc_IP_Attendant/' . $dirNum, | |
$attributes); | |
if ($data) { | |
$data = $json ? $data : json_decode($data); | |
return $data; | |
} else { | |
return false; | |
} | |
} | |
public function setAttendantTscIP($dirNum, $attributes = [], $parameters, $json = true) | |
{ | |
$data = $this->_send('Node/' . $this->node . '/Attendant/1/Attendant_Console/' . $dirNum . '/Tsc_IP_Attendant/' . $dirNum, | |
$attributes, $parameters); | |
if ($data) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
/** | |
* Gibt Status zurück (Rufumleitung, DND, Telefon sperren) | |
* | |
* @param $dirNum string Telefonnummer | |
*/ | |
public function getDynamicStateSubscriber($dirNum) | |
{ | |
$data = $this->_send('Node/' . $this->node . '/Subscriber/' . $dirNum . '/Dynamic_State_Subscriber/1'); | |
if ($data) { | |
$data = json_decode($data); | |
return $data; | |
} | |
} | |
/** | |
* @param $dirNum string Telefonnummer | |
* @param $parameters array Parameter - Für DND: "Do_Not_Disturb: true/false" - Für Appereat sperren: Padlock: | |
* true/false - Für Umleitung: Forward: NO_RV (Keine Rul.)|RV_OC_OR_NON_REP (Rul. bei bel und k. | |
* ANtw.) | RV_IMD (Rul. Sofort) | RV_SU_OCC (Rul. Sof. bei besetzt) | RV_SU_NON_REP (Rul. wenn | |
* keine ANtwort) | |
* | |
* @return bool | |
*/ | |
public function setDynamicStateSubscriber($dirNum, $parameters) | |
{ | |
$data = $this->_send('Node/' . $this->node . '/Subscriber/' . $dirNum . '/Dynamic_State_Subscriber/1', false, | |
$parameters); | |
if ($data) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public function close() | |
{ | |
curl_close($this->ch); | |
} | |
public static function getForwardType($type = false) | |
{ | |
if ($type) { | |
return static::$_forwardTypes[$type]; | |
} else { | |
return static::$_forwardTypes; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment