Mirroring the language update from the 1.4 branch yesterday.
[moodle.git] / mod / chat / chatd.php
CommitLineData
34308732 1#!/usr/bin/php -q
8e7eec60 2<?php
3
5a60e822 4define('QUIRK_CHUNK_UPDATE', 0x0001);
5
6fb2f823 6echo "Moodle chat daemon v1.0 on PHP ".phpversion()." (\$Id$)\n\n";
8e7eec60 7
8/// Set up all the variables we need /////////////////////////////////////
9
10/// $CFG variables are now defined in database by chat/lib.php
11
12$_SERVER['PHP_SELF'] = "dummy";
13$_SERVER['SERVER_NAME'] = "dummy";
14
15include('../../config.php');
16include('lib.php');
17
18$_SERVER['SERVER_NAME'] = $CFG->chat_serverhost;
19$_SERVER['PHP_SELF'] = "http://$CFG->chat_serverhost:$CFG->chat_serverport/mod/chat/chatd.php";
20
21$safemode = ini_get('safe_mode');
22
23if(!empty($safemode)) {
24 die("Error: Cannot run with PHP safe_mode = On. Turn off safe_mode.\n");
25}
26
27@set_time_limit (0);
28set_magic_quotes_runtime(0);
29
30error_reporting(E_ALL);
31
32function chat_empty_connection() {
33 return array('sid' => NULL, 'handle' => NULL, 'ip' => NULL, 'port' => NULL, 'groupid' => NULL);
34}
35
36class ChatConnection {
e7d27884 37 // Chat-related info
38 var $sid = NULL;
39 var $type = NULL;
40 //var $groupid = NULL;
8e7eec60 41
e7d27884 42 // PHP-level info
43 var $handle = NULL;
8e7eec60 44
e7d27884 45 // TCP/IP
46 var $ip = NULL;
47 var $port = NULL;
8e7eec60 48
e7d27884 49 function ChatConnection($resource) {
50 $this->handle = $resource;
51 socket_getpeername($this->handle, &$this->ip, &$this->port);
52 }
8e7eec60 53}
54
55class ChatDaemon {
6fb2f823 56 var $_resetsocket = false;
57 var $_readytogo = false;
58 var $_logfile = false;
59 var $_trace_to_console = true;
60 var $_trace_to_stdout = true;
61 var $_logfile_name = 'chatd.log';
62 var $_last_idle_poll = 0;
63
64 var $conn_ufo = array(); // Connections not identified yet
8e7eec60 65 var $conn_side = array(); // Sessions with sidekicks waiting for the main connection to be processed
66 var $conn_half = array(); // Sessions that have valid connections but not all of them
67 var $conn_sets = array(); // Sessions with complete connection sets sets
68 var $sets_info = array(); // Keyed by sessionid exactly like conn_sets, one of these for each of those
6fb2f823 69 var $chatrooms = array(); // Keyed by chatid, holding arrays of data
8e7eec60 70
d9e8ba0a 71 function ChatDaemon() {
d9e8ba0a 72 $this->_trace_level = E_ALL ^ E_USER_NOTICE;
73 $this->_pcntl_exists = function_exists('pcntl_fork');
74 $this->_time_rest_socket = 20;
75 $this->_beepsoundsrc = $GLOBALS['CFG']->wwwroot.'/mod/chat/beep.wav';
76 $this->_freq_update_records = 15;
6fb2f823 77 $this->_freq_poll_idle_chat = 35;
78 $this->_stdout = fopen('php://stdout', 'w');
79 if($this->_stdout) {
80 // Avoid double traces for everything
81 $this->_trace_to_console = false;
82 }
83 }
84
85 function poll_idle_chats($now) {
86 $this->trace('Polling chats to detect disconnected users');
87 if(!empty($this->chatrooms)) {
88 foreach($this->chatrooms as $chatid => $chatroom) {
89 if(!empty($chatroom['users'])) {
90 foreach($chatroom['users'] as $sessionid => $userid) {
91 // We will be polling each user as required
92 if($this->sets_info[$sessionid]['chatuser']->lastmessageping < $this->_last_idle_poll) {
93 // This user hasn't been polled since his last message
94 if($this->write_data($this->conn_sets[$sessionid][CHAT_CONNECTION_CHANNEL], '<!-- poll -->') === false) {
95 // User appears to have disconnected
96 $this->disconnect_session($sessionid);
97 }
98 }
99 }
100 }
101 }
102 }
103 $this->_last_idle_poll = $now;
d9e8ba0a 104 }
105
106 function query_start() {
107 return $this->_readytogo;
108 }
109
110 function trace($message, $level = E_USER_NOTICE) {
111 $severity = '';
112
113 switch($level) {
114 case E_USER_WARNING: $severity = '*IMPORTANT* '; break;
115 case E_USER_ERROR: $severity = ' *CRITICAL* '; break;
116 }
117
118 $date = date('[Y-m-d H:i:s] ');
119 $message = $date.$severity.$message."\n";
120
121 if ($this->_trace_level & $level) {
122 // It is accepted for output
123
124 // Error-class traces go to STDERR too
125 if($level & E_USER_ERROR) {
126 fwrite(STDERR, $message);
127 }
128
129 // Emit the message to wherever we should
130 if($this->_trace_to_stdout) {
6fb2f823 131 fwrite($this->_stdout, $message);
132 fflush($this->_stdout);
d9e8ba0a 133 }
134 if($this->_trace_to_console) {
135 echo $message;
136 flush();
137 }
138 if($this->_logfile) {
139 fwrite($this->_logfile, $message);
140 fflush($this->_logfile);
141 }
142 }
143 }
144
6fb2f823 145 function write_data($connection, $text) {
146 $written = @socket_write($connection, $text, strlen($text));
147 if($written === false) {
148 // $this->trace("socket_write() failed: reason: " . socket_strerror(socket_last_error($connection)));
149 return false;
150 }
151 return true;
152
153 // Enclosing the above code inside this blocks makes sure that
154 // "a socket write operation will not block". I 'm not so sure
155 // if this is needed, as we have a nonblocking socket anyway.
156 // If trouble starts to creep up, we 'll restore this.
157// $check_socket = array($connection);
158// $socket_changed = socket_select($read = NULL, $check_socket, $except = NULL, 0, 0);
159// if($socket_changed > 0) {
160//
161// // ABOVE CODE GOES HERE
162//
163// }
164// return false;
165 }
166
82a524ef 167 function update_lastmessageping($sessionid, $time = NULL) {
168 // TODO: this can and should be written as a single UPDATE query
169 if(empty($this->sets_info[$sessionid])) {
d9e8ba0a 170 $this->trace('update_lastmessageping() called for an invalid SID: '.$sessionid, E_USER_WARNING);
82a524ef 171 return false;
172 }
173
82a524ef 174 if(empty($time)) {
6fb2f823 175 $time = time();
82a524ef 176 }
177
178 // We 'll be cheating a little, and NOT updating lastmessageping
179 // as often as we have to, so we can save on DB queries (imagine MANY users)
180 $this->sets_info[$sessionid]['chatuser']->lastmessageping = $time;
d9e8ba0a 181 $this->sets_info[$sessionid]['chatuser']->lastping = $time;
82a524ef 182
183 // This will set it just fine for bookkeeping purposes.
6fb2f823 184 if($time - $this->sets_info[$sessionid]['lastinfocommit'] > $this->_freq_update_records) {
82a524ef 185 // commit to permanent storage
d9e8ba0a 186 // $this->trace('Committing volatile lastmessageping for session '.$sessionid);
6fb2f823 187 $this->sets_info[$sessionid]['lastinfocommit'] = $time;
82a524ef 188 update_record('chat_users', $this->sets_info[$sessionid]['chatuser']);
189 }
190 return true;
191 }
192
b5de723d 193 function get_user_window($sessionid) {
82a524ef 194
195 global $CFG, $THEME;
196
197 static $str;
198
199 $info = &$this->sets_info[$sessionid];
200 $oldlang = chat_language_override($info['lang']);
201
202 $timenow = time();
203
204 if (empty($str)) {
205 $str->idle = get_string("idle", "chat");
206 $str->beep = get_string("beep", "chat");
207 $str->day = get_string("day");
208 $str->days = get_string("days");
209 $str->hour = get_string("hour");
210 $str->hours = get_string("hours");
211 $str->min = get_string("min");
212 $str->mins = get_string("mins");
213 $str->sec = get_string("sec");
214 $str->secs = get_string("secs");
215 }
216
b5de723d 217 ob_start();
82a524ef 218 echo '<html><head>';
219 echo '<script language="JavaScript">';
f0232c84 220 echo "<!-- //hide\n";
82a524ef 221
222 echo 'function openpopup(url,name,options,fullscreen) {';
223 echo 'fullurl = "'.$CFG->wwwroot.'" + url;';
224 echo 'windowobj = window.open(fullurl,name,options);';
225 echo 'if (fullscreen) {';
226 echo ' windowobj.moveTo(0,0);';
227 echo ' windowobj.resizeTo(screen.availWidth,screen.availHeight); ';
228 echo '}';
229 echo 'windowobj.focus();';
230 echo 'return false;';
f0232c84 231 echo "}\n-->\n";
82a524ef 232 echo '</script></head><body style="font-face: serif;" bgcolor="'.$THEME->body.'">';
233
234 echo '<table style="width: 100%;"><tbody>';
235 if(empty($this->sets_info)) {
236 // No users
237 echo '<tr><td>&nbsp;</td></tr>';
238 }
239 else {
240 foreach ($this->sets_info as $usersid => $userinfo) {
241 $lastping = $timenow - $userinfo['chatuser']->lastmessageping;
dfd629d7 242 $popuppar = '\'/user/view.php?id='.$userinfo['user']->id.'&amp;course='.$userinfo['courseid'].'\',\'user'.$userinfo['chatuser']->id.'\',\'\'';
82a524ef 243 echo '<tr><td width="35">';
dfd629d7 244 echo '<a target="_new" onclick="return openpopup('.$popuppar.');" href="'.$CFG->wwwroot.'/user/view.php?id='.$userinfo['chatuser']->id.'&amp;course='.$userinfo['courseid'].'">';
82a524ef 245 print_user_picture($userinfo['user']->id, 0, $userinfo['user']->picture, false, false, false);
246 echo "</a></td><td valign=center>";
247 echo "<p><font size=1>";
248 echo fullname($userinfo['user'])."<br />";
e7d27884 249 echo "<font color=\"#888888\">$str->idle: ".format_time($lastping, $str)."</font> ";
250 echo '<a target="empty" href="http://'.$CFG->chat_serverhost.':'.$CFG->chat_serverport.'/?win=beep&beep='.$userinfo['user']->id.
251 '&chat_sid='.$sessionid.'&groupid='.$this->sets_info[$sessionid]['groupid'].'">'.$str->beep."</a>\n";
82a524ef 252 echo "</font></p>";
253 echo "<td></tr>";
254 }
255 }
256 echo '</tbody></table>';
b5de723d 257
fbabbd23 258 // About 2K of HTML comments to force browsers to render the HTML
f0232c84 259 // echo $GLOBALS['CHAT_DUMMY_DATA'];
fbabbd23 260
b5de723d 261 echo "</body>\n</html>\n";
82a524ef 262
263 chat_language_restore($oldlang);
b5de723d 264 return ob_get_clean();
265
266 }
267
8e7eec60 268 function new_ufo_id() {
269 static $id = 0;
270 if($id++ === 0x1000000) { // Cycling very very slowly to prevent overflow
271 $id = 0;
272 }
273 return $id;
274 }
275
276 function process_sidekicks($sessionid) {
277 if(empty($this->conn_side[$sessionid])) {
278 return true;
279 }
280 foreach($this->conn_side[$sessionid] as $sideid => $sidekick) {
e7d27884 281 // TODO: is this late-dispatch working correctly?
8e7eec60 282 $this->dispatch_sidekick($sidekick['handle'], $sidekick['type'], $sessionid, $sidekick['customdata']);
283 unset($this->conn_side[$sessionid][$sideid]);
284 }
285 return true;
286 }
287
288 function dispatch_sidekick($handle, $type, $sessionid, $customdata) {
289 global $CFG;
290
291 switch($type) {
e7d27884 292 case CHAT_SIDEKICK_BEEP:
293 // Incoming beep
294 $msg = &New stdClass;
295 $msg->chatid = $this->sets_info[$sessionid]['chatid'];
296 $msg->userid = $this->sets_info[$sessionid]['userid'];
297 $msg->groupid = $this->sets_info[$sessionid]['groupid'];
298 $msg->system = 0;
299 $msg->message = 'beep '.$customdata['beep'];
300 $msg->timestamp = time();
301
302 // Commit to DB
303 insert_record('chat_messages', $msg);
304
305 // OK, now push it out to all users
306 $this->message_broadcast($msg, $this->sets_info[$sessionid]['user']);
307
308 // Update that user's lastmessageping
309 $this->update_lastmessageping($sessionid, $msg->timestamp);
310
311 // We did our work, but before slamming the door on the poor browser
312 // show the courtesy of responding to the HTTP request. Otherwise, some
313 // browsers decide to get vengeance by flooding us with repeat requests.
314
315 $header = "HTTP/1.1 200 OK\n";
316 $header .= "Connection: close\n";
317 $header .= "Date: ".date('r')."\n";
318 $header .= "Server: Moodle\n";
319 $header .= "Content-Type: text/html\n";
320 $header .= "Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT\n";
321 $header .= "Cache-Control: no-cache, must-revalidate\n";
322 $header .= "Expires: Wed, 4 Oct 1978 09:32:45 GMT\n";
323 $header .= "\n";
324
325 // That's enough headers for one lousy dummy response
6fb2f823 326 $this->write_data($handle, $header);
e7d27884 327 // All done
328 break;
329
8e7eec60 330 case CHAT_SIDEKICK_USERS:
fbabbd23 331 // A request to paint a user window
b5de723d 332
333 $content = $this->get_user_window($sessionid);
334
335 $header = "HTTP/1.1 200 OK\n";
336 $header .= "Connection: close\n";
337 $header .= "Date: ".date('r')."\n";
338 $header .= "Server: Moodle\n";
339 $header .= "Content-Type: text/html\n";
340 $header .= "Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT\n";
341 $header .= "Cache-Control: no-cache, must-revalidate\n";
342 $header .= "Expires: Wed, 4 Oct 1978 09:32:45 GMT\n";
343 $header .= "Content-Length: ".strlen($content)."\n";
6fb2f823 344
345 // The refresh value is 2 seconds higher than the configuration variable because we are doing JS refreshes all the time.
346 // However, if the JS doesn't work for some reason, we still want to refresh once in a while.
347 $header .= "Refresh: ".(intval($CFG->chat_refresh_userlist) + 2)."; URL=http://$CFG->chat_serverhost:$CFG->chat_serverport/?win=users&".
b5de723d 348 "chat_sid=".$sessionid."&groupid=".$this->sets_info[$sessionid]['groupid']."\n";
349 $header .= "\n";
350
351 // That's enough headers for one lousy dummy response
d9e8ba0a 352 $this->trace('writing users http response to handle '.$handle);
6fb2f823 353 $this->write_data($handle, $header . $content);
b5de723d 354
82a524ef 355/*
356 $header = "HTTP/1.1 200 OK\n";
357 $header .= "Connection: close\n";
358 $header .= "Date: ".date('r')."\n";
359 $header .= "Server: Moodle\n";
360 $header .= "Content-Type: text/html\n";
361 $header .= "Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT\n";
362 $header .= "Cache-Control: no-cache, must-revalidate\n";
363 $header .= "Expires: Wed, 4 Oct 1978 09:32:45 GMT\n";
364 $header .= "\n";
d9e8ba0a 365 $this->trace('writing users http response to handle '.$handle);
6fb2f823 366 $this->write_data($handle, $header);
82a524ef 367*/
8e7eec60 368 break;
369 case CHAT_SIDEKICK_MESSAGE:
370 // Incoming message
dfd629d7 371
372 // Browser stupidity protection from duplicate messages:
373 $messageindex = intval($customdata['index']);
d9e8ba0a 374
dfd629d7 375 if($this->sets_info[$sessionid]['lastmessageindex'] >= $messageindex) {
376 // We have already broadcasted that!
6fb2f823 377 // $this->trace('discarding message with stale index');
dfd629d7 378 break;
379 }
380 else {
381 // Update our info
382 $this->sets_info[$sessionid]['lastmessageindex'] = $messageindex;
383 }
384
e7d27884 385 $msg = &New stdClass;
8e7eec60 386 $msg->chatid = $this->sets_info[$sessionid]['chatid'];
387 $msg->userid = $this->sets_info[$sessionid]['userid'];
388 $msg->groupid = $this->sets_info[$sessionid]['groupid'];
389 $msg->system = 0;
390 $msg->message = urldecode($customdata['message']); // have to undo the browser's encoding
391 $msg->timestamp = time();
392
393 if(empty($msg->message)) {
394 // Someone just hit ENTER, send them on their way
395 break;
396 }
397
b5de723d 398 // Commit to DB
399 insert_record('chat_messages', $msg);
8e7eec60 400
401 // OK, now push it out to all users
b5de723d 402 $this->message_broadcast($msg, $this->sets_info[$sessionid]['user']);
8e7eec60 403
404 // Update that user's lastmessageping
82a524ef 405 $this->update_lastmessageping($sessionid, $msg->timestamp);
8e7eec60 406
b5de723d 407 // We did our work, but before slamming the door on the poor browser
408 // show the courtesy of responding to the HTTP request. Otherwise, some
409 // browsers decide to get vengeance by flooding us with repeat requests.
410
411 $header = "HTTP/1.1 200 OK\n";
412 $header .= "Connection: close\n";
413 $header .= "Date: ".date('r')."\n";
414 $header .= "Server: Moodle\n";
415 $header .= "Content-Type: text/html\n";
416 $header .= "Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT\n";
417 $header .= "Cache-Control: no-cache, must-revalidate\n";
418 $header .= "Expires: Wed, 4 Oct 1978 09:32:45 GMT\n";
419 $header .= "\n";
420
421 // That's enough headers for one lousy dummy response
6fb2f823 422 $this->write_data($handle, $header);
b5de723d 423
8e7eec60 424 // All done
425 break;
426 }
427
428 socket_shutdown($handle);
429 socket_close($handle);
430 }
431
a8185f2e 432 function promote_final($sessionid, $groupid, $customdata) {
8e7eec60 433 if(isset($this->conn_sets[$sessionid])) {
d9e8ba0a 434 $this->trace('Set cannot be finalized: Session '.$sessionid.' is already active');
8e7eec60 435 return false;
436 }
437
438 $chatuser = get_record('chat_users', 'sid', $sessionid);
439 if($chatuser === false) {
440 $this->dismiss_half($sessionid);
441 return false;
442 }
443 $chat = get_record('chat', 'id', $chatuser->chatid);
444 if($chat === false) {
445 $this->dismiss_half($sessionid);
446 return false;
447 }
b5de723d 448 $user = get_record('user', 'id', $chatuser->userid);
449 if($user === false) {
450 $this->dismiss_half($sessionid);
451 return false;
452 }
453 $course = get_record('course', 'id', $chat->course); {
454 if($course === false) {
455 $this->dismiss_half($sessionid);
456 return false;
457 }
458 }
8e7eec60 459
b5de723d 460 global $CHAT_HTMLHEAD_JS, $CFG;
461
462 // A really sad thing, to have to do this by hand.... :-(
463 $lang = NULL;
464 if(empty($lang) && !empty($course->lang)) {
465 $lang = $course->lang;
466 }
467 if(empty($lang) && !empty($user->lang)) {
468 $lang = $user->lang;
469 }
470 if(empty($lang)) {
471 $lang = $CFG->lang;
472 }
8e7eec60 473
474 $this->conn_sets[$sessionid] = $this->conn_half[$sessionid];
82a524ef 475
476 // This whole thing needs to be purged of redundant info, and the
477 // code base to follow suit. But AFTER development is done.
b5de723d 478 $this->sets_info[$sessionid] = array(
82a524ef 479 'lastinfocommit' => 0,
dfd629d7 480 'lastmessageindex' => 0,
82a524ef 481 'courseid' => $course->id,
482 'chatuser' => $chatuser,
6fb2f823 483 'chatid' => $chat->id,
b5de723d 484 'user' => $user,
6fb2f823 485 'userid' => $user->id,
b5de723d 486 'groupid' => $groupid,
5a60e822 487 'lang' => $lang,
a8185f2e 488 'quirks' => $customdata['quirks']
b5de723d 489 );
490
6fb2f823 491 // If we know nothing about this chatroom, initialize it and add the user
492 if(!isset($this->chatrooms[$chat->id]['users'])) {
493 $this->chatrooms[$chat->id]['users'] = array($sessionid => $user->id);
494 }
495 else {
496 // Otherwise just add the user
497 $this->chatrooms[$chat->id]['users'][$sessionid] = $user->id;
498 }
499
500 // $this->trace('QUIRKS value for this connection is '.$customdata['quirks']);
5a60e822 501
8e7eec60 502 $this->dismiss_half($sessionid, false);
6fb2f823 503 $this->write_data($this->conn_sets[$sessionid][CHAT_CONNECTION_CHANNEL], $CHAT_HTMLHEAD_JS);
504 $this->trace('Connection accepted: '.$this->conn_sets[$sessionid][CHAT_CONNECTION_CHANNEL].', SID: '.$sessionid.' UID: '.$chatuser->userid.' GID: '.intval($groupid), E_USER_WARNING);
8e7eec60 505
506 // Finally, broadcast the "entered the chat" message
507
e7d27884 508 $msg = &New stdClass;
8e7eec60 509 $msg->chatid = $chatuser->chatid;
510 $msg->userid = $chatuser->userid;
511 $msg->groupid = 0;
512 $msg->system = 1;
513 $msg->message = 'enter';
514 $msg->timestamp = time();
515
516 insert_record('chat_messages', $msg);
b5de723d 517 $this->message_broadcast($msg, $this->sets_info[$sessionid]['user']);
8e7eec60 518
519 return true;
520 }
521
522 function promote_ufo($handle, $type, $sessionid, $groupid, $customdata) {
523 if(empty($this->conn_ufo)) {
524 return false;
525 }
526 foreach($this->conn_ufo as $id => $ufo) {
e7d27884 527 if($ufo->handle == $handle) {
8e7eec60 528 // OK, got the id of the UFO, but what is it?
529
530 if($type & CHAT_SIDEKICK) {
531 // Is the main connection ready?
532 if(isset($this->conn_sets[$sessionid])) {
533 // Yes, so dispatch this sidekick now and be done with it
d9e8ba0a 534 //$this->trace('Dispatching sidekick immediately');
8e7eec60 535 $this->dispatch_sidekick($handle, $type, $sessionid, $customdata);
536 $this->dismiss_ufo($handle, false);
537 }
538 else {
539 // No, so put it in the waiting list
d9e8ba0a 540 $this->trace('sidekick waiting');
8e7eec60 541 $this->conn_side[$sessionid][] = array('type' => $type, 'handle' => $handle, 'customdata' => $customdata);
542 }
543 return true;
544 }
545
546 // If it's not a sidekick, at this point it can only be da man
547
548 if($type & CHAT_CONNECTION) {
549 // This forces a new connection right now...
d9e8ba0a 550 $this->trace('Incoming connection from '.$ufo->ip.':'.$ufo->port);
8e7eec60 551
552 // Do we have such a connection active?
553 if(isset($this->conn_sets[$sessionid])) {
554 // Yes, so regrettably we cannot promote you
d9e8ba0a 555 $this->trace('Connection rejected: session '.$sessionid.' is already final');
8e7eec60 556 $this->dismiss_ufo($handle);
557 return false;
558 }
559
560 // Join this with what we may have already
561 $this->conn_half[$sessionid][$type] = $handle;
562
563 // Do the bookkeeping
a8185f2e 564 $this->promote_final($sessionid, $groupid, $customdata);
8e7eec60 565
566 // It's not an UFO anymore
567 $this->dismiss_ufo($handle, false);
568
569 // Dispatch waiting sidekicks
570 $this->process_sidekicks($sessionid);
571
572 return true;
573 }
8e7eec60 574 }
575 }
576 return false;
577 }
578
579 function dismiss_half($sessionid, $disconnect = true) {
580 if(!isset($this->conn_half[$sessionid])) {
581 return false;
582 }
583 if($disconnect) {
584 foreach($this->conn_half[$sessionid] as $handle) {
e7d27884 585 @socket_shutdown($handle);
586 @socket_close($handle);
8e7eec60 587 }
588 }
589 unset($this->conn_half[$sessionid]);
590 return true;
591 }
592
593 function dismiss_set($sessionid) {
e7d27884 594 if(!empty($this->conn_sets[$sessionid])) {
595 foreach($this->conn_sets[$sessionid] as $handle) {
596 // Since we want to dismiss this, don't generate any errors if it's dead already
597 @socket_shutdown($handle);
598 @socket_close($handle);
599 }
8e7eec60 600 }
6fb2f823 601 $chatroom = $this->sets_info[$sessionid]['chatid'];
602 $userid = $this->sets_info[$sessionid]['userid'];
8e7eec60 603 unset($this->conn_sets[$sessionid]);
604 unset($this->sets_info[$sessionid]);
6fb2f823 605 unset($this->chatrooms[$chatroom]['users'][$sessionid]);
8e7eec60 606 return true;
607 }
608
609
610 function dismiss_ufo($handle, $disconnect = true) {
611 if(empty($this->conn_ufo)) {
612 return false;
613 }
614 foreach($this->conn_ufo as $id => $ufo) {
e7d27884 615 if($ufo->handle == $handle) {
8e7eec60 616 unset($this->conn_ufo[$id]);
617 if($disconnect) {
6fb2f823 618 $this->write_data($handle, "You don't seem to be a valid client.\n");
8e7eec60 619 socket_shutdown($handle);
620 socket_close($handle);
621 }
622 return true;
623 }
624 }
625 return false;
626 }
627
628 function conn_accept() {
629 $handle = @socket_accept($this->listen_socket);
630 if(!$handle) {
631 return false;
632 }
633
e7d27884 634 $newconn = &New ChatConnection($handle);
8e7eec60 635 $id = $this->new_ufo_id();
e7d27884 636 $this->conn_ufo[$id] = $newconn;
8e7eec60 637
d9e8ba0a 638 //$this->trace('UFO #'.$id.': connection from '.$newconn->ip.' on port '.$newconn->port.', '.$newconn->handle);
8e7eec60 639 }
640
641 function conn_activity_ufo (&$handles) {
642 $monitor = array();
643 if(!empty($this->conn_ufo)) {
644 foreach($this->conn_ufo as $ufoid => $ufo) {
e7d27884 645 $monitor[$ufoid] = $ufo->handle;
8e7eec60 646 }
647 }
648
649 if(empty($monitor)) {
650 $handles = array();
651 return 0;
652 }
653
654 $retval = socket_select($monitor, $a = NULL, $b = NULL, NULL);
655 $handles = $monitor;
656
657 return $retval;
658 }
659
b5de723d 660 function message_broadcast($message, $sender) {
8e7eec60 661 if(empty($this->conn_sets)) {
662 return true;
663 }
664
6fb2f823 665 $now = time();
666
667 // First of all, mark this chatroom as having had activity now
668 $this->chatrooms[$message->chatid]['lastactivity'] = $now;
669
8e7eec60 670 foreach($this->sets_info as $sessionid => $info) {
671 // We need to get handles from users that are in the same chatroom, same group
672 if($info['chatid'] == $message->chatid &&
673 ($info['groupid'] == $message->groupid || $message->groupid == 0))
674 {
675
676 // Simply give them the message
82a524ef 677 $output = chat_format_message_manually($message, 0, $sender, $info['user'], $info['lang']);
d9e8ba0a 678 $this->trace('Delivering message "'.$output->text.'" to '.$this->conn_sets[$sessionid][CHAT_CONNECTION_CHANNEL]);
8e7eec60 679
b5de723d 680 if($output->beep) {
6fb2f823 681 $this->write_data($this->conn_sets[$sessionid][CHAT_CONNECTION_CHANNEL], '<embed src="'.$this->_beepsoundsrc.'" autostart="true" hidden="true" />');
b5de723d 682 }
683
5a60e822 684 if($info['quirks'] & QUIRK_CHUNK_UPDATE) {
685 $output->html .= $GLOBALS['CHAT_DUMMY_DATA'];
686 $output->html .= $GLOBALS['CHAT_DUMMY_DATA'];
687 $output->html .= $GLOBALS['CHAT_DUMMY_DATA'];
688 }
f0232c84 689
6fb2f823 690 if(!$this->write_data($this->conn_sets[$sessionid][CHAT_CONNECTION_CHANNEL], $output->html)) {
691 $this->disconnect_session($sessionid);
8e7eec60 692 }
d9e8ba0a 693 //$this->trace('Sent to UID '.$this->sets_info[$sessionid]['userid'].': '.$message->text_);
8e7eec60 694 }
695 }
696 }
697
6fb2f823 698 function disconnect_session($sessionid) {
699 $info = $this->sets_info[$sessionid];
700
701 delete_records('chat_users', 'sid', $sessionid);
702 $msg = &New stdClass;
703 $msg->chatid = $info['chatid'];
704 $msg->userid = $info['userid'];
705 $msg->groupid = 0;
706 $msg->system = 1;
707 $msg->message = 'exit';
708 $msg->timestamp = time();
709
710 $this->trace('User has disconnected, destroying uid '.$info['userid'].' with SID '.$sessionid, E_USER_WARNING);
711 insert_record('chat_messages', $msg);
712
713 // *************************** IMPORTANT
714 //
715 // Kill him BEFORE broadcasting, otherwise we 'll get infinite recursion!
716 //
717 // **********************************************************************
718 $latesender = $info['user'];
719 $this->dismiss_set($sessionid);
720 $this->message_broadcast($msg, $latesender);
8e7eec60 721 }
722
d9e8ba0a 723 function fatal($message) {
724 $message .= "\n";
725 if($this->_logfile) {
6fb2f823 726 $this->trace($message, E_USER_ERROR);
d9e8ba0a 727 }
728 echo "FATAL ERROR:: $message\n";
729 die();
730 }
731
6fb2f823 732 function init_sockets() {
733 global $CFG;
734
735 $this->trace('Setting up sockets');
736
737 if(false === ($this->listen_socket = socket_create(AF_INET, SOCK_STREAM, 0))) {
738 // Failed to create socket
739 $lasterr = socket_last_error();
740 $this->fatal('Error: socket_create() failed: '. socket_strerror($lasterr).' ['.$lasterr.']');
741 }
742
743 //socket_close($DAEMON->listen_socket);
744 //die();
745
746 if(!socket_bind($this->listen_socket, $CFG->chat_serverip, $CFG->chat_serverport)) {
747 // Failed to bind socket
748 $lasterr = socket_last_error();
749 $this->fatal('Error: socket_bind() failed: '. socket_strerror($lasterr).' ['.$lasterr.']');
750 }
751
752 if(!socket_listen($this->listen_socket, $CFG->chat_servermax)) {
753 // Failed to get socket to listen
754 $lasterr = socket_last_error();
755 $this->fatal('Error: socket_listen() failed: '. socket_strerror($lasterr).' ['.$lasterr.']');
756 }
757
758 // Socket has been initialized and is ready
759 $this->trace('Socket opened on port '.$CFG->chat_serverport);
760
761 // [pj]: I really must have a good read on sockets. What exactly does this do?
762 // http://www.unixguide.net/network/socketfaq/4.5.shtml is still not enlightening enough for me.
763 socket_set_option($this->listen_socket, SOL_SOCKET, SO_REUSEADDR, 1);
764 socket_set_nonblock($this->listen_socket);
765 }
766
d9e8ba0a 767 function cli_switch($switch, $param = NULL) {
768 switch($switch) { //LOL
6fb2f823 769 case 'reset':
770 // Reset sockets
771 $this->_resetsocket = true;
772 return false;
d9e8ba0a 773 case 'start':
774 // Start the daemon
775 $this->_readytogo = true;
776 return false;
777 break;
778 case 'v':
779 // Verbose mode
780 $this->_trace_level = E_ALL;
781 return false;
782 break;
783 case 'l':
784 // Use logfile
785 if(!empty($param)) {
786 $this->_logfile_name = $param;
787 }
788 $this->_logfile = @fopen($this->_logfile_name, 'a+');
789 if($this->_logfile == false) {
790 $this->fatal('Failed to open '.$this->_logfile_name.' for writing');
791 }
792 return false;
793 default:
794 // Unrecognized
795 $this->fatal('Unrecognized command line switch: '.$switch);
796 break;
797 }
798 return false;
799 }
800
8e7eec60 801}
802
803// Connection telltale
804define('CHAT_CONNECTION', 0x10);
805// Connections: Incrementing sequence, 0x10 to 0x1f
806define('CHAT_CONNECTION_CHANNEL', 0x11);
807
808// Sidekick telltale
809define('CHAT_SIDEKICK', 0x20);
810// Sidekicks: Incrementing sequence, 0x21 to 0x2f
811define('CHAT_SIDEKICK_USERS', 0x21);
812define('CHAT_SIDEKICK_MESSAGE', 0x22);
e7d27884 813define('CHAT_SIDEKICK_BEEP', 0x23);
8e7eec60 814
815
816$DAEMON = New ChatDaemon;
8e7eec60 817
818/// Check the parameters //////////////////////////////////////////////////////
819
d9e8ba0a 820unset($argv[0]);
821$commandline = implode(' ', $argv);
822if(strpos($commandline, '-') === false) {
823 if(!empty($commandline)) {
824 // We cannot have received any meaningful parameters
825 $DAEMON->fatal('Garbage in command line');
8e7eec60 826 }
d9e8ba0a 827}
828else {
829 // Parse command line
830 $switches = preg_split('/(-{1,2}[a-zA-Z]+) */', $commandline, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
8e7eec60 831
d9e8ba0a 832 // Taking advantage of the fact that $switches is indexed with incrementing numeric keys
833 // We will be using that to pass additional information to those switches who need it
834 $numswitches = count($switches);
8e7eec60 835
d9e8ba0a 836 // Fancy way to give a "hyphen" boolean flag to each "switch"
837 $switches = array_map(create_function('$x', 'return array("str" => $x, "hyphen" => (substr($x, 0, 1) == "-"));'), $switches);
838
839 for($i = 0; $i < $numswitches; ++$i) {
840
841 $switch = $switches[$i]['str'];
842 $params = ($i == $numswitches - 1 ? NULL :
843 ($switches[$i + 1]['hyphen'] ? NULL : trim($switches[$i + 1]['str']))
844 );
845
846 if(substr($switch, 0, 2) == '--') {
847 // Double-hyphen switch
848 $DAEMON->cli_switch(strtolower(substr($switch, 2)), $params);
849 }
850 else if(substr($switch, 0, 1) == '-') {
851 // Single-hyphen switch(es), may be more than one run together
852 $switch = substr($switch, 1); // Get rid of the -
853 $len = strlen($switch);
854 for($j = 0; $j < $len; ++$j) {
855 $DAEMON->cli_switch(strtolower(substr($switch, $j, 1)), $params);
856 }
857 }
858 }
859}
860
861if(!$DAEMON->query_start()) {
862 // For some reason we didn't start, so print out some info
863 echo 'Starts the Moodle chat socket server on port '.$CFG->chat_serverport;
864 echo "\n\n";
865 echo "Usage: chatd.php [parameters]\n\n";
866 echo "Parameters:\n";
867 echo " --start Starts the daemon\n";
868 echo " -v Verbose mode (prints trivial information messages)\n";
869 echo " -l [logfile] Log all messages to logfile (if not specified, chatd.log)\n";
870 echo "Example:\n";
871 echo " chatd.php --start -l\n\n";
872 die();
873}
8e7eec60 874
8e7eec60 875if (!function_exists('socket_set_option')) {
876 // PHP < 4.3
877 if (!function_exists('socket_setopt')) {
878 // No socket_setopt!
879 echo "Error: Neither socket_setopt() nor socket_set_option() exists.\n";
880 echo "Possibly PHP has not been compiled with --enable-sockets.\n\n";
881 die();
882 }
883 function socket_set_option($socket, $level, $name, $val) {
884 return socket_setopt($socket, $level, $name, $val);
885 }
886}
887
6fb2f823 888$DAEMON->init_sockets();
8e7eec60 889
8e7eec60 890/*
891declare(ticks=1);
892
893$pid = pcntl_fork();
894if ($pid == -1) {
895 die("could not fork");
896} else if ($pid) {
897 exit(); // we are the parent
898} else {
899 // we are the child
900}
901
902// detatch from the controlling terminal
903if (!posix_setsid()) {
904 die("could not detach from terminal");
905}
906
907// setup signal handlers
908pcntl_signal(SIGTERM, "sig_handler");
909pcntl_signal(SIGHUP, "sig_handler");
8e7eec60 910
d9e8ba0a 911if($DAEMON->_pcntl_exists && false) {
912 $DAEMON->trace('Unholy spirit possession: daemonizing');
8e7eec60 913 $DAEMON->pid = pcntl_fork();
914 if($pid == -1) {
d9e8ba0a 915 $DAEMON->trace('Process fork failed, terminating');
8e7eec60 916 die();
917 }
918 else if($pid) {
919 // We are the parent
d9e8ba0a 920 $DAEMON->trace('Successfully forked the daemon with PID '.$pid);
8e7eec60 921 die();
922 }
923 else {
924 // We are the daemon! :P
925 }
926
927 // FROM NOW ON, IT'S THE DAEMON THAT'S RUNNING!
928
929 // Detach from controlling terminal
930 if(!posix_setsid()) {
d9e8ba0a 931 $DAEMON->trace('Could not detach daemon process from terminal!');
8e7eec60 932 }
933}
934else {
935 // Cannot go demonic
d9e8ba0a 936 $DAEMON->trace('Unholy spirit possession failed: PHP is not compiled with --enable-pcntl');
8e7eec60 937}
6fb2f823 938*/
8e7eec60 939
d9e8ba0a 940$DAEMON->trace('Started Moodle chatd on port '.$CFG->chat_serverport.', listening socket '.$DAEMON->listen_socket, E_USER_WARNING);
8e7eec60 941
942while(true) {
943 $active = array();
944
945 // First of all, let's see if any of our UFOs has identified itself
946 if($DAEMON->conn_activity_ufo($active)) {
947 foreach($active as $handle) {
948 $read_socket = array($handle);
949 $changed = socket_select($read_socket, $write = NULL, $except = NULL, 0, 0);
950
951 if($changed > 0) {
952 // Let's see what it has to say
953
954 $data = socket_read($handle, 512);
955 if(empty($data)) {
956 continue;
957 }
958
e7d27884 959 if(!ereg('win=(chat|users|message|beep).*&chat_sid=([a-zA-Z0-9]*)&groupid=([0-9]*) HTTP', $data, $info)) {
8e7eec60 960 // Malformed data
d9e8ba0a 961 $DAEMON->trace('UFO with '.$handle.': Request with malformed data; connection closed', E_USER_WARNING);
8e7eec60 962 $DAEMON->dismiss_ufo($handle);
963 continue;
964 }
965
966 $type = $info[1];
967 $sessionid = $info[2];
968 $groupid = $info[3];
969
970 $customdata = array();
971
972 switch($type) {
973 case 'chat':
974 $type = CHAT_CONNECTION_CHANNEL;
695a4bff 975 $customdata['quirks'] = 0;
976 if(strpos($data, 'Safari')) {
d9e8ba0a 977 $DAEMON->trace('Safari identified...', E_USER_WARNING);
695a4bff 978 $customdata['quirks'] += QUIRK_CHUNK_UPDATE;
5a60e822 979 }
8e7eec60 980 break;
981 case 'users':
982 $type = CHAT_SIDEKICK_USERS;
983 break;
e7d27884 984 case 'beep':
985 $type = CHAT_SIDEKICK_BEEP;
986 if(!ereg('beep=([^&]*)[& ]', $data, $info)) {
d9e8ba0a 987 $DAEMON->trace('Beep sidekick did not contain a valid userid', E_USER_WARNING);
e7d27884 988 $DAEMON->dismiss_ufo($handle);
989 continue;
990 }
991 else {
992 $customdata = array('beep' => intval($info[1]));
993 }
994 break;
8e7eec60 995 case 'message':
996 $type = CHAT_SIDEKICK_MESSAGE;
dfd629d7 997 if(!ereg('chat_message=([^&]*)[& ]chat_msgidnr=([^&]*)[& ]', $data, $info)) {
d9e8ba0a 998 $DAEMON->trace('Message sidekick did not contain a valid message', E_USER_WARNING);
8e7eec60 999 $DAEMON->dismiss_ufo($handle);
1000 continue;
1001 }
1002 else {
dfd629d7 1003 $customdata = array('message' => $info[1], 'index' => $info[2]);
8e7eec60 1004 }
1005 break;
1006 default:
d9e8ba0a 1007 $DAEMON->trace('UFO with '.$handle.': Request with unknown type; connection closed', E_USER_WARNING);
8e7eec60 1008 $DAEMON->dismiss_ufo($handle);
1009 continue;
1010 break;
1011 }
1012
1013 // OK, now we know it's something good... promote it and pass it all the data it needs
1014 $DAEMON->promote_ufo($handle, $type, $sessionid, $groupid, $customdata);
1015 continue;
1016 }
1017 }
1018 }
1019
6fb2f823 1020 $now = time();
1021
1022 // Clean up chatrooms with no activity as required
1023 if($now - $DAEMON->_last_idle_poll > $DAEMON->_freq_poll_idle_chat) {
1024 $DAEMON->poll_idle_chats($now);
1025 }
1026
8e7eec60 1027 // Finally, accept new connections
1028 $DAEMON->conn_accept();
1029
d9e8ba0a 1030 usleep($DAEMON->_time_rest_socket);
8e7eec60 1031}
1032
1033@socket_shutdown($DAEMON->listen_socket, 0);
1034die("\n\n-- terminated --\n");
1035
8e7eec60 1036?>