function validateParameterEndpoint($arrParameters, $module_name, $dsnAsterisk, $dsnSqlite) { // Listar todos los proveedores disponibles $base_dir = dirname($_SERVER['SCRIPT_FILENAME']); $sVendorCfgDir = "{$base_dir}/modules/{$module_name}/libs/vendors"; if (!is_dir($sVendorCfgDir)) { return _tr('Vendor configuration directory not found!'); } $h = opendir($sVendorCfgDir); $vendorList = array(); while (($s = readdir($h)) !== false) { $regs = NULL; if (preg_match('/^(.+)\\.cfg\\.php/', $s, $regs)) { $vendorList[] = $regs[1]; } } closedir($h); $paloEndPoint = new paloSantoEndPoint($dsnAsterisk, $dsnSqlite); $arrDeviceFreePBX = $paloEndPoint->getDeviceFreePBX(); $arrDeviceFreePBXAll = $paloEndPoint->getDeviceFreePBX(true); $error = false; foreach ($arrParameters as $key => $values) { if (substr($key, 0, 6) == "epmac_") { //encontre una mac seleccionada entoces por forma empirica con ayuda del mac_adress obtego los parametros q se relacionan con esa mac. $tmpMac = substr($key, 6); $macExists = false; foreach ($_SESSION["elastix_endpoints"] as $endpoint) { if ($endpoint[2] == $tmpMac) { $macExists = true; break; } } if (!$macExists) { $error .= _tr("The mac was not found") . ": {$tmpMac}<br />"; } else { // Revisar que la subcadena sea realmente una dirección MAC if (!preg_match('/^((([[:xdigit:]]){2}:){5}([[:xdigit:]]){2})$/i', $tmpMac)) { $error .= "Invalid MAC address for endpoint<br />"; } $tmpDevice = $arrParameters["id_device_{$tmpMac}"]; $tmpModel = $arrParameters["id_model_device_{$tmpMac}"]; $tmpVendor = $arrParameters["name_vendor_device_{$tmpMac}"]; $tmpidVendor = $arrParameters["id_vendor_device_{$tmpMac}"]; $tmpModelsVendor = $paloEndPoint->getAllModelsVendor($tmpVendor); if (!array_key_exists($tmpModel, $tmpModelsVendor)) { $error .= "The model entered does not exist or does not belong to this vendor. <br />"; } if ($tmpVendor == "Elastix") { $endpointElastix = $paloEndPoint->getVendorByName("Grandstream"); $tmpVendor = $endpointElastix["name"]; $tmpidVendor = $endpointElastix["id"]; } $dataVendor = $paloEndPoint->getVendor(substr($tmpMac, 0, 8)); if (!isset($dataVendor["name"]) || $dataVendor["name"] != $tmpVendor || !isset($dataVendor["id"]) || $dataVendor["id"] != $tmpidVendor) { $error .= "The id or/and name of vendor do not match with the mac address. <br />"; } if (isset($tmpModel) && $tmpModel != "") { if ($paloEndPoint->modelSupportIAX($tmpModel)) { $comboDevices = combo($arrDeviceFreePBXAll, $tmpDevice); if (!array_key_exists($tmpDevice, $arrDeviceFreePBXAll)) { $error .= "The assigned User Extension does not exist or is not allowed. <br />"; } } else { $comboDevices = combo($arrDeviceFreePBX, $tmpDevice); if (!array_key_exists($tmpDevice, $arrDeviceFreePBX)) { $error .= "The assigned User Extension does not exist or is not allowed. <br />"; } } } else { $comboDevices = combo(array("Select a model" => _tr("Select a model")), ""); } if ($tmpDevice == "unselected" || $tmpDevice == "no_device" || $tmpModel == "unselected" || $tmpDevice == "Select a model") { //el primero que encuentre sin seleccionar mantiene el error $error .= "The mac adress {$tmpMac} unselected Phone Type or User Extension. <br />"; } // Revisar que el vendedor es uno de los vendedores conocidos if (!in_array($tmpVendor, $vendorList)) { $error .= "Invalid or unsupported vendor<br />"; } $macWithout2Points = str_replace(":", "", $tmpMac); //PASO 2: Recorro el arreglo de la sesion para modificar y mantener los valores q el usuario ha decidido elegir asi cuando halla un error los datos persisten. if (isset($_SESSION['elastix_endpoints'])) { foreach ($_SESSION['elastix_endpoints'] as &$data) { //tomo la referencia del elemento para poder modificar su contenido por referencia. if ($data[2] == $tmpMac) { $data[0] = "<input type='checkbox' name='epmac_{$tmpMac}' checked='checked' />"; $data[5] = "<select name='id_model_device_{$tmpMac}' onchange='getDevices(this,\"{$macWithout2Points}\");'>" . combo($tmpModelsVendor, $tmpModel) . "</select>"; $data[6] = "<select name='id_device_{$tmpMac}' id='id_device_{$macWithout2Points}'>" . $comboDevices . "</select>"; } } } } } } return $error; }
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; }