Adding users

Discussion forum for MOD Writers regarding MOD Development.
MaxJ84
Registered User
Posts: 13
Joined: Tue Oct 21, 2008 12:02 am

Adding users

Post by MaxJ84 » Tue Oct 21, 2008 2:01 am

Hello,

I have 307 users to add to the forum, with predefined user names and I generated random passwords. Is there any way that I can generate a PHP file to add all these users? I can make something that will generate the PHP lines to add each user, so I really just need to know how to add one user with a custom PHP script, and then I'll multiply it by the number of users I have to enter...

Thank you very much!!!
I'd really appreciate a fast reply on this... Been working 3 days on a row on that forum, now I'm almost done, just can't find a way to add users.

Thank you very much!

MJ

User avatar
D¡cky
Former Team Member
Posts: 11812
Joined: Tue Jan 25, 2005 8:38 pm
Location: New Hampshire, USA
Name: Richard Foote
Contact:

Re: Adding users

Post by D¡cky » Tue Oct 21, 2008 3:10 am

There is no easy way at this time. There is a MOD, ACP Add User MOD to add users one at a time. Other than that, you could write your own script using the function user_add() in includes/functions_user.php
Have you hugged someone today?

MaxJ84
Registered User
Posts: 13
Joined: Tue Oct 21, 2008 12:02 am

Re: Adding users

Post by MaxJ84 » Tue Oct 21, 2008 2:32 pm

D¡cky wrote:Other than that, you could write your own script using the function user_add() in includes/functions_user.php
That sounds like a good idea. I'll generate the whole script with a spreadsheet software or in C, I'll run it on the server and the job will be done. :)

Now, how do I go about this? I was trying to firstly simply hash the passwords to add them manually in the database, but using user_add() might be much faster and easier.

I had this: (But now ameeck told me I had to include the complete phpBB headers...?)

Code: Select all

<?php
define('IN_PHPBB', true);
include("includes/functions.php");
include_once("includes/functions_user.php");
echo phpbb_hash("6Dc0QEwp") . "<br />";
?>
I suppose I should remove the echo line and replace it by a call to user_add() ?
Can someone show me how to do a call to this function? I only have the username and password to enter. I'll go in the database to change the timezone, gmt, some custom fields and anything else that might be left to add/change for the new users. I want to leave the email field blank so they can, on first login, go in the UCP and enter their own email address.

MaxJ84
Registered User
Posts: 13
Joined: Tue Oct 21, 2008 12:02 am

Re: Adding users

Post by MaxJ84 » Tue Oct 21, 2008 5:02 pm

For example, I want to add the user "User1" with password "pwdUser1", what would the PHP file adding that user be like?

User avatar
Brf
Support Team Member
Support Team Member
Posts: 51874
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: Adding users

Post by Brf » Tue Oct 21, 2008 5:09 pm

You are not going to get anywhere without including common.php.

MaxJ84
Registered User
Posts: 13
Joined: Tue Oct 21, 2008 12:02 am

Re: Adding users

Post by MaxJ84 » Tue Oct 21, 2008 5:17 pm

Okay... Here's a better attempt I guess... Is this what I need? What's missing? How does that work? What's that cp_data? I'm not kidding, I know nothing about this, and I really need help.

Code: Select all

<?php
define('IN_PHPBB', true);
include("includes/functions.php");
include_once("includes/functions_user.php");
include("includes/common.php");
$user_row = array(
					'username'				=> "User1",
					'user_password'			=> phpbb_hash("PwdUser1"),
					'user_timezone'			=> -5,
					'user_dst'				=> 1,
				);
user_add($user_row, $cp_data);
$user_row = array(
					'username'				=> "User2",
					'user_password'			=> phpbb_hash("PwdUser2"),
					'user_timezone'			=> -5,
					'user_dst'				=> 1,
				);
user_add($user_row, $cp_data);
// etc., etc., etc.
?>
Thank you very much,

MJ

