############################################################## 
## MOD Title:          Admin add users 
## MOD Author:         Niels < ncr@db9.dk > (Niels Chr. Rd) http://mods.db9.dk 
## MOD Description:    Admin can now create a new user, using admin panel
##                     user management. In file admin_users.php is defined
##                     witch user should be used as "standard", so you may
##                     configure what default settings new users will have.
## MOD Version:        0.10.1 Beta 
## Compatibility:      2.0.5->2.0.6
## 
## Installation Level: Easy
## Installation Time:  3 Minutes (1mn by EasyMOD of Nuttzy)
## Files To Edit:      4
##      admin/admin_users.php
##      language/lang_english/lang_admin.php
##      templates/subSilver/admin/user_edit_body.tpl
##      templates/subSilver/admin/user_select_body.tpl
##
## Included Files:     0 
## 
############################################################## 
## For Security Purposes, Please Check: http://www.phpbb.com/mods/ for the 
## latest version of this MOD. Downloading this MOD from other sites could cause malicious code 
## to enter into your phpBB Forum. As such, phpBB will not offer support for MOD's not offered 
## in our MOD-Database, located at: http://www.phpbb.com/mods/ 
############################################################## 
## Author Notes: 
##
## 1. Full MOD description
## -----------
## Admin can now create a new user, using admin panel user management.
## In file admin_users.php is defined witch user should be used as
## "standard", so you may configure what default settings new users
## will have.
## This also support the usergroups, so makeing the "default" user
## member of some usergroups, will make all new users created from
## ACP members of the same usergroups.
## The default password is also hardcoded into this file (look in
## the top of the file) - admin may of course overwrite this, when
## creating the users.
##
## 2. EasyMOD
## -----------
## This MOD is compatible and can be installed by EasyMOD
## of Nuttzy (but is not officially EasyMOD Compliant)!
## http://area51.phpbb.com/phpBB22/viewforum.php?sid=&f=15
##
## However, on alpha releases of EM and meanwhile beta or 
## final release some actions are NOT performed.
## You'll have to do them manually !
##
## 2.1 Translation are not managed
## -----------
## EM can not already manage actions for any other
## language than English (but language intructions are proceed
## to all installed languages in order to prevent errors).
## So the translations provided with this MOD must be installed
## manually if you need them.
##
## 3. Official last version link
## -----------
## Meanwhile the phpBB group validation and as the MOD is not yet
## in the phpBB MOD database, check this official link for updates...
## http://mods.db9.dk/viewtopic.php?t=1475
##
############################################################## 
## MOD History: 
## 
##   2003-12-05 - Version 0.10.1
##      - phpBB template & EasyMOD compliance enhancement
##      - Fix 2 instruction errors
##      - Add delimiters for the MOD code inserted
##
##   2003-08-05 - Version 0.10.0
##      - complete re-write of the mod
##      - support a "user template", witch make it posible to
##        define how standart users should be set up 
##      - you may define a "standart" password 
##      - new users become members of the same usergroups as
##        the standart user 
##      - admin may "cancel" the creation of the user, as long
##        as not submitted 
##      - works toghether with protect user account mod
##        (force user to change password) 
##      - EM ready 
##
##   ????-??-?? - Version 0.9.2
##      - change for 2.0.6. + made compt. with attachemt mod
##
##   ????-??-?? - Version 0.9.1
##      - corrected that add button was showen into user permission
##        page as well
##
##   ????-??-?? - Version 1.2.5
##      - Initial release
## 
############################################################## 
## Before Adding This MOD To Your Forum, You Should Back Up All Files Related To This MOD 
############################################################## 
# 
#-----[ OPEN ]------------------------------------------------
# 
admin/admin_users.php

# 
#-----[ FIND ]------------------------------------------------ 
# 
define('IN_PHPBB', 1);

# 
#-----[ AFTER, ADD ]------------------------------------------ 
# 

// Start add - Admin add user MOD
// define a "dummy user", the profile settings of this user, will be used as default settings for new users
define('DEFAULT_USER_ID', 2);
define('DEFAULT_PASSWD', '123456');
// End add - Admin add user MOD

# 
#-----[ FIND ]------------------------------------------------ 
#
//
// Begin program

# 
#-----[ BEFORE, ADD ]----------------------------------------- 
#

