function ConferenceSetup()
{
    global $mysqli;
    $session =& $_SESSION['ConferenceSetup'];
    $smarty = smarty_init(dirname(__FILE__) . '/templates');
    // Init message (Message)
    $Message = isset($_REQUEST['msg']) ? $_REQUEST['msg'] : "";
    // Init form data (Extension)
    if (@$_REQUEST['submit'] == 'save') {
        $Conference = formdata_from_post();
        $Errors = formdata_validate($Conference);
        if (count($Errors) == 0) {
            formdata_save($Conference);
            asterisk_UpdateConf('confbridge.conf');
            asterisk_Reload();
            header("Location: ConferenceSetup.php?msg=MODIFY_CONFERENCE");
            die;
        }
    } else {
        $Conference = formdata_from_db($_SESSION['_USER']['PK_Extension']);
    }
    // Init Available Accounts (Accounts)
    $query = "\n\t\tSELECT\n\t\t\tExtensions.PK_Extension,\n\t\t\tExtension,\n            Name\n\t\tFROM\n\t\t\tExtensions\n\t\tWHERE\n\t\t\tExtensions.Type IN ('Virtual', 'SipPhone')\n\t\tORDER BY Extension\n\t";
    $result = $mysqli->query($query) or die($mysqli->error . $query);
    $Accounts = array();
    while ($row = $result->fetch_assoc()) {
        $Accounts[] = $row;
    }
    $smarty->assign('Conference', $Conference);
    $smarty->assign('Accounts', $Accounts);
    $smarty->assign('Message', $Message);
    $smarty->assign('Errors', $Errors);
    return $smarty->fetch('ConferenceSetup.tpl');
}
function VoipProviders_Delete()
{
    global $mysqli;
    $smarty = smarty_init(dirname(__FILE__) . '/templates');
    $PK_SipProvider = $_REQUEST['PK_SipProvider'];
    // In confirmed, do the actual delete
    if (@$_REQUEST['submit'] == 'delete_confirm') {
        $query = "DELETE FROM SipProviders WHERE PK_SipProvider = {$PK_SipProvider} LIMIT 1";
        $mysqli->query($query) or die($mysqli->error . $query);
        if ($mysqli->affected_rows != 1) {
            return;
        }
        $query = "DELETE FROM SipProvider_Codecs WHERE FK_SipProvider = {$PK_SipProvider}";
        $mysqli->query($query) or die($mysqli->error . $query);
        $query = "DELETE FROM SipProvider_Rules WHERE FK_SipProvider = {$PK_SipProvider}";
        $mysqli->query($query) or die($mysqli->error . $query);
        $query = "DELETE FROM SipProvider_Statuses WHERE FK_SipProvider = {$PK_SipProvider}";
        $mysqli->query($query) or die($mysqli->error . $query);
        $query = "DELETE FROM IncomingRoutes WHERE ProviderType='SIP' AND ProviderID = {$PK_SipProvider}";
        $mysqli->query($query) or die($mysqli->error . $query);
        asterisk_UpdateConf('sip.conf');
        asterisk_UpdateConf('extensions.conf');
        asterisk_Reload();
        header('Location: VoipProviders_List.php?msg=DELETE_SIP_PROVIDER');
        die;
    }
    // Init extension info (Extension)
    $query = "\n\t\tSELECT\n\t\t\tPK_SipProvider,\n\t\t\tName\n\t\tFROM\n\t\t\tSipProviders\n\t\tWHERE\n\t\t\tPK_SipProvider = {$PK_SipProvider}\n\t\tLIMIT 1\n\t";
    $result = $mysqli->query($query) or die($mysqli->error);
    $Provider = $result->fetch_assoc();
    $smarty->assign('Provider', $Provider);
    return $smarty->fetch('VoipProviders_Delete.tpl');
}
function Extensions_Queue_Delete()
{
    global $mysqli;
    $smarty = smarty_init(dirname(__FILE__) . '/templates');
    $PK_Extension = $_REQUEST['PK_Extension'];
    // In confirmed, do the actual delete
    if (@$_REQUEST['submit'] == 'delete_confirm') {
        $query = "DELETE FROM Extensions WHERE PK_Extension = {$PK_Extension} LIMIT 1";
        $mysqli->query($query) or die($mysqli->error);
        $query = "DELETE FROM Ext_Queues WHERE PK_Extension = {$PK_Extension} LIMIT 1";
        $mysqli->query($query) or die($mysqli->error);
        $query = "DELETE FROM Ext_Queue_Members WHERE FK_Extension = {$PK_Extension}";
        $mysqli->query($query) or die($mysqli->error);
        asterisk_UpdateConf('queues.conf');
        asterisk_Reload();
        header('Location: Extensions_List.php?msg=DELETE_QUEUE_EXTENSION');
        die;
    }
    // Init extension info (Extension)
    $query = "\n\t\tSELECT\n\t\t\tPK_Extension,\n\t\t\tName\n\t\tFROM\n\t\t\tExt_Queues\n\t\tWHERE\n\t\t\tPK_Extension = {$PK_Extension}\n\t\tLIMIT 1\n\t";
    $result = $mysqli->query($query) or die($mysqli->error);
    $Queue = $result->fetch_assoc();
    $smarty->assign('Queue', $Queue);
    return $smarty->fetch('Extensions_Queue_Delete.tpl');
}
function Dongles_Delete()
{
    global $mysqli;
    $smarty = smarty_init(dirname(__FILE__) . '/templates');
    $PK_Dongle = $_REQUEST['PK_Dongle'];
    // In confirmed, do the actual delete
    if (@$_REQUEST['submit'] == 'delete_confirm') {
        $query = "DELETE FROM Dongles WHERE PK_Dongle = {$PK_Dongle} LIMIT 1";
        $mysqli->query($query) or die($mysqli->error . $query);
        if ($mysqli->affected_rows != 1) {
            return;
        }
        $query = "DELETE FROM Dongle_Rules WHERE FK_Dongle = {$PK_Dongle}";
        $mysqli->query($query) or die($mysqli->error . $query);
        $query = "DELETE FROM Dongle_Statuses WHERE FK_Dongle = {$PK_Dongle}";
        $mysqli->query($query) or die($mysqli->error . $query);
        asterisk_UpdateConf('dongle.conf');
        asterisk_UpdateConf('extensions.conf');
        asterisk_Reload();
        header('Location: Dongles_List.php?msg=DELETE_DONGLE');
    } else {
        // Init extension info (Extension)
        $query = "\n\t\tSELECT\n\t\t\tPK_Dongle,\n\t\t\tName\n\t\tFROM\n\t\t\tDongles\n\t\tWHERE\n\t\t\tPK_Dongle = {$PK_Dongle}\n\t\tLIMIT 1\n\t";
        $result = $mysqli->query($query) or die($mysqli->error);
        $Dongle = $result->fetch_assoc();
        $smarty->assign('Dongle', $Dongle);
        return $smarty->fetch('Dongles_Delete.tpl');
    }
}
function Extensions_Queue_Modify()
{
    global $mysqli;
    $session =& $_SESSION['Extensions_Queue_Modify'];
    $smarty = smarty_init(dirname(__FILE__) . '/templates');
    // Init message (Message)
    $Message = isset($_REQUEST['msg']) ? $_REQUEST['msg'] : "";
    // Init Available ringing strategies (RingStrategies)
    $query = "SELECT PK_RingStrategy, Name, Description FROM RingStrategies";
    $result = $mysqli->query($query) or die($mysqli->error . $query);
    $RingStrategies = array();
    while ($row = $result->fetch_assoc()) {
        $RingStrategies[] = $row;
    }
    // Init Available Members (Members)
    $query = "\n\t\tSELECT\n\t\t\tExtensions.PK_Extension,\n\t\t\tExtension,\n            Name\n\t\tFROM\n\t\t\tExtensions\n\t\tWHERE\n\t\t\tExtensions.Type IN ('Virtual', 'SipPhone')\n\t\tORDER BY Extension\n\t";
    $result = $mysqli->query($query) or die($mysqli->error . $query);
    $Members = array();
    while ($row = $result->fetch_assoc()) {
        $Members[] = $row;
    }
    // Init available Sounds (SoundsFiles)
    $query = "\n\t\tSELECT\n\t\t\tPK_SoundFile,\n\t\t\tSoundFiles.Name,\n\t\t\tSoundFiles.Description,\n\t\t\tSoundLanguages.Name AS Language\n\t\tFROM\n\t\t\tSoundFiles\n\t\t\tINNER JOIN SoundEntries   ON FK_SoundEntry    = PK_SoundEntry\n\t\t\tINNER JOIN SoundFolders   ON FK_SoundFolder   = PK_SoundFolder\n\t\t\tINNER JOIN SoundLanguages ON FK_SoundLanguage = PK_SoundLanguage\n\t\tWHERE\n\t\t\tSoundFolders.Name = 'Call Queues'\n\t\tORDER BY\n\t\t\tSoundLanguages.Name ASC,\n\t\t\tSoundFiles.Name     ASC\n\t";
    $result = $mysqli->query($query) or die($mysqli->error);
    $SoundFiles = array();
    while ($row = $result->fetch_assoc()) {
        $SoundFiles[$row['Language']][$row['PK_SoundFile']] = $row['Name'];
    }
    // Init available moh groups
    $query = "SELECT * FROM Moh_Groups ORDER BY Name ASC";
    $result = $mysqli->query($query) or die($mysqli->error);
    $MohGroups = array();
    while ($row = $result->fetch_assoc()) {
        $MohGroups[] = $row;
    }
    // Init form data (Queue)
    if (@$_REQUEST['submit'] == 'save') {
        $Queue = formdata_from_post();
        $Errors = formdata_validate($Queue);
        if (count($Errors) == 0) {
            $id = formdata_save($Queue);
            asterisk_UpdateConf('queues.conf');
            asterisk_Reload();
            header("Location: Extensions_List.php?msg=MODIFY_QUEUE_EXTENSION&hilight={$id}");
            die;
        }
    } elseif (@$_REQUEST['PK_Extension'] != "") {
        $Queue = formdata_from_db($_REQUEST['PK_Extension']);
    } else {
        $Queue = array('MemberRingTime' => '16', 'NextWaitTime' => '5', 'WrapUpTime' => '30', 'PlayMohInQueue' => '1', 'AnnounceFrequency' => '90', 'FK_Sound_PickupAnnouncement' => '4552', 'FK_Sound_YouAreNext' => '4401', 'FK_Sound_ThereAre' => '4249', 'FK_Sound_CallsWaiting' => '4394', 'FK_Sound_HoldTime' => '4351', 'FK_Sound_Minutes' => '4361', 'FK_Sound_ThankYou' => '4243');
    }
    $smarty->assign('Queue', $Queue);
    $smarty->assign('RingStrategies', $RingStrategies);
    $smarty->assign('SoundFiles', $SoundFiles);
    $smarty->assign('MohGroups', $MohGroups);
    $smarty->assign('Members', $Members);
    $smarty->assign('Message', $Message);
    $smarty->assign('Errors', $Errors);
    return $smarty->fetch('Extensions_Queue_Modify.tpl');
}
function MOH_Groups_Modify()
{
    global $mysqli;
    $session =& $_SESSION['MOH_Groups_Modify'];
    $smarty = smarty_init(dirname(__FILE__) . '/templates');
    // Init message (Message)
    $Message = isset($_REQUEST['msg']) ? $_REQUEST['msg'] : "";
    if (@$_REQUEST['submit'] == 'save') {
        $Group = formdata_from_post();
        $Errors = formdata_validate($Group);
        if (count($Errors) == 0) {
            if ($Group['PK_Group'] != '') {
                $msg = 'MODIFY_MOH_GROUP';
            } else {
                $msg = 'ADD_MOH_GROUP';
            }
            $id = formdata_save($Group);
            asterisk_UpdateConf('musiconhold.conf');
            asterisk_Reload();
            header("Location: MOH_Groups_List.php?hilight={$id}&msg={$msg}");
            die;
        }
    } elseif (@$_REQUEST['PK_Group'] != "") {
        $Group = formdata_from_db($_REQUEST['PK_Group']);
    } else {
        $Group = formdata_from_default();
    }
    $smarty->assign('Group', $Group);
    $smarty->assign('Errors', $Errors);
    return $smarty->fetch('MOH_Groups_Modify.tpl');
}
function Account_Modify()
{
    $session =& $_SESSION['Account_Modify'];
    $smarty = smarty_init(dirname(__FILE__) . '/templates');
    // Init message (Message)
    $Message = isset($_REQUEST['msg']) ? $_REQUEST['msg'] : "";
    // Init form data (Extension)
    if (@$_REQUEST['submit'] == 'save') {
        $Extension = formdata_from_post();
        $Errors = formdata_validate($Extension);
        if (count($Errors) == 0) {
            $id = formdata_save($Extension);
            asterisk_UpdateConf('sip.conf');
            asterisk_UpdateConf('voicemail.conf');
            asterisk_Reload();
            header("Location: Account_Modify.php?msg=MODIFY_EXTENSION&hilight={$id}");
            die;
        }
    } else {
        $Extension = formdata_from_db($_SESSION['_USER']['PK_Extension']);
    }
    $smarty->assign('Extension', $Extension);
    $smarty->assign('Message', $Message);
    $smarty->assign('Errors', $Errors);
    return $smarty->fetch('Account_Modify.tpl');
}
function Recordings_DeleteRule()
{
    global $mysqli;
    include dirname(__FILE__) . '/../include/config.inc.php';
    $smarty = smarty_init(dirname(__FILE__) . '/templates');
    $path = $conf['dirs']['moh'];
    $PK_Rule = $_REQUEST['PK_Rule'];
    // In confirmed, do the actual delete
    if (@$_REQUEST['submit'] == 'delete_confirm') {
        //delete files from database
        $query = "DELETE FROM RecordingRules WHERE PK_Rule = {$PK_Rule}";
        $mysqli->query($query) or die($mysqli->error . $query);
        $query = "DELETE FROM RecordingRules_Extensions WHERE FK_Rule = {$PK_Rule}";
        $mysqli->query($query) or die($mysqli->error . $query);
        $query = "DELETE FROM RecordingRules_Groups WHERE FK_Rule = {$PK_Rule}";
        $mysqli->query($query) or die($mysqli->error . $query);
        asterisk_UpdateConf('musiconhold.conf');
        asterisk_Reload();
        header('Location: Recordings_List.php?msg=DELETE_REC_RULE');
    } else {
        $query = "SELECT * FROM RecordingRules WHERE PK_Rule =  {$PK_Rule};";
        $result = $mysqli->query($query) or die($mysqli->error);
        $RecordingRule = $result->fetch_assoc();
        $smarty->assign('RecordingRule', $RecordingRule);
        return $smarty->fetch('Recordings_DeleteRule.tpl');
    }
}
function Extensions_SipPhone_Delete()
{
    global $mysqli;
    $smarty = smarty_init(dirname(__FILE__) . '/templates');
    $PK_Extension = $_REQUEST['PK_Extension'];
    // In confirmed, do the actual delete
    if (@$_REQUEST['submit'] == 'delete_confirm') {
        $query = "DELETE FROM Extensions WHERE PK_Extension = {$PK_Extension} LIMIT 1";
        $mysqli->query($query) or die($mysqli->error);
        $query = "DELETE FROM Ext_SipPhones WHERE PK_Extension = {$PK_Extension} LIMIT 1";
        $mysqli->query($query) or die($mysqli->error);
        if ($mysqli->affected_rows != 1) {
            return;
        }
        $query = "DELETE FROM Ext_SipPhones_Codecs WHERE FK_Extension = {$PK_Extension}";
        $mysqli->query($query) or die($mysqli->error);
        $query = "DELETE FROM Extension_Groups WHERE FK_Extension = {$PK_Extension}";
        $mysqli->query($query) or die($mysqli->error);
        $query = "DELETE FROM Ext_SipPhones_Features WHERE FK_Extension = {$PK_Extension}";
        $mysqli->query($query) or die($mysqli->error);
        $query = "DELETE FROM Extension_Rules WHERE FK_Extension = {$PK_Extension}";
        $mysqli->query($query) or die($mysqli->error);
        asterisk_UpdateConf('sip.conf');
        asterisk_UpdateConf('voicemail.conf');
        asterisk_Reload();
        header('Location: Extensions_List.php?msg=DELETE_SIPPHONE_EXTENSION');
        die;
    }
    // Init extension info (Extension)
    $query = "\n\t\tSELECT\n\t\t\tPK_Extension,\n\t\t\tExtension\n\t\tFROM\n\t\t\tExtensions\n\t\tWHERE\n\t\t\tPK_Extension = {$PK_Extension}\n\t\tLIMIT 1\n\t";
    $result = $mysqli->query($query) or die($mysqli->error . $query);
    $Extension = $result->fetch_assoc();
    $smarty->assign('Extension', $Extension);
    return $smarty->fetch('Extensions_SipPhone_Delete.tpl');
}
function VoipProviders_Modify()
{
    global $mysqli;
    $session =& $_SESSION['VoipProviders_Modify'];
    $smarty = smarty_init(dirname(__FILE__) . '/templates');
    //	myprint($_REQUEST);
    // Init message (Message)
    $Message = isset($_REQUEST['msg']) ? $_REQUEST['msg'] : "";
    // Init Available DTMF Modes (DTMFModes)
    $query = "SELECT PK_DTMFMode, Name, Description FROM DTMFModes";
    $result = $mysqli->query($query) or die($mysqli->errno());
    $DTMFModes = array();
    while ($row = $result->fetch_assoc()) {
        $DTMFModes[] = $row;
    }
    // Init available codecs (Codecs)
    $query = "SELECT PK_Codec, Name, Description, Recomended FROM Codecs";
    $result = $mysqli->query($query) or die($mysqli->error);
    $Codecs = array();
    while ($row = $result->fetch_assoc()) {
        $Codecs[] = $row;
    }
    // Init available outgoing rules (Rules)
    $query = "SELECT * FROM OutgoingRules ORDER BY Name";
    $result = $mysqli->query($query) or die($mysqli->errno());
    $Rules = array();
    while ($row = $result->fetch_assoc()) {
        $Rules[] = $row;
    }
    // Init form data (Providers)
    if ($_REQUEST['submit'] == 'save') {
        $Provider = formdata_from_post();
        $Errors = formdata_validate($Provider);
        if (count($Errors) == 0) {
            $id = formdata_save($Provider);
            asterisk_UpdateConf('sip.conf');
            asterisk_UpdateConf('extensions.conf');
            asterisk_Reload();
            header("Location: VoipProviders_List.php?msg=MODIFY_SIP_PROVIDER&hilight={$id}");
            die;
        }
    } elseif ($_REQUEST['PK_SipProvider'] != "") {
        $Provider = formdata_from_db($_REQUEST['PK_SipProvider']);
    } else {
        $Provider = formdata_from_default();
    }
    $smarty->assign('Provider', $Provider);
    $smarty->assign('DTMFModes', $DTMFModes);
    $smarty->assign('Codecs', $Codecs);
    $smarty->assign('Message', $Message);
    $smarty->assign('Errors', $Errors);
    $smarty->assign('Rules', $Rules);
    return $smarty->fetch('VoipProviders_Modify.tpl');
}
function Extensions_Agent_Modify()
{
    global $mysqli;
    $session =& $_SESSION['Extensions_Agent_Modify'];
    $smarty = smarty_init(dirname(__FILE__) . '/templates');
    // Init message (Message)
    $Message = isset($_REQUEST['msg']) ? $_REQUEST['msg'] : "";
    // Init available extension groups (Groups)
    $query = "SELECT PK_Group, Name FROM Groups";
    $result = $mysqli->query($query) or die($mysqli->errno());
    $Groups = array();
    while ($row = $result->fetch_assoc()) {
        $Groups[] = $row;
    }
    // Init available outgoing rules (Rules)
    $query = "SELECT * FROM OutgoingRules ORDER BY Name";
    $result = $mysqli->query($query) or die($mysqli->errno());
    $Rules = array();
    while ($row = $result->fetch_assoc()) {
        $Rules[] = $row;
    }
    // Init available extension groups (Features)
    $query = "SELECT PK_Feature, Name FROM Features";
    $result = $mysqli->query($query) or die($mysqli->errno());
    $Features = array();
    while ($row = $result->fetch_assoc()) {
        $Features[] = $row;
    }
    // Init form data (Extension)
    if (@$_REQUEST['submit'] == 'save') {
        $Extension = formdata_from_post();
        $Errors = formdata_validate($Extension);
        if (count($Errors) == 0) {
            $id = formdata_save($Extension);
            asterisk_UpdateConf('sip.conf');
            asterisk_UpdateConf('voicemail.conf');
            asterisk_Reload();
            header("Location: Extensions_List.php?msg=MODIFY_AGENT_EXTENSION&hilight={$id}");
            die;
        }
    } elseif (@$_REQUEST['PK_Extension'] != "") {
        $Extension = formdata_from_db($_REQUEST['PK_Extension']);
    } else {
        $Extension = formdata_from_template($_REQUEST['FK_Template']);
    }
    $smarty->assign('Extension', $Extension);
    $smarty->assign('Features', $Features);
    $smarty->assign('Groups', $Groups);
    $smarty->assign('Message', $Message);
    $smarty->assign('Errors', $Errors);
    $smarty->assign('Rules', $Rules);
    return $smarty->fetch('Extensions_Agent_Modify.tpl');
}
Example #12
0
function Dongles_Modify()
{
    global $mysqli;
    $session =& $_SESSION['Dongles_Modify'];
    $smarty = smarty_init(dirname(__FILE__) . '/templates');
    //	myprint($_REQUEST);
    // Init message (Message)
    $Message = isset($_REQUEST['msg']) ? $_REQUEST['msg'] : "";
    // Init available outgoing rules (Rules)
    $query = "SELECT * FROM OutgoingRules ORDER BY Name";
    $result = $mysqli->query($query) or die($mysqli->errno());
    $Rules = array();
    while ($row = $result->fetch_assoc()) {
        $Rules[] = $row;
    }
    $discovery_response = explode("\n", asterisk_Cmd('dongle discovery'));
    $discovered_dongles = array();
    $discovered_i = -1;
    for ($i = 0; $i < sizeof($discovery_response); $i++) {
        if (substr($discovery_response[$i], 0, 1) == "[") {
            $discovered_i++;
            $discovered_dongles[$discovered_i] = array();
        } elseif (substr($discovery_response[$i], 0, 4) == "imsi") {
            $discovered_dongles[$discovered_i]["IMSI"] = substr($discovery_response[$i], 5);
        } elseif (substr($discovery_response[$i], 0, 4) == "imei") {
            $discovered_dongles[$discovered_i]["IMEI"] = substr($discovery_response[$i], 5);
        }
    }
    // Init form data (Providers)
    if ($_REQUEST['submit'] == 'save') {
        $Dongle = formdata_from_post();
        $Errors = formdata_validate($Dongle);
        if (count($Errors) == 0) {
            $id = formdata_save($Dongle);
            asterisk_UpdateConf('dongle.conf');
            asterisk_UpdateConf('extensions.conf');
            asterisk_Reload();
            header("Location: Dongles_List.php?msg=MODIFY_DONGLE&hilight={$id}");
            die;
        }
    } elseif ($_REQUEST['PK_Dongle'] != "") {
        $Dongle = formdata_from_db($_REQUEST['PK_Dongle']);
    } else {
        $Dongle = formdata_from_default();
    }
    $smarty->assign('Dongle', $Dongle);
    $smarty->assign('DiscoveredDongles', $discovered_dongles);
    $smarty->assign('Message', $Message);
    $smarty->assign('Errors', $Errors);
    $smarty->assign('Rules', $Rules);
    return $smarty->fetch('Dongles_Modify.tpl');
}
function OutgoingCalls_Rule_Modify()
{
    global $mysqli;
    $smarty = smarty_init(dirname(__FILE__) . '/templates');
    // Init form data (Rule, Errors)
    if (@$_REQUEST['submit'] == 'save') {
        $Rule = formdata_from_post();
        $Errors = formdata_validate($Rule);
        if (count($Errors) == 0) {
            $id = formdata_save($Rule);
            asterisk_UpdateConf('extensions.conf');
            asterisk_Reload();
            if ($Rule['PK_OutgoingRule'] == '') {
                header("Location: OutgoingCalls.php?msg=CREATE_RULE&hilight={$id}");
                die;
            } else {
                header("Location: OutgoingCalls.php?msg=MODIFY_RULE&hilight={$id}");
                die;
            }
        }
    } else {
        if ($_REQUEST['PK_OutgoingRule'] != "") {
            $Rule = formdata_from_db($_REQUEST['PK_OutgoingRule']);
        } else {
            $Rule['Allow'] = '1';
            $Rule['Final'] = '1';
        }
    }
    // SipProviders
    $SipProviders = array();
    $query = "SELECT * FROM SipProviders ORDER BY Name";
    $result = $mysqli->query($query) or die($mysqli->error);
    while ($row = $result->fetch_assoc()) {
        $SipProviders[] = $row;
    }
    // IaxProviders
    $Dongles = array();
    $query = "SELECT * FROM Dongles ORDER BY Name";
    $result = $mysqli->query($query) or die($mysqli->error);
    while ($row = $result->fetch_assoc()) {
        $Dongles[] = $row;
    }
    $smarty->assign('SipProviders', $SipProviders);
    $smarty->assign('Dongles', $Dongles);
    $smarty->assign('Rule', $Rule);
    $smarty->assign('Errors', $Errors);
    return $smarty->fetch('OutgoingCalls_Rule_Modify.tpl');
}
function MOH_Files_ListGroup_Ajax()
{
    global $mysqli;
    include dirname(__FILE__) . '/../include/config.inc.php';
    $path = $conf['dirs']['moh'];
    $session =& $_SESSION['MOH_Files_ListGroup_Ajax'];
    $smarty = smarty_init(dirname(__FILE__) . '/templates');
    $response = array();
    switch ($_REQUEST['Action']) {
        case 'UpdateRuleOrder':
            $order = 1;
            foreach ($_REQUEST['Rules'] as $PK_Rule) {
                $PK_Rule = explode('_', $PK_Rule);
                $PK = $PK_Rule[1];
                //disk
                $query = "SELECT\n\t\t\t\t\t\t\t\t`FK_Group`, `Order`, `Fileext`\n\t\t\t\t\t\t  FROM\n\t\t\t\t\t\t\t\t`Moh_Files`\n\t\t\t\t\t\t  WHERE `PK_File` = {$PK}";
                $result = $mysqli->query($query) or die($mysqli->error);
                $File_src = $result->fetch_assoc();
                $PK_Group = $File_src['FK_Group'];
                $old_order = $File_src['Order'];
                $extension = $File_src['Fileext'];
                $src = $path;
                $src .= "/group_" . str_pad($PK_Group, 10, "0", STR_PAD_LEFT);
                $src .= "/file_" . str_pad($old_order, 6, "0", STR_PAD_LEFT);
                $src .= "_" . str_pad($PK, 9, "0", STR_PAD_LEFT) . "." . $extension;
                $dest = $path;
                $dest .= "/group_" . str_pad($PK_Group, 10, "0", STR_PAD_LEFT);
                $dest .= "/file_" . str_pad($order, 6, "0", STR_PAD_LEFT);
                $dest .= "_" . str_pad($PK, 9, "0", STR_PAD_LEFT) . "." . $extension;
                rename($src, $dest);
                //db
                $query = "UPDATE `Moh_Files` SET `Order` = '" . intval($order) . "' WHERE `PK_File` = '" . intval($PK) . "'";
                $mysqli->query($query) or die($mysqli->error);
                $order++;
            }
            asterisk_UpdateConf('musiconhold.conf');
            asterisk_Reload();
            break;
    }
    echo json_encode($response);
}
function MOH_Files_Modify()
{
    global $mysqli;
    include dirname(__FILE__) . '/../include/config.inc.php';
    $session =& $_SESSION['MOH_Files_Modify'];
    $Message = isset($_REQUEST['msg']) ? $_REQUEST['msg'] : "";
    $smarty = smarty_init(dirname(__FILE__) . '/templates');
    $action = $_REQUEST['action'];
    if ($action == 'uploadfile') {
        $FK_Group = $_REQUEST['id_group'];
        $bigFK_Group = str_pad($FK_Group, 10, "0", STR_PAD_LEFT);
        $uploadPath = $conf['dirs']['moh'] . "/group_" . $bigFK_Group . "/";
        $filename_ext = explode(".", $_FILES['file']['name']['0']);
        $filename = "";
        for ($i = 0; $i < count($filename_ext) - 1; $i++) {
            $filename .= $filename_ext[$i];
        }
        $extension = $filename_ext[count($filename_ext) - 1];
        $query = "SELECT MAX(`Order`) FROM Moh_Files WHERE FK_Group = '{$FK_Group}'";
        $result = $mysqli->query($query) or die($mysqli->error);
        $row = $result->fetch_row();
        $order = $row['0'] + 1;
        $Errors = upload_file($uploadPath, $filename, $extension, $order, $FK_Group);
        if (empty($Errors)) {
            asterisk_UpdateConf('musiconhold.conf');
            asterisk_Reload();
            header("Location: MOH_Files_ListGroup.php?PK_Group={$FK_Group}");
            die;
        }
    }
    // Init available groups (Groups)
    $query = "SELECT * FROM Moh_Groups";
    $result = $mysqli->query($query) or die($mysqli->error . $query);
    while ($row = $result->fetch_assoc()) {
        $Groups[] = $row;
    }
    $smarty->assign('Groups', $Groups);
    $smarty->assign('Message', $Message);
    $smarty->assign('Errors', $Errors);
    return $smarty->fetch('MOH_Files_Modify.tpl');
}
function MOH_Groups_Delete()
{
    global $mysqli;
    include dirname(__FILE__) . '/../include/config.inc.php';
    $smarty = smarty_init(dirname(__FILE__) . '/templates');
    $path = $conf['dirs']['moh'];
    $PK_Group = $_REQUEST['PK_Group'];
    // In confirmed, do the actual delete
    if (@$_REQUEST['submit'] == 'delete_confirm') {
        //delete files from database
        $query = "DELETE FROM Moh_Files  WHERE FK_Group = {$PK_Group}";
        $mysqli->query($query) or die($mysqli->error . $query);
        //delete files from hdd
        $handle = @opendir($path . "/group_" . str_pad($PK_Group, 10, "0", STR_PAD_LEFT) . "/");
        if ($handle) {
            while (false !== ($file = readdir($handle))) {
                if ($file != "." && $file != "..") {
                    @unlink($path . "/group_" . str_pad($PK_Group, 10, "0", STR_PAD_LEFT) . "/" . $file);
                }
            }
            closedir($handle);
            @rmdir($path . "/group_" . str_pad($PK_Group, 10, "0", STR_PAD_LEFT));
        }
        //delete directories from database
        $query = "DELETE FROM Moh_Groups WHERE PK_Group = '{$PK_Group}'";
        $mysqli->query($query) or die($mysqli->error . $query);
        asterisk_UpdateConf('musiconhold.conf');
        asterisk_Reload();
        header('Location: MOH_Groups_List.php');
        die;
    }
    $query = "SELECT * FROM Moh_Groups WHERE PK_Group =  {$PK_Group};";
    $result = $mysqli->query($query) or die($mysqli->error);
    $Group = $result->fetch_assoc();
    $smarty->assign('Group', $Group);
    return $smarty->fetch('MOH_Groups_Delete.tpl');
}
function OutgoingCalls_Ajax()
{
    global $mysqli;
    $session =& $_SESSION['OutgoingCallsAjax'];
    $smarty = smarty_init(dirname(__FILE__) . '/templates');
    $data = $_POST;
    $response = array();
    switch ($data['Action']) {
        case 'DeleteRule':
            $query = "DELETE FROM OutgoingRules WHERE PK_OutgoingRule = " . intval($data['ID']) . " LIMIT 1";
            $mysqli->query($query) or die($mysqli->error);
            $query = "DELETE FROM Extension_Rules WHERE FK_OutgoingRule = " . intval($data['ID']) . "";
            $mysqli->query($query) or die($mysqli->error);
            $query = "DELETE FROM Template_Rules WHERE FK_OutgoingRule = " . intval($data['ID']) . "";
            $mysqli->query($query) or die($mysqli->error);
            $query = "DELETE FROM OutgoingCIDRules WHERE FK_OutgoingRule = " . intval($data['ID']) . " ";
            $mysqli->query($query) or die($mysqli->error);
            $response['ID'] = $data['ID'];
            break;
        case 'DeleteCIDRule':
            $query = "DELETE FROM OutgoingCIDRules WHERE PK_OutgoingCIDRule = " . intval($data['ID']) . " LIMIT 1";
            $mysqli->query($query) or die($mysqli->error);
            $response['ID'] = $data['ID'];
            break;
        case 'UpdateRuleOrder':
            $order = 1;
            foreach ($_REQUEST['Rules'] as $PK_Rule) {
                $PK_Rule = explode('_', $PK_Rule);
                $PK_Rule = $PK_Rule[1];
                $response['test'] .= "{$order}-{$PK_Rule} , ";
                $query = "UPDATE OutgoingRules SET RuleOrder = {$order} WHERE PK_OutgoingRule = {$PK_Rule} LIMIT 1";
                $mysqli->query($query) or die($mysqli->error);
                $order++;
            }
            break;
        case 'UpdateCIDRule':
            if ($data['ExtensionStart'] == "") {
                $errors['ExtensionStart']['Invalid'] = true;
            } elseif (intval($data['ExtensionStart']) . "" != $data['ExtensionStart']) {
                $errors['ExtensionStart']['Invalid'] = true;
            } elseif (strlen($data['ExtensionStart']) < 3 || strlen($data['ExtensionStart']) > 5) {
                $errors['ExtensionStart']['Invalid'] = true;
            }
            if ($data['Type'] == 'Multiple') {
                if ($data['ExtensionEnd'] == "") {
                    $errors['ExtensionEnd']['Invalid'] = true;
                } elseif (intval($data['ExtensionEnd']) . "" != $data['ExtensionEnd']) {
                    $errors['ExtensionEnd']['Invalid'] = true;
                } elseif (strlen($data['ExtensionEnd']) < 3 || strlen($data['ExtensionEnd']) > 5) {
                    $errors['ExtensionEnd']['Invalid'] = true;
                }
            } else {
                $data['ExtensionEnd'] = 0;
            }
            if (!preg_match('/^[0-9]?$/', $data['FK_OutgoingRule'])) {
                $data['FK_OutgoingRule'] = 0;
            }
            if (!preg_match('/^[0-9]?$/', $data['Add'])) {
                $data['Add'] = 0;
            }
            $query = "\n\t\t\t\tUPDATE\n\t\t\t\t\tOutgoingCIDRules\n\t\t\t\tSET\n\t\t\t\t\tType            = '" . $mysqli->real_escape_string($data['Type']) . "',\n\t\t\t\t\tExtensionStart  = '" . $mysqli->real_escape_string($data['ExtensionStart']) . "',\n\t\t\t\t\tExtensionEnd    = '" . $mysqli->real_escape_string($data['ExtensionEnd']) . "',\n\t\t\t\t\tFK_OutgoingRule = '" . $mysqli->real_escape_string($data['FK_OutgoingRule']) . "',\n\t\t\t\t\t`Add`           =  " . intval($data['Add']) . ",\n\t\t\t\t\tPrependDigits   = '" . $mysqli->real_escape_string($data['PrependDigits']) . "',\n\t\t\t\t\tName            = '" . $mysqli->real_escape_string($data['Name']) . "',\n\t\t\t\t\tNumber          = '" . $mysqli->real_escape_string($data['Number']) . "'\n\t\t\t\tWHERE\n\t\t\t\t\tPK_OutgoingCIDRule = " . intval($data['ID']) . "\n\t\t\t\tLIMIT 1\n\t\t\t";
            $mysqli->query($query) or die($mysqli->error . $query);
            $response['ID'] = $data['ID'];
            $response['Errors'] = $errors;
            break;
    }
    asterisk_UpdateConf('extensions.conf');
    asterisk_Reload();
    echo json_encode($response);
}
function IncomingCalls_Ajax()
{
    global $mysqli;
    $session =& $_SESSION['IncomingCallsAjax'];
    $smarty = smarty_init(dirname(__FILE__) . '/templates');
    $data = $_POST;
    $response = array();
    switch ($data['Action']) {
        case 'DeleteRule':
            $query = "DELETE FROM IncomingRules WHERE PK_IncomingRule = " . intval($data['ID']) . " LIMIT 1";
            $mysqli->query($query) or die($mysqli->error);
            $response['ID'] = $data['ID'];
            break;
        case 'SaveRule':
            // Validation
            if ($data['Digits'] == "") {
                $errors['Digits']['Invalid'] = true;
            } elseif (intval($data['Digits']) . "" != $data['Digits']) {
                $errors['Digits']['Invalid'] = true;
            } elseif (strlen($data['Digits']) > 20) {
                $errors['Digits']['Invalid'] = true;
            }
            if ($data['BlockType'] == 'undefined') {
                if ($data['Extension'] == "") {
                    $errors['Extension']['Invalid'] = true;
                } elseif (intval($data['Extension']) . "" != $data['Extension']) {
                    $errors['Extension']['Invalid'] = true;
                } elseif (strlen($data['Extension']) < 3 || strlen($data['Extension']) > 5) {
                    $errors['Extension']['Invalid'] = true;
                }
            }
            // Update
            if (count($errors) == 0) {
                $query = "\n\t\t\t\t\tUPDATE\n\t\t\t\t\t\tIncomingRules\n\t\t\t\t\tSET\n\t\t\t\t\t\tSubject         = '" . $mysqli->real_escape_string($data['Subject']) . "',\n\t\t\t\t\t\tDigits          = '" . $mysqli->real_escape_string($data['Digits']) . "',\n\t\t\t\t\t\tExtension       = '" . $mysqli->real_escape_string($data['Extension']) . "',\n\t\t\t\t\t\t" . ($data['BlockType'] != 'undefined' ? "BlockType = '" . $mysqli->real_escape_string($data['BlockType']) . "'," : '') . "\n\t\t\t\t\t\t\tFK_Timeframe    =  " . intval($data['FK_Timeframe']) . "\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tPK_IncomingRule =  " . intval($data['ID']) . "\n\t\t\t\t\tLIMIT 1\n\t\t\t\t";
                $mysqli->query($query) or die($mysqli->error);
            }
            $response['ID'] = $data['ID'];
            $response['Errors'] = $errors;
            break;
        case 'UpdateRuleOrder':
            $order = 1;
            foreach ($_REQUEST['Rules'] as $PK_Rule) {
                $PK_Rule = explode('_', $PK_Rule);
                $PK_Rule = $PK_Rule[1];
                $response['test'] .= "{$order}-{$PK_Rule} , ";
                $query = "UPDATE IncomingRules SET RuleOrder = {$order} WHERE PK_IncomingRule = {$PK_Rule} LIMIT 1";
                $mysqli->query($query) or die($mysqli->error);
                $order++;
            }
            break;
        case 'DeleteRoute':
            $query = "DELETE FROM IncomingRoutes WHERE PK_IncomingRoute = " . intval($data['ID']) . " LIMIT 1";
            $mysqli->query($query) or die($mysqli->error);
            $response['ID'] = $data['ID'];
            break;
        case 'SaveRoute':
            // Validation
            $errors = array();
            $aux = explode('~', $data['Provider'], 2);
            $data['ProviderType'] = $aux[0];
            $data['ProviderID'] = $aux[1];
            if ($data['StartNumber'] == "") {
                $errors['StartNumber']['Invalid'] = true;
            } elseif (!preg_match('/^[#*]{0,1}[0-9]{1,31}$/', $data['StartNumber'])) {
                $errors['StartNumber']['Invalid'] = true;
            }
            if ($data['RouteType'] == 'multiple') {
                if ($data['EndNumber'] == "") {
                    $errors['EndNumber']['Invalid'] = true;
                } elseif (!preg_match('/^[#*]{0,1}[0-9]{1,31}$/', $data['EndNumber'])) {
                    $errors['EndNumber']['Invalid'] = true;
                }
                if (!preg_match('/^[0-9]+$/', $data['TrimFront'])) {
                    $errors['TrimFront']['Invalid'] = true;
                }
                if (!preg_match('/^[0-9]+$/', $data['Add'])) {
                    $errors['Add']['Invalid'] = true;
                }
            }
            // Update
            if (count($errors) == 0) {
                $query = "\n\t\t\t\t\tUPDATE\n\t\t\t\t\t\tIncomingRoutes\n\t\t\t\t\tSET\n\t\t\t\t\t\t" . ($data['EndNumber'] != 'undefined' ? "EndNumber   = '" . $mysqli->real_escape_string($data['EndNumber']) . "'," : '') . "\n\t\t\t\t\t\t" . ($data['TrimFront'] != 'undefined' ? "TrimFront   = " . intval($data['TrimFront']) . "," : '') . "\n\t\t\t\t\t\t" . ($data['Add'] != 'undefined' ? "`Add`         = " . intval($data['Add']) . "," : '') . "\n\t\t\t\t\t\t" . ($data['Extension'] != 'undefined' ? "Extension   = '" . $mysqli->real_escape_string($data['Extension']) . "'," : '') . "\n\t\t\t\t\t\tStartNumber  = '" . $mysqli->real_escape_string($data['StartNumber']) . "',\n\t\t\t\t\t\tProviderType = '" . $mysqli->real_escape_string($data['ProviderType']) . "',\n\t\t\t\t\t\tProviderID   = '" . $mysqli->real_escape_string($data['ProviderID']) . "'\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tPK_IncomingRoute =  " . intval($data['ID']) . "\n\t\t\t\t\tLIMIT 1\n\t\t\t\t";
                $mysqli->query($query);
                # or die($mysqli->error);
            }
            $response['ID'] = $data['ID'];
            $response['Errors'] = $errors;
            break;
    }
    asterisk_UpdateConf('extensions.conf');
    asterisk_Reload();
    echo json_encode($response);
}