User avatar
Brf
Support Team Member
Support Team Member
Posts: 51874
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: Adding users

Post by Brf » Tue Oct 21, 2008 5:19 pm

Use this article: http://www.phpbb.com/kb/article/add-a-n ... -to-phpbb/ to tell you how to setup your includes.

Check ucp_register.php to see all of the things you need to put in the $user-row[]

MaxJ84
Registered User
Posts: 13
Joined: Tue Oct 21, 2008 12:02 am

Re: Adding users

Post by MaxJ84 » Tue Oct 21, 2008 5:31 pm

Okay, lets try something maybe even better:

Code: Select all

<?php
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

$user_row = array(
	'username'				=> "User1",
	'user_password'			=> phpbb_hash("PwdUser1"),
	'user_email'			=> "",
	'group_id'				=> 3, [b]// I believe that's the "registered user" group?[/b]
	'user_timezone'			=> -5,
	'user_dst'				=> 1,
	'user_lang'				=> "", [b]// I believe empty is default?[/b]
	'user_type'				=> 0, [b]// According to the database it's default.[/b]
	'user_actkey'			=> "", // Don't know what this is, but default is empty.
	'user_ip'				=> "",
	'user_regdate'			=> time(),
	'user_inactive_reason'	=> 0,
	'user_inactive_time'	=> 0,
);
user_add($user_row, $cp_data);

$user_row = array(
	'username'				=> "User2",
	'user_password'			=> phpbb_hash("PwdUser2"),
	'user_email'			=> "",
	'group_id'				=> 3,
	'user_timezone'			=> -5,
	'user_dst'				=> 1,
	'user_lang'				=> "",
	'user_type'				=> 0,
	'user_actkey'			=> "",
	'user_ip'				=> "",
	'user_regdate'			=> time(),
	'user_inactive_reason'	=> 0,
	'user_inactive_time'	=> 0,
);
user_add($user_row, $cp_data);

[b]// etc., etc., etc.[/b]
Still missing something?

Thank you,

MJ

User avatar
Brf
Support Team Member
Support Team Member
Posts: 51874
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: Adding users

Post by Brf » Tue Oct 21, 2008 5:37 pm

I am thinking user_lang should be "en" and Registered Users is 2 by default.
You will have to include functions_user.php, since that is where user_add() is.

MaxJ84
Registered User
Posts: 13
Joined: Tue Oct 21, 2008 12:02 am

Re: Adding users

Post by MaxJ84 » Tue Oct 21, 2008 6:28 pm

Okay, even though the database defaults are 3 and "", you're right, when users are actually added to the database, all registered users have goup_id 2 and language "en".

So lets try an attempt with this:

Code: Select all

<?php
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_user.' . $phpEx);

$user_row = array(
   'username'                    => "F-103",
   'user_password'             => phpbb_hash("GLGbamVt"),
   'user_email'                  => "",
   'group_id'                     => 2,
   'user_timezone'            => -5,
   'user_dst'                     => 1,
   'user_lang'                   => "en",
   'user_type'                   => 0,
   'user_actkey'                => "",
   'user_ip'                       => "",
   'user_regdate'               => time(),
   'user_inactive_reason'   => 0,
   'user_inactive_time'      => 0,
);
user_add($user_row, $cp_data);
?>
And it worked!!!

Thank you very much everyone! :D

(Don't lock this thread as I'll post my tools here so that anyone who needs to mass add users will be able to do so and very quickly in the future.)

User avatar
Brf
Support Team Member
Support Team Member
Posts: 51874
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: Adding users

Post by Brf » Tue Oct 21, 2008 6:34 pm

Moving this to Mod Writers Discussion

MaxJ84
Registered User
Posts: 13
Joined: Tue Oct 21, 2008 12:02 am

Here is my little tool to ease the mass addition of users.

Post by MaxJ84 » Tue Oct 21, 2008 9:07 pm

Here is my little tool to ease the mass addition of users. I hope it might eventually help someone else!

