Location: PHPKode > projects > BuddyServ IRC Services > buddyserv1.5.1-release/extensions/buddyseed.1.0-geneva/class/Class.torrent.php
<?php

/***************************************************************************
 *   Copyright (C) 2009 by lynk   					                       *
 *   hide@address.com   						                           *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

/* Torrent creator */

class torrent
{
	protected $name;
	protected $category;
	protected $folder;
	protected $nfo;
	static $categories = array(
			'TV-XVID'	=> 5,
			'XVID'		=> 10,
			'0DAY'		=> 0,
			'XXX'		=> 6,
			'APPS'		=> 0,
			'DVDR'		=> 11,
			'PC-GAMES'	=> 2,
			'WII'		=> 27,
			'REQUESTS'	=> 0
			);
	protected $infohash;
	protected $success=false;
	
	/* !!READ!!
	 * Destruction of this object WILL also result in the destruction
	 * of the temporary .torrent in the root directory. This is necessary
	 * cleanup, wouldnt want anyone accidently 'delete' something they
	 * shouldnt trying to clean them */
	public function __destruct()
	{
		/* remove the torrent in the current directory if exists */
		if (file_exists($this->name.'.torrent'))
			unlink($this->name.'.torrent.');
			
	}
	
	
	
	/* Calling construct causes a call to bittornado in torrentflux
	 * Its much easier to have something stable to rely on for the
	 * base torrent creation. Parts of the code are from torrentflux THNKS!
	 */
	public function __construct($name, $nfo, $category)
	{
		global $remote;
		
		$this->category = $category;
			
		global $config;
		$this->name = $name;
		$this->nfo = $nfo;
		
		$run = "nohup ".$config["python"]." -OO ".$config["maketorrent"] ." ".escapeshellarg($config['announcer'])." " .UPDIR . $name . " ";
		$run .= '--target '.$name.'.torrent';
		
		exec($run);

        // We want to check to make sure the file was successful
        $success = false;
        $fn = getcwd().'/'.$name.'.torrent';
        $raw = file_get_contents(getcwd().'/'.$name.'.torrent');
        
        if( preg_match( "/6:pieces([^:]+):/i", $raw, $results ) )
        {
            // This means it is a valid torrent
            $success = true;
            $this->success = true;

            if( preg_match( "/6:pieces([^:]+):/i", $raw, $results ) )     
          	{
                $pos = strpos( $raw, "6:pieces" ) + 9 + strlen( $results[1] ) + $results[1];
                $fp = @fopen($name.'.torrent', "r+" );
                 @fseek( $fp, $pos, SEEK_SET );

                 @fwrite( $fp, "7:privatei1eee" );
                 @fclose( $fp );
             }
        }
        
        if (!$success)
        	return interm::send("\0034Failed:\003 Problem creating torrent: ".$name);
        	
        $dict = bdec_file($fn, (1024*1024));
		
		$dict=bdec(benc($dict)); // double up on the becoding solves the occassional misgenerated infohash
		$dict['value']['comment']=bdec(benc_str( "In using this torrent you are bound by the TorrentMax Confidentiality Agreement By Law")); // change torrent comment
		list($ann, $info) = dict_check($dict, "announce(string):info");
		unset($dict['value']['created by']); //Null the created_by field///
		$this->infohash = pack("H*", sha1($info["string"]));
		
		/* This is the copy we will upload to the server */
		$fp = fopen(getcwd().'/'.$name.'.torrent', "w");
		
		if ($fp)
		{
        	@fwrite($fp, benc($dict), strlen(benc($dict)));
   			fclose($fp);
		}
		
		else 
			return interm::send("\0034Failed:\003 Unable to save torrent meta for: ".$name);

		/* save the torrent we are going to seed after appending our seedkey 
		 * we save this one in the proper directory. The one we generated in
		 * the root will be sent to the webserver. It does not contain the seedkey */
		$dict['value']['announce']['value'] = "http://torrentmax.org/announce.php?passkey=".$config['passkey'];
		$dict['value']['announce']['string'] = strlen($dict['value']['announce']['value']).":".$dict['value']['announce']['value'];
		$dict['value']['announce']['strlen'] = strlen($dict['value']['announce']['string']);
		
		$fp = fopen(TORRENTDIR.$name.'.torrent', "w");
		
		if ($fp)
		{
        	@fwrite($fp, benc($dict), strlen(benc($dict)));
   			fclose($fp);
		}
		
		else 
			return interm::send("\0034Failed:\003 Unable to save torrent seedkey for: ".$name);
			
        interm::send("\0033Created:\003 Torrent created: ".$name);
        
  }
    
 	public function __get($what)
	{
		return ($this->{$what} ? $this->{$what} : null);
	}
	
    static function real_category($which)
    {
    	$which = strtoupper($which);
    	
    	if (array_key_exists($which, self::$categories))
    	{
    		$real = self::$categories[$which];
    		
    		if ($real === 0)
    			return 0;
    			
    		return $real;
    	}
    	
    	return false;
    }
}
?>
Return current item: BuddyServ IRC Services