Erstmal Sorry dass dieses Script so lange ist. Es gibt folgendes Problem:
Beim Einstieg in den Chat wird der Name und ein Schluesselwert für Gender angezeigt (z.B. name&1) und zwar mit folgendem befehl
Wenn man jedoch eine Zeile in den Chat eingibt verschwindet der Wert "gender" des zuletzt hinzugefügen name aus dem display (nicht name&4 sondern nur name) irritierenderweise gibt es dieses Problem im Firefox nicht.
Hier nun die Classe:
Beim Einstieg in den Chat wird der Name und ein Schluesselwert für Gender angezeigt (z.B. name&1) und zwar mit folgendem befehl
PHP-Code:
if(!empty($_GET['name']) AND !empty($_GET['gender']))
{
$room= $_GET['room'];
$_SESSION['SES_USER_NAME']=$_GET['name'];
$_SESSION['GENDERTYPE']=$_GET['gender'];
$mychat=new Chat($room);
if($mychat->addUser($_GET['name'], $_GET['gender']))
{
$_SESSION['SES_USER_NAME']='';
$_SESSION['MSG_LINES']=0;
echo $mychat->room->getError();
}
}
PHP-Code:
if(!empty($_POST['msg']))
$mychat->putMessage(trim($_POST['msg']),$_SESSION['SES_USER_NAME']);
Hier nun die Classe:
PHP-Code:
<?php
class Chat
{
var $room;
var $filename;
function Chat($roomName="",$maxuser=20)
{
$this->filename=$roomName;
if(empty($roomName))
{
$this->filename=time();
$roomName=$this->filename;
}
$this->addRoom($roomName,$maxuser);
}
function putMessage($msg,$userName="")
{
$this->room->putMessage($msg,$userName);
$this->_savechat();
}
function getMessage($fromline=0,$asArray=false)
{
return $this->room->getMessage($fromline,$asArray);
}
function getMessageForUser($userName,$asArray=false)
{
$message = $this->room->getMessageForUser($userName,$asArray);
$this->_savechat();
return $message;
}
function getRoomName()
{
return $this->filename;
}
function addRoom($roomName,$maxuser=20)
{
if(!$this->_is_room_exists($roomName))
{
$this->room=new ChatRoom($roomName,$maxuser);
$this->_savechat();
}
else
$this->room=$this->_getroom();
}
function addUser($userName, $genderType)
{
if($ret=$this->room->addUser($userName, $genderType)===false)
return false;
$this->_savechat();
}
function delUser($userName)
{
$this->room->delUser($userName);
if($this->room->isRoomEmpty())
$this->_removechat();
else
$this->_savechat();
}
function getUsers()
{
return $this->room->getUsers();
}
function close()
{
unset($this);
}
/* functions for internal uses */
function _savechat()
{
$fp=@fopen($this->filename,'w');
@fwrite($fp,serialize($this->room));
@fclose($fp);
}
function _getroom()
{
return unserialize(file_get_contents($this->filename));
}
function _is_room_exists($room)
{
return is_file($room);
}
function _removechat()
{
@unlink($this->filename);
}
}
?>
<?php
class ChatRoom
{
var $name;
var $maxuser;
var $users;
var $gender;
var $error;
var $message;
var $maxline;
var $remlines;
var $onefromip;
var $mymessages;
function ChatRoom($name="",$maxuser=20)
{
$this->name=$name;
$this->maxuser=$maxuser;
$this->maxline=200;
$this->remlines=1;
$this->onefromip=false;
}
function OnlyOneUserFromIP($onefromip=false)
{
$this->onefromip=$onefromip;
}
function expandRoom($len)
{
$this->maxuser+=$len;
}
function shrinkRoom($len)
{
$this->maxuser-=$len;
}
function setWelcomeMessage($msg)
{
$this->mymessages['welcome']=$msg;
}
function setByeMessage($msg)
{
$this->mymessages['bye']=$msg;
}
function setMaxUser($maxuser)
{
$this->maxuser=$maxuser;
}
function setMaxMessageLine($lines=200)
{
$this->maxline=$lines;
}
function setRemLine($lines=1)
{
$this->remlines=$lines;
}
function addUser($userName, $gender)
{
if(empty($userName))
{
$this->error="Can't add user without having name!";
return false;
}
elseif(count($this->users) == $this->maxuser && $this->maxuser>0)
{
$this->error="Dieser Chatraum is voll besetzt - leider.";
return false;
}
elseif(@array_key_exists($userName,$this->users))
{
$this->error="<span style=\"font-family: courier new, verdana; font-size: 18px; color: #ffffff;\"><b>".$userName."</b> befindet sich bereits in diesem Raum.</span>";
return false;
}
elseif($this->onefromip)
{
$user=$this->getUserByIP($_SERVER['REMOTE_ADDR']);
if($user!=false)
{
$this->error=$user." is already in room from this IP address.";
return false;
}
}
$user=new User($userName, $gender);
$this->users[$userName]=$user;
$this->genders[$gender]=$gendertype;
if(!empty($this->mymessages['welcome']))
$this->putMessage($this->mymessages['welcome']);
}
function delUser($userName)
{
$user=$this->users[$userName];
$idx=@array_search($userName,@array_keys($this->users));
@array_splice($this->users,$idx,1);
if(!empty($this->mymessages['bye']))
$this->putMessage($this->mymessages['bye']);
}
function isRoomEmpty()
{
return empty($this->users);
}
function putMessage($msg,$userName="")
{
if(count($this->message) > $this->maxline && $this->maxline>0)
{
for($i=1;$i<=$this->remlines;$i++)
{
@array_shift($this->message);
}
foreach($this->users as $user)
{
$user->lastline-=$this->remlines;
if($user->lastline<0)
$user->lastline=0;
}
}
if(!empty($userName) && !empty($msg))
{
$this->message[]="<span style=color:#".$hexa1.$hexa1x.$hexa2.$hexa2x.$hexa3.$hexa3x."><b>".$userName.": </b>".$msg."</span>";
} elseif(!empty($msg)) {
$this->message[]="<span style=color:#".$hexa1.$hexa1x.$hexa2.$hexa2x.$hexa3.$hexa3x.">".$msg."</span>";
/*$user=&$this->users[$userName];
$user->lastline++;*/
}
}
function getMessageForUser($userName,$asArray=false)
{
$user=&$this->users[$userName];
$messages=$this->getMessage($user->lastline,$asArray);
$user->lastline=count($this->message);
return $messages;
}
function getMessage($fromline=0,$asArray=false)
{
$return="";
for($i=$fromline;$i<count($this->message);$i++)
{
if($asArray)
$return[]=$this->message[$i];
else
$return.=$this->message[$i]."<br>";
}
return $return;
}
function getError()
{
return $this->error;
}
function resetRoom()
{
$this->makeEmpty();
$this->name="";
$this->maxuser=2;
$this->maxline=200;
$this->remlines=1;
}
function setRoomName($name)
{
$this->name=name;
}
function getRoomName()
{
return $this->name;
}
function getUsers()
{
$countusers = 0;
$countgenders = 0;
$user4display = Array();
foreach(array_keys($this->users) as $user)
{
$countusers++;
$user4display[] = "<span style=color:#".$hexa1.$hexa1x.$hexa2.$hexa2x.$hexa3.$hexa3x.">".$user."</span></a>";
}
$gender4display = Array();
foreach(array_keys($this->genders) as $gender)
{
$gender4display[] = $gender;
}
$array4returnusersandgenders = Array();
for($i=0; $i<$countusers; $i++)
{
$array4returnusersandgenders[] = $user4display[$i]."&".$gender4display[$i];
}
return @implode("<br>",$array4returnusersandgenders);
}
function getUser($userName, $gender)
{
return $this->users[$userName]."&".$this->genders[$gender];
}
function getUserByIP($ip,$onlyName=false)
{
if(!is_array($this->users))
{
$this->error="No user is exists into room.";
return false;
}
foreach($this->users as $user)
{
if($user->ip==$ip)
{
if($onlyName)
return $user->name;
else
return $user;
}
}
$this->error="No user found with this IP (".$ip.")";
return false;
}
function makeEmpty()
{
unset($this->users);
unset($this->message);
}
}
?>
<?php
/* defining the diffrent status */
define("OFFLINE",0);
define("ONLINE",1);
class User
{
var $name;
var $ip;
var $status;
var $lastline;
function User($user='')
{
$this->setUser($user);
$this->ip = $_SERVER['REMOTE_ADDR'];
$this->status = ONLINE;
$this->lastline=0;
}
function setUser($name)
{
$this->name=$name;
}
function setStatus($status)
{
$this->status=$status;
}
function getUser()
{
return $this->name;
}
function getStatus()
{
return $this->status;
}
function getIP()
{
return $this->ip;
}
}
?>
Kommentar