Merge branch 'MDL-31500_master-revert-ui-changes' of git://github.com/dmonllao/moodle
[moodle.git] / mod / chat / chatd.php
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.
13 //
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
19  *
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
23  */
25 define('CLI_SCRIPT', true);
27 require(dirname(dirname(dirname(__FILE__))).'/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);
64 }
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);
81     }
82 }
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.wav';
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;
114         }
115     }
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);
121         }
122         return true;
123     }
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);
143                             }
144                         }
145                     }
146                 }
147             }
148         }
149         $this->_last_idle_poll = $now;
150     }
152     public function query_start() {
153         return $this->_readytogo;
154     }
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;
170         }
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);
181             }
183             // Emit the message to wherever we should.
184             if ($this->_trace_to_stdout) {
185                 fwrite($this->_stdout, $message);
186                 fflush($this->_stdout);
187             }
188             if ($this->_trace_to_console) {
189                 echo $message;
190                 flush();
191             }
192             if ($this->_logfile) {
193                 fwrite($this->_logfile, $message);
194                 fflush($this->_logfile);
195             }
196         }
197     }
199     public function write_data($connection, $text) {
200         $written = @socket_write($connection, $text, strlen($text));
201         if ($written === false) {
202             return false;
203         }
204         return true;
205     }
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;
213         }
215         // Does promote_final() already finish its job?
216         if (!isset($this->sets_info[$sessionid]['lastinfocommit'])) {
217             return false;
218         }
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']);
228         }
229         return true;
230     }
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');
254         }
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);
270             }
271             windowobj.focus();
272             return false;
273         }
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>";
305         }
307         echo '</tbody></table>';
308         echo "</body>\n</html>\n";
310         return ob_get_clean();
312     }
314     public function new_ufo_id() {
315         static $id = 0;
316         if ($id++ === 0x1000000) { // Cycling very very slowly to prevent overflow.
317             $id = 0;
318         }
319         return $id;
320     }
322     public function process_sidekicks($sessionid) {
323         if (empty($this->conn_side[$sessionid])) {
324             return true;
325         }
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]);
330         }
331         return true;
332     }
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->system    = 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;
425                 }
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->system    = 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;
438                 }
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;
478         }
480         socket_shutdown($handle);
481         socket_close($handle);
482     }
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;
490         }
492         $chatuser = $DB->get_record('chat_users', array('sid' => $sessionid));
493         if ($chatuser === false) {
494             $this->dismiss_half($sessionid);
495             return false;
496         }
497         $chat = $DB->get_record('chat', array('id' => $chatuser->chatid));
498         if ($chat === false) {
499             $this->dismiss_half($sessionid);
500             return false;
501         }
502         $user = $DB->get_record('user', array('id' => $chatuser->userid));
503         if ($user === false) {
504             $this->dismiss_half($sessionid);
505             return false;
506         }
507         $course = $DB->get_record('course', array('id' => $chat->course));
508         if ($course === false) {
509             $this->dismiss_half($sessionid);
510             return false;
511         }
512         if (!($cm = get_coursemodule_from_instance('chat', $chat->id, $course->id))) {
513             $this->dismiss_half($sessionid);
514             return false;
515         }
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']
536         );
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;
544         }
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->system = 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;
581     }
583     public function promote_ufo($handle, $type, $sessionid, $customdata) {
584         if (empty($this->conn_ufo)) {
585             return false;
586         }
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);
601                     }
602                     return true;
603                 }
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;
617                     }
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;
632                 }
633             }
634         }
635         return false;
636     }
638     public function dismiss_half($sessionid, $disconnect = true) {
639         if (!isset($this->conn_half[$sessionid])) {
640             return false;
641         }
642         if ($disconnect) {
643             foreach ($this->conn_half[$sessionid] as $handle) {
644                 @socket_shutdown($handle);
645                 @socket_close($handle);
646             }
647         }
648         unset($this->conn_half[$sessionid]);
649         return true;
650     }
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);
658             }
659         }
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;
667     }
669     public function dismiss_ufo($handle, $disconnect = true, $message = null) {
670         if (empty($this->conn_ufo)) {
671             return false;
672         }
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");
679                     }
680                     socket_shutdown($handle);
681                     socket_close($handle);
682                 }
683                 return true;
684             }
685         }
686         return false;
687     }
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;
697         }
698         $handle = socket_accept($this->listen_socket);
699         if (!$handle) {
700             return false;
701         }
703         $newconn = new ChatConnection($handle);
704         $id = $this->new_ufo_id();
705         $this->conn_ufo[$id] = $newconn;
706     }
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);
717                 }
718             }
719         }
721         if (empty($monitor)) {
722             $handles = array();
723             return 0;
724         }
726         $a = null;
727         $b = null;
728         $retval = socket_select($monitor, $a, $b, null);
729         $handles = $monitor;
731         return $retval;
732     }
734     public function message_broadcast($message, $sender) {
736         if (empty($this->conn_sets)) {
737             return true;
738         }
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                         $this->write_data($this->conn_sets[$sessionid][CHAT_CONNECTION_CHANNEL],
758                                           '<embed src="'.$this->_beepsoundsrc.'" autostart="true" hidden="true" />');
759                     }
761                     if ($info['quirks'] & QUIRK_CHUNK_UPDATE) {
762                         $output->html .= $GLOBALS['CHAT_DUMMY_DATA'];
763                         $output->html .= $GLOBALS['CHAT_DUMMY_DATA'];
764                         $output->html .= $GLOBALS['CHAT_DUMMY_DATA'];
765                     }
767                     if (!$this->write_data($this->conn_sets[$sessionid][CHAT_CONNECTION_CHANNEL], $output->html)) {
768                         $this->disconnect_session($sessionid);
769                     }
770                 }
771             }
772         }
773     }
775     public function disconnect_session($sessionid) {
776         global $DB;
778         $info = $this->sets_info[$sessionid];
780         $DB->delete_records('chat_users', array('sid' => $sessionid));
781         $msg = new stdClass;
782         $msg->chatid = $info['chatid'];
783         $msg->userid = $info['userid'];
784         $msg->groupid = $info['groupid'];
785         $msg->system = 1;
786         $msg->message = 'exit';
787         $msg->timestamp = time();
789         $this->trace('User has disconnected, destroying uid '.$info['userid'].' with SID '.$sessionid, E_USER_WARNING);
790         chat_send_chatmessage($info['chatuser'], $msg->message, true);
792         // IMPORTANT, kill him BEFORE broadcasting, otherwise we 'll get infinite recursion!
793         $latesender = $info['user'];
794         $this->dismiss_set($sessionid);
795         $this->message_broadcast($msg, $latesender);
796     }
798     public function fatal($message) {
799         $message .= "\n";
800         if ($this->_logfile) {
801             $this->trace($message, E_USER_ERROR);
802         }
803         echo "FATAL ERROR:: $message\n";
804         die();
805     }
807     public function init_sockets() {
808         global $CFG;
810         $this->trace('Setting up sockets');
812         if (false === ($this->listen_socket = socket_create(AF_INET, SOCK_STREAM, 0))) {
813             // Failed to create socket.
814             $lasterr = socket_last_error();
815             $this->fatal('socket_create() failed: '. socket_strerror($lasterr).' ['.$lasterr.']');
816         }
818         if (!socket_bind($this->listen_socket, $CFG->chat_serverip, $CFG->chat_serverport)) {
819             // Failed to bind socket.
820             $lasterr = socket_last_error();
821             $this->fatal('socket_bind() failed: '. socket_strerror($lasterr).' ['.$lasterr.']');
822         }
824         if (!socket_listen($this->listen_socket, $CFG->chat_servermax)) {
825             // Failed to get socket to listen.
826             $lasterr = socket_last_error();
827             $this->fatal('socket_listen() failed: '. socket_strerror($lasterr).' ['.$lasterr.']');
828         }
830         // Socket has been initialized and is ready.
831         $this->trace('Socket opened on port '.$CFG->chat_serverport);
833         // What exactly does this do? http://www.unixguide.net/network/socketfaq/4.5.shtml is still not enlightening enough for me.
834         socket_set_option($this->listen_socket, SOL_SOCKET, SO_REUSEADDR, 1);
835         socket_set_nonblock($this->listen_socket);
836     }
838     public function cli_switch($switch, $param = null) {
839         switch($switch) { // LOL!
840             case 'reset':
841                 // Reset sockets.
842                 $this->_resetsocket = true;
843                 return false;
844             case 'start':
845                 // Start the daemon.
846                 $this->_readytogo = true;
847                 return false;
848             break;
849             case 'v':
850                 // Verbose mode.
851                 $this->_trace_level = E_ALL;
852                 return false;
853             break;
854             case 'l':
855                 // Use logfile.
856                 if (!empty($param)) {
857                     $this->_logfile_name = $param;
858                 }
859                 $this->_logfile = @fopen($this->_logfile_name, 'a+');
860                 if ($this->_logfile == false) {
861                     $this->fatal('Failed to open '.$this->_logfile_name.' for writing');
862                 }
863                 return false;
864             default:
865                 // Unrecognized.
866                 $this->fatal('Unrecognized command line switch: '.$switch);
867             break;
868         }
869         return false;
870     }
874 $daemon = new ChatDaemon;
875 set_error_handler(array($daemon, 'error_handler'));
877 // Check the parameters.
879 unset($argv[0]);
880 $commandline = implode(' ', $argv);
881 if (strpos($commandline, '-') === false) {
882     if (!empty($commandline)) {
883         // We cannot have received any meaningful parameters.
884         $daemon->fatal('Garbage in command line');
885     }
886 } else {
887     // Parse command line.
888     $switches = preg_split('/(-{1,2}[a-zA-Z]+) */', $commandline, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
890     // Taking advantage of the fact that $switches is indexed with incrementing numeric keys.
891     // We will be using that to pass additional information to those switches who need it.
892     $numswitches = count($switches);
894     // Fancy way to give a "hyphen" boolean flag to each "switch".
895     $switches = array_map(create_function('$x', 'return array("str" => $x, "hyphen" => (substr($x, 0, 1) == "-"));'), $switches);
897     for ($i = 0; $i < $numswitches; ++$i) {
899         $switch = $switches[$i]['str'];
900         $params = ($i == $numswitches - 1 ? null :
901                                             ($switches[$i + 1]['hyphen'] ? null : trim($switches[$i + 1]['str']))
902                   );
904         if (substr($switch, 0, 2) == '--') {
905             // Double-hyphen switch.
906             $daemon->cli_switch(strtolower(substr($switch, 2)), $params);
907         } else if (substr($switch, 0, 1) == '-') {
908             // Single-hyphen switch(es), may be more than one run together.
909             $switch = substr($switch, 1); // Get rid of the "-".
910             $len = strlen($switch);
911             for ($j = 0; $j < $len; ++$j) {
912                 $daemon->cli_switch(strtolower(substr($switch, $j, 1)), $params);
913             }
914         }
915     }
918 if (!$daemon->query_start()) {
919     // For some reason we didn't start, so print out some info.
920     echo 'Starts the Moodle chat socket server on port '.$CFG->chat_serverport;
921     echo "\n\n";
922     echo "Usage: chatd.php [parameters]\n\n";
923     echo "Parameters:\n";
924     echo "  --start         Starts the daemon\n";
925     echo "  -v              Verbose mode (prints trivial information messages)\n";
926     echo "  -l [logfile]    Log all messages to logfile (if not specified, chatd.log)\n";
927     echo "Example:\n";
928     echo "  chatd.php --start -l\n\n";
929     die();
932 if (!function_exists('socket_set_option')) {
933     echo "Error: Function socket_set_option() does not exist.\n";
934     echo "Possibly PHP has not been compiled with --enable-sockets.\n\n";
935     die();
938 $daemon->init_sockets();
940 $daemon->trace('Started Moodle chatd on port '.$CFG->chat_serverport.', listening socket '.$daemon->listen_socket, E_USER_WARNING);
942 // Clear the decks of old stuff.
943 $DB->delete_records('chat_users', array('version' => 'sockets'));
945 while (true) {
946     $active = array();
948     // First of all, let's see if any of our UFOs have identified itself.
949     if ($daemon->conn_activity_ufo($active)) {
950         foreach ($active as $handle) {
951             $readsocket = array($handle);
952             $write = null;
953             $except = null;
954             $changed = socket_select($readsocket, $write, $except, 0, 0);
956             if ($changed > 0) {
957                 // Let's see what it has to say.
959                 $data = socket_read($handle, 2048); // Should be more than 512 to prevent empty pages and repeated messages!
960                 if (empty($data)) {
961                     continue;
962                 }
964                 if (strlen($data) == 2048) { // If socket_read has more data, ignore all data.
965                     $daemon->trace('UFO with '.$handle.': Data too long; connection closed', E_USER_WARNING);
966                     $daemon->dismiss_ufo($handle, true, 'Data too long; connection closed');
967                     continue;
968                 }
970                 // Ignore desktop browser fake "favorite icon" requests.
971                 if (strpos($data, 'GET /favicon.ico HTTP') === 0) {
972                     // Known malformed data, drop it without any further notice.
973                     continue;
974                 }
976                 if (!preg_match('/win=(chat|users|message|beep).*&chat_sid=([a-zA-Z0-9]*) HTTP/', $data, $info)) {
977                     // Malformed data.
978                     $daemon->trace('UFO with '.$handle.': Request with malformed data; connection closed', E_USER_WARNING);
979                     $daemon->dismiss_ufo($handle, true, 'Request with malformed data; connection closed');
980                     continue;
981                 }
983                 $type      = $info[1];
984                 $sessionid = $info[2];
986                 $customdata = array();
988                 switch($type) {
989                     case 'chat':
990                         $type = CHAT_CONNECTION_CHANNEL;
991                         $customdata['quirks'] = 0;
992                         if (strpos($data, 'Safari')) {
993                             $daemon->trace('Safari identified...', E_USER_WARNING);
994                             $customdata['quirks'] += QUIRK_CHUNK_UPDATE;
995                         }
996                     break;
997                     case 'users':
998                         $type = CHAT_SIDEKICK_USERS;
999                     break;
1000                     case 'beep':
1001                         $type = CHAT_SIDEKICK_BEEP;
1002                         if (!preg_match('/beep=([^&]*)[& ]/', $data, $info)) {
1003                             $daemon->trace('Beep sidekick did not contain a valid userid', E_USER_WARNING);
1004                             $daemon->dismiss_ufo($handle, true, 'Request with malformed data; connection closed');
1005                             continue;
1006                         } else {
1007                             $customdata = array('beep' => intval($info[1]));
1008                         }
1009                     break;
1010                     case 'message':
1011                         $type = CHAT_SIDEKICK_MESSAGE;
1012                         if (!preg_match('/chat_message=([^&]*)[& ]chat_msgidnr=([^&]*)[& ]/', $data, $info)) {
1013                             $daemon->trace('Message sidekick did not contain a valid message', E_USER_WARNING);
1014                             $daemon->dismiss_ufo($handle, true, 'Request with malformed data; connection closed');
1015                             continue;
1016                         } else {
1017                             $customdata = array('message' => $info[1], 'index' => $info[2]);
1018                         }
1019                     break;
1020                     default:
1021                         $daemon->trace('UFO with '.$handle.': Request with unknown type; connection closed', E_USER_WARNING);
1022                         $daemon->dismiss_ufo($handle, true, 'Request with unknown type; connection closed');
1023                         continue;
1024                     break;
1025                 }
1027                 // OK, now we know it's something good. Promote it and pass it all the data it needs.
1028                 $daemon->promote_ufo($handle, $type, $sessionid, $customdata);
1029                 continue;
1030             }
1031         }
1032     }
1034     $now = time();
1036     // Clean up chatrooms with no activity as required.
1037     if ($now - $daemon->_last_idle_poll >= $daemon->_freq_poll_idle_chat) {
1038         $daemon->poll_idle_chats($now);
1039     }
1041     // Finally, accept new connections.
1042     $daemon->conn_accept();
1044     usleep($daemon->_time_rest_socket);
1047 @socket_shutdown($daemon->listen_socket, 0);
1048 die("\n\n-- terminated --\n");