Passed
Push — master ( 8d8e58...047d50 )
by Michael
02:21
created

PhotoHandler   F

Complexity

Total Complexity 121

Size/Duplication

Total Lines 1096
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1096
rs 0.6568
c 0
b 0
f 0
wmc 121

48 Methods

Rating   Name   Duplication   Size   Complexity  
A _makeBorder() 0 16 2
C addLocalPhoto() 0 69 8
A getPhotoAlbumId() 0 15 2
A modifyPhoto() 0 6 1
A getAlbumCurrentPhotoPlace() 0 13 1
A _makeThumb() 0 18 3
A getAlbumCount() 0 5 1
A _addInCriteria() 0 11 3
A nbPhoto() 0 5 1
A updateEcard() 0 5 1
C _largePhotoTreatment() 0 34 8
A createPhoto() 0 6 1
A _getFileSize() 0 3 1
A getAlbumPhoto() 0 7 1
A _getPhotoSize() 0 6 2
A getLastPhoto() 0 13 1
A deletePhotoByCat() 0 6 2
A _haveLargePhoto() 0 3 1
A getAlbumPrevPhoto() 0 14 1
C getSearchedPhoto() 0 43 7
A getTopViewPhoto() 0 13 1
A getAlbumPhotoAdminPage() 0 11 1
A getInsertId() 0 3 1
A getAllSize() 0 3 1
A getSlideshowAlbumPhoto() 0 9 1
A getTopEcardPhoto() 0 13 1
A getTopRatedPhoto() 0 13 1
A deletePhoto() 0 6 2
A getAlbumPhotoPage() 0 13 2
A _getUploadPhotoPath() 0 3 1
B getCatPhoto() 0 25 3
A getAlbumNextPhoto() 0 14 1
D _mediumPhotoTreatment() 0 44 10
A _makeFileName() 0 10 2
A getPhoto() 0 12 2
A updateDownload() 0 5 1
A deleteFile() 0 3 1
A getTopSubmitter() 0 8 1
A __construct() 0 3 1
C _makeWatermark() 0 57 12
A getPendingPhoto() 0 5 1
A updateHits() 0 5 1
A getRandomPhoto() 0 13 2
A rebuildThumbnail() 0 5 2
B _getAutoDescription() 0 24 1
B _getImageDimension() 0 24 4
D postPhotoTraitement() 0 112 15
A updateNbRating() 0 5 1

How to fix   Complexity   

Complex Class

Complex classes like PhotoHandler 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 PhotoHandler, and based on these observations, apply Extract Interface, too.

1
<?php namespace XoopsModules\Extgallery;
2
3
/**
4
 * ExtGallery Class Manager
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
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
 *
13
 * @copyright   {@link https://xoops.org/ XOOPS Project}
14
 * @license     GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
15
 * @author      Zoullou (http://www.zoullou.net)
16
 * @package     ExtGallery
17
 */
18
19
use XoopsModules\Extgallery;
20
21
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
22
23
24
/**
25
 * Class PhotoHandler
26
 */
