<?php
/*
 * 
 * Autor: Anton.skorodumov
 * e-mail askorodumov87@gmail.com
 */
class php_telnet {

    var $use_usleep = 0;
    var $sleeptime = 25000;
    var $fp = NULL;
    var $err_num = 0; /*
     * 1 - Host not found
     * 2 - Connetion abort
     * 9 - Connection close
     */
    var $show_rez = true;
    var $cmd_open_connect = array('ff', 'fd', '03', 'ff', 'fb', '18', 'ff', 'fb', '1f',
        'ff', 'fb', '20', 'ff', 'fb', '21', 'ff', 'fb', '22',
        'ff', 'fb', '27', 'ff', 'fd', '05', 'ff', 'fb', '23');
    var $cmd_term_type = array('ff', 'fa', '18', '00', '78', '74', '65', '72',
        '6d', '2d', '32', '35', '36', '63', '6f', '6c', '6f', '72', 'ff', 'f0');
    var $cmd_establ_connect = array('ff', 'fd', '03', 'ff', 'fb', '01');
/*
 *
 */
    function __construct($host, $user, $pass) {
        if (strlen($host)) {
            if (preg_match('/[^0-9.]/', $host)) {
                $ip = gethostbyname($host);
                if ($ip == $host) {
                    $ip = '';
                    $this->err_num = 2;
                }
            } else {
                $ip = $host;
            }
        } else {
            $ip = '127.0.0.1';
            $this->err_num = 1;
        }

        $this->connect($ip);
        if (!$this->err_num) {

            $this->do_comand($user . "\r");

            echo $this->do_comand($pass . "\r", TRUE);
        }
    }

    function do_comand($cmd, $return = false) {
        if (!$this->fp) {
            $this->err_num = 2;
            return;
        }

        fputs($this->fp, $cmd);
        $this->Sleep();
        if ($this->show_rez && $return) {

            return $this->get_response();
        }
    }

    function get_response($do_decode = false) {
        $r = '';
        do {
            $r .= fread($this->fp, 8024);
            $s = socket_get_status($this->fp);
        } while ($s['unread_bytes']);

        if ($do_decode) {

            return $this->decode_comand($r);
        } else {
            return $r;
        }
    }

    private function decode_comand($response) {

        $n = -1;
        $r = array();
        for ($i = 0; $i < strlen($response); $i++) {
            $tmp = dechex(ord($response[$i]));
            if ($tmp == 'ff') {
                $n++;
                $r[$n] = '';
            } else {
                $r[$n] .= $tmp;
            }
        }
        return $r;
    }

    function code_comand($code) {
        $cmd = '';
        foreach ($code as $value) {
            $cmd .= chr(hexdec($value));
        }

        return $cmd;
    }

    private function connect($host) {
        $this->fp = fsockopen($host, 23);
        if ($this->fp) {
            $this->do_comand($this->code_comand($this->cmd_open_connect));

            $res = $this->decode_comand(fread($this->fp, 1024));
            if (in_array('fa181', $res)) {
                $this->do_comand($this->code_comand($this->cmd_term_type));
            }
            $this->do_comand($this->code_comand($this->cmd_establ_connect));
        } else {
            $this->err_num = 9;
        }
    }

    function disconect($cmd_exit='') {
        if ($cmd_exit) $this->do_comand ($cmd_exit."\r");
        if ($this->fp) {
            fclose($this->fp);
            $this->fp = NULL;
        }
    }

    function Sleep() {
        if ($this->use_usleep) {
            usleep($this->sleeptime);
        } else {
            sleep(1);
        }
    }

}
