Passed
Push — master ( 48a6f8...570eae )
by Michael
46s queued 14s
created

ImageHandler   F

Complexity

Total Complexity 61

Size/Duplication

Total Lines 639
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 61
eloc 334
c 1
b 0
f 0
dl 0
loc 639
rs 3.52

15 Methods

Rating   Name   Duplication   Size   Complexity  
B getObjects() 0 31 7
A renderFormEdit() 0 22 1
A delete() 0 21 4
A create() 0 14 2
B resizeImage() 0 65 7
A getLastPictures() 0 19 2
B insert2() 0 67 9
A deleteAll() 0 13 4
A receivePicture() 0 67 3
A getCount() 0 13 4
A renderFormSubmit() 0 18 1
A get2() 0 15 3
A __construct() 0 12 2
B getLastPicturesForBlock() 0 53 5
B makeAvatar() 0 63 7

How to fix   Complexity   

Complex Class

Complex classes like ImageHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ImageHandler, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Suico;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
*/
16
17
use CriteriaElement;
18
use Xmf\Request;
19
use XoopsDatabase;
20
use XoopsFormButton;
21
use XoopsFormFile;
22
use XoopsFormHidden;
23
use XoopsFormLabel;
24
use XoopsFormText;
25
use XoopsMediaUploader;
26
use XoopsObject;
27
use XoopsPersistableObjectHandler;
28
use XoopsThemeForm;
29
30
/**
31
 * @category        Module
32
 * @package         suico
33
 * @copyright       {@link https://xoops.org/ XOOPS Project}
34
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
35
 * @author          Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
36
 */
37
/**
38
 * Protection against inclusion outside the site
39
 */
40
if (!\defined('XOOPS_ROOT_PATH')) {
41
    die('XOOPS root path not defined');
42
}
43
/**
44
 * Includes of form objects and uploader
45
 */
46
require_once XOOPS_ROOT_PATH . '/class/uploader.php';
47
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
48
require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
49
50
/**
51
 * suico_imageshandler class.
52
 * This class provides simple mechanism for Image object and generate forms for inclusion etc
53
 */
