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

xoopstube_top.php ➔ getRandomVideo()   B

Complexity

Conditions 9
Paths 16

Size

Total Lines 57
Code Lines 43

Duplication

Lines 25
Ratio 43.86 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 9
eloc 43
c 2
b 0
f 0
nc 16
nop 1
dl 25
loc 57
rs 7.0745

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Module: XoopsTube
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 *
9
 * PHP version 5
10
 *
11
 * @category        Module
12
 * @package         Xoopstube
13
 * @author          XOOPS Development Team
14
 * @copyright       2001-2016 XOOPS Project (http://xoops.org)
15
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
16
 * @link            http://xoops.org/
17
 * @since           1.0.6
18
 */
19
20
/**
21
 * @param int    $cid
22
 * @param string $permType
23
 * @param bool   $redirect
24
 *
25
 * @return bool
26
 */
27 View Code Duplication
function checkBlockGroups($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
checkBlockGroups 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
    $groups        = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : XOOPS_GROUP_ANONYMOUS;
31
    $gpermHandler  = xoops_getHandler('groupperm');
32
    $moduleHandler = xoops_getHandler('module');
33
    $module        = $moduleHandler->getByDirname($moduleDirName);
34
    if (!$gpermHandler->checkRight($permType, $cid, $groups, $module->getVar('mid'))) {
35
        if (false === $redirect) {
36
            return false;
37
        } else {
38
            redirect_header('index.php', 3, _NOPERM);
39
        }
40
    }
41
    unset($module);
42
43
    return true;
44
}
45
46
/**
47
 * @param int    $cid
48
 * @param string $permType
49
 * @param bool   $redirect
50
 *
51
 * @return bool
52
 */
53
function xtubeCheckBlockGroups($cid = 0, $permType = 'XTubeCatPerm', $redirect = false)
0 ignored issues
show
Coding Style introduced by
xtubeCheckBlockGroups 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...
54
{
55
    $moduleDirName = basename(dirname(__DIR__));
56
    $groups        = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : XOOPS_GROUP_ANONYMOUS;
57
    $moduleHandler = xoops_getHandler('module');
58
    $xtubeModule   = $moduleHandler->getByDirname($moduleDirName);
59
    $gpermHandler  = xoops_getHandler('groupperm');
60
    if (!$gpermHandler->checkRight($permType, $cid, $groups, $xtubeModule->getVar('mid'))) {
61
        if (false === $redirect) {
62
            return false;
63
        }
64
    }
65
66
    return true;
67
}
68
69
/**
70
 * @param       $bvidid
71
 * @param       $btitle
72
 * @param       $bsource
73
 * @param       $bpicurl
74
 * @param array $size
75
 *
76
 * @return string
77
 */
78
function getThumbsTopVideoBlock($bvidid, $btitle, $bsource, $bpicurl, $size = array())
79
{
80
    $thumbb            = '';
81
    $moduleHandler     = xoops_getHandler('module');
82
    $xtubeModule       = $moduleHandler->getByDirname('xoopstube');
83
    $configHandler     = xoops_getHandler('config');
84
    $xtubeModuleConfig = $configHandler->getConfigsByCat(0, $xtubeModule->getVar('mid'));
85
    if (isset($size['shotwidth'])) {
86
        $xtubeModuleConfig['shotwidth'] = $size['shotwidth'];
87
    }
88
    if (isset($size['shotheight'])) {
89
        $xtubeModuleConfig['shotheight'] = $size['shotheight'];
90
    }
91
    // Determine if video source YouTube
92 View Code Duplication
    if (0 == $bsource) {
0 ignored issues
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...
93
        $thumbb = '<img src="http://img.youtube.com/vi/' . $bvidid . '/default.jpg" title="' . $btitle . '" alt="' . $btitle . '" width="' . $xtubeModuleConfig['shotwidth'] . '" height="'
94
                  . $xtubeModuleConfig['shotheight'] . '"  border="0" />';
95
    }
96
    // Determine if video source MetaCafe
97
    if ($bsource == 1) {
98
        list($metaclip) = explode('[/]', $bvidid);
99
        $videothumb['metathumb'] = $metaclip;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$videothumb was never initialized. Although not strictly required by PHP, it is generally a good practice to add $videothumb = 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...
100
        $thumbb                  =
101
            '<img src="http://www.metacafe.com/thumb/' . $videothumb['metathumb'] . '.jpg" title="' . $btitle . '" alt="' . $btitle . '" width="' . $xtubeModuleConfig['shotwidth'] . '" height="'
102
            . $xtubeModuleConfig['shotheight'] . '"  border="0" />';
103
    }
104
    // Determine if video source iFilm/Spike
105
    if ($bsource == 2) {
106
        $thumbb = '<img src="http://img2.ifilmpro.com/resize/image/stills/films/resize/istd/' . $bvidid . '.jpg?width=' . $xtubeModuleConfig['shotwidth'] . ' title="' . $btitle . '" alt="' . $btitle
107
                  . '" border="0" />';
108
    }
109
    // Determine if video source Photobucket
110 View Code Duplication
    if ($bsource == 3) {
0 ignored issues
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...
111
        $thumbb = '<img src="http://i153.photobucket.com/albums/' . $bvidid . '.jpg" title="' . $btitle . '" alt="' . $btitle . '" width="' . $xtubeModuleConfig['shotwidth'] . '" height="'
112
                  . $xtubeModuleConfig['shotheight'] . '"  border="0" />';
113
    }
114
    // Determine if video source Google Video / MySpace TV / DailyMotion
115 View Code Duplication
    if ($bsource == 100) {
0 ignored issues
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...
116
        $thumbb = '<img src="' . $bpicurl . '" title="' . $btitle . '" alt="' . $btitle . '" width="' . $xtubeModuleConfig['shotwidth'] . '" height="' . $xtubeModuleConfig['shotheight']
117
                  . '"  border="0" />';
118
    }
119
    // Determine if video source MySpace TV
120 View Code Duplication
    if ($bsource == 101) {
0 ignored issues
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...
121
        $thumbb = '<img src="' . $bpicurl . '" title="' . $btitle . '" alt="' . $btitle . '" width="' . $xtubeModuleConfig['shotwidth'] . '" height="' . $xtubeModuleConfig['shotheight']
122
                  . '"  border="0" />';
123
    }
124
    // Determine if video source DailyMotion
125 View Code Duplication
    if ($bsource == 102) {
0 ignored issues
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...
126
        $thumbb = '<img src="' . $bpicurl . '" title="' . $btitle . '" alt="' . $btitle . '" width="' . $xtubeModuleConfig['shotwidth'] . '" height="' . $xtubeModuleConfig['shotheight']
127
                  . '"  border="0" />';
128
    }
129
130
    return $thumbb;
131
}
132
133
/* Function: b_xoopstube_spotlight_show
134
 * Input   : $options[0] = date for the most recent videos
135
 *             hits for the most popular videos
136
 *           $block['content'] = The optional above content
137
 *           $options[1] = How many videos are displayes
138
 * Output  : Returns the most recent or most popular videos
139
 */
140
/**
141
 * @param $options
142
 *
143
 * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be null|array?

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...
144
 */
145
function getSpotlightVideos($options)
0 ignored issues
show
Coding Style introduced by
getSpotlightVideos 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...
146
{
147
    include_once dirname(__DIR__) . '/include/video.php';
148
    $block             = array();
149
    $moduleHandler     = xoops_getHandler('module');
150
    $xtubeModule       = $moduleHandler->getByDirname('xoopstube');
151
    $configHandler     = xoops_getHandler('config');
152
    $xtubeModuleConfig = $configHandler->getConfigsByCat(0, $xtubeModule->getVar('mid'));
153
    $xtubemyts         = MyTextSanitizer:: getInstance();
154
155
    $options[1] = 4;
156
    $result     =
157
        $GLOBALS['xoopsDB']->query('SELECT lid, cid, title, vidid, date, hits, vidsource, picurl FROM ' . $GLOBALS['xoopsDB']->prefix('xoopstube_videos') . ' WHERE published > 0 AND published <= '
158
                                   . time() . ' AND (expired = 0 OR expired > ' . time() . ') AND offline = 0 ORDER BY ' . $options[0] . ' DESC', $options[1], 0);
159
160
    $i = 0;
161
    while (false !== ($myrow = $GLOBALS['xoopsDB']->fetchArray($result))) {
162 View Code Duplication
        if (false === checkBlockGroups($myrow['cid']) || $myrow['cid'] == 0) {
0 ignored issues
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...
163
            continue;
164
        }
165
        if (false === xtubeCheckBlockGroups($myrow['cid'])) {
166
            exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function getSpotlightVideos() 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...
167
        }
168
        $videoload = array();
169
        $title     = $xtubemyts->htmlSpecialChars($xtubemyts->stripSlashesGPC($myrow['title']));
170 View Code Duplication
        if (!XOOPS_USE_MULTIBYTES) {
0 ignored issues
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...
171
            if (strlen($myrow['title']) >= $options[2]) {
172
                $title = substr($myrow['title'], 0, $options[2] - 1) . '...';
173
            }
174
        }
175
        $videoload['id']    = (int)$myrow['lid'];
176
        $videoload['cid']   = (int)$myrow['cid'];
177
        $videoload['title'] = $title;
178 View Code Duplication
        if ($options[0] == 'date') {
0 ignored issues
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...
179
            $videoload['date'] = formatTimestamp($myrow['date'], $xtubeModuleConfig['dateformat']);
180
        } elseif ('hits' === $options[0]) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of 'hits' (string) and $options[0] (integer) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
181
            $videoload['hits'] = $myrow['hits'];
182
        }
183
184
        $size               = array();
185
        $rate               = 425 / 350;
186
        $size['shotwidth']  = '100';
187
        $size['shotheight'] = (int)($size['shotwidth'] / $rate);
188
189
        if (0 == $i && 0 == $myrow['vidsource']) {
190
            $videowidth             = 340;
191
            $videoheight            = (int)($videowidth / $rate);
192
            $showvideo              = '<object width="' . $videowidth . '" height="' . $videoheight . '"><param name="movie" value="http://www.youtube.com/v/' . $myrow['vidid']
193
                                      . '"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/' . $myrow['vidid']
194
                                      . '" type="application/x-shockwave-flash" wmode="transparent" width="' . $videowidth . '" height="' . $videoheight . '"></embed></object>';
195
            $videoload['showvideo'] = $showvideo;
196
        }
197
198
        $videoload['videothumb'] = getThumbsTopVideoBlock($myrow['vidid'], $title, $myrow['vidsource'], $myrow['picurl'], $size);
199
200
        $videoload['dirname'] = $xtubeModule->getVar('dirname');
201
        $block['videos'][]    = $videoload;
202
        ++$i;
203
    }
204
    unset($_block_check_array);
205
206
    return $block;
207
}
208
209
// Function: showTopVideoBlock
210
// Input   : $options[0] = date for the most recent videos
211
//             hits for the most popular videos
212
//           $block['content'] = The optional above content
213
//           $options[1] = How many videos are displayes
214
//           $options[2] = Length of title
215
//           $options[3] = Set date format
216
// Output  : Returns the most recent or most popular videos
217
/**
218
 * @param $options
219
 *
220
 * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be null|array?

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...
221
 */
