File.php 2.42 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
53
54
55
56
57
58
59
60
        $keyStoreExtra = $sipStore . '-' . $uploadFeName;

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

        // Exist old cached upload? remove.
        if (isset($oldArr[FILES_TMP_NAME]) && $oldArr[FILES_TMP_NAME] != '' && file_exists($oldArr[FILES_TMP_NAME] . '.cached')) {
            if (!unlink($oldArr[FILES_TMP_NAME] . '.cached')) {
                throw new UserFormException("Failed to delete previous temporary uploaded file: " . $oldArr[FILES_TMP_NAME] . '.cached', ERROR_DELETE_TMP_UPLOAD);
            }
        }

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

63
        move_uploaded_file($newArr[FILES_TMP_NAME], $newArr[FILES_TMP_NAME] . '.cached');
Carsten  Rose's avatar
Carsten Rose committed
64

65
        $this->store->setVar($keyStoreExtra, $newArr, STORE_EXTRA);
Carsten  Rose's avatar
Carsten Rose committed
66
67
    }
}