How to programmatically create and log in drupal users

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.

Helen Fry – Dilbert strip February 16, 2010

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

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

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 :)

How I recovered my Thunderbird mail from backup

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.

Techcrunch site “hacked”

Some content below may not be safe for work (wink wink) :D

So TechCrunch got broken into/”hacked”/cracked. Apparently because someone was pissed off by the ads shown. Heh heh.

So here’s a screenshot before they fix things.

 

TechCrunch

TechCrunch hacked

This is the text inserted into the TechCrunch page

So Arrington, how much did all the media coverage yesterday brought you in trough the welcome.html ad you
forced
people to? What a fucking retarded move was that you twat. You should be thanking me and sucking on my fucking ballsack for not deleting everyone on the box and publishing the mysql, if that’s what you want O.K, I can do that. Also, you fucking dickwads from sites like Yahoo!, BBC and plenty more, where the FUCK do you see adult content on
http://dupedb.com/
???????? I mean honestly, are you fucktards also in just for the money?!?!?!
So Arrington, how much did all the media coverage yesterday brought you in trough the welcome.html ad you
forced
people to? What a fucking retarded move was that you twat. You should be thanking me and sucking on my fucking ballsack for not deleting everyone on the box and publishing the mysql, if that’s what you want O.K, I can do that. Also, you fucking dickwads from sites like Yahoo!, BBC and plenty more, where the FUCK do you see adult content on
http://dupedb.com/
???????? I mean honestly, are you fucktards also in just for the money?!?!?!

 

Update : TechCrunch has posted on this issue and promised to update the post with info as it comes along.

Firebug opens automatically for all pages

Faced this bizzare issue today in Firefox. Firebug was opening automatically for all pages. Even on closing firebug, all I had to do was click on a new link in the same tab for it to appear again. This happened for all tabs.

Turns out I had accidently turned on “On for all Web Pages” option. Right clicked on the Firebug icon to turn it off. Problem solved :D

Right click Firebug

Right click Firebug to disable "On For All Web Pages"

Booking tatkal tickets on IRCTC the supercool way

This article is a short demo to using Firefox’s Autofill Forms extension to book tatkal tickets. First step is to go to https://addons.mozilla.org/en-US/firefox/addon/4775 and install the extension. You’ll need to restart Firefox for the extension to take effect.

Now that it’s done, head to the IRCTC website and login. Now head to the Quick Book page. You can fill in the details.quick book on IRCTC
If you do not know the train number, you can get it from the “Plan My Travel” page. Now that you’ve filled up all the details you want to, right click on one of the form textboxes. Click on the “Add complete form as profile” option.add complete form as profile

A new window like this will now open up

Autofill forms profile

You can close the window and reload the QuickBook page. Press the Alt+j key combination to autofill the form. You can reload the page as many times as you want and press Alt+j to autofill.

 

This post is dedicated to my good friend Dhanya.

Edit : OK, I’m giving into blackmail from someone crafty and cunning(not my words) :P

@gjoos : i want my name included in that blogpost if you want me to TRY booking tickets for you tomorrow !!! :P

So, Divya chechi.. this post is dedicated to you as well :P

Update : Wondering what CKWL means? Check out my blog post on CKWL status on Tatkal railway ticket.

CKWL on Tatkal railway ticket

So the other day I booked a tatkal ticket online from IRCTC. After the usual 500 errors and multiple tries I came to the “Congratulations!” page where they gave me the ticket details. And this is what I found in the ticket’s status “CKWL 23″. I wondered what the heck it was. Googling did not help. I shot off a mail to the IRCTC customer care and within half an hour I got their reply. Reproducing their reply below -

Dear Customer,

Kindly note that CK stands for Tatkal CKWL means Tatkal Wait list Ticket.

Thanks & Regards

So there it goes. Official reply from IRCTC. CKWL is the waiting list for the tatkal quota. It’s just like the “WL” you get on the normal ticket, except it’s on the tatkal quota. No idea what CK stands for though.

Although the reply wasn’t exactly Shakespeare, I was impressed that they got back to me so soon. It’s nice to see the Railways doing good stuff. There’s still room to improve though.

Have you had a similar experience with IRCTC? Frustrating one? Good one? The comment form is right below :)

Update : Found something in the IRCTC site in the ngpay FAQ. It says(in point 9)

Booking can be done against general (GN), ladies (LD) and tatkal (CK) quota berths/seats only.

 

. What this means is that GNWL is General Quota Waiting list, LDWL is Ladies Quota Waiting List and CKWL is Tatkal Quota Waiting list.

If you’re looking to improve the time it takes to book tatkal tickets, you can try the Autofill forms extension. Check out my blog post on how to use the Autofill forms addon.

Comparison of Google Public DNS and Open DNS response

Google announced that it’s making 2 DNS servers available publicly. The servers are 8.8.8.8 and 8.8.4.4 . They’ve also given configuration instructions on their DNS page. I configured my /etc/resolv.conf to refer to Google’s DNS. Before this I ran some quick tests using dig to compare it with OpenDNS.

$ dig @208.67.222.222 gingerjoos.com