As I'm not a web scripting guy at all, I made it in C++. I would have included a Windows executable, but attachments are not allowed. (I'll put a link to the *.exe near the source code.)

Example of utilization:
users.txt

Code: Select all

Bob
Rob
Foo
Bar
Input/Output to the console:

Code: Select all

File containing the user list: users.txt
4 users found.
You can override some default values here...
Enter the group ID for these users [2]:
Enter the time zone for these users [-5]: 0
Use DST? (0 for false) [1]: 0
Enter the language code for these users [en]:
Generating files...
Done.
Example output files:
UsersPwds.txt

Code: Select all

Bob	hUE7lLz5
Rob	XKUhsDQg
Foo	8UhGYKw2
Bar	8WErGqNG
So you can keep a record and use them for whatever the task. You can just import the file to a spreadsheet program and manipulate the fields like you want or create another program which does what you need.

messages.txt

Code: Select all

Here are your informations for the forum login:
Username: Bob    Password: hUE7lLz5

Here are your informations for the forum login:
Username: Rob    Password: XKUhsDQg

Here are your informations for the forum login:
Username: Foo    Password: 8UhGYKw2

Here are your informations for the forum login:
Username: Bar    Password: 8WErGqNG
Very useful in my case. I need to give the username and the password to each user. Obviously, nothing forces you to use each output file!

And the last, but certainly not the least:
addusers.php

Code: Select all

<?php
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_user.' . $phpEx);

$user_row = array(
	'username'			=> "Bob",
	'user_password'			=> phpbb_hash("hUE7lLz5"),
	'user_email'			=> "",
	'group_id'			=> 2,
	'user_timezone'			=> 0,
	'user_dst'			=> 0,
	'user_lang'			=> "en",
	'user_type'			=> 0,
	'user_actkey'			=> "",
	'user_ip'			=> "",
	'user_regdate'			=> time(),
	'user_inactive_reason'		=> 0,
	'user_inactive_time'		=> 0,
);
user_add($user_row, $cp_data);

$user_row = array(
	'username'			=> "Rob",
	'user_password'			=> phpbb_hash("XKUhsDQg"),
	'user_email'			=> "",
	'group_id'			=> 2,
	'user_timezone'			=> 0,
	'user_dst'			=> 0,
	'user_lang'			=> "en",
	'user_type'			=> 0,
	'user_actkey'			=> "",
	'user_ip'			=> "",
	'user_regdate'			=> time(),
	'user_inactive_reason'		=> 0,
	'user_inactive_time'		=> 0,
);
user_add($user_row, $cp_data);

$user_row = array(
	'username'			=> "Foo",
	'user_password'			=> phpbb_hash("8UhGYKw2"),
	'user_email'			=> "",
	'group_id'			=> 2,
	'user_timezone'			=> 0,
	'user_dst'			=> 0,
	'user_lang'			=> "en",
	'user_type'			=> 0,
	'user_actkey'			=> "",
	'user_ip'			=> "",
	'user_regdate'			=> time(),
	'user_inactive_reason'		=> 0,
	'user_inactive_time'		=> 0,
);
user_add($user_row, $cp_data);

$user_row = array(
	'username'			=> "Bar",
	'user_password'			=> phpbb_hash("8WErGqNG"),
	'user_email'			=> "",
	'group_id'			=> 2,
	'user_timezone'			=> 0,
	'user_dst'			=> 0,
	'user_lang'			=> "en",
	'user_type'			=> 0,
	'user_actkey'			=> "",
	'user_ip'			=> "",
	'user_regdate'			=> time(),
	'user_inactive_reason'		=> 0,
	'user_inactive_time'		=> 0,
);
user_add($user_row, $cp_data);

?>
So then you just have to put that file in your phpBB forum installation root directory and run the script! :D

Source code:

Code: Select all

