Utility   A
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 233
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 81
c 1
b 0
f 0
dl 0
loc 233
rs 9.52
wmc 36

10 Methods

Rating   Name   Duplication   Size   Complexity  
A redirect() 0 3 1
A formMarkRequiredFields() 0 16 6
A getWysiwygForm() 0 22 2
A needsAsterisk() 0 10 3
B getModuleOption() 0 31 10
A isBot() 0 21 4
A getInstance() 0 8 2
A javascriptLinkConfirm() 0 7 2
A getModule() 0 15 5
A javascriptEscape() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Marquee;
4
5
use Xmf\Request;
6
7
/** @var Helper $helper */
8
9
/**
10
 * Class Utility
11
 */
12
class Utility extends Common\SysUtility
13
{
14
    //--------------- Custom module methods -----------------------------
15
    public const MODULE_NAME = 'marquee';
16
17
    /**
18
     * Access the only instance of this class
19
     *
20
     * @return \XoopsModules\Marquee\Utility
21
     *
22
     * @static
23
     * @staticvar   object
24
     */
25
    public static function getInstance()
26
    {
27
        static $instance;
28
        if (null === $instance) {
29
            $instance = new static();
30
        }
31
32
        return $instance;
33
    }
34
35
    /**
36
     * Retrieve an editor according to the module's option "form_options"
37
     *
38
     * @param string $caption Caption to give to the editor
39
     * @param string $name    Editor's name
40
     * @param string $value   Editor's value
41
     * @param string $width   Editor's width
42
     * @param string $height  Editor's height
43
     * @param string $supplemental
44
     *
45
     * @return \XoopsFormDhtmlTextArea|\XoopsFormEditor The editor to use
46
     */
47
    public static function getWysiwygForm(
48
        $caption,
49
        $name,
50
        $value = '',
51
        $width = '100%',
0 ignored issues
show
Unused Code introduced by
The parameter $width is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

51
        /** @scrutinizer ignore-unused */ $width = '100%',

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

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

52
        /** @scrutinizer ignore-unused */ $height = '400px',

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

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

53
        /** @scrutinizer ignore-unused */ $supplemental = ''

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

Loading history...
54
    ) {
55
        $helper = Helper::getInstance();
56
        if (\class_exists('XoopsFormEditor')) {
57
            $options['name']   = $name;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = array(); before regardless.
Loading history...
58
            $options['value']  = $value;
59
            $options['rows']   = 35;
60
            $options['cols']   = '100%';
61
            $options['width']  = '100%';
62
            $options['height'] = '400px';
63
            $editor            = new \XoopsFormEditor($caption, $helper->getConfig('form_options'), $options, $nohtml = false, $onfailure = 'textarea');
64
        } else {
65
            $editor = new \XoopsFormDhtmlTextArea($caption, $name, $value);
66
        }
67
68
        return $editor;
69
    }
70
71
    /**
72
     * Create (in a link) a javascript confirmation's box
73
     *
74
     * @param string $message Message to display
75
     * @param bool   $form    Is this a confirmation for a form ?
76
     *
77
     * @return string the javascript code to insert in the link (or in the form)
78
     */
79
    public static function javascriptLinkConfirm($message, $form = false)
80
    {
81
        if (!$form) {
82
            return "onclick=\"javascript:return confirm('" . \str_replace("'", ' ', $message) . "')\"";
83
        }
84
85
        return "onSubmit=\"javascript:return confirm('" . \str_replace("'", ' ', $message) . "')\"";
86
    }
87
88
    /**
89
     * Redirect user with a message
90
     *
91
     * @param string $message message to display
92
     * @param string $url     The place where to go
93
     * @param mixed  $time
94
     */
95
    public static function redirect($message = '', $url = 'index.php', $time = 2): void
96
    {
97
        \redirect_header($url, $time, $message);
98
    }
99
100
    /**
101
     * Internal function used to get the handler of the current module
102
     * @return \XoopsModule The module
103
     * @deprecated  use $helper->getModule();
104
     */
105
    protected static function getModule()
106
    {
107
        $moduleDirName = \basename(\dirname(__DIR__));
108
        static $mymodule;
109
        if (null === $mymodule) {
110
            global $xoopsModule;
111
            if (null !== $xoopsModule && \is_object($xoopsModule) && $moduleDirName == $xoopsModule->getVar('dirname')) {
112
                $mymodule = $xoopsModule;
113
            } else {
114
                $moduleHandler = \xoops_getHandler('module');
115
                $mymodule      = $moduleHandler->getByDirname($moduleDirName);
0 ignored issues
show
Bug introduced by
The method getByDirname() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsModuleHandler or XoopsPersistableObjectHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

115
                /** @scrutinizer ignore-call */ 
116
                $mymodule      = $moduleHandler->getByDirname($moduleDirName);
Loading history...
116
            }
117
        }
118
119
        return $mymodule;
120
    }
121
122
    /**
123
     * This function indicates if the current Xoops version needs to add asterisks to required fields in forms
124
     *
125
     * @return bool Yes = we need to add them, false = no
126
     */
127
    public static function needsAsterisk()
128
    {
129
        if (false === mb_stripos(\XOOPS_VERSION, 'legacy')) {
130
            $xv = \xoops_trim(\str_replace('XOOPS ', '', \XOOPS_VERSION));
131
            if ((int)mb_substr($xv, 4, 2) >= 17) {
132
                return false;
133
            }
134
        }
135
136
        return true;
137
    }
138
139
    /**
140
     * Mark the mandatory fields of a form with a star
141
     *
142
     * @param \XoopsThemeForm $sform The form to modify
143
     *
144
     * @return \XoopsThemeForm The modified form
145
     * @internal param string $caracter The character to use to mark fields
146
     */
147
    public static function formMarkRequiredFields($sform)
148
    {
149
        $required = $elements = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $elements is dead and can be removed.
Loading history...
150
        if (self::needsAsterisk()) {
151
            foreach ($sform->getRequired() as $item) {
152
                $required[] = $item->_name;
153
            }
154
            $elements = &$sform->getElements();
155
            foreach ($elements as $i => $iValue) {
156
                if (\is_object($iValue) && \in_array($iValue->_name, $required, true)) {
157
                    $iValue->_caption .= ' *';
158
                }
159
            }
160
        }
161
162
        return $sform;
163
    }
164
165
    /**
166
     * @param        $option
167
     * @param string $repmodule
168
     * @return bool
169
     */
170
    public static function getModuleOption($option, $repmodule = 'marquee')
171
    {
172
        global $xoopsModule;
173
        $helper = Helper::getInstance();
174
        static $tbloptions = [];
175
        if (\is_array($tbloptions) && \array_key_exists($option, $tbloptions)) {
176
            return $tbloptions[$option];
177
        }
178
        $retval = false;
179
        if (null !== $helper->getModule()
180
            && (\is_object($xoopsModule) && $xoopsModule->getVar('dirname') == $repmodule
181
                && $xoopsModule->getVar('isactive'))) {
182
            if ('' !== $helper->getConfig($option)) {
183
                $retval = $helper->getConfig($option);
184
            }
185
        } else {
186
            /** @var \XoopsModuleHandler $moduleHandler */
187
            $moduleHandler = \xoops_getHandler('module');
188
            $module        = $moduleHandler->getByDirname($repmodule);
189
            /** @var \XoopsConfigHandler $configHandler */
190
            $configHandler = \xoops_getHandler('config');
191
            if ($module) {
192
                $moduleConfig = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
193
                if (isset($moduleConfig[$option])) {
194
                    $retval = $moduleConfig[$option];
195
                }
196
            }
197
        }
198
        $tbloptions[$option] = $retval;
199
200
        return $retval;
201
    }
202
203
    /**
204
     * Verify if the current "user" is a bot or not
205
     *
206
     * If you have a problem with this function, insert the folowing code just before the line if (\Xmf\Request::hasVar('news_cache_bot', 'SESSION'))) { :
207
     * return false;
208
     *
209
     * @author           Hervé Thouzard (https://www.herve-thouzard.com)
210
     * @copyright    (c) Hervé Thouzard
211
     */
212
    public static function isBot()
213
    {
214
        if (Request::hasVar('marquee_cache_bot', 'SESSION')) {
215
            return $_SESSION['marquee_cache_bot'];
216
        }
217
        // Add here every bot you know separated by a pipe | (not matter with the upper or lower cases)
218
        // If you want to see the result for yourself, add your navigator's user agent at the end (mozilla for example)
219
        $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';
220
        $botlist      = \mb_strtoupper($botlist);
221
        $currentagent = \mb_strtoupper(\xoops_getenv('HTTP_USER_AGENT'));
222
        $retval       = false;
223
        $botarray     = \explode('|', $botlist);
224
        foreach ($botarray as $onebot) {
225
            if (false !== \mb_strpos($currentagent, $onebot)) {
226
                $retval = true;
227
                break;
228
            }
229
        }
230
        $_SESSION['marquee_cache_bot'] = $retval;
231
232
        return $retval;
233
    }
234
235
    /**
236
     * Escape a string so that it can be included in a javascript string
237
     *
238
     * @param string $string
239
     *
240
     * @return mixed
241
     */
242
    public static function javascriptEscape($string)
243
    {
244
        return \str_replace("'", "\\'", $string);
245
    }
246
}
247