54
class ImageHandler extends XoopsPersistableObjectHandler
55
{
56
    public $helper;
57
    public $isAdmin;
58
59
    /**
60
     * Constructor
61
     * @param \XoopsDatabase|null             $xoopsDatabase
62
     * @param \XoopsModules\Suico\Helper|null $helper
63
     */
64
    public function __construct(
65
        ?XoopsDatabase $xoopsDatabase = null,
66
        $helper = null
67
    ) {
68
        /** @var \XoopsModules\Suico\Helper $this->helper */
69
        if (null === $helper) {
70
            $this->helper = Helper::getInstance();
71
        } else {
72
            $this->helper = $helper;
73
        }
74
        $isAdmin = $this->helper->isUserAdmin();
0 ignored issues
show
Unused Code introduced by
The assignment to $isAdmin is dead and can be removed.
Loading history...
75
        parent::__construct($xoopsDatabase, 'suico_images', Image::class, 'image_id', 'title', 'caption');
0 ignored issues
show
Unused Code introduced by
The call to XoopsPersistableObjectHandler::__construct() has too many arguments starting with 'caption'. ( Ignorable by Annotation )

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

75
        parent::/** @scrutinizer ignore-call */ 
76
                __construct($xoopsDatabase, 'suico_images', Image::class, 'image_id', 'title', 'caption');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
76
    }
77
78
    /**
79
     * create a new Groups
80
     *
81
     * @param bool $isNew flag the new objects as "new"?
82
     * @return \XoopsObject Groups
83
     */
84
    public function create(
85
        $isNew = true
86
    ) {
87
        $obj = parent::create($isNew);
88
        //        if ($isNew) {
89
        //            $obj->setDefaultPermissions();
90
        //        }
91
        if ($isNew) {
92
            $obj->setNew();
93
        } else {
94
            $obj->unsetNew();
95
        }
96
        $obj->helper = $this->helper;
97
        return $obj;
98
    }
99
100
    /**
101
     * retrieve a Image
102
     *
103
     * @param int  $id of the Image
104
     * @param null $fields
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fields is correct as it would always require null to be passed?
Loading history...
105
     * @return mixed reference to the {@link Image} object, FALSE if failed
106
     */
107
    public function get2(
108
        $id = null,
109
        $fields = null
0 ignored issues
show
Unused Code introduced by
The parameter $fields is not used and could be removed. ( Ignorable by Annotation )

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

109
        /** @scrutinizer ignore-unused */ $fields = null

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
110
    ) {
111
        $sql = 'SELECT * FROM ' . $this->db->prefix('suico_images') . ' WHERE image_id=' . $id;
112
        if (!$result = $this->db->query($sql)) {
113
            return false;
114
        }
115
        $numrows = $this->db->getRowsNum($result);
116
        if (1 === $numrows) {
117
            $image = new Image();
118
            $image->assignVars($this->db->fetchArray($result));
119
            return $image;
120
        }
121
        return false;
122
    }
123
124
    /**
125
     * insert a new Image in the database
126
     *
127
     * @param \XoopsObject $xoopsObject reference to the {@link Image} object
128
     * @param bool         $force
129
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
130
     */
131
    public function insert2(
132
        XoopsObject $xoopsObject,
133
        $force = false
134
    ) {
135
        global $xoopsConfig;
136
        if (!$xoopsObject instanceof Image) {
137
            return false;
138
        }
139
        if (!$xoopsObject->isDirty()) {
140
            return true;
141
        }
142
        if (!$xoopsObject->cleanVars()) {
143
            return false;
144
        }
145
        $image_id = '';
146
        foreach ($xoopsObject->cleanVars as $k => $v) {
147
            ${$k} = $v;
148
        }
149
        //        $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)';
150
        if ($xoopsObject->isNew()) {
151
            // ajout/modification d'un Image
152
            $xoopsObject = new Image();
153
            $format      = 'INSERT INTO %s (image_id, title, caption, date_created, date_updated, uid_owner, filename, private)';
154
            $format      .= 'VALUES (%u, %s, %s, %s, %s, %s, %s, 0)';
155
            $sql         = \sprintf(
156
                $format,
157
                $this->db->prefix('suico_images'),
158
                $image_id,
159
                $this->db->quoteString($title),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $title seems to be never defined.
Loading history...
160
                $this->db->quoteString($caption),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $caption seems to be never defined.
Loading history...
161
                \time(),//$now,
162
                \time(),//$now,
163
                $this->db->quoteString($uid_owner),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $uid_owner seems to be never defined.
Loading history...
164
                $this->db->quoteString($filename)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $filename seems to be never defined.
Loading history...
165
            );
166
            $force       = true;
167
        } else {
168
            $format = 'UPDATE %s SET ';
169
            $format .= 'image_id=%u, title=%s, caption=%s, date_created=%s, date_updated=%s, uid_owner=%s, filename=%s, private=%s';
170
            $format .= ' WHERE image_id = %u';
171
            $sql    = \sprintf(
172
                $format,
173
                $this->db->prefix('suico_images'),
174
                $image_id,
175
                $this->db->quoteString($title),
176
                $this->db->quoteString($caption),
177
                $xoopsObject->getVar('date_created'), // $now,
0 ignored issues
show
Bug introduced by
It seems like $xoopsObject->getVar('date_created') can also be of type array and array; however, parameter $args of sprintf() 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

177
                /** @scrutinizer ignore-type */ $xoopsObject->getVar('date_created'), // $now,
Loading history...
178
                $xoopsObject->getVar('date_updated'), // $now,
179
                $this->db->quoteString($uid_owner),
180
                $this->db->quoteString($filename),
181
                $this->db->quoteString($private),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $private seems to be never defined.
Loading history...
182
                $image_id
183
            );
184
        }
185
        if ($force) {
186
            $result = $this->db->queryF($sql);
187
        } else {
188
            $result = $this->db->query($sql);
189
        }
190
        if (!$result) {
191
            return false;
192
        }
193
        if (empty($image_id)) {
0 ignored issues
show
introduced by
The condition empty($image_id) is always true.
Loading history...
194
            $image_id = $this->db->getInsertId();
195
        }
196
        $xoopsObject->assignVar('image_id', $image_id);
197
        return true;
198
    }
199
200
    /**
201
     * delete a Image from the database
202
     *
203
     * @param \XoopsObject $xoopsObject reference to the Image to delete
204
     * @param bool         $force
205
     * @return bool FALSE if failed.
206
     */
207
    public function delete(
208
        XoopsObject $xoopsObject,
209
        $force = false
210
    ) {
211
        if (!$xoopsObject instanceof Image) {
212
            return false;
213
        }
214
        $sql = \sprintf(
215
            'DELETE FROM %s WHERE image_id = %u',
216
            $this->db->prefix('suico_images'),
217
            $xoopsObject->getVar('image_id')
0 ignored issues
show
Bug introduced by
It seems like $xoopsObject->getVar('image_id') can also be of type array and array; however, parameter $args of sprintf() 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

217
            /** @scrutinizer ignore-type */ $xoopsObject->getVar('image_id')
Loading history...
218
        );
219
        if ($force) {
220
            $result = $this->db->queryF($sql);
221
        } else {
222
            $result = $this->db->query($sql);
223
        }
224
        if (!$result) {
225
            return false;
226
        }
227
        return true;
228
    }
229
230
    /**
231
     * retrieve suico_imagess from the database
232
     *
233
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met
234
     * @param bool                                 $id_as_key       use the UID as key for the array?
235
     * @param bool                                 $as_object
236
     * @return array array of {@link Image} objects
237
     */
238
    public function &getObjects(
239
        ?CriteriaElement $criteriaElement = null,
240
        $id_as_key = false,
241
        $as_object = true
242
    ) {
243
        $ret   = [];
244
        $limit = $start = 0;
245
        $sql   = 'SELECT * FROM ' . $this->db->prefix('suico_images');
246
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
247
            $sql .= ' ' . $criteriaElement->renderWhere();
0 ignored issues
show
Bug introduced by
The method renderWhere() does not exist on CriteriaElement. Did you maybe mean render()? ( Ignorable by Annotation )

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

247
            $sql .= ' ' . $criteriaElement->/** @scrutinizer ignore-call */ renderWhere();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
248
            if ('' !== $criteriaElement->getSort()) {
249
                $sql .= ' ORDER BY ' . $criteriaElement->getSort() . ' ' . $criteriaElement->getOrder();
250
            }
251
            $limit = $criteriaElement->getLimit();
252
            $start = $criteriaElement->getStart();
253
        }
254
        $result = $this->db->query($sql, $limit, $start);
255
        if (!$result) {
256
            return $ret;
257
        }
258
        while (false !== ($myrow = $this->db->fetchArray($result))) {
259
            $image = new Image();
260
            $image->assignVars($myrow);
261
            if (!$id_as_key) {
262
                $ret[] = &$image;
263
            } else {
264
                $ret[$myrow['image_id']] = &$image;
265
            }
266
            unset($image);
267
        }
268
        return $ret;
269
    }
