Location: PHPKode > projects > BuddyServ IRC Services > buddyserv1.5.1-release/extensions/buddyseed.1.0-geneva/buddyseed.php
#!/usr/local/bin/php

<?php

/* THIS WILL NOT WORK WITHOUT BuddyServ V2.0 or later.
 *
 ***************************************************************************
 *   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.             *
 ***************************************************************************/

/* Main script */

define('VERSION', '1.0-geneva');

echo "Attempting to fork()...\n";
$pid = pcntl_fork();

/* Check for a error */
if ($pid == -1)
{
	die('Unable to fork() process. Task terminating!');
}

elseif ($pid != 0)
{
	/* this is the parent, tell the user that the script is running, and then quit */
	echo "BuddySeed (Service Extension) sucessfully entering background mode\n\n";
	exit;
}

/* We are now in the child process. Start setting timeouts, and signal handling */
ini_set("max_execution_time", "0");
ini_set("max_input_time", "0");
set_time_limit(0);

declare(ticks = 1);

require_once('config.php');
require_once('torrentfunc.php');

$local = new mysql(false);
$remote= new mysql(false);

$local->connect($config['database_local']['host'],3306,
	$config['database_local']['password'],$config['database_local']['user'],$config['database_local']['db']);
	
$remote->connect($config['database_remote']['host'],3306,
	$config['database_remote']['password'],$config['database_remote']['user'],$config['database_remote']['db']);

while (1)
{
	maingo();
}

function maingo()
{
	global $local, $remote;
	
	interm::send('Initializing... -BuddySeed v'.VERSION);
	
	while (1)
	{
		echo 'doing loop';
		
		$query = $remote->query("SELECT * FROM `buddyserv_releases` WHERE `start` = 1 ORDER BY `id` ASC LIMIT 10");
		
		if ($remote->num_rows($query) == 0)
		{
			
			sleep(10);
			interm::send("PING");
			continue;
		}
		
		/* New releases to download */
		if ($remote->num_rows($query) > 0)
		{
			while ($each = $remote->fetch_array($query))
			{

				$realcat = torrent::real_category($each['category']);
        
       			if ($realcat === 0)
       			{
       				$remote->query("DELETE FROM `buddyserv_releases` WHERE `category` = '".mysql_real_escape_string($each['category'])."' AND `name` = '".mysql_real_escape_string($each['name'])."' LIMIT 1");
        			interm::send("\0037Halted\003: [\00310".$each['category']."\003] not allowed: \00310".$each['name']."\003");
        			continue;
       			}
       			
       			if ($realcat === false)
       			{
       				$remote->query("DELETE FROM `buddyserv_releases` WHERE `category` = '".mysql_real_escape_string($each['category'])."' AND `name` = '".mysql_real_escape_string($each['name'])."' LIMIT 1");
        			interm::send("\0037Halted\003: [\00310".$each['category']."\003] not supported: \00310".$each['name']."\003");
        			continue;
       			}
        	
				$download = new download($each['category'], $each['name'], false, false);
				
				
				if ($download->run(true))
				{
					/* Create the torrent now */
					$tor = new torrent($each['name'], ($download->nfo ? $download->nfo : NULL), $each['category']);
					
					        /* We made it, now we need to send the information to the database */
		        
					 if (!$tor->success)
					 {
						continue;
					 }
					 
		        /* If we have an nfo file, read it to memory, and send as description */
		        if ($tor->nfo)
		        {
		        	
		        	$fs = fopen(UPDIR.$tor->name.'/'.$tor->nfo, 'r');
		        	
		        	if (!$fs)
		        		$description = str_replace('.', ' ', $tor->name);
		        		
		        	else 
		        	{
		        		
		        		while ($line = fgets($fs))
		        		{
		        			$description .= $line."\r\n";
		        		}
		        	}
		        	
		        	fclose($fs);
		        }
		        
		        $description = $remote->escape($description);
		        
		       	$pre_query = $remote->query("SELECT *, UNIX_TIMESTAMP(timestamp) AS timestamp FROM pre WHERE name = '".$tor->name."'");
				
				if($remote->num_rows($pre_query) > 0)
				{
				    $rls = $remote->fetch_array($pre_query);
				    $rls_time = time() - $rls['timestamp'];
				    $days = floor($rls_time / (60 * 60 * 24));
				    $remainder = $rls_time % (60 * 60 * 24);
				    $hours = floor($remainder / (60 * 60));
				    $remainder = $remainder % (60 * 60);
				    $minutes = floor($remainder / 60);
				    $seconds = $remainder % 60;
				    
				    $rls_time_ago = '';
				    
				    if($days > 0)
				        $rls_time_ago .= $days.' Days, ';
				        
				    if($hours > 0)
				        $rls_time_ago .= $hours.' Hr, ';
				        
				    if($minutes > 0)
				        $rls_time_ago .= $minutes.' Min, ';
				        
				    $rls_time_ago .= $seconds.' Seconds';
				    
				}
				
				else 
				{
					$rls_time_ago = null;
				}
				$fail = false;
				$hash = mysql_real_escape_string($tor->infohash);
				$remote->query("INSERT INTO `torrents` (`info_hash`, `name`, `filename`, `save_as`, `search_text`, `descr`, `ori_descr`,".
					"`size`, `added`, `type`, `numfiles`, `visible`, `owner`, `nfo`, `afterpre`, `scene`, `category`) VALUES ".
					"('".$hash."', '".$tor->name."', '".$tor->name.".torrent','".$tor->name."', '".
					str_replace('.', ' ', $tor->name)."', '".$description."', '', '".$download->size."',".
					"FROM_UNIXTIME(".time()."), 'multi', '".count($download->filelist)."', 'no', '".USERID."', '".$description."', '".$rls_time_ago."', 'yes', '".$realcat."')") or $fail=true;

		     if (!$fail)
		       $id = $remote->insert_id();
		      
		      else 
		      	$id = false;
		      	
		       	if (!$id)
		      	{
		       		interm::send("\0034Error\003: [\00310".$each['category']."\003] could not add to database: \00310".$each['name']."\003");
		  			continue;
				}
				
				$docheck = UPDIR.$tor->name.'/';
				
				foreach ($download->filelist as $toadd)
				{
					if (filesize($docheck.$toadd) == -1)
						continue;
						
					$remote->query("INSERT INTO `files` (`torrent`, `filename`, `size`) VALUES ('".$id."', '".$toadd."', '".filesize($docheck.$toadd)."')");
				}
				
				/* rename the file */
			//	rename($each['name'].'.torrent', $id.'.torrent');
				/*Ftp To sever */
				
				if (file_exists($each['name'].'.torrent'))
				{
					$ff = ftp_connect(RFTP_HOST, 21);
					$s1 = ftp_login($ff, RFTP_USER, RFTP_PASS);
					$s2 = ftp_put($ff,$id.'.torrent', $each['name'].'.torrent', FTP_BINARY);
					ftp_close($ff);
				}
				
				if (!$s1 or !$s2)
				{
					interm::send("\0034Failed\003: FTP to TorrentMax failed: \00310".$each['name']."\003");
					continue;
				}
				
				$remote->query("DELETE FROM `buddyserv_releases` WHERE `category` = '".mysql_real_escape_string($each['category'])."' AND `name` = '".mysql_real_escape_string($each['name'])."' LIMIT 1");		
				interm::send("\0033Complete\003: (".$id.") \00310".$each['name']."\003");
			}
		}
		
		//sleep(10);
		//quit('End of script execution');
		//echo 'End result';
	}
	
	}
	
	
}

