ModerateHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Newbb;
4
5
/**
6
 * NewBB 5.0x,  the forum module for XOOPS project
7
 *
8
 * @copyright      XOOPS Project (https://xoops.org)
9
 * @license        GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
10
 * @author         Taiwen Jiang (phppp or D.J.) <[email protected]>
11
 * @since          4.00
12
 * @package        module::newbb
13
 */
14
15
use Xmf\IPAddress;
16
17
 /**
18
 * Class ModerateHandler
19
 */
20
class ModerateHandler extends \XoopsPersistableObjectHandler
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)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
145
            //xoops_error($this->db->error());
146
            return false;
147
        }
148
149
        return true;
150
    }
151
}
152