-
Notifications
You must be signed in to change notification settings - Fork 1
/
messages.php
285 lines (284 loc) · 12.9 KB
/
messages.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
<?php
define('HEADER_TEXT', 'Messaging');
require_once __DIR__ . '/includes/globals.php';
if(!$site->checkEnabled('messaging'))
$mtg->error("The messaging ability has been disabled");
$users->checkBan('messages');
require_once __DIR__ . '/includes/class/jbbcode/Parser.php';
$parser = new JBBCode\Parser();
$parser->addCodeDefinitionSet(new JBBCode\DefaultCodeDefinitionSet());
require_once __DIR__ . '/includes/class/class_mtg_paginate.php';
$pages = new Paginator();
require_once __DIR__ . '/includes/securimage/securimage.php';
$securimage = new Securimage();
$_GET['ID'] = isset($_GET['ID']) && ctype_digit($_GET['ID']) ? $_GET['ID'] : null;
$_GET['action'] = isset($_GET['action']) && ctype_alpha($_GET['action']) ? strtolower(trim($_GET['action'])) : null;
$read = [
1 => '<span class="small green">Read</span>',
0 => '<span class="small red">Unread</span>'
];
?><table width="100%" class="pure-table">
<tr>
<th width="33%"><a href="messages.php">Inbox</a></th>
<th width="34%"><a href="messages.php?action=write">Write</a></th>
<th width="33%"><a href="messages.php?action=archive">Archive</a></th>
</tr>
</table><?php
switch($_GET['action']) {
case 'write':
if(!array_key_exists('submit', $_POST)) {
?><h3 class="content-subhead">Message - Compose</h3>
<form action="messages.php?action=write" method="post" class="pure-form pure-form-aligned">
<div class="pure-control-group">
<label for="recipients">Enter player IDs, separated by a comma</label>
<input type="text" class="pure-input-1-2" name="user2" value="<?php echo isset($_GET['player']) && ctype_digit($_GET['player']) ? $_GET['player'] : null;?>" placeholder="Example: 1,2,3" />
</div>
<div class="pure-control-group">
<label for="subject">Subject</label>
<input type="text" class="pure-input-1-2" name="subject" placeholder="Example: Hi there!" />
</div>
<div class="pure-control-group">
<label for="message">Message</label>
<textarea rows="7" cols="50" name="message" class="pure-input-1-2"><?php echo isset($_GET['msg']) ? urldecode($_GET['msg']) : null;?></textarea>
</div><?php
if($set['captcha_messages']) {
?><div class="pure-control-group">
<label for="image">Captcha Image</label>
<img id="captcha" src="/includes/securimage/securimage_show.php" alt="CAPTCHA Image" />
</div>
<div class="pure-control-group">
<label for="code">Captcha Code</label>
<input type="text" name="captcha_code" size="10" maxlength="6" class="pure-u-1-3" required />
<a href="#" onclick="document.getElementById('captcha').src = '/includes/securimage/securimage_show.php?' + Math.random(); return false">[ Different Image ]</a>
</div><?php
}
?><div class="pure-controls">
<button type="submit" name="submit" value="true" class="pure-button pure-button-primary"><i class="fa fa-envelope"></i> Send Message</button>
<button type="reset" class="pure-button pure-button-secondary"><i class="fa fa-recycle"></i> Reset</button>
</div>
</form><?php
if($users->exists($_GET['ID'])) {
?><table width="100%" class="pure-table">
<tr>
<th colspan="2">The 5 most recent messages between you and <?php echo $users->name($_GET['ID']);?></th>
</tr><?php
$db->query('SELECT `time_sent`, `message`, `sender` FROM `users_messages` WHERE (`sender` = ? AND `receiver` = ?) OR (`receiver` = ? AND `sender` = ?) ORDER BY `time_sent` DESC LIMIT 5');
$db->execute([$my['id'], $_GET['ID'], $_GET['ID'], $my['id']]);
if(!$db->num_rows())
echo '<tr><td colspan="2" class="center">You haven\'t spoken to '.$users->name($_GET['ID']).' yet</td></tr>';
else {
$rows = $db->fetch_row();
foreach($rows as $r) {
$parser->parse($mtg->format($r['message'], true));
?><tr>
<td width="25%" valign="top">
<strong><?php echo $_GET['ID'] == $r['sender'] ? $users->name($_GET['ID']) : 'You';?> wrote:</strong><br />
<span class="small"><?php echo date('F j, Y, g:i:s a', strtotime($r['time_sent']));?></span>
</td>
<td valign="top"><?php echo str_replace('[username]', $users->name($my['id']), $parser->getAsHTML());?></td>
</tr><?php
}
}
?></table><?php
}
} else {
if($set['captcha_messages']) {
$_POST['captcha_code'] = array_key_exists('captcha_code', $_POST) && ctype_digit($_POST['captcha_code']) && strlen($_POST['captcha_code']) == 6 ? $_POST['captcha_code'] : null;
if($securimage->check($_POST['captcha_code']) == false)
$mtg->error('You didn\'t enter a valid code');
}
$subj = array_key_exists('subject', $_POST) && is_string($_POST['subject']) ? trim(strip_tags($_POST['subject'])) : 'n/a';
if(strlen($subj) > 50)
$mtg->error('Subjects are limited to 50 characters');
$msg = array_key_exists('message', $_POST) && is_string($_POST['message']) ? trim(strip_tags($_POST['message'])) : null;
if(empty($msg))
$mtg->error('You must enter a message');
if(strlen($msg) > 65536)
$mtg->error('Messages are limited to 65,536 characters');
$_POST['user1'] = array_key_exists('user1', $_POST) && ctype_digit($_POST['user1']) ? $_POST['user1'] : null;
$_POST['user2'] = array_key_exists('user2', $_POST) && is_string($_POST['user2']) ? str_replace(' ', '', trim($_POST['user2'])) : null;
if(empty($_POST['user1']) && empty($_POST['user2']))
$mtg->error('You must select at least 1 option as a recipient');
if(!empty($_POST['user1']) && !empty($_POST['user2']))
$mtg->error('You must select only 1 option as a recipient');
$sendto = empty($_POST['user1']) ? $_POST['user2'] : $_POST['user1'];
$sentTo = '';
$unique = array_unique(array_values(array_filter(explode(',', $sendto))));
if(count(array_keys($unique)) > 10)
$mtg->error('You can\'t send the same message to more than 10 people at once');
$ids = [];
$db->query("SELECT `id` FROM `users` ORDER BY `id` ASC");
$db->execute();
$rows = $db->fetch_row();
foreach($rows as $row)
$ids[] .= implode(',', $row);
$uni = array_intersect($ids, $unique);
$count = count(array_keys($uni));
if(!$count)
$mtg->error('No players were found');
$msg = $count > 1 ? $msg . "\r\n\r\nMessage sent to: " . messageUsernames($uni) : $msg;
$db->startTrans();
foreach($uni as $to) {
$db->query('SELECT `message` FROM `users_messages` WHERE `sender` = ? AND `receiver` = ? AND `read` = 0 ORDER BY `time_sent` DESC LIMIT 1');
$db->execute([$my['id'], $to]);
if($db->num_rows())
if($msg == $db->fetch_single())
$mtg->error('Double submission detected');
$sentTo .= $users->name($to) . ', ';
$users->send_message($to, $my['id'], $subj, $msg);
}
$db->endTrans();
$mtg->success('Your message has been sent to ' . substr($sentTo, 0, -2));
}
break;
case 'read':
?><h3 class="content-subhead">Message - Read Message</h3><?php
if(empty($_GET['ID']))
$mtg->error('You didn\'t select a valid message');
$db->query("SELECT `receiver`, `sender` FROM `users_messages` WHERE `id` = ?");
$db->execute([$_GET['ID']]);
if(!$db->num_rows())
$mtg->error('That message doesn\'t exist');
$msg = $db->fetch_row(true);
if($msg['receiver'] != $my['id'])
$mtg->error('That\'s not your message to read');
$db->query('UPDATE `users_messages` SET `read` = 1 WHERE `receiver` = ? AND `sender` = ?');
$db->execute([$my['id'], $msg['sender']]);
$db->query('SELECT * FROM `users_messages` WHERE (`receiver` = ? AND `sender` = ?) OR (`receiver` = ? AND `sender` = ?) ORDER BY `time_sent` DESC LIMIT 20');
$db->execute([$my['id'], $msg['sender'], $msg['sender'], $my['id']]);
?><table width="100%" class="pure-table pure-table-striped">
<tr>
<th width="30%">Details</th>
<th width="70%">Message</th>
</tr><?php
if(!$db->num_rows())
echo '<tr><td colspan="2" class="center">No conversation was found between '.$users->name($msg['sender']).' and yourself</td></tr>';
else {
$rows = $db->fetch_row();
foreach($rows as $row) {
$parser->parse($mtg->format($row['message'], true));
?><tr>
<td>
<strong>Sender:</strong> <?php echo $users->name($row['sender'], true, true);?><br />
<strong>Subject:</strong> <?php echo $mtg->format($row['subject']);?><br />
<strong>Sent:</strong> <?php echo date('F j, Y, g:i:sa', strtotime($row['time_sent']));?><br />
<strong>Status:</strong> <?php echo $read[$row['read']];?>
</td>
<td><?php echo str_replace(['[username]', '{username}'], $users->name($my['id']), $parser->getAsHTML());?></td>
</tr><?php
}
}
?></table><?php
break;
case 'delete':
?><h3 class="content-subhead">Message - Delete Message</h3><?php
if(empty($_GET['ID']))
$mtg->error('Invalid ID.');
$db->query('SELECT `receiver` FROM `users_messages` WHERE `id` = ?');
$db->execute([$_GET['ID']]);
if(!$db->num_rows())
$mtg->error('That message doesn\'t exist');
if($db->fetch_single() != $my['id'])
$mtg->error('That message isn\'t addressed to you');
$db->query('UPDATE `users_messages` SET `deleted` = 1 WHERE `id` = ?');
$db->execute([$_GET['ID']]);
$mtg->success('Your message has been sent to your archive');
break;
case 'archive':
?><h3 class="content-subhead">Message - Archived Messages</h3><?php
$db->query('SELECT COUNT(`id`) FROM `users_messages` WHERE `deleted` = 1 AND `receiver` = ?');
$db->execute([$my['id']]);
$cnt = $db->fetch_single();
if(!$cnt)
$mtg->error('You have no archived messages');
$pages->items_total = $cnt;
$pages->paginate();
$db->query('SELECT `id`, `sender`, `time_sent`, `subject`, `message` FROM `users_messages` WHERE `deleted` = 1 AND `receiver` = ? ORDER BY `id` DESC '.$pages->limit);
$db->execute([$my['id']]);
?><h4>Your archive</h4>
<p class="paginate"><?php echo $pages->display_pages();?></p><br />
<table class="pure-table pure-table-striped" width="100%">
<thead>
<tr>
<th width="25%">Details</th>
<th width="75%">Message</th>
</tr>
</thead><?php
if(!$db->num_rows())
echo '<tr><td colspan="2" class="center">You have no archived messages</td></tr>';
else {
$rows = $db->fetch_row();
foreach($rows as $row) {
$parser->parse($mtg->format($row['message'], true));
?><tr>
<td>
<strong>From:</strong> <?php echo $users->name($row['sender'], true);?><br />
<strong>Sent:</strong> <?php echo date('H:i:s d/m/Y', strtotime($row['time_sent']));?><br />
<strong>Subject:</strong> <?php echo $mtg->format($row['subject']);?><br /><br />
<a href="messages.php?action=restore&ID=<?php echo $row['id'];?>">Move to Inbox</a>
</td>
<td><?php echo $parser->getAsHTML();?></td>
</tr><?php
}
}
?></table><br />
<p class="paginate"><?php echo $pages->display_pages();?></p><?php
break;
case 'restore':
?><h3 class="content-subhead">Message - Restore Message</h3><?php
if(empty($_GET['ID']))
$mtg->error('You didn\'t select a valid message');
$db->query('SELECT `receiver`, `deleted` FROM `users_messages` WHERE `id` = ?');
$db->execute([$_GET['ID']]);
if(!$db->num_rows())
$mtg->error('That message doesn\'t exist');
$row = $db->fetch_row(true);
if($row['receiver'] != $my['id'])
$mtg->error('That message isn\'t yours');
if(!$row['deleted'])
$mtg->error('That message isn\'t marked as deleted');
$db->query('UPDATE `users_messages` SET `deleted` = 0 WHERE `id` = ?');
$db->execute([$_GET['ID']]);
$mtg->success('The message has been moved back to your Inbox');
break;
default:
?><h3 class="content-subhead">Messages</h3><?php
?><table width="100%" class="pure-table pure-table-striped">
<tr>
<th width="20%">Conversation</th>
<th width="60%">Most Recent Message Received</th>
<th width="20%">Actions</th>
</tr><?php
$db->query('SELECT * FROM ( ' .
'SELECT `id`, `sender`, `time_sent`, `message`, `read` FROM `users_messages` WHERE `receiver` = ? AND `deleted` = 0 ORDER BY `time_sent` DESC LIMIT 20) AS `conf` ' .
'GROUP BY `sender` ORDER BY `id` DESC');
$db->execute([$my['id']]);
if(!$db->num_rows())
echo '<tr><td colspan="3" class="center">You have no messages</td></tr>';
else {
$rows = $db->fetch_row();
foreach($rows as $row) {
$parser->parse($mtg->format($row['message'], true));
?><tr>
<td><?php echo $users->name($row['sender'], true);?></td>
<td><?php echo str_replace(['[username]', '{username}'], $users->name($my['id']), $parser->getAsHTML()),'<br /><br /><span class="small">',date('F j, Y, g:i:s a', strtotime($row['time_sent'])),'</span> - '.$read[$row['read']];?></td>
<td>
<a href="messages.php?action=read&ID=<?php echo $row['id'];?>">Read</a> ·
<a href="messages.php?action=write&player=<?php echo $row['sender'];?>">Respond</a> ·
<a href="messages.php?action=delete&ID=<?php echo $row['id'];?>">Archive</a>
</td>
</tr><?php
}
}
?></table><?php
break;
}
function messageUsernames(array $array = null) {
global $users;
$ret = '';
if(!count($array))
return null;
foreach($array as $id)
$ret .= $users->name($id, false, true).', ';
return substr($ret, 0, -2);
}