Passed
Pull Request — master (#58)
by Michael
03:14
created

FineimpuploadHandler   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 230
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 114
c 0
b 0
f 0
dl 0
loc 230
rs 10
wmc 24

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handleImageDB() 0 32 2
A getImageDim() 0 30 5
C storeUploadedFile() 0 90 16
A __construct() 0 5 1
1
<?php
2
3
namespace XoopsModules\Tdmdownloads\Common;
4
5
/**
6
 * SystemFineImUploadHandler class to work with ajaxfineupload.php endpoint
7
 * to facilitate uploads for the system image manager
8
 *
9
 * Do not use or reference this directly from your client-side code.
10
 * Instead, this should be required via the endpoint.php or endpoint-cors.php
11
 * file(s).
12
 *
13
 * @license   MIT License (MIT)
14
 * @copyright Copyright (c) 2015-present, Widen Enterprises, Inc.
15
 * @link      https://github.com/FineUploader/php-traditional-server
16
 *
17
 * The MIT License (MIT)
18
 *
19
 * Copyright (c) 2015-present, Widen Enterprises, Inc.
20
 *
21
 * Permission is hereby granted, free of charge, to any person obtaining a copy
22
 * of this software and associated documentation files (the "Software"), to deal
23
 * in the Software without restriction, including without limitation the rights
24
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25
 * copies of the Software, and to permit persons to whom the Software is
26
 * furnished to do so, subject to the following conditions:
27
 *
28
 * The above copyright notice and this permission notice shall be included in all
29
 * copies or substantial portions of the Software.
30
 *
31
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37
 * SOFTWARE.
38
 */
39
40
use XoopsModules\Tdmdownloads;
41
42
//class FineImpUploadHandler extends \SystemFineUploadHandler
43
44
/**
45
 * Class FineimpuploadHandler
46
 * @package XoopsModules\Tdmdownloads\Common
47
 */
48
class FineimpuploadHandler extends \SystemFineUploadHandler
49
{
50
    /**
51
     * @var int
52
     */
53
    private $permUseralbum = 0;
54
    /**
55
     * @var int
56
     */
57
    private $imageId = 0;
58
    /**
59
     * @var string
60
     */
61
    private $imageName = null;
62
    /**
63
     * @var string
64
     */
65
    private $imageNameLarge = null;
66
    /**
67
     * @var string
68
     */
69
    private $imageNicename = null;
70
    /**
71
     * @var string
72
     */
73
    private $imagePath = null;
74
    /**
75
     * @var string
76
     */
77
    private $imageNameOrig = null;
78
    /**
79
     * @var string
80
     */
81
    private $imageMimetype = null;
82
    /**
83
     * @var int
84
     */
85
    private $imageSize = 0;
86
    /**
87
     * @var int
88
     */
89
    private $imageWidth = 0;
90
    /**
91
     * @var int
92
     */
93
    private $imageHeight = 0;
94
    /**
95
     * @var string
96
     */
97
    private $pathUpload = null;
98
99
    /**
100
     * XoopsFineImUploadHandler constructor.
101
     * @param \stdClass $claims claims passed in JWT header
102
     */
103
    public function __construct(\stdClass $claims)
104
    {
105
        parent::__construct($claims);
106
        $this->allowedMimeTypes = ['image/gif', 'image/jpeg', 'image/png', 'application/zip'];
107
        $this->allowedExtensions = ['gif', 'jpeg', 'jpg', 'png', 'zip'];
108
    }
109
110
    /**
111
     * @param $target
112
     * @param $mimeType
113
     * @param $uid
114
     * @return array|bool
115
     */
116
    protected function storeUploadedFile($target, $mimeType, $uid)
117
    {
118
        $moduleDirName      = basename(dirname(dirname(__DIR__)));
119
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
120
        include_once XOOPS_ROOT_PATH .'/modules/'. $moduleDirName .'/header.php';
121
        $this->pathUpload = constant($moduleDirNameUpper . '_' . 'UPLOAD_IMAGE_PATH');
122
        $utility = new \XoopsModules\Tdmdownloads\Utility();
123
        /** @var \XoopsModules\Tdmdownloads\Helper $helper */
124
        $helper = \XoopsModules\Tdmdownloads\Helper::getInstance();
125
126
//        if ( WGGALLERY_PERM_SUBMITAPPR === $permissionsHandler->permGlobalSubmit()) {
127
//            $this->permUseralbum = WGGALLERY_STATE_APPROVAL_VAL;
128
//        } else {
129
//            $this->permUseralbum = WGGALLERY_STATE_ONLINE_VAL;
130
//        }
131
132
        $this->permUseralbum = 1; //TODO: handle an option, whether images should be online immediately or not
133
134
        $pathParts = pathinfo($this->getName());
135
136
        $this->imageName      = uniqid('img', true) . '.' . strtolower($pathParts['extension']);
137
        $this->imageNicename = str_replace(['_', '-'], ' ', $pathParts['filename']);
138
        $this->imageNameLarge = uniqid('imgl', true) . '.' . strtolower($pathParts['extension']);
139
        $this->imagePath      = $this->pathUpload . '/large/' . $this->imageNameLarge;
140
141
        if (false === move_uploaded_file($_FILES[$this->inputName]['tmp_name'], $this->imagePath)) {
142
            return false;
143
        }
144
145
        $this->imageNameOrig = $_FILES[$this->inputName]['name'];
146
        $this->imageMimetype = $_FILES[$this->inputName]['type'];
147
        $this->imageSize     = $_FILES[$this->inputName]['size'];
148
149
        $ret = $this->handleImageDB();
150
        if (false === $ret) {
151
            return [
152
                'error' => sprintf(_FAILSAVEIMG, $this->imageNicename)
153
            ];
154
        }
155
156
        // load watermark settings
157
        $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...
158
        $wmId = $albumObj->getVar('alb_wmid');
159
        $wmTargetM = false;
160
        $wmTargetL = false;
161
        if ( 0 < $wmId) {
162
            $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...
163
            $wmTarget = $watermarksObj->getVar('wm_target');
164
            if ( constant($moduleDirNameUpper . '_' . 'WATERMARK_TARGET_A') === $wmTarget || constant($moduleDirNameUpper . '_' . 'WATERMARK_TARGET_M') === $wmTarget) {$wmTargetM = true;}
165
            if ( constant($moduleDirNameUpper . '_' . 'WATERMARK_TARGET_A') === $wmTarget || constant($moduleDirNameUpper . '_' . 'WATERMARK_TARGET_L') === $wmTarget) {$wmTargetL = true;}
166
        }
167
168
        // create medium image
169
        // $ret = $this->resizeImage($this->pathUpload . '/medium/' . $this->imageName, $helper->getConfig('maxwidth_medium'), $helper->getConfig('maxheight_medium'));
170
        $ret = $utility->resizeImage($this->imagePath, $this->pathUpload . '/medium/' . $this->imageName, $helper->getConfig('maxwidth_medium'), $helper->getConfig('maxheight_medium'), $this->imageMimetype);
171
        if (false === $ret) {
0 ignored issues
show
introduced by
The condition false === $ret is always false.
Loading history...
172
            return ['error' => sprintf(constant($moduleDirNameUpper . '_' . 'FAILSAVEIMG_MEDIUM'), $this->imageNicename)];
173
        }
174
        if ('copy' === $ret) {
175
            copy($this->pathUpload . '/large/' . $this->imageNameLarge, $this->pathUpload . '/medium/' . $this->imageName);
176
        }
177
178
        // create thumb
179
        // $ret = $this->resizeImage($this->pathUpload . '/thumbs/' . $this->imageName, $helper->getConfig('maxwidth_thumbs'), $helper->getConfig('maxheight_thumbs'));
180
        $ret = $utility->resizeImage($this->imagePath, $this->pathUpload . '/thumbs/' . $this->imageName, $helper->getConfig('maxwidth_thumbs'), $helper->getConfig('maxheight_thumbs'), $this->imageMimetype);
181
        if (false === $ret) {
0 ignored issues
show
introduced by
The condition false === $ret is always false.
Loading history...
182
            return ['error' => sprintf(constant($moduleDirNameUpper . '_' . 'FAILSAVEIMG_THUMBS'), $this->imageNicename)];
183
        }
184
        if ('copy' === $ret) {
185
            copy($this->pathUpload . '/large/' . $this->imageNameLarge, $this->pathUpload . '/thumbs/' . $this->imageName);
186
        }
187
188
        // add watermark to large image
189
        if ( true === $wmTargetL) {
190
            $imgWm = $this->pathUpload . '/large/' . $this->imageNameLarge;
191
            $resWm = $watermarksHandler->watermarkImage( $wmId, $imgWm, $imgWm );
192
            if ( true !== $resWm) {
193
                return ['error' => sprintf(constant($moduleDirNameUpper . '_' . 'FAILSAVEWM_LARGE'), $this->imageNicename, $resWm)];
194
        }
195
    }
196
        // add watermark to medium image
197
        if ( true === $wmTargetM) {
198
            $imgWm = $this->pathUpload . '/medium/' . $this->imageName;
199
            $resWm = $watermarksHandler->watermarkImage( $wmId, $imgWm, $imgWm );
200
            if ( true !== $resWm) {
201
                return ['error' => sprintf(constant($moduleDirNameUpper . '_' . 'FAILSAVEWM_MEDIUM'), $this->imageNicename, $resWm)];
202
            }
203
        }
204
205
        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...
206
    }
207
208
    /**
209
     * @return bool
210
     */
211
    private function handleImageDB () {
212
        $moduleDirName      = basename(dirname(dirname(__DIR__)));
213
        include_once XOOPS_ROOT_PATH .'/modules/'. $moduleDirName .'/header.php';
214
        global $xoopsUser;
215
216
        $this->getImageDim();
217
218
        $helper = \XoopsModules\Tdmdownloads\Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
219
//      $imagesHandler = $helper->getHandler('Images');
220
        $imagesHandler = new \XoopsModules\Tdmdownloads\Common\ImagesHandler();
221
        $imagesObj = $imagesHandler->create();
222
        // Set Vars
223
        $imagesObj->setVar('img_title', $this->imageNicename);
224
        $imagesObj->setVar('img_desc', '');
225
        $imagesObj->setVar('img_name', $this->imageName);
226
        $imagesObj->setVar('img_namelarge', $this->imageNameLarge);
227
        $imagesObj->setVar('img_nameorig', $this->imageNameOrig);
228
        $imagesObj->setVar('img_mimetype', $this->imageMimetype);
229
        $imagesObj->setVar('img_size', $this->imageSize);
230
        $imagesObj->setVar('img_resx', $this->imageWidth);
231
        $imagesObj->setVar('img_resy', $this->imageHeight);
232
        $imagesObj->setVar('img_albid', $this->claims->cat);
233
        $imagesObj->setVar('img_state', $this->permUseralbum);
234
        $imagesObj->setVar('img_date', time());
235
        $imagesObj->setVar('img_submitter', $xoopsUser->id());
236
        $imagesObj->setVar('img_ip', $_SERVER['REMOTE_ADDR']);
237
        // Insert Data
238
        if ($imagesHandler->insert($imagesObj)) {
239
            $this->imageId = $imagesHandler->getInsertId();
240
            return true;
241
        }
242
        return false;
243
    }
244
245
    /**
246
     * @return bool|string
247
     */
248
    private function getImageDim () {
249
250
        switch ($this->imageMimetype) {
251
            case'image/png':
252
                $img = imagecreatefrompng($this->imagePath);
253
                break;
254
            case'image/jpeg':
255
                $img = imagecreatefromjpeg($this->imagePath);
256
                break;
257
            case'image/gif':
258
                $img = imagecreatefromgif($this->imagePath);
259
                break;
260
261
            case'application/zip':
262
                $this->imageWidth = 0;
263
                $this->imageHeight = 0;
264
                //                $img = imagecreatefromgif($this->imagePath);
265
                break;
266
267
            default:
268
                $this->imageWidth  = 0;
269
                $this->imageHeight = 0;
270
                return 'Unsupported format';
271
        }
272
        $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...
273
        $this->imageHeight = imagesy($img);
274
275
        imagedestroy($img);
276
277
        return true;
278
    }
279
}
280