Completed
Branch master (923121)
by Michael
05:29 queued 02:40
created

Xoopsjlm_picturesHandler::receivePicture()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 48
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 48
rs 9.125
cc 3
eloc 32
nc 3
nop 9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 42 and the first side effect is on line 31.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/*
3
-------------------------------------------------------------------------
4
                     ADSLIGHT 2 : Module for Xoops
5
6
        Redesigned and ameliorate By Luc Bizet user at www.frxoops.org
7
        Started with the Classifieds module and made MANY changes
8
        Website : http://www.luc-bizet.fr
9
        Contact : [email protected]
10
-------------------------------------------------------------------------
11
             Original credits below Version History
12
##########################################################################
13
#                    Classified Module for Xoops                         #
14
#  By John Mordo user jlm69 at www.xoops.org and www.jlmzone.com         #
15
#      Started with the MyAds module and made MANY changes               #
16
##########################################################################
17
 Original Author: Pascal Le Boustouller
18
 Author Website : [email protected]
19
 Licence Type   : GPL
20
-------------------------------------------------------------------------
21
*/
22
23
/**
24
 * Protection against inclusion outside the site
25
 */
26
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
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...
27
28
/**
29
 * Includes of form objects and uploader
30
 */
31
include_once XOOPS_ROOT_PATH . '/class/uploader.php';
32
include_once XOOPS_ROOT_PATH . '/kernel/object.php';
33
include_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
34
include_once XOOPS_ROOT_PATH . '/kernel/object.php';
35
include_once XOOPS_ROOT_PATH . '/modules/adslight/include/functions.php';
36
37
/**
38
 * light_pictures class.
39
 * $this class is responsible for providing data access mechanisms to the data source
40
 * of XOOPS user class objects.
41
 */
42
class jlm_pictures extends XoopsObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
43
{
44
    public $db;
45
    // constructor
46
    /**
47
     * @param null $id
48
     * @param null $lid
49
     */
50
    public function __construct($id = null, $lid = null)
51
    {
52
        $this->db = XoopsDatabaseFactory::getDatabaseConnection();
53
        $this->initVar('cod_img', XOBJ_DTYPE_INT, null, false, 10);
54
        $this->initVar('title', XOBJ_DTYPE_TXTBOX, null, false);
55
        $this->initVar('date_added', XOBJ_DTYPE_TXTBOX, null, false);
56
        $this->initVar('date_modified', XOBJ_DTYPE_TXTBOX, null, false);
57
        $this->initVar('lid', XOBJ_DTYPE_INT, null, false, 10);
58
        $this->initVar('uid_owner', XOBJ_DTYPE_TXTBOX, null, false);
59
        $this->initVar('url', XOBJ_DTYPE_TXTBOX, null, false);
60
        if (!empty($lid)) {
61
            if (is_array($lid)) {
62
                $this->assignVars($lid);
63
            } else {
64
                $this->load((int)$lid);
65
            }
66
        } else {
67
            $this->setNew();
68
        }
69
    }
70
71
    /**
72
     * @param $id
73
     */
74
    public function load($id)
75
    {
76
        global $moduleDirName;
1 ignored issue
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...
77
        $sql   = 'SELECT * FROM ' . $this->db->prefix('adslight_pictures') . ' WHERE cod_img=' . $id . '';
78
        $myrow = $this->db->fetchArray($this->db->query($sql));
79
        $this->assignVars($myrow);
80
        if (!$myrow) {
81
            $this->setNew();
82
        }
83
    }
84
85
    /**
86
     * @param array  $criteria
87
     * @param bool   $asobject
88
     * @param string $sort
89
     * @param string $order
90
     * @param int    $limit
91
     * @param int    $start
92
     *
93
     * @return array
94
     */
95
    public function getAll_pictures($criteria = array(), $asobject = false, $sort = 'cod_img', $order = 'ASC', $limit = 0, $start = 0)
96
    {
97
        global $moduleDirName;
1 ignored issue
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...
98
        $db          = XoopsDatabaseFactory::getDatabaseConnection();
99
        $ret         = array();
100
        $where_query = '';
101
        if (is_array($criteria) && count($criteria) > 0) {
102
            $where_query = ' WHERE';
103
            foreach ($criteria as $c) {
104
                $where_query .= " $c AND";
105
            }
106
            $where_query = substr($where_query, 0, -4);
107
        } elseif (!is_array($criteria) && $criteria) {
108
            $where_query = " WHERE {$criteria}";
109
        }
110
        if (!$asobject) {
111
            $sql    = 'SELECT cod_img FROM ' . $db->prefix('adslight_pictures') . "$where_query ORDER BY $sort $order";
112
            $result = $db->query($sql, $limit, $start);
113
            while ($myrow = $db->fetchArray($result)) {
114
                $ret[] = $myrow['jlm_pictures_id'];
115
            }
116
        } else {
117
            $sql    = 'SELECT * FROM ' . $db->prefix('adslight_pictures') . "$where_query ORDER BY $sort $order";
118
            $result = $db->query($sql, $limit, $start);
119
            while ($myrow = $db->fetchArray($result)) {
120
                $ret[] = new jlm_pictures($myrow);
121
            }
122
        }
123
124
        return $ret;
125
    }
126
}
127
128
// -------------------------------------------------------------------------
129
// ------------------light_pictures user handler class -------------------
130
// -------------------------------------------------------------------------
131
/**
132
 * light_pictureshandler class.
133
 * This class provides simple mechanism for light_pictures object and generate forms for inclusion etc
134
 */
