diff options
Diffstat (limited to 'phpBB/includes/notifications')
-rw-r--r-- | phpBB/includes/notifications/method/base.php | 92 | ||||
-rw-r--r-- | phpBB/includes/notifications/method/email.php | 108 | ||||
-rw-r--r-- | phpBB/includes/notifications/method/interface.php | 27 | ||||
-rw-r--r-- | phpBB/includes/notifications/method/jabber.php | 62 | ||||
-rw-r--r-- | phpBB/includes/notifications/service.php | 369 | ||||
-rw-r--r-- | phpBB/includes/notifications/type/base.php | 327 | ||||
-rw-r--r-- | phpBB/includes/notifications/type/interface.php | 49 | ||||
-rw-r--r-- | phpBB/includes/notifications/type/pm.php | 184 | ||||
-rw-r--r-- | phpBB/includes/notifications/type/post.php | 220 | ||||
-rw-r--r-- | phpBB/includes/notifications/type/quote.php | 202 | ||||
-rw-r--r-- | phpBB/includes/notifications/type/topic.php | 222 |
11 files changed, 1862 insertions, 0 deletions
diff --git a/phpBB/includes/notifications/method/base.php b/phpBB/includes/notifications/method/base.php new file mode 100644 index 0000000000..b860fcffda --- /dev/null +++ b/phpBB/includes/notifications/method/base.php @@ -0,0 +1,92 @@ +<?php +/** +* +* @package notifications +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +use Symfony\Component\DependencyInjection\ContainerBuilder; + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ + exit; +} + +/** +* Base notifications method class +* @package notifications +*/ +abstract class phpbb_notifications_method_base implements phpbb_notifications_method_interface +{ + protected $phpbb_container; + protected $service; + protected $db; + protected $user; + protected $phpbb_root_path; + protected $php_ext; + + /** + * Desired notifications + * unique by (type, type_id, user_id, method) + * if multiple methods are desired, multiple rows will exist. + * + * method of "none" will over-ride any other options + * + * item_type + * item_id + * user_id + * method + * none (will never receive notifications) + * standard (listed in notifications window + * popup? + * email + * jabber + * sms? + */ + + /** + * Queue of messages to be sent + * + * @var array + */ + protected $queue = array(); + + public function __construct(ContainerBuilder $phpbb_container) + { + // phpBB Container + $this->phpbb_container = $phpbb_container; + + // Service + $this->service = $phpbb_container->get('notifications'); + + // 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'); + } + + /** + * Add a notification to the queue + * + * @param phpbb_notifications_type_interface $notification + */ + public function add_to_queue(phpbb_notifications_type_interface $notification) + { + $this->queue[] = $notification; + } + + /** + * Empty the queue + */ + protected function empty_queue() + { + $this->queue = array(); + } +} diff --git a/phpBB/includes/notifications/method/email.php b/phpBB/includes/notifications/method/email.php new file mode 100644 index 0000000000..1bcea13d92 --- /dev/null +++ b/phpBB/includes/notifications/method/email.php @@ -0,0 +1,108 @@ +<?php +/** +* +* @package notifications +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ + exit; +} + +/** +* Email notification method class +* This class handles sending emails for notifications +* +* @package notifications +*/ +class phpbb_notifications_method_email extends phpbb_notifications_method_base +{ + /** + * Notify method (since jabber gets sent through the same messenger, we let the jabber class inherit from this to reduce code duplication) + * + * @var mixed + */ + protected $notify_method = NOTIFY_EMAIL; + + /** + * Is this method available for the user? + * This is checked on the notifications options + */ + public function is_available() + { + // Email is always available + return true; + } + + public function notify() + { + 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; + } + + // We do not send emails to banned users + if (!function_exists('phpbb_get_banned_user_ids')) + { + include($this->phpbb_container->getParameter('core.root_path') . 'includes/functions_user.' . $this->phpbb_container->getParameter('core.php_ext')); + } + $banned_users = phpbb_get_banned_user_ids($user_ids); + + // Load all the users we need + $this->service->load_users($user_ids); + + // 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) + { + $user = $this->service->get_user($notification->user_id); + + if ($user['user_type'] == USER_IGNORE || in_array($notification->user_id, $banned_users)) + { + continue; + } + + $messenger->template('notification', $user['user_lang']); + + $messenger->to($user['user_email'], $user['username']); + + $messenger->assign_vars(array( + 'USERNAME' => $user['username'], + + 'MESSAGE' => htmlspecialchars_decode($notification->get_title()), + + 'U_VIEW_MESSAGE' => $notification->get_full_url(), + + 'U_UNSUBSCRIBE' => $notification->get_unsubscribe_url(), + )); + + $messenger->send($this->notify_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/method/interface.php b/phpBB/includes/notifications/method/interface.php new file mode 100644 index 0000000000..820cf4fc12 --- /dev/null +++ b/phpBB/includes/notifications/method/interface.php @@ -0,0 +1,27 @@ +<?php +/** +* +* @package notifications +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ + exit; +} + +/** +* Base notifications method interface +* @package notifications +*/ +interface phpbb_notifications_method_interface +{ + public function is_available(); + + public function notify(); +} diff --git a/phpBB/includes/notifications/method/jabber.php b/phpBB/includes/notifications/method/jabber.php new file mode 100644 index 0000000000..738400a50e --- /dev/null +++ b/phpBB/includes/notifications/method/jabber.php @@ -0,0 +1,62 @@ +<?php +/** +* +* @package notifications +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ + exit; +} + +/** +* Jabber notification method class +* This class handles sending Jabber messages for notifications +* +* @package notifications +*/ +class phpbb_notifications_method_jabber extends phpbb_notifications_method_email +{ + /** + * Notify method (since jabber gets sent through the same messenger, we let the jabber class inherit from this to reduce code duplication) + * + * @var mixed + */ + protected $notify_method = NOTIFY_IM; + + /** + * Is this method available for the user? + * This is checked on the notifications options + */ + public function is_available() + { + return ($this->global_available() && $this->phpbb_container->get('user')->data['jabber']); + } + + /** + * Is this method available at all? + * This is checked before notifications are sent + */ + public function global_available() + { + $config = $this->phpbb_container->get('config'); + + return ($config['jab_enable'] && @extension_loaded('xml')); + } + + public function notify() + { + if (!$this->global_available()) + { + return; + } + + return parent::notify(); + } +} diff --git a/phpBB/includes/notifications/service.php b/phpBB/includes/notifications/service.php new file mode 100644 index 0000000000..8be8ae2a95 --- /dev/null +++ b/phpBB/includes/notifications/service.php @@ -0,0 +1,369 @@ +<?php +/** +* +* @package notifications +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +use Symfony\Component\DependencyInjection\ContainerBuilder; + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ + exit; +} + +/** +* Notifications service class +* @package notifications +*/ +class phpbb_notifications_service +{ + protected $phpbb_container; + protected $db; + + /** + * Users loaded from the DB + * + * @var array Array of user data that we've loaded from the DB + */ + protected $users = array(); + + public function __construct(ContainerBuilder $phpbb_container) + { + $this->phpbb_container = $phpbb_container; + + // Some common things we're going to use + $this->db = $phpbb_container->get('dbal.conn'); + } + + /** + * Load the user's notifications + * + * @param array $options Optional options to control what notifications are loaded + * user_id User id to load notifications for (Default: $user->data['user_id']) + * order_by Order by (Default: time) + * order_dir Order direction (Default: DESC) + * limit Number of notifications to load (Default: 5) + * start Notifications offset (Default: 0) + */ + public function load_notifications($options = array()) + { + $user = $this->phpbb_container->get('user'); + + // Merge default options + $options = array_merge(array( + 'user_id' => $user->data['user_id'], + 'order_by' => 'time', + 'order_dir' => 'DESC', + 'limit' => 5, + 'start' => 0, + ), $options); + + $notifications = $user_ids = array(); + $load_special = array(); + + $sql = 'SELECT * + FROM ' . NOTIFICATIONS_TABLE . ' + WHERE user_id = ' . (int) $options['user_id'] . ' + ORDER BY ' . $this->db->sql_escape($options['order_by']) . ' ' . $this->db->sql_escape($options['order_dir']); + $result = $this->db->sql_query_limit($sql, $options['limit'], $options['start']); + + while ($row = $this->db->sql_fetchrow($result)) + { + $item_type_class_name = $this->get_item_type_class_name($row['item_type'], true); + + $notification = new $item_type_class_name($this->phpbb_container, $row); + + // Array of user_ids to query all at once + $user_ids = array_merge($user_ids, $notification->users_to_query()); + + // Some notification types also require querying additional tables themselves + if (!isset($load_special[$row['item_type']])) + { + $load_special[$row['item_type']] = array(); + } + $load_special[$row['item_type']] = array_merge($load_special[$row['item_type']], $notification->get_load_special()); + + $notifications[] = $notification; + } + $this->db->sql_freeresult($result); + + $this->load_users($user_ids); + + // Allow each type to load it's own special items + foreach ($load_special as $item_type => $data) + { + $item_type_class_name = $this->get_item_type_class_name($item_type, true); + + $item_type_class_name::load_special($this->phpbb_container, $data, $notifications); + } + + return $notifications; + } + + /** + * Mark notifications read + * + * @param string $item_type item type + * @param bool|int|array $item_id Item id or array of item ids. False to mark read for all item ids + * @param bool|int|array $user_id User id or array of user ids. False to mark read for all user ids + * @param bool|int $time Time at which to mark all notifications prior to as read. False to mark all as read. (Default: False) + */ + public function mark_notifications_read($item_type, $item_id, $user_id, $time = false) + { + $time = ($time) ?: time(); + + $this->get_item_type_class_name($item_type); + + $sql = 'UPDATE ' . NOTIFICATIONS_TABLE . " + SET unread = 0 + WHERE item_type = '" . $this->db->sql_escape($item_type) . "' + AND time <= " . $time . + (($item_id !== false) ? ' AND ' . (is_array($item_id) ? $this->db->sql_in_set('item_id', $item_id) : 'item_id = ' . (int) $item_id) : '') . + (($user_id !== false) ? ' AND ' . (is_array($user_id) ? $this->db->sql_in_set('user_id', $user_id) : 'user_id = ' . (int) $user_id) : ''); + $this->db->sql_query($sql); + } + + /** + * Mark notifications read from a parent identifier + * + * @param string $item_type item type + * @param bool|int|array $item_parent_id Item parent id or array of item parent ids. False to mark read for all item parent ids + * @param bool|int|array $user_id User id or array of user ids. False to mark read for all user ids + * @param bool|int $time Time at which to mark all notifications prior to as read. False to mark all as read. (Default: False) + */ + public function mark_notifications_read_by_parent($item_type, $item_parent_id, $user_id, $time = false) + { + $time = ($time) ?: time(); + + $item_type_class_name = $this->get_item_type_class_name($item_type); + + $sql = 'UPDATE ' . NOTIFICATIONS_TABLE . " + SET unread = 0 + WHERE item_type = '" . $this->db->sql_escape($item_type) . "' + AND time <= " . $time . + (($item_parent_id !== false) ? ' AND ' . (is_array($item_parent_id) ? $this->db->sql_in_set('item_parent_id', $item_parent_id) : 'item_parent_id = ' . (int) $item_parent_id) : '') . + (($user_id !== false) ? ' AND ' . (is_array($user_id) ? $this->db->sql_in_set('user_id', $user_id) : 'user_id = ' . (int) $user_id) : ''); + $this->db->sql_query($sql); + } + + /** + * Add a notification + * + * @param string $item_type Type identifier + * @param array $data Data specific for this type that will be inserted + */ + public function add_notifications($item_type, $data) + { + $item_type_class_name = $this->get_item_type_class_name($item_type); + + $item_id = $item_type_class_name::get_item_id($data); + + // find out which users want to receive this type of notification + $notify_users = $item_type_class_name::find_users_for_notification($this->phpbb_container, $data); + + $this->add_notifications_for_users($item_type, $data, $notify_users); + } + + /** + * Add a notification for specific users + * + * @param string $item_type Type identifier + * @param array $data Data specific for this type that will be inserted + * @param array $notify_users User list to notify + */ + public function add_notifications_for_users($item_type, $data, $notify_users) + { + $item_type_class_name = $this->get_item_type_class_name($item_type); + + $item_id = $item_type_class_name::get_item_id($data); + + $user_ids = array(); + $notification_objects = $notification_methods = array(); + $new_rows = array(); + + // Never send notifications to the anonymous user or the current user! + unset($notify_users[ANONYMOUS], $notify_users[$this->phpbb_container->get('user')->data['user_id']]); + + // 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 + // todo Users should not receive notifications from multiple events from the same item (ex: for a topic reply with a quote including your username) + // Probably should be handled within each type? + $sql = 'SELECT user_id + FROM ' . NOTIFICATIONS_TABLE . " + WHERE item_type = '" . $this->db->sql_escape($item_type) . "' + AND item_id = " . (int) $item_id; + $result = $this->db->sql_query($sql); + while ($row = $this->db->sql_fetchrow($result)) + { + unset($notify_users[$row['user_id']]); + } + $this->db->sql_freeresult($result); + + if (!sizeof($notify_users)) + { + return; + } + + // Go through each user so we can insert a row in the DB and then notify them by their desired means + foreach ($notify_users as $user => $methods) + { + $notification = new $item_type_class_name($this->phpbb_container); + + $notification->user_id = (int) $user; + + // Store the creation array in our new rows that will be inserted later + $new_rows[] = $notification->create_insert_array($data); + + // Users are needed to send notifications + $user_ids = array_merge($user_ids, $notification->users_to_query()); + + foreach ($methods as $method) + { + // setup the notification methods and add the notification to the queue + if ($method) // blank means we just insert it as a notification, but do not notify them by any other means + { + if (!isset($notification_methods[$method])) + { + $method_class_name = 'phpbb_notifications_method_' . $method; + $notification_methods[$method] = new $method_class_name($this->phpbb_container); + } + + $notification_methods[$method]->add_to_queue($notification); + } + } + } + + // insert into the db + $this->db->sql_multi_insert(NOTIFICATIONS_TABLE, $new_rows); + + // We need to load all of the users to send notifications + $this->load_users($user_ids); + + // run the queue for each method to send notifications + foreach ($notification_methods as $method) + { + $method->notify(); + } + } + + /** + * Update a notification + * + * @param string $item_type Type identifier + * @param array $data Data specific for this type that will be updated + */ + public function update_notifications($item_type, $data) + { + $item_type_class_name = $this->get_item_type_class_name($item_type); + + // Allow the notifications class to over-ride the update_notifications functionality + if (method_exists($item_type_class_name, 'update_notifications')) + { + // Return False to over-ride the rest of the update + if ($item_type_class_name::update_notifications($this->phpbb_container, $data) === false) + { + return; + } + } + + $item_id = $item_type_class_name::get_item_id($data); + + $notification = new $item_type_class_name($this->phpbb_container); + $update_array = $notification->create_update_array($data); + + $sql = 'UPDATE ' . NOTIFICATIONS_TABLE . ' + SET ' . $this->db->sql_build_array('UPDATE', $update_array) . " + WHERE item_type = '" . $this->db->sql_escape($item_type) . "' + AND item_id = " . (int) $item_id; + $this->db->sql_query($sql); + } + + /** + * Delete a notification + * + * @param string $item_type Type identifier + * @param int|array $item_id Identifier within the type (or array of ids) + * @param array $data Data specific for this type that will be updated + */ + public function delete_notifications($item_type, $item_id) + { + $this->get_item_type_class_name($item_type); + + $sql = 'DELETE FROM ' . NOTIFICATIONS_TABLE . " + WHERE item_type = '" . $this->db->sql_escape($item_type) . "' + AND " . (is_array($item_id) ? $this->db->sql_in_set('item_id', $item_id) : 'item_id = ' . (int) $item_id); + $this->db->sql_query($sql); + } + + public function add_subscription($item_type, $item_id, $method = '') + { + $this->get_item_type_class_name($item_type); + + $sql = 'INSERT INTO ' . USER_NOTIFICATIONS_TABLE . ' ' . + $this->db->sql_build_array('INSERT', array( + 'item_type' => $item_type, + 'item_id' => (int) $item_id, + 'user_id' => $this->phpbb_container->get('user')->data['user_id'], + 'method' => $method, + )); + $this->db->sql_query($sql); + } + + /** + * Load user helper + * + * @param array $user_ids + */ + public function load_users($user_ids) + { + // Load the users + $user_ids = array_unique($user_ids); + + // Do not load users we already have in $this->users + $user_ids = array_diff($user_ids, array_keys($this->users)); + + if (sizeof($user_ids)) + { + $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)) + { + $this->users[$row['user_id']] = $row; + } + $this->db->sql_freeresult($result); + } + } + + /** + * Get a user row from our users cache + * + * @param int $user_id + * @return array + */ + public function get_user($user_id) + { + return $this->users[$user_id]; + } + + /** + * Helper to get the notifications item type class name and clean it if unsafe + */ + private function get_item_type_class_name(&$item_type, $safe = false) + { + if (!$safe) + { + $item_type = preg_replace('#[^a-z]#', '', $item_type); + } + + return 'phpbb_notifications_type_' . $item_type; + } +} diff --git a/phpBB/includes/notifications/type/base.php b/phpBB/includes/notifications/type/base.php new file mode 100644 index 0000000000..daca3b43cb --- /dev/null +++ b/phpBB/includes/notifications/type/base.php @@ -0,0 +1,327 @@ +<?php +/** +* +* @package notifications +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +use Symfony\Component\DependencyInjection\ContainerBuilder; + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ + exit; +} + +/** +* Base notifications class +* @package notifications +*/ +abstract class phpbb_notifications_type_base implements phpbb_notifications_type_interface +{ + protected $phpbb_container; + protected $service; + protected $db; + protected $phpbb_root_path; + protected $php_ext; + + /** + * Array of user data containing information needed to output the notifications to the template + * + * @var array + */ + protected $users = array(); + + /** + * Indentification data + * item_type + * item_id + * item_parent_id // Parent item id (ex: for topic => forum_id, for post => topic_id, etc) + * user_id + * unread + * + * time + * data (special serialized field that each notification type can use to store stuff) + * + * @var array $data Notification row from the database + * This must be private, all interaction should use __get(), __set(), get_data(), set_data() + */ + private $data = array(); + + public function __construct(ContainerBuilder $phpbb_container, $data = array()) + { + // phpBB Container + $this->phpbb_container = $phpbb_container; + + // Service + $this->service = $phpbb_container->get('notifications'); + + // 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'); + + // The row from the database (unless this is a new notification we're going to add) + $this->data = $data; + $this->data['data'] = (isset($this->data['data'])) ? unserialize($this->data['data']) : array(); + } + + public function __get($name) + { + return $this->data[$name]; + } + + public function __set($name, $value) + { + $this->data[$name] = $value; + } + + /** + * Get special data (only important for the classes that extend this) + * + * @param string $name Name of the variable to get + * + * @return mixed + */ + protected function get_data($name) + { + return (isset($this->data['data'][$name])) ? $this->data['data'][$name] : null; + } + + /** + * Set special data (only important for the classes that extend this) + * + * @param string $name Name of the variable to set + * @param mixed $value Value to set to the variable + */ + protected function set_data($name, $value) + { + $this->data['data'][$name] = $value; + } + + /** + * Prepare to output the notification to the template + */ + public function prepare_for_display() + { + $user = $this->phpbb_container->get('user'); + + return array( + 'AVATAR' => $this->get_avatar(), + + 'FORMATTED_TITLE' => $this->get_formatted_title(), + 'TITLE' => $this->get_title(), + + 'URL' => $this->get_url(), + 'TIME' => $user->format_date($this->time), + + 'UNREAD' => $this->unread, + ); + } + + /** + * Mark this item read + * + * @param bool $return True to return a string containing the SQL code to update this item, False to execute it (Default: False) + * @return string + */ + public function mark_read($return = true) + { + return $this->mark(false, $return); + } + + /** + * Mark this item unread + * + * @param bool $return True to return a string containing the SQL code to update this item, False to execute it (Default: False) + * @return string + */ + public function mark_unread($return = true) + { + return $this->mark(true, $return); + } + + /** + * Function for preparing the data for insertion in an SQL query + * (The service handles insertion) + * + * @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($type_data) + { + // Defaults + $data = array_merge(array( + 'item_id' => static::get_item_id($type_data), + 'item_type' => $this->get_item_type(), + 'item_parent_id' => static::get_item_parent_id($type_data), + + 'time' => time(), + 'unread' => true, + + 'data' => array(), + ), $this->data); + + $data['data'] = serialize($data['data']); + + return $data; + } + + /** + * Function for preparing the data for update in an SQL query + * (The service handles insertion) + * + * @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($type_data) + { + $data = $this->create_insert_array($type_data); + + // Unset data unique to each row + unset( + $data['notification_id'], + $data['unread'], + $data['user_id'] + ); + + return $data; + } + + /** + * -------------- Fall back functions ------------------- + */ + + /** + * Get the formatted title of this notification (fall-back) + * + * @return string + */ + public function get_formatted_title() + { + return $this->get_title(); + } + + /** + * URL to unsubscribe to this notification (fall-back) + * + * @param string|bool $method Method name to unsubscribe from (email|jabber|etc), False to unsubscribe from all notifications for this item + */ + public function get_unsubscribe_url($method = false) + { + return false; + } + + /** + * Get the user's avatar (fall-back) + */ + public function get_avatar() + { + return ''; + } + + /** + * Get the special items to load (fall-back) + */ + public function get_load_special() + { + return array(); + } + + /** + * Load the special items (fall-back) + */ + public static function load_special(ContainerBuilder $phpbb_container, $data, $notifications) + { + return; + } + + /** + * -------------- Helper functions ------------------- + */ + + /** + * Find the users who want to receive notifications (helper) + * + * @param ContainerBuilder $phpbb_container + * @param array $item_id The item_id to search for + * + * @return array + */ + protected static function _find_users_for_notification(ContainerBuilder $phpbb_container, $item_id) + { + $db = $phpbb_container->get('dbal.conn'); + + $rowset = array(); + + $sql = 'SELECT * + FROM ' . USER_NOTIFICATIONS_TABLE . " + WHERE item_type = '" . static::get_item_type() . "' + AND item_id = " . (int) $item_id; + $result = $db->sql_query($sql); + while ($row = $db->sql_fetchrow($result)) + { + if (!isset($rowset[$row['user_id']])) + { + $rowset[$row['user_id']] = array(); + } + + $rowset[$row['user_id']][] = $row['method']; + } + $db->sql_freeresult($result); + + return $rowset; + } + + /** + * Get avatar helper + * + * @param int $user_id + * @return string + */ + protected function _get_avatar($user_id) + { + $user = $this->service->get_user($user_id); + + if (!function_exists('get_user_avatar')) + { + include($this->phpbb_root_path . 'includes/functions_display.' . $this->php_ext); + } + + return get_user_avatar($user['user_avatar'], $user['user_avatar_type'], $user['user_avatar_width'], $user['user_avatar_height'], $user['username'], false, 'notifications-avatar'); + } + + /** + * Mark this item read/unread helper + * + * @param bool $unread Unread (True/False) (Default: False) + * @param bool $return True to return a string containing the SQL code to update this item, False to execute it (Default: False) + * @return string + */ + protected function mark($unread = true, $return = false) + { + $where = array( + 'item_type = ' . $this->db->sql_escape($this->item_type), + 'item_id = ' . (int) $this->item_id, + 'user_id = ' . (int) $this->user_id, + ); + $where = implode(' AND ' . $where); + + if ($return) + { + return $where; + } + + $sql = 'UPDATE ' . NOTIFICATIONS_TABLE . ' + SET unread = ' . (bool) $unread . ' + WHERE ' . $where; + $this->db->sql_query($sql); + } +} diff --git a/phpBB/includes/notifications/type/interface.php b/phpBB/includes/notifications/type/interface.php new file mode 100644 index 0000000000..c1c0eb0b0c --- /dev/null +++ b/phpBB/includes/notifications/type/interface.php @@ -0,0 +1,49 @@ +<?php +/** +* +* @package notifications +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ + exit; +} + +/** +* Base notifications interface +* @package notifications +*/ +interface phpbb_notifications_type_interface +{ + public static function get_item_type(); + + public static function get_item_id($type_data); + + public static function find_users_for_notification(ContainerBuilder $phpbb_container, $type_data); + + public function get_title(); + + public function get_formatted_title(); + + public function get_url(); + + public function get_full_url(); + + public function get_unsubscribe_url($method); + + public function mark_read($return); + + public function mark_unread($return); + + public function create_insert_array($type_data); + + public function get_load_special(); + + public static function load_special(ContainerBuilder $phpbb_container, $data, $notifications); +} diff --git a/phpBB/includes/notifications/type/pm.php b/phpBB/includes/notifications/type/pm.php new file mode 100644 index 0000000000..89f338f3f9 --- /dev/null +++ b/phpBB/includes/notifications/type/pm.php @@ -0,0 +1,184 @@ +<?php +/** +* +* @package notifications +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +use Symfony\Component\DependencyInjection\ContainerBuilder; + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ + exit; +} + +/** +* Private message notifications class +* This class handles notifications for private messages +* +* @package notifications +*/ +class phpbb_notifications_type_pm extends phpbb_notifications_type_base +{ + /** + * Get the type of notification this is + * phpbb_notifications_type_ + */ + public static function get_item_type() + { + return 'pm'; + } + + /** + * Get the id of the + * + * @param array $pm The data from the private message + */ + public static function get_item_id($pm) + { + return (int) $pm['msg_id']; + } + + /** + * Get the id of the parent + * + * @param array $pm The data from the pm + */ + public static function get_item_parent_id($pm) + { + // No parent + return 0; + } + + /** + * Find the users who want to receive notifications + * + * @param ContainerBuilder $phpbb_container + * @param array $pm Data from + * + * @return array + */ + public static function find_users_for_notification(ContainerBuilder $phpbb_container, $pm) + { + $service = $phpbb_container->get('notifications'); + $db = $phpbb_container->get('dbal.conn'); + $user = $phpbb_container->get('user'); + + if (!sizeof($pm['recipients'])) + { + return array(); + } + + $service->load_users(array_keys($pm['recipients'])); + + $notify_users = array(); + + foreach (array_keys($pm['recipients']) as $user_id) + { + $recipient = $service->get_user($user_id); + + if ($recipient['user_notify_pm']) + { + $notify_users[$recipient['user_id']] = array(); + + if ($recipient['user_notify_type'] == NOTIFY_EMAIL || $recipient['user_notify_type'] == NOTIFY_BOTH) + { + $notify_users[$recipient['user_id']][] = 'email'; + } + + if ($recipient['user_notify_type'] == NOTIFY_IM || $recipient['user_notify_type'] == NOTIFY_BOTH) + { + $notify_users[$recipient['user_id']][] = 'jabber'; + } + } + } + + return $notify_users; + } + + /** + * Get the user's avatar + */ + public function get_avatar() + { + return $this->_get_avatar($this->get_data('from_user_id')); + } + + /** + * Get the HTML formatted title of this notification + * + * @return string + */ + public function get_formatted_title() + { + $user_data = $this->service->get_user($this->get_data('from_user_id')); + + $username = get_username_string('no_profile', $user_data['user_id'], $user_data['username'], $user_data['user_colour']); + + return $this->phpbb_container->get('user')->lang('NOTIFICATION_PM', $username, $this->get_data('message_subject')); + } + + /** + * Get the plain text title of this notification + * + * @return string + */ + public function get_title() + { + $user_data = $this->service->get_user($this->get_data('from_user_id')); + + return $this->phpbb_container->get('user')->lang('NOTIFICATION_PM', $user_data['username'], $this->get_data('message_subject')); + } + + /** + * Get the url to this item + * + * @return string URL + */ + public function get_url() + { + return append_sid($this->phpbb_root_path . 'ucp.' . $this->php_ext, "i=pm&mode=view&p={$this->item_id}"); + } + + /** + * Get the full url to this item + * + * @return string URL + */ + public function get_full_url() + { + return generate_board_url() . "/ucp.{$this->php_ext}?i=pm&mode=view&p={$this->item_id}"; + } + + /** + * Users needed to query before this notification can be displayed + * + * @return array Array of user_ids + */ + public function users_to_query() + { + return array($this->data['from_user_id']); + } + + /** + * Function for preparing the data for insertion in an SQL query + * (The service handles insertion) + * + * @param array $post Data from submit_post + * + * @return array Array of data ready to be inserted into the database + */ + public function create_insert_array($pm) + { + $this->set_data('from_user_id', $pm['from_user_id']); + + $this->set_data('message_subject', $pm['message_subject']); + + return parent::create_insert_array($pm); + } +} diff --git a/phpBB/includes/notifications/type/post.php b/phpBB/includes/notifications/type/post.php new file mode 100644 index 0000000000..324a40888d --- /dev/null +++ b/phpBB/includes/notifications/type/post.php @@ -0,0 +1,220 @@ +<?php +/** +* +* @package notifications +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +use Symfony\Component\DependencyInjection\ContainerBuilder; + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ + exit; +} + +/** +* Post notifications class +* This class handles notifications for replies to a topic +* +* @package notifications +*/ +class phpbb_notifications_type_post extends phpbb_notifications_type_base +{ + /** + * Get the type of notification this is + * phpbb_notifications_type_ + */ + public static function get_item_type() + { + return 'post'; + } + + /** + * Get the id of the item + * + * @param array $post The data from the post + */ + public static function get_item_id($post) + { + return (int) $post['post_id']; + } + + /** + * Get the id of the parent + * + * @param array $post The data from the post + */ + public static function get_item_parent_id($post) + { + return (int) $post['topic_id']; + } + + /** + * Find the users who want to receive notifications + * + * @param ContainerBuilder $phpbb_container + * @param array $post Data from + * + * @return array + */ + public static function find_users_for_notification(ContainerBuilder $phpbb_container, $post) + { + // Let's continue to use the phpBB subscriptions system, at least for now. + // It may not be the nicest thing, but it is already working and it would be significant work to replace it + //$users = parent::_find_users_for_notification($phpbb_container, $post['topic_id']); + + $db = $phpbb_container->get('dbal.conn'); + + $users = array(); + + /* todo + * find what type of notification they'd like to receive + * make sure not to send duplicate notifications + */ + $sql = 'SELECT user_id + FROM ' . TOPICS_WATCH_TABLE . ' + WHERE topic_id = ' . (int) $post['topic_id'] . ' + AND notify_status = ' . NOTIFY_YES; + $result = $db->sql_query($sql); + while ($row = $db->sql_fetchrow($result)) + { + $users[$row['user_id']] = array(''); + } + $db->sql_freeresult($result); + + if (empty($users)) + { + return array(); + } + + $auth_read = $phpbb_container->get('auth')->acl_get_list(array_keys($users), 'f_read', $post['forum_id']); + + if (empty($auth_read)) + { + return array(); + } + + $notify_users = array(); + + foreach ($auth_read[$post['forum_id']]['f_read'] as $user_id) + { + $notify_users[$user_id] = $users[$user_id]; + } + + return $notify_users; + } + + /** + * Get the user's avatar + */ + public function get_avatar() + { + return $this->_get_avatar($this->get_data('poster_id')); + } + + /** + * Get the HTML formatted title of this notification + * + * @return string + */ + public function get_formatted_title() + { + if ($this->get_data('post_username')) + { + $username = $this->get_data('post_username'); + } + else + { + $user_data = $this->service->get_user($this->get_data('poster_id')); + + $username = get_username_string('no_profile', $user_data['user_id'], $user_data['username'], $user_data['user_colour']); + } + + return $this->phpbb_container->get('user')->lang( + 'NOTIFICATION_POST', + $username, + censor_text($this->get_data('topic_title')) + ); + } + + /** + * Get the title of this notification + * + * @return string + */ + public function get_title() + { + if ($this->get_data('post_username')) + { + $username = $this->get_data('post_username'); + } + else + { + $user_data = $this->service->get_user($this->get_data('poster_id')); + + $username = $user_data['username']; + } + + return $this->phpbb_container->get('user')->lang( + 'NOTIFICATION_POST', + $username, + censor_text($this->get_data('topic_title')) + ); + } + + /** + * Get the url to this item + * + * @return string URL + */ + public function get_url() + { + return append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "p={$this->item_id}#p{$this->item_id}"); + } + + /** + * Get the full url to this item + * + * @return string URL + */ + public function get_full_url() + { + return generate_board_url() . "/viewtopic.{$this->php_ext}?p={$this->item_id}#p{$this->item_id}"; + } + + /** + * Users needed to query before this notification can be displayed + * + * @return array Array of user_ids + */ + public function users_to_query() + { + return array($this->data['poster_id']); + } + + /** + * Function for preparing the data for insertion in an SQL query + * (The service handles insertion) + * + * @param array $post Data from submit_post + * + * @return array Array of data ready to be inserted into the database + */ + public function create_insert_array($post) + { + $this->set_data('poster_id', $post['poster_id']); + + $this->set_data('topic_title', $post['topic_title']); + + $this->set_data('post_username', (($post['post_username'] != $this->phpbb_container->get('user')->data['username']) ? $post['post_username'] : '')); + + $this->set_data('forum_name', $post['forum_name']); + + return parent::create_insert_array($post); + } +} diff --git a/phpBB/includes/notifications/type/quote.php b/phpBB/includes/notifications/type/quote.php new file mode 100644 index 0000000000..11186f3685 --- /dev/null +++ b/phpBB/includes/notifications/type/quote.php @@ -0,0 +1,202 @@ +<?php +/** +* +* @package notifications +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +use Symfony\Component\DependencyInjection\ContainerBuilder; + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ + exit; +} + +/** +* Post tagging notifications class +* This class handles notifications for tagging users in a post (ex: @EXreaction) +* +* @package notifications +*/ +class phpbb_notifications_type_quote extends phpbb_notifications_type_post +{ + protected static $regular_expression_match = '#\[quote="(.+?)":#'; + + /** + * Get the type of notification this is + * phpbb_notifications_type_ + */ + public static function get_item_type() + { + return 'quote'; + } + + /** + * Find the users who want to receive notifications + * + * @param ContainerBuilder $phpbb_container + * @param array $post Data from + * + * @return array + */ + public static function find_users_for_notification(ContainerBuilder $phpbb_container, $post) + { + $db = $phpbb_container->get('dbal.conn'); + + $usernames = false; + preg_match_all(self::$regular_expression_match, $post['message'], $usernames); + + if (empty($usernames[1])) + { + return array(); + } + + $usernames[1] = array_unique($usernames[1]); + + $usernames = array_map('utf8_clean_string', $usernames[1]); + + $users = array(); + + /* todo + * find what type of notification they'd like to receive + */ + $sql = 'SELECT user_id + FROM ' . USERS_TABLE . ' + WHERE ' . $db->sql_in_set('username_clean', $usernames); + $result = $db->sql_query($sql); + while ($row = $db->sql_fetchrow($result)) + { + $users[$row['user_id']] = array(''); + } + $db->sql_freeresult($result); + + if (empty($users)) + { + return array(); + } + + $auth_read = $phpbb_container->get('auth')->acl_get_list(array_keys($users), 'f_read', $post['forum_id']); + + if (empty($auth_read)) + { + return array(); + } + + $notify_users = array(); + + foreach ($auth_read[$post['forum_id']]['f_read'] as $user_id) + { + $notify_users[$user_id] = $users[$user_id]; + } + + return $notify_users; + } + + /** + * Get the HTML formatted title of this notification + * + * @return string + */ + public function get_formatted_title() + { + if ($this->get_data('post_username')) + { + $username = $this->get_data('post_username'); + } + else + { + $user_data = $this->service->get_user($this->get_data('poster_id')); + + $username = get_username_string('no_profile', $user_data['user_id'], $user_data['username'], $user_data['user_colour']); + } + + return $this->phpbb_container->get('user')->lang( + 'NOTIFICATION_QUOTE', + $username, + censor_text($this->get_data('topic_title')) + ); + } + + /** + * Get the title of this notification + * + * @return string + */ + public function get_title() + { + if ($this->get_data('post_username')) + { + $username = $this->get_data('post_username'); + } + else + { + $user_data = $this->service->get_user($this->get_data('poster_id')); + + $username = $user_data['username']; + } + + return $this->phpbb_container->get('user')->lang( + 'NOTIFICATION_QUOTE', + $username, + censor_text($this->get_data('topic_title')) + ); + } + + /** + * Update a notification + * + * @param ContainerBuilder $phpbb_container + * @param array $data Data specific for this type that will be updated + */ + public static function update_notifications(ContainerBuilder $phpbb_container, $post) + { + $service = $phpbb_container->get('notifications'); + $db = $phpbb_container->get('dbal.conn'); + + $old_notifications = array(); + $sql = 'SELECT user_id + FROM ' . NOTIFICATIONS_TABLE . " + WHERE item_type = '" . self::get_item_type() . "' + AND item_id = " . self::get_item_id($post); + $result = $db->sql_query($sql); + while ($row = $db->sql_fetchrow($result)) + { + $old_notifications[] = $row['user_id']; + } + $db->sql_freeresult($result); + + // Find the new users to notify + $notifications = self::find_users_for_notification($phpbb_container, $post); + + // Find the notifications we must delete + $remove_notifications = array_diff($old_notifications, array_keys($notifications)); + + // Find the notifications we must add + $add_notifications = array(); + foreach (array_diff(array_keys($notifications), $old_notifications) as $user_id) + { + $add_notifications[$user_id] = $notifications[$user_id]; + } + + // Add the necessary notifications + $service->add_notifications_for_users(self::get_item_type(), $post, $add_notifications); + + // Remove the necessary notifications + if (!empty($remove_notifications)) + { + $sql = 'DELETE FROM ' . NOTIFICATIONS_TABLE . " + WHERE item_type = '" . self::get_item_type() . "' + AND item_id = " . self::get_item_id($post) . ' + AND ' . $db->sql_in_set('user_id', $remove_notifications); + $db->sql_query($sql); + } + + // return true to continue with the update code in the notifications service (this will update the rest of the notifications) + return true; + } +} diff --git a/phpBB/includes/notifications/type/topic.php b/phpBB/includes/notifications/type/topic.php new file mode 100644 index 0000000000..0fce65a0cf --- /dev/null +++ b/phpBB/includes/notifications/type/topic.php @@ -0,0 +1,222 @@ +<?php +/** +* +* @package notifications +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +use Symfony\Component\DependencyInjection\ContainerBuilder; + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ + exit; +} + +/** +* Topic notifications class +* This class handles notifications for new topics +* +* @package notifications +*/ +class phpbb_notifications_type_topic extends phpbb_notifications_type_base +{ + /** + * Get the type of notification this is + * phpbb_notifications_type_ + */ + public static function get_item_type() + { + return 'topic'; + } + + /** + * Get the id of the item + * + * @param array $post The data from the post + */ + public static function get_item_id($post) + { + return (int) $post['topic_id']; + } + + /** + * Get the id of the parent + * + * @param array $post The data from the post + */ + public static function get_item_parent_id($post) + { + return (int) $post['forum_id']; + } + + /** + * Find the users who want to receive notifications + * + * @param ContainerBuilder $phpbb_container + * @param array $post Data from + * + * @return array + */ + public static function find_users_for_notification(ContainerBuilder $phpbb_container, $topic) + { + // Let's continue to use the phpBB subscriptions system, at least for now. + // It may not be the nicest thing, but it is already working and it would be significant work to replace it + //$users = parent::_find_users_for_notification($phpbb_container, $topic['forum_id']); + + $db = $phpbb_container->get('dbal.conn'); + + $users = array(); + + /* todo + * find what type of notification they'd like to receive + * make sure not to send duplicate notifications + */ + $sql = 'SELECT user_id + FROM ' . FORUMS_WATCH_TABLE . ' + WHERE forum_id = ' . (int) $topic['forum_id'] . ' + AND notify_status = ' . NOTIFY_YES; + $result = $db->sql_query($sql); + while ($row = $db->sql_fetchrow($result)) + { + $users[$row['user_id']] = array(''); + } + $db->sql_freeresult($result); + + if (empty($users)) + { + return array(); + } + + $auth_read = $phpbb_container->get('auth')->acl_get_list(array_keys($users), 'f_read', $topic['forum_id']); + + if (empty($auth_read)) + { + return array(); + } + + $notify_users = array(); + + foreach ($auth_read[$topic['forum_id']]['f_read'] as $user_id) + { + $notify_users[$user_id] = $users[$user_id]; + } + + return $notify_users; + } + + /** + * Get the user's avatar + */ + public function get_avatar() + { + return $this->_get_avatar($this->get_data('poster_id')); + } + + /** + * Get the HTML formatted title of this notification + * + * @return string + */ + public function get_formatted_title() + { + if ($this->get_data('post_username')) + { + $username = $this->get_data('post_username'); + } + else + { + $user_data = $this->service->get_user($this->get_data('poster_id')); + + $username = get_username_string('no_profile', $user_data['user_id'], $user_data['username'], $user_data['user_colour']); + } + + return $this->phpbb_container->get('user')->lang( + 'NOTIFICATION_TOPIC', + $username, + censor_text($this->get_data('topic_title')), + $this->get_data('forum_name') + ); + } + + /** + * Get the title of this notification + * + * @return string + */ + public function get_title() + { + if ($this->get_data('post_username')) + { + $username = $this->get_data('post_username'); + } + else + { + $user_data = $this->service->get_user($this->get_data('poster_id')); + + $username = $user_data['username']; + } + + return $this->phpbb_container->get('user')->lang( + 'NOTIFICATION_TOPIC', + $username, + censor_text($this->get_data('topic_title')), + $this->get_data('forum_name') + ); + } + + /** + * Get the url to this item + * + * @return string URL + */ + public function get_url() + { + return append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "f={$this->item_parent_id}&t={$this->item_id}"); + } + + /** + * Get the full url to this item + * + * @return string URL + */ + public function get_full_url() + { + return generate_board_url() . "/viewtopic.{$this->php_ext}?f={$this->item_parent_id}&t={$this->item_id}"; + } + + /** + * Users needed to query before this notification can be displayed + * + * @return array Array of user_ids + */ + public function users_to_query() + { + return array($this->data['poster_id']); + } + + /** + * Function for preparing the data for insertion in an SQL query + * (The service handles insertion) + * + * @param array $post Data from submit_post + * + * @return array Array of data ready to be inserted into the database + */ + public function create_insert_array($post) + { + $this->set_data('poster_id', $post['poster_id']); + + $this->set_data('topic_title', $post['topic_title']); + + $this->set_data('post_username', (($post['post_username'] != $this->phpbb_container->get('user')->data['username']) ? $post['post_username'] : '')); + + $this->set_data('forum_name', $post['forum_name']); + + return parent::create_insert_array($post); + } +} |