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

xoopstube_top.php ➔ getSpotlightVideos()   C

Complexity

Conditions 11
Paths 21

Size

Total Lines 74
Code Lines 56

Duplication

Lines 13
Ratio 17.57 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 56
c 1
b 0
f 0
nc 21
nop 1
dl 13
loc 74
rs 5.5364

How to fix   Long Method    Complexity   

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