share-canvas.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. /* Receives a recipient email address and sends him a previously generated canvas in PDF on behalf of the user that is signed in */
  3. session_start();
  4. if(!isset($_POST['share_email']) || !isset($_SESSION['userlogin'])) {
  5. // Recipient or sender emails not present
  6. echo 400;
  7. }
  8. else {
  9. $params = array();
  10. parse_str($_POST['share_email'], $params);
  11. if(!array_key_exists('share-canvas-email', $params)) {
  12. echo 400; // Missing parameters
  13. }
  14. else { // All parameters received
  15. $senderEmail = $_SESSION['userlogin'];
  16. $recipientEmail = $params['share-canvas-email'];
  17. $path = "../saved-pdf/Ethics-Canvas.pdf";
  18. require_once('../../php/db_utils.php');
  19. $conn = db_connect(); // Connect to the database
  20. if(!($result = mysqli_query($conn, "SELECT name FROM user WHERE username = '$senderEmail'"))) {
  21. echo 400; // Wrong query
  22. }
  23. else if(mysqli_num_rows($result) != 1) { // User not registered or duplicated
  24. echo 401;
  25. }
  26. else { // Share canvas
  27. $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
  28. $senderName = $row['name'];
  29. // Share canvas email
  30. $shareEmail = "Hello,<br><br>$senderName ($senderEmail) wanted to share with you the attached Ethics Canvas generated at <a href='https://ethicscanvas.org' target='_new'>EthicsCanvas.org</a>.<br><br>Kind regards,<br><br>The Ethics Canvas Team";
  31. // Send activation email
  32. include('../../php/mailer.php');
  33. smtpmailer($recipientEmail, "$senderName shared this Ethics Canvas with you", $shareEmail, $path);
  34. echo 200;
  35. }
  36. mysqli_free_result($result);
  37. db_close($conn); // Close the database
  38. }
  39. }
  40. ?>