<?php
session_start();
ob_start(); //required for plugins/filters and PDF output
require_once '../cfg/param.php'; // Config file
//Language in mc_settings
if($_SESSION['lang']=="")
{
$_SESSION['lang']="en";
}
if($_GET['lang']!=""){$_SESSION['lang']=$_GET['lang'];}
$lang=$_SESSION['lang'];
include '../locales/'.$lang.'/locale.'.$lang.".php"; // exemple "locales/fr/locale.fr with fr as $lang variable
function print_connect_form()
{
global $locales, $cfg_host, $cfg_user, $cfg_password, $cfg_database;
echo '<form method="post" action="index.php?step=1b"><table><tr>
<td>'.$locales["Connect_host"].'</td><td><input type="text" name="dbhost" value="';
if($_POST["dbhost"]!="") echo $_POST["dbhost"]; else echo $cfg_host;
echo '"/></td></tr>
<td>'.$locales["Connect_user"].'</td><td><input type="text" name="dbuser" value="';
if($_POST["dbuser"]!="") echo $_POST["dbuser"]; else echo $cfg_user;
echo '"/></td></tr>
<td>'.$locales["Connect_pass"].'</td><td><input type="password" name="dbpassword" value="';
if($_POST["dbpassword"]!="") echo $_POST["dbpassword"]; else echo $cfg_password;
echo '"/></td></tr>
<td>'.$locales["Connect_db"].'</td><td><input type="text" name="dbbase" value="';
if($_POST["dbbase"]!="") echo $_POST["dbbase"]; else echo $cfg_database;
echo '"/></td></tr>
<td></td><td><input type="submit" name="dbsubmit" value="'.$locales["Next_step"].'"/></td></tr>';
}
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
?>
<head>
<title><?php echo $locales["Installation"]; if($_GET["step"]!="") echo " - ".$locales["Installation_step"]." : ".$_GET["step"];?></title>
</head>
<body>
<?php
echo "<h1>".$locales["Installation"]."</h1>\n";
if($_GET["step"]!="") echo "<h2>".$locales["Installation_step"]." : ".$_GET["step"]."</h2>\n";
//Language
echo '<form method="get" name="language_select" action="index.php">';
$get_keys = array_keys($_GET);
for($i=0;$i<sizeOf($get_keys);$i++)
{
if($get_keys[$i] != "lang")
echo "<input type=\"hidden\" name=\"".$get_keys[$i]."\" value=\"".$_GET[$get_keys[$i]]."\"/>";
}
echo '<select name="lang" label="'.$locales["Language"].'">';
if($fp = fopen("../locales/lc.locales","r"))
{
while (!feof($fp))
{
$ligne = fgets($fp, 4096);
if (preg_match("/^(.+):(.+)$/", $ligne, $regs)) {
echo "<option value=\"".$regs[1]."\"";
if($regs[1]==$lang) echo " selected=\"selected\"";
echo ">".$regs[2]."</option>";
}
}
fclose($fp);
}
echo '</optgroup></select><input type="submit" value="OK" style="display:none;"/></form>';
switch($_GET["step"])
{
case "1b":
if(($_POST["dbhost"]!="")&&($_POST["dbuser"]!="")&&($_POST["dbpassword"]!="")&&($_POST["dbbase"]!=""))
{
//Testing database connection
$DB_connexion = mysql_connect($_POST["dbhost"], $_POST["dbuser"], $_POST["dbpassword"]);
if (! $DB_connexion){
//problem with the connection
echo '<p>'.$locales["Connexion_error"]."</p>\n";
print_connect_form();
}
else
if(!mysql_select_db($_POST["dbbase"])){
echo '<p>'.$locales["Connexion_error"]."</p>\n";
print_connect_form();
}
//else everything is OK, saving config, and go to step 3
else
{
if($param_file_open = fopen("../cfg/param.php","r"))
{
$contenu_param_file_open = fread($param_file_open, filesize("../cfg/param.php"));
fclose($param_file_open);
$contenu_param_file_open = preg_replace('/\$'."cfg_host(.*)=(.*);(.*)/",'\$cfg_host = "'.$_POST['dbhost']."\"; //Set during install", $contenu_param_file_open);
$contenu_param_file_open = preg_replace('/\$'."cfg_user(.*)=(.*);(.*)/", "\$cfg_user = \"".$_POST["dbuser"]."\"; //Set during install", $contenu_param_file_open);
$contenu_param_file_open = preg_replace('/\$'."cfg_password(.*)=(.*);(.*)/", "\$cfg_password = \"".$_POST["dbpassword"]."\"; //Set during install", $contenu_param_file_open);
$contenu_param_file_open = preg_replace('/\$'."cfg_database(.*)=(.*);(.*)/", "\$cfg_database = \"".$_POST["dbbase"]."\"; //Set during install", $contenu_param_file_open);
if($param_file_write = fopen("../cfg/param.php","w"))
{fwrite($param_file_write, $contenu_param_file_open); fclose($param_file_write);}
}
header("Location:index.php?step=2");
}
}
else
{
echo '<p>'.$locales["Fill_all_fields"]."</p>\n";
print_connect_form();
}
break;
case "1a":
//Testing database connection
$DB_connexion = mysql_connect($cgf_host, $cfg_user, $cfg_password);
if (! $DB_connexion){
//problem with the connection
echo '<p>'.$locales["Connexion_error"]."</p>\n";
print_connect_form();
}
else if(!mysql_select_db($cfg_database)){
echo '<p>'.$locales["Connexion_error"]."</p>\n";
print_connect_form();
}
else header("Location:index.php?step=2");
break;
case "2":
// Posted data, saving and proceeding
if($_POST["prefix_submit"]!="")
{
$traitOK = true;
if($_POST["dbprefix"]!=$cfg_table_prefix)
{
if($param_file_open = fopen("../cfg/param.php","r"))
{
$contenu_param_file_open = fread($param_file_open, filesize("../cfg/param.php"));
fclose($param_file_open);
$contenu_param_file_open = preg_replace('/\$'."cfg_table_prefix(.*)=(.*);(.*)/",'\$cfg_table_prefix = "'.$_POST['dbprefix']."\"; //Set during install", $contenu_param_file_open);
if($param_file_write = fopen("../cfg/param.php","w"))
{
if(fwrite($param_file_write, $contenu_param_file_open))
fclose($param_file_write);
else $traitOK = false;
}
else $traitOK = false;
}
else $traitOK = false;
}
if($traitOK) header("Location:index.php?step=3");
}
//nothing posted yet
echo '<form method="post"><p>'.$locales["Data_Table_prefix"].' : <input type="text" name="dbprefix" value="'.$cfg_table_prefix.'"/> <input type="submit" name="prefix_submit" value="'.$locales["Next_step"].'"/></p></form>';
break;
case "3":
// Posted data, saving and proceeding
if($_POST["dblaunch"]!="")
{
$traitOK = true;
if($sql_file_open = fopen($_POST["install-type"],"r"))
{
$contenu_sql_file_open = fread($sql_file_open, filesize($_POST["install-type"]));
fclose($sql_file_open);
$comment_patterns = array('/\/\*.*(\n)*.*(\*\/)?/', //C comments
'/\s*--.*\n/', //inline comments start with --
'/\s*#.*\n/', //inline comments start with #
);
$contenu_sql_file_open = preg_replace($comment_patterns, "\n", $contenu_sql_file_open);
$contenu_sql_file_open = preg_replace('/mc_/', $cfg_table_prefix, $contenu_sql_file_open);
$statements = explode(";\n", $contenu_sql_file_open);
$statements = preg_replace("/\s/", ' ', $statements);
$DB_connexion = mysql_connect($cfg_host, $cfg_user, $cfg_password);
if (! $DB_connexion){
die('<p>'.$locales["Connexion_error"]."</p>\n");
}
if(!mysql_select_db($cfg_database)){
die('<p>'.$locales["Connexion_error"]."</p>\n");
}
$compteurQuery=1;
foreach ($statements as $query) {
if (trim($query) != '') {
echo '<p>'.$locales["Executing_query"].' : '.$compteurQuery++."</p>\n";
$result_requete=mysql_query($query,$DB_connexion);
if(!$result_requete)
{
echo '<p style="color : red;">'.$locales["ErrMsg_SQL"]." : ".mysql_error($DB_connexion)."</p>";
$traitOK = false;
}
else echo '<p style="color : green;">OK</p>';
}
}
}
if($traitOK)
{
echo '<form method="POST" action="index.php?step=4"><p>'.$locales["Update_finished"].' <input type="submit" name="update_finished" value="'.$locales["Next_step"].'"/></p></form>';
}
}
//nothing posted yet
else
{
echo '<form method="post"><p>'.$locales["Install_type"].' : <select name="install-type">';
if ($dh = opendir("."))
{
while (($SQLfile = readdir($dh)) !== false)
if (substr($SQLfile, strlen($SQLfile) - 4) == '.sql')
{
echo '<option value="'.$SQLfile.'">'.str_replace("_"," ",substr($SQLfile, 0,strlen($SQLfile) - 4))."</option>";
}
}
closedir($dh);
echo '</select></p>
<p>'.$locales["Launch_SQL_script"].' <input type="submit" name="dblaunch" value="'.$locales["Next_step"].'"/></p></form>';
}
break;
case "4":
// Posted data, saving and proceeding
if($_POST["settings_submit"]!="")
{
$traitOK = true;
if(($_POST["site_title"]=="")||($_POST["site_email"]=="")||($_POST["site_author"]=="")||($_POST["admin_login"]=="")||($_POST["admin_password"]=="")||($_POST["admin_password_confirm"]=="")||($_POST["check_version_url"]=="")||($_POST["log_entries"]==""))
{
echo '<p>'.$locales["Fill_all_fields"]."</p>\n";
$traitOK = false;
}
if($traitOK)
{
if($_POST["admin_password"] != $_POST["admin_password_confirm"])
{
echo '<p>'.$locales["ErrMsg_PwDiff"]."</p>\n";
$traitOK = false;
}
}
if($traitOK)
{
$DB_connexion = mysql_connect($cfg_host, $cfg_user, $cfg_password);
if (! $DB_connexion){
die('<p>'.$locales["Connexion_error"]."</p>\n");
}
if(!mysql_select_db($cfg_database)){
die('<p>'.$locales["Connexion_error"]."</p>\n");
}
$requete_insert_admin = "INSERT INTO ".$cfg_table_prefix."users(login,password,email,level,language) VALUES ('".addslashes($_POST["admin_login"])."', MD5('".addslashes($_POST["admin_password"])."'),'".addslashes($_POST["site_email"])."', '3','".$lang."')";
$envoi_requete_insert_admin = mysql_query($requete_insert_admin,$DB_connexion);
if(!$envoi_requete_insert_admin)
{'<p>'.$locales["ErrMsg_SQL"]." : ".mysql_error($DB_connexion)."</p>\n";$traitOK = false;}
}
if($traitOK)
{
if($param_file_open = fopen("../cfg/param.php","r"))
{
$contenu_param_file_open = fread($param_file_open, filesize("../cfg/param.php"));
fclose($param_file_open);
if($_POST['site_title']!=$cfg_site_title) $contenu_param_file_open = preg_replace('/\$'."cfg_site_title(.*)=(.*);(.*)/",'\$cfg_site_title = "'.$_POST['site_title']."\"; //Set during install", $contenu_param_file_open);
if($_POST['site_email']!=$cfg_site_email) $contenu_param_file_open = preg_replace('/\$'."cfg_site_email(.*)=(.*);(.*)/",'\$cfg_site_email = "'.$_POST['site_email']."\"; //Set during install", $contenu_param_file_open);
if($_POST['site_author']!=$cfg_site_author) $contenu_param_file_open = preg_replace('/\$'."cfg_site_author(.*)=(.*);(.*)/",'\$cfg_site_author = "'.$_POST['site_author']."\"; //Set during install", $contenu_param_file_open);
if($_POST['check_version_url']!=$cfg_check_version_site) $contenu_param_file_open = preg_replace('/\$'."cfg_check_version_site(.*)=(.*);(.*)/",'\$cfg_check_version_site = "'.$_POST['check_version_url']."\"; //Set during install", $contenu_param_file_open);
if($_POST['log_entries']!=$cfg_max_log_entries) $contenu_param_file_open = preg_replace('/\$'."cfg_max_log_entries(.*)=(.*);(.*)/",'\$cfg_max_log_entries = "'.$_POST['log_entries']."\"; //Set during install", $contenu_param_file_open);
if($param_file_write = fopen("../cfg/param.php","w"))
{
if(fwrite($param_file_write, $contenu_param_file_open))
fclose($param_file_write);
else $traitOK = false;
}
else $traitOK = false;
}
else $traitOK = false;
}
if($traitOK) header("Location:index.php?step=5");
}
//nothing posted yet
echo '<form method="post"><table><tr><td>'.$locales["Site_Title"].'</td><td><input type="text" name="site_title" value="';
if($_POST["site_title"]!="") echo $_POST["site_title"]; else echo $cfg_site_title;
echo '"/></td></tr><tr><td>'.$locales["Site_email"].'</td><td><input type="text" name="site_email" value="';
if($_POST["site_email"]!="") echo $_POST["site_email"]; else echo $cfg_site_email;
echo '"/></td></tr><tr><td>'.$locales["Site_author"].'</td><td><input type="text" name="site_author" value="';
if($_POST["site_author"]!="") echo $_POST["site_author"]; else echo $cfg_site_author;
echo '"/></td></tr><tr><td>'.$locales["admin_login"].'</td><td><input type="text" name="admin_login" value="';
if($_POST["admin_login"]!="") echo $_POST["admin_login"];
echo '"/></td></tr><tr><td>'.$locales["UserPassWord"].'</td><td><input type="password" name="admin_password" value="';
echo '"/></td></tr><tr><td>'.$locales["UserConfirmPassword"].'</td><td><input type="password" name="admin_password_confirm" value="';
echo '"/></td></tr><tr><td>'.$locales["Site_check_version"].'</td><td><input type="text" name="check_version_url" value="';
if($_POST["check_version_url"]!="") echo $_POST["check_version_url"]; else echo $cfg_check_version_site;
echo '"/></td></tr><tr><td>'.$locales["Site_log_entries"].'</td><td><input type="text" name="log_entries" value="';
if($_POST["log_entries"]!="") echo $_POST["log_entries"]; else echo $cfg_max_log_entries;
echo '"/></td></tr><tr><td></td><td><input type="submit" name="settings_submit" value="'.$locales["Next_step"].'"/></td</tr></table></form>';
break;
case "5":
if($_POST["install_finished"]!="")
{
if ($dh = opendir("."))
{
while (($file = readdir($dh)) !== false)
unlink($file);
}
closedir($dh);
rmdir(".");
header("Location:../index.php");
}
echo '<form method="POST"><p>'.$locales["Install_finished"].' <input type="submit" name="install_finished" value="'.$locales["Next_step"].'"/></p></form>';
break;
case "":
if(($cfg_host=="")||($cfg_user=="")||($cfg_password=="")||($cfg_database==""))
{
echo '<h2>'.$locales["Connect_param"]."</h2>\n";
print_connect_form();
}
else header("Location:index.php?step=1a");
break;
}
ob_end_flush();
?>
</body>
</html>