Location: PHPKode > projects > chillyCMS > chillyCMS/installation/install.include.php
<?php
//#################################################################################################
//	Installation helper functions
//#################################################################################################
//	languageform()
//#################################################################################################
defined('DOIT') or die('Restricted access');

//Step 0///////////////////////////////////////////////////////////////////////////////////////////
function step0() {
	global $l_inst,$l_gen,$installlang;

	//Read languages from available language files
	$languages=array();
	
	//Load all installation language files from language folder
	$handle = opendir('languages');
	if ($handle) {
		while (false !== ($file = readdir($handle))) {
			if ($file != '.' && $file != '..' && !is_dir($file)) {
				$fileending = substr($file, strlen($file) - 4);
				if ($fileending == '.php') {
					$file = substr($file, 0, -4);
					$languages[$file] = $file;
				}
			}	
		}
	}
	
	//If no language file was found insert standard file (en.php)
	if (empty($languages)) { $languages['en'] = 'en'; }
	
	//build form
	$f = new Form('','post',false,$l_gen['lbl_ok'],$l_gen['lbl_error'],false,true);
	$f->addSelect('installlang',$l_inst['lbl_lang'],$languages,false,$installlang,1,false,'myid3','myclass','mytitle',false);
	$f->addInput('hidden','step',false,'step0');
	$f->addButton('submit','submit',false,$l_inst['lbl_tostep'].'1 >>',false,false,'floatright');
	
	//no error handling needed here
	$f->getErrors();

	return $f->render('table');
}


//Step 1///////////////////////////////////////////////////////////////////////////////////////////
function step1(&$printform,&$msg) {
	global $l_inst,$l_gen,$installlang,$page;
	
	$doit = $configcreationfailed = $dbcreationfailed = false;

	//get permissions
	$installationpermission = intval(substr(decoct(fileperms('.')),2));
	$configpermission = intval(substr(decoct(fileperms('../config.php')),3));
	//TODO
	//echo "Permissions for installation folder: $installationpermission<br />";
	//echo "Permissions for config file: $configpermission<br />";

	//If one of the files is not writable
	//if ($configpermission<755 or $installationpermission<755) {
		//Try to change permissions
		//if (!set_permissions()) { $msg=array($l_inst["msg_permissions"],"bad"); }
	//}

	//TODO:remove standard values
	$f = new Form('','post',false,$l_inst['msg_db_ok'],$l_inst['msg_db_err'],false,true);
	$f->addInput('text','dbhost',$l_inst['lbl_dbhost'],'localhost',true,false,false,false,false,false,false,'left',false,array($l_inst["msg_dbhost_err"],'below'));
	$f->addInput('text','dbuser',$l_inst['lbl_dbuser'],'root',true,false,false,false,false,false,false,'left',false,array($l_inst["msg_dbuser_err"],'below'));
	$f->addInput('password','dbpw',$l_inst['lbl_dbpw'],'root',true,false,false,false,false,false,false,'left',false,array($l_inst["msg_dbpw_err"],'below'));
	$f->addInput('text','dbname',$l_inst['lbl_dbname'],'cms',true,false,false,false,false,false,false,'left',false,array($l_inst["msg_dbname_err"],'below'));

	$f->addInput('hidden','installlang',false,$installlang);
	$f->addInput('hidden','step',false,'step1');
	$f->addButton('submit','submit',false,$l_inst['lbl_tostep'].'2 >>',false,false,'floatright');

	//Back button
	$b = new Form('','post',false,false,false,true);
	$b->addInput('hidden','step',false,'step0');
	$b->addInput('hidden','backbutton',false,'backbutton');
	$b->addButton('submit','submit',false,$l_gen['lbl_back']);

	//error handling
	if (isset($_POST['step']) && $_POST['step']=='step1' && !isset($_POST['backbutton'])) {
		$f->getErrors();
		$msg = $f->getMsg();

		//get values to compare
		$myvalues = $f->getCleanValues('html');

		$link = @mysql_connect($myvalues['dbhost'], $myvalues['dbuser'], $myvalues['dbpw']);
		if (!$link) {
			$f->setError('dbhost');
			$f->setError('dbuser');
			$f->setError('dbpw');
		} else {
			if (!@mysql_select_db($myvalues['dbname'], $link)) {
				$f->setError('dbname');
			} else {
				if (!create_configfile()) {
					$msg = array($l_inst['msg_cfg_err'],'bad');
					$configcreationfailed = true;
				} else {
					$page = new Database($myvalues['dbhost'], $myvalues['dbuser'], $myvalues['dbpw'], $myvalues['dbname']);
					if (!exec_sql('database.sql')) {
						$msg = array($l_inst['msg_db_err'],'bad');
						$dbcreationfailed = true;
					}
				}
			}
		}
		//repair page variable as it has been destroyed calling exec_sql
		$page = new Installpage();
		
		//if there were no errors proceed with whatever
		if (is_array($f->errors) && sizeof($f->errors)<1 && !$dbcreationfailed && !$configcreationfailed) { $doit = true; } else { $doit = false; }
	}

	$printform = $f->render('table').$b->render('plain');
	
	//do it :)
	if ($doit) {
		return true;
	} else {
		return false;
	}
}

