From d607f1c927ed6fb54d6c46eb13b5bcd4133f8cfc Mon Sep 17 00:00:00 2001
From: Cesar G <prototech91@gmail.com>
Date: Thu, 24 Oct 2013 02:37:20 -0700
Subject: [PATCH 1/2] [ticket/11746] Add "admin activation required"
 notification.

PHPBB3-11746
---
 phpBB/config/notifications.yml                |  18 ++
 phpBB/includes/acp/acp_users.php              |   3 +
 phpBB/includes/ucp/ucp_activate.php           |   5 +-
 phpBB/includes/ucp/ucp_register.php           |  43 +----
 phpBB/language/en/common.php                  |   1 +
 phpBB/language/en/ucp.php                     |   2 +
 .../notification/type/admin_activate_user.php | 174 ++++++++++++++++++
 7 files changed, 211 insertions(+), 35 deletions(-)
 create mode 100644 phpBB/phpbb/notification/type/admin_activate_user.php

diff --git a/phpBB/config/notifications.yml b/phpBB/config/notifications.yml
index 6fecae2aeb..c4295d8e52 100644
--- a/phpBB/config/notifications.yml
+++ b/phpBB/config/notifications.yml
@@ -319,6 +319,24 @@ services:
         tags:
             - { name: notification.type }
 
+    notification.type.admin_activate_user:
+        class: phpbb\notification\type\admin_activate_user
+        scope: prototype # scope MUST be prototype for this to work!
+        arguments:
+            - @user_loader
+            - @dbal.conn
+            - @cache.driver
+            - @user
+            - @auth
+            - @config
+            - %core.root_path%
+            - %core.php_ext%
+            - %tables.notification_types%
+            - %tables.notifications%
+            - %tables.user_notifications%
+        tags:
+            - { name: notification.type }
+
     notification.method.email:
         class: phpbb\notification\method\email
         scope: prototype # scope MUST be prototype for this to work!
