Completed
Push — master ( 96da4e...7c5656 )
by Michael
04:52
created

PlanetUtility::planetParseFunction()   D

Complexity

Conditions 10
Paths 5

Size

Total Lines 26
Code Lines 18

Duplication

Lines 26
Ratio 100 %

Importance

Changes 0
Metric Value
cc 10
eloc 18
nc 5
nop 3
dl 26
loc 26
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
2
//
3
// ------------------------------------------------------------------------ //
4
// This program is free software; you can redistribute it and/or modify     //
5
// it under the terms of the GNU General Public License as published by     //
6
// the Free Software Foundation; either version 2 of the License, or        //
7
// (at your option) any later version.                                      //
8
//                                                                          //
9
// You may not change or alter any portion of this comment or credits       //
10
// of supporting developers from this source code or any supporting         //
11
// source code which is considered copyrighted (c) material of the          //
12
// original comment or credit authors.                                      //
13
//                                                                          //
14
// This program is distributed in the hope that it will be useful,          //
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of           //
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
17
// GNU General Public License for more details.                             //
18
//                                                                          //
19
// You should have received a copy of the GNU General Public License        //
20
// along with this program; if not, write to the Free Software              //
21
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
22
// ------------------------------------------------------------------------ //
23
// Author: phppp (D.J., [email protected])                                  //
24
// URL: https://xoops.org                         //
25
// Project: Article Project                                                 //
26
// ------------------------------------------------------------------------ //
27
use Xmf\Request;
28
29
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
30
31
$current_path = __FILE__;
32 View Code Duplication
if (DIRECTORY_SEPARATOR !== '/') {
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...
33
    $current_path = str_replace(strpos($current_path, '\\\\', 2) ? '\\\\' : DIRECTORY_SEPARATOR, '/', $current_path);
34
}
35
$url_arr               = explode('/', strstr($current_path, '/modules/'));
36
$GLOBALS['moddirname'] = $url_arr[2];
37
38
if (!defined('planet_FUNCTIONS')):
39
    define('planet_FUNCTIONS', 1);
40
41
    require XOOPS_ROOT_PATH . '/modules/' . $GLOBALS['moddirname'] . '/include/vars.php';
42
    require_once XOOPS_ROOT_PATH . '/class/xoopslists.php';
43
    require_once XOOPS_ROOT_PATH . '/Frameworks/art/functions.php';
44
45
    /**
46
     * Class PlanetUtility
47
     */
48
    class PlanetUtility
