2004-08-23 08:48:57 +00:00
|
|
|
#!/usr/bin/php -q
|
2004-07-30 15:32:36 +00:00
|
|
|
<?php
|
|
|
|
|
2004-08-23 15:25:18 +00:00
|
|
|
define('QUIRK_CHUNK_UPDATE', 0x0001);
|
|
|
|
|
2004-08-14 12:37:39 +00:00
|
|
|
echo "Moodle chat daemon v1.0 (\$Id$)\n\n";
|
2004-07-30 15:32:36 +00:00
|
|
|
|
|
|
|
/// 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";
|
|
|
|
|
|
|
|
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";
|
|
|
|
|
|
|
|
$safemode = ini_get('safe_mode');
|
|
|
|
|
|
|
|
if(!empty($safemode)) {
|
|
|
|
die("Error: Cannot run with PHP safe_mode = On. Turn off safe_mode.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
@set_time_limit (0);
|
|
|
|
set_magic_quotes_runtime(0);
|
|
|
|
|
|
|
|
error_reporting(E_ALL);
|
|
|
|
|
|
|
|
function chat_empty_connection() {
|
|
|
|
return array('sid' => NULL, 'handle' => NULL, 'ip' => NULL, 'port' => NULL, 'groupid' => NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
class ChatConnection {
|
2004-08-12 12:31:38 +00:00
|
|
|
// Chat-related info
|
|
|
|
var $sid = NULL;
|
|
|
|
var $type = NULL;
|
|
|
|
//var $groupid = NULL;
|
2004-07-30 15:32:36 +00:00
|
|
|
|
2004-08-12 12:31:38 +00:00
|
|
|
// PHP-level info
|
|
|
|
var $handle = NULL;
|
2004-07-30 15:32:36 +00:00
|
|
|
|
2004-08-12 12:31:38 +00:00
|
|
|
// TCP/IP
|
|
|
|
var $ip = NULL;
|
|
|
|
var $port = NULL;
|
2004-07-30 15:32:36 +00:00
|
|
|
|
2004-08-12 12:31:38 +00:00
|
|
|
function ChatConnection($resource) {
|
|
|
|
$this->handle = $resource;
|
|
|
|
socket_getpeername($this->handle, &$this->ip, &$this->port);
|
|
|
|
}
|
2004-07-30 15:32:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class ChatDaemon {
|
2004-08-24 12:44:33 +00:00
|
|
|
var $_readytogo = false;
|
|
|
|
var $_logfile = false;
|
|
|
|
var $_trace_to_console = true;
|
|
|
|
var $_trace_to_stdout = true;
|
|
|
|
var $_logfile_name = 'chatd.log';
|
|
|
|
|
2004-07-30 15:32:36 +00:00
|
|
|
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
|
|
|
|
var $sets_info = array(); // Keyed by sessionid exactly like conn_sets, one of these for each of those
|
|
|
|
|
|
|
|
var $message_queue = array(); // Holds messages that we haven't committed to the DB yet
|
|
|
|
|
2004-08-24 12:44:33 +00:00
|
|
|
function ChatDaemon() {
|
|
|
|
// Check the STDOUT constant
|
|
|
|
$this->_trace_to_stdout = defined('STDOUT');
|
|
|
|
$this->_trace_level = E_ALL ^ E_USER_NOTICE;
|
|
|
|
$this->_pcntl_exists = function_exists('pcntl_fork');
|
|
|
|
$this->_time_rest_socket = 20;
|
|
|
|
$this->_beepsoundsrc = $GLOBALS['CFG']->wwwroot.'/mod/chat/beep.wav';
|
|
|
|
$this->_freq_update_records = 15;
|
|
|
|
}
|
|
|
|
|
|
|
|
function query_start() {
|
|
|
|
return $this->_readytogo;
|
|
|
|
}
|
|
|
|
|
|
|
|
function trace($message, $level = E_USER_NOTICE) {
|
|
|
|
$severity = '';
|
|
|
|
|
|
|
|
switch($level) {
|
|
|
|
case E_USER_WARNING: $severity = '*IMPORTANT* '; break;
|
|
|
|
case E_USER_ERROR: $severity = ' *CRITICAL* '; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$date = date('[Y-m-d H:i:s] ');
|
|
|
|
$message = $date.$severity.$message."\n";
|
|
|
|
|
|
|
|
if ($this->_trace_level & $level) {
|
|
|
|
// It is accepted for output
|
|
|
|
|
|
|
|
// Error-class traces go to STDERR too
|
|
|
|
if($level & E_USER_ERROR) {
|
|
|
|
fwrite(STDERR, $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the message to wherever we should
|
|
|
|
if($this->_trace_to_stdout) {
|
|
|
|
fwrite(STDOUT, $message);
|
|
|
|
}
|
|
|
|
if($this->_trace_to_console) {
|
|
|
|
echo $message;
|
|
|
|
flush();
|
|
|
|
}
|
|
|
|
if($this->_logfile) {
|
|
|
|
fwrite($this->_logfile, $message);
|
|
|
|
fflush($this->_logfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-11 14:45:40 +00:00
|
|
|
function update_lastmessageping($sessionid, $time = NULL) {
|
|
|
|
// TODO: this can and should be written as a single UPDATE query
|
|
|
|
if(empty($this->sets_info[$sessionid])) {
|
2004-08-24 12:44:33 +00:00
|
|
|
$this->trace('update_lastmessageping() called for an invalid SID: '.$sessionid, E_USER_WARNING);
|
2004-08-11 14:45:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$now = time();
|
|
|
|
if(empty($time)) {
|
|
|
|
$time = $now;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
2004-08-24 12:44:33 +00:00
|
|
|
$this->sets_info[$sessionid]['chatuser']->lastping = $time;
|
2004-08-11 14:45:40 +00:00
|
|
|
|
|
|
|
// This will set it just fine for bookkeeping purposes.
|
2004-08-24 12:44:33 +00:00
|
|
|
if($now - $this->sets_info[$sessionid]['lastinfocommit'] > $this->_freq_update_records) {
|
2004-08-11 14:45:40 +00:00
|
|
|
// commit to permanent storage
|
2004-08-24 12:44:33 +00:00
|
|
|
// $this->trace('Committing volatile lastmessageping for session '.$sessionid);
|
2004-08-11 14:45:40 +00:00
|
|
|
$this->sets_info[$sessionid]['lastinfocommit'] = $now;
|
|
|
|
update_record('chat_users', $this->sets_info[$sessionid]['chatuser']);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-08-10 13:50:58 +00:00
|
|
|
function get_user_window($sessionid) {
|
2004-08-11 14:45:40 +00:00
|
|
|
|
|
|
|
global $CFG, $THEME;
|
|
|
|
|
|
|
|
static $str;
|
|
|
|
|
|
|
|
$info = &$this->sets_info[$sessionid];
|
|
|
|
$oldlang = chat_language_override($info['lang']);
|
|
|
|
|
|
|
|
$timenow = time();
|
|
|
|
|
|
|
|
if (empty($str)) {
|
|
|
|
$str->idle = get_string("idle", "chat");
|
|
|
|
$str->beep = get_string("beep", "chat");
|
|
|
|
$str->day = get_string("day");
|
|
|
|
$str->days = get_string("days");
|
|
|
|
$str->hour = get_string("hour");
|
|
|
|
$str->hours = get_string("hours");
|
|
|
|
$str->min = get_string("min");
|
|
|
|
$str->mins = get_string("mins");
|
|
|
|
$str->sec = get_string("sec");
|
|
|
|
$str->secs = get_string("secs");
|
|
|
|
}
|
|
|
|
|
2004-08-10 13:50:58 +00:00
|
|
|
ob_start();
|
2004-08-11 14:45:40 +00:00
|
|
|
echo '<html><head>';
|
|
|
|
echo '<script language="JavaScript">';
|
2004-08-23 15:03:26 +00:00
|
|
|
echo "<!-- //hide\n";
|
2004-08-11 14:45:40 +00:00
|
|
|
|
|
|
|
echo 'function openpopup(url,name,options,fullscreen) {';
|
|
|
|
echo 'fullurl = "'.$CFG->wwwroot.'" + url;';
|
|
|
|
echo 'windowobj = window.open(fullurl,name,options);';
|
|
|
|
echo 'if (fullscreen) {';
|
|
|
|
echo ' windowobj.moveTo(0,0);';
|
|
|
|
echo ' windowobj.resizeTo(screen.availWidth,screen.availHeight); ';
|
|
|
|
echo '}';
|
|
|
|
echo 'windowobj.focus();';
|
|
|
|
echo 'return false;';
|
2004-08-23 15:03:26 +00:00
|
|
|
echo "}\n-->\n";
|
2004-08-11 14:45:40 +00:00
|
|
|
echo '</script></head><body style="font-face: serif;" bgcolor="'.$THEME->body.'">';
|
|
|
|
|
|
|
|
echo '<table style="width: 100%;"><tbody>';
|
|
|
|
if(empty($this->sets_info)) {
|
|
|
|
// No users
|
|
|
|
echo '<tr><td> </td></tr>';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
foreach ($this->sets_info as $usersid => $userinfo) {
|
|
|
|
$lastping = $timenow - $userinfo['chatuser']->lastmessageping;
|
2004-08-14 16:45:13 +00:00
|
|
|
$popuppar = '\'/user/view.php?id='.$userinfo['user']->id.'&course='.$userinfo['courseid'].'\',\'user'.$userinfo['chatuser']->id.'\',\'\'';
|
2004-08-11 14:45:40 +00:00
|
|
|
echo '<tr><td width="35">';
|
2004-08-14 16:45:13 +00:00
|
|
|
echo '<a target="_new" onclick="return openpopup('.$popuppar.');" href="'.$CFG->wwwroot.'/user/view.php?id='.$userinfo['chatuser']->id.'&course='.$userinfo['courseid'].'">';
|
2004-08-11 14:45:40 +00:00
|
|
|
print_user_picture($userinfo['user']->id, 0, $userinfo['user']->picture, false, false, false);
|
|
|
|
echo "</a></td><td valign=center>";
|
|
|
|
echo "<p><font size=1>";
|
|
|
|
echo fullname($userinfo['user'])."<br />";
|
2004-08-12 12:31:38 +00:00
|
|
|
echo "<font color=\"#888888\">$str->idle: ".format_time($lastping, $str)."</font> ";
|
|
|
|
echo '<a target="empty" href="http://'.$CFG->chat_serverhost.':'.$CFG->chat_serverport.'/?win=beep&beep='.$userinfo['user']->id.
|
|
|
|
'&chat_sid='.$sessionid.'&groupid='.$this->sets_info[$sessionid]['groupid'].'">'.$str->beep."</a>\n";
|
2004-08-11 14:45:40 +00:00
|
|
|
echo "</font></p>";
|
|
|
|
echo "<td></tr>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
echo '</tbody></table>';
|
2004-08-10 13:50:58 +00:00
|
|
|
|
2004-08-23 14:40:53 +00:00
|
|
|
// About 2K of HTML comments to force browsers to render the HTML
|
2004-08-23 15:03:26 +00:00
|
|
|
// echo $GLOBALS['CHAT_DUMMY_DATA'];
|
2004-08-23 14:40:53 +00:00
|
|
|
|
2004-08-10 13:50:58 +00:00
|
|
|
echo "</body>\n</html>\n";
|
2004-08-11 14:45:40 +00:00
|
|
|
|
|
|
|
chat_language_restore($oldlang);
|
2004-08-10 13:50:58 +00:00
|
|
|
return ob_get_clean();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2004-07-30 15:32:36 +00:00
|
|
|
function new_ufo_id() {
|
|
|
|
static $id = 0;
|
|
|
|
if($id++ === 0x1000000) { // Cycling very very slowly to prevent overflow
|
|
|
|
$id = 0;
|
|
|
|
}
|
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
function process_sidekicks($sessionid) {
|
|
|
|
if(empty($this->conn_side[$sessionid])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
foreach($this->conn_side[$sessionid] as $sideid => $sidekick) {
|
2004-08-12 12:31:38 +00:00
|
|
|
// TODO: is this late-dispatch working correctly?
|
2004-07-30 15:32:36 +00:00
|
|
|
$this->dispatch_sidekick($sidekick['handle'], $sidekick['type'], $sessionid, $sidekick['customdata']);
|
|
|
|
unset($this->conn_side[$sessionid][$sideid]);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function dispatch_sidekick($handle, $type, $sessionid, $customdata) {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
switch($type) {
|
2004-08-12 12:31:38 +00:00
|
|
|
case CHAT_SIDEKICK_BEEP:
|
|
|
|
// Incoming beep
|
|
|
|
$msg = &New stdClass;
|
|
|
|
$msg->chatid = $this->sets_info[$sessionid]['chatid'];
|
|
|
|
$msg->userid = $this->sets_info[$sessionid]['userid'];
|
|
|
|
$msg->groupid = $this->sets_info[$sessionid]['groupid'];
|
|
|
|
$msg->system = 0;
|
|
|
|
$msg->message = 'beep '.$customdata['beep'];
|
|
|
|
$msg->timestamp = time();
|
|
|
|
|
|
|
|
// Commit to DB
|
|
|
|
insert_record('chat_messages', $msg);
|
|
|
|
|
|
|
|
// OK, now push it out to all users
|
|
|
|
$this->message_broadcast($msg, $this->sets_info[$sessionid]['user']);
|
|
|
|
|
|
|
|
// Update that user's lastmessageping
|
|
|
|
$this->update_lastmessageping($sessionid, $msg->timestamp);
|
|
|
|
|
|
|
|
// We did our work, but before slamming the door on the poor browser
|
|
|
|
// show the courtesy of responding to the HTTP request. Otherwise, some
|
|
|
|
// browsers decide to get vengeance by flooding us with repeat requests.
|
|
|
|
|
|
|
|
$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";
|
|
|
|
|
|
|
|
// That's enough headers for one lousy dummy response
|
|
|
|
chat_socket_write($handle, $header);
|
|
|
|
// All done
|
|
|
|
break;
|
|
|
|
|
2004-07-30 15:32:36 +00:00
|
|
|
case CHAT_SIDEKICK_USERS:
|
2004-08-23 14:40:53 +00:00
|
|
|
// A request to paint a user window
|
2004-08-10 13:50:58 +00:00
|
|
|
|
|
|
|
$content = $this->get_user_window($sessionid);
|
|
|
|
|
|
|
|
$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 .= "Content-Length: ".strlen($content)."\n";
|
2004-08-11 14:45:40 +00:00
|
|
|
$header .= "Refresh: $CFG->chat_refresh_userlist; URL=http://$CFG->chat_serverhost:$CFG->chat_serverport/?win=users&".
|
2004-08-10 13:50:58 +00:00
|
|
|
"chat_sid=".$sessionid."&groupid=".$this->sets_info[$sessionid]['groupid']."\n";
|
|
|
|
$header .= "\n";
|
|
|
|
|
|
|
|
// That's enough headers for one lousy dummy response
|
2004-08-24 12:44:33 +00:00
|
|
|
$this->trace('writing users http response to handle '.$handle);
|
2004-08-10 13:50:58 +00:00
|
|
|
chat_socket_write($handle, $header . $content);
|
|
|
|
|
2004-08-11 14:45:40 +00:00
|
|
|
/*
|
|
|
|
$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";
|
2004-08-24 12:44:33 +00:00
|
|
|
$this->trace('writing users http response to handle '.$handle);
|
2004-08-11 14:45:40 +00:00
|
|
|
chat_socket_write($handle, $header);
|
|
|
|
*/
|
2004-07-30 15:32:36 +00:00
|
|
|
break;
|
|
|
|
case CHAT_SIDEKICK_MESSAGE:
|
|
|
|
// Incoming message
|
2004-08-14 16:45:13 +00:00
|
|
|
|
|
|
|
// Browser stupidity protection from duplicate messages:
|
|
|
|
$messageindex = intval($customdata['index']);
|
2004-08-24 12:44:33 +00:00
|
|
|
|
2004-08-14 16:45:13 +00:00
|
|
|
if($this->sets_info[$sessionid]['lastmessageindex'] >= $messageindex) {
|
|
|
|
// We have already broadcasted that!
|
2004-08-24 12:44:33 +00:00
|
|
|
$this->trace('discarding message with stale index');
|
2004-08-14 16:45:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Update our info
|
|
|
|
$this->sets_info[$sessionid]['lastmessageindex'] = $messageindex;
|
|
|
|
}
|
|
|
|
|
2004-08-12 12:31:38 +00:00
|
|
|
$msg = &New stdClass;
|
2004-07-30 15:32:36 +00:00
|
|
|
$msg->chatid = $this->sets_info[$sessionid]['chatid'];
|
|
|
|
$msg->userid = $this->sets_info[$sessionid]['userid'];
|
|
|
|
$msg->groupid = $this->sets_info[$sessionid]['groupid'];
|
|
|
|
$msg->system = 0;
|
|
|
|
$msg->message = urldecode($customdata['message']); // have to undo the browser's encoding
|
|
|
|
$msg->timestamp = time();
|
|
|
|
|
|
|
|
if(empty($msg->message)) {
|
|
|
|
// Someone just hit ENTER, send them on their way
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-08-10 13:50:58 +00:00
|
|
|
// Commit to DB
|
|
|
|
insert_record('chat_messages', $msg);
|
2004-07-30 15:32:36 +00:00
|
|
|
|
|
|
|
// OK, now push it out to all users
|
2004-08-10 13:50:58 +00:00
|
|
|
$this->message_broadcast($msg, $this->sets_info[$sessionid]['user']);
|
2004-07-30 15:32:36 +00:00
|
|
|
|
|
|
|
// Update that user's lastmessageping
|
2004-08-11 14:45:40 +00:00
|
|
|
$this->update_lastmessageping($sessionid, $msg->timestamp);
|
2004-07-30 15:32:36 +00:00
|
|
|
|
2004-08-10 13:50:58 +00:00
|
|
|
// We did our work, but before slamming the door on the poor browser
|
|
|
|
// show the courtesy of responding to the HTTP request. Otherwise, some
|
|
|
|
// browsers decide to get vengeance by flooding us with repeat requests.
|
|
|
|
|
|
|
|
$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";
|
|
|
|
|
|
|
|
// That's enough headers for one lousy dummy response
|
|
|
|
chat_socket_write($handle, $header);
|
|
|
|
|
2004-07-30 15:32:36 +00:00
|
|
|
// All done
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
socket_shutdown($handle);
|
|
|
|
socket_close($handle);
|
|
|
|
}
|
|
|
|
|
2004-08-23 15:42:53 +00:00
|
|
|
function promote_final($sessionid, $groupid, $customdata) {
|
2004-07-30 15:32:36 +00:00
|
|
|
if(isset($this->conn_sets[$sessionid])) {
|
2004-08-24 12:44:33 +00:00
|
|
|
$this->trace('Set cannot be finalized: Session '.$sessionid.' is already active');
|
2004-07-30 15:32:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$chatuser = get_record('chat_users', 'sid', $sessionid);
|
|
|
|
if($chatuser === false) {
|
|
|
|
$this->dismiss_half($sessionid);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$chat = get_record('chat', 'id', $chatuser->chatid);
|
|
|
|
if($chat === false) {
|
|
|
|
$this->dismiss_half($sessionid);
|
|
|
|
return false;
|
|
|
|
}
|
2004-08-10 13:50:58 +00:00
|
|
|
$user = get_record('user', 'id', $chatuser->userid);
|
|
|
|
if($user === false) {
|
|
|
|
$this->dismiss_half($sessionid);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$course = get_record('course', 'id', $chat->course); {
|
|
|
|
if($course === false) {
|
|
|
|
$this->dismiss_half($sessionid);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2004-07-30 15:32:36 +00:00
|
|
|
|
2004-08-10 13:50:58 +00:00
|
|
|
global $CHAT_HTMLHEAD_JS, $CFG;
|
|
|
|
|
|
|
|
// A really sad thing, to have to do this by hand.... :-(
|
|
|
|
$lang = NULL;
|
|
|
|
if(empty($lang) && !empty($course->lang)) {
|
|
|
|
$lang = $course->lang;
|
|
|
|
}
|
|
|
|
if(empty($lang) && !empty($user->lang)) {
|
|
|
|
$lang = $user->lang;
|
|
|
|
}
|
|
|
|
if(empty($lang)) {
|
|
|
|
$lang = $CFG->lang;
|
|
|
|
}
|
2004-07-30 15:32:36 +00:00
|
|
|
|
|
|
|
$this->conn_sets[$sessionid] = $this->conn_half[$sessionid];
|
2004-08-11 14:45:40 +00:00
|
|
|
|
|
|
|
// This whole thing needs to be purged of redundant info, and the
|
|
|
|
// code base to follow suit. But AFTER development is done.
|
2004-08-10 13:50:58 +00:00
|
|
|
$this->sets_info[$sessionid] = array(
|
2004-08-11 14:45:40 +00:00
|
|
|
'lastinfocommit' => 0,
|
2004-08-14 16:45:13 +00:00
|
|
|
'lastmessageindex' => 0,
|
2004-08-11 14:45:40 +00:00
|
|
|
'courseid' => $course->id,
|
|
|
|
'chatuser' => $chatuser,
|
2004-08-10 13:50:58 +00:00
|
|
|
'chatid' => $chatuser->chatid,
|
|
|
|
'user' => $user,
|
|
|
|
'userid' => $chatuser->userid,
|
|
|
|
'groupid' => $groupid,
|
2004-08-23 15:25:18 +00:00
|
|
|
'lang' => $lang,
|
2004-08-23 15:42:53 +00:00
|
|
|
'quirks' => $customdata['quirks']
|
2004-08-10 13:50:58 +00:00
|
|
|
);
|
|
|
|
|
2004-08-24 12:44:33 +00:00
|
|
|
$this->trace('QUIRKS value for this connection is '.$customdata['quirks']);
|
2004-08-23 15:25:18 +00:00
|
|
|
|
2004-07-30 15:32:36 +00:00
|
|
|
$this->dismiss_half($sessionid, false);
|
|
|
|
chat_socket_write($this->conn_sets[$sessionid][CHAT_CONNECTION_CHANNEL], $CHAT_HTMLHEAD_JS);
|
2004-08-24 12:44:33 +00:00
|
|
|
$this->trace('Connection accepted: '.$this->conn_sets[$sessionid][CHAT_CONNECTION_CHANNEL].', SID: '.$sessionid.' UID: '.$chatuser->userid.' GID: '.intval($groupid));
|
2004-07-30 15:32:36 +00:00
|
|
|
|
|
|
|
// Finally, broadcast the "entered the chat" message
|
|
|
|
|
2004-08-12 12:31:38 +00:00
|
|
|
$msg = &New stdClass;
|
2004-07-30 15:32:36 +00:00
|
|
|
$msg->chatid = $chatuser->chatid;
|
|
|
|
$msg->userid = $chatuser->userid;
|
|
|
|
$msg->groupid = 0;
|
|
|
|
$msg->system = 1;
|
|
|
|
$msg->message = 'enter';
|
|
|
|
$msg->timestamp = time();
|
|
|
|
|
|
|
|
insert_record('chat_messages', $msg);
|
2004-08-10 13:50:58 +00:00
|
|
|
$this->message_broadcast($msg, $this->sets_info[$sessionid]['user']);
|
2004-07-30 15:32:36 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function promote_ufo($handle, $type, $sessionid, $groupid, $customdata) {
|
|
|
|
if(empty($this->conn_ufo)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
foreach($this->conn_ufo as $id => $ufo) {
|
2004-08-12 12:31:38 +00:00
|
|
|
if($ufo->handle == $handle) {
|
2004-07-30 15:32:36 +00:00
|
|
|
// OK, got the id of the UFO, but what is it?
|
|
|
|
|
|
|
|
if($type & CHAT_SIDEKICK) {
|
|
|
|
// Is the main connection ready?
|
|
|
|
if(isset($this->conn_sets[$sessionid])) {
|
|
|
|
// Yes, so dispatch this sidekick now and be done with it
|
2004-08-24 12:44:33 +00:00
|
|
|
//$this->trace('Dispatching sidekick immediately');
|
2004-07-30 15:32:36 +00:00
|
|
|
$this->dispatch_sidekick($handle, $type, $sessionid, $customdata);
|
|
|
|
$this->dismiss_ufo($handle, false);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// No, so put it in the waiting list
|
2004-08-24 12:44:33 +00:00
|
|
|
$this->trace('sidekick waiting');
|
2004-07-30 15:32:36 +00:00
|
|
|
$this->conn_side[$sessionid][] = array('type' => $type, 'handle' => $handle, 'customdata' => $customdata);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's not a sidekick, at this point it can only be da man
|
|
|
|
|
|
|
|
if($type & CHAT_CONNECTION) {
|
|
|
|
// This forces a new connection right now...
|
2004-08-24 12:44:33 +00:00
|
|
|
$this->trace('Incoming connection from '.$ufo->ip.':'.$ufo->port);
|
2004-07-30 15:32:36 +00:00
|
|
|
|
|
|
|
// Do we have such a connection active?
|
|
|
|
if(isset($this->conn_sets[$sessionid])) {
|
|
|
|
// Yes, so regrettably we cannot promote you
|
2004-08-24 12:44:33 +00:00
|
|
|
$this->trace('Connection rejected: session '.$sessionid.' is already final');
|
2004-07-30 15:32:36 +00:00
|
|
|
$this->dismiss_ufo($handle);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join this with what we may have already
|
|
|
|
$this->conn_half[$sessionid][$type] = $handle;
|
|
|
|
|
|
|
|
// Do the bookkeeping
|
2004-08-23 15:42:53 +00:00
|
|
|
$this->promote_final($sessionid, $groupid, $customdata);
|
2004-07-30 15:32:36 +00:00
|
|
|
|
|
|
|
// It's not an UFO anymore
|
|
|
|
$this->dismiss_ufo($handle, false);
|
|
|
|
|
|
|
|
// Dispatch waiting sidekicks
|
|
|
|
$this->process_sidekicks($sessionid);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function dismiss_half($sessionid, $disconnect = true) {
|
|
|
|
if(!isset($this->conn_half[$sessionid])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if($disconnect) {
|
|
|
|
foreach($this->conn_half[$sessionid] as $handle) {
|
2004-08-12 12:31:38 +00:00
|
|
|
@socket_shutdown($handle);
|
|
|
|
@socket_close($handle);
|
2004-07-30 15:32:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($this->conn_half[$sessionid]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function dismiss_set($sessionid) {
|
2004-08-12 12:31:38 +00:00
|
|
|
if(!empty($this->conn_sets[$sessionid])) {
|
|
|
|
foreach($this->conn_sets[$sessionid] as $handle) {
|
|
|
|
// Since we want to dismiss this, don't generate any errors if it's dead already
|
|
|
|
@socket_shutdown($handle);
|
|
|
|
@socket_close($handle);
|
|
|
|
}
|
2004-07-30 15:32:36 +00:00
|
|
|
}
|
|
|
|
unset($this->conn_sets[$sessionid]);
|
|
|
|
unset($this->sets_info[$sessionid]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function dismiss_ufo($handle, $disconnect = true) {
|
|
|
|
if(empty($this->conn_ufo)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
foreach($this->conn_ufo as $id => $ufo) {
|
2004-08-12 12:31:38 +00:00
|
|
|
if($ufo->handle == $handle) {
|
2004-07-30 15:32:36 +00:00
|
|
|
unset($this->conn_ufo[$id]);
|
|
|
|
if($disconnect) {
|
|
|
|
chat_socket_write($handle, "You don't seem to be a valid client.\n");
|
|
|
|
socket_shutdown($handle);
|
|
|
|
socket_close($handle);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function conn_accept() {
|
|
|
|
$handle = @socket_accept($this->listen_socket);
|
|
|
|
if(!$handle) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-08-12 12:31:38 +00:00
|
|
|
$newconn = &New ChatConnection($handle);
|
2004-07-30 15:32:36 +00:00
|
|
|
$id = $this->new_ufo_id();
|
2004-08-12 12:31:38 +00:00
|
|
|
$this->conn_ufo[$id] = $newconn;
|
2004-07-30 15:32:36 +00:00
|
|
|
|
2004-08-24 12:44:33 +00:00
|
|
|
//$this->trace('UFO #'.$id.': connection from '.$newconn->ip.' on port '.$newconn->port.', '.$newconn->handle);
|
2004-07-30 15:32:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function conn_activity_ufo (&$handles) {
|
|
|
|
$monitor = array();
|
|
|
|
if(!empty($this->conn_ufo)) {
|
|
|
|
foreach($this->conn_ufo as $ufoid => $ufo) {
|
2004-08-12 12:31:38 +00:00
|
|
|
$monitor[$ufoid] = $ufo->handle;
|
2004-07-30 15:32:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(empty($monitor)) {
|
|
|
|
$handles = array();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$retval = socket_select($monitor, $a = NULL, $b = NULL, NULL);
|
|
|
|
$handles = $monitor;
|
|
|
|
|
|
|
|
return $retval;
|
|
|
|
}
|
|
|
|
|
2004-08-10 13:50:58 +00:00
|
|
|
function message_broadcast($message, $sender) {
|
2004-07-30 15:32:36 +00:00
|
|
|
if(empty($this->conn_sets)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($this->sets_info as $sessionid => $info) {
|
|
|
|
// We need to get handles from users that are in the same chatroom, same group
|
|
|
|
if($info['chatid'] == $message->chatid &&
|
|
|
|
($info['groupid'] == $message->groupid || $message->groupid == 0))
|
|
|
|
{
|
|
|
|
|
|
|
|
// Simply give them the message
|
2004-08-11 14:45:40 +00:00
|
|
|
$output = chat_format_message_manually($message, 0, $sender, $info['user'], $info['lang']);
|
2004-08-24 12:44:33 +00:00
|
|
|
$this->trace('Delivering message "'.$output->text.'" to '.$this->conn_sets[$sessionid][CHAT_CONNECTION_CHANNEL]);
|
2004-07-30 15:32:36 +00:00
|
|
|
|
2004-08-10 13:50:58 +00:00
|
|
|
if($output->beep) {
|
2004-08-24 12:44:33 +00:00
|
|
|
chat_socket_write($this->conn_sets[$sessionid][CHAT_CONNECTION_CHANNEL], '<embed src="'.$this->_beepsoundsrc.'" autostart="true" hidden="true" />');
|
2004-08-10 13:50:58 +00:00
|
|
|
}
|
|
|
|
|
2004-08-23 15:25:18 +00:00
|
|
|
if($info['quirks'] & QUIRK_CHUNK_UPDATE) {
|
|
|
|
$output->html .= $GLOBALS['CHAT_DUMMY_DATA'];
|
|
|
|
$output->html .= $GLOBALS['CHAT_DUMMY_DATA'];
|
|
|
|
$output->html .= $GLOBALS['CHAT_DUMMY_DATA'];
|
|
|
|
}
|
2004-08-23 15:03:26 +00:00
|
|
|
|
2004-08-10 13:50:58 +00:00
|
|
|
if(!chat_socket_write($this->conn_sets[$sessionid][CHAT_CONNECTION_CHANNEL], $output->html)) {
|
2004-07-30 15:32:36 +00:00
|
|
|
|
|
|
|
// Send failed! We must now disconnect/forget about the user FIRST
|
|
|
|
// and THEN broadcast a message to all others... otherwise, infinite recursion.
|
|
|
|
|
|
|
|
delete_records('chat_users', 'sid', $sessionid);
|
2004-08-12 12:31:38 +00:00
|
|
|
$msg = &New stdClass;
|
2004-07-30 15:32:36 +00:00
|
|
|
$msg->chatid = $info['chatid'];
|
|
|
|
$msg->userid = $info['userid'];
|
|
|
|
$msg->groupid = 0;
|
|
|
|
$msg->system = 1;
|
|
|
|
$msg->message = 'exit';
|
|
|
|
$msg->timestamp = time();
|
|
|
|
|
2004-08-24 12:44:33 +00:00
|
|
|
$this->trace('Client socket write failed, destroying uid '.$info['userid'].' with SID '.$sessionid);
|
2004-07-30 15:32:36 +00:00
|
|
|
insert_record('chat_messages', $msg);
|
|
|
|
|
2004-08-10 13:50:58 +00:00
|
|
|
// *************************** IMPORTANT
|
|
|
|
//
|
|
|
|
// Kill him BEFORE broadcasting, otherwise we 'll get infinite recursion!
|
|
|
|
//
|
|
|
|
// **********************************************************************
|
|
|
|
$latesender = $this->sets_info[$sessionid]['user'];
|
2004-07-30 15:32:36 +00:00
|
|
|
$this->dismiss_set($sessionid);
|
2004-08-10 13:50:58 +00:00
|
|
|
$this->message_broadcast($msg, $latesender);
|
2004-07-30 15:32:36 +00:00
|
|
|
}
|
2004-08-24 12:44:33 +00:00
|
|
|
//$this->trace('Sent to UID '.$this->sets_info[$sessionid]['userid'].': '.$message->text_);
|
2004-07-30 15:32:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function message_commit() {
|
|
|
|
}
|
|
|
|
|
2004-08-24 12:44:33 +00:00
|
|
|
function fatal($message) {
|
|
|
|
$message .= "\n";
|
|
|
|
if($this->_logfile) {
|
|
|
|
$this->trace($message);
|
|
|
|
}
|
|
|
|
echo "FATAL ERROR:: $message\n";
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
|
|
|
|
function cli_switch($switch, $param = NULL) {
|
|
|
|
switch($switch) { //LOL
|
|
|
|
case 'start':
|
|
|
|
// Start the daemon
|
|
|
|
$this->_readytogo = true;
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
// Verbose mode
|
|
|
|
$this->_trace_level = E_ALL;
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
// Use logfile
|
|
|
|
if(!empty($param)) {
|
|
|
|
$this->_logfile_name = $param;
|
|
|
|
}
|
|
|
|
$this->_logfile = @fopen($this->_logfile_name, 'a+');
|
|
|
|
if($this->_logfile == false) {
|
|
|
|
$this->fatal('Failed to open '.$this->_logfile_name.' for writing');
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
// Unrecognized
|
|
|
|
$this->fatal('Unrecognized command line switch: '.$switch);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-07-30 15:32:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
2004-08-12 12:31:38 +00:00
|
|
|
define('CHAT_SIDEKICK_BEEP', 0x23);
|
2004-07-30 15:32:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
$DAEMON = New ChatDaemon;
|
|
|
|
|
|
|
|
/// Check the parameters //////////////////////////////////////////////////////
|
|
|
|
|
2004-08-24 12:44:33 +00:00
|
|
|
unset($argv[0]);
|
|
|
|
$commandline = implode(' ', $argv);
|
|
|
|
if(strpos($commandline, '-') === false) {
|
|
|
|
if(!empty($commandline)) {
|
|
|
|
// We cannot have received any meaningful parameters
|
|
|
|
$DAEMON->fatal('Garbage in command line');
|
2004-07-30 15:32:36 +00:00
|
|
|
}
|
2004-08-24 12:44:33 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Parse command line
|
|
|
|
$switches = preg_split('/(-{1,2}[a-zA-Z]+) */', $commandline, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
|
2004-07-30 15:32:36 +00:00
|
|
|
|
2004-08-24 12:44:33 +00:00
|
|
|
// Taking advantage of the fact that $switches is indexed with incrementing numeric keys
|
|
|
|
// We will be using that to pass additional information to those switches who need it
|
|
|
|
$numswitches = count($switches);
|
2004-07-30 15:32:36 +00:00
|
|
|
|
2004-08-24 12:44:33 +00:00
|
|
|
// Fancy way to give a "hyphen" boolean flag to each "switch"
|
|
|
|
$switches = array_map(create_function('$x', 'return array("str" => $x, "hyphen" => (substr($x, 0, 1) == "-"));'), $switches);
|
|
|
|
|
|
|
|
for($i = 0; $i < $numswitches; ++$i) {
|
|
|
|
|
|
|
|
$switch = $switches[$i]['str'];
|
|
|
|
$params = ($i == $numswitches - 1 ? NULL :
|
|
|
|
($switches[$i + 1]['hyphen'] ? NULL : trim($switches[$i + 1]['str']))
|
|
|
|
);
|
|
|
|
|
|
|
|
if(substr($switch, 0, 2) == '--') {
|
|
|
|
// Double-hyphen switch
|
|
|
|
$DAEMON->cli_switch(strtolower(substr($switch, 2)), $params);
|
|
|
|
}
|
|
|
|
else if(substr($switch, 0, 1) == '-') {
|
|
|
|
// Single-hyphen switch(es), may be more than one run together
|
|
|
|
$switch = substr($switch, 1); // Get rid of the -
|
|
|
|
$len = strlen($switch);
|
|
|
|
for($j = 0; $j < $len; ++$j) {
|
|
|
|
$DAEMON->cli_switch(strtolower(substr($switch, $j, 1)), $params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$DAEMON->query_start()) {
|
|
|
|
// For some reason we didn't start, so print out some info
|
|
|
|
echo 'Starts the Moodle chat socket server on port '.$CFG->chat_serverport;
|
|
|
|
echo "\n\n";
|
|
|
|
echo "Usage: chatd.php [parameters]\n\n";
|
|
|
|
echo "Parameters:\n";
|
|
|
|
echo " --start Starts the daemon\n";
|
|
|
|
echo " -v Verbose mode (prints trivial information messages)\n";
|
|
|
|
echo " -l [logfile] Log all messages to logfile (if not specified, chatd.log)\n";
|
|
|
|
echo "Example:\n";
|
|
|
|
echo " chatd.php --start -l\n\n";
|
|
|
|
die();
|
|
|
|
}
|
2004-07-30 15:32:36 +00:00
|
|
|
|
|
|
|
/// Try to set up all the sockets ////////////////////////////////////////////////
|
|
|
|
|
2004-08-24 12:44:33 +00:00
|
|
|
$DAEMON->trace('Setting up sockets');
|
2004-07-30 15:32:36 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creating socket
|
|
|
|
|
|
|
|
if(false === ($DAEMON->listen_socket = socket_create(AF_INET, SOCK_STREAM, 0))) {
|
|
|
|
// Failed to create socket
|
|
|
|
$DAEMON->last_error = socket_last_error();
|
|
|
|
echo "Error: socket_create() failed: ". socket_strerror(socket_last_error($DAEMON->last_error)).' ['.$DAEMON->last_error."]\n";
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
|
|
|
|
//socket_close($DAEMON->listen_socket);
|
|
|
|
//die();
|
|
|
|
|
|
|
|
if(!socket_bind($DAEMON->listen_socket, $CFG->chat_serverip, $CFG->chat_serverport)) {
|
|
|
|
// Failed to bind socket
|
|
|
|
$DAEMON->last_error = socket_last_error();
|
|
|
|
echo "Error: socket_bind() failed: ". socket_strerror(socket_last_error($DAEMON->last_error)).' ['.$DAEMON->last_error."]\n";
|
2004-08-10 13:50:58 +00:00
|
|
|
|
2004-08-12 12:31:38 +00:00
|
|
|
if($DAEMON->last_error != 98) {
|
|
|
|
die();
|
2004-08-10 13:50:58 +00:00
|
|
|
}
|
2004-08-12 12:31:38 +00:00
|
|
|
|
2004-07-30 15:32:36 +00:00
|
|
|
}
|
|
|
|
if(!socket_listen($DAEMON->listen_socket, $CFG->chat_servermax)) {
|
|
|
|
// Failed to get socket to listen
|
|
|
|
$DAEMON->last_error = socket_last_error();
|
|
|
|
echo "Error: socket_listen() failed: ". socket_strerror(socket_last_error($DAEMON->last_error)).' ['.$DAEMON->last_error."]\n";
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Socket has been initialized and is ready
|
2004-08-24 12:44:33 +00:00
|
|
|
$DAEMON->trace('Socket opened on port '.$CFG->chat_serverport);
|
2004-07-30 15:32:36 +00:00
|
|
|
|
|
|
|
// [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($DAEMON->listen_socket, SOL_SOCKET, SO_REUSEADDR, 1);
|
|
|
|
socket_set_nonblock($DAEMON->listen_socket);
|
|
|
|
|
|
|
|
/// Sockets all set up! Now we loop and process incoming data.
|
|
|
|
/*
|
|
|
|
declare(ticks=1);
|
|
|
|
|
|
|
|
$pid = pcntl_fork();
|
|
|
|
if ($pid == -1) {
|
|
|
|
die("could not fork");
|
|
|
|
} else if ($pid) {
|
|
|
|
exit(); // we are the parent
|
|
|
|
} else {
|
|
|
|
// we are the child
|
|
|
|
}
|
|
|
|
|
|
|
|
// detatch from the controlling terminal
|
|
|
|
if (!posix_setsid()) {
|
|
|
|
die("could not detach from terminal");
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup signal handlers
|
|
|
|
pcntl_signal(SIGTERM, "sig_handler");
|
|
|
|
pcntl_signal(SIGHUP, "sig_handler");
|
|
|
|
*/
|
|
|
|
|
2004-08-24 12:44:33 +00:00
|
|
|
if($DAEMON->_pcntl_exists && false) {
|
|
|
|
$DAEMON->trace('Unholy spirit possession: daemonizing');
|
2004-07-30 15:32:36 +00:00
|
|
|
$DAEMON->pid = pcntl_fork();
|
|
|
|
if($pid == -1) {
|
2004-08-24 12:44:33 +00:00
|
|
|
$DAEMON->trace('Process fork failed, terminating');
|
2004-07-30 15:32:36 +00:00
|
|
|
die();
|
|
|
|
}
|
|
|
|
else if($pid) {
|
|
|
|
// We are the parent
|
2004-08-24 12:44:33 +00:00
|
|
|
$DAEMON->trace('Successfully forked the daemon with PID '.$pid);
|
2004-07-30 15:32:36 +00:00
|
|
|
die();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// We are the daemon! :P
|
|
|
|
}
|
|
|
|
|
|
|
|
// FROM NOW ON, IT'S THE DAEMON THAT'S RUNNING!
|
|
|
|
|
|
|
|
// Detach from controlling terminal
|
|
|
|
if(!posix_setsid()) {
|
2004-08-24 12:44:33 +00:00
|
|
|
$DAEMON->trace('Could not detach daemon process from terminal!');
|
2004-07-30 15:32:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Cannot go demonic
|
2004-08-24 12:44:33 +00:00
|
|
|
$DAEMON->trace('Unholy spirit possession failed: PHP is not compiled with --enable-pcntl');
|
2004-07-30 15:32:36 +00:00
|
|
|
}
|
|
|
|
|
2004-08-24 12:44:33 +00:00
|
|
|
$DAEMON->trace('Started Moodle chatd on port '.$CFG->chat_serverport.', listening socket '.$DAEMON->listen_socket, E_USER_WARNING);
|
2004-07-30 15:32:36 +00:00
|
|
|
|
|
|
|
while(true) {
|
|
|
|
$active = array();
|
|
|
|
|
|
|
|
// First of all, let's see if any of our UFOs has identified itself
|
|
|
|
if($DAEMON->conn_activity_ufo($active)) {
|
|
|
|
foreach($active as $handle) {
|
|
|
|
$read_socket = array($handle);
|
|
|
|
$changed = socket_select($read_socket, $write = NULL, $except = NULL, 0, 0);
|
|
|
|
|
|
|
|
if($changed > 0) {
|
|
|
|
// Let's see what it has to say
|
|
|
|
|
|
|
|
$data = socket_read($handle, 512);
|
|
|
|
if(empty($data)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-08-12 12:31:38 +00:00
|
|
|
if(!ereg('win=(chat|users|message|beep).*&chat_sid=([a-zA-Z0-9]*)&groupid=([0-9]*) HTTP', $data, $info)) {
|
2004-07-30 15:32:36 +00:00
|
|
|
// Malformed data
|
2004-08-24 12:44:33 +00:00
|
|
|
$DAEMON->trace('UFO with '.$handle.': Request with malformed data; connection closed', E_USER_WARNING);
|
2004-07-30 15:32:36 +00:00
|
|
|
$DAEMON->dismiss_ufo($handle);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$type = $info[1];
|
|
|
|
$sessionid = $info[2];
|
|
|
|
$groupid = $info[3];
|
|
|
|
|
|
|
|
$customdata = array();
|
|
|
|
|
|
|
|
switch($type) {
|
|
|
|
case 'chat':
|
|
|
|
$type = CHAT_CONNECTION_CHANNEL;
|
2004-08-23 15:35:08 +00:00
|
|
|
$customdata['quirks'] = 0;
|
|
|
|
if(strpos($data, 'Safari')) {
|
2004-08-24 12:44:33 +00:00
|
|
|
$DAEMON->trace('Safari identified...', E_USER_WARNING);
|
2004-08-23 15:35:08 +00:00
|
|
|
$customdata['quirks'] += QUIRK_CHUNK_UPDATE;
|
2004-08-23 15:25:18 +00:00
|
|
|
}
|
2004-07-30 15:32:36 +00:00
|
|
|
break;
|
|
|
|
case 'users':
|
|
|
|
$type = CHAT_SIDEKICK_USERS;
|
|
|
|
break;
|
2004-08-12 12:31:38 +00:00
|
|
|
case 'beep':
|
|
|
|
$type = CHAT_SIDEKICK_BEEP;
|
|
|
|
if(!ereg('beep=([^&]*)[& ]', $data, $info)) {
|
2004-08-24 12:44:33 +00:00
|
|
|
$DAEMON->trace('Beep sidekick did not contain a valid userid', E_USER_WARNING);
|
2004-08-12 12:31:38 +00:00
|
|
|
$DAEMON->dismiss_ufo($handle);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$customdata = array('beep' => intval($info[1]));
|
|
|
|
}
|
|
|
|
break;
|
2004-07-30 15:32:36 +00:00
|
|
|
case 'message':
|
|
|
|
$type = CHAT_SIDEKICK_MESSAGE;
|
2004-08-14 16:45:13 +00:00
|
|
|
if(!ereg('chat_message=([^&]*)[& ]chat_msgidnr=([^&]*)[& ]', $data, $info)) {
|
2004-08-24 12:44:33 +00:00
|
|
|
$DAEMON->trace('Message sidekick did not contain a valid message', E_USER_WARNING);
|
2004-07-30 15:32:36 +00:00
|
|
|
$DAEMON->dismiss_ufo($handle);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
2004-08-14 16:45:13 +00:00
|
|
|
$customdata = array('message' => $info[1], 'index' => $info[2]);
|
2004-07-30 15:32:36 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2004-08-24 12:44:33 +00:00
|
|
|
$DAEMON->trace('UFO with '.$handle.': Request with unknown type; connection closed', E_USER_WARNING);
|
2004-07-30 15:32:36 +00:00
|
|
|
$DAEMON->dismiss_ufo($handle);
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// OK, now we know it's something good... promote it and pass it all the data it needs
|
|
|
|
$DAEMON->promote_ufo($handle, $type, $sessionid, $groupid, $customdata);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, accept new connections
|
|
|
|
$DAEMON->conn_accept();
|
|
|
|
|
2004-08-24 12:44:33 +00:00
|
|
|
usleep($DAEMON->_time_rest_socket);
|
2004-07-30 15:32:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@socket_shutdown($DAEMON->listen_socket, 0);
|
|
|
|
die("\n\n-- terminated --\n");
|
|
|
|
|
|
|
|
|
|
|
|
function chat_socket_write($connection, $text) {
|
|
|
|
$check_socket = array($connection);
|
|
|
|
$socket_changed = socket_select($read = NULL, $check_socket, $except = NULL, 0, 0);
|
|
|
|
if($socket_changed > 0) {
|
2004-08-10 13:50:58 +00:00
|
|
|
$written = socket_write($connection, $text, strlen($text));
|
2004-07-30 15:32:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|