//Step 2///////////////////////////////////////////////////////////////////////////////////////////
function step2(&$printform,&$msg) {
	global $l_inst,$l_gen,$installlang,$page;
	
	$doit = false;
	
	//TODO: hints
	$f = new Form("",'post',false,$l_inst['msg_admin_ok'],$l_inst['msg_admin_err'],false,true);
	$f->addInput('text','adminuser',$l_inst['lbl_uname'],'admin',true,array('/^[0-9a-zA-Z]*$/','Please use only numbers and letters'),false,false,false,false,false,'left',false,array($l_inst["msg_auser_hint"],'below'));
	$f->addInput('text','adminname',$l_inst['lbl_rname'],'Administrator',true,false,false,false,false,false,false,'left',false,array($l_inst["msg_aname_hint"],'below'));
	$f->addInput('password','adminpw',$l_inst['lbl_pw'],'demo',true,false,false,false,false,false,false,'left',false,array($l_inst["msg_apw_hint"],'below'));
	$f->addInput('text','adminemail',$l_inst['lbl_email'],'hide@address.com',true,false,false,false,false,false,false,'left',false,array($l_inst["msg_amail_hint"],'below'));

	$f->addInput('hidden','installlang',false,$installlang);
	$f->addInput('hidden','step',false,'step2');
	$f->addButton('submit','submit',false,$l_inst['lbl_tostep'].'3 >>',false,false,'floatright');
	
	//Back button
	$b = new Form("",'post',false,false,false,true);
	$b->addInput('hidden','step',false,'step1');
	$b->addInput('hidden','backbutton',false,'backbutton');
	$b->addButton('submit','submit',false,$l_gen['lbl_back']);
	
	//error handling
	if (isset($_POST['step']) && $_POST['step']=='step2' && !isset($_POST['backbutton'])) {
		$f->getErrors();
		$msg = $f->getMsg();

		//get values to compare
		$myvalues = $f->getCleanValues('html');

		//additional error handling
		$adminpw = hash("sha512", $myvalues["adminpw"]);
		$adminemail = email_chars($myvalues["adminemail"]);
		if (!$adminemail) { $f->setError('adminemail'); }
		
		//if there were no errors proceed with whatever
		if (is_array($f->errors) && sizeof($f->errors)<1) { $doit = true; } else { $doit = false; }
	}

	$printform = $f->render('table').$b->render('plain');

	
	//do it :)
	if ($doit) {
		//insert admin into database
		$mydbvalues = $f->getCleanValues('db');
		
		$sql = "update system_users set `user`='".$mydbvalues['adminuser']."', `name`='".$mydbvalues['adminname'].
		"', `pw`='$adminpw', `email`='".$mydbvalues['adminemail']."', `language`='$installlang' where `uid`=1";

		if ($page->query($sql)) {
			$return = true;
		} else {
			$return = false;
		}

		if (!$return) { $msg = array($l_inst["msg_admin_err"],"bad"); }
		return $return;
	} else {
		return false;
	}
}

