1. Code restructuring

2. Small logic fixes (e.g. only delete chat_user records WHERE type='sockets')
3. More informative messages when rejecting connections for some reason
4. Better handling of updating chat_user records (lastping updated correctly)
5. Custom error handling, so that PHP errors show up in the trace()s
This commit is contained in:
defacer
2004-08-26 08:48:15 +00:00
parent d1dec2d112
commit ebc9be85c7
+76 -74
View File
@@ -1,37 +1,50 @@
#!/usr/bin/php -q
<?php
// Browser quirks
define('QUIRK_CHUNK_UPDATE', 0x0001);
echo "Moodle chat daemon v1.0 on PHP ".phpversion()." (\$Id$)\n\n";
// Connection telltale
define('CHAT_CONNECTION', 0x10);
// Connections: Incrementing sequence, 0x10 to 0x1f
define('CHAT_CONNECTION_CHANNEL', 0x11);
// Sidekick telltale
define('CHAT_SIDEKICK', 0x20);
// Sidekicks: Incrementing sequence, 0x21 to 0x2f
define('CHAT_SIDEKICK_USERS', 0x21);
define('CHAT_SIDEKICK_MESSAGE', 0x22);
define('CHAT_SIDEKICK_BEEP', 0x23);
$phpversion = phpversion();
echo 'Moodle chat daemon v1.0 on PHP '.$phpversion." (\$Id$)\n\n";
/// Set up all the variables we need /////////////////////////////////////
/// $CFG variables are now defined in database by chat/lib.php
$_SERVER['PHP_SELF'] = "dummy";
$_SERVER['SERVER_NAME'] = "dummy";
$_SERVER['PHP_SELF'] = 'dummy';
$_SERVER['SERVER_NAME'] = 'dummy';
include('../../config.php');
include('lib.php');
$_SERVER['SERVER_NAME'] = $CFG->chat_serverhost;
$_SERVER['PHP_SELF'] = "http://$CFG->chat_serverhost:$CFG->chat_serverport/mod/chat/chatd.php";
$_SERVER['PHP_SELF'] = "http://$CFG->chat_serverhost:$CFG->chat_serverport/mod/chat/chatd.php";
$safemode = ini_get('safe_mode');
if($phpversion < '4.3') {
die("Error: The Moodle chat daemon requires at least PHP version 4.3 to run.\n Since your version is $phpversion, you have to upgrade.\n\n");
}
if(!empty($safemode)) {
die("Error: Cannot run with PHP safe_mode = On. Turn off safe_mode.\n");
die("Error: Cannot run with PHP safe_mode = On. Turn off safe_mode in php.ini.\n");
}
@set_time_limit (0);
set_magic_quotes_runtime(0);
error_reporting(E_ALL);
/// Clear the decks of old stuff
delete_records('chat_users');
function chat_empty_connection() {
return array('sid' => NULL, 'handle' => NULL, 'ip' => NULL, 'port' => NULL, 'groupid' => NULL);
}
@@ -64,7 +77,7 @@ class ChatDaemon {
var $_logfile_name = 'chatd.log';
var $_last_idle_poll = 0;
var $conn_ufo = array(); // Connections not identified yet
var $conn_ufo = array(); // Connections not identified yet
var $conn_side = array(); // Sessions with sidekicks waiting for the main connection to be processed
var $conn_half = array(); // Sessions that have valid connections but not all of them
var $conn_sets = array(); // Sessions with complete connection sets sets
@@ -85,6 +98,12 @@ class ChatDaemon {
}
}
function error_handler ($errno, $errmsg, $filename, $linenum, $vars) {
echo $errno;
$this->trace($errmsg.' on line '.$linenum, $errno);
return true;
}
function poll_idle_chats($now) {
$this->trace('Polling chats to detect disconnected users');
if(!empty($this->chatrooms)) {
@@ -116,6 +135,8 @@ class ChatDaemon {
switch($level) {
case E_USER_WARNING: $severity = '*IMPORTANT* '; break;
case E_USER_ERROR: $severity = ' *CRITICAL* '; break;
case E_NOTICE:
case E_WARNING: $severity = ' *CRITICAL* [php] '; break;
}
$date = date('[Y-m-d H:i:s] ');
@@ -167,27 +188,20 @@ class ChatDaemon {
// return false;
}
function update_lastmessageping($sessionid, $time = NULL) {
function user_lazy_update($sessionid) {
// TODO: this can and should be written as a single UPDATE query
if(empty($this->sets_info[$sessionid])) {
$this->trace('update_lastmessageping() called for an invalid SID: '.$sessionid, E_USER_WARNING);
$this->trace('user_lazy_update() called for an invalid SID: '.$sessionid, E_USER_WARNING);
return false;
}
if(empty($time)) {
$time = time();
}
$now = time();
// We 'll be cheating a little, and NOT updating lastmessageping
// as often as we have to, so we can save on DB queries (imagine MANY users)
$this->sets_info[$sessionid]['chatuser']->lastmessageping = $time;
$this->sets_info[$sessionid]['chatuser']->lastping = $time;
// This will set it just fine for bookkeeping purposes.
if($time - $this->sets_info[$sessionid]['lastinfocommit'] > $this->_freq_update_records) {
// We 'll be cheating a little, and NOT updating the record data as
// often as we can, so that we save on DB queries (imagine MANY users)
if($now - $this->sets_info[$sessionid]['lastinfocommit'] > $this->_freq_update_records) {
// commit to permanent storage
// $this->trace('Committing volatile lastmessageping for session '.$sessionid);
$this->sets_info[$sessionid]['lastinfocommit'] = $time;
$this->sets_info[$sessionid]['lastinfocommit'] = $now;
update_record('chat_users', $this->sets_info[$sessionid]['chatuser']);
}
return true;
@@ -309,7 +323,9 @@ class ChatDaemon {
$this->message_broadcast($msg, $this->sets_info[$sessionid]['user']);
// Update that user's lastmessageping
$this->update_lastmessageping($sessionid, $msg->timestamp);
$this->sets_info[$sessionid]['chatuser']->lastping = $msg->timestamp;
$this->sets_info[$sessionid]['chatuser']->lastmessageping = $msg->timestamp;
$this->user_lazy_update($sessionid);
// We did our work, but before slamming the door on the poor browser
// show the courtesy of responding to the HTTP request. Otherwise, some
@@ -355,20 +371,12 @@ class ChatDaemon {
$this->trace('writing users http response to handle '.$handle);
$this->write_data($handle, $header . $content);
/*
$header = "HTTP/1.1 200 OK\n";
$header .= "Connection: close\n";
$header .= "Date: ".date('r')."\n";
$header .= "Server: Moodle\n";
$header .= "Content-Type: text/html\n";
$header .= "Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT\n";
$header .= "Cache-Control: no-cache, must-revalidate\n";
$header .= "Expires: Wed, 4 Oct 1978 09:32:45 GMT\n";
$header .= "\n";
$this->trace('writing users http response to handle '.$handle);
$this->write_data($handle, $header);
*/
// Update that user's lastping
$this->sets_info[$sessionid]['chatuser']->lastping = time();
$this->user_lazy_update($sessionid);
break;
case CHAT_SIDEKICK_MESSAGE:
// Incoming message
@@ -405,7 +413,9 @@ class ChatDaemon {
$this->message_broadcast($msg, $this->sets_info[$sessionid]['user']);
// Update that user's lastmessageping
$this->update_lastmessageping($sessionid, $msg->timestamp);
$this->sets_info[$sessionid]['chatuser']->lastping = $msg->timestamp;
$this->sets_info[$sessionid]['chatuser']->lastmessageping = $msg->timestamp;
$this->user_lazy_update($sessionid);
// We did our work, but before slamming the door on the poor browser
// show the courtesy of responding to the HTTP request. Otherwise, some
@@ -454,7 +464,7 @@ class ChatDaemon {
return false;
}
$course = get_record('course', 'id', $chat->course); {
if($course === false) {
if($course === false) {
$this->dismiss_half($sessionid);
return false;
}
@@ -556,7 +566,7 @@ class ChatDaemon {
if(isset($this->conn_sets[$sessionid])) {
// Yes, so regrettably we cannot promote you
$this->trace('Connection rejected: session '.$sessionid.' is already final');
$this->dismiss_ufo($handle);
$this->dismiss_ufo($handle, true, 'Your SID was rejected.');
return false;
}
@@ -610,7 +620,7 @@ class ChatDaemon {
}
function dismiss_ufo($handle, $disconnect = true) {
function dismiss_ufo($handle, $disconnect = true, $message = NULL) {
if(empty($this->conn_ufo)) {
return false;
}
@@ -618,7 +628,9 @@ class ChatDaemon {
if($ufo->handle == $handle) {
unset($this->conn_ufo[$id]);
if($disconnect) {
$this->write_data($handle, "You don't seem to be a valid client.\n");
if(!empty($message)) {
$this->write_data($handle, $message."\n\n");
}
socket_shutdown($handle);
socket_close($handle);
}
@@ -629,7 +641,13 @@ class ChatDaemon {
}
function conn_accept() {
$handle = @socket_accept($this->listen_socket);
$read_socket = array($this->listen_socket);
$changed = socket_select($read_socket, $write = NULL, $except = NULL, 0, 0);
if(!$changed) {
return false;
}
$handle = socket_accept($this->listen_socket);
if(!$handle) {
return false;
}
@@ -677,7 +695,7 @@ class ChatDaemon {
{
// Simply give them the message
$output = chat_format_message_manually($message, $this->sets_info[$sessionid]['courseid'], $sender, $info['user'], $info['lang']);
$output = chat_format_message_manually($message, $info['courseid'], $sender, $info['user'], $info['lang']);
$this->trace('Delivering message "'.$output->text.'" to '.$this->conn_sets[$sessionid][CHAT_CONNECTION_CHANNEL]);
if($output->beep) {
@@ -763,7 +781,7 @@ class ChatDaemon {
// [pj]: I really must have a good read on sockets. What exactly does this do?
// http://www.unixguide.net/network/socketfaq/4.5.shtml is still not enlightening enough for me.
socket_set_option($this->listen_socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_setopt($this->listen_socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_set_nonblock($this->listen_socket);
}
@@ -803,20 +821,8 @@ class ChatDaemon {
}
// Connection telltale
define('CHAT_CONNECTION', 0x10);
// Connections: Incrementing sequence, 0x10 to 0x1f
define('CHAT_CONNECTION_CHANNEL', 0x11);
// Sidekick telltale
define('CHAT_SIDEKICK', 0x20);
// Sidekicks: Incrementing sequence, 0x21 to 0x2f
define('CHAT_SIDEKICK_USERS', 0x21);
define('CHAT_SIDEKICK_MESSAGE', 0x22);
define('CHAT_SIDEKICK_BEEP', 0x23);
$DAEMON = New ChatDaemon;
set_error_handler(array($DAEMON, 'error_handler'));
/// Check the parameters //////////////////////////////////////////////////////
@@ -875,17 +881,10 @@ if(!$DAEMON->query_start()) {
die();
}
if (!function_exists('socket_set_option')) {
// PHP < 4.3
if (!function_exists('socket_setopt')) {
// No socket_setopt!
echo "Error: Neither socket_setopt() nor socket_set_option() exists.\n";
echo "Possibly PHP has not been compiled with --enable-sockets.\n\n";
die();
}
function socket_set_option($socket, $level, $name, $val) {
return socket_setopt($socket, $level, $name, $val);
}
if (!function_exists('socket_setopt')) {
echo "Error: Function socket_setopt() does not exist.\n";
echo "Possibly PHP has not been compiled with --enable-sockets.\n\n";
die();
}
$DAEMON->init_sockets();
@@ -942,6 +941,9 @@ else {
$DAEMON->trace('Started Moodle chatd on port '.$CFG->chat_serverport.', listening socket '.$DAEMON->listen_socket, E_USER_WARNING);
/// Clear the decks of old stuff
delete_records('chat_users', 'version', 'sockets');
while(true) {
$active = array();
@@ -962,7 +964,7 @@ while(true) {
if(!ereg('win=(chat|users|message|beep).*&chat_sid=([a-zA-Z0-9]*)&groupid=([0-9]*) HTTP', $data, $info)) {
// Malformed data
$DAEMON->trace('UFO with '.$handle.': Request with malformed data; connection closed', E_USER_WARNING);
$DAEMON->dismiss_ufo($handle);
$DAEMON->dismiss_ufo($handle, true, 'Request with malformed data; connection closed');
continue;
}
@@ -988,7 +990,7 @@ while(true) {
$type = CHAT_SIDEKICK_BEEP;
if(!ereg('beep=([^&]*)[& ]', $data, $info)) {
$DAEMON->trace('Beep sidekick did not contain a valid userid', E_USER_WARNING);
$DAEMON->dismiss_ufo($handle);
$DAEMON->dismiss_ufo($handle, true, 'Request with malformed data; connection closed');
continue;
}
else {
@@ -999,7 +1001,7 @@ while(true) {
$type = CHAT_SIDEKICK_MESSAGE;
if(!ereg('chat_message=([^&]*)[& ]chat_msgidnr=([^&]*)[& ]', $data, $info)) {
$DAEMON->trace('Message sidekick did not contain a valid message', E_USER_WARNING);
$DAEMON->dismiss_ufo($handle);
$DAEMON->dismiss_ufo($handle, true, 'Request with malformed data; connection closed');
continue;
}
else {
@@ -1008,7 +1010,7 @@ while(true) {
break;
default:
$DAEMON->trace('UFO with '.$handle.': Request with unknown type; connection closed', E_USER_WARNING);
$DAEMON->dismiss_ufo($handle);
$DAEMON->dismiss_ufo($handle, true, 'Request with unknown type; connection closed');
continue;
break;
}