270
271
    /**
272
     * count suico_imagess matching a condition
273
     *
274
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} to match
275
     * @return int count of suico_imagess
276
     */
277
    public function getCount(
278
        ?CriteriaElement $criteriaElement = null
279
    ) {
280
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('suico_images');
281
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
282
            $sql .= ' ' . $criteriaElement->renderWhere();
283
        }
284
        $result = $this->db->query($sql);
285
        if (!$result) {
286
            return 0;
287
        }
288
        [$count] = $this->db->fetchRow($result);
289
        return (int)$count;
290
    }
291
292
    /**
293
     * delete suico_imagess matching a set of conditions
294
     *
295
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement}
296
     * @param bool                                 $force
297
     * @param bool                                 $asObject
298
     * @return bool FALSE if deletion failed
299
     */
300
    public function deleteAll(
301
        ?CriteriaElement $criteriaElement = null,
302
        $force = true,
303
        $asObject = false
304
    ) {
305
        $sql = 'DELETE FROM ' . $this->db->prefix('suico_images');
306
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
307
            $sql .= ' ' . $criteriaElement->renderWhere();
308
        }
309
        if (!$result = $this->db->query($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
310
            return false;
311
        }
312
        return true;
313
    }
314
315
    /**
316
     * Render a form to send pictures
317
     *
318
     * @param int       $maxbytes the maximum size of a picture
319
     * @param \XoopsTpl $xoopsTpl the one in which the form will be rendered
320
     * @return bool TRUE
321
     *
322
     * obs: Some functions wont work on php 4 so edit lines down under acording to your version
323
     */