27
class PhotoHandler extends Extgallery\PersistableObjectHandler
28
{
29
    public $photoUploader = null;
30
31
    /**
32
     * @param $db
33
     * @param $type
34
     */
35
    public function __construct(\XoopsDatabase $db, $type)
0 ignored issues
show
Bug introduced by
The type XoopsDatabase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
    {
37
        parent::__construct($db, 'extgallery_' . $type . 'photo',  ucfirst($type) . 'Photo', 'photo_id');
38
    }
39
40
    /**
41
     * @param $data
42
     *
43
     * @return bool
44
     */
45
    public function createPhoto($data)
46
    {
47
        $photo = $this->create();
48
        $photo->setVars($data);
49
50
        return $this->insert($photo, true);
51
    }
52
53
    /**
54
     * @param $photoId
55
     * @param $data
56
     *
57
     * @return bool
58
     */
59
    public function modifyPhoto($photoId, $data)
60
    {
61
        $photo = $this->get($photoId);
62
        $photo->setVars($data);
63
64
        return $this->insert($photo, true);
65
    }
66
67
    /**
68
     * @param $photo
69
     */
70
    public function deletePhoto(&$photo)
71
    {
72
        if ('' == $photo->getVar('photo_serveur')) {
73
            $this->deleteFile($photo);
0 ignored issues
show
Unused Code introduced by
The call to XoopsModules\Extgallery\PhotoHandler::deleteFile() has too many arguments starting with $photo. ( Ignorable by Annotation )

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

73
            $this->/** @scrutinizer ignore-call */ 
74
                   deleteFile($photo);

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...
74
        }
75
        $this->deleteById($photo->getVar('photo_id'), true);
76
    }
77
78
    /**
79
     * @param $catId
80
     */
81
    public function deletePhotoByCat($catId)
82
    {
83
        $criteria = new \Criteria('cat_id', $catId);
0 ignored issues
show
Bug introduced by
The type Criteria was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
84
        $photos   =& $this->getObjects($criteria);
85
        foreach ($photos as $photo) {
86
            $this->deletePhoto($photo);
87
        }
88
    }
89
90
    public function deleteFile()
91
    {
92
        exit('deleteFile() method must be defined on sub classes');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
93
    }
94
95
    /**
96
     * @param $photoId
97
     *
98
     * @return bool
99
     */
100
    public function getPhoto($photoId)
101
    {
102
        $criteria = new \CriteriaCompo();
0 ignored issues
show
Bug introduced by
The type CriteriaCompo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
103
        $criteria->add(new \Criteria('photo_id', $photoId));
104
        $criteria->add(new \Criteria('photo_approved', 1));
105
106
        $photo =& $this->getObjects($criteria);
107
        if (1 != count($photo)) {
108
            return false;
109
        }
110
111
        return $photo[0];
112
    }
113
114
    /**
115
     * @param $cat
116
     *
117
     * @return int
118
     */
119
    public function nbPhoto(&$cat)
120
    {
121
        $criteria = new \Criteria('cat_id', $cat->getVar('cat_id'));
122
123
        return $this->getCount($criteria);
124
    }
125
126
    /**
127
     * @param $catId
128
     * @param $start
129
     * @param $sortby
130
     * @param $orderby
131
     *
132
     * @return array
133
     */
134
    public function getAlbumPhotoPage($catId, $start, $sortby, $orderby)
135
    {
136
        $criteria = new \CriteriaCompo();
137
        $criteria->add(new \Criteria('cat_id', $catId));
138
        $criteria->add(new \Criteria('photo_approved', 1));
139
        $criteria->setStart($start);
140
        $criteria->setLimit($GLOBALS['xoopsModuleConfig']['nb_column'] * $GLOBALS['xoopsModuleConfig']['nb_line']);
141
        if ('' == $criteria->getSort()) {
142
            $criteria->setSort($sortby);
143
            $criteria->setOrder($orderby);
144
        }
145
146
        return $this->getObjects($criteria);
147
    }
148
149
    /**
150
     * @param $catId
151
     * @param $start
152
     *
153
     * @return array
154
     */
155
    public function getAlbumPhotoAdminPage($catId, $start)
156
    {
157
        $criteria = new \CriteriaCompo();
158
        $criteria->add(new \Criteria('cat_id', $catId));
159
        $criteria->add(new \Criteria('photo_approved', 1));
160
        $criteria->setStart($start);
161
        $criteria->setLimit($GLOBALS['xoopsModuleConfig']['admin_nb_photo']);
162
        $criteria->setSort('photo_weight, photo_id');
163
        $criteria->setOrder($GLOBALS['xoopsModuleConfig']['display_set_order']);
164
165
        return $this->getObjects($criteria);
166
    }
167
168
    /**
169
     * @param $catId
170
     *
171
     * @return array
172
     */
173
    public function getSlideshowAlbumPhoto($catId)
174
    {
175
        $criteria = new \CriteriaCompo();
176
        $criteria->add(new \Criteria('cat_id', $catId));
177
        $criteria->add(new \Criteria('photo_approved', 1));
178
        $criteria->setSort('photo_weight, photo_id');
179
        $criteria->setOrder($GLOBALS['xoopsModuleConfig']['display_set_order']);
180
181
        return $this->getObjects($criteria, false, false);
182
    }
183
184
    /**
185
     * @param $catId
186
     *
187
     * @return array
188
     */
189
    public function getPhotoAlbumId($catId)
190
    {
191
        $criteria = new \CriteriaCompo();
192
        $criteria->add(new \Criteria('cat_id', $catId));
193
        $criteria->add(new \Criteria('photo_approved', 1));
194
195
        $sql = 'SELECT photo_id FROM ' . $this->db->prefix('extgallery_publicphoto') . ' ' . $criteria->renderWhere() . ' ORDER BY photo_weight, photo_id ASC;';
196
197
        $result = $this->db->query($sql);
198
        $ret    = [];
199
        while (false !== ($myrow = $this->db->fetchArray($result))) {
200
            $ret[] = $myrow['photo_id'];
201
        }
202
203
        return $ret;
204
    }
205
206
    /**
207
     * @param $catId
208
     * @param $photoId
209
     *
210
     * @return array
211
     */
212
    public function getAlbumPrevPhoto($catId, $photoId)
213
    {
214
        $catHandler = Extgallery\Helper::getInstance()->getHandler('PublicCategory');
215
216
        $criteria = new \CriteriaCompo();
217
        $criteria->add($catHandler->getCatRestrictCriteria());
218
        $criteria->add(new \Criteria('photo_approved', 1));
219
        $criteria->add(new \Criteria('cat_id', $catId));
220
        $criteria->add(new \Criteria('photo_id', $photoId, '<'));
221
        $criteria->setSort('photo_weight, photo_id');
222
        $criteria->setOrder('DESC');
223
        $criteria->setLimit(1);
224
225
        return $this->getObjects($criteria);
226
    }
227
228
    /**
229
     * @param $catId
230
     * @param $photoId
231
     *
232
     * @return array
233
     */
234
    public function getAlbumNextPhoto($catId, $photoId)
235
    {
236
        $catHandler = Extgallery\Helper::getInstance()->getHandler('PublicCategory');
237
238
        $criteria = new \CriteriaCompo();
239
        $criteria->add($catHandler->getCatRestrictCriteria());
240
        $criteria->add(new \Criteria('photo_approved', 1));
241
        $criteria->add(new \Criteria('cat_id', $catId));
242
        $criteria->add(new \Criteria('photo_id', $photoId, '>'));
243
        $criteria->setSort('photo_weight, photo_id');
244
        $criteria->setOrder('ASC');
245
        $criteria->setLimit(1);
246
247
        return $this->getObjects($criteria);
248
    }
249
250
    /**
251
     * @param $catId
252
     * @param $photoId
253
     *
254
     * @return int
255
     */
256
    public function getAlbumCurrentPhotoPlace($catId, $photoId)
257
    {
258
        $catHandler = Extgallery\Helper::getInstance()->getHandler('PublicCategory');
259
260
        $criteria = new \CriteriaCompo();
261
        $criteria->add($catHandler->getCatRestrictCriteria());
262
        $criteria->add(new \Criteria('photo_approved', 1));
263
        $criteria->add(new \Criteria('cat_id', $catId));
264
        $criteria->add(new \Criteria('photo_id', $photoId, '<='));
265
        $criteria->setSort('photo_weight, photo_id');
266
        $criteria->setOrder('DESC');
267
268
        return $this->getCount($criteria);
269
    }
270
271
    /**
272
     * @param $catId
273
     *
274
     * @return array
275
     */
276
    public function getAlbumPhoto($catId)
277
    {
278
        $criteria = new \Criteria('cat_id', $catId);
279
        $criteria->setSort('photo_weight, photo_id');
280
        $criteria->setOrder('ASC');
281
282
        return $this->getObjects($criteria);
283
    }
284
285
    /**
286
     * @param $category
287
     *
288
     * @return array
289
     */
290
    public function getCatPhoto(&$category)
291
    {
292
        $catHandler = Extgallery\Helper::getInstance()->getHandler('PublicCategory');
293
294
        $criteria = new \CriteriaCompo();
295
        $criteria->add(new \Criteria('nleft', $category->getVar('nleft'), '>='));
296
        $criteria->add(new \Criteria('nright', $category->getVar('nright'), '<='));
297
298
        $cats = $catHandler->getObjects($criteria);
299
300
        $count = count($cats);
301
        if ($count > 0) {
302
            $in = '(' . $cats[0]->getVar('cat_id');
303
            array_shift($cats);
304
            /** @var Extgallery\Category $cat */
305
            foreach ($cats as $cat) {
306
                $in .= ',' . $cat->getVar('cat_id');
307
            }
308
            $in       .= ')';
309
            $criteria = new \Criteria('cat_id', $in, 'IN');
310
        } else {
311
            $criteria = new \Criteria('cat_id', '(0)', 'IN');
312
        }
313
314
        return $this->getObjects($criteria);
315
    }
316
317
    /**
318
     * @param $catId
319
     *
320
     * @return int
321
     */
322
    public function getAlbumCount($catId)
323
    {
324
        $criteria = new \Criteria('cat_id', $catId);
325
326
        return $this->getCount($criteria);
327
    }
328
329
    /**
330
     * @param $photoId
331
     *
332
     * @return bool
333
     */
334
    public function updateHits($photoId)
335
    {
336
        $criteria = new \Criteria('photo_id', $photoId);
337
338
        return $this->updateCounter('photo_hits', $criteria);
339
    }
340
341
    /**
342
     * @param $photoId
343
     *
344
     * @return bool
345
     */
346
    public function updateNbRating($photoId)
347
    {
348
        $criteria = new \Criteria('photo_id', $photoId);
349
350
        return $this->updateCounter('photo_nbrating', $criteria);
351
    }
352
353
    /**
354
     * @param $photoId
355
     *
356
     * @return bool
357
     */
358
    public function updateDownload($photoId)
359
    {
360
        $criteria = new \Criteria('photo_id', $photoId);
361
362
        return $this->updateCounter('photo_download', $criteria);
363
    }
364
365
    /**
366
     * @param $photoId
367
     *
368
     * @return bool
369
     */
370
    public function updateEcard($photoId)
371
    {
372
        $criteria = new \Criteria('photo_id', $photoId);
373
374
        return $this->updateCounter('photo_ecard', $criteria);
375
    }
376
377
    public function getAllSize()
378
    {
379
        exit('getAllSize() method must be defined on sub classes');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
380
    }
381
382
    /**
383
     * @param $imageTransform
384
     */
385
    public function _makeWatermark(&$imageTransform)
386
    {
387
        if (!function_exists('imagettfbbox')) {
388
            return;
389
        }
390
391
        global $xoopsModuleConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
392
393
        /*  Text position param
394
        /
395
        /   0 : orig
396
        /   -1 : opposit
397
        /   1 : center
398
        /
399
        */
400
        if ('tl' === $xoopsModuleConfig['watermark_position']) {
401
            $x = 0;
402
            $y = 0;
403
        } elseif ('tr' === $xoopsModuleConfig['watermark_position']) {
404
            $x = -1;
405
            $y = 0;
406
        } elseif ('bl' === $xoopsModuleConfig['watermark_position']) {
407
            $x = 0;
408
            $y = -1;
409
        } elseif ('br' === $xoopsModuleConfig['watermark_position']) {
410
            $x = -1;
411
            $y = -1;
412
        } elseif ('tc' === $xoopsModuleConfig['watermark_position']) {
413
            $x = 1;
414
            $y = 0;
415
        } elseif ('bc' === $xoopsModuleConfig['watermark_position']) {
416
            $x = 1;
417
            $y = -1;
418
        } elseif ('lc' === $xoopsModuleConfig['watermark_position']) {
419
            $x = 0;
420
            $y = 1;
421
        } elseif ('rc' === $xoopsModuleConfig['watermark_position']) {
422
            $x = -1;
423
            $y = 1;
424
        } elseif ('cc' === $xoopsModuleConfig['watermark_position']) {
425
            $x = 1;
426
            $y = 1;
427
        }
428
429
        $text = (0 == $xoopsModuleConfig['watermark_type']) ? $GLOBALS['xoopsUser']->getVar('uname') : $xoopsModuleConfig['watermark_text'];
430
431
        $watermarkParams = [
432
            'text'         => $text,
433
            'x'            => $x,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $x does not seem to be defined for all execution paths leading up to this point.
Loading history...
434
            'y'            => $y,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $y does not seem to be defined for all execution paths leading up to this point.
Loading history...
435
            'color'        => $xoopsModuleConfig['watermark_color'],
436
            'font'         => XOOPS_ROOT_PATH . '/modules/extgallery/fonts/' . $xoopsModuleConfig['watermark_font'],
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Extgallery\XOOPS_ROOT_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
437
            'size'         => $xoopsModuleConfig['watermark_fontsize'],
438
            'resize_first' => false,
439
            'padding'      => $xoopsModuleConfig['watermark_padding']
440
        ];
441
        $imageTransform->addText($watermarkParams);
442
    }
443
444
    /**
445
     * @param $imageTransform
446
     */
447
    public function _makeBorder(&$imageTransform)
448
    {
449
        global $xoopsModuleConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
450
451
        $borders   = [];
452
        $borders[] = [
453
            'borderWidth' => $xoopsModuleConfig['inner_border_size'],
454
            'borderColor' => $xoopsModuleConfig['inner_border_color']
455
        ];
456
        $borders[] = [
457
            'borderWidth' => $xoopsModuleConfig['outer_border_size'],
458
            'borderColor' => $xoopsModuleConfig['outer_border_color']
459
        ];
460
//        $imageTransform->addBorders($borders);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
461
        foreach ($borders as $border) {
462
            $imageTransform->addBorder($border['borderWidth'], $border['borderColor']);
463
        }
464
    }
465
466
    public function _getUploadPhotoPath()
467
    {
468
        exit('_getUploadPhotoPath() method must be defined on sub classes');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
469
    }
470
471
    /**
472
     * @param $photoName
473
     */
474
    public function _largePhotoTreatment($photoName)
475
    {
476
        global $xoopsModuleConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
477
478
        // Check if must save large photo
479
        if ($xoopsModuleConfig['save_large']) {
480
481
            // Define Graphical library path
482
            if (!defined('IMAGE_TRANSFORM_IM_PATH') && 'imagick' === $xoopsModuleConfig['graphic_lib']) {
483
                define('IMAGE_TRANSFORM_IM_PATH', $xoopsModuleConfig['graphic_lib_path']);
484
            }
485
            $imageFactory   = new \Image_Transform;
486
            $imageTransform = $imageFactory->factory($xoopsModuleConfig['graphic_lib']);
487
488
            $filePath = $this->_getUploadPhotoPath();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $filePath is correct as $this->_getUploadPhotoPath() targeting XoopsModules\Extgallery\...::_getUploadPhotoPath() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
489
            $imageTransform->load($filePath . $photoName);
490
491
            // Save large photo only if it's bigger than medium size
492
            if ($imageTransform->getImageWidth() > $xoopsModuleConfig['medium_width']
493
                || $imageTransform->getImageHeight() > $xoopsModuleConfig['medium_heigth']) {
494
495
                // Make watermark
496
                if ($xoopsModuleConfig['enable_large_watermark']) {
497
                    $this->_makeWatermark($imageTransform);
498
                }
499
500
                // Make border
501
                if ($xoopsModuleConfig['enable_large_border']) {
502
                    $this->_makeBorder($imageTransform);
503
                }
504
505
                $largeFilePath = $filePath . 'large/large_' . $photoName;
506
                $imageTransform->save($largeFilePath, '', 100);
507
                $imageTransform->free();
508
            }
509
        }
510
    }
511
512
    /**
513
     * @param             $photoName
514
     * @param null|string $filePath
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $mediumFilePath is correct as it would always require null to be passed?
Loading history...
515
     * @param null        $mediumFilePath
516
     */
517
    public function _mediumPhotoTreatment($photoName, $filePath = null, $mediumFilePath = null)
518
    {
519
        global $xoopsModuleConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
520
521
        // Define Graphical library path
522
        if (!defined('IMAGE_TRANSFORM_IM_PATH') && 'imagick' === $xoopsModuleConfig['graphic_lib']) {
523
            define('IMAGE_TRANSFORM_IM_PATH', $xoopsModuleConfig['graphic_lib_path']);
524
        }
525
        $imageFactory   = new \Image_Transform;
526
        $imageTransform = $imageFactory->factory($xoopsModuleConfig['graphic_lib']);
527
528
        if (null === $filePath) {
529
            $filePath = $this->_getUploadPhotoPath();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $filePath is correct as $this->_getUploadPhotoPath() targeting XoopsModules\Extgallery\...::_getUploadPhotoPath() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
530
        }
531
        if (null === $mediumFilePath) {
532
            $mediumFilePath = $filePath . 'medium/' . $photoName;
533
        }
534
        $imageTransform->load($filePath . $photoName);
535
536
        // Fitting image to desired size
537
        if ($xoopsModuleConfig['enable_medium_border']) {
538
            $borderSize = ($xoopsModuleConfig['inner_border_size'] * 2) + ($xoopsModuleConfig['outer_border_size'] * 2);
539
        } else {
540
            $borderSize = 0;
541
        }
542
        $imageTransform->fit($xoopsModuleConfig['medium_width'] - $borderSize, $xoopsModuleConfig['medium_heigth'] - $borderSize);
543
        $imageTransform->save($mediumFilePath, '', $xoopsModuleConfig['medium_quality']);
544
        $imageTransform->free();
545
546
        if ($xoopsModuleConfig['enable_medium_watermark'] || $xoopsModuleConfig['enable_medium_border']) {
547
            $imageTransform->load($mediumFilePath);
548
549
            // Make watermark
550
            if ($xoopsModuleConfig['enable_medium_watermark']) {
551
                $this->_makeWatermark($imageTransform);
552
            }
553
554
            // Make border
555
            if ($xoopsModuleConfig['enable_medium_border']) {
556
                $this->_makeBorder($imageTransform);
557
            }
558
559
            $imageTransform->save($mediumFilePath, '', $xoopsModuleConfig['medium_quality']);
560
            $imageTransform->free();
561
        }
562
    }
563
564
    /**
565
     * @param $photoName
566
     */
567
    public function _makeThumb($photoName)
568
    {
569
        global $xoopsModuleConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
570
571
        // Define Graphical library path
572
        if (!defined('IMAGE_TRANSFORM_IM_PATH') && 'imagick' === $xoopsModuleConfig['graphic_lib']) {
573
            define('IMAGE_TRANSFORM_IM_PATH', $xoopsModuleConfig['graphic_lib_path']);
574
        }
575
        $imageFactory   = new \Image_Transform;
576
        $imageTransform = $imageFactory->factory($xoopsModuleConfig['graphic_lib']);
577
578
        $filePath  = $this->_getUploadPhotoPath() . 'medium/' . $photoName;
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->_getUploadPhotoPath() targeting XoopsModules\Extgallery\...::_getUploadPhotoPath() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
579
        $thumbPath = $this->_getUploadPhotoPath() . 'thumb/thumb_' . $photoName;
580
581
        $imageTransform->load($filePath);
582
        $imageTransform->fit($xoopsModuleConfig['thumb_width'], $xoopsModuleConfig['thumb_heigth']);
583
        $imageTransform->save($thumbPath, '', $xoopsModuleConfig['thumb_quality']);
584
        $imageTransform->free();
585
    }
586
587
    /**
588
     * @param $photoName
589
     *
590
     * @return bool
591
     */
592
    public function _haveLargePhoto($photoName)
593
    {
594
        return file_exists($this->_getUploadPhotoPath() . 'large/large_' . $photoName);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->_getUploadPhotoPath() targeting XoopsModules\Extgallery\...::_getUploadPhotoPath() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
595
    }
596
597
    /**
598
     * @param $photoName
599
     *
600
     * @return array
601
     */
602
    public function _getImageDimension($photoName)
603
    {
604
        global $xoopsModuleConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
605
606
        // Define Graphical library path
607
        if (!defined('IMAGE_TRANSFORM_IM_PATH') && 'imagick' === $xoopsModuleConfig['graphic_lib']) {
608
            define('IMAGE_TRANSFORM_IM_PATH', $xoopsModuleConfig['graphic_lib_path']);
609
        }
610
        $imageFactory   = new \Image_Transform;
611
        $imageTransform = $imageFactory->factory($xoopsModuleConfig['graphic_lib']);
612
613
        $ret = [];
614
        if ($this->_haveLargePhoto($photoName)) {
615
            $imageTransform->load($this->_getUploadPhotoPath() . 'large/large_' . $photoName);
616
            $ret['width']  = $imageTransform->getImageWidth();
617
            $ret['height'] = $imageTransform->getImageHeight();
618
        } else {
619
            $imageTransform->load($this->_getUploadPhotoPath() . 'medium/' . $photoName);
620
            $ret['width']  = $imageTransform->getImageWidth();
621
            $ret['height'] = $imageTransform->getImageHeight();
622
        }
623
        $imageTransform->free();
624
625
        return $ret;
626
    }
627
628
    /**
629
     * @param $photoName
630
     *
631
     * @return string
632
     */
633
    public function _getAutoDescription($photoName)
634
    {
635
        global $xoopsModuleConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
636
637
        //DNPROSSI
638
        /*if ($xoopsModuleConfig['enable_longdesc']) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
639
            $newphotoname = '';
640
            $newnewphotoname = '';
641
            $patterns = array();
642
            $patterns[0] = "/-/";
643
            $patterns[1] = "/_/";
644
            $replacements = array();
645
            $replacements[0] = " ";
646
            $replacements[1] = "'";
647
            $newphotoName = substr($photoName, strpos($photoName, "-") + 1);
648
            $newphotoName = substr($newphotoName, strpos($newphotoName, "-") + 1);
649
650
            return preg_replace($patterns, $replacements, substr($newphotoName,0,-12));
651
        } else { */
652
        $matches = [];
653
        preg_match_all($xoopsModuleConfig['photoname_pattern'], substr($photoName, 0, -12), $matches);
654
        preg_match_all($xoopsModuleConfig['photoname_pattern'], $photoName, $matches);
655
656
        return implode(' ', $matches[1]);
657
        //}
658
    }
659
660
    /**
661
     * @param $fileName
662
     *
663
     * @return string
664
     */
665
    public function _makeFileName($fileName)
666
    {
667
        //DNPROSSI
668
        //$fileName = preg_replace("/[^a-zA-Z0-9()_\.-]/", "-", $fileName);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
669
        $fileName = preg_replace("/[^a-zA-Z0-9_\.-]/", '-', $fileName);
670
671
        $fileName = explode('.', $fileName);
672
        $userId   = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0;
673
674
        return $fileName[0] . '_' . $userId . '_' . substr(md5(uniqid(mt_rand(), true)), 27) . '.' . $fileName[1];
675
    }
676
677
    /**
678
     * @param $photoName
679
     *
680
     * @return float
681
     */
682
    public function _getPhotoSize($photoName)
683
    {
684
        if ($this->_haveLargePhoto($photoName)) {
685
            return $this->_getFileSize('large/large_' . $photoName);
686
        } else {
687
            return $this->_getFileSize($photoName);
688
        }
689
    }
690
691
    /**
692
     * @param $fileName
693
     *
694
     * @return float
695
     */
696
    public function _getFileSize($fileName)
697
    {
698
        return round(filesize($this->_getUploadPhotoPath() . $fileName) / 1024, 2);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->_getUploadPhotoPath() targeting XoopsModules\Extgallery\...::_getUploadPhotoPath() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
699
    }
700
701
    /**
702
     * @param $catId
703
     */
704
    public function rebuildThumbnail($catId)
705
    {
706
        $photos = $this->getAlbumPhoto($catId);
707
        foreach ($photos as $photo) {
708
            $this->_makeThumb($photo->getVar('photo_name'));
709
        }
710
    }
711
712
    /* Return Code :
713
        0 : Photo added
714
        1 : Photo pending
715
        2 : This is not an album
716
        3 : HTTP Upload error
717
        4 : File rejected
718
        5 : File chunk receive
719
        */
720
    /**
721
     * @param      $file
722
     * @param bool $checkMd5
723
     *
724
     * @return int
725
     */
726
    public function postPhotoTraitement($file, $checkMd5 = false)
727
    {
728
        //        require_once XOOPS_ROOT_PATH.'/modules/extgallery/class/photoUploader.php';
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
729
730
        $catHandler = Extgallery\Helper::getInstance()->getHandler('PublicCategory');
731
732
        $catId = (int)$_POST['cat_id'];
733
734
        // If isn't an album when stop the traitment
735
        $cat = $catHandler->getCat($catId);
736
        if (null !== $cat && (1 != $cat->getVar('nright') - $cat->getVar('nleft'))) {
737
            return 2;
738
        }
739
740
        $allowedMimeTypes = ['image/jpeg', 'image/jpg', 'image/pjpeg', 'image/gif', 'image/png', 'image/x-png'];
741
        //        $allowedMimeTypes = array('jpg/jpeg', 'image/bmp', 'image/gif', 'image/jpeg', 'image/jpg', 'image/x-png', 'image/png');
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
742
743
        $uploadDir = XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo/';
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Extgallery\XOOPS_ROOT_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
744
745
        //        $this->photoUploader = new Extgallery\PhotoUploader($uploadDir,  50000000, 5000, 5000);
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
746
        //        $this->photoUploader->checkMd5 = $checkMd5;
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
747
        //        $this->photoUploader->fetchPhoto($_FILES[$file]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
748
749
        //------------------------
750
        require_once XOOPS_ROOT_PATH . '/class/uploader.php';
751
        $this->photoUploader = new \XoopsMediaUploader($uploadDir, $allowedMimeTypes, 50000000, 5000, 5000);
0 ignored issues
show
Bug introduced by
The type XoopsMediaUploader was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
752
753
        $jupart  = isset($_POST['jupart']) ? (int)$_POST['jupart'] : 0;
754
        $jufinal = isset($_POST['jufinal']) ? (int)$_POST['jufinal'] : 1;
755
756
        if ($this->photoUploader->fetchMedia($file) && $this->photoUploader->upload()) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
757
        } else {
758
            // We got a chunk, so we don't add photo to database
759
            if ($jupart && !$jufinal) {
760
                return 5;
761
            } else {
762
                return 4;
763
            }
764
        }
765
766
        //---------------------------
767
768
        /*
769
770
                $jupart = (isset($_POST['jupart'])) ? (int) $_POST['jupart'] : 0;
771
                $jufinal = (isset($_POST['jufinal'])) ? (int) $_POST['jufinal'] : 1;
772
773
                if ($this->photoUploader->isError()) {
774
                    return 4;
775
                // We got a chunk, so we don't add photo to database
776
                } elseif ($jupart && !$jufinal) {
777
                    return 5;
778
                }
779
        */
780
781
        //DNPROSSI - add missing title and description on upload
782
        $photoTitle = '';
783
        $photoDesc  = '';
784
        $photoExtra = '';
785
        $photoTag   = '';
786
787
        if (isset($_POST['photo_title'])) {
788
            $photoTitle = $_POST['photo_title'];
789
        }
790
        if (isset($_POST['photo_desc'])) {
791
            $photoDesc = $_POST['photo_desc'];
792
        }
793
        if (isset($_POST['photo_extra'])) {
794
            $photoExtra = $_POST['photo_extra'];
795
        }
796
        if (isset($_POST['tag'])) {
797
            $photoTag = $_POST['tag'];
798
        }
799
800
        $photoStatus = $this->addLocalPhoto($catId, $this->photoUploader->getSavedFileName(), $photoTitle, $photoDesc, $photoExtra, $photoTag);
801
        /** @var Extgallery\Category $cat */
802
        $cat = $catHandler->getCat($catId);
803
        $cat->setVar('cat_isalbum', 1);
804
        $catHandler->insert($cat);
805
806
        /** @var \XoopsNotificationHandler $notificationHandler */
807
        $notificationHandler = xoops_getHandler('notification');
0 ignored issues
show
Bug introduced by
The function xoops_getHandler was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

807
        $notificationHandler = /** @scrutinizer ignore-call */ xoops_getHandler('notification');
Loading history...
808
        $extraTags           = [
809
            'X_ITEM_CAT'     => $cat->getVar('cat_name'),
810
            'X_ITEM_NBPHOTO' => 1
811
        ];
812
813
        if (1 == $photoStatus) {
814
            $extraTags['X_ITEM_URL'] = XOOPS_URL . '/modules/extgallery/public-album.php?id=' . $cat->getVar('cat_id');
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Extgallery\XOOPS_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
815
            $notificationHandler->triggerEvent('global', 0, 'new_photo', $extraTags);
816
            $notificationHandler->triggerEvent('album', $cat->getVar('cat_id'), 'new_photo_album', $extraTags);
817
818
            // Update album count
819
            if (0 == $cat->getVar('cat_nb_photo')) {
820
                $criteria = new \CriteriaCompo();
821
                $criteria->add(new \Criteria('nleft', $cat->getVar('nleft'), '<'));
822
                $criteria->add(new \Criteria('nright', $cat->getVar('nright'), '>'));
823
                $catHandler->updateFieldValue('cat_nb_album', 'cat_nb_album + 1', $criteria);
824
            }
825
826
            // Update photo count
827
            $criteria = new \CriteriaCompo();
828
            $criteria->add(new \Criteria('nleft', $cat->getVar('nleft'), '<='));
829
            $criteria->add(new \Criteria('nright', $cat->getVar('nright'), '>='));
830
            $catHandler->updateFieldValue('cat_nb_photo', 'cat_nb_photo + 1', $criteria);
831
832
            return 0;
833
        } else {
834
            $extraTags['X_ITEM_URL'] = XOOPS_URL . '/modules/extgallery/admin/photo.php';
835
            $notificationHandler->triggerEvent('global', 0, 'new_photo_pending', $extraTags);
836
837
            return 1;
838
        }
839
    }