// Start add - Admin add user MOD
$new_user = (isset($HTTP_POST_VARS['new_user'])) ? (($HTTP_POST_VARS['new_user']==TRUE) ? TRUE : 0 ) : 0 ;
if ($new_user)
{
	//see if user already exist
	if (get_userdata($HTTP_POST_VARS['username']))
	{
		message_die(GENERAL_MESSAGE, $lang['Username_taken'] );
	}
	//see if default user exist
	if ( !($default_user = get_userdata(DEFAULT_USER_ID) ) )
	{
		message_die(CRITICAL_MESSAGE, 'The DEFAULT_USER_ID are not set correctly, please correct this in admin/admin_users.php');
	}
	if ($mode == 'save' && isset( $HTTP_POST_VARS['submit'] ) )
	{
		//we need to create the user
		$sql = "SELECT MAX(user_id) AS total
			FROM " . USERS_TABLE;
		if ( !($result = $db->sql_query($sql)) )
		{
			message_die(GENERAL_ERROR, 'Could not obtain next user_id information', '', __LINE__, __FILE__, $sql);
		}
		if ( !($row = $db->sql_fetchrow($result)) )
		{
			message_die(GENERAL_ERROR, 'Could not obtain next user_id information', '', __LINE__, __FILE__, $sql);
		}
		$user_id = $row['total'] + 1;
		$sql = "INSERT INTO " . USERS_TABLE . "	(user_id, username, user_regdate, user_active)
			VALUES ($user_id, 'new_user', " . time() . ",'0')";
		if ( !($result = $db->sql_query($sql, BEGIN_TRANSACTION)) )
		{
			message_die(GENERAL_ERROR, 'Could not insert data into users table', '', __LINE__, __FILE__, $sql);
		}
		$sql = "INSERT INTO " . GROUPS_TABLE . " (group_name, group_description, group_single_user, group_moderator)
			VALUES ('', 'Personal User', 1, 0)";
		if ( !($result = $db->sql_query($sql)) )
		{
			message_die(GENERAL_ERROR, 'Could not insert data into groups table', '', __LINE__, __FILE__, $sql);
		}
		$group_id = $db->sql_nextid();
		//go get the usergroups, the default user are member of
		$sql = "SELECT g.group_id
			FROM " . USER_GROUP_TABLE . " ug, " . GROUPS_TABLE . " g
			WHERE NOT g.group_single_user AND ug.group_id=g.group_id AND ug.user_id='".DEFAULT_USER_ID."'";
		if ( !($result = $db->sql_query($sql)) )
		{
			message_die(GENERAL_ERROR, 'Could not obtain default user group information', '', __LINE__, __FILE__, $sql);
		}
		while ($group_data = $db->sql_fetchrow($result))
		{
			//user join default groups
			$sql = "INSERT INTO " . USER_GROUP_TABLE . " (group_id, user_id, user_pending) 
				VALUES (".$group_data['group_id'].", $user_id, '0')";
			if ( !($db->sql_query($sql)) )
			{
				message_die(GENERAL_ERROR, 'Error insert default groupst', '', __LINE__, __FILE__, $sql);
			}
		}

		$sql = "INSERT INTO " . USER_GROUP_TABLE . " (user_id, group_id, user_pending)
			VALUES ($user_id, $group_id, 0)";
		if( !($result = $db->sql_query($sql, END_TRANSACTION)) )
		{
			message_die(GENERAL_ERROR, 'Could not insert data into user_group table', '', __LINE__, __FILE__, $sql);
		}
		$HTTP_POST_VARS[POST_USERS_URL] = $user_id;
	} else
	{
		//make script use default user as a starting point
		$HTTP_POST_VARS[POST_USERS_URL] = DEFAULT_USER_ID;
	}
}
// End add - Admin add user MOD

# 
#-----[ FIND ]------------------------------------------------ 
#
$user_id = intval($HTTP_POST_VARS['id']);

# 
#-----[ REPLACE WITH ]---------------------------------------- 
#

// Start replacement - Admin add user MOD
$user_id = ($new_user) ? $user_id : intval($HTTP_POST_VARS['id']);
// End replacement - Admin add user MOD

# 
#-----[ FIND ]------------------------------------------------ 
#
if( $HTTP_POST_VARS['deleteuser'] )

# 
#-----[ IN-LINE FIND ]---------------------------------------- 
#
)

# 
#-----[ IN-LINE BEFORE, ADD ]--------------------------------- 
#
&& $new_user==0

# 
#-----[ FIND ]------------------------------------------------ 
#
if ($signature != '')

# 
#-----[ BEFORE, ADD ]----------------------------------------- 
#

// Start add - Admin add user MOD
if ($new_user)
{
	//no password given for this new user, create default password
	$password = md5(DEFAULT_PASSWD);
	$passwd_sql = "user_password = '$password', ";
	//send out email notification goes here
}
// End add - Admin add user MOD

# 
#-----[ FIND ]------------------------------------------------ 
#
//
// Now parse and display it as a template

# 
#-----[ BEFORE, ADD ]----------------------------------------- 
#

// Start add - Admin add user MOD
		if ($new_user)
		{
			$this_userdata['username'] = $HTTP_POST_VARS['username'];
			$this_userdata['user_email'] = '';
			$this_userdata['user_passwd_change'] = 0;
		} else
		{
			$template->assign_block_vars('switch_show_delete', array());
		}
