aboutsummaryrefslogtreecommitdiff
path: root/mod/invitefriends/actions/invite.php
blob: d942bd36c4c3a52abe75f657521c46c58579a3a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php

/**
 * Elgg invite friends action
 *
 * @package ElggInviteFriends
 */

$emails = get_input('emails');
$emailmessage = get_input('emailmessage');

$emails = trim($emails);
if (strlen($emails) > 0) {
	$emails = preg_split('/\\s+/', $emails, -1, PREG_SPLIT_NO_EMPTY);
}

if (!is_array($emails) || count($emails) == 0) {
	register_error(elgg_echo('invitefriends:noemails'));
	forward($_SERVER['HTTP_REFERER']);
}

$current_user = get_loggedin_user();

$error = FALSE;
$bad_emails = array();
$already_members = array();
$sent_total = 0;
foreach ($emails as $email) {

	$email = trim($email);
	if (empty($email)) {
		continue;
	}

	// send out other email addresses
	if (!is_email_address($email)) {
		$error = TRUE;
		$bad_emails[] = $email;
		continue;
	}

	if (get_user_by_email($email)) {
		$error = TRUE;
		$already_members[] = $email;
		continue;
	}

	$link = $CONFIG->wwwroot . 'pg/register?friend_guid=' . $current_user->guid . '&invitecode=' . generate_invite_code($current_user->username);
	$message = sprintf(elgg_echo('invitefriends:email'),
					$CONFIG->site->name,
					$current_user->name,
					$emailmessage,
					$link
	);

	$subject = sprintf(elgg_echo('invitefriends:subject'), $CONFIG->site->name);

	// create the from address
	$site = get_entity($CONFIG->site_guid);
	if (($site) && (isset($site->email))) {
		$from = $site->email;
	} else {
		$from = 'noreply@' . get_site_domain($CONFIG->site_guid);
	}

	elgg_send_email($from, $email, $subject, $message);
	$sent_total++;
}

if ($error) {
	register_error(sprintf(elgg_echo('invitefriends:invitations_sent'), $sent_total));

	if (count($bad_emails) > 0) {
		register_error(sprintf(elgg_echo('invitefriends:email_error'), implode(', ', $bad_emails)));
	}

	if (count($already_members) > 0) {
		register_error(sprintf(elgg_echo('invitefriends:already_members'), implode(', ', $already_members)));
	}
	
} else {
	system_message(elgg_echo('invitefriends:success'));
}

forward($_SERVER['HTTP_REFERER']);