invite.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Elgg invite friends action
  4. *
  5. * @package ElggInviteFriends
  6. */
  7. elgg_make_sticky_form('invitefriends');
  8. if (!elgg_get_config('allow_registration')) {
  9. register_error(elgg_echo('invitefriends:registration_disabled'));
  10. forward(REFERER);
  11. }
  12. $site = elgg_get_site_entity();
  13. $emails = get_input('emails');
  14. $emailmessage = get_input('emailmessage');
  15. $emails = trim($emails);
  16. if (strlen($emails) > 0) {
  17. $emails = preg_split('/\\s+/', $emails, -1, PREG_SPLIT_NO_EMPTY);
  18. }
  19. if (!is_array($emails) || count($emails) == 0) {
  20. register_error(elgg_echo('invitefriends:noemails'));
  21. forward(REFERER);
  22. }
  23. $current_user = elgg_get_logged_in_user_entity();
  24. $error = FALSE;
  25. $bad_emails = array();
  26. $already_members = array();
  27. $sent_total = 0;
  28. foreach ($emails as $email) {
  29. $email = trim($email);
  30. if (empty($email)) {
  31. continue;
  32. }
  33. // send out other email addresses
  34. if (!is_email_address($email)) {
  35. $error = TRUE;
  36. $bad_emails[] = $email;
  37. continue;
  38. }
  39. if (get_user_by_email($email)) {
  40. $error = TRUE;
  41. $already_members[] = $email;
  42. continue;
  43. }
  44. $link = elgg_get_site_url() . 'register?' . http_build_query(array(
  45. 'friend_guid' => $current_user->guid,
  46. 'invitecode' => generate_invite_code($current_user->username),
  47. ));
  48. $message = elgg_echo('invitefriends:email', array(
  49. $site->name,
  50. $current_user->name,
  51. $emailmessage,
  52. $link,
  53. ));
  54. $subject = elgg_echo('invitefriends:subject', array($site->name));
  55. // create the from address
  56. $site = get_entity($site->guid);
  57. if ($site && $site->email) {
  58. $from = $site->email;
  59. } else {
  60. $from = 'noreply@' . $site->getDomain();
  61. }
  62. elgg_send_email($from, $email, $subject, $message);
  63. $sent_total++;
  64. }
  65. if ($error) {
  66. register_error(elgg_echo('invitefriends:invitations_sent', array($sent_total)));
  67. if (count($bad_emails) > 0) {
  68. register_error(elgg_echo('invitefriends:email_error', array(implode(', ', $bad_emails))));
  69. }
  70. if (count($already_members) > 0) {
  71. register_error(elgg_echo('invitefriends:already_members', array(implode(', ', $already_members))));
  72. }
  73. } else {
  74. elgg_clear_sticky_form('invitefriends');
  75. system_message(elgg_echo('invitefriends:success'));
  76. }
  77. forward(REFERER);