qa-event-notify.php 11 KB
Newer Older
Scott committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	Description: Event module for sending notification emails


	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/

Scott committed
22 23 24 25
class qa_event_notify
{
	public function process_event($event, $userid, $handle, $cookieid, $params)
	{
Scott committed
26 27 28
		require_once QA_INCLUDE_DIR . 'app/emails.php';
		require_once QA_INCLUDE_DIR . 'app/format.php';
		require_once QA_INCLUDE_DIR . 'util/string.php';
Scott committed
29 30


Scott committed
31 32
		switch ($event) {
			case 'q_post':
Scott committed
33 34
				$followanswer = @$params['followanswer'];
				$sendhandle = isset($handle) ? $handle : (strlen($params['name']) ? $params['name'] : qa_lang('main/anonymous'));
Scott committed
35

Scott committed
36
				if (isset($followanswer['notify']) && !qa_post_is_by_user($followanswer, $userid, $cookieid)) {
Scott committed
37 38
					$blockwordspreg = qa_get_block_words_preg();
					$sendtext = qa_viewer_text($followanswer['content'], $followanswer['format'], array('blockwordspreg' => $blockwordspreg));
Scott committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

					qa_send_notification($followanswer['userid'], $followanswer['notify'], @$followanswer['handle'], qa_lang('emails/a_followed_subject'), qa_lang('emails/a_followed_body'), array(
						'^q_handle' => $sendhandle,
						'^q_title' => qa_block_words_replace($params['title'], $blockwordspreg),
						'^a_content' => $sendtext,
						'^url' => qa_q_path($params['postid'], $params['title'], true),
					));
				}

				if (qa_opt('notify_admin_q_post'))
					qa_send_notification(null, qa_opt('feedback_email'), null, qa_lang('emails/q_posted_subject'), qa_lang('emails/q_posted_body'), array(
						'^q_handle' => $sendhandle,
						'^q_title' => $params['title'], // don't censor title or content here since we want the admin to see bad words
						'^q_content' => $params['text'],
						'^url' => qa_q_path($params['postid'], $params['title'], true),
					));

				break;


			case 'a_post':
Scott committed
60
				$question = $params['parent'];
Scott committed
61 62 63 64 65 66 67 68 69 70 71 72

				if (isset($question['notify']) && !qa_post_is_by_user($question, $userid, $cookieid))
					qa_send_notification($question['userid'], $question['notify'], @$question['handle'], qa_lang('emails/q_answered_subject'), qa_lang('emails/q_answered_body'), array(
						'^a_handle' => isset($handle) ? $handle : (strlen($params['name']) ? $params['name'] : qa_lang('main/anonymous')),
						'^q_title' => $question['title'],
						'^a_content' => qa_block_words_replace($params['text'], qa_get_block_words_preg()),
						'^url' => qa_q_path($question['postid'], $question['title'], true, 'A', $params['postid']),
					));
				break;


			case 'c_post':
Scott committed
73 74
				$parent = $params['parent'];
				$question = $params['question'];
Scott committed
75

Scott committed
76 77
				$senttoemail = array(); // to ensure each user or email gets only one notification about an added comment
				$senttouserid = array();
Scott committed
78 79 80

				switch ($parent['basetype']) {
					case 'Q':
Scott committed
81 82 83
						$subject = qa_lang('emails/q_commented_subject');
						$body = qa_lang('emails/q_commented_body');
						$context = $parent['title'];
Scott committed
84 85 86
						break;

					case 'A':
Scott committed
87 88 89
						$subject = qa_lang('emails/a_commented_subject');
						$body = qa_lang('emails/a_commented_body');
						$context = qa_viewer_text($parent['content'], $parent['format']);
Scott committed
90 91 92
						break;
				}

Scott committed
93 94 95 96 97
				$blockwordspreg = qa_get_block_words_preg();
				$sendhandle = isset($handle) ? $handle : (strlen($params['name']) ? $params['name'] : qa_lang('main/anonymous'));
				$sendcontext = qa_block_words_replace($context, $blockwordspreg);
				$sendtext = qa_block_words_replace($params['text'], $blockwordspreg);
				$sendurl = qa_q_path($question['postid'], $question['title'], true, 'C', $params['postid']);
Scott committed
98 99

				if (isset($parent['notify']) && !qa_post_is_by_user($parent, $userid, $cookieid)) {
Scott committed
100 101
					$senduserid = $parent['userid'];
					$sendemail = @$parent['notify'];
Scott committed
102 103

					if (qa_email_validate($sendemail))
Scott committed
104
						$senttoemail[$sendemail] = true;
Scott committed
105
					elseif (isset($senduserid))
Scott committed
106
						$senttouserid[$senduserid] = true;
Scott committed
107 108 109 110 111 112 113 114 115

					qa_send_notification($senduserid, $sendemail, @$parent['handle'], $subject, $body, array(
						'^c_handle' => $sendhandle,
						'^c_context' => $sendcontext,
						'^c_content' => $sendtext,
						'^url' => $sendurl,
					));
				}

Scott committed
116
				foreach ($params['thread'] as $comment) {
Scott committed
117
					if (isset($comment['notify']) && !qa_post_is_by_user($comment, $userid, $cookieid)) {
Scott committed
118 119
						$senduserid = $comment['userid'];
						$sendemail = @$comment['notify'];
Scott committed
120 121 122 123

						if (qa_email_validate($sendemail)) {
							if (@$senttoemail[$sendemail])
								continue;
Scott committed
124

Scott committed
125
							$senttoemail[$sendemail] = true;
Scott committed
126 127 128 129 130

						} elseif (isset($senduserid)) {
							if (@$senttouserid[$senduserid])
								continue;

Scott committed
131
							$senttouserid[$senduserid] = true;
Scott committed
132
						}
Scott committed
133

Scott committed
134
						qa_send_notification($senduserid, $sendemail, @$comment['handle'], qa_lang('emails/c_commented_subject'), qa_lang('emails/c_commented_body'), array(
Scott committed
135 136 137 138 139 140
							'^c_handle' => $sendhandle,
							'^c_context' => $sendcontext,
							'^c_content' => $sendtext,
							'^url' => $sendurl,
						));
					}
Scott committed
141
				}
Scott committed
142 143 144 145 146
				break;


			case 'q_queue':
			case 'q_requeue':
Scott committed
147
				if (qa_opt('moderate_notify_admin')) {
Scott committed
148
					qa_send_notification(null, qa_opt('feedback_email'), null,
Scott committed
149 150
						($event == 'q_requeue') ? qa_lang('emails/remoderate_subject') : qa_lang('emails/moderate_subject'),
						($event == 'q_requeue') ? qa_lang('emails/remoderate_body') : qa_lang('emails/moderate_body'),
Scott committed
151 152 153
						array(
							'^p_handle' => isset($handle) ? $handle : (strlen($params['name']) ? $params['name'] :
								(strlen(@$oldquestion['name']) ? $oldquestion['name'] : qa_lang('main/anonymous'))),
Scott committed
154
							'^p_context' => trim(@$params['title'] . "\n\n" . $params['text']), // don't censor for admin
Scott committed
155 156 157 158
							'^url' => qa_q_path($params['postid'], $params['title'], true),
							'^a_url' => qa_path_absolute('admin/moderate'),
						)
					);
Scott committed
159
				}
Scott committed
160 161 162 163 164
				break;


			case 'a_queue':
			case 'a_requeue':
Scott committed
165
				if (qa_opt('moderate_notify_admin')) {
Scott committed
166
					qa_send_notification(null, qa_opt('feedback_email'), null,
Scott committed
167 168
						($event == 'a_requeue') ? qa_lang('emails/remoderate_subject') : qa_lang('emails/moderate_subject'),
						($event == 'a_requeue') ? qa_lang('emails/remoderate_body') : qa_lang('emails/moderate_body'),
Scott committed
169 170 171 172
						array(
							'^p_handle' => isset($handle) ? $handle : (strlen($params['name']) ? $params['name'] :
								(strlen(@$oldanswer['name']) ? $oldanswer['name'] : qa_lang('main/anonymous'))),
							'^p_context' => $params['text'], // don't censor for admin
Scott committed
173
							'^url' => qa_q_path($params['parentid'], $params['parent']['title'], true, 'A', $params['postid']),
Scott committed
174 175 176
							'^a_url' => qa_path_absolute('admin/moderate'),
						)
					);
Scott committed
177
				}
Scott committed
178 179 180 181 182
				break;


			case 'c_queue':
			case 'c_requeue':
Scott committed
183
				if (qa_opt('moderate_notify_admin')) {
Scott committed
184
					qa_send_notification(null, qa_opt('feedback_email'), null,
Scott committed
185 186
						($event == 'c_requeue') ? qa_lang('emails/remoderate_subject') : qa_lang('emails/moderate_subject'),
						($event == 'c_requeue') ? qa_lang('emails/remoderate_body') : qa_lang('emails/moderate_body'),
Scott committed
187 188 189
						array(
							'^p_handle' => isset($handle) ? $handle : (strlen($params['name']) ? $params['name'] :
								(strlen(@$oldcomment['name']) ? $oldcomment['name'] : // could also be after answer converted to comment
Scott committed
190
									(strlen(@$oldanswer['name']) ? $oldanswer['name'] : qa_lang('main/anonymous')))),
Scott committed
191 192 193 194 195
							'^p_context' => $params['text'], // don't censor for admin
							'^url' => qa_q_path($params['questionid'], $params['question']['title'], true, 'C', $params['postid']),
							'^a_url' => qa_path_absolute('admin/moderate'),
						)
					);
Scott committed
196
				}
Scott committed
197 198 199 200 201 202
				break;


			case 'q_flag':
			case 'a_flag':
			case 'c_flag':
Scott committed
203 204 205
				$flagcount = $params['flagcount'];
				$oldpost = $params['oldpost'];
				$notifycount = $flagcount - qa_opt('flagging_notify_first');
Scott committed
206

Scott committed
207
				if ($notifycount >= 0 && ($notifycount % qa_opt('flagging_notify_every')) == 0) {
Scott committed
208 209 210
					qa_send_notification(null, qa_opt('feedback_email'), null, qa_lang('emails/flagged_subject'), qa_lang('emails/flagged_body'), array(
						'^p_handle' => isset($oldpost['handle']) ? $oldpost['handle'] :
							(strlen($oldpost['name']) ? $oldpost['name'] : qa_lang('main/anonymous')),
Scott committed
211 212
						'^flags' => ($flagcount == 1) ? qa_lang_html_sub('main/1_flag', '1', '1') : qa_lang_html_sub('main/x_flags', $flagcount),
						'^p_context' => trim(@$oldpost['title'] . "\n\n" . qa_viewer_text($oldpost['content'], $oldpost['format'])), // don't censor for admin
Scott committed
213 214 215
						'^url' => qa_q_path($params['questionid'], $params['question']['title'], true, $oldpost['basetype'], $oldpost['postid']),
						'^a_url' => qa_path_absolute('admin/flagged'),
					));
Scott committed
216
				}
Scott committed
217 218 219 220
				break;


			case 'a_select':
Scott committed
221
				$answer = $params['answer'];
Scott committed
222 223

				if (isset($answer['notify']) && !qa_post_is_by_user($answer, $userid, $cookieid)) {
Scott committed
224 225
					$blockwordspreg = qa_get_block_words_preg();
					$sendcontent = qa_viewer_text($answer['content'], $answer['format'], array('blockwordspreg' => $blockwordspreg));
Scott committed
226 227 228 229 230 231 232 233 234 235 236 237

					qa_send_notification($answer['userid'], $answer['notify'], @$answer['handle'], qa_lang('emails/a_selected_subject'), qa_lang('emails/a_selected_body'), array(
						'^s_handle' => isset($handle) ? $handle : qa_lang('main/anonymous'),
						'^q_title' => qa_block_words_replace($params['parent']['title'], $blockwordspreg),
						'^a_content' => $sendcontent,
						'^url' => qa_q_path($params['parentid'], $params['parent']['title'], true, 'A', $params['postid']),
					));
				}
				break;


			case 'u_register':
Scott committed
238
				if (qa_opt('register_notify_admin')) {
Scott committed
239 240
					qa_send_notification(null, qa_opt('feedback_email'), null, qa_lang('emails/u_registered_subject'),
						qa_opt('moderate_users') ? qa_lang('emails/u_to_approve_body') : qa_lang('emails/u_registered_body'), array(
Scott committed
241 242 243 244 245
							'^u_handle' => $handle,
							'^url' => qa_path_absolute('user/' . $handle),
							'^a_url' => qa_path_absolute('admin/approve'),
						));
				}
Scott committed
246 247 248 249
				break;


			case 'u_level':
Scott committed
250
				if ($params['level'] >= QA_USER_LEVEL_APPROVED && $params['oldlevel'] < QA_USER_LEVEL_APPROVED) {
Scott committed
251
					qa_send_notification($params['userid'], null, $params['handle'], qa_lang('emails/u_approved_subject'), qa_lang('emails/u_approved_body'), array(
Scott committed
252
						'^url' => qa_path_absolute('user/' . $params['handle']),
Scott committed
253
					));
Scott committed
254
				}
Scott committed
255 256 257 258
				break;


			case 'u_wall_post':
Scott committed
259 260
				if ($userid != $params['userid']) {
					$blockwordspreg = qa_get_block_words_preg();
Scott committed
261

Scott committed
262 263 264
					qa_send_notification($params['userid'], null, $params['handle'], qa_lang('emails/wall_post_subject'), qa_lang('emails/wall_post_body'), array(
						'^f_handle' => isset($handle) ? $handle : qa_lang('main/anonymous'),
						'^post' => qa_block_words_replace($params['text'], $blockwordspreg),
Scott committed
265
						'^url' => qa_path_absolute('user/' . $params['handle'], null, 'wall'),
Scott committed
266 267 268
					));
				}
				break;
Scott committed
269 270
		}
	}
Scott committed
271
}