qa-event-notify.php 11.5 KB
Newer Older
Gideon Greenspan committed
1 2 3 4 5 6 7
<?php

/*
	Question2Answer (c) Gideon Greenspan

	http://www.question2answer.org/

Scott Vivian committed
8

Gideon Greenspan committed
9 10 11 12 13 14 15 16 17
	File: qa-include/qa-event-notify.php
	Version: See define()s at top of qa-include/qa-base.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.
Scott Vivian committed
18

Gideon Greenspan committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
	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
*/

	if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
		header('Location: ../');
		exit;
	}


	class qa_event_notify {

35
		public function process_event($event, $userid, $handle, $cookieid, $params)
Gideon Greenspan committed
36 37 38 39 40
		{
			require_once QA_INCLUDE_DIR.'qa-app-emails.php';
			require_once QA_INCLUDE_DIR.'qa-app-format.php';
			require_once QA_INCLUDE_DIR.'qa-util-string.php';

Scott Vivian committed
41

Gideon Greenspan committed
42 43 44
			switch ($event) {
				case 'q_post':
					$followanswer=@$params['followanswer'];
Gideon Greenspan committed
45
					$sendhandle=isset($handle) ? $handle : (strlen($params['name']) ? $params['name'] : qa_lang('main/anonymous'));
Scott Vivian committed
46

Gideon Greenspan committed
47 48 49
					if (isset($followanswer['notify']) && !qa_post_is_by_user($followanswer, $userid, $cookieid)) {
						$blockwordspreg=qa_get_block_words_preg();
						$sendtext=qa_viewer_text($followanswer['content'], $followanswer['format'], array('blockwordspreg' => $blockwordspreg));
Scott Vivian committed
50

Gideon Greenspan committed
51
						qa_send_notification($followanswer['userid'], $followanswer['notify'], @$followanswer['handle'], qa_lang('emails/a_followed_subject'), qa_lang('emails/a_followed_body'), array(
Gideon Greenspan committed
52
							'^q_handle' => $sendhandle,
Gideon Greenspan committed
53 54 55 56 57
							'^q_title' => qa_block_words_replace($params['title'], $blockwordspreg),
							'^a_content' => $sendtext,
							'^url' => qa_q_path($params['postid'], $params['title'], true),
						));
					}
Scott Vivian committed
58

Gideon Greenspan committed
59 60
					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(
Gideon Greenspan committed
61
							'^q_handle' => $sendhandle,
Gideon Greenspan committed
62 63 64 65 66 67 68
							'^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;

Scott Vivian committed
69

Gideon Greenspan committed
70 71
				case 'a_post':
					$question=$params['parent'];
Scott Vivian committed
72

Gideon Greenspan committed
73 74
					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(
Gideon Greenspan committed
75
							'^a_handle' => isset($handle) ? $handle : (strlen($params['name']) ? $params['name'] : qa_lang('main/anonymous')),
Gideon Greenspan committed
76 77 78 79 80 81
							'^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;

Scott Vivian committed
82

Gideon Greenspan committed
83 84 85
				case 'c_post':
					$parent=$params['parent'];
					$question=$params['question'];
Scott Vivian committed
86

Gideon Greenspan committed
87 88
					$senttoemail=array(); // to ensure each user or email gets only one notification about an added comment
					$senttouserid=array();
Scott Vivian committed
89

Gideon Greenspan committed
90 91 92 93 94 95
					switch ($parent['basetype']) {
						case 'Q':
							$subject=qa_lang('emails/q_commented_subject');
							$body=qa_lang('emails/q_commented_body');
							$context=$parent['title'];
							break;
Scott Vivian committed
96

Gideon Greenspan committed
97 98 99 100 101 102
						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;
					}
Scott Vivian committed
103

Gideon Greenspan committed
104
					$blockwordspreg=qa_get_block_words_preg();
Gideon Greenspan committed
105
					$sendhandle=isset($handle) ? $handle : (strlen($params['name']) ? $params['name'] : qa_lang('main/anonymous'));
Gideon Greenspan committed
106 107
					$sendcontext=qa_block_words_replace($context, $blockwordspreg);
					$sendtext=qa_block_words_replace($params['text'], $blockwordspreg);
Gideon Greenspan committed
108
					$sendurl=qa_q_path($question['postid'], $question['title'], true, 'C', $params['postid']);
Scott Vivian committed
109

Gideon Greenspan committed
110 111 112
					if (isset($parent['notify']) && !qa_post_is_by_user($parent, $userid, $cookieid)) {
						$senduserid=$parent['userid'];
						$sendemail=@$parent['notify'];
Scott Vivian committed
113

Gideon Greenspan committed
114 115 116 117
						if (qa_email_validate($sendemail))
							$senttoemail[$sendemail]=true;
						elseif (isset($senduserid))
							$senttouserid[$senduserid]=true;
Scott Vivian committed
118

Gideon Greenspan committed
119 120 121 122 123 124 125
						qa_send_notification($senduserid, $sendemail, @$parent['handle'], $subject, $body, array(
							'^c_handle' => $sendhandle,
							'^c_context' => $sendcontext,
							'^c_content' => $sendtext,
							'^url' => $sendurl,
						));
					}
Scott Vivian committed
126

Gideon Greenspan committed
127 128 129 130
					foreach ($params['thread'] as $comment)
						if (isset($comment['notify']) && !qa_post_is_by_user($comment, $userid, $cookieid)) {
							$senduserid=$comment['userid'];
							$sendemail=@$comment['notify'];
Scott Vivian committed
131

Gideon Greenspan committed
132 133 134
							if (qa_email_validate($sendemail)) {
								if (@$senttoemail[$sendemail])
									continue;
Scott Vivian committed
135

Gideon Greenspan committed
136
								$senttoemail[$sendemail]=true;
Scott Vivian committed
137

Gideon Greenspan committed
138 139 140
							} elseif (isset($senduserid)) {
								if (@$senttouserid[$senduserid])
									continue;
Scott Vivian committed
141

Gideon Greenspan committed
142 143
								$senttouserid[$senduserid]=true;
							}
Scott Vivian committed
144

Gideon Greenspan committed
145 146 147 148 149 150 151 152 153
							qa_send_notification($senduserid, $sendemail, @$comment['handle'], qa_lang('emails/c_commented_subject'), qa_lang('emails/c_commented_body'), array(
								'^c_handle' => $sendhandle,
								'^c_context' => $sendcontext,
								'^c_content' => $sendtext,
								'^url' => $sendurl,
							));
						}
					break;

Scott Vivian committed
154

Gideon Greenspan committed
155
				case 'q_queue':
Gideon Greenspan committed
156
				case 'q_requeue':
Gideon Greenspan committed
157
					if (qa_opt('moderate_notify_admin'))
Gideon Greenspan committed
158 159 160 161 162 163
						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'))),
Gideon Greenspan committed
164
								'^p_context' => trim(@$params['title']."\n\n".$params['text']), // don't censor for admin
Gideon Greenspan committed
165 166 167 168
								'^url' => qa_q_path($params['postid'], $params['title'], true),
								'^a_url' => qa_path_absolute('admin/moderate'),
							)
						);
Gideon Greenspan committed
169
					break;
Scott Vivian committed
170

Gideon Greenspan committed
171 172

				case 'a_queue':
Gideon Greenspan committed
173
				case 'a_requeue':
Gideon Greenspan committed
174
					if (qa_opt('moderate_notify_admin'))
Gideon Greenspan committed
175 176 177 178 179 180
						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'))),
Gideon Greenspan committed
181
								'^p_context' => $params['text'], // don't censor for admin
Gideon Greenspan committed
182 183 184 185
								'^url' => qa_q_path($params['parentid'], $params['parent']['title'], true, 'A', $params['postid']),
								'^a_url' => qa_path_absolute('admin/moderate'),
							)
						);
Gideon Greenspan committed
186
					break;
Scott Vivian committed
187

Gideon Greenspan committed
188 189

				case 'c_queue':
Gideon Greenspan committed
190
				case 'c_requeue':
Gideon Greenspan committed
191
					if (qa_opt('moderate_notify_admin'))
Gideon Greenspan committed
192 193 194 195 196 197 198
						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')))),
Gideon Greenspan committed
199
								'^p_context' => $params['text'], // don't censor for admin
Gideon Greenspan committed
200 201 202 203
								'^url' => qa_q_path($params['questionid'], $params['question']['title'], true, 'C', $params['postid']),
								'^a_url' => qa_path_absolute('admin/moderate'),
							)
						);
