Completed
Push — master ( bbb4f4...ddc2b8 )
by Michael
13s
created

xoopstube_top.php ➔ getRandomVideo()   C

Complexity

Conditions 9
Paths 16

Size

Total Lines 58
Code Lines 43

Duplication

Lines 25
Ratio 43.1 %

Importance

Changes 0
Metric Value
cc 9
eloc 43
nc 16
nop 1
dl 25
loc 58
rs 6.9928
c 0
b 0
f 0

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
    /** @var XoopsModuleHandler $moduleHandler */
33
    $moduleHandler = xoops_getHandler('module');
34
    $module        = $moduleHandler->getByDirname($moduleDirName);
35
    if (!$gpermHandler->checkRight($permType, $cid, $groups, $module->getVar('mid'))) {
36
        if (false === $redirect) {
37
            return false;
38
        } else {
39
            redirect_header('index.php', 3, _NOPERM);
40
        }
41
    }
42
    unset($module);
43
44
    return true;
45
}
46
47
/**
48
 * @param int    $cid
49
 * @param string $permType
50
 * @param bool   $redirect
51
 *
52
 * @return bool
53
 */
54
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...
55
{
56
    $moduleDirName = basename(dirname(__DIR__));
57
    $groups        = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : XOOPS_GROUP_ANONYMOUS;
58
    /** @var XoopsModuleHandler $moduleHandler */
59
    $moduleHandler = xoops_getHandler('module');
60
    $xtubeModule   = $moduleHandler->getByDirname($moduleDirName);
61
    $gpermHandler  = xoops_getHandler('groupperm');
62
    if (!$gpermHandler->checkRight($permType, $cid, $groups, $xtubeModule->getVar('mid'))) {
63
        if (false === $redirect) {
64
            return false;
65
        }
66
    }
67
68
    return true;
69
}
70
71
/**
72
 * @param       $bvidid
73
 * @param       $btitle
74
 * @param       $bsource
75
 * @param       $bpicurl
76
 * @param array $size
77
 *
78
 * @return string
79
 */
80
function getThumbsTopVideoBlock($bvidid, $btitle, $bsource, $bpicurl, $size = array())
81
{
82
    $thumbb            = '';
83
    /** @var XoopsModuleHandler $moduleHandler */
84
    $moduleHandler     = xoops_getHandler('module');
85
    $xtubeModule       = $moduleHandler->getByDirname('xoopstube');
86
    $configHandler     = xoops_getHandler('config');
87
    $xtubeModuleConfig = $configHandler->getConfigsByCat(0, $xtubeModule->getVar('mid'));
88
    if (isset($size['shotwidth'])) {
89
        $xtubeModuleConfig['shotwidth'] = $size['shotwidth'];
90
    }
91
    if (isset($size['shotheight'])) {
92
        $xtubeModuleConfig['shotheight'] = $size['shotheight'];
93
    }
94
    // Determine if video source YouTube
95 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...
96
        $thumbb = '<img src="http://img.youtube.com/vi/' . $bvidid . '/default.jpg" title="' . $btitle . '" alt="' . $btitle . '" width="' . $xtubeModuleConfig['shotwidth'] . '" height="'
97
                  . $xtubeModuleConfig['shotheight'] . '"  border="0" />';
98
    }
99
    // Determine if video source MetaCafe
100
    if ($bsource == 1) {
101
        list($metaclip) = explode('[/]', $bvidid);
102
        $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...
103
        $thumbb                  =
104
            '<img src="http://www.metacafe.com/thumb/' . $videothumb['metathumb'] . '.jpg" title="' . $btitle . '" alt="' . $btitle . '" width="' . $xtubeModuleConfig['shotwidth'] . '" height="'
105
            . $xtubeModuleConfig['shotheight'] . '"  border="0" />';
106
    }
107
    // Determine if video source iFilm/Spike