840
841
    /**
842
     * @param        $catId
843
     * @param        $dirtyPhotoName
844
     * @param string $photoTitle
845
     * @param string $photoDesc
846
     * @param string $photoExtra
847
     * @param string $photoTag
848
     *
849
     * @return mixed
850
     */
851
    public function addLocalPhoto(
852
        $catId,
853
        $dirtyPhotoName,
854
        $photoTitle = '',
855
        $photoDesc = '',
856
        $photoExtra = '',
857
        $photoTag = ''
858
    ) {
859
        require_once XOOPS_ROOT_PATH . '/modules/extgallery/class/pear/Image/Transform.php';
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Extgallery\XOOPS_ROOT_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
860
861
        global $xoopsModuleConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
862
        $permHandler = Extgallery\PublicPermHandler::getInstance();
863
864
        // Replace all bad file name character
865
        $photoName = $this->_makeFileName($dirtyPhotoName);
866
        rename($this->_getUploadPhotoPath() . $dirtyPhotoName, $this->_getUploadPhotoPath() . $photoName);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->_getUploadPhotoPath() targeting XoopsModules\Extgallery\...::_getUploadPhotoPath() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
867
868
        //DNPROSSI - changed photo_desc to photo_title
869
        // Making auto description
870
        if ('' === $photoTitle) {
871
            $photoTitle = $this->_getAutoDescription($photoName);
872
        }
873
874
        $originalName = '';
875
        // Save original photo
876
        if ($xoopsModuleConfig['save_large'] && $xoopsModuleConfig['save_original']) {
877
            $fileName     = explode('.', $photoName);
878
            $originalName = md5(uniqid(mt_rand(), true)) . '.' . $fileName[1];
879
            copy($this->_getUploadPhotoPath() . $photoName, $this->_getUploadPhotoPath() . 'original/' . $originalName);
880
        }
881
882
        $this->_largePhotoTreatment($photoName);
883
884
        $this->_mediumPhotoTreatment($photoName);
885
886
        $this->_makeThumb($photoName);
887
888
        $imageDimension = $this->_getImageDimension($photoName);
889
890
        $userId = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0;
891
        $data   = [
892
            'cat_id'          => $catId,
893
            'photo_title'     => $photoTitle,
894
            'photo_desc'      => $photoDesc,
895
            'photo_name'      => $photoName,
896
            'photo_orig_name' => $originalName,
897
            'uid'             => $userId,
898
            'photo_size'      => $this->_getPhotoSize($photoName),
899
            'photo_res_x'     => $imageDimension['width'],
900
            'photo_res_y'     => $imageDimension['height'],
901
            'photo_date'      => time(),
902
            'photo_havelarge' => $this->_haveLargePhoto($photoName),
903
            'photo_approved'  => $permHandler->isAllowed($GLOBALS['xoopsUser'], 'public_autoapprove', $catId),
904
            'photo_extra'     => $photoExtra,
905
            'dohtml'          => $xoopsModuleConfig['allow_html']
906
        ];
907
908
        // Deleting working photo
909
        unlink($this->_getUploadPhotoPath() . $photoName);
910
911
        $this->createPhoto($data);
912
913
        if (1 == $xoopsModuleConfig['usetag'] || (is_dir('../tag') || is_dir('../../tag'))) {
914
            $newid      = $this->db->getInsertId();
915
            $tagHandler = \XoopsModules\Tag\Helper::getInstance()->getHandler('Tag'); // xoops_getModuleHandler('tag', 'tag');
0 ignored issues
show
Bug introduced by
The type XoopsModules\Tag\Helper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
916
            $tagHandler->updateByItem($photoTag, $newid, 'extgallery', 0);
917
        }
918
919
        return $data['photo_approved'];
920
    }
