Location: PHPKode > projects > Internet Media Manager > imm-0.3/configure.php
#!/usr/bin/php
<?php


if ( !function_exists('file_put_contents') ){
	function file_put_contents($path, $content){
		$fp = fopen($path, 'w');
		fwrite($fp, $content);
		fclose($fp);
	}
}

if ( !@$argv ) die("This can only be run from the command line");

$conf = parse_ini_file(dirname(__FILE__).'/conf.ini', true);




$db = null;
while ( !$db ){

	
	$host = null;
	while ( !$host ){
		fwrite(STDOUT, "MySQL Host [".$conf['_database']['host']."]:");
		$host = trim(fgets(STDIN));
		if ( !$host ) $host = $conf['_database']['host'];
	}
	
	$user = null;
	while ( !$user ){
		fwrite(STDOUT, "MySQL Username [".$conf['_database']['user']."]:");
		$user = trim(fgets(STDIN));
		if ( !$user ) $user = $conf['_database']['user'];
	}
	
	
	$password = null;
	while ( !$password ){
		fwrite(STDOUT, "MySQL Password: [*****]:");
		$password = preg_replace('/\r?\n$/', '', `stty -echo; head -n1`);
		echo "\n";
		if ( !$password ) $password = $conf['_database']['password'];
		`stty echo`;
	}
	







	$db = @mysql_connect($host, $user, $password);
	if ( !$db ){
		fwrite(STDOUT, "Failed to connect to mysql: ".mysql_error());
		$conf['_database']['host'] = $host;
		$conf['_database']['user'] = $user;
		$conf['_database']['password'] = $password;
		$user = $host = $password = null;
	}
	
	
	
}

fwrite(STDOUT, "Connected to host ".$host." as user ".$user."\n");

$res = null;
while ( !$res ){
	$dbname = null;
	while (!$dbname ){
		fwrite(STDOUT, "MySQL Database Name [".$conf['_database']['name']."]:");
		$dbname = trim(fgets(STDIN));
		if ( !$dbname ){
			$dbname = $conf['_database']['name'];
		}
	}
	
	$res = mysql_select_db($dbname, $db);
	if ( !$res ){
		$conf['_database']['name'] = $dbname;
		$dbname = null;
	}
}

fwrite(STDOUT, "Selected database ".$dbname."\n");

$mysql = null;
while ( !$mysql ){
	fwrite(STDOUT, "Path to mysql [".trim(`which mysql`)."]:");
	$mysql = trim(fgets(STDIN));
	if ( !$mysql ) $mysql = trim(`which mysql`);
}

$command = "$mysql -h ".escapeshellarg($host)." -u ".escapeshellarg($user)." -p".escapeshellarg($password)." ".escapeshellarg($dbname)." < install/install.sql";
$res = exec($command);
//if ( !$res ){
//	echo "Failed to create tables in database $dbname.  Please check your connection information and try again.\n";
//	exit;
//}

fwrite(STDOUT, "Tables for MMI successfully created in database $dbname\n");



/*
// Now let's set up the S3 configuration
$use_s3 = '';
while ( !preg_match('/^[ynYN]/', $use_s3) ){
	fwrite(STDOUT, "Do you wish to use Amazon's Simple Storage Service (S3)? [n]:");
	$use_s3 = trim(fgets(STDIN));
	if ( !$use_s3 ) $use_s3 = 'n';
}

if ( preg_match('/^ynYN/', $use_s3) ){
	$bucket = null;
	while ( !$bucket ){
		fwrite(STDOUT,"Enter bucket name where files should be stored [".@$conf['s3']['bucket']."]:");
		$bucket = trim(fgets(STDIN));
		if ( !$bucket ) $bucket = @$conf['s3']['bucket'];
	}	
	
	
	$access_key = null;
	while ( !$access_key ){
		fwrite(STDOUT, "Enter your S3 Access Key [".@$conf['s3']['access_key']."]:");
		$access_key = trim(fgets(STDIN));
		if ( !$access_key ) $access_key = @$conf['s3']['access_key'];
	}
	
	$secret_key = null;
	while (!#
}

*/