135
class Xoopsjlm_picturesHandler extends XoopsObjectHandler
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
136
{
137
    /**
138
     * create a new light_pictures
139
     *
140
     * @param  bool $isNew flag the new objects as "new"?
141
     * @return object light_pictures
142
     */
143
    public function create($isNew = true)
144
    {
145
        $jlm_pictures = new jlm_pictures();
146
        if ($isNew) {
147
            $jlm_pictures->setNew();
148
        } else {
149
            $jlm_pictures->unsetNew();
150
        }
151
152
        return $jlm_pictures;
153
    }
154
155
    /**
156
     * retrieve a light_pictures
157
     *
158
     * @param int $id of the light_pictures
159
     * @param     $lid
160
     *
161
     * @return mixed reference to the {@link light_pictures} object, FALSE if failed
162
     */
163
    public function get($id, $lid = null)
164
    {
165
        global $moduleDirName;
1 ignored issue
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...
166
167
        $sql = 'SELECT * FROM ' . $this->db->prefix('adslight_pictures') . ' WHERE cod_img=' . $id . ' and lid=' . $lid . '';
168
        if (!$result = $this->db->query($sql)) {
169
            return false;
170
        }
171
        $numrows = $this->db->getRowsNum($result);
172
        if ($numrows == 1) {
173
            $jlm_pictures = new jlm_pictures();
174
            $jlm_pictures->assignVars($this->db->fetchArray($result));
175
176
            return $jlm_pictures;
177
        }
178
179
        return false;
180
    }
181
182
    /**
183
     * insert a new light_pictures in the database
184
     *
185
     * @param XoopsObject $jlm_pictures
186
     * @param bool        $force
187
     * @internal param object $light_pictures reference to the {@link light_pictures} object object
188
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
189
     */
190
    public function insert(XoopsObject $jlm_pictures, $force = false)
191
    {
192
        global $xoopsConfig, $lid, $moduleDirName;
1 ignored issue
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...
193
        if (get_class($jlm_pictures) !== 'jlm_pictures') {
194
            return false;
195
        }
196
        if (!$jlm_pictures->isDirty()) {
197
            return true;
198
        }
199
        if (!$jlm_pictures->cleanVars()) {
200
            return false;
201
        }
202
        foreach ($jlm_pictures->cleanVars as $k => $v) {
203
            ${$k} = $v;
204
        }
205
        $now = time();
206
        if ($jlm_pictures->isNew()) {
207
            // ajout/modification d'un jlm_pictures
208
            $jlm_pictures = new jlm_pictures();
209
210
            $format = 'INSERT INTO %s (cod_img, title, date_added, date_modified, lid, uid_owner, url)';
211
            $format .= 'VALUES (%u, %s, %s, %s, %s, %s, %s)';
212
            $sql   = sprintf($format, $this->db->prefix('adslight_pictures'), $cod_img, $this->db->quoteString($title), $now, $now, $this->db->quoteString($lid), $this->db->quoteString($uid_owner),
0 ignored issues
show
Bug introduced by
The variable $cod_img seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
Bug introduced by
The variable $title does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $uid_owner does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
213
                             $this->db->quoteString($url));
0 ignored issues
show
Bug introduced by
The variable $url does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
214
            $force = true;
215
        } else {
216
            $format = 'UPDATE %s SET ';
217
            $format .= 'cod_img=%u, title=%s, date_added=%s, date_modified=%s, lid=%s, uid_owner=%s, url=%s';
218
            $format .= ' WHERE cod_img = %u';
219
            $sql = sprintf($format, $this->db->prefix('adslight_pictures'), $cod_img, $this->db->quoteString($title), $now, $now, $this->db->quoteString($lid), $this->db->quoteString($uid_owner),
0 ignored issues
show
Bug introduced by
The variable $cod_img seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
220
                           $this->db->quoteString($url), $cod_img);
0 ignored issues
show
Bug introduced by
The variable $cod_img seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
221
        }
222 View Code Duplication
        if (false != $force) {
1 ignored issue
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
223
            $result = $this->db->queryF($sql);
224
        } else {
225
            $result = $this->db->query($sql);
226
        }
227
        if (!$result) {
228
            return false;
229
        }
230
        if (empty($cod_img)) {
0 ignored issues
show
Bug introduced by
The variable $cod_img seems only to be defined at a later point. As such the call to empty() seems to always evaluate to true.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
231
            $cod_img = $this->db->getInsertId();
232
        }
233
        $jlm_pictures->assignVar('cod_img', $cod_img);
234
        $jlm_pictures->assignVar('lid', $lid);
235
        $jlm_pictures->assignVar('url', $url);
236
237
        return true;
238
    }
239
240
    /**
241
     * delete a jlm_pictures from the database
242
     *
243
     * @param  XoopsObject $jlm_pictures reference to the jlm_pictures to delete
244
     * @param  bool        $force
245
     * @return bool        FALSE if failed.
246
     */
247
    public function delete(XoopsObject $jlm_pictures, $force = false)
248
    {
249
        global $moduleDirName;
1 ignored issue
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...
250
251
        if (get_class($jlm_pictures) !== 'jlm_pictures') {
252
            return false;
253
        }
254
        $sql = sprintf('DELETE FROM %s WHERE cod_img = %u', $this->db->prefix('adslight_pictures'), $jlm_pictures->getVar('cod_img'));
255 View Code Duplication
        if (false != $force) {
1 ignored issue
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
256
            $result = $this->db->queryF($sql);
257
        } else {
258
            $result = $this->db->query($sql);
259
        }
260
        if (!$result) {
261
            return false;
262
        }
263
264
        return true;
265
    }
266
267
    /**
268
     * retrieve jlm_pictures from the database
269
     *
270
     * @param  object $criteria  {@link CriteriaElement} conditions to be met
0 ignored issues
show
Documentation introduced by
Should the type for parameter $criteria not be object|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
271
     * @param  bool   $id_as_key use the UID as key for the array?
272
     * @return array  array of {@link jlm_pictures} objects
273
     */
274
    public function &getObjects($criteria = null, $id_as_key = false)
275
    {
276
        global $moduleDirName;
1 ignored issue
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...
277
278
        $ret   = array();
279
        $limit = $start = 0;
280
        $sql   = 'SELECT * FROM ' . $this->db->prefix('adslight_pictures');
281
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
282
            $sql .= ' ' . $criteria->renderWhere();
283
            if ($criteria->getSort() != '') {
284
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
285
            }
286
            $limit = $criteria->getLimit();
287
            $start = $criteria->getStart();
288
        }
289
        $result = $this->db->query($sql, $limit, $start);
290
        if (!$result) {
291
            return $ret;
292
        }
293
        while ($myrow = $this->db->fetchArray($result)) {
294
            $jlm_pictures = new jlm_pictures();
295
            $jlm_pictures->assignVars($myrow);
296
            if (!$id_as_key) {
297
                $ret[] =& $jlm_pictures;
298
            } else {
299
                $ret[$myrow['cod_img']] =& $jlm_pictures;
300
            }
301
            unset($jlm_pictures);
302
        }
303
304
        return $ret;
305
    }
