Logging in to drupal 5 using perl

Normally you can call the drupal cron page (/cron.php) as the anonymous user - but - to work around an issue with the simplenews module and taxonomy access control I need to run the cron script as a logged in user.

You need to do three things (for drupal 5 - I used to be able to start at step 2 for drupal 4).

  1. Grab the root page to get the PHPSESSID cookie
  2. Login to associate your cookie with a logged in session
  3. Call the script you want - in this case cron.php

Here's the script I ended up using

#!/usr/bin/perl

use strict;

use HTTP::Request::Common; use HTTP::Response;

use LWP::UserAgent;

sub getCookie { my $host = shift;

my $ua = LWP::UserAgent->new;

$ua->cookie_jar({});

my $url = "http://" . $host . "";

my $response = $ua->get($url);

return $ua; }

sub login { my $host = shift; my $user = shift; my $pass = shift; my $ua = shift;

my $url = "http://" . $host . "/user/login";

my $response = $ua->request(POST $url, [ 'name' => $user, 'pass' => $pass, 'form_id' => 'user_login', 'op' => 'Log in']);

# Note - the 'op' value MUST match the name on the log in button of your page

return $ua; }

sub cron { my $host = shift; my $ua = shift;

my $url = "http://" . $host . "/cron.php";

my $response = $ua->post($url); }

cron("yourhost", login("yourhost", "youruser", "yourpass", getCookie("yourhost")));