324
    public function renderFormSubmit(
325
        $maxbytes,
326
        $xoopsTpl
327
    ) {
328
        $form          = new XoopsThemeForm(\_MD_SUICO_SUBMIT_PIC_TITLE, 'form_picture', 'submitImage.php', 'post', true);
329
        $field_url     = new XoopsFormFile(\_MD_SUICO_SELECT_PHOTO, 'sel_photo', 2000000);
330
        $field_title   = new XoopsFormText(\_MD_SUICO_PHOTOTITLE, 'title', 35, 55);
331
        $field_caption = new XoopsFormText(\_MD_SUICO_CAPTION, 'caption', 35, 55);
332
        $form->setExtra('enctype="multipart/form-data"');
333
        $buttonSend    = new XoopsFormButton('', 'submit_button', \_MD_SUICO_UPLOADPICTURE, 'submit');
334
        $field_warning = new XoopsFormLabel(\sprintf(\_MD_SUICO_YOU_CAN_UPLOAD, $maxbytes / 1024));
335
        $form->addElement($field_warning);
336
        $form->addElement($field_url, true);
337
        $form->addElement($field_title);
338
        $form->addElement($field_caption);
339
        $form->addElement($buttonSend);
340
        $form->assign($xoopsTpl); //If your server is php 5
341
        return true;
342
    }
343
344
    /**
345
     * Render a form to edit the description of the pictures
346
     *
347
     * @param        $title
348
     * @param string $caption  The description of the picture
349
     * @param int    $image_id the id of the image in database
350
     * @param string $filename the url to the thumb of the image so it can be displayed
351
     * @return bool TRUE
352
     */
353
    public function renderFormEdit(
354
        $title,
355
        $caption,
356
        $image_id,
357
        $filename
358
    ) {
359
        $form          = new XoopsThemeForm(\_MD_SUICO_EDIT_PICTURE, 'form_picture', 'editpicture.php', 'post', true);
360
        $field_title   = new XoopsFormText($title, 'title', 35, 55);
361
        $field_caption = new XoopsFormText($caption, 'caption', 35, 55);
362
        $form->setExtra('enctype="multipart/form-data"');
363
        $buttonSend     = new XoopsFormButton('', 'submit_button', \_MD_SUICO_SUBMIT, 'submit');
364
        $field_warning  = new XoopsFormLabel("<img src='" . $filename . "' alt='thumb'>");
365
        $field_image_id = new XoopsFormHidden('image_id', $image_id);
366
        $field_marker   = new XoopsFormHidden('marker', 1);
367
        $form->addElement($field_warning);
368
        $form->addElement($field_title);
369
        $form->addElement($field_caption);
370
        $form->addElement($field_image_id);
371
        $form->addElement($field_marker);
372
        $form->addElement($buttonSend);
373
        $form->display();
374
        return true;
375
    }
376
377
    /**
378
     * Upload the file and Save into database
379
     *
380
     * @param string $title         A litle title of the file
381
     * @param string $caption       A litle description of the file
382
     * @param string $pathUpload    The path to where the file should be uploaded
383
     * @param int    $thumbwidth    the width in pixels that the thumbnail will have
384
     * @param int    $thumbheight   the height in pixels that the thumbnail will have
385
     * @param int    $pictwidth     the width in pixels that the pic will have
386
     * @param int    $pictheight    the height in pixels that the pic will have
387
     * @param int    $maxfilebytes  the maximum size a file can have to be uploaded in bytes
388
     * @param int    $maxfilewidth  the maximum width in pixels that a pic can have
389
     * @param int    $maxfileheight the maximum height in pixels that a pic can have
390
     * @return bool FALSE if upload fails or database fails
391
     */
