<?php

require_once dirname(__FILE__) . '/../../lib/phoneprovisioning.php';
require_once "snom.php";
require_once "TimezoneHelper.php";

function snom_prov_error_handler($type, $msg, $file, $line)
{
    Log::e($type . " " . $msg . " " . $file . ":" . $line, "snom-dls");
}
set_error_handler('snom_prov_error_handler');

class SnomPhoneProvisioning extends VirtualPhoneProvisioningController
{

    const LOG_TAG = 'snom-dls';

    private $sipAccount = null;

    protected function getProvURL($mac = null)
    {
        $url = parent::getProvURL();
        $url = $url . "{mac}.xml";
        return $url;
    }

    protected function getPhonebookURL()
    {
        $url = parent::getProvURL();
        $url = $url . "pb/$this->mMacAddress.xml";
        return $url;
    }

    protected function getLoginAppURL($mac)
    {
        $url = parent::getProvURL();
        $url .= "login/" . $mac . ".xml";
        return $url;
    }

    protected function getSnomLanguageSettingUrl($mac, $type)
    {
        $url = parent::getProvURL();
        $url .= "langSettings-$mac/$type.xml";
        return $url;
    }

    protected function getSnomWebLanguageSettingsFileURL($mac, $type, $file)
    {
        $phone = Phones::model()->findByAttributes(array("mac_addr" => $mac));
        if (isset($phone->type)) {
            if (startsWith($phone->type, 'snomD120')) {
                $file = '10_1/' . $file;
            }
        }
        $url = parent::getProvURL();
        $url .= "getLanguage-$mac/$type/$file";
        return $url;
    }

    protected function getConfigAppURL()
    {
        $url = parent::getProvURL();
        $url = $url . "cfgapp/$this->mMacAddress.xml";
        return $url;
    }

    protected function checkMacAddress($mac)
    {
        return parent::checkMacAddress($mac);
    }

    protected function checkUserAgent($ua)
    {
        //$ua = "Mozilla/4.0 (compatible; snom870-SIP 8.7.3.15 SPEAr300 SNOM 1.4)";

        if (preg_match("/snom715-SIP\s+(\d+\.\d+\.\d+.\d+ )(\d.+\d-{1})/", $ua)) {
            $ua = str_replace('snom715', 'snomD715', $ua);  // needed because SNOM uses 'snom715' instead of 'snomD715' in Snom D715 user-agent
        }

        $known_models = Snom::getSupportedPhoneModels();
        Log::d($ua . " contacs us", self::LOG_TAG);

        $matches = preg_match('/snom[D0-9PA]{3,4}/i', $ua, $model);
        $model = $model[0];

        if (!array_key_exists($model, $known_models)) {
            return false;
        }

        preg_match('/snom[D0-9PA]{3,4}-SIP\s+(\d+\.\d+\.\d+.\d+)/', $ua, $fw);
        $fw = $fw[1];

        $vParts = explode(".", $fw);
        $vPartsNormal = array();
        $vPartsNormal[] = str_pad($vParts[0], 2, '0', STR_PAD_LEFT);
        $vPartsNormal[] = str_pad($vParts[1], 2, '0', STR_PAD_LEFT);
        $vPartsNormal[] = str_pad($vParts[2], 2, '0', STR_PAD_LEFT);
        if (count($vParts) > 3) {
            $vPartsNormal[] = str_pad($vParts[3], 2, '0', STR_PAD_LEFT);
        } else {
            $vPartsNormal[] = "00";
        }

        $firmwareVersion = implode(".", $vPartsNormal);

        $this->setCurrentPhoneModel($known_models[$model]);
        $this->setCurrentFirmwareVersion($firmwareVersion);
        return true;
    }

    protected function runFirmwareUpdate(Firmware $fwUpdate)
    {
        // Log::d(print_r($_REQUEST, true), self::LOG_TAG);

        if (array_key_exists("firmwareDL", $_REQUEST) && @$_REQUEST["firmwareDL"] == "true") { // this is the second request of the phone after we had said that he have to upgrade the FW...
            //<firmware perm="">
            // http://domain/3x0/snom3x0-7.X-SIP-f.bin
            //</firmware>
            $this->setting("firmware-settings", "firmware", "http://" . cfg('PROV_HOST') . ":" . cfg('PROV_PORT') . "/firmware/" . $fwUpdate->fileName, $perm = "");
        } else { // First request which need to know where to get some Firmware...
            $this->psetting('firmware_status', "http://" . cfg('PROV_HOST') . ":" . cfg('PROV_PORT') . "/{mac}.xml?firmwareDL=true", "");
        }
        return false;
    }

