Просмотр файла functions.php

Размер файла: 18.03Kb
<?php
//WebIcqLite code

class WebIcqLite_TLV {
    var $type;
    var $size;
    var $error;

    var $types = array
    (
        'UIN'                 =>  1, // 0x01
        'DATA'                =>  2, // 0x02
        'CLIENT'            =>  3, // 0x03
        'ERROR_URL'            =>  4, // 0x04
        'RECONECT_HERE'        =>  5, // 0x05
        'COOKIE'            =>  6, // 0x06
        'SNAC_VERSION'        =>  7, // 0x07
        'ERROR_SUBCODE'        =>  8, // 0x08
        'DISCONECT_REASON'    =>  9, // 0x09
        'RECONECT_HOST'        => 10, // 0x0A
        'URL'                => 11, // 0x0B
        'DEBUG_DATA'        => 12, // 0x0C
        'SERVICE'            => 13, // 0x0D
        'CLIENT_COUNTRY'    => 14, // 0x0E
        'CLIENT_LNG'        => 15, // 0x0F
        'SCRIPT'            => 16, // 0x10
        'USER_EMAIL'        => 17, // 0x11
        'OLD_PASSWORD'        => 18, // 0x12
        'REG_STATUS'        => 19, // 0x13
        'DISTRIB_NUMBER'    => 20, // 0x14
        'PERSONAL_TEXT'        => 21, // 0x15
        'CLIENT_ID'            => 22, // 0x16
        'CLI_MAJOR_VER'     => 23, // 0x17
        'CLI_MINOR_VER'     => 24, // 0x18
        'CLI_LESSER_VER'     => 25, // 0x19
        'CLI_BUILD_NUMBER'    => 26, // 0x1A
//        'PASSWORD'            => 37
    );

    function setTLV($type, $value, $length = false)
    {
        switch ($length)
        {
            case 1:
                $format = 'c';
                break;
            case 2:
                $format = 'n';
                break;
            case 4:
                $format = 'N';
                break;
            default:
                $format = 'a*';
                break;
        }
        if ($length === false)
        {
            $length = strlen($value);
        }
        return pack('nn'.$format, $this->types[$type], $length, $value);
    }

    function getTLV($data)
    {
        $arr = unpack('n2', substr($data, 0, 4));
        $this->type = $arr[1];
        $this->size = $arr[2];
        return substr($data, 4, $this->size);
    }

    function getTLVFragment($data)
    {
        $frg = unpack('cid/cversion/nsize', substr($data, 0, 4));
        $frg['data'] = substr($data, 4, $frg['size']);
        return $frg;
    }
}

class WebIcqLite_SNAC extends WebIcqLite_TLV {

    var $request_id = 0;
    var $uin;

    function setSNAC0102()
    {
        $this->request_id++;
        $out = pack('nnnN', 1, 2, 0, $this->request_id);
        $out .= pack('n*', 1, 3, 272, 650);
        $out .= pack('n*', 2, 1, 272, 650);
        $out .= pack('n*', 3, 1, 272, 650);
        $out .= pack('n*', 21, 1, 272, 650);
        $out .= pack('n*', 4, 1, 272, 650);
        $out .= pack('n*', 6, 1, 272, 650);
        $out .= pack('n*', 9, 1, 272, 650);
        $out .= pack('n*', 10, 1, 272, 650);

        return $out;
    }

    function setSNAC0406($uin, $message)
    {
        $this->request_id++;
        $cookie = microtime();
        $out = pack('nnnNdnca*', 4, 6, 0, $this->request_id, $cookie, 2, strlen($uin), $uin);

        $capabilities = pack('H*', '094613494C7F11D18222444553540000'); // utf-8 support
        // '97B12751243C4334AD22D6ABF73F1492' rtf support

        $data = pack('nd', 0, $cookie).$capabilities;
        $data .= pack('nnn', 10, 2, 1);
        $data .= pack('nn', 15, 0);
        $data .= pack('nnvvddnVn', 10001, strlen($message)+62, 27, 8, 0, 0, 0, 3, $this->request_id);
        $data .= pack('nndnn', 14, $this->request_id, 0, 0, 0); //45
        $data .= pack('ncvnva*', 1, 0, 0, 1, (strlen($message)+1), $message);
        $data .= pack('H*', '0000000000FFFFFF00');
        $out .= $this->setTLV('RECONECT_HERE', $data);
        $out .= $this->setTLV('CLIENT', '');
        return $out;
    }