/**
 * addusers.cpp
 * Written by Maxime Joanis
 * (phpBB support forum user MaxJ84)
 * 2008/10/21
 * 
 * This program takes a list of user names (in a text file, one by row) and
 * returns a file with automatically generated passwords for each user and a
 * file addusers.php which can be placed in the root directory of a phpBB 3.x
 * forum installation to add the users all at once to that forum.
 *
 * You can use and modify this program according to your needs. Giving credits
 * for the original work is always appreciated!
**/

#include <iostream>
#include <fstream>
#include <ctime>
#include <vector>
#include <string>
#include <iomanip>

using namespace std;

int main() {
	// Defining constants.
	const short PWD_LENGTH = 8;
	const short FILENAMESIZE = 30;
	const char passwordCharSet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456798";

	// Declaring variables...
	unsigned int userIndex = 0;
	string user, tempString;
	vector<string> users;
	char password[PWD_LENGTH + 1], filename[FILENAMESIZE];
	ifstream fUserList;
	ofstream fUserPwdList("UsersPwds.txt");
	ofstream fAddUsersPhp("addusers.php");
	ofstream fMessages("messages.txt");

	// Defaults.
	short groupId = 2, timeZone = -5, dst = 1;
	string lang = "en";

	// Opening user list file.
	cout << "File containing the user list: ";
	cin >> setw(FILENAMESIZE) >> filename;
	fUserList.open(filename);
	if (!fUserList) {
		cout << "Error: file not found!\n";
		return 1;
	}

	// Reading user list file.
	while (fUserList >> user) {
		users.push_back(user);
		userIndex++;
	}
	cout << userIndex << " users found.\n";

	// Overriding defaults if needed.
	cout << "You can override some default values here...\n";
	cout << "Enter the group ID for these users [" << groupId << "]: ";
	cin.ignore();
	getline(cin, tempString);
	if (tempString != "")
		groupId = atoi(tempString.c_str());
	cout << "Enter the time zone for these users [" << timeZone << "]: ";
	getline(cin, tempString);
	if (tempString != "")
		timeZone = atoi(tempString.c_str());
	cout << "Use DST? (0 for false) [" << dst << "]: ";
	getline(cin, tempString);
	if (tempString != "")
		dst = atoi(tempString.c_str());
	cout << "Enter the language code for these users [" << lang << "]: ";
	getline(cin, tempString);
	if (tempString != "")
		lang = tempString;

	// Generate pseudo-random seed.
	srand((int)time(0));

	// Generating output files.
	cout << "Generating files...\n";
	fAddUsersPhp << "<?php\ndefine(\'IN_PHPBB\', true);\n$phpbb_root_path = (d"
		<< "efined(\'PHPBB_ROOT_PATH\')) ? PHPBB_ROOT_PATH : \'./\';\n$phpEx ="
		<< " substr(strrchr(__FILE__, \'.\'), 1);\ninclude($phpbb_root_path . "
		<< "\'common.\' . $phpEx);\ninclude($phpbb_root_path . \'includes/func"
		<< "tions_user.\' . $phpEx);\n\n";
	for (userIndex = 0;userIndex < users.size();userIndex++) {
		for (int i = 0;i < PWD_LENGTH;i++) {
			password[i] = passwordCharSet[rand() % (sizeof(passwordCharSet) - 1)];
		}
		password[PWD_LENGTH] = '\0';
		fUserPwdList << users[userIndex] << "\t" << password << endl;
		fAddUsersPhp << "$user_row = array(" << endl
			<< "\t\'username\'\t\t\t=> \"" << users[userIndex] << "\",\n"
			<< "\t\'user_password\'\t\t\t=> phpbb_hash(\"" << password << "\"),\n"
			<< "\t\'user_email\'\t\t\t=> \"\",\n"
			<< "\t\'group_id\'\t\t\t=> " << groupId << ",\n"
			<< "\t\'user_timezone\'\t\t\t=> " << timeZone << ",\n"
			<< "\t\'user_dst\'\t\t\t=> " << dst << ",\n"
			<< "\t\'user_lang\'\t\t\t=> \"" << lang << "\",\n"
			<< "\t\'user_type\'\t\t\t=> 0,\n"
			<< "\t\'user_actkey\'\t\t\t=> \"\",\n"
			<< "\t\'user_ip\'\t\t\t=> \"\",\n"
			<< "\t\'user_regdate\'\t\t\t=> time(),\n"
			<< "\t\'user_inactive_reason\'\t\t=> 0,\n"
			<< "\t\'user_inactive_time\'\t\t=> 0,\n);\n"
			<< "user_add($user_row, $cp_data);\n\n";
		fMessages << "Here are your informations for the forum login:\n";
		fMessages << "Username: " << users[userIndex] << "    Password: " << password << "\n\n";
	}
	fAddUsersPhp << "?>";
	cout << "Done.\n\n";

	// Closing files.
	fUserList.close();
	fUserPwdList.close();
	fAddUsersPhp.close();
	fMessages.close();
	return 0;
}
It was compiled using Microsoft Visual Studio 2005 Pro. Ed. I tested it on XP and Vista. You probably can compile it on other operating systems with no or minor modifications. Here's the EXECUTABLE!