921
922
    /**
923
     * @param $queryArray
924
     * @param $condition
925
     * @param $limit
926
     * @param $start
927
     * @param $userId
928
     *
929
     * @return array
930
     */
931
    public function getSearchedPhoto($queryArray, $condition, $limit, $start, $userId)
932
    {
933
        $criteria = new \CriteriaCompo();
934
        if ($userId > 0) {
935
            $criteria->add(new \Criteria('uid', $userId));
936
        }
937
        $criteria->add(new \Criteria('photo_approved', 1));
938
        if (is_array($queryArray) && count($queryArray) > 0) {
939
            $subCriteria = new \CriteriaCompo();
940
            foreach ($queryArray as $keyWord) {
941
                $keyWordCriteria = new \CriteriaCompo();
942
                $keyWordCriteria->add(new \Criteria('photo_title', '%' . $keyWord . '%', 'LIKE'));
943
                $keyWordCriteria->add(new \Criteria('photo_desc', '%' . $keyWord . '%', 'LIKE'), 'OR');
944
                $keyWordCriteria->add(new \Criteria('photo_name', '%' . $keyWord . '%', 'LIKE'), 'OR');
945
                $subCriteria->add($keyWordCriteria, $condition);
946
                unset($keyWordCriteria);
947
            }
948
            $criteria->add($subCriteria);
949
        }
950
        $criteria->setStart($start);
951
        $criteria->setLimit($limit);
952
        $criteria->setSort('photo_date');
953
954
        $photos =& $this->getObjects($criteria);
955
956
        $ret = [];
957
        foreach ($photos as $photo) {
958
            if ($photo->getVar('photo_title')) {
959
                $title = $photo->getVar('photo_title');
960
            } else {
961
                $title = $photo->getVar('photo_desc');
962
            }
963
            $data  = [
964
                'image' => 'assets/images/extgallery-posticon.gif',
965
                'link'  => 'public-photo.php?photoId=' . $photo->getVar('photo_id'),
966
                'title' => $title,
967
                'time'  => $photo->getVar('photo_date'),
968
                'uid'   => $photo->getVar('uid')
969
            ];
970
            $ret[] = $data;
971
        }
972
973
        return $ret;
974
    }
