<?php
class VGrab_haporn
{
    var $url;
    var $page;
    var $curl;
    function __construct()
    {
        $this->curl = new VCurl();
    }
    
    function VGrab_haporn()
    {
        $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('/Haporn.com - (.*?) - Free Adult Video Sharing Website/ms', $this->page, $matches);
        if ( isset($matches['0']) ) {
			$title 	= str_replace('Haporn.com -', '', $matches['0']);
			$title	= str_replace('- Free Adult Video Sharing Website', '', $title);
            return trim(strip_tags($title));
        }
    }
    
    function getVideoTags()
    {
        preg_match('/<DIV class="videoDetailEntry-videoDetail-tags-div">(.*?)<\/DIV>/', $this->page, $matches);
        if ( isset($matches['0']) ) {
            $tags   = trim(strip_tags($matches['0']));
            $tags   = str_replace('Tags:', '', $tags);
            $tags   = str_replace('&nbsp;', '', $tags);
            
            return $tags;
        }
    }
    
    function getVideoCategory()
    {
        preg_match('/<span class="watch-channel-stat">Channels:<\/span>(.*?)<\/DIV>/', $this->page, $matches);
        if ( isset($matches['0']) ) {
            $category   = trim(strip_tags($matches['0']));
            $category   = str_replace('Channels:', '', $category);
            $category   = trim(str_replace('&nbsp;', '', $category));
            $pos        = strpos($category, ' ');
            $category   = substr($category, 0, $pos);
            
            return $category;
        }
    }
    
    function getVideoId()
    {
        $url_array = explode('/', $this->url);
        foreach ( $url_array as $key => $value ) {
            if ( $value == 'video' ) {
                if ( isset($url_array[$key+1]) ) {
                    $this->video_id = $url_array[$key+1];
                    return $this->video_id;
                }
            }
        }
    }
    
    function getVideoUrl()
    {
        $player_config_url = 'http://www.haporn.com/opt/1/' .$this->video_id. '/';
        $this->getPage($player_config_url);

        preg_match('/FLVPath(.*?)\/>/', $this->page, $matches);
        if ( isset($matches['0']) ) {
            $flv_url    = str_replace('FLVPath" Value="', '', $matches['0']);
            $flv_url    = str_replace('/>', '', $flv_url);
            
            return base64_decode($flv_url);
        }
    }
}
