1<?php 2 3if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../../').'/'); 4require_once(DOKU_INC.'inc/init.php'); 5require_once(DOKU_INC.'inc/common.php'); 6 7function startsWith($haystack, $needle) 8{ 9 return !strncmp($haystack, $needle, strlen($needle)); 10} 11 $result=""; 12 $cmd = $_GET['cmd']; 13 $room = str_replace(array(' ','.','/',':'),array('','','-','-'),$_GET['room']); // need to clean this. remove spaces, remove dots , change slashes to underlines 14 $filename = DOKU_INC.'data/chats/log_'.$room.'.txt'; 15 switch( $cmd ){ 16 case 'send':// got a message from user 17 $msg = str_replace( array("\r","\n"), '\r', trim($_GET['msg']) ); 18 if( strlen($msg) > 0 ) { 19 // here is where you check for special commands 20 $newmsgline = ""; 21 if( startsWith($msg,"/" )) { 22 if( startsWith( $msg, "/me ") ) { 23 $newmsgline = ".\t«".htmlspecialchars($_GET['user'])." ".htmlspecialchars( substr( $msg , 4) )."»\n"; 24 } elseif ( startsWith( $msg, "/time") ){ 25 $newmsgline = ".\t«Current server time is ".date('Y-m-d H:i:s')." [".date_default_timezone_get()."]»\n"; 26 } elseif ( startsWith( $msg, "/flip") ){ 27 $coin = array("heads","tails"); 28 $newmsgline = ".\t«".htmlspecialchars($_GET['user'])." flips a coin. It is ".$coin[rand(0,1)]."»\n"; 29 } elseif ( startsWith( $msg, "/roll") ){ 30 $dicesides = intval(substr( $msg , 6 )); 31 if( $dicesides < 2 ) { $dicesides = 100; } 32 $newmsgline = ".\t«".htmlspecialchars($_GET['user'])." rolls a ".rand(1,$dicesides)." out of ".$dicesides."»\n"; 33 } else { 34 $result = "Commands:<br>"; 35 $result .= "/me action - emote an action. (/me smiles)<br>"; 36 $result .= "/time - display server time.<br>"; 37 $result .= "/flip - user flips a coin.<br>"; 38 $result .= "/roll # - user rolls a # sided dice (defaults 100).<br>"; 39 } 40 } else { 41 // store the user and message in tab separated text columns. prevent HTML injection 42 $newmsgline = htmlspecialchars($_GET['user'])."\t".htmlspecialchars($msg)."\n"; 43 } 44 if( $newmsgline != "" ) { 45 $fh = fopen($filename,'a+'); 46 fwrite( $fh , $newmsgline ); 47 fclose($fh ); 48 } 49 break; 50 } else { 51 $result = ""; 52 break; 53 } 54 case 'update': // give us lines after previous count, and the new count 55 $linecount = 0; 56 $result = ""; 57 $startline = $_GET['start']; 58 $fh = @fopen( $filename, "r" ); 59 if( $fh ) { 60 while(!feof($fh)){ 61 $line = fgets($fh); 62 if( $linecount >= $startline ) $result .= $line; 63 $linecount++; 64 } 65 fclose($fh); 66 if( $startline > $linecount ) { 67 // file was reset, start the client at the beginning 68 $linecount = 0; 69 } 70 } 71 $result .= (string)($linecount-1); // last line in response is the new current count 72 break; 73 case 'entered': // someone entered the chat room. 74 $newmsgline = ".\t".htmlspecialchars($_GET['user'])." entered the chat.\n"; 75 $fh = fopen($filename,'a+'); 76 fwrite( $fh , $newmsgline ); 77 fclose($fh ); 78 break; 79 case 'exited': // someone left the chat room. 80 $newmsgline = ".\t".htmlspecialchars($_GET['user'])." left the chat.\n"; 81 $fh = fopen($filename,'a+'); 82 fwrite( $fh , $newmsgline ); 83 fclose($fh ); 84 break; 85 86 default: break; 87 } 88 89echo $result; 90?>