Location: PHPKode > scripts > GalleryShuffle > v1.0/php/install.php
<?php


include($phpFolder . "FUNCTIONS.php");
include($phpFolder . "DB.php");
include($phpFolder . "HTML.php");
include($phpFolder . "LANG.php");


FUNCTIONS::encodePost();
FUNCTIONS::encodeGet();


if(isset($_POST['lang']) && $_POST['lang'] != NULL){
	LANG::createSingleton($_POST['lang']);
	$LANG = LANG::getSingleton();
}

$buffer = HTML::getHtmlHead("GalleryShuffle installation", 
	Array("css/all.css",
	"css/front.css"),
	Array(), 
	Array("js/typeface-0.15.js",
	"js/brush_script_std_medium.typeface.js"),
	"");

	
$buffer .= '
	<div id="welcome">';
	
$buffer .= '
		<p class="title">GalleryShuffle installation</p><form method="POST" action="index.php">';

	
if(isset($_POST['mail'])){
	
	$state = '';
	
	$passwordLength = mb_strlen(html_entity_decode($_POST['password1'], ENT_QUOTES, "UTF-8"));
	
	
	if($_POST['mail'] == '') $state .= '<li><i>' . $LANG->traduc['adminMail'] . '</i>' . $LANG->traduc['WarningCannotBeNull'] . '</li>';
	elseif(!FUNCTIONS::isMailAddress($_POST['mail'])) $state .= '<li>' . $LANG->traduc['WarningShouldBeMail'] . '</li>';
	
	if($passwordLength < 6 || $passwordLength > 10) $state .= '<li>' . $LANG->traduc['passwordTooShort'] . '</li>';
	elseif($_POST['password1'] != $_POST['password2']) $state .= '<li>' . $LANG->traduc['passwordsNotMatch'] . '</li>';
	
	if($_POST['dbport'] != '' && !FUNCTIONS::isInt($_POST['dbport'])) $state .= '<li><i>' . $LANG->traduc['dataBaseUser'] . '</i>' . $LANG->traduc['WarningShouldBeInt'] . '</li>';
	
	if($_POST['dbname'] == '') $state .= '<li><i>' . $LANG->traduc['dataBaseName'] . '</i>' . $LANG->traduc['WarningCannotBeNull'] . '</li>';
	if($_POST['dbaddress'] == '') $state .= '<li><i>' . $LANG->traduc['dataBaseAddress'] . '</i>' . $LANG->traduc['WarningCannotBeNull'] . '</li>';
	if($_POST['dbuser'] == '') $state .= '<li><i>' . $LANG->traduc['dataBaseUser'] . '</i>' . $LANG->traduc['WarningCannotBeNull'] . '</li>';
	
	// if(!DB::Open($_POST['dbname'], $_POST['dbaddress'], $_POST['dbport'], $_POST['dbuser'], $_POST['dbpassword']))
		// $state .= '<li><b>' . $LANG->traduc['WarningDataBaseConnectFaild'] . '</b></li>';
	
	
	if($state == '' && !mysql_connect($_POST['dbaddress'] . ":" . $_POST['dbport'], $_POST['dbuser'], $_POST['dbpassword']))
		 $state .= '<li><b>' . $LANG->traduc['WarningDataBaseConnectFaild'] . '</b></li>';
	
	if($state == ''){
		
		chmod("content/", 0755);
		chmod("content/large", 0755);
		chmod("content/small", 0755);
		chmod("php/", 0755);
		chmod("lang/", 0755);
		
		$imageAccessUID = md5(uniqid("",true));
		
		writeInConfigFile($imageAccessUID);
		//writeHtaccess("content/");
		writeHtaccess("php/");
		writeHtaccess("lang/");
		
		writeContentHtaccess("content/", $imageAccessUID);
		
		//chmod("php/", 0644);
		//chmod("lang/", 0644);
		
		dataBaseCreateTables();
		
		$buffer .= '
		<p class="state">' . $LANG->traduc['installComplete'] . '</p>' . "
		<script>
			var timer = setInterval( function(evt) {
				window.location.href='index.php';
			}, 1000);
		</script>";
		
	}else{
	
		$buffer .= '<ul style="position: normal;" class="state">' . $state . '</ul>';
		$buffer .= getInstallForm($LANG);
		
	}
	
	
}elseif(isset($_POST['lang']) && $_POST['lang'] != NULL){
	
	
	$buffer .= getInstallForm($LANG);

	
}else{

	$buffer .= '
		<p>Welcome on Gallery Shuffle Installation<br /><br /><br /><b>Required configuration :</b><br /><br /></p>';
		
	$phpVersion = phpversion();
	preg_match_all('/[0-9]+/', $phpVersion, $phpVersions);
	$phpVersionIsCorrect = false;
	if($phpVersions[0][0] >= 5 && $phpVersions[0][1] >= 2) $phpVersionIsCorrect = true;
	
	$_SESSION['open'] = true; 
	
	$buffer .= '
		<p class="';
	if($phpVersionIsCorrect) $buffer .= 'valide';
	else $buffer .= 'error';
	$buffer .= '">PHP v5.2 or higher</p>';
	
	$buffer .= '
		<p class="';
	if(isset($_SESSION['open'])) $buffer .= 'valide';
	else $buffer .= 'error';
	$buffer .= '">Sessions are enabled</p>';
	
	if($phpVersionIsCorrect && isset($_SESSION['open'])){
	
		$buffer .= '
			<p><br /><br /><br />Please choose your language to begin :<br /><br />
			<select id="langSelect" name="lang" onchange="javascript: this.style.backgroundImage = ' . "'url(lang/flags/'+this.options[this.selectedIndex].innerHTML+'.png)';" . '" >';
			
		$langDir = opendir("lang");
		while ($file = readdir($langDir)) {

			if($file != "." && $file != ".." && $file != ".htaccess" && $file != "flags"){
				
				$langCode = str_replace(".csv", "", $file);
				
				$buffer .= '
				<option ';
				
				if($langCode == 'EN') $buffer .= 'selected="selected" ';
				
				$buffer .= 'value="' . $langCode . '" style="background-image: url(lang/flags/' . $langCode . '.png);">' . $langCode . '</option>';
				
			}
		
		}
		closedir($langDir);
			
		$buffer .= '
			</select>
			<input style="cursor: pointer;" type="submit" value="Begin installation >>"/>';
		
	}
}
	
	
$buffer .= '
	</form></div>';