392
    public function receivePicture(
393
        $title,
394
        $caption,
395
        $pathUpload,
396
        $thumbwidth,
397
        $thumbheight,
398
        $pictwidth,
399
        $pictheight,
400
        $maxfilebytes,
401
        $maxfilewidth,
402
        $maxfileheight
403
    ) {
404
        global $xoopsUser, $xoopsDB;
405
        //search logged user id
406
        $uid = $xoopsUser->getVar('uid');
407
        //create a hash so it does not erase another file
408
        //$hash1 = date();
409
        //$hash = substr($hash1,0,4);
410
        // mimetypes and settings put this in admin part later
411
        $allowed_mimetypes = Helper::getInstance()->getConfig(
412
            'mimetypes'
413
        );
414
        $maxfilesize       = $maxfilebytes;
415
        //        $uploadDir = \XOOPS_UPLOAD_PATH . '/suico/images/';
416
        // create the object to upload
417
        $uploader = new XoopsMediaUploader(
418
            $pathUpload, $allowed_mimetypes, $maxfilesize, $maxfilewidth, $maxfileheight
419
        );
420
        // fetch the media
421
        if ($uploader->fetchMedia(Request::getArray('xoops_upload_file', '', 'POST')[0])) {
422
            //lets create a name for it
423
            $uploader->setPrefix('pic_' . $uid . '_');
424
            //now let s upload the file
425
            if (!$uploader->upload()) {
426
                // if there are errors lets return them
427
                echo '<div style="color:#FF0000; background-color:#FFEAF4; border-color:#FF0000; border-width:thick; border-style:solid; text-align:center"><p>' . $uploader->getErrors() . '</p></div>';
428
                return false;
429
            }
430
            // now let s create a new object picture and set its variables
431
            $picture  = $this->create();
432
            $filename = $uploader->getSavedFileName();
433
            $picture->setVar('filename', $filename);
434
            $picture->setVar('title', $title);
435
            $picture->setVar('caption', $caption);
436
            $picture->setVar('date_created', \time());
437
            $picture->setVar('date_updated', \time());
438
            $picture->setVar('private', 0);
439
            $uid = $xoopsUser->getVar('uid');
440
            $picture->setVar('uid_owner', $uid);
441
            $this->insert($picture);
442
            $saved_destination = $uploader->getSavedDestination();
443
            //print_r($_FILES);
444
            //$this->resizeImage($saved_destination,false, $thumbwidth, $thumbheight, $pictwidth, $pictheight,$pathUpload);
445
            //$this->resizeImage($saved_destination,true, $thumbwidth, $thumbheight, $pictwidth, $pictheight,$pathUpload);
446
            $this->resizeImage(
447
                $saved_destination,
448
                $thumbwidth,
449
                $thumbheight,
450
                $pictwidth,
451
                $pictheight,
452
                $pathUpload
453
            );
454
        } else {
455
            echo '<div style="color:#FF0000; background-color:#FFEAF4; border-color:#FF0000; border-width:thick; border-style:solid; text-align:center"><p>' . $uploader->getErrors() . '</p></div>';
456
            return false;
457
        }
458
        return true;
459
    }
460
461
    /**
462
     * Resize a picture and save it to $pathUpload
463
     *
464
     * @param string $img         the path to the file
465
     * @param int    $thumbwidth  the width in pixels that the thumbnail will have
466
     * @param int    $thumbheight the height in pixels that the thumbnail will have
467
     * @param int    $pictwidth   the width in pixels that the pic will have
468
     * @param int    $pictheight  the height in pixels that the pic will have
469
     * @param string $pathUpload  The path to where the files should be saved after resizing
470
     */