function duration($s, $e)
{  
    /* Find out the seconds between each dates */
    $timestamp = $e - $s;
    
    /* Cleaver Maths! */
    $years=floor($timestamp/(60*60*24*365));$timestamp%=60*60*24*365;
    $weeks=floor($timestamp/(60*60*24*7));$timestamp%=60*60*24*7;
    $days=floor($timestamp/(60*60*24));$timestamp%=60*60*24;
    $hrs=floor($timestamp/(60*60));$timestamp%=60*60;
    $mins=floor($timestamp/60);$secs=$timestamp%60;
   
    /* Display for date, can be modified more to take the S off */
    if ($years >= 1) { $str.= $years.'y'; }
    if ($weeks >= 1) { $str.= $weeks.'w'; }
    if ($days >= 1) { $str.=$days.'d'; }
    if ($hrs >= 1) { $str.=$hrs.' h'; }
    if ($mins >= 1) { $str.=$mins.'m'; }
    
    if (!$str)
    	$str = $timestamp."s";
   
     return $str;      
}
function convert_bytes($size)
{
	$sizetype = 0;
	
	while (1)
	{
		if (($size / 1024) > 1)
		{
			$sizetype++;
			$size = ($size / 1024);
				
				//	echo "size is now: {$size} with type {$sizetype}";
		}
				
		else 
			break;
		}

		$size = sprintf("%.2f", $size);
			
		if ($sizetype == 1)
			$byte = 'KB';
			
		elseif ($sizetype == 2)
			$byte = 'MB';
				
		elseif ($sizetype == 3)
			$byte = 'GiB';
				
		elseif ($sizetype == 4)
			$byte = 'TB';
			
		$array['size'] = $size;
		$array['byte'] = $byte;
		
		return $array;
}
function quit($reason='no reason')
{
	interm::send('Shutting down... '.$reason);
	exit;
}

/* Autoloading */

function __autoload($class)
{
	if (!class_exists($class))
	{
		if (file_exists('class/Class.'. $class .'.php'))
		{
			require_once('class/Class.'. $class .'.php');
		}
		
		else 
		{
			// PUT ITERM HERE
		}
	}
}

?>
Return current item: BuddyServ IRC Services