975
976
    /**
977
     * @return array
978
     */
979
    public function getPendingPhoto()
980
    {
981
        $criteria = new \Criteria('photo_approved', 0);
982
983
        return $this->getObjects($criteria);
984
    }
985
986
    /**
987
     * @param $criteria
988
     * @param $data
989
     */
990
    public function _addInCriteria(&$criteria, $data)
991
    {
992
        $count = count($data);
993
        if ($count > 0) {
994
            $in = '(' . $data[0];
995
            array_shift($data);
996
            foreach ($data as $elmt) {
997
                $in .= ',' . $elmt;
998
            }
999
            $in .= ')';
1000
            $criteria->add(new \Criteria('cat_id', $in, 'IN'));
1001
        }
1002
    }
1003
1004
    /**
1005
     * @param $param
1006
     *
1007
     * @return array
1008
     */
1009
    public function getRandomPhoto($param)
1010
    {
1011
        $catHandler = Extgallery\Helper::getInstance()->getHandler('PublicCategory');
1012
        $criteria   = new \CriteriaCompo();
1013
        if (null !== $catHandler->getCatRestrictCriteria()) {
1014
            $criteria->add($catHandler->getCatRestrictCriteria());
1015
        }
1016
        $criteria->add(new \Criteria('photo_approved', 1));
1017
        $this->_addInCriteria($criteria, $param['cat']);
1018
        $criteria->setSort('RAND()');
1019
        $criteria->setLimit($param['limit']);
1020
1021
        return $this->getObjects($criteria);
1022
    }
