Joomla

Bulk Upload Users to Joomla

I have to say this saved my life, totally awesome. Bulk Upload

<?php

/*
Script to bulk import users into a Joomla installation (v 1.5.7).

This script is free to use, modify and distribute,
however is comes with absolutely no guarantees!

Be careful and check everything in here before running
it on your installation - has little to no error checking and is
in no shape or form robust...
And of course MAKE A BACKUP OF YOUR DATABASE BEFORE YOU BEGIN.

That said, I do hope it is useful, Paul.
paul@paulferrett.com

*/

// Hande form upload
if(isset($_POST['import'])) {

$mysql_host = trim($_POST['mysql_host']);
$mysql_user = trim($_POST['mysql_username']);
$mysql_password = trim($_POST['mysql_password']);
$mysql_schema = trim($_POST['mysql_schema']);

$table_prefix = trim($_POST['table_prefix']);

if(!mysql_connect($mysql_host, $mysql_user, $mysql_password) || !mysql_select_db($mysql_schema)) {
echo
'Supplied MySQL details were incorrect - aborting';
return;
}

// Get the joomla groups
$sql = sprintf('
SELECT  `id`, `value`
FROM    `%score_acl_aro_groups`
'
,
$table_prefix
);
$rs = mysql_query($sql);
$groups = array();
while(
$group = mysql_fetch_object($rs)) {
$groups[$group->value] = $group->id;
}

$fp = fopen($_FILES['csv']['tmp_name'], 'r');
while(
$user = fgetcsv($fp)) {

printf('Importing "%s" ... ', $user[0]);

// Lookup and verify user group
if(!isset($groups[$user[4]])) {
printf('error: Invalid group (%s) for %s. Defaulting to <code>Registered</code><br />%s', $user[4], $user[0], PHP_EOL);
$user[4] = 'Registered';
}

// Insert record into wsers
$sql = sprintf('
INSERT INTO `%susers`
SET
`name`            = "%s",
`username`        = "%s",
`email`           = "%s",
`password`        = "%s",
`usertype`        = "%s",
`block`           = "%s",
`sendEmail`       = "%s",
`gid`             = "%s",
`registerDate`    = NOW(),
`lastvisitDate`   = "0000-00-00 00:00:00",
`activation`      = "",
`params`          = ""
'
,
$table_prefix,
sql_prep($user[0]),
sql_prep($user[1]),
sql_prep($user[2]),
isset(
$_POST['md5_passwords']) ? md5($user[3]) : sql_prep($user[3]),
sql_prep($user[4]),
sql_prep($user[5]),
sql_prep($user[6]),
$groups[$user[4]]
);
mysql_query($sql);
// Get back ther user's ID
list($user_id) = mysql_fetch_row(mysql_query('SELECT LAST_INSERT_ID()'));

// Insert record into core_acl_aro
$sql = sprintf('
INSERT INTO `%score_acl_aro`
SET
`section_value`   = "users",
`value`           = %d,
`name`            = "%s"
'
,
$table_prefix,
$user_id,
sql_prep($user[0])
);
mysql_query($sql);

// Insert record into core_acl_groups_aro_map
$sql = sprintf('
INSERT INTO `%score_acl_groups_aro_map`
SET
`group_id`        = %d,
`aro_id`          = LAST_INSERT_ID()
'
,
$table_prefix,
$groups[$user[4]]
);
mysql_query($sql);

echo 'done.';
flush();
}

echo '<br /><br /><strong>Done</strong>';

} else {
// show upload form
?>
<html><head><title>Bulk import users into Joomla 1.5</title></head><body>
<h1>Import Users to Joomla</h1>
<p>
Use this script to do a bulk import of users into Joomla 1.5.<br />
Upload a CSV file with the following format:<br />
<code>
name, username, email, password, usertype, block, send_email
</code><br />
Wrap details with commas in them in quotes.
</p>
<hr />
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="import" value="1" />
<table cellpadding="4px">
<tr>
<td>CSV File: </td>
<td><input type="file" name="csv" /></td>
</tr>
<tr>
<td>MD5 Hash Passwords: </td>
<td><input type="checkbox" name="md5_passwords" /><br /><small>*Check this option if the passwords in your CSV are in plain text</small></td>
</tr>
<tr>
<td>Joomla Table Prefix: </td>
<td><input type="text" name="table_prefix" value="jos_" /></td>
</tr>
<tr>
<td>Joomla Database Name: </td>
<td><input type="text" name="mysql_schema" value="joomla" /></td>
</tr>
<tr>
<td>MySQL Host: </td>
<td><input type="text" name="mysql_host" value="localhost" /></td>
</tr>
<tr>
<td>MySQL Username: </td>
<td><input type="text" name="mysql_username" value="" /></td>
</tr>
<tr>
<td>MySQL Password: </td>
<td><input type="text" name="mysql_password" value="" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value=" Import Users! " /></td>
</tr>
</table>
</form>
</body></html>
<?php
}

function sql_prep($var) {
return
mysql_real_escape_string($var);
}

Tags:

Friday, May 29th, 2009 Joomla, Web Development No Comments

Joomla Unique Menus for Guests and Registered Users

I’ve been doing a lot of Joomla development lately, in particular for a site that has a public and private side.

I was looking for a solution that would allow me to have unique menus for guests vs registetered users and found a way to do it with help from the following articles.

Joomla – Change login link to logout

Joomla 1.5 Template Positions – for this article I used the section for adding new module positions since the first article doesn’t cover that.

Hope this helps!

Tags:

Thursday, May 21st, 2009 Joomla No Comments