<?php // pineapple_login.php

  // force https, for web server when live (sikp if development server.
  if( substr_compare($_SERVER['HTTP_HOST'], 'localhost', 0, 9) != 0 )
  {
    if($_SERVER["HTTPS"] != "on"){
        header("Location: https://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
        exit();
    }
  }

	require_once "./pineapple.php";		// this file has the menu, header and css code for the site
	require_once "./mysql/helper.php";	// this is general purpose mysql helper code, code work anywhere

	// This line renders the menu and header for the site
	pineapple_html_begin("Pineapple PNP", "Pineapple Login");

  //print "<pre>";
  //print_r($_SERVER);
  //print "</pre>";
  
	if( isset($_POST['email']) )
		$email = $_POST['email'];
	elseif( isset($_SESSION['email']) )
		$email = $_SESSION['email'];
	
	if( isset($_POST['password']) )
		$password = $_POST['password'];
	elseif( isset($_SESSION['password']) )
		$password = $_SESSION['password'];

  // No attempt at login has occrued, present user with login page
	if(!isset($email)) {
		?>
      <p><form method="post" action="<?=$_SERVER['PHP_SELF']?>">
        E-Mail Address: <input type="text" name="email" size="8" /><br />
        Password: <input type="password" name="password" SIZE="8" /><br />
      <input type="submit" value="Log in" />
      </form></p>
      <a href="pineapple_forgot_password.php">I forgot my password</a><br />
      <a href="pineapple_signup.php">Create new account</a>
		<?php
	}
  else
  {
    // If we are not loging out, check to see if user name and password are correct
    if( !isset($_GET['logout']) )
    {
      // Lookup submitted user_email and password
      $db = db_connect("pontech_pineapplepnp");
      $sql = "SELECT * FROM user WHERE email = ? AND password = PASSWORD(?)";
      // execute sql statement
      $sth = $db->prepare($sql);
      $sth->execute(array($email, $password));

      // get row count
      $num_rows = $sth->rowCount();
      $row = $sth->fetch(PDO::FETCH_BOTH);

      if ($num_rows == 0) {
        ?>
        <h1> Access Denied </h1>
        <p>Your email or password is incorrect, or you are not a
           registered user on this site. To try logging in again, click
           <a href="<?=$_SERVER['PHP_SELF']?>">here</a>. To register for instant
           access, click <a href="pineapple_signup.php">here</a>.</p>
        <?php
      }
      elseif ($num_rows == 1) {
        // Set session vars if user account found
        $_SESSION['email'] = $email;
        $_SESSION['password'] = $password;
        $_SESSION['admin'] = $row['admin'];
        // todo: set session variable with permisions
        ?>
        <h1> Access Granted </h1>
        <?php
      }
    }
    else
    {
      unset($_SESSION['email']);
      unset($_SESSION['password']);
      unset($_SESSION['admin']);
      ?>
      <h1> You have been logged out </h1>
      <?php
    }
  }

exit_page:
  // This is the end of the HTML body, scripting starts after this.
  pineapple_script_end();
?>
  <script>
    // Reload the menus using AJAX after the user has logged in.
    url = "pineapple_menu.php";
    //$.get(url, function(data, status) {
    //  // Remove the old menu
		//	$("#pineapple_menu").remove();
    //  // Install the new menu
		//	$("#pineapple_menu_reload").html(data);  // Data is the html returned from .get
		//});
  </script>
<?php
	// This is the end of the HTML file, scripting starts after this.
  pineapple_html_end();

//$username = $result->fetchColumn(3);
?>