Completed
Push — master ( 204b42...65a2ef )
by Michael
03:03
created

UserlogQuery   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 265
Duplicated Lines 4.53 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 12
loc 265
rs 10
wmc 24
lcom 2
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getInstance() 0 9 2
D loginregHistoryShow() 12 65 13
A loginregHistoryForm() 0 52 1
A stats_typeShow() 0 11 2
B stats_typeForm() 0 42 1
B modulesadminShow() 0 27 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 34 and the first side effect is on line 23.

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
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
/**
12
 *  userlog module
13
 *
14
 * @copyright       XOOPS Project (http://xoops.org)
15
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
16
 * @package         userlog class
17
 * @since           1.16
18
 * @author          irmtfan ([email protected])
19
 * @author          XOOPS Project <www.xoops.org> <www.xoops.ir>
20
 */
21
// Important note: use $eleNamePrefix = "options" because it is hard-coded in XOOPS CORE > BLOCKS
22
23
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
24
include_once dirname(__DIR__) . '/include/common.php';
25
26
xoops_loadLanguage('admin', USERLOG_DIRNAME);
27
xoops_load('XoopsFormLoader');
28
xoops_loadLanguage('user');
29
xoops_loadLanguage('findusers');
30
31
/**
32
 * Class UserlogQuery
33
 */