222
function showTopVideoBlock($options)
0 ignored issues
show
Coding Style introduced by
showTopVideoBlock 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...
223
{
224
    $moduleDirName     = basename(dirname(__DIR__));
225
    $block             = array();
226
    $moduleHandler     = xoops_getHandler('module');
227
    $xtubeModule       = $moduleHandler->getByDirname($moduleDirName);
228
    $configHandler     = xoops_getHandler('config');
229
    $xtubeModuleConfig = $configHandler->getConfigsByCat(0, $xtubeModule->getVar('mid'));
230
    $xtubemyts         = MyTextSanitizer:: getInstance();
231
232
    if (isset($options[4]) && ($options[4] > 0)) {
233
        $result = $GLOBALS['xoopsDB']->query('SELECT lid, cid, title, vidid, screenshot, published, hits, vidsource, picurl FROM ' . $GLOBALS['xoopsDB']->prefix('xoopstube_videos') . ' WHERE published>0
234
                                    AND published<=' . time() . '
235
                                    AND (expired=0 OR expired>' . time() . ')
236
                                    AND offline=0
237
                                    AND cid=' . $options[4] . '
238
                                    ORDER BY ' . $options[0] . '
239
                                    DESC', $options[1], 0);
240
    } else {
241
        $result = $GLOBALS['xoopsDB']->query('SELECT lid, cid, title, vidid, screenshot, published, hits, vidsource, picurl FROM ' . $GLOBALS['xoopsDB']->prefix('xoopstube_videos') . ' WHERE published>0
242
                                    AND published<=' . time() . '
243
                                    AND (expired=0 OR expired>' . time() . ')
244
                                    AND offline=0
245
                                    ORDER BY ' . $options[0] . '
246
                                    DESC', $options[1], 0);
247
    }
248
249
    include_once XOOPS_ROOT_PATH . '/modules/' . $xtubeModule->getVar('dirname') . '/include/video.php';
250
    include_once XOOPS_ROOT_PATH . '/modules/' . $xtubeModule->getVar('dirname') . '/class/utilities.php';
251
252
    while (false !== ($myrow = $GLOBALS['xoopsDB']->fetchArray($result))) {
253 View Code Duplication
        if (false === checkBlockGroups($myrow['cid']) || 0 == $myrow['cid']) {
0 ignored issues
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...
254
            continue;
255
        }
256
257
        if (false === xtubeCheckBlockGroups($myrow['cid'])) {
258
            exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function showTopVideoBlock() 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...
259
        }
260
261
        $videoload = array();
262
        $title     = $xtubemyts->htmlSpecialChars($xtubemyts->stripSlashesGPC($myrow['title']));
263 View Code Duplication
        if (!XOOPS_USE_MULTIBYTES) {
0 ignored issues
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...
264
            if (strlen($myrow['title']) >= $options[2]) {
265
                $title = substr($myrow['title'], 0, $options[2] - 1) . '...';
266
            }
267
        }
268
        $videoload['id']    = (int)$myrow['lid'];
269
        $videoload['cid']   = (int)$myrow['cid'];
270
        $videoload['title'] = $title;
271 View Code Duplication
        if ('published' === $options[0]) {
0 ignored issues
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...
272
            $videoload['date'] = XoopstubeUtilities::xtubeGetTimestamp(formatTimestamp($myrow['published'], $options[3]));
273
        } elseif ('hits' === $options[0]) {
274
            $videoload['hits'] = $myrow['hits'];
275
        }
276
277
        $videoload['videothumb'] =
278
            xtubeGetVideoThumb($myrow['vidid'], $title, $myrow['vidsource'], $myrow['picurl'], $xtubeModuleConfig['videoimgdir'] . '/' . $myrow['screenshot'], $xtubeModuleConfig['shotwidth'],
279
                               $xtubeModuleConfig['shotheight']);
280
        $videoload['dirname']    = $xtubeModule->getVar('dirname');
281
        $videoload['width']      = $xtubeModuleConfig['shotwidth'] + 2;
282
        $block['videos'][]       = $videoload;
283
    }
284
    unset($_block_check_array);
285
286
    return $block;
287
}
288
289
// Function: getRandomVideo
290
// Output  : Returns random video
291
/**
292
 * @param $options
293
 *
294
 * @return array
295
 */
296
function getRandomVideo($options)
0 ignored issues
show
Coding Style introduced by
getRandomVideo 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...
297
{
298
    global $xtubemyts;
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...
299
    $moduleDirName     = basename(dirname(__DIR__));
300
    $block             = array();
301
    $moduleHandler     = xoops_getHandler('module');
302
    $xtubeModule       = $moduleHandler->getByDirname($moduleDirName);
303
    $configHandler     = xoops_getHandler('config');
304
    $xtubeModuleConfig = $configHandler->getConfigsByCat(0, $xtubeModule->getVar('mid'));
305
    $xtubemyts         = MyTextSanitizer:: getInstance();
306
307 View Code Duplication
    if (isset($options[4]) && ($options[4] > 0)) {
0 ignored issues
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...
308
        $result2 = $GLOBALS['xoopsDB']->query('SELECT lid, cid, title, vidid, screenshot, published, vidsource, picurl FROM ' . $GLOBALS['xoopsDB']->prefix('xoopstube_videos') . ' WHERE published > 0
309
                                    AND published<=' . time() . '
310
                                    AND (expired=0 OR expired>' . time() . ')
311
                                    AND offline=0
312
                                    AND cid=' . $options[4] . '
313
                                    ORDER BY RAND() LIMIT ' . $options[1]);
314
    } else {
315
        $result2 = $GLOBALS['xoopsDB']->query('SELECT lid, cid, title, vidid, screenshot, published, vidsource, picurl FROM ' . $GLOBALS['xoopsDB']->prefix('xoopstube_videos') . ' WHERE published > 0
316
                                    AND published<=' . time() . '
317
                                    AND (expired=0 OR expired>' . time() . ')
318
                                    AND offline=0
319
                                    ORDER BY RAND() LIMIT ' . $options[1]);
320
    }
321
322
    include_once XOOPS_ROOT_PATH . '/modules/' . $xtubeModule->getVar('dirname') . '/include/video.php';
323
    include_once XOOPS_ROOT_PATH . '/modules/' . $xtubeModule->getVar('dirname') . '/class/utilities.php';
324
325
    while (false !== ($myrow = $GLOBALS['xoopsDB']->fetchArray($result2))) {
326 View Code Duplication
        if (false === checkBlockGroups($myrow['cid']) || 0 == $myrow['cid']) {
0 ignored issues
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...
327
            continue;
328
        }
329
        $videorandom = array();
330
        $title       = $xtubemyts->htmlSpecialChars($xtubemyts->stripSlashesGPC($myrow['title']));
331 View Code Duplication
        if (!XOOPS_USE_MULTIBYTES) {
0 ignored issues
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...
332
            if (strlen($myrow['title']) >= $options[2]) {
333
                $title = substr($myrow['title'], 0, $options[2] - 1) . '...';
334
            }
335
        }
336
        $videorandom['id']    = (int)$myrow['lid'];
337
        $videorandom['cid']   = (int)$myrow['cid'];
338
        $videorandom['title'] = $title;
339 View Code Duplication
        if (isset($options[3])) {
0 ignored issues
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...
340
            $videorandom['date'] = XoopstubeUtilities::xtubeGetTimestamp(formatTimestamp($myrow['published'], $options[3]));
341
        }
342
        $videorandom['videothumb'] =
343
            xtubeGetVideoThumb($myrow['vidid'], $myrow['title'], $myrow['vidsource'], $myrow['picurl'], $xtubeModuleConfig['videoimgdir'] . '/' . $myrow['screenshot'], $xtubeModuleConfig['shotwidth'],
344
                               $xtubeModuleConfig['shotheight']);
345
        $videorandom['dirname']    = $xtubeModule->getVar('dirname');
346
        $videorandomh['width']     = $xtubeModuleConfig['shotwidth'] + 2;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$videorandomh was never initialized. Although not strictly required by PHP, it is generally a good practice to add $videorandomh = 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...
347
        $block['random'][]         = $videorandom;
348
    }
349
    unset($_block_check_array);
350
351
    return $block;
352
}
353
354
// Function: b_xoopstube_random_h
355
// Output  : Returns random video in horizontal block
356
/**
357
 * @param $options
358
 *
359
 * @return array
360
 */
361
function getRandomVideoForHorizontalBlock($options)
0 ignored issues
show
Coding Style introduced by
getRandomVideoForHorizontalBlock 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 $xtubemyts;
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...
364
    $moduleDirName     = basename(dirname(__DIR__));
365
    $block             = array();
366
    $moduleHandler     = xoops_getHandler('module');
367
    $xtubeModule       = $moduleHandler->getByDirname($moduleDirName);
368
    $configHandler     = xoops_getHandler('config');
369
    $xtubeModuleConfig = $configHandler->getConfigsByCat(0, $xtubeModule->getVar('mid'));
370
    $xtubemyts         = MyTextSanitizer:: getInstance();
371
372 View Code Duplication
    if (isset($options[4]) && ($options[4] > 0)) {
0 ignored issues
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...
373
        $result2 = $GLOBALS['xoopsDB']->query('SELECT lid, cid, title, vidid, screenshot, published, vidsource, picurl FROM ' . $GLOBALS['xoopsDB']->prefix('xoopstube_videos') . ' WHERE published > 0
374
                                    AND published<=' . time() . '
375
                                    AND (expired=0 OR expired>' . time() . ')
376
                                    AND offline=0
377
                                    AND cid=' . $options[4] . '
378
                                    ORDER BY RAND() LIMIT ' . $options[1]);
379
    } else {
380
        $result2 = $GLOBALS['xoopsDB']->query('SELECT lid, cid, title, vidid, screenshot, published, vidsource, picurl FROM ' . $GLOBALS['xoopsDB']->prefix('xoopstube_videos') . ' WHERE published > 0
381
                                    AND published<=' . time() . '
382
                                    AND (expired=0 OR expired>' . time() . ')
383
                                    AND offline=0
384
                                    ORDER BY RAND() LIMIT ' . $options[1]);
385
    }
386
387
    include_once XOOPS_ROOT_PATH . '/modules/' . $xtubeModule->getVar('dirname') . '/include/video.php';
388
    include_once XOOPS_ROOT_PATH . '/modules/' . $xtubeModule->getVar('dirname') . '/class/utilities.php';
389
390
    while (false !== ($myrow = $GLOBALS['xoopsDB']->fetchArray($result2))) {
391 View Code Duplication
        if (false === checkBlockGroups($myrow['cid']) || 0 == $myrow['cid']) {
0 ignored issues
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...
392
            continue;
393
        }
394
        $videorandomh            = array();
395
        $title                   = $xtubemyts->htmlSpecialChars($xtubemyts->stripSlashesGPC($myrow['title']));
396
        $videorandomh['balloon'] = $myrow['title'];
397 View Code Duplication
        if (!XOOPS_USE_MULTIBYTES) {
0 ignored issues
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...
398
            if (strlen($myrow['title']) >= $options[2]) {
399
                $title = substr($myrow['title'], 0, $options[2] - 1) . '...';
400
            }
401
        }
402
        $videorandomh['id']    = (int)$myrow['lid'];
403
        $videorandomh['cid']   = (int)$myrow['cid'];
404
        $videorandomh['title'] = $title;
405 View Code Duplication
        if (isset($options[3])) {
0 ignored issues
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...
406
            $videorandomh['date'] = XoopstubeUtilities::xtubeGetTimestamp(formatTimestamp($myrow['published'], $options[3]));
407
        }
408
        include_once XOOPS_ROOT_PATH . '/modules/' . $xtubeModule->getVar('dirname') . '/include/video.php';
409
        $videorandomh['videothumb'] =
410
            xtubeGetVideoThumb($myrow['vidid'], $myrow['title'], $myrow['vidsource'], $myrow['picurl'], $xtubeModuleConfig['videoimgdir'] . '/' . $myrow['screenshot'], $xtubeModuleConfig['shotwidth'],
411
                               $xtubeModuleConfig['shotheight']);
412
        $videorandomh['dirname']    = $xtubeModule->getVar('dirname');
413
        $videorandomh['width']      = $xtubeModuleConfig['shotwidth'] + 2;
414
        $block['random'][]          = $videorandomh;
415
    }
416
    unset($_block_check_array);
417
418
    return $block;
419
}
420
421
// editTopVideoBlock()
422
// @param $options
423
// @return
424
/**
425
 * @param $options
426
 *
427
 * @return string
428
 */
429
function editTopVideoBlock($options)
0 ignored issues
show
Coding Style introduced by
editTopVideoBlock 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...
430
{
431
    $form = '' . _MB_XOOPSTUBE_DISP . '&nbsp;';
432
    $form .= "<input type='hidden' name='options[]' value='";
433
    if ('published' === $options[0]) {
434
        $form .= "published'";
435
    }
436
    if ('random' === $options[0]) {
437
        $form .= "random'";
438
    }
439
    if ('randomh' === $options[0]) {
440
        $form .= "randomh'";
441
    } else {
442
        $form .= "hits'";
443
    }
444
    $form .= ' />';
445
    $form .= "<input type='text' name='options[]' value='" . $options[1] . "' />&nbsp;" . _MB_XOOPSTUBE_FILES . '';
446
    $form .= '&nbsp;<br>' . _MB_XOOPSTUBE_CHARS . "&nbsp;<input type='text' name='options[]' value='" . $options[2] . "' />&nbsp;" . _MB_XOOPSTUBE_LENGTH . '';
447
    $form .= '&nbsp;<br>' . _MB_XOOPSTUBE_DATEFORMAT . "&nbsp;<input type='text' name='options[]' value='" . $options[3] . "' />&nbsp;" . _MB_XOOPSTUBE_DATEFORMATMANUAL;
448
449
    $cat_arr = array();
0 ignored issues
show
Unused Code introduced by
$cat_arr 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...
450
    include_once XOOPS_ROOT_PATH . '/modules/xoopstube/class/xoopstubetree.php';
451
    $xt      = new XoopstubeTree($GLOBALS['xoopsDB']->prefix('xoopstube_cat'), 'cid', 'pid');
452
    $cat_arr = $xt->getChildTreeArray(0, 'title');
453
454
    $form .= '<br>' . _MB_XOOPSTUBE_SELECTCAT . "<br><select name=\"options[]\" multiple=\"multiple\" size=\"5\">";
455
    $form = false === array_search(0, $options) ? $form . "<option value=\"0\">" . _MB_XOOPSTUBE_ALLCAT . '</option>' : $form . "<option value=\"0\" selected=\"selected\">" . _MB_XOOPSTUBE_ALLCAT
456
                                                                                                                        . '</option>';
457
458
    foreach ($cat_arr as $catlist) {
459
        if (false === array_search($catlist, $options)) {
460
            $form .= "<option value=\"" . $catlist['cid'] . "\">" . $catlist['title'] . '</option>';
461
        } else {
462
            $form .= "<option value=\"" . $catlist['cid'] . "\" selected=\"selected\">" . $catlist['title'] . '</option>';
463
        }
464
    }
465
    $form .= '</select>';
466
467
    return $form;
468
}
469