Location: PHPKode > projects > Nzbirc > nzbirc-1.4/nzbirc.php
<?php

/**************************************************
 * NZBirc v1
 * Copyright (c) 2006 Harry Bragg
 * tiberious.org
 * Main IRC file [extends SmartIRC]
 **************************************************
 *
 * Full GPL License: <http://www.gnu.org/licenses/gpl.txt>
 *
 * 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
 */


include_once( 'SmartIRC.php' );

// check the configuration file location, note userPath is required

// linux : nzbirc.php userPath [first] [daemon]
// windows : nzbirc.php [first] [daemon]

if ( $_SERVER['argc'] == 1 )
{
	if (strtoupper(substr(PHP_OS, 0,3) != 'WIN')) {
		printf("NZBirc - NZB administration through IRC\n\n");
		printf("usage: php nzbirc.php /path/to/config/file/ [first] [daemon]\n");
		printf("or call the bash launcher: nzbirc [--first]\n\n");
		printf(" - the path to the config file (not the file)\n");
		printf(" - if first is set, then the setup procedure will run\n");
		exit(0);
	}
}

$largs = $_SERVER['argv'];
array_shift( $largs );	

if (strtoupper(substr(PHP_OS, 0,3) != 'WIN'))
{
	array_shift( $largs );
	
	if ( isset( $_SERVER['argv'][1] ) )
	{
		$userPath = $_SERVER['argv'][1];
		if ( substr( $userPath, -1) != '/' )
		{
			$userPath = $userPath.'/';
		}
	}
	else
	{
		$userPath = realpath(dirname(__FILE__));
	}
}
else
{
	$userPath = realpath(dirname(__FILE__));
}

$first = false;

foreach( $largs as $mArg )
{
	switch ($mArg)
	{
		case 'first':
			$first = true;
			break;
		case 'daemon':
			$daemon = true;
			break;
	}
}

$inc = false;
if ( ( isset( $userPath ) ) && ( strlen( $userPath ) > 0 ) )
{
	if ( is_dir( $userPath ) )
	{
		if ( substr( $userPath, -1) != '/' )
		{
			$userPath = $userPath.'/';
		}
		if ( file_exists( $userPath.'config.inc.php') )
		{
			include_once( $userPath.'config.inc.php' );
			$inc = true;
		}
	}
}
else
{
	printf("Userpath not set, printing some debug info\n\n");
	printf("_SERVER['argc']: \n");
	var_dump( $_SERVER['argc'] );
	printf("_SERVER['argv']: \n");
	var_dump( $_SERVER['argv'] );
	exit(1);
}

// create userPath
// copy current config file to userPath
// or copy orig config file to userPath
// if orig: ask for store.xml file location
// copy store.xml & worker.xml to userPath
if ( !$inc )
{
	$first = true;
	
	if ( !isset( $userPath ) )
	{
		exit("Unable to find userPath, cannot load the config file\n");
	}
	
	echo "Unable to find config file, creating a new one\n";
	
	// check if userPath exists
	if ( !is_dir( $userPath ) )
	{
		// attempt to create it
		if (!mkdir( $userPath, 0755, true ) )
		{
			// create folder failed
			exit("Error: failed to create userPath: ".$userPath.", please check the path and permissions\n");
		}
	}
	
	if ( !is_writable( $userPath ) )
	{
		exit("Error: userPath: ".$userPath.", is not writable, please check the path and permissions\n");
	}
	
	// check if an existing config file exists in the home dir
	$src = dirname(__FILE__).'/config.inc.php';
	$dst = $userPath.'config.inc.php';
	if ( file_exists( $src ) )
	{
		// copy base to new file
		if ( !copy( $src, $dst ) )
		{
			printf('Error: Failed to copy config found in current directory [%s] to new config [%s]', $src, $dst);	
		}
		else
		{
			printf("Copied existing configuration file: %s to: %s\n", $src, $dst);
			$new = false;
			$new_config_copy = true;
		}
	}
	
	if (!$new_config_copy)
	{
		// check if source config file exists
		$osrc = dirname(__FILE__).'/config.inc.copy.php';
		if ( file_exists( $osrc ) )
		{
			// copy base to new file
			if ( !copy( $osrc, $dst ) )
			{
				exit('Error: Failed to copy original config ['.$osrc.'] to new config ['.$dst.']');	
			}
			else
			{
				printf("Successfully created new config file at: %s\n", $dst);
				$new = true;
			}
		}
		else
		{
			exit('Error: Unable to find local config file to copy, tried: '.$osrc.' and '.$src);
		}
	}
	
	// include new config file
	include_once( $dst );
	
	$inc = true;
}

