|
Server : Apache/2.4.62 System : FreeBSD fbsdweb2.web.rcn.net 14.1-RELEASE FreeBSD 14.1-RELEASE releng/14.1-n267679-10e31f0946d8 GENERIC amd64 User : www ( 80) PHP Version : 8.3.8 Disable Function : NONE Directory : /domains/logicswapweb/aplicaciones/admon/catalogo/fnc/ |
Upload File : |
<?php
class Archivo extends Componente{
/*constructor*/
function Archivo()
{
$this->DBManager('MySQL');
$this->connect();
}
/*cargar archivo*/
function captureUpload($destDir,$nameCallback = false,$fieldName = '_upload',$maxFileSize = false)
{
//make sure something is there
if(!isset($_FILES[$fieldName]) || !isset($_FILES)|| !is_array($_FILES[$fieldName]) || !$_FILES[$fieldName]['name'])
return array(false,"El formulario no tiene la etiqueta enctype=\"multipart/form-data\"");
//normalize the file variable
$file = $_FILES[$fieldName];
if (!isset($file['type'])) $file['type'] = '';
if (!isset($file['size'])) $file['size'] = '';
if (!isset($file['tmp_name'])) $file['tmp_name'] = '';
$file['name'] = preg_replace('/[^a-zA-Z0-9\.\$\%\'\`\-\@\{\}\~\!\#\(\)\&\_\^]/','',str_replace(array(' ','%20'),array('_','_'),$file['name']));
//was it to big?
if($maxFileSize && ($file['size'] > $maxFileSize))
return array(false,"El tama�o del archivo es muy grande");
//normalize destDir
if(strlen($destDir)>0 && $destDir[strlen($destDir)-1] != "/")
$destDir = $destDir.'/';
//should we change the filename via a callback?
if($nameCallback)
$file['name'] = call_user_func_array($nameCallback, array($file,$destDir));
$i = 0;
//if the filename already exists, append _copy_x (with extension)
if(strpos($file['name'],'.') !== false){
$bits = explode('.',$file['name']);
$ext = array_pop($bits);
while(file_exists($destDir.implode('.', $bits).($i?'_copy_'.$i:'').'.'.$ext)){
++$i;
$file['name'] = implode('.',$bits).($i?'_copy_'.$i:'').'.'.$ext;
}
//if the filename already exists, append _copy_x (no extension)
} else {
while(file_exists($destDir.$file['name'].($i ?'_copy_'.$i:''))){
++$i;
$file['name'] = $file['name'].($i?'_copy_'.$i:'');
}
}
//$this->mostrarMensaje( $destDir.$file['name']);
//and now the big moment
if(!@copy($file['tmp_name'], $destDir.$file['name']))
return array(false,"Permission denied to copy the file");
else
return array(true,$file['name']);
}
/* modificar archivo*/
function mod_archivo($titulo,$archivo,$file,$id_arc,$id_pro)
{
$sql = "SELECT id FROM catalogo_archivos WHERE producto='$id_pro' AND titulo='$titulo' AND id!='$id_arc'";
$this->execute($sql);
$cant=$this->numRows();
if($cant==0){
$response;
$rutafoto ="../archivos/".$file;
if (file_exists($rutafoto)){
if(!unlink($rutafoto)){
$this->mostrarMensaje("Error: no se pudo borrar el archivo ".$file." del servidor \\nPermiso denegado");
}
list($success,$response) = $this->captureUpload('../archivos/',false,'archivo');
if(!$success){
$this->mostrarMensaje("Error: ".$response);
}else{
$sql="update catalogo_archivos set titulo='$titulo', fichero='$response' where id='$id_arc'";
}
}else{
list($success,$response) = $this->captureUpload('../archivos/',false,'archivo');
if(!$success){
$this->mostrarMensaje("Error: ".$response);
}else{
$sql="update archivo set titulo='$titulo', fichero='$response' where id='$id_arc'";
}
}
if($this->execute($sql)){
$this->mostrarMensaje("Archivo modificado con �xito!");
return true;
}else{
$this->mostrarMensaje("�Impossible to consult the database!");
}
}else{
$this->mostrarMensaje("There is already a file with the same name. Specify different filename.");
}
return false;
}
// Esta funcion solo sirve para consultas donde se nesecita
// conocer el valor de un campo espesifico.
// ej: $sql="SELECT nombre FROM catalogo_producto WHERE id='$id_pro'";
// $func es el nombre del metodo desde donde se invoca a consultarCampo,
// esto para poder mostrar el error adecuadamente
function consultarCampo($sql, $func)
{
//$this->setFiles(4);
if($this->execute($sql))
{
$n = $this->fetchRow();
return $n[0];
}else
{
$out=trim($this->getMensaje("[016]","error"));
$out.="\\n:Desc: ".trim($this->getException());
$this->setSalida(false,$out,$func,$this->nombre);
return false;
}
}
/*
Esta funcion retorna un array con el contenido de una consulta donde se esperan varios resultados;
*/
function consultarCampos($sql, $func)
{
//$this->setFiles(4);
$array = array();
$i = 0;
if($this->execute($sql))
{
while($row = $this->fetchRow())
{
$array[] = $row;
$i++;
}
if($i > 0)
return $array;
else return NULL;
}else
{
$out=trim($this->getMensaje("[016]","error"));
$out.="\\n:Desc: ".trim($this->getException());
$this->setSalida(false,$out,$func,$this->nombre);
return NULL;
}
}
/* agregar archivo*/
/*
$titulo = id_producto
*/
function add_archivo($id_pro, $titulo,$archivo)
{
if($id_pro)
{
$sql = "SELECT id FROM catalogo_archivos WHERE producto=$id_pro and titulo like '$titulo'";
$this->execute($sql);
$cant=$this->numRows();
if($cant==0)
{
$response;
list($success,$response) = $this->captureUpload('../archivos/',false,'archivo');
if(!$success)
{
$this->mostrarMensaje("Error: ".$response);
return false;
}else
{
$sql="insert into catalogo_archivos (producto, titulo, fichero) values ('$id_pro','$titulo','$response')";
//echo $sql="INSERT INTO `catalogo_archivos` ( `id` , `producto` , `titulo` , `fichero` )VALUES (default, '$id_pro','$titulo','$response');";
if($this->execute($sql))
{
$this->mostrarMensaje("File added successfully!");
}else
{
$this->mostrarMensaje("�Impossible to consult the database!");
return false;
}
}
}else
{
$this->mostrarMensaje("There is already a file with this name!");
return false;
}
}else
{
$this->mostrarMensaje("No existe el documento especificado!");
return false;
}
return true;
}
/* eliminar archivo*/
function eli_archivo($id_arc)
{
$sql="select fichero from catalogo_archivos where id='$id_arc'";
$this->execute($sql);
if(list($fichero)=$this->fetchRow())
{
//borro archivos
$rutafoto="../archivos/".$fichero;
if (file_exists($rutafoto)){
if(!unlink($rutafoto)){
$this->mostrarMensaje("Error: no se pudo borrar el archivo del servidor \\nPermiso denegado!");
}else{
$sql="delete from catalogo_archivos where id = '$id_arc'";
if($this->execute($sql)){
$this->mostrarMensaje("File removed successfully!");
}else{
$this->mostrarMensaje("�Impossible to consult the database!!");
}
}
}else{
$sql="delete from catalogo_archivos where id = '$id_arc'";
if($this->execute($sql)){
$this->mostrarMensaje("File removed successfully!");
}else{
$this->mostrarMensaje("�Impossible to consult the database!!");
}
}
}else{
$this->mostrarMensaje("�Impossible to consult the database!");
}
return true;
}
function listarArchivos($id_pro, $nun_pag=1, $limite=10)
{
$inicio = ($nun_pag -1)*$limite;
$script = '<script>
var t = new SortTable("t");
t.AddColumn("Nombre","nowrap","left","");
t.AddColumn("File","nowrap","left","");
t.AddColumn("Numero","","center","");';
$sql = "SELECT id, titulo, fichero FROM catalogo_archivos WHERE producto = '$id_pro' ORDER BY titulo limit $inicio, $limite";
$this->execute($sql);
if($this->numRows()>0){
$i = 1; $limite =10; $write=0;
while ((list($id, $titulo, $fichero)=$this->fetchRow())) {
if($i==1){
$script .= 't.AddLine(\'<input type="hidden" name="mat" value="'.$id.'"><IMG src="img/disket.gif" onClick="fncModificarArchivo('.$id.')"> <A onClick="fncModificarArchivo('.$id.')" class="texto1" > '.$titulo.' </A>\',\'<A onClick="fncModificarArchivo('.$id.')" style=" width: 150; font-family:Arial, Helvetica, sans-serif; color:#000000; font-size:12px; font-weight:normal " > '.$fichero.' </A>\',\'<input type="radio" name="sel" value="'.$id.'" style=" border-width:0">\' ); ';
$script .= 't.AddLineProperties(\'align="center" onMouseOver="mOvr(this,\\\'#FAFF95\\\');" onMouseOut="mOut(this,\\\'#FFFFFF\\\')" id="tr_'.$i.'"\');';
}else{
$script .= 't.AddLine(\'<input type="hidden" name="mat" value="'.$id.'"><IMG src="img/disket.gif" onClick="fncModificarArchivo('.$id.')"> <A onClick="fncModificarArchivo('.$id.')" style=" width: 150; font-family:Arial, Helvetica, sans-serif; color:#000000; font-size:12px; font-weight:normal " > '.$titulo.' </A>\',\'<A onClick="fncModificarArchivo('.$id.')" style=" width: 150; font-family:Arial, Helvetica, sans-serif; color:#000000; font-size:12px; font-weight:normal " > '.$fichero.' </A>\',\'<input type="radio" name="sel" value="'.$id.'" style=" border-width:0">\' );';
$script .= 't.AddLineProperties(\'align="center" onMouseOver="mOvr(this,\\\'#FAFF95\\\');" onMouseOut="mOut(this,\\\'#FFFFFF\\\')" id="tr_'.$i.'" \');';
}
$i++;
}
$script .= 't.WriteRows();';
}else{
$msg = "There is no file saved";
$script .= 'document.write(\'<TR><TD colspan="3" align="center"><BR>There is no file saved.<BR> </TD></>\');';
}
$script .= '</script>';
echo $script;
}
function numeroArchivos($id_pro)
{
$sql = "SELECT count(*) FROM catalogo_archivos WHERE producto = '$id_pro'";
$num = $this->consultarCampo($sql, "numeroArchivos($id_pro)");
if($num) return $num;
return 0;
}
}
//[email protected]
?>