2018-07-03 11:12:32 +02: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-07-03 11:12:32 +02:00
|
|
|
|
2020-12-29 15:45:39 +01:00
|
|
|
use Util\FileSystem;
|
|
|
|
|
2018-07-03 11:12:32 +02:00
|
|
|
/**
|
|
|
|
* Classe per la gestione degli upload del progetto.
|
|
|
|
*
|
|
|
|
* @since 2.4.1
|
|
|
|
*/
|
|
|
|
class Uploads
|
|
|
|
{
|
2020-12-06 16:35:36 +01:00
|
|
|
/** @var array Elenco delle tipologie di file pericolose */
|
|
|
|
protected static $not_allowed_types = [
|
|
|
|
'php' => 'application/php',
|
|
|
|
'php5' => 'application/php',
|
|
|
|
'phtml' => 'application/php',
|
2018-12-29 12:03:22 +01:00
|
|
|
'html' => 'text/html',
|
2020-12-06 16:35:36 +01:00
|
|
|
'htm' => 'text/html',
|
2018-12-29 12:03:22 +01:00
|
|
|
];
|
|
|
|
|
2018-07-10 12:07:48 +02:00
|
|
|
/**
|
2019-01-04 11:39:30 +01:00
|
|
|
* Restituisce l'elenco degli allegati registrati per un determinato modulo/plugin e record.
|
2018-07-10 12:07:48 +02:00
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function get($data)
|
|
|
|
{
|
2018-09-20 12:05:22 +02:00
|
|
|
$database = database();
|
2018-07-03 11:12:32 +02:00
|
|
|
|
2018-07-10 12:07:48 +02:00
|
|
|
$uploads = $database->select('zz_files', '*', [
|
2018-08-31 11:39:38 +02:00
|
|
|
'id_module' => !empty($data['id_module']) && empty($data['id_plugin']) ? $data['id_module'] : null,
|
2018-07-10 12:07:48 +02:00
|
|
|
'id_plugin' => !empty($data['id_plugin']) ? $data['id_plugin'] : null,
|
|
|
|
'id_record' => $data['id_record'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $uploads;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce il nome della cartella per l'upload degli allegati per un determinato modulo/plugin.
|
|
|
|
*
|
|
|
|
* @param string|int $id_module
|
|
|
|
* @param string|int $id_plugin
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-07-09 12:57:55 +02:00
|
|
|
public static function getDirectory($id_module, $id_plugin = null)
|
2018-07-03 21:22:29 +02:00
|
|
|
{
|
|
|
|
if (empty($id_plugin)) {
|
2019-07-18 18:33:56 +02:00
|
|
|
$structure = Modules::get($id_module);
|
2018-07-03 21:22:29 +02:00
|
|
|
} else {
|
2019-07-18 18:33:56 +02:00
|
|
|
$structure = Plugins::get($id_plugin);
|
2018-07-03 21:22:29 +02:00
|
|
|
}
|
|
|
|
|
2019-07-18 18:33:56 +02:00
|
|
|
return $structure->upload_directory;
|
2018-07-03 21:22:29 +02:00
|
|
|
}
|
|
|
|
|
2018-08-31 11:39:38 +02:00
|
|
|
/**
|
|
|
|
* Individua il nome fisico per il file indicato.
|
|
|
|
*
|
|
|
|
* @param string $source
|
2018-09-03 16:49:43 +02:00
|
|
|
* @param array $data
|
|
|
|
*
|
2018-08-31 11:39:38 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2018-09-25 16:47:44 +02:00
|
|
|
public static function getName($source, $data)
|
2018-08-31 11:39:38 +02:00
|
|
|
{
|
2018-11-09 13:15:19 +01:00
|
|
|
$extension = strtolower(self::fileInfo($source)['extension']);
|
2020-12-06 16:35:36 +01:00
|
|
|
$allowed = self::isSupportedType($extension);
|
|
|
|
|
|
|
|
if (!$allowed) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-31 11:39:38 +02:00
|
|
|
|
2020-09-23 13:36:37 +02:00
|
|
|
$directory = base_dir().'/'.self::getDirectory($data['id_module'], $data['id_plugin']);
|
2018-08-31 11:39:38 +02:00
|
|
|
|
|
|
|
do {
|
|
|
|
$filename = random_string().'.'.$extension;
|
|
|
|
} while (file_exists($directory.'/'.$filename));
|
|
|
|
|
|
|
|
return $filename;
|
|
|
|
}
|
|
|
|
|
2018-07-03 11:12:32 +02:00
|
|
|
/**
|
|
|
|
* Effettua l'upload di un file nella cartella indicata.
|
|
|
|
*
|
2019-02-12 11:42:48 +01:00
|
|
|
* @param array $source
|
|
|
|
* @param array $data
|
|
|
|
* @param array $options
|
2018-07-03 11:12:32 +02:00
|
|
|
*
|
2018-07-03 21:22:29 +02:00
|
|
|
* @return string
|
2018-07-03 11:12:32 +02:00
|
|
|
*/
|
2018-07-03 21:22:29 +02:00
|
|
|
public static function upload($source, $data, $options = [])
|
2018-07-03 11:12:32 +02:00
|
|
|
{
|
2019-02-12 11:42:48 +01:00
|
|
|
$original = isset($source['name']) ? $source['name'] : basename($source);
|
2018-07-03 11:12:32 +02:00
|
|
|
|
2018-08-31 11:39:38 +02:00
|
|
|
$filename = self::getName($original, $data);
|
2020-09-23 13:36:37 +02:00
|
|
|
$directory = base_dir().'/'.self::getDirectory($data['id_module'], $data['id_plugin']);
|
2018-07-03 21:22:29 +02:00
|
|
|
|
2018-07-03 11:12:32 +02:00
|
|
|
// Creazione file fisico
|
2018-11-30 15:33:25 +01:00
|
|
|
if (
|
|
|
|
!directory($directory) ||
|
2020-12-14 11:05:17 +01:00
|
|
|
(is_array($source) && is_uploaded_file($source['tmp_name']) && !move_uploaded_file($source['tmp_name'], $directory.'/'.$filename)) ||
|
2018-11-30 15:33:25 +01:00
|
|
|
(is_string($source) && !copy($source, $directory.'/'.$filename))
|
|
|
|
) {
|
2018-07-03 21:22:29 +02:00
|
|
|
return null;
|
2018-07-03 11:12:32 +02:00
|
|
|
}
|
|
|
|
|
2018-07-10 12:07:48 +02:00
|
|
|
// Registrazione del file
|
|
|
|
$data['filename'] = $filename;
|
|
|
|
$data['original'] = $original;
|
2020-12-29 15:45:39 +01:00
|
|
|
$data['size'] = FileSystem::fileSize($directory.'/'.$filename);
|
2018-07-10 12:07:48 +02:00
|
|
|
self::register($data);
|
|
|
|
|
2018-08-31 11:39:38 +02:00
|
|
|
// Operazioni finali
|
|
|
|
self::processOptions($data, $options);
|
2018-07-10 12:07:48 +02:00
|
|
|
|
|
|
|
return $filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registra nel database il file caricato con i dati richiesti.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
*/
|
|
|
|
public static function register($data)
|
|
|
|
{
|
2018-09-20 12:05:22 +02:00
|
|
|
$database = database();
|
2018-07-03 11:12:32 +02:00
|
|
|
|
|
|
|
$database->insert('zz_files', [
|
2018-08-31 11:39:38 +02:00
|
|
|
'name' => !empty($data['name']) ? $data['name'] : $data['original'],
|
2018-07-10 12:07:48 +02:00
|
|
|
'filename' => !empty($data['filename']) ? $data['filename'] : $data['original'],
|
|
|
|
'original' => $data['original'],
|
2018-07-03 11:12:32 +02:00
|
|
|
'category' => !empty($data['category']) ? $data['category'] : null,
|
2018-08-31 11:39:38 +02:00
|
|
|
'id_module' => !empty($data['id_module']) && empty($data['id_plugin']) ? $data['id_module'] : null,
|
2018-07-03 11:12:32 +02:00
|
|
|
'id_plugin' => !empty($data['id_plugin']) ? $data['id_plugin'] : null,
|
2018-07-03 21:22:29 +02:00
|
|
|
'id_record' => $data['id_record'],
|
2019-04-04 08:30:58 -07:00
|
|
|
'size' => $data['size'],
|
2019-07-31 11:52:13 +02:00
|
|
|
'created_by' => auth()->getUser()->id,
|
2018-07-03 11:12:32 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-07-10 12:07:48 +02:00
|
|
|
/**
|
|
|
|
* Elimina l'allegato indicato.
|
|
|
|
*
|
|
|
|
* @param string $filename
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return string Nome del file
|
|
|
|
*/
|
2018-07-03 21:22:29 +02:00
|
|
|
public static function delete($filename, $data)
|
|
|
|
{
|
2020-06-30 13:26:15 +02:00
|
|
|
if (!empty($filename)) {
|
2020-06-25 12:53:12 +02:00
|
|
|
$database = database();
|
2018-07-03 21:22:29 +02:00
|
|
|
|
2020-06-25 12:53:12 +02:00
|
|
|
$name = $database->selectOne('zz_files', ['name'], [
|
|
|
|
'filename' => $filename,
|
2018-09-28 18:11:42 +02:00
|
|
|
'id_module' => !empty($data['id_module']) && empty($data['id_plugin']) ? $data['id_module'] : null,
|
2018-07-03 21:22:29 +02:00
|
|
|
'id_plugin' => !empty($data['id_plugin']) ? $data['id_plugin'] : null,
|
|
|
|
'id_record' => $data['id_record'],
|
2020-06-25 12:53:12 +02:00
|
|
|
])['name'];
|
2018-07-03 21:22:29 +02:00
|
|
|
|
2020-06-25 12:53:12 +02:00
|
|
|
$fileinfo = self::fileInfo($filename);
|
2020-09-23 13:36:37 +02:00
|
|
|
$directory = base_dir().'/'.self::getDirectory($data['id_module'], $data['id_plugin']);
|
2018-07-03 21:22:29 +02:00
|
|
|
|
2020-06-25 12:53:12 +02:00
|
|
|
$files = [
|
|
|
|
$directory.'/'.$fileinfo['basename'],
|
|
|
|
$directory.'/'.$fileinfo['filename'].'_thumb600.'.$fileinfo['extension'],
|
|
|
|
$directory.'/'.$fileinfo['filename'].'_thumb100.'.$fileinfo['extension'],
|
|
|
|
$directory.'/'.$fileinfo['filename'].'_thumb250.'.$fileinfo['extension'],
|
|
|
|
];
|
|
|
|
|
|
|
|
if (delete($files)) {
|
|
|
|
$database->delete('zz_files', [
|
|
|
|
'filename' => $fileinfo['basename'],
|
|
|
|
'id_module' => !empty($data['id_module']) && empty($data['id_plugin']) ? $data['id_module'] : null,
|
|
|
|
'id_plugin' => !empty($data['id_plugin']) ? $data['id_plugin'] : null,
|
|
|
|
'id_record' => $data['id_record'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
}
|
2020-06-30 13:26:15 +02:00
|
|
|
|
2018-07-03 21:22:29 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-07-10 12:07:48 +02:00
|
|
|
/**
|
|
|
|
* Rimuove tutti gli allegati di un determinato modulo/plugin e record.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
*/
|
2018-07-07 14:24:59 +02:00
|
|
|
public static function deleteLinked($data)
|
|
|
|
{
|
2018-07-10 12:07:48 +02:00
|
|
|
$uploads = self::get($data);
|
2018-07-07 14:24:59 +02:00
|
|
|
|
|
|
|
foreach ($uploads as $upload) {
|
|
|
|
self::delete($upload['filename'], $data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 12:07:48 +02:00
|
|
|
/**
|
|
|
|
* Restituisce le informazioni relative al file indicato.
|
|
|
|
*
|
|
|
|
* @param string $filepath
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-07-03 21:22:29 +02:00
|
|
|
public static function fileInfo($filepath)
|
|
|
|
{
|
|
|
|
$infos = pathinfo($filepath);
|
|
|
|
$infos['extension'] = strtolower($infos['extension']);
|
|
|
|
|
|
|
|
return $infos;
|
|
|
|
}
|
2018-07-10 12:07:48 +02:00
|
|
|
|
2018-07-19 11:48:07 +02:00
|
|
|
/**
|
|
|
|
* Genera un ID fittizio per l'aggiunta di allegati a livello temporaneo.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public static function getFakeID()
|
|
|
|
{
|
|
|
|
return -rand(1, 9999);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sposta gli allegati fittizi a un record reale.
|
|
|
|
*
|
|
|
|
* @param int $fake_id
|
|
|
|
* @param int $id_record
|
|
|
|
*/
|
|
|
|
public static function updateFake($fake_id, $id_record)
|
|
|
|
{
|
2018-09-24 18:10:16 +02:00
|
|
|
database()->update('zz_files', [
|
2018-07-19 11:48:07 +02:00
|
|
|
'id_record' => $id_record,
|
|
|
|
], [
|
|
|
|
'id_record' => $fake_id,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-08-31 11:39:38 +02:00
|
|
|
/**
|
|
|
|
* Copia gli allegati di un record in un altro record.
|
|
|
|
*
|
|
|
|
* @param array $from
|
|
|
|
* @param array $to
|
2018-09-03 16:49:43 +02:00
|
|
|
*
|
2018-08-31 11:39:38 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function copy($from, $to)
|
|
|
|
{
|
|
|
|
$attachments = self::get($from);
|
|
|
|
|
2020-09-23 13:36:37 +02:00
|
|
|
$directory = base_dir().'/'.self::getDirectory($to['id_module'], $to['id_plugin']);
|
|
|
|
$directory_from = base_dir().'/'.self::getDirectory($from['id_module'], $from['id_plugin']);
|
2018-08-31 11:39:38 +02:00
|
|
|
|
|
|
|
foreach ($attachments as $attachment) {
|
|
|
|
$data = array_merge($attachment, $to);
|
|
|
|
|
|
|
|
// Individuazione del nuovo nome fisico
|
|
|
|
$data['filename'] = self::getName($directory_from.'/'.$attachment['filename'], $data);
|
|
|
|
|
|
|
|
// Copia fisica
|
|
|
|
if (!copy($directory_from.'/'.$attachment['filename'], $directory.'/'.$data['filename'])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Registrazione del file
|
|
|
|
self::register($data);
|
|
|
|
|
|
|
|
// Operazioni finali
|
2019-01-02 14:15:16 +01:00
|
|
|
$options = [];
|
2018-08-31 11:39:38 +02:00
|
|
|
self::processOptions($data, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-29 12:03:22 +01:00
|
|
|
protected static function processOptions($data, $options)
|
|
|
|
{
|
2020-09-23 13:36:37 +02:00
|
|
|
$directory = base_dir().'/'.self::getDirectory($data['id_module'], $data['id_plugin']);
|
2018-12-29 12:03:22 +01:00
|
|
|
|
|
|
|
if (!empty($options['thumbnails'])) {
|
|
|
|
self::thumbnails($directory.'/'.$data['filename'], $directory);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Controlla se l'estensione è supportata dal sistema di upload.
|
|
|
|
*
|
|
|
|
* @param string $extension
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected static function isSupportedType($extension)
|
|
|
|
{
|
2020-12-06 16:35:36 +01:00
|
|
|
return !in_array(strtolower($extension), array_keys(self::$not_allowed_types));
|
2018-12-29 12:03:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Genera le thumbnails per le immagini.
|
|
|
|
*
|
|
|
|
* @param string $filepath
|
|
|
|
* @param string $directory
|
|
|
|
*/
|
|
|
|
protected static function thumbnails($filepath, $directory = null)
|
|
|
|
{
|
|
|
|
$fileinfo = self::fileInfo($filepath);
|
|
|
|
$directory = empty($directory) ? dirname($filepath) : $directory;
|
|
|
|
|
2020-05-04 09:45:48 +02:00
|
|
|
if (!in_array(mime_content_type($filepath), ['image/x-png', 'image/gif', 'image/jpeg'])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-29 12:03:22 +01:00
|
|
|
$driver = extension_loaded('gd') ? 'gd' : 'imagick';
|
|
|
|
Intervention\Image\ImageManagerStatic::configure(['driver' => $driver]);
|
|
|
|
|
|
|
|
$img = Intervention\Image\ImageManagerStatic::make($filepath);
|
|
|
|
|
|
|
|
$img->resize(600, null, function ($constraint) {
|
|
|
|
$constraint->aspectRatio();
|
|
|
|
});
|
|
|
|
$img->save(slashes($directory.'/'.$fileinfo['filename'].'_thumb600.'.$fileinfo['extension']));
|
|
|
|
|
|
|
|
$img->resize(250, null, function ($constraint) {
|
|
|
|
$constraint->aspectRatio();
|
|
|
|
});
|
|
|
|
$img->save(slashes($directory.'/'.$fileinfo['filename'].'_thumb250.'.$fileinfo['extension']));
|
|
|
|
|
|
|
|
$img->resize(100, null, function ($constraint) {
|
|
|
|
$constraint->aspectRatio();
|
|
|
|
});
|
|
|
|
$img->save(slashes($directory.'/'.$fileinfo['filename'].'_thumb100.'.$fileinfo['extension']));
|
|
|
|
}
|
2018-07-03 11:12:32 +02:00
|
|
|
}
|