<?php
class VGrab_wanktube
{
    var $url;
    var $page;
    var $curl;
    function __construct()
    {
        $this->curl = new VCurl();
    }
    
    function VGrab_pornhub()
    {
        $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('/<title>(.*?)<\/title>/', $this->page, $matches);
        if ( isset($matches['0']) ) {
            $title  = str_replace('<title>', '', $matches['0']);
            $title  = str_replace('</title>', '', $title);
            
            return $title;
        }
    }
    
    function getVideoTags()
    {
        preg_match('/Tags:<\/SPAN>(.*?)<\/DIV>/', $this->page, $matches);
        if ( isset($matches['0']) ) {
            preg_match_all('/>[a-zA-Z0-9]+<\/A>/', $matches['0'], $matches_tag);
            if ( isset($matches_tag['0']) ) {
                $tag_links  = $matches_tag['0'];
                $tags       = array();
                foreach ( $tag_links as $link ) {
                    $tag    = str_replace('>', '', $link);
                    $tag    = str_replace('</A', '', $tag);
                    $tags[] = $tag;
                }
                
                return implode(' ', $tags);
            }
        }
    }
    
    function getVideoCategory()
    {
        preg_match('/Channels:<\/SPAN>(.*?)<\/DIV>/', $this->page, $matches);
        if ( isset($matches['0']) ) {
            preg_match('/>[a-zA-Z0-9]+<\/A>/', $matches['0'], $matches_channel);
            if ( isset($matches_channel['0']) ) {
                $category   = str_replace('>', '', $matches_channel['0']);                
                return str_replace('</A', '', $category);
            }
        }
    }
    
    function getVideoId()
    {
        preg_match('/http:\/\/www.wanktube.com\/flvideo\/(.*?).flv"/ms', $this->page, $matches);
        if ( isset($matches['0']) ) {
            $video_id       = str_replace('"', '', $matches['0']);
            $video_id       = str_replace('http://www.wanktube.com/flvideo/', '', $video_id);
            $video_id       = str_replace('.flv', '', $video_id);
            $this->video_id = $video_id;
            return $video_id;
        }         
    }
    
    function getVideoUrl()
    {
        $url    = 'http://www.wanktube.com/flvideo/' .$this->video_id. '.flv';
        return $url;
    }
}