306
307
    /**
308
     * count jlm_pictures matching a condition
309
     *
310
     * @param  object $criteria {@link CriteriaElement} to match
0 ignored issues
show
Documentation introduced by
Should the type for parameter $criteria not be object|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
311
     * @return int    count of jlm_pictures
312
     */
313
    public function getCount($criteria = null)
314
    {
315
        global $moduleDirName;
1 ignored issue
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...
316
317
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('adslight_pictures');
318
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
319
            $sql .= ' ' . $criteria->renderWhere();
320
        }
321
        $result = $this->db->query($sql);
322
        if (!$result) {
323
            return 0;
324
        }
325
        list($count) = $this->db->fetchRow($result);
326
327
        return $count;
328
    }
329
330
    /**
331
     * delete jlm_pictures matching a set of conditions
332
     *
333
     * @param  object $criteria {@link CriteriaElement}
0 ignored issues
show
Documentation introduced by
Should the type for parameter $criteria not be object|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
334
     * @return bool   FALSE if deletion failed
335
     */
336
    public function deleteAll($criteria = null)
337
    {
338
        global $moduleDirName;
1 ignored issue
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...
339
        $sql = 'DELETE FROM ' . $this->db->prefix('adslight_pictures');
340
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
341
            $sql .= ' ' . $criteria->renderWhere();
342
        }
