| 1 |
<?php
|
| 2 |
// $Id: chatroomread.php,v 1.29.4.7 2009/10/29 18:04:12 justinrandell Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Process chatroom polling requests.
|
| 7 |
*
|
| 8 |
* This file is a performance hack, aimed at making moderately sized rooms
|
| 9 |
* possible with this module.
|
| 10 |
*
|
| 11 |
* The intent is to handle the polling requests from chatrooms without doing
|
| 12 |
* a full Drupal bootstrap unless absolutely necessary. All simple polling
|
| 13 |
* requests are handled by this script, as they are by far the most common
|
| 14 |
* type of request, and are very easy to check against a cache.
|
| 15 |
*
|
| 16 |
* First, we check to see if the last message seen in a given chat client-side
|
| 17 |
* is older than the latest message in that chat server-side.
|
| 18 |
*
|
| 19 |
* Only if the chat being polled has messages newer than what the requesting
|
| 20 |
* client has seen do we bootstrap Drupal.
|
| 21 |
*/
|
| 22 |
|
| 23 |
// We need the $last_msg_id, $chat_id and $chat_cache_file to check the
|
| 24 |
// cache for this chat.
|
| 25 |
if (!isset($_POST['latest_msg_id']) || !preg_match('/^\d+$/', $_POST['latest_msg_id'])) {
|
| 26 |
exit;
|
| 27 |
}
|
| 28 |
$client_latest_msg_id = $_POST['latest_msg_id'];
|
| 29 |
|
| 30 |
if (!isset($_POST['chat_id']) || !preg_match('/^\d+$/', $_POST['chat_id'])) {
|
| 31 |
exit;
|
| 32 |
}
|
| 33 |
$chat_id = $_POST['chat_id'];
|
| 34 |
|
| 35 |
if (!isset($_POST['chat_cache_directory']) || !is_dir($_POST['chat_cache_directory'])) {
|
| 36 |
exit;
|
| 37 |
}
|
| 38 |
$chat_cache_file = $_POST['chat_cache_directory'] . '/chatroom.chat.' . $chat_id . '.cache';
|
| 39 |
|
| 40 |
if (!isset($_POST['skip_cache'])) {
|
| 41 |
exit;
|
| 42 |
}
|
| 43 |
$skip_cache = $_POST['skip_cache'] == 1 ? TRUE : FALSE;
|
| 44 |
|
| 45 |
// We let the client signal that we should skip the cache. Right now we're
|
| 46 |
// using this to make sure user's last-seen time is updated, and there may
|
| 47 |
// be more uses for it down the track.
|
| 48 |
if (!$skip_cache && file_exists($chat_cache_file)) {
|
| 49 |
|
| 50 |
// Do a quick DoS check - we don't validate the path, so we have to make
|
| 51 |
// sure we're not reading arbitrarily big files into memory. Our cache file
|
| 52 |
// should contain a single numeric id. So, if the file is bigger than 25
|
| 53 |
// bytes, something is fishy, and we should just bail out.
|
| 54 |
$file_stats = stat($chat_cache_file);
|
| 55 |
if ($file_stats['size'] > 25) {
|
| 56 |
exit;
|
| 57 |
}
|
| 58 |
|
| 59 |
$server_latest_msg_id = trim(file_get_contents($chat_cache_file));
|
| 60 |
if ($server_latest_msg_id == $client_latest_msg_id) {
|
| 61 |
print json_encode(array('data' => array('cacheHit' => 1, 'messages' => array())));
|
| 62 |
exit;
|
| 63 |
}
|
| 64 |
}
|
| 65 |
|
| 66 |
// Make this look like a normal request to Drupal, then execute index.php.
|
| 67 |
$_GET['q'] = "chatroom/chat/get/latest/messages/$chat_id/$client_latest_msg_id";
|
| 68 |
require_once './index.php';
|
| 69 |
|