id = $file_id; $this->name = ''; $this->ext = ''; $this->father = 0; $this->partes = 0; $this->ambito = ''; $this->acceso = ''; $this->mimetype = ''; $this->sizereal = 0; $this->content_binary = ''; $this->errno = 0; $this->error = ''; $this->mimetype_obj = new Mimetype_class(); $this->forced_download = $forced; $this->tabla = ''; } // ---------------------------------------------------------- public function any_error() { return ($this->errno != 0); } // ---------------------------------------------------------- public function get_errno() { return $this->errno; } // ---------------------------------------------------------- public function get_error() { return $this->error; } // ---------------------------------------------------------- private function set_error( $error, $errno = '0' ) { $this->errno = ($errno == 0 ? 1 : $errno); $this->error = $error; } // ---------------------------------------------------------- private function clean_extension( $extension ) { return "." . trim(strtolower($extension)); } // ---------------------------------------------------------- public function download_prepare( ) { $this->sql_get_data(); $this->sql_get_content(); $result = $this->sql_download_log(); if ($result == -1) { $this->set_error( kMSG_BAD_LOG, -1 ); } if ($this->partes > 1) { ini_set('memory_limit', 1024*1024*MAX_SIZE_FILES); for ($i=2; $i<=$this->partes; $i++) { $this->sql_get_size($i); } } } // ---------------------------------------------------------- public function download( ) { /* $forzar_descarga = FALSE; if (!$this->forced_download) { $extension = trim(strtolower($this->ext)); $valid_ext = array('.pdf', '.txt', '.mp3', '.jpg', '.png', '.gif', '.swf', '.mpeg'); if (in_array($extension, $valid_ext)) { $forzar_descarga = FALSE; } } */ // Fuerza los ficheros de tipo a vĂ­deo a ser descargados /* $ext_videos = array('.swf', '.flv', '.3gp', '.3g2', '.avi', '.mp4', '.asf', '.mov', '.mpg'); if ( in_array($this->ext, $ext_videos) ) { $this->forced_download = TRUE; } */ // Cabeceras header( 'Content-type: '.$this->mimetype ); if ($this->forced_download) { header( 'Content-Disposition: attachment; filename="'.$this->name.'"' ); } else { header( 'Content-Disposition: inline; filename="'.$this->name.'"' ); } header( 'Content-Transfer-Encoding: binary' ); header( 'Content-Length: '.$this->sizereal ); // Datos del fichero echo $this->content_binary; if ($this->partes > 1) { for ($i=2; $i<=$this->partes; $i++) { echo $this->sql_get_parte($i); } } } // ---------------------------------------------------------- // ========================================================== // SQL // ---------------------------------------------------------- private function sql_get_data( ) { global $db; $q_data_SELECT = "NOMBRE, EXTENSION, PADRE, PARTES, AMBITO, ACCESO" . " FROM BIBLIOTECA_ELEMENTOS" . " WHERE CLAVE = '%s' LIMIT 1"; $query = sprintf( $q_data_SELECT, $this->id ); $result = $db->query_select( $query ); $row = $result->fetch_assoc(); $this->name = $row["NOMBRE"]; $this->ext = $this->clean_extension( $row["EXTENSION"] ); $this->father = $row["PADRE"]; $this->partes = $row["PARTES"]; $this->ambito = $row["AMBITO"]; $this->acceso = $row["ACCESO"]; $result->close(); $this->mimetype = $this->mimetype_obj->get_mimetype( $this->ext ); } // ---------------------------------------------------------- private function sql_get_table_sufix( ) { global $db; $q_SELECT = "SUFIJO" . " FROM BIBLIOTECA_ELEMENTOS" . " WHERE CLAVE = '%s'" . " AND TIPO='D' LIMIT 1"; $query = sprintf( $q_SELECT, $this->id ); $result = $db->query_select( $query ); $row = $result->fetch_assoc(); $tablasufijo = 'FICHEROS_' . $row["SUFIJO"]; $result->close(); return $tablasufijo; } // ---------------------------------------------------------- private function sql_get_content( ) { global $db; $tablasufijo = $this->sql_get_table_sufix(); $q_SELECT = "FICHERO, TAMANOREAL" . " FROM " . $tablasufijo . " WHERE CLAVE = '%s'" . " AND PARTE = 1" . " AND VERSION = '0' LIMIT 1"; $query = sprintf( $q_SELECT, $this->id ); $result = $db->query_select( $query ); $row = $result->fetch_assoc(); $this->sizereal = $row["TAMANOREAL"]; $this->content_binary = $row["FICHERO"]; $result->close(); } // ---------------------------------------------------------- private function sql_get_size( $num ) { global $db; $tablasufijo = $this->sql_get_table_sufix(); $q_SELECT = "TAMANO" . " FROM " . $tablasufijo . " WHERE PARTE = '%s'" . " AND CLAVE = '%s'" . " AND VERSION = '0' LIMIT 1"; $query = sprintf( $q_SELECT, $num, $this->id ); $result = $db->query_select( $query ); $row = $result->fetch_assoc(); $this->sizereal += $row["TAMANO"]; $result->close(); } private function sql_get_parte( $num ) { global $db; $tablasufijo = $this->sql_get_table_sufix(); $q_SELECT = "FICHERO" . " FROM " . $tablasufijo . " WHERE PARTE = '%s'" . " AND CLAVE = '%s'" . " AND VERSION = '0' LIMIT 1"; $query = sprintf( $q_SELECT, $num, $this->id ); $result = $db->query_select( $query ); $row = $result->fetch_assoc(); return $row["FICHERO"]; $result->close(); } // ---------------------------------------------------------- private function sql_download_log( ) { global $db, $user; $user_id = $user->get_id(); $user_group = $user->get_group(); $q_INSERT = "INTO BIBLIOTECA_HISTORIAL (" . "CLAVE,USUARIO,FECHA,ACCION,DOCUMENTO,CARPETA,DESTINO," . "ORDENADOR,MENSAJE,AMBITO,ACCESO,TIPO,TAMANO,CREADOR," . "RUTA,DETALLE,CONTROL" . ") VALUES (" . "'".$this->id."'," . "'".$user_id."'," . "NOW()," . " ".kLOG_OPEN."," . "'".$this->name."'," . "'".$this->father."'," . "''," . "'".$user->get_ip()."'," . "''," . "'".$this->ambito."'," . "'".$this->acceso."'," . "'D'," . "'".$this->sizereal."'," . "'".$user_id."'," . "''," . "''," . "0)"; $query = $q_INSERT; if ( debug_on() ) { $user->set_session_value('Q_DOWNLOAD_LOG', $query); } return $db->query_insert($query); } // -------------------------------------------------------------- } ?>