471
    public function resizeImage(
472
        $img,
473
        $thumbwidth,
474
        $thumbheight,
475
        $pictwidth,
476
        $pictheight,
477
        $pathUpload
478
    ) {
479
        $img2   = $img;
480
        $path   = \pathinfo($img);
481
        $img    = \imagecreatefromjpeg($img);
482
        $xratio = $thumbwidth / \imagesx($img);
0 ignored issues
show
Bug introduced by
It seems like $img can also be of type false; however, parameter $image of imagesx() does only seem to accept resource, 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

482
        $xratio = $thumbwidth / \imagesx(/** @scrutinizer ignore-type */ $img);
Loading history...
483
        $yratio = $thumbheight / \imagesy($img);
0 ignored issues
show
Bug introduced by
It seems like $img can also be of type false; however, parameter $image of imagesy() does only seem to accept resource, 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

483
        $yratio = $thumbheight / \imagesy(/** @scrutinizer ignore-type */ $img);
Loading history...
484
        if ($xratio < 1 || $yratio < 1) {
485
            if ($xratio < $yratio) {
486
                $resized = \imagecreatetruecolor($thumbwidth, (int)\floor(\imagesy($img) * $xratio));
487
            } else {
488
                $resized = \imagecreatetruecolor((int)\floor(\imagesx($img) * $yratio), $thumbheight);
489
            }
490
            \imagecopyresampled(
491
                $resized,
0 ignored issues
show
Bug introduced by
It seems like $resized can also be of type false; however, parameter $dst_image of imagecopyresampled() does only seem to accept resource, 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

491
                /** @scrutinizer ignore-type */ $resized,
Loading history...
492
                $img,
0 ignored issues
show
Bug introduced by
It seems like $img can also be of type false; however, parameter $src_image of imagecopyresampled() does only seem to accept resource, 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

492
                /** @scrutinizer ignore-type */ $img,
Loading history...
493
                0,
494
                0,
495
                0,
496
                0,
497
                \imagesx($resized) + 1,
498
                \imagesy($resized) + 1,
499
                \imagesx($img),
500
                \imagesy($img)
501
            );
502
            \imagejpeg($resized, $pathUpload . '/thumb_' . $path['basename']);
0 ignored issues
show
Bug introduced by
It seems like $resized can also be of type false; however, parameter $image of imagejpeg() does only seem to accept resource, 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

502
            \imagejpeg(/** @scrutinizer ignore-type */ $resized, $pathUpload . '/thumb_' . $path['basename']);
Loading history...
503
            \imagedestroy($resized);
0 ignored issues
show
Bug introduced by
It seems like $resized can also be of type false; however, parameter $image of imagedestroy() does only seem to accept resource, 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

503
            \imagedestroy(/** @scrutinizer ignore-type */ $resized);
Loading history...
504
        } else {
505
            \imagejpeg($img, $pathUpload . '/thumb_' . $path['basename']);
506
        }
507
        \imagedestroy($img);
508
        $path2   = \pathinfo($img2);
509
        $img2    = \imagecreatefromjpeg($img2);
510
        $xratio2 = $pictwidth / \imagesx($img2);
511
        $yratio2 = $pictheight / \imagesy($img2);
512
        if ($xratio2 < 1 || $yratio2 < 1) {
513
            if ($xratio2 < $yratio2) {
514
                $resized2 = \imagecreatetruecolor($pictwidth, (int)\floor(\imagesy($img2) * $xratio2));
515
            } else {
516
                $resized2 = \imagecreatetruecolor((int)\floor(\imagesx($img2) * $yratio2), $pictheight);
517
            }
518
            \imagecopyresampled(
519
                $resized2,
520
                $img2,
521
                0,
522
                0,
523
                0,
524
                0,
525
                \imagesx($resized2) + 1,
526
                \imagesy($resized2) + 1,
527
                \imagesx($img2),
528
                \imagesy($img2)
529
            );
530
            \imagejpeg($resized2, $pathUpload . '/resized_' . $path2['basename']);
531
            \imagedestroy($resized2);
532
        } else {
533
            \imagejpeg($img2, $pathUpload . '/resized_' . $path2['basename']);
534
        }
535
        \imagedestroy($img2);
536
    }
537
538
    /**
539
     * @param $limit
540
     * @return array
541
     */
542
    public function getLastPictures($limit)
543
    {
544
        $ret    = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
545
        $sql    = 'SELECT uname, t.uid_owner, t.filename FROM ' . $this->db->prefix(
546
                'suico_images'
547
            ) . ' AS t, ' . $this->db->prefix(
548
                'users'
549
            );
550
        $sql    .= ' WHERE uid_owner = uid AND private=0 ORDER BY image_id DESC';
551
        $result = $this->db->query($sql, $limit, 0);
552
        $vetor  = [];
553
        $i      = 0;
554
        while (false !== ($myrow = $this->db->fetchArray($result))) {
555
            $vetor[$i]['uid_owner']    = $myrow['uid_owner'];
556
            $vetor[$i]['uname']        = $myrow['uname'];
557
            $vetor[$i]['img_filename'] = $myrow['filename'];
558
            $i++;
559
        }
560
        return $vetor;
561
    }
562
563
    /**
564
     * @param $limit
565
     * @return array
566
     */
567
    public function getLastPicturesForBlock($limit)
