Classi per la configurazione upload
Cristiano Magro
2019-08-26 8699b028e87bbbb973c89f8d558038982b969294
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/**
 * Created by PhpStorm.
 * User: Cristiano Magro
 * Date: 16/01/2018
 * Time: 16:57
 * @package Plupload
 * @since v0.3.2
 */
 
/**
 * Class Upload_FileUp manipolazione dettagli file caricato dagestire.
 *
 * Si tratta di un wrapper per gestire il file appena caricato.
 *
 */
class Upload_FileUp
{
 
    const EMPTY_OBJ = "vuoto";
 
    private $request;
    private $beanFile;
 
    /* @var $logger WrapLog */
    private $logger;
    private $pathMap;
 
    public static function create($pathMap = null)
    {
        $instance = new self();
        $instance->request = new stdClass();
        $instance->beanFile = new stdClass();
        $instance->pathMap = $pathMap;
 
        return $instance;
    }
 
    public function setLogger($logger)
    {
        $this->logger = WrapLog::create($logger);
        return $this;
    }
 
    /**
     * Imposto i path di destinazione del file.
     *
     * Ricavo dalla mappa il path su cui memorizzare il file corrente, sulla base del parametro
     * passato nella Request. Se non trovo corrispondenza viene lanciata eccezione.
     * @param array $cfgPathMap elenco percorsi validi per la memorizzazione.
     * @return \Upload_FileUp fluent style
     * @throws Upload_Exc_Error
     */
    public function decodeUploadPath(array $cfgPathMap)
    {
        $this->pathMap = $cfgPathMap;
        $key = $this->getRequestValue('dirDestinazione', null);
 
        $path = $this->getMapValue($key);
        $pathDocumentRoot = filter_input(INPUT_SERVER, 'DOCUMENT_ROOT');
 
        //genero il percorso reale dove andrà salvato il file
        $this->request->pathForUpload =  $pathDocumentRoot. '/../' . $path;
        return $this;
    }
 
    /**
     * Ritorno il valore della Request preso dalla chiamata.
     *
     * Se non è definito ritorno il valore di default|null
     * @param string $nomeParam
     * @param string $default
     * @return mixed null|valore chiamata
     */
    public function getRequestValue($nomeParam, $default = null)
    {
        if (isset($this->request->{$nomeParam})) {
            return $this->request->{$nomeParam};
        }
        return $default;
    }
 
    /**
     * Recupero i parametri passati in GET nella chiamata.
     *
     * Costruisco una request con i parametri standard della chiamata: ope|dest|rand_value
     * @return \Upload_FileUp
     * @deprecated 0.3.2 usare readRequest();
     */
    public function getParametri()
    {
        $this->readRequest();
        return $this;
    }
 
    /**
     * Recupero i parametri passati in GET nella chiamata.
     *
     * Costruisco una request con i parametri standard della chiamata: ope|dest|rand_value
     * @return \Upload_FileUp
     * @since 0.3.2
     */
    public function readRequest()
    {
        $this->request->operazione = $_REQUEST['ope'];
        $this->request->dirDestinazione = $_REQUEST['dest'];
        $this->request->rand_value = $_REQUEST['rand_value'];
 
        return $this;
    }
 
    public function getRequestPathForUpload()
    {
        return $this->request->pathForUpload;
    }
 
    /**
     * Ricostruisco i dati del BEAN del file caricato sul sistema
     */
    public function getFileBean()
    {
        if (!empty($_FILES)) {
            $this->setTempNomeFile($_FILES['file']['tmp_name']);
            $this->setOrigNomeFile($_FILES['file']['name']);
            $this->setSizeFile($_FILES['file']['size']);
        } else {
            $this->setOrigNomeFile('');
        }
        return $this;
    }
 
    public function setTempNomeFile($x)
    {
        $this->beanFile->tempNomeFile = $x;
    }
 
    public function setOrigNomeFile($x)
    {
        $this->beanFile->origNomeFile = $x;
    }
 
    public function setSizeFile($x)
    {
        $this->beanFile->sizeFile = $x;
    }
 
    public function getTempNomeFile()
    {
        return $this->beanFile->tempNomeFile;
    }
 
    /**
     * Ritorno l'estensione del file caricato dall'utente
     * @return string Estenzione del file
     */
    public function getOrigNomeFileExt()
    {
        $path_info = pathinfo($this->getOrigNomeFile());
        return $path_info['extension'];
    }
 
    public function getOrigNomeFile()
    {
        return $this->beanFile->origNomeFile;
    }
 
    public function getSizeFile()
    {
        return $this->beanFile->sizeFile;
    }
 
    /**
     * Il tipo di operazione richiesta nella chiamata.
     * Per selezionare il case switch da attivare
     * @return string operazione da attivare
     */
    public function getOperazione()
    {
        return $this->request->operazione;
    }
 
    /**
     * ricostruisco il nome del file da registrare su disco.
     *
     * Vengono ripuliti i caratteri strani e le lettere accentate.
     * @return string
     */
    public function getNomeFileDisco()
    {
        $rand = $this->getRequestRandomValue();
        $nome = $this->getOrigNomeFile();
        return self::getNomeFile($rand, $nome);
    }
 
    public function getRequestRandomValue()
    {
        return $this->request->rand_value;
    }
 
    /**
     * ricostruisco il nome del file da registrare su disco.
     *
     * Vengono ripuliti i caratteri strani e le lettere accentate.
     * @param type $rand
     * @param string $nome
     * @return string
     */
    public static function getNomeFile($rand, $nome)
    {
        return $rand . "_" . FileUpload::clearNomeFile($nome);
    }
 
    /**
     * @deprecated 0.3.2 utilizzare toString
     */
    public function stampaTutto()
    {
        if ($this->logger instanceof WrapLog) {
            $this->logger->debug($this->request);
            $this->logger->debug($this->beanFile);
        } else {
            error_log('errorlog non attivo');
        }
    }
 
    public function __toString()
    {
        return $this->toString();
    }
 
    /**
     * Creo una stringa della struttura interna per la stampa sul logger.
     *
     * @return mixed|string struttura interna per il log
     */
    public function toString()
    {
        $text = "Richiesta: " . $this->convertToString($this->request) . PHP_EOL;
        $text .= "Bean: " . $this->convertToString($this->beanFile);
        return $text;
    }
 
    /**
     * Converto la struttura in una stringa per la stamapa.
     *
     * Se la struttura e' vuota ritorno "vuota"
     * @param stdClass $data struttura dati
     * @return string  conversione della struttura
     */
    private function convertToString(stdClass $data)
    {
        //effettuo il cast ad array per testare se la classe e' vuota
        $tmp = (array)$data;
        if (empty($tmp)) {
            return self::EMPTY_OBJ;
        } else {
            return print_r($data, true);
        }
    }
 
    /**
     * Decodifico mediante la mappa il percorso da utilizzare.
     * @param $chiave della Request per il percorso
     * @return mixed  path associato
     * @throws Upload_Exc_Error
     * @since 0.3.2
     */
    protected function getMapValue($chiave)
    {
        if (!isset($cfgPathMap[$chiave])) {
            throw new Upload_Exc_Error('Decodifica chiave percorso non definita');
        }
        return $cfgPathMap[$chiave];
    }
 
}