SHARE
TWEET

Untitled

a guest Feb 20th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. include("get_section_problem.php");
  3. date_default_timezone_set('America/Los_Angeles');
  4. $METHOD = $_SERVER['REQUEST_METHOD'];
  5. if ($METHOD == "GET") {
  6.     output_form();
  7. } else if ($METHOD == "POST") {
  8.     assert_params();
  9.     submit();
  10. }
  11.  
  12. function output_form() {
  13.     $UWNETID = $_SERVER["PHP_AUTH_USER"];
  14.  
  15.     global $SECTION;
  16.     global $SECTION_NUMBER;
  17.  
  18.     $SECTION = $SECTION;
  19.     $SECTION_NUMBER = $SECTION_NUMBER;
  20. ?>
  21. <!DOCTYPE html><html><head><title>CSE 154 -- <?=$SECTION?> section<?=$SECTION_NUMBER?> ONLINE HW TURNIN</title></head><body>
  22.     <h1>Welcome, <?=$UWNETID?>!</h1>
  23.     <p>This is the section <?=$SECTION?> online homework #<?=$SECTION_NUMBER?> turnin.</p>
  24.     <div style="border:3px dashed black;margin:15px;">
  25.         <?php
  26.         global $SECTION_PROBLEM_FILE;
  27.         print get_section_problem($SECTION_NUMBER);
  28.         ?>
  29.     </div>
  30.     <form method="POST">
  31.         <textarea style="width:100%;height:200px;" name="content">Enter section problem solution here!</textarea>
  32.         <button id="submit">Submit</button>
  33.         <input type="hidden" value="<?=$UWNETID?>" name="uwnetid" />
  34.         <input type="hidden" name="section" value="<?=$SECTION?>" />
  35.         <input type="hidden" name="section_number" value="<?=$SECTION_NUMBER?>" />
  36.     </form>
  37.  
  38. </body></html>
  39. <?php
  40. }
  41.  
  42. function assert_params() {
  43.     if (!isset($_POST["section"]) ||
  44.         !isset($_POST["section_number"]) ||
  45.         !isset($_POST["uwnetid"]) ||
  46.         !isset($_POST["content"])) {
  47.         die("missing params");
  48.     }
  49.     if (strlen($_POST["section"]) != 2) {
  50.         die("section is two letters (ex: AA, AB, BC, etc...");
  51.     }
  52.     if (!is_numeric($_POST["section_number"])) {
  53.         die("section_number should be numeric");
  54.     }
  55. }
  56.  
  57. function submit() {
  58.     $SERVER = "localhost";
  59.     $USER = "zcava";
  60.     $PASSWORD = "k8UrHxcnDWwr4";
  61.     $DBNAME = "sectCheckin";
  62.     define("DBNAME", $DBNAME, true);
  63.  
  64.     try {
  65.         $DB = new PDO("mysql:dbname=$DBNAME;host=$SERVER", $USER, $PASSWORD);
  66.     } catch (PDOException $err) {
  67.         die("Connection to database failed, alert a TA: ".$err->getMessage());
  68.     }
  69.     // check authentication
  70.     $auth = isset($_SERVER["AUTH_TYPE"]) && $_SERVER["AUTH_TYPE"] == "UWNetID" && isset($_SERVER["PHP_AUTH_USER"]);
  71.     $query = $DB->prepare('INSERT INTO '.DBNAME.'.section (section_number,section,uwnetid,content) VALUES (:section_number,:section,:uwnetid,:content)');
  72.     $query->bindValue(":section"        ,$_POST["section"]);
  73.     $query->bindValue(":uwnetid"        ,$_POST["uwnetid"]);
  74.     $query->bindValue(":content"        ,$_POST["content"]);
  75.     $query->bindValue(":section_number" ,$_POST["section_number"]);
  76.     if(!$query->execute()) {
  77.         print "<h1>FAILURE</h1>";
  78.     } else {
  79.         print "<h1>SUCCESS</h1><p>Your section homework was successfully submitted, ".$_SERVER["PHP_AUTH_USER"]."</p>";
  80.     }
  81. }
  82.  
  83. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top