<?php
class VGrab_redtube
{
    var $page;
    var $curl;
    function __construct()
    {
        $this->curl = new VCurl();
    }
    
    function VGrab_redtube()
    {
        $this->__construct();
    }

    function getPage( $url ) {
        $this->url = trim($url, '/');
        if ( $this->page = $this->curl->saveToString($url, 'pp=1;') ) {
            $this->page = trim($this->page);
            $this->page = str_replace("\n", '', $this->page);
            $this->page = str_replace("\r", '', $this->page);
            $this->page = preg_replace('/\s\s+/', ' ', $this->page);
            return true;
        }
        
        return false;
    }
    
    function getVideoTitle()
    {
        preg_match('/<title>&nbsp;RedTube - (.*?).<\/title>/', $this->page, $matches);
        if ( isset($matches['0']) ) {
            $title  = $matches['0'];
            $title  = str_replace('&nbsp;RedTube - ', '', $title);
            $title  = str_replace('<title>', '', $title);
            $title  = str_replace('</title>', '', $title);
            return $title;
        }
    }
    
    function getVideoTags()
    {
        return '';
    }
    
    function getVideoCategory()
    {
        return '';
    }
    
    function getVideoId()
    {
        return intval(strtolower(substr($this->url, strrpos($this->url, '/')+1)));
    }
    
    private function _uniord($ch)
    {
        $n = ord($ch{0});
        if ($n < 128) {
            return $n;
        }

        if ($n < 192 || $n > 253) {
            return false;
        }

        $arr = array(1 => 192, 2 => 224, 3 => 240, 4 => 248, 5 => 252);
        foreach ($arr as $key => $val) {
            if ($n >= $val) {
                $char[] = ord($ch{$key}) - 128;
                $range  = $val;
            } else {
                break;
            }
        }

        $retval = ($n - $range) * pow(64, sizeof($char));
        foreach ($char as $key => $val) {
            $pow        = sizeof($char) - ($key + 1);
            $retval    += $val * pow(64, $pow);
        }

        return $retval;
    }
    
    function getVideoUrl()
    {
        $p_file = '' .$this->getVideoId();
        if ($p_file == '') {
            $p_file=1;
        }
        $flor           = floor(($p_file/1000));
        $v_fileFloat    = '' .$flor;
        $v_fileLength   = strlen($p_file);
        for ($a = 1; $a <= 7 - $v_fileLength; $a++) {
            $p_file = '0' .$p_file;
        }

        $v_fileLength = strlen($v_fileFloat);
        for ($a = 1; $a <= 7 - $v_fileLength;$a++) {
            $v_fileFloat = '0' .$v_fileFloat;
        }
        
        $map        = array(
            "R", "1", "5", "3", "4", "2", "O", "7", "K", "9",
            "H", "B", "C", "D", "X", "F", "G", "A", "I", "J", "8", "L",
            "M", "Z", "6", "P", "Q", "0", "S", "T", "U", "V", "W", "E", "Y", "N"
        );
        $mapping    = '';
        $myInt      = 0;
        for ($a = 0; $a <= 6; $a++) {
            $bracket    = floor(substr($p_file,$a,1)*($a+1));
            $myInt      = $myInt + $bracket;
        }
        
        $myChar     = '' .$myInt;
        $myInt      = 0;
        for ($a = 0;$a< strlen($myChar);$a++) {
            $bracket    = floor(substr($myChar,$a,1));
            $myInt      = $myInt + $bracket;
        }
        
        if ($myInt >= 10) {
            $newChar = '' .$myInt;
        } else {
            $newChar = '0' .$myInt;
        }

        $x          = $this->_uniord(substr($p_file,3,1));
        $xx         = $x-48+$myInt+3;
        $mapping    = $mapping . $map[$xx];
        $x          = substr($newChar,1,1);
        $mapping    = $mapping . $x;
        $x          = $this->_uniord(substr($p_file,0,1));
        $xx         = $x-48+$myInt+2;
        $mapping    = $mapping . $map[$xx];
        $x          = $this->_uniord(substr($p_file,2,1));
        $xx         = $x-48+$myInt+1;
        $mapping    = $mapping . $map[$xx];
        $x          = $this->_uniord(substr($p_file,5,1));
        $xx         = $x-48+$myInt+6;
        $mapping    = $mapping . $map[$xx];
        $x          = $this->_uniord(substr($p_file,1,1));
        $xx         = $x-48+$myInt+5;
        $mapping    = $mapping . $map[$xx];
        $x          = substr($newChar,0,1);
        $mapping    = $mapping . $x;
        $x          = $this->_uniord(substr($p_file,4,1));
        $xx         = $x-48+$myInt+7;
        $mapping    = $mapping . $map[$xx];
        $x          = $this->_uniord(substr($p_file,6,1));
        $xx         = $x-48+$myInt+4;
        $mapping    = $mapping . $map[$xx];
        
        return 'http://dl.redtube.com/_videos_t4vn23s9jc5498tgj49icfj4678/' .$v_fileFloat. '/' .$mapping. '.flv';
    }
}
?>
