PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/class_notifications.php

http://github.com/MightyGorgon/icy_phoenix
PHP | 410 lines | 301 code | 59 blank | 50 comment | 41 complexity | e9e6b152a7acd23155e6414a3e7190b0 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package Icy Phoenix
  5. * @version $Id$
  6. * @copyright (c) 2008 Icy Phoenix
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. if (!defined('IN_ICYPHOENIX'))
  11. {
  12. die('Hacking attempt');
  13. }
  14. class class_notifications
  15. {
  16. var $notify_userdata = array();
  17. var $notify_userid = array();
  18. var $notify_forum_name = '';
  19. var $exclude_users = array();
  20. /**
  21. * Initialize the class
  22. */
  23. function __construct()
  24. {
  25. global $config, $db, $user;
  26. // Build exclusion list
  27. $sql = "SELECT ban_userid FROM " . BANLIST_TABLE . " WHERE ban_userid <> 0 ORDER BY ban_userid ASC";
  28. $result = $db->sql_query($sql, 86400, 'ban_', USERS_CACHE_FOLDER);
  29. $this->exclude_users = array($user->data['user_id'], ANONYMOUS);
  30. while ($row = $db->sql_fetchrow($result))
  31. {
  32. if (isset($row['ban_userid']) && !empty($row['ban_userid']))
  33. {
  34. $this->exclude_users[] = $row['ban_userid'];
  35. }
  36. }
  37. $db->sql_freeresult($result);
  38. // Sixty second limit
  39. @set_time_limit(60);
  40. // Let's do some checking to make sure that mass mail functions are working in win32 versions of php.
  41. if (!$config['smtp_delivery'] && preg_match('/[c-z]:\\\.*/i', getenv('PATH')))
  42. {
  43. $ini_val = (@phpversion() >= '4.0.0') ? 'ini_get' : 'get_cfg_var';
  44. // We are running on windows, force delivery to use our smtp functions since php's are broken by default
  45. $config['smtp_delivery'] = 1;
  46. $config['smtp_host'] = @$ini_val('SMTP');
  47. }
  48. }
  49. /**
  50. * Send user notifications on new topic or reply
  51. */
  52. function send_notifications($mode, &$post_data, &$topic_title, &$forum_id, &$topic_id, &$post_id, &$notify_user)
  53. {
  54. global $config, $lang, $db, $user;
  55. global $bbcode;
  56. $current_time = time();
  57. include_once(IP_ROOT_PATH . 'includes/bbcode.' . PHP_EXT);
  58. if ($mode != 'delete')
  59. {
  60. if ($mode == 'reply')
  61. {
  62. // Look for users with notification enabled
  63. $sql = "SELECT u.user_id, u.user_email, u.user_lang, u.username, f.forum_name
  64. FROM " . USERS_TABLE . " u, " . TOPICS_WATCH_TABLE . " tw, " . FORUMS_TABLE . " f
  65. WHERE tw.topic_id = " . $topic_id . "
  66. AND " . $db->sql_in_set('tw.user_id', $this->exclude_users, true, true) . "
  67. AND tw.notify_status = " . TOPIC_WATCH_UN_NOTIFIED . "
  68. AND f.forum_id = " . $forum_id . "
  69. AND u.user_id = tw.user_id
  70. AND u.user_active = 1";
  71. $result = $db->sql_query($sql);
  72. while ($row = $db->sql_fetchrow($result))
  73. {
  74. if (!in_array($row['user_id'], $this->notify_userid))
  75. {
  76. if ($row['user_email'] != '')
  77. {
  78. $this->notify_userdata[] = array(
  79. 'username' => $row['username'],
  80. 'user_email' => $row['user_email'],
  81. 'user_lang' => $row['user_lang']
  82. );
  83. }
  84. $this->notify_userid[] = $row['user_id'];
  85. $this->notify_forum_name = $row['forum_name'];
  86. }
  87. }
  88. $db->sql_freeresult($result);
  89. }
  90. if (($mode == 'newtopic') || ($mode == 'reply'))
  91. {
  92. // Reply or New Topic forum notification
  93. $sql = "SELECT u.user_id, u.user_email, u.user_lang, f.forum_name
  94. FROM " . USERS_TABLE . " u, " . FORUMS_WATCH_TABLE . " fw, " . FORUMS_TABLE . " f
  95. WHERE fw.forum_id = " . $forum_id . "
  96. AND " . $db->sql_in_set('fw.user_id', array_merge($this->exclude_users, $this->notify_userid), true, true) . "
  97. AND fw.notify_status = " . TOPIC_WATCH_UN_NOTIFIED . "
  98. AND f.forum_id = " . $forum_id . "
  99. AND f.forum_notify = '1'
  100. AND u.user_id = fw.user_id
  101. AND u.user_active = 1";
  102. $result = $db->sql_query($sql);
  103. while ($row = $db->sql_fetchrow($result))
  104. {
  105. if (!in_array($row['user_id'], $this->notify_userid))
  106. {
  107. if ($row['user_email'] != '')
  108. {
  109. $this->notify_userdata[] = array(
  110. 'username' => $row['username'],
  111. 'user_email' => $row['user_email'],
  112. 'user_lang' => $row['user_lang']
  113. );
  114. }
  115. $this->notify_userid[] = $row['user_id'];
  116. $this->notify_forum_name = $row['forum_name'];
  117. }
  118. }
  119. $db->sql_freeresult($result);
  120. }
  121. // Users array built, so start sending notifications
  122. if (sizeof($this->notify_userdata) > 0)
  123. {
  124. include_once(IP_ROOT_PATH . 'includes/emailer.' . PHP_EXT);
  125. $emailer = new emailer();
  126. $server_url = create_server_url();
  127. $topic_title = unprepare_message($topic_title);
  128. $topic_title = censor_text($topic_title);
  129. $post_text = unprepare_message($post_data['message']);
  130. $post_text = censor_text($post_text);
  131. if (!empty($config['html_email']))
  132. {
  133. $bbcode->allow_bbcode = (!empty($config['allow_bbcode']) ? $config['allow_bbcode'] : false);
  134. $bbcode->allow_html = (!empty($config['allow_html']) ? $config['allow_html'] : false);
  135. $bbcode->allow_smilies = (!empty($config['allow_smilies']) ? $config['allow_smilies'] : false);
  136. $post_text = $bbcode->parse($post_text);
  137. }
  138. else
  139. {
  140. $post_text = $bbcode->plain_message($post_text, '');
  141. }
  142. for ($i = 0; $i < sizeof($this->notify_userdata); $i++)
  143. {
  144. $emailer->use_template('topic_notify', $this->notify_userdata[$i]['user_lang']);
  145. $emailer->bcc($this->notify_userdata[$i]['user_email']);
  146. // The Topic_reply_notification lang string below will be used
  147. // if for some reason the mail template subject cannot be read
  148. // ... note it will not necessarily be in the posters own language!
  149. $emailer->set_subject($lang['Topic_reply_notification']);
  150. // This is a nasty kludge to remove the username var ... till (if?) translators update their templates
  151. $emailer->msg = preg_replace('#[ ]?{USERNAME}#', $this->notify_userdata[$i]['username'], $emailer->msg);
  152. if ($config['url_rw'] == '1')
  153. {
  154. $topic_url = $server_url . str_replace ('--', '-', make_url_friendly($topic_title) . '-vp' . $post_id . '.html#p' . $post_id);
  155. }
  156. else
  157. {
  158. $topic_url = $server_url . CMS_PAGE_VIEWTOPIC . '?' . POST_POST_URL . '=' . $post_id . '#p' . $post_id;
  159. }
  160. $email_sig = create_signature($config['board_email_sig']);
  161. $emailer->assign_vars(array(
  162. 'EMAIL_SIG' => $email_sig,
  163. 'SITENAME' => $config['sitename'],
  164. 'TOPIC_TITLE' => $topic_title,
  165. 'POST_TEXT' => $post_text,
  166. 'POSTERNAME' => $post_data['username'],
  167. 'FORUM_NAME' => $this->notify_forum_name,
  168. 'ROOT' => $server_url,
  169. 'U_TOPIC' => $topic_url,
  170. 'U_STOP_WATCHING_TOPIC' => $server_url . CMS_PAGE_VIEWTOPIC . '?' . POST_TOPIC_URL . '=' . $topic_id . '&unwatch=topic'
  171. )
  172. );
  173. $emailer->send();
  174. $emailer->reset();
  175. }
  176. }
  177. // Emails sent, so set users were notified
  178. $sql = "UPDATE " . TOPICS_WATCH_TABLE . "
  179. SET notify_status = " . TOPIC_WATCH_NOTIFIED . "
  180. WHERE topic_id = " . $topic_id . "
  181. AND " . $db->sql_in_set('user_id', $this->notify_userid, false, true);
  182. $db->sql_query($sql);
  183. $sql = "UPDATE " . FORUMS_WATCH_TABLE . "
  184. SET notify_status = " . TOPIC_WATCH_NOTIFIED . "
  185. WHERE forum_id = " . $forum_id . "
  186. AND " . $db->sql_in_set('user_id', $this->notify_userid, false, true);
  187. $db->sql_query($sql);
  188. // Delete notification for poster if present, or re-activate it if requested
  189. if (!$notify_user && !empty($row['topic_id']))
  190. {
  191. $this->delete_topic_watch($user->data['user_id'], $topic_id);
  192. }
  193. elseif ($notify_user && empty($row['topic_id']))
  194. {
  195. $this->delete_topic_watch($user->data['user_id'], $topic_id);
  196. $this->insert_topic_watch($user->data['user_id'], $topic_id, $forum_id, TOPIC_WATCH_UN_NOTIFIED);
  197. }
  198. }
  199. }
  200. /**
  201. * Add topic watch entry
  202. */
  203. function insert_topic_watch($user_id, $topic_id, $forum_id, $notify_status)
  204. {
  205. global $db;
  206. $sql = "INSERT INTO " . TOPICS_WATCH_TABLE . " (user_id, topic_id, forum_id, notify_status)
  207. VALUES (" . (int) $user_id . ", " . (int) $topic_id . ", " . (int) $forum_id . ", " . (int) $notify_status . ")";
  208. $result = $db->sql_query($sql);
  209. }
  210. /**
  211. * Update topic watch entry
  212. */
  213. function update_topic_watch($user_id, $topic_id, $forum_id, $notify_status)
  214. {
  215. global $db;
  216. $this->delete_topic_watch($user_id, $topic_id);
  217. $this->insert_topic_watch($user_id, $topic_id, $forum_id, $notify_status);
  218. }
  219. /**
  220. * Remove topic watch entries
  221. */
  222. function delete_topic_watch($user_id, $topic_id)
  223. {
  224. global $db;
  225. $sql = "DELETE FROM " . TOPICS_WATCH_TABLE . "
  226. WHERE topic_id = " . (int) $topic_id . "
  227. AND user_id = " . (int) $user_id;
  228. $result = $db->sql_query($sql);
  229. }
  230. /**
  231. * Delete user(s) notifications
  232. */
  233. function delete_user_notifications($user_id)
  234. {
  235. global $db;
  236. if (!is_array($user_id))
  237. {
  238. $user_id = array($user_id);
  239. }
  240. // Delete users notifications
  241. $sql = "DELETE FROM " . TOPICS_WATCH_TABLE . "
  242. WHERE " . $db->sql_in_set('user_id', $user_id);
  243. $db->sql_query($sql);
  244. $sql = "DELETE FROM " . FORUMS_WATCH_TABLE . "
  245. WHERE " . $db->sql_in_set('user_id', $user_id);
  246. $db->sql_query($sql);
  247. }
  248. /**
  249. * Delete notifications for users that cannot read in a forum
  250. */
  251. function delete_not_auth_notifications($forum_ids = '')
  252. {
  253. global $db;
  254. // Build the forums array
  255. if (!empty($forum_ids) && !is_array($forum_ids))
  256. {
  257. $forum_ids = array($forum_ids);
  258. }
  259. elseif(empty($forum_ids))
  260. {
  261. $sql = "SELECT forum_id
  262. FROM " . FORUMS_TABLE . "
  263. WHERE forum_type = " . FORUM_POST;
  264. $result = $db->sql_query($sql);
  265. $forum_ids = array();
  266. while ($row = $db->sql_fetchrow($result))
  267. {
  268. $forum_ids[] = $row['forum_id'];
  269. }
  270. }
  271. // Seek for users with topic/forum notifications enabled within this forum(s)
  272. $sql = "SELECT u.user_id, u.user_level
  273. FROM " . USERS_TABLE . " u, " . TOPICS_WATCH_TABLE . " tw
  274. WHERE u.user_id = tw.user_id
  275. AND " . $db->sql_in_set('tw.forum_id', $forum_ids) . "
  276. GROUP BY (u.user_id)
  277. ORDER BY u.user_id ASC";
  278. $result = $db->sql_query($sql);
  279. $row = $db->sql_fetchrowset($result);
  280. $db->sql_freeresult($result);
  281. $exclude_sql = '';
  282. for ($i = 0; $i < sizeof($row); $i++)
  283. {
  284. $exclude_sql .= (!empty($exclude_sql) ? ',' : '') . $row[$i]['user_id'];
  285. }
  286. $exclude_sql = !empty($exclude_sql) ? ' AND u.user_id NOT IN (' . $exclude_sql . ')' : '';
  287. $sql = "SELECT u.user_id, u.user_level
  288. FROM " . USERS_TABLE . " u, " . FORUMS_WATCH_TABLE . " fw
  289. WHERE u.user_id = fw.user_id
  290. AND " . $db->sql_in_set('fw.forum_id', $forum_ids) . $exclude_sql . "
  291. GROUP BY (u.user_id)
  292. ORDER BY u.user_id ASC";
  293. $result = $db->sql_query($sql);
  294. $row2 = $db->sql_fetchrowset($result);
  295. $db->sql_freeresult($result);
  296. if (!is_array($row2))
  297. {
  298. $row2 = array();
  299. }
  300. // Build the not authed user array
  301. $row = array_merge($row, $row2);
  302. $not_auth_data = array();
  303. for ($i = 0; $i < sizeof($row); $i++)
  304. {
  305. $user_info = array(
  306. 'user_id' => intval($row[$i]['user_id']),
  307. 'user_level' => $row[$i]['user_level'],
  308. 'session_logged_in' => true,
  309. 'is_bot' => false
  310. );
  311. for ($j = 0; $j < sizeof($forum_ids); $j++)
  312. {
  313. $is_auth = auth(AUTH_READ, $forum_ids[$j], $user_info);
  314. if (!$is_auth['auth_read'])
  315. {
  316. if (!isset($not_auth_data[$user_info['user_id']]))
  317. {
  318. $not_auth_data[$user_info['user_id']] = array();
  319. }
  320. $not_auth_data[$user_info['user_id']] = array_merge($not_auth_data[$user_info['user_id']], array($forum_ids[$j]));
  321. }
  322. }
  323. }
  324. // Build the sql statement and execute the delete query
  325. if (sizeof($not_auth_data) > 0)
  326. {
  327. $where_sql = '';
  328. foreach($not_auth_data as $user_id => $forum_array)
  329. {
  330. $where_sql .= (empty($where_sql) ? ' WHERE' : ' OR') . ' (user_id = ' . $user_id . ' AND ' . $db->sql_in_set('forum_id', $forum_array) . ') ';
  331. }
  332. $sql = "DELETE FROM " . TOPICS_WATCH_TABLE . $where_sql;
  333. $db->sql_query($sql);
  334. $sql = "DELETE FROM " . FORUMS_WATCH_TABLE . $where_sql;
  335. $db->sql_query($sql);
  336. }
  337. }
  338. /**
  339. * ReSync forum_id in notifications table
  340. */
  341. function topic_notify_resync()
  342. {
  343. global $db, $cache;
  344. $sql = "UPDATE " . TOPICS_TABLE . " t, " . TOPICS_WATCH_TABLE . " tw
  345. SET tw.forum_id = t.forum_id
  346. WHERE tw.topic_id = t.topic_id";
  347. $result = $db->sql_query($sql);
  348. return true;
  349. }
  350. }
  351. ?>