34
class UserlogQuery
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...
35
{
36
    public $userlog = null;
37
38
    /**
39
     *
40
     */
41
    protected function __construct()
42
    {
43
        $this->userlog = Userlog::getInstance();
44
    }
45
46
    /**
47
     * @return UserlogQuery
48
     */
49
    public static function getInstance()
50
    {
51
        static $instance;
52
        if (null === $instance) {
53
            $instance = new static();
54
        }
55
56
        return $instance;
57
    }
58
    // args[0] - number of items to show in block. the default is 10
59
    // args[1] - login or register or both radio select
60
    // args[2] - failed or successful or both radio select
61
    // args[3] - inactive or active or both
62
    // args[4] - never login before or login before or both
63
    // args[5] - Order - DESC, ASC default: DESC
64
    /**
65
     * @param $args
66
     *
67
     * @return array
68
     */
69
    public function loginregHistoryShow($args)
70
    {
71
        $criteria = new CriteriaCompo();
72
        $criteria->add(new Criteria('uid', 0), 'AND');
73
        $criteria->add(new Criteria('post', '%pass%', 'LIKE'), 'AND'); // login or register
74
        $criteria->add(new Criteria('post', '%login_patch%', 'LIKE'), 'AND'); // login/register was patched
75
        $opt[0] = 0;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$opt was never initialized. Although not strictly required by PHP, it is generally a good practice to add $opt = 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...
76
        $opt[1] = 'NOT LIKE';
77
        $opt[2] = 'LIKE';
78
79
        $i = 1;
80 View Code Duplication
        if (!empty($args[$i])) {
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...
81
            $criteria->add(new Criteria('post', '%vpass%', $opt[$args[$i]]), 'AND'); // login "NOT LIKE" register "LIKE"
82
        }
83
        ++$i; //2
84 View Code Duplication
        if (!empty($args[$i])) {
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...
85
            $criteria->add(new Criteria('post', '%success%', $opt[$args[$i]]), 'AND'); // falied "NOT LIKE" success "LIKE"
86
        }
87
        ++$i; //3
88 View Code Duplication
        if (!empty($args[$i])) {
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...
89
            $criteria->add(new Criteria('post', '%level%', $opt[$args[$i]]), 'AND'); // inactive "NOT LIKE" active "LIKE"
90
        }
91
        ++$i; //4
92 View Code Duplication
        if (!empty($args[$i])) {
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...
93
            $criteria->add(new Criteria('post', '%last_visit%', $opt[$args[$i]]), 'AND'); // never login before "NOT LIKE" login before "LIKE"
94
        }
95
        $loginsObj = $this->userlog->getHandler('log')->getLogs($args[0], 0, $criteria, 'log_id', $args[5], array('log_id', 'log_time', 'post'), true); // true => as Obj
96
        $block     = array();
97
        if (empty($loginsObj)) {
98
            return $block;
99
        }
100
        foreach ($loginsObj as $log_id => $loginObj) {
101
            $block[$log_id]                    = $loginObj->post(); // dont use getVar("post")
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% 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...
102
            $block[$log_id]['loginOrRegister'] = !empty($block[$log_id]['vpass']) ? 'register' : 'login';
103
            if (!empty($block[$log_id]['success'])) {
104
                $block[$log_id]['msg'] = _AM_USERLOG_SUCCESS . ' ';
105
                if (empty($block[$log_id]['level'])) {
106
                    $block[$log_id]['msg'] .= _MA_USER_LEVEL_INACTIVE;
107
                    $block[$log_id]['color'] = 'YELLOW';
108
                } else {
109
                    $block[$log_id]['msg'] .= _MA_USER_LEVEL_ACTIVE;
110
                    $block[$log_id]['color'] = 'GREEN';
111
                }
112
                if (empty($block[$log_id]['last_visit'])) {
113
                    if ($block[$log_id]['loginOrRegister'] === 'register') {
114
                        $block[$log_id]['msg'] .= ' ' . sprintf(_US_HASJUSTREG, $block[$log_id]['uname']);
115
                        $block[$log_id]['color'] = 'GREEN';
116
                    } else {
117
                        $block[$log_id]['msg'] .= ' ' . sprintf(_US_CONFMAIL, $block[$log_id]['uname']);
118
                        $block[$log_id]['color'] = 'BROWN';
119
                    }
120
                }
121
            } else {
122
                $block[$log_id]['success'] = 0;
123
                $block[$log_id]['msg']     = _AM_USERLOG_FAIL . ' ';
124
                $block[$log_id]['msg'] .= ($block[$log_id]['loginOrRegister'] === 'register') ? _ERRORS : _US_INCORRECTLOGIN;
125
                $block[$log_id]['color'] = 'RED';
126
            }
127
            $this->userlog->setConfig('format_date', $this->userlog->getConfig('format_date_history'));
128
            $block[$log_id]['log_time'] = $loginObj->log_time();
129
        }
130
        unset($block[$log_id]['pass'], $block[$log_id]['vpass']);
131
132
        return $block;
133
    }
134
135
    /**
136
     * @param        $args
137
     * @param string $eleNamePrefix
138
     *
139
     * @return string
140
     */
141
    public function loginregHistoryForm($args, $eleNamePrefix = 'options')
142
    {
143
        // include_once XOOPS_ROOT_PATH . "/class/blockform.php"; //reserve for 2.6
144
        xoops_load('XoopsFormLoader');
145
        // $form = new XoopsBlockForm(); //reserve for 2.6
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% 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...
146
        $form = new XoopsThemeForm(_AM_USERLOG_LOGIN_REG_HISTORY, 'login_reg_history', '');
147
148
        $i = 0;
149
        // number of items to display element
150
        $numitemsEle = new XoopsFormText(_AM_USERLOG_ITEMS_NUM, "{$eleNamePrefix}[{$i}]", 10, 255, (int)$args[$i]);
151
152
        ++$i;
153
        $loginRegRadioEle = new XoopsFormRadio(_LOGIN . '|' . _REGISTER, "{$eleNamePrefix}[{$i}]", $args[$i]);
154
        $loginRegRadioEle->addOption(1, _LOGIN);
155
        $loginRegRadioEle->addOption(2, _REGISTER);
156
        $loginRegRadioEle->addOption(0, _ALL);
157
158
        ++$i;
159
        $failSucRadioEle = new XoopsFormRadio(_AM_USERLOG_FAIL . '|' . _AM_USERLOG_SUCCESS, "{$eleNamePrefix}[{$i}]", $args[$i]);
160
        $failSucRadioEle->addOption(1, _LOGIN . '|' . _REGISTER . ' ' . _AM_USERLOG_FAIL);
161
        $failSucRadioEle->addOption(2, _LOGIN . '|' . _REGISTER . ' ' . _AM_USERLOG_SUCCESS);
162
        $failSucRadioEle->addOption(0, _ALL);
163
164
        ++$i;
165
        $inactiveActiveRadioEle = new XoopsFormRadio(_MA_USER_LEVEL_INACTIVE . '|' . _MA_USER_LEVEL_ACTIVE, "{$eleNamePrefix}[{$i}]", $args[$i]);
166
        $inactiveActiveRadioEle->addOption(1, _MA_USER_LEVEL_INACTIVE);
167
        $inactiveActiveRadioEle->addOption(2, _MA_USER_LEVEL_ACTIVE);
168
        $inactiveActiveRadioEle->addOption(0, _ALL);
169
170
        ++$i;
171
        $lastVisitRadioEle = new XoopsFormRadio(_AM_USERLOG_LAST_LOGIN, "{$eleNamePrefix}[{$i}]", $args[$i]);
172
        $lastVisitRadioEle->addOption(1, _NONE);
173
        $lastVisitRadioEle->addOption(2, _YES);
174
        $lastVisitRadioEle->addOption(0, _ALL);
175
        $lastVisitRadioEle->setDescription(_AM_USERLOG_LAST_LOGIN_DSC);
176
177
        ++$i;
178
        $orderEle = new XoopsFormSelect(_AM_USERLOG_ORDER, "{$eleNamePrefix}[{$i}]", $args[$i]);
179
        $orderEle->addOption('DESC', _DESCENDING);
180
        $orderEle->addOption('ASC', _ASCENDING);
181
        $orderEle->setDescription(_AM_USERLOG_ORDER_DSC);
182
183
        // add all elements to form
184
        $form->addElement($numitemsEle);
185
        $form->addElement($loginRegRadioEle);
186
        $form->addElement($failSucRadioEle);
187
        $form->addElement($inactiveActiveRadioEle);
188
        $form->addElement($lastVisitRadioEle);
189
        $form->addElement($orderEle);
190
191
        return $form->render();
192
    }
193
194
    // args[0] - number of items to show in block. the default is 10
195
    // args[1] - stats_type - referral (default), browser, OS
196
    // args[2] - Sort - stats_link, stats_value (default), time_update
197
    // args[3] - Order - DESC, ASC default: DESC
198
    /**
199
     * @param $args
200
     *
201
     * @return array|bool
202
     */
203
    public function stats_typeShow($args)
204
    {
205
        $statsObj = UserlogStats::getInstance();
206
        $refViews = $statsObj->getAll($args[1], 0, $args[0], $args[2], $args[3]); // getAll($type = array(), $start = 0, $limit = 0, $sort = "stats_value", $order = "DESC", $otherCriteria = null)
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% 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...
207
        if (empty($refViews)) {
208
            return false;
209
        }
210
        $block = array('stats' => $refViews, 'stats_type' => $args[1]);
211
212
        return $block;
213
    }
214
215
    /**
216
     * @param        $args
217
     * @param string $eleNamePrefix
218
     *
219
     * @return string
220
     */
221
    public function stats_typeForm($args, $eleNamePrefix = 'options')
222
    {
223
        // include_once XOOPS_ROOT_PATH . "/class/blockform.php"; //reserve for 2.6
224
        xoops_load('XoopsFormLoader');
225
        // $form = new XoopsBlockForm(); //reserve for 2.6
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% 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...
226
        $form = new XoopsThemeForm(_AM_USERLOG_STATS_TYPE, 'stats_type', '');
227
228
        $i = 0;
229
        // number of items to display element
230
        $numitemsEle = new XoopsFormText(_AM_USERLOG_ITEMS_NUM, "{$eleNamePrefix}[{$i}]", 10, 255, (int)$args[$i]);
231
        ++$i;
232
        $typeEle = new XoopsFormSelect(_AM_USERLOG_STATS_TYPE, "{$eleNamePrefix}[{$i}]", $args[$i]);
233
        $typeEle->addOptionArray(array(
234
                                     'referral' => _AM_USERLOG_STATS_REFERRAL,
235
                                     'browser'  => _AM_USERLOG_STATS_BROWSER,
236
                                     'OS'       => _AM_USERLOG_STATS_OS
237
                                 ));
238
        $typeEle->setDescription(_AM_USERLOG_STATS_TYPE_DSC);
239
240
        ++$i;
241
        $sortEle = new XoopsFormSelect(_AM_USERLOG_SORT, "{$eleNamePrefix}[{$i}]", $args[$i]);
242
        $sortEle->addOptionArray(array(
243
                                     'stats_link'  => _AM_USERLOG_ITEM_NAME,
244
                                     'stats_value' => _AM_USERLOG_VIEW,
245
                                     'time_update' => _AM_USERLOG_STATS_TIME_UPDATE
246
                                 ));
247
        $sortEle->setDescription(_AM_USERLOG_SORT_DSC);
248
249
        ++$i;
250
        $orderEle = new XoopsFormSelect(_AM_USERLOG_ORDER, "{$eleNamePrefix}[{$i}]", $args[$i]);
251
        $orderEle->addOption('DESC', _DESCENDING);
252
        $orderEle->addOption('ASC', _ASCENDING);
253
        $orderEle->setDescription(_AM_USERLOG_ORDER_DSC);
254
255
        // add all elements to form
256
        $form->addElement($numitemsEle);
257
        $form->addElement($typeEle);
258
        $form->addElement($sortEle);
259
        $form->addElement($orderEle);
260
261
        return $form->render();
262
    }
263
264
    // args[0] - number of items to show in block. the default is 10
265
    // args[1] - module dirname - 0 or empty = all modules
266
    /**
267
     * @param $args
268
     *
269
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be false|array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
270
     */
271
    public function modulesadminShow($args)
272
    {
273
        xoops_loadLanguage('admin/modulesadmin', 'system');
274
        $criteria = new CriteriaCompo();
275
        $criteria->add(new Criteria('module', 'system'), 'AND');
276
        $criteria->add(new Criteria('request_method', 'POST'), 'AND'); // only POST method
277
        $refLike = '%modulesadmin&op=%';
278
        if (!empty($args[1])) {
279
            $refLike .= "module={$args[1]}";
280
        }
281
        $criteria->add(new Criteria('referer', "{$refLike}", 'LIKE'), 'AND'); // modules admin
282
283
        $modulesadminObjs = $this->userlog->getHandler('log')->getLogs($args[0], 0, $criteria, 'log_id', 'DESC', array('log_id', 'log_time', 'referer'), true); // true => as Obj
284
        if (empty($modulesadminObjs)) {
285
            return false;
286
        }
287
        $block = array();
288
        foreach ($modulesadminObjs as $maObj) {
289
            $query = parse_url($maObj->referer(), PHP_URL_QUERY);
290
            parse_str($query, $moduleAdmin);
291
            $moduleAdmin['op_lang']          = constant('_AM_SYSTEM_MODULES_' . strtoupper($moduleAdmin['op']));
292
            $moduleAdmin['log_time']         = $maObj->log_time();
293
            $block[$maObj->getVar('log_id')] = $moduleAdmin;
294
        }
295
296
        return $block;
297
    }
298
}
299