Location: PHPKode > scripts > OurWebFTP > ourwebftp/setup.php
<?
define( 'SETUP_FILE', 'mwftp5_distrib.php' );
define( 'MAIN_DIR', dirname(__FILE__) );
define( 'LD_DIR', MAIN_DIR . '/mwftp5' );
define( 'THIS_FILE',  basename( __FILE__) );

define( 'FILE_TYPE_SKIP',	0 );
define( 'FILE_TYPE_APP',	1 );
define( 'FILE_TYPE_CONF',	2 );

$app = 'hoster';
switch( $app ){
	case 'hoster':
		$versionFile = LD_DIR . '/hoster/version.txt';
		break;
	case 'personal':
		$versionFile = LD_DIR . '/personal/version.txt';
		break;
	case 'free':
		$versionFile = LD_DIR . '/free/version.txt';
		break;
	}
define( 'VERSION_FILE', $versionFile );

$webDir = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['REQUEST_URI']);
if( substr($webDir, -1) == '/' )
	$webDir = substr($webDir, 0, -1);
define( 'WEB_DIR',		$webDir );

start_html();
if( checkReady() ){
	init();
	listSetupOptions();
	if ( isset($_REQUEST['step']) ){
		$step = $_REQUEST['step'];
		eval("step_$step();");
		}
	else
		step_start();
	}
end_html();

function listSetupOptions(){
	$options = array (
		'start'		=> 'Start',
		'core'	=> 'Install Application Files',
		'conf'	=> 'Create Default Configuration',
		);

	echo '<P>';
	foreach( $options as $key => $desc )
		echo '<A HREF="' . THIS_FILE . '?step=' . $key . '">' . $desc . '</A>&nbsp;&nbsp;';
	echo '</P>';
	}

function checkReady(){
//here I'll check if we have $setup_file and ds directory with 777 mode
	$main_dir = MAIN_DIR;
	$setup_file = SETUP_FILE;

	// CHECK IF SETUP FILE EXISTS
	if(! file_exists("$main_dir/$setup_file") ){
		echo "I found no <B>$main_dir/$setup_file</B>! It should be uploaded to the same directory where this setup file is. Please put it in the correct place and try again.<BR>";
		return 0;
		}

	// CHECK FILE FORMAT
// first line should look like this
// setupInfo : version date
	$ifh = fopen( MAIN_DIR . '/' . SETUP_FILE, 'rb');
	$firstLine = fgets($ifh, 1024);
	fclose($ifh);
	$firstLine = trim($firstLine);
	if( preg_match('/^setupInfo\s*:\s*([\d\.]+)\s*(.+)$/', $firstLine, $matches) ){
		// OK
		}
	else {
		echo "Wrong format of the installation file!<BR>";
		return 0;
		}		

	// CHECK LD2 DIR
	if(! is_dir(LD_DIR) ){
		echo "I found no <B>" . LD_DIR . "</B> directory! It should be created in the same directory where this setup file is. Please create it and try again.<BR>";
		return 0;
		}

	if( !( is_readable(LD_DIR) && is_writable(LD_DIR) ) ){
		echo "I see I do not have enough privileges to write to <B>" . LD_DIR . "</B> directory!<BR>";
		echo "I should be able to write in it. Please chmod it to 777 (you can make it thru your general ftp client).";
		return 0;
		}
	
	return 1;
	}

function init(){
	// GET SETUP FILE INFO
	$ifh = fopen( MAIN_DIR . '/' . SETUP_FILE, 'rb');
	$firstLine = fgets($ifh, 1024);
	fclose($ifh);

	$firstLine = trim($firstLine);
	if( preg_match('/^setupInfo\s*:\s*([\d\.]+\s+)\s*(.+)$/', $firstLine, $matches) )
		$versionString = $matches[1] . ' (' . $matches[2] . ')';
	else
		$versionString = 'N/A';
	define( 'SETUP_VERSION', $versionString );

	// GET CURRENT INSTALLATION INFO
	if( file_exists(VERSION_FILE) ){
		$versionContent = file(VERSION_FILE);
		$currentVersion = $versionContent[0];
		}
	else
		$currentVersion = 'N/A';
	define( 'CURRENT_VERSION', $currentVersion );

	// DEFINE MAJOR VERSIONS
	if( SETUP_VERSION != 'N/A' ){
		preg_match( "/^(\d\.\d+)/", SETUP_VERSION, $matches );
		define( 'SETUP_VERSION_MAJOR', $matches[1] );
		}
	else
		define( 'SETUP_VERSION_MAJOR', 'N/A' );

	if( CURRENT_VERSION != 'N/A' ){
		preg_match( "/^(\d\.\d)/", CURRENT_VERSION, $matches );
		define( 'CURRENT_VERSION_MAJOR', $matches[1] );
		}
	else
		define( 'CURRENT_VERSION_MAJOR', 'N/A' );
	}

function step_start() {
//welcome screen
	echo "Please select an installation option above.";
	echo "<P>If you are installing OurWebFTP from scratch, you will need perform all the steps from left to right.";
	echo "To upgrade your current installation you will only need to select <B>Install Application Files</B> option.";

	echo '<P><B>Setup file details:</B>';
	echo '<BR>===================<BR>';
	echo 'Version: ' . SETUP_VERSION . '<BR>';

	echo '<P><B>Current installation details:</B>';
	echo '<BR>===================<BR>';
	echo 'Version: ' . CURRENT_VERSION . '<BR>';
	}

