<?php
/**
*
* @package phpBB3
* @copyright (c) 2009 Teckie @ http://marikit.org/
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
    exit;
}

/**
 * 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) {
    
// Get these values from your phpList config.php file here
    // I don't want to go and include the whole phpList config.php file
    // since there might be global variables that would be overwritten 
    // or smt.
    
$database_host "localhost";
    
$database_name "database";
    
$database_user "username";
    
$database_password "password";
    
$table_prefix "phplist_";
    
$usertable_prefix "phplist_user_";

    
// Setup and connect to the database (this only needs to be done once per session
    // You don't need to change anything here.
    
$phplist_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');

    
// Let's clear the database password since we don't need it anymore
    
unset($database_password);

    
// 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 && mysql_num_rows($result) > 0) {
        
$userId mysql_result($result0);
    } 

    
// if the user doesn't exist, create them
    
if (empty($userId)) {
        
// 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)."'";
            
$result mysql_query($sql);
            if (
$result && mysql_num_rows($result) > 0) {
                
$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 && mysql_num_rows($result) > 0) {
            
$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(),'Subscribe 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);
    
    
mysql_close($phplist_link);
    
    
// reconnect to the phpbb database
    // We have to reconnect to the phpbb database since it looks like
    // we lost the connection when we connected to the phplist database.
    // I think, you can comment this out if your phpbb and phplist resides 
    // in the same database.
    
global $phpbb_root_path$phpEx;
    include(
$phpbb_root_path 'config.' $phpEx);
    
mysql_connect($dbhost$dbuser$dbpasswd) or die('Could not connect: ' mysql_error());
    
mysql_select_db($dbname);

    
// We do not need this any longer, unset for safety purposes
    
unset($dbpasswd);
    
}
?>