49
    {
50
51
        /**
52
         * Function to display messages
53
         *
54
         * @var mixed $messages
55
         * @return bool
56
         */
57
        public static function planetDisplayMessage($message)
58
        {
59
            return mod_message($message);
60
        }
61
62
        /**
63
         * Function to parse arguments for a page according to $_SERVER['REQUEST_URI']
64
         *
65
         * @var array $args_numeric array of numeric variable values
66
         * @var array $args         array of indexed variables: name and value
67
         * @var array $args_string  array of string variable values
68
         *
69
         * @return bool true on args parsed
70
         */
71
72
        /* known issues:
73
         * - "/" in a string
74
         * - "&" in a string
75
        */
76 View Code Duplication
        public static function planetParseArguments(&$args_numeric, &$args, &$args_string)
0 ignored issues
show
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...
77
        {
78
            $args_abb     = array(
79
                'a' => 'article',
80
                'b' => 'blog',
81
                'c' => 'category',
82
                'l' => 'list',
83
                'o' => 'sort',
84
                's' => 'start',
85
                'u' => 'uid'
86
            );
87
            $args         = array();
88
            $args_numeric = array();
89
            $args_string  = array();
90
            if (preg_match("/[^\?]*\.php[\/|\?]([^\?]*)/i", Request::getUrl('REQUEST_URI', '', 'SERVER'), $matches)) {
91
                $vars = preg_split("/[\/|&]/", $matches[1]);
92
                $vars = array_map('trim', $vars);
93
                if (count($vars) > 0) {
94
                    foreach ($vars as $var) {
95
                        if (is_numeric($var)) {
96
                            $args_numeric[] = $var;
97
                        } elseif (false === strpos($var, '=')) {
98
                            if (is_numeric(substr($var, 1))) {
99
                                $args[$args_abb[strtolower($var{0})]] = (int)substr($var, 1);
100
                            } else {
101
                                $args_string[] = urldecode($var);
102
                            }
103
                        } else {
104
                            parse_str($var, $args);
105
                        }
106
                    }
107
                }
108
            }
109
110
            return (count($args) + count($args_numeric) + count($args_string) == 0) ? null : true;
111
        }
112
113
        /**
114
         * Function to parse class prefix
115
         *
116
         * @var string $class_string string to be parsed
117
         * @var mixed  $pattern
118
         * @var mixed  $replacement
119
         *
120
         * @return bool true on success
121
         */
122 View Code Duplication
        public static function planetParseClass($class_string, $pattern = '', $replacement = '')
0 ignored issues
show
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...
123
        {
124
            if (empty($class_string)) {
125
                return;
126
            }
127
            $patterns     = array("/\[CLASS_PREFIX\]/");
128
            $replacements = array(ucfirst(strtolower($GLOBALS['moddirname'])));
129
            if (!empty($pattern) && !is_array($pattern) && !is_array($replacement)) {
130
                $pattern     = array($pattern);
131
                $replacement = array($replacement);
132
            }
133
            if (is_array($pattern) && count($pattern) > 0) {
134
                $ii = 0;
135
                foreach ($pattern as $pat) {
136
                    if (!in_array($pat, $patterns)) {
137
                        $patterns[]     = $pat;
138
                        $replacements[] = isset($replacement[$ii]) ? $replacement[$ii] : '';
139
                    }
140
                    ++$ii;
141
                }
142
            }
143
            $class_string = preg_replace($patterns, $replacements, $class_string);
144
            eval($class_string);
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
145
146
            return true;
147
        }
148
149
        /**
150
         * Function to parse function prefix
151
         *
152
         * @var string $function_string string to be parsed
153
         * @var mixed  $pattern
154
         * @var mixed  $replacement
155
         *
156
         * @return bool true on success
157
         */
158 View Code Duplication
        public static function planetParseFunction($function_string, $pattern = '', $replacement = '')
0 ignored issues
show
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...
159
        {
160
            if (empty($function_string)) {
161
                return;
162
            }
163
            $patterns     = array("/\[DIRNAME\]/", "/\[VAR_PREFIX\]/");
164
            $replacements = array($GLOBALS['moddirname'], $GLOBALS['VAR_PREFIX']);
165
            if (!empty($pattern) && !is_array($pattern) && !is_array($replacement)) {
166
                $pattern     = array($pattern);
167
                $replacement = array($replacement);
168
            }
169
            if (is_array($pattern) && count($pattern) > 0) {
170
                $ii = 0;
171
                foreach ($pattern as $pat) {
172
                    if (!in_array($pat, $patterns)) {
173
                        $patterns[]     = $pat;
174
                        $replacements[] = isset($replacement[$ii]) ? $replacement[$ii] : '';
175
                    }
176
                    ++$ii;
177
                }
178
            }
179
            $function_string = preg_replace($patterns, $replacements, $function_string);
180
            eval($function_string);
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
181
182
            return true;
183
        }
184
185
        /**
186
         * Function to convert UNIX time to formatted time string
187
         * @param        $time
188
         * @param string $format
189
         * @return string
190
         */
191
        public static function planetFormatTimestamp($time, $format = '')
192
        {
193
            if (empty($time)) {
194
                return '';
195
            }
196
197
            return formatTimestamp($time, $format);
198
        }
199
200
        /**
201
         * Function to a list of user names associated with their user IDs
202
         * @param int  $userid
203
         * @param int  $usereal
204
         * @param bool $linked
205
         * @return array
206
         */
207 View Code Duplication
        public static function &planetGetUnameFromId($userid, $usereal = 0, $linked = false)
0 ignored issues
show
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...
208
        {
209
            if (!is_array($userid)) {
210
                $userid = array($userid);
211
            }
212
            $users =& mod_getUnameFromIds($userid, $usereal, $linked);
213
214
            return $users;
215
        }
216
217
        /**
218
         * Function to parse links, links are delimited by link break, URL and title of a link are delimited by space
219
         *
220
         * @var string $text raw content
221
         *
222
         * @return array associative array of link url and title
223
         */
224 View Code Duplication
        public static function &planetParseLinks($text)
0 ignored issues
show
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...
225
        {
226
            $myts       = MyTextSanitizer::getInstance();
227
            $link_array = preg_split("/(\r\n|\r|\n)( *)/", $text);
228
            $links      = array();
229
            if (count($link_array) > 0) {
230
                foreach ($link_array as $link) {
231
                    @list($url, $title) = array_map('trim', preg_split('/ /', $link, 2));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
232
                    if (empty($url)) {
233
                        continue;
234
                    }
235
                    //if(empty($title)) $title = $url;
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% 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...
236
                    $links[] = array('url' => $url, 'title' => $myts->htmlSpecialChars($title));
237
                }
238
            }
239
240
            return $links;
241
        }
242
243
        /**
244
         * @param $pagename
245
         * @return string
246
         */
247
        public static function planetGetTemplate($pagename)
248
        {
249
            return $GLOBALS['VAR_PREFIX'] . '_' . $pagename . '.tpl';
250
        }
251
252
        /**
253
         * @param int $currentoption
254
         */
255
        //public static function planet_adminmenu($currentoption = -1)
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% 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...
256
        //{
257
        //    loadModuleAdminMenu($currentoption, '');
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
258
        //
259
        //    return;
260
        //}
261
262
        /**
263
         * Function to send a trackback
264
         *
265
         * @param $article
266
         * @param $comment
267
         * @return bool
268
         */
269 View Code Duplication
        public static function planetSendTrackback(&$article, &$comment)
0 ignored issues
show
Unused Code introduced by
The parameter $comment 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...
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...
270
        {
271
            $blogHandler = xoops_getModuleHandler('blog', $GLOBALS['moddirname']);
272
            $blog_obj    = $blogHandler->get($article->getVar('blog_id'));
273
            if (!$pattern = $blog_obj->getVar('blog_trackback')) {
274
                return false;
275
            }
276
277
            @list($pat, $rep) = array_map('trim', preg_split("#[\s]+#", $pattern));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
278
            $trackback_url = preg_replace('#' . $pat . '#', $rep, $article_obj->getVar('art_link'));
0 ignored issues
show
Bug introduced by
The variable $article_obj does not exist. Did you mean $article?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
279
280
            return static::planetTrackback($trackback_url, $article);
281
        }
282
283
        /**
284
         * @param $trackback_url
285
         * @param $article
286
         * @return bool
287
         */
288 View Code Duplication
        public static function planetTrackback($trackback_url, $article)
0 ignored issues
show
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...
289
        {
290
            global $myts, $xoopsConfig, $xoopsModule, $xoopsModuleConfig;
291
292
            $title         = $article->getVar('art_title');
293
            $excerpt       = $article->getVar('art_content');
294
            $blog_name     = $xoopsConfig['sitename'] . '-' . $xoopsModule->getVar('name');
295
            $title         = xoops_utf8_encode($title);
296
            $excerpt       = xoops_utf8_encode($excerpt);
297
            $blog_name     = xoops_utf8_encode($blog_name);
298
            $charset       = 'utf-8';
299
            $title1        = urlencode($title);
300
            $excerpt1      = urlencode($excerpt);
301
            $name1         = urlencode($blog_name);
302
            $url           = urlencode(XOOPS_URL . '/modules/' . $GLOBALS['moddirname'] . '/view.article.php' . URL_DELIMITER . '' . $article->getVar('art_id'));
303
            $query_string  = "title=$title1&url=$url&blog_name=$name1&excerpt=$excerpt1&charset=$charset";
304
            $trackback_url = parse_url($trackback_url);
305
306
            $http_request = 'POST ' . $trackback_url['path'] . ($trackback_url['query'] ? '?' . $trackback_url['query'] : '') . " HTTP/1.0\r\n";
307
            $http_request .= 'Host: ' . $trackback_url['host'] . "\r\n";
308
            $http_request .= 'Content-Type: application/x-www-form-urlencoded; charset=' . $charset . "\r\n";
309
            $http_request .= 'Content-Length: ' . strlen($query_string) . "\r\n";
310
            $http_request .= 'User-Agent: XOOPS Blogs/' . XOOPS_VERSION;
311
            $http_request .= "\r\n\r\n";
312
            $http_request .= $query_string;
313
            if ('' == $trackback_url['port']) {
314
                $trackback_url['port'] = 80;
315
            }
316
            $fs = @fsockopen($trackback_url['host'], $trackback_url['port'], $errno, $errstr, 4);
317
            @fwrite($fs, $http_request);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
318
            if ($xoopsModuleConfig['do_debug']) {
319
                $debug_file = XOOPS_CACHE_PATH . '/' . $GLOBALS['moddirname'] . '_trackback.log';
320
                $fr         = "\n*****\nRequest:\n\n$http_request\n\nResponse:\n\n";
321
                $fr         .= "CHARSET:$charset\n";
322
                $fr         .= "NAME:$blog_name\n";
323
                $fr         .= 'TITLE:' . $title . "\n";
324
                $fr         .= "EXCERPT:$excerpt\n\n";
325
                while (!@feof($fs)) {
326
                    $fr .= @fgets($fs, 4096);
327
                }
328
                $fr .= "\n\n";
329
330
                if ($fp = fopen($debug_file, 'a')) {
331
                    fwrite($fp, $fr);
332
                    fclose($fp);
333
                } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches 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 else branches can be removed.

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

could be turned into

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

This is much more concise to read.

Loading history...
334
                }
335
            }
336
            @fclose($fs);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
337
338
            return true;
339
        }
