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: 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: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 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: 172: 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: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476: 477: 478: 479: 480: 481: 482: 483: 484: 485: 486: 487: 488: 489: 490: 491: 492: 493: 494: 495: 496: 497: 498: 499: 500: 501: 502: 503: 504: 505: 506: 507: 508: 509: 510: 511: 512: 513: 514: 515: 516: 517: 518: 519:
<?php
class CreatePost_Notify_Background extends SMF_BackgroundTask
{
const NOTIFY_TYPE_REPLY_AND_MODIFY = 1;
const NOTIFY_TYPE_REPLY_AND_TOPIC_START_FOLLOWING = 2;
const NOTIFY_TYPE_ONLY_REPLIES = 3;
const NOTIFY_TYPE_NOTHING = 4;
const FREQUENCY_NOTHING = 0;
const FREQUENCY_EVERYTHING = 1;
const FREQUENCY_FIRST_UNREAD_MSG = 2;
const FREQUENCY_DAILY_DIGEST = 3;
const FREQUENCY_WEEKLY_DIGEST = 4;
public function execute()
{
global $smcFunc, $sourcedir, $scripturl, $language, $modSettings, $user_info;
require_once($sourcedir . '/Subs-Post.php');
require_once($sourcedir . '/Mentions.php');
require_once($sourcedir . '/Subs-Notify.php');
require_once($sourcedir . '/Subs.php');
require_once($sourcedir . '/ScheduledTasks.php');
loadEssentialThemeData();
$msgOptions = $this->_details['msgOptions'];
$topicOptions = $this->_details['topicOptions'];
$posterOptions = $this->_details['posterOptions'];
$type = $this->_details['type'];
$members = array();
$quotedMembers = array();
$done_members = array();
$alert_rows = array();
$receiving_members = array();
if ($type == 'reply' || $type == 'topic')
{
$quotedMembers = self::getQuotedMembers($msgOptions, $posterOptions);
$members = array_keys($quotedMembers);
}
if (!empty($msgOptions['mentioned_members']))
{
Mentions::insertMentions('msg', $msgOptions['id'], $msgOptions['mentioned_members'], $posterOptions['id']);
$members = array_merge($members, array_keys($msgOptions['mentioned_members']));
}
$request = $smcFunc['db_query']('', '
SELECT
ln.id_member, ln.id_board, ln.id_topic, ln.sent,
mem.email_address, mem.lngfile, mem.pm_ignore_list,
mem.id_group, mem.id_post_group, mem.additional_groups,
mem.time_format, mem.time_offset, mem.timezone,
b.member_groups, t.id_member_started, t.id_member_updated
FROM {db_prefix}log_notify AS ln
INNER JOIN {db_prefix}members AS mem ON (ln.id_member = mem.id_member)
LEFT JOIN {db_prefix}topics AS t ON (t.id_topic = ln.id_topic)
LEFT JOIN {db_prefix}boards AS b ON (b.id_board = ln.id_board OR b.id_board = t.id_board)
WHERE ln.id_member != {int:member}
AND (ln.id_topic = {int:topic} OR ln.id_board = {int:board})',
array(
'member' => $posterOptions['id'],
'topic' => $topicOptions['id'],
'board' => $topicOptions['board'],
)
);
$watched = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$groups = array_merge(array($row['id_group'], $row['id_post_group']), (empty($row['additional_groups']) ? array() : explode(',', $row['additional_groups'])));
if (!in_array(1, $groups) && count(array_intersect($groups, explode(',', $row['member_groups']))) == 0)
continue;
else
{
$row['groups'] = $groups;
unset($row['id_group'], $row['id_post_group'], $row['additional_groups']);
}
$members[] = $row['id_member'];
$watched[$row['id_member']] = $row;
}
$smcFunc['db_free_result']($request);
if ($type == 'edit')
{
$unnotified = array_filter($watched, function ($member)
{
return empty($member['sent']);
});
$members = array_intersect($members, array_keys($unnotified));
$quotedMembers = array_intersect_key($quotedMembers, $unnotified);
$msgOptions['mentioned_members'] = array_intersect_key($msgOptions['mentioned_members'], $unnotified);
$watched = array();
}
if (empty($members))
return true;
$members = array_unique($members);
$prefs = getNotifyPrefs($members, '', true);
$disable = array('attach', 'img', 'iurl', 'url', 'youtube');
if (!empty($modSettings['disabledBBC']))
{
$disabledBBC = $modSettings['disabledBBC'];
$disable = array_unique(array_merge($disable, explode(',', $modSettings['disabledBBC'])));
}
$modSettings['disabledBBC'] = implode(',', $disable);
if (!empty($msgOptions['mentioned_members']))
{
$mentioned_members = Mentions::getMentionsByContent('msg', $msgOptions['id'], array_keys($msgOptions['mentioned_members']));
self::handleMentionedNotifications($msgOptions, $mentioned_members, $prefs, $done_members, $alert_rows);
}
self::handleQuoteNotifications($msgOptions, $posterOptions, $quotedMembers, $prefs, $done_members, $alert_rows);
foreach ($done_members as $done_member)
{
$receiving_members[] = $done_member;
unset($watched[$done_member]);
}
$parsed_message = array();
foreach ($watched as $member => $data)
{
$frequency = isset($prefs[$member]['msg_notify_pref']) ? $prefs[$member]['msg_notify_pref'] : self::FREQUENCY_NOTHING;
$notify_types = !empty($prefs[$member]['msg_notify_type']) ? $prefs[$member]['msg_notify_type'] : self::NOTIFY_TYPE_REPLY_AND_MODIFY;
if (!empty($data['pm_ignore_list']) && in_array($data['id_member_updated'], explode(',', $data['pm_ignore_list'])))
continue;
if (!in_array($type, array('reply', 'topic')) && $notify_types == self::NOTIFY_TYPE_REPLY_AND_TOPIC_START_FOLLOWING && $member != $data['id_member_started'])
continue;
elseif (!in_array($type, array('reply', 'topic')) && $notify_types == self::NOTIFY_TYPE_ONLY_REPLIES)
continue;
elseif ($notify_types == self::NOTIFY_TYPE_NOTHING)
continue;
if (in_array($frequency, array(self::FREQUENCY_NOTHING, self::FREQUENCY_DAILY_DIGEST, self::FREQUENCY_WEEKLY_DIGEST)))
continue;
elseif ($frequency == self::FREQUENCY_FIRST_UNREAD_MSG && $data['sent'])
continue;
elseif (!empty($this->_details['members_only']) && !in_array($member, $this->_details['members_only']))
continue;
if (!empty($data['id_topic']) && $type != 'topic' && !empty($prefs[$member]))
{
$pref = !empty($prefs[$member]['topic_notify_' . $topicOptions['id']]) ? $prefs[$member]['topic_notify_' . $topicOptions['id']] : (!empty($prefs[$member]['topic_notify']) ? $prefs[$member]['topic_notify'] : 0);
$message_type = 'notification_' . $type;
if ($type == 'reply')
{
if (!empty($prefs[$member]['msg_receive_body']))
$message_type .= '_body';
if (!empty($frequency))
$message_type .= '_once';
}
$content_type = 'topic';
}
elseif ($type == 'topic')
{
$pref = !empty($prefs[$member]['board_notify_' . $topicOptions['board']]) ? $prefs[$member]['board_notify_' . $topicOptions['board']] : (!empty($prefs[$member]['board_notify']) ? $prefs[$member]['board_notify'] : 0);
$content_type = 'board';
$message_type = !empty($frequency) ? 'notify_boards_once' : 'notify_boards';
if (!empty($prefs[$member]['msg_receive_body']))
$message_type .= '_body';
}
else
continue;
$receiver_lang = empty($data['lngfile']) || empty($modSettings['userLanguage']) ? $language : $data['lngfile'];
if (isset($user_info))
$real_user_info = $user_info;
$user_info = array(
'id' => $member,
'language' => $receiver_lang,
'groups' => $data['groups'],
'is_guest' => false,
'time_format' => empty($data['time_format']) ? $modSettings['time_format'] : $data['time_format'],
);
$user_info['is_admin'] = in_array(1, $user_info['groups']);
if (!empty($data['timezone']))
{
$tz_system = new DateTimeZone(@date_default_timezone_get());
$tz_user = new DateTimeZone($data['timezone']);
$time_system = new DateTime('now', $tz_system);
$time_user = new DateTime('now', $tz_user);
$user_info['time_offset'] = ($tz_user->getOffset($time_user) - $tz_system->getOffset($time_system)) / 3600;
}
else
$user_info['time_offset'] = empty($data['time_offset']) ? 0 : $data['time_offset'];
$localization = implode('|', array($receiver_lang, $user_info['time_offset'], $user_info['time_format']));
if (empty($parsed_message[$localization]))
{
loadLanguage('index+Modifications', $receiver_lang, false);
$parsed_message[$localization]['subject'] = $msgOptions['subject'];
$parsed_message[$localization]['body'] = $msgOptions['body'];
censorText($parsed_message[$localization]['subject']);
censorText($parsed_message[$localization]['body']);
$parsed_message[$localization]['subject'] = un_htmlspecialchars($parsed_message[$localization]['subject']);
$parsed_message[$localization]['body'] = trim(un_htmlspecialchars(strip_tags(strtr(parse_bbc($parsed_message[$localization]['body'], false), array('<br>' => "\n", '</div>' => "\n", '</li>' => "\n", '[' => '[', ']' => ']', ''' => '\'', '</tr>' => "\n", '</td>' => "\t", '<hr>' => "\n---------------------------------------------------------------\n")))));
}
if (isset($real_user_info))
{
$user_info = $real_user_info;
unset($real_user_info);
}
else
$user_info = null;
if ($pref & self::RECEIVE_NOTIFY_EMAIL)
{
$replacements = array(
'TOPICSUBJECT' => $parsed_message[$localization]['subject'],
'POSTERNAME' => un_htmlspecialchars($posterOptions['name']),
'TOPICLINK' => $scripturl . '?topic=' . $topicOptions['id'] . '.new#new',
'MESSAGE' => $parsed_message[$localization]['body'],
'UNSUBSCRIBELINK' => $scripturl . '?action=notifyboard;board=' . $topicOptions['board'] . '.0',
);
$emaildata = loadEmailTemplate($message_type, $replacements, $receiver_lang);
$mail_result = sendmail($data['email_address'], $emaildata['subject'], $emaildata['body'], null, 'm' . $topicOptions['id'], $emaildata['is_html']);
if ($mail_result === false)
continue;
}
if ($pref & self::RECEIVE_NOTIFY_ALERT)
{
$alert_rows[] = array(
'alert_time' => time(),
'id_member' => $member,
'id_member_started' => in_array($type, array('topic', 'reply')) ? $posterOptions['id'] : 0,
'member_name' => in_array($type, array('topic', 'reply')) ? $posterOptions['name'] : '',
'content_type' => $content_type,
'content_id' => $topicOptions['id'],
'content_action' => $type,
'is_read' => 0,
'extra' => $smcFunc['json_encode'](array(
'topic' => $topicOptions['id'],
'board' => $topicOptions['board'],
'content_subject' => $parsed_message[$localization]['subject'],
'content_link' => $scripturl . '?topic=' . $topicOptions['id'] . (in_array($type, array('reply', 'topic')) ? '.new;topicseen#new' : '.0'),
)),
);
$receiving_members[] = $member;
}
$smcFunc['db_query']('', '
UPDATE {db_prefix}log_notify
SET sent = {int:is_sent}
WHERE (id_topic = {int:topic} OR id_board = {int:board})
AND id_member = {int:member}',
array(
'topic' => $topicOptions['id'],
'board' => $topicOptions['board'],
'member' => $member,
'is_sent' => 1,
)
);
}
if (!empty($disabledBBC))
$modSettings['disabledBBC'] = $disabledBBC;
$smcFunc['db_insert']('',
'{db_prefix}log_digest',
array(
'id_topic' => 'int', 'id_msg' => 'int', 'note_type' => 'string', 'exclude' => 'int',
),
array($topicOptions['id'], $msgOptions['id'], $type, $posterOptions['id']),
array()
);
if (!empty($alert_rows))
$smcFunc['db_insert']('',
'{db_prefix}user_alerts',
array('alert_time' => 'int', 'id_member' => 'int', 'id_member_started' => 'int', 'member_name' => 'string',
'content_type' => 'string', 'content_id' => 'int', 'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string'),
$alert_rows,
array()
);
if (!empty($receiving_members))
updateMemberData($receiving_members, array('alerts' => '+'));
return true;
}
protected static function handleQuoteNotifications($msgOptions, $posterOptions, $quotedMembers, $prefs, &$done_members, &$alert_rows)
{
global $smcFunc, $modSettings, $language, $scripturl;
foreach ($quotedMembers as $id => $member)
{
if (!isset($prefs[$id]) || $id == $posterOptions['id'] || empty($prefs[$id]['msg_quote']))
continue;
$done_members[] = $id;
if ($prefs[$id]['msg_quote'] & self::RECEIVE_NOTIFY_EMAIL)
{
$replacements = array(
'CONTENTSUBJECT' => $msgOptions['subject'],
'QUOTENAME' => $posterOptions['name'],
'MEMBERNAME' => $member['real_name'],
'CONTENTLINK' => $scripturl . '?msg=' . $msgOptions['id'],
);
$emaildata = loadEmailTemplate('msg_quote', $replacements, empty($member['lngfile']) || empty($modSettings['userLanguage']) ? $language : $member['lngfile']);
sendmail($member['email_address'], $emaildata['subject'], $emaildata['body'], null, 'msg_quote_' . $msgOptions['id'], $emaildata['is_html'], 2);
}
if ($prefs[$id]['msg_quote'] & self::RECEIVE_NOTIFY_ALERT)
{
$alert_rows[] = array(
'alert_time' => time(),
'id_member' => $member['id_member'],
'id_member_started' => $posterOptions['id'],
'member_name' => $posterOptions['name'],
'content_type' => 'msg',
'content_id' => $msgOptions['id'],
'content_action' => 'quote',
'is_read' => 0,
'extra' => $smcFunc['json_encode'](array(
'content_subject' => $msgOptions['subject'],
'content_link' => $scripturl . '?msg=' . $msgOptions['id'],
)),
);
}
}
}
protected static function getQuotedMembers($msgOptions, $posterOptions)
{
global $smcFunc;
$blocks = preg_split('/(\[quote.*?\]|\[\/quote\])/i', $msgOptions['body'], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
$quote_level = 0;
$message = '';
foreach ($blocks as $block)
{
if (preg_match('/\[quote(.*)?\]/i', $block, $matches))
{
if ($quote_level == 0)
$message .= '[quote' . $matches[1] . ']';
$quote_level++;
}
elseif (preg_match('/\[\/quote\]/i', $block))
{
if ($quote_level <= 1)
$message .= '[/quote]';
if ($quote_level >= 1)
{
$quote_level--;
$message .= "\n";
}
}
elseif ($quote_level <= 1)
$message .= $block;
}
preg_match_all('/\[quote.*?link=msg=([0-9]+).*?\]/i', $message, $matches);
$id_msgs = $matches[1];
foreach ($id_msgs as $k => $id_msg)
$id_msgs[$k] = (int) $id_msg;
if (empty($id_msgs))
return array();
$request = $smcFunc['db_query']('', '
SELECT m.id_member, mem.email_address, mem.lngfile, mem.real_name
FROM {db_prefix}messages AS m
INNER JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
WHERE id_msg IN ({array_int:msgs})
LIMIT {int:count}',
array(
'msgs' => array_unique($id_msgs),
'count' => count(array_unique($id_msgs)),
)
);
$members = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
if ($posterOptions['id'] == $row['id_member'])
continue;
$members[$row['id_member']] = $row;
}
return $members;
}
protected static function handleMentionedNotifications($msgOptions, $members, $prefs, &$done_members, &$alert_rows)
{
global $smcFunc, $scripturl, $language, $modSettings;
foreach ($members as $id => $member)
{
if (!empty($prefs[$id]['msg_mention']))
$done_members[] = $id;
else
continue;
if ($prefs[$id]['msg_mention'] & self::RECEIVE_NOTIFY_EMAIL)
{
$replacements = array(
'CONTENTSUBJECT' => $msgOptions['subject'],
'MENTIONNAME' => $member['mentioned_by']['name'],
'MEMBERNAME' => $member['real_name'],
'CONTENTLINK' => $scripturl . '?msg=' . $msgOptions['id'],
);
$emaildata = loadEmailTemplate('msg_mention', $replacements, empty($member['lngfile']) || empty($modSettings['userLanguage']) ? $language : $member['lngfile']);
sendmail($member['email_address'], $emaildata['subject'], $emaildata['body'], null, 'msg_mention_' . $msgOptions['id'], $emaildata['is_html'], 2);
}
if ($prefs[$id]['msg_mention'] & self::RECEIVE_NOTIFY_ALERT)
{
$alert_rows[] = array(
'alert_time' => time(),
'id_member' => $member['id'],
'id_member_started' => $member['mentioned_by']['id'],
'member_name' => $member['mentioned_by']['name'],
'content_type' => 'msg',
'content_id' => $msgOptions['id'],
'content_action' => 'mention',
'is_read' => 0,
'extra' => $smcFunc['json_encode'](array(
'content_subject' => $msgOptions['subject'],
'content_link' => $scripturl . '?msg=' . $msgOptions['id'],
)),
);
}
}
}
}
?>