    protected function tzOffsetToSnomTz($offset)
    {
        $tz = array();
        $tz["-10"] = "USA-10";
        $tz["-9"] = "USA-9";
        $tz["-8"] = "USA-8";
        $tz["-7"] = "CAN-7";
        $tz["-6"] = "CAN-6";
        $tz["-5"] = "CAN-5";
        $tz["-4.5"] = "VEN-4.5";
        $tz["-4"] = "CAN-4";
        $tz["-3"] = "GRL-3";
        $tz["-3.5"] = "CAN-3.5";
        $tz["-2"] = "BRA-2";
        $tz["-1"] = "PRT-1";
        $tz["0"] = "GBR-0";
        $tz["1"] = "GER+1";
        $tz["2"] = "FIN+2";
        $tz["3"] = "RUS+3";
        $tz["3.5"] = "IRN+3.5";
        $tz["4"] = "RUS+4";
        $tz["5"] = "RUS+5";
        $tz["6"] = "RUS+6";
        $tz["7"] = "RUS+7";
        $tz["8"] = "KOR+8";
        $tz["9"] = "JPN+9";
        $tz["9.5"] = "AUS+9.5";
        $tz["10"] = "AUS+10";
        $tz["10.5"] = "AUS+10.5";
        $tz["11"] = "NCL+11";
        $tz["12"] = "NZL+12";
        $tz["12.75"] = "NZL+12.75";
        $tz["13"] = "TON+13";

        if (array_key_exists($offset, $tz)) {
            return $tz[$offset];
        }
        return "GBR-0";
    }