108
    if ($bsource == 2) {
109
        $thumbb = '<img src="http://img2.ifilmpro.com/resize/image/stills/films/resize/istd/' . $bvidid . '.jpg?width=' . $xtubeModuleConfig['shotwidth'] . ' title="' . $btitle . '" alt="' . $btitle
110
                  . '" border="0" />';
111
    }
112
    // Determine if video source Photobucket
113 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...
114
        $thumbb = '<img src="http://i153.photobucket.com/albums/' . $bvidid . '.jpg" title="' . $btitle . '" alt="' . $btitle . '" width="' . $xtubeModuleConfig['shotwidth'] . '" height="'
115
                  . $xtubeModuleConfig['shotheight'] . '"  border="0" />';
116
    }
117
    // Determine if video source Google Video / MySpace TV / DailyMotion
118 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...
119
        $thumbb = '<img src="' . $bpicurl . '" title="' . $btitle . '" alt="' . $btitle . '" width="' . $xtubeModuleConfig['shotwidth'] . '" height="' . $xtubeModuleConfig['shotheight']
120
                  . '"  border="0" />';
121
    }
122
    // Determine if video source MySpace TV
123 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...
124
        $thumbb = '<img src="' . $bpicurl . '" title="' . $btitle . '" alt="' . $btitle . '" width="' . $xtubeModuleConfig['shotwidth'] . '" height="' . $xtubeModuleConfig['shotheight']
125
                  . '"  border="0" />';
126
    }
127
    // Determine if video source DailyMotion
128 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...
129
        $thumbb = '<img src="' . $bpicurl . '" title="' . $btitle . '" alt="' . $btitle . '" width="' . $xtubeModuleConfig['shotwidth'] . '" height="' . $xtubeModuleConfig['shotheight']
130
                  . '"  border="0" />';
131
    }
132
133
    return $thumbb;
134
}
135
136
/* Function: b_xoopstube_spotlight_show
137
 * Input   : $options[0] = date for the most recent videos
138
 *             hits for the most popular videos
139
 *           $block['content'] = The optional above content
140
 *           $options[1] = How many videos are displayes
141
 * Output  : Returns the most recent or most popular videos
142
 */
143
/**
144
 * @param $options
145
 *
146
 * @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...
147
 */
148
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...
149
{
150
    require_once __DIR__ . '/../include/video.php';
151
    $block             = array();
152
    /** @var XoopsModuleHandler $moduleHandler */
153
    $moduleHandler     = xoops_getHandler('module');
154
    $xtubeModule       = $moduleHandler->getByDirname('xoopstube');
155
    $configHandler     = xoops_getHandler('config');
156
    $xtubeModuleConfig = $configHandler->getConfigsByCat(0, $xtubeModule->getVar('mid'));
157
    $xtubemyts         = MyTextSanitizer:: getInstance();
158
159
    $options[1] = 4;
160
    $result     =
161
        $GLOBALS['xoopsDB']->query('SELECT lid, cid, title, vidid, date, hits, vidsource, picurl FROM ' . $GLOBALS['xoopsDB']->prefix('xoopstube_videos') . ' WHERE published > 0 AND published <= '
162
                                   . time() . ' AND (expired = 0 OR expired > ' . time() . ') AND offline = 0 ORDER BY ' . $options[0] . ' DESC', $options[1], 0);
163
164
    $i = 0;
165
    while (false !== ($myrow = $GLOBALS['xoopsDB']->fetchArray($result))) {
166 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...
167
            continue;
168
        }
169
        if (false === xtubeCheckBlockGroups($myrow['cid'])) {
170
            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...
171
        }
172
        $videoload = array();
173
        $title     = $xtubemyts->htmlSpecialChars($xtubemyts->stripSlashesGPC($myrow['title']));
174 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...
175
            if (strlen($myrow['title']) >= $options[2]) {
176
                $title = substr($myrow['title'], 0, $options[2] - 1) . '...';
177
            }
178
        }
179
        $videoload['id']    = (int)$myrow['lid'];
180
        $videoload['cid']   = (int)$myrow['cid'];
