<?php
	if(isset($_POST["sqlHost"])) {
		$mHost = $_POST["sqlHost"];
		$mUser = $_POST["sqlUser"];
		$mPass = $_POST["sqlPass"];
		$mData = $_POST["sqlData"];
		
		$mSocialName = $_POST["settingsSocialname"];
		$mCaptcha = $_POST["settingsCaptcha"];
		$mTimestamp = $_POST["settingsTimestamp"];
		$mComments = $_POST["settingsComments"];
		$mEmail = $_POST["settingsEmail"];
		$mPath = $_POST["settingsPath"];
		
		$mFirstname = $_POST["adminFirstname"];
		$mLastname = $_POST["adminLastname"];
		$mPassAdmin = $_POST["adminPass"];
		$mEmailAdmin = $_POST["adminEmail"];
		
		$mSMTPHost = $_POST["settingsSMTPHost"];
		$mSMTPPort = $_POST["settingsSMTPPort"];
		$mSMTPMail = $_POST["settingsSMTPMail"];
		$mSMTPPass = $_POST["settingsSMTPPass"];
		
		$mConnection = mysql_connect($mHost, $mUser, $mPass);
		if($mConnection && strlen($mEmailAdmin) > 0) {
			$mDatabase = mysql_select_db($mData);
			if($mDatabase) {
				$mConfigFile = fopen("../system.engine/system.config.php", "w");
				fwrite($mConfigFile, '<?php
									class systemCONFIG {
										public $mSQLInformation = array("s.HOST" => "'.$mHost.'", "s.USER" => "'.$mUser.'", "s.PASS" => "'.$mPass.'", "s.DATA" => "'.$mData.'", "s.PORT" => "3306");
										
										public function getCountry() {
											return substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0, 2);
										}
										
										public $mSocialName = null;
										public $mCaptchaType = 0;
										public $mTimeStampType = 0;
										public $mCommentsDisplay = 0;
										public $mEmailRegistration = 0;
										
										public $mAds = array();
										public $mPolicy = array();
										public $mMail = array();
										public $mCompletePath = null;
										
										public function loadConfiguration() {
											global $systemHandler;
											$mSource = $systemHandler->loadHandler("EXECUTION")->runQuery("SELECT * FROM web_settings", "")->fetch_assoc();
											
											$this->mSocialName = $mSource["social_name"];
											$this->mCaptchaType = $mSource["captcha"];
											$this->mTimeStampType = $mSource["timestamp"];
											$this->mCommentsDisplay = $mSource["comment_display"];
											$this->mEmailRegistration = $mSource["email_registration"];
											$this->mCompletePath = $mSource["complete_path"];
											
											$mAdsSource = $systemHandler->loadHandler("EXECUTION")->runQuery("SELECT * FROM web_advertisement", "")->fetch_assoc();
											$this->mAds[0] = $mAdsSource["post_advertisement"];
											$this->mAds[1] = $mAdsSource["profile_advertisement"];
											$this->mAds[2] = $mAdsSource["index_advertisement"];
											
											$mPolicySource = $systemHandler->loadHandler("EXECUTION")->runQuery("SELECT * FROM web_policy", "")->fetch_assoc();
											$this->mPolicy[0] = $mPolicySource["terms_of_use"];
											$this->mPolicy[1] = $mPolicySource["disclaimer"];
											$this->mPolicy[2] = $mPolicySource["privacy_policy"];
											$this->mPolicy[3] = $mPolicySource["about"];
											
											$this->mMail["SMTP_HOST"] = "'.$mSMTPHost.'";
											$this->mMail["SMTP_PORT"] = '.$mSMTPPort.';
											$this->mMail["OWN_EMAIL"] = "'.$mSMTPMail.'";
											$this->mMail["OWN_EMAIL_PASS"] = "'.$mSMTPPass.'"; // For SMTP authentification
											$this->mMail["EMAIL_SENDER_NAME"] = $this->mSocialName;
										}
									}
								?>') or die("Please CHMOD (777) the folder /system.engine/");
				
				mysql_query("CREATE TABLE `web_advertisement` (
							  `post_advertisement` text,
							  `profile_advertisement` text,
							  `index_advertisement` text
							) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
				mysql_query("INSERT INTO `web_advertisement` VALUES ('', '', '');");
				mysql_query("CREATE TABLE `web_albums` (
							  `id` int(11) NOT NULL AUTO_INCREMENT,
							  `album_name` varchar(255) NOT NULL,
							  `album_owner_id` int(11) NOT NULL,
							  `album_privacy` enum('friends','single','everyone') DEFAULT 'friends',
							  `album_is_standard` enum('no','yes') DEFAULT 'no',
							  `album_date_created` int(11) DEFAULT NULL,
							  PRIMARY KEY (`id`)
							) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8;");
				mysql_query("CREATE TABLE `web_comments` (
							  `id` int(11) NOT NULL AUTO_INCREMENT,
							  `user_id` int(11) NOT NULL,
							  `container_id` int(11) DEFAULT NULL,
							  `submit_date` int(11) DEFAULT NULL,
							  `comment_text` varchar(255) DEFAULT NULL,
							  `image_id` int(11) DEFAULT NULL,
							  PRIMARY KEY (`id`)
							) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;");
				mysql_query("CREATE TABLE `web_friendrequests` (
							  `id` int(11) NOT NULL AUTO_INCREMENT,
							  `sender_id` int(11) NOT NULL,
							  `receiver_id` int(11) NOT NULL,
							  PRIMARY KEY (`id`)
							) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;");
				mysql_query("CREATE TABLE `web_friends` (
							  `id` int(11) NOT NULL AUTO_INCREMENT,
							  `user_one_id` int(11) NOT NULL,
							  `user_two_id` int(11) NOT NULL,
							  PRIMARY KEY (`id`)
							) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;");
				mysql_query("CREATE TABLE `web_images` (
							  `id` int(11) NOT NULL AUTO_INCREMENT,
							  `image_hash` varchar(255) DEFAULT NULL,
							  `image_user_id` int(11) DEFAULT NULL,
							  `image_album_id` int(11) NOT NULL DEFAULT '-1',
							  PRIMARY KEY (`id`)
							) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;");
				mysql_query("CREATE TABLE `web_likes` (
							  `id` int(11) NOT NULL AUTO_INCREMENT,
							  `submit_date` int(11) DEFAULT NULL,
							  `user_id` int(11) DEFAULT NULL,
							  `container_id` int(11) DEFAULT NULL,
							  `comment_id` int(11) DEFAULT NULL,
							  `image_id` int(11) DEFAULT NULL,
							  PRIMARY KEY (`id`)
							) ENGINE=InnoDB AUTO_INCREMENT=37 DEFAULT CHARSET=utf8;");
				mysql_query("CREATE TABLE `web_messages` (
							  `id` int(11) NOT NULL AUTO_INCREMENT,
							  `user_one` int(11) NOT NULL,
							  `user_two` int(11) NOT NULL,
							  `submit_date` int(11) NOT NULL,
							  `message` varchar(500) NOT NULL,
							  `message_seen_user` int(11) NOT NULL DEFAULT '0',
							  `message_seen` int(1) NOT NULL DEFAULT '0',
							  PRIMARY KEY (`id`)
							) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8;");
				mysql_query("CREATE TABLE `web_notifications` (
							  `id` int(11) NOT NULL AUTO_INCREMENT,
							  `user_id` int(11) NOT NULL,
							  `has_seen` int(1) NOT NULL,
							  `submit_date` int(11) NOT NULL,
							  `notification_type` int(11) NOT NULL,
							  `sender_user` int(11) NOT NULL,
							  `container_id` int(11) DEFAULT NULL,
							  `comment_id` int(11) DEFAULT NULL,
							  `picture_id` int(11) DEFAULT NULL,
							  PRIMARY KEY (`id`)
							) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;");
				mysql_query("CREATE TABLE `web_policy` (
							  `terms_of_use` text NOT NULL,
							  `disclaimer` text NOT NULL,
							  `privacy_policy` text NOT NULL,
							  `about` text NOT NULL
							) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
				mysql_query("INSERT INTO `web_policy` VALUES ('<strong>Terms of use</strong><br />You can change the terms of use in the admin panel.', '<strong>Disclaimer</strong><br />You can change the disclaimer in the admin panel.', '<strong>Privacy Policy</strong><br />You can change the privacy policy in the admin panel.', '<strong>About Elitenetwork</strong><br />Elitenetwork has been written by <a href=\"http://elitecode.de/\">Elitecode</a>.');");
				mysql_query("CREATE TABLE `web_reports` (
							  `id` int(11) NOT NULL AUTO_INCREMENT,
							  `report_type` enum('container','comment') NOT NULL DEFAULT 'container',
							  `report_id` int(11) NOT NULL,
							  `report_user` int(11) NOT NULL,
							  `report_time` int(11) NOT NULL,
							  PRIMARY KEY (`id`)
							) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;");
				mysql_query("CREATE TABLE `web_settings` (
							  `social_name` varchar(255) NOT NULL DEFAULT 'Elitenetwork',
							  `captcha` int(1) NOT NULL,
							  `timestamp` int(1) NOT NULL,
							  `chat_type` int(1) NOT NULL,
							  `comment_display` int(1) NOT NULL,
							  `email_registration` int(1) NOT NULL,
							  `complete_path` varchar(500) NOT NULL
							) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
				mysql_query("INSERT INTO `web_settings` VALUES ('Persian Script', '1', '2', '1', '6', '0', 'http://elitecode.de/cc/elitenetwork');");
				mysql_query("CREATE TABLE `web_timeline` (
							  `id` int(11) NOT NULL AUTO_INCREMENT,
							  `post_user_id` int(11) NOT NULL,
							  `post_text` text,
							  `post_location` varchar(255) DEFAULT NULL,
							  `post_privacy` int(1) DEFAULT NULL,
							  `post_hashtags_list` varchar(255) DEFAULT NULL,
							  `post_mentions_list` varchar(255) DEFAULT NULL,
							  `post_submit_date` int(11) DEFAULT NULL,
							  `post_image_id` int(11) DEFAULT NULL,
							  `post_entry_location` int(11) DEFAULT NULL,
							  `post_share_id` int(11) DEFAULT '-1',
							  PRIMARY KEY (`id`)
							) ENGINE=InnoDB AUTO_INCREMENT=113 DEFAULT CHARSET=utf8;");
				mysql_query("CREATE TABLE `web_user` (
							  `id` int(11) NOT NULL AUTO_INCREMENT,
							  `firstname` varchar(20) DEFAULT NULL,
							  `mail` varchar(255) DEFAULT NULL,
							  `lastname` varchar(20) DEFAULT NULL,
							  `password` varchar(255) DEFAULT NULL,
							  `signup_time` int(11) DEFAULT NULL,
							  `signin_time` int(11) DEFAULT NULL,
							  `secret_hash` varchar(255) DEFAULT NULL,
							  `chat_hash` varchar(255) DEFAULT NULL,
							  `profile_hash` varchar(255) DEFAULT NULL,
							  `gender` int(1) NOT NULL DEFAULT '0',
							  `chat_status` int(1) NOT NULL DEFAULT '0',
							  `is_admin` int(1) NOT NULL DEFAULT '0',
							  `workplace` varchar(255) DEFAULT 'Envato Marketplace',
							  `residence` varchar(255) DEFAULT 'Berlin',
							  `details` varchar(255) DEFAULT 'To be changed..',
							  `lost_hash` varchar(255) DEFAULT NULL,
							  PRIMARY KEY (`id`)
							) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;");
				mysql_query(vsprintf("INSERT INTO web_friends (user_one_id, user_two_id) VALUES ('%s', '%s')", array(-1, -1)));
				
				mysql_query(vsprintf("UPDATE web_settings SET social_name = '%s', captcha = '%s', timestamp = '%s', comment_display = '%s', email_registration = '%s', complete_path = '%s'", array($mSocialName, $mCaptcha, $mTimestamp, $mComments, $mEmail, $mPath)));
				mysql_query(vsprintf("INSERT INTO web_user (firstname, mail, lastname, password, signup_time, signin_time, secret_hash, chat_hash, profile_hash, gender, chat_status, is_admin) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", array($mFirstname, $mEmailAdmin, $mLastname, hash("sha256", $mPassAdmin), time(), time(), '', '', '0', '0', '0', '1')));
				die("<center style=\"margin-top: 50px\"><h1>Installation is completed!<br /><strong>PLEASE DELETE THIS FOLDER IF EVERYTHING WORKS PROPERLY ON THE HOMEPAGE</strong><br /><a href=\"../\">Go to your social network &raquo;</a></h1></center>");
			} else die("Couldn't load the database. Does it even exist?");
		} else die("Couldn't connect to db. Did you change the SQL information?");
	}
