Completed
Branch master (923121)
by Michael
05:29 queued 02:40
created

XoopsGTicket::__construct()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.439
cc 6
eloc 15
nc 6
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 31 and the first side effect is on line 414.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/*
3
-------------------------------------------------------------------------
4
                     ADSLIGHT 2 : Module for Xoops
5
6
        Redesigned and ameliorate By Luc Bizet user at www.frxoops.org
7
        Started with the Classifieds module and made MANY changes
8
        Website : http://www.luc-bizet.fr
9
        Contact : [email protected]
10
-------------------------------------------------------------------------
11
             Original credits below Version History
12
##########################################################################
13
#                    Classified Module for Xoops                         #
14
#  By John Mordo user jlm69 at www.xoops.org and www.jlmzone.com         #
15
#      Started with the MyAds module and made MANY changes               #
16
##########################################################################
17
 Original Author: Pascal Le Boustouller
18
 Author Website : [email protected]
19
 Licence Type   : GPL
20
-------------------------------------------------------------------------
21
*/
22
23
// GIJOE's Ticket Class (based on Marijuana's Oreteki XOOPS)
24
// nobunobu's suggestions are applied
25
26
if (!class_exists('XoopsGTicket')) {
27
28
    /**
29
     * Class XoopsGTicket
30
     */
31
    class XoopsGTicket
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
32
    {
33
        public $_errors       = array();
34
        public $_latest_token = '';
35
        public $messages      = array();
36
37
        /**
38
         * XoopsGTicket constructor.
39
         */
40
        public function __construct()
41
        {
42
            global $xoopsConfig;
1 ignored issue
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...
43
44
            // language file
45
            if (defined('XOOPS_ROOT_PATH') && !empty($xoopsConfig['language']) && false === strpos($xoopsConfig['language'], '/')) {
46
                if (file_exists(dirname(__DIR__) . '/language/' . $xoopsConfig['language'] . '/gticket_messages.phtml')) {
47
                    include dirname(__DIR__) . '/language/' . $xoopsConfig['language'] . '/gticket_messages.phtml';
48
                }
49
            }
50
51
            // default messages
52
            if (empty($this->messages)) {
53
                $this->messages = array(
54
                    'err_general'       => 'GTicket Error',
55
                    'err_nostubs'       => 'No stubs found',
56
                    'err_noticket'      => 'No ticket found',
57
                    'err_nopair'        => 'No valid ticket-stub pair found',
58
                    'err_timeout'       => 'Time out',
59
                    'err_areaorref'     => 'Invalid area or referer',
60
                    'fmt_prompt4repost' => 'error(s) found:<br><span style="background-color:red;font-weight:bold;color:white;">%s</span><br>Confirm it.<br>And do you want to post again?',
61
                    'btn_repost'        => 'repost'
62
                );
63
            }
64
        }
65
66
        // render form as plain html
67
        /**
68
         * @param string $salt
69
         * @param int    $timeout
70
         * @param string $area
71
         *
72
         * @return string
73
         */
74
        public function getTicketHtml($salt = '', $timeout = 1800, $area = '')
75
        {
76
            return '<input type="hidden" name="XOOPS_G_TICKET" value="' . $this->issue($salt, $timeout, $area) . '" />';
77
        }
78
79
        // returns an object of XoopsFormHidden including theh ticket
80
        /**
81
         * @param string $salt
82
         * @param int    $timeout
83
         * @param string $area
84
         *
85
         * @return XoopsFormHidden
86
         */
87
        public function getTicketXoopsForm($salt = '', $timeout = 1800, $area = '')
88
        {
89
            return new XoopsFormHidden('XOOPS_G_TICKET', $this->issue($salt, $timeout, $area));
90
        }
91
92
        // add a ticket as Hidden Element into XoopsForm
93
        /**
94
         * @param        $form
95
         * @param string $salt
96
         * @param int    $timeout
97
         * @param string $area
98
         */
99
        public function addTicketXoopsFormElement(&$form, $salt = '', $timeout = 1800, $area = '')
100
        {
101
            $form->addElement(new XoopsFormHidden('XOOPS_G_TICKET', $this->issue($salt, $timeout, $area)));
102
        }
103
104
        // returns an array for xoops_confirm() ;
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% 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...
105
        /**
106
         * @param string $salt
107
         * @param int    $timeout
108
         * @param string $area
109
         *
110
         * @return array
111
         */
112
        public function getTicketArray($salt = '', $timeout = 1800, $area = '')
113
        {
114
            return array('XOOPS_G_TICKET' => $this->issue($salt, $timeout, $area));
115
        }
116
117
        // return GET parameter string.
118
        /**
119
         * @param string $salt
120
         * @param bool   $noamp
121
         * @param int    $timeout
122
         * @param string $area
123
         *
124
         * @return string
125
         */
126
        public function getTicketParamString($salt = '', $noamp = false, $timeout = 1800, $area = '')
127
        {
128
            return ($noamp ? '' : '&amp;') . 'XOOPS_G_TICKET=' . $this->issue($salt, $timeout, $area);
129
        }
130
131
        // issue a ticket
132
        /**
133
         * @param string $salt
134
         * @param int    $timeout
135
         * @param string $area
136
         *
137
         * @return string
138
         */
139
        public function issue($salt = '', $timeout = 1800, $area = '')
0 ignored issues
show
Coding Style introduced by
issue uses the super-global variable $_SERVER 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...
Coding Style introduced by
issue 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...
140
        {
141
            global $xoopsModule;
1 ignored issue
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...
142
143
            // create a token
144
            list($usec, $sec) = explode(' ', microtime());
145
            $appendix_salt       = empty($_SERVER['PATH']) ? XOOPS_DB_NAME : $_SERVER['PATH'];
146
            $token               = crypt($salt . $usec . $appendix_salt . $sec, $salt);
147
            $this->_latest_token = $token;
148
149
            if (empty($_SESSION['XOOPS_G_STUBS'])) {
150
                $_SESSION['XOOPS_G_STUBS'] = array();
151
            }
152
153
            // limit max stubs 10
154
            if (count($_SESSION['XOOPS_G_STUBS']) > 10) {
155
                $_SESSION['XOOPS_G_STUBS'] = array_slice($_SESSION['XOOPS_G_STUBS'], -10);
156
            }
157
158
            // record referer if browser send it
159
            $referer = empty($_SERVER['HTTP_REFERER']) ? '' : $_SERVER['REQUEST_URI'];
160
161
            // area as module's dirname
162
            if (!$area && is_object(@$xoopsModule)) {
163
                $area = $xoopsModule->getVar('dirname');
164
            }
165
166
            // store stub
167
            $_SESSION['XOOPS_G_STUBS'][] = array(
168
                'expire'  => time() + $timeout,
169
                'referer' => $referer,
170
                'area'    => $area,
171
                'token'   => $token
172
            );
173
174
            // paid md5ed token as a ticket
175
            return md5($token . XOOPS_DB_PREFIX);
176
        }
177
178
        // check a ticket
179
        /**
180
         * @param bool   $post
181
         * @param string $area
182
         * @param bool   $allow_repost
183
         *
184
         * @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...
185
         */
186
        public function check($post = true, $area = '', $allow_repost = true)
0 ignored issues
show
Coding Style introduced by
check 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...
Coding Style introduced by
check uses the super-global variable $_POST 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...
Coding Style introduced by
check uses the super-global variable $_GET 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...
Coding Style introduced by
check uses the super-global variable $_SERVER 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...
187
        {
188
            global $xoopsModule;
1 ignored issue
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...
189
190
            $this->_errors = array();
191
192
            // CHECK: stubs are not stored in session
193
            if (!is_array(@$_SESSION['XOOPS_G_STUBS'])) {
194
                $this->_errors[]           = $this->messages['err_nostubs'];
195
                $_SESSION['XOOPS_G_STUBS'] = array();
196
            }
197
198
            // get key&val of the ticket from a user's query
199
            $ticket = $post ? @$_POST['XOOPS_G_TICKET'] : @$_GET['XOOPS_G_TICKET'];
200
201
            // CHECK: no tickets found
202
            if (empty($ticket)) {
203
                $this->_errors[] = $this->messages['err_noticket'];
204
            }
205
206
            // gargage collection & find a right stub
207
            $stubs_tmp                 = $_SESSION['XOOPS_G_STUBS'];
208
            $_SESSION['XOOPS_G_STUBS'] = array();
209
            foreach ($stubs_tmp as $stub) {
210
                // default lifetime 30min
211
                if ($stub['expire'] >= time()) {
212
                    if (md5($stub['token'] . XOOPS_DB_PREFIX) === $ticket) {
213
                        $found_stub = $stub;
214
                    } else {
215
                        // store the other valid stubs into session
216
                        $_SESSION['XOOPS_G_STUBS'][] = $stub;
217
                    }
218
                } else {
219
                    if (md5($stub['token'] . XOOPS_DB_PREFIX) === $ticket) {
220
                        // not CSRF but Time-Out
221
                        $timeout_flag = true;
222
                    }
223
                }
224
            }
225
226
            // CHECK: the right stub found or not
227
            if (empty($found_stub)) {
228
                if (empty($timeout_flag)) {
229
                    $this->_errors[] = $this->messages['err_nopair'];
230
                } else {
231
                    $this->_errors[] = $this->messages['err_timeout'];
232
                }
233
            } else {
234
235
                // set area if necessary
236
                // area as module's dirname
237
                if (!$area && is_object(@$xoopsModule)) {
238
                    $area = $xoopsModule->getVar('dirname');
239
                }
240
241
                // check area or referer
242
                if (@$found_stub['area'] == $area) {
243
                    $area_check = true;
244
                }
245
                if (!empty($found_stub['referer']) && true === strpos(@$_SERVER['HTTP_REFERER'], $found_stub['referer'])) {
246
                    $referer_check = true;
247
                }
248
249
                if (empty($area_check) && empty($referer_check)) { // loose
250
                    $this->_errors[] = $this->messages['err_areaorref'];
251
                }
252
            }
253
254
            if (!empty($this->_errors)) {
255
                if ($allow_repost) {
256
                    // repost form
257
                    $this->draw_repost_form($area);
258
                    exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method check() 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...
259
                } else {
260
                    // failed
261
                    $this->clear();
262
263
                    return false;
264
                }
265
            } else {
266
                // all green
267
                return true;
268
            }
269
        }
270
271
        // draw form for repost
272
        /**
273
         * @param string $area
274
         */
275
        public function draw_repost_form($area = '')
0 ignored issues
show
Coding Style introduced by
draw_repost_form uses the super-global variable $_SERVER 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...
Coding Style introduced by
draw_repost_form uses the super-global variable $_POST 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...
276
        {
277
            // Notify which file is broken
278
            if (headers_sent()) {
279
                restore_error_handler();
280
                set_error_handler('GTicket_ErrorHandler4FindOutput');
281
                header('Dummy: for warning');
282
                restore_error_handler();
283
                exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method draw_repost_form() 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...
284
            }
285
286
            error_reporting(0);
287
            while (ob_get_level()) {
288
                ob_end_clean();
289
            }
290
291
            $table = '<table>';
292
            $form  = '<form action="?' . htmlspecialchars(@$_SERVER['QUERY_STRING'], ENT_QUOTES) . '" method="post" >';
293
            foreach ($_POST as $key => $val) {
294
                if ($key === 'XOOPS_G_TICKET') {
295
                    continue;
296
                }
297
                if (get_magic_quotes_gpc()) {
298
                    $key = stripslashes($key);
299
                }
300
                if (is_array($val)) {
301
                    list($tmp_table, $tmp_form) = $this->extract_post_recursive(htmlspecialchars($key, ENT_QUOTES), $val);
302
                    $table .= $tmp_table;
303
                    $form .= $tmp_form;
304
                } else {
305
                    if (get_magic_quotes_gpc()) {
306
                        $val = stripslashes($val);
307
                    }
308
                    $table .= '<tr><th>' . htmlspecialchars($key, ENT_QUOTES) . '</th><td>' . htmlspecialchars($val, ENT_QUOTES) . '</td></tr>' . "\n";
309
                    $form .= '<input type="hidden" name="' . htmlspecialchars($key, ENT_QUOTES) . '" value="' . htmlspecialchars($val, ENT_QUOTES) . '" />' . "\n";
310
                }
311
            }
312
            $table .= '</table>';
313
            $form .= $this->getTicketHtml(__LINE__, 300, $area) . '<input type="submit" value="' . $this->messages['btn_repost'] . '" /></form>';
314
315
            echo '<html><head><title>' .
316
                 $this->messages['err_general'] .
317
                 '</title><style>table,td,th {border:solid black 1px; border-collapse:collapse;}</style></head><body>' .
318
                 sprintf($this->messages['fmt_prompt4repost'], $this->getErrors()) .
319
                 $table .
320
                 $form .
321
                 '</body></html>';
322
        }
323
324
        /**
325
         * @param $key_name
326
         * @param $tmp_array
327
         *
328
         * @return array
329
         */
330
        public function extract_post_recursive($key_name, $tmp_array)
331
        {
332
            $table = '';
333
            $form  = '';
334
            foreach ($tmp_array as $key => $val) {
335
                if (get_magic_quotes_gpc()) {
336
                    $key = stripslashes($key);
337
                }
338
                if (is_array($val)) {
339
                    list($tmp_table, $tmp_form) = $this->extract_post_recursive($key_name . '[' . htmlspecialchars($key, ENT_QUOTES) . ']', $val);
340
                    $table .= $tmp_table;
341
                    $form .= $tmp_form;
342
                } else {
343
                    if (get_magic_quotes_gpc()) {
344
                        $val = stripslashes($val);
345
                    }
346
                    $table .= '<tr><th>' . $key_name . '[' . htmlspecialchars($key, ENT_QUOTES) . ']</th><td>' . htmlspecialchars($val, ENT_QUOTES) . '</td></tr>' . "\n";
347
                    $form .= '<input type="hidden" name="' . $key_name . '[' . htmlspecialchars($key, ENT_QUOTES) . ']" value="' . htmlspecialchars($val, ENT_QUOTES) . '" />' . "\n";
348
                }
349
            }
350
351
            return array($table, $form);
352
        }
353
354
        // clear all stubs
355
        public function clear()
0 ignored issues
show
Coding Style introduced by
clear 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...
356
        {
357
            $_SESSION['XOOPS_G_STUBS'] = array();
358
        }
359
360
        // Ticket Using
361
        /**
362
         * @return bool
363
         */
364
        public function using()
0 ignored issues
show
Coding Style introduced by
using 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...
365
        {
366
            if (!empty($_SESSION['XOOPS_G_STUBS'])) {
367
                return true;
368
            } else {
369
                return false;
370
            }
371
        }
372
373
        // return errors
374
        /**
375
         * @param bool $ashtml
376
         *
377
         * @return array|string
378
         */
379
        public function getErrors($ashtml = true)
380
        {
381
            if ($ashtml) {
382
                $ret = '';
383
                foreach ($this->_errors as $msg) {
384
                    $ret .= "$msg<br>\n";
385
                }
386
            } else {
387
                $ret = $this->_errors;
388
            }
389
390
            return $ret;
391
        }
392
393
        // end of class
394
    }
395
396
    /**
397
     * @param $errNo
398
     * @param $errStr
399
     * @param $errFile
400
     * @param $errLine
401
     */
402
    function GTicket_ErrorHandler4FindOutput($errNo, $errStr, $errFile, $errLine)
0 ignored issues
show
Unused Code introduced by
The parameter $errNo 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...
Unused Code introduced by
The parameter $errFile 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...
Unused Code introduced by
The parameter $errLine 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...
403
    {
404
        if (preg_match('?' . preg_quote(XOOPS_ROOT_PATH) . '([^:]+)\:(\d+)?', $errStr, $regs)) {
405
            echo 'Irregular output! check the file ' . htmlspecialchars($regs[1]) . ' line ' . htmlspecialchars($regs[2]);
406
        } else {
407
            echo 'Irregular output! check language files etc.';
408
        }
409
410
        return;
411
    }
412
413
    // create a instance in global scope
414
    $GLOBALS['xoopsGTicket'] = new XoopsGTicket();
415
}
416
417
if (!function_exists('admin_refcheck')) {
418
419
    //Admin Referer Check By Marijuana(Rev.011)
420
    /**
421
     * @param string $chkref
422
     *
423
     * @return bool
424
     */
425
    function admin_refcheck($chkref = '')
0 ignored issues
show
Coding Style introduced by
admin_refcheck uses the super-global variable $_SERVER 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...
426
    {
427
        if (empty($_SERVER['HTTP_REFERER'])) {
428
            return true;
429
        } else {
430
            $ref = $_SERVER['HTTP_REFERER'];
431
        }
432
        $cr = XOOPS_URL;
433
        if ($chkref != '') {
434
            $cr .= $chkref;
435
        }
436
        if (strpos($ref, $cr) !== 0) {
437
            return false;
438
        }
439
440
        return true;
441
    }
442
}
443