<?php

require_once dirname(__FILE__) . '/../../lib/phoneprovisioning.php';
require_once dirname(__FILE__) . '/../tiptel-30xx/Tiptel30xx.php';
require_once dirname(__FILE__) . '/../tiptel-30xx/Tiptel30xxTimeZone.php';
require_once "TimezoneHelper.php";

class TiptelPhoneProvisioning extends VirtualPhoneProvisioningController
{

    const LOG_TAG = 'tiptel-30xx-dls';

    protected $lineKeyIndex;

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

    protected function checkUserAgent($ua)
    {
        $ua = str_replace("Tiptel Tiptel", "Tiptel", $ua);
        $ua_parts = explode(' ', $ua);

        if (strToLower(@$ua_parts[0]) === 'tiptel') {
            $found = false;
            $phoneType = substr(strToLower(@$ua_parts[1]), 0, 2);
            if ($phoneType === '30') {
                $ua_model = strToLower(@$ua_parts[0] . @$ua_parts[1]);
                foreach ($this->getSupportedPhoneModels() as $model) {
                    if ($model->getUserAgent() == $ua_model) {
                        $this->setCurrentPhoneModel($model);
                        $found = true;
                        break;
                    }
                }
            }
            # find out the firmware version of the phone
            $fw_vers = (preg_match('/(\d+\.\d+\.\d+\.\d+)/', @$ua_parts[2], $m)) ? $m[1] : '0.0.0.0';
            $fw_vers_nrml = $this->tiptel_normalize_version($fw_vers);
            $this->setCurrentFirmwareVersion($fw_vers_nrml);
            return $found;
        }
        return parent::checkUserAgent($ua);
    }

    protected function getSupportedPhoneModels()
    {
        return Tiptel30xx::getSupportedPhoneModels();
    }

    protected function tiptel_normalize_version($fwvers)
    {
        return Tiptel30xx::tiptel_normalize_version($fwvers);
    }

    protected function runFirmwareUpdate(Firmware $fwUpdate)
    {
        $this->psetting('P192', $this->getFirmwareURL());
        return true;    // Hotfix: Htek is sending only 4 digit firmware but 5 digits are needed. So we can't use the normal firmware scenario.
    }

    protected function getFirmwareURL()
    {
        return parent::getProvURL() . 'firmware/';
    }

    protected function getRingtoneURL($mac)
    {
        return parent::getProvURL() . 'ringtone/' . $mac;
    }

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

    protected function getPhonebookURL()
    {
        return $this->getXMLURL('pb');
    }

    protected function getLoginAppURL()
    {
        return $this->getXMLURL('login');
    }

    protected function getConfigAppURL()
    {
        return $this->getXMLURL('cfgapp');
    }

    protected function getVoicemailAppURL()
    {
        return $this->getXMLURL('voicemail');
    }