181
        $videoload['title'] = $title;
182 View Code Duplication
        if ($options[0] === 'date') {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $options[0] (integer) and 'date' (string) can never be identical. Maybe you want to use a loose comparison == instead?
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...
183
            $videoload['date'] = formatTimestamp($myrow['date'], $xtubeModuleConfig['dateformat']);
184
        } 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...
185
            $videoload['hits'] = $myrow['hits'];
186
        }
187
188
        $size               = array();
189
        $rate               = 425 / 350;
190
        $size['shotwidth']  = '100';
191
        $size['shotheight'] = (int)($size['shotwidth'] / $rate);
192
193
        if (0 == $i && 0 == $myrow['vidsource']) {
194
            $videowidth             = 340;
195
            $videoheight            = (int)($videowidth / $rate);
196
            $showvideo              = '<object width="' . $videowidth . '" height="' . $videoheight . '"><param name="movie" value="http://www.youtube.com/v/' . $myrow['vidid']
197
                                      . '"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/' . $myrow['vidid']
198
                                      . '" type="application/x-shockwave-flash" wmode="transparent" width="' . $videowidth . '" height="' . $videoheight . '"></embed></object>';
199
            $videoload['showvideo'] = $showvideo;
200
        }
201
202
        $videoload['videothumb'] = getThumbsTopVideoBlock($myrow['vidid'], $title, $myrow['vidsource'], $myrow['picurl'], $size);
203
204
        $videoload['dirname'] = $xtubeModule->getVar('dirname');
205
        $block['videos'][]    = $videoload;
206
        ++$i;
207
    }
208
    unset($_block_check_array);
209
210
    return $block;
211
}
212
213
// Function: showTopVideoBlock
214
// Input   : $options[0] = date for the most recent videos
215
//             hits for the most popular videos
216
//           $block['content'] = The optional above content
217
//           $options[1] = How many videos are displayes
218
//           $options[2] = Length of title
219
//           $options[3] = Set date format
220
// Output  : Returns the most recent or most popular videos
221
/**
222
 * @param $options
223
 *
224
 * @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...
225
 */
226
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...
227
{
228
    $moduleDirName     = basename(dirname(__DIR__));
229
    $block             = array();
230
    /** @var XoopsModuleHandler $moduleHandler */
231
    $moduleHandler     = xoops_getHandler('module');
232
    $xtubeModule       = $moduleHandler->getByDirname($moduleDirName);
233
    $configHandler     = xoops_getHandler('config');
234
    $xtubeModuleConfig = $configHandler->getConfigsByCat(0, $xtubeModule->getVar('mid'));
235
    $xtubemyts         = MyTextSanitizer:: getInstance();
236
237
    if (isset($options[4]) && ($options[4] > 0)) {
238
        $result = $GLOBALS['xoopsDB']->query('SELECT lid, cid, title, vidid, screenshot, published, hits, vidsource, picurl FROM ' . $GLOBALS['xoopsDB']->prefix('xoopstube_videos') . ' WHERE published>0
239
                                    AND published<=' . time() . '
240
                                    AND (expired=0 OR expired>' . time() . ')
241
                                    AND offline=0
242
                                    AND cid=' . $options[4] . '
243
                                    ORDER BY ' . $options[0] . '
244
                                    DESC', $options[1], 0);
245
    } else {
246
        $result = $GLOBALS['xoopsDB']->query('SELECT lid, cid, title, vidid, screenshot, published, hits, vidsource, picurl FROM ' . $GLOBALS['xoopsDB']->prefix('xoopstube_videos') . ' WHERE published>0
247
                                    AND published<=' . time() . '
248
                                    AND (expired=0 OR expired>' . time() . ')
249
                                    AND offline=0
250
                                    ORDER BY ' . $options[0] . '
251
                                    DESC', $options[1], 0);
252
    }
253
254
    require_once XOOPS_ROOT_PATH . '/modules/' . $xtubeModule->getVar('dirname') . '/include/video.php';
255
    require_once XOOPS_ROOT_PATH . '/modules/' . $xtubeModule->getVar('dirname') . '/class/utility.php';
256
257
    while (false !== ($myrow = $GLOBALS['xoopsDB']->fetchArray($result))) {
258 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...
259
            continue;
260
        }
261
262
        if (false === xtubeCheckBlockGroups($myrow['cid'])) {
263
            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...
264
        }
265
266
        $videoload = array();
267
        $title     = $xtubemyts->htmlSpecialChars($xtubemyts->stripSlashesGPC($myrow['title']));
268 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...
269
            if (strlen($myrow['title']) >= $options[2]) {
270
                $title = substr($myrow['title'], 0, $options[2] - 1) . '...';
271
            }
272
        }
273
        $videoload['id']    = (int)$myrow['lid'];
274
        $videoload['cid']   = (int)$myrow['cid'];
275
        $videoload['title'] = $title;
276 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...
277
            $videoload['date'] = XoopstubeUtility::xtubeGetTimestamp(formatTimestamp($myrow['published'], $options[3]));
278
        } elseif ('hits' === $options[0]) {
279
            $videoload['hits'] = $myrow['hits'];
280
        }
281
282
        $videoload['videothumb'] = xtubeGetVideoThumb($myrow['vidid'], $title, $myrow['vidsource'], $myrow['picurl'], $xtubeModuleConfig['videoimgdir'] . '/' . $myrow['screenshot'], $xtubeModuleConfig['shotwidth'], $xtubeModuleConfig['shotheight']);
283
        $videoload['dirname']    = $xtubeModule->getVar('dirname');
284
        $videoload['width']      = $xtubeModuleConfig['shotwidth'] + 2;
285
        $block['videos'][]       = $videoload;
286
    }
