strlen($_POST['message']) || strlen($_POST['message']) > 65535) { return 'fail'; } $receiver = $_POST['receiver']; $esc_message = mysql_real_escape_string($_POST['message']); $sender = $myUser['username']; if (!validateUsername($receiver) || !queryUser($receiver)) { return 'fail'; } mysql_query("insert into user_msg (sender, receiver, message, send_time) values ('$sender', '$receiver', '$esc_message', now())"); return "ok"; } function getConversations() { global $myUser; $username = $myUser['username']; $result = mysql_query( "select * from user_msg where sender = '$username' or receiver = '$username' order by send_time DESC" ); $ret = array(); while ($msg = mysql_fetch_array($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() { global $myUser; $username = $myUser['username']; if (!isset($_GET['conversationName']) || !validateUsername($_GET['conversationName'])) { return '[]'; } if (!isset($_GET['pageNumber']) || !validateUInt($_GET['pageNumber'])) { return '[]'; } $conversationName = $_GET['conversationName']; $pageNumber = ($_GET['pageNumber'] - 1) * 10; mysql_query("update user_msg set read_time = now() where sender = '$conversationName' and receiver = '$username' and read_time is null"); $result = mysql_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 = mysql_fetch_array($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 = <<

私信