340
341
        /**
342
         * Function to ping servers
343
         * @param $server
344
         * @param $id
345
         */
346 View Code Duplication
        public static function planetGetPing($server, $id)
0 ignored issues
show
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...
347
        {
348
            if (is_array($server)) {
349
                foreach ($server as $serv) {
350
                    PlanetUtility::planetGetPing($serv, $id);
351
                }
352
            }
353
            require_once XOOPS_ROOT_PATH . '/modules/' . $GLOBALS['moddirname'] . '/class-IXR.php';
354
355
            // using a timeout of 3 seconds should be enough to cover slow servers
356
            $client            = new IXR_Client($server, false);
357
            $client->timeout   = 3;
358
            $client->useragent .= ' -- XOOPS Article/' . XOOPS_VERSION;
359
360
            // when set to true, this outputs debug messages by itself
361
            $client->debug = false;
362
363
            $blogname = xoops_utf8_encode($GLOBALS['xoopsModule']->getVar('name'));
364
            $home     = XOOPS_URL . '/modules/' . $GLOBALS['moddirname'] . '/';
365
            $rss2_url = XOOPS_URL . '/modules/' . $GLOBALS['moddirname'] . '/xml.php' . URL_DELIMITER . 'rss2.0/' . $id;
366
367
            if (!$client->query('weblogUpdates.extendedPing', $blogname, $home, $rss2_url)) { // then try a normal ping
368
                $client->query('weblogUpdates.ping', $blogname, $home);
369
            }
370
        }
