Passed
Push — master ( 565d4a...492cc7 )
by Michael
07:40
created
class/RateHandler.php 1 patch
Indentation   +26 added lines, -26 removed lines patch added patch discarded remove patch
@@ -39,32 +39,32 @@
 block discarded – undo
39 39
  */
40 40
 class RateHandler extends \XoopsPersistableObjectHandler
41 41
 {
42
-    /**
43
-     * @param \XoopsDatabase $db
44
-     */
45
-    public function __construct(\XoopsDatabase $db = null)
46
-    {
47
-        parent::__construct($db, 'newbb_votedata', Rate::class, 'ratingid', '');
48
-    }
42
+	/**
43
+	 * @param \XoopsDatabase $db
44
+	 */
45
+	public function __construct(\XoopsDatabase $db = null)
46
+	{
47
+		parent::__construct($db, 'newbb_votedata', Rate::class, 'ratingid', '');
48
+	}
49 49
 
50
-    /**
51
-     *
52
-     */
53
-    public function synchronization()
54
-    {
55
-        //        return;
56
-    }
50
+	/**
51
+	 *
52
+	 */
53
+	public function synchronization()
54
+	{
55
+		//        return;
56
+	}
57 57
 
58
-    /**
59
-     * clean orphan items from database
60
-     *
61
-     * @param  string $table_link
62
-     * @param  string $field_link
63
-     * @param  string $field_object
64
-     * @return bool   true on success
65
-     */
66
-    public function cleanOrphan($table_link = '', $field_link = '', $field_object = '') //cleanOrphan()
67
-    {
68
-        return parent::cleanOrphan($this->db->prefix('newbb_topics'), 'topic_id');
69
-    }
58
+	/**
59
+	 * clean orphan items from database
60
+	 *
61
+	 * @param  string $table_link
62
+	 * @param  string $field_link
63
+	 * @param  string $field_object
64
+	 * @return bool   true on success
65
+	 */
66
+	public function cleanOrphan($table_link = '', $field_link = '', $field_object = '') //cleanOrphan()
67
+	{
68
+		return parent::cleanOrphan($this->db->prefix('newbb_topics'), 'topic_id');
69
+	}
70 70
 }
Please login to merge, or discard this patch.
class/UserstatsHandler.php 1 patch
Indentation   +62 added lines, -62 removed lines patch added patch discarded remove patch
@@ -20,47 +20,47 @@  discard block
 block discarded – undo
20 20
  */
21 21
 class UserstatsHandler extends \XoopsPersistableObjectHandler