?>
<!DOCTYPE html>
<!--[if IE 8]> <html lang="en" class="ie8"> <![endif]-->  
<!--[if IE 9]> <html lang="en" class="ie9"> <![endif]-->  
<!--[if !IE]><!--> <html lang="en"> <!--<![endif]-->  
<head>
    <title>Elitenetwork installation</title>
    <!-- Meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Responsive HTML5 Website landing Page for Developers">
    <meta name="author" content="3rd Wave Media">    
    <link rel="shortcut icon" href="favicon.ico">  
    <link href='http://fonts.googleapis.com/css?family=Lato:300,400,300italic,400italic' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Montserrat:400,700' rel='stylesheet' type='text/css'> 
    <!-- Global CSS -->
    <link rel="stylesheet" href="assets/plugins/bootstrap/css/bootstrap.min.css">   
    <!-- Plugins CSS -->
    <link rel="stylesheet" href="assets/plugins/font-awesome/css/font-awesome.css">
    <!-- github acitivity css -->
    <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/octicons/2.0.2/octicons.min.css">
    <link rel="stylesheet" href="http://caseyscarborough.github.io/github-activity/github-activity-0.1.0.min.css">
    
    <!-- Theme CSS -->  
    <link id="theme-style" rel="stylesheet" href="assets/css/styles.css">
    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
	
	<style type="text/css">
		.unvisibleRow {
			display: none
		}
	</style>
    
