Classi per la configurazione upload
Cristiano Magro
2018-01-16 e3143d210800f4e51f6fb6da83881139d04f01d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?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));
    }
 
}