weekly release 3.7.1+
[moodle.git] / mod / chat / chatd.php
blob47a4297d90a60e390424d725eef6fd85bc214ad9
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Chat daemon
20 * @package mod_chat
21 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 define('CLI_SCRIPT', true);
27 require(__DIR__.'/../../config.php');
28 require_once($CFG->dirroot . '/mod/chat/lib.php');
30 // Browser quirks.
31 define('QUIRK_CHUNK_UPDATE', 0x0001);
33 // Connection telltale.
34 define('CHAT_CONNECTION', 0x10);
35 // Connections: Incrementing sequence, 0x10 to 0x1f.
36 define('CHAT_CONNECTION_CHANNEL', 0x11);
38 // Sidekick telltale.
39 define('CHAT_SIDEKICK', 0x20);
40 // Sidekicks: Incrementing sequence, 0x21 to 0x2f.
41 define('CHAT_SIDEKICK_USERS', 0x21);
42 define('CHAT_SIDEKICK_MESSAGE', 0x22);
43 define('CHAT_SIDEKICK_BEEP', 0x23);
45 $phpversion = phpversion();
46 echo 'Moodle chat daemon v1.0 on PHP '.$phpversion."\n\n";
48 // Set up all the variables we need.
50 // The $CFG variables are now defined in database by chat/lib.php.
52 $_SERVER['PHP_SELF'] = 'dummy';
53 $_SERVER['SERVER_NAME'] = 'dummy';
54 $_SERVER['HTTP_USER_AGENT'] = 'dummy';
56 $_SERVER['SERVER_NAME'] = $CFG->chat_serverhost;
57 $_SERVER['PHP_SELF'] = "http://$CFG->chat_serverhost:$CFG->chat_serverport/mod/chat/chatd.php";
59 core_php_time_limit::raise(0);
60 error_reporting(E_ALL);
62 function chat_empty_connection() {
63 return array('sid' => null, 'handle' => null, 'ip' => null, 'port' => null, 'groupid' => null);
66 class ChatConnection {
67 // Chat-related info.
68 public $sid = null;
69 public $type = null;
71 // PHP-level info.
72 public $handle = null;
74 // TCP/IP.
75 public $ip = null;
76 public $port = null;
78 public function __construct($resource) {
79 $this->handle = $resource;
80 @socket_getpeername($this->handle, $this->ip, $this->port);
84 class ChatDaemon {
85 public $_resetsocket = false;
86 public $_readytogo = false;
87 public $_logfile = false;
88 public $_trace_to_console = true;
89 public $_trace_to_stdout = true;
90 public $_logfile_name = 'chatd.log';
91 public $_last_idle_poll = 0;
93 public $connectionsunidentified = array(); // Connections not identified yet.
94 public $connectionsside = array(); // Sessions with sidekicks waiting for the main connection to be processed.
95 public $connectionshalf = array(); // Sessions that have valid connections but not all of them.
96 public $connectionssets = array(); // Sessions with complete connection sets.
97 public $setsinfo = array(); // Keyed by sessionid exactly like conn_sets, one of these for each of those.
98 public $chatrooms = array(); // Keyed by chatid, holding arrays of data.
100 // IMPORTANT: $connectionssets, $setsinfo and $chatrooms must remain synchronized!
101 // Pay extra attention when you write code that affects any of them!
103 public function __construct() {
104 $this->_trace_level = E_ALL ^ E_USER_NOTICE;
105 $this->_pcntl_exists = function_exists('pcntl_fork');
106 $this->_time_rest_socket = 20;
107 $this->_beepsoundsrc = $GLOBALS['CFG']->wwwroot.'/mod/chat/beep.mp3';
108 $this->_freq_update_records = 20;
109 $this->_freq_poll_idle_chat = $GLOBALS['CFG']->chat_old_ping;
110 $this->_stdout = fopen('php://stdout', 'w');
111 if ($this->_stdout) {
112 // Avoid double traces for everything.
113 $this->_trace_to_console = false;
117 public function error_handler ($errno, $errmsg, $filename, $linenum, $vars) {
118 // Checks if an error needs to be suppressed due to @.
119 if (error_reporting() != 0) {
120 $this->trace($errmsg.' on line '.$linenum, $errno);
122 return true;
125 public function poll_idle_chats($now) {
126 $this->trace('Polling chats to detect disconnected users');
127 if (!empty($this->chatrooms)) {
128 foreach ($this->chatrooms as $chatid => $chatroom) {
129 if (!empty($chatroom['users'])) {
130 foreach ($chatroom['users'] as $sessionid => $userid) {
131 // We will be polling each user as required.
132 $this->trace('...shall we poll '.$sessionid.'?');
133 if (!empty($this->sets_info[$sessionid]) && isset($this->sets_info[$sessionid]['chatuser']) &&
134 // Having tried to exclude race conditions as already done in user_lazy_update()
135 // please do the real job by checking the last poll.
136 ($this->sets_info[$sessionid]['chatuser']->lastmessageping < $this->_last_idle_poll)) {
137 $this->trace('YES!');
138 // This user hasn't been polled since his last message.
139 $result = $this->write_data($this->conn_sets[$sessionid][CHAT_CONNECTION_CHANNEL], '<!-- poll -->');
140 if ($result === false) {
141 // User appears to have disconnected.
142 $this->disconnect_session($sessionid);
149 $this->_last_idle_poll = $now;
152 public function query_start() {
153 return $this->_readytogo;
156 public function trace($message, $level = E_USER_NOTICE) {
157 $severity = '';
159 switch($level) {
160 case E_USER_WARNING:
161 $severity = '*IMPORTANT* ';
162 break;
163 case E_USER_ERROR:
164 $severity = ' *CRITICAL* ';
165 break;
166 case E_NOTICE:
167 case E_WARNING:
168 $severity = ' *CRITICAL* [php] ';
169 break;
172 $date = date('[Y-m-d H:i:s] ');
173 $message = $date.$severity.$message."\n";
175 if ($this->_trace_level & $level) {
176 // It is accepted for output.
178 // Error-class traces go to STDERR too.
179 if ($level & E_USER_ERROR) {
180 fwrite(STDERR, $message);
183 // Emit the message to wherever we should.
184 if ($this->_trace_to_stdout) {
185 fwrite($this->_stdout, $message);
186 fflush($this->_stdout);
188 if ($this->_trace_to_console) {
189 echo $message;
190 flush();
192 if ($this->_logfile) {
193 fwrite($this->_logfile, $message);
194 fflush($this->_logfile);
199 public function write_data($connection, $text) {
200 $written = @socket_write($connection, $text, strlen($text));
201 if ($written === false) {
202 return false;
204 return true;
207 public function user_lazy_update($sessionid) {
208 global $DB;
210 if (empty($this->sets_info[$sessionid])) {
211 $this->trace('user_lazy_update() called for an invalid SID: '.$sessionid, E_USER_WARNING);
212 return false;
215 // Does promote_final() already finish its job?
216 if (!isset($this->sets_info[$sessionid]['lastinfocommit'])) {
217 return false;
220 $now = time();
222 // We 'll be cheating a little, and NOT updating the record data as
223 // often as we can, so that we save on DB queries (imagine MANY users).
224 if ($now - $this->sets_info[$sessionid]['lastinfocommit'] > $this->_freq_update_records) {
225 // Commit to permanent storage.
226 $this->sets_info[$sessionid]['lastinfocommit'] = $now;
227 $DB->update_record('chat_users', $this->sets_info[$sessionid]['chatuser']);
229 return true;
232 public function get_user_window($sessionid) {
233 global $CFG, $OUTPUT;
235 static $str;
237 $info = &$this->sets_info[$sessionid];
239 $timenow = time();
241 if (empty($str)) {
242 $str = new stdClass();
243 $str->idle = get_string("idle", "chat");
244 $str->beep = get_string("beep", "chat");
245 $str->day = get_string("day");
246 $str->days = get_string("days");
247 $str->hour = get_string("hour");
248 $str->hours = get_string("hours");
249 $str->min = get_string("min");
250 $str->mins = get_string("mins");
251 $str->sec = get_string("sec");
252 $str->secs = get_string("secs");
253 $str->years = get_string('years');
256 ob_start();
257 $refreshinval = $CFG->chat_refresh_userlist * 1000;
258 echo <<<EOD
259 <html><head>
260 <meta http-equiv="refresh" content="$refreshinval">
261 <style type="text/css"> img{border:0} </style>
262 <script type="text/javascript">
263 //<![CDATA[
264 function openpopup(url,name,options,fullscreen) {
265 fullurl = "$CFG->wwwroot" + url;
266 windowobj = window.open(fullurl,name,options);
267 if (fullscreen) {
268 windowobj.moveTo(0,0);
269 windowobj.resizeTo(screen.availWidth,screen.availHeight);
271 windowobj.focus();
272 return false;
274 //]]>
275 </script></head><body><table><tbody>
276 EOD;
278 // Get the users from that chatroom.
279 $users = $this->chatrooms[$info['chatid']]['users'];
281 foreach ($users as $usersessionid => $userid) {
282 // Fetch each user's sessionid and then the rest of his data from $this->sets_info.
283 $userinfo = $this->sets_info[$usersessionid];
285 $lastping = $timenow - $userinfo['chatuser']->lastmessageping;
287 echo '<tr><td width="35">';
289 $link = '/user/view.php?id='.$userinfo['user']->id.'&course='.$userinfo['courseid'];
290 $anchortagcontents = $OUTPUT->user_picture($userinfo['user'], array('courseid' => $userinfo['courseid']));
292 $action = new popup_action('click', $link, 'user'.$userinfo['chatuser']->id);
293 $anchortag = $OUTPUT->action_link($link, $anchortagcontents, $action);
295 echo $anchortag;
296 echo "</td><td valign=\"center\">";
297 echo "<p><font size=\"1\">";
298 echo fullname($userinfo['user'])."<br />";
299 echo "<font color=\"#888888\">$str->idle: ".format_time($lastping, $str)."</font> ";
300 echo '<a target="empty" href="http://'.$CFG->chat_serverhost.':'.$CFG->chat_serverport.
301 '/?win=beep&amp;beep='.$userinfo['user']->id.
302 '&chat_sid='.$sessionid.'">'.$str->beep."</a>\n";
303 echo "</font></p>";
304 echo "<td></tr>";
307 echo '</tbody></table>';
308 echo "</body>\n</html>\n";
310 return ob_get_clean();
314 public function new_ufo_id() {
315 static $id = 0;
316 if ($id++ === 0x1000000) { // Cycling very very slowly to prevent overflow.
317 $id = 0;
319 return $id;
322 public function process_sidekicks($sessionid) {
323 if (empty($this->conn_side[$sessionid])) {
324 return true;
326 foreach ($this->conn_side[$sessionid] as $sideid => $sidekick) {
327 // TODO: is this late-dispatch working correctly?
328 $this->dispatch_sidekick($sidekick['handle'], $sidekick['type'], $sessionid, $sidekick['customdata']);
329 unset($this->conn_side[$sessionid][$sideid]);
331 return true;
334 public function dispatch_sidekick($handle, $type, $sessionid, $customdata) {
335 global $CFG, $DB;
337 switch($type) {
338 case CHAT_SIDEKICK_BEEP:
340 // Incoming beep.
341 $msg = new stdClass;
342 $msg->chatid = $this->sets_info[$sessionid]['chatid'];
343 $msg->userid = $this->sets_info[$sessionid]['userid'];
344 $msg->groupid = $this->sets_info[$sessionid]['groupid'];
345 $msg->issystem = 0;
346 $msg->message = 'beep '.$customdata['beep'];
347 $msg->timestamp = time();
349 // Commit to DB.
350 chat_send_chatmessage($this->sets_info[$sessionid]['chatuser'], $msg->message, false,
351 $this->sets_info[$sessionid]['cm']);
353 // OK, now push it out to all users.
354 $this->message_broadcast($msg, $this->sets_info[$sessionid]['user']);
356 // Update that user's lastmessageping.
357 $this->sets_info[$sessionid]['chatuser']->lastping = $msg->timestamp;
358 $this->sets_info[$sessionid]['chatuser']->lastmessageping = $msg->timestamp;
359 $this->user_lazy_update($sessionid);
361 // We did our work, but before slamming the door on the poor browser
362 // show the courtesy of responding to the HTTP request. Otherwise, some
363 // browsers decide to get vengeance by flooding us with repeat requests.
365 $header = "HTTP/1.1 200 OK\n";
366 $header .= "Connection: close\n";
367 $header .= "Date: ".date('r')."\n";
368 $header .= "Server: Moodle\n";
369 $header .= "Content-Type: text/html; charset=utf-8\n";
370 $header .= "Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT\n";
371 $header .= "Cache-Control: no-cache, must-revalidate\n";
372 $header .= "Expires: Wed, 4 Oct 1978 09:32:45 GMT\n";
373 $header .= "\n";
375 // That's enough headers for one lousy dummy response.
376 $this->write_data($handle, $header);
377 // All done.
378 break;
380 case CHAT_SIDEKICK_USERS:
381 // A request to paint a user window.
383 $content = $this->get_user_window($sessionid);
385 $header = "HTTP/1.1 200 OK\n";
386 $header .= "Connection: close\n";
387 $header .= "Date: ".date('r')."\n";
388 $header .= "Server: Moodle\n";
389 $header .= "Content-Type: text/html; charset=utf-8\n";
390 $header .= "Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT\n";
391 $header .= "Cache-Control: no-cache, must-revalidate\n";
392 $header .= "Expires: Wed, 4 Oct 1978 09:32:45 GMT\n";
393 $header .= "Content-Length: ".strlen($content)."\n";
395 // The refresh value is 2 seconds higher than the configuration variable.
396 // This is because we are doing JS refreshes all the time.
397 // However, if the JS doesn't work for some reason, we still want to refresh once in a while.
398 $header .= "Refresh: ".(intval($CFG->chat_refresh_userlist) + 2).
399 "; url=http://$CFG->chat_serverhost:$CFG->chat_serverport/?win=users&".
400 "chat_sid=".$sessionid."\n";
401 $header .= "\n";
403 // That's enough headers for one lousy dummy response.
404 $this->trace('writing users http response to handle '.$handle);
405 $this->write_data($handle, $header . $content);
407 // Update that user's lastping.
408 $this->sets_info[$sessionid]['chatuser']->lastping = time();
409 $this->user_lazy_update($sessionid);
411 break;
413 case CHAT_SIDEKICK_MESSAGE:
414 // Incoming message.
416 // Browser stupidity protection from duplicate messages.
417 $messageindex = intval($customdata['index']);
419 if ($this->sets_info[$sessionid]['lastmessageindex'] >= $messageindex) {
420 // We have already broadcasted that!
421 break;
422 } else {
423 // Update our info.
424 $this->sets_info[$sessionid]['lastmessageindex'] = $messageindex;
427 $msg = new stdClass;
428 $msg->chatid = $this->sets_info[$sessionid]['chatid'];
429 $msg->userid = $this->sets_info[$sessionid]['userid'];
430 $msg->groupid = $this->sets_info[$sessionid]['groupid'];
431 $msg->issystem = 0;
432 $msg->message = urldecode($customdata['message']); // Have to undo the browser's encoding.
433 $msg->timestamp = time();
435 if (empty($msg->message)) {
436 // Someone just hit ENTER, send them on their way.
437 break;
440 // A slight hack to prevent malformed SQL inserts.
441 $origmsg = $msg->message;
442 $msg->message = $msg->message;
444 // Commit to DB.
445 chat_send_chatmessage($this->sets_info[$sessionid]['chatuser'], $msg->message, false,
446 $this->sets_info[$sessionid]['cm']);
448 // Undo the hack.
449 $msg->message = $origmsg;
451 // OK, now push it out to all users.
452 $this->message_broadcast($msg, $this->sets_info[$sessionid]['user']);
454 // Update that user's lastmessageping.
455 $this->sets_info[$sessionid]['chatuser']->lastping = $msg->timestamp;
456 $this->sets_info[$sessionid]['chatuser']->lastmessageping = $msg->timestamp;
457 $this->user_lazy_update($sessionid);
459 // We did our work, but before slamming the door on the poor browser
460 // show the courtesy of responding to the HTTP request. Otherwise, some
461 // browsers decide to get vengeance by flooding us with repeat requests.
463 $header = "HTTP/1.1 200 OK\n";
464 $header .= "Connection: close\n";
465 $header .= "Date: ".date('r')."\n";
466 $header .= "Server: Moodle\n";
467 $header .= "Content-Type: text/html; charset=utf-8\n";
468 $header .= "Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT\n";
469 $header .= "Cache-Control: no-cache, must-revalidate\n";
470 $header .= "Expires: Wed, 4 Oct 1978 09:32:45 GMT\n";
471 $header .= "\n";
473 // That's enough headers for one lousy dummy response.
474 $this->write_data($handle, $header);
476 // All done.
477 break;
480 socket_shutdown($handle);
481 socket_close($handle);
484 public function promote_final($sessionid, $customdata) {
485 global $DB;
487 if (isset($this->conn_sets[$sessionid])) {
488 $this->trace('Set cannot be finalized: Session '.$sessionid.' is already active');
489 return false;
492 $chatuser = $DB->get_record('chat_users', array('sid' => $sessionid));
493 if ($chatuser === false) {
494 $this->dismiss_half($sessionid);
495 return false;
497 $chat = $DB->get_record('chat', array('id' => $chatuser->chatid));
498 if ($chat === false) {
499 $this->dismiss_half($sessionid);
500 return false;
502 $user = $DB->get_record('user', array('id' => $chatuser->userid));
503 if ($user === false) {
504 $this->dismiss_half($sessionid);
505 return false;
507 $course = $DB->get_record('course', array('id' => $chat->course));
508 if ($course === false) {
509 $this->dismiss_half($sessionid);
510 return false;
512 if (!($cm = get_coursemodule_from_instance('chat', $chat->id, $course->id))) {
513 $this->dismiss_half($sessionid);
514 return false;
517 global $CHAT_HTMLHEAD_JS;
519 $this->conn_sets[$sessionid] = $this->conn_half[$sessionid];
521 // This whole thing needs to be purged of redundant info, and the
522 // code base to follow suit. But AFTER development is done.
523 $this->sets_info[$sessionid] = array(
524 'lastinfocommit' => 0,
525 'lastmessageindex' => 0,
526 'course' => $course,
527 'courseid' => $course->id,
528 'chatuser' => $chatuser,
529 'chatid' => $chat->id,
530 'cm' => $cm,
531 'user' => $user,
532 'userid' => $user->id,
533 'groupid' => $chatuser->groupid,
534 'lang' => $chatuser->lang,
535 'quirks' => $customdata['quirks']
538 // If we know nothing about this chatroom, initialize it and add the user.
539 if (!isset($this->chatrooms[$chat->id]['users'])) {
540 $this->chatrooms[$chat->id]['users'] = array($sessionid => $user->id);
541 } else {
542 // Otherwise just add the user.
543 $this->chatrooms[$chat->id]['users'][$sessionid] = $user->id;
546 $header = "HTTP/1.1 200 OK\n";
547 $header .= "Connection: close\n";
548 $header .= "Date: ".date('r')."\n";
549 $header .= "Server: Moodle\n";
550 $header .= "Content-Type: text/html; charset=utf-8\n";
551 $header .= "Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT\n";
552 $header .= "Cache-Control: no-cache, must-revalidate\n";
553 $header .= "Expires: Wed, 4 Oct 1978 09:32:45 GMT\n";
554 $header .= "\n";
556 $this->dismiss_half($sessionid, false);
557 $this->write_data($this->conn_sets[$sessionid][CHAT_CONNECTION_CHANNEL], $header . $CHAT_HTMLHEAD_JS);
558 $this->trace('Connection accepted: '
559 .$this->conn_sets[$sessionid][CHAT_CONNECTION_CHANNEL]
560 .', SID: '
561 .$sessionid
562 .' UID: '
563 .$chatuser->userid
564 .' GID: '
565 .$chatuser->groupid, E_USER_WARNING);
567 // Finally, broadcast the "entered the chat" message.
569 $msg = new stdClass;
570 $msg->chatid = $chatuser->chatid;
571 $msg->userid = $chatuser->userid;
572 $msg->groupid = $chatuser->groupid;
573 $msg->issystem = 1;
574 $msg->message = 'enter';
575 $msg->timestamp = time();
577 chat_send_chatmessage($chatuser, $msg->message, true);
578 $this->message_broadcast($msg, $this->sets_info[$sessionid]['user']);
580 return true;
583 public function promote_ufo($handle, $type, $sessionid, $customdata) {
584 if (empty($this->conn_ufo)) {
585 return false;
587 foreach ($this->conn_ufo as $id => $ufo) {
588 if ($ufo->handle == $handle) {
589 // OK, got the id of the UFO, but what is it?
591 if ($type & CHAT_SIDEKICK) {
592 // Is the main connection ready?
593 if (isset($this->conn_sets[$sessionid])) {
594 // Yes, so dispatch this sidekick now and be done with it.
595 $this->dispatch_sidekick($handle, $type, $sessionid, $customdata);
596 $this->dismiss_ufo($handle, false);
597 } else {
598 // No, so put it in the waiting list.
599 $this->trace('sidekick waiting');
600 $this->conn_side[$sessionid][] = array('type' => $type, 'handle' => $handle, 'customdata' => $customdata);
602 return true;
605 // If it's not a sidekick, at this point it can only be da man.
607 if ($type & CHAT_CONNECTION) {
608 // This forces a new connection right now.
609 $this->trace('Incoming connection from '.$ufo->ip.':'.$ufo->port);
611 // Do we have such a connection active?
612 if (isset($this->conn_sets[$sessionid])) {
613 // Yes, so regrettably we cannot promote you.
614 $this->trace('Connection rejected: session '.$sessionid.' is already final');
615 $this->dismiss_ufo($handle, true, 'Your SID was rejected.');
616 return false;
619 // Join this with what we may have already.
620 $this->conn_half[$sessionid][$type] = $handle;
622 // Do the bookkeeping.
623 $this->promote_final($sessionid, $customdata);
625 // It's not a UFO anymore.
626 $this->dismiss_ufo($handle, false);
628 // Dispatch waiting sidekicks.
629 $this->process_sidekicks($sessionid);
631 return true;
635 return false;
638 public function dismiss_half($sessionid, $disconnect = true) {
639 if (!isset($this->conn_half[$sessionid])) {
640 return false;
642 if ($disconnect) {
643 foreach ($this->conn_half[$sessionid] as $handle) {
644 @socket_shutdown($handle);
645 @socket_close($handle);
648 unset($this->conn_half[$sessionid]);
649 return true;
652 public function dismiss_set($sessionid) {
653 if (!empty($this->conn_sets[$sessionid])) {
654 foreach ($this->conn_sets[$sessionid] as $handle) {
655 // Since we want to dismiss this, don't generate any errors if it's dead already.
656 @socket_shutdown($handle);
657 @socket_close($handle);
660 $chatroom = $this->sets_info[$sessionid]['chatid'];
661 $userid = $this->sets_info[$sessionid]['userid'];
662 unset($this->conn_sets[$sessionid]);
663 unset($this->sets_info[$sessionid]);
664 unset($this->chatrooms[$chatroom]['users'][$sessionid]);
665 $this->trace('Removed all traces of user with session '.$sessionid, E_USER_NOTICE);
666 return true;
669 public function dismiss_ufo($handle, $disconnect = true, $message = null) {
670 if (empty($this->conn_ufo)) {
671 return false;
673 foreach ($this->conn_ufo as $id => $ufo) {
674 if ($ufo->handle == $handle) {
675 unset($this->conn_ufo[$id]);
676 if ($disconnect) {
677 if (!empty($message)) {
678 $this->write_data($handle, $message."\n\n");
680 socket_shutdown($handle);
681 socket_close($handle);
683 return true;
686 return false;
689 public function conn_accept() {
690 $readsocket = array($this->listen_socket);
691 $write = null;
692 $except = null;
693 $changed = socket_select($readsocket, $write, $except, 0, 0);
695 if (!$changed) {
696 return false;
698 $handle = socket_accept($this->listen_socket);
699 if (!$handle) {
700 return false;
703 $newconn = new ChatConnection($handle);
704 $id = $this->new_ufo_id();
705 $this->conn_ufo[$id] = $newconn;
708 public function conn_activity_ufo(&$handles) {
709 $monitor = array();
710 if (!empty($this->conn_ufo)) {
711 foreach ($this->conn_ufo as $ufoid => $ufo) {
712 // Avoid socket_select() warnings by preventing the check over invalid resources.
713 if (is_resource($ufo->handle)) {
714 $monitor[$ufoid] = $ufo->handle;
715 } else {
716 $this->dismiss_ufo($ufo->handle, false);
721 if (empty($monitor)) {
722 $handles = array();
723 return 0;
726 $a = null;
727 $b = null;
728 $retval = socket_select($monitor, $a, $b, null);
729 $handles = $monitor;
731 return $retval;
734 public function message_broadcast($message, $sender) {
736 if (empty($this->conn_sets)) {
737 return true;
740 $now = time();
742 // First of all, mark this chatroom as having had activity now.
743 $this->chatrooms[$message->chatid]['lastactivity'] = $now;
745 foreach ($this->sets_info as $sessionid => $info) {
746 // We need to get handles from users that are in the same chatroom, same group.
747 if ($info['chatid'] == $message->chatid &&
748 ($info['groupid'] == $message->groupid || $message->groupid == 0)) {
750 // Simply give them the message.
751 $output = chat_format_message_manually($message, $info['courseid'], $sender, $info['user']);
752 if ($output !== false) {
753 $this->trace('Delivering message "'.$output->text.'" to ' .
754 $this->conn_sets[$sessionid][CHAT_CONNECTION_CHANNEL]);
756 if ($output->beep) {
757 $playscript = '(function() { var audioElement = document.createElement("audio");' . "\n";
758 $playscript .= 'audioElement.setAttribute("src", "'.$this->_beepsoundsrc.'");' . "\n";
759 $playscript .= 'audioElement.play(); })();' . "\n";
760 $this->write_data($this->conn_sets[$sessionid][CHAT_CONNECTION_CHANNEL],
761 '<script>' . $playscript . '</script>');
764 if ($info['quirks'] & QUIRK_CHUNK_UPDATE) {
765 $output->html .= $GLOBALS['CHAT_DUMMY_DATA'];
766 $output->html .= $GLOBALS['CHAT_DUMMY_DATA'];
767 $output->html .= $GLOBALS['CHAT_DUMMY_DATA'];
770 if (!$this->write_data($this->conn_sets[$sessionid][CHAT_CONNECTION_CHANNEL], $output->html)) {
771 $this->disconnect_session($sessionid);
778 public function disconnect_session($sessionid) {
779 global $DB;
781 $info = $this->sets_info[$sessionid];
783 $DB->delete_records('chat_users', array('sid' => $sessionid));
784 $msg = new stdClass;
785 $msg->chatid = $info['chatid'];
786 $msg->userid = $info['userid'];
787 $msg->groupid = $info['groupid'];
788 $msg->issystem = 1;
789 $msg->message = 'exit';
790 $msg->timestamp = time();
792 $this->trace('User has disconnected, destroying uid '.$info['userid'].' with SID '.$sessionid, E_USER_WARNING);
793 chat_send_chatmessage($info['chatuser'], $msg->message, true);
795 // IMPORTANT, kill him BEFORE broadcasting, otherwise we 'll get infinite recursion!
796 $latesender = $info['user'];
797 $this->dismiss_set($sessionid);
798 $this->message_broadcast($msg, $latesender);
801 public function fatal($message) {
802 $message .= "\n";
803 if ($this->_logfile) {
804 $this->trace($message, E_USER_ERROR);
806 echo "FATAL ERROR:: $message\n";
807 die();
810 public function init_sockets() {
811 global $CFG;
813 $this->trace('Setting up sockets');
815 if (false === ($this->listen_socket = socket_create(AF_INET, SOCK_STREAM, 0))) {
816 // Failed to create socket.
817 $lasterr = socket_last_error();
818 $this->fatal('socket_create() failed: '. socket_strerror($lasterr).' ['.$lasterr.']');
821 if (!socket_bind($this->listen_socket, $CFG->chat_serverip, $CFG->chat_serverport)) {
822 // Failed to bind socket.
823 $lasterr = socket_last_error();
824 $this->fatal('socket_bind() failed: '. socket_strerror($lasterr).' ['.$lasterr.']');
827 if (!socket_listen($this->listen_socket, $CFG->chat_servermax)) {
828 // Failed to get socket to listen.
829 $lasterr = socket_last_error();
830 $this->fatal('socket_listen() failed: '. socket_strerror($lasterr).' ['.$lasterr.']');
833 // Socket has been initialized and is ready.
834 $this->trace('Socket opened on port '.$CFG->chat_serverport);
836 // What exactly does this do? http://www.unixguide.net/network/socketfaq/4.5.shtml is still not enlightening enough for me.
837 socket_set_option($this->listen_socket, SOL_SOCKET, SO_REUSEADDR, 1);
838 socket_set_nonblock($this->listen_socket);
841 public function cli_switch($switch, $param = null) {
842 switch($switch) { // LOL!
843 case 'reset':
844 // Reset sockets.
845 $this->_resetsocket = true;
846 return false;
847 case 'start':
848 // Start the daemon.
849 $this->_readytogo = true;
850 return false;
851 break;
852 case 'v':
853 // Verbose mode.
854 $this->_trace_level = E_ALL;
855 return false;
856 break;
857 case 'l':
858 // Use logfile.
859 if (!empty($param)) {
860 $this->_logfile_name = $param;
862 $this->_logfile = @fopen($this->_logfile_name, 'a+');
863 if ($this->_logfile == false) {
864 $this->fatal('Failed to open '.$this->_logfile_name.' for writing');
866 return false;
867 default:
868 // Unrecognized.
869 $this->fatal('Unrecognized command line switch: '.$switch);
870 break;
872 return false;
877 $daemon = new ChatDaemon;
878 set_error_handler(array($daemon, 'error_handler'));
880 // Check the parameters.
882 unset($argv[0]);
883 $commandline = implode(' ', $argv);
884 if (strpos($commandline, '-') === false) {
885 if (!empty($commandline)) {
886 // We cannot have received any meaningful parameters.
887 $daemon->fatal('Garbage in command line');
889 } else {
890 // Parse command line.
891 $switches = preg_split('/(-{1,2}[a-zA-Z]+) */', $commandline, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
893 // Taking advantage of the fact that $switches is indexed with incrementing numeric keys.
894 // We will be using that to pass additional information to those switches who need it.
895 $numswitches = count($switches);
897 // Fancy way to give a "hyphen" boolean flag to each "switch".
898 $switches = array_map(function($x) {
899 return array("str" => $x, "hyphen" => (substr($x, 0, 1) == "-"));
900 }, $switches);
902 for ($i = 0; $i < $numswitches; ++$i) {
904 $switch = $switches[$i]['str'];
905 $params = ($i == $numswitches - 1 ? null :
906 ($switches[$i + 1]['hyphen'] ? null : trim($switches[$i + 1]['str']))
909 if (substr($switch, 0, 2) == '--') {
910 // Double-hyphen switch.
911 $daemon->cli_switch(strtolower(substr($switch, 2)), $params);
912 } else if (substr($switch, 0, 1) == '-') {
913 // Single-hyphen switch(es), may be more than one run together.
914 $switch = substr($switch, 1); // Get rid of the "-".
915 $len = strlen($switch);
916 for ($j = 0; $j < $len; ++$j) {
917 $daemon->cli_switch(strtolower(substr($switch, $j, 1)), $params);
923 if (!$daemon->query_start()) {
924 // For some reason we didn't start, so print out some info.
925 echo 'Starts the Moodle chat socket server on port '.$CFG->chat_serverport;
926 echo "\n\n";
927 echo "Usage: chatd.php [parameters]\n\n";
928 echo "Parameters:\n";
929 echo " --start Starts the daemon\n";
930 echo " -v Verbose mode (prints trivial information messages)\n";
931 echo " -l [logfile] Log all messages to logfile (if not specified, chatd.log)\n";
932 echo "Example:\n";
933 echo " chatd.php --start -l\n\n";
934 die();
937 if (!function_exists('socket_set_option')) {
938 echo "Error: Function socket_set_option() does not exist.\n";
939 echo "Possibly PHP has not been compiled with --enable-sockets.\n\n";
940 die();
943 $daemon->init_sockets();
945 $daemon->trace('Started Moodle chatd on port '.$CFG->chat_serverport.', listening socket '.$daemon->listen_socket, E_USER_WARNING);
947 // Clear the decks of old stuff.
948 $DB->delete_records('chat_users', array('version' => 'sockets'));
950 while (true) {
951 $active = array();
953 // First of all, let's see if any of our UFOs have identified itself.
954 if ($daemon->conn_activity_ufo($active)) {
955 foreach ($active as $handle) {
956 $readsocket = array($handle);
957 $write = null;
958 $except = null;
959 $changed = socket_select($readsocket, $write, $except, 0, 0);
961 if ($changed > 0) {
962 // Let's see what it has to say.
964 $data = socket_read($handle, 2048); // Should be more than 512 to prevent empty pages and repeated messages!
965 if (empty($data)) {
966 continue;
969 if (strlen($data) == 2048) { // If socket_read has more data, ignore all data.
970 $daemon->trace('UFO with '.$handle.': Data too long; connection closed', E_USER_WARNING);
971 $daemon->dismiss_ufo($handle, true, 'Data too long; connection closed');
972 continue;
975 // Ignore desktop browser fake "favorite icon" requests.
976 if (strpos($data, 'GET /favicon.ico HTTP') === 0) {
977 // Known malformed data, drop it without any further notice.
978 continue;
981 if (!preg_match('/win=(chat|users|message|beep).*&chat_sid=([a-zA-Z0-9]*) HTTP/', $data, $info)) {
982 // Malformed data.
983 $daemon->trace('UFO with '.$handle.': Request with malformed data; connection closed', E_USER_WARNING);
984 $daemon->dismiss_ufo($handle, true, 'Request with malformed data; connection closed');
985 continue;
988 $type = $info[1];
989 $sessionid = $info[2];
991 $customdata = array();
993 switch($type) {
994 case 'chat':
995 $type = CHAT_CONNECTION_CHANNEL;
996 $customdata['quirks'] = 0;
997 if (strpos($data, 'Safari')) {
998 $daemon->trace('Safari identified...', E_USER_WARNING);
999 $customdata['quirks'] += QUIRK_CHUNK_UPDATE;
1001 break;
1002 case 'users':
1003 $type = CHAT_SIDEKICK_USERS;
1004 break;
1005 case 'beep':
1006 $type = CHAT_SIDEKICK_BEEP;
1007 if (!preg_match('/beep=([^&]*)[& ]/', $data, $info)) {
1008 $daemon->trace('Beep sidekick did not contain a valid userid', E_USER_WARNING);
1009 $daemon->dismiss_ufo($handle, true, 'Request with malformed data; connection closed');
1010 continue 2;
1011 } else {
1012 $customdata = array('beep' => intval($info[1]));
1014 break;
1015 case 'message':
1016 $type = CHAT_SIDEKICK_MESSAGE;
1017 if (!preg_match('/chat_message=([^&]*)[& ]chat_msgidnr=([^&]*)[& ]/', $data, $info)) {
1018 $daemon->trace('Message sidekick did not contain a valid message', E_USER_WARNING);
1019 $daemon->dismiss_ufo($handle, true, 'Request with malformed data; connection closed');
1020 continue 2;
1021 } else {
1022 $customdata = array('message' => $info[1], 'index' => $info[2]);
1024 break;
1025 default:
1026 $daemon->trace('UFO with '.$handle.': Request with unknown type; connection closed', E_USER_WARNING);
1027 $daemon->dismiss_ufo($handle, true, 'Request with unknown type; connection closed');
1028 continue 2;
1029 break;
1032 // OK, now we know it's something good. Promote it and pass it all the data it needs.
1033 $daemon->promote_ufo($handle, $type, $sessionid, $customdata);
1034 continue;
1039 $now = time();
1041 // Clean up chatrooms with no activity as required.
1042 if ($now - $daemon->_last_idle_poll >= $daemon->_freq_poll_idle_chat) {
1043 $daemon->poll_idle_chats($now);
1046 // Finally, accept new connections.
1047 $daemon->conn_accept();
1049 usleep($daemon->_time_rest_socket);
1052 @socket_shutdown($daemon->listen_socket, 0);
1053 die("\n\n-- terminated --\n");