if ( !is_dir( $userPath.'store' ) )
{
	printf("Unable to find a store at location: %s\n", $userPath.'store');
	
	$dst = $userPath.'store';
	
	// check source for store folder
	$src = dirname(__FILE__).'/store';
	
	if ( is_dir( $src ) )
	{
		printf("Found store in root directory [%s], copying...\n", $src);
		
		dirCopy( $src, $dst, 0755 );
		
		printf("Copied store files from [%s] to [%s]\n", $src, $dst);
	}
	else 
	{
		if ( !isset( $config->moduleConf['store'] ) )
		{
			$config->moduleConf['store']['filename'] = 'store.xml';
		}
		// check if store.xml doesnt exist in its current location
		if ( !file_exists( $config->moduleConf['store']['filename'] ) )
		{
			printf("Unable to find existing store files, \nWould you like to import existing versions?\n");
			printf("If this is your first time running nzbirc, you can select No to these questions\n");
			// Running a new setup with a new configuration file
			// would the user like to import a store.xml file?
			printf("Import an existing store.xml file? [y/N]: ");
			fscanf(STDIN, "%s\n", $simport);
			
			if ( $simport == 'y' )
			{
				// ask for the location
				$path = getVar("Path to store.xml file [ie /path/to/store.xml]: ", "%s\n");
				
				while ( !file_exists( $path ) )
				{
					printf("Error: File %s does not exist.\n", $path );
					$path = getVar("Path to store.xml file [ie /path/to/store.xml] (q to stop): ", "%s\n");
					
					if ( $path == 'q' )
					{
						$pfail = true;
						break;
					}
				}
				
				if (!$pfail)
				{
					// copying store.xml and worker.xml (if exists) to userPath
					$dst = $userPath.'store.xml';
					
					printf("Copying %s to %s...\n", $path, $dst);
					if ( copy( $path, $dst ) )
					{
						printf("Successfully copied store.xml from [%s] to [%s]\n", $path, $dst);
						// change configuration file to reflect changes
						$config->moduleConf['store']['filename'] = $dst;
						$ossucc = true;
					}
					else 
					{
						printf("Failed to copy %s to %s\n", $path, $dst);
					}
	
					$wpath = dirname($path).'/worker.xml';
					
					if ( file_exists( $wpath ) )
					{
						$wdst = $userPath.'worker.xml';
						
						printf("Copying %s to %s...\n", $wpath, $wdst);
						if ( copy( $wpath, $wdst ) )
						{
							printf("Successfully copied worker.xml from [%s] to [%s]\n", $wpath, $wdst);
							// change configuration file to reflect changes
							$config->moduleConf['store']['workerFilename'] = $wdst;
						}
						else 
						{
							printf("Failed to copy %s to %s\n", $wpath, $wdst);
						}
					}
				}
			}
			
			if (!$ossucc)
			{
				printf("Import an existing store directory? [y/N]: ");
				fscanf(STDIN, "%s\n", $simport);	
				
				if ( $simport == 'y' )
				{
					// ask for the location
					$path = getVar("Path to store directory [ie /path/to/store/]: ", "%s\n");
					$pfail = false;
					
					while ( !is_dir( $path ) )
					{
						printf("Error: Path %s is not a directory.\n", $path );
						$path = getVar("Path to store directory [ie /path/to/store/] (q to stop): ", "%s\n");
						
						if ( $path == 'q' )
						{
							$pfail = true;
							break;
						}
					}
									
					if (!$pfail)
					{
						$dst = $userPath.'store';
						
						printf("Copying store files from directory: %s...\n", $path);
						
						dirCopy( $path, $dst, 0755 );

						printf("Copied store files from [%s] to [%s]\n", $path, $dst);
					}
				}
			}
		}
	}
}

