-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMessage.php
293 lines (237 loc) · 8.66 KB
/
Message.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
namespace Models;
class Message extends \DB\SQL\Mapper
{
public static $rows = [
'msgID' => 0,
'msgTime' => 0,
'msgModified' => 0,
'reason' => '',
'reasonby' => '',
'boardID' => 0,
'topicID' => 0,
'approved' => 1,
'userID' => 0,
'userName' => 'Guest',
'userEmail' => '',
'userIP' => '',
'title' => '',
'body' => '',
'tags' => '',
'url' => '',
];
public static $topicRows = [
'locked' => 0,
'sticky' => 0,
];
private static $_prefix;
function __construct(\DB\SQL $db)
{
self::$_prefix = \Base::instance()->get('_db.prefix');
parent::__construct($db, self::$_prefix . 'message');
}
function entries($params = [])
{
$f3 = \Base::instance();
$entries = [];
$entries = $this->db->exec('
SELECT t.locked, t.sticky, t.numReplies, m.msgID, m.msgTime, m.title, m.tags, m.msgModified, m.reason, m.reasonBy, m.url, m.body, m.userEmail, IFNULL(u.userID, 0) AS userID, IFNULL(u.userName, m.userName) AS userName, IFNULL(u.avatar, "") AS avatar, (u.last_active >= UNIX_TIMESTAMP() - 300) AS isOnline
FROM '. self::$_prefix .'topic AS t
LEFT JOIN '. self::$_prefix .'message AS m ON (m.msgID = t.fmsgID)
LEFT JOIN '. self::$_prefix .'user AS u ON (u.userID = m.userID)
WHERE t.boardID = :board
ORDER BY m.msgID DESC
LIMIT :start, :limit', $params, 300);
// Add a nice description and a real date.
foreach ($entries as $k => $v)
$entries[$k] = $f3->get('Tools')->prepareData($v);
return $entries;
}
function entryInfo($id = 0)
{
$f3 = \Base::instance();
$r = [];
$r = $this->db->exec('
SELECT t.locked, t.sticky, t.lmsgID, t.numReplies, m.msgID, m.topicID, m.msgTime, m.title, m.msgModified, m.reason, m.reasonBy, m.tags, m.url, m.boardID, m.body, b.title AS boardTitle, b.url AS boardUrl, m.userEmail, IFNULL(u.userID, 0) AS userID, IFNULL(u.userName, m.userName) AS userName, IFNULL(u.avatar, "") AS avatar, (u.last_active >= UNIX_TIMESTAMP() - 300) AS isOnline
FROM '. self::$_prefix .'topic AS t
LEFT JOIN '. self::$_prefix .'message AS m ON (m.msgID = t.fmsgID)
LEFT JOIN '. self::$_prefix .'board AS b ON (b.boardID = t.boardID)
LEFT JOIN '. self::$_prefix .'user AS u ON (u.userID = m.userID)
WHERE t.topicID = :topic
ORDER BY m.msgID DESC
LIMIT 1', [
':topic' => $id,
], 300);
if (empty($r))
return [];
$r = $r[0];
return $f3->get('Tools')->prepareData($r);
}
function latestTopics($limit = 10, $ttl = 300)
{
$f3 = \Base::instance();
$data = [];
// Cache is set on call.
$data = $this->db->exec('
SELECT t.locked, t.sticky, t.fmsgID, t.lmsgID, t.numReplies, m.msgID, m.url, m.topicID, m.msgTime, m.title, m.msgModified, m.reason, m.reasonBy, m.tags, m.boardID, m.body, b.title AS boardTitle, b.url AS boardUrl, m.userEmail, IFNULL(u.userID, 0) AS userID, IFNULL(u.userName, m.userName) AS userName, IFNULL(u.avatar, "") AS avatar, (u.last_active >= UNIX_TIMESTAMP() - 300) AS isOnline
FROM '. $this->table() .' AS m
LEFT JOIN '. self::$_prefix .'topic AS t ON (t.fmsgID = m.msgID)
LEFT JOIN '. self::$_prefix .'board AS b ON (b.boardID = t.boardID)
LEFT JOIN '. self::$_prefix .'user AS u ON (u.userID = m.userID)
ORDER BY t.topicID DESC
LIMIT :limit', [':limit' => $limit], $ttl);
if (empty($data))
return [];
foreach ($data as $k => $v)
{
if (empty($data[$k]['topicID']))
{
unset($data[$k]);
continue;
}
$data[$k] = $f3->get('Tools')->prepareData($v);
}
return $data;
}
function latestMessages($limit = 5, $ttl = 300)
{
$f3 = \Base::instance();
$data = [];
$data = $this->db->exec('
SELECT fm.url, t.locked, t.sticky, t.lmsgID, t.numReplies, m.msgID, m.topicID, m.msgTime, m.title, m.tags, m.boardID, m.userEmail, IFNULL(u.userID, 0) AS userID, IFNULL(u.userName, m.userName) AS userName, IFNULL(u.avatar, "") AS avatar, (u.last_active >= UNIX_TIMESTAMP() - 300) AS isOnline
FROM '. $this->table() .' AS m
LEFT JOIN '. self::$_prefix .'topic AS t ON (t.topicID = m.topicID)
LEFT JOIN '. self::$_prefix .'message AS fm ON (fm.msgID = t.fmsgID)
LEFT JOIN '. self::$_prefix .'user AS u ON (u.userID = m.userID)
ORDER BY m.msgID DESC
LIMIT :limit', [':limit' => $limit], $ttl);
foreach ($data as $k => $r)
$data[$k] = $f3->get('Tools')->prepareData($r);
return $data;
}
function comments($params = [], $page = 0)
{
$f3 = \Base::instance();
$data = [];
$result = $this->db->exec('
SELECT fm.url, m.msgID, m.topicID, m.body, m.title, m.msgTime, m.msgModified, m.reason, m.reasonBy, m.userEmail, IFNULL(u.userID, 0) AS userID, IFNULL(u.userName, m.userName) AS userName, IFNULL(u.avatar, "") AS avatar, (u.last_active >= UNIX_TIMESTAMP() - 300) AS isOnline, t.locked, t.sticky, t.numReplies
FROM '. $this->table() .' AS m
LEFT JOIN '. self::$_prefix .'user AS u ON (u.userID = m.userID)
LEFT JOIN '. self::$_prefix .'topic AS t ON (t.topicID = m.topicID)
LEFT JOIN '. $this->table() .' AS fm ON (fm.msgID = t.fmsgID)
WHERE m.topicID = :topic
ORDER BY msgID ASC
LIMIT :start, :limit', $params);
foreach ($result as $k => $v)
$data[$v['msgID']] = $f3->get('Tools')->prepareData($v, $page);
return $data;
}
function userMessages($params = [], $ttl = 300)
{
$f3 = \Base::instance();
$data = [];
$data = $this->db->exec('
SELECT fm.url, t.locked, t.sticky, t.lmsgID, t.numReplies, m.msgID, m.topicID, m.msgTime, m.title, m.tags, m.boardID, b.title AS boardTitle, b.url AS boardUrl, m.userEmail, IFNULL(u.userID, 0) AS userID, IFNULL(u.userName, m.userName) AS userName, IFNULL(u.avatar, "") AS avatar, (u.last_active >= UNIX_TIMESTAMP() - 300) AS isOnline
FROM '. $this->table() .' AS fm
LEFT JOIN '. self::$_prefix .'topic AS t ON (t.fmsgID = fm.msgID)
LEFT JOIN '. $this->table() .' AS m ON (m.msgID = t.lmsgID)
LEFT JOIN '. self::$_prefix .'board AS b ON (b.boardID = t.boardID)
LEFT JOIN '. self::$_prefix .'user AS u ON (u.userID = m.userID)
WHERE m.userID = :user
ORDER BY m.msgID DESC
LIMIT :limit', $params, $ttl);
foreach ($data as $k => $r)
$data[$k] = $f3->get('Tools')->prepareData($r);
return $data;
}
function deleteMessages($ids = [])
{
$this->db->exec('DELETE FROM '. $this->table() .' WHERE msgID IN(:ids)', [':ids' => implode(',', $ids)]);
}
function getMessageIDs($id = 0)
{
$ids = [];
$data = $this->db->exec('
SELECT msgID
FROM '. $this->table() .'
WHERE topicID = :topic',[
':topic' => $id,
]);
foreach ($data as $value)
$ids[] = (int) $value['msgID'];
return $ids;
}
function createEntry($params = [])
{
if (empty($params))
return false;
// Make sure we are working with fresh data.
$this->reset();
// Need these. For reasons!
$f3 = \Base::instance();
$topicModel = new \Models\Topic($f3->get('DB'));
$userModel = new \Models\User($f3->get('DB'));
$params = array_merge(self::$rows, $params);
// Clean up the tags.
if (!empty($params['tags']))
$params['tags'] = $f3->get('Tools')->commaSeparated($params['tags']);
$topicParams = array_map(function($var) use($f3){
return $f3->get('Tools')->sanitize($var);
}, array_intersect_key($params, self::$topicRows));
// Be nice.
$params = array_map(function($var) use($f3){
return $f3->get('Tools')->sanitize($var);
}, array_intersect_key($params, self::$rows));
// These pesky fields need to be assigned at this point and place in time!
if (!empty($params['msgID']))
{
$params['msgModified'] = time();
$params['reasonBy'] = $f3->get('currentUser')->userName;
}
$params['msgTime'] = time();
$params['userIP'] = $f3->ip();
// Are we editing?
if (!empty($params['msgID']))
{
$this->load(['msgID = ?', $params['msgID']]);
$params['msgTime'] = $this->msgTime;
}
$this->copyFrom($params);
// Save.
$this->save();
// Is a reply?
if (!empty($params['topicID']))
{
$topicInfo = $topicModel->load(['topicID = ?', $params['topicID']]);
$topicModel->lmsgID = $this->msgID;
}
// No? then create it.
else
{
$topicModel->fmsgID = $this->msgID;
$topicModel->lmsgID = $this->msgID;
$topicModel->boardID = $this->boardID;
$topicModel->solved = 0;
}
$topicModel->copyFrom($topicParams);
// Done.
$topicModel->save();
// Update the number of replies.
$topicModel->updateNumReplies($topicModel->topicID);
// Now that we have the topic ID, create the slug.
$this->url = $f3->get('Tools')->slug($params['title']) .'-'. $topicModel->topicID;
// And update the topic.
$this->topicID = $topicModel->topicID;
// Lastly, if theres an user, update the user table.
if($this->userID)
{
$userModel->load(['userID = ?', $this->userID]);
$userModel->posts++;
$userModel->lmsgID = $this->msgID;
$userModel->save();
}
// Clean up.
$this->save();
$topicModel->reset();
}
}