    function setSNAC0406offline($uin, $message)
    {
        $this->request_id++;
        $cookie = microtime();
        $out = pack('nnnNdnca*', 4, 6, 0, $this->request_id, $cookie, 1, strlen($uin), $uin);

        $data = pack('ccnc', 5, 1, 1, 1);
        $data .= pack('ccnnna*', 1, 1, strlen($message)+4, 3, 0, $message);
        $out .= $this->setTLV('DATA', $data);
        $out .= $this->setTLV('CLIENT', '');
        $out .= $this->setTLV('COOKIE', '');
        return $out;
    }

    function getSNAC0407($body)
    {
        if (strlen($body))
        {
            $msg = unpack('nfamily/nsubtype/nflags/Nrequestid/N2msgid/nchannel/cnamesize', $body);
            if ($msg['family'] == 4 && $msg['subtype'] == 7)
            {
                $body = substr($body, 21);
                $from = substr($body, 0, $msg['namesize']);
                $channel = $msg['channel'];
                $body = substr($body, $msg['namesize']);
                $msg = unpack('nwarnlevel/nTLVnumber', $body);
                $body = substr($body, 4);
                for ($i = 0; $i <= $msg['TLVnumber']; $i++)
                {
                    $part = $this->getTLV($body);
                    $body = substr($body, 4 + $this->size);
                    if ($channel == 1 && $this->type == 2)
                    {
                        while (strlen($part))
                        {
                            $frg = $this->getTLVFragment($part);
                            if ($frg['id'] == 1 && $frg['version'] == 1)
                            {
                                return array('from' => $from, 'message' => substr($frg['data'], 4));
                            }
                            $part = substr($part, 4+$frg['size']);
                        }
                        return false;
                    }
                }
            }
        }
        return false;
    }
    function dump($str)
    {
        $f = fopen('dump', 'a');
        fwrite($f, $str);
        fclose($f);
    }

}

class WebIcqLite_FLAP extends WebIcqLite_SNAC{

    var $socet;
    var $command = 0x2A;
    var $channel;
    var $sequence;
    var $body;
    var $info = array();

    function WebIcqLite_FLAP() {
        $this->sequence = rand(1, 30000);
    }