    protected function loadConfigValues($mac = null)
    {
        $template = $this->getTemplate();
        $Phone = $this->getPhoneModel();
        $SipAccount = $Phone->getSipAccount();
        $User = $SipAccount->getUser();
        $label = $this->getAccountLabel($User, $SipAccount);
        $userLanguage = $this->getUserLanguage($User);

        foreach ($template as $key => $value) {
            $this->psetting($key, $value);
        }

        $this->psetting('P8849', $this->getCallWaitingSettings($User)); // Call Waiting
        $this->psetting('P8850', $this->getCallWaitingSettings($User)); // Call Waiting Tone

        $this->psetting('P65', 0);          // Send Anonymous (Clir)
        $this->psetting('P183', $this->getSRTPMode());                  // SRTP Mode
        $this->psetting('P20646', $this->getZRTPEncryption($User));     // ZRTP Encryption
        $this->psetting('P75', date('I'));                              // Daylight Saving Time

        $this->psetting('P47', $SipAccount->getSIPRegistrar() . ":" . cfg("INTERNAL_SIP_PORT"));         // Primary SIP Server
        $this->psetting('P35', $SipAccount->authname);                  // SIP User ID
        $this->psetting('P36', $SipAccount->authname);                  // Authenticate ID
        $this->psetting('P34', $SipAccount->secret);                    // Authenticate Password
        $this->psetting('P20000', $label);                              // User Label

        $this->psetting('P33', 'voicemail');                            // Voice Mail UserID

        if (!isset($this->mCurrentSettings['P192'])) {                  // Hotfix: Htek is sending only 4 digit firmware but 5 digits are needed. So we can't use the normal firmware scenario.
            $this->psetting('P192', $this->getProvURL());               // Firmware Path
        }

        $this->psetting('P237', $this->getProvURL());                   // Config Server Path
        $this->psetting('P8621', $userLanguage);                        // Phone GUI Language
        $this->psetting('P2525', $userLanguage);                        // Web GUI Language

        $this->psetting('P8624', $this->getTimeFormat($User));          // Time Format
        $this->psetting('P102', $this->getDateFormat($User));           // Date Display Format
        $this->psetting('P30', cfg('NTP_HOST'));                        // NTP Server
        $this->psetting('P64', $this->getTimeZone($User));              // Time Zone

        $this->psetting('P20166', 2);                                   // DTMF Type: 0 = RFC2833, 1 = IN Audio, 2 = SIP INFO

        $this->psetting('P32', round($SipAccount->expires / 60));   // Account1.RegisterExpiration in minutes
        $this->psetting('P732', round($SipAccount->expires / 60));  // Account2.RegisterExpiration in minutes
        $this->psetting('P512', round($SipAccount->expires / 60));  // Account3.RegisterExpiration in minutes
        $this->psetting('P612', round($SipAccount->expires / 60));  // Account4.RegisterExpiration in minutes

        $this->psetting('P260', $SipAccount->expires);  // Account1.SessionExpiration
        $this->psetting('P434', $SipAccount->expires);  // Account2.SessionExpiration
        $this->psetting('P534', $SipAccount->expires);  // Account3.SessionExpiration
        $this->psetting('P634', $SipAccount->expires);  // Account4.SessionExpiration  

        $this->psetting('P20970', $SipAccount->expires);    // Account1.SubscribeExpires
        $this->psetting('P20971', $SipAccount->expires);    // Account2.SubscribeExpires
        $this->psetting('P20972', $SipAccount->expires);    // Account3.SubscribeExpires
        $this->psetting('P20973', $SipAccount->expires);    // Account4.SubscribeExpires

        $this->psetting('P20157', 1); // Account1.CallerDisplaySource: 0 = FROM, 1 = PAI-FROM, 2 = PAI-RPID-FROM, 3 = RPID-PAI-FROM, 4 = RPID-FROM

        $this->psetting('P20478', 0);    // Line Page Indicator

        $this->psetting('P24748', 0);   // Call Completion
        $this->psetting('P24744', 1);   // Show Account Label on Display

        $this->psetting('P4705', cfg('DP_PICKUP_PREFIX'));   // Direct Call Pickup Code
        $this->psetting('P4706', cfg('DP_PICKUP_PREFIX'));   // Group Call Pickup Code

        $this->psetting('P72', 0); //Use # As Dial Key.0 - no, 1 - yes

        $this->psetting('P20058', $this->getToneCountryPattern($User)); //Tone Country Pattern

        $this->setPhoneCodecs();
        $this->setRingtones();
        $this->setRingtoneServerPath($User, $mac);
        $this->setVolumeSettings($User);

        If (endsWith($Phone->type, '3030')) {
            $this->psetting('P231', 2);
            $this->psetting('P40001', 1);
        }

        if ($this->mCurrentFirmwareVersion >= '01.00.03.97') {
            $this->psetting('P1300', $this->getKeyToneSettings($User));     // Keypad DTMF Tone: 1 = off; 0 = on
            $this->setXMLToProgrammableKeys($User);
        }

        $this->processSoftkeys();
        $this->changeSettingsWithProvisioningProfile($User, $Phone->type);

        return true;
    }

    protected function getKeyToneSettings($User)
    {
        // Keypad DTMF Tone: 1 = off; 0 = on
        $keytone = 1;
        if (isset($User->key_tone) && !empty($User->key_tone)) {
            $keytone = 0;
        }
        return $keytone;
    }

    protected function getXmlPhonebookKeyName($language)
    {
        $keyName = "Dir.";
        if ($language === "de_de") {
            $keyName = "Verz.";
        }
        return $keyName;
    }