    protected function loadConfigValues($mac = NULL)
    {
        $this->getTemplate(); //loads the template into the current Settings Array

        $Phone = $this->getPhoneModel();
        $phoneType = $Phone->type;
        $SipAccount = $Phone->getSipAccount();
        $this->sipAccount = $SipAccount;
        $User = $SipAccount->getUser();
        $callwaiting = "off";
        $buttonsound = "off";
        if ($User && $User->call_waiting) {
            $callwaiting = "on";
        }

        if ($User && $User->key_tone) {
            $buttonsound = "on";
        }

        $srtp = "off";
        if (cfg("RTP_ENABLE_SRTP") == "1") {
            $srtp = "on";
        }
        $this->psetting('user_srtp', $srtp, "R", '1');
        $this->psetting('user_auth_tag', 'off', "", "1"); // on => AES-32 | off => AES-80

        $this->psetting('user_host', $SipAccount->getSIPRegistrar(), "R", "1");

        // Set Outbound proxy to use TLS if available
        $user_outbound_proxy = $SipAccount->getSIPRegistrar();
        if (cfg("INTERNAL_SSL_ENABLE") == "1") {
            $user_outbound_proxy .= ":" . cfg("INTERNAL_TLS_PORT") . ";transport=tls";
        } else {
            $user_outbound_proxy .= ":" . cfg("INTERNAL_SIP_PORT");
        }
        $this->psetting('user_outbound', $user_outbound_proxy, "R", "1");

        $this->psetting('network_id_port', cfg("INTERNAL_SIP_PORT"), "R");
        $this->psetting('user_name', $SipAccount->authname, "R", "1");
        //$this->psetting('user_realname', $SipAccount->getLabel(), "R", "1");
        $this->psetting('user_pass', $SipAccount->secret, "R", "1");
        $this->psetting('user_mailbox', $SipAccount->authname, "R", "1");
        $this->psetting('user_expiry', $SipAccount->expires, "RW", "1");
        $this->psetting('user_pname', $SipAccount->authname, "R", "1");

        if ($phoneType == "snomPA1") {
            $this->psetting('user_auto_connect', 'on', "RW", "1");
        }

        $label = $SipAccount->getLabel();
        if ($User) {
            if ($User->defaultCallForwardSet != $User->callForwardSet) {
                $preset = ForwardPreset::model()->findByPk($User->callForwardSet);
                if ($preset != NULL) {
                    $label = "[" . $SipAccount->authname . "] " . $preset->name;
                } else {
                    $label = "FWD?";
                }
            }
        }
        $this->psetting('user_realname', $label, "R", "1");

        $this->psetting('dkey_retrieve', 'speed voicemail');
        $this->psetting('setting_server', $this->getProvURL(), "RW");

        // XML - Login
        if (!$User) {
            $this->psetting('dkey_snom', "url " . $this->getLoginAppURL($Phone->mac_addr), "R");
        } else {
            $this->psetting('dkey_snom', "keyevent", "RW");
        }

        //user specific settings like sounds etc.
        $this->psetting('call_waiting', $callwaiting, "RW");
        $this->psetting('keytones', $buttonsound, "RW");

        $this->psetting('ntp_server', cfg("NTP_HOST"), "RW");

        // zone zone_desc +1 
        $tz = cfg("TIMEZONE");
        if ($User && $User->user_timezone != "") {
            $tz = $User->user_timezone;
        }

        $tz_offset = get_timezone_offset("UTC", $tz, true);

        // $this->psetting("utc_offset",$tz_offset*60*60, "RW");
        $this->psetting("timezone", $this->tzOffsetToSnomTz($tz_offset), "RW");

        // date_us_format
        $dateformat = cfg("USER_DATE_FORMAT");
        if ($User && $User->user_date_format != "") {
            $dateformat = $User->user_date_format;
        }

        $date_us_format = "yes";
        if ($dateformat == "dd.MM.yyyy") {
            $date_us_format = "no";
        }
        $this->psetting('date_us_format', $date_us_format, "RW");

        // time_24_format
        $time_format = cfg("USER_TIME_FORMAT");
        if ($User && $User->user_time_format != "") {
            $time_format = $User->user_time_format;
        }

        if ($time_format == "24") {
            $this->psetting('time_24_format', "yes");
        } else {
            $this->psetting('time_24_format', "no");
        }

        // XML-Phonebook
        $this->psetting('dkey_directory', 'keyevent F_ADR_BOOK', "");
        if ($User) {
            $this->psetting('dkey_directory', "url " . $this->getPhonebookURL(), "R");
        }

        // XML-ConfigApp
        $this->psetting('dkey_transfer', 'keyevent F_TRANSFER', "");
        if ($User && !startsWith($phoneType, 'snomD375')) {
            $this->psetting('dkey_transfer', "url " . $this->getConfigAppURL(), "R");
        }

        $this->psetting('user_dtmf_info', 'sip_info_only', "RW", "1");

        $this->psetting('show_call_status', 'off');
        $this->psetting('advertisement', 'off');


        if (startsWith($phoneType, 'snom300') || startsWith($phoneType, 'snom360') || startsWith($phoneType, 'snomD120')) {
            $this->getLanguageSettings($mac, 'gui-languages', 1);
            $this->getLanguageSettings($mac, 'web-languages', 2);
        }

        $this->psetting('language', $this->getUserLanguage($User, $phoneType));
        $this->psetting('web_language', $this->getUserLanguage($User, $phoneType));

        $this->psetting('ipv4_conflict_detection', 'detect_defend');

        $this->setVolumeSettings($User);
        $this->setAudioCodecs();

        $this->processSoftkeys();

        $this->changeSettingsWithProvisioningProfile($User, $phoneType);
        return true;
    }

    protected function replaceSettingsWithParameters($params)
    {
        $this->psetting($params['name'], $params['value'], "RW", $params['meta']);
        Log::d("[$this->mMacAddress] Overwriting key from parameter profile: " . $params['name'] . " = " . $params['value'], self::LOG_TAG);
    }

    protected function processSoftkeys()
    {
        $Phone = $this->getPhoneModel();
        $SipAccount = $Phone->getSipAccount();
        $User = $SipAccount->getUser();

        $levels = array("line", "memory", "ext0", "usb0");
        foreach ($levels as $level) {
            $softKeys = $Phone->getCurrentPhoneKeys($User, $level);
            foreach ($softKeys as $softKey) {
                $this->processSoftKey($softKey);
            }
        }
    }

