mirror of
https://github.com/devcode-it/openstamanager.git
synced 2025-01-31 07:55:17 +01:00
Introduzione connettori per il caricamento dei file
This commit is contained in:
parent
d818d2e393
commit
f53559eef1
20
actions.php
20
actions.php
@ -46,13 +46,6 @@ if (filter('op') == 'aggiungi-allegato' || filter('op') == 'rimuovi-allegato') {
|
||||
]));
|
||||
}
|
||||
|
||||
// Controllo sui permessi di scrittura per il file system
|
||||
elseif (!directory($upload_dir)) {
|
||||
flash()->error(tr('Non hai i permessi di scrittura nella cartella _DIR_!', [
|
||||
'_DIR_' => '"files"',
|
||||
]));
|
||||
}
|
||||
|
||||
// Gestione delle operazioni
|
||||
else {
|
||||
// UPLOAD PER CKEDITOR
|
||||
@ -145,6 +138,7 @@ if (filter('op') == 'aggiungi-allegato' || filter('op') == 'rimuovi-allegato') {
|
||||
|
||||
// DELETE
|
||||
elseif (filter('op') == 'rimuovi-allegato' && filter('filename') !== null) {
|
||||
|
||||
$name = Uploads::delete(filter('filename'), [
|
||||
'id_module' => $id_module,
|
||||
'id_plugin' => $id_plugin,
|
||||
@ -168,7 +162,17 @@ if (filter('op') == 'aggiungi-allegato' || filter('op') == 'rimuovi-allegato') {
|
||||
elseif (filter('op') == 'download-allegato') {
|
||||
$rs = $dbo->fetchArray('SELECT * FROM zz_files WHERE id_module='.prepare($id_module).' AND id='.prepare(filter('id')).' AND filename='.prepare(filter('filename')));
|
||||
|
||||
download($upload_dir.'/'.$rs[0]['filename'], $rs[0]['original']);
|
||||
//download($upload_dir.'/'.$rs[0]['filename'], $rs[0]['original']);
|
||||
$file = Models\Upload::find($rs[0]['id']);
|
||||
|
||||
if (!empty($file)) {
|
||||
$content = $file->get_contents();
|
||||
|
||||
header('Content-Type: application/octet-stream');
|
||||
header('Content-Transfer-Encoding: Binary');
|
||||
header('Content-disposition: attachment; filename="'.basename($file->original_name).'"');
|
||||
echo $content;
|
||||
}
|
||||
} elseif (filter('op') == 'visualizza-modifica-allegato') {
|
||||
include_once base_dir().'/include/modifica_allegato.php';
|
||||
}
|
||||
|
@ -43,6 +43,8 @@
|
||||
"league/csv": "^9.7.0",
|
||||
"league/oauth2-client": "^2.6",
|
||||
"league/oauth2-google": "^4.0",
|
||||
"league/flysystem": "^3.0",
|
||||
"league/flysystem-ftp": "^3.0",
|
||||
"maximebf/debugbar": "^1.15",
|
||||
"monolog/monolog": "^1.22",
|
||||
"mpdf/mpdf": "^v8.0.10",
|
||||
@ -110,6 +112,7 @@
|
||||
"Modules\\Impostazioni\\": ["modules/impostazioni/custom/src/", "modules/impostazioni/src/"],
|
||||
"Modules\\Partitario\\": ["modules/partitario/custom/src/", "modules/partitario/src/"],
|
||||
"Modules\\StatoEmail\\": ["modules/stato_email/custom/src/", "modules/stato_email/src/"],
|
||||
"Modules\\FileAdapters\\": ["modules/adattatori_archiviazione/custom/src/", "modules/adattatori_archiviazione/src/"],
|
||||
"Plugins\\ExportFE\\": ["plugins/exportFE/custom/src/", "plugins/exportFE/src/"],
|
||||
"Plugins\\ImportFE\\": ["plugins/importFE/custom/src/", "plugins/importFE/src/"],
|
||||
"Plugins\\ReceiptFE\\": ["plugins/receiptFE/custom/src/", "plugins/receiptFE/src/"],
|
||||
|
58
modules/adattatori_archiviazione/actions.php
Normal file
58
modules/adattatori_archiviazione/actions.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/*
|
||||
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
|
||||
* Copyright (C) DevCode s.r.l.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
include_once __DIR__.'/../../core.php';
|
||||
|
||||
use Modules\FileAdapters\FileAdapter;
|
||||
|
||||
switch (filter('op')) {
|
||||
case 'add':
|
||||
|
||||
$adapter = new FileAdapter();
|
||||
|
||||
$adapter->name = post('name');
|
||||
$adapter->class = "\\Modules\\FileAdapters\\Adapters\\".post('class');
|
||||
|
||||
$adapter->save();
|
||||
|
||||
$id_record = $adapter->id;
|
||||
|
||||
flash()->info(tr('Nuoovo adattatore aggiunto!'));
|
||||
|
||||
break;
|
||||
|
||||
case 'update':
|
||||
|
||||
$adapter->name = post('name');
|
||||
$adapter->class = "\\Modules\\FileAdapters\\Adapters\\".post('class');
|
||||
$adapter->options = post('options');
|
||||
$adapter->is_default = post('is_default');
|
||||
|
||||
$adapter->save();
|
||||
|
||||
flash()->info(tr('Adattatore modificato correttamente!'));
|
||||
|
||||
break;
|
||||
|
||||
case 'delete':
|
||||
|
||||
$adapter->delete();
|
||||
|
||||
break;
|
||||
}
|
43
modules/adattatori_archiviazione/add.php
Normal file
43
modules/adattatori_archiviazione/add.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
/*
|
||||
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
|
||||
* Copyright (C) DevCode s.r.l.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
include_once __DIR__.'/../../core.php';
|
||||
|
||||
?><form action="" method="post" id="add-form">
|
||||
<input type="hidden" name="op" value="add">
|
||||
<input type="hidden" name="backto" value="record-edit">
|
||||
<input type="hidden" name="dir" value="<?php echo $dir; ?>">
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
{[ "type": "text", "label": "<?php echo tr('Name'); ?>", "name": "name", "required": 1 ]}
|
||||
</div>
|
||||
|
||||
<div class="col-md-6">
|
||||
{[ "type": "select", "label": "<?php echo tr('Tipo archiviazione'); ?>", "name": "class", "values": "list=\"LocalAdapter\":\"<?=tr('Archiviazione locale')?>\",\"FTPAdapter\":\"<?=tr('Archiviazione FTP')?>\"", "required": 1 ]}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- PULSANTI -->
|
||||
<div class="row">
|
||||
<div class="col-md-12 text-right">
|
||||
<button type="submit" class="btn btn-primary"><i class="fa fa-plus"></i> <?php echo tr('Aggiungi'); ?></button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
64
modules/adattatori_archiviazione/edit.php
Normal file
64
modules/adattatori_archiviazione/edit.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
/*
|
||||
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
|
||||
* Copyright (C) DevCode s.r.l.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
include_once __DIR__.'/../../core.php';
|
||||
|
||||
?><form action="" method="post" id="add-form">
|
||||
<input type="hidden" name="op" value="update">
|
||||
<input type="hidden" name="backto" value="record-edit">
|
||||
<input type="hidden" name="dir" value="<?php echo $dir; ?>">
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-5">
|
||||
{[ "type": "text", "label": "<?php echo tr('Name'); ?>", "name": "name", "value": "$name$", "required": 1 ]}
|
||||
</div>
|
||||
|
||||
<div class="col-md-5">
|
||||
{[ "type": "select", "label": "<?php echo tr('Tipo archiviazione'); ?>", "name": "class", "value": "<?=explode('\\', $record["class"])[4]?>", "values": "list=\"LocalAdapter\":\"Archiviazione locale\",\"FTPAdapter\":\"Archiviazione FTP\"", "required": 1 ]}
|
||||
</div>
|
||||
|
||||
<div class="col-md-2">
|
||||
{[ "type": "checkbox", "label": "<?php echo tr('Predefinito'); ?>", "name": "is_default", "value": "$is_default$" ]}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
{[ "type": "textarea", "label": "<?php echo tr('Options'); ?>", "name": "options", "value": "$options$", "required": 0, "help": "Specifica le opzioni per il tipo di archiviazione in formato JSON" ]}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- PULSANTI -->
|
||||
<?php
|
||||
if(!$adapter->is_default && $adapter->can_delete) {
|
||||
echo '
|
||||
<a class="btn btn-danger ask" data-backto="record-list">
|
||||
<i id ="elimina" class="fa fa-trash"></i> <?php echo tr(\'Elimina\'); ?>
|
||||
</a>';
|
||||
}else{
|
||||
echo '
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
<div class="alert alert-warning">L\'adattatore non può essere eliminato.</div>
|
||||
</div>
|
||||
</div>';
|
||||
}
|
||||
?>
|
||||
|
||||
</form>
|
28
modules/adattatori_archiviazione/init.php
Normal file
28
modules/adattatori_archiviazione/init.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
/*
|
||||
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
|
||||
* Copyright (C) DevCode s.r.l.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
include_once __DIR__.'/../../core.php';
|
||||
|
||||
use Modules\FileAdapters\FileAdapter;
|
||||
|
||||
if (isset($id_record)) {
|
||||
$adapter = FileAdapter::find($id_record);
|
||||
|
||||
$record = $dbo->fetchOne('SELECT * FROM `zz_storage_adapters` WHERE `id`='.prepare($id_record));
|
||||
}
|
34
modules/adattatori_archiviazione/src/Adapters/FTPAdapter.php
Normal file
34
modules/adattatori_archiviazione/src/Adapters/FTPAdapter.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
/*
|
||||
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
|
||||
* Copyright (C) DevCode s.r.l.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
namespace Modules\FileAdapters\Adapters;
|
||||
|
||||
use League\Flysystem\Ftp\FTPAdapter as OriginalAdapter;
|
||||
use League\Flysystem\Ftp\FtpConnectionOptions;
|
||||
|
||||
class FTPAdapter extends OriginalAdapter {
|
||||
|
||||
public function __construct( $options ){
|
||||
|
||||
$options = json_decode($options, 1);
|
||||
|
||||
parent::__construct(FtpConnectionOptions::fromArray($options));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
/*
|
||||
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
|
||||
* Copyright (C) DevCode s.r.l.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
namespace Modules\FileAdapters\Adapters;
|
||||
|
||||
use League\Flysystem\Local\LocalFilesystemAdapter as OriginalAdapter;
|
||||
|
||||
class LocalAdapter extends OriginalAdapter {
|
||||
|
||||
public function __construct( $options ){
|
||||
|
||||
$options = json_decode($options);
|
||||
|
||||
parent::__construct(base_dir()."/".$options->directory);
|
||||
}
|
||||
|
||||
}
|
61
modules/adattatori_archiviazione/src/FileAdapter.php
Normal file
61
modules/adattatori_archiviazione/src/FileAdapter.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
/*
|
||||
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
|
||||
* Copyright (C) DevCode s.r.l.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
namespace Modules\FileAdapters;
|
||||
|
||||
use Common\SimpleModelTrait;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Traits\LocalPoolTrait;
|
||||
|
||||
class FileAdapter extends Model
|
||||
{
|
||||
use SimpleModelTrait;
|
||||
use LocalPoolTrait;
|
||||
use SoftDeletes;
|
||||
|
||||
protected $table = 'zz_storage_adapters';
|
||||
|
||||
public function testConnection()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function setIsDefaultAttribute($valore){
|
||||
|
||||
self::getAll()->where('id', '!=', $this->id)->each(function($item){
|
||||
$item->attributes['is_default'] = false;
|
||||
$item->save();
|
||||
});
|
||||
|
||||
$this->attributes['is_default'] = $valore;
|
||||
}
|
||||
|
||||
public static function getDefaultConnector(){
|
||||
|
||||
return self::where('is_default', 1)->first();
|
||||
}
|
||||
|
||||
public static function getLocalConnector(){
|
||||
|
||||
return self::where('is_local', 1)->first();
|
||||
}
|
||||
|
||||
|
||||
}
|
73
modules/adattatori_archiviazione/src/OSMFilesystem.php
Normal file
73
modules/adattatori_archiviazione/src/OSMFilesystem.php
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
/*
|
||||
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
|
||||
* Copyright (C) DevCode s.r.l.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
namespace Modules\FileAdapters;
|
||||
|
||||
use League\Flysystem\Filesystem;
|
||||
|
||||
class OSMFilesystem extends Filesystem
|
||||
{
|
||||
|
||||
/** @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',
|
||||
];
|
||||
|
||||
public function upload($directory, $filename, $contents){
|
||||
|
||||
//Verifico se l'esensione non è consentita
|
||||
$extension = pathinfo($filename, PATHINFO_EXTENSION);
|
||||
$extension = strtolower($extension);
|
||||
|
||||
// Controllo sulle estensioni permesse
|
||||
$allowed = self::isSupportedType($extension);
|
||||
if (!$allowed) {
|
||||
flash()->error(tr('Estensione non supportata!'));
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!$this->directoryExists($directory)){
|
||||
try{
|
||||
$this->createDirectory($directory);
|
||||
}catch(\Exception $e){
|
||||
flash()->error(tr('Impossibile creare la cartella controllare i permessi!'));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
do {
|
||||
$filename = random_string().'.'.$extension;
|
||||
} while ($this->fileExists($directory.'/'.$filename));
|
||||
|
||||
|
||||
$this->write($directory."/".$filename, $contents);
|
||||
return ["filename" => $filename, "extension" => $extension];
|
||||
}
|
||||
|
||||
protected static function isSupportedType($extension)
|
||||
{
|
||||
return !in_array(strtolower($extension), array_keys(self::$not_allowed_types));
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -19,6 +19,7 @@
|
||||
|
||||
use Modules\Importazione\Import;
|
||||
use Models\Module;
|
||||
use Modules\FileAdapters\FileAdapter;
|
||||
|
||||
include_once __DIR__.'/../../core.php';
|
||||
|
||||
@ -28,13 +29,16 @@ switch (filter('op')) {
|
||||
case 'add':
|
||||
$id_import = filter('id_import');
|
||||
$import = Import::find($id_import);
|
||||
$id_adapter = FileAdapter::getLocalConnector()->id;
|
||||
|
||||
$id_record = $import->id;
|
||||
|
||||
Uploads::upload($_FILES['file'], [
|
||||
'id_module' => $id_module,
|
||||
'id_record' => $id_record,
|
||||
'id_adapter' => $id_adapter,
|
||||
]);
|
||||
|
||||
break;
|
||||
|
||||
case 'example':
|
||||
|
@ -33,7 +33,7 @@ if (empty($id_record)) {
|
||||
$import_selezionato = $import->class;
|
||||
|
||||
// Inizializzazione del lettore CSV
|
||||
$csv = new $import_selezionato($record->filepath);
|
||||
$csv = new $import_selezionato($record->local_filepath);
|
||||
$fields = $csv->getAvailableFields();
|
||||
|
||||
// Generazione della base per i campi da selezionare
|
||||
|
@ -22,21 +22,13 @@ namespace Models;
|
||||
use Common\SimpleModelTrait;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Intervention\Image\ImageManagerStatic;
|
||||
use Util\FileSystem;
|
||||
use Modules\FileAdapters\FileAdapter;
|
||||
use Modules\FileAdapters\OSMFilesystem;
|
||||
|
||||
class Upload extends Model
|
||||
{
|
||||
use SimpleModelTrait;
|
||||
|
||||
/** @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',
|
||||
];
|
||||
|
||||
/** @var array Elenco delle estensioni file per mime type */
|
||||
protected static $extension_association = [
|
||||
'image/gif' => 'gif',
|
||||
@ -99,31 +91,49 @@ class Upload extends Model
|
||||
$model->id_plugin = !empty($data['id_plugin']) ? $data['id_plugin'] : null;
|
||||
$model->id_record = !empty($data['id_record']) ? $data['id_record'] : null;
|
||||
|
||||
// Definizione del nome fisico del file
|
||||
$directory = base_dir().'/'.$model->directory;
|
||||
$filename = self::getNextName($original_name, $directory);
|
||||
if (empty($filename)) {
|
||||
throw new \UnexpectedValueException("Estensione dell'allegato non supportata");
|
||||
//Caricamento file con interfaccia di upload
|
||||
|
||||
//Verifico qual'è il metodo predefinito al momento
|
||||
if(!empty($data['id_adapter'])){
|
||||
$adapter_config = FileAdapter::find($data['id_adapter']);
|
||||
}else{
|
||||
$adapter_config = FileAdapter::getDefaultConnector();
|
||||
}
|
||||
$model->filename = $filename;
|
||||
$class = $adapter_config->class;
|
||||
|
||||
// Creazione del file fisico
|
||||
directory($directory);
|
||||
$file = slashes($directory.DIRECTORY_SEPARATOR.$filename);
|
||||
if (
|
||||
(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)
|
||||
) {
|
||||
throw new \UnexpectedValueException("Errore durante il salvataggio dell'allegato");
|
||||
//Costruisco l'oggetto che mi permetterà di effettuare il caricamento
|
||||
$adapter = new $class($adapter_config->options);
|
||||
$filesystem = new OSMFilesystem($adapter);
|
||||
|
||||
//Su source arriva direttamente il contenuto o il formato file
|
||||
if (is_array($source)) {
|
||||
//Caricamento con l'interfaccia di upload
|
||||
try{
|
||||
$file = $filesystem->upload($model->directory, $original_name, file_get_contents($source['tmp_name']));
|
||||
}catch(\Exception $e){
|
||||
flash()->error(tr('Impossibile creare il file!'));
|
||||
return false;
|
||||
}
|
||||
|
||||
// Aggiornamento dimensione fisica e responsabile del caricamento
|
||||
$model->size = $source['size'];
|
||||
}else{
|
||||
//Caricamento con l'interfaccia di upload
|
||||
try{
|
||||
$file = $filesystem->upload($model->directory, $original_name, $source);
|
||||
}catch(\Exception $e){
|
||||
flash()->error(tr('Impossibile creare il file!'));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Aggiornamento dimensione fisica e responsabile del caricamento
|
||||
$model->size = FileSystem::fileSize($file);
|
||||
$model->id_adapter = $adapter_config->id;
|
||||
$model->filename = $file['filename'];
|
||||
|
||||
$model->user()->associate(auth()->getUser());
|
||||
|
||||
// Rimozione estensione dal nome visibile
|
||||
$extension = $model->extension;
|
||||
$extension = $file['extension'];
|
||||
if (string_ends_with($model->name, $extension)) {
|
||||
$length = strlen($extension) + 1;
|
||||
$model->name = substr($model->name, 0, -$length);
|
||||
@ -134,29 +144,6 @@ class Upload extends Model
|
||||
return $model;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getInfoAttribute()
|
||||
{
|
||||
if (!isset($this->file_info)) {
|
||||
$filepath = $this->filepath;
|
||||
$infos = self::getInfo($filepath);
|
||||
|
||||
$this->file_info = $infos;
|
||||
}
|
||||
|
||||
return $this->file_info;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getExtensionAttribute()
|
||||
{
|
||||
return strtolower($this->info['extension']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
@ -164,23 +151,7 @@ class Upload extends Model
|
||||
{
|
||||
$parent = $this->plugin ?: $this->module;
|
||||
|
||||
return $parent->upload_directory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getFilepathAttribute()
|
||||
{
|
||||
return $this->directory.'/'.$this->filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getFileurlAttribute()
|
||||
{
|
||||
return str_replace('\\', '/', $this->filepath);
|
||||
return strtolower($parent->name);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -196,16 +167,6 @@ class Upload extends Model
|
||||
$this->attributes['original'] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restituisce i contenuti del file.
|
||||
*
|
||||
* @return false|string
|
||||
*/
|
||||
public function getContent()
|
||||
{
|
||||
return file_get_contents($this->filepath);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
@ -254,17 +215,14 @@ class Upload extends Model
|
||||
|
||||
public function delete()
|
||||
{
|
||||
$info = $this->info;
|
||||
$directory = base_dir().'/'.$this->directory;
|
||||
$adapter_config = FileAdapter::find($this->id_adapter);
|
||||
$class = $adapter_config->class;
|
||||
|
||||
$files = [
|
||||
$directory.'/'.$info['basename'],
|
||||
$directory.'/'.$info['filename'].'_thumb600.'.$info['extension'],
|
||||
$directory.'/'.$info['filename'].'_thumb100.'.$info['extension'],
|
||||
$directory.'/'.$info['filename'].'_thumb250.'.$info['extension'],
|
||||
];
|
||||
//Costruisco l'oggetto che mi permetterà di effettuare il caricamento
|
||||
$adapter = new $class($adapter_config->options);
|
||||
$filesystem = new OSMFilesystem($adapter);
|
||||
|
||||
delete($files);
|
||||
$filesystem->delete($this->directory."/".$this->filename);
|
||||
|
||||
return parent::delete();
|
||||
}
|
||||
@ -280,16 +238,24 @@ class Upload extends Model
|
||||
|
||||
public function copia($data)
|
||||
{
|
||||
$result = self::build(base_dir().'/'.$this->filepath, $data, $this->name, $this->category);
|
||||
$data['original_name'] = $this->original_name;
|
||||
|
||||
$adapter_config = FileAdapter::find($this->id_adapter);
|
||||
$class = $adapter_config->class;
|
||||
|
||||
//Costruisco l'oggetto che mi permetterà di effettuare il caricamento
|
||||
$adapter = new $class($adapter_config->options);
|
||||
$filesystem = new OSMFilesystem($adapter);
|
||||
|
||||
$source = $this->get_contents();
|
||||
|
||||
$file = $filesystem->read($this->directory."/".$this->filename);
|
||||
|
||||
$result = self::build($file, $data, $this->name, $this->category);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function getInfo($file)
|
||||
{
|
||||
return pathinfo($file);
|
||||
}
|
||||
|
||||
/* Relazioni Eloquent */
|
||||
|
||||
public function module()
|
||||
@ -307,39 +273,33 @@ class Upload extends Model
|
||||
return $this->belongsTo(User::class, 'created_by');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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));
|
||||
public function get_contents(){
|
||||
$adapter_config = FileAdapter::find($this->id_adapter);
|
||||
$class = $adapter_config->class;
|
||||
|
||||
//Costruisco l'oggetto che mi permetterà di effettuare il caricamento
|
||||
$adapter = new $class($adapter_config->options);
|
||||
$filesystem = new OSMFilesystem($adapter);
|
||||
|
||||
return $filesystem->read($this->directory."/".$this->filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Genera casualmente il nome fisico per il file.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function getNextName($file, $directory)
|
||||
public function getLocalFilepathAttribute()
|
||||
{
|
||||
$extension = self::getInfo($file)['extension'];
|
||||
$extension = strtolower($extension);
|
||||
$parent = $this->plugin ?: $this->module;
|
||||
|
||||
// Controllo sulle estensioni permesse
|
||||
$allowed = self::isSupportedType($extension);
|
||||
if (!$allowed) {
|
||||
return false;
|
||||
}
|
||||
return $parent->upload_directory.'/'.$this->filename;
|
||||
}
|
||||
|
||||
do {
|
||||
$filename = random_string().'.'.$extension;
|
||||
} while (file_exists($directory.'/'.$filename));
|
||||
|
||||
return $filename;
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLocalFileurlAttribute()
|
||||
{
|
||||
return str_replace('\\', '/', $this->local_filepath);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -376,4 +336,44 @@ class Upload extends Model
|
||||
});
|
||||
$img->save(slashes($directory.'/'.$info['filename'].'_thumb100.'.$info['extension']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getInfoAttribute()
|
||||
{
|
||||
if (!isset($this->file_info)) {
|
||||
$filepath = $this->local_filepath;
|
||||
$infos = self::getInfo($local_filepath);
|
||||
|
||||
$this->file_info = $infos;
|
||||
}
|
||||
|
||||
return $this->file_info;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getExtensionAttribute()
|
||||
{
|
||||
$extension = pathinfo($this->filename, PATHINFO_EXTENSION);
|
||||
$extension = strtolower($extension);
|
||||
return $extension;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restituisce i contenuti del file locale.
|
||||
*
|
||||
* @return false|string
|
||||
*/
|
||||
public function getContent()
|
||||
{
|
||||
return file_get_contents($this->local_filepath);
|
||||
}
|
||||
|
||||
public static function getInfo($file)
|
||||
{
|
||||
return pathinfo($file);
|
||||
}
|
||||
}
|
||||
|
@ -100,34 +100,18 @@ class Uploads
|
||||
'id_plugin' => !empty($data['id_plugin']) ? $data['id_plugin'] : null,
|
||||
'id_record' => $data['id_record'],
|
||||
]);
|
||||
$name = $file['name'];
|
||||
|
||||
$fileinfo = self::fileInfo($filename);
|
||||
$directory = base_dir().'/'.self::getDirectory($data['id_module'], $data['id_plugin']);
|
||||
|
||||
$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'],
|
||||
]);
|
||||
|
||||
if ((new Module())->getByName('Stampe')->id_record == $data['id_module']) {
|
||||
$database->delete('zz_files_print', [
|
||||
'id_file' => $file['id'],
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
if(!empty($file)){
|
||||
$name = $file['name'];
|
||||
|
||||
$upload = Upload::find($file['id']);
|
||||
$upload->delete();
|
||||
|
||||
return $name;
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
|
44
view.php
44
view.php
@ -27,20 +27,38 @@ if (empty($file)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$link = base_path().'/'.$file->filepath;
|
||||
$file_content = $file->get_contents();
|
||||
|
||||
// Force download of the file
|
||||
if (get('download') == '1') {
|
||||
header('Content-Type: application/octet-stream');
|
||||
header('Content-Transfer-Encoding: Binary');
|
||||
header('Content-disposition: attachment; filename="'.basename($file->original_name).'"');
|
||||
readfile(base_dir().'/'.$file->filepath);
|
||||
// download(base_dir().'/'.$file->filepath, basename($file->original_name));
|
||||
echo $file_content;
|
||||
exit;
|
||||
}
|
||||
|
||||
// Force preview of the file
|
||||
if (get('preview') == '1') {
|
||||
if ($file->isImage()) {
|
||||
|
||||
$finfo = finfo_open();
|
||||
$mime_type = finfo_buffer($finfo, $file_content, FILEINFO_MIME_TYPE);
|
||||
finfo_close($finfo);
|
||||
|
||||
header('Content-Type: '.$mime_type);
|
||||
echo $file_content;
|
||||
|
||||
} elseif ($file->isPDF()) {
|
||||
|
||||
header("Content-type: application/pdf");
|
||||
echo $file_content;
|
||||
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($file->isFatturaElettronica()) {
|
||||
$content = file_get_contents(base_dir().'/'.$file->filepath);
|
||||
|
||||
// Individuazione stylesheet
|
||||
$default_stylesheet = 'asso-invoice';
|
||||
@ -55,7 +73,7 @@ if ($file->isFatturaElettronica()) {
|
||||
|
||||
// XML
|
||||
$xml = new DOMDocument();
|
||||
$xml->loadXML($content);
|
||||
$xml->loadXML($file_content);
|
||||
|
||||
// XSL
|
||||
$xsl = new DOMDocument();
|
||||
@ -89,15 +107,19 @@ if ($file->isFatturaElettronica()) {
|
||||
</style>';
|
||||
|
||||
if ($file->isImage()) {
|
||||
echo '
|
||||
<img src="'.$link.'"></img>';
|
||||
} elseif ($file->isPDF()) {
|
||||
$preview = Prints::getPDFLink($file->filepath);
|
||||
|
||||
echo '
|
||||
<iframe src="'.($preview ?: $link).'">
|
||||
<a src="'.$link.'">'.tr('Il browser non supporta i contenuti iframe: clicca qui per raggiungere il file originale').'</a>
|
||||
<iframe src="'.base_path().'/view.php?file_id='.$file_id.'&preview=1">
|
||||
<a src="'.base_path().'/view.php?file_id='.$file_id.'&download=1">'.tr('Il browser non supporta i contenuti iframe: clicca qui per raggiungere il file originale').'</a>
|
||||
</iframe>';
|
||||
|
||||
} elseif ($file->isPDF()) {
|
||||
|
||||
echo '
|
||||
<iframe src="'.base_path().'/view.php?file_id='.$file_id.'&preview=1">
|
||||
<a src="'.base_path().'/view.php?file_id='.$file_id.'&download=1">'.tr('Il browser non supporta i contenuti iframe: clicca qui per raggiungere il file originale').'</a>
|
||||
</iframe>';
|
||||
|
||||
} else {
|
||||
echo '
|
||||
<iframe src="'.base_path().'/view.php?file_id='.$file_id.'&download=1">
|
||||
|
Loading…
x
Reference in New Issue
Block a user