Marius van Witzenburg We fight for our survival, we fight!

14Jun/117

Download Redtube movies with a simple PHP class

Posted by mariusvw

For donations I build everything...

Simply put this file on a server where you want and start downloading ;-)

class RedTube {
    function form() {
        echo '<form method="post"><input type="text" name="url"><input type="submit"></form>';
    }
 
    function download($url = null) { 
        if ($this->validate($url)) {
            $content = $this->fetch($url);
 
            preg_match('#<h1 class="videoTitle">(.*)</h1>#', $content, $d1);
            //preg_match('#<source src=\'(.*)\' type=\\\"video/mp4\\\">#', $content, $d2);
            preg_match('#<source src=\'(.*)\' type=\'video/mp4\'>#', $content, $d2);
 
            if (!empty($d1[1]) && !empty($d2[1])) {
                header("Content-type: application/octet-stream");
                header('Content-Disposition: attachment; filename="' . addslashes($d1[1] . '.mp4') . '"'); 
                $this->fetch($d2[1], true);
            }       
        }       
    }
 
    private function validate($url = null) { 
        if (preg_match('#^http://(www.)?redtube.com/[0-9]+$#', $url)) {
            return true;
        }       
        return false;
    }
 
    private function fetch($url = null, $stream = false) {
        $handle = fopen($url, 'r');
        $buffer = null; 
        if ($handle) {
            if ($stream) {
                while (!feof($handle)) {
                    echo fgets($handle, 4096);
                }       
            } else {
                while (!feof($handle)) {
                    $buffer .= fgets($handle, 4096);
                }       
            }       
            fclose($handle);
        }       
        return $buffer;
    }
}
 
$rt = new RedTube;
 
if (!empty($_POST['url'])) {
    $rt->download($_POST['url']);
} else {
    $rt->form();
}