    protected function processSoftKey(PhoneKey $key)
    {
        if ($key->function === 'no') {
            return;
        }
        if (strpos($key->key_idx, 'ext0.') !== false) {
            $key->key_idx = str_replace('ext0.', '', $key->key_idx);
            if ($key->key_idx == $key->name) {
                $key->key_idx = (int) $key->key_idx - 1;
            }
        }

        if (strpos($key->key_idx, 'usb0.') !== false) {
            $key->key_idx = str_replace('usb0.', '', $key->key_idx);
        }

        $keyFunction = $this->convertSoftKeyFunction($key->function, 'blf');
        $function = $this->getSoftKeyFunction($key, $keyFunction);
        Log::d(print_r($key, true), self::LOG_TAG);

        $context = '1';
        $label = $key->label;
        if (empty($key->function)) {
            $context = 'active';
            $label = '';
        }
        $attributes = array('context' => $context, 'label' => $label, 'idx' => $key->key_idx);
        $this->setting('functionKeys', 'fkey', $function, 'RW', $key->key_idx, $attributes);
    }

    protected function getSoftKeyFunction($key, $keyFunction)
    {
        $function = 'line';
        if (!empty($keyFunction)) {
            if ($keyFunction == "blf") {
                $function = $this->getFormatedBlfKeyFunction($key);
            } else if (startsWith($keyFunction, 'f')) {
                $function = "keyevent " . strtoupper($keyFunction);
            } else {
                $function = $keyFunction . ' ' . $key->number_data;
            }
        }
        return $function;
    }

    protected function getFormatedBlfKeyFunction($key)
    {
        $pickupCode = $this->convertPickupCode($key->function, array('blf'));
        if ($pickupCode != '') {
            $pickupCode = "|$pickupCode";
        }
        $numberData = $this->convertSoftKeyValue($key);
        $function = "blf <sip:" . $numberData . "@" . $this->sipAccount->getSIPRegistrar() . ";user=phone>" . $pickupCode;
        return $function;
    }

    protected function generateOutput()
    {
        $xw = new xmlWriter();
        $xw->openMemory();
        $xw->setIndent(TRUE);
        $xw->startDocument('1.0', 'UTF-8'); // start document

        $xw->startElement("settings");

        //All sections in here, eg. phone-settings, uploads
        foreach ($this->mCurrentSettings as $section_name => $section_values) {

            $xw->startElement($section_name); // start element for current section
            $xw->writeAttribute("e", "2");

            if (is_array($section_values)) { // write childs in element for current section
                foreach ($section_values as $key => $val) {
                    //start element for current key
                    $xw->startElement($val["key"]);
                    //write attributes for current key
                    foreach ($val["attributes"] as $attribute => $attributeValue) {
                        $xw->writeAttribute($attribute, $attributeValue);
                    }
                    //write value (text) for current key
                    $xw->text($val["value"]);
                    //end current key element
                    $xw->endElement();
                    if (array_key_exists("idx", $val) && @$val['idx'] != null) {
                        Log::d("[" . $this->mMacAddress . "] writing(" . $section_name . "): " . $key . " = " . $val["value"] . " IDX: " . @$val['idx'], self::LOG_TAG);
                    } else {
                        Log::d("[" . $this->mMacAddress . "] writing(" . $section_name . "): " . $key . " = " . $val["value"], self::LOG_TAG);
                    }
                }
            }
            $xw->endElement();
        }

        $xw->endElement();
        $xw->endDocument(); // end document

        $xml = $xw->outputMemory();
        Log::d($xml, self::LOG_TAG);
        echo $xml;
        return true;
    }

    protected function _getSupportedPhoneModels()
    {
        return Snom::getSupportedPhoneModels();
    }

    private function readTemplateFile($filename)
    {
        $data = file_get_contents($filename);
        $xml = new SimpleXMLElement($data);
        foreach ($xml as $section => $element) {
            foreach ($element as $key => $value) {
                $attributes = (array) $value->attributes();
                $this->setting($section, $key, strval($value), "RW", null, @$attributes["@attributes"]);
            }
        }
    }

    private function getTemplate()
    {
        // READ general snom template
        $filename = dirname(__FILE__) . "/templates/snom.xml";
        if (file_exists($filename)) {
            $this->readTemplateFile($filename);
        }

        // Read phone specific template if this exists
        $filename = dirname(__FILE__) . "/templates/" . $this->mCurrentPhoneModel->getPhoneModelName() . ".xml";
        if (file_exists($filename)) {
            $this->readTemplateFile($filename);
        }
        return $this->mCurrentSettings;
    }

