Non puoi selezionare più di 25 argomenti
Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
328 righe
9.1 KiB
328 righe
9.1 KiB
<?php |
|
|
|
namespace App\Classes; |
|
|
|
use LogicException; |
|
use App\Models\Main\Tenant; |
|
use InvalidArgumentException; |
|
use Illuminate\Support\Facades\DB; |
|
use App\Jobs\Preservation\SendFileJob; |
|
use Illuminate\Support\Facades\Config; |
|
use Facade\Ignition\Exceptions\InvalidConfig; |
|
use App\Interfaces\PreservationClassInterface; |
|
use App\Models\Tenant\Conservazione\File as Item; |
|
use App\Models\Tenant\Conservazione\Conservazione as Package; |
|
use Exception; |
|
use UnexpectedValueException; |
|
|
|
$path = dirname(base_path()); |
|
include "{$path}/config.php"; |
|
class PreservationClass { |
|
|
|
/** |
|
* Preservation engine |
|
* |
|
* @var mixed |
|
*/ |
|
private $engine; |
|
|
|
/** |
|
* Tenant |
|
* |
|
* @var mixed |
|
*/ |
|
private $tenant; |
|
|
|
/** |
|
* Package |
|
* |
|
* @var mixed |
|
*/ |
|
private $package; |
|
|
|
/** |
|
* Debug |
|
* |
|
* @var Bool |
|
*/ |
|
private $debug; |
|
|
|
/** |
|
* Create a new preservation instance. |
|
* |
|
* @param Int $tenant |
|
* @param Int $package |
|
* @return void |
|
*/ |
|
public function __construct(Int $tenant, Int $package) |
|
{ |
|
global $pdo; |
|
|
|
$this->debug = FALSE; |
|
|
|
$this->tenant = Tenant::findOrFail($tenant); |
|
|
|
$prefix = config('json.prefix_db'); |
|
config(['database.connections.mysql-tenant.database' => "{$prefix}_ente{$this->tenant->codice}"]); |
|
DB::purge('mysql-tenant'); |
|
|
|
$this->package = Package::findOrFail($package); |
|
|
|
if(config('json.developEnv', false)) { |
|
|
|
$env = "beta"; |
|
|
|
} else if(config('json.demoEnv', false)) { |
|
|
|
$env = "demo"; |
|
|
|
} else { |
|
|
|
$env = "prod"; |
|
|
|
} |
|
|
|
config(['json.preservation' => config("preservation.drivers.{$env}.default.settings")]); |
|
|
|
$this->engine = config("preservation.drivers.{$env}.default.class"); |
|
$custom = config("preservation.drivers.{$env}.custom.{$this->tenant->codice}.class", null); |
|
|
|
if(! empty($custom)) { |
|
|
|
$this->engine = $custom; |
|
config(['json.preservation' => config("preservation.drivers.{$env}.custom.{$this->tenant->codice}.settings")]); |
|
|
|
} |
|
|
|
if(! empty($this->engine)) { |
|
|
|
$reflector = new \ReflectionClass($this->engine); |
|
if(file_exists($reflector->getFileName())) { |
|
|
|
$this->engine = new $this->engine($this->tenant); |
|
$this->engine->debug = $this->debug; |
|
|
|
} |
|
|
|
} |
|
|
|
$pdo->setConnessioneEnte($this->tenant->codice); |
|
|
|
$ente = new \ente(); |
|
$ente->excludeConnector = true; |
|
$ente->init($this->tenant->dominio); |
|
if(empty($ente->codice)) { |
|
|
|
throw new InvalidConfig("Failed to initialize EnteClass with: {$this->tenant->dominio}"); |
|
|
|
} |
|
|
|
$_SESSION["ente"] = $ente; |
|
|
|
$_SESSION["utente"] = new \App\Classes\Misc\User; |
|
$_SESSION["utente"]->codice_ente = $_SESSION["ente"]->getInfo()["codice"]; |
|
|
|
if (!defined("__CMDS_JOB_EXEC")) { |
|
define("__CMDS_JOB_EXEC", TRUE); |
|
} |
|
} |
|
|
|
/** |
|
* Attempts to send/create a documents' collection. |
|
* |
|
* @return Bool The database connection. |
|
* |
|
* @throws Exception |
|
*/ |
|
public function pushCollection() { |
|
|
|
$package = Package::findOrFail($this->package->codice); |
|
if($package->tipologia == "conservazione") { |
|
|
|
if($package->stato >= 5 && $package->stato < 20) { |
|
|
|
if(empty($this->package->pid)) { |
|
|
|
$this->engine->pushCollection($this->package); |
|
|
|
} |
|
|
|
$package->stato = 10; |
|
$package->save(); |
|
|
|
} |
|
|
|
if($package->items()->count() < 1) { |
|
|
|
$package->stato = 90; |
|
$package->save(); |
|
|
|
throw new LogicException("No file available in package ID: {$package->codice}"); |
|
|
|
} |
|
|
|
foreach($package->items()->get() as $file) { |
|
|
|
$this->pushItem($file->codice); |
|
|
|
} |
|
|
|
$this->engine->checkPreservationStatus($this->tenant, $this->package); |
|
|
|
} |
|
|
|
|
|
} |
|
|
|
/** |
|
* Retrive Item information |
|
* |
|
* @param mixed $item |
|
* @return Array |
|
*/ |
|
public function getItem(Int $item) : ?Array |
|
{ |
|
global $config; |
|
|
|
ini_set('memory_limit', 1024 * 5 . "M"); |
|
|
|
$item = Item::findOrFail($item); |
|
|
|
$content = null; |
|
|
|
$meta = json_decode(base64_decode(gzinflate($item->metadati)), TRUE); |
|
$meta["amministrazione_titolare"] = collect($_SESSION["ente"]->getInfo())->only("codice", "codice_ipa", "dominio", "denominazione", "cf", "pec", "indirizzo", "citta", "provincia", "cap", "stato", "telefono")->toArray(); |
|
$meta["oggetto"] = "Fascicolo {{$item->package->codice}}"; |
|
$meta["materia"] = ""; |
|
$meta["procedimento"] = ""; |
|
|
|
if($item->package->modulo == "gare" && ! empty($meta["info"])) { |
|
$meta["materia"] = "Gara di appalto"; |
|
$meta["oggetto"] = "Fascicolo di Gara {ID:{$meta["info"]["gara"]["id"]}}"; |
|
if(! empty($meta["cup"])) { $meta["oggetto"] = "{$meta["oggetto"]} - CUP {$meta["cup"]}"; } |
|
$meta["procedimento"] = $meta["info"]["gara"]["oggetto"]; |
|
} |
|
|
|
switch($item->tabella_riferimento) { |
|
case 'b_filemanager': |
|
$file = \filesManager::fileInfo($item->codice_riferimento, $meta["nomeFile"], $meta["source_id"], $meta["source_type"], true); |
|
if(! empty($file["content"])) { |
|
$content = &$file["content"]; |
|
} |
|
break; |
|
case 'b_gare_buste': |
|
$gara = \gara::init($meta["codice_gara"], $meta["codice_round"]); |
|
$gara->setLotto($meta["codice_lotto"]); |
|
$busta = $gara->getBusta($meta["codice_partecipante"], $meta["richiesta"]["codice"], false, true); |
|
if(! empty($busta["content"])) { |
|
$content = &$busta["content"]; |
|
} |
|
break; |
|
case 'r_richieste_oe': |
|
if(! empty($meta["codice_gara"])) { |
|
$oggetto = \gara::init($meta["codice_gara"], $meta["codice_round"]); |
|
$oggetto->setLotto($meta["codice_lotto"]); |
|
} |
|
if(! empty($meta["codice_contratto"])) { |
|
$oggetto = \stipula::init($meta["codice_contratto"]); |
|
} |
|
if(! empty($oggetto)) { |
|
$integrazioni = $oggetto->getManagerRichieste(); |
|
$documento = $integrazioni->getContent($meta["richiesta"]["codice"], $meta["codice_relazione"], true); |
|
if(! empty($documento["content"])) { |
|
$content = &$documento["content"]; |
|
} |
|
} |
|
break; |
|
case 'r_comunicazioni_utenti': |
|
if(file_exists($meta["path"])) { |
|
$content = file_get_contents($meta["path"]); |
|
if ($config["enable_smtp_send"]) { |
|
$content = gzuncompress($content); |
|
} |
|
} |
|
break; |
|
case 'b_contratti_sottoscritti': |
|
$contratto = \stipula::init($meta["codice_contratto"]); |
|
if(! empty($contratto)) { |
|
$contratto_sottoscritto = $contratto->getContrattoSottoscritto(true, $meta["codice_firmatario"]); |
|
if(! empty($contratto_sottoscritto["content"])) { |
|
$content = &$contratto_sottoscritto["content"]; |
|
} |
|
} |
|
break; |
|
default: |
|
throw new Exception("I file della tabella {$item->tabella_riferimento} non sono gestiti correttamente."); |
|
break; |
|
} |
|
|
|
if(empty($content)) { |
|
|
|
$item->stato = 90; |
|
$item->package->pid = null; |
|
$item->save(); |
|
|
|
$item->package->items()->delete(); |
|
|
|
throw new UnexpectedValueException("Invalid content file for item id {$item->codice}"); |
|
|
|
} |
|
|
|
return [&$item, &$meta, &$content]; |
|
|
|
} |
|
|
|
|
|
/** |
|
* Attempts to send a document. |
|
* |
|
* @return void |
|
*/ |
|
public function pushItem(Int $item) { |
|
|
|
list($item, $meta, $content) = $this->getItem($item); |
|
if(($item->package->stato >= 5 && $item->package->stato < 20) && ($item->stato < 20 || $item->stato == 90)) { |
|
|
|
$item->stato = 10; |
|
$item->save(); |
|
|
|
$this->engine->pushItem($item, $meta, $content); |
|
|
|
} |
|
|
|
} |
|
|
|
/** |
|
* Check if files preservation has been completed. |
|
* |
|
* @param mixed $var |
|
* @return void |
|
*/ |
|
public function checkPreservationStatus() |
|
{ |
|
$package = Package::findOrFail($this->package->codice); |
|
$this->engine->checkPreservationStatus($this->tenant, $package); |
|
} |
|
|
|
/** |
|
* Get Tenant's ID |
|
* |
|
* @return Int |
|
*/ |
|
public function getTenant() : Int |
|
{ |
|
return $this->tenant->codice; |
|
} |
|
|
|
/** |
|
* Enable debugging mode |
|
* |
|
* @return void |
|
*/ |
|
public function enableDebugging() |
|
{ |
|
$this->debug = true; |
|
$this->engine->debug = $this->debug; |
|
} |
|
|
|
}
|
|
|