File.php 3.63 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
     * @var Store
Carsten  Rose's avatar
Carsten Rose committed
20
21
22
     */
    private $store = null;

23
24
25
26
27
28
29
30
    /**
     * @var Session
     */
    private $session = null;

    /**
     * @param bool|false $phpUnit
     */
Carsten  Rose's avatar
Carsten Rose committed
31
    public function __construct($phpUnit = false) {
32
        $this->session = Session::getInstance($phpUnit);
33

34
        $this->store = Store::getInstance('', $phpUnit);
35

36
37
38
39
40
41
42
43
44
        $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
45
46
    }

47
48
49
    /**
     * @throws UserFormException
     */
Carsten  Rose's avatar
Carsten Rose committed
50
51
    public function process() {

52
53
54
55
        $sipUpload = $this->store->getVar(SIP_SIP, STORE_SIP);
        if ($sipUpload === false) {
            throw new UserFormException('SIP invalid: ' . $sipUpload, ERROR_SIP_INVALID);
        }
56
        $statusUpload = $this->store->getVar($sipUpload, STORE_EXTRA, SANITIZE_ALLOW_ALL);
57
58
        if ($statusUpload === false) {
            $statusUpload = array();
Carsten  Rose's avatar
Carsten Rose committed
59
60
        }

61
62
63
64
65
66
67
68
69
70
71
72
        $action = $this->store->getVar(FILE_ACTION, STORE_CLIENT, SANITIZE_ALLOW_ALNUMX);
        switch ($action) {
            case FILE_ACTION_UPLOAD:
                $this->doUpload($sipUpload, $statusUpload);
                break;
            case FILE_ACTION_DELETE:
                $this->doDelete($sipUpload, $statusUpload);
                break;
            default:
                throw new UserFormException("Unknown FILE_ACTION: $action", ERROR_UNKNOWN_ACTION);
        }
    }
Carsten  Rose's avatar
Carsten Rose committed
73

74
75
76
77
78
79
    /**
     * @param string $keyStoreExtra
     * @throws CodeException
     * @throws UserFormException
     */
    private function doUpload($sipUpload, $statusUpload) {
80
81

        list($dummy, $newArr) = each($_FILES);
82
        $statusUpload = array_merge($statusUpload, $newArr);
83

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

88
89
90
91
92
        //TODO: do necessary checks with uploaded file HERE!!!


        // rename uploaded file: ?.cached
        $filenameCached = Support::extendFilename($statusUpload[FILES_TMP_NAME], UPLOAD_CACHED);
Carsten  Rose's avatar
Carsten Rose committed
93
        move_uploaded_file($newArr[FILES_TMP_NAME], $filenameCached);
Carsten  Rose's avatar
Carsten Rose committed
94

95
        $this->store->setVar($sipUpload, $statusUpload, STORE_EXTRA);
Carsten  Rose's avatar
Carsten Rose committed
96
    }
Carsten  Rose's avatar
Carsten Rose committed
97

98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    /**
     * @param string $keyStoreExtra
     * @throws CodeException
     * @throws UserFormException
     */
    private function doDelete($sipUpload, $statusUpload) {

        if (isset($statusUpload[FILES_TMP_NAME]) && $statusUpload[FILES_TMP_NAME] != '') {
            $file = Support::extendFilename($statusUpload[FILES_TMP_NAME], UPLOAD_CACHED);
            if (file_exists($file)) {
                if (!unlink($file)) {
                    throw new UserFormException('unlink file: ' . $file, ERROR_IO_UNLINK);
                }
            }
            $statusUpload[FILES_TMP_NAME] = '';
        }
        $statusUpload[FILES_FLAG_DELETE] = '1';
        $this->store->setVar($sipUpload, $statusUpload, STORE_EXTRA);
    }
Carsten  Rose's avatar
Carsten Rose committed
117
}