<?php
|
|
/**
|
* Bean dati di upload
|
*
|
* Raccolgo i dati necessari per effettuare l'upload dei file e configurare la <br>
|
* pagina in modo dinamico, da passare a Smarty per inizializzare il codice <br>
|
* javascript di gestione del plupload
|
*
|
* @author Cristiano Magro
|
*
|
*/
|
class SetupUpload {
|
|
private $queueLimitUpload;
|
private $totLimitUpload;
|
private $sizeLimitFileUpload;
|
private $totLimitUploadTest;
|
private $fileFilterExt;
|
private $random;
|
private $pathUploadKey;
|
private $numMaxFiles = '1';
|
private $sizeMaxFilesByte;
|
private $sizeMaxFilesDesc;
|
private $debugPageActive = false;
|
|
/**
|
* Builder statico
|
*
|
* @return \SetupUpload
|
*/
|
public static function create() {
|
$instance = new self;
|
return $instance;
|
}
|
|
public function getQueueLimit() {
|
return $this->queueLimitUpload;
|
}
|
|
public function setQueueLimit($x) {
|
$this->queueLimitUpload = $x;
|
return $this;
|
}
|
|
public function getTotalLimit() {
|
return $this->totLimitUpload;
|
}
|
|
public function setTotalLimit($x) {
|
$this->totLimitUpload = $x;
|
return $this;
|
}
|
|
public function getSizeLimitFile() {
|
return $this->sizeLimitFileUpload;
|
}
|
|
/**
|
* Dimensione massima del singolo file caricato
|
*
|
* Accetta sia '1024' che '1 kb' effettuando la conversione interna
|
* necessaria.
|
* @param string $x sia '1024' che '1 kb'
|
* @return \SetupUpload
|
*/
|
public function setSizeLimitFile($x) {
|
$this->sizeLimitFileUpload = self::str2Bytes($x);
|
return $this;
|
}
|
|
public function getLimitText() {
|
return $this->totLimitUploadTest;
|
}
|
|
public function setLimitText($x) {
|
$this->totLimitUploadTest = $x;
|
return $this;
|
}
|
|
public function getFilesFilter() {
|
return $this->fileFilterExt;
|
}
|
|
public function setFilesFilter($x) {
|
$this->fileFilterExt = $x;
|
return $this;
|
}
|
|
public function getRandomValue() {
|
return $this->random;
|
}
|
|
public function setRandomValue($x) {
|
$this->random = $x;
|
return $this;
|
}
|
|
public function getPathUploadKey() {
|
return $this->pathUploadKey;
|
}
|
|
/**
|
* Chiave per decodificare la directory di salvataggio dei file
|
*
|
* La chiave viene utilizzata per ricavare dalla mappatura delle directory, il
|
* path in cui salvare il file. Da utilizzare per offuscare il filesystem del
|
* server. La directory non viene mai passata nei parametri.
|
*
|
* @param string $pathKey necessaria per accedere alla mappatura
|
*
|
* @return \SetupUpload
|
*/
|
public function setPathUploadKey($pathKey) {
|
$this->pathUploadKey = $pathKey;
|
return $this;
|
}
|
|
/**
|
* Restituisce il numero massimo di file previsti nella coda
|
* @return integer Max numero file
|
*/
|
public function getNumMaxFiles() {
|
return $this->numMaxFiles;
|
}
|
|
/**
|
* Viene impostato il numero massimo di file caricabili
|
* @param integer $numero
|
* @return \SetupUpload fluent style
|
*/
|
public function setNumMaxFiles($numero) {
|
if (!is_integer($numero) || $numero <= 0) {
|
throw new UploadFile_Exc();
|
}
|
|
$this->numMaxFiles = $numero;
|
return $this;
|
}
|
|
public function getSizeMaxFilesByte() {
|
return $this->sizeMaxFilesByte;
|
}
|
|
/**
|
* Dimensione Totale massima dei file caricabili
|
*
|
* Accetta sia '1024' che '1 kb' effettuando la conversione interna
|
* necessaria.
|
* @param string $x sia '1024' che '1 kb'
|
* @return \SetupUpload
|
*/
|
public function setSizeMaxFilesByte($x) {
|
$this->sizeMaxFilesByte = self::str2Bytes($x);
|
return $this;
|
}
|
|
public function getSizeMaxFilesDesc() {
|
return $this->sizeMaxFilesDesc;
|
}
|
|
public function setSizeMaxFilesDesc($x) {
|
$this->sizeMaxFilesDesc = $x;
|
return $this;
|
}
|
|
public function getDebugActive() {
|
return $this->debugPageActive;
|
}
|
|
public function setDebugActive($x) {
|
$this->debugPageActive = $x;
|
return $this;
|
}
|
|
/**
|
* Creo JSON con i dati per inizializzare la pagina
|
*
|
* Per inizializzare l'oggetto JS che fa da schiavo a pluploader per la
|
* configurazione della pagina, creo un json string con i dati di configurazione
|
* che verranno passati a Smarty.
|
*
|
* @return string json
|
*/
|
public function createJsonSetup() {
|
$dati = array(
|
'QUEUE_LIMIT_UPLOAD' => $this->getQueueLimit(),
|
'TOT_LIMIT_UPLOAD' => $this->getSizeMaxFilesByte(),
|
'SIZE_LIMIT_UPLOAD' => $this->getSizeLimitFile(),
|
'TOT_LIMIT_UPLOAD_TXT' => $this->getSizeMaxFilesDesc(),
|
'NUM_MAX_FILES' => $this->getNumMaxFiles(),
|
);
|
|
return json_encode($dati);
|
}
|
|
/**
|
* Converte una stringa nei corrispondenti bytes.
|
*
|
* 1 Mb = 1024^2
|
*
|
* @param string $strSize Stringa da convertire
|
* @throws UploadFile_Excs
|
* @return integer
|
*/
|
public static function str2Bytes($strSize) {
|
$Units = array('b' => 0, 'kb' => 1, 'mb' => 2, 'gb' => 3, 'tb' => 4);
|
|
$strUnit = strtolower($strSize);
|
$strUnit = preg_replace('/[^a-z]/', '', $strUnit);
|
$strValue = preg_replace('/[^0-9]/', '', $strSize);
|
$value = intval($strValue);
|
|
//validazione
|
if (strlen(trim($strSize)) === 0) {
|
//una stringa vuota non può essere convertita
|
throw new Upload_Exc_Error();
|
} elseif (strlen(trim($strValue)) === 0) {
|
//se non trovo la parte del valore della stringa non posso convertire
|
throw new Upload_Exc_Error();
|
} elseif (strlen($strUnit) >= 1 && !array_key_exists($strUnit, $Units)) {
|
//l'unita' non e' nota
|
throw new Upload_Exc_Error();
|
}
|
|
$Exponent = isset($Units[$strUnit]) ? $Units[$strUnit] : 0;
|
|
return ($value * pow(1024, $Exponent));
|
}
|
|
}
|