diff --git a/phpBB/includes/acp/acp_users.php b/phpBB/includes/acp/acp_users.php
index 8853200ddc..2a79ec05c0 100644
--- a/phpBB/includes/acp/acp_users.php
+++ b/phpBB/includes/acp/acp_users.php
@@ -396,6 +396,9 @@ class acp_users
 							{
 								if ($config['require_activation'] == USER_ACTIVATION_ADMIN)
 								{
+									$phpbb_notifications = $phpbb_container->get('notification_manager');
+									$phpbb_notifications->mark_notifications_read('admin_activate_user', $user_row['user_id'], false);
+
 									include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
 
 									$messenger = new messenger(false);
diff --git a/phpBB/includes/ucp/ucp_activate.php b/phpBB/includes/ucp/ucp_activate.php
index 898dacd831..6af0154334 100644
--- a/phpBB/includes/ucp/ucp_activate.php
+++ b/phpBB/includes/ucp/ucp_activate.php
@@ -27,7 +27,7 @@ class ucp_activate
 	function main($id, $mode)
 	{
 		global $config, $phpbb_root_path, $phpEx;
-		global $db, $user, $auth, $template;
+		global $db, $user, $auth, $template, $phpbb_container;
 
 		$user_id = request_var('u', 0);
 		$key = request_var('k', '');
@@ -108,6 +108,9 @@ class ucp_activate
 
 		if ($config['require_activation'] == USER_ACTIVATION_ADMIN && !$update_password)
 		{
+			$phpbb_notifications = $phpbb_container->get('notification_manager');
+			$phpbb_notifications->mark_notifications_read('admin_activate_user', $user_row['user_id'], false);
+
 			include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
 
 			$messenger = new messenger(false);
diff --git a/phpBB/includes/ucp/ucp_register.php b/phpBB/includes/ucp/ucp_register.php
index 1f9ab23326..1641c6eef1 100644
--- a/phpBB/includes/ucp/ucp_register.php
+++ b/phpBB/includes/ucp/ucp_register.php
@@ -379,41 +379,16 @@ class ucp_register
 					}
 
 					$messenger->send(NOTIFY_EMAIL);
+				}
 
-					if ($config['require_activation'] == USER_ACTIVATION_ADMIN)
-					{
-						// Grab an array of user_id's with a_user permissions ... these users can activate a user
-						$admin_ary = $auth->acl_get_list(false, 'a_user', false);
-						$admin_ary = (!empty($admin_ary[0]['a_user'])) ? $admin_ary[0]['a_user'] : array();
-
-						// Also include founders
-						$where_sql = ' WHERE user_type = ' . USER_FOUNDER;
-
-						if (sizeof($admin_ary))
-						{
-							$where_sql .= ' OR ' . $db->sql_in_set('user_id', $admin_ary);
-						}
-
-						$sql = 'SELECT user_id, username, user_email, user_lang, user_jabber, user_notify_type
-							FROM ' . USERS_TABLE . ' ' .
-							$where_sql;
-						$result = $db->sql_query($sql);
-
-						while ($row = $db->sql_fetchrow($result))
-						{
-							$messenger->template('admin_activate', $row['user_lang']);
-							$messenger->set_addresses($row);
-
-							$messenger->assign_vars(array(
-								'USERNAME'			=> htmlspecialchars_decode($data['username']),
-								'U_USER_DETAILS'	=> "$server_url/memberlist.$phpEx?mode=viewprofile&u=$user_id",
-								'U_ACTIVATE'		=> "$server_url/ucp.$phpEx?mode=activate&u=$user_id&k=$user_actkey")
-							);
-
-							$messenger->send($row['user_notify_type']);
-						}
-						$db->sql_freeresult($result);
-					}
+				if ($config['require_activation'] == USER_ACTIVATION_ADMIN)
+				{
+					$phpbb_notifications = $phpbb_container->get('notification_manager');
+					$phpbb_notifications->add_notifications('admin_activate_user', array(
+						'user_id'		=> $user_id,
+						'user_actkey'	=> $user_row['user_actkey'],
+						'user_regdate'	=> $user_row['user_regdate'],
+					));
 				}
 
 				// Perform account linking if necessary
diff --git a/phpBB/language/en/common.php b/phpBB/language/en/common.php
index bb1c4698ec..1d75c6ea0d 100644
--- a/phpBB/language/en/common.php
+++ b/phpBB/language/en/common.php
@@ -430,6 +430,7 @@ $lang = array_merge($lang, array(
 	'NOTIFICATION_TOPIC_DISAPPROVED'	=> 'Your topic "%1$s" was disapproved for reason: "%2$s".',
 	'NOTIFICATION_TOPIC_IN_QUEUE'		=> 'A new topic titled "%2$s" was posted by %1$s and needs approval.',
 	'NOTIFICATION_TYPE_NOT_EXIST'		=> 'The notification type "%s" is missing from the file system.',
+	'NOTIFICATION_ADMIN_ACTIVATE_USER'	=> 'The user ā€œ%1$sā€ is newly registered and requires activation.',
 	'NOTIFY_ADMIN'				=> 'Please notify the board administrator or webmaster.',
 	'NOTIFY_ADMIN_EMAIL'		=> 'Please notify the board administrator or webmaster: <a href="mailto:%1$s">%1$s</a>',
 	'NO_ACCESS_ATTACHMENT'		=> 'You are not allowed to access this file.',
diff --git a/phpBB/language/en/ucp.php b/phpBB/language/en/ucp.php
index 2f4d35a5b4..161ec6c67f 100644
--- a/phpBB/language/en/ucp.php
+++ b/phpBB/language/en/ucp.php
@@ -312,6 +312,7 @@ $lang = array_merge($lang, array(
 	'NOTIFICATIONS_MARK_ALL_READ_SUCCESS'				=> 'All notifications have been marked read.',
 	'NOTIFICATION_GROUP_MISCELLANEOUS'					=> 'Miscellaneous Notifications',
 	'NOTIFICATION_GROUP_MODERATION'						=> 'Moderation Notifications',
+	'NOTIFICATION_GROUP_ADMINISTRATION'					=> 'Administration Notifications',
 	'NOTIFICATION_GROUP_POSTING'						=> 'Posting Notifications',
 	'NOTIFICATION_METHOD_EMAIL'							=> 'Email',
 	'NOTIFICATION_METHOD_JABBER'						=> 'Jabber',
@@ -325,6 +326,7 @@ $lang = array_merge($lang, array(
 	'NOTIFICATION_TYPE_QUOTE'							=> 'Someone quotes you in a post',
 	'NOTIFICATION_TYPE_REPORT'							=> 'Someone reports a post',
 	'NOTIFICATION_TYPE_TOPIC'				   			=> 'Someone creates a topic in a forum to which you are subscribed',
+	'NOTIFICATION_TYPE_ADMIN_ACTIVATE_USER'				=> 'New registered user requiring activation',
 
 	'NOTIFY_METHOD'					=> 'Notification method',
 	'NOTIFY_METHOD_BOTH'			=> 'Both',
diff --git a/phpBB/phpbb/notification/type/admin_activate_user.php b/phpBB/phpbb/notification/type/admin_activate_user.php
new file mode 100644
index 0000000000..1231c0b75d
--- /dev/null
+++ b/phpBB/phpbb/notification/type/admin_activate_user.php
@@ -0,0 +1,174 @@
+<?php
+/**
+*
+* @package notifications
+* @copyright (c) 2013 phpBB Group
+* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
+*
+*/
+
+namespace phpbb\notification\type;
+
+/**
+* @ignore
+*/
+if (!defined('IN_PHPBB'))
+{
+	exit;
+}
+
+/**
+* Admin activation notifications class
+* This class handles notifications for users requiring admin activation
+*
+* @package notifications
+*/
+class admin_activate_user extends \phpbb\notification\type\base
+{
+	/**
+	* {@inheritdoc}
+	*/
+	public function get_type()
+	{
+		return 'admin_activate_user';
+	}
+
+	/**
+	* {@inheritdoc}
+	*/
+	protected $language_key = 'NOTIFICATION_ADMIN_ACTIVATE_USER';
+
+	/**
+	* {@inheritdoc}
+	*/
+	public static $notification_option = array(
+		'lang'	=> 'NOTIFICATION_TYPE_ADMIN_ACTIVATE_USER',
+		'group'	=> 'NOTIFICATION_GROUP_ADMINISTRATION',
+	);
+
+	/**
+	* {@inheritdoc}
+	*/
+	public function is_available()
+	{
+		return ($this->auth->acl_get('a_user') && $this->config['require_activation'] == USER_ACTIVATION_ADMIN);
+	}
+
+	/**
+	* {@inheritdoc}
+	*/
+	public static function get_item_id($user)
+	{
+		return (int) $user['user_id'];
+	}
+
+	/**
+	* {@inheritdoc}
+	*/
+	public static function get_item_parent_id($post)
+	{
+		return 0;
+	}
+
+	/**
+	* {@inheritdoc}
+	*/
+	public function find_users_for_notification($user, $options = array())
+	{
+		$options = array_merge(array(
+			'ignore_users'	=> array(),
+		), $options);
+
+		// Grab admins that have permission to administer users.
+		$admin_ary = $this->auth->acl_get_list(false, 'a_user', false);
+		$users = (!empty($admin_ary[0]['a_user'])) ? $admin_ary[0]['a_user'] : array();
+
+		// Grab founders
+		$sql = 'SELECT user_id
+			FROM ' . USERS_TABLE . '
+			WHERE user_type = ' . USER_FOUNDER;
+		$result = $this->db->sql_query($sql);
+
+		while ($row = $this->db->sql_fetchrow($sql))
+		{
+			$users[] = (int) $row['user_id'];
+		}
+		$this->db->sql_freeresult($result);
+
+		if (empty($users))
+		{
+			return array();
+		}
+		$users = array_unique($users);
+
+		return $this->check_user_notification_options($users, $options);
+	}
+
+	/**
+	* {@inheritdoc}
+	*/
+	public function get_avatar()
+	{
+		return $this->user_loader->get_avatar($this->item_id);
+	}
+
+	/**
+	* {@inheritdoc}
+	*/
+	public function get_title()
+	{
+		$username = $this->user_loader->get_username($this->item_id, 'no_profile');
+
+		return $this->user->lang($this->language_key, $username);
+	}
+
+	/**
+	* {@inheritdoc}
+	*/
+	public function get_email_template()
+	{
+		return 'admin_activate';
+	}
+
+	/**
+	* {@inheritdoc}
+	*/
+	public function get_email_template_variables()
+	{
+		$board_url = generate_board_url();
+		$username = $this->user_loader->get_username($this->item_id, 'no_profile');
+
+		return array(
+			'USERNAME'			=> htmlspecialchars_decode($username),
+			'U_USER_DETAILS'	=> "{$board_url}/memberlist.{$this->php_ext}?mode=viewprofile&u={$this->item_id}",
+			'U_ACTIVATE'		=> "{$board_url}/ucp.{$this->php_ext}?mode=activate&u={$this->item_id}&k={$this->get_data('user_actkey')}",
+		);
+	}
+
+	/**
+	* {@inheritdoc}
+	*/
+	public function get_url()
+	{
+		return append_sid($this->phpbb_root_path . 'memberlist.' . $this->php_ext, "mode=viewprofile&u={$this->item_id}");
+	}
+
+	/**
+	* {@inheritdoc}
+	*/
+	public function users_to_query()
+	{
+		return array($this->item_id);
+	}
+
+	/**
+	* {@inheritdoc}
+	*/
+	public function create_insert_array($user, $pre_create_data)
+	{
+		$this->set_data('user_actkey', $user['user_actkey']);
+		$this->notification_time = $user['user_regdate'];
+
+		return parent::create_insert_array($user, $pre_create_data);
+	}
+}

From c5fc8c43cbca82d2a064859828950477cfd5d767 Mon Sep 17 00:00:00 2001
From: Cesar G <prototech91@gmail.com>
Date: Sat, 26 Oct 2013 01:01:08 -0700
Subject: [PATCH 2/2] [ticket/11746] Delete the notification after user is
 approved & fix language.

PHPBB3-11746
---
 phpBB/includes/acp/acp_users.php    | 2 +-
 phpBB/includes/ucp/ucp_activate.php | 2 +-
 phpBB/language/en/ucp.php           | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/phpBB/includes/acp/acp_users.php b/phpBB/includes/acp/acp_users.php
index 2a79ec05c0..3f1f39724e 100644
--- a/phpBB/includes/acp/acp_users.php
+++ b/phpBB/includes/acp/acp_users.php
@@ -397,7 +397,7 @@ class acp_users
 								if ($config['require_activation'] == USER_ACTIVATION_ADMIN)
 								{
 									$phpbb_notifications = $phpbb_container->get('notification_manager');
-									$phpbb_notifications->mark_notifications_read('admin_activate_user', $user_row['user_id'], false);
+									$phpbb_notifications->delete_notifications('admin_activate_user', $user_row['user_id']);
 
 									include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
 
diff --git a/phpBB/includes/ucp/ucp_activate.php b/phpBB/includes/ucp/ucp_activate.php
index 6af0154334..2a94acbe02 100644
--- a/phpBB/includes/ucp/ucp_activate.php
+++ b/phpBB/includes/ucp/ucp_activate.php
@@ -109,7 +109,7 @@ class ucp_activate
 		if ($config['require_activation'] == USER_ACTIVATION_ADMIN && !$update_password)
 		{
 			$phpbb_notifications = $phpbb_container->get('notification_manager');
-			$phpbb_notifications->mark_notifications_read('admin_activate_user', $user_row['user_id'], false);
+			$phpbb_notifications->delete_notifications('admin_activate_user', $user_row['user_id']);
 
 			include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
 
diff --git a/phpBB/language/en/ucp.php b/phpBB/language/en/ucp.php
index 161ec6c67f..9a5713e1cd 100644
--- a/phpBB/language/en/ucp.php
+++ b/phpBB/language/en/ucp.php
@@ -326,7 +326,7 @@ $lang = array_merge($lang, array(
 	'NOTIFICATION_TYPE_QUOTE'							=> 'Someone quotes you in a post',
 	'NOTIFICATION_TYPE_REPORT'							=> 'Someone reports a post',
 	'NOTIFICATION_TYPE_TOPIC'				   			=> 'Someone creates a topic in a forum to which you are subscribed',
-	'NOTIFICATION_TYPE_ADMIN_ACTIVATE_USER'				=> 'New registered user requiring activation',
+	'NOTIFICATION_TYPE_ADMIN_ACTIVATE_USER'				=> 'Newly registered user requiring activation',
 
 	'NOTIFY_METHOD'					=> 'Notification method',
 	'NOTIFY_METHOD_BOTH'			=> 'Both',