File.php 2.53 KB
Newer Older
Carsten  Rose's avatar
Carsten Rose committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 4/25/16
 * Time: 10:39 PM
 */

namespace qfq;

require_once(__DIR__ . '/store/Store.php');
require_once(__DIR__ . '/Constants.php');

class File {

16
17
    private $uploadErrMsg = array();

Carsten  Rose's avatar
Carsten Rose committed
18
19
20
21
22
23
24
    /**
     * @var null|Store
     */
    private $store = null;

    public function __construct($phpUnit = false) {
        $this->store = Store::getInstance('', $phpUnit);
25
26
27
28
29
30
31
32
33
34

        $this->uploadErrMsg = [
            UPLOAD_ERR_INI_SIZE => "The uploaded file exceeds the upload_max_filesize directive in php.ini",
            UPLOAD_ERR_FORM_SIZE => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
            UPLOAD_ERR_PARTIAL => "The uploaded file was only partially uploaded",
            UPLOAD_ERR_NO_FILE => "No file was uploaded",
            UPLOAD_ERR_NO_TMP_DIR => "Missing a temporary folder",
            UPLOAD_ERR_CANT_WRITE => "Failed to write file to disk",
            UPLOAD_ERR_EXTENSION => "File upload stopped by extension"
        ];
Carsten  Rose's avatar
Carsten Rose committed
35
36
37
38
    }

    public function process() {

39
40
41
        $sipClient = $this->store->getVar(CLIENT_SIP, STORE_CLIENT, SANITIZE_ALLOW_ALNUMX);
        $sipStore = $this->store->getVar(SIP_SIP, STORE_SIP);
        if ($sipClient === false || $sipStore === false || $sipClient !== $sipStore) {
Carsten  Rose's avatar
Carsten Rose committed
42
43
44
45
46
            throw new UserFormException('SIP invalid', ERROR_SIP_INVALID);
        }

        $uploadFeName = $this->store->getVar(CLIENT_UPLOAD_FE_NAME, STORE_CLIENT, SANITIZE_ALLOW_ALNUMX);

47
48
49
50
51
52
        $keyStoreExtra = $sipStore . '-' . $uploadFeName;

        list($dummy, $newArr) = each($_FILES);
        $oldArr = $this->store->getVar($keyStoreExtra, STORE_EXTRA, SANITIZE_ALLOW_ALL);

        // Exist old cached upload? remove.
Carsten  Rose's avatar
Carsten Rose committed
53
54
55
        $filenameCached = Support::extendFilename($oldArr[FILES_TMP_NAME], UPLOAD_CACHED);

        if (isset($oldArr[FILES_TMP_NAME]) && $oldArr[FILES_TMP_NAME] != '' && file_exists($filenameCached)) {
56
            if (!unlink($oldArr[FILES_TMP_NAME] . '.cached')) {
Carsten  Rose's avatar
Carsten Rose committed
57
                throw new UserFormException("Failed to delete cached uploaded file: " . $filenameCached, ERROR_DELETE_TMP_UPLOAD);
58
59
60
61
62
            }
        }

        if ($newArr[FILES_ERROR] !== UPLOAD_ERR_OK) {
            throw new UserFormException($this->uploadErrMsg[$newArr[FILES_ERROR]], ERROR_UPLOAD);
Carsten  Rose's avatar
Carsten Rose committed
63
64
        }

Carsten  Rose's avatar
Carsten Rose committed
65
66
        $filenameCached = Support::extendFilename($newArr[FILES_TMP_NAME], UPLOAD_CACHED);
        move_uploaded_file($newArr[FILES_TMP_NAME], $filenameCached);
Carsten  Rose's avatar
Carsten Rose committed
67

68
        $this->store->setVar($keyStoreExtra, $newArr, STORE_EXTRA);
Carsten  Rose's avatar
Carsten Rose committed
69
    }
Carsten  Rose's avatar
Carsten Rose committed
70

Carsten  Rose's avatar
Carsten Rose committed
71
}