</head> 

<body>
    <!-- ******HEADER****** --> 
    <header class="header">
        <div class="container">                       
            <img class="profile-image img-responsive pull-left" src="assets/images/elitenetwork_logo.png" style="margin-top: 15px" alt="James Lee" />
            <div class="profile-content pull-left">
                <h1 class="name">Elitenetwork installation</h1>
                <h2 class="desc">powerful social network script</h2> 
            </div><!--//profile-->           
        </div><!--//container-->
    </header><!--//header-->
    
    <div class="container sections-wrapper">
		<form method="POST" action="index.php">
        <div class="row">
            <div class="primary col-md-12 col-sm-12 col-xs-12">
                <section class="about section">
                    <div class="section-inner">
                        <h2 class="heading">MySQL details</h2>
                        <div class="content">
							<p>We require your SQL-Server information to upload the database for you. Please provide valid SQL informations.</p>
							<hr />
							
							<div class="row">
								<div class="col-md-6 col-xs-12">
									<label for="sqlHost">SQL Host <small>(Default: 127.0.0.1)</small></label>
									<input value="127.0.0.1" name="sqlHost" id="sqlHost" type="text" class="form-control" />
								</div>
								<div class="col-md-6 col-xs-12">
									<label for="sqlUser">SQL User <small>(Default: root)</small></label>
									<input value="root" name="sqlUser" id="sqlUser" type="text" class="form-control" />
								</div>
							</div>
								<br />
							<div class="row">
								<div class="col-md-6 col-xs-12">
									<label for="sqlPass">SQL Password</label>
									<input value="" name="sqlPass" id="sqlPass" type="password" class="form-control" />
								</div>
								<div class="col-md-6 col-xs-12">
									<label for="sqlData">SQL Database</label>
									<input value="" name="sqlData" id="sqlData" type="text" class="form-control" />
								</div>
							</div>
								<br />
							<div class="row">
								<div class="col-md-3 col-md-offset-9" id="sqlBtnArea">
									<center>
										<a class="btn btn-block btn-primary" id="sqlCheck">
											Check the SQL connection
										</a>
									</center>
								</div>
							</div>
							
                        </div><!--//content-->
                    </div><!--//section-inner-->                 
                </section><!--//section-->
            </div><!--//primary-->
           
        </div><!--//row-->
		
		<div class="row unvisibleRow">
            <div class="primary col-md-12 col-sm-12 col-xs-12">
                <section class="about section">
                    <div class="section-inner">
                        <h2 class="heading">General settings</h2>
                        <div class="content">
							<p>Now it's part to configure your social network. Change between names and settings.</p>
							<hr />
							
							<div class="row">
								<div class="col-md-6 col-xs-12">
									<label for="settingsSocialname">Social network name</label>
									<input value="Elitenetwork" name="settingsSocialname" id="settingsSocialname" type="text" class="form-control" />
								</div>
								<div class="col-md-3 col-xs-6">
									<label for="settingsCaptcha">Captcha at registration</label>
									<select name="settingsCaptcha" class="form-control">
										<option value="1" selected>Activated</option>
										<option value="2">Deactivated</option>
									</select>
								</div>
								
								<div class="col-md-3 col-xs-6">
									<label for="settingsTimestamp">Timestamp</label>
									<select name="settingsTimestamp" class="form-control">
										<option value="2" selected>use 'ago 10 seconds' - Simple format</option>
										<option value="1">PM/AM - American</option>
										<option value="0">00:00 - European</option>
									</select>
								</div>
							</div>
								<br />
								
							<div class="row">
								<div class="col-md-3 col-xs-12">
									<label for="settingsSMTPHost">SMTP Server</label>
									<input value="smtp.gmail.com" name="settingsSMTPHost" id="settingsSMTPHost" type="text" class="form-control" />
								</div>
								
								<div class="col-md-3 col-xs-12">
									<label for="settingsSMTPPort">SMTP Port</label>
									<input value="587" name="settingsSMTPPort" id="settingsSMTPPort" type="text" class="form-control" />
								</div>
								
								<div class="col-md-3 col-xs-12">
									<label for="settingsSMTPMail">Email address</label>
									<input value="" name="settingsSMTPMail" id="settingsSMTPMail" type="text" class="form-control" />
								</div>
								
								<div class="col-md-3 col-xs-12">
									<label for="settingsSMTPPass">Email password <small>Required for SMTP authentification</small></label>
									<input value="" name="settingsSMTPPass" id="settingsSMTPPass" type="password" class="form-control" />
								</div>
							</div>
							
								<br />
							<div class="row">
								<div class="col-md-4 col-xs-12">
									<label for="settingsComments">Amount of visible comments for each post</label>
									<select name="settingsComments" class="form-control">
										<option value="6">6</option>
										<option value="3" selected>3</option>
										<option value="2">2</option>
										<option value="1">1</option>
									</select>
								</div>
								
								<div class="col-md-4 col-xs-12">
									<label for="settingsEmail">Email after registration</label>
									<select name="settingsEmail" class="form-control">
										<option value="1" selected>Activated</option>
										<option value="0">Deactivated</option>
									</select>
								</div>
								
								<div class="col-md-4 col-xs-12">
									<label for="settingsPath">The URL to your social network WITHOUT ending slash</label>
									<input type="text" class="form-control" name="settingsPath" />
								</div>
							</div>
							
                        </div><!--//content-->
                    </div><!--//section-inner-->                 
                </section><!--//section-->
            </div><!--//primary-->
           
        </div><!--//row-->
		
        <div class="row unvisibleRow">
            <div class="primary col-md-12 col-sm-12 col-xs-12">
                <section class="about section">
                    <div class="section-inner">
                        <h2 class="heading">Admin user information</h2>
                        <div class="content">
							<p>Because there are no users in the database, we require your admin information. This form is something like a admin-signup for you.</p>
							<hr />
							
							<div class="row">
								<div class="col-md-3 col-xs-6">
									<label for="adminFirstname">Firstname</small></label>
									<input name="adminFirstname" id="adminFirstname" type="text" class="form-control" />
								</div>
								<div class="col-md-3 col-xs-6">
									<label for="adminLastname">Lastname</small></label>
									<input name="adminLastname" id="adminLastname" type="text" class="form-control" />
								</div>
								
								<div class="col-md-6">
									<label for="adminPass">Password</small></label>
									<input name="adminPass" id="adminPass" type="password" class="form-control" />
								</div>
							</div>
								<br />
							<div class="row">
								<div class="col-md-12 col-xs-12">
									<label for="adminEmail">Email address <small>Required for login</small></label>
									<input value="" name="adminEmail" id="adminEmail" type="email" class="form-control" />
								</div>
							</div>
								<br />
							<div class="row">
								<div class="col-md-12">
									<input type="submit" class="btn btn-block btn-lg btn-primary" id="sqlCheck" value="Install elitenetwork and continue to the landing page" />
								</div>
							</div>
							
                        </div><!--//content-->
                    </div><!--//section-inner-->                 
                </section><!--//section-->
            </div><!--//primary-->
          </form>
        </div><!--//row-->
    </div><!--//masonry-->
 
    <!-- Javascript -->          
    <script type="text/javascript" src="assets/plugins/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" src="assets/plugins/jquery-migrate-1.2.1.min.js"></script>
    <script type="text/javascript" src="assets/plugins/bootstrap/js/bootstrap.min.js"></script>    
    <script type="text/javascript" src="assets/plugins/jquery-rss/dist/jquery.rss.min.js"></script>
	
	<script type="text/javascript">
		$(document).ready(function () {
			$("body").on("click", "#sqlCheck", function () {
				$.ajax({
					url: "ajax.mysql.check.php",
					type: "POST",
					dataType: "JSON",
					data: { sqlHost: $("#sqlHost").val(), sqlUser: $("#sqlUser").val(), sqlPass: $("#sqlPass").val(), sqlData: $("#sqlData").val() },
					success: function(ajaxResponse) {
						if(ajaxResponse.isValid == true) {
							$("#sqlHost").attr("disabled");
							$("#sqlUser").attr("disabled");
							$("#sqlPass").attr("disabled");
							$("#sqlData").attr("disabled");
							$("#sqlBtnArea").html("<strong>Successful connection!</strong>");
							$(".unvisibleRow").fadeIn();
						} else {
							alert("The entered sql information is incorrect. Please try it again. We couldn't create a connection to your sql server!");
						}
					}
				});
			});
		});
	</script>
</body>
</html> 

