1
|
|
|
<?php |
|
|
|
|
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
|
|
|
/** |
13
|
|
|
* userlog module |
14
|
|
|
* |
15
|
|
|
* @copyright XOOPS Project (https://xoops.org) |
16
|
|
|
* @license GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html) |
17
|
|
|
* @package userlog class |
18
|
|
|
* @since 1 |
19
|
|
|
* @author irmtfan ([email protected]) |
20
|
|
|
* @author XOOPS Project <www.xoops.org> <www.xoops.ir> |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
use Xmf\Request; |
24
|
|
|
|
25
|
|
|
defined('XOOPS_ROOT_PATH') || exit('Restricted access.'); |
26
|
|
|
require_once __DIR__ . '/phpbrowscap/Browscap.php'; |
27
|
|
|
|
28
|
|
|
// The Browscap class is in the phpbrowscap namespace, so import it |
29
|
|
|
use phpbrowscap\Browscap; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class Userlog |
33
|
|
|
*/ |
34
|
|
|
class Userlog extends \Xmf\Module\Helper |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
public $logmodule; |
37
|
|
|
public $user; |
38
|
|
|
public $debugArray = []; |
39
|
|
|
public $logext = 'log'; |
40
|
|
|
public $cookiePrefix = ''; |
41
|
|
|
public $groupList; |
42
|
|
|
public $browscap; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $debug |
46
|
|
|
*/ |
47
|
|
|
protected function __construct($debug) |
48
|
|
|
{ |
49
|
|
|
$this->debug = $debug; |
50
|
|
|
$this->dirname = USERLOG_DIRNAME; |
51
|
|
|
$this->cookiePrefix = USERLOG_DIRNAME . '_' . ($this->getUser() ? $this->getUser()->getVar('uid') : ''); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param bool $debug |
56
|
|
|
* |
57
|
|
|
* @return Userlog |
58
|
|
|
*/ |
59
|
|
|
public static function getInstance($debug = false) |
60
|
|
|
{ |
61
|
|
|
static $instance; |
62
|
|
|
if (null === $instance) { |
63
|
|
|
$instance = new static($debug); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $instance; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return null|\XoopsModule |
71
|
|
|
*/ |
72
|
|
|
public function getLogModule() |
73
|
|
|
{ |
74
|
|
|
if (null === $this->logmodule) { |
75
|
|
|
$this->initLogModule(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this->logmodule; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param array $dirnames |
83
|
|
|
* @param null $otherCriteria |
84
|
|
|
* @param bool $asObj |
85
|
|
|
* |
86
|
|
|
* @return mixed |
87
|
|
|
*/ |
88
|
|
|
public function getModules($dirnames = [], $otherCriteria = null, $asObj = false) |
89
|
|
|
{ |
90
|
|
|
// get all dirnames |
91
|
|
|
/** @var XoopsModuleHandler $moduleHandler */ |
92
|
|
|
$moduleHandler = xoops_getHandler('module'); |
93
|
|
|
$criteria = new CriteriaCompo(); |
94
|
|
|
if (count($dirnames) > 0) { |
95
|
|
|
foreach ($dirnames as $mDir) { |
96
|
|
|
$criteria->add(new Criteria('dirname', $mDir), 'OR'); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
if (!empty($otherCriteria)) { |
100
|
|
|
$criteria->add($otherCriteria); |
101
|
|
|
} |
102
|
|
|
$criteria->add(new Criteria('isactive', 1), 'AND'); |
103
|
|
|
$modules = $moduleHandler->getObjects($criteria, true); |
104
|
|
|
if ($asObj) { |
105
|
|
|
return $modules; |
106
|
|
|
} |
107
|
|
|
$dirNames['system-root'] = _YOURHOME; |
|
|
|
|
108
|
|
|
foreach ($modules as $module) { |
109
|
|
|
$dirNames[$module->dirname()] = $module->name(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $dirNames; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return null |
117
|
|
|
*/ |
118
|
|
|
public function getUser() |
119
|
|
|
{ |
120
|
|
|
if (null === $this->user) { |
121
|
|
|
$this->initUser(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $this->user; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return null|array |
129
|
|
|
*/ |
130
|
|
|
public function getGroupList() |
131
|
|
|
{ |
132
|
|
|
if (null === $this->groupList) { |
133
|
|
|
$this->initGroupList(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $this->groupList; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return null |
141
|
|
|
*/ |
142
|
|
|
public function getBrowsCap() |
143
|
|
|
{ |
144
|
|
|
if (null === $this->browscap) { |
145
|
|
|
$this->initBrowsCap(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $this->browscap; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param null $name |
154
|
|
|
* @param null $value |
155
|
|
|
* |
156
|
|
|
* @return mixed |
157
|
|
|
*/ |
158
|
|
|
public function setConfig($name = null, $value = null) |
159
|
|
|
{ |
160
|
|
|
if (null === $this->configs) { |
161
|
|
|
$this->initConfig(); |
162
|
|
|
} |
163
|
|
|
$this->configs[$name] = $value; |
164
|
|
|
$this->addLog("Setting config '{$name}' : " . $this->configs[$name]); |
165
|
|
|
|
166
|
|
|
return $this->configs[$name]; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
|
|
public function getAllLogFiles() |
173
|
|
|
{ |
174
|
|
|
$logPaths = $this->object->getInfo('log_paths'); |
175
|
|
|
$currentPath = $this->getConfig('logfilepath'); |
|
|
|
|
176
|
|
|
$allFiles = []; |
177
|
|
|
$totalFiles = 0; |
178
|
|
|
foreach ($logPaths as $path) { |
179
|
|
|
$folderHandler = XoopsFile::getHandler('folder', $path . '/' . USERLOG_DIRNAME); |
180
|
|
|
$allFiles[$path . '/' . USERLOG_DIRNAME] = $folderHandler->find('.*' . $this->logext); |
181
|
|
|
$totalFiles += count($allFiles[$path . '/' . USERLOG_DIRNAME]); |
182
|
|
|
} |
183
|
|
|
if (empty($totalFiles)) { |
184
|
|
|
return [[], 0]; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return [$allFiles, $totalFiles]; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return string |
192
|
|
|
*/ |
193
|
|
|
public function getWorkingFile() |
194
|
|
|
{ |
195
|
|
|
$logFileName = $this->getConfig('logfilepath') . '/' . USERLOG_DIRNAME . '/' . $this->getConfig('logfilename'); |
196
|
|
|
|
197
|
|
|
return $logFileName . '.' . $this->logext; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param $array |
202
|
|
|
* @param null $keys |
203
|
|
|
* |
204
|
|
|
* @return array |
205
|
|
|
*/ |
206
|
|
|
public function getFromKeys($array, $keys = null) |
207
|
|
|
{ |
208
|
|
|
if (empty($keys)) { |
209
|
|
|
return $array; |
210
|
|
|
} // all keys |
211
|
|
|
$keyarr = is_string($keys) ? explode(',', $keys) : $keys; |
212
|
|
|
if (empty($keyarr[0])) { |
213
|
|
|
return $array; |
214
|
|
|
} // all keys |
215
|
|
|
$keyarr = array_intersect(array_keys($array), $keyarr); // keys should be in array |
216
|
|
|
$ret = []; |
217
|
|
|
foreach ($keyarr as $key) { |
218
|
|
|
$ret[$key] = $array[$key]; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return $ret; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param int $since |
226
|
|
|
* |
227
|
|
|
* @return int |
228
|
|
|
*/ |
229
|
|
|
public function getSinceTime($since = 1) // one day |
230
|
|
|
{ |
231
|
|
|
if ($since > 0) { |
232
|
|
|
return (int)$since * 24 * 3600; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return (int)abs($since) * 3600; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param null $intTime |
240
|
|
|
* @param string $dateFormat |
241
|
|
|
* @param null|string $timeoffset |
242
|
|
|
* |
243
|
|
|
* @return bool|string |
244
|
|
|
*/ |
245
|
|
|
public function formatTime($intTime = null, $dateFormat = 'c', $timeoffset = null) |
246
|
|
|
{ |
247
|
|
|
if (empty($intTime)) { |
248
|
|
|
return false; |
249
|
|
|
} |
250
|
|
|
if ('custom' === $dateFormat || 'c' === $dateFormat) { |
251
|
|
|
$dateFormat = $this->getConfig('format_date'); |
252
|
|
|
} |
253
|
|
|
xoops_load('XoopsLocal'); |
254
|
|
|
|
255
|
|
|
return class_exists('XoopsLocal') ? XoopsLocal::formatTimestamp($intTime, $dateFormat, $timeoffset) : XoopsLocale::formatTimestamp($intTime, $dateFormat, $timeoffset); // use XoopsLocale in xoops26 |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param string $name |
260
|
|
|
* |
261
|
|
|
* @return array |
262
|
|
|
*/ |
263
|
|
|
public function getCookie($name = 'TOGGLE') |
264
|
|
|
{ |
265
|
|
|
$toggles = Request::getString($this->cookiePrefix . $name, null, 'cookie'); |
266
|
|
|
|
267
|
|
|
return explode(',', $toggles); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @param int $prob |
272
|
|
|
* |
273
|
|
|
* @return bool |
274
|
|
|
*/ |
275
|
|
|
public function probCheck($prob = 11) |
276
|
|
|
{ |
277
|
|
|
mt_srand((double)microtime() * 1000000); |
278
|
|
|
// check probabillity 11 means 10%, 100 means 100% |
279
|
|
|
$ret = mt_rand(1, 100) > $prob; |
280
|
|
|
|
281
|
|
|
return $ret; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @param null $post |
286
|
|
|
* @param int $uid |
287
|
|
|
* @param bool $unsetPass |
288
|
|
|
* |
289
|
|
|
* @return null |
290
|
|
|
*/ |
291
|
|
|
public function patchLoginHistory($post = null, $uid = 0, $unsetPass = true) |
292
|
|
|
{ |
293
|
|
|
if ($uid > 0 || empty($post['pass']) || empty($post['uname'])) { |
294
|
|
|
return $post; |
295
|
|
|
} |
296
|
|
|
$postPatch = $post; |
297
|
|
|
$postPatch['login_patch'] = 1; |
298
|
|
|
if ($unsetPass) { |
299
|
|
|
$postPatch['pass'] = 'unset_pass'; |
300
|
|
|
if (isset($postPatch['vpass'])) { |
301
|
|
|
$postPatch['vpass'] = 'unset_vpass'; |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
$memberHandler = xoops_getHandler('member'); |
305
|
|
|
$loginSuccess = $memberHandler->loginUser($post['uname'], $post['pass']); // check login to find if this user is exist in database |
306
|
|
|
// only for successful login/register |
307
|
|
|
if (is_object($loginSuccess)) { |
308
|
|
|
$postPatch['success'] = 1; |
309
|
|
|
$postPatch['uid'] = $loginSuccess->getVar('uid'); |
310
|
|
|
if (0 < ($level = $loginSuccess->getVar('level'))) { |
311
|
|
|
$postPatch['level'] = $level; |
312
|
|
|
} |
313
|
|
|
if (0 < ($last_visit = $loginSuccess->getVar('last_login'))) { |
314
|
|
|
$postPatch['last_visit'] = $last_visit; |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
return $postPatch; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
|
322
|
|
|
private function initLogModule() |
323
|
|
|
{ |
324
|
|
|
global $xoopsModule; |
|
|
|
|
325
|
|
|
if (isset($xoopsModule) && is_object($xoopsModule)) { |
326
|
|
|
$this->logmodule = $xoopsModule; |
327
|
|
|
} else { |
328
|
|
|
$hModule = xoops_getHandler('module'); |
329
|
|
|
$this->logmodule = $hModule->getByDirname('system'); |
330
|
|
|
$this->logmodule->setVar('dirname', 'system-root'); |
331
|
|
|
} |
332
|
|
|
$this->addLog('INIT LOGMODULE'); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
private function initUser() |
336
|
|
|
{ |
337
|
|
|
global $xoopsUser; |
|
|
|
|
338
|
|
|
if (isset($xoopsUser) && is_object($xoopsUser)) { |
339
|
|
|
$this->user = $xoopsUser; |
340
|
|
|
} else { |
341
|
|
|
$this->user = null; |
342
|
|
|
} |
343
|
|
|
$this->addLog('INIT USER'); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
private function initGroupList() |
347
|
|
|
{ |
348
|
|
|
/** @var \XoopsMemberHandler $groupHandler */ |
349
|
|
|
$groupHandler = xoops_getHandler('member'); |
350
|
|
|
$this->groupList = $groupHandler->getGroupList(); |
351
|
|
|
$this->addLog('INIT GROUP LIST'); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* @return bool |
356
|
|
|
* @throws \phpbrowscap\Exception |
357
|
|
|
*/ |
358
|
|
|
private function initBrowsCap() |
359
|
|
|
{ |
360
|
|
|
$browscapCache = XOOPS_CACHE_PATH . '/browscap'; |
361
|
|
|
// force to create file if not exist |
362
|
|
|
$folderHandler = XoopsFile::getHandler('folder', $browscapCache, true); |
363
|
|
|
if (!$folderHandler->pwd()) { |
364
|
|
|
// Errors Warning: mkdir() [function.mkdir]: Permission denied in file /class/file/folder.php line 529 |
365
|
|
|
$this->addLog("Cannot create folder ({$browscapCache})"); |
366
|
|
|
|
367
|
|
|
return false; |
368
|
|
|
} |
369
|
|
|
// Creates a new Browscap object (loads or creates the cache) |
370
|
|
|
// $bc = new Browscap('path/to/the/cache/dir'); |
|
|
|
|
371
|
|
|
$this->browscap = new Browscap($browscapCache); |
372
|
|
|
$this->addLog('INIT BrowsCap'); |
373
|
|
|
|
374
|
|
|
return true; |
375
|
|
|
} |
376
|
|
|
} |
377
|
|
|
|
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.