mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
queda preview y cargar ficheros del server
This commit is contained in:
100
ci4/app/Models/Presupuestos/PresupuestoFicheroModel.php
Normal file
100
ci4/app/Models/Presupuestos/PresupuestoFicheroModel.php
Normal file
@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Presupuestos;
|
||||
|
||||
// Clave secreta para el cifrado y descifrado
|
||||
define('SECRET_KEY', 'qnENEMHWt6sJgzGtEdfrT');
|
||||
|
||||
// Longitud del IV para AES-256-CBC
|
||||
define('IV_LENGTH', openssl_cipher_iv_length('aes-256-cbc'));
|
||||
|
||||
|
||||
class PresupuestoFicheroModel extends \App\Models\BaseModel
|
||||
{
|
||||
protected $table = "presupuesto_ficheros";
|
||||
|
||||
|
||||
/**
|
||||
* Whether primary key uses auto increment.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $useAutoIncrement = true;
|
||||
|
||||
|
||||
protected $allowedFields = ["presupuesto_id", "nombre", "file_path", "upload_by", "upload_at"];
|
||||
protected $returnType = "App\Entities\Presupuestos\PresupuestoFicheroEntity";
|
||||
|
||||
protected $useTimestamps = false;
|
||||
protected $useSoftDeletes = false;
|
||||
|
||||
public static $labelField = "nombre";
|
||||
|
||||
|
||||
public function saveFileInBBDD($presupuesto_id, $filename, $user_id) {
|
||||
try{
|
||||
$new_filename = $this->generateFileHash($filename);
|
||||
|
||||
$this->db->table($this->table . " t1")
|
||||
->set('presupuesto_id', $presupuesto_id)
|
||||
->set('nombre', $filename)
|
||||
->set('file_path', WRITEPATH . 'uploads/presupuestos/' . $new_filename)
|
||||
->set('upload_by', $user_id)
|
||||
->set('upload_at', date('Y-m-d H:i:s'))
|
||||
->insert();
|
||||
|
||||
return $new_filename;
|
||||
} catch (\Exception $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public function deleteFiles($presupuesto_id){
|
||||
|
||||
$files = $this->db
|
||||
->table($this->table . " t1")
|
||||
->where('presupuesto_id', $presupuesto_id)->get()->getResult();
|
||||
if($files){
|
||||
foreach($files as $file){
|
||||
if(file_exists($file->file_path)){
|
||||
unlink($file->file_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->db
|
||||
->table($this->table . " t1")
|
||||
->where('presupuesto_id', $presupuesto_id)->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Función para convertir el nombre y extensión de un fichero en un hash único
|
||||
* usando cifrado simétrico.
|
||||
*
|
||||
* @param string $filename Nombre del fichero con extensión
|
||||
* @return string Hash encriptado del fichero
|
||||
*/
|
||||
private function generateFileHash($filename) {
|
||||
$iv = openssl_random_pseudo_bytes(IV_LENGTH); // Generar un IV aleatorio
|
||||
$encrypted = openssl_encrypt($filename, 'aes-256-cbc', SECRET_KEY, 0, $iv);
|
||||
return base64_encode($iv . $encrypted); // Combinar IV y texto encriptado, y codificar en base64
|
||||
}
|
||||
|
||||
/**
|
||||
* Función para obtener el nombre del fichero original a partir de un hash encriptado
|
||||
* usando cifrado simétrico.
|
||||
*
|
||||
* @param string $hash Hash encriptado del fichero
|
||||
* @return string|null Nombre original del fichero, o null si falla el descifrado
|
||||
*/
|
||||
private function getFileNameFromHash($hash) {
|
||||
$data = base64_decode($hash);
|
||||
if ($data === false) {
|
||||
return null; // Error en la decodificación base64
|
||||
}
|
||||
$iv = substr($data, 0, IV_LENGTH);
|
||||
$encrypted = substr($data, IV_LENGTH);
|
||||
$decrypted = openssl_decrypt($encrypted, 'aes-256-cbc', SECRET_KEY, 0, $iv);
|
||||
return $decrypted !== false ? $decrypted : null; // Verificar si el descifrado fue exitoso
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user