2018-11-30 15:45:43 +01:00
|
|
|
<?php
|
2020-09-07 15:04:06 +02:00
|
|
|
/*
|
|
|
|
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
|
2021-01-20 15:08:51 +01:00
|
|
|
* Copyright (C) DevCode s.r.l.
|
2020-09-07 15:04:06 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2018-11-30 15:45:43 +01:00
|
|
|
|
|
|
|
namespace Models;
|
|
|
|
|
2020-09-22 20:28:37 +02:00
|
|
|
use Common\SimpleModelTrait;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2019-07-18 18:33:56 +02:00
|
|
|
use Intervention\Image\ImageManagerStatic;
|
2021-03-08 11:20:04 +01:00
|
|
|
use UnexpectedValueException;
|
2020-12-29 15:45:39 +01:00
|
|
|
use Util\FileSystem;
|
2018-11-30 15:45:43 +01:00
|
|
|
|
|
|
|
class Upload extends Model
|
|
|
|
{
|
2020-09-22 20:28:37 +02:00
|
|
|
use SimpleModelTrait;
|
|
|
|
|
2021-03-08 11:20:04 +01:00
|
|
|
/** @var array Elenco delle tipologie di file pericolose */
|
|
|
|
protected static $not_allowed_types = [
|
|
|
|
'php' => 'application/php',
|
|
|
|
'php5' => 'application/php',
|
|
|
|
'phtml' => 'application/php',
|
|
|
|
'html' => 'text/html',
|
|
|
|
'htm' => 'text/html',
|
|
|
|
];
|
|
|
|
|
2018-11-30 15:45:43 +01:00
|
|
|
protected $table = 'zz_files';
|
|
|
|
|
2019-07-18 18:33:56 +02:00
|
|
|
protected $file_info;
|
|
|
|
|
2021-03-08 09:48:22 +01:00
|
|
|
/**
|
|
|
|
* @return mixed|string
|
|
|
|
*/
|
2018-11-30 19:40:06 +01:00
|
|
|
public function getCategoryAttribute()
|
|
|
|
{
|
|
|
|
return $this->attributes['category'] ?: 'Generale';
|
|
|
|
}
|
|
|
|
|
2021-03-08 09:48:22 +01:00
|
|
|
/**
|
|
|
|
* @param $value
|
|
|
|
*/
|
|
|
|
public function setCategoryAttribute($value)
|
|
|
|
{
|
2021-03-15 16:39:32 +01:00
|
|
|
if ($value == 'Generale' || $value == '') {
|
2021-03-08 09:48:22 +01:00
|
|
|
$value = null;
|
|
|
|
}
|
|
|
|
|
2021-03-15 16:39:32 +01:00
|
|
|
$this->attributes['category'] = $value;
|
2021-03-08 09:48:22 +01:00
|
|
|
}
|
|
|
|
|
2019-01-17 16:11:10 +01:00
|
|
|
/**
|
2019-07-18 18:33:56 +02:00
|
|
|
* Crea un nuovo upload.
|
|
|
|
*
|
2021-03-08 11:20:04 +01:00
|
|
|
* @param string|array $source
|
2021-03-15 16:39:32 +01:00
|
|
|
* @param array $data
|
|
|
|
* @param null $name
|
|
|
|
* @param null $category
|
2019-07-18 18:33:56 +02:00
|
|
|
*
|
|
|
|
* @return self
|
2019-01-17 16:11:10 +01:00
|
|
|
*/
|
2019-07-18 18:33:56 +02:00
|
|
|
public static function build($source, $data, $name = null, $category = null)
|
2019-01-17 16:11:10 +01:00
|
|
|
{
|
2020-09-22 20:28:37 +02:00
|
|
|
$model = new static();
|
2019-07-18 18:33:56 +02:00
|
|
|
|
|
|
|
// Informazioni di base
|
2019-07-31 11:52:13 +02:00
|
|
|
$original_name = isset($source['name']) ? $source['name'] : basename($source);
|
2021-03-08 11:20:04 +01:00
|
|
|
$name = isset($data['name']) ? $data['name'] : $name;
|
|
|
|
$category = isset($data['category']) ? $data['category'] : $category;
|
2019-07-18 18:33:56 +02:00
|
|
|
|
2021-02-26 12:25:32 +01:00
|
|
|
// Nome e categoria dell'allegato
|
2019-07-18 18:33:56 +02:00
|
|
|
$model->name = !empty($name) ? $name : $original_name;
|
|
|
|
$model->category = $category;
|
|
|
|
|
2021-02-26 12:25:32 +01:00
|
|
|
// Nome di origine dell'allegato
|
|
|
|
$original_name = !empty($data['original_name']) ? $data['original_name'] : $original_name; // Campo "original_name" variato in modo dinamico
|
|
|
|
$model->original_name = $original_name; // Fix per "original" di Eloquent
|
|
|
|
|
|
|
|
// Informazioni sulle relazioni
|
2021-03-08 11:20:04 +01:00
|
|
|
$model->id_module = !empty($data['id_module']) && empty($data['id_plugin']) ? $data['id_module'] : null;
|
2021-02-26 12:25:32 +01:00
|
|
|
$model->id_plugin = !empty($data['id_plugin']) ? $data['id_plugin'] : null;
|
|
|
|
$model->id_record = !empty($data['id_record']) ? $data['id_record'] : null;
|
2019-07-18 18:33:56 +02:00
|
|
|
|
2021-03-08 11:20:04 +01:00
|
|
|
// Definizione del nome fisico del file
|
2020-09-23 13:36:37 +02:00
|
|
|
$directory = base_dir().'/'.$model->directory;
|
2019-07-18 18:33:56 +02:00
|
|
|
$filename = self::getNextName($original_name, $directory);
|
2021-03-15 16:39:32 +01:00
|
|
|
if (empty($filename)) {
|
2021-03-08 11:20:04 +01:00
|
|
|
throw new UnexpectedValueException("Estensione dell'allegato non supportata");
|
|
|
|
}
|
2019-07-18 18:33:56 +02:00
|
|
|
$model->filename = $filename;
|
2019-01-17 16:11:10 +01:00
|
|
|
|
2021-03-08 11:20:04 +01:00
|
|
|
// Creazione del file fisico
|
2019-09-09 09:53:51 +02:00
|
|
|
directory($directory);
|
2021-04-02 14:58:34 +02:00
|
|
|
$file = slashes($directory.DIRECTORY_SEPARATOR.$filename);
|
2019-07-18 18:33:56 +02:00
|
|
|
if (
|
2021-04-02 14:58:34 +02:00
|
|
|
(is_array($source) && is_uploaded_file($source['tmp_name']) && !move_uploaded_file($source['tmp_name'], $file)) ||
|
|
|
|
(is_string($source) && is_file($source) && !copy($source, $file)) ||
|
|
|
|
(is_string($source) && !is_file($source) && file_put_contents($file, $source) === false)
|
2019-07-18 18:33:56 +02:00
|
|
|
) {
|
2021-03-08 11:20:04 +01:00
|
|
|
throw new UnexpectedValueException("Errore durante il salvataggio dell'allegato");
|
2019-01-17 16:11:10 +01:00
|
|
|
}
|
|
|
|
|
2021-03-08 11:20:04 +01:00
|
|
|
// Aggiornamento dimensione fisica e responsabile del caricamento
|
2021-04-02 14:58:34 +02:00
|
|
|
$model->size = FileSystem::fileSize($file);
|
2019-07-31 11:52:13 +02:00
|
|
|
$model->user()->associate(auth()->getUser());
|
2019-07-18 18:33:56 +02:00
|
|
|
|
2021-03-08 11:20:04 +01:00
|
|
|
// Rimozione estensione dal nome visibile
|
|
|
|
$extension = $model->extension;
|
|
|
|
if (string_ends_with($model->name, $extension)) {
|
|
|
|
$length = strlen($extension) + 1;
|
|
|
|
$model->name = substr($model->name, 0, -$length);
|
|
|
|
}
|
|
|
|
|
2019-07-18 18:33:56 +02:00
|
|
|
$model->save();
|
|
|
|
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getInfoAttribute()
|
|
|
|
{
|
|
|
|
if (!isset($this->file_info)) {
|
|
|
|
$filepath = $this->filepath;
|
|
|
|
$infos = self::getInfo($filepath);
|
|
|
|
|
|
|
|
$this->file_info = $infos;
|
|
|
|
}
|
2019-01-17 16:11:10 +01:00
|
|
|
|
2019-07-18 18:33:56 +02:00
|
|
|
return $this->file_info;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getExtensionAttribute()
|
|
|
|
{
|
|
|
|
return strtolower($this->info['extension']);
|
2019-01-17 16:11:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-07-18 18:33:56 +02:00
|
|
|
public function getDirectoryAttribute()
|
2019-01-17 16:11:10 +01:00
|
|
|
{
|
|
|
|
$parent = $this->plugin ?: $this->module;
|
|
|
|
|
2019-07-18 18:33:56 +02:00
|
|
|
return $parent->upload_directory;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFilepathAttribute()
|
|
|
|
{
|
|
|
|
return $this->directory.'/'.$this->filename;
|
2019-01-17 16:11:10 +01:00
|
|
|
}
|
|
|
|
|
2019-07-25 15:48:28 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFileurlAttribute()
|
|
|
|
{
|
|
|
|
return str_replace('\\', '/', $this->filepath);
|
|
|
|
}
|
|
|
|
|
2019-07-19 18:11:07 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getOriginalNameAttribute()
|
|
|
|
{
|
|
|
|
return $this->attributes['original'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setOriginalNameAttribute($value)
|
|
|
|
{
|
|
|
|
$this->attributes['original'] = $value;
|
|
|
|
}
|
|
|
|
|
2020-07-08 10:40:04 +02:00
|
|
|
/**
|
|
|
|
* Restituisce i contenuti del file.
|
|
|
|
*
|
|
|
|
* @return false|string
|
|
|
|
*/
|
|
|
|
public function getContent()
|
|
|
|
{
|
|
|
|
return file_get_contents($this->filepath);
|
|
|
|
}
|
|
|
|
|
2019-01-17 16:11:10 +01:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isImage()
|
|
|
|
{
|
2021-08-03 14:51:53 +02:00
|
|
|
$list = ['jpg', 'png', 'gif', 'jpeg', 'bmp'];
|
2019-01-17 16:11:10 +01:00
|
|
|
|
|
|
|
return in_array($this->extension, $list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isFatturaElettronica()
|
|
|
|
{
|
|
|
|
return $this->extension == 'xml' && strtolower($this->category) == 'fattura elettronica';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isPDF()
|
|
|
|
{
|
|
|
|
return $this->extension == 'pdf';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasPreview()
|
|
|
|
{
|
|
|
|
return $this->isImage() || $this->isFatturaElettronica() || $this->isPDF();
|
|
|
|
}
|
|
|
|
|
2019-07-18 18:33:56 +02:00
|
|
|
public function delete()
|
|
|
|
{
|
|
|
|
$info = $this->info;
|
2020-09-23 13:36:37 +02:00
|
|
|
$directory = base_dir().'/'.$this->directory;
|
2019-07-18 18:33:56 +02:00
|
|
|
|
|
|
|
$files = [
|
|
|
|
$directory.'/'.$info['basename'],
|
|
|
|
$directory.'/'.$info['filename'].'_thumb600.'.$info['extension'],
|
|
|
|
$directory.'/'.$info['filename'].'_thumb100.'.$info['extension'],
|
|
|
|
$directory.'/'.$info['filename'].'_thumb250.'.$info['extension'],
|
|
|
|
];
|
|
|
|
|
|
|
|
delete($files);
|
|
|
|
|
|
|
|
return parent::delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function save(array $options = [])
|
|
|
|
{
|
|
|
|
if ($this->isImage()) {
|
|
|
|
//self::generateThumbnails($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::save($options);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function copia($data)
|
|
|
|
{
|
2020-09-23 13:36:37 +02:00
|
|
|
$result = self::build(base_dir().'/'.$this->filepath, $data, $this->name, $this->category);
|
2019-07-18 18:33:56 +02:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getInfo($file)
|
|
|
|
{
|
|
|
|
return pathinfo($file);
|
|
|
|
}
|
|
|
|
|
2019-07-31 11:52:13 +02:00
|
|
|
/* Relazioni Eloquent */
|
|
|
|
|
|
|
|
public function module()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Module::class, 'id_module');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function plugin()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Plugin::class, 'id_plugin');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(User::class, 'created_by');
|
|
|
|
}
|
|
|
|
|
2021-03-15 16:39:32 +01:00
|
|
|
/**
|
|
|
|
* Controlla se l'estensione è supportata dal sistema di upload.
|
|
|
|
*
|
|
|
|
* @param string $extension
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected static function isSupportedType($extension)
|
|
|
|
{
|
|
|
|
return !in_array(strtolower($extension), array_keys(self::$not_allowed_types));
|
|
|
|
}
|
|
|
|
|
2019-07-18 18:33:56 +02:00
|
|
|
/**
|
|
|
|
* Genera casualmente il nome fisico per il file.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected static function getNextName($file, $directory)
|
|
|
|
{
|
|
|
|
$extension = self::getInfo($file)['extension'];
|
|
|
|
$extension = strtolower($extension);
|
|
|
|
|
2021-03-08 11:20:04 +01:00
|
|
|
// Controllo sulle estensioni permesse
|
|
|
|
$allowed = self::isSupportedType($extension);
|
|
|
|
if (!$allowed) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-18 18:33:56 +02:00
|
|
|
do {
|
|
|
|
$filename = random_string().'.'.$extension;
|
|
|
|
} while (file_exists($directory.'/'.$filename));
|
|
|
|
|
|
|
|
return $filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Genera le thumbnails per le immagini.
|
|
|
|
*/
|
|
|
|
protected static function generateThumbnails($upload)
|
|
|
|
{
|
|
|
|
$info = $upload->info;
|
|
|
|
$directory = $upload->directory;
|
|
|
|
|
|
|
|
$filepath = $upload->filepath;
|
|
|
|
|
2020-05-04 09:45:48 +02:00
|
|
|
if (!in_array(mime_content_type($filepath), ['image/x-png', 'image/gif', 'image/jpeg'])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-18 18:33:56 +02:00
|
|
|
$driver = extension_loaded('gd') ? 'gd' : 'imagick';
|
|
|
|
ImageManagerStatic::configure(['driver' => $driver]);
|
|
|
|
|
|
|
|
$img = ImageManagerStatic::make($filepath);
|
|
|
|
|
|
|
|
$img->resize(600, null, function ($constraint) {
|
|
|
|
$constraint->aspectRatio();
|
|
|
|
});
|
|
|
|
$img->save(slashes($directory.'/'.$info['filename'].'_thumb600.'.$info['extension']));
|
|
|
|
|
|
|
|
$img->resize(250, null, function ($constraint) {
|
|
|
|
$constraint->aspectRatio();
|
|
|
|
});
|
|
|
|
$img->save(slashes($directory.'/'.$info['filename'].'_thumb250.'.$info['extension']));
|
|
|
|
|
|
|
|
$img->resize(100, null, function ($constraint) {
|
|
|
|
$constraint->aspectRatio();
|
|
|
|
});
|
|
|
|
$img->save(slashes($directory.'/'.$info['filename'].'_thumb100.'.$info['extension']));
|
|
|
|
}
|
2018-11-30 15:45:43 +01:00
|
|
|
}
|