343
        if (!$result = $this->db->query($sql)) {
344
            return false;
345
        }
346
347
        return true;
348
    }
349
350
    /**
351
     * Render a form to send pictures
352
     *
353
     * @param         $uid
354
     * @param         $lid
355
     * @param  int    $maxbytes the maximum size of a picture
356
     * @param  object $xoopsTpl the one in which the form will be rendered
357
     * @return bool   TRUE
358
     *
359
     * obs: Some functions wont work on php 4 so edit lines down under acording to your version
360
     */
361
    public function renderFormSubmit($uid, $lid, $maxbytes, $xoopsTpl)
1 ignored issue
show
Coding Style introduced by
renderFormSubmit uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
362
    {
363
        global $moduleDirName, $main_lang, $xoopsUser;
1 ignored issue
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...
364
        $form       =
365
            new XoopsThemeForm(constant('_ADSLIGHT_SUBMIT_PIC_TITLE'), 'form_picture', '' . XOOPS_URL . "/modules/adslight/add_photo.php?lid=$lid&uid=" . $xoopsUser->getVar('uid') . '', 'post', true);
366
        $field_url  = new XoopsFormFile(constant('_ADSLIGHT_SELECT_PHOTO'), 'sel_photo', 2000000);
367
        $field_desc = new XoopsFormText(constant('_ADSLIGHT_CAPTION'), 'caption', 35, 55);
368
        $form->setExtra('enctype="multipart/form-data"');
369
        $button_send   = new XoopsFormButton('', 'submit_button', constant('_ADSLIGHT_UPLOADPICTURE'), 'submit');
370
        $field_warning = new XoopsFormLabel(sprintf(constant('_ADSLIGHT_YOUCANUPLOAD'), $maxbytes / 1024));
371
        $field_lid     = new XoopsFormHidden('lid', $lid);
372
        $field_uid     = new XoopsFormHidden('uid', $uid);
373
        /**
374
         * Check if using Xoops or XoopsCube (by jlm69)
375
         */
376
377
        $xCube = false;
378
        if (preg_match('/^XOOPS Cube/', XOOPS_VERSION)) { // XOOPS Cube 2.1x
379
            $xCube = true;
380
        }
381
382
        /**
383
         * Verify Ticket (by jlm69)
384
         * If your site is XoopsCube it uses $xoopsGTicket for the token.
385
         * If your site is Xoops it uses xoopsSecurity for the token.
386
         */
387
388 View Code Duplication
        if ($xCube) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
389
            $GLOBALS['xoopsGTicket']->addTicketXoopsFormElement($form, __LINE__, 1800, 'token');
390
        } else {
391
            $field_token = $GLOBALS['xoopsSecurity']->getTokenHTML();
392
        }
393
        $form->addElement($field_warning);
394
        $form->addElement($field_url, true);
395
        $form->addElement($field_desc, true);
396
        $form->addElement($field_lid, true);
397
        $form->addElement($field_uid, true);
398
399
        $form->addElement($field_token, true);
0 ignored issues
show
Bug introduced by
The variable $field_token does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
400
401
        $form->addElement($button_send);
402
        if (str_replace('.', '', PHP_VERSION) > 499) {
403
            $form->assign($xoopsTpl);
404
        } else {
405
            $form->display();
406
        }
407
408
        return true;
409
    }
410
411
    /**
412
     * Render a form to edit the description of the pictures
413
     *
414
     * @param  string $caption  The description of the picture
415
     * @param  int    $cod_img  the id of the image in database
416
     * @param  text   $filename the url to the thumb of the image so it can be displayed
417
     * @return bool   TRUE
418
     */
419
    public function renderFormEdit($caption, $cod_img, $filename)