    protected function getXmlVoicemailKeyName($language)
    {
        $keyName = "VMail";
        if ($language === "de_de") {
            $keyName = "AB";
        }
        return $keyName;
    }

    protected function getXmlConfigKeyName($language)
    {
        $keyName = "Forward";
        if ($language === "de_de") {
            $keyName = "Weiterleitung";
        }
        return $keyName;
    }

    protected function getAccountLabel($User, $SipAccount)
    {
        $label = $SipAccount->getShortLabel(true);
        // check if user has callforwarding -> Change displayname!
        if ($User && $User->defaultCallForwardSet != $User->callForwardSet) {
            $preset = ForwardPreset::model()->findByPk($User->callForwardSet);
            if ($preset != NULL) {
                $label = $preset->name;
            } else {
                $label = "FWD?";
            }
        }
        return $label;
    }

    protected function getTemplate()
    {
        $contents = file_get_contents(dirname(__FILE__) . "/templates/tiptel-30xx.tpl");
        $contents = explode("\n", $contents);
        $config = array();
        foreach ($contents as $line) {
            $line = trim($line);
            if (strlen($line) == 0) {
                continue;
            }

            if ($line[0] == "#") {
                continue;
            }
            $parts = explode(" =", $line);
            $key = trim($parts[0]);
            $value = trim(@$parts[1]);

            $config[$key] = $value;
        }
        return $config;
    }

    protected function psetting($sectionname, $val = '')
    {
        if (preg_match('/^P[0-9]{1,5}$/S', $sectionname)) {
            $this->mCurrentSettings[$sectionname] = $val;
        }
    }

    protected function generateOutput()
    {
        echo "Flag=5678&CheckSum=0&";
        foreach ($this->mCurrentSettings as $pKey => $value) {
            echo "$pKey=$value&";
        }
        echo "end";
        return false;
    }

    protected function getCallWaitingSettings($User)
    {
        $callwaiting = 0;
        if ($User && $User->call_waiting) {
            $callwaiting = 1;
        }
        return $callwaiting;
    }

    protected function getClirSettings($User)
    {
        return 0;
    }

    protected function getSRTPMode()
    {
        $srtpMode = 0;
        if (cfg("RTP_ENABLE_SRTP") == "1") {
            $srtpMode = 2;
        }
        return $srtpMode;
    }

    protected function getZRTPEncryption($User)
    {
        $zrtpMode = 0;
        if (cfg("RTP_ENABLE_ZRTP") == '1') {
            $zrtpMode = 1;
        } else if ($User && $User->zrtp == '1') {
            $zrtpMode = 1;
        }
        return $zrtpMode;
    }

    protected function getUserLanguage($User)
    {
        $language = $this->getLanguage($User);
        return $this->convertLanguageCode($language);
    }

    protected function getLanguage($User)
    {
        $language = cfg("GUI_LANGS");
        if ($User && $User->user_language != "") {
            $language = $User->user_language;
        }
        return $language;
    }