$buffer .= HTML::getHtmlFoot();

echo $buffer;


function getInstallForm($LANG) 
{
	
	$formBuffer = '
		<input style="display: none;" name="lang" value="' . $_POST['lang'] . '"/>
		
		<p class="subtitle">' . $LANG->traduc['admin'] . '</p>
		
		<br /><label for="mail">' . $LANG->traduc['adminMail'] . '&nbsp;<span class="star">*</span>&nbsp;:</label><input size="25" id="mail" name="mail" ';
	if(isset($_POST['mail']) && $_POST['mail'] != NULL) $formBuffer .= 'value="' . $_POST['mail'] . '"';
	$formBuffer .= '/>
		
		<br /><label for="password1">' . $LANG->traduc['enterNewPassword'] . '&nbsp;<span class="star">*</span>&nbsp;:</label><input type="password" size="10" id="password1" name="password1" />
		<br /><label for="password2">' . $LANG->traduc['confirmNewPassword'] . '&nbsp;<span class="star">*</span>&nbsp;:</label><input type="password" size="10" id="password2" name="password2" />
		
		<p class="subtitle">' . $LANG->traduc['dataBase'] . '</p>
		
		<br /><label for="dbname">' . $LANG->traduc['dataBaseName'] . '&nbsp;<span class="star">*</span>&nbsp;:</label><input size="20" id="dbname" name="dbname" ';
	if(isset($_POST['dbname']) && $_POST['dbname'] != NULL) $formBuffer .= 'value="' . $_POST['dbname'] . '"';
	$formBuffer .= '/>
		
		<br /><label for="dbaddress">' . $LANG->traduc['dataBaseAddress'] . '&nbsp;<span class="star">*</span>&nbsp;:</label><input size="20" id="dbaddress" name="dbaddress" ';
	if(isset($_POST['dbaddress']) && $_POST['dbaddress'] != NULL) $formBuffer .= 'value="' . $_POST['dbaddress'] . '"';
	else $formBuffer .= 'value="localhost"';
	$formBuffer .= '/>
		
		<br /><label for="dbport">' . $LANG->traduc['dataBasePort'] . '&nbsp;:</label><input size="20" id="dbport" name="dbport" ';
	if(isset($_POST['dbport']) && $_POST['dbport'] != NULL) $formBuffer .= 'value="' . $_POST['dbport'] . '"';
	$formBuffer .= '/>
		
		<br /><label for="dbuser">' . $LANG->traduc['dataBaseUser'] . '&nbsp;<span class="star">*</span>&nbsp;:</label><input size="20" id="dbuser" name="dbuser" ';
	if(isset($_POST['dbuser']) && $_POST['dbuser'] != NULL) $formBuffer .= 'value="' . $_POST['dbuser'] . '"';
	$formBuffer .= '/>
		
		<br /><label for="dbpassword">' . $LANG->traduc['dataBasePassword'] . '&nbsp;<span class="star">*</span>&nbsp;:</label><input size="20" id="dbpassword" name="dbpassword" type="password" />
		
		<p class="subtitle">' . $LANG->traduc['gallery'] . '</p>
		
		<br /><label for="title">' . $LANG->traduc['title'] . '&nbsp;:</label><input size="20" id="title" name="title" ';
	if(isset($_POST['title']) && $_POST['title'] != NULL) $formBuffer .= 'value="' . $_POST['title'] . '"';
	$formBuffer .= '/>
		
		<br /><label for="subtitle">' . $LANG->traduc['subtitle'] . '&nbsp;:</label><input size="20" id="subtitle" name="subtitle" ';
	if(isset($_POST['subtitle']) && $_POST['subtitle'] != NULL) $formBuffer .= 'value="' . $_POST['subtitle'] . '"';
	$formBuffer .= '/>
		
		<br /><br /><br /><input style="cursor: pointer;" type="submit"/>
		<br /><br /><p style="font-size: 10px;"><span class="star">*</span> ' . $LANG->traduc['requiredInfos'] . '</p>
		';
		
	return $formBuffer;
	
}


