You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. // reset error message
  3. unset($_SESSION['errormsg']);
  4. // Database class for DB operations. These work, they are NOT been tampered with.
  5. // Do not change this class.
  6. class MyDB extends SQLite3
  7. {
  8. function __construct()
  9. {
  10. $this->open('./dbase/comments.db');
  11. }
  12. function savecomment($parent_id, $name, $email, $comment)
  13. {
  14. $this->exec("INSERT INTO comments (parent_id, author, email, comment, published) values(" . $parent_id . ", '" . $name . "', '" . $email . "', '" . $comment . "', 1);");
  15. $rowid = $this->lastInsertRowID();
  16. $res = $this->query("select hierarchy from comments where id=" . $parent_id);
  17. $hierarchy = $res->fetchArray()['hierarchy'];
  18. $nh = $hierarchy . "-" . $rowid;
  19. $this->exec("update comments set hierarchy='" . $nh . "' where id=" . $rowid);
  20. }
  21. }
  22. // cleanup the user input. Courtesy of w3schools. This function is correct
  23. function test_input($data)
  24. {
  25. $data = trim($data);
  26. $data = stripslashes($data);
  27. $data = htmlspecialchars($data);
  28. return $data;
  29. }
  30. //Validate the name, set error and return to index if necessary.
  31. // only letters and spaces are allowed`;
  32. function validate_name($name)
  33. {
  34. $name = test_input($name);
  35. if (!preg_match("/^[a-zA-Z ]*$/", $name)) {
  36. // https://www.w3schools.com/php/func_regex_preg_match.asp
  37. ...
  38. }
  39. }
  40. // Validate the email, set error message and return to index if necessary
  41. function validate_email($email)
  42. {
  43. $email = test_input($email);
  44. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  45. // https://www.w3schools.com/php/php_form_url_email.asp
  46. ...
  47. }
  48. return $email;
  49. }
  50. // comments should be checked for javascript and url,
  51. // but we just convert all non alphanumerics to url encoding to prevent
  52. // sql injection
  53. // we set cookies for the name and emailaddress
  54. // likely, the same user might be entering more comments
  55. setcookie("commentname", $_POST['name'], time() + 3600);
  56. setcookie("commentemail", $_POST['email'], time() + 3600);
  57. // new database object
  58. $db = new MyDB();
  59. // validate the user input
  60. $name = validate_name($_POST['name']);
  61. $email = validate_email($_POST['email']);
  62. $comment = validate_comment($_POST['comment']);
  63. // parent_id = stored id.
  64. if (isset($_SESSION['parent_id'])) {
  65. $parent_id = $_SESSION['parent_id'];
  66. } else {
  67. $parent_id = 0;
  68. }
  69. // save in database and clean up
  70. if ($db => savecomment($parent_id, $name, $email, $comment)) {
  71. session_unset();
  72. session_destroy();
  73. }
  74. ?>
  75. <html>
  76. <head>
  77. <link rel="stylesheet" href="./css/bootstrap.min.css">
  78. <link rel="stylesheet" href="./style/style.css">
  79. <script src="./js/bootstrap.min.js"></script>
  80. </head>
  81. <body>
  82. <div id="wrapwrap">
  83. <div class="container">
  84. <div id="header" class="row">
  85. <h1>Examen PHP: &lt; Vul hier je naam in &gt; </h1>
  86. </div>
  87. <div id="main" class="row">
  88. <h1>Thank you for your comment</h1>
  89. <hr/>
  90. <a href="index.php">Return to homepage</a>
  91. </div>
  92. </div>
  93. </html>