Location: PHPKode > projects > BuddyServ IRC Services > buddyserv1.5.1-release/extensions/buddyseed.1.0-geneva/class/Class.download.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.             *
 ***************************************************************************/

/* Download object. */

class download
{
	private $data;
	protected $root;
	protected $folder;
	protected $filelist=array();
	protected $torrent;
	protected $current_piece=0;
	protected $status = 0;
	protected $directory;
	protected $start_time;
	protected $end_time;
	protected $size=0;
	public $nfo;
	
	
	public function __get($what)
	{
		return ($this->{$what} ? $this->{$what} : null);
	}
	
	public function __construct($category, $folder,$start=false,$verbose=true)
	{
		$this->root = $category;
		$this->folder = $folder;
		$this->directory = $category.'/'.$folder.'/';
		
		if ($start)
			$this->run($verbose);
	}
	
	public function run($verbose)
	{
		global $remote, $config;
	
		$this->start_time = time();
		$ftp = ftp_ssl_connect($config['ftp']['host'],$config['ftp']['port']);
		
		if (!$ftp)
			return interm::send('FTP connect failed: '.$this->folder);
			
		/* try login */
		if (!ftp_login($ftp,$config['ftp']['user'], $config['ftp']['password']))
			return interm::send("\0034!\2Severe!\2\003: FTP Login rejected!: ".$config['ftp']['user']);

		if (!@ftp_chdir($ftp, $this->root))
		{
			$remote->query("DELETE FROM `buddyserv_releases` WHERE `category` = '".mysql_real_escape_string($this->root)."' AND `name` = '".mysql_real_escape_string($this->folder)."' LIMIT 1");
			return interm::send("\0034Missing\003: ROOT for ".$this->folder);
		}
		
		if (!@ftp_chdir($ftp, $this->folder))
		{
			$remote->query("DELETE FROM `buddyserv_releases` WHERE `category` = '".mysql_real_escape_string($this->root)."' AND `name` = '".mysql_real_escape_string($this->folder)."' LIMIT 1");		
			return interm::send("\0034Missing\003: '".$this->folder);
		}
		
		$files = ftp_nlist($ftp, ".");
		
		if (!$files)
		{
			$remote->query("DELETE FROM `buddyserv_releases` WHERE `category` = '".mysql_real_escape_string($this->root)."' AND `name` = '".mysql_real_escape_string($this->folder)."' LIMIT 1");
			return interm::send("\0037Empty\003: ".$this->folder);
		}
		
		$this->filelist = array();
		
		foreach ($files as $null => $name)
		{
			// ignore parent folder/current
			if ($name == '.' or $name == '..')
				continue;
				
			if (substr($name, 0, 1) == '[')
				continue;
				
			if (substr($name, (strlen($name) - 4), strlen($name)) == '.nfo')
				$this->nfo = $name;
				
			$this->filelist[] = $name;	
		}
		
		if (count($this->filelist) == 0)
		{
			$remote->query("DELETE FROM `buddyserv_releases` WHERE `category` = '".mysql_real_escape_string($this->root)."' AND `name` = '".mysql_real_escape_string($this->folder)."' LIMIT 1");
			return interm::send("\0037Empty\003: No files in folder: \00310".$this->folder);
		}
		
		if (!is_dir(UPDIR.$this->folder) and !mkdir(UPDIR.$this->folder))
		{
			return interm::send("\0034\2!Severe!\003: Unable to create download directory: ".$this->folder);
		}
		
		/* all good, give the log our update and continue */
		interm::send("\00310Active\003: fetching ".count($this->filelist)." files; \00310".$this->folder);
		
		foreach ($this->filelist as $one)
		{
			$starttime = time();
			$fileis = UPDIR.$this->folder.'/'.$one;
			$extension = substr($one,(strlen($one) - 4), strlen($one));
			
			if (($extension == '.nfo') or ($extension == '.txt'))
				$type = FTP_ASCII;
			
			else 
				$type = FTP_BINARY;
				
			if (@ftp_chdir($ftp, $one))
			{
				// directory
				@ftp_chdir($ftp, '..');
			}
			
			else
			{
				if (!ftp_get($ftp, $fileis, $one, $type))
				{
					$remote->query("DELETE FROM `buddyserv_releases` WHERE `category` = '".mysql_real_escape_string($this->root)."' AND `name` = '".mysql_real_escape_string($this->folder)."' LIMIT 1");
					interm::send("\0034Failed\003: Transfering ".$one." from \00310".$this->folder."\003");
				}
				
				$this->size += filesize($fileis);
			}
			if ((time() - $starttime) > 20)
				interm::send("PING");
			
			
		}
		
		$this->end_time = time();
		$size = convert_bytes($this->size);
		interm::send("\0033Fetched\003: [".$size['size'].$size['byte']."] in ".duration($this->start_time, $this->end_time)." => \00310".$this->folder);
		
		return true;
	}
	
}

?>
Return current item: BuddyServ IRC Services