Completed
Push — master ( a1059e...55b12e )
by Michael
01:29
created

Utility::getModuleOption()   D

Complexity

Conditions 10
Paths 6

Size

Total Lines 31
Code Lines 21

Duplication

Lines 31
Ratio 100 %

Importance

Changes 0
Metric Value
cc 10
eloc 21
nc 6
nop 2
dl 31
loc 31
rs 4.8196
c 0
b 0
f 0

How to fix   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 namespace XoopsModules\Marquee;
2
3
use Xmf\Request;
4
use \XoopsModules\Marquee\Common;
5
6
/**
7
 * Class Utility
8
 */
9
class Utility
10
{
11
    use Common\VersionChecks; //checkVerXoops, checkVerPhp Traits
12
13
    use Common\ServerStats; // getServerStats Trait
14
15
    use Common\FilesManagement; // Files Management Trait
16
17
    //--------------- Custom module methods -----------------------------
18
    const MODULE_NAME = 'marquee';
19
20
    /**
21
     * truncateHtml can truncate a string up to a number of characters while preserving whole words and HTML tags
22
     * www.gsdesign.ro/blog/cut-html-string-without-breaking-the-tags
23
     * www.cakephp.org
24
     *
25
     * @param string  $text         String to truncate.
26
     * @param integer $length       Length of returned string, including ellipsis.
27
     * @param string  $ending       Ending to be appended to the trimmed string.
28
     * @param boolean $exact        If false, $text will not be cut mid-word
29
     * @param boolean $considerHtml If true, HTML tags would be handled correctly
30
     *
31
     * @return string Trimmed string.
32
     */
33
    public static function truncateHtml($text, $length = 100, $ending = '...', $exact = false, $considerHtml = true)
34
    {
35
        if ($considerHtml) {
36
            // if the plain text is shorter than the maximum length, return the whole text
37
            if (strlen(preg_replace('/<.*?' . '>/', '', $text)) <= $length) {
38
                return $text;
39
            }
40
            // splits all html-tags to scanable lines
41
            preg_match_all('/(<.+?' . '>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER);
42
            $total_length = strlen($ending);
43
            $open_tags    = [];
44
            $truncate     = '';
45
            foreach ($lines as $line_matchings) {
0 ignored issues
show
Bug introduced by
The expression $lines of type null|array<integer,array<integer,string>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
46
                // if there is any html-tag in this line, handle it and add it (uncounted) to the output
47
                if (!empty($line_matchings[1])) {
48
                    // if it's an "empty element" with or without xhtml-conform closing slash
49
                    if (preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
50
                        // do nothing
51
                        // if tag is a closing tag
52
                    } elseif (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) {
53
                        // delete tag from $open_tags list
54
                        $pos = array_search($tag_matchings[1], $open_tags);
55
                        if (false !== $pos) {
56
                            unset($open_tags[$pos]);
57
                        }
58
                        // if tag is an opening tag
59
                    } elseif (preg_match('/^<\s*([^\s>!]+).*?' . '>$/s', $line_matchings[1], $tag_matchings)) {
60
                        // add tag to the beginning of $open_tags list
61
                        array_unshift($open_tags, strtolower($tag_matchings[1]));
62
                    }
63
                    // add html-tag to $truncate'd text
64
                    $truncate .= $line_matchings[1];
65
                }
66
                // calculate the length of the plain text part of the line; handle entities as one character
67
                $content_length = strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', ' ', $line_matchings[2]));
68
                if ($total_length + $content_length > $length) {
69
                    // the number of characters which are left
70
                    $left            = $length - $total_length;
71
                    $entities_length = 0;
72
                    // search for html entities
73
                    if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', $line_matchings[2], $entities, PREG_OFFSET_CAPTURE)) {
74
                        // calculate the real length of all entities in the legal range
75
                        foreach ($entities[0] as $entity) {
76
                            if ($entity[1] + 1 - $entities_length <= $left) {
77
                                $left--;
78
                                $entities_length += strlen($entity[0]);
79
                            } else {
80
                                // no more characters left
81
                                break;
82
                            }
83
                        }
84
                    }
85
                    $truncate .= substr($line_matchings[2], 0, $left + $entities_length);
86
                    // maximum lenght is reached, so get off the loop
87
                    break;
88
                } else {
89
                    $truncate     .= $line_matchings[2];
90
                    $total_length += $content_length;
91
                }
92
                // if the maximum length is reached, get off the loop
93
                if ($total_length >= $length) {
94
                    break;
95
                }
96
            }
97
        } else {
98
            if (strlen($text) <= $length) {
99
                return $text;
100
            } else {
101
                $truncate = substr($text, 0, $length - strlen($ending));
102
            }
103
        }
104
        // if the words shouldn't be cut in the middle...
105
        if (!$exact) {
106
            // ...search the last occurance of a space...
107
            $spacepos = strrpos($truncate, ' ');
108
            if (isset($spacepos)) {
109
                // ...and cut the text in this position
110
                $truncate = substr($truncate, 0, $spacepos);
111
            }
112
        }
113
        // add the defined ending to the text
114
        $truncate .= $ending;
115
        if ($considerHtml) {
116
            // close all unclosed html-tags
117
            foreach ($open_tags as $tag) {
0 ignored issues
show
Bug introduced by
The variable $open_tags does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
118
                $truncate .= '</' . $tag . '>';
119
            }
120
        }
121
122
        return $truncate;
123
    }
124
125
    /**
126
     * Access the only instance of this class
127
     *
128
     * @return \XoopsModules\Marquee\Utility
129
     *
130
     * @static
131
     * @staticvar   object
132
     */
133
    public static function getInstance()
134
    {
135
        static $instance;
136
        if (null === $instance) {
137
            $instance = new static();
138
        }
139
140
        return $instance;
141
    }
142
143
    /**
144
     * Returns a module's option (with cache)
145
     *
146
     * @param string  $option    module option's name
147
     * @param boolean $withCache Do we have to use some cache ?
148
     *
149
     * @return mixed option's value
150
     */
151
//    public static function getModuleOption($option, $withCache = true)
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
152
//    {
153
//        global $xoopsModuleConfig, $xoopsModule;
154
//        $repmodule = self::MODULE_NAME;
155
//        static $options = [];
156
//        if (is_array($options) && array_key_exists($option, $options) && $withCache) {
157
//            return $options[$option];
158
//        }
159
//
160
//        $retval = false;
161
//        if (null !== $xoopsModuleConfig && (is_object($xoopsModule) && ($xoopsModule->getVar('dirname') == $repmodule) && $xoopsModule->getVar('isactive'))) {
162
//            if (isset($xoopsModuleConfig[$option])) {
163
//                $retval = $xoopsModuleConfig[$option];
164
//            }
165
//        } else {
166
//            /** @var \XoopsModuleHandler $moduleHandler */
167
//            $moduleHandler = xoops_getHandler('module');
168
//            $module        = $moduleHandler->getByDirname($repmodule);
169
//            $configHandler = xoops_getHandler('config');
170
//            if ($module) {
171
//                $moduleConfig = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
172
//                if (isset($moduleConfig[$option])) {
173
//                    $retval = $moduleConfig[$option];
174
//                }
175
//            }
176
//        }
177
//        $options[$option] = $retval;
178
//
179
//        return $retval;
180
//    }
181
182
    /**
183
     * Is Xoops 2.3.x ?
184
     *
185
     * @return boolean need to say it ?
186
     */
187
    //    function isX23()
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
188
    //    {
189
    //        $x23 = false;
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
190
    //        $xv  = str_replace('XOOPS ', '', XOOPS_VERSION);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
191
    //        if ((int)(substr($xv, 2, 1)) >= 3) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
192
    //            $x23 = true;
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
193
    //        }
194
    //
195
    //        return $x23;
196
    //    }
197
198
    /**
199
     * Retreive an editor according to the module's option "form_options"
200
     *
201
     * @param string $caption Caption to give to the editor
202
     * @param string $name    Editor's name
203
     * @param string $value   Editor's value
204
     * @param string $width   Editor's width
205
     * @param string $height  Editor's height
206
     * @param string $supplemental
207
     *
208
     * @return \XoopsFormDhtmlTextArea|\XoopsFormEditor The editor to use
209
     */
210
    public static function getWysiwygForm(
211
        $caption,
212
        $name,
213
        $value = '',
214
        $width = '100%',
0 ignored issues
show
Unused Code introduced by
The parameter $width is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
215
        $height = '400px',
0 ignored issues
show
Unused Code introduced by
The parameter $height is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
216
        $supplemental = ''
0 ignored issues
show
Unused Code introduced by
The parameter $supplemental is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
217
    ) {
218
        global $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...
219
        if (class_exists('XoopsFormEditor')) {
220
            $options['name']   = $name;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = 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...
221
            $options['value']  = $value;
222
            $options['rows']   = 35;
223
            $options['cols']   = '100%';
224
            $options['width']  = '100%';
225
            $options['height'] = '400px';
226
            $editor            = new \XoopsFormEditor($caption, $xoopsModuleConfig['form_options'], $options, $nohtml = false, $onfailure = 'textarea');
227
        } else {
228
            $editor = new \XoopsFormDhtmlTextArea($caption, $name, $value, '100%', '100%');
229
        }
230
231
        return $editor;
232
    }
233
234
    /**
235
     * Create (in a link) a javascript confirmation's box
236
     *
237
     * @param string  $message Message to display
238
     * @param boolean $form    Is this a confirmation for a form ?
239
     *
240
     * @return string the javascript code to insert in the link (or in the form)
241
     */
242
    public static function javascriptLinkConfirm($message, $form = false)
243
    {
244
        if (!$form) {
245
            return "onclick=\"javascript:return confirm('" . str_replace("'", ' ', $message) . "')\"";
246
        } else {
247
            return "onSubmit=\"javascript:return confirm('" . str_replace("'", ' ', $message) . "')\"";
248
        }
249
    }
250
251
    /**
252
     * Redirect user with a message
253
     *
254
     * @param string $message message to display
255
     * @param string $url     The place where to go
256
     * @param        integer  timeout Time to wait before to redirect
257
     */
258
    public static function redirect($message = '', $url = 'index.php', $time = 2)
259
    {
260
        redirect_header($url, $time, $message);
261
    }
262
263
    /**
264
     * Internal function used to get the handler of the current module
265
     *
266
     * @return \XoopsModule The module
267
     */
268
    protected static function getModule()
269
    {
270
        static $mymodule;
271
        if (null === $mymodule) {
272
            global $xoopsModule;
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...
273
            if (null !== $xoopsModule && is_object($xoopsModule) && REFERENCES_DIRNAME == $xoopsModule->getVar('dirname')) {
274
                $mymodule =& $xoopsModule;
275
            } else {
276
                $hModule  = xoops_getHandler('module');
277
                $mymodule = $hModule->getByDirname(REFERENCES_DIRNAME);
278
            }
279
        }
280
281
        return $mymodule;
282
    }
283
284
    /**
285
     * Returns the module's name (as defined by the user in the module manager) with cache
286
     *
287
     * @return string Module's name
288
     */
289
    public static function getModuleName()
290
    {
291
        static $moduleName;
292
        if (null === $moduleName) {
293
            $mymodule   = self::getModule();
294
            $moduleName = $mymodule->getVar('name');
295
        }
296
297
        return $moduleName;
298
    }
299
300
    /**
301
     * This function indicates if the current Xoops version needs to add asterisks to required fields in forms
302
     *
303
     * @return boolean Yes = we need to add them, false = no
304
     */
305
    public static function needsAsterisk()
306
    {
307
        if (self::isX23()) {
0 ignored issues
show
Bug introduced by
The method isX23() does not seem to exist on object<XoopsModules\Marquee\Utility>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
308
            return false;
309
        }
310
        if (false !== stripos(XOOPS_VERSION, 'impresscms')) {
311
            return false;
312
        }
313
        if (false === stripos(XOOPS_VERSION, 'legacy')) {
314
            $xv = xoops_trim(str_replace('XOOPS ', '', XOOPS_VERSION));
315
            if ((int)substr($xv, 4, 2) >= 17) {
316
                return false;
317
            }
318
        }
319
320
        return true;
321
    }
322
323
    /**
324
     * Mark the mandatory fields of a form with a star
325
     *
326
     * @param \XoopsThemeForm $sform The form to modify
327
     *
328
     * @internal param string $caracter The character to use to mark fields
329
     * @return \XoopsThemeForm The modified form
330
     */
331
    public static function formMarkRequiredFields(&$sform)
332
    {
333
        $required = $elements = [];
334
        if (self::needsAsterisk()) {
335
            foreach ($sform->getRequired() as $item) {
336
                $required[] = $item->_name;
337
            }
338
            $elements =& $sform->getElements();
339
            $cnt      = count($elements);
340
            for ($i = 0; $i < $cnt; ++$i) {
341
                if (is_object($elements[$i]) && in_array($elements[$i]->_name, $required)) {
342
                    $elements[$i]->_caption .= ' *';
343
                }
344
            }
345
        }
346
347
        return $sform;
348
    }
349
350 View Code Duplication
    public static function getModuleOption($option, $repmodule = 'marquee')
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Duplication introduced by
This method 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...
351
    {
352
        global $xoopsModuleConfig, $xoopsModule;
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...
353
        static $tbloptions = [];
354
        if (is_array($tbloptions) && array_key_exists($option, $tbloptions)) {
355
            return $tbloptions[$option];
356
        }
357
358
        $retval = false;
359
        if (null !== $xoopsModuleConfig
360
            && (is_object($xoopsModule) && $xoopsModule->getVar('dirname') == $repmodule
361
                && $xoopsModule->getVar('isactive'))) {
362
            if (isset($xoopsModuleConfig[$option])) {
363
                $retval = $xoopsModuleConfig[$option];
364
            }
365
        } else {
366
            /** @var \XoopsModuleHandler $moduleHandler */
367
            $moduleHandler = xoops_getHandler('module');
368
            $module        = $moduleHandler->getByDirname($repmodule);
369
            $configHandler = xoops_getHandler('config');
370
            if ($module) {
371
                $moduleConfig = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
372
                if (isset($moduleConfig[$option])) {
373
                    $retval = $moduleConfig[$option];
374
                }
375
            }
376
        }
377
        $tbloptions[$option] = $retval;
378
379
        return $retval;
380
    }
381
382
    /**
383
     * Verify if the current "user" is a bot or not
384
     *
385
     * If you have a problem with this function, insert the folowing code just before the line if (isset($_SESSION['news_cache_bot'])) { :
386
     * return false;
387
     *
388
     * @package          Marquee
389
     * @author           Hervé Thouzard (http://www.herve-thouzard.com)
390
     * @copyright    (c) Hervé Thouzard
391
     */
392 View Code Duplication
    public static function isBot()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Duplication introduced by
This method 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
isBot uses the super-global variable $_SESSION 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...
393
    {
394
        if (isset($_SESSION['marquee_cache_bot'])) {
395
            return $_SESSION['marquee_cache_bot'];
396
        } else {
397
            // Add here every bot you know separated by a pipe | (not matter with the upper or lower cases)
398
            // If you want to see the result for yourself, add your navigator's user agent at the end (mozilla for example)
399
            $botlist      = 'AbachoBOT|Arachnoidea|ASPSeek|Atomz|cosmos|crawl25-public.alexa.com|CrawlerBoy Pinpoint.com|Crawler|DeepIndex|EchO!|exabot|Excalibur Internet Spider|FAST-WebCrawler|Fluffy the spider|GAIS Robot/1.0B2|GaisLab data gatherer|Google|Googlebot-Image|googlebot|Gulliver|ia_archiver|Infoseek|Links2Go|Lycos_Spider_(modspider)|Lycos_Spider_(T-Rex)|MantraAgent|Mata Hari|Mercator|MicrosoftPrototypeCrawler|[email protected]|MSNBOT|NEC Research Agent|NetMechanic|Nokia-WAPToolkit|nttdirectory_robot|Openfind|Oracle Ultra Search|PicoSearch|Pompos|Scooter|Slider_Search_v1-de|Slurp|Slurp.so|SlySearch|Spider|Spinne|SurferF3|Surfnomore Spider|suzuran|teomaagent1|TurnitinBot|Ultraseek|VoilaBot|vspider|W3C_Validator|Web Link Validator|WebTrends|WebZIP|whatUseek_winona|WISEbot|Xenu Link Sleuth|ZyBorg';
400
            $botlist      = strtoupper($botlist);
401
            $currentagent = strtoupper(xoops_getenv('HTTP_USER_AGENT'));
402
            $retval       = false;
403
            $botarray     = explode('|', $botlist);
404
            foreach ($botarray as $onebot) {
405
                if (false !== strpos($currentagent, $onebot)) {
406
                    $retval = true;
407
                    break;
408
                }
409
            }
410
        }
411
        $_SESSION['marquee_cache_bot'] = $retval;
412
413
        return $retval;
414
    }
415
416
    /**
417
     * Escape a string so that it can be included in a javascript string
418
     *
419
     * @param $string
420
     *
421
     * @return mixed
422
     */
423
    public static function javascriptEscape($string)
424
    {
425
        return str_replace("'", "\\'", $string);
426
    }
427
}
428