Passed
Push — master ( 7d4f9d...f5c894 )
by Goffy
12:14
created

getFormUploadImage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 21
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 31
rs 9.584
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
/**
16
 * wg Image Editor for xoops
17
 *
18
 * @copyright      xoops
19
 * @license        GPL 2.0 or later
20
 * @package        general
21
 * @author         Wedega - Email:<[email protected]> - Website:<https://wedega.com>
22
 * @version        1.1 image_editor.php XOOPS Project (www.xoops.org) $
23
 */
24
25
use Xmf\Request;
26
use XoopsModules\Wgevents;
27
use XoopsModules\Wgevents\{
28
    Constants,
29
    Utility,
30
    Common\Resizer
31
};
32
33
include __DIR__ . '/header.php';
34
$GLOBALS['xoopsOption']['template_main'] = 'wgevents_image_editor.tpl';
35
36
require_once \XOOPS_ROOT_PATH . '/header.php';
37
38
$utility = new \XoopsModules\Wgevents\Utility();
39
40
$op         = Request::getString('op', 'list');
41
$sourceId   = Request::getInt('id', Request::getInt('imageIdCrop'));
42
$origin     = Request::getString('imageOrigin');
43
$start      = Request::getInt('start');
44
$limit      = Request::getInt('limit', $helper->getConfig('userpager'));
45
$img_resize = Request::getInt('img_resize');
46
47
$uid = $xoopsUser instanceof \XoopsUser ? $xoopsUser->id() : 0;
48
49
// get all objects/classes/vars needed for image editor
50
$imageClass = 0; //identifier for different image classes; currently not used
51
$imgCurrent = [];
52
53
//if ($imageClass === Constants::IMAGECLASS_MEMBER) {
54
    $imageId      = $sourceId;
55
    $imageHandler = $eventHandler;
56
    $imageObj     = $eventHandler->get($sourceId);
57
    $imageOrigin  = 'id';
58
    $imgName  = \mb_substr(\str_replace(' ', '', $imageObj->getVar('name')), 0, 20) . '.jpg';
59
    $imageDir = '/uploads/wgevents/events/logos/' . $uid . '/';
60
    $imgPath  = \XOOPS_ROOT_PATH . $imageDir;
61
    $imgUrl   = \XOOPS_URL . $imageDir;
62
    $imgFinal = $imgPath . $imgName;
63
    $imgTemp  = \WGEVENTS_UPLOAD_PATH . '/temp/' . $imgName;
64
    $redir    = 'event.php?op=list&amp;start=' . $start . '&amp;limit=' . $limit;
65
    $nameObj  = 'title';
66
    $fieldObj = 'logo';
67
    $submObj  = 'submitter';
