00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00033 class ilChatServerCommunicator
00034 {
00035 var $chat;
00036
00037 var $message;
00038 var $socket_p;
00039 var $type;
00040 var $rcp_id;
00041 var $rcp_login;
00042 var $kicked_user;
00049 function ilChatServerCommunicator(&$chat_obj)
00050 {
00051 define(TIMEOUT,2);
00052
00053 $this->chat =& $chat_obj;
00054 }
00055
00056
00057 function setRecipientId($a_id)
00058 {
00059 $this->rcp_id = $a_id;
00060
00061 if($this->rcp_id)
00062 {
00063 $tmp_user =& new ilObjUser($this->rcp_id);
00064 $this->setRecipientLogin($tmp_user->getLogin());
00065 unset($tmp_user);
00066 }
00067 }
00068 function getRecipientId()
00069 {
00070 return $this->rcp_id;
00071 }
00072 function setRecipientLogin($a_login)
00073 {
00074 $this->rcp_login = $a_login;
00075 }
00076 function getRecipientLogin()
00077 {
00078 return $this->rcp_login;
00079 }
00080 function setKickedUser($k_user)
00081 {
00082 $this->kicked_user = $k_user;
00083 }
00084 function getKickedUser()
00085 {
00086 return $this->kicked_user;
00087 }
00088 function setMessage($a_message)
00089 {
00090 $this->message = $a_message;
00091 }
00092 function getMessage()
00093 {
00094 return $this->message;
00095 }
00096 function setType($a_type)
00097 {
00098 $this->type = $a_type;
00099 }
00100 function getType()
00101 {
00102 return $this->type ? $this->type : 'normal';
00103 }
00104 function getHtml()
00105 {
00106 return $this->html;
00107 }
00108
00109 function send()
00110 {
00111 if(!$this->__openSocket())
00112 {
00113 return false;
00114 }
00115 fputs($this->socket_p,$this->__formatMessage());
00116 fclose($this->socket_p);
00117
00118 return true;
00119 }
00120
00121 function getServerFrameSource()
00122 {
00123 return sprintf("http://%s:%s/%s%s",
00124 $this->chat->server_conf->getExternalIp(),
00125 $this->chat->server_conf->getPort(),
00126 $this->__formatLogin($this->chat->chat_user->getLogin()),
00127 $this->__getFormattedChatroom());
00128
00129 }
00130
00131 function isAlive()
00132 {
00133 $this->setType("test");
00134 return $this->send();
00135 }
00136
00137
00138 function __getFormattedChatroom()
00139 {
00140 $nic = $this->chat->server_conf->getNic();
00141
00142 return $nic.$this->chat->chat_room->getInternalName().
00143 substr("______________",0,14-strlen($this->chat->chat_room->getInternalName()));
00144 }
00145 function __formatLogin($a_login)
00146 {
00147 $nic = $this->chat->server_conf->getNic();
00148
00149 return substr($nic.md5($a_login),0,32);
00150 }
00151 function __formatMessage()
00152 {
00153 $this->__emoticons();
00154
00155 switch($this->getType())
00156 {
00157 case 'private':
00158
00159 return "|".$this->__formatLogin($this->getRecipientLogin()).
00160 $this->__formatLogin($this->chat->chat_user->getLogin()).
00161 $this->__getFormattedChatroom().$this->getMessage()."<br />";
00162
00163 case 'address':
00164
00165 $this->chat->chat_room->appendMessageToDb($this->getMessage());
00166 return ">".$this->__getFormattedChatroom().$this->getMessage()."<br />";
00167
00168 case 'normal':
00169
00170 $this->chat->chat_room->appendMessageToDb($this->getMessage());
00171 return ">".$this->__getFormattedChatroom().$this->getMessage()."<br />";
00172
00173 case 'login':
00174 return "!".$this->__formatLogin($this->chat->chat_user->getLogin()).$_SERVER["REMOTE_ADDR"];
00175
00176 case "logout":
00177 return "-".$this->__formatLogin($this->chat->chat_user->getLogin());
00178
00179 case "kick":
00180 return "GET /moderate?".$this->chat->server_conf->getModeratorPassword().
00181 "&kick&".$this->__formatLogin($this->getKickedUser()).
00182 "&".$this->__getFormattedChatroom();
00183
00184 case "delete":
00185 return "GET /moderate?".$this->chat->server_conf->getModeratorPassword().
00186 "&delete&".$this->__getFormattedChatroom();
00187
00188 case "empty":
00189 return "GET /moderate?".$this->chat->server_conf->getModeratorPassword().
00190 "&clear&".$this->__getFormattedChatroom();
00191
00192 case "test":
00193 return "GET /Version";
00194
00195 default:
00196 return "GET /Version";
00197
00198 }
00199 }
00200
00201 function __emoticons()
00202 {
00203 $str = $this->getMessage();
00204 $str = str_replace(":)", "<img src=\"" . ILIAS_HTTP_PATH .
00205 "/templates/default/images/emoticons/icon_smile.gif\" border=0>", $str);
00206 $str = str_replace(":-)", "<img src=\"" . ILIAS_HTTP_PATH .
00207 "/templates/default/images/emoticons/icon_smile.gif\" border=0>", $str);
00208 $str = str_replace(":smile:", "<img src=\"" . ILIAS_HTTP_PATH .
00209 "/templates/default/images/emoticons/icon_smile.gif\" border=0>", $str);
00210 $str = str_replace(";)", "<img src=\"" . ILIAS_HTTP_PATH .
00211 "/templates/default/images/emoticons/icon_wink.gif\" border=0>", $str);
00212 $str = str_replace(";-)", "<img src=\"" . ILIAS_HTTP_PATH .
00213 "/templates/default/images/emoticons/icon_wink.gif\" border=0>", $str);
00214 $str = str_replace(":wink:", "<img src=\"" . ILIAS_HTTP_PATH .
00215 "/templates/default/images/emoticons/icon_wink.gif\" border=0>", $str);
00216 $str = str_replace(":D", "<img src=\"" . ILIAS_HTTP_PATH .
00217 "/templates/default/images/emoticons/icon_laugh.gif\" border=0>", $str);
00218 $str = str_replace(":-D", "<img src=\"" . ILIAS_HTTP_PATH .
00219 "/templates/default/images/emoticons/icon_laugh.gif\" border=0>", $str);
00220 $str = str_replace(":laugh:", "<img src=\"" . ILIAS_HTTP_PATH .
00221 "/templates/default/images/emoticons/icon_laugh.gif\" border=0>", $str);
00222 $str = str_replace(":grin:", "<img src=\"" . ILIAS_HTTP_PATH .
00223 "/templates/default/images/emoticons/icon_laugh.gif\" border=0>", $str);
00224 $str = str_replace(":biggrin:", "<img src=\"" . ILIAS_HTTP_PATH .
00225 "/templates/default/images/emoticons/icon_laugh.gif\" border=0>", $str);
00226 $str = str_replace(":(", "<img src=\"" . ILIAS_HTTP_PATH .
00227 "/templates/default/images/emoticons/icon_sad.gif\" border=0>", $str);
00228 $str = str_replace(":-(", "<img src=\"" . ILIAS_HTTP_PATH .
00229 "/templates/default/images/emoticons/icon_sad.gif\" border=0>", $str);
00230 $str = str_replace(":sad:", "<img src=\"" . ILIAS_HTTP_PATH .
00231 "/templates/default/images/emoticons/icon_sad.gif\" border=0>", $str);
00232 $str = str_replace(":o", "<img src=\"" . ILIAS_HTTP_PATH .
00233 "/templates/default/images/emoticons/icon_shocked.gif\" border=0>", $str);
00234 $str = str_replace(":-o", "<img src=\"" . ILIAS_HTTP_PATH .
00235 "/templates/default/images/emoticons/icon_shocked.gif\" border=0>", $str);
00236 $str = str_replace(":shocked:", "<img src=\"" . ILIAS_HTTP_PATH .
00237 "/templates/default/images/emoticons/icon_smile.gif\" border=0>", $str);
00238 $str = str_replace(":p", "<img src=\"" . ILIAS_HTTP_PATH .
00239 "/templates/default/images/emoticons/icon_tongue.gif\" border=0>", $str);
00240 $str = str_replace(":-p", "<img src=\"" . ILIAS_HTTP_PATH .
00241 "/templates/default/images/emoticons/icon_tongue.gif\" border=0>", $str);
00242 $str = str_replace(":tongue:", "<img src=\"" . ILIAS_HTTP_PATH .
00243 "/templates/default/images/emoticons/icon_smile.gif\" border=0>", $str);
00244 $str = str_replace(":cool:", "<img src=\"" . ILIAS_HTTP_PATH .
00245 "/templates/default/images/emoticons/icon_cool.gif\" border=0>", $str);
00246 $str = str_replace(":eek:", "<img src=\"" . ILIAS_HTTP_PATH .
00247 "/templates/default/images/emoticons/icon_eek.gif\" border=0>", $str);
00248 $str = str_replace(":||", "<img src=\"" . ILIAS_HTTP_PATH .
00249 "/templates/default/images/emoticons/icon_angry.gif\" border=0>", $str);
00250 $str = str_replace(":-||", "<img src=\"" . ILIAS_HTTP_PATH .
00251 "/templates/default/images/emoticons/icon_angry.gif\" border=0>", $str);
00252 $str = str_replace(":angry:", "<img src=\"" . ILIAS_HTTP_PATH .
00253 "/templates/default/images/emoticons/icon_angry.gif\" border=0>", $str);
00254 $str = str_replace(":flush:", "<img src=\"" . ILIAS_HTTP_PATH .
00255 "/templates/default/images/emoticons/icon_flush.gif\" border=0>", $str);
00256 $str = str_replace(":idea:", "<img src=\"" . ILIAS_HTTP_PATH .
00257 "/templates/default/images/emoticons/icon_idea.gif\" border=0>", $str);
00258 $str = str_replace(":thumbup:", "<img src=\"" . ILIAS_HTTP_PATH .
00259 "/templates/default/images/emoticons/icon_thumbup.gif\" border=0>", $str);
00260 $str = str_replace(":thumbdown:", "<img src=\"" . ILIAS_HTTP_PATH .
00261 "/templates/default/images/emoticons/icon_thumbdown.gif\" border=0>", $str);
00262 $this->setMessage($str);
00263 }
00264
00265 function __openSocket()
00266 {
00267 $this->socket_p = @fsockopen($this->chat->server_conf->getInternalIp(),
00268 $this->chat->server_conf->getPort(), $errno, $errstr, TIMEOUT);
00269
00270 return $this->socket_p == null ? false : true;
00271 }
00272
00273
00274 function _initObject()
00275 {
00276 global $ilias;
00277
00278 $query = "SELECT ref_id FROM object_data NATURAL JOIN object_reference ".
00279 "WHERE type = 'chac'";
00280
00281 $res = $ilias->db->query($query);
00282 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00283 {
00284 $ref_id = $row->ref_id;
00285 }
00286
00287
00288 return new ilChatServerCommunicator($tmp =& ilObjectFactory::getInstanceByRefId($ref_id));
00289 }
00290
00291 function _login()
00292 {
00293 $obj =& ilChatServerCommunicator::_initObject();
00294
00295
00296 $obj->setType("login");
00297 $obj->chat->chat_user->setUserId($_SESSION["AccountId"]);
00298 $obj->send();
00299 }
00300
00301 function _logout()
00302 {
00303 $obj =& ilChatServerCommunicator::_initObject();
00304
00305
00306 $obj->setType("logout");
00307 $obj->chat->chat_user->setUserId($_SESSION["AccountId"]);
00308 $obj->send();
00309 }
00310
00311
00312 }
00313 ?>