fwrite(STDOUT, "Writing conf.ini file ....");
$confini_path = dirname(__FILE__).'/conf.ini';
$content = file_get_contents($confini_path);
$content = preg_replace('/^\W*host=.*$/m', 'host="'.$host.'"', $content);
$content = preg_replace('/^\W*user=.*$/m', 'user="'.$user.'"', $content);
$content = preg_replace('/^\W*password=.*/m', 'password="'.$password.'"', $content);
$content = preg_replace('/^\W*name=.*/m', 'name="'.$dbname.'"', $content);
file_put_contents($confini_path, $content);
fwrite(STDOUT, "Complete!\n");


fwrite(STDOUT, "We need to know the URL path to your IMM installation.  E.g. if your IMM installation is accessible at http://yourdomain.com/path/to/imm, then the URL path is /path/to/imm.\n");
$imm_path = null;
while (!$imm_path){
	fwrite(STDOUT, "IMM URL Path:");
	$imm_path = trim(fgets(STDIN));
}

fwrite(STDOUT, "Writing files/.htaccess file ....");

if ( !$imm_path or $imm_path{strlen($imm_path)-1} != '/' ) $imm_path .= '/';
$files_url_path = $imm_path.'files';
$htaccess_path = dirname(__FILE__).'/files/.htaccess';
$content = file_get_contents($htaccess_path);
$content = preg_replace('/^RewriteBase .*$/m', 'RewriteBase '.$files_url_path, $content);
file_put_contents($htaccess_path, $content);

fwrite(STDOUT, "Complete!\n");


$photos_path = dirname(__FILE__).'/files/photos';
$cache_path = dirname(__FILE__).'/files/cache';
$templates_c_path = dirname(__FILE__).'/templates_c';
$import_path = dirname(__FILE__).'/import';
fwrite(STDOUT, "chmod 0777 ".$templates_c_path." ....");
chmod($templates_c_path, 0777);
fwrite(STDOUT, "Complete!\n");

fwrite(STDOUT, "chmod 0777 ".$cache_path." ....");
chmod($cache_path, 0777);
fwrite(STDOUT, "Complete!\n");

fwrite(STDOUT, "chmod 0777 ".$photos_path." ....");
chmod($photos_path, 0777);
fwrite(STDOUT, "Complete!\n");

fwrite(STDOUT, "chmod 0777 ".$import_path." ....");
chmod($import_path, 0777);
fwrite(STDOUT, "Complete!\n");


$cron = '';
while ( !preg_match('/^[ynYN]/', $cron) ){
	fwrite(STDOUT, "Do you wish to set up a cron script to periodically upload large files to Amazon's simple storage service (S3)? [y]:");
	$cron = trim(fgets(STDIN));
}

if ( preg_match('/^[ynYN]/', $cron) ){
	$php = null;
	fwrite(STDOUT, "Path to PHP [/usr/bin/php]:");
	$php = trim(fgets(STDIN));
	if ( !$php ) $php = '/usr/bin/php';

	$command = "0,5,10,15,20,25,30,35,40,45,50,55 * * * * $php ".dirname(__FILE__).'/cron.php upload';
	
	fwrite(STDOUT, "Removing old cron jobs ....\n");
	$tempfile = tempnam(dirname(__FILE__), 'oldcrontab');
	exec('crontab -l  > '.escapeshellarg($tempfile));
	$content = file_get_contents($tempfile);
	if ( !preg_match('/^'.preg_quote($command, '/').'$/m', $content) ){
	 	$content .= $command."\n";
	 }
	//echo $content;exit;
	file_put_contents($tempfile, $content);
	//exec('"'.escapeshellarg($command).'" >> '.escapeshellarg($tempfile));
	exec('crontab '.escapeshellarg($tempfile));
	fwrite(STDOUT, "Complete!\n");
}

fwrite(STDOUT, "Installation Complete!!!\n");



	
	






Return current item: Internet Media Manager