Completed
Pull Request — master (#1925)
by
unknown
14:46
created

invite.php ➔ invitationExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 10
ccs 0
cts 8
cp 0
crap 6
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
if ( elgg_is_active_plugin('web_services') && elgg_is_active_plugin('gcRegistration_invitation') && elgg_get_config('allow_registration') ) {
4
	
5
	elgg_ws_expose_function(
6
		"gc.invite",
7
		"talent_cloud_invite",
8
		array(
9
			"email" => array('type' => 'string', 'required' => true, 'description' => 'Email address of user to invite'),
10
			"en_message" => array('type' => 'string', 'required' => false, 'description' => 'English personalized message to incude in invite'),
11
			"fr_message" => array('type' => 'string', 'required' => false, 'description' => 'French personalized message to incude in invite'),
12
		),
13
		'Invites Talent Cloud user to register for GCcollab Account',
14
		'POST',
15
		true,
16
		false
17
	);
18
19
	function talent_cloud_invite($email, $en_message = NULL, $fr_message = NULL) {
20
		
21
		// Security validation on input parameters from API
22
23
		$email = test_input($email);
24
		$en_message = test_input($en_message);
25
		$fr_message = test_input($fr_message);
26
27
		// Secondary validation for valid email
28
29
		$validation_errors = array();
30
		if(!is_email_address($email)){
31
			return json_encode(array('success'=>'false', 'errors'=>'The email submitted is not a valid email'));
32
		};
33
34
	    // Create TalentCloud user if they don't exist
35
36
		$api_user = get_user_by_username('TalentCloud');
37
38
		if (!$api_user){;
39
			$api_user = register_user('TalentCloud',substr(str_shuffle(md5(time())),0,10), 'TalentCloud', '[email protected]', false);
40
		};
41
42
		// Check to see if invitaiton exists, if yes then we only need to send email and not register address again
43
44 View Code Duplication
		if (!invitationExists($email)){			
45
			$data = array('inviter' => $api_user->guid, 'emails' => [$email]);
46
			elgg_trigger_plugin_hook('gcRegistration_email_invitation', 'all', $data);
47
		};
48
		
49
		// Get site variables to construct email message
50
51
		$site = elgg_get_site_entity();
52
53
		// Set custom personalized message
54
		// Check priority order - API first / Settings second / Hardcode default third
55
	
56
		if ($en_message){
57
			$emailmessage_en = $en_message;
58
		} else {
59
			$emailmessage_en = elgg_get_plugin_setting('en_message','talent_cloud_invite_api');
60
			if (!$emailmessage_en){
61
				$emailmessage_en = 'Personalized message from GC Talent Cloud';
62
			};	
63
		};
64
		
65
		if ($fr_message){
66
			$emailmessage_fr = $fr_message;
67
		} else {
68
			$emailmessage_fr = elgg_get_plugin_setting('fr_message','talent_cloud_invite_api');
69
			if (!$emailmessage_fr){
70
				$emailmessage_fr = 'Message personalisé de Nuage de talents du GC';
71
			};
72
		};
73
74
		$link_en = elgg_get_plugin_setting('en_link', 'talent_cloud_invite_api');
75
		$link_fr = elgg_get_plugin_setting('fr_link', 'talent_cloud_invite_api');
76
77
		if (!$link_en){
78
			$link_en = "<a href='https://account.gccollab.ca/register/'>GCcollab Account Registration</a>";
79
		};
80
		if (!$link_fr){
81
			$link_fr = "<a href='https://account.gccollab.ca/register/'>Compte GCcollab pour créer un compte</a>";
82
		}
83
84
		$subject = elgg_echo('cp_notify:subject:invite_new_user',array(),'en') . ' | ' . elgg_echo('cp_notify:subject:invite_new_user',array(),'fr');
85
86
		$cp_notify_msg_title_en = elgg_echo('cp_notify:body_invite_new_user:title', array(elgg_get_plugin_setting('en_name', 'talent_cloud_invite_api')),'en');
87
		$cp_notify_msg_title_fr = elgg_echo('cp_notify:body_invite_new_user:title', array(elgg_get_plugin_setting('fr_name', 'talent_cloud_invite_api')),'fr');
88
89
		$cp_notify_msg_description_en = elgg_echo('cp_notify:body_invite_new_user:description',array(elgg_get_plugin_setting('en_name', 'talent_cloud_invite_api'), $emailmessage_en, $link_en),'en');
90
		$cp_notify_msg_description_fr = elgg_echo('cp_notify:body_invite_new_user:description',array(elgg_get_plugin_setting('fr_name', 'talent_cloud_invite_api'), $emailmessage_fr, $link_fr),'fr');
91
92
		$email_notification_header = elgg_echo('cp_notification:email_header',array(),'en') . ' | ' . elgg_echo('cp_notification:email_header',array(),'fr');
93
94
		$french_follows = elgg_echo('cp_notify:french_follows',array());
95
96
		$current_year = date('Y');
97
		$current_site = elgg_get_site_entity()->name;
98
99
		$template = elgg_echo("
100
			<html>
101
			<body>
102
				<!-- beginning of email template -->
103
				<div width='100%' bgcolor='#fcfcfc'>
104
					<div>
105
						<div>
106
			
107
							<div align='center' width='100%' style='background-color:#f5f5f5; padding:20px 30px 15px 30px; font-family: sans-serif; font-size: 12px; color: #79579D'>
108
								{$email_notification_header}
109
							</div>
110
			
111
							<div width='100%' style='padding: 0 0 0 10px; color:#ffffff; font-family: sans-serif; font-size: 35px; line-height:38px; font-weight: bold; background-color:#46246A;'>
112
								<span style='padding: 0 0 0 3px; font-size: 20px; color: #ffffff; font-family: sans-serif;'>GCcollab</span>
113
							</div>
114
			
115
							<div style='height:1px; background:#bdbdbd; border-bottom:1px solid #ffffff'></div>
116
			
117
							<div width='100%' style='padding:30px 30px 10px 30px; font-size:12px; line-height:22px; font-family:sans-serif;'>
118
			
119
								<span style='font-size:12px; font-weight: normal;'>{$french_follows}</span><br/>
120
			
121
							</div>
122
			
123
			
124
			
125
							<div width='100%' style='padding:30px 30px 30px 30px; color:#153643; font-family:sans-serif; font-size:16px; line-height:22px; '>
126
			
127
								<h4 style='padding: 0px 0px 5px 0px; font-family:sans-serif';>
128
									<strong> {$cp_notify_msg_title_en} </strong>
129
								</h4>
130
			
131
								{$cp_notify_msg_description_en}
132
			
133
							</div>
134
							<div style='margin-top:15px; padding: 5px; color: #6d6d6d; border-bottom: 1px solid #ddd;'>
135
							</div>
136
			
137
							<div width='100%' style='padding:30px 30px 30px 30px; color:#153643; font-family:sans-serif; font-size:16px; line-height:22px;'>
138
			
139
								<h4 style='padding: 0px 0px 5px 0px; font-family:sans-serif;'>
140
									<strong> {$cp_notify_msg_title_fr} </strong>
141
								</h4>
142
			
143
			
144
								{$cp_notify_msg_description_fr}
145
			
146
							</div>
147
								<div style='margin-top:15px; padding: 5px; color: #6d6d6d;'>
148
							</div>
149
			
150
							<div style='height:1px; background:#bdbdbd; border-bottom:1px solid #ffffff'></div>
151
			
152
							<div align='center' width='100%' style='background-color:#f5f5f5; padding:20px 30px 15px 30px; font-family: sans-serif; font-size: 16px; color: #79579D'>
153
								{$current_site} © {$current_year}
154
							</div>
155
						</div>
156
					</div>
157
				</div>
158
			</body>
159
			</html>");
160
161
		$result = phpmailer_send($email, $email, $subject, $template, NULL, true);
162
163
		if ($result){
164
			return json_encode(array('success'=>'true'));
165
		} else {
166
			return json_encode(array('success'=>'false', 'error'=>'Error trying to send email'));
167
		}
168
169
	};
170
171
	function invitationExists($emailaddress){
172
		$query = "SELECT * FROM email_invitations WHERE `email` = '" . $emailaddress . "'";
173
		$result = get_data($query);
174
		if (count($result) > 0){
175
			return true;
176
		} else{
177
			return false;
178
		};
179
180
	};
181
182
	function test_input($data) {
183
		$data = trim($data);
184
		$data = stripslashes($data);
185
		$data = htmlspecialchars($data);
186
		return $data;
187
	};
188
		
189
};