    protected function psetting($key, $val = '', $perm = "R", $idx = null, $attributes = null)
    {
        $this->setting("phone-settings", $key, $val, $perm, $idx, $attributes);
    }

    protected function setting($section, $key, $val = '', $perm = "R", $idx = null, $attributes = null)
    {
        $value = array();
        $value["attributes"] = array();
        $value["attributes"]["perm"] = $perm;
        if ($idx != null) {
            $value["attributes"]["idx"] = $idx;
        }
        $value["value"] = $val;
        $value["key"] = $key;

        if (is_array($attributes)) {
            $value["attributes"] = array_merge($value["attributes"], $attributes);
        }

        if ($idx == null) {
            $this->mCurrentSettings[$section][$key] = $value;
        } else {
            $this->mCurrentSettings[$section][$key . "-" . $idx] = $value;
        }
    }

    protected function getLanguageSettings($mac, $langFile, $idx)
    {
        $attributes['url'] = $this->getSnomLanguageSettingUrl($mac, $langFile);
        $this->setting("setting-files", "file", "", "", $idx, $attributes);
    }

    public function generateLanguageOutput($mac, $languageType, $getLanguageFile)
    {
        $languageFolder = Yii::app()->basePath . "/../snom/";
        $phoneType = $this->getPhoneTypeByMac($mac);
        if (startsWith($phoneType, 'snomD120')) {
            $languageFolder .= '10_1/';
        }

        $languagesArray = $this->getLanguageFileArray($languageFolder);
        if ($getLanguageFile) {
            if (!empty($_REQUEST['file'])) {
                return $this->generateLanguageFile($_REQUEST['file'], $languageFolder);
            }
        }
        return $this->generateLanguageSettingsFile($mac, $languageType, $languagesArray);
    }

    protected function getLanguageFileArray($languageFolder)
    {
        $languageArray = array();
        $languageFile = '';
        if (file_exists($languageFolder)) {
            $allFiles = scandir($languageFolder);
            foreach ($allFiles as $file) {
                $fileEnding = pathinfo($file, PATHINFO_EXTENSION);
                if ($fileEnding == 'xml') {
                    if (strpos($file, 'web') !== false) {
                        $languageFile = 'web-languages';
                    } else if (strpos($file, 'gui') !== false) {
                        $languageFile = 'gui-languages';
                    }
                    $languageArray[$languageFile][] = $file;
                }
            }
        }
        return $languageArray;
    }

    protected function generateLanguageSettingsFile($mac, $languageType, $languagesArray)
    {
        $xw = new xmlWriter();
        $xw->openMemory();
        $xw->setIndent(TRUE);
        $xw->startDocument('1.0', 'UTF-8'); // start document
        $xw->startElement($languageType);
        $languageName = '';
        $phoneType = $this->getPhoneTypeByMac($mac);

        foreach ($languagesArray[$languageType] as $value) {
            $xw->startElement('language'); // start element for current section
            if (strpos($value, 'DE') !== false) {
                $languageName = $this->convertSnomLanguageCode('de_de', $phoneType);
            } else if (strpos($value, 'EN') !== false) {
                $languageName = $this->convertSnomLanguageCode('en_us', $phoneType);
            } else if (strpos($value, 'FR') !== false) {
                $languageName = $this->convertSnomLanguageCode('fr_fr', $phoneType);
            } else if (strpos($value, 'NL') !== false && startsWith($phoneType, 'snomD120')) {
                $languageName = $this->convertSnomLanguageCode('nl_nl', $phoneType);
            }
            $path = $this->getSnomWebLanguageSettingsFileURL($mac, $languageType, $value);

            $xw->writeAttribute("url", $path);
            $xw->writeAttribute("name", $languageName);
            $xw->endElement();
        }

        $xw->endElement();
        $xw->endDocument(); // end document

        $xml = $xw->outputMemory();
        Log::d($xml, self::LOG_TAG);
        echo $xml;
        return true;
    }

    protected function getPhoneTypeByMac($mac)
    {
        $phone = Phones::model()->findByAttributes(array("mac_addr" => $mac));
        $phoneType = '';
        if (!empty($phone->type)) {
            $phoneType = $phone->type;
        }
        return $phoneType;
    }