68
/*
69
} else {
70
    // only used if image editor is used for different source
71
    // TODO: maybe for categories
72
}
73
*/
74
75
$imgCurrent['img_name'] = $imageObj->getVar($fieldObj);
76
$imgCurrent['src'] = $imgUrl . $imageObj->getVar($fieldObj);
77
$imgCurrent['origin'] = $imageClass;
78
$images = [];
79
80
$image_array = \XoopsLists::getImgListAsArray($imgPath);
81
$i = 0;
82
foreach ($image_array as $image_img) {
83
    if ('blank.gif' !== $image_img) {
84
        $i++;
85
        $images[$i]['id'] = 'imageSelect'.$i;
86
        $images[$i]['name'] = $image_img;
87
        $images[$i]['title'] = $image_img;
88
        $images[$i]['origin'] = 0; //identifier for different image classes; currently not used
89
        if ($imgCurrent['img_name'] === $image_img) {
90
            $images[$i]['selected'] = 1;
91
        }
92
        $images[$i]['src'] = $imgUrl . $image_img;
93
    }
94
}
95
// var_dump($images);
96
$GLOBALS['xoopsTpl']->assign('images', $images);
97
unset($images);
98
// end: get all objects/classes/vars needed for image editor
99
100
// Define Stylesheet
101
$GLOBALS['xoTheme']->addStylesheet(\WGEVENTS_URL . '/assets/css/style.css');
102
$GLOBALS['xoTheme']->addStylesheet(\WGEVENTS_URL . '/assets/css/cropper/imageeditor.css');
103
104
// add scripts
105
//$GLOBALS['xoTheme']->addScript(\XOOPS_URL . '/modules/wgevents/assets/js/admin.js');
106
107
// assign vars
108
$GLOBALS['xoopsTpl']->assign('wgevents_url', \WGEVENTS_URL);
109
$GLOBALS['xoopsTpl']->assign('wgevents_icon_url_16', \WGEVENTS_ICONS_URL . '/16');
110
$GLOBALS['xoopsTpl']->assign('wgevents_icon_url_32', \WGEVENTS_ICONS_URL . '/32');
111
$GLOBALS['xoopsTpl']->assign('wgevents_upload_url', \WGEVENTS_UPLOAD_URL);
112
$GLOBALS['xoopsTpl']->assign('wgevents_upload_path', \WGEVENTS_UPLOAD_PATH);
113
$GLOBALS['xoopsTpl']->assign('wgevents_image_editor', \WGEVENTS_URL);
114
$GLOBALS['xoopsTpl']->assign('wgevents_upload_image_url', $imgUrl);
115
$GLOBALS['xoopsTpl']->assign('gridtarget', $imgName);
116
$GLOBALS['xoopsTpl']->assign('imgCurrent', $imgCurrent);
117
$GLOBALS['xoopsTpl']->assign('imageId', $imageId);
118
$GLOBALS['xoopsTpl']->assign('imageOrigin', $imageOrigin);
119
120
// Breadcrumbs
121
$xoBreadcrumbs[] = ['title' => \_MA_WGEVENTS_INDEX, 'link' => 'index.php'];
122
$xoBreadcrumbs[] = ['title' => $imageObj->getVar('name'), 'link' => 'event.php?op=show&amp;id=' . $imageObj->getVar('id')];
123
$xoBreadcrumbs[] = ['title' => \_MA_WGEVENTS_IMG_EDITOR];
124
125
// get config for images
126
$maxwidth  = $helper->getConfig('maxwidth_image');
127
$maxheight = $helper->getConfig('maxheight_image');
128
$maxsize   = $helper->getConfig('maxsize_image');
129
$mimetypes = $helper->getConfig('mimetypes_image');
130
131
switch ($op) {
132
133
    case 'creategrid':
134
        // create an image grid based on given sources
135
        $type   = Request::getInt('type', 4);
136
        $src[1] = Request::getString('src1');
137
        $src[2] = Request::getString('src2');
138
        $src[3] = Request::getString('src3');
139
        $src[4] = Request::getString('src4');
140
        $src[5] = Request::getString('src5');
141
        $src[6] = Request::getString('src6');
142
        $target = Request::getString('target');
143
144
        $images = [];
145
        for ($i = 1; $i <= 6; $i++) {
146
            if ('' !== $src[$i]) {
147
                $file       = \str_replace(\XOOPS_URL, \XOOPS_ROOT_PATH, $src[$i]);
148
                $images[$i] = ['file' => $file, 'mimetype' => \mime_content_type($file)];
149
            }
150
        }
151
152
        // create basic image
153
        $tmp   = \imagecreatetruecolor($maxwidth, $maxheight);
154
        $imgBg = imagecolorallocate($tmp, 0, 0, 0);
155
        imagefilledrectangle($tmp, 0, 0, $maxwidth, $maxheight, $imgBg);
156
157
        $final = XOOPS_UPLOAD_PATH . '/wgevents/temp/' . $target;
158
        \unlink($final);
159
        \imagejpeg($tmp, $final);
160
        \imagedestroy($tmp);
161
162
        $imgTemp = XOOPS_UPLOAD_PATH . '/wgevents/temp/' . $uid . 'imgTemp';
163
164
        $imgHandler = new Resizer();
165
        if (4 === $type) {
166
            for ($i = 1; $i <= 4; $i++) {
167
                \unlink($imgTemp . $i . '.jpg');
168
                $imgHandler->sourceFile    = $images[$i]['file'];
169
                $imgHandler->endFile       = $imgTemp . $i . '.jpg';
170
                $imgHandler->imageMimetype = $images[$i]['mimetype'];
171
                $imgHandler->maxWidth      = (int)\round($maxwidth / 2 - 1);
172
                $imgHandler->maxHeight     = (int)\round($maxheight / 2 - 1);
173
                $imgHandler->jpgQuality    = 90;
174
                $imgHandler->resizeAndCrop();
175
            }
176
            $imgHandler->mergeType = 4;
177
            $imgHandler->endFile   = $final;
178
            $imgHandler->maxWidth  = $maxwidth;
179
            $imgHandler->maxHeight = $maxheight;
180
            for ($i = 1; $i <= 4; $i++) {
181
                $imgHandler->sourceFile = $imgTemp . $i . '.jpg';
182
                $imgHandler->mergePos   = $i;
183
                $imgHandler->mergeImage();
184
                \unlink($imgTemp . $i . '.jpg');
185
            }
186
        }
187
        if (6 === $type) {
188
            for ($i = 1; $i <= 6; $i++) {
189
                $imgHandler->sourceFile    = $images[$i]['file'];
190
                $imgHandler->endFile       = $imgTemp . $i . '.jpg';
191
                $imgHandler->imageMimetype = $images[$i]['mimetype'];
192
                $imgHandler->maxWidth      = (int)\round($maxwidth / 3 - 1);
193
                $imgHandler->maxHeight     = (int)\round($maxheight / 2 - 1);
194
                $imgHandler->resizeAndCrop();
195
            }
196
            $imgHandler->mergeType = 6;
197
            $imgHandler->endFile   = $final;
198
            $imgHandler->maxWidth  = $maxwidth;
199
            $imgHandler->maxHeight = $maxheight;
200
            for ($i = 1; $i <= 6; $i++) {
201
                $imgHandler->sourceFile = $imgTemp . $i . '.jpg';
202
                $imgHandler->mergePos   = $i;
203
                $imgHandler->mergeImage();
204
                \unlink($imgTemp . $i . '.jpg');
205
            }
206
        }
207
        break;
208
209
    case 'cropimage':
210
        // save base64_image and resize to maxwidth/maxheight
211
        $base64_image_content = Request::getString('croppedImage');
212
        if (\preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
213
            $type = $result[2];
214
            \file_put_contents($imgTemp, base64_decode(\str_replace($result[1], '', $base64_image_content), true));
215
        }
216
217
        $imgHandler                = new Resizer();
218
        $imgHandler->sourceFile    = $imgTemp;
219
        $imgHandler->endFile       = $imgTemp;
220
        $imgHandler->imageMimetype = 'image/jpeg';
221
        $imgHandler->maxWidth      = $maxwidth;
222
        $imgHandler->maxHeight     = $maxheight;
223
        $ret                       = $imgHandler->resizeImage();
224
225
        //\unlink($imgFinal);
226
        break;
227
    case 'saveImageSelected':
228
        // save image selected from list of available images in upload folder
229
        // Set Vars
230
        $image_id = Request::getString('image_id');
231
        // remove '_image' from id
232
        $image_id = \substr($image_id, 0, -6);
233
        $imageObj->setVar($fieldObj, $image_id);
234
        $imageObj->setVar($submObj, $uid);
235
        // Insert Data
236
        if ($imageHandler->insert($imageObj)) {
237
            \redirect_header($redir, 2, _MA_WGEVENTS_FORM_OK);
238
        }
239
        $GLOBALS['xoopsTpl']->assign('error', $imageObj->getHtmlErrors());
240
        break;
241
242
    case 'saveGrid':
243
        // save before created grid image
244
        $imgTempGrid = Request::getString('gridImgFinal');
245
        $ret = \rename($imgTempGrid, $imgFinal);
246
        // Set Vars
247
        $imageObj->setVar($fieldObj, $imgName);
248
        $imageObj->setVar($submObj, $uid);
249
        // Insert Data
250
        if ($imageHandler->insert($imageObj)) {
251
            \redirect_header($redir, 2, _MA_WGEVENTS_FORM_OK);
252
        }
253
        $GLOBALS['xoopsTpl']->assign('error', $imageObj->getHtmlErrors());
254
255
        break;
256
    case 'saveCrop':
257
        // save before created cropped image
258
        \unlink($imgFinal);
259
        $ret = \rename($imgTemp, $imgFinal);
260
        // Set Vars
261
        $imageObj->setVar($fieldObj, $imgName);
262
        $imageObj->setVar($submObj, $uid);
263
        // Insert Data
264
        if ($imageHandler->insert($imageObj, true)) {
265
            \redirect_header($redir, 2, _MA_WGEVENTS_FORM_OK);
266
        }
267
        $GLOBALS['xoopsTpl']->assign('error', $imageObj->getHtmlErrors());
268
269
        break;
270
    case 'uploadImage':
271
        // Security Check
272
        if (!$GLOBALS['xoopsSecurity']->check()) {
273
            \redirect_header($redir, 3, \implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
274
        }
275
        // Set Vars
276
        require_once \XOOPS_ROOT_PATH . '/class/uploader.php';
277
        $fileName       = $_FILES['attachedfile']['name'];
278
        $imageMimetype  = $_FILES['attachedfile']['type'];
279
        $uploaderErrors = '';
280
        $uploader       = new \XoopsMediaUploader($imgPath, $mimetypes, $maxsize, null, null);
281
        if ($uploader->fetchMedia($_POST['xoops_upload_file'][0])) {
282
            $extension = \preg_replace('/^.+\.([^.]+)$/sU', '', $fileName);
283
            $imgName   .= '.' . $extension;
284
            $uploader->setPrefix($imgName);
285
            $uploader->fetchMedia($_POST['xoops_upload_file'][0]);
286
            if (!$uploader->upload()) {
287
                $uploaderErrors = $uploader->getErrors();
288
            } else {
289
                $savedFilename = $uploader->getSavedFileName();
290
                $imageObj->setVar($fieldObj, $savedFilename);
291
                // resize image
292
                if (1 == $img_resize) {
293
                    $imgHandler                = new Resizer();
294
                    $imgHandler->sourceFile    = $imgPath . $savedFilename;
295
                    $imgHandler->endFile       = $imgPath . $savedFilename;
296
                    $imgHandler->imageMimetype = $imageMimetype;
297
                    $imgHandler->maxWidth      = $maxwidth;
298
                    $imgHandler->maxHeight     = $maxheight;
299
                    $result                    = $imgHandler->resizeImage();
300
                }
301
302
                $imageObj->setVar($fieldObj, $savedFilename);
303
                $imageObj->setVar($submObj, $uid);
304
            }
305
        } else {
306
            if ($fileName > '') {
307
                $uploaderErrors = $uploader->getErrors();
308
            }
309
        }
310
        if ('' !== $uploaderErrors) {
311
            \redirect_header($redir, 5, $uploaderErrors);
312
        }
313
        // Insert Data
314
        if ($imageHandler->insert($imageObj)) {
315
            \redirect_header($redir, 2, _MA_WGEVENTS_FORM_OK);
316
        }
317
        // Get Form
318
        $GLOBALS['xoopsTpl']->assign('error', $imageObj->getHtmlErrors());
319
        $form = $imageObj->getFormUploadImage($imageOrigin, $imageId);
320
        $GLOBALS['xoopsTpl']->assign('form', $form->render());
321
        break;
322
323
    case 'imghandler':
324
    default:
325
        $GLOBALS['xoTheme']->addStylesheet(\WGEVENTS_URL . '/assets/css/cropper/cropper.min.css');
326
        $GLOBALS['xoTheme']->addScript(\WGEVENTS_URL . '/assets/js/cropper/cropper.min.js');
327
        $GLOBALS['xoTheme']->addScript(\WGEVENTS_URL . '/assets/js/cropper/cropper-main.js');
328
329
        $GLOBALS['xoopsTpl']->assign('nbModals', [1, 2, 3, 4, 5, 6]);
330
331
        // get form for upload album image
332
        $currImage   = $imageObj->getVar($fieldObj);
333
        if ('' == $currImage) {
334
            $currImage = 'blank.gif';
335
        }
336
        $image_path = $imgPath . $currImage;
337
        $width = 0;
338
        $height= 0;
339
        if (\file_exists($image_path)) {
340
            // get size of current album image
341
            list($width, $height, $type, $attr) = \getimagesize($image_path);
342
        }
343
        $GLOBALS['xoopsTpl']->assign('image_path', $image_path);
344
        $GLOBALS['xoopsTpl']->assign('albimage_width', $width);
345
        $GLOBALS['xoopsTpl']->assign('albimage_height', $height);
346
347
        $form = getFormUploadImage($imageOrigin, $imageId);
348
        $GLOBALS['xoopsTpl']->assign('form_uploadimage', $form->render());
349
350
        $GLOBALS['xoopsTpl']->assign('btn_style', 'btn-default');
351
352
        break;
353
}
354
355
$GLOBALS['xoopsTpl']->assign('panel_type', $helper->getConfig('panel_type'));
356
357
include __DIR__ . '/footer.php';
358
359
/**
360
 * @public function getFormUploadImage:
361
 * provide form for uploading a new image
362
 * @param $imageOrigin
363
 * @param $imageId
364
 * @return \XoopsThemeForm
365
 */
366
function getFormUploadImage($imageOrigin, $imageId)
367
{
368
    $helper = \XoopsModules\Wgevents\Helper::getInstance();
369
    // Get Theme Form
370
    \xoops_load('XoopsFormLoader');
371
    $form = new \XoopsThemeForm('', 'formuploadimage', 'image_editor.php', 'post', true);
372
    $form->setExtra('enctype="multipart/form-data"');
373
    // upload new image
374
    $imageTray1      = new \XoopsFormElementTray(\_MA_WGEVENTS_FORM_UPLOAD_IMG, '<br>');
375
    $imageFileSelect = new \XoopsFormFile('', 'attachedfile', $helper->getConfig('maxsize'));
376
    $imageTray1->addElement($imageFileSelect);
377
    $form->addElement($imageTray1);
378
379
    $cond = \_MA_WGEVENTS_IMG_MAXSIZE . ': ' . ($helper->getConfig('maxsize_image') / 1048576) . ' ' . \_MA_WGEVENTS_SIZE_MB . '<br>';
380
    //$cond .= \_MI_WGEVENTS_MAXWIDTH . ': ' . $helper->getConfig('maxwidth') . ' px<br>';
381
    //$cond .= \_MI_WGEVENTS_MAXHEIGHT . ': ' . $helper->getConfig('maxheight') . ' px<br>';
382
    $cond .= \_MA_WGEVENTS_IMG_MIMETYPES . ': ' . \implode(', ', $helper->getConfig('mimetypes_image')) . '<br>';
0 ignored issues
show
Bug introduced by
It seems like $helper->getConfig('mimetypes_image') can also be of type null; however, parameter $pieces of implode() does only seem to accept array, 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

382
    $cond .= \_MA_WGEVENTS_IMG_MIMETYPES . ': ' . \implode(', ', /** @scrutinizer ignore-type */ $helper->getConfig('mimetypes_image')) . '<br>';
Loading history...
383
    $form->addElement(new \XoopsFormLabel(\_MA_WGEVENTS_IMG_EDITOR_UPLOAD, $cond));
384
385
    $imageTray3      = new \XoopsFormElementTray(\_MA_WGEVENTS_IMG_EDITOR_RESIZE, '');
386
    $resizeinfo = \str_replace('%w', (string)$helper->getConfig('maxwidth_image'), \_MA_WGEVENTS_IMG_EDITOR_RESIZE_DESC);
387
    $resizeinfo = \str_replace('%h', (string)$helper->getConfig('maxheight_image'), $resizeinfo);
388
    $imageTray3->addElement(new \XoopsFormLabel($resizeinfo, ''));
389
    $imageTray3->addElement(new \XoopsFormRadioYN('', 'img_resize', 1));
390
    $form->addElement($imageTray3);
391
392
    $form->addElement(new \XoopsFormHidden($imageOrigin, $imageId));
393
    $form->addElement(new \XoopsFormHidden('op', 'uploadImage'));
394
    $form->addElement(new \XoopsFormButtonTray('', \_SUBMIT, 'submit', '', false));
395
396
    return $form;
397
}
398