/**
 * Get a variable from the user
 *
 * @param string $string - The question to ask
 * @param string $match - The pattern to match (%s\n or %d\n)
 * @param mixed $exist - If an existing value exists (string if yes, false otherwise)
 * @return string - the answer from the user
 */
function getVar( $string, $match, $exist = false )
{
	if ( $exist !== false )
	{
		printf($string, $exist);
		fscanf(STDIN, $match, $tmpVar);
		if (($tmpVar == '') || ($tmpVar == '0'))
			return $exist;
		return $tmpVar;
	}
	else
	{
		printf($string);
		fscanf(STDIN, $match, $tmpVar);
		return $tmpVar;
	}
}

/**
 * Copy a folder
 *
 * @param string $srcDir - The source directory
 * @param string $dstDir - The destination directory
 * @param int $mode - The mode to create the folders/files
 */
function dirCopy( $srcDir, $dstDir, $mode )
{
	if ( substr( $srcDir, -1) != '/' )
		$srcDir .= '/';
	if ( substr( $dstDir, -1) != '/' )
		$dstDir .= '/';
	
	printf("DIR: %s > %s\n", $srcDir, $dstDir );  
	if ( !is_dir( $dstDir ) )
	{
		mkdir( $dstDir, $mode, true );
	}
	
	if ( $cDir = opendir( $srcDir ) )
	{
		while ( $file = readdir( $cDir ) )
		{
			if ( ( $file == '.' ) || ( $file == '..' ) ) continue;
					
			$srcFile = $srcDir . $file;
			$dstFile = $dstDir . $file;
			
			if ( is_file( $srcFile ) )
			{
				if ( copy( $srcFile, $dstFile ) )
				{
					chmod( $dstFile, $mode );
					printf('%s ',$file);
				}
				else
				{
					printf("\nFailed file: %s\n", $file);
				}
			}
			else if ( is_dir( $srcFile ) )
			{
				dirCopy( $srcFile, $dstFile, $mode );
			}
		}
		closedir( $cDir );
	}
	printf("\n");
}


if ( $new_config_copy )
{
	printf("\nFound an existing configuration file. \n");
	printf("If we didnt we would run the setup precedure here.\n");
	
	// configure hellanzb?
	printf("Re-configure NZBirc anyway? [y/N]: ");
	fscanf(STDIN, "%s\n", $reconf);
	
	if ( strtolower( $reconf ) == 'y' )
	{
		$first = true;
	}
	else
	{
		$first = false;
	}
}

