Completed
Push — master ( 3024c9...954431 )
by Michael
04:21
created

search.inc.php ➔ xtubeCheckSearchGroups()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 13

Duplication

Lines 22
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 13
c 2
b 0
f 0
nc 6
nop 3
dl 22
loc 22
rs 8.9197
1
<?php
2
3
/**
4
 * Module: XoopsTube
5
 *
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 *
10
 * PHP version 5
11
 *
12
 * @category        Module
13
 * @package         Xoopstube
14
 * @author          XOOPS Development Team
15
 * @copyright       2001-2016 XOOPS Project (http://xoops.org)
16
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
17
 * @link            http://xoops.org/
18
 * @since           1.0.6
19
 *
20
 * @param int    $cid
21
 * @param string $permType
22
 * @param bool   $redirect
23
 *
24
 * @return bool
25
 */
26
27 View Code Duplication
function xtubeCheckSearchGroups($cid = 0, $permType = 'XTubeCatPerm', $redirect = false)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
Coding Style introduced by
xtubeCheckSearchGroups 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...
28
{
29
    $moduleDirName = basename(dirname(__DIR__));
30
    //    $modulePath = dirname(__DIR__);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
31
32
    $groups       = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : XOOPS_GROUP_ANONYMOUS;
33
    $gpermHandler = xoops_getHandler('groupperm');
34
35
    $moduleHandler = xoops_getHandler('module');
36
    $module        = $moduleHandler->getByDirname($moduleDirName);
37
38
    if (!$gpermHandler->checkRight($permType, $cid, $groups, $module->getVar('mid'))) {
39
        if (false === $redirect) {
40
            return false;
41
        } else {
42
            redirect_header('index.php', 3, _NOPERM);
43
        }
44
    }
45
    unset($module);
46
47
    return true;
48
}
49
50
/**
51
 * @param $queryarray
52
 * @param $andor
53
 * @param $limit
54
 * @param $offset
55
 * @param $userid
56
 *
57
 * @return array
58
 */
59
function xtubeSearch($queryarray, $andor, $limit, $offset, $userid)
0 ignored issues
show
Coding Style introduced by
xtubeSearch 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...
60
{
61
    $sql    = 'SELECT cid, pid FROM ' . $GLOBALS['xoopsDB']->prefix('xoopstube_cat');
62
    $result = $GLOBALS['xoopsDB']->query($sql);
63
    while (false !== ($_search_group_check = $GLOBALS['xoopsDB']->fetchArray($result))) {
64
        $_search_check_array[$_search_group_check['cid']] = $_search_group_check;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$_search_check_array was never initialized. Although not strictly required by PHP, it is generally a good practice to add $_search_check_array = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
65
    }
66
67
    $sql = 'SELECT lid, cid, title, submitter, published, description FROM ' . $GLOBALS['xoopsDB']->prefix('xoopstube_videos');
68
    $sql .= ' WHERE published > 0 AND published <= ' . time() . ' AND ( expired = 0 OR expired > ' . time() . ') AND offline = 0 AND cid > 0';
69
70
    if ($userid !== 0) {
71
        $sql .= ' AND submitter=' . $userid . ' ';
72
    }
73
74
    // because count() returns 1 even if a supplied variable
75
    // is not an array, we must check if $querryarray is really an array
76
    if (is_array($queryarray) && $count = count($queryarray)) {
77
        $sql .= " AND ((title LIKE LOWER('%$queryarray[0]%') OR LOWER(description) LIKE LOWER('%$queryarray[0]%'))";
78
        for ($i = 1; $i < $count; ++$i) {
79
            $sql .= " $andor ";
80
            $sql .= "(title LIKE LOWER('%$queryarray[$i]%') OR LOWER(description) LIKE LOWER('%$queryarray[$i]%'))";
81
        }
82
        $sql .= ') ';
83
    }
84
    $sql .= 'ORDER BY published DESC';
85
    $result = $GLOBALS['xoopsDB']->query($sql, $limit, $offset);
86
    $ret    = array();
87
    $i      = 0;
88
89
    while (false !== ($myrow = $GLOBALS['xoopsDB']->fetchArray($result))) {
90
        // Following is commented out because it can cause a conflict with View Account function (userinfo.php)
91
        //        if ( false == xtubeCheckSearchGroups( $myrow['cid'] ) ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
92
        //            continue;
93
        //        }
94
        $ret[$i]['image'] = 'assets/images/size2.gif';
95
        $ret[$i]['link']  = 'singlevideo.php?cid=' . $myrow['cid'] . '&amp;lid=' . $myrow['lid'];
96
        $ret[$i]['title'] = $myrow['title'];
97
        $ret[$i]['time']  = $myrow['published'];
98
        $ret[$i]['uid']   = $myrow['submitter'];
99
        ++$i;
100
    }
101
    unset($_search_check_array);
102
103
    return $ret;
104
}
105