287
    unset($_block_check_array);
288
289
    return $block;
290
}
291
292
// Function: getRandomVideo
293
// Output  : Returns random video
294
/**
295
 * @param $options
296
 *
297
 * @return array
298
 */
299
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...
300
{
301
    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...
302
    $moduleDirName     = basename(dirname(__DIR__));
303
    $block             = array();
304
    /** @var XoopsModuleHandler $moduleHandler */
305
    $moduleHandler     = xoops_getHandler('module');
306
    $xtubeModule       = $moduleHandler->getByDirname($moduleDirName);
307
    $configHandler     = xoops_getHandler('config');
308
    $xtubeModuleConfig = $configHandler->getConfigsByCat(0, $xtubeModule->getVar('mid'));
309
    $xtubemyts         = MyTextSanitizer:: getInstance();
310
311 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...
312
        $result2 = $GLOBALS['xoopsDB']->query('SELECT lid, cid, title, vidid, screenshot, published, vidsource, picurl FROM ' . $GLOBALS['xoopsDB']->prefix('xoopstube_videos') . ' WHERE published > 0
313
                                    AND published<=' . time() . '
314
                                    AND (expired=0 OR expired>' . time() . ')
315
                                    AND offline=0
316
                                    AND cid=' . $options[4] . '
317
                                    ORDER BY RAND() LIMIT ' . $options[1]);
318
    } else {
319
        $result2 = $GLOBALS['xoopsDB']->query('SELECT lid, cid, title, vidid, screenshot, published, vidsource, picurl FROM ' . $GLOBALS['xoopsDB']->prefix('xoopstube_videos') . ' WHERE published > 0
320
                                    AND published<=' . time() . '
321
                                    AND (expired=0 OR expired>' . time() . ')
322
                                    AND offline=0
323
                                    ORDER BY RAND() LIMIT ' . $options[1]);
324
    }
325
326
    require_once XOOPS_ROOT_PATH . '/modules/' . $xtubeModule->getVar('dirname') . '/include/video.php';
327
    require_once XOOPS_ROOT_PATH . '/modules/' . $xtubeModule->getVar('dirname') . '/class/utility.php';
328
329
    while (false !== ($myrow = $GLOBALS['xoopsDB']->fetchArray($result2))) {
330 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...
331
            continue;
332
        }
333
        $videorandom = array();
334
        $title       = $xtubemyts->htmlSpecialChars($xtubemyts->stripSlashesGPC($myrow['title']));
335 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...
336
            if (strlen($myrow['title']) >= $options[2]) {
337
                $title = substr($myrow['title'], 0, $options[2] - 1) . '...';
338
            }
339
        }
340
        $videorandom['id']    = (int)$myrow['lid'];
341
        $videorandom['cid']   = (int)$myrow['cid'];
342
        $videorandom['title'] = $title;
343 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...
344
            $videorandom['date'] = XoopstubeUtility::xtubeGetTimestamp(formatTimestamp($myrow['published'], $options[3]));
345
        }
