#!/usr/bin/python

# Import modules for CGI handling 
import cgi, cgitb 

# Import modules for filename reference
import sys
from os import path

# Create instance of FieldStorage 
form = cgi.FieldStorage() 

# Get data from fields
placename = form.getvalue('placename')

print """
#Content-type:text/html\r\n\r\n 
<html>
<head>
<title>Place test</title>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>

<?php
 
// grab recaptcha library
require_once "recaptchalib.php";

# print POST values
  foreach ($_POST as $key => $value) {
    echo '<p><strong>' . $key.':</strong> '.$value.'</p>';
  }

// your secret key
$secret = "6LekBEYUAAAAAFp4agLQPEa5LuPedzKDR9peydu9";
// empty response
$response = null;
// check secret key
$reCaptcha = new ReCaptcha($secret);

// if submitted check response
if ($_POST["g-recaptcha-response"]) {
    $response = $reCaptcha->verifyResponse(
        $_SERVER["REMOTE_ADDR"],
        $_POST["g-recaptcha-response"]
    );
}

 
?>

# the %s in the following line will be replaced with placename
<h2>Tell us about the sustainability practices at %s!</h2>

<?php
  if ($response != null && $response->success) {
    echo "Hi " . $_POST["name"] . " (" . $_POST["email"] . "), thanks for submitting the form!";
  } else {
?>
# the %s in the following line will be replaced with the name of this file
    <form action="cgi-bin/placeposttest.py" method="post">
      <label for="name">Name:</label>
      <input name="name" required><br />
      <label for="email">Email:</label>
      <input name="email" type="email" required><br />
      <div class="g-recaptcha" data-sitekey="6LekBEYUAAAAAC3eXEwbRQIAon-h3giSc9veasIz"></div> 
      <input type="submit" value="Submit" /> 
    </form>
<?php { ?>

</body>
</html>

""" % (placename)
# """ % (placename, path.basename(sys.argv[0]))