371
372
        /**
373
         * Function to respond to a trackback
374
         * @param int    $error
375
         * @param string $error_message
376
         */
377 View Code Duplication
        public static function planetRespondToTrackback($error = 0, $error_message = '')
0 ignored issues
show
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...
378
        {
379
            $charset       = 'utf-8';
380
            $error_message = xoops_utf8_encode($error_message);
381
            header('Content-Type: text/xml; charset="' . $charset . '"');
382
            if ($error) {
383
                echo '<?xml version="1.0" encoding="' . $charset . '"?' . ">\n";
384
                echo "<response>\n";
385
                echo "<error>1</error>\n";
386
                echo "<message>$error_message</message>\n";
387
                echo '</response>';
388
                die();
389
            } else {
390
                echo '<?xml version="1.0" encoding="' . $charset . '"?' . ">\n";
391
                echo "<response>\n";
392
                echo "<error>0</error>\n";
393
                echo '</response>';
394
            }
395
        }
396
397
        /**
398
         * Function to set a cookie with module-specified name
399
         *
400
         * using customized serialization method
401
         * @param        $name
402
         * @param string $string
403
         * @param int    $expire
404
         */
405 View Code Duplication
        public static function planetSetCookie($name, $string = '', $expire = 0)
0 ignored issues
show
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...
406
        {
407
            if (is_array($string)) {
408
                $value = array();
409
                foreach ($string as $key => $val) {
410
                    $value[] = $key . '|' . $val;
411
                }
412
                $string = implode(',', $value);
413
            }
414
            setcookie($GLOBALS['VAR_PREFIX'] . $name, $string, (int)$expire, '/');
415
        }
