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

    function getPage( $url ) {
        $this->url  = $url;
        if ( $this->page = $this->curl->saveToString($url) ) {
            $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('/<strong>Title<\/strong>: (.*?)<\/td>/', $this->page, $matches);
        if ( isset($matches['0']) ) {
            $title  = str_replace('<strong>Title</strong>: ', '', $matches['0']);
            $title  = str_replace('</td>', '', $title);
            
            return $title;
        }
    }
    
    function getVideoTags()
    {
        preg_match('/<strong>Tags<\/strong>:(.*?)<\/td>/', $this->page, $matches);
        if ( isset($matches['0']) ) {
            $tag_string = str_replace('<strong>Tags</strong>:', '', $matches['0']);
            $tag_string = str_replace('</td>', '', $tag_string);
            preg_match_all('/<b>(.*?)<\/b>/', $tag_string, $matches_tag);
            if ( isset($matches_tag['0']) ) {
                $tag_links  = $matches_tag['0'];
                $tags       = array();
                foreach ( $tag_links as $link ) {
                    $tag    = str_replace('<b>', '', $link);
                    $tag    = str_replace('</b>', '', $tag);
                    $tags[] = $tag;
                }
                
                return implode(' ', $tags);
            }
        }
    }
    
    function getVideoCategory()
    {
        preg_match('/<strong>Category<\/strong>:(.*?)<\/td>/', $this->page, $matches);
        if ( isset($matches['0']) ) {
            preg_match('/<b>(.*?)<\/b>/', $matches['0'], $matches_category);
            if ( isset($matches_category['0']) ) {
                $category   = str_replace('<b>', '', $matches_category['0']);
                $category   = str_replace('</b>', '', $category);
                
                return $category;
            }
        }
    }
    
    function getVideoId()
    {
        preg_match('/needLogin\(\'vote\(1, (.*?)\)\'/', $this->page, $matches);
        if ( isset($matches['0']) ) {
            $video_id   = str_replace(' ', '', $matches['0']);
            $video_id   = str_replace('needLogin(\'vote(1,', '', $video_id);
            $video_id   = str_replace(')\'', '', $video_id);
            
            return $video_id;
        }
    }
    
    function getVideoUrl()
    {
        preg_match('/var videourl="(.*?)";/', $this->page, $matches);
        if ( isset($matches['0']) ) {
            $url    = str_replace('var videourl="', '', $matches['0']);
            $url    = str_replace('";', '', $url);
            
            return $url;
        }
    }
}