    function getFLAP()
    {
        if($this->socet && !socket_last_error($this->socet))
        {
            $header = @socket_read($this->socet, 6);
            if ($header)
            {
                $header = unpack('c2channel/n2size', $header);
                $this->channel = $header['channel2'];
                $this->body = socket_read($this->socet, $header['size2']);
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    function parseCookieFLAP()
    {
        $this->getFLAP();
        $this->info = array();
        while($this->body != '')
        {
            $info = $this->getTLV($this->body);
            $key = array_search($this->type, $this->types);
            if($key)
            {
                $this->info[$key] = $info;
            }
            $this->body = substr($this->body, ($this->size+4));
        }
    }

    function parseAnswerFLAP()
    {
        $this->getFLAP();
        $array = unpack('n3int/Nint', $this->body);
        while ($array['int'] != $this->request_id)
        {
            $this->getFLAP();
            $array = unpack('n3int/Nint', $this->body);
        }

        $this->error = 'Соединение прервано';
        if ($array['int1'] == 4)
        {
            switch ($array['int2'])
            {
                case 1:
                        $this->error = 'Ошибка в отправке сообщения';
                        return false;
                    break;
                case 0x0c:
                        return true;
                    break;
            }
        }

        $this->error = 'Соединение прервано';
        return false;
    }

    function prepare()
    {
        $this->sequence++;
        $out = pack('ccnn', $this->command, $this->channel, $this->sequence, strlen($this->body)).$this->body;
        return $out;
    }

    function login($uin, $password)
    {
        $this->getFLAP();
        $this->uin = $uin;
        $this->body .= $this->setTLV('UIN',                 "$uin");
        $this->body .= $this->setTLV('DATA',                 $this->xorpass($password));
        $this->body .= $this->setTLV('CLIENT',                 'ICQBasic');
        $this->body .= $this->setTLV('CLIENT_ID',             266, 2);
        $this->body .= $this->setTLV('CLI_MAJOR_VER',         20, 2);
        $this->body .= $this->setTLV('CLI_MINOR_VER',         34, 2);
        $this->body .= $this->setTLV('CLI_LESSER_VER',         0, 2);
        $this->body .= $this->setTLV('CLI_BUILD_NUMBER',     2321, 2);
        $this->body .= $this->setTLV('DISTRIB_NUMBER',         1085, 4);
        $this->body .= $this->setTLV('CLIENT_LNG',             'en');
        $this->body .= $this->setTLV('CLIENT_COUNTRY',         'us');


        $this->channel = 1;
        $pack = $this->prepare();
        socket_write($this->socet, $pack, strlen($pack));
        $this->parseCookieFLAP();

        $this->body = 0x0000;
        $pack = $this->prepare();
        @socket_write($this->socet, $pack, strlen($pack));
        $this->close();

        if(isset($this->info['RECONECT_HERE']))
        {
            $url = explode(':', $this->info['RECONECT_HERE']);
            if(!$this->open($url))
            {
                $this->error = isset($this->info['DISCONECT_REASON']) ? $this->info['DISCONECT_REASON'] : 'Unable to reconnect';
                return false;
            }
        }
        else
        {
            $this->error = isset($this->info['DISCONECT_REASON']) ? $this->info['DISCONECT_REASON'] : 'UIN blocked, please try again 20 min later.';
            return false;
        }

        $this->getFLAP();
        $this->body .= $this->setTLV('COOKIE', $this->info['COOKIE']);
        $pack = $this->prepare();
        if (!socket_write($this->socet, $pack, strlen($pack)))
        {
            $this->error = 'Соединение прервано';
            return false;
        }
        $this->getFLAP();
        $this->body = $this->setSNAC0102();
        $pack = $this->prepare();
        if (!socket_write($this->socet, $pack, strlen($pack)))
        {
            $this->error = 'Соединение прервано';
            return false;
        }
        return true;
    }

    function write_message($uin, $message)
    {
        $this->body = $this->setSNAC0406($uin, $message);
        $pack = $this->prepare();
        if (!socket_write($this->socet, $pack, strlen($pack)))
        {
            $this->error = 'Соединение прервано';
            return false;
        }
        if (! $this->parseAnswerFLAP()) {
            // try to send offline message

            $this->body = $this->setSNAC0406offline($uin, $message);
            $pack = $this->prepare();
            if (!socket_write($this->socet, $pack, strlen($pack)))
            {
                $this->error = 'Can`t send offline message, server close connection';
                return false;
            }
            if (! $this->parseAnswerFLAP())
            {
                return false;
            }
            else
            {
                $this->error = 'Client is offline. Message sent to server.';
                return false;
            }
        }

        return true;
    }

    function read_message()
    {
        while($this->getFLAP())
        {
            $message = $this->getSNAC0407($this->body);
            if($message){
                return $message;
            }
        }
        return false;
    }

    function xorpass($pass)
    {
        $roast = array(0xF3, 0x26, 0x81, 0xC4, 0x39, 0x86, 0xDB, 0x92, 0x71, 0xA3, 0xB9, 0xE6, 0x53, 0x7A, 0x95, 0x7c);
        $roasting_pass = '';
        for ($i=0; $i<strlen($pass); $i++)
        {
            $roasting_pass .= chr($roast[$i] ^ ord($pass{$i}));
        }
        return($roasting_pass);
    }

    function open($url = array('login.icq.com', 5190))
    {
        $this->socet = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        if ($this->socet < 0 || $this->socet === false)
        {
            $this->error = "socket_create() failed: reason: " . socket_strerror($this->socet);
            return false;
        }
        $result = socket_connect($this->socet, gethostbyname($url[0]), $url[1]);
        if ($result < 0 || $result === false)
        {
            $this->error = "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket));
            return false;
        }
        return true;
    }

    function close()
    {
        return socket_close($this->socet);
    }
}

class WebIcqLite extends WebIcqLite_FLAP {

    function WebIcqLite ()
    {
        $this->WebIcqLite_FLAP();
    }

    function is_connected()
    {
        if(!$this->socet || socket_last_error($this->socet))
        {
            $this->error = socket_strerror(socket_last_error($socket));
            return false;
        }
        return true;
    }

    function connect($uin, $pass)
    {
        if (!$this->open())
        {
            return false;
        }

        return $this->login($uin, $pass);
    }

    function disconnect()
    {
        return $this->close();
    }

    function get_message()
    {
        return $this->read_message();
    }

    function send_message($uin, $message)
    {
        return $this->write_message($uin, $message);
    }
}
/*======================================================================*\
	Функция:	utf_to_win
	Назначение:	перекодирует любой текст кодировку Win-1251
	Ввод:		$a    любой текст
	Вывод:		echo utf_to_win($a);	выводит перекодированный текст
\*======================================================================*/
$_utf8win1251 = array( 
"\xD0\x90"=>"\xC0","\xD0\x91"=>"\xC1","\xD0\x92"=>"\xC2","\xD0\x93"=>"\xC3","\xD0\x94"=>"\xC4", 
"\xD0\x95"=>"\xC5","\xD0\x81"=>"\xA8","\xD0\x96"=>"\xC6","\xD0\x97"=>"\xC7","\xD0\x98"=>"\xC8", 
"\xD0\x99"=>"\xC9","\xD0\x9A"=>"\xCA","\xD0\x9B"=>"\xCB","\xD0\x9C"=>"\xCC","\xD0\x9D"=>"\xCD", 
"\xD0\x9E"=>"\xCE","\xD0\x9F"=>"\xCF","\xD0\xA0"=>"\xD0","\xD0\xA1"=>"\xD1","\xD0\xA2"=>"\xD2", 
"\xD0\xA3"=>"\xD3","\xD0\xA4"=>"\xD4","\xD0\xA5"=>"\xD5","\xD0\xA6"=>"\xD6","\xD0\xA7"=>"\xD7", 
"\xD0\xA8"=>"\xD8","\xD0\xA9"=>"\xD9","\xD0\xAA"=>"\xDA","\xD0\xAB"=>"\xDB","\xD0\xAC"=>"\xDC", 
"\xD0\xAD"=>"\xDD","\xD0\xAE"=>"\xDE","\xD0\xAF"=>"\xDF","\xD0\x87"=>"\xAF","\xD0\x86"=>"\xB2", 
"\xD0\x84"=>"\xAA","\xD0\x8E"=>"\xA1","\xD0\xB0"=>"\xE0","\xD0\xB1"=>"\xE1","\xD0\xB2"=>"\xE2", 
"\xD0\xB3"=>"\xE3","\xD0\xB4"=>"\xE4","\xD0\xB5"=>"\xE5","\xD1\x91"=>"\xB8","\xD0\xB6"=>"\xE6", 
"\xD0\xB7"=>"\xE7","\xD0\xB8"=>"\xE8","\xD0\xB9"=>"\xE9","\xD0\xBA"=>"\xEA","\xD0\xBB"=>"\xEB", 
"\xD0\xBC"=>"\xEC","\xD0\xBD"=>"\xED","\xD0\xBE"=>"\xEE","\xD0\xBF"=>"\xEF","\xD1\x80"=>"\xF0", 
"\xD1\x81"=>"\xF1","\xD1\x82"=>"\xF2","\xD1\x83"=>"\xF3","\xD1\x84"=>"\xF4","\xD1\x85"=>"\xF5", 
"\xD1\x86"=>"\xF6","\xD1\x87"=>"\xF7","\xD1\x88"=>"\xF8","\xD1\x89"=>"\xF9","\xD1\x8A"=>"\xFA", 
"\xD1\x8B"=>"\xFB","\xD1\x8C"=>"\xFC","\xD1\x8D"=>"\xFD","\xD1\x8E"=>"\xFE","\xD1\x8F"=>"\xFF", 
"\xD1\x96"=>"\xB3","\xD1\x97"=>"\xBF","\xD1\x94"=>"\xBA","\xD1\x9E"=>"\xA2"); 

function utf_to_win($a)
{ 
  global $_utf8win1251; 
    if (is_array($a))
	{ 
        foreach ($a as $k => $v)
		{ 
            if (is_array($v))
              $a[$k] = utf_to_win($v); 
            else
              $a[$k] = strtr($v, $_utf8win1251); 
         
	    } 
      return $a; 
    }
	else 
      return strtr($a, $_utf8win1251);
}
/*======================================================================*\
	Функция:	win_to_utf
	Назначение:	перекодирует любой текст в кодировку utf-8
	Ввод:		$a    любой текст
	Вывод:		echo win_to_utf($a);	выводит перекодированный текст
\*======================================================================*/
$_win1251utf8 = array( 
"\xC0"=>"\xD0\x90","\xC1"=>"\xD0\x91","\xC2"=>"\xD0\x92","\xC3"=>"\xD0\x93","\xC4"=>"\xD0\x94", 
"\xC5"=>"\xD0\x95","\xA8"=>"\xD0\x81","\xC6"=>"\xD0\x96","\xC7"=>"\xD0\x97","\xC8"=>"\xD0\x98", 
"\xC9"=>"\xD0\x99","\xCA"=>"\xD0\x9A","\xCB"=>"\xD0\x9B","\xCC"=>"\xD0\x9C","\xCD"=>"\xD0\x9D", 
"\xCE"=>"\xD0\x9E","\xCF"=>"\xD0\x9F","\xD0"=>"\xD0\xA0","\xD1"=>"\xD0\xA1","\xD2"=>"\xD0\xA2", 
"\xD3"=>"\xD0\xA3","\xD4"=>"\xD0\xA4","\xD5"=>"\xD0\xA5","\xD6"=>"\xD0\xA6","\xD7"=>"\xD0\xA7", 
"\xD8"=>"\xD0\xA8","\xD9"=>"\xD0\xA9","\xDA"=>"\xD0\xAA","\xDB"=>"\xD0\xAB","\xDC"=>"\xD0\xAC", 
"\xDD"=>"\xD0\xAD","\xDE"=>"\xD0\xAE","\xDF"=>"\xD0\xAF","\xAF"=>"\xD0\x87","\xB2"=>"\xD0\x86", 
"\xAA"=>"\xD0\x84","\xA1"=>"\xD0\x8E","\xE0"=>"\xD0\xB0","\xE1"=>"\xD0\xB1","\xE2"=>"\xD0\xB2", 
"\xE3"=>"\xD0\xB3","\xE4"=>"\xD0\xB4","\xE5"=>"\xD0\xB5","\xB8"=>"\xD1\x91","\xE6"=>"\xD0\xB6", 
"\xE7"=>"\xD0\xB7","\xE8"=>"\xD0\xB8","\xE9"=>"\xD0\xB9","\xEA"=>"\xD0\xBA","\xEB"=>"\xD0\xBB", 
"\xEC"=>"\xD0\xBC","\xED"=>"\xD0\xBD","\xEE"=>"\xD0\xBE","\xEF"=>"\xD0\xBF","\xF0"=>"\xD1\x80", 
"\xF1"=>"\xD1\x81","\xF2"=>"\xD1\x82","\xF3"=>"\xD1\x83","\xF4"=>"\xD1\x84","\xF5"=>"\xD1\x85", 
"\xF6"=>"\xD1\x86","\xF7"=>"\xD1\x87","\xF8"=>"\xD1\x88","\xF9"=>"\xD1\x89","\xFA"=>"\xD1\x8A", 
"\xFB"=>"\xD1\x8B","\xFC"=>"\xD1\x8C","\xFD"=>"\xD1\x8D","\xFE"=>"\xD1\x8E","\xFF"=>"\xD1\x8F", 
"\xB3"=>"\xD1\x96","\xBF"=>"\xD1\x97","\xBA"=>"\xD1\x94","\xA2"=>"\xD1\x9E"); 

function win_to_utf($a)
{ 
  global $_win1251utf8; 
    if (is_array($a))
	{ 
        foreach ($a as $k => $v)
		{ 
            if (is_array($v))
              $a[$k] = win_to_utf($v); 
            else
              $a[$k] = strtr($v, $_win1251utf8); 
         
	    } 
      return $a; 
    }
	else 
      return strtr($a, $_win1251utf8);
}
?>