// End add - Admin add user MOD

# 
#-----[ FIND ]------------------------------------------------ 
#
$s_hidden_fields .= '<input type="hidden" name="id" 

# 
#-----[ BEFORE, ADD ]----------------------------------------- 
#

// Start add - Admin add user MOD
$s_hidden_fields .= '<input type="hidden" name="new_user" value="'.$new_user.'" />';
// End add - Admin add user MOD

# 
#-----[ FIND ]------------------------------------------------ 
#
$lang['User_admin_explain'],

# 
#-----[ IN-LINE FIND ]---------------------------------------- 
#
$lang['User_admin_explain'],

# 
#-----[ IN-LINE REPLACE WITH ]-------------------------------- 
#
($new_user) ? sprintf( $lang['Create_user_explain'],'<a href="'.append_sid('/profile.'.$phpEx.'?mode=viewprofile&'.POST_USERS_URL.'='.$default_user['user_id']).'">'.$default_user['username'].'</a>', DEFAULT_PASSWD ) : $lang['User_admin_explain'],

# 
#-----[ FIND ]------------------------------------------------ 
#
$s_hidden_fields .= '<input type="hidden" name="id" 

# 
#-----[ BEFORE, ADD ]----------------------------------------- 
#

// Start add - Admin add user MOD
$s_hidden_fields .= '<input type="hidden" name="new_user" value="'.$new_user.'" />';
// End add - Admin add user MOD

# 
#-----[ FIND ]------------------------------------------------ 
#
$lang['User_admin_explain'],

# 
#-----[ IN-LINE FIND ]---------------------------------------- 
#
$lang['User_admin_explain'],

# 
#-----[ IN-LINE REPLACE WITH ]-------------------------------- 
#
($new_user) ? sprintf( $lang['Create_user_explain'],'<a href="'.append_sid('/profile.'.$phpEx.'?mode=viewprofile&'.POST_USERS_URL.'='.$default_user['user_id']).'">'.$default_user['username'].'</a>', DEFAULT_PASSWD ) : $lang['User_admin_explain'],

# 
#-----[ FIND ]------------------------------------------------ 
#
'body' => 'admin/user_select_body.tpl')
);

# 
#-----[ AFTER, ADD ]------------------------------------------ 
#

// Start add - Admin add user MOD
$template->assign_block_vars('switch_add_user_on', array());
// End add - Admin add user MOD

# 
#-----[ FIND ]------------------------------------------------ 
#
'L_FIND_USERNAME' => $lang['Find_username'],

# 
#-----[ AFTER, ADD ]------------------------------------------ 
#

// Start add - Admin add user MOD
'L_CREATE_USER' => $lang['Create_user'],
// End add - Admin add user MOD

# 
#-----[ OPEN ]------------------------------------------------ 
#
language/lang_english/lang_admin.php

# 
#-----[ FIND ]------------------------------------------------ 
#
?>

# 
#-----[ BEFORE, ADD ]----------------------------------------- 
#

// Start add - Admin add user MOD
$lang['Create_user'] = 'Create new user';
$lang['Create_user_explain'] = 'You are about to create a new user, when creating a new user, the script will look up the data from this user %s, the user ID of this user is hard coded into the file admin_users.php, you may change this setting in the top of this file if another user ID should be used.<br />There is 2 exeptions from this: <br />1. users Password will default to "%s" if you do not specify differently into the admin add user page<br />2. users email must be filled into the admin add user page';
// End add - Admin add user MOD

# 
#-----[ OPEN ]------------------------------------------------ 
# 
templates/subSilver/admin/user_edit_body.tpl 

# 
#-----[ FIND ]------------------------------------------------ 
#
<tr> 
{L_DELETE_USER}

# 
#-----[ BEFORE, ADD ]----------------------------------------- 
#

<!-- Start add - Admin add user MOD -->
<!-- BEGIN switch_show_delete -->
<!-- End add - Admin add user MOD -->

# 
#-----[ FIND ]------------------------------------------------ 
#
{L_DELETE_USER_EXPLAIN}
</tr>

# 
#-----[ AFTER, ADD ]------------------------------------------ 
#

<!-- Start add - Admin add user MOD -->
<!-- END switch_show_delete -->
<!-- End add - Admin add user MOD -->

# 
#-----[ OPEN ]------------------------------------------------ 
# 
templates/subSilver/admin/user_select_body.tpl 

# 
#-----[ FIND ]------------------------------------------------ 
#
<input type="text" class="post" name="username"

# 
#-----[ IN-LINE FIND ]---------------------------------------- 
#
;return false;" />

# 
#-----[ IN-LINE AFTER, ADD ]---------------------------------- 
#
<input type="checkbox" name="new_user">{L_CREATE_USER}

# 
#-----[ SAVE/CLOSE ALL FILES ]-------------------------------- 
# 
# EoM