0 ) { // here is where you check for special commands $newmsgline = ""; if( startsWith($msg,"/" )) { if( startsWith( $msg, "/me ") ) { $newmsgline = ".\t«".htmlspecialchars($_GET['user'])." ".htmlspecialchars( substr( $msg , 4) )."»\n"; } elseif ( startsWith( $msg, "/time") ){ $newmsgline = ".\t«Current server time is ".date('Y-m-d H:i:s')." [".date_default_timezone_get()."]»\n"; } elseif ( startsWith( $msg, "/flip") ){ $coin = array("heads","tails"); $newmsgline = ".\t«".htmlspecialchars($_GET['user'])." flips a coin. It is ".$coin[rand(0,1)]."»\n"; } elseif ( startsWith( $msg, "/roll") ){ $dicesides = intval(substr( $msg , 6 )); if( $dicesides < 2 ) { $dicesides = 100; } $newmsgline = ".\t«".htmlspecialchars($_GET['user'])." rolls a ".rand(1,$dicesides)." out of ".$dicesides."»\n"; } else { $result = "Commands:
"; $result .= "/me action - emote an action. (/me smiles)
"; $result .= "/time - display server time.
"; $result .= "/flip - user flips a coin.
"; $result .= "/roll # - user rolls a # sided dice (defaults 100).
"; } } else { // store the user and message in tab separated text columns. prevent HTML injection $newmsgline = htmlspecialchars($_GET['user'])."\t".htmlspecialchars($msg)."\n"; } if( $newmsgline != "" ) { $fh = fopen($filename,'a+'); fwrite( $fh , $newmsgline ); fclose($fh ); } break; } else { $result = ""; break; } case 'update': // give us lines after previous count, and the new count $linecount = 0; $result = ""; $startline = $_GET['start']; $fh = @fopen( $filename, "r" ); if( $fh ) { while(!feof($fh)){ $line = fgets($fh); if( $linecount >= $startline ) $result .= $line; $linecount++; } fclose($fh); if( $startline > $linecount ) { // file was reset, start the client at the beginning $linecount = 0; } } $result .= (string)($linecount-1); // last line in response is the new current count break; case 'entered': // someone entered the chat room. $newmsgline = ".\t".htmlspecialchars($_GET['user'])." entered the chat.\n"; $fh = fopen($filename,'a+'); fwrite( $fh , $newmsgline ); fclose($fh ); break; case 'exited': // someone left the chat room. $newmsgline = ".\t".htmlspecialchars($_GET['user'])." left the chat.\n"; $fh = fopen($filename,'a+'); fwrite( $fh , $newmsgline ); fclose($fh ); break; default: break; } echo $result; ?>