Classi per la configurazione upload
Cristiano Magro
2017-12-27 4136ac971482754da6d364bc27b14f45c2cedf6a
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
<?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;
    }
 
    public function setSizeLimitFile($x) {
        $this->sizeLimitFileUpload = $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;
    }
 
    public function getNumMaxFiles() {
        return $this->numMaxFiles;
    }
 
    public function setNumMaxFiles($x) {
        $this->numMaxFiles = $x;
        return $this;
    }
 
    public function getSizeMaxFilesByte() {
        return $this->sizeMaxFilesByte;
    }
 
    public function setSizeMaxFilesByte($x) {
        $this->sizeMaxFilesByte = $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
     * @return integer
     */
    public static function str2Bytes($strSize) {
        $strUnit = strtolower($strSize);
        $strUnit = preg_replace('/[^a-z]/', '', $strUnit);
 
        $value = intval(preg_replace('/[^0-9]/', '', $strSize));
 
        $Units = array('b' => 0, 'kb' => 1, 'mb' => 2, 'gb' => 3, 'tb' => 4);
        $Exponent = isset($Units[$strUnit]) ? $Units[$strUnit] : 0;
 
        return ($value * pow(1024, $Exponent));
    }
 
}