1 ignored issue
show
Coding Style introduced by
renderFormEdit uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
420
    {
421
        global $moduleDirName, $main_lang;
1 ignored issue
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...
422
423
        $form       = new XoopsThemeForm(_ADSLIGHT_EDIT_CAPTION, 'form_picture', 'editdesc.php', 'post', true);
424
        $field_desc = new XoopsFormText($caption, 'caption', 35, 55);
425
        $form->setExtra('enctype="multipart/form-data"');
426
        $button_send   = new XoopsFormButton('' . _ADSLIGHT_EDIT . '', 'submit_button', 'Submit', 'submit');
427
        $field_warning = new XoopsFormLabel("<img src='" . $filename . "' alt='sssss'>");
428
        $field_cod_img = new XoopsFormHidden('cod_img', $cod_img);
429
        //  $field_lid = new XoopsFormHidden("lid",$lid);
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...
430
        $field_marker = new XoopsFormHidden('marker', 1);
431
432
        /**
433
         * Check if using Xoops or XoopsCube (by jlm69)
434
         * Right now Xoops does not have a directory called preload, Xoops Cube does.
435
         * If this finds a diectory called preload in the Xoops Root folder $xCube=true.
436
         * This could change if Xoops adds a Directory called preload
437
         */
438
439
        $xCube   = false;
0 ignored issues
show
Unused Code introduced by
$xCube is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
440
        $preload = XOOPS_ROOT_PATH . '/preload';
441
        if (is_dir($preload)) {
442
            $xCube = true;
0 ignored issues
show
Unused Code introduced by
$xCube is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
443
        }
444
445
        /**
446
         * Verify Ticket (by jlm69)
447
         * If your site is XoopsCube it uses $xoopsGTicket for the token.
448
         * If your site is Xoops it uses xoopsSecurity for the token.
449
         */
450
451 View Code Duplication
        if ($xCube = true) {
1 ignored issue
show
Unused Code introduced by
$xCube is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
452
            $GLOBALS['xoopsGTicket']->addTicketXoopsFormElement($form, __LINE__, 1800, 'token');
453
        } else {
454
            $field_token = $GLOBALS['xoopsSecurity']->getTokenHTML();
455
        }
456
457
        $form->addElement($field_warning);
458
        $form->addElement($field_desc);
459
        $form->addElement($field_cod_img);
460
        $form->addElement($field_marker);
461
        $form->addElement($field_token);
0 ignored issues
show
Bug introduced by
The variable $field_token does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
462
        $form->addElement($button_send);
463
        $form->display();
464
465
        return true;
466
    }
467
468
    /**
469
     * Upload the file and Save into database
470
     *
471
     * @param  text $title         A litle description of the file
472
     * @param  text $path_upload   The path to where the file should be uploaded
473
     * @param  int  $thumbwidth    the width in pixels that the thumbnail will have
474
     * @param  int  $thumbheight   the height in pixels that the thumbnail will have
475
     * @param  int  $pictwidth     the width in pixels that the pic will have
476
     * @param  int  $pictheight    the height in pixels that the pic will have
477
     * @param  int  $maxfilebytes  the maximum size a file can have to be uploaded in bytes
478
     * @param  int  $maxfilewidth  the maximum width in pixels that a pic can have
479
     * @param  int  $maxfileheight the maximum height in pixels that a pic can have
480
     * @return bool FALSE if upload fails or database fails
481
     */
482
    public function receivePicture($title, $path_upload, $thumbwidth, $thumbheight, $pictwidth, $pictheight, $maxfilebytes, $maxfilewidth, $maxfileheight)