1023
1024
    /**
1025
     * @param $param
1026
     *
1027
     * @return array
1028
     */
1029
    public function getLastPhoto($param)
1030
    {
1031
        $catHandler = Extgallery\Helper::getInstance()->getHandler('PublicCategory');
1032
1033
        $criteria = new \CriteriaCompo();
1034
        $criteria->add($catHandler->getCatRestrictCriteria());
1035
        $criteria->add(new \Criteria('photo_approved', 1));
1036
        $this->_addInCriteria($criteria, $param['cat']);
1037
        $criteria->setSort('photo_date');
1038
        $criteria->setOrder('DESC');
1039
        $criteria->setLimit($param['limit']);
1040
1041
        return $this->getObjects($criteria);
1042
    }
1043
1044
    /**
1045
     * @param $param
1046
     *
1047
     * @return array
1048
     */
1049
    public function getTopViewPhoto($param)
1050
    {
1051
        $catHandler = Extgallery\Helper::getInstance()->getHandler('PublicCategory');
1052
1053
        $criteria = new \CriteriaCompo();
1054
        $criteria->add($catHandler->getCatRestrictCriteria());
1055
        $criteria->add(new \Criteria('photo_approved', 1));
1056
        $this->_addInCriteria($criteria, $param['cat']);
1057
        $criteria->setSort('photo_hits');
1058
        $criteria->setOrder('DESC');
1059
        $criteria->setLimit($param['limit']);
1060
1061
        return $this->getObjects($criteria);
1062
    }
