The name of the script is LCsub.php and it will be used to remotely subscribe a user to a list. LC stands for "list control". If we can get this to work, it will be trivial to implement LCunsub.php.
I used PHP a lot a year ago, and I am not going to be able to get this working on my own. But I am sure many others could finish this. The script (see below) attempts to login and pass parameters to PHPlist in the background. There may be a simpler approach, but I did not want to directly manipulate the database because then the script might break when the DB is upgraded. Also, it might be necessary to pass parameters to phplist using a 'post' command and it might be necessary to establish a session, things that I don't yet know how to do.
Can anyone help? I designed this for version 2.94; if we can get this working it could be developed into an API for phplist.
-rich
- Code: Select all
<?php
/* LCsub.php -- remote List Control via HTTP, subscribe function
First draft by Rich Cowan, 7/26/05
I haven't used PHP in a while so this will need some debugging
to get it working.
THE CONCEPT:
You have a PHPlist installation. You would like to
be able to issue a simple HTTP command to subscribe
or unsubscribe someone from a list. To do this,
you will need two php files:
LCsub.php -- will subscribe a user
LCunsub.php -- will unsubscribe a user
Below is just the code for LCsub.
USAGE:
(we assuming list #1, master password is "plist")
Command:
http://mydomain.com/lists/LCsub.php?pwd=plist&email=johndoe%40aol.com
Result:
This will subscribe John Doe to the default email list, the list
with an id of 1; note that the '@' sign has been replaced here
by %40 which is needed by most web servers.
Command:
http://mydomain.com/lists/LCsub.php?pwd=plist&email=johndoe%40aol.com&lid=2&first=John&last=Doe&state=TX
Result:
This will subscribe John Doe to email list #2, and also add
user data for him, namely John's first name, last name, and
state, which must be set up as phplist attributes for List #2.
We assume (for now) that John is not an existing user.
INSTALLATION AND CONFIGURATION:
Just copy this script to the home directory of phplist, the lists folder.
To make it executable (on Linux), do a 'chmod 755 phplist'
To configure, just replace the values below for settings with the
location of your phplist installation, and a working admin password
for this installation, the names of your attributes, and finally
the default list ID to modify if the enduser does not supply a list ID.
Questions? Feel free to email me, richcowan <at> gmail.com
*/
// GLOBAL VARIABLES
var $u, $p, $domain, $lid; // configuration settings
var $pwd, $email, $first, $last, $org, $state; //http parameters
var $tmp; // temporary
// CONFIGURATION SETTINGS. Set them up for your host
$lid = 1; //lid is the default PHPlist List ID to use
$domain = "mysite.com/lists"
$u = "admin"
$p = "phplist"
$attr1 = "first"
$attr2 = "last"
$attr3 = "state"
//THE CODE
// 1) Retrieve the password parameter supplied in http request
$pwd = _GET['pwd'];
if ($pwd == 'masterpassword') { // make sure password matches
// 2) if script password is correct, then retrieve other parameters
$email = _GET['email'];
$attr1 = _GET['$attr1'];
$attr2 = _GET['$attr2'];
$attr3 = _GET['$attr3'];
$tmp = _GET['lid'];
if ($tmp != "") {$lid = $tmp; } //user may override default list ID
// 3) Now simulate login to phplist
// Using a 'get' request, might need to be rewritten as a 'post'
$tmp = fopen("http://www.phplist.com/lists/admin.php?login=$u?password=$p");
echo($tmp); //debug code, ok to remove
// 4) Now we build the string for subscribing
$tmp = "http://$domain/admin/?action=insert&doadd=yes&id=$lid";
$tmp += "&email=$email;
if ($attr1 != "") { $tmp += "&attribute1=$attr1";
if ($attr2 != "") { $tmp += "&attribute2=$attr2";
if ($attr3 != "") { $tmp += "&attribute3=$attr3";
echo($tmp); //debug code, ok to remove
// 5) Now we issue the subscribe command!
$tmp = fopen($tmp);
echo($tmp); //debug code, ok to remove
} // end of if clause
// close the php tag
?>