568
    {
569
        global $xoopsUser, $xoopsDB;
570
        $memberHandler = xoops_getHandler('member');
0 ignored issues
show
Unused Code introduced by
The assignment to $memberHandler is dead and can be removed.
Loading history...
571
        $user = $xoopsUser;
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
572
        $user2 = $GLOBALS['xoopsUser'];
0 ignored issues
show
Unused Code introduced by
The assignment to $user2 is dead and can be removed.
Loading history...
573
        if (is_object($xoopsUser)) {
574
            $uid = $xoopsUser->getVar('uid');
575
        }
576
577
        $controller = new PhotosController($xoopsDB, $xoopsUser);
578
579
        $isAdmin     = Helper::getInstance()->isUserAdmin();
0 ignored issues
show
Unused Code introduced by
The assignment to $isAdmin is dead and can be removed.
Loading history...
580
        $isUser      = $controller->isUser;
581
        $isAnonymous = $controller->isAnonym;
582
583
        $ret    = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
584
585
        if (1 == $isAnonymous) {
586
            $sql = 'SELECT uname, t.uid_owner, t.filename, t.title, t.caption, t.date_created, t.date_updated  FROM ' . $this->db->prefix('suico_images') . ' AS t';
587
            $sql .= ' INNER JOIN ' . $this->db->prefix('users') . ' u ON t.uid_owner=u.uid';
588
            $sql .= ' INNER JOIN ' . $this->db->prefix('suico_configs') . ' c on t.uid_owner=c.config_uid';
589
            $sql .= ' WHERE private=0 AND c.pictures < 2 ';
590
            $sql .= ' ORDER BY image_id DESC';
591
        }
592
        if (1 == $isUser) {
593
            $sql0 = 'SELECT f.friend2_uid FROM ' . $this->db->prefix('suico_friendships') . ' AS f';
594
            $sql0 .= ' WHERE f.friend1_uid = '. $uid ;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $uid does not seem to be defined for all execution paths leading up to this point.
Loading history...
595
            $sql = 'SELECT uname, t.uid_owner, t.filename, t.title, t.caption, t.date_created, t.date_updated  FROM ' . $this->db->prefix('suico_images') . ' AS t';
596
            $sql .= ' INNER JOIN ' . $this->db->prefix('users') . ' u ON t.uid_owner=u.uid';
597
            $sql .= ' INNER JOIN ' . $this->db->prefix('suico_configs') . ' c on t.uid_owner=c.config_uid';
598
            $sql .= ' WHERE (private=0 AND c.pictures < 3 )'; //all pictures visible to members
599
            $sql .= ' OR ( private=0 AND c.pictures = 3 AND c.config_uid IN ( '. $sql0 .')) '; //pictures visible to friends
600
            $sql .= ' OR ( c.config_uid = '. $uid .' AND c.pictures = 4) '; //my private pictures
601
            $sql .= ' ORDER BY image_id DESC';
602
        }
603
604
        $result = $this->db->query($sql, $limit, 0);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sql does not seem to be defined for all execution paths leading up to this point.
Loading history...
605
606
607
        $vetor  = [];
608
        $i      = 0;
609
        while (false !== ($myrow = $this->db->fetchArray($result))) {
610
            $vetor[$i]['uid_owner']    = $myrow['uid_owner'];
611
            $vetor[$i]['uname']        = $myrow['uname'];
612
            $vetor[$i]['img_filename'] = $myrow['filename'];
613
            $vetor[$i]['title']        = $myrow['title'];
614
            $vetor[$i]['caption']      = $myrow['caption'];
615
            $vetor[$i]['date_created'] = formatTimestamp($myrow['date_created']);
616
            $vetor[$i]['date_updated'] = formatTimestamp($myrow['date_updated']);
617
            $i++;
618
        }
619
        return $vetor;
620
    }
621
622
    /**
623
     * Resize a picture and save it to $pathUpload
624
     *
625
     * @param string $img        the path to the file
626
     * @param        $width
627
     * @param        $height
628
     * @param string $pathUpload The path to where the files should be saved after resizing
629
     */