Thanks again for helping me with this! Hope this helps someone.


EDIT: Hehe... For my own needs, it just outputted a addusers.php file of 123 kB. So happy I didn't have to write it all by copy and paste or some other primitive techniques.

User avatar
EAM
Registered User
Posts: 93
Joined: Mon Nov 24, 2008 12:45 pm

Re: Adding users

Post by EAM » Sat Dec 06, 2008 4:17 pm

Hi,

Thanks for this post. I'm trying to use it but I'm getting an error when I run the script. I am on my localhost and I have the addusers.php in the phpbb root directory and the error that I am getting is:

IIS received the request; however, an internal error occurred during the processing of the request. The root cause of this error depends on which module handles the request and what was happening in the worker process when this error occurred.
IIS was not able to access the web.config file for the Web site or application. This can occur if the NTFS permissions are set incorrectly.
IIS was not able to process configuration for the Web site or application.
The authenticated user does not have permission to use this DLL.
The request is mapped to a managed handler but the .NET Extensibility Feature is not installed.


My Forum runs ok when I access it. thanks

User avatar
EAM
Registered User
Posts: 93
Joined: Mon Nov 24, 2008 12:45 pm

Re: Adding users

Post by EAM » Sat Dec 06, 2008 5:46 pm

Never mind. It was an error in some things that I added to the script. I've got it working with active users but I may need to add some as inactive. Would you know if I need to put in the user_actkey for them or can I still leave it blank. I believe that I can put in USER_INACTIVE and INACTIVE_REGISTER for the type and reason. Thanks

User avatar
CoffeeGrinder
Registered User
Posts: 14
Joined: Mon Dec 07, 2009 12:24 am

Re: Adding users

Post by CoffeeGrinder » Wed Dec 09, 2009 5:55 am

Sorry for bumping a thread but I am trying to use this script and I'm getting this error:

Code: Select all

Warning: include(./common.php) [function.include]: failed to open stream: No such file or directory in D:\customer\moo\www\bbs\install\addusers.php on line 5

Warning: include() [function.include]: Failed opening './common.php' for inclusion (include_path='.;C:\php5\pear') in D:\customer\moo\www\bbs\install\addusers.php on line 5

Warning: include(./includes/functions_user.php) [function.include]: failed to open stream: No such file or directory in D:\customer\moo\www\bbs\install\addusers.php on line 6

Warning: include() [function.include]: Failed opening './includes/functions_user.php' for inclusion (include_path='.;C:\php5\pear') in D:\customer\moo\www\bbs\install\addusers.php on line 6

Fatal error: Call to undefined function phpbb_hash() in D:\customer\moo\www\bbs\install\addusers.php on line 10
It obviously has to do with the D:\ but I don't know how to fix it.

Any help?

Locked

Return to “[3.0.x] MOD Writers Discussion”