1<?php 2/** 3 * Elgg invite friends action 4 */ 5 6use Elgg\Email; 7 8elgg_make_sticky_form('invitefriends'); 9 10if (!elgg_get_config('allow_registration')) { 11 return elgg_error_response(elgg_echo('invitefriends:registration_disabled')); 12} 13 14$site = elgg_get_site_entity(); 15// create the from address 16$from = \Elgg\Email\Address::getFormattedEmailAddress($site->getEmailAddress(), $site->getDisplayName()); 17 18$emails = get_input('emails'); 19$emailmessage = get_input('emailmessage'); 20 21$emails = trim($emails); 22if (elgg_strlen($emails) > 0) { 23 $emails = preg_split('/\\s+/', $emails, -1, PREG_SPLIT_NO_EMPTY); 24} 25 26if (!is_array($emails) || count($emails) == 0) { 27 return elgg_error_response(elgg_echo('invitefriends:noemails')); 28} 29 30$current_user = elgg_get_logged_in_user_entity(); 31 32$error = false; 33$bad_emails = []; 34$already_members = []; 35$sent_total = 0; 36foreach ($emails as $email_address) { 37 $email_address = trim($email_address); 38 if (empty($email_address)) { 39 continue; 40 } 41 42 // send out other email addresses 43 if (!is_email_address($email_address)) { 44 $error = true; 45 $bad_emails[] = $email_address; 46 continue; 47 } 48 49 if (get_user_by_email($email_address)) { 50 $error = true; 51 $already_members[] = $email_address; 52 continue; 53 } 54 55 $link = elgg_get_registration_url([ 56 'friend_guid' => $current_user->guid, 57 'invitecode' => generate_invite_code($current_user->username), 58 ]); 59 60 $email = Email::factory([ 61 'to' => $email_address, 62 'from' => $from, 63 'subject' => elgg_echo('invitefriends:subject', [$site->getDisplayName()]), 64 'body' => elgg_echo('invitefriends:email', [ 65 $site->getDisplayName(), 66 $current_user->getDisplayName(), 67 $emailmessage, 68 $link, 69 ]), 70 ]); 71 72 elgg_send_email($email); 73 $sent_total++; 74} 75 76if ($error) { 77 register_error(elgg_echo('invitefriends:invitations_sent', [$sent_total])); 78 79 if (count($bad_emails) > 0) { 80 register_error(elgg_echo('invitefriends:email_error', [implode(', ', $bad_emails)])); 81 } 82 83 if (count($already_members) > 0) { 84 register_error(elgg_echo('invitefriends:already_members', [implode(', ', $already_members)])); 85 } 86 87 return elgg_error_response(); 88} 89 90elgg_clear_sticky_form('invitefriends'); 91 92return elgg_ok_response('', elgg_echo('invitefriends:success')); 93