From 7fee0cfdf60c9aeebcd498d2f41696bb7fed2dd0 Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Sat, 8 Sep 2012 15:48:46 -0500 Subject: [PATCH] [ticket/11103] Work on the pm type and email method PHPBB3-11103 --- phpBB/includes/functions_privmsgs.php | 13 ++++ phpBB/includes/notifications/method/base.php | 11 ++- phpBB/includes/notifications/method/email.php | 61 ++++++++++++++++ phpBB/includes/notifications/service.php | 14 +--- phpBB/includes/notifications/type/base.php | 13 ++-- .../includes/notifications/type/interface.php | 6 +- phpBB/includes/notifications/type/pm.php | 69 +++++++++++++++++-- 7 files changed, 159 insertions(+), 28 deletions(-) diff --git a/phpBB/includes/functions_privmsgs.php b/phpBB/includes/functions_privmsgs.php index 9e055a319f..99ad2ad791 100644 --- a/phpBB/includes/functions_privmsgs.php +++ b/phpBB/includes/functions_privmsgs.php @@ -1855,6 +1855,19 @@ function submit_pm($mode, $subject, &$data, $put_in_outbox = true) */ function pm_notification($mode, $author, $recipients, $subject, $message, $msg_id) { + global $phpbb_container; + + $phpbb_notifications = $phpbb_container->get('notifications'); + + $phpbb_notifications->add_notifications('pm', array( + 'author_id' => $author, + 'recipients' => $recipients, + 'message_subject' => $subject, + 'msg_id' => $msg_id, + )); + + return; + global $db, $user, $config, $phpbb_root_path, $phpEx, $auth; $subject = censor_text($subject); diff --git a/phpBB/includes/notifications/method/base.php b/phpBB/includes/notifications/method/base.php index dbf851a059..98c06509c6 100644 --- a/phpBB/includes/notifications/method/base.php +++ b/phpBB/includes/notifications/method/base.php @@ -26,6 +26,8 @@ abstract class phpbb_notifications_method_base implements phpbb_notifications_me protected $phpbb_container; protected $db; protected $user; + protected $phpbb_root_path; + protected $php_ext; /** * Queue of messages to be sent @@ -42,6 +44,9 @@ abstract class phpbb_notifications_method_base implements phpbb_notifications_me // Some common things we're going to use $this->db = $phpbb_container->get('dbal.conn'); $this->user = $phpbb_container->get('user'); + + $this->phpbb_root_path = $phpbb_container->getParameter('core.root_path'); + $this->php_ext = $phpbb_container->getParameter('core.php_ext'); } /** @@ -65,7 +70,11 @@ abstract class phpbb_notifications_method_base implements phpbb_notifications_me $this->notify($notification); } - // Empty queue + $this->empty_queue(); + } + + protected function empty_queue() + { $this->queue = array(); } } diff --git a/phpBB/includes/notifications/method/email.php b/phpBB/includes/notifications/method/email.php index 0c3cb4bd85..725ede7913 100644 --- a/phpBB/includes/notifications/method/email.php +++ b/phpBB/includes/notifications/method/email.php @@ -33,4 +33,65 @@ class phpbb_notifications_method_email extends phpbb_notifications_method_base { // email the user } + + public function run_queue() + { + if (!sizeof($this->queue)) + { + return; + } + + // Load all users we want to notify (we need their email address) + $user_ids = $users = array(); + foreach ($this->queue as $notification) + { + $user_ids[] = $notification->user_id; + } + + $sql = 'SELECT * FROM ' . USERS_TABLE . ' + WHERE ' . $this->db->sql_in_set('user_id', $user_ids); + $result = $this->db->sql_query($sql); + while ($row = $this->db->sql_fetchrow($result)) + { + $users[$row['user_id']] = $row; + } + $this->db->sql_freeresult($result); + + // Load the messenger + if (!class_exists('messenger')) + { + include($this->phpbb_root_path . 'includes/functions_messenger.' . $this->php_ext); + } + $messenger = new messenger(); + $board_url = generate_board_url(); + + // Time to go through the queue and send emails + foreach ($this->queue as $notification) + { + $notification->users($users); + + $user = $notification->get_user(); + + $messenger->template('privmsg_notify', $user['user_lang']); + + $messenger->to($user['user_email'], $user['username']); + + $messenger->assign_vars(array( + 'SUBJECT' => htmlspecialchars_decode($notification->get_title()), + 'AUTHOR_NAME' => '', + 'USERNAME' => htmlspecialchars_decode($user['username']), + + 'U_INBOX' => $board_url . "/ucp.{$this->php_ext}?i=pm&folder=inbox", + 'U_VIEW_MESSAGE' => $board_url . "/ucp.{$this->php_ext}?i=pm&mode=view&p={$notification->get_item_id()}", + )); + + $messenger->send($addr['method']); + } + + // Save the queue in the messenger class (has to be called or these emails could be lost?) + $messenger->save_queue(); + + // We're done, empty the queue + $this->empty_queue(); + } } diff --git a/phpBB/includes/notifications/service.php b/phpBB/includes/notifications/service.php index 32211b26cf..ba57fe9f72 100644 --- a/phpBB/includes/notifications/service.php +++ b/phpBB/includes/notifications/service.php @@ -141,19 +141,7 @@ class phpbb_notifications_service */ // find out which users want to receive this type of notification - $sql = 'SELECT user_id FROM ' . USERS_TABLE . ' - WHERE ' . $this->db->sql_in_set('user_id', array(2)); - $result = $this->db->sql_query($sql); - while ($row = $this->db->sql_fetchrow($result)) - { - if (!isset($notify_users[$row['user_id']])) - { - $notify_users[$row['user_id']] = array(); - } - - $notify_users[$row['user_id']][] = ''; - } - $this->db->sql_freeresult($result); + $notify_users = $item_type_class_name::find_users_for_notification($data); // Make sure not to send new notifications to users who've already been notified about this item // This may happen when an item was added, but now new users are able to see the item diff --git a/phpBB/includes/notifications/type/base.php b/phpBB/includes/notifications/type/base.php index 89143873a8..f031abae77 100644 --- a/phpBB/includes/notifications/type/base.php +++ b/phpBB/includes/notifications/type/base.php @@ -58,6 +58,7 @@ abstract class phpbb_notifications_type_base implements phpbb_notifications_type // Some common things we're going to use $this->db = $phpbb_container->get('dbal.conn'); + $this->phpbb_root_path = $phpbb_container->getParameter('core.root_path'); $this->php_ext = $phpbb_container->getParameter('core.php_ext'); @@ -114,7 +115,7 @@ abstract class phpbb_notifications_type_base implements phpbb_notifications_type * @param int $user_id * @return array */ - protected function get_user($user_id) + public function get_user($user_id) { return $this->users[$user_id]; } @@ -149,11 +150,11 @@ abstract class phpbb_notifications_type_base implements phpbb_notifications_type * Function for preparing the data for insertion in an SQL query * (The service handles insertion) * - * @param array $special_data Data unique to this notification type + * @param array $type_data Data unique to this notification type * * @return array Array of data ready to be inserted into the database */ - public function create_insert_array($special_data) + public function create_insert_array($type_data) { // Defaults $data = array_merge(array( @@ -173,13 +174,13 @@ abstract class phpbb_notifications_type_base implements phpbb_notifications_type * Function for preparing the data for update in an SQL query * (The service handles insertion) * - * @param array $special_data Data unique to this notification type + * @param array $type_data Data unique to this notification type * * @return array Array of data ready to be updated in the database */ - public function create_update_array($special_data) + public function create_update_array($type_data) { - $data = $this->create_insert_array($special_data); + $data = $this->create_insert_array($type_data); // Unset data unique to each row unset( diff --git a/phpBB/includes/notifications/type/interface.php b/phpBB/includes/notifications/type/interface.php index b1ee9ae0b6..b710a75606 100644 --- a/phpBB/includes/notifications/type/interface.php +++ b/phpBB/includes/notifications/type/interface.php @@ -23,11 +23,13 @@ interface phpbb_notifications_type_interface { public static function get_item_type(); - public static function get_item_id($post); + public static function get_item_id($type_data); public function get_title(); public function get_url(); - public function create_insert_array($special_data); + public function create_insert_array($type_data); + + public function find_users_for_notification($type_data); } diff --git a/phpBB/includes/notifications/type/pm.php b/phpBB/includes/notifications/type/pm.php index 191c0b7e7f..7685a49614 100644 --- a/phpBB/includes/notifications/type/pm.php +++ b/phpBB/includes/notifications/type/pm.php @@ -84,16 +84,73 @@ class phpbb_notifications_type_pm extends phpbb_notifications_type_base * * @return array Array of data ready to be inserted into the database */ - public function create_insert_array($post) + public function create_insert_array($pm) { - $this->item_id = $post['msg_id']; + $this->item_id = $pm['msg_id']; - $this->set_data('author_id', $post['author_id']); + $this->set_data('author_id', $pm['author_id']); - $this->set_data('message_subject', $post['message_subject']); + $this->set_data('message_subject', $pm['message_subject']); - $this->time = $post['message_time']; + return parent::create_insert_array($pm); + } - return parent::create_insert_array($post); + /** + * Find the users who want to receive notifications + * + * @param array $pm Data from + * @return array + */ + public function find_users_for_notification($pm) + { + $user = $this->phpbb_container->get('user'); + + // Exclude guests, current user and banned users from notifications + unset($pm['recipients'][ANONYMOUS], $pm['recipients'][$user->data['user_id']]); + + if (!sizeof($pm['recipients'])) + { + return; + } + + if (!function_exists('phpbb_get_banned_user_ids')) + { + include($phpbb_root_path . 'includes/functions_user.' . $phpEx); + } + $banned_users = phpbb_get_banned_user_ids(array_keys($pm['recipients'])); + $pm['recipients'] = array_diff(array_keys($pm['recipients']), $banned_users); + + if (!sizeof($pm['recipients'])) + { + return; + } + + $sql = 'SELECT user_id, user_notify_pm, user_notify_type + FROM ' . USERS_TABLE . ' + WHERE ' . $db->sql_in_set('user_id', $pm['recipients']); + $result = $db->sql_query($sql); + + $pm['recipients'] = array(); + + while ($row = $db->sql_fetchrow($result)) + { + if ($row['user_notify_pm']) + { + $pm['recipients'][$row['user_id']] = array(); + + if ($row['user_notify_type'] == NOTIFY_EMAIL || $row['user_notify_type'] == NOTIFY_BOTH) + { + $pm['recipients'][$row['user_id']][] = 'email'; + } + + if ($row['user_notify_type'] == NOTIFY_IM || $row['user_notify_type'] == NOTIFY_BOTH) + { + $pm['recipients'][$row['user_id']][] = 'jabber'; + } + } + } + $db->sql_freeresult($result); + + return $pm['recipients']; } }