functions.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * All helper functions are bundled here
  4. */
  5. /**
  6. * Generate a validation token
  7. *
  8. * @param string $type what kind of token
  9. * @param int $user_guid the user_guid to generate for
  10. *
  11. * @return bool|string
  12. */
  13. function acount_removal_generate_confirm_token($type, $user_guid) {
  14. $result = false;
  15. if (!empty($user_guid) && ($user = get_user($user_guid)) && in_array($type, array("remove", "disable"))) {
  16. $site_secret = get_site_secret();
  17. $user_salt = $user->salt;
  18. $result = md5($site_secret . $user_guid . $type . $user_salt);
  19. }
  20. return $result;
  21. }
  22. /**
  23. * Validate a token for account removal
  24. *
  25. * @param string $token the token to validate
  26. * @param string $type what kind of token
  27. * @param int $user_guid the user_guid to validate for
  28. *
  29. * @return bool
  30. */
  31. function acount_removal_validate_confirm_token($token, $type, $user_guid) {
  32. $result = false;
  33. if (!empty($user_guid) && !empty($token) && in_array($type, array("remove", "disable"))) {
  34. $new_token = acount_removal_generate_confirm_token($type, $user_guid);
  35. if ($token === $new_token) {
  36. $result = true;
  37. }
  38. }
  39. return $result;
  40. }
  41. /**
  42. * Send a notification to the user for confirmation of account removal
  43. *
  44. * @param string $type what kind of removal
  45. * @param int $user_guid the user_guid to send the notification to
  46. *
  47. * @return bool
  48. */
  49. function account_removal_send_notification($type, $user_guid) {
  50. $result = false;
  51. $site = elgg_get_site_entity();
  52. if (!empty($user_guid) && ($user = get_user($user_guid)) && in_array($type, array("remove", "disable"))) {
  53. $token = acount_removal_generate_confirm_token($type, $user_guid);
  54. $url = elgg_get_site_url() . "account_removal/" . $type . "/?confirm_token=" . $token;
  55. $subject = elgg_echo("account_removal:message:" . $type . ":subject", array($site->name));
  56. $message = elgg_echo("account_removal:message:" . $type . ":body", array($user->name, $url));
  57. notify_user($user_guid, $site->getGUID(), $subject, $message, null, "email");
  58. $result = true;
  59. }
  60. return $result;
  61. }
  62. /**
  63. * Send a thank you notification after account removal
  64. *
  65. * @param string $type what kind of removal
  66. * @param int $user_guid the user_guid to send the notification to
  67. *
  68. * @return bool
  69. */
  70. function account_removal_send_thank_notification($type, $user_guid) {
  71. $result = false;
  72. $site = elgg_get_site_entity();
  73. if (!empty($user_guid) && ($user = get_user($user_guid)) && in_array($type, array("remove", "disable"))) {
  74. $subject = elgg_echo("account_removal:message:thank_you:" . $type . ":subject", array($site->name));
  75. $message = elgg_echo("account_removal:message:thank_you:" . $type . ":body", array($user->name, $site->name));
  76. notify_user($user_guid, $site->getGUID(), $subject, $message, null, "email");
  77. $result = true;
  78. }
  79. return $result;
  80. }