File.php 3.59 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
23
24
     */
    private $store = null;

    public function __construct($phpUnit = false) {
        $this->store = Store::getInstance('', $phpUnit);
25

26
27
28
//        $sessionName = $this->store->getVar(SYSTEM_SESSION_NAME, STORE_SYSTEM);
//        $this->sip = new Sip($sessionName);

29
30
31
32
33
34
35
36
37
        $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
38
39
    }

40
41
42
    /**
     * @throws UserFormException
     */
Carsten  Rose's avatar
Carsten Rose committed
43
44
    public function process() {

45
46
47
48
        $sipUpload = $this->store->getVar(SIP_SIP, STORE_SIP);
        if ($sipUpload === false) {
            throw new UserFormException('SIP invalid: ' . $sipUpload, ERROR_SIP_INVALID);
        }
49
        $statusUpload = $this->store->getVar($sipUpload, STORE_EXTRA, SANITIZE_ALLOW_ALL);
50
51
        if ($statusUpload === false) {
            $statusUpload = array();
Carsten  Rose's avatar
Carsten Rose committed
52
53
        }

54
55
56
57
58
59
60
61
62
63
64
65
        $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
66

67
68
69
70
71
72
    /**
     * @param string $keyStoreExtra
     * @throws CodeException
     * @throws UserFormException
     */
    private function doUpload($sipUpload, $statusUpload) {
73
74

        list($dummy, $newArr) = each($_FILES);
75
        $statusUpload = array_merge($statusUpload, $newArr);
76

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

81
82
83
84
85
        //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
86
        move_uploaded_file($newArr[FILES_TMP_NAME], $filenameCached);
Carsten  Rose's avatar
Carsten Rose committed
87

88
        $this->store->setVar($sipUpload, $statusUpload, STORE_EXTRA);
Carsten  Rose's avatar
Carsten Rose committed
89
    }
Carsten  Rose's avatar
Carsten Rose committed
90

91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    /**
     * @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
110
}