//Step 3///////////////////////////////////////////////////////////////////////////////////////////
function step3(&$printform,&$msg) {
	global $l_inst,$l_gen,$installlang,$page;
	
	$doit = false;
	
	//Read languages from available language files
	$languages=array();
	
	//Load all installation language files from language folder
	$handle = opendir(PATH.'/languages');
	if ($handle) {
		while (false !== ($file = readdir($handle))) {
			if ($file != "." && $file != ".." && !is_dir($file)) {
				$fileending = substr($file, strlen($file) - 4);
				if ($fileending == ".php") {
					$file = substr($file, 0, -4);
					$languages[$file] = $file;
				}
			}	
		}
	}
	
	//If no language file was found insert standard file (en.php)
	if (empty($languages)) { $languages['en'] = 'en'; }
	
	//TODO: hints
	$f = new Form("",'post',false,$l_gen['lbl_ok'],$l_gen['lbl_error'],false,true);
	$f->addSelect('sitelang',$l_inst['lbl_lang'],$languages,false,$installlang);
	$f->addInput('text','sitename',$l_inst['lbl_sitename'],$l_inst["lbl_sitenamest"],true,false,false,false,false,false,false,'left',false,array('Please enter a sitename','below'));
	$f->addInput('text','siteauthor',$l_inst['lbl_author'],$l_inst["lbl_authorst"],true,false,false,false,false,false,false,'left',false,array('Please enter an author','below'));
	$f->addInput('text','siteemail',$l_inst['lbl_email'],$l_inst["lbl_emailst"],true,false,false,false,false,false,false,'left',false,array('Please enter a valid email address','below'));
	$f->addInput('text','sitekeywords',$l_inst['lbl_keywords'],$l_inst["lbl_keywordsst"],false,false,false,false,false,false,false,'left');
	$f->addInput('text','sitedesc',$l_inst['lbl_desc'],$l_inst["lbl_descst"],false,false,false,false,false,false,false,'left');
	$f->addChoice('checkbox','sitedemo','Install demo content?',array('sitedemo'=>''),false,false,false,'button floatleft');
	
	$f->addInput('hidden','installlang',false,$installlang);
	$f->addInput('hidden','step',false,'step3');
	$f->addButton('submit','submit',false,$l_inst['lbl_finish'].'!',false,false,'floatright');
	
	//Back button
	$b = new Form("",'post',false,false,false,true);
	$b->addInput('hidden','step',false,'step2');
	$b->addInput('hidden','backbutton',false,'backbutton');
	$b->addButton('submit','submit',false,$l_gen['lbl_back']);
	
	//error handling
	if (isset($_POST['step']) && $_POST['step']=='step3' && !isset($_POST['backbutton'])) {
		$f->getErrors();
		$msg = $f->getMsg();

		//get values to compare
		$myvalues = $f->getCleanValues('html');

		//additional error handling
		if (strlen($myvalues['sitelang'])<1 or strlen($myvalues['sitelang'])>3) { $f->setError('sitelang');  }
		$siteemail = email_chars($_POST["siteemail"]);
		if (!$siteemail) { $f->setError('siteemail'); }
		
		//if there were no errors proceed with whatever
		if (is_array($f->errors) && sizeof($f->errors)<1) { $doit = true; } else { $doit = false; }
	}
	
	$printform = $f->render('table').$b->render('plain');
	
	//do it :)
	if ($doit) {
	
		$error=false;
		$mydbvalues = $f->getCleanValues('db');

		//insert settings
		$sql="update site_settings set `value`='".$mydbvalues['sitelang']."' where `key`='language'";
		if (!$page->query($sql)) { $error=true; }
		$sql="update site_settings set `value`='".$mydbvalues['sitename']."' where `key`='sitename'";
		if (!$page->query($sql)) { $error=true; }
		$sql="update site_settings set `value`='".$mydbvalues['siteauthor']."' where `key`='author'";
		if (!$page->query($sql)) { $error=true; }
		$sql="update site_settings set `value`='".$mydbvalues['siteemail']."' where `key`='siteemail'";
		if (!$page->query($sql)) { $error=true; }
		$sql="update site_settings set `value`='blue' where `key`='backendstyle'";
		if (!$page->query($sql)) { $error=true; }
		$sql="update site_settings set `value`='blue' where `key`='template'";
		if (!$page->query($sql)) { $error=true; }
		$sql="update site_settings set `value`='".$mydbvalues['sitekeywords']."' where `key`='keywords'";
		if (!$page->query($sql)) { $error=true; }
		$sql="update site_settings set `value`='".$mydbvalues['sitedesc']."' where `key`='description'";
		if (!$page->query($sql)) { $error=true; }

		if (!$error) {
			$return = true;
			include("testdata.php");
		} else {
			$return = false;
			$msg = array($l_inst["msg_siteinfo_err"],"bad");
		}
		
		return $return;	
	}
}

//Complete/////////////////////////////////////////////////////////////////////////////////////////
function complete(&$printform,&$msg) {
	global $l_inst,$l_gen;
	
	$f = new Form("",'post',false,$l_gen['lbl_ok'],$l_gen['lbl_error'],false,true);
	$f->addButton('link',$l_inst['lbl_tobackend'],false,false,URL.'/admin/index.php','button','floatright');
	
	$b = new Form("",'post',false,false,false,true);
	$b->addInput('hidden','step',false,'step1');
	$b->addInput('hidden','backbutton',false,'backbutton');
	$b->addButton('submit','submit',false,$l_gen['lbl_back']);
	
	$msg = false;
	$printform = $f->render('table').$b->render('plain');
}

//Set Permissions for installation folder and config file//////////////////////////////////////////
function set_permissions() {
	$error=false;
	if (!@chmod("../config.php", 755)) { $error=true; }
	if (!chmod_r(dirname(__FILE__),777)) { $error=true;; }
	if (!$error) { return true; } else { return false; }
}

//Create Configfile////////////////////////////////////////////////////////////////////////////////
function create_configfile() {
	$dbhost=$_POST["dbhost"];
	$dbname=$_POST["dbname"];
	$dbuser=$_POST["dbuser"];
	$dbpw=$_POST["dbpw"];

	$config="defined('DOIT') or die('Restricted access');
define('DB_HOST', '".$dbhost."');
define('DB_USER', '".$dbuser."');
define('DB_PW', '".$dbpw."');
define('DB_DB', '".$dbname."');
?>";
	if (write_file('../config.php',"<?php\n".$config)) {
		return true;
	} else {
		return false;
	}
}
?>
Return current item: chillyCMS