Location: PHPKode > scripts > AskPeople-Free PHP survey application > AskPeople_2_2_2/install.php
<?php

session_start();

require_once('includes/functions.inc.php');

function loadUsers($src) {
	$open = fopen($src,"r");
	if ($open) {
		// load file into a raw array
		$rawdata = file($src);

		fclose ($open);		
		$fields = explode("\t",$rawdata[1]); // line 1 is php opener line 2 is fieldnames
		
		for ($n=2;$n<count($rawdata)-1;$n++) { // last line is php closer
			// load each line of raw data into a var
			$newdata = ($rawdata[$n]);
			// clean up the line of data
			$newdata = stripslashes($newdata);
			// break line into field array
			$tempdata = explode ("\t",$newdata);
			
			foreach((array)$fields as $k => $f) {
				$thisrow[$f] = $tempdata[$k];
			}
			// add field array to article array
			$usersarray[$thisrow['user']] = $thisrow;
		}
		return ($usersarray);		
	} else {
		return false;
	}
}

$globalsettings['organisationName'] = "AskPeople";
$thispage = "Installer";

include('includes/header.php');

switch ($_REQUEST['action']) { // questionnaire to be saved

	case "SendRegistration" :

		// prepare user registration string
		$headers .= "From: hide@address.com\r\n";
		
		$to = "hide@address.com";
		$subject = '[ASKPEOPLE] New installation registration'; 
		$message = "New AskPeople installation created at {$_POST['url']}\r\nSUBSCRIBE: {$_POST['email']}";

		// mail registration to AskPeople registration address
		$sent = mail($to,$subject,$message,$headers,"-hide@address.com");		

		if ($sent) { // show confirmation, and reminder to delete install.php
			$outputform = "
			  <div style='padding: 20px;'>
				<h2>Registration sent</h2>
				<p style='margin: 5px 0 10px 0;'>
				  Thank you for registering. We'll let you know about any updates and important news.
				</p>
				<p style='margin: 5px 0 10px 0;'>
				  Remember, if you find AskPeople useful, please <strong><a href='http://www.askpeople.co.uk'>consider making a donation</a></strong> to support further development.
				</p>
				
				<p style='margin: 5px 0 10px 0; font-size: 2em;'>
				  <a href='{$_POST['url']}'>Log in now</a>
				</p>
			  </div>	
			";
		} else {
			$outputform = "
			  <div style='padding: 20px;'>
				<h2>Sorry, there was a problem sending your registration</h2>
				<p style='margin: 5px 0 10px 0;'>
				  Thanks for trying though - if you have time, please email 'SUBSCRIBE' to <a href='mailto:hide@address.com'>hide@address.com</a>.
				</p>
				<p style='margin: 5px 0 10px 0;'>
				  Remember, if you find AskPeople useful, please <strong><a href='http://www.askpeople.co.uk'>consider making a donation</a></strong> to support further development.
				</p>
			  </div>	
			";		
		}

	  break;

	case "Confirm" :

		// validate $install array: check format of email, username, base dir and orgname
		
		
		if (validEmail(stripslashes($_POST[install][email])) == FALSE) {
			$errorurl .= urlencode("Please enter a valid email address.<br />");
		}

		if (strlen($_POST[install][username])<1) {
			$errorurl .= urlencode("Please enter a username - you'll need it to log in.<br />");
		}

		if (strlen($_POST[install][pwd1])<4 || (stripslashes($_POST[install][pwd1]) != stripslashes($_POST[install][pwd2]))) {
			$errorurl .= urlencode("Please ensure your password is entered correctly, and is at least 4 characters<br />");
		}
		
		if ($errorurl) {
			header("Location: install.php?errormsg=".$errorurl);
		}

		// print_r($_POST['install']);

		// create relevant config files

		// CHMOD files & directories
		@chmod('surveys/',0777);
		@chmod('templates/',0777);
		@chmod('templates/images/',0777);
		@chmod('trash/',0777);
		@chmod('trash/surveys/',0777);
		@chmod('rss/',0777);

		// make some .htaccess files to secure the data directories
		$makeht1 = saveFile("surveys/.htaccess","deny from all");
		$makeht2 = saveFile("trash/surveys/.htaccess","deny from all");

		// update apconfig.php

		$cpath = "surveys/apconfig.php";
		$loaded = loadFile($cpath);
			
		$parse = preg_match("/\/\/startOfLabels(.*?)\/\/endOfLabels/si",$loaded,$match);		
		$labelschunk = $match[1];

		$outputconf .= "<?php\n\n//startOfSettings\n";
		$outputconf .= "\t\$globalsettings['organisationName'] = \"".stripslashes($_POST[install][orgname])."\";\n";
		$outputconf .= "\t\$globalsettings['logoutURL'] = \"".stripslashes($_POST[install][basedir])."\";\n";
		$outputconf .= "\t\$globalsettings['defaultTemplate'] = \"AskPeopleDefault.php\";\n";
		$outputconf .= "\t\$globalsettings['defaultQuestionType'] = \"smallbox\";\n";
		$outputconf .= "//endOfSettings\n\n//startOfLabels\n" . $labelschunk . "//endOfLabels\n\n";
		$outputconf .= "\n?>";
				
		$saved = saveFile($cpath,$outputconf);
		@chmod('surveys/apconfig.php',0666);

		// create the master user (access level=10, ID=1)

		$outputusers .= "<?php /*\nuser\tname\tID\tpassword\temail\tlevel\tblank\n";
		$outputusers .= strtolower(trim(stripslashes($_POST[install][username]))) . "\t" . trim(stripslashes($_POST[install][realname])) . "\t" . "1" . "\t" . trim(stripslashes($_POST[install][pwd1])) . "\t" . trim(stripslashes($_POST[install][email])) . "\t" . "10" . "\t\n";			
		$outputusers .= "*/ ?>";	
		
		$saved = saveFile('users.inc.php',$outputusers);
		@chmod('users.inc.php',0666);
				
		// show confirmation, including advice to delete install.php

		$outputform = "
		  <form action='install.php' method='post' id='installform' style='padding: 20px;'>
		   <h2>Installation complete</h2>
		    <p style='margin: 5px 0 10px 0;'>
		      Thank you. You've now finished installing AskPeople. <strong>Please delete the install.php file from your server.</strong>
		    </p>	
		  
   		   <h2>Register free</h2>
		    <p style='margin: 5px 0 10px 0;'>
		      We'd like to keep you up to date with new releases and important information about AskPeople. 
		    </p>
		    <p style='margin: 5px 0 10px 0;'>
			Clicking the button below will simply add <strong>&lt;".stripslashes($_POST[install][email])."&gt;</strong> to our very low-volume mailing list.
		   	We'll never abuse, sell or spam your email, and you can unsubscribe at any time by emailing 'UNSUBSCRIBE' to hide@address.com
		    </p>
		   <input type='submit' value='Send registration info' style='font-size: 1.5em;' /> or <a href='".stripslashes($_POST[install][basedir])."'>log in now</a>
		   <input type='hidden' name='email' value='{$_POST[install][email]}' />
		   <input type='hidden' name='url' value='{$_POST[install][basedir]}' />
		   <input type='hidden' name='action' value='SendRegistration' />
		  </form>
		  ";

	  break;


	default :

		// check if install has already been attempted, if so block re-installation
		$usertest = loadUsers('users.inc.php');
		//print_r($usertest);
		if (count($usertest)>0) { // users already defined, so block installation
			die("<div class='clearbox'><h2>Attempted re-installation blocked</h2><p>It looks like AskPeople has already been installed and at least partially configured in this directory. Re-installation is blocked for security purposes.</p><p>If you want to re-install the software, please download the latest version from <a href='http://www.askpeople.co.uk'>http://www.askpeople.co.uk</a> and install into a new directory. For the best security, make sure you delete install.php following installation.</p></div>");
		}

		// work out what base dir is
 		$protocol = ($_SERVER['HTTPS'] == "on") ? "https://" : "http://"; // if installed on https server
		$basedir = $protocol . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/";

		// show form

		if ($_REQUEST['errormsg']) {
			$error = "<div class='errormsg' style='font-weight:bold;color:red'><p>".stripslashes(urldecode($_REQUEST['errormsg']))."</p></div>";
			echo "<pre><!-- ";
			print_r($_POST);
			echo " --></pre>"; // for debugging installer issues
		}

		$outputform = "
		  <form action='install.php' method='post' id='installform' style='padding: 20px;'>
		   {$error}
		   <h2>About your installation of AskPeople</h2>
		    <p style='margin: 5px 0 10px 0;'><label for='orgname'>What you'd like to call this installation: <em>(maybe your name or your organisation's name?)</em></label><br />
		     <input type='text' id='orgname' name=\"install[orgname]\" style='font-size: 1.3em;' size='50' /></p>

		    <p style='margin: 5px 0 10px 0;'><label for='basedir'>Confirm the base directory for this installation:</label><br />
		     <input type='text' id='basedir' name=\"install[basedir]\" style='font-size: 1.3em;' size='50' value=\"{$basedir}\" /></p>

		   <h2>Set up your master login:</h2>
		    <p style='margin: 5px 0 10px 0;'><label for='realname'>Your name:</label><br />
		     <input type='text' id='realname' name=\"install[realname]\" style='font-size: 1.3em;' size='40' /></p>

		    <p style='margin: 5px 0 10px 0;'><label for='email'>Your email:</label><br />
		     <input type='text' id='email' name=\"install[email]\" style='font-size: 1.3em;' size='40' /></p>

		    <p style='margin: 5px 0 10px 0;'><label for='username'>Your preferred username:</label><br />
		     <input type='text' id='username' name=\"install[username]\" style='font-size: 1.3em;' size='20' /></p>

		    <p style='margin: 5px 0 20px 0;'><label for='password1'>Your password:</label><br />
		     <input type='password' id='password1' name=\"install[pwd1]\" style='font-size: 1.3em;' size='20' /> <label for='password2'>and again to check: <input type='password' id='password2' name=\"install[pwd2]\" style='font-size: 1.3em;' size='20' /></label></p>

		   <input type='submit' value='Install AskPeople Now' style='font-size: 1.5em;' />
		   <input type='hidden' name='action' value='Confirm' />
		  </form>
		";

		if ($_REQUEST['errormsg']) {
			echo "<pre><!-- ";
			print_r($_POST);
			echo " --></pre>"; // for debugging installer issues
		}

	  break;

}

// output the HTML
echo ($outputform);

include('includes/footer.php');

?>
Return current item: AskPeople-Free PHP survey application