22 22
 {
23
-    /**
24
-     * @param \XoopsDatabase $db
25
-     */
26
-    public function __construct(\XoopsDatabase $db = null)
27
-    {
28
-        parent::__construct($db, 'newbb_user_stats', Userstats::class, 'uid', '');
29
-    }
30
-
31
-    /**
32
-     * @param  null $db
33
-     * @return UserstatsHandler
34
-     */
35
-    public static function getInstance($db = null)
36
-    {
37
-        static $instance;
38
-        if (null === $instance) {
39
-            $instance = new static($db);
40
-        }
41
-
42
-        return $instance;
43
-    }
44
-
45
-    /**
46
-     * @param  mixed $id
47
-     * @param  null  $fields
48
-     * @return null|\XoopsObject
49
-     */
50
-    public function get($id = null, $fields = null) //get($id)
51
-    {
52
-        $object = null;
53
-        if (!$id = (int)$id) {
54
-            return $object;
55
-        }
56
-        $object = $this->create(false);
57
-        $object->setVar($this->keyName, $id);
58
-        if (!$row = $this->getStats($id)) {
59
-            return $object;
60
-        }
61
-        $object->assignVars($row);
62
-
63
-        /*
23
+	/**
24
+	 * @param \XoopsDatabase $db
25
+	 */
26
+	public function __construct(\XoopsDatabase $db = null)
27
+	{
28
+		parent::__construct($db, 'newbb_user_stats', Userstats::class, 'uid', '');
29
+	}
30
+
31
+	/**
32
+	 * @param  null $db
33
+	 * @return UserstatsHandler
34
+	 */
35
+	public static function getInstance($db = null)
36
+	{
37
+		static $instance;
38
+		if (null === $instance) {
39
+			$instance = new static($db);
40
+		}
41
+
42
+		return $instance;
43
+	}
44
+
45
+	/**
46
+	 * @param  mixed $id
47
+	 * @param  null  $fields
48
+	 * @return null|\XoopsObject
49
+	 */
50
+	public function get($id = null, $fields = null) //get($id)
51
+	{
52
+		$object = null;
53
+		if (!$id = (int)$id) {
54
+			return $object;
55
+		}
56
+		$object = $this->create(false);
57
+		$object->setVar($this->keyName, $id);
58
+		if (!$row = $this->getStats($id)) {
59
+			return $object;
60
+		}
61
+		$object->assignVars($row);
62
+
63
+		/*
64 64
         $sql = "SELECT * FROM " . $this->table . " WHERE ".$this->keyName." = " . $id;
65 65
         if (!$result = $this->db->query($sql)) {
66 66
             return $object;
@@ -70,27 +70,27 @@  discard block
 block discarded – undo
70 70
         }
71 71
         */
72 72
 
73
-        return $object;
74
-    }
75
-
76
-    /**
77
-     * @param $id
78
-     * @return null|array
79
-     */
80
-    public function getStats($id)
81
-    {
82
-        if (empty($id)) {
83
-            return null;
84
-        }
85
-        $sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->keyName . ' = ' . (int)$id;
86
-        if (!$result = $this->db->query($sql)) {
87
-            return null;
88
-        }
89
-        $row = $this->db->fetchArray($result);
90
-
91
-        return $row;
92
-    }
93
-    /*
73
+		return $object;
74
+	}
75
+
76
+	/**
77
+	 * @param $id
78
+	 * @return null|array
79
+	 */
80
+	public function getStats($id)
81
+	{
82
+		if (empty($id)) {
83
+			return null;
84
+		}
85
+		$sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->keyName . ' = ' . (int)$id;
86
+		if (!$result = $this->db->query($sql)) {
87
+			return null;
88
+		}
89
+		$row = $this->db->fetchArray($result);
90
+
91
+		return $row;
92
+	}
93
+	/*
94 94
         function insert(\XoopsObject $object, $force = true)
95 95
         {
96 96
             if (!$object->isDirty()) {
Please login to merge, or discard this patch.
class/PermissionHandler.php 1 patch
Indentation   +398 added lines, -398 removed lines patch added patch discarded remove patch
@@ -19,7 +19,7 @@  discard block
 block discarded – undo
19 19
 
20 20
 // Initializing XoopsGroupPermHandler if not loaded yet
21 21
 if (!class_exists('XoopsGroupPermHandler')) {
22
-    require_once $GLOBALS['xoops']->path('kernel/groupperm.php');
22
+	require_once $GLOBALS['xoops']->path('kernel/groupperm.php');
23 23
 }
24 24
 
25 25
 /**
@@ -27,402 +27,402 @@  discard block
 block discarded – undo
27 27
  */
28 28
 class PermissionHandler extends \XoopsGroupPermHandler
29 29
 {
30
-    /** @var \Xmf\Module\Helper\Cache */
31
-    protected $cacheHelper;
32
-
33
-    /** @var array */
34
-    private $_handler;
35
-
36
-    /**
37
-     * @param \XoopsDatabase $db
38
-     */
39
-    public function __construct(\XoopsDatabase $db = null)
40
-    {
41
-        $this->cacheHelper = new \Xmf\Module\Helper\Cache('newbb');
42
-
43
-        $this->db = $db;
44
-        parent::__construct($db);
45
-    }
46
-
47
-    /**
48
-     * @param $name
49
-     * @return mixed
50
-     */
51
-    public function loadHandler($name)
52
-    {
53
-        if (!isset($this->_handler[$name])) {
30
+	/** @var \Xmf\Module\Helper\Cache */
31
+	protected $cacheHelper;
32
+
33
+	/** @var array */
34
+	private $_handler;
35
+
36
+	/**
37
+	 * @param \XoopsDatabase $db
38
+	 */
39
+	public function __construct(\XoopsDatabase $db = null)
40
+	{
41
+		$this->cacheHelper = new \Xmf\Module\Helper\Cache('newbb');
42
+
43
+		$this->db = $db;
44
+		parent::__construct($db);
45
+	}
46
+
47
+	/**
48
+	 * @param $name
49
+	 * @return mixed
50
+	 */
51
+	public function loadHandler($name)
52
+	{
53
+		if (!isset($this->_handler[$name])) {
54 54
 //            require_once __DIR__ . "/permission.{$name}.php";
55
-            $className             = '\\XoopsModules\\Newbb\\Permission' . ucfirst($name) . 'Handler';
56
-            $this->_handler[$name] = new $className($this->db);
57
-        }
58
-
59
-        return $this->_handler[$name];
60
-    }
61
-
62
-    /**
63
-     * @param  bool $fullname
64
-     * @return mixed
65
-     */
66
-    public function getValidForumPerms($fullname = false)
67
-    {
68
-        $handler = $this->loadHandler('forum');
69
-
70
-        return $handler->getValidPerms($fullname);
71
-    }
72
-
73
-    /**
74
-     * @param  int  $forum
75
-     * @param  bool $topic_locked
76
-     * @param  bool $isAdmin
77
-     * @return mixed
78
-     */
79
-    public function getPermissionTable($forum = 0, $topic_locked = false, $isAdmin = false)
80
-    {
81
-        $handler = $this->loadHandler('forum');
82
-        $perm    = $handler->getPermissionTable($forum, $topic_locked, $isAdmin);
83
-
84
-        return $perm;
85
-    }
86
-
87
-    /**
88
-     * @param $forum_id
89
-     * @return mixed
90
-     */
91
-    public function deleteByForum($forum_id)
92
-    {
93
-        $this->cacheHelper->delete('permission_forum');
94
-        $handler = $this->loadHandler('forum');
95
-
96
-        return $handler->deleteByForum($forum_id);
97
-    }
98
-
99
-    /**
100
-     * @param $cat_id
101
-     * @return mixed
102
-     */
103
-    public function deleteByCategory($cat_id)
104
-    {
105
-        $this->cacheHelper->delete('permission_category');
106
-        $handler = $this->loadHandler('category');
107
-
108
-        return $handler->deleteByCategory($cat_id);
109
-    }
110
-
111
-    /**
112
-     * @param        $category
113
-     * @param  array $groups
114
-     * @return mixed
115
-     */
116
-    public function setCategoryPermission($category, array $groups = [])
117
-    {
118
-        $this->cacheHelper->delete('permission_category');
119
-        $handler = $this->loadHandler('category');
120
-
121
-        return $handler->setCategoryPermission($category, $groups);
122
-    }
123
-
124
-    /**
125
-     * @param         $type
126
-     * @param  string $gperm_name
127
-     * @param  int    $id
128
-     * @return bool
129
-     */
130
-    public function getPermission($type, $gperm_name = 'access', $id = 0)
131
-    {
132
-        global $xoopsModule;
133
-        $ret = false;
134
-        if ($GLOBALS['xoopsUserIsAdmin'] && 'newbb' === $xoopsModule->getVar('dirname')) {
135
-            $ret = true;
136
-        }
137
-
138
-        $groups = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : [XOOPS_GROUP_ANONYMOUS];
139
-        if (!$groups) {
140
-            $ret = false;
141
-        }
142
-        if (!$allowed_groups = $this->getGroups("{$type}_{$gperm_name}", $id)) {
143
-            $ret = false;
144
-        }
145
-
146
-        if (count(array_intersect($allowed_groups, $groups)) > 0) {
147
-            $ret = true;
148
-        }
149
-
150
-        return $ret;
151
-    }
152
-
153
-    /**
154
-     * @param  string $perm_name
155
-     * @return array
156
-     */
157
-    public function &getCategories($perm_name = 'access')
158
-    {
159
-        $ret = $this->getAllowedItems('category', "category_{$perm_name}");
160
-
161
-        return $ret;
162
-    }
163
-
164
-    /**
165
-     * @param  string $perm_name
166
-     * @return array
167
-     */
168
-    public function getForums($perm_name = 'access')
169
-    {
170
-        $ret = $this->getAllowedItems('forum', "forum_{$perm_name}");
171
-
172
-        return $ret;
173
-    }
174
-
175
-    /**
176
-     * @param $type
177
-     * @param $perm_name
178
-     * @return array
179
-     */
180
-    public function getAllowedItems($type, $perm_name)
181
-    {
182
-        $ret = [];
183
-
184
-        $groups = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : [XOOPS_GROUP_ANONYMOUS];
185
-        if (count($groups) < 1) {
186
-            return $ret;
187
-        }
188
-
189
-        if (!$_cachedPerms = $this->loadPermData($perm_name, $type)) {
190
-            return $ret;
191
-        }
192
-
193
-        $allowed_items = [];
194
-        foreach ($_cachedPerms as $id => $allowed_groups) {
195
-            if (0 == $id || empty($allowed_groups)) {
196
-                continue;
197
-            }
198
-
199
-            if (array_intersect($groups, $allowed_groups)) {
200
-                $allowed_items[$id] = 1;
201
-            }
202
-        }
203
-        unset($_cachedPerms);
204
-        $ret = array_keys($allowed_items);
205
-
206
-        return $ret;
207
-    }
208
-
209
-    /**
210
-     * @param        $gperm_name
211
-     * @param  int   $id
212
-     * @return array
213
-     */
214
-    public function getGroups($gperm_name, $id = 0)
215
-    {
216
-        $_cachedPerms = $this->loadPermData($gperm_name);
217
-        $groups       = empty($_cachedPerms[$id]) ? [] : array_unique($_cachedPerms[$id]);
218
-        unset($_cachedPerms);
219
-
220
-        return $groups;
221
-    }
222
-
223
-    /**
224
-     * @param  string $perm_name
225
-     * @return array
226
-     */
227
-    public function createPermData($perm_name = 'forum_all')
228
-    {
229
-        global $xoopsModule;
230
-        /** @var \XoopsModuleHandler $moduleHandler */
231
-        $perms = [];
232
-
233
-        if (is_object($xoopsModule) && 'newbb' === $xoopsModule->getVar('dirname')) {
234
-            $modid = $xoopsModule->getVar('mid');
235
-        } else {
236
-            $moduleHandler = xoops_getHandler('module');
237
-            $module        = $moduleHandler->getByDirname('newbb');
238
-            $modid         = $module->getVar('mid');
239
-            unset($module);
240
-        }
241
-
242
-        if (in_array($perm_name, ['forum_all', 'category_all'], true)) {
243
-            /** @var \XoopsMemberHandler $memberHandler */
244
-            $memberHandler = xoops_getHandler('member');
245
-            $groups        = array_keys($memberHandler->getGroupList());
246
-
247
-            $type          = ('category_all' === $perm_name) ? 'category' : 'forum';
248
-            $objectHandler = Newbb\Helper::getInstance()->getHandler($type);
249
-            $object_ids    = $objectHandler->getIds();
250
-            foreach ($object_ids as $item_id) {
251
-                $perms[$perm_name][$item_id] = $groups;
252
-            }
253
-        } else {
254
-            $grouppermHandler = xoops_getHandler('groupperm');
255
-            $criteria     = new \CriteriaCompo(new \Criteria('gperm_modid', $modid));
256
-            if (!empty($perm_name) && 'forum_all' !== $perm_name && 'category_all' !== $perm_name) {
257
-                $criteria->add(new \Criteria('gperm_name', $perm_name));
258
-            }
259
-            $permissions = $this->getObjects($criteria);
260
-
261
-            foreach ($permissions as $gperm) {
262
-                $item_id                                         = $gperm->getVar('gperm_itemid');
263
-                $group_id                                        = (int)$gperm->getVar('gperm_groupid');
264
-                $perms[$gperm->getVar('gperm_name')][$item_id][] = $group_id;
265
-            }
266
-        }
267
-        if (count($perms) > 0) {
268
-            foreach (array_keys($perms) as $perm) {
269
-                $this->cacheHelper->write("permission_{$perm}", $perms[$perm]);
270
-            }
271
-        }
272
-        $ret = !empty($perm_name) ? @$perms[$perm_name] : $perms;
273
-
274
-        return $ret;
275
-    }
276
-
277
-    /**
278
-     * @param  string $perm_name
279
-     * @return array|mixed|null
280
-     */
281
-    public function &loadPermData($perm_name = 'forum_access')
282
-    {
283
-        if (!$perms = $this->cacheHelper->read("permission_{$perm_name}")) {
284
-            $perms = $this->createPermData($perm_name);
285
-        }
286
-
287
-        return $perms;
288
-    }
289
-
290
-    /**
291
-     * @param       $perm
292
-     * @param       $itemid
293
-     * @param       $groupid
294
-     * @param  null $mid
295
-     * @return bool
296
-     */
297
-    public function validateRight($perm, $itemid, $groupid, $mid = null)
298
-    {
299
-        if (empty($mid)) {
300
-            if (is_object($GLOBALS['xoopsModule']) && 'newbb' === $GLOBALS['xoopsModule']->getVar('dirname')) {
301
-                $mid = $GLOBALS['xoopsModule']->getVar('mid');
302
-            } else {
303
-                /** @var \XoopsModuleHandler $moduleHandler */
304
-                $moduleHandler = xoops_getHandler('module');
305
-                $mod           = $moduleHandler->getByDirname('newbb');
306
-                $mid           = $mod->getVar('mid');
307
-                unset($mod);
308
-            }
309
-        }
310
-        if ($this->myCheckRight($perm, $itemid, $groupid, $mid)) {
311
-            return true;
312
-        }
313
-        $this->cacheHelper->delete('permission');
314
-        $this->addRight($perm, $itemid, $groupid, $mid);
315
-
316
-        return true;
317
-    }
318
-
319
-    /**
320
-     * Check permission (directly)
321
-     *
322
-     * @param string $gperm_name   Name of permission
323
-     * @param int    $gperm_itemid ID of an item
324
-     * @param        int           /array $gperm_groupid A group ID or an array of group IDs
325
-     * @param int    $gperm_modid  ID of a module
326
-     *
327
-     * @return bool TRUE if permission is enabled
328
-     */
329
-    public function myCheckRight($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid = 1)
330
-    {
331
-        $ret      = false;
332
-        $criteria = new \CriteriaCompo(new \Criteria('gperm_modid', $gperm_modid));
333
-        $criteria->add(new \Criteria('gperm_name', $gperm_name));
334
-        $gperm_itemid = (int)$gperm_itemid;
335
-        if ($gperm_itemid > 0) {
336
-            $criteria->add(new \Criteria('gperm_itemid', $gperm_itemid));
337
-        }
338
-        if (is_array($gperm_groupid)) {
339
-            $criteria2 = new \CriteriaCompo();
340
-            foreach ($gperm_groupid as $gid) {
341
-                $criteria2->add(new \Criteria('gperm_groupid', $gid), 'OR');
342
-            }
343
-            $criteria->add($criteria2);
344
-        } else {
345
-            $criteria->add(new \Criteria('gperm_groupid', $gperm_groupid));
346
-        }
347
-        if ($this->getCount($criteria) > 0) {
348
-            $ret = true;
349
-        }
350
-
351
-        return $ret;
352
-    }
353
-
354
-    /**
355
-     * @param       $perm
356
-     * @param       $itemid
357
-     * @param       $groupid
358
-     * @param  null $mid
359
-     * @return bool
360
-     */
361
-    public function deleteRight($perm, $itemid, $groupid, $mid = null)
362
-    {
363
-        $this->cacheHelper->delete('permission');
364
-        if (null === $mid) {
365
-            if (is_object($GLOBALS['xoopsModule']) && 'newbb' === $GLOBALS['xoopsModule']->getVar('dirname')) {
366
-                $mid = $GLOBALS['xoopsModule']->getVar('mid');
367
-            } else {
368
-                /** @var \XoopsModuleHandler $moduleHandler */
369
-                $moduleHandler = xoops_getHandler('module');
370
-                $mod           = $moduleHandler->getByDirname('newbb');
371
-                $mid           = $mod->getVar('mid');
372
-                unset($mod);
373
-            }
374
-        }
375
-        if (is_callable('parent::deleteRight')) {
376
-            return parent::deleteRight($perm, $itemid, $groupid, $mid);
377
-        } else {
378
-            $criteria = new \CriteriaCompo(new \Criteria('gperm_name', $perm));
379
-            $criteria->add(new \Criteria('gperm_groupid', $groupid));
380
-            $criteria->add(new \Criteria('gperm_itemid', $itemid));
381
-            $criteria->add(new \Criteria('gperm_modid', $mid));
382
-            $permsObject = $this->getObjects($criteria);
383
-            if (!empty($permsObject)) {
384
-                foreach ($permsObject as $permObject) {
385
-                    $this->delete($permObject);
386
-                }
387
-            }
388
-            unset($criteria, $permsObject);
389
-        }
390
-
391
-        return true;
392
-    }
393
-
394
-    /**
395
-     * @param        $forum
396
-     * @param  int   $mid
397
-     * @return mixed
398
-     */
399
-    public function applyTemplate($forum, $mid = 0)
400
-    {
401
-        $this->cacheHelper->delete('permission_forum');
402
-        $handler = $this->loadHandler('forum');
403
-
404
-        return $handler->applyTemplate($forum, $mid);
405
-    }
406
-
407
-    /**
408
-     * @return mixed
409
-     */
410
-    public function getTemplate()
411
-    {
412
-        $handler  = $this->loadHandler('forum');
413
-        $template = $handler->getTemplate();
414
-
415
-        return $template;
416
-    }
417
-
418
-    /**
419
-     * @param $perms
420
-     * @return mixed
421
-     */
422
-    public function setTemplate($perms)
423
-    {
424
-        $handler = $this->loadHandler('forum');
425
-
426
-        return $handler->setTemplate($perms);
427
-    }
55
+			$className             = '\\XoopsModules\\Newbb\\Permission' . ucfirst($name) . 'Handler';
56
+			$this->_handler[$name] = new $className($this->db);
57
+		}
58
+
59
+		return $this->_handler[$name];
60
+	}
61
+
62
+	/**
63
+	 * @param  bool $fullname
64
+	 * @return mixed
65
+	 */
66
+	public function getValidForumPerms($fullname = false)
67
+	{
68
+		$handler = $this->loadHandler('forum');
69
+
70
+		return $handler->getValidPerms($fullname);
71
+	}
72
+
73
+	/**
74
+	 * @param  int  $forum
75
+	 * @param  bool $topic_locked
76
+	 * @param  bool $isAdmin
77
+	 * @return mixed
78
+	 */
79
+	public function getPermissionTable($forum = 0, $topic_locked = false, $isAdmin = false)
80
+	{
81
+		$handler = $this->loadHandler('forum');
82
+		$perm    = $handler->getPermissionTable($forum, $topic_locked, $isAdmin);
83
+
84
+		return $perm;
85
+	}
86
+
87
+	/**
88
+	 * @param $forum_id
89
+	 * @return mixed
90
+	 */
91
+	public function deleteByForum($forum_id)
92
+	{
93
+		$this->cacheHelper->delete('permission_forum');
94
+		$handler = $this->loadHandler('forum');
95
+
96
+		return $handler->deleteByForum($forum_id);
97
+	}
98
+
99
+	/**
100
+	 * @param $cat_id
101
+	 * @return mixed
102
+	 */
103
+	public function deleteByCategory($cat_id)
104
+	{
105
+		$this->cacheHelper->delete('permission_category');
106
+		$handler = $this->loadHandler('category');
107
+
108
+		return $handler->deleteByCategory($cat_id);
109
+	}
110
+
111
+	/**
112
+	 * @param        $category
113
+	 * @param  array $groups
114
+	 * @return mixed
115
+	 */
116
+	public function setCategoryPermission($category, array $groups = [])
117
+	{
118
+		$this->cacheHelper->delete('permission_category');
119
+		$handler = $this->loadHandler('category');
120
+
121
+		return $handler->setCategoryPermission($category, $groups);
122
+	}
123
+
124
+	/**
125
+	 * @param         $type
126
+	 * @param  string $gperm_name
127
+	 * @param  int    $id
128
+	 * @return bool
129
+	 */
130
+	public function getPermission($type, $gperm_name = 'access', $id = 0)
131
+	{
132
+		global $xoopsModule;
133
+		$ret = false;
134
+		if ($GLOBALS['xoopsUserIsAdmin'] && 'newbb' === $xoopsModule->getVar('dirname')) {
135
+			$ret = true;
136
+		}
137
+
138
+		$groups = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : [XOOPS_GROUP_ANONYMOUS];
139
+		if (!$groups) {
140
+			$ret = false;
141
+		}
142
+		if (!$allowed_groups = $this->getGroups("{$type}_{$gperm_name}", $id)) {
143
+			$ret = false;
144
+		}
145
+
146
+		if (count(array_intersect($allowed_groups, $groups)) > 0) {
147
+			$ret = true;
148
+		}
149
+
150
+		return $ret;
151
+	}
152
+
153
+	/**
154
+	 * @param  string $perm_name
155
+	 * @return array
156
+	 */
157
+	public function &getCategories($perm_name = 'access')
158
+	{
159
+		$ret = $this->getAllowedItems('category', "category_{$perm_name}");
160
+
161
+		return $ret;
162
+	}
163
+
164
+	/**
165
+	 * @param  string $perm_name
166
+	 * @return array
167
+	 */
168
+	public function getForums($perm_name = 'access')
169
+	{
170
+		$ret = $this->getAllowedItems('forum', "forum_{$perm_name}");
171
+
172
+		return $ret;
173
+	}
174
+
175
+	/**
176
+	 * @param $type
177
+	 * @param $perm_name
178
+	 * @return array
179
+	 */
180
+	public function getAllowedItems($type, $perm_name)
181
+	{
182
+		$ret = [];
183
+
184
+		$groups = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : [XOOPS_GROUP_ANONYMOUS];
185
+		if (count($groups) < 1) {
186
+			return $ret;
187
+		}
188
+
189
+		if (!$_cachedPerms = $this->loadPermData($perm_name, $type)) {
190
+			return $ret;
191
+		}
192
+
193
+		$allowed_items = [];
194
+		foreach ($_cachedPerms as $id => $allowed_groups) {
195
+			if (0 == $id || empty($allowed_groups)) {
196
+				continue;
197
+			}
198
+
199
+			if (array_intersect($groups, $allowed_groups)) {
200
+				$allowed_items[$id] = 1;
201
+			}
202
+		}
203
+		unset($_cachedPerms);
204
+		$ret = array_keys($allowed_items);
205
+
206
+		return $ret;
207
+	}
208
+
209
+	/**
210
+	 * @param        $gperm_name
211
+	 * @param  int   $id
212
+	 * @return array
213
+	 */
214
+	public function getGroups($gperm_name, $id = 0)
215
+	{
216
+		$_cachedPerms = $this->loadPermData($gperm_name);
217
+		$groups       = empty($_cachedPerms[$id]) ? [] : array_unique($_cachedPerms[$id]);
218
+		unset($_cachedPerms);
219
+
220
+		return $groups;
221
+	}
222
+
223
+	/**
224
+	 * @param  string $perm_name
225
+	 * @return array
226
+	 */
227
+	public function createPermData($perm_name = 'forum_all')
228
+	{
229
+		global $xoopsModule;
230
+		/** @var \XoopsModuleHandler $moduleHandler */
231
+		$perms = [];
232
+
233
+		if (is_object($xoopsModule) && 'newbb' === $xoopsModule->getVar('dirname')) {
234
+			$modid = $xoopsModule->getVar('mid');
235
+		} else {
236
+			$moduleHandler = xoops_getHandler('module');
237
+			$module        = $moduleHandler->getByDirname('newbb');
238
+			$modid         = $module->getVar('mid');
239
+			unset($module);
240
+		}
241
+
242
+		if (in_array($perm_name, ['forum_all', 'category_all'], true)) {
243
+			/** @var \XoopsMemberHandler $memberHandler */
244
+			$memberHandler = xoops_getHandler('member');
245
+			$groups        = array_keys($memberHandler->getGroupList());
246
+
247
+			$type          = ('category_all' === $perm_name) ? 'category' : 'forum';
248
+			$objectHandler = Newbb\Helper::getInstance()->getHandler($type);
249
+			$object_ids    = $objectHandler->getIds();
250
+			foreach ($object_ids as $item_id) {
251
+				$perms[$perm_name][$item_id] = $groups;
252
+			}
253
+		} else {
254
+			$grouppermHandler = xoops_getHandler('groupperm');
255
+			$criteria     = new \CriteriaCompo(new \Criteria('gperm_modid', $modid));
256
+			if (!empty($perm_name) && 'forum_all' !== $perm_name && 'category_all' !== $perm_name) {
257
+				$criteria->add(new \Criteria('gperm_name', $perm_name));
258
+			}
259
+			$permissions = $this->getObjects($criteria);
260
+
261
+			foreach ($permissions as $gperm) {
262
+				$item_id                                         = $gperm->getVar('gperm_itemid');
263
+				$group_id                                        = (int)$gperm->getVar('gperm_groupid');
264
+				$perms[$gperm->getVar('gperm_name')][$item_id][] = $group_id;
265
+			}
266
+		}
267
+		if (count($perms) > 0) {
268
+			foreach (array_keys($perms) as $perm) {
269
+				$this->cacheHelper->write("permission_{$perm}", $perms[$perm]);
270
+			}
271
+		}
272
+		$ret = !empty($perm_name) ? @$perms[$perm_name] : $perms;
273
+
274
+		return $ret;
275
+	}
276
+
277
+	/**
278
+	 * @param  string $perm_name
279
+	 * @return array|mixed|null
280
+	 */
281
+	public function &loadPermData($perm_name = 'forum_access')
282
+	{
283
+		if (!$perms = $this->cacheHelper->read("permission_{$perm_name}")) {
284
+			$perms = $this->createPermData($perm_name);
285
+		}
286
+
287
+		return $perms;
288
+	}
289
+
290
+	/**
291
+	 * @param       $perm
292
+	 * @param       $itemid
293
+	 * @param       $groupid
294
+	 * @param  null $mid
295
+	 * @return bool
296
+	 */
297
+	public function validateRight($perm, $itemid, $groupid, $mid = null)
298
+	{
299
+		if (empty($mid)) {
300
+			if (is_object($GLOBALS['xoopsModule']) && 'newbb' === $GLOBALS['xoopsModule']->getVar('dirname')) {
301
+				$mid = $GLOBALS['xoopsModule']->getVar('mid');
302
+			} else {
303
+				/** @var \XoopsModuleHandler $moduleHandler */
304
+				$moduleHandler = xoops_getHandler('module');
305
+				$mod           = $moduleHandler->getByDirname('newbb');
306
+				$mid           = $mod->getVar('mid');
307
+				unset($mod);
308
+			}
309
+		}
310
+		if ($this->myCheckRight($perm, $itemid, $groupid, $mid)) {
311
+			return true;
312
+		}
313
+		$this->cacheHelper->delete('permission');
314
+		$this->addRight($perm, $itemid, $groupid, $mid);
315
+
316
+		return true;
317
+	}
318
+
319
+	/**
320
+	 * Check permission (directly)
321
+	 *
322
+	 * @param string $gperm_name   Name of permission
323
+	 * @param int    $gperm_itemid ID of an item
324
+	 * @param        int           /array $gperm_groupid A group ID or an array of group IDs
325
+	 * @param int    $gperm_modid  ID of a module
326
+	 *
327
+	 * @return bool TRUE if permission is enabled
328
+	 */
329
+	public function myCheckRight($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid = 1)
330
+	{
331
+		$ret      = false;
332
+		$criteria = new \CriteriaCompo(new \Criteria('gperm_modid', $gperm_modid));
333
+		$criteria->add(new \Criteria('gperm_name', $gperm_name));
334
+		$gperm_itemid = (int)$gperm_itemid;
335
+		if ($gperm_itemid > 0) {
336
+			$criteria->add(new \Criteria('gperm_itemid', $gperm_itemid));
337
+		}
338
+		if (is_array($gperm_groupid)) {
339
+			$criteria2 = new \CriteriaCompo();
340
+			foreach ($gperm_groupid as $gid) {
341
+				$criteria2->add(new \Criteria('gperm_groupid', $gid), 'OR');
342
+			}
343
+			$criteria->add($criteria2);
344
+		} else {
345
+			$criteria->add(new \Criteria('gperm_groupid', $gperm_groupid));
346
+		}
347
+		if ($this->getCount($criteria) > 0) {
348
+			$ret = true;
349
+		}
350
+
351
+		return $ret;
352
+	}
353
+
354
+	/**
355
+	 * @param       $perm
356
+	 * @param       $itemid
357
+	 * @param       $groupid
358
+	 * @param  null $mid
359
+	 * @return bool
360
+	 */
361
+	public function deleteRight($perm, $itemid, $groupid, $mid = null)
362
+	{
363
+		$this->cacheHelper->delete('permission');
364
+		if (null === $mid) {
365
+			if (is_object($GLOBALS['xoopsModule']) && 'newbb' === $GLOBALS['xoopsModule']->getVar('dirname')) {
366
+				$mid = $GLOBALS['xoopsModule']->getVar('mid');
367
+			} else {
368
+				/** @var \XoopsModuleHandler $moduleHandler */
369
+				$moduleHandler = xoops_getHandler('module');
370
+				$mod           = $moduleHandler->getByDirname('newbb');
371
+				$mid           = $mod->getVar('mid');
372
+				unset($mod);
373
+			}
374
+		}
375
+		if (is_callable('parent::deleteRight')) {
376
+			return parent::deleteRight($perm, $itemid, $groupid, $mid);
377
+		} else {
378
+			$criteria = new \CriteriaCompo(new \Criteria('gperm_name', $perm));
379
+			$criteria->add(new \Criteria('gperm_groupid', $groupid));
380
+			$criteria->add(new \Criteria('gperm_itemid', $itemid));
381
+			$criteria->add(new \Criteria('gperm_modid', $mid));
382
+			$permsObject = $this->getObjects($criteria);
383
+			if (!empty($permsObject)) {
384
+				foreach ($permsObject as $permObject) {
385
+					$this->delete($permObject);
386
+				}
387
+			}
388
+			unset($criteria, $permsObject);
389
+		}
390
+
391
+		return true;
392
+	}
393
+
394
+	/**
395
+	 * @param        $forum
396
+	 * @param  int   $mid
397
+	 * @return mixed
398
+	 */
399
+	public function applyTemplate($forum, $mid = 0)
400
+	{
401
+		$this->cacheHelper->delete('permission_forum');
402
+		$handler = $this->loadHandler('forum');
403
+
404
+		return $handler->applyTemplate($forum, $mid);
405
+	}
406
+
407
+	/**
408
+	 * @return mixed
409
+	 */
410
+	public function getTemplate()
411
+	{
412
+		$handler  = $this->loadHandler('forum');
413
+		$template = $handler->getTemplate();
414
+
415
+		return $template;
416
+	}
417
+
418
+	/**
419
+	 * @param $perms
420
+	 * @return mixed
421
+	 */
422
+	public function setTemplate($perms)
423
+	{
424
+		$handler = $this->loadHandler('forum');
425
+
426
+		return $handler->setTemplate($perms);
427
+	}
428 428
 }
Please login to merge, or discard this patch.
class/ReportHandler.php 1 patch
Indentation   +100 added lines, -100 removed lines patch added patch discarded remove patch
@@ -19,114 +19,114 @@
 block discarded – undo
19 19
  */
20 20
 class ReportHandler extends \XoopsPersistableObjectHandler
21 21
 {
22
-    /**
23
-     * @param \XoopsDatabase $db
24
-     */
25
-    public function __construct(\XoopsDatabase $db = null)
26
-    {
27
-        parent::__construct($db, 'newbb_report', Report::class, 'report_id', '');
28
-    }
22
+	/**
23
+	 * @param \XoopsDatabase $db
24
+	 */
25
+	public function __construct(\XoopsDatabase $db = null)
26
+	{
27
+		parent::__construct($db, 'newbb_report', Report::class, 'report_id', '');
28
+	}
29 29
 
30
-    /**
31
-     * @param $posts
32
-     * @return array
33
-     */
34
-    public function getByPost($posts)
35
-    {
36
-        $ret = [];
37
-        if (!$posts) {
38
-            return $ret;
39
-        }
40
-        if (!is_array($posts)) {
41
-            $posts = [$posts];
42
-        }
43
-        $post_criteria = new \Criteria('post_id', '(' . implode(', ', $posts) . ')', 'IN');
44
-        $ret           = $this->getAll($post_criteria);
30
+	/**
31
+	 * @param $posts
32
+	 * @return array
33
+	 */
34
+	public function getByPost($posts)
35
+	{
36
+		$ret = [];
37
+		if (!$posts) {
38
+			return $ret;
39
+		}
40
+		if (!is_array($posts)) {
41
+			$posts = [$posts];
42
+		}
43
+		$post_criteria = new \Criteria('post_id', '(' . implode(', ', $posts) . ')', 'IN');
44
+		$ret           = $this->getAll($post_criteria);
45 45
 
46
-        return $ret;
47
-    }
46
+		return $ret;
47
+	}
48 48
 
49
-    /**
50
-     * @param  int|array    $forums
51
-     * @param  string $order
52
-     * @param  int    $perpage
53
-     * @param         $start
54
-     * @param  int    $report_result
55
-     * @param  int    $report_id
56
-     * @return array
57
-     */
58
-    public function getAllReports(
59
-        $forums = 0,
60
-        $order = 'ASC',
61
-        $perpage = 0,
62
-        &$start,
63
-        $report_result = 0,
64
-        $report_id = 0
65
-    ) {
66
-        $forumCriteria = '';
67
-        $row           = [];
68
-        if ('DESC' === $order) {
69
-            $operator_for_position = '>';
70
-        } else {
71
-            $order                 = 'ASC';
72
-            $operator_for_position = '<';
73
-        }
74
-        $order_criteria = " ORDER BY r.report_id $order";
49
+	/**
50
+	 * @param  int|array    $forums
51
+	 * @param  string $order
52
+	 * @param  int    $perpage
53
+	 * @param         $start
54
+	 * @param  int    $report_result
55
+	 * @param  int    $report_id
56
+	 * @return array
57
+	 */
58
+	public function getAllReports(
59
+		$forums = 0,
60
+		$order = 'ASC',
61
+		$perpage = 0,
62
+		&$start,
63
+		$report_result = 0,
64
+		$report_id = 0
65
+	) {
66
+		$forumCriteria = '';
67
+		$row           = [];
68
+		if ('DESC' === $order) {
69
+			$operator_for_position = '>';
70
+		} else {
71
+			$order                 = 'ASC';
72
+			$operator_for_position = '<';
73
+		}
74
+		$order_criteria = " ORDER BY r.report_id $order";
75 75
 
76
-        if ($perpage <= 0) {
77
-            $perpage = 10;
78
-        }
79
-        if (empty($start)) {
80
-            $start = 0;
81
-        }
82
-        $result_criteria = ' AND r.report_result = ' . $report_result;
76
+		if ($perpage <= 0) {
77
+			$perpage = 10;
78
+		}
79
+		if (empty($start)) {
80
+			$start = 0;
81
+		}
82
+		$result_criteria = ' AND r.report_result = ' . $report_result;
83 83
 
84
-        if ($forums) {
85
-            $forumCriteria = '';
86
-        } elseif (!is_array($forums)) {
87
-            $forums        = [$forums];
88
-            $forumCriteria = ' AND p.forum_id IN (' . implode(',', $forums) . ')';
89
-        }
90
-        $tables_criteria = ' FROM ' . $this->db->prefix('newbb_report') . ' r, ' . $this->db->prefix('newbb_posts') . ' p WHERE r.post_id= p.post_id';
84
+		if ($forums) {
85
+			$forumCriteria = '';
86
+		} elseif (!is_array($forums)) {
87
+			$forums        = [$forums];
88
+			$forumCriteria = ' AND p.forum_id IN (' . implode(',', $forums) . ')';
89
+		}
90
+		$tables_criteria = ' FROM ' . $this->db->prefix('newbb_report') . ' r, ' . $this->db->prefix('newbb_posts') . ' p WHERE r.post_id= p.post_id';
91 91
 
92
-        if ($report_id) {
93
-            $result = $this->db->query('SELECT COUNT(*) as report_count' . $tables_criteria . $forumCriteria . $result_criteria . " AND report_id $operator_for_position $report_id" . $order_criteria);
94
-            if ($result) {
95
-                $row = $this->db->fetchArray($result);
96
-            }
97
-            $position = $row['report_count'];
98
-            $start    = (int)($position / $perpage) * $perpage;
99
-        }
92
+		if ($report_id) {
93
+			$result = $this->db->query('SELECT COUNT(*) as report_count' . $tables_criteria . $forumCriteria . $result_criteria . " AND report_id $operator_for_position $report_id" . $order_criteria);
94
+			if ($result) {
95
+				$row = $this->db->fetchArray($result);
96
+			}
97
+			$position = $row['report_count'];
98
+			$start    = (int)($position / $perpage) * $perpage;
99
+		}
100 100
 
101
-        $sql    = 'SELECT r.*, p.subject, p.topic_id, p.forum_id' . $tables_criteria . $forumCriteria . $result_criteria . $order_criteria;
102
-        $result = $this->db->query($sql, $perpage, $start);
103
-        $ret    = [];
104
-        //$reportHandler =  Newbb\Helper::getInstance()->getHandler('Report');
105
-        while (false !== ($myrow = $this->db->fetchArray($result))) {
106
-            $ret[] = $myrow; // return as array
107
-        }
101
+		$sql    = 'SELECT r.*, p.subject, p.topic_id, p.forum_id' . $tables_criteria . $forumCriteria . $result_criteria . $order_criteria;
102
+		$result = $this->db->query($sql, $perpage, $start);
103
+		$ret    = [];
104
+		//$reportHandler =  Newbb\Helper::getInstance()->getHandler('Report');
105
+		while (false !== ($myrow = $this->db->fetchArray($result))) {
106
+			$ret[] = $myrow; // return as array
107
+		}
108 108
 
109
-        return $ret;
110
-    }
109
+		return $ret;
110
+	}
111 111
 
112
-    /**
113
-     *
114
-     */
115
-    public function synchronization()
116
-    {
117
-        //        return;
118
-    }
112
+	/**
113
+	 *
114
+	 */
115
+	public function synchronization()
116
+	{
117
+		//        return;
118
+	}
119 119
 
120
-    /**
121
-     * clean orphan items from database
122
-     *
123
-     * @param  string $table_link
124
-     * @param  string $field_link
125
-     * @param  string $field_object
126
-     * @return bool   true on success
127
-     */
128
-    public function cleanOrphan($table_link = '', $field_link = '', $field_object = '') //cleanOrphan()
129
-    {
130
-        return parent::cleanOrphan($this->db->prefix('newbb_posts'), 'post_id');
131
-    }
120
+	/**
121
+	 * clean orphan items from database
122
+	 *
123
+	 * @param  string $table_link
124
+	 * @param  string $field_link
125
+	 * @param  string $field_object
126
+	 * @return bool   true on success
127
+	 */
128
+	public function cleanOrphan($table_link = '', $field_link = '', $field_object = '') //cleanOrphan()
129
+	{
130
+		return parent::cleanOrphan($this->db->prefix('newbb_posts'), 'post_id');
131
+	}
132 132
 }
Please login to merge, or discard this patch.
class/PostHandler.php 2 patches
Indentation   +545 added lines, -545 removed lines patch added patch discarded remove patch
@@ -41,555 +41,555 @@
 block discarded – undo
41 41
  */
42 42
 class PostHandler extends \XoopsPersistableObjectHandler
43 43
 {
44
-    /**
45
-     * @param \XoopsDatabase $db
46
-     */
47
-    public function __construct(\XoopsDatabase $db = null)
48
-    {
49
-        parent::__construct($db, 'newbb_posts', Post::class, 'post_id', 'subject');
50
-    }
51
-
52
-    /**
53
-     * @param  mixed $id
54
-     * @param  null  $var
55
-     * @return null|\XoopsObject
56
-     */
57
-    public function get($id = null, $var = null) //get($id)
58
-    {
59
-        $id   = (int)$id;
60
-        $post = null;
61
-        $sql  = 'SELECT p.*, t.* FROM ' . $this->db->prefix('newbb_posts') . ' p LEFT JOIN ' . $this->db->prefix('newbb_posts_text') . ' t ON p.post_id=t.post_id WHERE p.post_id=' . $id;
62
-        if ($array = $this->db->fetchArray($this->db->query($sql))) {
63
-            $post = $this->create(false);
64
-            $post->assignVars($array);
65
-        }
66
-
67
-        return $post;
68
-    }
69
-
70
-    /**
71
-     * @param  int             $limit
72
-     * @param  int             $start
73
-     * @param  \CriteriaElement $criteria
74
-     * @param  null            $fields
75
-     * @param  bool            $asObject
76
-     * @param  int             $topic_id
77
-     * @param  int             $approved
78
-     * @return array
79
-     */
80
-    //    public function getByLimit($topic_id, $limit, $approved = 1)
81
-    public function &getByLimit(
82
-        $limit = 0,
83
-        $start = 0,
84
-        \CriteriaElement $criteria = null,
85
-        $fields = null,
86
-        $asObject = true,
87
-        $topic_id = 0,
88
-        $approved = 1
89
-    ) {
90
-        $sql    = 'SELECT p.*, t.*, tp.topic_status FROM '
91
-                  . $this->db->prefix('newbb_posts')
92
-                  . ' p LEFT JOIN '
93
-                  . $this->db->prefix('newbb_posts_text')
94
-                  . ' t ON p.post_id=t.post_id LEFT JOIN '
95
-                  . $this->db->prefix('newbb_topics')
96
-                  . ' tp ON tp.topic_id=p.topic_id WHERE p.topic_id='
97
-                  . $topic_id
98
-                  . ' AND p.approved ='
99
-                  . $approved
100
-                  . ' ORDER BY p.post_time DESC';
101
-        $result = $this->db->query($sql, $limit, 0);
102
-        $ret    = [];
103
-        while (false !== ($myrow = $this->db->fetchArray($result))) {
104
-            $post = $this->create(false);
105
-            $post->assignVars($myrow);
106
-
107
-            $ret[$myrow['post_id']] = $post;
108
-            unset($post);
109
-        }
110
-
111
-        return $ret;
112
-    }
113
-
114
-    /**
115
-     * @param Post $post
116
-     * @return mixed
117
-     */
118
-    public function getPostForPDF(&$post)
119
-    {
120
-        return $post->getPostBody(true);
121
-    }
122
-
123
-    /**
124
-     * @param Post $post
125
-     * @return mixed
126
-     */
127
-    public function getPostForPrint(&$post)
128
-    {
129
-        return $post->getPostBody();
130
-    }
131
-
132
-    /**
133
-     * @param  int|Post|\XoopsObject $post
134
-     * @param  bool $force
135
-     * @return bool
136
-     */
137
-    public function approve(&$post, $force = false)
138
-    {
139
-        if (empty($post)) {
140
-            return false;
141
-        }
142
-        if (is_numeric($post)) {
143
-            $post = $this->get($post);
144
-        }
145
-
146
-        $wasApproved = $post->getVar('approved');
147
-        // irmtfan approve post if the approved = 0 (pending) or -1 (deleted)
148
-        if (empty($force) && $wasApproved > 0) {
149
-            return true;
150
-        }
151
-        $post->setVar('approved', 1);
152
-        $this->insert($post, true);
153
-
154
-        /** @var Newbb\TopicHandler $topicHandler */
155
-        $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic');
156
-        $topicObject  = $topicHandler->get($post->getVar('topic_id'));
157
-        if ($topicObject->getVar('topic_last_post_id') < $post->getVar('post_id')) {
158
-            $topicObject->setVar('topic_last_post_id', $post->getVar('post_id'));
159
-        }
160
-        if ($post->isTopic()) {
161
-            $topicObject->setVar('approved', 1);
162
-        } else {
163
-            $topicObject->setVar('topic_replies', $topicObject->getVar('topic_replies') + 1);
164
-        }
165
-        $topicHandler->insert($topicObject, true);
166
-
167
-        /** @var Newbb\ForumHandler $forumHandler */
168
-        $forumHandler = Newbb\Helper::getInstance()->getHandler('Forum');
169
-        $forumObject  = $forumHandler->get($post->getVar('forum_id'));
170
-        if ($forumObject->getVar('forum_last_post_id') < $post->getVar('post_id')) {
171
-            $forumObject->setVar('forum_last_post_id', $post->getVar('post_id'));
172
-        }
173
-        $forumObject->setVar('forum_posts', $forumObject->getVar('forum_posts') + 1);
174
-        if ($post->isTopic()) {
175
-            $forumObject->setVar('forum_topics', $forumObject->getVar('forum_topics') + 1);
176
-        }
177
-        $forumHandler->insert($forumObject, true);
178
-
179
-        // Update user stats
180
-        if ($post->getVar('uid') > 0) {
181
-            /** @var \XoopsMemberHandler $memberHandler */
182
-            $memberHandler = xoops_getHandler('member');
183
-            $poster        = $memberHandler->getUser($post->getVar('uid'));
184
-            if (is_object($poster) && $post->getVar('uid') == $poster->getVar('uid')) {
185
-                $poster->setVar('posts', $poster->getVar('posts') + 1);
186
-                $res = $memberHandler->insertUser($poster, true);
187
-                unset($poster);
188
-            }
189
-        }
190
-
191
-        // Update forum stats
192
-        /** @var StatsHandler $statsHandler */
193
-        $statsHandler = Newbb\Helper::getInstance()->getHandler('Stats');
194
-        $statsHandler->update($post->getVar('forum_id'), 'post');
195
-        if ($post->isTopic()) {
196
-            $statsHandler->update($post->getVar('forum_id'), 'topic');
197
-        }
198
-
199
-        return true;
200
-    }
201
-
202
-    /**
203
-     * @param \XoopsObject $post
204
-     * @param  bool        $force
205
-     * @return bool
206
-     */
207
-    public function insert(\XoopsObject $post, $force = true) //insert(&$post, $force = true)
208
-    {
209
-        $topicObject = null;
210
-        // Set the post time
211
-        // The time should be "publish" time. To be adjusted later
212
-        if (!$post->getVar('post_time')) {
213
-            $post->setVar('post_time', time());
214
-        }
215
-
216
-        /** @var Newbb\TopicHandler $topicHandler */
217
-        $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic');
218
-        // Verify the topic ID
219
-        if ($topic_id = $post->getVar('topic_id')) {
220
-            $topicObject = $topicHandler->get($topic_id);
221
-            // Invalid topic OR the topic is no approved and the post is not top post
222
-            if (!$topicObject//    || (!$post->isTopic() && $topicObject->getVar("approved") < 1)
223
-            ) {
224
-                return false;
225
-            }
226
-        }
227
-        if (empty($topic_id)) {
228
-            $post->setVar('topic_id', 0);
229
-            $post->setVar('pid', 0);
230
-            $post->setNew();
231
-            $topicObject = $topicHandler->create();
232
-        }
233
-        $textHandler    = Newbb\Helper::getInstance()->getHandler('Text');
234
-        $post_text_vars = ['post_text', 'post_edit', 'dohtml', 'doxcode', 'dosmiley', 'doimage', 'dobr'];
235
-        if ($post->isNew()) {
236
-            if (!$topic_id = $post->getVar('topic_id')) {
237
-                $topicObject->setVar('topic_title', $post->getVar('subject', 'n'));
238
-                $topicObject->setVar('topic_poster', $post->getVar('uid'));
239
-                $topicObject->setVar('forum_id', $post->getVar('forum_id'));
240
-                $topicObject->setVar('topic_time', $post->getVar('post_time'));
241
-                $topicObject->setVar('poster_name', $post->getVar('poster_name'));
242
-                $topicObject->setVar('approved', $post->getVar('approved'));
243
-
244
-                if (!$topic_id = $topicHandler->insert($topicObject, $force)) {
245
-                    $post->deleteAttachment();
246
-                    $post->setErrors('insert topic error');
247
-
248
-                    //xoops_error($topicObject->getErrors());
249
-                    return false;
250
-                }
251
-                $post->setVar('topic_id', $topic_id);
252
-
253
-                $pid = 0;
254
-                $post->setVar('pid', 0);
255
-            } elseif (!$post->getVar('pid')) {
256
-                $pid = $topicHandler->getTopPostId($topic_id);
257
-                $post->setVar('pid', $pid);
258
-            }
259
-
260
-            $textObject = $textHandler->create();
261
-            foreach ($post_text_vars as $key) {
262
-                $textObject->vars[$key] = $post->vars[$key];
263
-            }
264
-            $post->destroyVars($post_text_vars);
265
-
266
-            //            if (!$post_id = parent::insert($post, $force)) {
267
-            //                return false;
268
-            //            }
269
-
270
-            if (!$post_id = parent::insert($post, $force)) {
271
-                return false;
272
-            } else {
273
-                $post->unsetNew();
274
-            }
275
-
276
-            $textObject->setVar('post_id', $post_id);
277
-            if (!$textHandler->insert($textObject, $force)) {
278
-                $this->delete($post);
279
-                $post->setErrors('post text insert error');
280
-
281
-                //xoops_error($textObject->getErrors());
282
-                return false;
283
-            }
284
-            if ($post->getVar('approved') > 0) {
285
-                $this->approve($post, true);
286
-            }
287
-            $post->setVar('post_id', $post_id);
288
-        } else {
289
-            if ($post->isTopic()) {
290
-                if ($post->getVar('subject') !== $topicObject->getVar('topic_title')) {
291
-                    $topicObject->setVar('topic_title', $post->getVar('subject', 'n'));
292
-                }
293
-                if ($post->getVar('approved') !== $topicObject->getVar('approved')) {
294
-                    $topicObject->setVar('approved', $post->getVar('approved'));
295
-                }
296
-                $topicObject->setDirty();
297
-                if (!$result = $topicHandler->insert($topicObject, $force)) {
298
-                    $post->setErrors('update topic error');
299
-
300
-                    //xoops_error($topicObject->getErrors());
301
-                    return false;
302
-                }
303
-            }
304
-            $textObject = $textHandler->get($post->getVar('post_id'));
305
-            $textObject->setDirty();
306
-            foreach ($post_text_vars as $key) {
307
-                $textObject->vars[$key] = $post->vars[$key];
308
-            }
309
-            $post->destroyVars($post_text_vars);
310
-            if (!$post_id = parent::insert($post, $force)) {
311
-                //xoops_error($post->getErrors());
312
-                return false;
313
-            } else {
314
-                $post->unsetNew();
315
-            }
316
-            if (!$textHandler->insert($textObject, $force)) {
317
-                $post->setErrors('update post text error');
318
-
319
-                //xoops_error($textObject->getErrors());
320
-                return false;
321
-            }
322
-        }
323
-
324
-        return $post->getVar('post_id');
325
-    }
326
-
327
-    /**
328
-     * @param \XoopsObject|Post $post
329
-     * @param  bool        $isDeleteOne
330
-     * @param  bool        $force
331
-     * @return bool
332
-     */
333
-    public function delete(\XoopsObject $post, $isDeleteOne = true, $force = false)
334
-    {
335
-        if (!is_object($post) || 0 == $post->getVar('post_id')) {
336
-            return false;
337
-        }
338
-
339
-        if ($isDeleteOne) {
340
-            if ($post->isTopic()) {
341
-                $criteria = new \CriteriaCompo(new \Criteria('topic_id', $post->getVar('topic_id')));
342
-                $criteria->add(new \Criteria('approved', 1));
343
-                $criteria->add(new \Criteria('pid', 0, '>'));
344
-                if ($this->getPostCount($criteria) > 0) {
345
-                    return false;
346
-                }
347
-            }
348
-
349
-            return $this->myDelete($post, $force);
350
-        } else {
351
-            require_once $GLOBALS['xoops']->path('class/xoopstree.php');
352
-            $mytree = new \XoopsTree($this->db->prefix('newbb_posts'), 'post_id', 'pid');
353
-            $arr    = $mytree->getAllChild($post->getVar('post_id'));
354
-            // irmtfan - delete childs in a reverse order
355
-            for ($i = count($arr) - 1; $i >= 0; $i--) {
356
-                $childpost = $this->create(false);
357
-                $childpost->assignVars($arr[$i]);
358
-                $this->myDelete($childpost, $force);
359
-                unset($childpost);
360
-            }
361
-            $this->myDelete($post, $force);
362
-        }
363
-
364
-        return true;
365
-    }
366
-
367
-    /**
368
-     * @param  Post|\XoopsObject $post
369
-     * @param  bool $force
370
-     * @return bool
371
-     */
372
-    public function myDelete(Post $post, $force = false)
373
-    {
374
-        global $xoopsModule;
375
-
376
-        if (!is_object($post) || 0 == $post->getVar('post_id')) {
377
-            return false;
378
-        }
379
-
380
-        /* Set active post as deleted */
381
-        if ($post->getVar('approved') > 0 && empty($force)) {
382
-            $sql = 'UPDATE ' . $this->db->prefix('newbb_posts') . ' SET approved = -1 WHERE post_id = ' . $post->getVar('post_id');
383
-            if (!$result = $this->db->queryF($sql)) {
384
-            }
385
-            /* delete pending post directly */
386
-        } else {
387
-            $sql = sprintf('DELETE FROM `%s` WHERE post_id = %u', $this->db->prefix('newbb_posts'), $post->getVar('post_id'));
388
-            if (!$result = $this->db->queryF($sql)) {
389
-                $post->setErrors('delete post error: ' . $sql);
390
-
391
-                return false;
392
-            }
393
-            $post->deleteAttachment();
394
-
395
-            $sql = sprintf('DELETE FROM `%s` WHERE post_id = %u', $this->db->prefix('newbb_posts_text'), $post->getVar('post_id'));
396
-            if (!$result = $this->db->queryF($sql)) {
397
-                $post->setErrors('Could not remove post text: ' . $sql);
398
-
399
-                return false;
400
-            }
401
-        }
402
-
403
-        if ($post->isTopic()) {
404
-            $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic');
405
-            /** @var Topic $topicObject */
406
-            $topicObject = $topicHandler->get($post->getVar('topic_id'));
407
-            if (is_object($topicObject) && $topicObject->getVar('approved') > 0 && empty($force)) {
408
-                $topiccount_toupdate = 1;
409
-                $topicObject->setVar('approved', -1);
410
-                $topicHandler->insert($topicObject);
411
-                xoops_notification_deletebyitem($xoopsModule->getVar('mid'), 'thread', $post->getVar('topic_id'));
412
-            } else {
413
-                if (is_object($topicObject)) {
414
-                    if ($topicObject->getVar('approved') > 0) {
415
-                        xoops_notification_deletebyitem($xoopsModule->getVar('mid'), 'thread', $post->getVar('topic_id'));
416
-                    }
417
-
418
-                    $poll_id = $topicObject->getVar('poll_id');
419
-                    // START irmtfan poll_module
420
-                    $topicObject->deletePoll($poll_id);
421
-                    // END irmtfan poll_module
422
-                }
423
-
424
-                $sql = sprintf('DELETE FROM `%s` WHERE topic_id = %u', $this->db->prefix('newbb_topics'), $post->getVar('topic_id'));
425
-                if (!$result = $this->db->queryF($sql)) {
426
-                    //xoops_error($this->db->error());
427
-                }
428
-                $sql = sprintf('DELETE FROM `%s` WHERE topic_id = %u', $this->db->prefix('newbb_votedata'), $post->getVar('topic_id'));
429
-                if (!$result = $this->db->queryF($sql)) {
430
-                    //xoops_error($this->db->error());
431
-                }
432
-            }
433
-        } else {
434
-            $sql = 'UPDATE ' . $this->db->prefix('newbb_topics') . ' t
44
+	/**
45
+	 * @param \XoopsDatabase $db
46
+	 */
47
+	public function __construct(\XoopsDatabase $db = null)
48
+	{
49
+		parent::__construct($db, 'newbb_posts', Post::class, 'post_id', 'subject');
50
+	}
51
+
52
+	/**
53
+	 * @param  mixed $id
54
+	 * @param  null  $var
55
+	 * @return null|\XoopsObject
56
+	 */
57
+	public function get($id = null, $var = null) //get($id)
58
+	{
59
+		$id   = (int)$id;
60
+		$post = null;
61
+		$sql  = 'SELECT p.*, t.* FROM ' . $this->db->prefix('newbb_posts') . ' p LEFT JOIN ' . $this->db->prefix('newbb_posts_text') . ' t ON p.post_id=t.post_id WHERE p.post_id=' . $id;
62
+		if ($array = $this->db->fetchArray($this->db->query($sql))) {
63
+			$post = $this->create(false);
64
+			$post->assignVars($array);
65
+		}
66
+
67
+		return $post;
68
+	}
69
+
70
+	/**
71
+	 * @param  int             $limit
72
+	 * @param  int             $start
73
+	 * @param  \CriteriaElement $criteria
74
+	 * @param  null            $fields
75
+	 * @param  bool            $asObject
76
+	 * @param  int             $topic_id
77
+	 * @param  int             $approved
78
+	 * @return array
79
+	 */
80
+	//    public function getByLimit($topic_id, $limit, $approved = 1)
81
+	public function &getByLimit(
82
+		$limit = 0,
83
+		$start = 0,
84
+		\CriteriaElement $criteria = null,
85
+		$fields = null,
86
+		$asObject = true,
87
+		$topic_id = 0,
88
+		$approved = 1
89
+	) {
90
+		$sql    = 'SELECT p.*, t.*, tp.topic_status FROM '
91
+				  . $this->db->prefix('newbb_posts')
92
+				  . ' p LEFT JOIN '
93
+				  . $this->db->prefix('newbb_posts_text')
94
+				  . ' t ON p.post_id=t.post_id LEFT JOIN '
95
+				  . $this->db->prefix('newbb_topics')
96
+				  . ' tp ON tp.topic_id=p.topic_id WHERE p.topic_id='
97
+				  . $topic_id
98
+				  . ' AND p.approved ='
99
+				  . $approved
100
+				  . ' ORDER BY p.post_time DESC';
101
+		$result = $this->db->query($sql, $limit, 0);
102
+		$ret    = [];
103
+		while (false !== ($myrow = $this->db->fetchArray($result))) {
104
+			$post = $this->create(false);
105
+			$post->assignVars($myrow);
106
+
107
+			$ret[$myrow['post_id']] = $post;
108
+			unset($post);
109
+		}
110
+
111
+		return $ret;
112
+	}
113
+
114
+	/**
115
+	 * @param Post $post
116
+	 * @return mixed
117
+	 */
118
+	public function getPostForPDF(&$post)
119
+	{
120
+		return $post->getPostBody(true);
121
+	}
122
+
123
+	/**
124
+	 * @param Post $post
125
+	 * @return mixed
126
+	 */
127
+	public function getPostForPrint(&$post)
128
+	{
129
+		return $post->getPostBody();
130
+	}
131
+
132
+	/**
133
+	 * @param  int|Post|\XoopsObject $post
134
+	 * @param  bool $force
135
+	 * @return bool
136
+	 */
137
+	public function approve(&$post, $force = false)
138
+	{
139
+		if (empty($post)) {
140
+			return false;
141
+		}
142
+		if (is_numeric($post)) {
143
+			$post = $this->get($post);
144
+		}
145
+
146
+		$wasApproved = $post->getVar('approved');
147
+		// irmtfan approve post if the approved = 0 (pending) or -1 (deleted)
148
+		if (empty($force) && $wasApproved > 0) {
149
+			return true;
150
+		}
151
+		$post->setVar('approved', 1);
152
+		$this->insert($post, true);
153
+
154
+		/** @var Newbb\TopicHandler $topicHandler */
155
+		$topicHandler = Newbb\Helper::getInstance()->getHandler('Topic');
156
+		$topicObject  = $topicHandler->get($post->getVar('topic_id'));
157
+		if ($topicObject->getVar('topic_last_post_id') < $post->getVar('post_id')) {
158
+			$topicObject->setVar('topic_last_post_id', $post->getVar('post_id'));
159
+		}
160
+		if ($post->isTopic()) {
161
+			$topicObject->setVar('approved', 1);
162
+		} else {
163
+			$topicObject->setVar('topic_replies', $topicObject->getVar('topic_replies') + 1);
164
+		}
165
+		$topicHandler->insert($topicObject, true);
166
+
167
+		/** @var Newbb\ForumHandler $forumHandler */
168
+		$forumHandler = Newbb\Helper::getInstance()->getHandler('Forum');
169
+		$forumObject  = $forumHandler->get($post->getVar('forum_id'));
170
+		if ($forumObject->getVar('forum_last_post_id') < $post->getVar('post_id')) {
171
+			$forumObject->setVar('forum_last_post_id', $post->getVar('post_id'));
172
+		}
173
+		$forumObject->setVar('forum_posts', $forumObject->getVar('forum_posts') + 1);
174
+		if ($post->isTopic()) {
175
+			$forumObject->setVar('forum_topics', $forumObject->getVar('forum_topics') + 1);
176
+		}
177
+		$forumHandler->insert($forumObject, true);
178
+
179
+		// Update user stats
180
+		if ($post->getVar('uid') > 0) {
181
+			/** @var \XoopsMemberHandler $memberHandler */
182
+			$memberHandler = xoops_getHandler('member');
183
+			$poster        = $memberHandler->getUser($post->getVar('uid'));
184
+			if (is_object($poster) && $post->getVar('uid') == $poster->getVar('uid')) {
185
+				$poster->setVar('posts', $poster->getVar('posts') + 1);
186
+				$res = $memberHandler->insertUser($poster, true);
187
+				unset($poster);
188
+			}
189
+		}
190
+
191
+		// Update forum stats
192
+		/** @var StatsHandler $statsHandler */
193
+		$statsHandler = Newbb\Helper::getInstance()->getHandler('Stats');
194
+		$statsHandler->update($post->getVar('forum_id'), 'post');
195
+		if ($post->isTopic()) {
196
+			$statsHandler->update($post->getVar('forum_id'), 'topic');
197
+		}
198
+
199
+		return true;
200
+	}
201
+
202
+	/**
203
+	 * @param \XoopsObject $post
204
+	 * @param  bool        $force
205
+	 * @return bool
206
+	 */
207
+	public function insert(\XoopsObject $post, $force = true) //insert(&$post, $force = true)
208
+	{
209
+		$topicObject = null;
210
+		// Set the post time
211
+		// The time should be "publish" time. To be adjusted later
212
+		if (!$post->getVar('post_time')) {
213
+			$post->setVar('post_time', time());
214
+		}
215
+
216
+		/** @var Newbb\TopicHandler $topicHandler */
217
+		$topicHandler = Newbb\Helper::getInstance()->getHandler('Topic');
218
+		// Verify the topic ID
219
+		if ($topic_id = $post->getVar('topic_id')) {
220
+			$topicObject = $topicHandler->get($topic_id);
221
+			// Invalid topic OR the topic is no approved and the post is not top post
222
+			if (!$topicObject//    || (!$post->isTopic() && $topicObject->getVar("approved") < 1)
223
+			) {
224
+				return false;
225
+			}
226
+		}
227
+		if (empty($topic_id)) {
228
+			$post->setVar('topic_id', 0);
229
+			$post->setVar('pid', 0);
230
+			$post->setNew();
231
+			$topicObject = $topicHandler->create();
232
+		}
233
+		$textHandler    = Newbb\Helper::getInstance()->getHandler('Text');
234
+		$post_text_vars = ['post_text', 'post_edit', 'dohtml', 'doxcode', 'dosmiley', 'doimage', 'dobr'];
235
+		if ($post->isNew()) {
236
+			if (!$topic_id = $post->getVar('topic_id')) {
237
+				$topicObject->setVar('topic_title', $post->getVar('subject', 'n'));
238
+				$topicObject->setVar('topic_poster', $post->getVar('uid'));
239
+				$topicObject->setVar('forum_id', $post->getVar('forum_id'));
240
+				$topicObject->setVar('topic_time', $post->getVar('post_time'));
241
+				$topicObject->setVar('poster_name', $post->getVar('poster_name'));
242
+				$topicObject->setVar('approved', $post->getVar('approved'));
243
+
244
+				if (!$topic_id = $topicHandler->insert($topicObject, $force)) {
245
+					$post->deleteAttachment();
246
+					$post->setErrors('insert topic error');
247
+
248
+					//xoops_error($topicObject->getErrors());
249
+					return false;
250
+				}
251
+				$post->setVar('topic_id', $topic_id);
252
+
253
+				$pid = 0;
254
+				$post->setVar('pid', 0);
255
+			} elseif (!$post->getVar('pid')) {
256
+				$pid = $topicHandler->getTopPostId($topic_id);
257
+				$post->setVar('pid', $pid);
258
+			}
259
+
260
+			$textObject = $textHandler->create();
261
+			foreach ($post_text_vars as $key) {
262
+				$textObject->vars[$key] = $post->vars[$key];
263
+			}
264
+			$post->destroyVars($post_text_vars);
265
+
266
+			//            if (!$post_id = parent::insert($post, $force)) {
267
+			//                return false;
268
+			//            }
269
+
270
+			if (!$post_id = parent::insert($post, $force)) {
271
+				return false;
272
+			} else {
273
+				$post->unsetNew();
274
+			}
275
+
276
+			$textObject->setVar('post_id', $post_id);
277
+			if (!$textHandler->insert($textObject, $force)) {
278
+				$this->delete($post);
279
+				$post->setErrors('post text insert error');
280
+
281
+				//xoops_error($textObject->getErrors());
282
+				return false;
283
+			}
284
+			if ($post->getVar('approved') > 0) {
285
+				$this->approve($post, true);
286
+			}
287
+			$post->setVar('post_id', $post_id);
288
+		} else {
289
+			if ($post->isTopic()) {
290
+				if ($post->getVar('subject') !== $topicObject->getVar('topic_title')) {
291
+					$topicObject->setVar('topic_title', $post->getVar('subject', 'n'));
292
+				}
293
+				if ($post->getVar('approved') !== $topicObject->getVar('approved')) {
294
+					$topicObject->setVar('approved', $post->getVar('approved'));
295
+				}
296
+				$topicObject->setDirty();
297
+				if (!$result = $topicHandler->insert($topicObject, $force)) {
298
+					$post->setErrors('update topic error');
299
+
300
+					//xoops_error($topicObject->getErrors());
301
+					return false;
302
+				}
303
+			}
304
+			$textObject = $textHandler->get($post->getVar('post_id'));
305
+			$textObject->setDirty();
306
+			foreach ($post_text_vars as $key) {
307
+				$textObject->vars[$key] = $post->vars[$key];
308
+			}
309
+			$post->destroyVars($post_text_vars);
310
+			if (!$post_id = parent::insert($post, $force)) {
311
+				//xoops_error($post->getErrors());
312
+				return false;
313
+			} else {
314
+				$post->unsetNew();
315
+			}
316
+			if (!$textHandler->insert($textObject, $force)) {
317
+				$post->setErrors('update post text error');
318
+
319
+				//xoops_error($textObject->getErrors());
320
+				return false;
321
+			}
322
+		}
323
+
324
+		return $post->getVar('post_id');
325
+	}
326
+
327
+	/**
328
+	 * @param \XoopsObject|Post $post
329
+	 * @param  bool        $isDeleteOne
330
+	 * @param  bool        $force
331
+	 * @return bool
332
+	 */
333
+	public function delete(\XoopsObject $post, $isDeleteOne = true, $force = false)
334
+	{
335
+		if (!is_object($post) || 0 == $post->getVar('post_id')) {
336
+			return false;
337
+		}
338
+
339
+		if ($isDeleteOne) {
340
+			if ($post->isTopic()) {
341
+				$criteria = new \CriteriaCompo(new \Criteria('topic_id', $post->getVar('topic_id')));
342
+				$criteria->add(new \Criteria('approved', 1));
343
+				$criteria->add(new \Criteria('pid', 0, '>'));
344
+				if ($this->getPostCount($criteria) > 0) {
345
+					return false;
346
+				}
347
+			}
348
+
349
+			return $this->myDelete($post, $force);
350
+		} else {
351
+			require_once $GLOBALS['xoops']->path('class/xoopstree.php');
352
+			$mytree = new \XoopsTree($this->db->prefix('newbb_posts'), 'post_id', 'pid');
353
+			$arr    = $mytree->getAllChild($post->getVar('post_id'));
354
+			// irmtfan - delete childs in a reverse order
355
+			for ($i = count($arr) - 1; $i >= 0; $i--) {
356
+				$childpost = $this->create(false);
357
+				$childpost->assignVars($arr[$i]);
358
+				$this->myDelete($childpost, $force);
359
+				unset($childpost);
360
+			}
361
+			$this->myDelete($post, $force);
362
+		}
363
+
364
+		return true;
365
+	}
366
+
367
+	/**
368
+	 * @param  Post|\XoopsObject $post
369
+	 * @param  bool $force
370
+	 * @return bool
371
+	 */
372
+	public function myDelete(Post $post, $force = false)
373
+	{
374
+		global $xoopsModule;
375
+
376
+		if (!is_object($post) || 0 == $post->getVar('post_id')) {
377
+			return false;
378
+		}
379
+
380
+		/* Set active post as deleted */
381
+		if ($post->getVar('approved') > 0 && empty($force)) {
382
+			$sql = 'UPDATE ' . $this->db->prefix('newbb_posts') . ' SET approved = -1 WHERE post_id = ' . $post->getVar('post_id');
383
+			if (!$result = $this->db->queryF($sql)) {
384
+			}
385
+			/* delete pending post directly */
386
+		} else {
387
+			$sql = sprintf('DELETE FROM `%s` WHERE post_id = %u', $this->db->prefix('newbb_posts'), $post->getVar('post_id'));
388
+			if (!$result = $this->db->queryF($sql)) {
389
+				$post->setErrors('delete post error: ' . $sql);
390
+
391
+				return false;
392
+			}
393
+			$post->deleteAttachment();
394
+
395
+			$sql = sprintf('DELETE FROM `%s` WHERE post_id = %u', $this->db->prefix('newbb_posts_text'), $post->getVar('post_id'));
396
+			if (!$result = $this->db->queryF($sql)) {
397
+				$post->setErrors('Could not remove post text: ' . $sql);
398
+
399
+				return false;
400
+			}
401
+		}
402
+
403
+		if ($post->isTopic()) {
404
+			$topicHandler = Newbb\Helper::getInstance()->getHandler('Topic');
405
+			/** @var Topic $topicObject */
406
+			$topicObject = $topicHandler->get($post->getVar('topic_id'));
407
+			if (is_object($topicObject) && $topicObject->getVar('approved') > 0 && empty($force)) {
408
+				$topiccount_toupdate = 1;
409
+				$topicObject->setVar('approved', -1);
410
+				$topicHandler->insert($topicObject);
411
+				xoops_notification_deletebyitem($xoopsModule->getVar('mid'), 'thread', $post->getVar('topic_id'));
412
+			} else {
413
+				if (is_object($topicObject)) {
414
+					if ($topicObject->getVar('approved') > 0) {
415
+						xoops_notification_deletebyitem($xoopsModule->getVar('mid'), 'thread', $post->getVar('topic_id'));
416
+					}
417
+
418
+					$poll_id = $topicObject->getVar('poll_id');
419
+					// START irmtfan poll_module
420
+					$topicObject->deletePoll($poll_id);
421
+					// END irmtfan poll_module
422
+				}
423
+
424
+				$sql = sprintf('DELETE FROM `%s` WHERE topic_id = %u', $this->db->prefix('newbb_topics'), $post->getVar('topic_id'));
425
+				if (!$result = $this->db->queryF($sql)) {
426
+					//xoops_error($this->db->error());
427
+				}
428
+				$sql = sprintf('DELETE FROM `%s` WHERE topic_id = %u', $this->db->prefix('newbb_votedata'), $post->getVar('topic_id'));
429
+				if (!$result = $this->db->queryF($sql)) {
430
+					//xoops_error($this->db->error());
431
+				}
432
+			}
433
+		} else {
434
+			$sql = 'UPDATE ' . $this->db->prefix('newbb_topics') . ' t
435 435
                             LEFT JOIN ' . $this->db->prefix('newbb_posts') . ' p ON p.topic_id = t.topic_id
436 436
                             SET t.topic_last_post_id = p.post_id
437 437
                             WHERE t.topic_last_post_id = ' . $post->getVar('post_id') . '
438 438
                                     AND p.post_id = (SELECT MAX(post_id) FROM ' . $this->db->prefix('newbb_posts') . ' WHERE topic_id=t.topic_id)';
439
-            if (!$result = $this->db->queryF($sql)) {
440
-            }
441
-        }
442
-
443
-        $postcount_toupdate = $post->getVar('approved');
444
-
445
-        if ($postcount_toupdate > 0) {
446
-
447
-            // Update user stats
448
-            if ($post->getVar('uid') > 0) {
449
-                /** @var \XoopsMemberHandler $memberHandler */
450
-                $memberHandler = xoops_getHandler('member');
451
-                $poster        = $memberHandler->getUser($post->getVar('uid'));
452
-                if (is_object($poster) && $post->getVar('uid') == $poster->getVar('uid')) {
453
-                    $poster->setVar('posts', $poster->getVar('posts') - 1);
454
-                    $res = $memberHandler->insertUser($poster, true);
455
-                    unset($poster);
456
-                }
457
-            }
458
-            // irmtfan - just update the pid for approved posts when the post is not topic (pid=0)
459
-            if (!$post->isTopic()) {
460
-                $sql = 'UPDATE ' . $this->db->prefix('newbb_posts') . ' SET pid = ' . $post->getVar('pid') . ' WHERE approved=1 AND pid=' . $post->getVar('post_id');
461
-                if (!$result = $this->db->queryF($sql)) {
462
-                    //xoops_error($this->db->error());
463
-                }
464
-            }
465
-        }
466
-
467
-        return true;
468
-    }
469
-
470
-    // START irmtfan enhance getPostCount when there is join (read_mode = 2)
471
-
472
-    /**
473
-     * @param  null $criteria
474
-     * @param  null $join
475
-     * @return int|null
476
-     */
477
-    public function getPostCount($criteria = null, $join = null)
478
-    {
479
-        // if not join get the count from XOOPS/class/model/stats as before
480
-        if (empty($join)) {
481
-            return parent::getCount($criteria);
482
-        }
483
-
484
-        $sql = 'SELECT COUNT(*) as count' . ' FROM ' . $this->db->prefix('newbb_posts') . ' AS p' . ' LEFT JOIN ' . $this->db->prefix('newbb_posts_text') . ' AS t ON t.post_id = p.post_id';
485
-        // LEFT JOIN
486
-        $sql .= $join;
487
-        // WHERE
488
-        if (null !== $criteria && is_subclass_of($criteria, 'CriteriaElement')) {
489
-            $sql .= ' ' . $criteria->renderWhere();
490
-        }
491
-        if (!$result = $this->db->query($sql)) {
492
-            //xoops_error($this->db->error().'<br>'.$sql);
493
-            return null;
494
-        }
495
-        $myrow = $this->db->fetchArray($result);
496
-        $count = $myrow['count'];
497
-
498
-        return $count;
499
-    }
500
-    // END irmtfan enhance getPostCount when there is join (read_mode = 2)
501
-    /*
439
+			if (!$result = $this->db->queryF($sql)) {
440
+			}
441
+		}
442
+
443
+		$postcount_toupdate = $post->getVar('approved');
444
+
445
+		if ($postcount_toupdate > 0) {
446
+
447
+			// Update user stats
448
+			if ($post->getVar('uid') > 0) {
449
+				/** @var \XoopsMemberHandler $memberHandler */
450
+				$memberHandler = xoops_getHandler('member');
451
+				$poster        = $memberHandler->getUser($post->getVar('uid'));
452
+				if (is_object($poster) && $post->getVar('uid') == $poster->getVar('uid')) {
453
+					$poster->setVar('posts', $poster->getVar('posts') - 1);
454
+					$res = $memberHandler->insertUser($poster, true);
455
+					unset($poster);
456
+				}
457
+			}
458
+			// irmtfan - just update the pid for approved posts when the post is not topic (pid=0)
459
+			if (!$post->isTopic()) {
460
+				$sql = 'UPDATE ' . $this->db->prefix('newbb_posts') . ' SET pid = ' . $post->getVar('pid') . ' WHERE approved=1 AND pid=' . $post->getVar('post_id');
461
+				if (!$result = $this->db->queryF($sql)) {
462
+					//xoops_error($this->db->error());
463
+				}
464
+			}
465
+		}
466
+
467
+		return true;
468
+	}
469
+
470
+	// START irmtfan enhance getPostCount when there is join (read_mode = 2)
471
+
472
+	/**
473
+	 * @param  null $criteria
474
+	 * @param  null $join
475
+	 * @return int|null
476
+	 */
477
+	public function getPostCount($criteria = null, $join = null)
478
+	{
479
+		// if not join get the count from XOOPS/class/model/stats as before
480
+		if (empty($join)) {
481
+			return parent::getCount($criteria);
482
+		}
483
+
484
+		$sql = 'SELECT COUNT(*) as count' . ' FROM ' . $this->db->prefix('newbb_posts') . ' AS p' . ' LEFT JOIN ' . $this->db->prefix('newbb_posts_text') . ' AS t ON t.post_id = p.post_id';
485
+		// LEFT JOIN
486
+		$sql .= $join;
487
+		// WHERE
488
+		if (null !== $criteria && is_subclass_of($criteria, 'CriteriaElement')) {
489
+			$sql .= ' ' . $criteria->renderWhere();
490
+		}
491
+		if (!$result = $this->db->query($sql)) {
492
+			//xoops_error($this->db->error().'<br>'.$sql);
493
+			return null;
494
+		}
495
+		$myrow = $this->db->fetchArray($result);
496
+		$count = $myrow['count'];
497
+
498
+		return $count;
499
+	}
500
+	// END irmtfan enhance getPostCount when there is join (read_mode = 2)
501
+	/*
502 502
      * TODO: combining viewtopic.php
503 503
      */
504
-    /**
505
-     * @param  null $criteria
506
-     * @param  int  $limit
507
-     * @param  int  $start
508
-     * @param  null $join
509
-     * @return array
510
-     */
511
-    public function getPostsByLimit($criteria = null, $limit = 1, $start = 0, $join = null)
512
-    {
513
-        $ret = [];
514
-        $sql = 'SELECT p.*, t.* ' . ' FROM ' . $this->db->prefix('newbb_posts') . ' AS p' . ' LEFT JOIN ' . $this->db->prefix('newbb_posts_text') . ' AS t ON t.post_id = p.post_id';
515
-        if (!empty($join)) {
516
-            $sql .= $join;
517
-        }
518
-        if (null !== $criteria && is_subclass_of($criteria, 'CriteriaElement')) {
519
-            $sql .= ' ' . $criteria->renderWhere();
520
-            if ('' !== $criteria->getSort()) {
521
-                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
522
-            }
523
-        }
524
-        $result = $this->db->query($sql, (int)$limit, (int)$start);
525
-        if (!$result) {
526
-            //xoops_error($this->db->error());
527
-            return $ret;
528
-        }
529
-        while (false !== ($myrow = $this->db->fetchArray($result))) {
530
-            $post = $this->create(false);
531
-            $post->assignVars($myrow);
532
-            $ret[$myrow['post_id']] = $post;
533
-            unset($post);
534
-        }
535
-
536
-        return $ret;
537
-    }
538
-
539
-    /**
540
-     * @return bool
541
-     */
542
-    public function synchronization()
543
-    {
544
-        //$this->cleanOrphan();
545
-        return true;
546
-    }
547
-
548
-    /**
549
-     * clean orphan items from database
550
-     *
551
-     * @param  string $table_link
552
-     * @param  string $field_link
553
-     * @param  string $field_object
554
-     * @return bool   true on success
555
-     */
556
-    public function cleanOrphan($table_link = '', $field_link = '', $field_object = '') //cleanOrphan()
557
-    {
558
-        $this->deleteAll(new \Criteria('post_time', 0), true, true);
559
-        parent::cleanOrphan($this->db->prefix('newbb_topics'), 'topic_id');
560
-        parent::cleanOrphan($this->db->prefix('newbb_posts_text'), 'post_id');
561
-
562
-        $sql = 'DELETE FROM ' . $this->db->prefix('newbb_posts_text') . ' WHERE (post_id NOT IN ( SELECT DISTINCT post_id FROM ' . $this->table . ') )';
563
-        if (!$result = $this->db->queryF($sql)) {
564
-            //xoops_error($this->db->error());
565
-            return false;
566
-        }
567
-
568
-        return true;
569
-    }
570
-
571
-    /**
572
-     * clean expired objects from database
573
-     *
574
-     * @param  int $expire time limit for expiration
575
-     * @return bool true on success
576
-     */
577
-    public function cleanExpires($expire = 0)
578
-    {
579
-        // irmtfan if 0 no cleanup look include/plugin.php
580
-        if (!func_num_args()) {
581
-            $newbbConfig = newbbLoadConfig();
582
-            $expire      = isset($newbbConfig['pending_expire']) ? (int)$newbbConfig['pending_expire'] : 7;
583
-            $expire      = $expire * 24 * 3600; // days to seconds
584
-        }
585
-        if (empty($expire)) {
586
-            return false;
587
-        }
588
-        $crit_expire = new \CriteriaCompo(new \Criteria('approved', 0, '<='));
589
-        //if (!empty($expire)) {
590
-        $crit_expire->add(new \Criteria('post_time', time() - (int)$expire, '<'));
591
-
592
-        //}
593
-        return $this->deleteAll($crit_expire, true/*, true*/);
594
-    }
504
+	/**
505
+	 * @param  null $criteria
506
+	 * @param  int  $limit
507
+	 * @param  int  $start
508
+	 * @param  null $join
509
+	 * @return array
510
+	 */
511
+	public function getPostsByLimit($criteria = null, $limit = 1, $start = 0, $join = null)
512
+	{
513
+		$ret = [];
514
+		$sql = 'SELECT p.*, t.* ' . ' FROM ' . $this->db->prefix('newbb_posts') . ' AS p' . ' LEFT JOIN ' . $this->db->prefix('newbb_posts_text') . ' AS t ON t.post_id = p.post_id';
515
+		if (!empty($join)) {
516
+			$sql .= $join;
517
+		}
518
+		if (null !== $criteria && is_subclass_of($criteria, 'CriteriaElement')) {
519
+			$sql .= ' ' . $criteria->renderWhere();
520
+			if ('' !== $criteria->getSort()) {
521
+				$sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
522
+			}
523
+		}
524
+		$result = $this->db->query($sql, (int)$limit, (int)$start);
525
+		if (!$result) {
526
+			//xoops_error($this->db->error());
527
+			return $ret;
528
+		}
529
+		while (false !== ($myrow = $this->db->fetchArray($result))) {
530
+			$post = $this->create(false);
531
+			$post->assignVars($myrow);
532
+			$ret[$myrow['post_id']] = $post;
533
+			unset($post);
534
+		}
535
+
536
+		return $ret;
537
+	}
538
+
539
+	/**
540
+	 * @return bool
541
+	 */
542
+	public function synchronization()
543
+	{
544
+		//$this->cleanOrphan();
545
+		return true;
546
+	}
547
+
548
+	/**
549
+	 * clean orphan items from database
550
+	 *
551
+	 * @param  string $table_link
552
+	 * @param  string $field_link
553
+	 * @param  string $field_object
554
+	 * @return bool   true on success
555
+	 */
556
+	public function cleanOrphan($table_link = '', $field_link = '', $field_object = '') //cleanOrphan()
557
+	{
558
+		$this->deleteAll(new \Criteria('post_time', 0), true, true);
559
+		parent::cleanOrphan($this->db->prefix('newbb_topics'), 'topic_id');
560
+		parent::cleanOrphan($this->db->prefix('newbb_posts_text'), 'post_id');
561
+
562
+		$sql = 'DELETE FROM ' . $this->db->prefix('newbb_posts_text') . ' WHERE (post_id NOT IN ( SELECT DISTINCT post_id FROM ' . $this->table . ') )';
563
+		if (!$result = $this->db->queryF($sql)) {
564
+			//xoops_error($this->db->error());
565
+			return false;
566
+		}
567
+
568
+		return true;
569
+	}
570
+
571
+	/**
572
+	 * clean expired objects from database
573
+	 *
574
+	 * @param  int $expire time limit for expiration
575
+	 * @return bool true on success
576
+	 */
577
+	public function cleanExpires($expire = 0)
578
+	{
579
+		// irmtfan if 0 no cleanup look include/plugin.php
580
+		if (!func_num_args()) {
581
+			$newbbConfig = newbbLoadConfig();
582
+			$expire      = isset($newbbConfig['pending_expire']) ? (int)$newbbConfig['pending_expire'] : 7;
583
+			$expire      = $expire * 24 * 3600; // days to seconds
584
+		}
585
+		if (empty($expire)) {
586
+			return false;
587
+		}
588
+		$crit_expire = new \CriteriaCompo(new \Criteria('approved', 0, '<='));
589
+		//if (!empty($expire)) {
590
+		$crit_expire->add(new \Criteria('post_time', time() - (int)$expire, '<'));
591
+
592
+		//}
593
+		return $this->deleteAll($crit_expire, true/*, true*/);
594
+	}
595 595
 }
Please login to merge, or discard this patch.
Spacing   +16 added lines, -16 removed lines patch added patch discarded remove patch
@@ -58,7 +58,7 @@  discard block
 block discarded – undo
58 58
     {
59 59
         $id   = (int)$id;
60 60
         $post = null;
61
-        $sql  = 'SELECT p.*, t.* FROM ' . $this->db->prefix('newbb_posts') . ' p LEFT JOIN ' . $this->db->prefix('newbb_posts_text') . ' t ON p.post_id=t.post_id WHERE p.post_id=' . $id;
61
+        $sql  = 'SELECT p.*, t.* FROM '.$this->db->prefix('newbb_posts').' p LEFT JOIN '.$this->db->prefix('newbb_posts_text').' t ON p.post_id=t.post_id WHERE p.post_id='.$id;
62 62
         if ($array = $this->db->fetchArray($this->db->query($sql))) {
63 63
             $post = $this->create(false);
64 64
             $post->assignVars($array);
@@ -87,7 +87,7 @@  discard block
 block discarded – undo
87 87
         $topic_id = 0,
88 88
         $approved = 1
89 89
     ) {
90
-        $sql    = 'SELECT p.*, t.*, tp.topic_status FROM '
90
+        $sql = 'SELECT p.*, t.*, tp.topic_status FROM '
91 91
                   . $this->db->prefix('newbb_posts')
92 92
                   . ' p LEFT JOIN '
93 93
                   . $this->db->prefix('newbb_posts_text')
@@ -379,14 +379,14 @@  discard block
 block discarded – undo
379 379
 
380 380
         /* Set active post as deleted */
381 381
         if ($post->getVar('approved') > 0 && empty($force)) {
382
-            $sql = 'UPDATE ' . $this->db->prefix('newbb_posts') . ' SET approved = -1 WHERE post_id = ' . $post->getVar('post_id');
382
+            $sql = 'UPDATE '.$this->db->prefix('newbb_posts').' SET approved = -1 WHERE post_id = '.$post->getVar('post_id');
383 383
             if (!$result = $this->db->queryF($sql)) {
384 384
             }
385 385
             /* delete pending post directly */
386 386
         } else {
387 387
             $sql = sprintf('DELETE FROM `%s` WHERE post_id = %u', $this->db->prefix('newbb_posts'), $post->getVar('post_id'));
388 388
             if (!$result = $this->db->queryF($sql)) {
389
-                $post->setErrors('delete post error: ' . $sql);
389
+                $post->setErrors('delete post error: '.$sql);
390 390
 
391 391
                 return false;
392 392
             }
@@ -394,7 +394,7 @@  discard block
 block discarded – undo
394 394
 
395 395
             $sql = sprintf('DELETE FROM `%s` WHERE post_id = %u', $this->db->prefix('newbb_posts_text'), $post->getVar('post_id'));
396 396
             if (!$result = $this->db->queryF($sql)) {
397
-                $post->setErrors('Could not remove post text: ' . $sql);
397
+                $post->setErrors('Could not remove post text: '.$sql);
398 398
 
399 399
                 return false;
400 400
             }
@@ -431,11 +431,11 @@  discard block
 block discarded – undo
431 431
                 }
432 432
             }
433 433
         } else {
434
-            $sql = 'UPDATE ' . $this->db->prefix('newbb_topics') . ' t
435
-                            LEFT JOIN ' . $this->db->prefix('newbb_posts') . ' p ON p.topic_id = t.topic_id
434
+            $sql = 'UPDATE '.$this->db->prefix('newbb_topics').' t
435
+                            LEFT JOIN ' . $this->db->prefix('newbb_posts').' p ON p.topic_id = t.topic_id
436 436
                             SET t.topic_last_post_id = p.post_id
437
-                            WHERE t.topic_last_post_id = ' . $post->getVar('post_id') . '
438
-                                    AND p.post_id = (SELECT MAX(post_id) FROM ' . $this->db->prefix('newbb_posts') . ' WHERE topic_id=t.topic_id)';
437
+                            WHERE t.topic_last_post_id = ' . $post->getVar('post_id').'
438
+                                    AND p.post_id = (SELECT MAX(post_id) FROM ' . $this->db->prefix('newbb_posts').' WHERE topic_id=t.topic_id)';
439 439
             if (!$result = $this->db->queryF($sql)) {
440 440
             }
441 441
         }
@@ -457,7 +457,7 @@  discard block
 block discarded – undo
457 457
             }