346
        $videorandom['videothumb'] =
347
            xtubeGetVideoThumb($myrow['vidid'], $myrow['title'], $myrow['vidsource'], $myrow['picurl'], $xtubeModuleConfig['videoimgdir'] . '/' . $myrow['screenshot'], $xtubeModuleConfig['shotwidth'],
348
                               $xtubeModuleConfig['shotheight']);
349
        $videorandom['dirname']    = $xtubeModule->getVar('dirname');
350
        $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...
351
        $block['random'][]         = $videorandom;
352
    }
353
    unset($_block_check_array);
354
355
    return $block;
356
}
357
358
// Function: b_xoopstube_random_h
359
// Output  : Returns random video in horizontal block
360
/**
361
 * @param $options
362
 *
363
 * @return array
364
 */
365
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...
366
{
367
    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...
368
    $moduleDirName     = basename(dirname(__DIR__));
369
    $block             = array();
370
    /** @var XoopsModuleHandler $moduleHandler */
371
    $moduleHandler     = xoops_getHandler('module');
372
    $xtubeModule       = $moduleHandler->getByDirname($moduleDirName);
373
    $configHandler     = xoops_getHandler('config');
374
    $xtubeModuleConfig = $configHandler->getConfigsByCat(0, $xtubeModule->getVar('mid'));
375
    $xtubemyts         = MyTextSanitizer:: getInstance();
376
377 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...
378
        $result2 = $GLOBALS['xoopsDB']->query('SELECT lid, cid, title, vidid, screenshot, published, vidsource, picurl FROM ' . $GLOBALS['xoopsDB']->prefix('xoopstube_videos') . ' WHERE published > 0
379
                                    AND published<=' . time() . '
380
                                    AND (expired=0 OR expired>' . time() . ')
381
                                    AND offline=0
382
                                    AND cid=' . $options[4] . '
383
                                    ORDER BY RAND() LIMIT ' . $options[1]);
384
    } else {
385
        $result2 = $GLOBALS['xoopsDB']->query('SELECT lid, cid, title, vidid, screenshot, published, vidsource, picurl FROM ' . $GLOBALS['xoopsDB']->prefix('xoopstube_videos') . ' WHERE published > 0
386
                                    AND published<=' . time() . '
387
                                    AND (expired=0 OR expired>' . time() . ')
388
                                    AND offline=0
389
                                    ORDER BY RAND() LIMIT ' . $options[1]);
390
    }
391
392
    require_once XOOPS_ROOT_PATH . '/modules/' . $xtubeModule->getVar('dirname') . '/include/video.php';
393
    require_once XOOPS_ROOT_PATH . '/modules/' . $xtubeModule->getVar('dirname') . '/class/utility.php';
394
395
    while (false !== ($myrow = $GLOBALS['xoopsDB']->fetchArray($result2))) {
396 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...
397
            continue;
398
        }
399
        $videorandomh            = array();
400
        $title                   = $xtubemyts->htmlSpecialChars($xtubemyts->stripSlashesGPC($myrow['title']));
401
        $videorandomh['balloon'] = $myrow['title'];
402 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...
403
            if (strlen($myrow['title']) >= $options[2]) {
404
                $title = substr($myrow['title'], 0, $options[2] - 1) . '...';
405
            }
406
        }
407
        $videorandomh['id']    = (int)$myrow['lid'];
408
        $videorandomh['cid']   = (int)$myrow['cid'];
409
        $videorandomh['title'] = $title;
410 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...
411
            $videorandomh['date'] = XoopstubeUtility::xtubeGetTimestamp(formatTimestamp($myrow['published'], $options[3]));
412
        }
