functions.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Helper functions
  4. *
  5. * @package Elgg.Core.Plugin
  6. * @subpackage UserValidationByEmail
  7. */
  8. /**
  9. * Generate an email activation code.
  10. *
  11. * @param int $user_guid The guid of the user
  12. * @param string $email_address Email address
  13. * @return string
  14. */
  15. function uservalidationbyemail_generate_code($user_guid, $email_address) {
  16. // Note: binding to site URL for multisite.
  17. $site_url = elgg_get_site_url();
  18. return elgg_build_hmac([(int)$user_guid, $email_address, $site_url])->getToken();
  19. }
  20. /**
  21. * Request user validation email.
  22. * Send email out to the address and request a confirmation.
  23. *
  24. * @param int $user_guid The user's GUID
  25. * @param bool $admin_requested Was it requested by admin
  26. * @return mixed
  27. */
  28. function uservalidationbyemail_request_validation($user_guid, $admin_requested = 'deprecated') {
  29. if ($admin_requested != 'deprecated') {
  30. elgg_deprecated_notice('Second param $admin_requested no more used in uservalidationbyemail_request_validation function', 1.9);
  31. }
  32. $site = elgg_get_site_entity();
  33. $user_guid = (int)$user_guid;
  34. $user = get_entity($user_guid);
  35. if (($user) && ($user instanceof ElggUser)) {
  36. // Work out validate link
  37. $code = uservalidationbyemail_generate_code($user_guid, $user->email);
  38. $link = "{$site->url}uservalidationbyemail/confirm?u=$user_guid&c=$code";
  39. // Get email to show in the next page
  40. elgg_get_session()->set('emailsent', $user->email);
  41. $subject = elgg_echo('email:validate:subject', array(
  42. $user->name,
  43. $site->name
  44. ), $user->language
  45. );
  46. $body = elgg_echo('email:validate:body', array(
  47. $user->name,
  48. $site->name,
  49. $link,
  50. $site->name,
  51. $site->url
  52. ), $user->language
  53. );
  54. // Send validation email
  55. $result = notify_user($user->guid, $site->guid, $subject, $body, array(), 'email');
  56. return $result;
  57. }
  58. return FALSE;
  59. }
  60. /**
  61. * Validate a user
  62. *
  63. * @param int $user_guid
  64. * @param string $code
  65. * @return bool
  66. */
  67. function uservalidationbyemail_validate_email($user_guid, $code) {
  68. $user = get_entity($user_guid);
  69. $site_url = elgg_get_site_url();
  70. $matches = elgg_build_hmac([(int)$user_guid, $user->email, $site_url])->matchesToken($code);
  71. if (!$matches) {
  72. return false;
  73. }
  74. return elgg_set_user_validation_status($user_guid, true, 'email');
  75. }
  76. /**
  77. * Return a where clause to get entities
  78. *
  79. * "Unvalidated" means metadata of validated is not set or not truthy.
  80. * We can't use elgg_get_entities_from_metadata() because you can't say
  81. * "where the entity has metadata set OR it's not equal to 1".
  82. *
  83. * @return array
  84. */
  85. function uservalidationbyemail_get_unvalidated_users_sql_where() {
  86. $db_prefix = elgg_get_config('dbprefix');
  87. $validated_id = elgg_get_metastring_id('validated');
  88. $one_id = elgg_get_metastring_id('1');
  89. // thanks to daveb@freenode for the SQL tips!
  90. $wheres = array();
  91. $wheres[] = "e.enabled='no'";
  92. $wheres[] = "NOT EXISTS (
  93. SELECT 1 FROM {$db_prefix}metadata md
  94. WHERE md.entity_guid = e.guid
  95. AND md.name_id = $validated_id
  96. AND md.value_id = $one_id)";
  97. return $wheres;
  98. }