if ( ( $inc ) && ( $first ) )
{
	// ask the user about some of the variables
	printf("\nRunning setup procedure, this will ask you some questions to setup\n");
	printf("the config file, \n\nrun nzbirc --first to do this again\n\n");
		
	if ( !$new )
	{
		$keys = array_keys( $config->irc );
		
		$k = $keys[0];
		
		$server = $config->irc[$k]['host'];
		$port = $config->irc[$k]['port'];
		$botname = $config->irc[$k]['nick'];
		$channel = $config->irc[$k]['channels'][0];

		$admin = $config->admin['nick'][0];
				
		$nusername = $config->moduleConf['nzb']['login']['username'];
		$npassword = $config->moduleConf['nzb']['login']['password'];
		$npath = $config->moduleConf['nzb']['download']['nzbPath'];
		
		$hhostname = $config->moduleConf['hella']['xmlrpc']['hostname'];
		$hport = $config->moduleConf['hella']['xmlrpc']['port'];
		$hpassword = $config->moduleConf['hella']['xmlrpc']['password'];		
	}
	
	$corr = false;
	
	while( !$corr )
	{
		// irc server
		if ( isset( $server ) )
			$server = getVar("IRC Server [default %s]: ", "%s\n", $server);
		else
			$server = getVar("IRC Server [ie irc.example.com]: ", "%s\n");
		
		// irc port
		if ( !isset( $port ) )
			$port = 6667;		
		$port = getVar("IRC Port [default %d]: ", "%d\n", $port);
		
		// irc channel
		if ( isset( $channel ) )
			$channel = getVar("IRC Channel [default %s]: ", "%s\n", $channel);
		else
			$channel = getVar("IRC Channel [ie #nzb]: ", "%s\n");

		// bot irc nick
		if ( !isset( $botname ) )
			$botname = '^nzb';
		$botname = getVar("Bot name [default %s]: ", "%s\n", $botname);

		// user name
		if ( isset( $admin ) )
			$admin = getVar("Your ircnick [default %s]: ", "%s\n", $admin);
		else
			$admin = getVar("Your ircnick: ", "%s\n");
			
		// configure hellanzb?
		printf("Configure Hellanzb? [Y/n]: ");
		fscanf(STDIN, "%s\n", $hconf);
		
		if ( ( $hconf == '' ) || ( strtolower( $hconf ) == 'y' ) )
		{
			$hconf = 'y';
			
			if ( isset( $hhostname ) )
				$hhostname = getVar("Hellanzb xmlrpc hostname [default %s]: ", "%s\n", $hhostname);
			else
				$hhostname = getVar("Hellanzb xmlrpc hostname: ", "%s\n");

			if ( !isset( $hport ) )
				$hport = 8760;	
			$hport = getVar("Hellanzb xmlrpc port [default %d]: ", "%d\n", $hport);

			if ( isset( $hpassword ) )
				$hpassword = getVar("Hellanzb xmlrpc password [default %s]: ", "%s\n", $hpassword);
			else
				$hpassword = getVar("Hellanzb xmlrpc password: ", "%s\n");
		}
		
		printf("Configure newzbin? [Y/n]: ");
		fscanf(STDIN, "%s\n", $nconf);
		
		if ( ( $nconf == '' ) || ( strtolower( $nconf ) == 'y' ) )
		{
			$nconf = 'y';
			
			if ( isset( $nusername ) )
				$nusername = getVar("Newzbin username [default %s]: ", "%s\n", $nusername);
			else
				$nusername = getVar("Newzbin username: ", "%s\n");

			if ( isset( $npassword ) )
				$npassword = getVar("Newzbin password [default %s]: ", "%s\n", $npassword);
			else
				$npassword = getVar("Newzbin password: ", "%s\n");
			
			if ( isset( $npath ) )
				$npath = getVar("Newzbin nzbPath [default %s]: ", "%s\n", $npath);
			else
				$npath = getVar("Newzbin nzbPath [ie /path/to/nzb/]: ", "%s\n");
			
			if ( substr( $npath, -1 ) != '/' )
				$npath .= '/';
		}
		
		// print out result and ask if it is correct
		printf("\nYour Settings: \n");
		printf("IRC Server:          %s\n", $server);
		printf("IRC Port:            %d\n", $port);
		printf("IRC Channel:         %s\n", $channel);
		printf("Bot name:            %s\n", $botname);
		printf("Your name:           %s\n", $admin);
		printf("Configure Hellanzb?: %s\n", $hconf);
		if ( $hconf == 'y' )
		{
			printf(" Hellanzb hostname:  %s\n", $hhostname);
			printf(" Hellanzb port:      %s\n", $hport);
			printf(" Hellanzb password:  %s\n", $hpassword);
		}
		printf("configure Newzbin?:  %s\n", $nconf);
		if ( $nconf == 'y' )
		{
			printf(" Newzbin username:   %s\n", $nusername);
			printf(" Newzbin password:   %s\n", $npassword);
			printf(" Newzbin nzbPath:    %s\n", $npath);
		}
		printf("\nAre these details correct? [Y/n/q]: ");
		fscanf(STDIN, "%s\n", $dc);
		if ( ( $dc == '') || ( strtolower( $dc ) == 'y' ) )
		{
			$corr = true;
		}
		else if ( strtolower( $dc ) == 'q' )
		{
			exit("Quitting...\n");
		}
	}
	
	if ( $corr )
	{
		// write config variables
		
		$keys = array_keys( $config->irc );
		
		$k = $keys[0];
		
		$config->irc[$k]['host'] = $server;
		$config->irc[$k]['port'] = $port;
		$config->irc[$k]['nick'] = $botname;
		$config->irc[$k]['channels'][0] = $channel;

		$config->admin['nick'][0] = $admin;
		
		$config->userPath = $userPath;
		$config->workerPidPath = $userPath.'worker.pid';
		$config->debugLogPath = $userPath.'debug.log';
		$config->workerLogPath = $userPath.'worker.log';		
		
		if ( $nconf )
		{
			$config->moduleConf['nzb']['nbLogin'] = true;
			$config->moduleConf['nzb']['login']['username'] = $nusername;
			$config->moduleConf['nzb']['login']['password'] = $npassword;
			$config->moduleConf['nzb']['download']['nzbPath'] = $npath;
		}
		
		if ( $hconf )
		{
			if (!in_array( 'hella', $config->modules ) )
			{
				$config->modules[] = 'hella';
			}
			$config->moduleConf['hella']['xmlrpc']['hostname'] = $hhostname;
			$config->moduleConf['hella']['xmlrpc']['port'] = $hport;
			$config->moduleConf['hella']['xmlrpc']['password'] = $hpassword;
		}
	}
}

