PHP UNRAR (good for rapidleech)

Status
Not open for further replies.

SplitIce

Active Member
2,474
2008
240
0
A simple php rar extractor, made for Mr-R-T for rapidleech v22 trial 2 which doesnt have rar/unrar support yet. You can change the files dir with the first variable in the script. Not my best work, took 9 mins.

PHP:
<?php
/*
Simple RAR Extractor


If running in rapidleech you may also need to add
<files rar.php>
allow from all
</files>
to .htaccess
*/
$dir = 'files';

if($_POST['file']){
    $file = new WFile_rar($_POST['file']);
    $files = $file->extract(array($_POST['password']));
    echo 'Extraction run<br /><br />';
}

echo '<table>';
$files = scandir($dir);
foreach($files as $f){
    if($f{0}!=='.'&&stripos($f,'.rar')){
        echo  '<tr><form action="rar.php" method="post"><input type="hidden" name="file" value="',$dir,'/',$f,'" /><td>',$f,'</td><td>Password: <input type="text" name="password" /></td><td><input type="submit" value="Extract" /></td></form></tr>';
    }
}
echo '</table>';



class WFile_rar {
    public $do = array('extract');
    public $ext = 'rar';
    private $files = array();
    
    private function _tdir($name) {
        global $core;
        mkdir ( $file );
        if (! chmod ( $file, 0777 ))
            die ( 'chmod() failed on file ' . $file );
        clearstatcache ();
        if (is_writable ( $file ))
            return $file; //File is writable, continue
        $core->error ( 'File is not writable: ' . $file, ERR_ERROR );
        return false;
    }

    private function _extract($to,$pass=false) {
        if($this->name){
            $password = '-p-';
            if($pass){
                $password = '-p'.escapeshellarg($pass);
            }
            exec('unrar e -o+ '.$password.' '.escapeshellarg($this->name).' '.$to.' 2>&1',$output);
            foreach($output as $o){
                if(strpos($o,'password incorrect')!==false||strpos($o,'CRC failed in')!==false){
                    return 'password';
                }
            }
            //Archive extracted successfully
            $this->files = scandir($to); //Get folder contents (extracted archive)
            return true;
        }
        return false;
    }
    
    function extract($passwords){
        global $core;
        $password = '';
        $to = dirname($this->name);
        $ret = $this->_extract($to);
        if($ret==='password'){//RAR is passworded
            $password = false;
            foreach($passwords as $pass){
                $r = $this->_extract($to,$pass);
                if($r===true){
                    $password = $pass;
                    break;
                }
            }
        }
        if($password===false&&$ret==='password'){
            die('Password couldnt be guessed');
            return false;
        }
        foreach($this->files as $k=>$f){
            $this->files[$k] = $f;
        }
        if($ret===true){//Most likely not passworded - else catch anyway
            return array(false,$this->files);
        }else if(trim($password)!==''){//Passworded
            return array(trim($password),$this->files);
        }
        return false;
    }
    
    function WFile_rar($name){
        $n = pathinfo($name);
            $this->name = $name;
    }
}
?>
 
10 comments
It works btw me and Mr-R-T are using it. It does require that you have unrar installed on the server. Its made up of code out of some of my old projects.
 
Status
Not open for further replies.
Back
Top