1
|
|
|
<?php declare(strict_types=1); |
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
|
|
|
|
41
|
|
|
//class FineImpUploadHandler extends \SystemFineUploadHandler |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Class FineimpuploadHandler |
46
|
|
|
* @package XoopsModules\Tdmdownloads\Common |
47
|
|
|
*/ |
48
|
|
|
class FineimpuploadHandler extends \SystemFineUploadHandler |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* @var int |
52
|
|
|
*/ |
53
|
|
|
|
54
|
|
|
private $permUseralbum = 0; |
55
|
|
|
/** |
56
|
|
|
* @var int |
57
|
|
|
*/ |
58
|
|
|
|
59
|
|
|
private $imageId = 0; |
60
|
|
|
/** |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
|
64
|
|
|
private $imageName = null; |
65
|
|
|
/** |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
|
69
|
|
|
private $imageNameLarge = null; |
70
|
|
|
/** |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
|
74
|
|
|
private $imageNicename = null; |
75
|
|
|
/** |
76
|
|
|
* @var string |
77
|
|
|
*/ |
78
|
|
|
|
79
|
|
|
private $imagePath = null; |
80
|
|
|
/** |
81
|
|
|
* @var string |
82
|
|
|
*/ |
83
|
|
|
|
84
|
|
|
private $imageNameOrig = null; |
85
|
|
|
/** |
86
|
|
|
* @var string |
87
|
|
|
*/ |
88
|
|
|
|
89
|
|
|
private $imageMimetype = null; |
90
|
|
|
/** |
91
|
|
|
* @var int |
92
|
|
|
*/ |
93
|
|
|
|
94
|
|
|
private $imageSize = 0; |
95
|
|
|
/** |
96
|
|
|
* @var int |
97
|
|
|
*/ |
98
|
|
|
|
99
|
|
|
private $imageWidth = 0; |
100
|
|
|
/** |
101
|
|
|
* @var int |
102
|
|
|
*/ |
103
|
|
|
|
104
|
|
|
private $imageHeight = 0; |
105
|
|
|
/** |
106
|
|
|
* @var string |
107
|
|
|
*/ |
108
|
|
|
|
109
|
|
|
private $pathUpload = null; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* XoopsFineImUploadHandler constructor. |
113
|
|
|
* @param \stdClass $claims claims passed in JWT header |
114
|
|
|
*/ |
115
|
|
|
public function __construct(\stdClass $claims) |
116
|
|
|
{ |
117
|
|
|
parent::__construct($claims); |
118
|
|
|
|
119
|
|
|
$this->allowedMimeTypes = ['image/gif', 'image/jpeg', 'image/png', 'application/zip']; |
120
|
|
|
|
121
|
|
|
$this->allowedExtensions = ['gif', 'jpeg', 'jpg', 'png', 'zip']; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param $target |
126
|
|
|
* @param $mimeType |
127
|
|
|
* @param $uid |
128
|
|
|
* @return array|bool |
129
|
|
|
*/ |
130
|
|
|
protected function storeUploadedFile($target, $mimeType, $uid) |
131
|
|
|
{ |
132
|
|
|
$moduleDirName = \basename(\dirname(\dirname(__DIR__))); |
133
|
|
|
|
134
|
|
|
$moduleDirNameUpper = \mb_strtoupper($moduleDirName); |
135
|
|
|
|
136
|
|
|
require_once XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/header.php'; |
137
|
|
|
|
138
|
|
|
$this->pathUpload = \constant($moduleDirNameUpper . '_' . 'UPLOAD_IMAGE_PATH'); |
139
|
|
|
|
140
|
|
|
$utility = new \XoopsModules\Tdmdownloads\Utility(); |
141
|
|
|
|
142
|
|
|
/** @var \XoopsModules\Tdmdownloads\Helper $helper */ |
143
|
|
|
|
144
|
|
|
$helper = \XoopsModules\Tdmdownloads\Helper::getInstance(); |
145
|
|
|
|
146
|
|
|
// if ( WGGALLERY_PERM_SUBMITAPPR === $permissionsHandler->permGlobalSubmit()) { |
147
|
|
|
|
148
|
|
|
// $this->permUseralbum = WGGALLERY_STATE_APPROVAL_VAL; |
149
|
|
|
|
150
|
|
|
// } else { |
151
|
|
|
|
152
|
|
|
// $this->permUseralbum = WGGALLERY_STATE_ONLINE_VAL; |
153
|
|
|
|
154
|
|
|
// } |
155
|
|
|
|
156
|
|
|
$this->permUseralbum = 1; //TODO: handle an option, whether images should be online immediately or not |
157
|
|
|
|
158
|
|
|
$pathParts = \pathinfo($this->getName()); |
|
|
|
|
159
|
|
|
|
160
|
|
|
$this->imageName = \uniqid('img', true) . '.' . \mb_strtolower($pathParts['extension']); |
161
|
|
|
|
162
|
|
|
$this->imageNicename = \str_replace(['_', '-'], ' ', $pathParts['filename']); |
163
|
|
|
|
164
|
|
|
$this->imageNameLarge = \uniqid('imgl', true) . '.' . \mb_strtolower($pathParts['extension']); |
165
|
|
|
|
166
|
|
|
$this->imagePath = $this->pathUpload . '/large/' . $this->imageNameLarge; |
167
|
|
|
|
168
|
|
|
if (!\move_uploaded_file($_FILES[$this->inputName]['tmp_name'], $this->imagePath)) { |
169
|
|
|
return false; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$this->imageNameOrig = $_FILES[$this->inputName]['name']; |
173
|
|
|
|
174
|
|
|
$this->imageMimetype = $_FILES[$this->inputName]['type']; |
175
|
|
|
|
176
|
|
|
$this->imageSize = $_FILES[$this->inputName]['size']; |
177
|
|
|
|
178
|
|
|
$ret = $this->handleImageDB(); |
179
|
|
|
|
180
|
|
|
if (!$ret) { |
181
|
|
|
return [ |
182
|
|
|
'error' => \sprintf(\_FAILSAVEIMG, $this->imageNicename), |
183
|
|
|
]; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
// load watermark settings |
187
|
|
|
|
188
|
|
|
$albumObj = $albumsHandler->get($this->claims->cat); |
|
|
|
|
189
|
|
|
|
190
|
|
|
$wmId = $albumObj->getVar('alb_wmid'); |
191
|
|
|
|
192
|
|
|
$wmTargetM = false; |
193
|
|
|
|
194
|
|
|
$wmTargetL = false; |
195
|
|
|
|
196
|
|
|
if ($wmId > 0) { |
197
|
|
|
$watermarksObj = $watermarksHandler->get($wmId); |
|
|
|
|
198
|
|
|
|
199
|
|
|
$wmTarget = $watermarksObj->getVar('wm_target'); |
200
|
|
|
|
201
|
|
|
if (\constant($moduleDirNameUpper . '_' . 'WATERMARK_TARGET_A') === $wmTarget || \constant($moduleDirNameUpper . '_' . 'WATERMARK_TARGET_M') === $wmTarget) { |
202
|
|
|
$wmTargetM = true; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
if (\constant($moduleDirNameUpper . '_' . 'WATERMARK_TARGET_A') === $wmTarget || \constant($moduleDirNameUpper . '_' . 'WATERMARK_TARGET_L') === $wmTarget) { |
206
|
|
|
$wmTargetL = true; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
// create medium image |
211
|
|
|
|
212
|
|
|
// $ret = $this->resizeImage($this->pathUpload . '/medium/' . $this->imageName, $helper->getConfig('maxwidth_medium'), $helper->getConfig('maxheight_medium')); |
213
|
|
|
|
214
|
|
|
$ret = $utility->resizeImage($this->imagePath, $this->pathUpload . '/medium/' . $this->imageName, $helper->getConfig('maxwidth_medium'), $helper->getConfig('maxheight_medium'), $this->imageMimetype); |
215
|
|
|
|
216
|
|
|
if (false === $ret) { |
|
|
|
|
217
|
|
|
return ['error' => \sprintf(\constant($moduleDirNameUpper . '_' . 'FAILSAVEIMG_MEDIUM'), $this->imageNicename)]; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
if ('copy' === $ret) { |
221
|
|
|
\copy($this->pathUpload . '/large/' . $this->imageNameLarge, $this->pathUpload . '/medium/' . $this->imageName); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
// create thumb |
225
|
|
|
|
226
|
|
|
// $ret = $this->resizeImage($this->pathUpload . '/thumbs/' . $this->imageName, $helper->getConfig('maxwidth_thumbs'), $helper->getConfig('maxheight_thumbs')); |
227
|
|
|
|
228
|
|
|
$ret = $utility->resizeImage($this->imagePath, $this->pathUpload . '/thumbs/' . $this->imageName, $helper->getConfig('maxwidth_thumbs'), $helper->getConfig('maxheight_thumbs'), $this->imageMimetype); |
229
|
|
|
|
230
|
|
|
if (false === $ret) { |
|
|
|
|
231
|
|
|
return ['error' => \sprintf(\constant($moduleDirNameUpper . '_' . 'FAILSAVEIMG_THUMBS'), $this->imageNicename)]; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
if ('copy' === $ret) { |
235
|
|
|
\copy($this->pathUpload . '/large/' . $this->imageNameLarge, $this->pathUpload . '/thumbs/' . $this->imageName); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
// add watermark to large image |
239
|
|
|
|
240
|
|
|
if ($wmTargetL) { |
241
|
|
|
$imgWm = $this->pathUpload . '/large/' . $this->imageNameLarge; |
242
|
|
|
|
243
|
|
|
$resWm = $watermarksHandler->watermarkImage($wmId, $imgWm, $imgWm); |
244
|
|
|
|
245
|
|
|
if (true !== $resWm) { |
246
|
|
|
return ['error' => \sprintf(\constant($moduleDirNameUpper . '_' . 'FAILSAVEWM_LARGE'), $this->imageNicename, $resWm)]; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
// add watermark to medium image |
251
|
|
|
|
252
|
|
|
if ($wmTargetM) { |
253
|
|
|
$imgWm = $this->pathUpload . '/medium/' . $this->imageName; |
254
|
|
|
|
255
|
|
|
$resWm = $watermarksHandler->watermarkImage($wmId, $imgWm, $imgWm); |
256
|
|
|
|
257
|
|
|
if (true !== $resWm) { |
258
|
|
|
return ['error' => \sprintf(\constant($moduleDirNameUpper . '_' . 'FAILSAVEWM_MEDIUM'), $this->imageNicename, $resWm)]; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
return ['success' => true, 'uuid' => $uuid]; |
|
|
|
|
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @return bool |
267
|
|
|
*/ |
268
|
|
|
private function handleImageDB() |
269
|
|
|
{ |
270
|
|
|
$moduleDirName = \basename(\dirname(\dirname(__DIR__))); |
271
|
|
|
|
272
|
|
|
require_once XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/header.php'; |
273
|
|
|
|
274
|
|
|
global $xoopsUser; |
275
|
|
|
|
276
|
|
|
$this->getImageDim(); |
277
|
|
|
|
278
|
|
|
$helper = \XoopsModules\Tdmdownloads\Helper::getInstance(); |
279
|
|
|
|
280
|
|
|
$imagesHandler = $helper->getHandler('Images'); |
281
|
|
|
|
282
|
|
|
// $imagesHandler = new \XoopsModules\Tdmdownloads\Common\ImagesHandler(); |
283
|
|
|
|
284
|
|
|
$imagesObj = $imagesHandler->create(); |
285
|
|
|
|
286
|
|
|
// Set Vars |
287
|
|
|
|
288
|
|
|
$imagesObj->setVar('img_title', $this->imageNicename); |
289
|
|
|
|
290
|
|
|
$imagesObj->setVar('img_desc', ''); |
291
|
|
|
|
292
|
|
|
$imagesObj->setVar('img_name', $this->imageName); |
293
|
|
|
|
294
|
|
|
$imagesObj->setVar('img_namelarge', $this->imageNameLarge); |
295
|
|
|
|
296
|
|
|
$imagesObj->setVar('img_nameorig', $this->imageNameOrig); |
297
|
|
|
|
298
|
|
|
$imagesObj->setVar('img_mimetype', $this->imageMimetype); |
299
|
|
|
|
300
|
|
|
$imagesObj->setVar('img_size', $this->imageSize); |
301
|
|
|
|
302
|
|
|
$imagesObj->setVar('img_resx', $this->imageWidth); |
303
|
|
|
|
304
|
|
|
$imagesObj->setVar('img_resy', $this->imageHeight); |
305
|
|
|
|
306
|
|
|
$imagesObj->setVar('img_albid', $this->claims->cat); |
307
|
|
|
|
308
|
|
|
$imagesObj->setVar('img_state', $this->permUseralbum); |
309
|
|
|
|
310
|
|
|
$imagesObj->setVar('img_date', \time()); |
311
|
|
|
|
312
|
|
|
$imagesObj->setVar('img_submitter', $xoopsUser->id()); |
313
|
|
|
|
314
|
|
|
$imagesObj->setVar('img_ip', $_SERVER['REMOTE_ADDR']); |
315
|
|
|
|
316
|
|
|
// Insert Data |
317
|
|
|
|
318
|
|
|
if ($imagesHandler->insert($imagesObj)) { |
319
|
|
|
$this->imageId = $imagesHandler->getInsertId(); |
|
|
|
|
320
|
|
|
|
321
|
|
|
return true; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
return false; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* @return bool|string |
329
|
|
|
*/ |
330
|
|
|
private function getImageDim() |
331
|
|
|
{ |
332
|
|
|
switch ($this->imageMimetype) { |
333
|
|
|
case 'image/png': |
334
|
|
|
$img = \imagecreatefrompng($this->imagePath); |
335
|
|
|
break; |
336
|
|
|
case 'image/jpeg': |
337
|
|
|
$img = \imagecreatefromjpeg($this->imagePath); |
338
|
|
|
break; |
339
|
|
|
case 'image/gif': |
340
|
|
|
$img = \imagecreatefromgif($this->imagePath); |
341
|
|
|
break; |
342
|
|
|
case 'application/zip': |
343
|
|
|
$this->imageWidth = 0; |
344
|
|
|
$this->imageHeight = 0; |
345
|
|
|
// $img = imagecreatefromgif($this->imagePath); |
346
|
|
|
break; |
347
|
|
|
default: |
348
|
|
|
$this->imageWidth = 0; |
349
|
|
|
$this->imageHeight = 0; |
350
|
|
|
|
351
|
|
|
return 'Unsupported format'; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
$this->imageWidth = \imagesx($img); |
|
|
|
|
355
|
|
|
|
356
|
|
|
$this->imageHeight = \imagesy($img); |
357
|
|
|
|
358
|
|
|
\imagedestroy($img); |
359
|
|
|
|
360
|
|
|
return true; |
361
|
|
|
} |
362
|
|
|
} |
363
|
|
|
|