strlen($_POST['message']) || strlen($_POST['message']) > 65535) { return 'fail'; } $receiver = $_POST['receiver']; $esc_message = DB::escape($_POST['message']); $sender = Auth::id(); if (!validateUsername($receiver) || !UOJUser::query($receiver)) { return 'fail'; } DB::query("insert into user_msg (sender, receiver, message, send_time) values ('$sender', '$receiver', '$esc_message', now())"); return "ok"; } function getConversations() { $username = Auth::id(); $result = DB::query("select * from user_msg where sender = '$username' or receiver = '$username' order by send_time DESC"); $ret = array(); while ($msg = DB::fetch($result)) { if ($msg['sender'] !== $username) { if (isset($ret[$msg['sender']])) { $ret[$msg['sender']][1] |= ($msg['read_time'] == null); continue; } $ret[$msg['sender']] = array($msg['send_time'], ($msg['read_time'] == null)); } else { if (isset($ret[$msg['receiver']])) { continue; } $ret[$msg['receiver']] = array($msg['send_time'], 0); } } $res = []; foreach ($ret as $name => $con) { $res[] = [$con[0], $con[1], $name]; } usort($res, function ($a, $b) { return -strcmp($a[0], $b[0]); }); return json_encode($res); } function getHistory() { $username = Auth::id(); if (!isset($_GET['conversationName']) || !validateUsername($_GET['conversationName'])) { return '[]'; } if (!isset($_GET['pageNumber']) || !validateUInt($_GET['pageNumber'])) { return '[]'; } $conversationName = $_GET['conversationName']; $pageNumber = ($_GET['pageNumber'] - 1) * 10; DB::query("update user_msg set read_time = now() where sender = '$conversationName' and receiver = '$username' and read_time is null"); $result = DB::query("select * from user_msg where (sender = '$username' and receiver = '$conversationName') or (sender = '$conversationName' and receiver = '$username') order by send_time DESC limit $pageNumber, 11"); $ret = array(); while ($msg = DB::fetch($result)) { $ret[] = array($msg['message'], $msg['send_time'], $msg['read_time'], $msg['id'], ($msg['sender'] == $username)); } return json_encode($ret); } /* function deleteMsg($msgId) { return 1; $str = <<

私信