Completed
Push — master ( 8ca430...3024c9 )
by Michael
03:12
created

search.inc.php ➔ xtubeSearch()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 48
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 29
c 1
b 0
f 0
nc 16
nop 5
dl 0
loc 48
rs 6.7272
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-2013 The XOOPS Project
16
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
17
 * @version         $Id$
18
 * @link            http://sourceforge.net/projects/xoops/
19
 * @since           1.0.6
20
 *
21
 * @param int    $cid
22
 * @param string $permType
23
 * @param bool   $redirect
24
 *
25
 * @return bool
26
 */
0 ignored issues
show
Documentation introduced by
Should the return type not be null|boolean?

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...
27
28 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...
29
{
30
    global $xoopsUser;
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...
31
32
    $mydirname = basename(dirname(__DIR__));
33
//    $mydirpath = 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...
34
35
    $groups        = is_object($xoopsUser) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
36
    $gperm_handler = & xoops_gethandler('groupperm');
37
38
    $module_handler = & xoops_gethandler('module');
39
    $module         = & $module_handler->getByDirname($mydirname);
40
41
    if (!$gperm_handler->checkRight($permType, $cid, $groups, $module->getVar('mid'))) {
42
        if ($redirect == false) {
0 ignored issues
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...
43
            return false;
44
        } else {
45
            redirect_header('index.php', 3, _NOPERM);
46
            exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The function xtubeCheckSearchGroups() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
47
        }
48
    }
49
    unset($module);
50
51
    return true;
52
}
53
54
/**
55
 * @param $queryarray
56
 * @param $andor
57
 * @param $limit
58
 * @param $offset
59
 * @param $userid
60
 *
61
 * @return array
62
 */
63
function xtubeSearch($queryarray, $andor, $limit, $offset, $userid)
64
{
65
    global $xoopsDB, $xoopsUser;
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...
66
67
    $sql    = "SELECT cid, pid FROM " . $xoopsDB->prefix('xoopstube_cat');
68
    $result = $xoopsDB->query($sql);
69
    while ($_search_group_check = $xoopsDB->fetchArray($result)) {
70
        $_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...
71
    }
72
73
    $sql = "SELECT lid, cid, title, submitter, published, description FROM " . $xoopsDB->prefix('xoopstube_videos');
74
    $sql .= " WHERE published > 0 AND published <= " . time() . " AND ( expired = 0 OR expired > " . time() . ") AND offline = 0 AND cid > 0";
75
76
    if ($userid != 0) {
77
        $sql .= " AND submitter=" . $userid . " ";
78
    }
79
80
    // because count() returns 1 even if a supplied variable
81
    // is not an array, we must check if $querryarray is really an array
82
    if (is_array($queryarray) && $count = count($queryarray)) {
83
        $sql .= " AND ((title LIKE LOWER('%$queryarray[0]%') OR LOWER(description) LIKE LOWER('%$queryarray[0]%'))";
84
        for ($i = 1; $i < $count; ++$i) {
85
            $sql .= " $andor ";
86
            $sql .= "(title LIKE LOWER('%$queryarray[$i]%') OR LOWER(description) LIKE LOWER('%$queryarray[$i]%'))";
87
        }
88
        $sql .= ") ";
89
    }
90
    $sql .= "ORDER BY published DESC";
91
    $result = $xoopsDB->query($sql, $limit, $offset);
92
    $ret    = array();
93
    $i      = 0;
94
95
    while ($myrow = $xoopsDB->fetchArray($result)) {
96
// Following is commented out because it can cause a conflict with View Account function (userinfo.php)
97
//        if ( false == xtubeCheckSearchGroups( $myrow['cid'] ) ) {
98
//            continue;
99
//        }
100
        $ret[$i]['image'] = 'assets/images/size2.gif';
101
        $ret[$i]['link']  = 'singlevideo.php?cid=' . $myrow['cid'] . '&amp;lid=' . $myrow['lid'];
102
        $ret[$i]['title'] = $myrow['title'];
103
        $ret[$i]['time']  = $myrow['published'];
104
        $ret[$i]['uid']   = $myrow['submitter'];
105
        ++$i;
106
    }
107
    unset($_search_check_array);
108
109
    return $ret;
110
}
111