458 458
             // irmtfan - just update the pid for approved posts when the post is not topic (pid=0)
459 459
             if (!$post->isTopic()) {
460
-                $sql = 'UPDATE ' . $this->db->prefix('newbb_posts') . ' SET pid = ' . $post->getVar('pid') . ' WHERE approved=1 AND pid=' . $post->getVar('post_id');
460
+                $sql = 'UPDATE '.$this->db->prefix('newbb_posts').' SET pid = '.$post->getVar('pid').' WHERE approved=1 AND pid='.$post->getVar('post_id');
461 461
                 if (!$result = $this->db->queryF($sql)) {
462 462
                     //xoops_error($this->db->error());
463 463
                 }
@@ -481,12 +481,12 @@  discard block
 block discarded – undo
481 481
             return parent::getCount($criteria);
482 482
         }
483 483
 
484
-        $sql = 'SELECT COUNT(*) as count' . ' FROM ' . $this->db->prefix('newbb_posts') . ' AS p' . ' LEFT JOIN ' . $this->db->prefix('newbb_posts_text') . ' AS t ON t.post_id = p.post_id';
484
+        $sql = 'SELECT COUNT(*) as count'.' FROM '.$this->db->prefix('newbb_posts').' AS p'.' LEFT JOIN '.$this->db->prefix('newbb_posts_text').' AS t ON t.post_id = p.post_id';
485 485
         // LEFT JOIN
