Passed
Pull Request — master (#88)
by Michael
02:56
created

FineimpuploadHandler   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 313
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 116
dl 0
loc 313
rs 10
c 1
b 0
f 0
wmc 24

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handleImageDB() 0 57 2
A getImageDim() 0 31 5
C storeUploadedFile() 0 133 16
A __construct() 0 7 1
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Tdmdownloads\Common;
4
5
use XoopsModules\Tdmdownloads\{
6
    Helper,
7
    Utility
8
};
9
10
/**
11
 * SystemFineImUploadHandler class to work with ajaxfineupload.php endpoint
12
 * to facilitate uploads for the system image manager
13
 *
14
 * Do not use or reference this directly from your client-side code.
15
 * Instead, this should be required via the endpoint.php or endpoint-cors.php
16
 * file(s).
17
 *
18
 * @license   MIT License (MIT)
19
 * @copyright Copyright (c) 2015-present, Widen Enterprises, Inc.
20
 * @link      https://github.com/FineUploader/php-traditional-server
21
 *
22
 * The MIT License (MIT)
23
 *
24
 * Copyright (c) 2015-present, Widen Enterprises, Inc.
25
 *
26
 * Permission is hereby granted, free of charge, to any person obtaining a copy
27
 * of this software and associated documentation files (the "Software"), to deal
28
 * in the Software without restriction, including without limitation the rights
29
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
30
 * copies of the Software, and to permit persons to whom the Software is
31
 * furnished to do so, subject to the following conditions:
32
 *
33
 * The above copyright notice and this permission notice shall be included in all
34
 * copies or substantial portions of the Software.
35
 *
36
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
39
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
40
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
41
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
42
 * SOFTWARE.
43
 */
44
45
46
//class FineImpUploadHandler extends \SystemFineUploadHandler
47
48
49
/**
50
 * Class FineimpuploadHandler
51
 * @package XoopsModules\Tdmdownloads\Common
52
 */
53
class FineimpuploadHandler extends \SystemFineUploadHandler
54
{
55
    /**
56
     * @var int
57
     */
58
59
    private $permUseralbum = 0;
60
    /**
61
     * @var int
62
     */
63
64
    private $imageId = 0;
65
    /**
66
     * @var string
67
     */
68
69
    private $imageName = null;
70
    /**
71
     * @var string
72
     */
73
74
    private $imageNameLarge = null;
75
    /**
76
     * @var string
77
     */
78
79
    private $imageNicename = null;
80
    /**
81
     * @var string
82
     */
83
84
    private $imagePath = null;
85
    /**
86
     * @var string
87
     */
88
89
    private $imageNameOrig = null;
90
    /**
91
     * @var string
92
     */
93
94
    private $imageMimetype = null;
95
    /**
96
     * @var int
97
     */
98
99
    private $imageSize = 0;
100
    /**
101
     * @var int
102
     */
103
104
    private $imageWidth = 0;
105
    /**
106
     * @var int
107
     */
108
109
    private $imageHeight = 0;
110
    /**
111
     * @var string
112
     */
113
114
    private $pathUpload = null;
115
116
    /**
117
     * XoopsFineImUploadHandler constructor.
118
     * @param \stdClass $claims claims passed in JWT header
119
     */
120
    public function __construct(\stdClass $claims)
121
    {
122
        parent::__construct($claims);
123
124
        $this->allowedMimeTypes = ['image/gif', 'image/jpeg', 'image/png', 'application/zip'];
125
126
        $this->allowedExtensions = ['gif', 'jpeg', 'jpg', 'png', 'zip'];
127
    }
128
129
    /**
130
     * @param $target
131
     * @param $mimeType
132
     * @param $uid
133
     * @return array|bool
134
     */
135
    protected function storeUploadedFile($target, $mimeType, $uid)
136
    {
137
        $moduleDirName = \basename(\dirname(\dirname(__DIR__)));
138
139
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
140
141
        require_once XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/header.php';
142
143
        $this->pathUpload = \constant($moduleDirNameUpper . '_' . 'UPLOAD_IMAGE_PATH');
144
145
        $utility = new Utility();
146
147
        /** @var \XoopsModules\Tdmdownloads\Helper $helper */
148
149
        $helper = Helper::getInstance();
150
151
        //        if ( WGGALLERY_PERM_SUBMITAPPR === $permissionsHandler->permGlobalSubmit()) {
152
153
        //            $this->permUseralbum = WGGALLERY_STATE_APPROVAL_VAL;
154
155
        //        } else {
156
157
        //            $this->permUseralbum = WGGALLERY_STATE_ONLINE_VAL;
158
159
        //        }
160
161
        $this->permUseralbum = 1; //TODO: handle an option, whether images should be online immediately or not
162
163
        $pathParts = \pathinfo($this->getName());
0 ignored issues
show
Bug introduced by
It seems like $this->getName() can also be of type null; however, parameter $path of pathinfo() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

163
        $pathParts = \pathinfo(/** @scrutinizer ignore-type */ $this->getName());
Loading history...
164
165
        $this->imageName = \uniqid('img', true) . '.' . \mb_strtolower($pathParts['extension']);
166
167
        $this->imageNicename = \str_replace(['_', '-'], ' ', $pathParts['filename']);
168
169
        $this->imageNameLarge = \uniqid('imgl', true) . '.' . \mb_strtolower($pathParts['extension']);
170
171
        $this->imagePath = $this->pathUpload . '/large/' . $this->imageNameLarge;
172
173
        if (!\move_uploaded_file($_FILES[$this->inputName]['tmp_name'], $this->imagePath)) {
174
            return false;
175
        }
176
177
        $this->imageNameOrig = $_FILES[$this->inputName]['name'];
178
179
        $this->imageMimetype = $_FILES[$this->inputName]['type'];
180
181
        $this->imageSize = $_FILES[$this->inputName]['size'];
182
183
        $ret = $this->handleImageDB();
184
185
        if (!$ret) {
186
            return [
187
                'error' => \sprintf(\_FAILSAVEIMG, $this->imageNicename),
188
            ];
189
        }
190
191
        // load watermark settings
192
193
        $albumObj = $albumsHandler->get($this->claims->cat);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $albumsHandler seems to be never defined.
Loading history...
194
195
        $wmId = $albumObj->getVar('alb_wmid');
196
197
        $wmTargetM = false;
198
199
        $wmTargetL = false;
200
201
        if ($wmId > 0) {
202
            $watermarksObj = $watermarksHandler->get($wmId);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $watermarksHandler does not exist. Did you maybe mean $watermarksObj?
Loading history...
203
204
            $wmTarget = $watermarksObj->getVar('wm_target');
205
206
            if (\constant($moduleDirNameUpper . '_' . 'WATERMARK_TARGET_A') === $wmTarget || \constant($moduleDirNameUpper . '_' . 'WATERMARK_TARGET_M') === $wmTarget) {
207
                $wmTargetM = true;
208
            }
209
210
            if (\constant($moduleDirNameUpper . '_' . 'WATERMARK_TARGET_A') === $wmTarget || \constant($moduleDirNameUpper . '_' . 'WATERMARK_TARGET_L') === $wmTarget) {
211
                $wmTargetL = true;
212
            }
213
        }
214
215
        // create medium image
216
217
        // $ret = $this->resizeImage($this->pathUpload . '/medium/' . $this->imageName, $helper->getConfig('maxwidth_medium'), $helper->getConfig('maxheight_medium'));
218
219
        $ret = $utility->resizeImage($this->imagePath, $this->pathUpload . '/medium/' . $this->imageName, $helper->getConfig('maxwidth_medium'), $helper->getConfig('maxheight_medium'), $this->imageMimetype);
220
221
        if (false === $ret) {
0 ignored issues
show
introduced by
The condition false === $ret is always false.
Loading history...
222
            return ['error' => \sprintf(\constant($moduleDirNameUpper . '_' . 'FAILSAVEIMG_MEDIUM'), $this->imageNicename)];
223
        }
224
225
        if ('copy' === $ret) {
226
            \copy($this->pathUpload . '/large/' . $this->imageNameLarge, $this->pathUpload . '/medium/' . $this->imageName);
227
        }
228
229
        // create thumb
230
231
        // $ret = $this->resizeImage($this->pathUpload . '/thumbs/' . $this->imageName, $helper->getConfig('maxwidth_thumbs'), $helper->getConfig('maxheight_thumbs'));
232
233
        $ret = $utility->resizeImage($this->imagePath, $this->pathUpload . '/thumbs/' . $this->imageName, $helper->getConfig('maxwidth_thumbs'), $helper->getConfig('maxheight_thumbs'), $this->imageMimetype);
234
235
        if (false === $ret) {
0 ignored issues
show
introduced by
The condition false === $ret is always false.
Loading history...
236
            return ['error' => \sprintf(\constant($moduleDirNameUpper . '_' . 'FAILSAVEIMG_THUMBS'), $this->imageNicename)];
237
        }
238
239
        if ('copy' === $ret) {
240
            \copy($this->pathUpload . '/large/' . $this->imageNameLarge, $this->pathUpload . '/thumbs/' . $this->imageName);
241
        }
242
243
        // add watermark to large image
244
245
        if ($wmTargetL) {
246
            $imgWm = $this->pathUpload . '/large/' . $this->imageNameLarge;
247
248
            $resWm = $watermarksHandler->watermarkImage($wmId, $imgWm, $imgWm);
249
250
            if (true !== $resWm) {
251
                return ['error' => \sprintf(\constant($moduleDirNameUpper . '_' . 'FAILSAVEWM_LARGE'), $this->imageNicename, $resWm)];
252
            }
253
        }
254
255
        // add watermark to medium image
256
257
        if ($wmTargetM) {
258
            $imgWm = $this->pathUpload . '/medium/' . $this->imageName;
259
260
            $resWm = $watermarksHandler->watermarkImage($wmId, $imgWm, $imgWm);
261
262
            if (true !== $resWm) {
263
                return ['error' => \sprintf(\constant($moduleDirNameUpper . '_' . 'FAILSAVEWM_MEDIUM'), $this->imageNicename, $resWm)];
264
            }
265
        }
266
267
        return ['success' => true, 'uuid' => $uuid];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $uuid does not exist. Did you maybe mean $uid?
Loading history...
268
    }
269
270
    /**
271
     * @return bool
272
     */
273
    private function handleImageDB()
274
    {
275
        $moduleDirName = \basename(\dirname(\dirname(__DIR__)));
276
277
        require_once XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/header.php';
278
279
        global $xoopsUser;
280
281
        $this->getImageDim();
282
283
        $helper = Helper::getInstance();
284
285
        $imagesHandler = $helper->getHandler('Images');
286
287
//        $imagesHandler = new \XoopsModules\Tdmdownloads\Common\ImagesHandler();
288
289
        $imagesObj = $imagesHandler->create();
290
291
        // Set Vars
292
293
        $imagesObj->setVar('img_title', $this->imageNicename);
294
295
        $imagesObj->setVar('img_desc', '');
296
297
        $imagesObj->setVar('img_name', $this->imageName);
298
299
        $imagesObj->setVar('img_namelarge', $this->imageNameLarge);
300
301
        $imagesObj->setVar('img_nameorig', $this->imageNameOrig);
302
303
        $imagesObj->setVar('img_mimetype', $this->imageMimetype);
304
305
        $imagesObj->setVar('img_size', $this->imageSize);
306
307
        $imagesObj->setVar('img_resx', $this->imageWidth);
308
309
        $imagesObj->setVar('img_resy', $this->imageHeight);
310
311
        $imagesObj->setVar('img_albid', $this->claims->cat);
312
313
        $imagesObj->setVar('img_state', $this->permUseralbum);
314
315
        $imagesObj->setVar('img_date', \time());
316
317
        $imagesObj->setVar('img_submitter', $xoopsUser->id());
318
319
        $imagesObj->setVar('img_ip', $_SERVER['REMOTE_ADDR']);
320
321
        // Insert Data
322
323
        if ($imagesHandler->insert($imagesObj)) {
324
            $this->imageId = $imagesHandler->getInsertId();
0 ignored issues
show
Bug introduced by
The method getInsertId() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

324
            /** @scrutinizer ignore-call */ 
325
            $this->imageId = $imagesHandler->getInsertId();
Loading history...
325
326
            return true;
327
        }
328
329
        return false;
330
    }
331
332
    /**
333
     * @return bool|string
334
     */
335
    private function getImageDim()
336
    {
337
        switch ($this->imageMimetype) {
338
            case 'image/png':
339
                $img = \imagecreatefrompng($this->imagePath);
340
                break;
341
            case 'image/jpeg':
342
                $img = \imagecreatefromjpeg($this->imagePath);
343
                break;
344
            case 'image/gif':
345
                $img = \imagecreatefromgif($this->imagePath);
346
                break;
347
            case 'application/zip':
348
                $this->imageWidth  = 0;
349
                $this->imageHeight = 0;
350
                //                $img = imagecreatefromgif($this->imagePath);
351
                break;
352
            default:
353
                $this->imageWidth  = 0;
354
                $this->imageHeight = 0;
355
356
                return 'Unsupported format';
357
        }
358
359
        $this->imageWidth = \imagesx($img);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $img does not seem to be defined for all execution paths leading up to this point.
Loading history...
360
361
        $this->imageHeight = \imagesy($img);
362
363
        \imagedestroy($img);
364
365
        return true;
366
    }
367
}
368