416
417
        /**
418
         * @param      $name
419
         * @param bool $isArray
420
         * @return array|null
421
         */
422 View Code Duplication
        public static function planetGetCookie($name, $isArray = false)
0 ignored issues
show
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...
423
        {
424
            $value = isset($_COOKIE[$GLOBALS['VAR_PREFIX'] . $name]) ? $_COOKIE[$GLOBALS['VAR_PREFIX'] . $name] : null;
425
            if ($isArray) {
426
                $_value = $value ? explode(',', $value) : array();
427
                $value  = array();
428
                if (count($_value) > 0) {
429
                    foreach ($_value as $string) {
430
                        $key         = substr($string, 0, strpos($string, '|'));
431
                        $val         = substr($string, strpos($string, '|') + 1);
432
                        $value[$key] = $val;
433
                    }
434
                }
435
                unset($_value);
436
            }
437
438
            return $value;
439
        }
440
441
        /**
442
         * Function to filter text
443
         *
444
         * @param $document
445
         * @return string filtered text
446
         */
447
        public static function &planetHtml2text(&$document)
448
        {
449
            $document = strip_tags($document);
450
451
            return $document;
452
        }
453
454
        // Adapted from PMA_getIp() [phpmyadmin project]
455
456
        /**
457
         * @param bool $asString
458
         * @return mixed
459
         */
460
        public static function planetGetIP($asString = false)
461
        {
462
            return mod_getIP($asString);
463
        }
464
465
        /**
466
         * @param $url
467
         * @return bool|mixed|string
468
         */
469
        public static function planetGetRemoteContent($url)
470
        {
471
            if ($data = static::planetFetchSnoopy($url)) {
472
                return $data;
473
            }
474
            if ($data = static::planetFetchCURL($url)) {
475
                return $data;
476
            }
477
            if ($data = static::planetFetchFopen($url)) {
478
                return $data;
479
            }
480
481
            return false;
482
        }
483
484
        /**
485
         * @param $url
486
         * @return string
487
         */
488 View Code Duplication
        public static function planetFetchSnoopy($url)
0 ignored issues
show
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...
489
        {
490
            require_once XOOPS_ROOT_PATH . '/class/snoopy.php';
491
            $snoopy = new Snoopy;
492
            $data   = '';
493
            if (@$snoopy->fetch($url)) {
494
                $data = is_array($snoopy->results) ? implode("\n", $snoopy->results) : $snoopy->results;
495
            }
496
497
            return $data;
498
        }
499
500
        /**
501
         * @param $url
502
         * @return bool|mixed
503
         */
504 View Code Duplication
        public static function planetFetchCURL($url)
0 ignored issues
show
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...
505
        {
506
            if (!function_exists('curl_init')) {
507
                return false;
508
            }
509
            $ch = curl_init();    // initialize curl handle
510
            curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
511
            curl_setopt($ch, CURLOPT_FAILONERROR, 1);
512
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
513
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return into a variable
514
            curl_setopt($ch, CURLOPT_TIMEOUT, 30); // times out after 31s
515
            $data = curl_exec($ch); // run the whole process
516
            curl_close($ch);
517
518
            return $data;
519
        }
520
521
        /**
522
         * @param $url
523
         * @return bool|string
524
         */
525 View Code Duplication
        public static function planetFetchFopen($url)
0 ignored issues
show
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...
526
        {
527
            if (!$fp = @fopen($url, 'r')) {
528
                return false;
529
            }
530
            $data = '';
531
            while (!feof($fp)) {
532
                $data .= fgets($fp, 1024);
533
            }
534
            fclose($fp);
535
536
            return $data;
537
        }
538
539
        /**
540
         * @param     $haystack
541
         * @param     $needle
542
         * @param int $offset
543
         * @return bool|int
544
         */
545 View Code Duplication
        public static function planetStrrPos($haystack, $needle, $offset = 0)
0 ignored issues
show
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...
546
        {
547
            if (substr(PHP_VERSION, 0, 1) == 5) {
548
                return strrpos($haystack, $needle, $offset);
549
            }
550
            $index = strpos(strrev($haystack), strrev($needle));
551
            if ($index === false) {
552
                return false;
553
            }
554
            $index = strlen($haystack) - strlen($needle) - $index;
555
556
            return $index;
557
        }
558
    }
559
560
endif;
561