1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
<?
/*
* licqweb. Copyright 2005, Philip Nelson
*
* This file is part of licqweb.
*
* licqweb is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* licqweb is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with licqweb; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
function readSocket() {
global $sock;
return socket_read($sock, 512, PHP_NORMAL_READ);
}
function sendData($cmd) {
global $sock;
socket_write($sock, $cmd, strlen($cmd));
}
function rmsLogin($uin, $passwd) {
global $errmsg, $sock, $server, $port, $loggedIn, $h, $a;
if ($loggedIn == true) {
return;
}
$loggedIn = true;
// Connect to the RMS server
socket_connect($sock, $server, $port);
// Get the version prompt
$packet = socket_read($sock, 512, PHP_NORMAL_READ);
// Get the UIN prompt
$packet = socket_read($sock, 512, PHP_NORMAL_READ);
if (!preg_match("/^300\s{1}.*/", $packet)) {
socket_close($sock);
$sock = 0;
$loggedIn = false;
return false;
}
//sendData($uin);
socket_write($sock, $uin, strlen($uin));
$packet = socket_read($sock, 512, PHP_NORMAL_READ);
if (!preg_match("/^301\s{1}.*/", $packet)) {
socket_close($sock);
$sock = 0;
$loggedIn = false;
return false;
}
sendData($passwd);
$packet = socket_read($sock, 512, PHP_NORMAL_READ);
if (!preg_match("/^200\s{1}.*/", $packet)) {
$errmsg = $packet;
socket_close($sock);
$sock = 0;
$loggedIn = false;
return false;
}
list($nick, $tmp) = sscanf($packet, "200 Hello %s %s");
return $nick;
}
function rmsGetlist($type = 'online') {
global $sock;
$packet = " ";
$cmd = "LIST $type\r\n";
if ($sock == 0) return;
socket_write($sock, $cmd, strlen($cmd));
$packet = socket_read($sock, 512, PHP_NORMAL_READ);
$users = array();
while (!preg_match("/^206$/", $packet)) {
if (preg_match("/^204\s{1}.*/", $packet)) {
$user = getUserStuff($packet);
array_push($users, $user);
$packet = socket_read($sock, 512, PHP_NORMAL_READ);
}
}
return $users;
}
function rmsViewEvent($id, $pp) {
global $sock;
$cmd = "VIEW " . $id . "." . $pp . "\r\n";
sendData($cmd);
$packet = socket_read($sock, 1024, PHP_NORMAL_READ);
if (substr($packet, 0, 3) == 502) {
return false;
}
$packet = socket_read($sock, 1024, PHP_NORMAL_READ);
$snttime = substr($packet, 12);
$packet = socket_read($sock, 1024, PHP_NORMAL_READ);
$packet = socket_read($sock, 1024, PHP_BINARY_READ);
$msg = str_replace("\r\n", "<br/>", $packet);
$msg = str_replace("\n", '<br/>', $msg);
preg_match("/^(.*)<br\/>223 Message Complete<br\/>$/", $msg, $matches);
$msg = $matches[1];
// kses input filtering
$allowed = array('b' => array(),
'i' => array(),
'a' => array('href' => 1, 'title' => 1),
'p' => array('align' => 1),
'br' => array(),
'font' => array('size' => 1, 'color' => 1, 'face' => 1)
);
if (get_magic_quotes_gpc()) {
$msg = stripslashes($msg);
}
return array('msg' => kses($msg, $allowed), 'time' => trim($snttime));
}
function rmsSendMessage($id, $pp, $msg) {
global $sock, $h, $a;
$cmd = "MESSAGE " . $id . "." . $pp . "\r\n";
sendData($cmd);
$packet = socket_read($sock, 1024, PHP_NORMAL_READ);
if (!preg_match("/^302\s{1}.*/", $packet)) {
return false;
}
$msgcontent = $msg . "\r\n.\r\n";
sendData($msgcontent);
$packet = socket_read($sock, 1024, PHP_NORMAL_READ); //e.g. 102 [1254] Sending message to foo@hotmail.com.
preg_match("/\[(\d+)\]/", $packet, $matches);
$packet = socket_read($sock, 1024, PHP_NORMAL_READ); //e.g. 203 [1254] Event done
preg_match("/\[(\d+)\] Event (.*)/", $packet, $matches2);
while ($matches[1] != $matches2[1]) {
//Event tag doesn't match the Event Done tag, keep trying
$packet = socket_read($sock, 1024, PHP_NORMAL_READ);
preg_match("/\[(\d+)\]/", $packet, $matches2);
}
return $matches2[2];
}
function rmsGetStatus() {
global $sock;
$cmd = "STATUS\r\n";
sendData($cmd);
$packet = socket_read($sock, 512, PHP_NORMAL_READ);
$ownerStatus = array();
while (!preg_match("/^212/", $packet)) {
list($tmp, $id, $pp, $status) = explode(' ', $packet, 4);
$ownerStatus[$pp] = array('id' => $id, 'status' => $status);
$packet = socket_read($sock, 512, PHP_NORMAL_READ);
}
return $ownerStatus;
}
function rmsChangeStatus($pp, $status) {
global $sock;
$cmd = "STATUS $status.$pp\r\n";
sendData($cmd);
$packet = socket_read($sock, 512, PHP_NORMAL_READ);
while (!preg_match("/^212/", $packet)) {
$packet = socket_read($sock, 512, PHP_NORMAL_READ);
}
return true;
}
function getUserStuff($packet) {
$id = " ";
$status = " ";
$nick = " ";
$newmsgs = " ";
$pp = " ";
// We have a user now, time to manipulate the string!
$i = 3;
while ($packet{$i} == ' ') {
$i++;
}
// Grab the id
while ($packet{$i} != ' ') {
$id .= $packet{$i};
$i++;
}
$id = trim($id);
while ($packet{$i} == ' ') {
$i++;
}
// Grab the Protocol
while ($packet{$i} != ' ') {
$pp .= $packet{$i};
$i++;
}
$stop = $i;
$pp = trim($pp);
// Grab the status
$i = strlen($packet) - 1;
while ($packet{$i} == ' ') {
$i--;
}
while (!($packet{$i} >= '0' && $packet{$i} <= '9')) {
$status .= $packet{$i};
$i--;
}
$status = strrev($status);
$status = trim($status);
// Grab the number of new messages
while ($packet{$i} == ' ') {
$i--;
}
while ($packet{$i} != ' ') {
$newmsgs .= $packet{$i};
$i--;
}
$newmsgs = strrev($newmsgs);
$newmsgs = trim($newmsgs);
// Grab the nick
while ($packet{$i} == ' ') {
$i--;
}
while ($i > $stop) {
$nick .= $packet{$i};
$i--;
}
$nick = strrev($nick);
$nick = kses(trim($nick));
// Now we have a user, lets build a list struct then display the list
$user = array('id' => $id, 'nick' => $nick, 'newmsgs' => $newmsgs, 'status' => $status, 'pp' => $pp);
return $user;
}
function xmlentities ($string) {
return str_replace (array('&', '"', "'", '<', '>'), array ('&' , '"', ''' , '<' , '>'), $string);
}
?>
|