Gideon Greenspan committed
204 205
					break;

Scott Vivian committed
206

Gideon Greenspan committed
207 208 209 210 211 212
				case 'q_flag':
				case 'a_flag':
				case 'c_flag':
					$flagcount=$params['flagcount'];
					$oldpost=$params['oldpost'];
					$notifycount=$flagcount-qa_opt('flagging_notify_first');
Scott Vivian committed
213

Gideon Greenspan committed
214 215
					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(
Gideon Greenspan committed
216 217
							'^p_handle' => isset($oldpost['handle']) ? $oldpost['handle'] :
								(strlen($oldpost['name']) ? $oldpost['name'] : qa_lang('main/anonymous')),
Gideon Greenspan committed
218
							'^flags' => ($flagcount==1) ? qa_lang_html_sub('main/1_flag', '1', '1') : qa_lang_html_sub('main/x_flags', $flagcount),
Gideon Greenspan committed
219
							'^p_context' => trim(@$oldpost['title']."\n\n".qa_viewer_text($oldpost['content'], $oldpost['format'])), // don't censor for admin
Gideon Greenspan committed
220
							'^url' => qa_q_path($params['questionid'], $params['question']['title'], true, $oldpost['basetype'], $oldpost['postid']),
Gideon Greenspan committed
221
							'^a_url' => qa_path_absolute('admin/flagged'),
Gideon Greenspan committed
222 223
						));
					break;
Scott Vivian committed
224 225


Gideon Greenspan committed
226 227
				case 'a_select':
					$answer=$params['answer'];
Scott Vivian committed
228

Gideon Greenspan committed
229 230 231
					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));
Scott Vivian committed
232

Gideon Greenspan committed
233 234 235 236 237 238 239 240
						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;
Scott Vivian committed
241

Gideon Greenspan committed
242 243 244 245 246 247 248 249 250
				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;
Scott Vivian committed
251

Gideon Greenspan committed
252 253 254 255 256 257
				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;
Scott Vivian committed
258

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

Gideon Greenspan committed
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'),
Gideon Greenspan committed
265
							'^post' => qa_block_words_replace($params['text'], $blockwordspreg),
Gideon Greenspan committed
266 267
							'^url' => qa_path_absolute('user/'.$params['handle'], null, 'wall'),
						));
Gideon Greenspan committed
268
					}
Gideon Greenspan committed
269
					break;
Gideon Greenspan committed
270 271
			}
		}
Scott Vivian committed
272

Gideon Greenspan committed
273
	}
Scott Vivian committed
274

Gideon Greenspan committed
275 276 277 278

/*
	Omit PHP closing tag to help avoid accidental output
*/