    public function convertLanguageCode($code)
    {
        $codes = array();
        $codes["en_us"] = 0;
        $codes["fr_fr"] = 1;
        $codes["de_de"] = 2;

        if ($this->mCurrentFirmwareVersion <= '01.00.03.88' || ($this->getPhoneModel()->type == 'tiptel-3030' && $this->mCurrentFirmwareVersion < '01.00.03.97')) {
            $codes["de_de"] = 1;
            $codes["fr_fr"] = 2;
        }

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

    protected function getTimeFormat($User)
    {
        $time_format = cfg("USER_TIME_FORMAT");
        if ($User && $User->user_time_format != "") {
            $time_format = $User->user_time_format;
        }

        $convertedTimeFormat = 1;
        if ($time_format == "24") {
            $convertedTimeFormat = 0;
        }
        return $convertedTimeFormat;
    }

    protected function getDateFormat($User)
    {
        $date_format = cfg("USER_DATE_FORMAT");
        if ($User && $User->user_date_format != "") {
            $date_format = $User->user_date_format;
        }
        return $this->convertDateFormat($date_format);
    }

    protected function convertDateFormat($date_format)
    {
        $convertedDateFormat = 2;
        if ($date_format == 'MM/dd/yyyy') {
            $convertedDateFormat = 1;
        }
        if ($date_format == 'yyyy-MM-dd') {
            $convertedDateFormat = 0;
        }
        return $convertedDateFormat;
    }

    protected function getTimeZone($User)
    {
        $date_format = cfg("TIMEZONE");
        if ($User && $User->user_timezone != "") {
            $date_format = $User->user_timezone;
        }
        return $this->convertTimeZone($date_format);
    }

    protected function convertTimeZone($date_format)
    {
        $date_format = str_replace('/', '_', $date_format);
        $date_format = strtoupper($date_format);
        return Tiptel30xxTimeZone::$$date_format;
    }

    protected function getFirmwareServerPath($Phone)
    {
        return 'update.tiptel-info.de/' . substr($Phone->type, 7);
    }

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

        $phoneKey = 57;
        foreach ($codecs as $codec) {
            if ($this->getPhoneReadableCodecs($codec) === false) {
                continue;
            }
            $this->psetting("P$phoneKey", $this->getPhoneReadableCodecs($codec));             //Account 1: P57-P62
            $this->psetting("P7$phoneKey", $this->getPhoneReadableCodecs($codec));            //Account 2: P757-P762
            $this->psetting("P5" . ((int) $phoneKey - 6), $this->getPhoneReadableCodecs($codec));   //Account 3: P551-P556
            $this->psetting("P6" . ((int) $phoneKey - 6), $this->getPhoneReadableCodecs($codec));   //Account 4: P651-P656

            if ($phoneKey < 62) {
                (int) $phoneKey++;
            }
        }
    }

    protected function getPhoneReadableCodecs($codecs)
    {
        $phoneCodecs = array(
            'G722' => '9',
            'PCMA' => '8',
            'PCMU' => '0',
            'G729' => '18',
            'GSM' => '3',
            'G723' => '4',
            'G726-32' => '2',
            'iLBC' => '20'
        );
        if (!isset($phoneCodecs[$codecs])) {
            return false;
        }

        return $phoneCodecs[$codecs];
    }

    protected function setXMLToProgrammableKeys($User)
    {
        $language = $this->getLanguage($User);
        if ($User) {
            $this->psetting('P43200', 38);                                          //Type
            $this->psetting('P20043', '');                                          //Label
            $this->psetting('P43400', '');                                          //Value

            $this->psetting('P43201', 14);                                          //Type
            $this->psetting('P20044', $this->getXmlPhonebookKeyName($language));    //Label
            $this->psetting('P43401', $this->getPhonebookURL());                    //Value

            $this->psetting('P43202', 14);                                          //Type
            $this->psetting('P20045', $this->getXmlVoicemailKeyName($language));    //Label
            $this->psetting('P43402', $this->getVoicemailAppURL());                 //Value

            $this->psetting('P43203', 14);                                          //Type
            $this->psetting('P20046', $this->getXmlConfigKeyName($language));       //Label
            $this->psetting('P43403', $this->getConfigAppURL());                    //Value
        } else {
            $this->psetting('P43200', 38);                                          //Type
            $this->psetting('P20043', '');                                          //Label
            $this->psetting('P43400', '');                                          //Value

            $this->psetting('P43201', 37);                                          //Type
            $this->psetting('P20044', '');                                          //Label
            $this->psetting('P43401', '');                                          //Value

            $this->psetting('P43202', 14);                                          //Type
            $this->psetting('P20045', 'Login');                                     //Label
            $this->psetting('P43402', $this->getLoginAppURL());                     //Value

            $this->psetting('P43203', 36);                                          //Type
            $this->psetting('P20046', '');                                          //Label
            $this->psetting('P43403', '');                                          //Value
        }
    }

    protected function processSoftkeys()
    {
        $Phone = $this->getPhoneModel();
        $SipAccount = $Phone->getSipAccount();
        $User = $SipAccount->getUser();
        $levels = array("line", "memory", 'ext0', 'ext1', 'ext2');
        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;
        }
        $keyFunction = $this->convertSoftKeyFunction($key->function, 3);
        $keyValue = $this->convertSoftKeyValue($key);
        $keyLabel = $key->label;
        $keyIndex = $this->convertKeyIndex($key);
        $pickupCode = $this->convertPickupCode($key->function, array(3, 'X06'));