function writeInConfigFile($imageAccessUID)
{
	
	$writeBuffer = "<?php

/**
* CONFIG PHP
**/

abstract class CONFIG
{

	const lang = '" . $_POST['lang'] . "';
	
	const title = 'GalleryShuffle';
	const version = '1.0';
	
	const picturesFolder = 'content/';
	const picturesSmallFolder = 'small/';
	const picturesLargeFolder = 'large/';	
	const uploadPictureSmallMaxSize = 250;
	const picturesExtension = '.png';
	
	const imageAccessUID = '" . $imageAccessUID . "';
	
	const DBname = '" . $_POST['dbname'] . "';
	const DBhostAddress = '" . $_POST['dbaddress'] . "';
	const DBhostPort = '" . $_POST['dbport'] . "';
	const DBuserName = '" . $_POST['dbuser'] . "';
	const DBpassword = '" . $_POST['dbpassword'] . "';
	
	const userLogin = '" . $_POST['mail'] . "';
	const userPassword = '" .  $_POST['password1'] . "';
	
	const galleryTitle = '" . $_POST['title'] . "';
	const gallerySubtitle = '" . $_POST['subtitle'] . "';
	const galleryIsShuffled = 1;
	const galleryIsLocked = 0;
	const galleryPassword = '';
	
	const uploadPictureLargeMaxSize = 800;
	
}

?>";
	
	
	$fp = fopen('php/CONFIG.php', "wb");
	fwrite($fp, $writeBuffer);
	fclose($fp); 
	
}

function writeHtaccess($filePath)
{
	
	$fp = fopen($filePath . '.htaccess', "wb");
	fwrite($fp, "Order deny,allow\ndeny from all");
	fclose($fp); 
	
}
function writeContentHtaccess($filePath, $imageAccessUID)
{
	
	$fp = fopen($filePath . '.htaccess', "wb");
	fwrite($fp, "Options -indexes\nRewriteEngine on\nReWriteRule .*\.(gif|png|jpe?g)$ - [F]");
	fclose($fp); 
	
	$fp = fopen($filePath . 'index.php', "wb");
	fwrite($fp, '<?php session_start(); if(isset($_GET["img"]) && $_GET["img"] != NULL && $_SESSION["imageAccessUID"] == "' . $imageAccessUID . '"){ readfile($_GET["img"]); exit; } ?> <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>403 Forbidden</title></head><body><h1>Forbidden</h1><p>You don' . "'" . 't have permission to access this folder.</p></body></html>');

	fclose($fp); 
	
}

function dataBaseCreateTables()
{
	
	DB::Open($_POST['dbname'], $_POST['dbaddress'], $_POST['dbport'], $_POST['dbuser'], $_POST['dbpassword']);
	
	$request = "CREATE TABLE IF NOT EXISTS `albums` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_bin NOT NULL,
  `URL` varchar(255) COLLATE utf8_bin NOT NULL,
  `default_picture_ID` int(11) NOT NULL,
  `is_private` tinyint(1) NOT NULL,
  `password` varchar(255) COLLATE utf8_bin NOT NULL,
  `is_active` tinyint(1) NOT NULL,
  `order` int(11) NOT NULL,
  `date_created` datetime NOT NULL,
  `date_modified` datetime NOT NULL,
  KEY `ID` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1 ;";

	DB::qry($request);

	$request = "CREATE TABLE IF NOT EXISTS `clients_logs` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `UID` varchar(255) COLLATE utf8_bin NOT NULL,
  `client_IP` varchar(255) COLLATE utf8_bin NOT NULL,
  `is_active` tinyint(1) NOT NULL,
  `is_for_gallery` tinyint(1) NOT NULL,
  `album_ID` int(11) NOT NULL,
  `date_created` datetime NOT NULL,
  `date_modified` datetime NOT NULL,
  KEY `ID` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1 ;";

	DB::qry($request);

	$request = "CREATE TABLE IF NOT EXISTS `pictures` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_bin NOT NULL,
  `UID` varchar(255) COLLATE utf8_bin NOT NULL,
  `album_ID` int(11) NOT NULL,
  `date_created` int(11) NOT NULL,
  `date_modified` int(11) NOT NULL,
  KEY `ID` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1 ;";

	DB::qry($request);

	$request = "CREATE TABLE IF NOT EXISTS `user_logs` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `UID` varchar(255) COLLATE utf8_bin NOT NULL,
  `client_IP` varchar(255) COLLATE utf8_bin NOT NULL,
  `is_active` tinyint(1) NOT NULL,
  `date_created` datetime NOT NULL,
  `date_modified` datetime NOT NULL,
  KEY `ID` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1 ;";
	
	DB::qry($request);
	
	
}


?>
Return current item: GalleryShuffle