486 486
         $sql .= $join;
487 487
         // WHERE
488 488
         if (null !== $criteria && is_subclass_of($criteria, 'CriteriaElement')) {
489
-            $sql .= ' ' . $criteria->renderWhere();
489
+            $sql .= ' '.$criteria->renderWhere();
490 490
         }
491 491
         if (!$result = $this->db->query($sql)) {
492 492
             //xoops_error($this->db->error().'<br>'.$sql);
@@ -511,14 +511,14 @@  discard block
 block discarded – undo
511 511
     public function getPostsByLimit($criteria = null, $limit = 1, $start = 0, $join = null)
512 512
     {
513 513
         $ret = [];
514
-        $sql = 'SELECT p.*, t.* ' . ' FROM ' . $this->db->prefix('newbb_posts') . ' AS p' . ' LEFT JOIN ' . $this->db->prefix('newbb_posts_text') . ' AS t ON t.post_id = p.post_id';
514
+        $sql = 'SELECT p.*, t.* '.' FROM '.$this->db->prefix('newbb_posts').' AS p'.' LEFT JOIN '.$this->db->prefix('newbb_posts_text').' AS t ON t.post_id = p.post_id';
515 515
         if (!empty($join)) {
516 516
             $sql .= $join;
517 517
         }
518 518
         if (null !== $criteria && is_subclass_of($criteria, 'CriteriaElement')) {
519
-            $sql .= ' ' . $criteria->renderWhere();
519
+            $sql .= ' '.$criteria->renderWhere();
520 520
             if ('' !== $criteria->getSort()) {
521
-                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
521
+                $sql .= ' ORDER BY '.$criteria->getSort().' '.$criteria->getOrder();
522 522
             }
523 523
         }
524 524
         $result = $this->db->query($sql, (int)$limit, (int)$start);
@@ -559,7 +559,7 @@  discard block
 block discarded – undo
559 559
         parent::cleanOrphan($this->db->prefix('newbb_topics'), 'topic_id');
560 560
         parent::cleanOrphan($this->db->prefix('newbb_posts_text'), 'post_id');
561 561
 
562
-        $sql = 'DELETE FROM ' . $this->db->prefix('newbb_posts_text') . ' WHERE (post_id NOT IN ( SELECT DISTINCT post_id FROM ' . $this->table . ') )';
562
+        $sql = 'DELETE FROM '.$this->db->prefix('newbb_posts_text').' WHERE (post_id NOT IN ( SELECT DISTINCT post_id FROM '.$this->table.') )';
563 563
         if (!$result = $this->db->queryF($sql)) {
564 564
             //xoops_error($this->db->error());
565 565
             return false;
Please login to merge, or discard this patch.
class/CategoryHandler.php 1 patch
Indentation   +132 added lines, -132 removed lines patch added patch discarded remove patch
@@ -19,136 +19,136 @@
 block discarded – undo
19 19
  */
20 20
 class CategoryHandler extends \XoopsPersistableObjectHandler
21 21
 {
22
-    /**
23
-     * @param null|\XoopsDatabase $db
24
-     */
25
-    public function __construct(\XoopsDatabase $db = null)
26
-    {
27
-        parent::__construct($db, 'newbb_categories', Category::class, 'cat_id', 'cat_title');
28
-    }
29
-
30
-    /**
31
-     * @param  string $perm
32
-     * @return mixed
33
-     */
34
-    public function getIdsByPermission($perm = 'access')
35
-    {
36
-        /** var Newbb\PermissionHandler $permHandler */
37
-        $permHandler = Newbb\Helper::getInstance()->getHandler('Permission');
38
-        return $permHandler->getCategories($perm);
39
-    }
40
-
41
-    /**
42
-     * @param  string $permission
43
-     * @param  null   $tags
44
-     * @param  bool   $asObject
45
-     * @return array
46
-     */
47
-    public function &getByPermission($permission = 'access', $tags = null, $asObject = true)
48
-    {
49
-        $categories = [];
50
-        if (!$valid_ids = $this->getIdsByPermission($permission)) {
51
-            return $categories;
52
-        }
53
-        $criteria = new \Criteria('cat_id', '(' . implode(', ', $valid_ids) . ')', 'IN');
54
-        $criteria->setSort('cat_order');
55
-        $categories = $this->getAll($criteria, $tags, $asObject);
56
-
57
-        return $categories;
58
-    }
59
-
60
-    /**
61
-     * @param \XoopsObject $category
62
-     * @param  bool        $force
63
-     * @return mixed
64
-     */
65
-    public function insert(\XoopsObject $category, $force = true)
66
-    {
67
-        $className = Category::class;
68
-        if (!($category instanceof $className)) {
69
-            return false;
70
-        }
71
-        parent::insert($category, $force);
72
-        if ($category->isNew()) {
73
-            $this->applyPermissionTemplate($category);
74
-        }
75
-
76
-        return $category->getVar('cat_id');
77
-    }
78
-
79
-    /**
80
-     * @param \XoopsObject $category
81
-     * @param  bool        $force
82
-     * @return bool|mixed
83
-     * @internal param Category $category
84
-     */
85
-    public function delete(\XoopsObject $category, $force = false)//delete(Category $category)
86
-    {
87
-        $className = Category::class;
88
-        if (!($category instanceof $className)) {
89
-            return false;
90
-        }
91
-        /** @var Newbb\ForumHandler $forumHandler */
92
-        $forumHandler = Newbb\Helper::getInstance()->getHandler('Forum');
93
-        $forumHandler->deleteAll(new \Criteria('cat_id', $category->getVar('cat_id')), true, true);
94
-        if ($result = parent::delete($category)) {
95
-            // Delete group permissions
96
-            return $this->deletePermission($category);
97
-        } else {
98
-            $category->setErrors('delete category error');
99
-
100
-            return false;
101
-        }
102
-    }
103
-
104
-    /**
105
-     * Check permission for a category
106
-     *
107
-     * @param  Category|int $category object or id
108
-     * @param  string       $perm     permission name
109
-     *
110
-     * @return bool
111
-     */
112
-    public function getPermission($category, $perm = 'access')
113
-    {
114
-        if ($GLOBALS['xoopsUserIsAdmin'] && 'newbb' === $GLOBALS['xoopsModule']->getVar('dirname')) {
115
-            return true;
116
-        }
117
-
118
-        $cat_id = is_object($category) ? $category->getVar('cat_id') : (int)$category;
119
-        /** @var PermissionHandler $permHandler */
120
-        $permHandler = Newbb\Helper::getInstance()->getHandler('Permission');
121
-        return $permHandler->getPermission('category', $perm, $cat_id);
122
-    }
123
-
124
-    /**
125
-     * @param Category $category
126
-     * @return mixed
127
-     */
128
-    public function deletePermission(Category $category)
129
-    {
130
-        /** @var PermissionHandler $permHandler */
131
-        $permHandler = Newbb\Helper::getInstance()->getHandler('Permission');
132
-        return $permHandler->deleteByCategory($category->getVar('cat_id'));
133
-    }
134
-
135
-    /**
136
-     * @param Category $category
137
-     * @return mixed
138
-     */
139
-    public function applyPermissionTemplate(Category $category)
140
-    {
141
-        /** @var PermissionHandler $permHandler */
142
-        $permHandler = Newbb\Helper::getInstance()->getHandler('Permission');
143
-        return $permHandler->setCategoryPermission($category->getVar('cat_id'));
144
-    }
145
-
146
-    /**
147
-     * @param  mixed $object
148
-     * @return bool
149
-     */
150
-    public function synchronization($object = null)
151
-    {
152
-        return true;
153
-    }
22
+	/**
23
+	 * @param null|\XoopsDatabase $db
24
+	 */
25
+	public function __construct(\XoopsDatabase $db = null)
26
+	{
27
+		parent::__construct($db, 'newbb_categories', Category::class, 'cat_id', 'cat_title');
28
+	}
29
+
30
+	/**
31
+	 * @param  string $perm
32
+	 * @return mixed
33
+	 */
34
+	public function getIdsByPermission($perm = 'access')
35
+	{
36
+		/** var Newbb\PermissionHandler $permHandler */
37
+		$permHandler = Newbb\Helper::getInstance()->getHandler('Permission');
38
+		return $permHandler->getCategories($perm);
39
+	}
40
+
41
+	/**
42
+	 * @param  string $permission
43
+	 * @param  null   $tags
44
+	 * @param  bool   $asObject
45
+	 * @return array
46
+	 */
47
+	public function &getByPermission($permission = 'access', $tags = null, $asObject = true)
48
+	{
49
+		$categories = [];
50
+		if (!$valid_ids = $this->getIdsByPermission($permission)) {
51
+			return $categories;
52
+		}
53
+		$criteria = new \Criteria('cat_id', '(' . implode(', ', $valid_ids) . ')', 'IN');
54
+		$criteria->setSort('cat_order');
55
+		$categories = $this->getAll($criteria, $tags, $asObject);
56
+
57
+		return $categories;
58
+	}
59
+
60
+	/**
61
+	 * @param \XoopsObject $category
62
+	 * @param  bool        $force
63
+	 * @return mixed
64
+	 */
65
+	public function insert(\XoopsObject $category, $force = true)
66
+	{
67
+		$className = Category::class;
68
+		if (!($category instanceof $className)) {
69
+			return false;
70
+		}
71
+		parent::insert($category, $force);
72
+		if ($category->isNew()) {
73
+			$this->applyPermissionTemplate($category);
74
+		}
75
+
76
+		return $category->getVar('cat_id');
77
+	}
78
+
79
+	/**
80
+	 * @param \XoopsObject $category
81
+	 * @param  bool        $force
82
+	 * @return bool|mixed
83
+	 * @internal param Category $category
84
+	 */
85
+	public function delete(\XoopsObject $category, $force = false)//delete(Category $category)
86
+	{
87
+		$className = Category::class;
88
+		if (!($category instanceof $className)) {
89
+			return false;
90
+		}
91
+		/** @var Newbb\ForumHandler $forumHandler */
92
+		$forumHandler = Newbb\Helper::getInstance()->getHandler('Forum');
93
+		$forumHandler->deleteAll(new \Criteria('cat_id', $category->getVar('cat_id')), true, true);
94
+		if ($result = parent::delete($category)) {
95
+			// Delete group permissions
96
+			return $this->deletePermission($category);
97
+		} else {
98
+			$category->setErrors('delete category error');
99
+
100
+			return false;
101
+		}
102
+	}
103
+
104
+	/**
105
+	 * Check permission for a category
106
+	 *
107
+	 * @param  Category|int $category object or id
108
+	 * @param  string       $perm     permission name
109
+	 *
110
+	 * @return bool
111
+	 */
112
+	public function getPermission($category, $perm = 'access')
113
+	{
114
+		if ($GLOBALS['xoopsUserIsAdmin'] && 'newbb' === $GLOBALS['xoopsModule']->getVar('dirname')) {
115
+			return true;
116
+		}
117
+
118
+		$cat_id = is_object($category) ? $category->getVar('cat_id') : (int)$category;
119
+		/** @var PermissionHandler $permHandler */
120
+		$permHandler = Newbb\Helper::getInstance()->getHandler('Permission');
121
+		return $permHandler->getPermission('category', $perm, $cat_id);
122
+	}
123
+
124
+	/**
125
+	 * @param Category $category
126
+	 * @return mixed
127
+	 */
128
+	public function deletePermission(Category $category)
129
+	{
130
+		/** @var PermissionHandler $permHandler */
131
+		$permHandler = Newbb\Helper::getInstance()->getHandler('Permission');
132
+		return $permHandler->deleteByCategory($category->getVar('cat_id'));
133
+	}
134
+
135
+	/**
136
+	 * @param Category $category
137
+	 * @return mixed
138
+	 */
139
+	public function applyPermissionTemplate(Category $category)
140
+	{
141
+		/** @var PermissionHandler $permHandler */
142
+		$permHandler = Newbb\Helper::getInstance()->getHandler('Permission');
143
+		return $permHandler->setCategoryPermission($category->getVar('cat_id'));
144
+	}
145
+
146
+	/**
147
+	 * @param  mixed $object
148
+	 * @return bool
149
+	 */
150
+	public function synchronization($object = null)
151
+	{
152
+		return true;
153
+	}
154 154
 }
Please login to merge, or discard this patch.
class/OnlineHandler.php 1 patch
Indentation   +352 added lines, -352 removed lines patch added patch discarded remove patch
@@ -22,356 +22,356 @@
 block discarded – undo
22 22
  */
23 23
 class OnlineHandler
24 24
 {
25
-    public $db;
26
-    public $forum_id;
27
-    public $forumObject;
28
-    public $topic_id;
29
-    public $user_ids = [];
30
-
31
-    /**
32
-     * OnlineHandler constructor.
33
-     * @param \XoopsDatabase $db
34
-     */
35
-    public function __construct(\XoopsDatabase $db = null)
36
-    {
37
-        $this->db = $db;
38
-    }
39
-
40
-    /**
41
-     * @param null|Newbb\Forum $forum
42
-     * @param null|Topic  $forumtopic
43
-     */
44
-    public function init($forum = null, $forumtopic = null)
45
-    {
46
-        if (is_object($forum)) {
47
-            $this->forum_id    = $forum->getVar('forum_id');
48
-            $this->forumObject = $forum;
49
-        } else {
50
-            $this->forum_id    = (int)$forum;
51
-            $this->forumObject = $forum;
52
-        }
53
-        if (is_object($forumtopic)) {
54
-            $this->topic_id = $forumtopic->getVar('topic_id');
55
-            if (empty($this->forum_id)) {
56
-                $this->forum_id = $forumtopic->getVar('forum_id');
57
-            }
58
-        } else {
59
-            $this->topic_id = (int)$forumtopic;
60
-        }
61
-
62
-        $this->update();
63
-    }
64
-
65
-    public function update()
66
-    {
67
-        global $xoopsModule;
68
-
69
-        mt_srand((double)microtime() * 1000000);
70
-        // set gc probabillity to 10% for now..
71
-        if (mt_rand(1, 100) < 60) {
72
-            $this->gc(150);
73
-        }
74
-        if (is_object($GLOBALS['xoopsUser'])) {
75
-            $uid   = $GLOBALS['xoopsUser']->getVar('uid');
76
-            $uname = $GLOBALS['xoopsUser']->getVar('uname');
77
-            $name  = $GLOBALS['xoopsUser']->getVar('name');
78
-        } else {
79
-            $uid   = 0;
80
-            $uname = '';
81
-            $name  = '';
82
-        }
83
-
84
-        $xoops_onlineHandler = xoops_getHandler('online');
85
-        $xoopsupdate         = $xoops_onlineHandler->write($uid, $uname, time(), $xoopsModule->getVar('mid'), \Xmf\IPAddress::fromRequest()->asReadable());
86
-        if (!$xoopsupdate) {
87
-            //xoops_error("newbb online upate error");
88
-        }
89
-
90
-        $uname = (empty($GLOBALS['xoopsModuleConfig']['show_realname']) || empty($name)) ? $uname : $name;
91
-        $this->write($uid, $uname, time(), $this->forum_id, IPAddress::fromRequest()->asReadable(), $this->topic_id);
92
-    }
93
-
94
-    /**
95
-     * @param $xoopsTpl
96
-     */
97
-    public function render(\Smarty $xoopsTpl)
98
-    {
99
-        require_once  dirname(__DIR__) . '/include/functions.render.php';
100
-        require_once  dirname(__DIR__) . '/include/functions.user.php';
101
-        $criteria = null;
102
-        if ($this->topic_id) {
103
-            $criteria = new \Criteria('online_topic', $this->topic_id);
104
-        } elseif ($this->forum_id) {
105
-            $criteria = new \Criteria('online_forum', $this->forum_id);
106
-        }
107
-        $users     = $this->getAll($criteria);
108
-        $num_total = count($users);
109
-
110
-        $num_user     = 0;
111
-        $users_id     = [];
112
-        $users_online = [];
113
-        for ($i = 0; $i < $num_total; ++$i) {
114
-            if (empty($users[$i]['online_uid'])) {
115
-                continue;
116
-            }
117
-            $users_id[]                             = $users[$i]['online_uid'];
118
-            $users_online[$users[$i]['online_uid']] = [
119
-                'link'  => XOOPS_URL . '/userinfo.php?uid=' . $users[$i]['online_uid'],
120
-                'uname' => $users[$i]['online_uname']
121
-            ];
122
-            ++$num_user;
123
-        }
124
-        $num_anonymous           = $num_total - $num_user;
125
-        $online                  = [];
126
-        $online['image']         = newbbDisplayImage('whosonline');
127
-        $online['num_total']     = $num_total;
128
-        $online['num_user']      = $num_user;
129
-        $online['num_anonymous'] = $num_anonymous;
130
-        $administrator_list      = newbbIsModuleAdministrators($users_id);
131
-        $moderator_list          = [];
132
-        if ($member_list = array_diff(array_keys($administrator_list), $users_id)) {
133
-            if (is_object($this->forumObject)) {
134
-                $moderator_list = $this->forumObject->getVar('forum_moderator');
135
-            } else {
136
-                $moderator_list = newbbIsForumModerators($member_list);
137
-            }
138
-        }
139
-        foreach ($users_online as $uid => $user) {
140
-            if (!empty($administrator_list[$uid])) {
141
-                $user['level'] = 2;
142
-            } elseif (!empty($moderator_list[$uid])) {
143
-                $user['level'] = 1;
144
-            } else {
145
-                $user['level'] = 0;
146
-            }
147
-            $online['users'][] = $user;
148
-        }
149
-
150
-        $xoopsTpl->assign_by_ref('online', $online);
151
-    }
152
-
153
-    /**
154
-     * Deprecated
155
-     */
156
-    public function showOnline()
157
-    {
158
-        require_once  dirname(__DIR__) . '/include/functions.render.php';
159
-        require_once  dirname(__DIR__) . '/include/functions.user.php';
160
-        $criteria = null;
161
-        if ($this->topic_id) {
162
-            $criteria = new \Criteria('online_topic', $this->topic_id);
163
-        } elseif ($this->forum_id) {
164
-            $criteria = new \Criteria('online_forum', $this->forum_id);
165
-        }
166
-        $users     = $this->getAll($criteria);
167
-        $num_total = count($users);
168
-
169
-        $num_user     = 0;
170
-        $users_id     = [];
171
-        $users_online = [];
172
-        for ($i = 0; $i < $num_total; ++$i) {
173
-            if (empty($users[$i]['online_uid'])) {
174
-                continue;
175
-            }
176
-            $users_id[]                             = $users[$i]['online_uid'];
177
-            $users_online[$users[$i]['online_uid']] = [
178
-                'link'  => XOOPS_URL . '/userinfo.php?uid=' . $users[$i]['online_uid'],
179
-                'uname' => $users[$i]['online_uname']
180
-            ];
181
-            ++$num_user;
182
-        }
183
-        $num_anonymous           = $num_total - $num_user;
184
-        $online                  = [];
185
-        $online['image']         = newbbDisplayImage('whosonline');
186
-        $online['statistik']     = newbbDisplayImage('statistik');
187
-        $online['num_total']     = $num_total;
188
-        $online['num_user']      = $num_user;
189
-        $online['num_anonymous'] = $num_anonymous;
190
-        $administrator_list      = newbbIsModuleAdministrators($users_id);
191
-        $moderator_list          = [];
192
-        if ($member_list = array_diff($users_id, array_keys($administrator_list))) {
193
-            if (is_object($this->forumObject)) {
194
-                $moderator_list = $this->forumObject->getVar('forum_moderator');
195
-            } else {
196
-                $moderator_list = newbbIsForumModerators($member_list);
197
-            }
198
-        }
199
-
200
-        foreach ($users_online as $uid => $user) {
201
-            if (in_array($uid, $administrator_list)) {
202
-                $user['level'] = 2;
203
-            } elseif (in_array($uid, $moderator_list)) {
204
-                $user['level'] = 1;
205
-            } else {
206
-                $user['level'] = 0;
207
-            }
208
-            $online['users'][] = $user;
209
-        }
210
-
211
-        return $online;
212
-    }
213
-
214
-    /**
215
-     * Write online information to the database
216
-     *
217
-     * @param  int    $uid      UID of the active user
218
-     * @param  string $uname    Username
219
-     * @param         $time
220
-     * @param  string $forum_id Current forum_id
221
-     * @param  string $ip       User's IP adress
222
-     * @param         $topic_id
223
-     * @return bool   TRUE on success
224
-     * @internal param string $timestamp
225
-     */
226
-    public function write($uid, $uname, $time, $forum_id, $ip, $topic_id)
227
-    {
228
-        global $xoopsModule, $xoopsDB;
229
-
230
-        $uid = (int)$uid;
231
-        if ($uid > 0) {
232
-            $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('newbb_online') . ' WHERE online_uid=' . $uid;
233
-        } else {
234
-            $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('newbb_online') . ' WHERE online_uid=' . $uid . " AND online_ip='" . $ip . "'";
235
-        }
236
-        list($count) = $this->db->fetchRow($this->db->queryF($sql));
237
-        if ($count > 0) {
238
-            $sql = 'UPDATE ' . $this->db->prefix('newbb_online') . " SET online_updated= '" . $time . "', online_forum = '" . $forum_id . "', online_topic = '" . $topic_id . "' WHERE online_uid = " . $uid;
239
-            if (0 == $uid) {
240
-                $sql .= " AND online_ip='" . $ip . "'";
241
-            }
242
-        } else {
243
-            $sql = sprintf('INSERT INTO `%s` (online_uid, online_uname, online_updated, online_ip, online_forum, online_topic) VALUES (%u, %s, %u, %s, %u, %u)', $this->db->prefix('newbb_online'), $uid, $this->db->quote($uname), $time, $this->db->quote($ip), $forum_id, $topic_id);
244
-        }
245
-        if (!$this->db->queryF($sql)) {
246
-            //xoops_error($this->db->error());
247
-            return false;
248
-        }
249
-
250
-        /** @var \XoopsOnlineHandler $xoops_onlineHandler */
251
-        $xoops_onlineHandler = xoops_getHandler('online');
252
-        $xoopsOnlineTable    = $xoops_onlineHandler->table;
253
-
254
-        $sql = 'DELETE FROM '
255
-               . $this->db->prefix('newbb_online')
256
-               . ' WHERE'
257
-               . ' ( online_uid > 0 AND online_uid NOT IN ( SELECT online_uid FROM '
258
-               . $xoopsOnlineTable
259
-               . ' WHERE online_module ='
260
-               . $xoopsModule->getVar('mid')
261
-               . ' ) )'
262
-               . ' OR ( online_uid = 0 AND online_ip NOT IN ( SELECT online_ip FROM '
263
-               . $xoopsOnlineTable
264
-               . ' WHERE online_module ='
265
-               . $xoopsModule->getVar('mid')
266
-               . ' AND online_uid = 0 ) )';
267
-
268
-        if ($result = $this->db->queryF($sql)) {
269
-            return true;
270
-        } else {
271
-            //xoops_error($this->db->error());
272
-            return false;
273
-        }
274
-    }
275
-
276
-    /**
277
-     * Garbage Collection
278
-     *
279
-     * Delete all online information that has not been updated for a certain time
280
-     *
281
-     * @param int $expire Expiration time in seconds
282
-     */
283
-    public function gc($expire)
284
-    {
285
-        global $xoopsModule;
286
-        $sql = 'DELETE FROM ' . $this->db->prefix('newbb_online') . ' WHERE online_updated < ' . (time() - (int)$expire);
287
-        $this->db->queryF($sql);
288
-
289
-        $xoops_onlineHandler = xoops_getHandler('online');
290
-        $xoops_onlineHandler->gc($expire);
291
-    }
292
-
293
-    /**
294
-     * Get an array of online information
295
-     *
296
-     * @param  \CriteriaElement $criteria {@link \CriteriaElement}
297
-     * @return array           Array of associative arrays of online information
298
-     */
299
-    public function getAll(\CriteriaElement $criteria = null)
300
-    {
301
-        $ret   = [];
302
-        $limit = $start = 0;
303
-        $sql   = 'SELECT * FROM ' . $this->db->prefix('newbb_online');
304
-        if (is_object($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
305
-            $sql   .= ' ' . $criteria->renderWhere();
306
-            $limit = $criteria->getLimit();
307
-            $start = $criteria->getStart();
308
-        }
309
-        $result = $this->db->query($sql, $limit, $start);
310
-        if (!$result) {
311
-            return $ret;
312
-        }
313
-        while (false !== ($myrow = $this->db->fetchArray($result))) {
314
-            $ret[] = $myrow;
315
-            if ($myrow['online_uid'] > 0) {
316
-                $this->user_ids[] = $myrow['online_uid'];
317
-            }
318
-            unset($myrow);
319
-        }
320
-        $this->user_ids = array_unique($this->user_ids);
321
-
322
-        return $ret;
323
-    }
324
-
325
-    /**
326
-     * @param $uids
327
-     * @return array
328
-     */
329
-    public function checkStatus($uids)
330
-    {
331
-        $online_users = [];
332
-        $ret          = [];
333
-        if (!empty($this->user_ids)) {
334
-            $online_users = $this->user_ids;
335
-        } else {
336
-            $sql = 'SELECT online_uid FROM ' . $this->db->prefix('newbb_online');
337
-            if (!empty($uids)) {
338
-                $sql .= ' WHERE online_uid IN (' . implode(', ', array_map('intval', $uids)) . ')';
339
-            }
340
-
341
-            $result = $this->db->query($sql);
342
-            if (!$result) {
343
-                return $ret;
344
-            }
345
-            while (false !== (list($uid) = $this->db->fetchRow($result))) {
346
-                $online_users[] = $uid;
347
-            }
348
-        }
349
-        foreach ($uids as $uid) {
350
-            if (in_array($uid, $online_users)) {
351
-                $ret[$uid] = 1;
352
-            }
353
-        }
354
-
355
-        return $ret;
356
-    }
357
-
358
-    /**
359
-     * Count the number of online users
360
-     *
361
-     * @param  \CriteriaElement $criteria {@link CriteriaElement}
362
-     * @return bool
363
-     */
364
-    public function getCount(\CriteriaElement $criteria = null)
365
-    {
366
-        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('newbb_online');
367
-        if (is_object($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
368
-            $sql .= ' ' . $criteria->renderWhere();
369
-        }
370
-        if (!$result = $this->db->query($sql)) {
371
-            return false;
372
-        }
373
-        list($ret) = $this->db->fetchRow($result);
374
-
375
-        return $ret;
376
-    }
25
+	public $db;
26
+	public $forum_id;
27
+	public $forumObject;
28
+	public $topic_id;
29
+	public $user_ids = [];
30
+
31
+	/**
32
+	 * OnlineHandler constructor.
33
+	 * @param \XoopsDatabase $db
34
+	 */
35
+	public function __construct(\XoopsDatabase $db = null)
36
+	{
37
+		$this->db = $db;
38
+	}
39
+
40
+	/**
41
+	 * @param null|Newbb\Forum $forum
42
+	 * @param null|Topic  $forumtopic
43
+	 */
44
+	public function init($forum = null, $forumtopic = null)
45
+	{
46
+		if (is_object($forum)) {
47
+			$this->forum_id    = $forum->getVar('forum_id');
48
+			$this->forumObject = $forum;
49
+		} else {
50
+			$this->forum_id    = (int)$forum;
51
+			$this->forumObject = $forum;
52
+		}
53
+		if (is_object($forumtopic)) {
54
+			$this->topic_id = $forumtopic->getVar('topic_id');
55
+			if (empty($this->forum_id)) {
56
+				$this->forum_id = $forumtopic->getVar('forum_id');
57
+			}
58
+		} else {
59
+			$this->topic_id = (int)$forumtopic;
60
+		}
61
+
62
+		$this->update();
63
+	}
64
+
65
+	public function update()
66
+	{
67
+		global $xoopsModule;
68
+
69
+		mt_srand((double)microtime() * 1000000);
70
+		// set gc probabillity to 10% for now..
71
+		if (mt_rand(1, 100) < 60) {
72
+			$this->gc(150);
73
+		}
74
+		if (is_object($GLOBALS['xoopsUser'])) {
75
+			$uid   = $GLOBALS['xoopsUser']->getVar('uid');
76
+			$uname = $GLOBALS['xoopsUser']->getVar('uname');
77
+			$name  = $GLOBALS['xoopsUser']->getVar('name');
78
+		} else {
79
+			$uid   = 0;
80
+			$uname = '';
81
+			$name  = '';
82
+		}
83
+
84
+		$xoops_onlineHandler = xoops_getHandler('online');
85
+		$xoopsupdate         = $xoops_onlineHandler->write($uid, $uname, time(), $xoopsModule->getVar('mid'), \Xmf\IPAddress::fromRequest()->asReadable());
86
+		if (!$xoopsupdate) {
87
+			//xoops_error("newbb online upate error");
88
+		}
89
+
90
+		$uname = (empty($GLOBALS['xoopsModuleConfig']['show_realname']) || empty($name)) ? $uname : $name;
91
+		$this->write($uid, $uname, time(), $this->forum_id, IPAddress::fromRequest()->asReadable(), $this->topic_id);
92
+	}
93
+
94
+	/**
95
+	 * @param $xoopsTpl
96
+	 */
97
+	public function render(\Smarty $xoopsTpl)
98
+	{
99
+		require_once  dirname(__DIR__) . '/include/functions.render.php';
100
+		require_once  dirname(__DIR__) . '/include/functions.user.php';
101
+		$criteria = null;
102
+		if ($this->topic_id) {
103
+			$criteria = new \Criteria('online_topic', $this->topic_id);
104
+		} elseif ($this->forum_id) {
105
+			$criteria = new \Criteria('online_forum', $this->forum_id);
106
+		}
107
+		$users     = $this->getAll($criteria);
108
+		$num_total = count($users);
109
+
110
+		$num_user     = 0;
111
+		$users_id     = [];
112
+		$users_online = [];
113
+		for ($i = 0; $i < $num_total; ++$i) {
114
+			if (empty($users[$i]['online_uid'])) {
115
+				continue;
116
+			}
117
+			$users_id[]                             = $users[$i]['online_uid'];
118
+			$users_online[$users[$i]['online_uid']] = [
119
+				'link'  => XOOPS_URL . '/userinfo.php?uid=' . $users[$i]['online_uid'],
120
+				'uname' => $users[$i]['online_uname']
121
+			];
122
+			++$num_user;
123
+		}
124
+		$num_anonymous           = $num_total - $num_user;
125
+		$online                  = [];
126
+		$online['image']         = newbbDisplayImage('whosonline');
127
+		$online['num_total']     = $num_total;
128
+		$online['num_user']      = $num_user;
129
+		$online['num_anonymous'] = $num_anonymous;
130
+		$administrator_list      = newbbIsModuleAdministrators($users_id);
131
+		$moderator_list          = [];
132
+		if ($member_list = array_diff(array_keys($administrator_list), $users_id)) {
133
+			if (is_object($this->forumObject)) {
134
+				$moderator_list = $this->forumObject->getVar('forum_moderator');
135
+			} else {
136
+				$moderator_list = newbbIsForumModerators($member_list);
137
+			}
138
+		}
139
+		foreach ($users_online as $uid => $user) {
140
+			if (!empty($administrator_list[$uid])) {
141
+				$user['level'] = 2;
142
+			} elseif (!empty($moderator_list[$uid])) {
143
+				$user['level'] = 1;
144
+			} else {
145
+				$user['level'] = 0;
146
+			}
147
+			$online['users'][] = $user;
148
+		}
149
+
150
+		$xoopsTpl->assign_by_ref('online', $online);
151
+	}
152
+
153
+	/**
154
+	 * Deprecated
155
+	 */
156
+	public function showOnline()
157
+	{
158
+		require_once  dirname(__DIR__) . '/include/functions.render.php';
159
+		require_once  dirname(__DIR__) . '/include/functions.user.php';
160
+		$criteria = null;
161
+		if ($this->topic_id) {
162
+			$criteria = new \Criteria('online_topic', $this->topic_id);
163
+		} elseif ($this->forum_id) {
164
+			$criteria = new \Criteria('online_forum', $this->forum_id);
165
+		}
166
+		$users     = $this->getAll($criteria);
167
+		$num_total = count($users);
168
+
169
+		$num_user     = 0;
170
+		$users_id     = [];
171
+		$users_online = [];
172
+		for ($i = 0; $i < $num_total; ++$i) {
173
+			if (empty($users[$i]['online_uid'])) {
174
+				continue;
175
+			}
176
+			$users_id[]                             = $users[$i]['online_uid'];
177
+			$users_online[$users[$i]['online_uid']] = [
178
+				'link'  => XOOPS_URL . '/userinfo.php?uid=' . $users[$i]['online_uid'],
179
+				'uname' => $users[$i]['online_uname']
180
+			];
181
+			++$num_user;
182
+		}
183
+		$num_anonymous           = $num_total - $num_user;
184
+		$online                  = [];
185
+		$online['image']         = newbbDisplayImage('whosonline');
186
+		$online['statistik']     = newbbDisplayImage('statistik');
187
+		$online['num_total']     = $num_total;
188
+		$online['num_user']      = $num_user;
189
+		$online['num_anonymous'] = $num_anonymous;
190
+		$administrator_list      = newbbIsModuleAdministrators($users_id);
191
+		$moderator_list          = [];
192
+		if ($member_list = array_diff($users_id, array_keys($administrator_list))) {
193
+			if (is_object($this->forumObject)) {
194
+				$moderator_list = $this->forumObject->getVar('forum_moderator');
195
+			} else {
196
+				$moderator_list = newbbIsForumModerators($member_list);
197
+			}
198
+		}
199
+
200
+		foreach ($users_online as $uid => $user) {
201
+			if (in_array($uid, $administrator_list)) {
202
+				$user['level'] = 2;
203
+			} elseif (in_array($uid, $moderator_list)) {
204
+				$user['level'] = 1;
205
+			} else {
206
+				$user['level'] = 0;
207
+			}
208
+			$online['users'][] = $user;
209
+		}
210
+
211
+		return $online;
212
+	}
213
+
214
+	/**
215
+	 * Write online information to the database
216
+	 *
217
+	 * @param  int    $uid      UID of the active user
218
+	 * @param  string $uname    Username
219
+	 * @param         $time
220
+	 * @param  string $forum_id Current forum_id
221
+	 * @param  string $ip       User's IP adress
222
+	 * @param         $topic_id
223
+	 * @return bool   TRUE on success
224
+	 * @internal param string $timestamp
225
+	 */
226
+	public function write($uid, $uname, $time, $forum_id, $ip, $topic_id)
227
+	{
228
+		global $xoopsModule, $xoopsDB;
229
+
230
+		$uid = (int)$uid;
231
+		if ($uid > 0) {
232
+			$sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('newbb_online') . ' WHERE online_uid=' . $uid;
233
+		} else {
234
+			$sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('newbb_online') . ' WHERE online_uid=' . $uid . " AND online_ip='" . $ip . "'";
235
+		}
236
+		list($count) = $this->db->fetchRow($this->db->queryF($sql));
237
+		if ($count > 0) {
238
+			$sql = 'UPDATE ' . $this->db->prefix('newbb_online') . " SET online_updated= '" . $time . "', online_forum = '" . $forum_id . "', online_topic = '" . $topic_id . "' WHERE online_uid = " . $uid;
239
+			if (0 == $uid) {
240
+				$sql .= " AND online_ip='" . $ip . "'";
241
+			}
242
+		} else {
243
+			$sql = sprintf('INSERT INTO `%s` (online_uid, online_uname, online_updated, online_ip, online_forum, online_topic) VALUES (%u, %s, %u, %s, %u, %u)', $this->db->prefix('newbb_online'), $uid, $this->db->quote($uname), $time, $this->db->quote($ip), $forum_id, $topic_id);
244
+		}
245
+		if (!$this->db->queryF($sql)) {
246
+			//xoops_error($this->db->error());
247
+			return false;
248
+		}
249
+
250
+		/** @var \XoopsOnlineHandler $xoops_onlineHandler */
251
+		$xoops_onlineHandler = xoops_getHandler('online');
252
+		$xoopsOnlineTable    = $xoops_onlineHandler->table;
253
+
254
+		$sql = 'DELETE FROM '
255
+			   . $this->db->prefix('newbb_online')
256
+			   . ' WHERE'
257
+			   . ' ( online_uid > 0 AND online_uid NOT IN ( SELECT online_uid FROM '
258
+			   . $xoopsOnlineTable
259
+			   . ' WHERE online_module ='
260
+			   . $xoopsModule->getVar('mid')
261
+			   . ' ) )'
262
+			   . ' OR ( online_uid = 0 AND online_ip NOT IN ( SELECT online_ip FROM '
263
+			   . $xoopsOnlineTable
264
+			   . ' WHERE online_module ='
265
+			   . $xoopsModule->getVar('mid')
266
+			   . ' AND online_uid = 0 ) )';
267
+
268
+		if ($result = $this->db->queryF($sql)) {
269
+			return true;
270
+		} else {
271
+			//xoops_error($this->db->error());
272
+			return false;
273
+		}
274
+	}
275
+
276
+	/**
277
+	 * Garbage Collection
278
+	 *
279
+	 * Delete all online information that has not been updated for a certain time
280
+	 *
281
+	 * @param int $expire Expiration time in seconds
282
+	 */
283
+	public function gc($expire)
284
+	{
285
+		global $xoopsModule;
286
+		$sql = 'DELETE FROM ' . $this->db->prefix('newbb_online') . ' WHERE online_updated < ' . (time() - (int)$expire);
287
+		$this->db->queryF($sql);
288
+
289
+		$xoops_onlineHandler = xoops_getHandler('online');
290
+		$xoops_onlineHandler->gc($expire);
291
+	}
292
+
293
+	/**
294
+	 * Get an array of online information
295
+	 *
296
+	 * @param  \CriteriaElement $criteria {@link \CriteriaElement}
297
+	 * @return array           Array of associative arrays of online information
298
+	 */
299
+	public function getAll(\CriteriaElement $criteria = null)
300
+	{
301
+		$ret   = [];
302
+		$limit = $start = 0;
303
+		$sql   = 'SELECT * FROM ' . $this->db->prefix('newbb_online');
304
+		if (is_object($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
305
+			$sql   .= ' ' . $criteria->renderWhere();
306
+			$limit = $criteria->getLimit();
307
+			$start = $criteria->getStart();
308
+		}
309
+		$result = $this->db->query($sql, $limit, $start);
310
+		if (!$result) {
311
+			return $ret;
312
+		}
313
+		while (false !== ($myrow = $this->db->fetchArray($result))) {
314
+			$ret[] = $myrow;
315
+			if ($myrow['online_uid'] > 0) {
316
+				$this->user_ids[] = $myrow['online_uid'];
317
+			}
318
+			unset($myrow);
319
+		}
320
+		$this->user_ids = array_unique($this->user_ids);
321
+
322
+		return $ret;
323
+	}
324
+
325
+	/**
326
+	 * @param $uids
327
+	 * @return array
328
+	 */
329
+	public function checkStatus($uids)
330
+	{
331
+		$online_users = [];
332
+		$ret          = [];
333
+		if (!empty($this->user_ids)) {
334
+			$online_users = $this->user_ids;
335
+		} else {
336
+			$sql = 'SELECT online_uid FROM ' . $this->db->prefix('newbb_online');
337
+			if (!empty($uids)) {
338
+				$sql .= ' WHERE online_uid IN (' . implode(', ', array_map('intval', $uids)) . ')';
339
+			}
340
+
341
+			$result = $this->db->query($sql);
342
+			if (!$result) {
343
+				return $ret;
344
+			}
345
+			while (false !== (list($uid) = $this->db->fetchRow($result))) {
346
+				$online_users[] = $uid;
347
+			}
348
+		}
349
+		foreach ($uids as $uid) {
350
+			if (in_array($uid, $online_users)) {
351
+				$ret[$uid] = 1;
352
+			}
353
+		}
354
+
355
+		return $ret;
356
+	}
357
+
358
+	/**
359
+	 * Count the number of online users
360
+	 *
361
+	 * @param  \CriteriaElement $criteria {@link CriteriaElement}
362
+	 * @return bool
363
+	 */
364
+	public function getCount(\CriteriaElement $criteria = null)
365
+	{
366
+		$sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('newbb_online');
367
+		if (is_object($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
368
+			$sql .= ' ' . $criteria->renderWhere();
369
+		}
370
+		if (!$result = $this->db->query($sql)) {
371
+			return false;
372
+		}
373
+		list($ret) = $this->db->fetchRow($result);
374
+
375
+		return $ret;
376
+	}
377 377
 }
Please login to merge, or discard this patch.
class/DigestHandler.php 1 patch
Indentation   +212 added lines, -212 removed lines patch added patch discarded remove patch
@@ -17,139 +17,139 @@  discard block
 block discarded – undo
17 17
  */
18 18
 class DigestHandler extends \XoopsPersistableObjectHandler
19 19
 {
20
-    public $last_digest;
20
+	public $last_digest;
21 21
 
22
-    /**
23
-     * Constructor
24
-     *
25
-     * @param null|\XoopsDatabase $db database connection
26
-     */
27
-    public function __construct(\XoopsDatabase $db = null)
28
-    {
29
-        parent::__construct($db, 'newbb_digest', Digest::class, 'digest_id');
30
-    }
22
+	/**
23
+	 * Constructor
24
+	 *
25
+	 * @param null|\XoopsDatabase $db database connection
26
+	 */
27
+	public function __construct(\XoopsDatabase $db = null)
28
+	{
29
+		parent::__construct($db, 'newbb_digest', Digest::class, 'digest_id');
30
+	}
31 31
 
32
-    /**
33
-     * @param  bool $isForced
34
-     * @return int
35
-     */
36
-    public function process($isForced = false)
37
-    {
38
-        $this->getLastDigest();
39
-        if (!$isForced) {
40
-            $status = $this->checkStatus();
41
-            if ($status < 1) {
42
-                return 1;
43
-            }
44
-        }
45
-        $digest = $this->create();
46
-        $status = $this->buildDigest($digest);
47
-        if (!$status) {
48
-            return 2;
49
-        }
50
-        $status = $this->insert($digest);
51
-        if (!$status) {
52
-            return 3;
53
-        }
54
-        $status = $this->notify($digest);
55
-        if (!$status) {
56
-            return 4;
57
-        }
32
+	/**
33
+	 * @param  bool $isForced
34
+	 * @return int
35
+	 */
36
+	public function process($isForced = false)
37
+	{
38
+		$this->getLastDigest();
39
+		if (!$isForced) {
40
+			$status = $this->checkStatus();
41
+			if ($status < 1) {
42
+				return 1;
43
+			}
44
+		}
45
+		$digest = $this->create();
46
+		$status = $this->buildDigest($digest);
47
+		if (!$status) {
48
+			return 2;
49
+		}
50
+		$status = $this->insert($digest);
51
+		if (!$status) {
52
+			return 3;
53
+		}
54
+		$status = $this->notify($digest);
55
+		if (!$status) {
56
+			return 4;
57
+		}
58 58
 
59
-        return 0;
60
-    }
59
+		return 0;
60
+	}
61 61
 
62
-    /**
63
-     * @param \XoopsObject $digest
64
-     * @return bool
65
-     */
66
-    public function notify(\XoopsObject $digest)
67
-    {
68
-        //$content                = $digest->getVar('digest_content');
69
-        /** @var \XoopsNotificationHandler $notificationHandler */
70
-        $notificationHandler    = xoops_getHandler('notification');
71
-        $tags['DIGEST_ID']      = $digest->getVar('digest_id');
72
-        $tags['DIGEST_CONTENT'] = $digest->getVar('digest_content', 'E');
73
-        $notificationHandler->triggerEvent('global', 0, 'digest', $tags);
62
+	/**
63
+	 * @param \XoopsObject $digest
64
+	 * @return bool
65
+	 */
66
+	public function notify(\XoopsObject $digest)
67
+	{
68
+		//$content                = $digest->getVar('digest_content');
69
+		/** @var \XoopsNotificationHandler $notificationHandler */
70
+		$notificationHandler    = xoops_getHandler('notification');
71
+		$tags['DIGEST_ID']      = $digest->getVar('digest_id');
72
+		$tags['DIGEST_CONTENT'] = $digest->getVar('digest_content', 'E');
73
+		$notificationHandler->triggerEvent('global', 0, 'digest', $tags);
74 74
 
75
-        return true;
76
-    }
75
+		return true;
76
+	}
77 77
 
78
-    /**
79
-     * @param        $start
80
-     * @param  int   $perpage
81
-     * @return array
82
-     */
83
-    public function getAllDigests($start = 0, $perpage = 5)
84
-    {
85
-        //        if (empty($start)) {
86
-        //            $start = 0;
87
-        //        }
78
+	/**
79
+	 * @param        $start
80
+	 * @param  int   $perpage
81
+	 * @return array
82
+	 */
83
+	public function getAllDigests($start = 0, $perpage = 5)
84
+	{
85
+		//        if (empty($start)) {
86
+		//            $start = 0;
87
+		//        }
88 88
 
89
-        $sql    = 'SELECT * FROM ' . $this->db->prefix('newbb_digest') . ' ORDER BY digest_id DESC';
90
-        $result = $this->db->query($sql, $perpage, $start);
91
-        $ret    = [];
92
-        //        $reportHandler =  Newbb\Helper::getInstance()->getHandler('Report');
93
-        while (false !== ($myrow = $this->db->fetchArray($result))) {
94
-            $ret[] = $myrow; // return as array
95
-        }
89
+		$sql    = 'SELECT * FROM ' . $this->db->prefix('newbb_digest') . ' ORDER BY digest_id DESC';
90
+		$result = $this->db->query($sql, $perpage, $start);
91
+		$ret    = [];
92
+		//        $reportHandler =  Newbb\Helper::getInstance()->getHandler('Report');
93
+		while (false !== ($myrow = $this->db->fetchArray($result))) {
94
+			$ret[] = $myrow; // return as array
95
+		}
96 96
 
97
-        return $ret;
98
-    }
97
+		return $ret;
98
+	}
99 99
 
100
-    /**
101
-     * @return int
102
-     */
103
-    public function getDigestCount()
104
-    {
105
-        $sql    = 'SELECT COUNT(*) AS count FROM ' . $this->db->prefix('newbb_digest');
106
-        $result = $this->db->query($sql);
107
-        if (!$result) {
108
-            return 0;
109
-        } else {
110
-            $array = $this->db->fetchArray($result);
100
+	/**
101
+	 * @return int
102
+	 */
103
+	public function getDigestCount()
104
+	{
105
+		$sql    = 'SELECT COUNT(*) AS count FROM ' . $this->db->prefix('newbb_digest');
106
+		$result = $this->db->query($sql);
107
+		if (!$result) {
108
+			return 0;
109
+		} else {
110
+			$array = $this->db->fetchArray($result);
111 111
 
112
-            return $array['count'];
113
-        }
114
-    }
112
+			return $array['count'];
113
+		}
114
+	}
115 115
 
116
-    public function getLastDigest()
117
-    {
118
-        $sql    = 'SELECT MAX(digest_time) AS last_digest FROM ' . $this->db->prefix('newbb_digest');
119
-        $result = $this->db->query($sql);
120
-        if (!$result) {
121
-            $this->last_digest = 0;
122
-        // echo "<br>no data:".$query;
123
-        } else {
124
-            $array             = $this->db->fetchArray($result);
125
-            $this->last_digest = isset($array['last_digest']) ? $array['last_digest'] : 0;
126
-        }
127
-    }
116
+	public function getLastDigest()
117
+	{
118
+		$sql    = 'SELECT MAX(digest_time) AS last_digest FROM ' . $this->db->prefix('newbb_digest');
119
+		$result = $this->db->query($sql);
120
+		if (!$result) {
121
+			$this->last_digest = 0;
122
+		// echo "<br>no data:".$query;
123
+		} else {
124
+			$array             = $this->db->fetchArray($result);
125
+			$this->last_digest = isset($array['last_digest']) ? $array['last_digest'] : 0;
126
+		}
127
+	}
128 128
 
129
-    /**
130
-     * @return int
131
-     */
132
-    public function checkStatus()
133
-    {
134
-        if (!isset($this->last_digest)) {
135
-            $this->getLastDigest();
136
-        }
137
-        $deadline  = (1 == $GLOBALS['xoopsModuleConfig']['email_digest']) ? 60 * 60 * 24 : 60 * 60 * 24 * 7;
138
-        $time_diff = time() - $this->last_digest;
129
+	/**
130
+	 * @return int
131
+	 */
132
+	public function checkStatus()
133
+	{
134
+		if (!isset($this->last_digest)) {
135
+			$this->getLastDigest();
136
+		}
137
+		$deadline  = (1 == $GLOBALS['xoopsModuleConfig']['email_digest']) ? 60 * 60 * 24 : 60 * 60 * 24 * 7;
138
+		$time_diff = time() - $this->last_digest;
139 139
 
140
-        return $time_diff - $deadline;
141
-    }
140
+		return $time_diff - $deadline;
141
+	}
142 142
 
143
-    /**
144
-     * @param  \XoopsObject $digest
145
-     * @param  bool         $force flag to force the query execution despite security settings
146
-     * @return mixed       object ID or false
147
-     */
148
-    public function insert(\XoopsObject $digest, $force = true)
149
-    {
150
-        $digest->setVar('digest_time', time());
151
-        return parent::insert($digest, $force);
152
-        /*
143
+	/**
144
+	 * @param  \XoopsObject $digest
145
+	 * @param  bool         $force flag to force the query execution despite security settings
146
+	 * @return mixed       object ID or false
147
+	 */
148
+	public function insert(\XoopsObject $digest, $force = true)
149
+	{
150
+		$digest->setVar('digest_time', time());
151
+		return parent::insert($digest, $force);
152
+		/*
153 153
         $content = $digest->getVar('digest_content', 'E');
154 154
 
155 155
         $id  = $this->db->genId($digest->table . '_digest_id_seq');
@@ -166,108 +166,108 @@  discard block
 block discarded – undo
166 166
 
167 167
         return true;
168 168
         */
169
-    }
169
+	}
170 170
 
171
-    /**
172
-     * @param \XoopsObject $digest
173
-     * @param  bool        $force (ignored)
174
-     * @return bool        FALSE if failed.
175
-     */
176
-    public function delete(\XoopsObject $digest, $force = false)
177
-    {
178
-        $digest_id = $digest->getVar('digest_id');
171
+	/**
172
+	 * @param \XoopsObject $digest
173
+	 * @param  bool        $force (ignored)
174
+	 * @return bool        FALSE if failed.
175
+	 */
176
+	public function delete(\XoopsObject $digest, $force = false)
177
+	{
178
+		$digest_id = $digest->getVar('digest_id');
179 179
 
180
-        if (!isset($this->last_digest)) {
181
-            $this->getLastDigest();
182
-        }
183
-        if ($this->last_digest == $digest_id) {
184
-            return false;
185
-        } // It is not allowed to delete the last digest
180
+		if (!isset($this->last_digest)) {
181
+			$this->getLastDigest();
182
+		}
183
+		if ($this->last_digest == $digest_id) {
184
+			return false;
185
+		} // It is not allowed to delete the last digest
186 186
 
187
-        return parent::delete($digest, true);
188
-    }
187
+		return parent::delete($digest, true);
188
+	}
189 189
 
190
-    /**
191
-     * @param \XoopsObject $digest
192
-     * @return bool
193
-     */
194
-    public function buildDigest(\XoopsObject $digest)
195
-    {
196
-        global $xoopsModule;
190
+	/**
191
+	 * @param \XoopsObject $digest
192
+	 * @return bool
193
+	 */
194
+	public function buildDigest(\XoopsObject $digest)
195
+	{
196
+		global $xoopsModule;
197 197
 
198
-        if (!defined('SUMMARY_LENGTH')) {
199
-            define('SUMMARY_LENGTH', 100);
200
-        }
198
+		if (!defined('SUMMARY_LENGTH')) {
199
+			define('SUMMARY_LENGTH', 100);
200
+		}
201 201
 
202
-        /** @var Newbb\ForumHandler $forumHandler */
203
-        $forumHandler         = Newbb\Helper::getInstance()->getHandler('Forum');
204
-        $thisUser             = $GLOBALS['xoopsUser'];
205
-        $GLOBALS['xoopsUser'] = null; // To get posts accessible by anonymous
206
-        $GLOBALS['xoopsUser'] = $thisUser;
202
+		/** @var Newbb\ForumHandler $forumHandler */
203
+		$forumHandler         = Newbb\Helper::getInstance()->getHandler('Forum');
204
+		$thisUser             = $GLOBALS['xoopsUser'];
205
+		$GLOBALS['xoopsUser'] = null; // To get posts accessible by anonymous
206
+		$GLOBALS['xoopsUser'] = $thisUser;
207 207
 
208
-        $accessForums    = $forumHandler->getIdsByPermission(); // get all accessible forums
209
-        $forumCriteria   = ' AND t.forum_id IN (' . implode(',', $accessForums) . ')';
210
-        $approveCriteria = ' AND t.approved = 1 AND p.approved = 1';
211
-        $time_criteria   = ' AND t.digest_time > ' . $this->last_digest;
208
+		$accessForums    = $forumHandler->getIdsByPermission(); // get all accessible forums
209
+		$forumCriteria   = ' AND t.forum_id IN (' . implode(',', $accessForums) . ')';
210
+		$approveCriteria = ' AND t.approved = 1 AND p.approved = 1';
211
+		$time_criteria   = ' AND t.digest_time > ' . $this->last_digest;
212 212
 
213
-        $karma_criteria = $GLOBALS['xoopsModuleConfig']['enable_karma'] ? ' AND p.post_karma=0' : '';
214
-        $reply_criteria = $GLOBALS['xoopsModuleConfig']['allow_require_reply'] ? ' AND p.require_reply=0' : '';
213
+		$karma_criteria = $GLOBALS['xoopsModuleConfig']['enable_karma'] ? ' AND p.post_karma=0' : '';
214
+		$reply_criteria = $GLOBALS['xoopsModuleConfig']['allow_require_reply'] ? ' AND p.require_reply=0' : '';
215 215
 
216
-        $query = 'SELECT t.topic_id, t.forum_id, t.topic_title, t.topic_time, t.digest_time, p.uid, p.poster_name, pt.post_text FROM '
217
-                 . $this->db->prefix('newbb_topics')
218
-                 . ' t, '
219
-                 . $this->db->prefix('newbb_posts_text')
220
-                 . ' pt, '
221
-                 . $this->db->prefix('newbb_posts')
222
-                 . ' p WHERE t.topic_digest = 1 AND p.topic_id=t.topic_id AND p.pid=0 '
223
-                 . $forumCriteria
224
-                 . $approveCriteria
225
-                 . $time_criteria
226
-                 . $karma_criteria
227
-                 . $reply_criteria
228
-                 . ' AND pt.post_id=p.post_id ORDER BY t.digest_time DESC';
229
-        if (!$result = $this->db->query($query)) {
230
-            //echo "<br>No result:<br>$query";
231
-            return false;
232
-        }
233
-        $rows  = [];
234
-        $users = [];
235
-        while (false !== ($row = $this->db->fetchArray($result))) {
236
-            $users[$row['uid']] = 1;
237
-            $rows[]             = $row;
238
-        }
239
-        if (count($rows) < 1) {
240
-            return false;
241
-        }
242
-        $uids = array_keys($users);
243
-        if (count($uids) > 0) {
244
-            /** @var \XoopsMemberHandler $memberHandler */
245
-            $memberHandler = xoops_getHandler('member');
246
-            $user_criteria = new \Criteria('uid', '(' . implode(',', $uids) . ')', 'IN');
247
-            $users         = $memberHandler->getUsers($user_criteria, true);
248
-        } else {
249
-            $users = [];
250
-        }
216
+		$query = 'SELECT t.topic_id, t.forum_id, t.topic_title, t.topic_time, t.digest_time, p.uid, p.poster_name, pt.post_text FROM '
217
+				 . $this->db->prefix('newbb_topics')
218
+				 . ' t, '
219
+				 . $this->db->prefix('newbb_posts_text')
220
+				 . ' pt, '
221
+				 . $this->db->prefix('newbb_posts')
222
+				 . ' p WHERE t.topic_digest = 1 AND p.topic_id=t.topic_id AND p.pid=0 '
223
+				 . $forumCriteria
224
+				 . $approveCriteria
225
+				 . $time_criteria
226
+				 . $karma_criteria
227
+				 . $reply_criteria
228
+				 . ' AND pt.post_id=p.post_id ORDER BY t.digest_time DESC';
229
+		if (!$result = $this->db->query($query)) {
230
+			//echo "<br>No result:<br>$query";
231
+			return false;
232
+		}
233
+		$rows  = [];
234
+		$users = [];
235
+		while (false !== ($row = $this->db->fetchArray($result))) {
236
+			$users[$row['uid']] = 1;
237
+			$rows[]             = $row;
238
+		}
239
+		if (count($rows) < 1) {
240
+			return false;
241
+		}
242
+		$uids = array_keys($users);
243
+		if (count($uids) > 0) {
244
+			/** @var \XoopsMemberHandler $memberHandler */
245
+			$memberHandler = xoops_getHandler('member');
246
+			$user_criteria = new \Criteria('uid', '(' . implode(',', $uids) . ')', 'IN');
247
+			$users         = $memberHandler->getUsers($user_criteria, true);
248
+		} else {
249
+			$users = [];
250
+		}
251 251
 
252
-        foreach ($rows as $topic) {
253
-            if ($topic['uid'] > 0) {
254
-                if (isset($users[$topic['uid']]) && is_object($users[$topic['uid']])
255
-                    && $users[$topic['uid']]->isActive()) {
256
-                    $topic['uname'] = $users[$topic['uid']]->getVar('uname');
257
-                } else {
258
-                    $topic['uname'] = $GLOBALS['xoopsConfig']['anonymous'];
259
-                }
260
-            } else {
261
-                $topic['uname'] = $topic['poster_name'] ?: $GLOBALS['xoopsConfig']['anonymous'];
262
-            }
263
-            $summary = \Xmf\Metagen::generateDescription($topic['post_text'], SUMMARY_LENGTH);
264
-            $author  = $topic['uname'] . ' (' . formatTimestamp($topic['topic_time']) . ')';
265
-            $link    = XOOPS_URL . '/modules/' . $xoopsModule->dirname() . '/viewtopic.php?topic_id=' . $topic['topic_id'] . '&amp;forum=' . $topic['forum_id'];
266
-            $title   = $topic['topic_title'];
267
-            $digest->addItem($title, $link, $author, $summary);
268
-        }
269
-        $digest->buildContent();
252
+		foreach ($rows as $topic) {
253
+			if ($topic['uid'] > 0) {
254
+				if (isset($users[$topic['uid']]) && is_object($users[$topic['uid']])
255
+					&& $users[$topic['uid']]->isActive()) {
256
+					$topic['uname'] = $users[$topic['uid']]->getVar('uname');
257
+				} else {
258
+					$topic['uname'] = $GLOBALS['xoopsConfig']['anonymous'];
259
+				}
260
+			} else {
261
+				$topic['uname'] = $topic['poster_name'] ?: $GLOBALS['xoopsConfig']['anonymous'];
262
+			}
263
+			$summary = \Xmf\Metagen::generateDescription($topic['post_text'], SUMMARY_LENGTH);
264
+			$author  = $topic['uname'] . ' (' . formatTimestamp($topic['topic_time']) . ')';
265
+			$link    = XOOPS_URL . '/modules/' . $xoopsModule->dirname() . '/viewtopic.php?topic_id=' . $topic['topic_id'] . '&amp;forum=' . $topic['forum_id'];
266
+			$title   = $topic['topic_title'];
267
+			$digest->addItem($title, $link, $author, $summary);
268
+		}
269
+		$digest->buildContent();
270 270
 
271
-        return true;
272
-    }
271
+		return true;
272
+	}
273 273
 }
Please login to merge, or discard this patch.
class/ModerateHandler.php 1 patch
Indentation   +129 added lines, -129 removed lines patch added patch discarded remove patch
@@ -19,133 +19,133 @@
 block discarded – undo
19 19
  */
20 20
 class ModerateHandler extends \XoopsPersistableObjectHandler
21 21
 {
22
-    /**
23
-     * @param null|\XoopsDatabase $db
24
-     */
25
-    public function __construct(\XoopsDatabase $db = null)
26
-    {
27
-        parent::__construct($db, 'newbb_moderates', Moderate::class, 'mod_id', 'uid');
28
-    }
29
-
30
-    /**
31
-     * Clear garbage
32
-     *
33
-     * Delete all moderation information that has expired
34
-     *
35
-     * @param int $expire Expiration time in UNIX, 0 for time()
36
-     */
37
-    public function clearGarbage($expire = 0)
38
-    {
39
-        $expire = time() - (int)$expire;
40
-        $sql    = sprintf('DELETE FROM `%s` WHERE mod_end < %u', $this->db->prefix('newbb_moderates'), $expire);
41
-        $this->db->queryF($sql);
42
-    }
43
-
44
-    /**
45
-     * Check if a user is moderated, according to his uid and ip
46
-     *
47
-     *
48
-     * @param  int    $uid user id
49
-     * @param  string|bool $ip  user ip
50
-     * @param  int    $forum
51
-     * @return bool true if IP is banned
52
-     */
53
-    public function verifyUser($uid = -1, $ip = '', $forum = 0)
54
-    {
55
-        error_reporting(E_ALL);
56
-        // if user is admin do not suspend
57
-        if (newbbIsAdmin($forum)) {
58
-            return true;
59
-        }
60
-
61
-        $uid = ($uid < 0) ? (is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0) : (int)$uid;
62
-
63
-        $criteria      = new \CriteriaCompo(new \Criteria('uid', (int)$uid));
64
-        $forumCriteria = new \CriteriaCompo(new \Criteria('forum_id', 0), 'OR');
65
-        if (!empty($forum)) {
66
-            $forumCriteria->add(new \Criteria('forum_id', (int)$forum), 'OR');
67
-        }
68
-        $criteria->add($forumCriteria);
69
-        $criteria->add(new \Criteria('mod_end', time(), '>'));
70
-
71
-        $matches = $this->getAll($criteria);
72
-
73
-        if (0 === count($matches)) {
74
-            return true; // no matches
75
-        }
76
-
77
-        if (count($matches) > 0 && $uid > 0) {
78
-            return false; // user is banned
79
-        }
80
-        // verify possible matches against IP address
81
-        $ip = empty($ip) ? IPAddress::fromRequest()->asReadable() : $ip;
82
-
83
-        foreach ($matches as $modMatch) {
84
-            $rawModIp = trim($modMatch->getVar('ip', 'n'));
85
-            if (empty($rawModIp)) {
86
-                return false; // banned without IP
87
-            }
88
-            $parts   = explode('/', $rawModIp);
89
-            $modIp   = $parts[0];
90
-            $checkIp = new IPAddress($modIp);
91
-            if (false !== $checkIp->asReadable()) {
92
-                $defaultMask = (6 === $checkIp->ipVersion()) ? 128 : 32;
93
-                $netMask     = isset($parts[1]) ? (int)$parts[1] : $defaultMask;
94
-                if ($checkIp->sameSubnet($ip, $netMask, $netMask)) {
95
-                    return false; // IP is banned
96
-                }
97
-            }
98
-        }
99
-
100
-        return true;
101
-    }
102
-
103
-    /**
104
-     * Get latest expiration for a user moderation
105
-     *
106
-     *
107
-     * @param  mixed  $item user id or ip
108
-     * @param  bool $isUid
109
-     * @return int
110
-     */
111
-    public function getLatest($item, $isUid = true)
112
-    {
113
-        $ips = [];
114
-        if ($isUid) {
115
-            $criteria = 'uid =' . (int)$item;
116
-        } else {
117
-            $ip_segs = explode('.', $item);
118
-            $segs    = min(count($ip_segs), 4);
119
-            for ($i = 1; $i <= $segs; ++$i) {
120
-                $ips[] = $this->db->quoteString(implode('.', array_slice($ip_segs, 0, $i)));
121
-            }
122
-            $criteria = 'ip IN(' . implode(',', $ips) . ')';
123
-        }
124
-        $sql = 'SELECT MAX(mod_end) AS expire FROM ' . $this->db->prefix('newbb_moderates') . ' WHERE ' . $criteria;
125
-        if (!$result = $this->db->query($sql)) {
126
-            return -1;
127
-        }
128
-        $row = $this->db->fetchArray($result);
129
-
130
-        return $row['expire'];
131
-    }
132
-
133
-    /**
134
-     * clean orphan items from database
135
-     *
136
-     * @param  string $table_link
137
-     * @param  string $field_link
138
-     * @param  string $field_object
139
-     * @return bool   true on success
140
-     */
141
-    public function cleanOrphan($table_link = '', $field_link = '', $field_object = '') //cleanOrphan()
142
-    {
143
-        $sql = 'DELETE FROM ' . $this->table . ' WHERE (forum_id >0 AND forum_id NOT IN ( SELECT DISTINCT forum_id FROM ' . $this->db->prefix('newbb_forums') . ') )';
144
-        if (!$result = $this->db->queryF($sql)) {
145
-            //xoops_error($this->db->error());
146
-            return false;
147
-        }
148
-
149
-        return true;
150
-    }
22
+	/**
23
+	 * @param null|\XoopsDatabase $db
24
+	 */
25
+	public function __construct(\XoopsDatabase $db = null)
26
+	{
27
+		parent::__construct($db, 'newbb_moderates', Moderate::class, 'mod_id', 'uid');
28
+	}
29
+
30
+	/**
31
+	 * Clear garbage
32
+	 *
33
+	 * Delete all moderation information that has expired
34
+	 *
35
+	 * @param int $expire Expiration time in UNIX, 0 for time()
36
+	 */
37
+	public function clearGarbage($expire = 0)
38
+	{
39
+		$expire = time() - (int)$expire;
40
+		$sql    = sprintf('DELETE FROM `%s` WHERE mod_end < %u', $this->db->prefix('newbb_moderates'), $expire);
41
+		$this->db->queryF($sql);
42
+	}
43
+
44
+	/**
45
+	 * Check if a user is moderated, according to his uid and ip
46
+	 *
47
+	 *
48
+	 * @param  int    $uid user id
49
+	 * @param  string|bool $ip  user ip
50
+	 * @param  int    $forum
51
+	 * @return bool true if IP is banned
52
+	 */
53
+	public function verifyUser($uid = -1, $ip = '', $forum = 0)
54
+	{
55
+		error_reporting(E_ALL);
56
+		// if user is admin do not suspend
57
+		if (newbbIsAdmin($forum)) {
58
+			return true;
59
+		}
60
+
61
+		$uid = ($uid < 0) ? (is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0) : (int)$uid;
62
+
63
+		$criteria      = new \CriteriaCompo(new \Criteria('uid', (int)$uid));
64
+		$forumCriteria = new \CriteriaCompo(new \Criteria('forum_id', 0), 'OR');
65
+		if (!empty($forum)) {
66
+			$forumCriteria->add(new \Criteria('forum_id', (int)$forum), 'OR');
67
+		}
68
+		$criteria->add($forumCriteria);
69
+		$criteria->add(new \Criteria('mod_end', time(), '>'));
70
+
71
+		$matches = $this->getAll($criteria);
72
+
73
+		if (0 === count($matches)) {
74
+			return true; // no matches
75
+		}
76
+
77
+		if (count($matches) > 0 && $uid > 0) {
78
+			return false; // user is banned
79
+		}
80
+		// verify possible matches against IP address
81
+		$ip = empty($ip) ? IPAddress::fromRequest()->asReadable() : $ip;
82
+
83
+		foreach ($matches as $modMatch) {
84
+			$rawModIp = trim($modMatch->getVar('ip', 'n'));
85
+			if (empty($rawModIp)) {
86
+				return false; // banned without IP
87
+			}
88
+			$parts   = explode('/', $rawModIp);
89
+			$modIp   = $parts[0];
90
+			$checkIp = new IPAddress($modIp);
91
+			if (false !== $checkIp->asReadable()) {
92
+				$defaultMask = (6 === $checkIp->ipVersion()) ? 128 : 32;
93
+				$netMask     = isset($parts[1]) ? (int)$parts[1] : $defaultMask;
94
+				if ($checkIp->sameSubnet($ip, $netMask, $netMask)) {
95
+					return false; // IP is banned
96
+				}
97
+			}
98
+		}
99
+
100
+		return true;
101
+	}
102
+
103
+	/**
104
+	 * Get latest expiration for a user moderation
105
+	 *
106
+	 *
107
+	 * @param  mixed  $item user id or ip
108
+	 * @param  bool $isUid
109
+	 * @return int
110
+	 */
111
+	public function getLatest($item, $isUid = true)
112
+	{
113
+		$ips = [];
114
+		if ($isUid) {
115
+			$criteria = 'uid =' . (int)$item;
116
+		} else {
117
+			$ip_segs = explode('.', $item);
118
+			$segs    = min(count($ip_segs), 4);
119
+			for ($i = 1; $i <= $segs; ++$i) {
120
+				$ips[] = $this->db->quoteString(implode('.', array_slice($ip_segs, 0, $i)));
121
+			}
122
+			$criteria = 'ip IN(' . implode(',', $ips) . ')';
123
+		}
124
+		$sql = 'SELECT MAX(mod_end) AS expire FROM ' . $this->db->prefix('newbb_moderates') . ' WHERE ' . $criteria;
125
+		if (!$result = $this->db->query($sql)) {
126
+			return -1;
127
+		}
128
+		$row = $this->db->fetchArray($result);
129
+
130
+		return $row['expire'];
131
+	}
132
+
133
+	/**
134
+	 * clean orphan items from database
135
+	 *
136
+	 * @param  string $table_link
137
+	 * @param  string $field_link
138
+	 * @param  string $field_object
139
+	 * @return bool   true on success
140
+	 */
141
+	public function cleanOrphan($table_link = '', $field_link = '', $field_object = '') //cleanOrphan()
142
+	{
143
+		$sql = 'DELETE FROM ' . $this->table . ' WHERE (forum_id >0 AND forum_id NOT IN ( SELECT DISTINCT forum_id FROM ' . $this->db->prefix('newbb_forums') . ') )';
144
+		if (!$result = $this->db->queryF($sql)) {
145
+			//xoops_error($this->db->error());
146
+			return false;
147
+		}
148
+
149
+		return true;
150
+	}
151 151
 }
Please login to merge, or discard this patch.