        if ($key->level === 'line') {
            if ((int) $keyIndex >= 9 && $keyValue !== null) {
                $this->psetting('P20478', 1);    // Line Page Indicator (set this to 1 to see the page changer at linekey 8)
            }

            if ((int) $keyIndex <= 3) {
                $this->setLineKeyZeroToThree($keyIndex, $keyFunction, $keyValue, $keyLabel, $pickupCode);
            } else if ((int) $keyIndex <= 36) {
                $this->setLineKeyFourToThirtySix($keyIndex, $keyFunction, $keyValue, $keyLabel, $pickupCode);
            } else {
                $this->setLineKeyThirtySevenToFortyFive($keyIndex, $keyFunction, $keyValue, $keyLabel, $pickupCode);
            }
        } else if ($key->level === 'memory') {
            $this->psetting('P4220' . $keyIndex, $keyFunction);
            $this->psetting('P4230' . $keyIndex, $keyValue);
            $this->psetting('P4240' . $keyIndex, 0);
            $this->psetting('P4250' . $keyIndex, $pickupCode);
        } else {
            $this->psetting('P' . $keyIndex, $keyFunction);
            $this->psetting('P' . ((int) $keyIndex + 1), $keyValue);
            $this->psetting('P' . ((int) $keyIndex + 2), $keyLabel);
            $this->psetting('P' . ((int) $keyIndex + 3), 0);
            $this->psetting('P' . ((int) $keyIndex + 4), $pickupCode);
        }
    }

    protected function setLineKeyZeroToThree($keyIndex, $keyFunction, $keyValue, $keyLabel, $pickupCode)
    {
        $this->psetting('P4120' . $keyIndex, $keyFunction);
        $this->psetting('P4130' . $keyIndex, $keyValue);
        $this->psetting('P4140' . $keyIndex, $keyLabel);
        $this->psetting('P4150' . $keyIndex, 0);
        $this->psetting('P4160' . $keyIndex, $pickupCode);
    }

    protected function setLineKeyFourToThirtySix($keyIndex, $keyFunction, $keyValue, $keyLabel, $pickupCode)
    {
        if ((int) $keyIndex === 4) {
            $this->lineKeyIndex = 20200;
        } else {
            $this->lineKeyIndex = (int) $this->lineKeyIndex + 5;
        }

        $keyIdx = $this->lineKeyIndex;
        $this->psetting('P' . $keyIdx, $keyFunction);
        $this->psetting('P' . ((int) $keyIdx + 1), $keyValue);
        $this->psetting('P' . ((int) $keyIdx + 2), $keyLabel);
        $this->psetting('P' . ((int) $keyIdx + 3), 0);
        $this->psetting('P' . ((int) $keyIdx + 4), $pickupCode);
    }

    protected function setLineKeyThirtySevenToFortyFive($keyIndex, $keyFunction, $keyValue, $keyLabel, $pickupCode)
    {
        if ((int) $keyIndex === 37) {
            $this->lineKeyIndex = 23000;
        } else {
            $this->lineKeyIndex = (int) $this->lineKeyIndex + 6;
        }

        $keyIdx = $this->lineKeyIndex;
        $this->psetting('P' . $keyIdx, $keyFunction);
        $this->psetting('P' . ((int) $keyIdx + 2), $keyValue);
        $this->psetting('P' . ((int) $keyIdx + 3), $keyLabel);
        $this->psetting('P' . ((int) $keyIdx + 4), 0);
        $this->psetting('P' . ((int) $keyIdx + 5), $pickupCode);
    }

    protected function convertKeyIndex($key)
    {
        $extIndex = 60000;
        if ($key->level == 'ext1') {
            $extIndex = 61000;
        } else if ($key->level == 'ext2') {
            $extIndex = 62000;
        }
        $keyIndex = (int) $key->key_idx - 1;
        if ($key->level !== 'line' && $key->level !== 'memory') {
            $key_idx = str_replace($key->level . '.', '', $key->key_idx);
            $keyIndex = (((int) $key_idx * 5) - 5) + $extIndex;
        }
        return $keyIndex;
    }

    protected function setRingtones()
    {
        $this->psetting('P1399', 'Ringer1');        // Alert Ring Text 1
        $this->psetting('P1402', 0);                // Alert Ring File 1

        $this->psetting('P1400', 'Ringer2');        // Alert Ring Text 2
        $this->psetting('P1403', 1);                // Alert Ring File 2

        $this->psetting('P1401', 'Ringer3');        // Alert Ring Text 3
        $this->psetting('P1404', 2);                // Alert Ring File 3

        $this->psetting('P20059', 'Ringer4');       // Alert Ring Text 4
        $this->psetting('P20066', 3);               // Alert Ring File 4

        $this->psetting('P20060', 'Ringer5');       // Alert Ring Text 5
        $this->psetting('P20067', 4);               // Alert Ring File 5

        $this->psetting('P20061', 'Ringer6');       // Alert Ring Text 6
        $this->psetting('P20068', 5);               // Alert Ring File 6

        $this->psetting('P20062', 'Ringer7');       // Alert Ring Text 7
        $this->psetting('P20069', 6);               // Alert Ring File 7

        $this->psetting('P20063', 'Ringer8');       // Alert Ring Text 8
        $this->psetting('P20070', 7);               // Alert Ring File 8

        $this->psetting('P20064', 'Ringer9');       // Alert Ring Text 9
        $this->psetting('P20071', 8);               // Alert Ring File 9

        $this->psetting('P20065', 'Ringer10');      // Alert Ring Text 10
        $this->psetting('P20072', 0);               // Alert Ring File 10
    }

    protected function setVolumeSettings($User)
    {
        if ($User) {
            $this->setVolumeUserSettings($User);
        } else {
            $this->psetting('P20083', 0);  // HandSet Send Volume
            $this->psetting('P20084', 0);  // HeadSet Send Volume
            $this->psetting('P249', 0);    // HandFree Send Volume
            $this->psetting('P8503', 5);   // Ring Volume
        }
    }

    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);
                }
                $result = round((int) $value * (7 / 10));
                if ($key == 'ringVolume') {
                    $result = round((int) $value * (16 / 10)) - 2;
                }
                $settings[$key] = $result;
            }

            $this->psetting('P20083', $this->getFormatedVolumeSetting($settings['receiverMicroVolume']));  // HandSet Send Volume
            $this->psetting('P20084', $this->getFormatedVolumeSetting($settings['headsetMicroVolume']));   // HeadSet Send Volume
            $this->psetting('P249', $this->getFormatedVolumeSetting($settings['speakerMicroVolume']));     // HandFree Send Volume
            $this->psetting('P8503', $settings['ringVolume']);   // Ring Volume
        }
    }

    protected function getFormatedVolumeSetting($setting)
    {
        $keyInOrder = array(
            1 => 6, //6 - -6dB
            2 => 5, //5 - -4dB
            3 => 4, //4 - -2dB
            4 => 0, //0 - 0dB default
            5 => 3, //3 - +2dB
            6 => 2, //2 - +4dB
            7 => 1  //1 - +6dB
        );

        $formatedSetting = 0;

        if (isset($keyInOrder[$setting])) {
            $formatedSetting = $keyInOrder[$setting];
        }
        return $formatedSetting;
    }

    protected function setRingtoneServerPath($User, $mac)
    {
        $ringtoneServerPath = '';
        if (!empty($User) && !empty($mac)) {
            $model = Ringtone::model()->findByAttributes(array('userId' => $User->id, 'fileName' => 'ring*'));
            if ($model) {
                $ringtoneServerPath = $this->getRingtoneURL($mac);
            }
        }
        $this->psetting('P20174', $ringtoneServerPath);
    }

    protected function getToneCountryPattern($User)
    {
        $userLanguage = $this->getLanguage($User);
        $toneSettingsList = array(
            'en_us' => 28,
            'de_de' => 11,
            'fr_fr' => 10,
            'nl_nl' => 21
        );

        $tonePattern = 0;
        if (isset($toneSettingsList[$userLanguage])) {
            $tonePattern = $toneSettingsList[$userLanguage];
        }
        return $tonePattern;
    }
}
