qa-event-notify.php 10.8 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 22
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	File: qa-include/qa-event-notify.php
	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
23 24 25 26 27 28
class qa_event_notify
{
	public function process_event($event, $userid, $handle, $cookieid, $params)
	{
		require_once QA_INCLUDE_DIR.'app/emails.php';
		require_once QA_INCLUDE_DIR.'app/format.php';
Scott committed
29
		require_once QA_INCLUDE_DIR.'util/string.php';
Scott committed
30 31


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

Scott committed
37
				if (isset($followanswer['notify']) && !qa_post_is_by_user($followanswer, $userid, $cookieid)) {
Scott committed
38
					$blockwordspreg=qa_get_block_words_preg();
Scott committed
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
					$sendtext=qa_viewer_text($followanswer['content'], $followanswer['format'], array('blockwordspreg' => $blockwordspreg));

					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':
				$question=$params['parent'];

				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':
				$parent=$params['parent'];
				$question=$params['question'];

				$senttoemail=array(); // to ensure each user or email gets only one notification about an added comment
				$senttouserid=array();

				switch ($parent['basetype']) {
					case 'Q':
						$subject=qa_lang('emails/q_commented_subject');
						$body=qa_lang('emails/q_commented_body');
						$context=$parent['title'];
						break;

					case 'A':
						$subject=qa_lang('emails/a_commented_subject');
						$body=qa_lang('emails/a_commented_body');
						$context=qa_viewer_text($parent['content'], $parent['format']);
						break;
				}

				$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']);

				if (isset($parent['notify']) && !qa_post_is_by_user($parent, $userid, $cookieid)) {
					$senduserid=$parent['userid'];
					$sendemail=@$parent['notify'];

					if (qa_email_validate($sendemail))
						$senttoemail[$sendemail]=true;
					elseif (isset($senduserid))
						$senttouserid[$senduserid]=true;

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

				foreach ($params['thread'] as $comment)
					if (isset($comment['notify']) && !qa_post_is_by_user($comment, $userid, $cookieid)) {
						$senduserid=$comment['userid'];
						$sendemail=@$comment['notify'];

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

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

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

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

Scott committed
135
						qa_send_notification($senduserid, $sendemail, @$comment['handle'], qa_lang('emails/c_commented_subject'), qa_lang('emails/c_commented_body'), array(
Scott committed
136 137 138 139 140 141
							'^c_handle' => $sendhandle,
							'^c_context' => $sendcontext,
							'^c_content' => $sendtext,
							'^url' => $sendurl,
						));
					}
Scott committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
				break;


			case 'q_queue':
			case 'q_requeue':
				if (qa_opt('moderate_notify_admin'))
					qa_send_notification(null, qa_opt('feedback_email'), null,
						($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'),
						array(
							'^p_handle' => isset($handle) ? $handle : (strlen($params['name']) ? $params['name'] :
								(strlen(@$oldquestion['name']) ? $oldquestion['name'] : qa_lang('main/anonymous'))),
							'^p_context' => trim(@$params['title']."\n\n".$params['text']), // don't censor for admin
							'^url' => qa_q_path($params['postid'], $params['title'], true),
							'^a_url' => qa_path_absolute('admin/moderate'),
						)
					);
				break;


			case 'a_queue':
			case 'a_requeue':
				if (qa_opt('moderate_notify_admin'))
					qa_send_notification(null, qa_opt('feedback_email'), null,
						($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'),
						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
172
							'^url' => qa_q_path($params['parentid'], $params['parent']['title'], true, 'A', $params['postid']),
Scott committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
							'^a_url' => qa_path_absolute('admin/moderate'),
						)
					);
				break;


			case 'c_queue':
			case 'c_requeue':
				if (qa_opt('moderate_notify_admin'))
					qa_send_notification(null, qa_opt('feedback_email'), null,
						($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'),
						array(
							'^p_handle' => isset($handle) ? $handle : (strlen($params['name']) ? $params['name'] :
								(strlen(@$oldcomment['name']) ? $oldcomment['name'] : // could also be after answer converted to comment
								(strlen(@$oldanswer['name']) ? $oldanswer['name'] : qa_lang('main/anonymous')))),
							'^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'),
						)
					);
				break;


			case 'q_flag':
			case 'a_flag':
			case 'c_flag':
				$flagcount=$params['flagcount'];
				$oldpost=$params['oldpost'];
				$notifycount=$flagcount-qa_opt('flagging_notify_first');

				if ( ($notifycount>=0) && (($notifycount % qa_opt('flagging_notify_every'))==0) )
					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')),
						'^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
						'^url' => qa_q_path($params['questionid'], $params['question']['title'], true, $oldpost['basetype'], $oldpost['postid']),
						'^a_url' => qa_path_absolute('admin/flagged'),
					));
				break;


			case 'a_select':
				$answer=$params['answer'];

				if (isset($answer['notify']) && !qa_post_is_by_user($answer, $userid, $cookieid)) {
					$blockwordspreg=qa_get_block_words_preg();
					$sendcontent=qa_viewer_text($answer['content'], $answer['format'], array('blockwordspreg' => $blockwordspreg));

					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':
				if (qa_opt('register_notify_admin'))
					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(
						'^u_handle' => $handle,
						'^url' => qa_path_absolute('user/'.$handle),
						'^a_url' => qa_path_absolute('admin/approve'),
					));
				break;


			case 'u_level':
				if ( ($params['level']>=QA_USER_LEVEL_APPROVED) && ($params['oldlevel']<QA_USER_LEVEL_APPROVED) )
					qa_send_notification($params['userid'], null, $params['handle'], qa_lang('emails/u_approved_subject'), qa_lang('emails/u_approved_body'), array(
						'^url' => qa_path_absolute('user/'.$params['handle']),
					));
				break;


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

Scott committed
256 257 258 259 260 261 262
					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),
						'^url' => qa_path_absolute('user/'.$params['handle'], null, 'wall'),
					));
				}
				break;
Scott committed
263 264
		}
	}
Scott committed
265
}