Dreams of Thought

Are dreams thoughts… or are thoughts dreams..

RSS Feed

Tag Archives: php

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

Pirate tweet

0 Comments

September 19 is International Talk Like a Pirate Day.For this day, the Yahoo! Query Language (YQL) team announced on the YDN blog that they were bringing out a “pirate table”. This table would allow us to translate plain English to piratespeak using YQL. I had been meaning to play around with YQL a bit and took this opportunity to dive in. I couldn’t complete it in time for September 19 because of work, but managed to finally get it in some shape today. What I made is something I call “Pirate Tweet”.

Pirate tweet allows you to read your most recent tweets in piratespeak. Just enter a twitter username and you get the tweets in piratespeak. You can get the source at http://labs.gingerjoos.com/piratetweet/piratetweet.tar.gz . Please do link back to this post if you do use it. It is written in PHP and uses PHP’s builtin SimpleXML as well as libcurl. What is in there is just a rough basic barebones quick-and-dirty stuff. It includes an index.php file which takes care of spitting out the actual html. The common.php file does the actual processing. pirate-tweet.css file is some very basic css.

The twitter RSS feed for a user is something like http://twitter.com/statuses/user_timeline/USERNAME.rss . This is fed into the YQL query which does the translation, like this

SELECT * FROM piratespeak.translate
WHERE html IN
(SELECT description FROM rss
WHERE url = "http://twitter.com/statuses/user_timeline/USERNAME.rss")

.
So you send this query to http://query.yahooapis.com/v1/public/yql with the params

<code>$params = array (
'q'      => $query,
'format' => 'xml',
'env'    => 'store://kid666.com/piratespeak',
);

So that’s basically what it does. Hope someone finds this useful :)

I must say that YQL seems like a very interesting tool. Hopefully the MS – Yahoo! deal will have no negative impact on it. Without doubt, YQL is the star in Yahoo!’s products for developers. The ability to express data as queries is mind – blowing. Looking forward to doing more stuff with YQL.

Known issues :

Even links are passed as is to the YQL query. This results in, for example, http://is.gd/3oBWf to become http://be.gd/3oBWf .

Acknowledgment :
Much thanks to the YQL guys at Yahoo! and their wonderful documentation.

Also see :

Previously mentioned blog post has some more stuff using the same table.

Chris Heilmann made some stuff with the table as well. Do check it out.

Filed under code
Sep 28, 2009

Intel 8085 microprocessor simulator

0 Comments

My friend Arun created an Intel 8085 microprocessor simulator as a hobby project sometime in his college. It’s written with php and javascript.

If you’re into 8085 programming, do try it out and let me/Arun know.

http://labs.kitiyo.com/8085/free-online-assember-simulator-8085.html

Jun 23, 2009