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