head	1.1;
access;
symbols;
locks; strict;
comment	@# @;


1.1
date	99.03.14.18.51.36;	author chuck;	state Exp;
branches;
next	;


desc
@@


1.1
log
@cjh: i *think* that the cvs code should be in line with that tarball now,
along with some of my modifications/cleanup. let me know if i missed
anything...
@
text
@#!/usr/local/bin/php -q

<?
	/* Filename:	setup
	 * Author:		Darrell Brogdon (darrell@@brogdon.net)
	 * Created:		12/10/98 @@ 11:26pm
	 * Description:	A simple questions-based program to easily set up CartServ.
	 */

	system("clear"); //Clear the screen.


	// Read information in from STDIN.  Equivalent to 'read' in Bash.
	Function input() {
		$fp = fopen("/dev/stdin", "r");
		$line = ereg_replace("\n", "", fgets($fp, 255));
		fclose($fp);
		return $line;
	}

	// Massreplace
    Function massreplace($string1, $string2, $filename) { 
        $fd = fopen( $filename,  "r" ); 
        $contents = fread( $fd, filesize( $filename ) ); 
        $size = filesize( $filename ); 
        fclose( $fd ); 
        $massreplace = ereg_replace($string1, $string2, $contents); 
        $fd = fopen($filename,  "w"); 
        fputs($fd, $massreplace, $size); 
        fclose($fd); 
    } 

	// Check the existance of htpasswd
	if( !file_exists("/usr/local/bin/htpasswd") ) {
		$EXIST = 0;
	}

	// Set the location of the '.htpasswd' file.
	exec("pwd", $thisPath);
	$PSWDFPATH = $thisPath[0];

	// Get information from the user.
	exec("hostname", $hostname);
	print("Domain name [$hostname[0]]: ");
	$DOMAIN = input();

	if( $DOMAIN == "" ) {
		$DOMAIN = $hostname[0];
	}

	exec("whoami", $id);
	print("MySQL Username [$id[0]]: ");
	$USERNAME = input();

	if( $USERNAME == "" ) {
		$USERNAME = $id[0];
	}

	print("MySQL Password: ");
	$PASSWORD = input();

	if( $PASSWORD == "" ) {
		print("Error:  Password cannot be blank!\n");
		print("MySQL Password: ");
		$PASSWORD = input();
	}

	print("Database name [cartserv]: ");
	$DBNAME = input();

	if( $DBNAME == "" ) {
		$DBNAME = "cartserv";
	}

	print("Do you have a database named '$DBNAME' created? [Y/n]: ");
	$DBINST = input();
	
	if( $DBINST == "" ) {
		$DBINST = "y";
	} else {
		print("You should use the mysqladmin utility to create a database before using CartServ or you will encounter errors.\n");
	}

	// Create the .htaccess file.
	$fp = fopen("$PSWDFPATH/backroom/.htaccess", "w");
	fputs($fp, "AuthUserFile $PSWDFPATH/priv/.htpasswd\n");
	fputs($fp, "AuthGroupFile /dev/null\n");
	fputs($fp, "AuthName \"CartServ Backroom Processing\"\n");
	fputs($fp, "AuthType Basic\n\n");
	fputs($fp, "<Limit GET>\n");
	fputs($fp, "require user $USERNAME\n");
	fputs($fp, "</Limit>\n");
	fclose( $fp );

	// Create the .htpasswd file.
	$fp = fopen("./priv/.htpasswd", "a");
	fputs($fp, "$USERNAME:" . crypt($PASSWORD));
	fclose( $fp );

	// Update 'db.inc' with username and password.
	massreplace("USER", $USERNAME, "backroom/db.inc");
	massreplace("PASS", $PASSWORD, "backroom/db.inc");
	massreplace("DATABASE", $DBNAME, "backroom/db.inc");
	massreplace("DATABASE", $DBNAME, "backroom/external_api.inc");
	massreplace("USER", $USERNAME, "backroom/.htaccess");
	massreplace("HTPATH", $PSWDFPATH, "backroom/.htaccess");
	// Install the database
	if( ($DBINST == "y") || ($DBINST == "Y") ) {
		exec("cat cartserv.sql | mysql -u $USERNAME -p$PASSWORD $DBNAME", $instArray);
	}

	print("\nSetup Complete!\n");
	
	if( $EXIST == 0 ) {
		print("To start using CartServ point your browser at 'http://$DOMAIN/backroom/'\n");
		print("Login: $USERNAME\n");
		print("Password: $PASSWORD\n\n");
	} else {
		print("Before using CartServ you should secure the Backroom by executing the following htpasswd command to ensure Backroom security:\n");
		print("htpasswd ./priv/.htpasswd $USERNAME\n\n");
	}
?>
@
