function deleteExten($smarty, $module_name, $local_templates_dir, $pDB, $arrConf, $credentials)
{
    $pExten = new paloSantoExtensions($pDB);
    $error = "";
    $continuar = true;
    $exito = false;
    $idExten = getParameter("id_exten");
    //obtenemos la informacion de la extension por el id dado, en caso de que la extensionpertenzca a un usuario activo
    //esta no puede volver a ser borrada
    if (!isset($idExten)) {
        $smarty->assign("mb_title", _tr("ERROR"));
        $smarty->assign("mb_message", _tr("Invalid Exten"));
        return reportExten($smarty, $module_name, $local_templates_dir, $pDB, $arrConf, $credentials);
    }
    $domain = getParameter('organization');
    if ($credentials['userlevel'] != 'superadmin') {
        $domain = $credentials['domain'];
    }
    $arrExten = $pExten->getExtensionById($idExten, $domain);
    if ($arrExten === false) {
        $smarty->assign("mb_title", _tr("ERROR"));
        $smarty->assign("mb_message", _tr($pExten->errMsg));
        return reportExten($smarty, $module_name, $local_templates_dir, $pDB, $arrConf, $credentials);
    } else {
        if (count($arrExten) == 0) {
            $smarty->assign("mb_title", _tr("ERROR"));
            $smarty->assign("mb_message", _tr("Extension doesn't exist"));
            return reportExten($smarty, $module_name, $local_templates_dir, $pDB, $arrConf, $credentials);
        } else {
            //commprobamos que la extension no le pertenezca a nigun usuario
            $query = "Select username from acl_user where extension=? and id_group in (select g.id from acl_group g join organization o on g.id_organization=o.id where o.domain=?)";
            $result = $pDB->getFirstRowQuery($query, false, array($arrExten["exten"], $arrExten["domain"]));
            if ($result === false) {
                $error = $pDB->errMsg;
            } elseif (count($result) > 0) {
                $error = _tr("Extension can't be deleted because bellow to user ") . $result[0];
            } else {
                $pDevice = new paloDevice($domain, $arrExten["technology"], $pDB);
                $pDB->beginTransaction();
                $exito = $pDevice->deleteExtension($arrExten["exten"]);
                if ($exito) {
                    $pDB->commit();
                    //recargamos la configuracion en realtime para que tomen efecto los cambios hechos en el dispositivo
                    $pDevice->tecnologia->prunePeer($arrExten["device"], $arrExten["technology"]);
                } else {
                    $pDB->rollBack();
                }
            }
        }
    }
    if ($exito) {
        $smarty->assign("mb_title", _tr("MESSAGE"));
        $smarty->assign("mb_message", _tr("The extensions was deleted successfully"));
        //mostramos el mensaje para crear los archivos de configuracion
        $pAstConf = new paloSantoASteriskConfig($pDB);
        $pAstConf->setReloadDialplan($domain, true);
    } else {
        $smarty->assign("mb_title", _tr("ERROR"));
        $smarty->assign("mb_message", _tr($error));
    }
    return reportExten($smarty, $module_name, $local_templates_dir, $pDB, $arrConf, $credentials);
}
 function deleteUserOrganization($idUser)
 {
     $pACL = new paloACL($this->_DB);
     $pEmail = new paloEmail($this->_DB);
     $pFax = new paloFax($this->_DB);
     $Exito = false;
     //1)se comprueba de que el ID de USUARIO se un numero
     //2)se verifica que exista dicho usuario
     //3)se recompila los datos del usuario de las tablas acl_user y user_properties
     //4)se elimina al usuario de la base
     //5)se elimina la extension de uso del usuario y la extension de fax
     //6)se trata de eliminar la cuenta de fax
     //7)se elimina el buzon de correo
     if (!preg_match('/^[[:digit:]]+$/', "{$idUser}")) {
         $this->errMsg = _tr("User ID is not numeric");
         return false;
     } else {
         $arrUser = $pACL->getUsers($idUser);
         if ($arrUser === false || count($arrUser) == 0) {
             $this->errMsg = _tr("User dosen't exist");
             return false;
         }
     }
     $idDomain = $arrUser[0][4];
     $query = "Select domain from organization where id=?";
     $getDomain = $this->_DB->getFirstRowQuery($query, false, array($idDomain));
     if ($getDomain == false) {
         $this->errMsg = $this->_DB->errMsg;
         return false;
     }
     $pDevice = new paloDevice($getDomain[0], "sip", $this->_DB);
     $arrExten = $pDevice->getExtension($arrUser[0][5]);
     $faxList = $pFax->getFaxList($arrUser[0][6], $getDomain[0]);
     $arrFaxExten = $faxList[0];
     $this->_DB->beginTransaction();
     //tomamos un backup de las extensiones que se van a eliminar de la base astDB por si algo sale mal
     //y ahi que restaurar la extension
     $arrExt = $pDevice->backupAstDBEXT($arrUser[0][5]);
     if ($pDevice->deleteExtension($arrUser[0][5])) {
         if ($pFax->deleteFaxByUser($idUser)) {
             if ($pACL->deleteUser($idUser)) {
                 if ($pEmail->deleteAccount($arrUser[0][1])) {
                     $Exito = true;
                     $this->_DB->commit();
                     $pDevice->tecnologia->prunePeer($arrExten["device"], $arrExten["tech"]);
                     $pDevice->tecnologia->prunePeer($arrFaxExten["device"], $arrFaxExten["tech"]);
                     $pFax->restartService();
                 } else {
                     $this->errMsg = _tr("Email Account cannot be deleted") . $pEmail->errMsg;
                     $this->_DB->rollBack();
                     $pDevice->restoreBackupAstDBEXT($arrExt);
                     $pFax->createFaxFileConfig($arrFaxExten['dev_id'], $getDomain[0]);
                 }
             } else {
                 $this->errMsg = $pACL->errMsg;
                 $this->_DB->rollBack();
                 $pDevice->restoreBackupAstDBEXT($arrExt);
                 $pFax->createFaxFileConfig($arrFaxExten['dev_id'], $getDomain[0]);
             }
         } else {
             $this->errMsg = _tr("Fax cannot be deleted") . $pFax->errMsg;
             $this->_DB->rollBack();
             $pDevice->restoreBackupAstDBEXT($arrExt);
         }
     } else {
         $this->errMsg = _tr("User Extension can't be deleted") . $pDevice->errMsg;
         $this->_DB->rollBack();
         $pDevice->restoreBackupAstDBEXT($arrExt);
     }
     return $Exito;
 }
 function createFaxToUser($arrProp)
 {
     require_once 'libs/paloSantoPBX.class.php';
     $tech = 'iax2';
     $pACL = new paloACL($this->_DB);
     // 1) Averiguar el numero de dispositivo que se puede usar
     if (!isset($arrProp['devId'])) {
         $devId = $this->getNewDevID();
         if ($devId == false) {
             $this->errMsg = _tr("Error to get Fax Device Identifier");
             return false;
         }
     } else {
         $devId = $arrProp['devId'];
     }
     if (!isset($arrProp['port'])) {
         $port = $this->getNextAvailablePort();
         if ($port == false) {
             $this->errMsg = _tr("Error to get Port for Fax");
             return false;
         }
     } else {
         $port = $arrProp['port'];
     }
     // 2) obtenemos los datos dle usuario para el cual se esta creando el fax
     //    comprobando de que este realmente exista
     if (isset($arrProp['idUser'])) {
         if (empty($arrProp['idUser'])) {
             $this->errMsg = _tr("Invalid User");
             return false;
         }
     } else {
         $this->errMsg = _tr("Invalid User");
         return false;
     }
     $arrUser = $pACL->getUsers2($arrProp['idUser']);
     if ($arrUser === false) {
         $this->errMsg = _tr("An error has occured when retrieved user data.");
         return false;
     } elseif (count($arrUser) == 0) {
         $this->errMsg = _tr("User does not exist.");
         return false;
     }
     $user = $arrUser[0];
     //cuando de crea un fax para un usario el nombre del peer usado para el fax
     //es igual code_username. Donde username es el useraname sin la parte del @dominio
     $username = strstr($user['username'], '@', true);
     $domain = $this->getDomainOrganization($user["id_organization"]);
     // 3) debemos crear el peer para el fax y ademas comprabar que la extension pasada como parametrono se este usando
     //    para los faxes siempre se usa tecnologia iax
     $pDevice = new paloDevice($domain, $tech, $this->_DB);
     if (!$pDevice->validatePaloDevice()) {
         $this->errMsg = $pDevice->errMsg;
         return false;
     }
     $orgCode = $pDevice->getCode();
     $extension = $user["fax_extension"];
     $device = "{$username}FX_{$orgCode}";
     if ($pDevice->existDevice($extension, $device, $tech) == true) {
         $this->errMsg = "Error Fax Number. " . $pDevice->errMsg;
         return false;
     }
     $clid_name = isset($arrProp['clid_name']) ? $arrProp['clid_name'] : $extension;
     if (!preg_match("/^[[:alnum:]_[:space:]-]+\$/", $clid_name)) {
         $clid_name = $extension;
     }
     $clid_number = isset($arrProp['clid_number']) ? $arrProp['clid_number'] : $extension;
     if (!preg_match("/^[[:alnum:]_[:space:]-]+\$/", $clid_number)) {
         $clid_number = $extension;
     }
     //creamos el peer
     $arrPeer = $arrProp;
     $arrPeer["name"] = "{$username}FX";
     $arrPeer["defaultip"] = "127.0.0.1";
     $arrPeer['secret'] = $user["md5_password"];
     $arrPeer["fullname"] = $clid_name;
     $arrPeer["cid_number"] = $clid_number;
     $arrPeer["port"] = $port;
     $arrPeer['dial'] = strtoupper($tech) . "/" . $device;
     $arrPeer['organization_domain'] = $domain;
     $pDevice->tecnologia->setGroupProp($arrPeer, $domain);
     if (empty($pDevice->tecnologia->context)) {
         $pDevice->tecnologia->context = 'from-internal';
     }
     $arrPeer['context'] = $pDevice->tecnologia->context;
     if ($pDevice->tecnologia->insertDB() == false) {
         $this->errMsg = "Error setting parameter {$type} device " . $pDevice->tecnologia->errMsg;
         return false;
     }
     $RT = 15;
     if (isset($ringTime)) {
         if (preg_match("/^[[:digit:]]+\$/", $arrProp['ringTime']) && ($arrProp['ringTime'] > 0 && $arrProp['ringTime'] < 60)) {
             $RT = $arrProp['ringTime'];
         }
     }
     // 3) creamos el registro en la tabla fax
     if (!$this->insertFaxDB($domain, $arrPeer['context'], $extension, $tech, $arrPeer['dial'], $device, $RT, $clid_name, $clid_number, $arrProp['area_code'], $arrProp['country_code'], $port, $devId, $arrProp['fax_content'], $arrProp['fax_subject'], $user['username'])) {
         return false;
     }
     // 4) Añadir el fax a los archivos de configuración
     if ($this->addFaxConfiguration($port, $devId, $arrProp['country_code'], $arrProp['area_code'], $clid_name, $clid_number, $device, $user['md5_password'], $user['username'])) {
         return true;
     } else {
         $this->errMsg = _tr("Error to create fax") . " " . $this->errMsg;
         return false;
     }
 }