UploadHandlerOld::canDo()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4888
c 0
b 0
f 0
cc 5
nc 4
nop 0
1
<?php
2
3
namespace yiicod\fileupload\components;
4
5
use Exception;
6
use UploadHandler as BaseUploadHandler;
7
use yii\helpers\ArrayHelper;
8
use yiicod\fileupload\components\base\AccessUploadInterface;
9
use yiicod\fileupload\components\base\EventsUploaderInterface;
10
use yiicod\fileupload\components\base\UploaderInterface;
11
12
/**
13
 * Class UploadHandler
14
 *
15
 * @package yiicod\fileupload\libs
16
 */
17
class UploadHandlerOld extends BaseUploadHandler
18
{
19
    /**
20
     * @var array
21
     */
22
    protected $clientOptions;
23
24
    /**
25
     * UploadHandler constructor.
26
     *
27
     * @param callable $userPostFunc
0 ignored issues
show
Bug introduced by
There is no parameter named $userPostFunc. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
28
     * @param array $options
29
     * @param bool $initialize
30
     * @param array $error_messages
31
     */
32
    public function __construct($clientOptions, $options = null, $initialize = true, $error_messages = null)
33
    {
34
        $this->clientOptions = $clientOptions;
35
36
        parent::__construct($options, $initialize, $error_messages);
37
    }
38
39
    /**
40
     * Post trigger.
41
     *
42
     * @param bool $print_response
43
     * @param bool $print_response
44
     *
45
     * @throws Exception
46
     */
47
    public function post($print_response = true)
48
    {
49
        $upload = isset($_FILES[$this->options['param_name']]) ?
50
            $_FILES[$this->options['param_name']] : null;
51
        if ($upload && is_array($upload['tmp_name'])) {
52
            foreach ($upload['tmp_name'] as $index => $value) {
53
                $this->fileTypeExecutable($upload['tmp_name'][$index]);
54
            }
55
        } else {
56
            $this->fileTypeExecutable($upload['tmp_name']);
57
        }
58
59
        $result = parent::post($print_response);
60
61
        if ($this->canDo()) {
62
            $can = true;
63
            list($userData, $uploader) = array_values($this->clientOptions);
64
            /** @var UploaderInterface $inst */
65
            $uploader = new $uploader();
66
            if (false === is_a($uploader, UploaderInterface::class)) {
67
                throw new Exception('Uploader class must be instanceof UploaderInterface', 500);
68
            }
69
            if (true === $can && true === is_a($uploader, AccessUploadInterface::class)) {
70
                $can = $uploader->can($userData);
71
            }
72
            if (true === $can && true === is_a($uploader, EventsUploaderInterface::class)) {
73
                $can = $uploader->beforeUploading($userData);
74
            }
75
            if (true === $can) {
76
                foreach ($result[$this->options['param_name']] as $i => $fileData) {
77
                    $result[$this->options['param_name']][$i] = $this->onFileUpload($uploader, $fileData, $userData);
78
                }
79
            }
80
            if (true === $can && true === is_a($uploader, EventsUploaderInterface::class)) {
81
                $uploader->afterUploading($userData, $result[$this->options['param_name']]);
82
            }
83
        }
84
85
        return $this->generate_response($result, $print_response);
86
    }
87
88
    /**
89
     * Callback on post end method.
90
     *
91
     * @param $fileData
92
     *
93
     * @return array
94
     *
95
     * @throws Exception
96
     */
97
    public function onFileUpload($uploader, $fileData, $userData)
98
    {
99
        $fileData = array_diff_key((array)$fileData, array_flip([
100
            'deleteUrl', 'deleteType', 'url',
101
        ]));
102
        $path = $this->get_upload_path();
103
        $filePath = (rtrim($path, '/') . '/' . $fileData['name']);
104
105
        if (false === isset($fileData['error'])) {
106
            $fileData['isSuccess'] = true;
107
108
            $result = $uploader->uploading($filePath, $userData, $fileData);
109
110
            if (is_array($result)) {
111
                $fileData = ArrayHelper::merge($fileData, $result);
112
            }
113
        } else {
114
            $fileData['isSuccess'] = false;
115
            $fileData = array_diff_key($fileData, array_flip([
116
                'url',
117
            ]));
118
        }
119
120
        return $fileData;
121
    }
122
123
    protected function canDo()
124
    {
125
        //Callback
126
        $content_range = $this->get_server_var('HTTP_CONTENT_RANGE') ?
127
            preg_split('/[^0-9]+/', $this->get_server_var('HTTP_CONTENT_RANGE')) : null;
128
129
        if (($this->get_server_var('HTTP_CONTENT_RANGE') && $content_range[3] - 1 == $content_range[2]) ||
130
            !$this->get_server_var('HTTP_CONTENT_RANGE')
131
        ) {
132
            return true;
133
        }
134
135
        return false;
136
    }
137
138
    /**
139
     * @param $file
140
     *
141
     * @throws Exception
142
     */
143
    protected function fileTypeExecutable($file)
144
    {
145
        if ($file && 'text/x-php' == mime_content_type($file)) {
146
            throw new Exception('File type was blocked', 403);
147
        }
148
    }
149
150
    /**
151
     * @param $file_path
152
     * @param $name
153
     * @param $size
154
     * @param $type
155
     * @param $error
156
     * @param $index
157
     * @param $content_range
158
     *
159
     * @return mixed|string
160
     */
161
    protected function trim_file_name($file_path, $name, $size, $type, $error, $index, $content_range)
162
    {
163
        $name = preg_replace('/[^A-Za-z0-9\-\.]/', '_', $name);
164
165
        return parent::trim_file_name($file_path, $name, $size, $type, $error, $index, $content_range);
166
    }
167
168
    /**
169
     * @param $name
170
     *
171
     * @return mixed
172
     */
173
    protected function upcount_name($name)
174
    {
175
        return preg_replace_callback(
176
            '/(?:(?:_([\d]+))?(\.[^.]+))?$/', [$this, 'upcount_name_callback'], $name, 1
177
        );
178
    }
179
180
    /**
181
     * @param $matches
182
     *
183
     * @return string
184
     */
185
    protected function upcount_name_callback($matches)
186
    {
187
        $index = isset($matches[1]) ? intval($matches[1]) + 1 : 1;
188
        $ext = isset($matches[2]) ? $matches[2] : '';
189
190
        return '_' . $index . $ext;
191
    }
192
}
193