<?php

if (!function_exists('gettext')) {
	echo 'You don\'t have Gettext extension installed. Please install it in order to proceed';
	die;
}
if (get_magic_quotes_gpc()) {
	echo 'Please turn off Magic Quotes from php settings in order to proceed';
	die;
}

if (ini_get('allow_url_fopen') != 1) {
  	echo 'allow_url_fopen function is disabled from server. Allow it to proceed!';
}

if (!defined('PDO::ATTR_DRIVER_NAME')) {
	echo _("Can't use database, PDO driver is missing. Install PDO capabiltities to your server, then try again.");
	die;
}

$sumfingIncl = true;
include '../translate.php';

if(isset($_POST['install'])) {
//print_r($_POST);
	$error = array();

	// connect
	try {
	$conn = new PDO("mysql:host=".trim($_POST['server_name']).";dbname=".trim($_POST['db_name']), trim($_POST['db_user']),trim($_POST['db_pass']));
	$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$conn->exec("SET CHARACTER SET utf8");
	$connection = true;
	} catch(PDOException $e) {
		$error[] = _("Unable to connect with database. Please check your settings.") . '<br>Error message: ' . $e->getMessage();
		$connection = false;
	}
	
	try {
		if($connection) {
			$sql = "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '".$_POST['db_name']."'";
			if($stmt = $conn->query($sql))
			$results = $stmt->fetch(PDO::FETCH_OBJ);
		}
	} catch(PDOException $e) {
		$error[] = _("No databases found with your given name. Give correct database name!") . '<br>Error message: ' . $e->getMessage();
	}
	
	foreach($_POST as $form) {
		if(strpos($form, "http://") !== false) {
			$error[] = _("Urls must be <strong>https://</strong>, secure urls.");
		}
	}
	
	
	if(count($error) == 0) {
		
		//INSTALL
		
		// Make config.thing file
		$conffile = "../config.thing.php";
		$fh = fopen ($conffile, "w+");
		chmod($conffile,0755);
		fclose($fh);
		if(file_exists($conffile)) {
			
			include 'codefile.php';
			if(file_put_contents($conffile, $code)) {
			
				// Create database tables
				if($sql = file_get_contents('db.sql')) {
				
					// Execute sql
					try {
					$query = $conn->exec($sql);
					$fileanddbok = true;
					} catch(PDOException $e) {
						$error[] = _("Unable to transfer tables to database:") . $_POST['db_name']  . '<br>Error message: ' . $e->getMessage();
					}
					
					if($fileanddbok) {
						
						// Install to page as tab
						# https://www.facebook.com/dialog/pagetab?app_id=YOUR_APP_ID&amp;next=YOUR_URL(app canvas url)
						echo ("<script> top.location.href='https://www.facebook.com/dialog/pagetab?app_id=".trim($_POST['app_id'])."&next=".trim($_POST['fb_app'])."'</script>");
					}
				}
			
				
			}
			
		}
					
	}
}
?>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" media="screen">
<!-- Javascripts -->
<script src="../js/jquery.js" type="text/javascript"></script>
<script src="../bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
</head>
<body>
<div class="container">
	
	<h3 class="text-center"><?php echo _("Viral Lottery Installation");?></h3>
    
    <form method="post" action="">
 	  
      <div class="span5 thumbnail">
      <legend><?php echo _("Database");?></legend>
      
        <label><?php echo _("Server Name");?></label>
        <input type="text" name="server_name" class="input-block-level" placeholder="Usually: localhost" value="<?php echo (isset($_POST['server_name'])) ? $_POST['server_name'] : '';?>">
        
        <label ><?php echo _("Database Name");?></label>
        <input type="text" name="db_name" class="input-block-level" value="<?php echo (isset($_POST['db_name'])) ? $_POST['db_name'] : '';?>">
        
        <label  ><?php echo _("Username");?></label>
        <input type="text" name="db_user" class="input-block-level" value="<?php echo (isset($_POST['db_user'])) ? $_POST['db_user'] : '';?>">
        
        <label ><?php echo _("Password");?></label>
        <input type="text" name="db_pass" class="input-block-level" value="<?php echo (isset($_POST['db_pass'])) ? $_POST['db_pass'] : '';?>">
      </div>  
      
      <div class="span6 thumbnail">
      <legend><?php echo _("Facebook");?></legend>
            
        <label ><?php echo _("App ID");?></label>
        <input type="text" name="app_id" class="input-block-level" value="<?php echo (isset($_POST['app_id'])) ? $_POST['app_id'] : '';?>">
        
        <label ><?php echo _("App SECRET");?></label>
        <input type="text" name="app_sec" class="input-block-level" value="<?php echo (isset($_POST['app_sec'])) ? $_POST['app_sec'] : '';?>">
        
        <label ><?php echo _("Facebook Fanpage URL");?></label>
        <input type="text" name="fb_page" class="input-block-level" placeholder="https://www.facebook.com/YOUR_FAN_PAGE/" value="<?php echo (isset($_POST['fb_page'])) ? $_POST['fb_page'] : '';?>">
        
        <label ><?php echo _("Facebook App URL");?></label>
        <input type="text" name="fb_app" class="input-block-level" placeholder="https://apps.facebook.com/YOUR_APP_NAME" value="<?php echo (isset($_POST['fb_app'])) ? $_POST['fb_app'] : '';?>">
        
        <label ><?php echo _("Uploaded App Files path");?></label>
        <input type="text" name="app_files_url" class="input-block-level" placeholder="https://yourdomain.com/appfiles/" value="<?php echo (isset($_POST['app_files_url'])) ? $_POST['app_files_url'] : '';?>">
      </div>
      <div class="clearfix"></div>
      <hr>
      <p><?php echo _("Attention! All URL-s must be secure, with https:// prefix. Forward slash at the end of URL doesn't hurt too!");?></p>
      <div class="span10 text-center"> 
      
		  <?php 
          if(is_array($error)) {
              foreach($error as $err) {
                echo '<div class="alert alert-error">' . $err . '</div>';	
              }
          }?>
        
          <button type="submit" name="install" class="btn btn-large btn-success"><?php echo _("Install Application");?></button>
      </div>
    </form>

</div>
</body>
</html>