function update()
 {
     $ser_id = $_REQUEST["ser_id"];
     $exp_id = $_REQUEST["exp_id"];
     $tra_id = $_REQUEST["tra_id"];
     $cue_id = $_REQUEST["cue_id"];
     // file
     $this->archivo = new tab_archivo();
     $this->archivo->setRequest2Object($_REQUEST);
     $this->archivo->setFil_id($_REQUEST['dov_id']);
     $msm_guardado_archivo = 0;
     if ($_FILES["archivo"]["error"] == 4) {
     } else {
         // Digital file
         $archivo_digital = new tab_archivo_digital();
         $archivo_type = $_FILES["archivo"]["type"];
         $archivo = $_FILES["archivo"]["tmp_name"];
         $archivo_size = $_FILES["archivo"]["size"];
         $archivo_name = $_FILES["archivo"]["name"];
         $nombre = basename($_FILES["archivo"]["name"]);
         $nombreFichero = $_FILES["archivo"]["name"];
         $archivo_name_array = explode(".", $archivo_name);
         $archivo_ext = array_pop($archivo_name_array);
         $archivo_cifrado = md5($archivo_name);
         $sis_tammax = 0;
         // Data Parameters
         $sis_tipcarga = 2;
         $nombreDirectorio = "";
         $tsistema = new tab_sistema();
         $sql = "SELECT *\r\n                FROM tab_sistema";
         $rows2 = $tsistema->dbselectBySQL($sql);
         if (count($rows2) >= 1) {
             $sis_tipcarga = $rows2[0]->sis_tipcarga;
             $sis_tammax = $rows2[0]->sis_tammax;
             $nombreDirectorio = $rows2[0]->sis_ruta;
         }
         // Verify size
         if ($archivo_size > $sis_tammax) {
             $errores["archivo"] = "El tamanio del fichero supera el limite permitido ({$maxsize} bytes)!";
             $error = true;
         } else {
             $nombreArchivo = $_FILES['archivo']['name'];
             if ($nombreArchivo == "") {
             } else {
                 if ($sis_tipcarga == 2) {
                     $error = false;
                     $copiarFichero = false;
                     if (is_uploaded_file($_FILES['archivo']['tmp_name'])) {
                         $nombreFichero = $_FILES['archivo']['name'];
                         $copiarFichero = true;
                         $nombreCompleto = $nombreDirectorio . $nombreFichero;
                         if (is_file($nombreCompleto)) {
                             $idUnico = time();
                             $nombreFichero = $idUnico . "-" . $nombreFichero;
                         }
                     } else {
                         if ($_FILES['archivo']['error'] == UPLOAD_ERR_FORM_SIZE) {
                             $maxsize = $_REQUEST['MAX_FILE_SIZE'];
                             $errores["archivo"] = "El tamanio del fichero supera el limite permitido ({$maxsize} bytes)!";
                             $error = true;
                         } else {
                             if ($_FILES['archivo']['name'] == "") {
                                 $nombreFichero = '';
                             } else {
                                 $errores["archivo"] = "No se ha podido subir el fichero!";
                                 $msm_guardado_archivo = 0;
                                 $error = true;
                             }
                         }
                     }
                     if ($error == false) {
                         // Validate
                         $dov_id = $_REQUEST['dov_id'];
                         $rows = $archivo_digital->dbselectByField("dov_id", $dov_id);
                         if (count($rows)) {
                             $sql = "UPDATE tab_archivo_digital\r\n                                    SET fil_nomoriginal='{$nombreFichero}', \r\n                                        fil_nomcifrado='{$archivo_cifrado}', \r\n                                        fil_tipo='{$archivo_type}', \r\n                                        fil_tamano='{$archivo_size}', \r\n                                        fil_extension='{$archivo_ext}', \r\n                                        nombre='{$nombre}', \r\n                                        mime='{$archivo_type}', \r\n                                        size={$archivo_size}, \r\n                                        archivo='{$nombreFichero}', \r\n                                        dov_estado=1\r\n                                    WHERE dov_id = '{$dov_id}' ";
                             $link = $archivo_digital->connect();
                             pg_query($link, "begin");
                             pg_query($link, $sql) or die(pg_last_error($link));
                             pg_query($link, "commit");
                             if ($copiarFichero) {
                                 move_uploaded_file($_FILES['archivo']['tmp_name'], $nombreDirectorio . $nombreFichero);
                             }
                             $msm_guardado_archivo = 1;
                         } else {
                             $sql = "INSERT INTO tab_archivo_digital(dov_id,\r\n                                                            fil_nomoriginal,\r\n                                                            fil_nomcifrado,\r\n                                                            fil_tipo,\r\n                                                            fil_tamano,\r\n                                                            fil_extension,\r\n                                                            nombre,\r\n                                                            mime,\r\n                                                            size,\r\n                                                            archivo,\r\n                                                            dov_estado)\r\n                                                   VALUES ({$dov_id},\r\n                                                           '{$nombreFichero}',\r\n                                                           '{$archivo_cifrado}',\r\n                                                           '{$archivo_type}',\r\n                                                           '{$archivo_size}' ,\r\n                                                           '{$archivo_ext}' ,\r\n                                                           '{$nombre}',\r\n                                                           '{$archivo_type}',\r\n                                                           {$archivo_size},\r\n                                                           '{$nombreFichero}',\r\n                                                           1)";
                             $link = $archivo_digital->connect();
                             pg_query($link, "begin");
                             pg_query($link, $sql) or die(pg_last_error($link));
                             pg_query($link, "commit");
                             if ($copiarFichero) {
                                 move_uploaded_file($_FILES['archivo']['tmp_name'], $nombreDirectorio . $nombreFichero);
                             }
                             $msm_guardado_archivo = 1;
                         }
                     }
                 } else {
                     // Validate
                     $dov_id = $_REQUEST['dov_id'];
                     $rows = $this->series->dbselectByField("dov_id", $dov_id);
                     $row = $rows[0];
                     if (count($row)) {
                         $sql = "UPDATE tab_archivo_digital\r\n                            SET fil_nomoriginal='{$archivo_name}', \r\n                            fil_nomcifrado='{$archivo_cifrado}', \r\n                            fil_tipo='{$archivo_type}', \r\n                            fil_tamano='{$archivo_size}', \r\n                            fil_extension='{$archivo_ext}', \r\n                            nombre='{$nombre}', \r\n                            archivo_oid={$oid}, \r\n                            mime='{$archivo_type}', \r\n                            size={$archivo_size}, \r\n                            archivo='{$nombreFichero}', \r\n                            dov_estado=1\r\n                            WHERE dov_id = {$dov_id} ";
                         $link = $archivo_digital->connect();
                         $fp = fopen($archivo, "rb");
                         $contenido = fread($fp, filesize($archivo));
                         fclose($fp);
                         pg_query($link, "begin");
                         $oid = pg_lo_create($link);
                         pg_query($link, $sql) or die(pg_last_error($link));
                         $blob = pg_lo_open($link, $oid, "w");
                         pg_lo_write($blob, $contenido);
                         pg_lo_close($blob);
                         pg_query($link, "commit");
                         $msm_guardado_archivo = 1;
                         //"Se subio correctamente el archivo"
                     } else {
                         $sql = "INSERT INTO tab_archivo_digital(dov_id,\r\n                                                                fil_nomoriginal,\r\n                                                                fil_nomcifrado,\r\n                                                                fil_tipo,\r\n                                                                fil_tamano,\r\n                                                                fil_extension,\r\n                                                                nombre,\r\n                                                                archivo_oid,\r\n                                                                mime,\r\n                                                                size,\r\n                                                                archivo)\r\n                                                        VALUES ({$dov_id},\r\n                                                                '{$archivo_name}',\r\n                                                                '{$archivo_cifrado}',\r\n                                                                '{$archivo_type}',\r\n                                                                '{$archivo_size}',\r\n                                                                '{$archivo_ext}',\r\n                                                                '{$nombre}',\r\n                                                                {$oid},\r\n                                                                '{$archivo_type}',\r\n                                                                {$archivo_size},\r\n                                                                '{$nombreFichero}')";
                         $link = $archivo_digital->connect();
                         $fp = fopen($archivo, "rb");
                         $contenido = fread($fp, filesize($archivo));
                         fclose($fp);
                         pg_query($link, "begin");
                         $oid = pg_lo_create($link);
                         pg_query($link, $sql) or die(pg_last_error($link));
                         $blob = pg_lo_open($link, $oid, "w");
                         pg_lo_write($blob, $contenido);
                         pg_lo_close($blob);
                         pg_query($link, "commit");
                         $msm_guardado_archivo = 1;
                     }
                 }
             }
         }
     }
     $dov_id = $_REQUEST['dov_id'];
     // Forwarding
     if ($_REQUEST['accion'] == 'guardarsinsalir') {
         $msm_guardado_archivo = 1;
         Header("Location: " . PATH_DOMAIN . "/archivo/view/" . $exp_id . "/" . $dov_id . "/" . $msm_guardado_archivo . "/");
         //        } else if ($_REQUEST ['accion'] == 'guardarnuevo') {
         //            $_SESSION['SER_ID'] = $_REQUEST['ser_id'];
         //            $this->add();
     } else {
         if ($_REQUEST['accion'] == 'guardar') {
             header("location:" . PATH_DOMAIN . "/archivo/viewTree/" . $exp_id . "/" . $msm_guardado_archivo . "/{$cue_id}/");
         } else {
             header("location:" . PATH_DOMAIN . "/archivo/viewTree/" . $exp_id . "/" . $msm_guardado_archivo . "/{$cue_id}/");
         }
     }
 }
 function download()
 {
     /*         * *************************************************** */
     /* TYPE SAVE  */
     /*         * *************************************************** */
     $sis_tipcarga = 1;
     //
     $archivo_digital = new tab_archivo_digital();
     //
     // A BD
     if ($sis_tipcarga == 2) {
         $error = "";
         /*
                       if (isset ( $_COOKIE [session_name ()] )) {
                       if (session_is_registered ( 'USU_ID' )) { */
         if (isset($_POST['fil_id_open'])) {
             $fil_id = $_POST['fil_id_open'];
         } else {
             $fil_id = VAR3;
         }
         if ($fil_id != '') {
             $archivo = new tab_archivo();
             $rowe = $archivo->dbSelectBySQL("SELECT * FROM tab_archivo WHERE fil_id =  '" . $fil_id . "'");
             if ($rowe[0]->fil_confidencialidad != '3') {
                 $rowa = $archivo->dbSelectBySQL("SELECT * FROM tab_archivo WHERE fil_id =  '" . $fil_id . "'");
                 $archivobin = new tab_archivobin();
                 $row = $archivobin->dbSelectBySQLField("SELECT fil_contenido FROM tab_archivobin WHERE fil_id =  '" . $fil_id . "'");
                 if (count($row) == 1 || count($rowa) == 1) {
                     $sql = "SELECT\r\n\t\t\t\t\t\t\ttab_archivo.fil_id,\r\n\t\t\t\t\t\t\ttab_archivo.fil_nomoriginal,\r\n\t\t\t\t\t\t\ttab_archivo.fil_nomcifrado,\r\n\t\t\t\t\t\t\ttab_archivo.fil_tamano,\r\n\t\t\t\t\t\t\ttab_archivo.fil_extension,\r\n\t\t\t\t\t\t\ttab_archivo.fil_tipo,\r\n\t\t\t\t\t\t\tcoalesce(tab_archivobin.fil_contenido,'-1') as fil_contenido\r\n\t\t\t\t\t\t\tFROM\r\n\t\t\t\t\t\t\ttab_archivo\r\n\t\t\t\t\t\t\tInner Join tab_archivobin ON tab_archivo.fil_id = tab_archivobin.fil_id WHERE tab_archivobin.fil_id =  '" . $fil_id . "'";
                     $r_files = $archivo->dbSelectBySQLArchive($sql);
                     //
                     $link = $archivo_digital->connect();
                     $sql = "select fil_id, nombre, mime, size, coalesce(archivo_oid,'-1') as archivo_oid, coalesce(archivo_bytea,'-1') as archivo_bytea from tab_archivo_digital where fil_id={$fil_id}";
                     $result = pg_query($link, $sql);
                     if (!$result || pg_num_rows($result) < 1) {
                         header("Location: index.php");
                         exit;
                     }
                     $row = pg_fetch_array($result, 0);
                     pg_free_result($result);
                     if ($row['archivo_bytea'] == -1 && $row['archivo_oid'] == -1) {
                         die('No existe el archivo para mostrar o bajar');
                     }
                     pg_query($link, "begin");
                     $file = pg_lo_open($link, $row['archivo_oid'], "r");
                     header("Cache-control: private");
                     header("Content-type: {$row['mime']}");
                     //if($f==1) header("Content-Disposition: attachment; filename=\"$row[nombre]\"");
                     header("Content-length: {$row['size']}");
                     header("Expires: " . gmdate("D, d M Y H:i:s", mktime(date("H") + 2, date("i"), date("s"), date("m"), date("d"), date("Y"))) . " GMT");
                     header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
                     header("Cache-Control: no-cache, must-revalidate");
                     header("Pragma: no-cache");
                     pg_lo_read_all($file);
                     pg_lo_close($file);
                     pg_query($link, "commit");
                     pg_close($link);
                 } else {
                     $error = "No existe el archivo.";
                 }
             } else {
                 if (isset($_POST['pass_open']) && $_POST['pass_open'] != '') {
                     $usuario = new tab_usuario();
                     $row_usu = $usuario->dbselectByField("usu_id", $_SESSION['USU_ID']);
                     $usuario = $row_usu[0];
                     if ($usuario->getUsu_leer_doc() == '1' && $usuario->getUsu_pass_leer() == md5($_POST['pass_open'])) {
                         $archivo = new tab_archivo();
                         $rowa = $archivo->dbSelectBySQLField("SELECT * FROM tab_archivo WHERE fil_id =  '" . $fil_id . "'");
                         $archivobin = new tab_archivobin();
                         $row = $archivobin->dbSelectBySQLField("SELECT fil_contenido FROM tab_archivobin WHERE fil_id =  '" . $fil_id . "'");
                         if (count($row) == 1 || count($rowa) == 1) {
                             $sql = "SELECT\r\n\t\t\t\t\t\t\t\t\ttab_archivo.fil_id,\r\n\t\t\t\t\t\t\t\t\ttab_archivo.fil_nomoriginal,\r\n\t\t\t\t\t\t\t\t\ttab_archivo.fil_nomcifrado,\r\n\t\t\t\t\t\t\t\t\ttab_archivo.fil_tamano,\r\n\t\t\t\t\t\t\t\t\ttab_archivo.fil_extension,\r\n\t\t\t\t\t\t\t\t\ttab_archivo.fil_tipo,\r\n\t\t\t\t\t\t\t\t\ttab_archivobin.fil_contenido\r\n\t\t\t\t\t\t\t\t\tFROM\r\n\t\t\t\t\t\t\t\t\ttab_archivo\r\n\t\t\t\t\t\t\t\t\tInner Join tab_archivobin ON tab_archivo.fil_id = tab_archivobin.fil_id WHERE tab_archivobin.fil_id =  '" . $fil_id . "'";
                             $r_files = $archivo->dbSelectBySQLArchive($sql);
                             header('Content-type:' . $r_files[0]->fil_tipo);
                             echo $r_files[0]->fil_contenido;
                         } else {
                             $error = "No existe el archivo.";
                         }
                     } else {
                         $error = 'Password incorrecto.';
                     }
                 } else {
                     $error = 'No tiene permisos para ver este archivo.';
                 }
             }
         } else {
             $error = 'No existe el archivo.';
         }
     } else {
         // A SERVER
         $error = "";
         if (isset($_POST['fil_id_open'])) {
             $fil_id = $_POST['fil_id_open'];
         } else {
             $fil_id = VAR3;
         }
         if ($fil_id != '') {
             //
             $link = $archivo_digital->connect();
             $sql = "select fil_id, nombre, mime, size, coalesce(archivo_oid,'-1') as archivo_oid, coalesce(archivo_bytea,'-1') as archivo_bytea from tab_archivo_digital where fil_id={$fil_id}";
             $result = pg_query($link, $sql);
             if (!$result || pg_num_rows($result) < 1) {
                 header("Location: index.php");
                 exit;
             }
             $row = pg_fetch_array($result, 0);
             // Data Parameters
             $nombreDirectorio = "";
             $tsistema = new tab_sistema();
             $sql = "SELECT *\r\n                        FROM tab_sistema";
             $rows2 = $tsistema->dbselectBySQL($sql);
             if (count($rows2) >= 1) {
                 $sis_tipcarga = $rows2[0]->sis_tipcarga;
                 $sis_tammax = $rows2[0]->sis_tammax;
                 $nombreDirectorio = $rows2[0]->sis_ruta;
             }
             $archivopdf = $nombreDirectorio . $row[1];
             $len = filesize($archivopdf);
             //$archivopdf = PATH_DOMAIN ."/". $nombreDirectorio . $row[1];
             //$len = filesize($archivopdf);
             header("Cache-control: private");
             //header("Content-type: $row[mime]");
             header("Content-type: {$row['2']}");
             //header("Content-Disposition: attachment; filename='".$archivopdf."'");
             header("Content-length: {$len}");
             header("Expires: " . gmdate("D, d M Y H:i:s", mktime(date("H") + 2, date("i"), date("s"), date("m"), date("d"), date("Y"))) . " GMT");
             header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
             header("Cache-Control: no-cache, must-revalidate");
             header("Pragma: no-cache");
             readfile($archivopdf);
             pg_free_result($result);
             pg_close($link);
         }
     }
     if ($error != '') {
         echo $error;
     }
 }