1063
1064
    /**
1065
     * @param $param
1066
     *
1067
     * @return array
1068
     */
1069
    public function getTopRatedPhoto($param)
1070
    {
1071
        $catHandler = Extgallery\Helper::getInstance()->getHandler('PublicCategory');
1072
1073
        $criteria = new \CriteriaCompo();
1074
        $criteria->add($catHandler->getCatRestrictCriteria());
1075
        $criteria->add(new \Criteria('photo_approved', 1));
1076
        $this->_addInCriteria($criteria, $param['cat']);
1077
        $criteria->setSort('photo_rating');
1078
        $criteria->setOrder('DESC');
1079
        $criteria->setLimit($param['limit']);
1080
1081
        return $this->getObjects($criteria);
1082
    }
1083
1084
    /**
1085
     * @param $param
1086
     *
1087
     * @return array
1088
     */
1089
    public function getTopEcardPhoto($param)
1090
    {
1091
        $catHandler = Extgallery\Helper::getInstance()->getHandler('PublicCategory');
1092
1093
        $criteria = new \CriteriaCompo();
1094
        $criteria->add($catHandler->getCatRestrictCriteria());
1095
        $criteria->add(new \Criteria('photo_approved', 1));
1096
        $this->_addInCriteria($criteria, $param['cat']);
1097
        $criteria->setSort('photo_ecard');
1098
        $criteria->setOrder('DESC');
1099
        $criteria->setLimit($param['limit']);
1100
1101
        return $this->getObjects($criteria);
1102
    }
1103
1104
    /**
1105
     * @param $param
1106
     */
1107
    public function getTopSubmitter($param)
1108
    {
1109
        $catHandler = Extgallery\Helper::getInstance()->getHandler('PublicCategory');
0 ignored issues
show
Unused Code introduced by
The assignment to $catHandler is dead and can be removed.
Loading history...
1110
1111
        $criteria = new \Criteria();
1112
        $this->_addInCriteria($criteria, $param['cat']);
1113
1114
        echo $criteria->renderWhere();
1115
    }
1116
1117
    /**
1118
     * @return mixed
1119
     */
1120
    public function getInsertId()
1121
    {
1122
        return $this->db->getInsertId();
1123
    }
1124
}
1125