0 ignored issues
show
Coding Style introduced by
receivePicture uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
receivePicture uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
483
    {
484
        global $xoopsUser, $xoopsDB, $_POST, $_FILES, $lid;
1 ignored issue
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...
485
        //busca id do user logado
486
        $uid = $xoopsUser->getVar('uid');
0 ignored issues
show
Unused Code introduced by
$uid is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
487
        $lid = $_POST['lid'];
488
        //create a hash so it does not erase another file
489
        $hash1 = time();
490
        $hash  = substr($hash1, 0, 4);
0 ignored issues
show
Unused Code introduced by
$hash is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
491
        // mimetypes and settings put this in admin part later
492
        $allowed_mimetypes = array('image/jpeg', 'image/gif');
493
        $maxfilesize       = $maxfilebytes;
494
        // create the object to upload
495
        $uploader = new XoopsMediaUploader($path_upload, $allowed_mimetypes, $maxfilesize, $maxfilewidth, $maxfileheight);
496
        // fetch the media
497
        if ($uploader->fetchMedia($_POST['xoops_upload_file'][0])) {
498
            //lets create a name for it
499
            $uploader->setPrefix('pic_' . $lid . '_');
500
            //now let s upload the file
501
            if (!$uploader->upload()) {
502
                // if there are errors lets return them
503
                echo "<div style=\"color:#FF0000; background-color:#FFEAF4; border-color:#FF0000; border-width:thick; border-style:solid; text-align:center\"><p>" .
504
                     $uploader->getErrors() .
505
                     '</p></div>';
506
507
                return false;
508
            } else {
509
                // now let s create a new object picture and set its variables
510
                $picture = $this->create();
511
                $url     = $uploader->getSavedFileName();
512
                $picture->setVar('url', $url);
513
                $picture->setVar('title', $title);
514
                $uid = $xoopsUser->getVar('uid');
515
                $lid = $lid;
0 ignored issues
show
Bug introduced by
Why assign $lid to itself?

This checks looks for cases where a variable has been assigned to itself.

This assignement can be removed without consequences.

Loading history...
516
                $picture->setVar('lid', $lid);
517
                $picture->setVar('uid_owner', $uid);
518
                $this->insert($picture);
519
                $saved_destination = $uploader->getSavedDestination();
520
                $this->resizeImage($saved_destination, $thumbwidth, $thumbheight, $pictwidth, $pictheight, $path_upload);
521
            }
522
        } else {
523
            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>';
524
525
            return false;
526
        }
527
528
        return true;
529
    }
530
531
    /**
532
     * Resize a picture and save it to $path_upload
533
     *
534
     * @param  text $img         the path to the file
535
     * @param  text $path_upload The path to where the files should be saved after resizing
536
     * @param  int  $thumbwidth  the width in pixels that the thumbnail will have
537
     * @param  int  $thumbheight the height in pixels that the thumbnail will have
538
     * @param  int  $pictwidth   the width in pixels that the pic will have
539
     * @param  int  $pictheight  the height in pixels that the pic will have
540
     * @return nothing
0 ignored issues
show
Documentation introduced by
Should the return type not be nothing|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
541
     */
542
    public function resizeImage($img, $thumbwidth, $thumbheight, $pictwidth, $pictheight, $path_upload)
543
    {
544
        $img2   = $img;
545
        $path   = pathinfo($img);
546
        $img    = imagecreatefromjpeg($img);
547
        $xratio = $thumbwidth / imagesx($img);
548
        $yratio = $thumbheight / imagesy($img);
549 View Code Duplication
        if ($xratio < 1 || $yratio < 1) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
550
            if ($xratio < $yratio) {
551
                $resized = imagecreatetruecolor($thumbwidth, floor(imagesy($img) * $xratio));
552
            } else {
553
                $resized = imagecreatetruecolor(floor(imagesx($img) * $yratio), $thumbheight);
554
            }
555
            imagecopyresampled($resized, $img, 0, 0, 0, 0, imagesx($resized) + 1, imagesy($resized) + 1, imagesx($img), imagesy($img));
556
            imagejpeg($resized, $path_upload . '/thumbs/thumb_' . $path['basename']);
557
            imagedestroy($resized);
558
        } else {
559
            imagejpeg($img, $path_upload . '/thumbs/thumb_' . $path['basename']);
560
        }
561
        imagedestroy($img);
562
        $path2   = pathinfo($img2);
563
        $img2    = imagecreatefromjpeg($img2);
564
        $xratio2 = $pictwidth / imagesx($img2);
565
        $yratio2 = $pictheight / imagesy($img2);
566 View Code Duplication
        if ($xratio2 < 1 || $yratio2 < 1) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
567
            if ($xratio2 < $yratio2) {
568
                $resized2 = imagecreatetruecolor($pictwidth, floor(imagesy($img2) * $xratio2));
569
            } else {
570
                $resized2 = imagecreatetruecolor(floor(imagesx($img2) * $yratio2), $pictheight);
571
            }
572
            imagecopyresampled($resized2, $img2, 0, 0, 0, 0, imagesx($resized2) + 1, imagesy($resized2) + 1, imagesx($img2), imagesy($img2));
573
            imagejpeg($resized2, $path_upload . '/midsize/resized_' . $path2['basename']);
574
            imagedestroy($resized2);
575
        } else {
576
            imagejpeg($img2, $path_upload . '/midsize/resized_' . $path2['basename']);
577
        }
578
        imagedestroy($img2);
579
    }
580
}
581