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).
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")));
Comments
Mechanize
Get yourself WWW::Mechanize
Looks useful
Thanks for your help - Simplenews is great, but like you, I don't want anonymous users to be able to see the newsletters. So if this works for me it'll mean I don't have to remember to invoke cron manually.
Can you make it clearer what you need to put it in as the variables and where to put them? I don't know perl at all I'm afraid. If I put the script in my cgi-bin, what command should I put in cron to run it?
How to run
The only line of the script you should need to change is
cron("yourhost", login("yourhost", "youruser", "yourpass", getCookie("yourhost")));
If your drupal site is on http://www.example.com, and you have a user with a login george and password ohfb9gj then
cron("http://www.example.com", login("http://www.example.com", "george", "ohfb9gj", getCookie("http://www.example.com")));
To run it I saved the file in /usr/local/bin/authcron.pl edited my crontab - instead of calling
0,30 * * * * curl -s http://www.example.com?cron.php
I have
0,30 * * * * /usr/local/bin/authcron.pl