Find this useful? Enter your email to receive occasional updates for securing PHP code.

Signing you up...

Thank you for signing up!

PHP Decode

<?php class routeros_api { var $debug = false; var $error_no; var $error_..

Decoded Output download

<?php 
class routeros_api 
{ 
    var $debug = false; 
    var $error_no; 
    var $error_str; 
    var $attempts = 5; 
    var $connected = false; 
    var $delay = 3; 
    var $port = 8728; 
    var $timeout = 3; 
    var $socket; 
    function debug($text) 
    { 
        if ($this->debug) { 
            echo $text . "\xa"; 
        } 
    } 
    function encode_length($length) 
    { 
        if ($length < 128) { 
            $length = chr($length); 
        } else { 
            if ($length < 16384) { 
                $length |= 32768; 
                $length = chr($length >> 8 & 255) . chr($length & 255); 
            } else { 
                if ($length < 2097152) { 
                    $length |= 12582912; 
                    $length = chr($length >> 8 & 255) . chr($length >> 8 & 255) . chr($length & 255); 
                } else { 
                    if ($length < 268435456) { 
                        $length |= 3758096384; 
                        $length = chr($length >> 8 & 255) . chr($length >> 8 & 255) . chr($length >> 8 & 255) . chr($length & 255); 
                    } else { 
                        if ($length >= 268435456) { 
                            $length = chr(240) . chr($length >> 8 & 255) . chr($length >> 8 & 255) . chr($length >> 8 & 255) . chr($length & 255); 
                        } 
                    } 
                } 
            } 
        } 
        return $length; 
    } 
    function connect($ip, $login, $password) 
    { 
        for ($ATTEMPT = 1; $ATTEMPT <= $this->attempts; $ATTEMPT++) { 
            $this->connected = false; 
            $this->debug("Connection attempt #" . $ATTEMPT . " to " . $ip . ":" . $this->port . "..."); 
            if ($this->socket = @fsockopen($ip, $this->port, $this->error_no, $this->error_str, $this->timeout)) { 
                socket_set_timeout($this->socket, $this->timeout); 
                $this->write("/login"); 
                $RESPONSE = $this->read(false); 
                if ($RESPONSE[0] == "!done") { 
                    if (preg_match_all("/[^=]+/i", $RESPONSE[1], $MATCHES)) { 
                        if ($MATCHES[0][0] == "ret" && strlen($MATCHES[0][1]) == 32) { 
                            $this->write("/login", false); 
                            $this->write("=name=" . $login, false); 
                            $this->write("=password=" . $password); 
                            $RESPONSE = $this->read(false); 
                            if ($RESPONSE[0] == "!done") { 
                                $this->connected = true; 
                                break; 
                            } 
                        } 
                    } 
                } 
                fclose($this->socket); 
            } 
            sleep($this->delay); 
        } 
        if ($this->connected) { 
            $this->debug("Connected..."); 
        } else { 
            $this->debug("Error..."); 
        } 
        return $this->connected; 
    } 
    function disconnect() 
    { 
        fclose($this->socket); 
        $this->connected = false; 
        $this->debug("Disconnected..."); 
    } 
    function desconectar_gaby() 
    { 
        $com = "/quit"; 
        fwrite($this->socket, $this->encode_length(strlen($com)) . $com); 
        sleep(1); 
        $this->connected = false; 
        $this->debug("gabydesconectado..."); 
    } 
    function parse_response($response) 
    { 
        if (is_array($response)) { 
            $PARSED = array(); 
            $CURRENT = null; 
            foreach ($response as $x) { 
                if (in_array($x, array("!fatal", "!re", "!trap"))) { 
                    if ($x == "!re") { 
                        $CURRENT = &$PARSED[]; 
                    } else { 
                        $CURRENT = &$PARSED[$x][]; 
                    } 
                } else { 
                    if ($x != "!done") { 
                        if (preg_match_all("/[^=]+/i", $x, $MATCHES)) { 
                            $CURRENT[$MATCHES[0][0]] = isset($MATCHES[0][1]) ? $MATCHES[0][1] : ''; 
                        } 
                    } 
                } 
            } 
            return $PARSED; 
        } else { 
            return array(); 
        } 
    } 
    function array_change_key_name(&$array) 
    { 
        if (is_array($array)) { 
            foreach ($array as $k => $v) { 
                $tmp = str_replace("-", "_", $k); 
                $tmp = str_replace("/", "_", $tmp); 
                if ($tmp) { 
                    $array_new[$tmp] = $v; 
                } else { 
                    $array_new[$k] = $v; 
                } 
            } 
            return $array_new; 
        } else { 
            return $array; 
        } 
    } 
    function parse_response4smarty($response) 
    { 
        if (is_array($response)) { 
            $PARSED = array(); 
            $CURRENT = null; 
            foreach ($response as $x) { 
                if (in_array($x, array("!fatal", "!re", "!trap"))) { 
                    if ($x == "!re") { 
                        $CURRENT = &$PARSED[]; 
                    } else { 
                        $CURRENT = &$PARSED[$x][]; 
                    } 
                } else { 
                    if ($x != "!done") { 
                        if (preg_match_all("/[^=]+/i", $x, $MATCHES)) { 
                            $CURRENT[$MATCHES[0][0]] = isset($MATCHES[0][1]) ? $MATCHES[0][1] : ''; 
                        } 
                    } 
                } 
            } 
            foreach ($PARSED as $key => $value) { 
                $PARSED[$key] = $this->array_change_key_name($value); 
            } 
            return $PARSED; 
        } else { 
            return array(); 
        } 
    } 
    function read($parse = true) 
    { 
        $RESPONSE = array(); 
        $inicio = microtime(true); 
        while (true) { 
            $BYTE = ord(fread($this->socket, 1)); 
            $LENGTH = 0; 
            if ($BYTE & 128) { 
                if (($BYTE & 192) == 128) { 
                    $LENGTH = (($BYTE & 63) << 8) + ord(fread($this->socket, 1)); 
                } else { 
                    if (($BYTE & 224) == 192) { 
                        $LENGTH = (($BYTE & 31) << 8) + ord(fread($this->socket, 1)); 
                        $LENGTH = ($LENGTH << 8) + ord(fread($this->socket, 1)); 
                    } else { 
                        if (($BYTE & 240) == 224) { 
                            $LENGTH = (($BYTE & 15) << 8) + ord(fread($this->socket, 1)); 
                            $LENGTH = ($LENGTH << 8) + ord(fread($this->socket, 1)); 
                            $LENGTH = ($LENGTH << 8) + ord(fread($this->socket, 1)); 
                        } else { 
                            $LENGTH = ord(fread($this->socket, 1)); 
                            $LENGTH = ($LENGTH << 8) + ord(fread($this->socket, 1)); 
                            $LENGTH = ($LENGTH << 8) + ord(fread($this->socket, 1)); 
                            $LENGTH = ($LENGTH << 8) + ord(fread($this->socket, 1)); 
                        } 
                    } 
                } 
            } else { 
                $LENGTH = $BYTE; 
            } 
            if ($LENGTH > 0) { 
                $_ = ''; 
                $retlen = 0; 
                while ($retlen < $LENGTH) { 
                    $toread = $LENGTH - $retlen; 
                    $_ .= fread($this->socket, $toread); 
                    $retlen = strlen($_); 
                } 
                $RESPONSE[] = $_; 
                $this->debug(">>> [" . $retlen . "/" . $LENGTH . " bytes read."); 
            } 
            if ($_ == "!done" || $_ == "=status=finished" || $_ == "=status=done testing" || $_ == "=status=can not connect") { 
                $receiveddone = true; 
            } 
            $STATUS = socket_get_status($this->socket); 
            if ($LENGTH > 0) { 
                $this->debug(">>> [" . $LENGTH . ", " . $STATUS["unread_bytes"] . "] " . $_); 
            } 
            if (!$this->connected && !$STATUS["unread_bytes"] || $this->connected && !$STATUS["unread_bytes"] && isset($receiveddone) && $receiveddone) { 
                break; 
            } 
            $tiempo = round((microtime(true) - $inicio) * 1000, 0); 
        } 
        if ($parse) { 
            $RESPONSE = $this->parse_response($RESPONSE); 
        } 
        return $RESPONSE; 
    } 
    function write($command, $param2 = true) 
    { 
        if ($command) { 
            $data = explode("
", $command); 
            foreach ($data as $com) { 
                $com = trim($com); 
                fwrite($this->socket, $this->encode_length(strlen($com)) . $com); 
                $this->debug("<<< [" . strlen($com) . "] " . $com); 
            } 
            if (gettype($param2) == "integer") { 
                fwrite($this->socket, $this->encode_length(strlen(".tag=" . $param2)) . ".tag=" . $param2 . chr(0)); 
                $this->debug("<<< [" . strlen(".tag=" . $param2) . "] .tag=" . $param2); 
            } else { 
                if (gettype($param2) == "boolean") { 
                    fwrite($this->socket, $param2 ? chr(0) : ''); 
                } 
            } 
            return true; 
        } else { 
            return false; 
        } 
    } 
} 
 ?>

Did this file decode correctly?

Original Code

<?php
class routeros_api
{
    var $debug = false;
    var $error_no;
    var $error_str;
    var $attempts = 5;
    var $connected = false;
    var $delay = 3;
    var $port = 8728;
    var $timeout = 3;
    var $socket;
    function debug($text)
    {
        if ($this->debug) {
            echo $text . "\xa";
        }
    }
    function encode_length($length)
    {
        if ($length < 128) {
            $length = chr($length);
        } else {
            if ($length < 16384) {
                $length |= 32768;
                $length = chr($length >> 8 & 255) . chr($length & 255);
            } else {
                if ($length < 2097152) {
                    $length |= 12582912;
                    $length = chr($length >> 8 & 255) . chr($length >> 8 & 255) . chr($length & 255);
                } else {
                    if ($length < 268435456) {
                        $length |= 3758096384;
                        $length = chr($length >> 8 & 255) . chr($length >> 8 & 255) . chr($length >> 8 & 255) . chr($length & 255);
                    } else {
                        if ($length >= 268435456) {
                            $length = chr(240) . chr($length >> 8 & 255) . chr($length >> 8 & 255) . chr($length >> 8 & 255) . chr($length & 255);
                        }
                    }
                }
            }
        }
        return $length;
    }
    function connect($ip, $login, $password)
    {
        for ($ATTEMPT = 1; $ATTEMPT <= $this->attempts; $ATTEMPT++) {
            $this->connected = false;
            $this->debug("\x43\157\x6e\156\x65\x63\164\x69\157\x6e\40\141\164\x74\145\155\160\164\40\43" . $ATTEMPT . "\x20\164\157\x20" . $ip . "\x3a" . $this->port . "\x2e\56\x2e");
            if ($this->socket = @fsockopen($ip, $this->port, $this->error_no, $this->error_str, $this->timeout)) {
                socket_set_timeout($this->socket, $this->timeout);
                $this->write("\x2f\x6c\157\147\151\x6e");
                $RESPONSE = $this->read(false);
                if ($RESPONSE[0] == "\x21\144\x6f\156\145") {
                    if (preg_match_all("\57\x5b\x5e\75\x5d\53\57\x69", $RESPONSE[1], $MATCHES)) {
                        if ($MATCHES[0][0] == "\162\145\x74" && strlen($MATCHES[0][1]) == 32) {
                            $this->write("\57\x6c\157\147\x69\156", false);
                            $this->write("\75\156\x61\155\x65\75" . $login, false);
                            $this->write("\x3d\x70\141\163\x73\167\157\162\x64\x3d" . $password);
                            $RESPONSE = $this->read(false);
                            if ($RESPONSE[0] == "\41\144\x6f\x6e\x65") {
                                $this->connected = true;
                                break;
                            }
                        }
                    }
                }
                fclose($this->socket);
            }
            sleep($this->delay);
        }
        if ($this->connected) {
            $this->debug("\x43\157\x6e\156\145\x63\164\145\x64\x2e\x2e\x2e");
        } else {
            $this->debug("\105\x72\162\x6f\162\56\56\x2e");
        }
        return $this->connected;
    }
    function disconnect()
    {
        fclose($this->socket);
        $this->connected = false;
        $this->debug("\104\151\x73\143\157\x6e\156\x65\143\164\145\x64\x2e\56\56");
    }
    function desconectar_gaby()
    {
        $com = "\57\161\x75\x69\x74";
        fwrite($this->socket, $this->encode_length(strlen($com)) . $com);
        sleep(1);
        $this->connected = false;
        $this->debug("\147\x61\x62\x79\144\x65\163\143\x6f\156\x65\x63\164\x61\x64\157\56\x2e\x2e");
    }
    function parse_response($response)
    {
        if (is_array($response)) {
            $PARSED = array();
            $CURRENT = null;
            foreach ($response as $x) {
                if (in_array($x, array("\41\146\x61\164\141\x6c", "\x21\x72\145", "\41\x74\162\141\x70"))) {
                    if ($x == "\x21\162\145") {
                        $CURRENT = &$PARSED[];
                    } else {
                        $CURRENT = &$PARSED[$x][];
                    }
                } else {
                    if ($x != "\x21\x64\157\x6e\x65") {
                        if (preg_match_all("\57\133\x5e\x3d\135\x2b\x2f\x69", $x, $MATCHES)) {
                            $CURRENT[$MATCHES[0][0]] = isset($MATCHES[0][1]) ? $MATCHES[0][1] : '';
                        }
                    }
                }
            }
            return $PARSED;
        } else {
            return array();
        }
    }
    function array_change_key_name(&$array)
    {
        if (is_array($array)) {
            foreach ($array as $k => $v) {
                $tmp = str_replace("\55", "\x5f", $k);
                $tmp = str_replace("\x2f", "\137", $tmp);
                if ($tmp) {
                    $array_new[$tmp] = $v;
                } else {
                    $array_new[$k] = $v;
                }
            }
            return $array_new;
        } else {
            return $array;
        }
    }
    function parse_response4smarty($response)
    {
        if (is_array($response)) {
            $PARSED = array();
            $CURRENT = null;
            foreach ($response as $x) {
                if (in_array($x, array("\x21\x66\x61\164\141\x6c", "\41\x72\145", "\41\164\x72\141\x70"))) {
                    if ($x == "\41\x72\145") {
                        $CURRENT = &$PARSED[];
                    } else {
                        $CURRENT = &$PARSED[$x][];
                    }
                } else {
                    if ($x != "\41\x64\157\156\145") {
                        if (preg_match_all("\x2f\x5b\x5e\75\x5d\53\57\x69", $x, $MATCHES)) {
                            $CURRENT[$MATCHES[0][0]] = isset($MATCHES[0][1]) ? $MATCHES[0][1] : '';
                        }
                    }
                }
            }
            foreach ($PARSED as $key => $value) {
                $PARSED[$key] = $this->array_change_key_name($value);
            }
            return $PARSED;
        } else {
            return array();
        }
    }
    function read($parse = true)
    {
        $RESPONSE = array();
        $inicio = microtime(true);
        while (true) {
            $BYTE = ord(fread($this->socket, 1));
            $LENGTH = 0;
            if ($BYTE & 128) {
                if (($BYTE & 192) == 128) {
                    $LENGTH = (($BYTE & 63) << 8) + ord(fread($this->socket, 1));
                } else {
                    if (($BYTE & 224) == 192) {
                        $LENGTH = (($BYTE & 31) << 8) + ord(fread($this->socket, 1));
                        $LENGTH = ($LENGTH << 8) + ord(fread($this->socket, 1));
                    } else {
                        if (($BYTE & 240) == 224) {
                            $LENGTH = (($BYTE & 15) << 8) + ord(fread($this->socket, 1));
                            $LENGTH = ($LENGTH << 8) + ord(fread($this->socket, 1));
                            $LENGTH = ($LENGTH << 8) + ord(fread($this->socket, 1));
                        } else {
                            $LENGTH = ord(fread($this->socket, 1));
                            $LENGTH = ($LENGTH << 8) + ord(fread($this->socket, 1));
                            $LENGTH = ($LENGTH << 8) + ord(fread($this->socket, 1));
                            $LENGTH = ($LENGTH << 8) + ord(fread($this->socket, 1));
                        }
                    }
                }
            } else {
                $LENGTH = $BYTE;
            }
            if ($LENGTH > 0) {
                $_ = '';
                $retlen = 0;
                while ($retlen < $LENGTH) {
                    $toread = $LENGTH - $retlen;
                    $_ .= fread($this->socket, $toread);
                    $retlen = strlen($_);
                }
                $RESPONSE[] = $_;
                $this->debug("\76\x3e\x3e\x20\133" . $retlen . "\x2f" . $LENGTH . "\40\142\171\x74\x65\x73\40\x72\x65\141\144\56");
            }
            if ($_ == "\41\x64\x6f\156\x65" || $_ == "\x3d\x73\164\141\164\x75\x73\75\x66\151\x6e\x69\163\x68\145\x64" || $_ == "\x3d\163\164\x61\x74\165\x73\75\x64\157\156\145\40\x74\x65\x73\164\x69\156\x67" || $_ == "\75\163\164\x61\x74\165\163\x3d\x63\x61\x6e\40\x6e\157\164\x20\143\x6f\x6e\x6e\x65\143\164") {
                $receiveddone = true;
            }
            $STATUS = socket_get_status($this->socket);
            if ($LENGTH > 0) {
                $this->debug("\76\x3e\x3e\40\133" . $LENGTH . "\54\x20" . $STATUS["\x75\x6e\162\x65\x61\x64\x5f\142\x79\x74\145\x73"] . "\x5d\40" . $_);
            }
            if (!$this->connected && !$STATUS["\x75\156\162\145\x61\144\137\142\171\164\145\163"] || $this->connected && !$STATUS["\165\x6e\162\145\x61\144\137\142\171\x74\145\x73"] && isset($receiveddone) && $receiveddone) {
                break;
            }
            $tiempo = round((microtime(true) - $inicio) * 1000, 0);
        }
        if ($parse) {
            $RESPONSE = $this->parse_response($RESPONSE);
        }
        return $RESPONSE;
    }
    function write($command, $param2 = true)
    {
        if ($command) {
            $data = explode("\12", $command);
            foreach ($data as $com) {
                $com = trim($com);
                fwrite($this->socket, $this->encode_length(strlen($com)) . $com);
                $this->debug("\74\x3c\74\40\133" . strlen($com) . "\x5d\40" . $com);
            }
            if (gettype($param2) == "\x69\x6e\164\145\147\145\162") {
                fwrite($this->socket, $this->encode_length(strlen("\56\164\x61\147\75" . $param2)) . "\56\164\141\147\75" . $param2 . chr(0));
                $this->debug("\x3c\74\74\40\x5b" . strlen("\56\164\141\x67\75" . $param2) . "\135\x20\56\x74\x61\x67\75" . $param2);
            } else {
                if (gettype($param2) == "\x62\x6f\x6f\x6c\145\141\x6e") {
                    fwrite($this->socket, $param2 ? chr(0) : '');
                }
            }
            return true;
        } else {
            return false;
        }
    }
}

Function Calls

None

Variables

None

Stats

MD5 ceaefb3d30e14fbf9a7148e3c9400901
Eval Count 0
Decode Time 54 ms