<?php

/**
 * PHPList Subscribe user
 *
 * @author 
 *         original function by Ravis @ http://forums.phplist.com/viewtopic.php?t=5009
 *        modified by Teckie
 * @param string $email User's email
 * @param int $listid Newsletter's id
 * @return string subscribe
 *
 * I removed the attributes variable since I personally don't need it. Feel free to re-add
 * it from Ravis' original function.
*/

function phplist_subscribeuser($email$listId) {
    
// Include your phpList config.php file here
    
require("/path/to/phplist/config/config.php");

    
// Setup and connect to the database (this only needs to be done once per session
    // You don't need to change anything here.
    
$link mysql_connect($database_host$database_user$database_password) or die('Could not connect: ' mysql_error());
    
mysql_select_db($database_name) or die('Could not select database');

    
// is there already a user in the system using this email addy?
    
$sql "SELECT id FROM ".$usertable_prefix."user WHERE email='".addslashes($email)."'";
    
$result mysql_query($sql);
    if (
$result) {
        
$userId mysql_result($result0);
    } 

    
// if the user doesn't exist, create them
    
if (empty($userId)) {
        
//echo "attempting to create new user";
        // create a unique id for the user (and make sure it's unique in the database)
        
do {
            
$uniqueId md5(uniqid(mt_rand(0,1000).$email));
            
$sql "SELECT COUNT(*) FROM ".$usertable_prefix."user WHERE uniqid='".addslashes($uniqueId)."'";
            
//$exists = $db->getOne($sql);
            
$result mysql_query($sql);
            if (
$result) {
                
$exists mysql_result($result0);
            }
        } while (
$exists);
        
// insert the user
        
$sql "INSERT INTO ".$usertable_prefix."user (email,entered,confirmed,uniqid) values ('".addslashes($email)."',now(),1,'".addslashes($uniqueId)."')";
        
$result mysql_query($sql);
        
// get the new user id
        
$sql "SELECT id FROM ".$usertable_prefix."user WHERE uniqid='".addslashes($uniqueId)."'";
        
$result mysql_query($sql);
        if (
$result) {
            
$userId mysql_result($result0);
        }
    }

    
// add a note saying we imported them manually
    
$sql "INSERT INTO ".$usertable_prefix."user_history (userid,date,summary) values('".addslashes($userId)."',now(),'Imported from forums.')";
    
$result mysql_query($sql);

    
// subscribe them to the specified list
    
$sql "REPLACE INTO ".$table_prefix."listuser (userid,listid,entered) VALUES ('".addslashes($userId)."','".addslashes($listId)."',now())";
    
$result mysql_query($sql);
   
    echo 
$userId "-" $email " added<br />";
}


// This is the actual importing of emails
require("/path/to/phpbb/config.php");
mysql_connect($dbhost$dbuser$dbpasswd) or die('Could not connect: ' mysql_error());
mysql_select_db($dbname) or die('Could not select database');

$sql "SELECT DISTINCT user_email FROM ".$table_prefix."users where user_email <> '' ORDER BY user_email ASC";
$result mysql_query($sql);

while (
$row mysql_fetch_array($result)) {
    
// change '1' to the id of the newsletter you want your users to be subscribed to
    
phplist_subscribeuser($row['user_email'], 1);
}



?>