Dreams of Thought

Are dreams thoughts… or are thoughts dreams..

RSS Feed

Monthly Archives: February 2010

How to programmatically create and log in drupal users

0 Comments

Creating a new user is very easy in Drupal 6. Here’s how.

<p>><p>$new_user = array(</p><p>'name'                   => $username,<br /> 'mail'                     => $mail,<br /> 'pass'                    => user_password(),<br /> 'status'                  => 1,<br /> 'auth_MODULENAME' => $username<br />)</p><p>$user = user_save(NULL,$new_user)</p><p>// log the user in</p><p>$user = user_authenticate($new_user)</p><p>

Now for the explanation. We create a $new_user array with values we want the newly created user to have. We pass this along to the user_save function and set the 1st parameter as NULL. From the code comments in the user module -

* @param $account
* The $user object for the user to modify or add. If $user->uid is
* omitted, a new user will be added.
*
* @param $array
* (optional) An array of fields and values to save. For example,
* array(‘name’ => ‘My name’); Setting a field to NULL deletes it from
* the data column.

So setting the 1st parameter as NULL creates a new user.

The parameters name, mail, pass and status are all self explanatory. The user_password function generates a random password (by default with a length of 10).

The (optional) ‘auth_MODULENAME’ element will record the user as being created externally. This will result in an entry in the authmap table like this -

“aid” “uid” “authname” “module”
“2″ “20″ “USERNAME” “MODULENAME”

The user_authenticate function logs the user in. This function expects an array as a parameter. It first loads the user in using the user_load function and in case of no errors logs the user in.

This approach of logging in a user is useful when we have the array with us which contains the raw values used to create the user. If all you have is the uid of the user, then logging the user in is very simple. Just use the global $user object.

</p><p>global $user</p><p>$account = user_load( array('name' => 'USERNAME') ); // or user_load(UID)</p><p>$user = $account</p><p>

Don’t use the user_authenticate function here as it expects the raw values(form values). This wil not work -

</p><p>$account = user_load( array('name' => 'USERNAME') ); // or user_load(UID)</p><p>user_authenticate((array)$account)</p><p>

It will take whatever password is stored in the user object(the raw password), md5 it and then run a query to load that user. Since the password in the query is not the raw password but the md5, the query will return nothing. This will cause an error in the user_authenticate function.

For a direct code example for programmatic log in, check the devel module’s devel_switch_user function.

Filed under drupal
Feb 22, 2010

Helen Fry – Dilbert strip February 16, 2010

0 Comments

Dilbert.com

I didn’t really get this. Who the heck is Helen Fry? I had to read the comments in the site to realise “Go to Helen Fry” should be read as “Go to Hell and Fry”! :D

There are other beauties in there as well – Sofa King, Helen Wait… :P

PS : click on the image to see it in full size

Filed under from the web
Feb 17, 2010

How to dump or export all the table definitions in a MySQL database

0 Comments

So you have¬† a database with loads of tables. You want the table definitions of all of them. You don’t really need the data. mysqldump to the rescue!

mysqldump -u USERNAME --password=PASSWORD --no-data --opt DB1 > DUMPFILE

That’s it! Easy as a pie :) The key here is the –no-data option. It dumps all the table definitions, but not the table data.

Want to do this for multiple databases? No problem.

mysqldump -u USERNAME --password=PASSWORD --no-data --opt --databases DB1 DB2 > DUMPFILE

The –databases option allows you to add multiple databases.

mysqldump -u USERNAME --password=PASSWORD --no-data --opt --all-databases > DUMPFILE

The –all-databases option allows you to dump all the databases.

mysqldump -u USERNAME --password=PASSWORD --no-data --opt DB1 --ignore-table DB1.TABLENAME1 --ignore-table DB1.TABLENAME2 > DUMPFILE

–ignore-table option allows you to skip dumping certain tables. Do not forget to specify the databasename when using this option.

 

Know any more tricks? Let us know in the comments below :)

Filed under code
Feb 5, 2010

How I recovered my Thunderbird mail from backup

0 Comments

So the best way to backup your mails from Thunderbird is to copy your complete profile directory. Follow these steps for restoring your mail.

The fool that I am, instead of copying the full profile directory I ended up backing up just the Mail subdirectory in the profile directory. It took me a while to figure out how to restore the backup, so I thought I would share it with you.

The first thing you do is install Thunderbird (if you haven’t already). Now Run( Windows : Start->Run or press Win key+r ; Mac/Linux – you’re smart enough to know :P ) this command “thunderbird.exe -profilemanager”. Create a dummy profile – just have some dummy email address in there. Now exit Thunderbird.

Next step is to find your newly created profile directory. Locate the Mail directory in there. Now you have to copy 2 files from your backup – both have the same name. One has a .msf extension and the other has no extension. I had created a separate thunderbird mail folder with a filter and I needed to backup just that one alone. So I just copied those 2 files (from the Inbox.sbd directory) and put them in the Local Folders subdirectory in the Mail directory (which already had empty files like Trash and Trash.msf).

The file without extension – this is the actual mail file. It’s in the mbox format. The other is an index file.

Acknowledgements :

I posted a question on Aardvarkhttp://vark.com/t/eee11d. I got replies from Shel and Moez. I would like to thank both of them for helping me out.

Filed under technology
Feb 2, 2010