    protected function generateLanguageFile($langName, $languageFolder)
    {
        $filename = $languageFolder . $langName;
        if (substr($filename, -4) !== '.xml') {
            $filename .= ".xml";
        }
        $data = file_get_contents($filename);
        header('Content-type: application/xml');
        echo $data;
        exit();
        return true;
    }

    protected function getUserLanguage($User, $phoneType)
    {
        $language = cfg("GUI_LANGS");
        if ($User && $User->user_language != "") {
            $language = $User->user_language;
        }
        return $this->convertSnomLanguageCode($language, $phoneType);
    }

    public function convertSnomLanguageCode($code, $phoneType)
    {
        $codes = array();
        $codes["en_us"] = 'English';
        $codes["de_de"] = 'Deutsch';
        $codes["fr_fr"] = 'Francais';
        $codes["nl_nl"] = 'Nederlands';

        if (startsWith($phoneType, 'snom300') || startsWith($phoneType, 'snom360')) {
            $codes["de_de"] = 'German';
        }

        if (array_key_exists($code, $codes)) {
            return $codes[$code];
        }
        return 'English';
    }

    protected function setVolumeSettings($User)
    {
        if ($User) {
            $this->setVolumeUserSettings($User);
        } else {
            $this->psetting('vol_ringer', 8);
            $this->psetting('vol_handset', 8);
            $this->psetting('vol_handset_mic', 4);
            $this->psetting('vol_headset', 8);
            $this->psetting('vol_headset_mic', 4);
            $this->psetting('vol_speaker', 8);
            $this->psetting('vol_speaker_mic', 4);
        }
    }

    protected function setVolumeUserSettings($User)
    {
        $userSettings = $User->getSettings();
        if ($userSettings) {
            $settings = $userSettings->attributes;
            unset($settings['userId']);
            foreach ($settings as $key => $value) {
                if (empty($value)) {
                    return $this->setVolumeSettings(false);
                }
                $divisor = 15 / 10;
                if (strpos($key, 'Micro')) {
                    $divisor = 8 / 10;
                }
                $value = (int) $value * $divisor;
                $settings[$key] = round($value);
            }

            $this->psetting('vol_ringer', $settings['ringVolume']);
            $this->psetting('vol_handset', $settings['receiverVolume']);
            $this->psetting('vol_handset_mic', $settings['receiverMicroVolume']);
            $this->psetting('vol_headset', $settings['headsetVolume']);
            $this->psetting('vol_headset_mic', $settings['headsetMicroVolume']);
            $this->psetting('vol_speaker', $settings['speakerVolume']);
            $this->psetting('vol_speaker_mic', $settings['speakerMicroVolume']);
        }
    }

    protected function setAudioCodecs()
    {
        $phonalisaCodecs = str_replace(' ', '', cfg('GLOBAL_CODEC_PREFS'));
        $codecs = explode(',', $phonalisaCodecs);

        $accountCount = 12;
        for ($i = 1; $i <= (int) $accountCount; $i++) {
            $this->setAudioCodecToSettings($codecs, $i);
        }
    }

    protected function setAudioCodecToSettings($codecs, $account)
    {
        $codecValue = '';
        foreach ($codecs as $codec) {
            $codec = strtolower($codec);
            if ($this->getPhoneReadableCodecs($codec) === false) {
                continue;
            }
            $codecValue .= $codec . ",";
        }
        $codecValue = rtrim($codecValue, ',');
        $this->psetting("codec_priority_list", $codecValue, 'R', $account);
    }

    protected function getPhoneReadableCodecs($codec)
    {
        $phoneType = $this->getPhoneModel()->type;
        $phoneCodecs = array(
            'g722',
            'pcmu',
            'pcma',
            'gsm',
            'g726-32',
            'aal2-g726-32',
            'g729',
            'telephone-event'
        );

        $snom3x8xList = array('snom300', 'snom320', 'snom370', 'snom821', 'snom870');
        if (in_array($phoneType, $snom3x8xList)) {
            $phoneCodecs[] = 'amr-0';
            $phoneCodecs[] = 'amrwb-0';
        }

        $snom7xList = array('snom710', 'snom715', 'snom720', 'snomD725', 'snom760', 'snomD765');
        if (in_array($phoneType, $snom7xList)) {
            $phoneCodecs[] = 'g723';
        }

        $result = false;
        if (in_array($codec, $phoneCodecs)) {
            $result = true;
        }

        return $result;
    }
}
