Install Error
			Shimmie needs to be run via a web server with PHP support -- you
			appear to be either opening the file from your hard disk, or your
			web server is mis-configured.
			
If you've installed a web server on your desktop PC, you probably
			want to visit the local web server.
			
For more help with installation, visit
			the
			documentation wiki.
		
 
		
			
Error\nPHP's GD extension seems to be missing, ".
		      "and imagemagick's \"convert\" command cannot be found - ".
			  "no thumbnailing engines are available.";
	}
	else {
		$gd = "";
	}
	print <<
			Shimmie Installer
			$gd
			Install
			
			Help
					
			Databases should be specified like so:
			
ie: protocol://username:password@host/database?options
			
eg: mysql://shimmie:pw123@localhost/shimmie?persist
			
			
For more help with installation, visit
			the
			documentation wiki.
		
 
EOD;
} // }}}
function install_process() { // {{{
	if(!isset($_POST['database_dsn']) || !isset($_POST["admin_name"]) || !isset($_POST["admin_pass"])) {
		die("Install is missing some paramaters (database_dsn, admin_name, or admin_pass)");
	}
	else if(strlen($_POST["admin_name"]) < 1 || strlen($_POST["admin_pass"]) < 1) {
		die("Admin name and password must be at least one character each");
	}
	else {
		$database_dsn = $_POST['database_dsn'];
		$admin_name = $_POST["admin_name"];
		$admin_pass = $_POST["admin_pass"];
	}
	set_admin_cookie($admin_name, $admin_pass);
	create_tables($database_dsn);
	insert_defaults($database_dsn, $admin_name, $admin_pass);
	build_dirs();
	write_config($database_dsn);
	
	header("Location: index.php?q=setup");
} // }}}
function set_admin_cookie($admin_name, $admin_pass) { // {{{
	$addr = $_SERVER['REMOTE_ADDR'];
	$addr = inet_ntop(inet_pton($addr) & inet_pton("255.255.0.0"));
	$hash = md5(strtolower($admin_name) . $admin_pass);
	setcookie("shm_user", $admin_name, time()+60*60*24*365);
	setcookie("shm_session", md5($hash.$addr), time()+60*60*24*7, "/");
} // }}}
function create_tables($dsn) { // {{{
	$db = NewADOConnection($dsn);
	if(!$db) {
		die("Couldn't connect to \"$dsn\"");
	}
	else {
		if(substr($dsn, 0, 5) == "mysql") {
			$db->Execute("SET NAMES utf8");
		}
		$schema = new adoSchema($db);
		$sql = $schema->ParseSchema("ext/upgrade/schema.xml");
		$result = $schema->ExecuteSchema();
		if(!$result) {
			die("Error creating tables from XML schema");
		}
	}
	$db->Close();
} // }}}
function insert_defaults($dsn, $admin_name, $admin_pass) { // {{{
	$db = NewADOConnection($dsn);
	if(!$db) {
		die("Couldn't connect to \"$dsn\"");
	}
	else {
		$config_insert = $db->Prepare("INSERT INTO config(name, value) VALUES(?, ?)");
		$user_insert = $db->Prepare("INSERT INTO users(name, pass, joindate, admin) VALUES(?, ?, now(), ?)");
		$admin_pass = md5(strtolower($admin_name).$admin_pass);
		$db->Execute($user_insert, Array('Anonymous', null, 'N'));
		$db->Execute($config_insert, Array('anon_id', $db->Insert_ID()));
		$db->Execute($user_insert, Array($admin_name, $admin_pass, 'Y'));
		if(check_im_version() > 0) {
			$db->Execute($config_insert, Array('thumb_engine', 'convert'));
		}
		$db->Close();
	}
} // }}}
function build_dirs() { // {{{
	if(!file_exists("images")) @mkdir("images"); // *try* and make default dirs. Ignore any errors -- 
	if(!file_exists("thumbs")) @mkdir("thumbs"); // if something is amiss, we'll tell the user later
	if(!file_exists("data")) @mkdir("data");
	if(
			((!file_exists("images") || !file_exists("thumbs") || !file_exists("data")) && !is_writable("./")) ||
			(!is_writable("images") || !is_writable("thumbs") || !is_writable("data"))
	) {
		print "Shimmie needs three folders in it's directory, 'images', 'thumbs', and 'data',
		       and they need to be writable by the PHP user (if you see this error,
			   if probably means the folders are owned by you, and they need to be
			   writable by the web server).
			   
			   Once you have created these folders, hit 'refresh' to continue.";
		exit;
	}
	else {
		assert(file_exists("images") && is_writable("images"));
		assert(file_exists("thumbs") && is_writable("thumbs"));
		assert(file_exists("data") && is_writable("data"));
		if(!file_exists("images/ff")) {
			for($i=0; $i<256; $i++) {
				mkdir(sprintf("images/%02x", $i));
				mkdir(sprintf("thumbs/%02x", $i));
			}
		}
	}
} // }}}
function write_config($dsn) { // {{{
	$file_content = "";
	
	if(is_writable("./") && installer_write_file("config.php", $file_content)) {
		assert(file_exists("config.php"));
	}
	else {
		$h_file_content = htmlentities($file_content);
		print <<
						
		One done, Continue
EOD;
		exit;
	}
} // }}}
?>