share-canvas.php 1.7 KB

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