function step_core(){
	echo "<H3>Install Application Files</H3>";

	$ifh = fopen( MAIN_DIR . '/' . SETUP_FILE, 'rb');
	rewind($ifh);
	//first line is service one
	$line = fgets($ifh, 1024);

	$fileCount = process_setup_file( $ifh, FILE_TYPE_APP );

	echo "<BR>$fileCount files have been extracted. <BR><BR>";

	// NOW WRITE THE WEB DIR INFO FILE
	$webDirFileContent = 
		"<?\n" . 
		"define( 'APP_WEB_DIR',	'" . WEB_DIR	. "');\n" . 
		"?>";

	$webDirFile = LD_DIR . '/web_dir.php';
	if( ! ($file = @fopen( $webDirFile, 'w' )) )
		fatalError( "Can't open the web dir configuration file <I>$webDirFile</I>for writing!" );
	if( ! fwrite( $file, $webDirFileContent ) )
		fatalError( "Can't write to the web dir configuration file <I>$webDirFile</I>!" );

	fclose($ifh);
	}

function step_conf(){
	echo "<H3>Install Default Configuration Files</H3>";

	$ifh = fopen( MAIN_DIR . '/' . SETUP_FILE, 'rb');
	rewind($ifh);
	//first line is service one
	$line = fgets($ifh, 1024);

	$fileCount = process_setup_file( $ifh, FILE_TYPE_CONF );
	fclose($ifh);

	echo "<BR>$fileCount files have been extracted. <BR><BR>";
	}

function process_setup_file( &$sf_h, $type = 0 ){
	$fileCount = 0;
	$file = null;
	echo "<B>Writing files:</B><BR>";

	while( ! feof($sf_h) ){
//		$line = trim(fgets($sf_h, 1024));
		$line = rtrim(fgets($sf_h, 1024));
		if( strlen($line) > 0 ){
			if( preg_match("/startFile\s*:\s*([^\s]+)\s*:\s*(\d)$/", $line, $matches) ){
				// NEW FILE
				// CLOSE OLD
				if( $file ){
					fclose($file);
					@chmod($fullFileName, 0666);
					$file = null;
					$fileCount++;
					}

				// START NEW
				$fileName = trim( $matches[1] );
				$fileType = (int)trim( $matches[2] );

				if( ($fileType != FILE_TYPE_SKIP) && ($type == $fileType) ){
					echo "$fileName<BR>";
					$fullFileName = MAIN_DIR . '/' . $fileName;
					makeDir( dirname($fullFileName) );
					if( ! ($file = fopen($fullFileName, 'w')) )
						fatalError( "Can't open file '$fullFileName' for writing<BR>" );
					}

				// copy the new non-application files if any appeared
				if( ($type != $fileType) && ($type == FILE_TYPE_APP) ){
					$fullFileName = MAIN_DIR . '/' . $fileName;
					if( ! file_exists($fullFileName) ){
						echo "$fileName<BR>";
						makeDir( dirname($fullFileName) );
						if( ! ($file = fopen($fullFileName, 'w')) )
							fatalError( "Can't open file '$fullFileName' for writing<BR>" );
						}
					}
				}
			else {
				// EXISTING FILE
				if( $file ){
					if( ftell($file) > 0 )
						fwrite($file, "\n");
					fwrite($file, $line);
					}
				}
			}
		else {
			// WRITE EMPTY LINE
			if( $file )
				fwrite($file, "\n");
			}
		}
	if( $file ){
		fclose($file);
		@chmod($fullFileName, 0666);
		}

	return $fileCount;
	}

function fatalError( $msg ){
	echo $msg;
	exit;
	}

function makeDir( $dir ){
//echo "making '$dir'<BR>";
	if( file_exists($dir) ){
// 		echo "Notice<BR>Directory<BR><B>$dir</B> already exists.";
 		return 1;
 		}

	if( ! file_exists(dirname($dir)) )
		makeDir( dirname($dir) );

	if( ! mkdir( $dir, 0777 ) ){
		fatalError("Cannot make directory<BR><B>$dir</B> with 0777 mode!");
		return 0;
		}
	return 1;
	}

function start_html(){ ?>
	<HTML><HEAD>
	<TITLE>OurWebFTP 5.3 Installation</TITLE>
	<STYLE TYPE=text/css>
	BODY {
		FONT-FAMILY: Arial, Helvetica;
		FONT-SIZE: 10pt;
		FONT-WEIGHT: normal;
		}
	TD {
		FONT-SIZE: 10pt;
		}
	TR.header {
		BACKGROUND-COLOR: #7DA7D9;
		FONT-WEIGHT: bold;
		}
	INPUT {
		BORDER-RIGHT: #555555 1px solid;
		BORDER-LEFT: #555555 1px solid;
		BORDER-TOP: #555555 1px solid;
		BORDER-BOTTOM: #555555 1px solid;
		CURSOR: auto;
		FONT-FAMILY: Tahoma, Verdana, Arial, Helvetica;
		FONT-WEIGHT: bold;
		FONT-SIZE: 8pt;
		}
	</STYLE>
	</HEAD>
	<BODY BGCOLOR="#BFD2E7">
	<CENTER>
	<H3>OurWebFTP 5.3 Installation</H3>
	<FORM NAME=my_form METHOD=POST ENCTYPE="multipart/form-data">
	<TABLE WIDTH="650"><TR><TD>
<?
	}

function end_html(){ ?>
	</TD></TR></TABLE></FORM>&copy <A HREF="http://www.hitcode.com/"><B><FONT COLOR="#000080">hit</FONT><FONT COLOR="#FF8000">code</FONT></B></A>, 2003-2005</CENTER></BODY></HTML>
<?	}
?>
Return current item: OurWebFTP