413
        require_once XOOPS_ROOT_PATH . '/modules/' . $xtubeModule->getVar('dirname') . '/include/video.php';
414
        $videorandomh['videothumb'] = xtubeGetVideoThumb($myrow['vidid'], $myrow['title'], $myrow['vidsource'], $myrow['picurl'], $xtubeModuleConfig['videoimgdir'] . '/' . $myrow['screenshot'], $xtubeModuleConfig['shotwidth'], $xtubeModuleConfig['shotheight']);
415
        $videorandomh['dirname']    = $xtubeModule->getVar('dirname');
416
        $videorandomh['width']      = $xtubeModuleConfig['shotwidth'] + 2;
417
        $block['random'][]          = $videorandomh;
418
    }
419
    unset($_block_check_array);
420
421
    return $block;
422
}
423
424
// editTopVideoBlock()
425
// @param $options
426
// @return
427
/**
428
 * @param $options
429
 *
430
 * @return string
431
 */
432
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...
433
{
434
    $form = '' . _MB_XOOPSTUBE_DISP . '&nbsp;';
435
    $form .= "<input type='hidden' name='options[]' value='";
436
    if ('published' === $options[0]) {
437
        $form .= "published'";
438
    }
439
    if ('random' === $options[0]) {
440
        $form .= "random'";
441
    }
442
    if ('randomh' === $options[0]) {
443
        $form .= "randomh'";
444
    } else {
445
        $form .= "hits'";
446
    }
447
    $form .= ' />';
448
    $form .= "<input type='text' name='options[]' value='" . $options[1] . "' />&nbsp;" . _MB_XOOPSTUBE_FILES . '';
449
    $form .= '&nbsp;<br>' . _MB_XOOPSTUBE_CHARS . "&nbsp;<input type='text' name='options[]' value='" . $options[2] . "' />&nbsp;" . _MB_XOOPSTUBE_LENGTH . '';
450
    $form .= '&nbsp;<br>' . _MB_XOOPSTUBE_DATEFORMAT . "&nbsp;<input type='text' name='options[]' value='" . $options[3] . "' />&nbsp;" . _MB_XOOPSTUBE_DATEFORMATMANUAL;
451
452
    $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...
453
    require_once XOOPS_ROOT_PATH . '/modules/xoopstube/class/xoopstubetree.php';
454
    $xt      = new XoopstubeTree($GLOBALS['xoopsDB']->prefix('xoopstube_cat'), 'cid', 'pid');
455
    $cat_arr = $xt->getChildTreeArray(0, 'title');
456
457
    $form .= '<br>' . _MB_XOOPSTUBE_SELECTCAT . "<br><select name=\"options[]\" multiple=\"multiple\" size=\"5\">";
458
    $form = false === array_search(0, $options) ? $form . "<option value=\"0\">" . _MB_XOOPSTUBE_ALLCAT . '</option>' : $form . "<option value=\"0\" selected=\"selected\">" . _MB_XOOPSTUBE_ALLCAT . '</option>';
459
460
    foreach ($cat_arr as $catlist) {
461
        if (false === array_search($catlist, $options)) {
462
            $form .= "<option value=\"" . $catlist['cid'] . "\">" . $catlist['title'] . '</option>';
463
        } else {
464
            $form .= "<option value=\"" . $catlist['cid'] . "\" selected=\"selected\">" . $catlist['title'] . '</option>';
465
        }
466
    }
467
    $form .= '</select>';
468
469
    return $form;
470
}
471