Esempio n. 1
0
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;
}