Example #1
0
function load_endpoint_from_csv($smarty, $arrLang, $ruta_archivo_csv, $base_dir, $dsnAsterisk, $dsnSqlite, $module_name, $local_templates_dir, $arrConf)
{
    $paloEndPoint = new paloSantoEndPoint($dsnAsterisk, $dsnSqlite);
    $arrEndpointsConf = $paloEndPoint->listEndpointConf();
    $arrVendor = $paloEndPoint->listVendor();
    $endpoint_mask = isset($_POST['endpoint_mask']) ? $_POST['endpoint_mask'] : network();
    $pValidator = new PaloValidar();
    $arrFindVendor = array();
    //variable de ayuda, para llamar solo una vez la funcion createFilesGlobal de cada vendor
    $arrayColumnas = array();
    $result = isValidCSV($arrLang, $ruta_archivo_csv, $arrayColumnas);
    if ($result != "valided") {
        $smarty->assign("mb_title", _tr('ERROR') . ":");
        $smarty->assign("mb_message", $result);
        return false;
    }
    if (!$pValidator->validar('endpoint_mask', $endpoint_mask, 'ip/mask')) {
        $smarty->assign("mb_title", _tr('ERROR') . ":");
        $smarty->assign("mb_message", _tr('Invalid Format IP address'));
        return false;
    }
    $pattonDevices = $paloEndPoint->getPattonDevices();
    $arrles = $paloEndPoint->endpointMap($endpoint_mask, $arrVendor, $arrEndpointsConf, $pattonDevices, true);
    if (!(is_array($arrles) && count($arrles) > 0)) {
        $smarty->assign("mb_title", _tr('ERROR') . ":");
        $smarty->assign("mb_message", _tr("There weren't  endpoints in the subnet."));
        return false;
    }
    $hArchivo = fopen($ruta_archivo_csv, 'r+');
    $lineProcessed = 0;
    $line = 0;
    $msg = "";
    if ($hArchivo) {
        $paloFileEndPoint = new PaloSantoFileEndPoint($arrConf["tftpboot_path"], $endpoint_mask);
        //Linea 1 header ignorada
        $tupla = fgetcsv($hArchivo, 4096, ",");
        //Desde linea 2 son datos
        while ($tupla = fgetcsv($hArchivo, 4096, ",")) {
            $line++;
            if (is_array($tupla) && count($tupla) >= 4) {
                $arrEndpoint = csv2Array($tupla, $arrayColumnas);
                if ($arrEndpoint['data'] == null) {
                    $msg .= _tr("Line") . " {$line}: {$arrEndpoint['msg']} <br />";
                } else {
                    $name_model = $arrEndpoint['data']['Model'];
                    $extension = $arrEndpoint['data']['Ext'];
                    $MAC = $arrEndpoint['data']['MAC'];
                    $macTMP = strtolower($MAC);
                    $macTMP = str_replace(":", "", $macTMP);
                    if (isset($arrles[$macTMP])) {
                        // Si el endpoint fue encontrado en la red.
                        $currentEndpointIP = $arrles[$macTMP]['ip_adress'];
                        $tech = $paloEndPoint->getTech($extension);
                        $freePBXParameters = $paloEndPoint->getDeviceFreePBXParameters($extension, $tech);
                        if (!(is_array($freePBXParameters) && count($freePBXParameters) > 0)) {
                            $msg .= _tr("Line") . " {$line}: " . _tr("Extension") . "  {$extension} (tech:{$tech})" . _tr("has not been created.") . "<br />";
                            continue;
                        }
                        $dataVendor = $paloEndPoint->getVendor(substr($MAC, 0, 8));
                        if ($dataVendor["name"] == "Grandstream") {
                            $arr = $paloFileEndPoint->getModelElastix("admin", "admin", $currentEndpointIP, 2);
                            if ($arr) {
                                $endpointElastix = $paloEndPoint->getVendorByName("Elastix");
                                $dataVendor["id"] = $endpointElastix["id"];
                                $dataVendor["name"] = $endpointElastix["name"];
                            }
                        }
                        $dataModel = $paloEndPoint->getModelByVendor($dataVendor["id"], $name_model);
                        if (!(is_array($dataVendor) && count($dataVendor) > 0)) {
                            $msg .= _tr("Line") . " {$line}: Vendor {$dataVendor['name']}" . _tr("not supported.") . "<br />";
                            continue;
                        }
                        if (!(is_array($dataModel) && count($dataModel) > 0)) {
                            //No existe el modelo
                            $msg .= _tr("Line") . "{$line}: Model {$name_model} of vendor {$dataVendor['name']}" . _tr("not supported.") . "<br />";
                            continue;
                        }
                        $tmpEndpoint['id_device'] = $freePBXParameters['id_device'];
                        $tmpEndpoint['desc_device'] = $freePBXParameters['desc_device'];
                        $tmpEndpoint['account'] = $freePBXParameters['account_device'];
                        $tmpEndpoint['secret'] = $freePBXParameters['secret_device'];
                        $tmpEndpoint['id_model'] = $dataModel["id"];
                        $tmpEndpoint['mac_adress'] = $MAC;
                        $tmpEndpoint['id_vendor'] = $dataVendor["id"];
                        $tmpEndpoint['name_vendor'] = $dataVendor["name"];
                        $tmpEndpoint['ip_adress'] = $currentEndpointIP;
                        $tmpEndpoint['comment'] = "Nada";
                        $arrParametersOld = $paloEndPoint->getParameters($MAC);
                        $arrParameters = $paloFileEndPoint->updateArrParameters($dataVendor["name"], $name_model, $arrParametersOld);
                        $tmpEndpoint['arrParameters'] = array_merge($arrParameters, $arrEndpoint['data']);
                        if ($paloEndPoint->createEndpointDB($tmpEndpoint)) {
                            //verifico si la funcion createFilesGlobal del vendor ya fue ejecutado
                            if (!in_array($dataVendor["name"], $arrFindVendor)) {
                                if ($paloFileEndPoint->createFilesGlobal($dataVendor["name"])) {
                                    $arrFindVendor[] = $dataVendor["name"];
                                }
                            }
                            //escribir archivos
                            $ArrayData['vendor'] = $dataVendor["name"];
                            $ArrayData['data'] = array("filename" => strtolower(str_replace(":", "", $MAC)), "DisplayName" => $tmpEndpoint['desc_device'], "id_device" => $tmpEndpoint['id_device'], "secret" => $tmpEndpoint['secret'], "model" => $dataModel['name'], "ip_endpoint" => $tmpEndpoint['ip_adress'], "arrParameters" => $tmpEndpoint['arrParameters'], "tech" => $tech);
                            if (!$paloFileEndPoint->createFiles($ArrayData)) {
                                if (isset($paloFileEndPoint->errMsg)) {
                                    $msg .= _tr("Line") . "{$line}: " . _tr("{$paloFileEndPoint->errMsg}.") . "<br />";
                                } else {
                                    $msg .= _tr("Line") . "{$line}: " . _tr("Error, Technology Device (SIP/IAX) not supported on endpoint.") . "<br />";
                                }
                            } else {
                                $lineProcessed++;
                            }
                        }
                    } else {
                        $v = isset($dataVendor['name']) ? $dataVendor['name'] : $arrEndpoint['data']['Vendor'];
                        $msg .= _tr("Line") . "{$line}: " . _tr("Endpoint wasn't founded in subnet.") . "(vendor:{$v} - model:{$name_model} - Ext:{$extension})<br />";
                    }
                }
            }
        }
        $smarty->assign("mb_title", _tr('Resume') . ":");
        $msg = _tr("Total endpoint processed") . ": {$lineProcessed} <br /> <br />{$msg}";
        $smarty->assign("mb_message", $msg);
        unlink($ruta_archivo_csv);
    }
    return true;
}
Example #2
0
function load_address_book_from_csv($smarty, $arrLang, $ruta_archivo, $pDB, $pDB_2)
{
    $Messages = "";
    $arrayColumnas = array();
    $pACL = new paloACL($pDB_2);
    $id_user = $pACL->getIdUser($_SESSION["elastix_user"]);
    $result = isValidCSV($arrLang, $ruta_archivo, $arrayColumnas);
    if ($result != 'true') {
        $smarty->assign("mb_title", $arrLang["Error"]);
        $smarty->assign("mb_message", $result);
        return;
    }
    $hArchivo = fopen($ruta_archivo, 'rt');
    $cont = 0;
    $pAdressBook = new paloAdressBook($pDB);
    if ($hArchivo) {
        //Linea 1 header ignorada
        $tupla = fgetcsv($hArchivo, 4096, ",");
        //Desde linea 2 son datos
        while ($tupla = fgetcsv($hArchivo, 4096, ",")) {
            if (is_array($tupla) && count($tupla) >= 3) {
                $data = array();
                $namedb = $tupla[$arrayColumnas[0]];
                $last_namedb = $tupla[$arrayColumnas[1]];
                $telefonodb = $tupla[$arrayColumnas[2]];
                $emaildb = isset($arrayColumnas[3]) ? $tupla[$arrayColumnas[3]] : "";
                $addressdb = isset($arrayColumnas[4]) ? $tupla[$arrayColumnas[4]] : "";
                $companydb = isset($arrayColumnas[5]) ? $tupla[$arrayColumnas[5]] : "";
                $statusdb = "isPrivate";
                $iduserdb = $id_user;
                $data = array($namedb, $last_namedb, $telefonodb, $emaildb, $iduserdb, $addressdb, $companydb, $statusdb);
                //Paso 1: verificar que no exista un usuario con los mismos datos
                $result = $pAdressBook->existContact($namedb, $last_namedb, $telefonodb);
                if (!$result) {
                    $Messages .= "{$arrLang["ERROR"]}:" . $pAdressBook->errMsg . "  <br />";
                } else {
                    if ($result['total'] > 0) {
                        $Messages .= "{$arrLang["ERROR"]}: {$arrLang["Contact Data already exists"]}: {$data['name']} <br />";
                    } else {
                        //Paso 2: creando en la contact data
                        if (!$pAdressBook->addContactCsv($data)) {
                            $Messages .= $arrLang["ERROR"] . $pDB->errMsg . "<br />";
                        }
                        $cont++;
                    }
                }
            }
        }
        $Messages .= $arrLang["Total contacts created"] . ": {$cont}<br />";
        $smarty->assign("mb_message", $Messages);
    }
    unlink($ruta_archivo);
}
Example #3
0
function load_extension_from_csv($smarty, $ruta_archivo, $base_dir, $pDB, $arrAST, $arrAMP)
{
    $Messages = "";
    $arrayColumnas = array();
    $data_connection = array('host' => "127.0.0.1", 'user' => "admin", 'password' => obtenerClaveAMIAdmin());
    $result = isValidCSV($pDB, $ruta_archivo, $arrayColumnas);
    if ($result != "valided") {
        $smarty->assign("mb_message", $result);
        return;
    }
    $hArchivo = fopen($ruta_archivo, 'r+');
    $cont = 0;
    $pLoadExtension = new paloSantoLoadExtension($pDB);
    if ($hArchivo) {
        //Linea 1 header ignorada
        $tupla = fgetcsv($hArchivo, 4096, ",");
        $prueba = count($tupla);
        //Desde linea 2 son datos
        while ($tupla = fgetcsv($hArchivo, 4096, ",")) {
            if (is_array($tupla) && count($tupla) >= 3) {
                $Name = $tupla[$arrayColumnas[0]];
                $Ext = $tupla[$arrayColumnas[1]];
                $Direct_DID = isset($arrayColumnas[2]) ? $tupla[$arrayColumnas[2]] : "";
                $Outbound_CID = isset($arrayColumnas[3]) ? $tupla[$arrayColumnas[3]] : "";
                $Call_Waiting = isset($arrayColumnas[4]) ? $tupla[$arrayColumnas[4]] : "";
                $Secret = $tupla[$arrayColumnas[5]];
                $VoiceMail = isset($arrayColumnas[6]) ? $tupla[$arrayColumnas[6]] : "";
                $VoiceMail_PW = isset($arrayColumnas[7]) ? $tupla[$arrayColumnas[7]] : "";
                $VM_Email_Address = isset($arrayColumnas[8]) ? $tupla[$arrayColumnas[8]] : "";
                $VM_Pager_Email_Addr = isset($arrayColumnas[9]) ? $tupla[$arrayColumnas[9]] : "";
                $VM_Options = isset($arrayColumnas[10]) ? $tupla[$arrayColumnas[10]] : "";
                $VM_EmailAttachment = isset($arrayColumnas[11]) ? $tupla[$arrayColumnas[11]] : "";
                $VM_Play_CID = isset($arrayColumnas[12]) ? $tupla[$arrayColumnas[12]] : "";
                $VM_Play_Envelope = isset($arrayColumnas[13]) ? $tupla[$arrayColumnas[13]] : "";
                $VM_Delete_Vmail = isset($arrayColumnas[14]) ? $tupla[$arrayColumnas[14]] : "";
                $Context = isset($arrayColumnas[15]) ? $tupla[$arrayColumnas[15]] : "from-internal";
                if (trim($Context) == "") {
                    $Context = "from-internal";
                }
                $Tech = strtolower($tupla[$arrayColumnas[16]]);
                $Callgroup = isset($arrayColumnas[17]) ? $tupla[$arrayColumnas[17]] : "";
                $Pickupgroup = isset($arrayColumnas[18]) ? $tupla[$arrayColumnas[18]] : "";
                $Disallow = isset($arrayColumnas[19]) ? $tupla[$arrayColumnas[19]] : "";
                $Allow = isset($arrayColumnas[20]) ? $tupla[$arrayColumnas[20]] : "";
                $Deny = isset($arrayColumnas[21]) ? $tupla[$arrayColumnas[21]] : "";
                $Permit = isset($arrayColumnas[22]) ? $tupla[$arrayColumnas[22]] : "";
                $Record_Incoming = isset($arrayColumnas[23]) ? $tupla[$arrayColumnas[23]] : "";
                $Record_Outgoing = isset($arrayColumnas[24]) ? $tupla[$arrayColumnas[24]] : "";
                //////////////////////////////////////////////////////////////////////////////////
                $Record_Incoming = strtolower($Record_Incoming);
                $Record_Outgoing = strtolower($Record_Outgoing);
                if (preg_match("/^(on demand|adhoc)/", $Record_Incoming)) {
                    $Record_Incoming = "Adhoc";
                } elseif (preg_match("/^always/", $Record_Incoming)) {
                    $Record_Incoming = "always";
                } elseif (preg_match("/^never/", $Record_Incoming)) {
                    $Record_Incoming = "never";
                }
                if (preg_match("/(on demand|adhoc)/", $Record_Outgoing)) {
                    $Record_Outgoing = "Adhoc";
                } elseif (preg_match("/^always/", $Record_Outgoing)) {
                    $Record_Outgoing = "always";
                } elseif (preg_match("/^never/", $Record_Outgoing)) {
                    $Record_Outgoing = "never";
                }
                //////////////////////////////////////////////////////////////////////////////////
                // validando para que coja las comillas
                $Outbound_CID = preg_replace('/“/', "\"", $Outbound_CID);
                $Outbound_CID = preg_replace('/”/', "\"", $Outbound_CID);
                //////////////////////////////////////////////////////////////////////////////////
                //Paso 1: creando en la tabla sip - iax
                if (!$pLoadExtension->createTechDevices($Ext, $Secret, $VoiceMail, $Context, $Tech, $Disallow, $Allow, $Deny, $Permit, $Callgroup, $Pickupgroup, $Record_Incoming, $Record_Outgoing)) {
                    $Messages .= "Ext: {$Ext} - " . _tr('Error updating Tech') . ": " . $pLoadExtension->errMsg . "<br />";
                } else {
                    //Paso 2: creando en la tabla users
                    if (!$pLoadExtension->createUsers($Ext, $Name, $VoiceMail, $Direct_DID, $Outbound_CID, $Record_Incoming, $Record_Outgoing)) {
                        $Messages .= "Ext: {$Ext} - " . _tr('Error updating Users') . ": " . $pLoadExtension->errMsg . "<br />";
                    }
                    //Paso 3: creando en la tabla devices
                    if (!$pLoadExtension->createDevices($Ext, $Tech, $Name)) {
                        $Messages .= "Ext: {$Ext} - " . _tr('Error updating Devices') . ": " . $pLoadExtension->errMsg . "<br />";
                    }
                    //Paso 4: creando en el archivo /etc/asterisk/voicemail.conf los voicemails
                    if (!$pLoadExtension->writeFileVoiceMail($Ext, $Name, $VoiceMail, $VoiceMail_PW, $VM_Email_Address, $VM_Pager_Email_Addr, $VM_Options, $VM_EmailAttachment, $VM_Play_CID, $VM_Play_Envelope, $VM_Delete_Vmail)) {
                        $Messages .= "Ext: {$Ext} - " . _tr('Error updating Voicemail') . "<br />";
                    }
                    //Paso 5: Configurando el call waiting
                    if (!$pLoadExtension->processCallWaiting($Call_Waiting, $Ext)) {
                        $Messages .= "Ext: {$Ext} - " . _tr('Error processing CallWaiting') . "<br />";
                    }
                    $outboundcid = preg_replace("/\"/", "'", $Outbound_CID);
                    $outboundcid = preg_replace("/\"/", "'", $outboundcid);
                    $outboundcid = preg_replace("/ /", "", $outboundcid);
                    if (!$pLoadExtension->putDataBaseFamily($data_connection, $Ext, $Tech, $Name, $VoiceMail, $outboundcid, $Record_Incoming, $Record_Outgoing)) {
                        $Messages .= "Ext: {$Ext} - " . _tr('Error processing Database Family') . "<br />";
                    }
                    $cont++;
                }
                ////////////////////////////////////////////////////////////////////////
                //Paso 7: Escribiendo en tabla incoming
                if ($Direct_DID !== "") {
                    if (!$pLoadExtension->createDirect_DID($Ext, $Direct_DID)) {
                        $Messages .= "Ext: {$Ext} - " . _tr('Error to insert or update Direct DID') . "<br />";
                    }
                }
                /////////////////////////////////////////////////////////////////////////
            }
        }
        //Paso 6: Realizo reload
        if (!$pLoadExtension->do_reloadAll($data_connection, $arrAST, $arrAMP)) {
            $Messages .= $pLoadExtension->errMsg;
        }
        $Messages .= _tr('Total extension updated') . ": {$cont}<br />";
        $smarty->assign("mb_message", $Messages);
    }
    unlink($ruta_archivo);
}