/** * Get email template variables * * @return array */ public function get_email_template_variables() { if ($this->get_data('post_username')) { $username = $this->get_data('post_username'); } else { $username = $this->user_loader->get_username($this->get_data('poster_id'), 'username'); } return array('AUTHOR_NAME' => htmlspecialchars_decode($username), 'POST_SUBJECT' => htmlspecialchars_decode(censor_text($this->get_data('post_subject'))), 'TOPIC_TITLE' => htmlspecialchars_decode(censor_text($this->get_data('topic_title'))), 'U_VIEW_POST' => generate_board_url() . "/viewtopic.{$this->php_ext}?p={$this->item_id}#p{$this->item_id}", 'U_NEWEST_POST' => generate_board_url() . "/viewtopic.{$this->php_ext}?f={$this->get_data('forum_id')}&t={$this->item_parent_id}&e=1&view=unread#unread", 'U_TOPIC' => generate_board_url() . "/viewtopic.{$this->php_ext}?f={$this->get_data('forum_id')}&t={$this->item_parent_id}", 'U_VIEW_TOPIC' => generate_board_url() . "/viewtopic.{$this->php_ext}?f={$this->get_data('forum_id')}&t={$this->item_parent_id}", 'U_FORUM' => generate_board_url() . "/viewforum.{$this->php_ext}?f={$this->get_data('forum_id')}", 'U_STOP_WATCHING_TOPIC' => generate_board_url() . "/viewtopic.{$this->php_ext}?uid={$this->user_id}&f={$this->get_data('forum_id')}&t={$this->item_parent_id}&unwatch=topic"); }
/** * Get email template variables * * @return array */ public function get_email_template_variables() { $board_url = generate_board_url(); if ($this->get_data('post_username')) { $username = $this->get_data('post_username'); } else { $username = $this->user_loader->get_username($this->get_data('poster_id'), 'username'); } return array('AUTHOR_NAME' => htmlspecialchars_decode($username), 'FORUM_NAME' => htmlspecialchars_decode($this->get_data('forum_name')), 'TOPIC_TITLE' => htmlspecialchars_decode(censor_text($this->get_data('topic_title'))), 'U_TOPIC' => "{$board_url}/viewtopic.{$this->php_ext}?f={$this->item_parent_id}&t={$this->item_id}", 'U_VIEW_TOPIC' => "{$board_url}/viewtopic.{$this->php_ext}?f={$this->item_parent_id}&t={$this->item_id}", 'U_FORUM' => "{$board_url}/viewforum.{$this->php_ext}?f={$this->item_parent_id}", 'U_STOP_WATCHING_FORUM' => "{$board_url}/viewforum.{$this->php_ext}?uid={$this->user_id}&f={$this->item_parent_id}&unwatch=forum"); }
/** * Notify using phpBB messenger * * @param int $notify_method Notify method for messenger (e.g. NOTIFY_IM) * @param string $template_dir_prefix Base directory to prepend to the email template name * * @return null */ protected function notify_using_messenger($notify_method, $template_dir_prefix = '') { if (empty($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_root_path . 'includes/functions_user.' . $this->php_ext; } $banned_users = phpbb_get_banned_user_ids($user_ids); // Load all the users we need $this->user_loader->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(); // Time to go through the queue and send emails /** @var \phpbb\notification\type\type_interface $notification */ foreach ($this->queue as $notification) { if ($notification->get_email_template() === false) { continue; } $user = $this->user_loader->get_user($notification->user_id); if ($user['user_type'] == USER_IGNORE || $user['user_type'] == USER_INACTIVE && $user['user_inactive_reason'] == INACTIVE_MANUAL || in_array($notification->user_id, $banned_users)) { continue; } $messenger->template($notification->get_email_template(), $user['user_lang'], '', $template_dir_prefix); $messenger->set_addresses($user); $messenger->assign_vars(array_merge(array('USERNAME' => $user['username'], 'U_NOTIFICATION_SETTINGS' => generate_board_url() . '/ucp.' . $this->php_ext . '?i=ucp_notifications&mode=notification_options'), $notification->get_email_template_variables())); $messenger->send($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(); }
/** * Add a notification for specific users * * @param string|array $notification_type_name Type identifier or array of item types (only acceptable if the $data is identical for the specified types) * @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($notification_type_name, $data, $notify_users) { if (is_array($notification_type_name)) { foreach ($notification_type_name as $type) { $this->add_notifications_for_users($type, $data, $notify_users); } return; } $notification_type_id = $this->get_notification_type_id($notification_type_name); $item_id = $this->get_item_type_class($notification_type_name)->get_item_id($data); $user_ids = array(); $notification_methods = array(); // Never send notifications to the anonymous user! unset($notify_users[ANONYMOUS]); // 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 // We remove each user which was already notified by at least one method. /** @var method\method_interface $method */ foreach ($this->get_subscription_methods_instances() as $method) { $notified_users = $method->get_notified_users($notification_type_id, array('item_id' => $item_id)); foreach ($notified_users as $user => $notifications) { unset($notify_users[$user]); } } if (!sizeof($notify_users)) { return; } // Allow notifications to perform actions before creating the insert array (such as run a query to cache some data needed for all notifications) $notification = $this->get_item_type_class($notification_type_name); $pre_create_data = $notification->pre_create_insert_array($data, $notify_users); unset($notification); // 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 = $this->get_item_type_class($notification_type_name); $notification->user_id = (int) $user; // Generate the insert_array $notification->create_insert_array($data, $pre_create_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 (!isset($notification_methods[$method])) { $notification_methods[$method] = $this->get_method_class($method); } $notification_methods[$method]->add_to_queue($notification); } } // We need to load all of the users to send notifications $this->user_loader->load_users($user_ids); // run the queue for each method to send notifications foreach ($notification_methods as $method) { $method->notify(); } }
/** * Executes the command user:delete * * Deletes a user from the database. An option to delete the user's posts * is available, by default posts will be retained. * * @param InputInterface $input The input stream used to get the options * @param OutputInterface $output The output stream, used to print messages * * @return int 0 if all is well, 1 if any errors occurred */ protected function execute(InputInterface $input, OutputInterface $output) { $name = $input->getArgument('username'); $mode = $input->getOption('delete-posts') ? 'remove' : 'retain'; if ($name) { $io = new SymfonyStyle($input, $output); $user_id = $this->user_loader->load_user_by_username($name); $user_row = $this->user_loader->get_user($user_id); if ($user_row['user_id'] == ANONYMOUS) { $io->error($this->language->lang('NO_USER')); return 1; } if (!function_exists('user_delete')) { require $this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext; } user_delete($mode, $user_row['user_id'], $user_row['username']); $this->log->add('admin', ANONYMOUS, '', 'LOG_USER_DELETED', false, array($user_row['username'])); $io->success($this->language->lang('USER_DELETED')); } return 0; }
/** * Executes the command user:activate * * Activate (or deactivate) a user account * * @param InputInterface $input The input stream used to get the options * @param OutputInterface $output The output stream, used to print messages * * @return int 0 if all is well, 1 if any errors occurred */ protected function execute(InputInterface $input, OutputInterface $output) { $io = new SymfonyStyle($input, $output); $name = $input->getArgument('username'); $mode = $input->getOption('deactivate') ? 'deactivate' : 'activate'; $user_id = $this->user_loader->load_user_by_username($name); $user_row = $this->user_loader->get_user($user_id); if ($user_row['user_id'] == ANONYMOUS) { $io->error($this->language->lang('NO_USER')); return 1; } // Check if the user is already active (or inactive) if ($mode == 'activate' && $user_row['user_type'] != USER_INACTIVE) { $io->error($this->language->lang('CLI_DESCRIPTION_USER_ACTIVATE_ACTIVE')); return 1; } else { if ($mode == 'deactivate' && $user_row['user_type'] == USER_INACTIVE) { $io->error($this->language->lang('CLI_DESCRIPTION_USER_ACTIVATE_INACTIVE')); return 1; } } // Activate the user account if (!function_exists('user_active_flip')) { require $this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext; } user_active_flip($mode, $user_row['user_id']); // Notify the user upon activation if ($mode == 'activate' && $this->config['require_activation'] == USER_ACTIVATION_ADMIN) { $this->send_notification($user_row, $input); } // Log and display the result $msg = $mode == 'activate' ? 'USER_ADMIN_ACTIVATED' : 'USER_ADMIN_DEACTIVED'; $log = $mode == 'activate' ? 'LOG_USER_ACTIVE' : 'LOG_USER_INACTIVE'; $this->log->add('admin', ANONYMOUS, '', $log, false, array($user_row['username'])); $this->log->add('user', ANONYMOUS, '', $log . '_USER', false, array('reportee_id' => $user_row['user_id'])); $io->success($this->language->lang($msg)); return 0; }
/** * Get email template variables * * @return array */ public function get_email_template_variables() { $user_data = $this->user_loader->get_user($this->get_data('from_user_id')); return array('AUTHOR_NAME' => htmlspecialchars_decode($user_data['username']), 'SUBJECT' => htmlspecialchars_decode(censor_text($this->get_data('message_subject'))), 'U_VIEW_MESSAGE' => generate_board_url() . '/ucp.' . $this->php_ext . "?i=pm&mode=view&p={$this->item_id}"); }
/** * {@inheritdoc} */ public function get_email_template_variables() { $user_data = $this->user_loader->get_user($this->item_id); return array('GROUP_NAME' => htmlspecialchars_decode($this->get_data('group_name')), 'REQUEST_USERNAME' => htmlspecialchars_decode($user_data['username']), 'U_PENDING' => generate_board_url() . "/ucp.{$this->php_ext}?i=groups&mode=manage&action=list&g={$this->item_parent_id}", 'U_GROUP' => generate_board_url() . "/memberlist.{$this->php_ext}?mode=group&g={$this->item_parent_id}"); }
/** * Add a notification for specific users * * @param string|array $notification_type_name Type identifier or array of item types (only acceptable if the $data is identical for the specified types) * @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($notification_type_name, $data, $notify_users) { if (is_array($notification_type_name)) { foreach ($notification_type_name as $type) { $this->add_notifications_for_users($type, $data, $notify_users); } return; } $notification_type_id = $this->get_notification_type_id($notification_type_name); $item_id = $this->get_item_type_class($notification_type_name)->get_item_id($data); $user_ids = array(); $notification_objects = $notification_methods = array(); // Never send notifications to the anonymous user! unset($notify_users[ANONYMOUS]); // 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 $sql = 'SELECT n.user_id FROM ' . $this->notifications_table . ' n, ' . $this->notification_types_table . ' nt WHERE n.notification_type_id = ' . (int) $notification_type_id . ' AND n.item_id = ' . (int) $item_id . ' AND nt.notification_type_id = n.notification_type_id AND nt.notification_type_enabled = 1'; $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; } // Allow notifications to perform actions before creating the insert array (such as run a query to cache some data needed for all notifications) $notification = $this->get_item_type_class($notification_type_name); $pre_create_data = $notification->pre_create_insert_array($data, $notify_users); unset($notification); $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, $this->notifications_table); // 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 = $this->get_item_type_class($notification_type_name); $notification->user_id = (int) $user; // Insert notification row using buffer. $insert_buffer->insert($notification->create_insert_array($data, $pre_create_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])) { $notification_methods[$method] = $this->get_method_class($method); } $notification_methods[$method]->add_to_queue($notification); } } } $insert_buffer->flush(); // We need to load all of the users to send notifications $this->user_loader->load_users($user_ids); // run the queue for each method to send notifications foreach ($notification_methods as $method) { $method->notify(); } }
/** * {@inheritdoc} */ public function get_url() { return $this->user_loader->get_username($this->item_id, 'profile'); }
/** * {@inheritdoc} */ public function load_notifications(array $options = array()) { // Merge default options $options = array_merge(array('notification_id' => false, 'user_id' => $this->user->data['user_id'], 'order_by' => 'notification_time', 'order_dir' => 'DESC', 'limit' => 0, 'start' => 0, 'all_unread' => false, 'count_unread' => false, 'count_total' => false), $options); // If all_unread, count_unread must be true $options['count_unread'] = $options['all_unread'] ? true : $options['count_unread']; // Anonymous users and bots never receive notifications if ($options['user_id'] == $this->user->data['user_id'] && ($this->user->data['user_id'] == ANONYMOUS || $this->user->data['user_type'] == USER_IGNORE)) { return array('notifications' => array(), 'unread_count' => 0, 'total_count' => 0); } $notifications = $user_ids = array(); $load_special = array(); $total_count = $unread_count = 0; if ($options['count_unread']) { // Get the total number of unread notifications $sql = 'SELECT COUNT(n.notification_id) AS unread_count FROM ' . $this->notifications_table . ' n, ' . $this->notification_types_table . ' nt WHERE n.user_id = ' . (int) $options['user_id'] . ' AND n.notification_read = 0 AND nt.notification_type_id = n.notification_type_id AND nt.notification_type_enabled = 1'; $result = $this->db->sql_query($sql); $unread_count = (int) $this->db->sql_fetchfield('unread_count'); $this->db->sql_freeresult($result); } if ($options['count_total']) { // Get the total number of notifications $sql = 'SELECT COUNT(n.notification_id) AS total_count FROM ' . $this->notifications_table . ' n, ' . $this->notification_types_table . ' nt WHERE n.user_id = ' . (int) $options['user_id'] . ' AND nt.notification_type_id = n.notification_type_id AND nt.notification_type_enabled = 1'; $result = $this->db->sql_query($sql); $total_count = (int) $this->db->sql_fetchfield('total_count'); $this->db->sql_freeresult($result); } if (!$options['count_total'] || $total_count) { $rowset = array(); // Get the main notifications $sql = 'SELECT n.*, nt.notification_type_name FROM ' . $this->notifications_table . ' n, ' . $this->notification_types_table . ' nt WHERE n.user_id = ' . (int) $options['user_id'] . ($options['notification_id'] ? is_array($options['notification_id']) ? ' AND ' . $this->db->sql_in_set('n.notification_id', $options['notification_id']) : ' AND n.notification_id = ' . (int) $options['notification_id'] : '') . ' AND nt.notification_type_id = n.notification_type_id AND nt.notification_type_enabled = 1 ORDER BY n.' . $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)) { $rowset[$row['notification_id']] = $row; } $this->db->sql_freeresult($result); // Get all unread notifications if ($unread_count && $options['all_unread'] && !empty($rowset)) { $sql = 'SELECT n.*, nt.notification_type_name FROM ' . $this->notifications_table . ' n, ' . $this->notification_types_table . ' nt WHERE n.user_id = ' . (int) $options['user_id'] . ' AND n.notification_read = 0 AND ' . $this->db->sql_in_set('n.notification_id', array_keys($rowset), true) . ' AND nt.notification_type_id = n.notification_type_id AND nt.notification_type_enabled = 1 ORDER BY n.' . $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)) { $rowset[$row['notification_id']] = $row; } $this->db->sql_freeresult($result); } foreach ($rowset as $row) { $notification = $this->notification_manager->get_item_type_class($row['notification_type_name'], $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['notification_type_name']])) { $load_special[$row['notification_type_name']] = array(); } $load_special[$row['notification_type_name']] = array_merge($load_special[$row['notification_type_name']], $notification->get_load_special()); $notifications[$row['notification_id']] = $notification; } $this->user_loader->load_users($user_ids); // Allow each type to load its own special items foreach ($load_special as $item_type => $data) { $item_class = $this->notification_manager->get_item_type_class($item_type); $item_class->load_special($data, $notifications); } } return array('notifications' => $notifications, 'unread_count' => $unread_count, 'total_count' => $total_count); }