Exemple #1
0
function handle_upload($f)
{
    global $SETTINGS;
    include_once dirname(__FILE__) . '/TEapi.inc.php';
    $torr = new Torrent();
    // test for possible gzip/bzip in torrent file
    $status = check_if_compressed($f);
    if ($status > 0) {
        switch ($status) {
            case TORRENT_IS_GZIP:
                // file is gzip, uncompress and resave
                $gz = gzopen($f, 'rb');
                $gzip = '';
                while (!feof($gz)) {
                    $gzip .= gzread($gz, 4096);
                }
                gzclose($gz);
                file_put_contents($f, $gzip);
                unset($gzip);
                break;
            case TORRENT_IS_BZ2:
                // file is bz2, uncompess and resave
                $bz = bzopen($f, 'rb');
                $bzip = '';
                while (!feof($bz)) {
                    $bzip .= bzread($bz, 4096);
                }
                bzclose($bz);
                file_put_contents($f, $bzip);
                unset($bzip);
                break;
        }
    }
    if (!$torr->load(file_get_contents($f))) {
        @unlink($f);
        // remove the temp file
        return TORRAGE_FILE_INVALID;
    }
    @unlink($f);
    // remove the temp file
    $hashtorr = create_hashtorr($torr->getHash());
    // Read tracker list from existsing torrents
    if (file_exists($SETTINGS['savepath'] . $hashtorr . '.torrent')) {
        $existtorr = new Torrent();
        if (!$existtorr->load(gzdecode(file_get_contents($SETTINGS['savepath'] . $hashtorr . '.torrent')))) {
            $existtrackers = array();
        } else {
            $existtrackers = __flattern_array($existtorr->getTrackers());
        }
    }
    $tr_from = __flattern_array($torr->getTrackers());
    $tr = array();
    if (!empty($existtrackers) && count($existtrackers) > 0) {
        include_once dirname(__FILE__) . '/whitelist.inc.php';
    } else {
        $tr = $tr_from;
    }
    $tr = array_merge($tr, $existtrackers);
    $trackers = array_unique($tr);
    // Do tracker cleaning
    include_once dirname(__FILE__) . '/clean.inc.php';
    // store trackers into new array format for bencoding
    $__trackers = array();
    if (!empty($trackers) && is_array($trackers)) {
        foreach ($trackers as $a) {
            $__trackers[] = array($a);
        }
    }
    unset($existtrackers, $tr, $tr_from, $trackers);
    $torr->torrent->remove('comment.utf-8');
    $torr->setComment('Torrent downloaded from torrent cache at ' . getProto() . $SETTINGS['torrstoredns']);
    $torr->setTrackers($__trackers);
    $tdata = $torr->bencode();
    if (empty($tdata)) {
        return TORRAGE_FILE_ERROR;
    }
    $savefile = $SETTINGS['savepath'] . $hashtorr . '.torrent';
    @mkdir(dirname($savefile), 0777, true);
    file_put_contents($savefile, gzencode($tdata, 9));
    if ($SETTINGS['sync']['enabled']) {
        add_tosyncfiles($torr->getHash());
    }
    // sync to any possible mirrors
    /**
     * @todo:
     * PUSH mechanism caused too much of a slowdown
     * when lots of incoming hashes were occuring.
     * Changing to a pull system. Left this here
     * incase can come up with some kind of nicer
     * system to push, maybe fork the process as not
     * to slow down this.
     **/
    /*
    if( count( $SETTINGS['sync']['mirrors'] ) > 0 )
    {
    	$files = array(
    		array(
    			'name' => 'torrent',
    			'type' => 'application/x-bittorrent',
    			'file' => $savefile
    		)
    	);
    	
    	$options = array(
    		'timeout' => 10,
    		'connecttimeout' => 5,
    		'dns_cache_timeout' => 60,
    	);
    	
    	// iterate through mirrors
    	foreach( $SETTINGS['sync']['mirrors'] as $mirror )
    	{
    		// upload to mirrors,
    		// but if mirror is actually itself, or it comes from one of our mirrors,
    		// ignore it
    		if( $mirror['active'] === true && !( $_SERVER['SERVER_ADDR'] == $mirror['ip'] ) && !( $_SERVER['REMOTE_ADDR'] == $mirror['ip'] ) )
    		{
    			// upload to mirror
    			http_post_fields( 'http://'.$mirror['domain'].'/autoupload.php', array(), $files, $options );
    		}
    	}
    }
    */
    return $torr->getHash();
}