; <<>> DiG 9.4.2 <<>> @208.67.222.222 gingerjoos.com
; (1 server found)
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 1273
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;gingerjoos.com.                        IN      A

;; ANSWER SECTION:
gingerjoos.com.         14400   IN      A       208.113.199.196

;; Query time: 751 msec
;; SERVER: 208.67.222.222#53(208.67.222.222)
;; WHEN: Fri Dec  4 09:13:58 2009
;; MSG SIZE  rcvd: 48

$ dig @8.8.8.8 gingerjoos.com

; <<>> DiG 9.4.2 <<>> @8.8.8.8 gingerjoos.com
; (1 server found)
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 2594
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;gingerjoos.com.                        IN      A

;; ANSWER SECTION:
gingerjoos.com.         14400   IN      A       208.113.199.196

;; Query time: 616 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Fri Dec  4 09:14:09 2009
;; MSG SIZE  rcvd: 48

$ dig @8.8.4.4 gingerjoos.com

; <<>> DiG 9.4.2 <<>> @8.8.4.4 gingerjoos.com
; (1 server found)
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 34741
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;gingerjoos.com.                        IN      A

;; ANSWER SECTION:
gingerjoos.com.         14321   IN      A       208.113.199.196

;; Query time: 123 msec
;; SERVER: 8.8.4.4#53(8.8.4.4)
;; WHEN: Fri Dec  4 09:15:28 2009
;; MSG SIZE  rcvd: 48

Dig allows you to directly query a DNS server and get its reply. It shows stats from that query – response. The stat we’re interested in is this one

;; Query time: 123 msec

The IP 208.67.222.222 is the address of the OpenDNS server. Let’s try one more query


$ dig @208.67.222.222 mec.ac.in

; <<>> DiG 9.4.2 <<>> @208.67.222.222 mec.ac.in
; (1 server found)
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 44308
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;mec.ac.in.                     IN      A

;; ANSWER SECTION:
mec.ac.in.              604800  IN      A       210.212.232.132

;; Query time: 389 msec
;; SERVER: 208.67.222.222#53(208.67.222.222)
;; WHEN: Sat Dec  5 12:38:57 2009
;; MSG SIZE  rcvd: 43

$ dig @8.8.8.8 mec.ac.in

; <<>> DiG 9.4.2 <<>> @8.8.8.8 mec.ac.in
; (1 server found)
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 56600
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;mec.ac.in.                     IN      A

;; ANSWER SECTION:
mec.ac.in.              160448  IN      A       210.212.232.131

;; Query time: 121 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Sat Dec  5 12:39:06 2009
;; MSG SIZE  rcvd: 43

You can try more queries and see for yourself. At least as of now Google DNS seems to be faster.

Gravatar support in Drupal

If you’ve been using Wordpress you probably know about Gravatar. Gravatar is an abbreviation for “Globally Recognized Avatar”. It’s a service which provides an avatar across sites. While Gravatar support is built-in in Wordpress, it requires an external module to be installed in Drupal. Recently I had to use the module in a Drupal 5 instance.

The module provides a gravatar only for comments. You can have the module insert the gravatar into the comment body itself or have it provide a field in the comment object ($comment->gravatar) which can be used by the theme. It does this by using the hook_comment() .

In our case though, we needed it to show the gravatar instead of the user picture if the user had not uploaded any. This was very easy to do. The key function here is _gravatar_get_gravatar(). You can provide it any or all of these parameters – mail, default, size or rating. Mail is the email id of the user. Gravatar can supply a unique generated avatar in case the email id you send them is not that of a registered Gravatar user. This can be of many types – identicon, wavatar or monsterid. Which of these are what you want, is specified in the default parameter. The size is of course, the size. The gravatars uploaded come with a rating – G, PG, R or X. You can specify a maximum rating with the rating parameter. You can read more about the gravatar parameters.

You can pass whichever of these parameters you want to _gravatar_get_gravatar() and it will return to you the url of the gravatar. Now pass this to the theme_gravatar() function in the gravatar module.

$picture = theme('gravatar',$gravatar_url,$username,$url);

You have to do all of this in the theme_user_picture function of your theme. If your theme is something like -

function THEMENAME_user_picture($account) {
  if (variable_get('user_pictures', 0)) {
    if ($account->picture && file_exists($account->picture)) {
      $picture = file_create_url($account->picture);
    }
    else if (variable_get('user_picture_default', '')) {
      $picture = variable_get('user_picture_default', '');
    }

You could change it to something like -

function THEMENAME_user_picture($account) {
  if (variable_get('user_pictures', 0)) {
    if ($account->picture && file_exists($account->picture)) {
      $picture = file_create_url($account->picture);
    }
    else if (user_access('use gravatar', $account) && (!user_access('disable own gravatar', $account) || !isset($account->gravatar) || $account->gravatar)) {
      $picture = _gravatar_get_gravatar(array('mail' => $account->mail));
      return theme('gravatar',$picture,$account->name,'');
    }
    else if (variable_get('user_picture_default', '')) {
      $picture = variable_get('user_picture_default', '');
    }

You could even rewrite the default theme_gravatar provided by the gravatar module with your own THEMENAME_gravatar theme function.