<?php // pineapple_login.php

  // force https, for web server when live (sikp if development server.
  if( $_SERVER['HTTP_HOST'] != 'localhost:8000' )
  {
    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
  require_once "./twilio-php/Services/Twilio.php"; // Twilio.com php API

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

  //print "<pre>";
  //print_r($_POST);
  //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)) {
		?>
      You must be logged in to use this service
		<?php
	}
  else
  {
    if( $_SESSION['admin'] == '0' ) {
		?>
      You must have admin rights to use this page.
		<?php
    }
    else{
      if( !isset($_POST['phone']) ){
        ?>
          <p><form method="post" action="<?=$_SERVER['PHP_SELF']?>">
            phone number: <input type="text" name="phone" size="8" /><br />
            Message: <input type="text" name="message" SIZE="8" /><br />
          <input type="submit" value="Send" />
          </form></p>
        <?php
      }
      else
      {
        if( $_SERVER['HTTP_HOST'] != 'localhost:8000' )
        {
          // Twilio Live Account
          $account_sid = 'AC68bfd174f7b80b767f9db92fd3618faa';
          $auth_token = '45bf4155f52a60397d726869bd5b091c';          
        }
        else
        {
          // Twilio Test Account 
          $account_sid = 'ACf083d7ff296771cbec32a9b8cfec2890';
          $auth_token = 'effabd6ec18f9f859020659851612f01';
        }

        $client = new Services_Twilio($account_sid, $auth_token); 
        $from_phone = '+19094816672';  // pontech.com land line
        
        //Your account verification code for pineapple pick and place, is, 1, 2, 3, 4.  That number again is, 1, 2, 3, 4.
        //$data = array('say'=>'Welcome to pineapple pick and place. Your verification code is, 1, 2, 3, 4,')
        $data = array('say'=>$_POST['message']);
        //print_r($data);
        //print "<br \>";

        $url = "http://pineapplepnp.com/twilio_twiml.php?" . http_build_query($data);

        //print $url;

        print "<h1> Calling with Verification Code<h1>";
        
        $call = $client->account->calls->create($from_phone, $_POST['phone'], $url, array( 
          'Method' => 'GET',  
          'FallbackMethod' => 'GET',  
          'StatusCallbackMethod' => 'GET',    
          'Record' => 'false', 
        ));

        print "<h1> Calling Complete<h1>";
        //print "<br \>";
        //print_r($call);
      }
    }
  }

exit_page:
  // This is the end of the HTML body, scripting starts after this.
  pineapple_script_end();
?>
  <script>
  </script>
<?php
	// This is the end of the HTML file, scripting starts after this.
  pineapple_html_end();

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