if ( !$inc )
{
	exit("Something went wrong, config file not included, and we couldn't figure out why, report this as a bug\n");
}

unset( $inc );

// check daemon here
if ( $daemon )
{
	// set it to spawn a daemon process
	$config->useFork = true;
}

// do some path checks here
if ( ( !isset( $config->userPath ) ) ||
	 ( $config->userPath == '' ) ||
	 ( $config->userPath == '/home/user/.nzbirc/' ) ||
	 ( $config->userPath != $userPath ) )
{
	$config->userPath = $userPath;
	$config->workerPidPath = $userPath.'worker.pid';
	$config->debugLogPath = $userPath.'debug.log';
	$config->workerLogPath = $userPath.'worker.log';
	
	if ( $config->moduleConf['nzb']['language'] == 'English' )
		$config->moduleConf['nzb']['language'] = ''; 
}

if ( ( !isset( $config->basePath ) ) ||
     ( $config->basePath == '' ) ||
     ( $config->basePath != realpath($argv[0] ) ) )
{
	$config->basePath = realpath(dirname(__FILE__));
	$config->modulePath = $config->basePath.'/modules';
	$config->workerPath = $config->basePath.'/worker.php';
	
	if ( $config->moduleConf['nzb']['language'] == 'English' )
		$config->moduleConf['nzb']['language'] = '';
}

// check log files
if ( ( !is_dir( dirname( $config->debugLogPath ) ) ) ||
     ( !is_writable( $config->debugLogPath ) ) )
{
	$config->debugLogPath = $config->userPath.'debug.log';
	$config->workerLogPath = $config->userPath.'worker.log';
	
	if ( $config->moduleConf['nzb']['language'] == 'English' )
		$config->moduleConf['nzb']['language'] = '';
}

if ( ( !is_dir( dirname( $config->workerLogPath ) ) ) ||
     ( !is_writable( $config->workerLogPath ) ) )
{
	$config->workerLogPath = $config->userPath.'worker.log';
	
	if ( $config->moduleConf['nzb']['language'] == 'English' )
		$config->moduleConf['nzb']['language'] = '';
}

unset( $largs, $daemon, $first );

include_once( 'irc.php' );

$irc = new irc( );

$irc->setup();

?>
Return current item: Nzbirc