1
|
|
|
<?php |
|
|
|
|
2
|
|
|
// |
3
|
|
|
// ------------------------------------------------------------------------ // |
4
|
|
|
// XOOPS - PHP Content Management System // |
5
|
|
|
// Copyright (c) 2000-2016 XOOPS.org // |
6
|
|
|
// <https://xoops.org/> // |
7
|
|
|
// ------------------------------------------------------------------------ // |
8
|
|
|
// This program is free software; you can redistribute it and/or modify // |
9
|
|
|
// it under the terms of the GNU General Public License as published by // |
10
|
|
|
// the Free Software Foundation; either version 2 of the License, or // |
11
|
|
|
// (at your option) any later version. // |
12
|
|
|
// // |
13
|
|
|
// You may not change or alter any portion of this comment or credits // |
14
|
|
|
// of supporting developers from this source code or any supporting // |
15
|
|
|
// source code which is considered copyrighted (c) material of the // |
16
|
|
|
// original comment or credit authors. // |
17
|
|
|
// // |
18
|
|
|
// This program is distributed in the hope that it will be useful, // |
19
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of // |
20
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
21
|
|
|
// GNU General Public License for more details. // |
22
|
|
|
// // |
23
|
|
|
// You should have received a copy of the GNU General Public License // |
24
|
|
|
// along with this program; if not, write to the Free Software // |
25
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // |
26
|
|
|
// ------------------------------------------------------------------------ // |
27
|
|
|
// Author: phppp (D.J., [email protected]) // |
28
|
|
|
// URL: https://xoops.org // |
29
|
|
|
// Project: Article Project // |
30
|
|
|
// ------------------------------------------------------------------------ // |
31
|
|
|
|
32
|
|
|
// defined('XOOPS_ROOT_PATH') || exit('Restricted access.'); |
|
|
|
|
33
|
|
|
|
34
|
|
|
defined('NEWBB_FUNCTIONS_INI') || include $GLOBALS['xoops']->path('modules/newbb/include/functions.ini.php'); |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* A handler for read/unread handling |
38
|
|
|
* |
39
|
|
|
* @package newbb |
40
|
|
|
* |
41
|
|
|
* @author D.J. (phppp, http://xoopsforge.com) |
42
|
|
|
* @copyright copyright (c) 2005 XOOPS.org |
43
|
|
|
*/ |
44
|
|
|
class Read extends XoopsObject |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* @internal param $type |
48
|
|
|
*/ |
49
|
|
|
public function __construct() |
50
|
|
|
{ |
51
|
|
|
// parent::__construct("newbb_reads_" . $type); |
|
|
|
|
52
|
|
|
parent::__construct(); |
53
|
|
|
$this->initVar('read_id', XOBJ_DTYPE_INT); |
54
|
|
|
$this->initVar('uid', XOBJ_DTYPE_INT); |
55
|
|
|
$this->initVar('read_item', XOBJ_DTYPE_INT); |
56
|
|
|
$this->initVar('post_id', XOBJ_DTYPE_INT); |
57
|
|
|
$this->initVar('read_time', XOBJ_DTYPE_INT); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Class NewbbReadHandler |
63
|
|
|
*/ |
64
|
|
|
class NewbbReadHandler extends XoopsPersistableObjectHandler |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
/** |
67
|
|
|
* Object type. |
68
|
|
|
* <ul> |
69
|
|
|
* <li>forum</li> |
70
|
|
|
* <li>topic</li> |
71
|
|
|
* </ul> |
72
|
|
|
* |
73
|
|
|
* @var string |
74
|
|
|
*/ |
75
|
|
|
public $type; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* seconds records will persist. |
79
|
|
|
* assigned from $GLOBALS['xoopsModuleConfig']["read_expire"] |
80
|
|
|
* <ul> |
81
|
|
|
* <li>positive days = delete all read records exist in the tables before expire time // irmtfan add comment</li> |
82
|
|
|
* <li>0 = never expires // irmtfan change comment</li> |
83
|
|
|
* <li>-1 or any negative days = never records // irmtfan change comment</li> |
84
|
|
|
* </ul> |
85
|
|
|
* |
86
|
|
|
* @var integer |
87
|
|
|
*/ |
88
|
|
|
public $lifetime; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* storage mode for records. |
92
|
|
|
* assigned from $GLOBALS['xoopsModuleConfig']["read_mode"] |
93
|
|
|
* <ul> |
94
|
|
|
* <li>0 = never records</li> |
95
|
|
|
* <li>1 = uses cookie</li> |
96
|
|
|
* <li>2 = stores in database</li> |
97
|
|
|
* </ul> |
98
|
|
|
* |
99
|
|
|
* @var integer |
100
|
|
|
*/ |
101
|
|
|
public $mode; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param XoopsDatabase|null $db |
105
|
|
|
* @param $type |
106
|
|
|
*/ |
107
|
|
|
public function __construct(XoopsDatabase $db, $type) |
108
|
|
|
{ |
109
|
|
|
$type = ('forum' === $type) ? 'forum' : 'topic'; |
110
|
|
|
parent::__construct($db, 'newbb_reads_' . $type, 'Read' . $type, 'read_id', 'post_id'); |
111
|
|
|
$this->type = $type; |
112
|
|
|
$newbbConfig = newbbLoadConfig(); |
113
|
|
|
// irmtfan if read_expire = 0 dont clean |
114
|
|
|
$this->lifetime = isset($newbbConfig['read_expire']) ? (int)$newbbConfig['read_expire'] * 24 * 3600 : 30 * 24 * 3600; |
115
|
|
|
$this->mode = isset($newbbConfig['read_mode']) ? $newbbConfig['read_mode'] : 2; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Clear garbage |
120
|
|
|
* |
121
|
|
|
* Delete all expired and duplicated records |
122
|
|
|
*/ |
123
|
|
|
// START irmtfan rephrase function to 1- add clearDuplicate and 2- dont clean when read_expire = 0 |
124
|
|
|
public function clearGarbage() |
125
|
|
|
{ |
126
|
|
|
// irmtfan clear duplicaed rows |
127
|
|
|
if (!$result = $this->clearDuplicate()) { |
128
|
|
|
return false; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$sql = 'DELETE bb FROM ' . $this->table . ' AS bb' . ' LEFT JOIN ' . $this->table . ' AS aa ON bb.read_item = aa.read_item ' . ' WHERE aa.post_id > bb.post_id'; |
132
|
|
|
if (!$result = $this->db->queryF($sql)) { |
133
|
|
|
//xoops_error($this->db->error()); |
|
|
|
|
134
|
|
|
return false; |
135
|
|
|
} |
136
|
|
|
// irmtfan if read_expire = 0 dont clean |
137
|
|
|
if (empty($this->lifetime)) { |
138
|
|
|
return true; |
139
|
|
|
} |
140
|
|
|
// irmtfan move here and rephrase |
141
|
|
|
$expire = time() - (int)$this->lifetime; |
142
|
|
|
$sql = 'DELETE FROM ' . $this->table . ' WHERE read_time < ' . $expire; |
143
|
|
|
if (!$result = $this->db->queryF($sql)) { |
144
|
|
|
//xoops_error($this->db->error()); |
|
|
|
|
145
|
|
|
return false; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return true; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
// END irmtfan rephrase function to 1- add clearDuplicate and 2- dont clean when read_expire = 0 |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param $read_item |
155
|
|
|
* @param null $uid |
156
|
|
|
* @return bool|mixed|null |
157
|
|
|
*/ |
158
|
|
|
public function getRead($read_item, $uid = null) |
159
|
|
|
{ |
160
|
|
|
if (empty($this->mode)) { |
161
|
|
|
return null; |
162
|
|
|
} |
163
|
|
|
if (1 == $this->mode) { |
164
|
|
|
return $this->getReadCookie($read_item); |
165
|
|
|
} else { |
166
|
|
|
return $this->getReadDb($read_item, $uid); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param $item_id |
172
|
|
|
* @return mixed |
173
|
|
|
*/ |
174
|
|
|
public function getReadCookie($item_id) |
175
|
|
|
{ |
176
|
|
|
$cookie_name = ('forum' === $this->type) ? 'LF' : 'LT'; |
177
|
|
|
$cookie_var = $item_id; |
178
|
|
|
// irmtfan set true to return array |
179
|
|
|
$lastview = newbbGetCookie($cookie_name, true); |
180
|
|
|
|
181
|
|
|
return @$lastview[$cookie_var]; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param $read_item |
186
|
|
|
* @param $uid |
187
|
|
|
* @return bool|null |
188
|
|
|
*/ |
189
|
|
|
public function getReadDb($read_item, $uid) |
|
|
|
|
190
|
|
|
{ |
191
|
|
View Code Duplication |
if (empty($uid)) { |
|
|
|
|
192
|
|
|
if (is_object($GLOBALS['xoopsUser'])) { |
193
|
|
|
$uid = $GLOBALS['xoopsUser']->getVar('uid'); |
194
|
|
|
} else { |
195
|
|
|
return false; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
$sql = 'SELECT post_id ' . ' FROM ' . $this->table . ' WHERE read_item = ' . (int)$read_item . ' AND uid = ' . (int)$uid; |
199
|
|
|
if (!$result = $this->db->queryF($sql, 1)) { |
200
|
|
|
return null; |
201
|
|
|
} |
202
|
|
|
list($post_id) = $this->db->fetchRow($result); |
203
|
|
|
|
204
|
|
|
return $post_id; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param $read_item |
209
|
|
|
* @param $post_id |
210
|
|
|
* @param null $uid |
211
|
|
|
* @return bool|mixed|void |
212
|
|
|
*/ |
213
|
|
|
public function setRead($read_item, $post_id, $uid = null) |
214
|
|
|
{ |
215
|
|
|
if (empty($this->mode)) { |
216
|
|
|
return true; |
217
|
|
|
} |
218
|
|
|
if (1 == $this->mode) { |
219
|
|
|
return $this->setReadCookie($read_item, $post_id); |
220
|
|
|
} else { |
221
|
|
|
return $this->setReadDb($read_item, $post_id, $uid); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param $read_item |
227
|
|
|
* @param $post_id |
228
|
|
|
*/ |
229
|
|
|
public function setReadCookie($read_item, $post_id) |
230
|
|
|
{ |
231
|
|
|
$cookie_name = ('forum' === $this->type) ? 'LF' : 'LT'; |
232
|
|
|
$lastview = newbbGetCookie($cookie_name, true); |
233
|
|
|
$lastview[$read_item] = time(); |
234
|
|
|
newbbSetCookie($cookie_name, $lastview); |
|
|
|
|
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param $read_item |
239
|
|
|
* @param $post_id |
240
|
|
|
* @param $uid |
241
|
|
|
* @return bool|mixed |
242
|
|
|
*/ |
243
|
|
|
public function setReadDb($read_item, $post_id, $uid) |
|
|
|
|
244
|
|
|
{ |
245
|
|
View Code Duplication |
if (empty($uid)) { |
|
|
|
|
246
|
|
|
if (is_object($GLOBALS['xoopsUser'])) { |
247
|
|
|
$uid = $GLOBALS['xoopsUser']->getVar('uid'); |
248
|
|
|
} else { |
249
|
|
|
return false; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
$sql = 'UPDATE ' . $this->table . ' SET post_id = ' . (int)$post_id . ',' . ' read_time =' . time() . ' WHERE read_item = ' . (int)$read_item . ' AND uid = ' . (int)$uid; |
254
|
|
|
if ($this->db->queryF($sql) && $this->db->getAffectedRows()) { |
255
|
|
|
return true; |
256
|
|
|
} |
257
|
|
|
$object = $this->create(); |
258
|
|
|
$object->setVar('read_item', $read_item); |
259
|
|
|
$object->setVar('post_id', $post_id); |
260
|
|
|
$object->setVar('uid', $uid); |
261
|
|
|
$object->setVar('read_time', time()); |
262
|
|
|
|
263
|
|
|
return parent::insert($object); |
|
|
|
|
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @param $items |
268
|
|
|
* @param null $uid |
269
|
|
|
* @return array|null |
270
|
|
|
*/ |
271
|
|
|
public function isReadItems(&$items, $uid = null) |
272
|
|
|
{ |
273
|
|
|
$ret = null; |
274
|
|
|
if (empty($this->mode)) { |
275
|
|
|
return $ret; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
if (1 == $this->mode) { |
279
|
|
|
$ret = $this->isReadItemsCookie($items); |
280
|
|
|
} else { |
281
|
|
|
$ret = $this->isReadItemsDb($items, $uid); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
return $ret; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @param $items |
289
|
|
|
* @return array |
290
|
|
|
*/ |
291
|
|
|
public function isReadItemsCookie(&$items) |
|
|
|
|
292
|
|
|
{ |
293
|
|
|
$cookie_name = ('forum' === $this->type) ? 'LF' : 'LT'; |
294
|
|
|
$cookie_vars = newbbGetCookie($cookie_name, true); |
295
|
|
|
|
296
|
|
|
$ret = []; |
297
|
|
|
foreach ($items as $key => $last_update) { |
298
|
|
|
$ret[$key] = (max(@$GLOBALS['last_visit'], @$cookie_vars[$key]) >= $last_update); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
return $ret; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @param $items |
306
|
|
|
* @param $uid |
307
|
|
|
* @return array |
308
|
|
|
*/ |
309
|
|
|
public function isReadItemsDb(&$items, $uid) |
|
|
|
|
310
|
|
|
{ |
311
|
|
|
$ret = []; |
312
|
|
|
if (empty($items)) { |
313
|
|
|
return $ret; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
View Code Duplication |
if (empty($uid)) { |
|
|
|
|
317
|
|
|
if (is_object($GLOBALS['xoopsUser'])) { |
318
|
|
|
$uid = $GLOBALS['xoopsUser']->getVar('uid'); |
319
|
|
|
} else { |
320
|
|
|
return $ret; |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
$criteria = new CriteriaCompo(new Criteria('uid', $uid)); |
325
|
|
|
$criteria->add(new Criteria('read_item', '(' . implode(', ', array_map('intval', array_keys($items))) . ')', 'IN')); |
326
|
|
|
$itemsObject = $this->getAll($criteria, ['read_item', 'post_id']); |
327
|
|
|
|
328
|
|
|
$items_list = []; |
329
|
|
|
foreach (array_keys($itemsObject) as $key) { |
330
|
|
|
$items_list[$itemsObject[$key]->getVar('read_item')] = $itemsObject[$key]->getVar('post_id'); |
331
|
|
|
} |
332
|
|
|
unset($itemsObject); |
333
|
|
|
|
334
|
|
|
foreach ($items as $key => $last_update) { |
335
|
|
|
$ret[$key] = (@$items_list[$key] >= $last_update); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
return $ret; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
// START irmtfan add clear duplicated rows function |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* @return bool |
345
|
|
|
*/ |
346
|
|
|
public function clearDuplicate() |
347
|
|
|
{ |
348
|
|
|
/** |
349
|
|
|
* This is needed for the following query GROUP BY clauses to work in MySQL 5.7. |
350
|
|
|
* This is a TEMPORARY fix. Needing this function is bad in the first place, but |
351
|
|
|
* needing sloppy SQL to make it work is worse. |
352
|
|
|
* @todo The schema itself should preclude the duplicates |
353
|
|
|
*/ |
354
|
|
|
$sql = "SET sql_mode=(SELECT REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY', ''))"; |
355
|
|
|
$this->db->queryF($sql); |
356
|
|
|
|
357
|
|
|
$sql = 'CREATE TABLE ' . $this->table . '_duplicate LIKE ' . $this->table . '; '; |
358
|
|
View Code Duplication |
if (!$result = $this->db->queryF($sql)) { |
|
|
|
|
359
|
|
|
xoops_error($this->db->error() . '<br>' . $sql); |
360
|
|
|
|
361
|
|
|
return false; |
362
|
|
|
} |
363
|
|
|
$sql = 'INSERT ' . $this->table . '_duplicate SELECT * FROM ' . $this->table . ' GROUP BY read_item, uid; '; |
364
|
|
View Code Duplication |
if (!$result = $this->db->queryF($sql)) { |
|
|
|
|
365
|
|
|
xoops_error($this->db->error() . '<br>' . $sql); |
366
|
|
|
|
367
|
|
|
return false; |
368
|
|
|
} |
369
|
|
|
$sql = 'RENAME TABLE ' . $this->table . ' TO ' . $this->table . '_with_duplicate; '; |
370
|
|
View Code Duplication |
if (!$result = $this->db->queryF($sql)) { |
|
|
|
|
371
|
|
|
xoops_error($this->db->error() . '<br>' . $sql); |
372
|
|
|
|
373
|
|
|
return false; |
374
|
|
|
} |
375
|
|
|
$sql = 'RENAME TABLE ' . $this->table . '_duplicate TO ' . $this->table . '; '; |
376
|
|
View Code Duplication |
if (!$result = $this->db->queryF($sql)) { |
|
|
|
|
377
|
|
|
xoops_error($this->db->error() . '<br>' . $sql); |
378
|
|
|
|
379
|
|
|
return false; |
380
|
|
|
} |
381
|
|
|
$sql = 'SHOW INDEX FROM ' . $this->table . " WHERE KEY_NAME = 'read_item_uid'"; |
382
|
|
|
$result = $this->db->queryF($sql); |
383
|
|
|
if (empty($result)) { |
384
|
|
|
$sql .= 'ALTER TABLE ' . $this->table . ' ADD INDEX read_item_uid ( read_item, uid ); '; |
385
|
|
View Code Duplication |
if (!$result = $this->db->queryF($sql)) { |
|
|
|
|
386
|
|
|
xoops_error($this->db->error() . '<br>' . $sql); |
387
|
|
|
|
388
|
|
|
return false; |
389
|
|
|
} |
390
|
|
|
} |
391
|
|
|
$sql = 'DROP TABLE ' . $this->table . '_with_duplicate; '; |
392
|
|
View Code Duplication |
if (!$result = $this->db->queryF($sql)) { |
|
|
|
|
393
|
|
|
xoops_error($this->db->error() . '<br>' . $sql); |
394
|
|
|
|
395
|
|
|
return false; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
return true; |
399
|
|
|
} |
400
|
|
|
// END irmtfan add clear duplicated rows function |
401
|
|
|
} |
402
|
|
|
|
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.