630
    public function makeAvatar(
631
        $img,
632
        $width,
0 ignored issues
show
Unused Code introduced by
The parameter $width is not used and could be removed. ( Ignorable by Annotation )

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

632
        /** @scrutinizer ignore-unused */ $width,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
633
        $height,
0 ignored issues
show
Unused Code introduced by
The parameter $height is not used and could be removed. ( Ignorable by Annotation )

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

633
        /** @scrutinizer ignore-unused */ $height,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
634
        $pathUpload
635
    ) {
636
        $img2   = $img;
637
        $path   = \pathinfo($img);
638
        $img    = \imagecreatefromjpeg($img);
639
        $xratio = $thumbwidth / \imagesx($img);
0 ignored issues
show
Bug introduced by
It seems like $img can also be of type false; however, parameter $image of imagesx() does only seem to accept resource, 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

639
        $xratio = $thumbwidth / \imagesx(/** @scrutinizer ignore-type */ $img);
Loading history...
Comprehensibility Best Practice introduced by
The variable $thumbwidth seems to be never defined.
Loading history...
640
        $yratio = $thumbheight / \imagesy($img);
0 ignored issues
show
Bug introduced by
It seems like $img can also be of type false; however, parameter $image of imagesy() does only seem to accept resource, 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

640
        $yratio = $thumbheight / \imagesy(/** @scrutinizer ignore-type */ $img);
Loading history...
Comprehensibility Best Practice introduced by
The variable $thumbheight does not exist. Did you maybe mean $height?
Loading history...
641
        if ($xratio < 1 || $yratio < 1) {
642
            if ($xratio < $yratio) {
643
                $resized = \imagecreatetruecolor($thumbwidth, (int)\floor(\imagesy($img) * $xratio));
644
            } else {
645
                $resized = \imagecreatetruecolor((int)\floor(\imagesx($img) * $yratio), $thumbheight);
646
            }
647
            \imagecopyresampled(
648
                $resized,
0 ignored issues
show
Bug introduced by
It seems like $resized can also be of type false; however, parameter $dst_image of imagecopyresampled() does only seem to accept resource, 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

648
                /** @scrutinizer ignore-type */ $resized,
Loading history...
649
                $img,
0 ignored issues
show
Bug introduced by
It seems like $img can also be of type false; however, parameter $src_image of imagecopyresampled() does only seem to accept resource, 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

649
                /** @scrutinizer ignore-type */ $img,
Loading history...
650
                0,
651
                0,
652
                0,
653
                0,
654
                \imagesx($resized) + 1,
655
                \imagesy($resized) + 1,
656
                \imagesx($img),
657
                \imagesy($img)
658
            );
659
            \imagejpeg($resized, $pathUpload . '/thumb_' . $path['basename']);
0 ignored issues
show
Bug introduced by
It seems like $resized can also be of type false; however, parameter $image of imagejpeg() does only seem to accept resource, 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

659
            \imagejpeg(/** @scrutinizer ignore-type */ $resized, $pathUpload . '/thumb_' . $path['basename']);
Loading history...
660
            \imagedestroy($resized);
0 ignored issues
show
Bug introduced by
It seems like $resized can also be of type false; however, parameter $image of imagedestroy() does only seem to accept resource, 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

660
            \imagedestroy(/** @scrutinizer ignore-type */ $resized);
Loading history...
661
        } else {
662
            \imagejpeg($img, $pathUpload . '/thumb_' . $path['basename']);
663
        }
664
        \imagedestroy($img);
665
        $path2   = \pathinfo($img2);
666
        $img2    = \imagecreatefromjpeg($img2);
667
        $xratio2 = $pictwidth / \imagesx($img2);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pictwidth seems to be never defined.
Loading history...
668
        $yratio2 = $pictheight / \imagesy($img2);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pictheight does not exist. Did you maybe mean $height?
Loading history...
669
        if ($xratio2 < 1 || $yratio2 < 1) {
670
            if ($xratio2 < $yratio2) {
671
                $resized2 = \imagecreatetruecolor($pictwidth, (int)\floor(\imagesy($img2) * $xratio2));
672
            } else {
673
                $resized2 = \imagecreatetruecolor((int)\floor(\imagesx($img2) * $yratio2), $pictheight);
674
            }
675
            \imagecopyresampled(
676
                $resized2,
677
                $img2,
678
                0,
679
                0,
680
                0,
681
                0,
682
                \imagesx($resized2) + 1,
683
                \imagesy($resized2) + 1,
684
                \imagesx($img2),
685
                \imagesy($img2)
686
            );
687
            \imagejpeg($resized2, $pathUpload . '/resized_' . $path2['basename']);
688
            \imagedestroy($resized2);
689
        } else {
690
            \imagejpeg($img2, $pathUpload . '/resized_' . $path2['basename']);
691
        }
692
        \imagedestroy($img2);
693
    }
694
}
695
696