Integrating PHPList and CodeIgniter

Hello to all. It was a busy month ,so I haven’t updated this blog for soo loong. Now I give you a small bonus :) . I was busy with a lot of stuff including writing a library for CodeIgniter dealing with PHPList integration. Before implementing it, I googled a lot for a ready to use library on this particular subject. No luck. So I found myself writing it for an hour and think it is pretty good in doing its job, at least for me :) Here is the library and enjoy.

NOTE: Have in mind that the CI and PHPLIST have a common function named “redirect”. You either need to rename that in CI or in PHPLIST.

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* CodeIgniter phplist Library
*
* CodeIgniter phplist bridge allowing adding users to lists and blacklisting them
*
* @author Georgi Budinov
* @version	1.0
* @link http://georgi.budinov.com
*/
class Phplist_library
{
    public $CI;

    /**
     * Constructor.
     */
    public function __construct()
    {
    	global $tables, $table_prefix, $config, $usertable_prefix, $database_connection;
        if (!isset($this->CI))
		{
			$this->CI =& get_instance();
		}

		$rootPath = $this->CI->config->item('root_path');

		define('BULLETIN_ROOT_PATH', $rootPath.'bulletin/');

		$phplist_root_path = (defined('PHPLIST_ROOT_PATH')) ? PHPLIST_ROOT_PATH : BULLETIN_ROOT_PATH;

		// Include needed files
		include($phplist_root_path . 'admin/defaultconfig.inc');
		include($phplist_root_path . 'config/config.php');
		include($phplist_root_path . 'admin/mysql.inc');
		include($phplist_root_path . 'admin/connect.php');
    }

    /**
     *
     * @param $email
     * @param $lists
     * @return mixed
     */
    public function user_add($email, $lists = array())
    {
    	if(!$email) return false;

    	if(is_array($lists) && count($lists))
    	{
    		$userID = addNewUser($email);

    		if($userID > 0)
    		{
    			foreach($lists as $list)
    			{
    				$this->userAddToList($userID, $list);
    			}
    		}
    	}
    	else
    	{
        	return addNewUser($email);
    	}
    }

	public function get_lists()
    {
    	$results = $this->getLists();

    	return $results;
    }

	public function user_add_list($email, $list)
    {
    	if(!$email || !$list) return;

    	$userid = $this->getUserIDByEmail($email);

    	if($userid > 0)
    	{
    		$this->userAddToList($userid, $list);
    	}
    	else
    	{
    		return;
    	}
    }

	public function user_remove_list($email, $list)
    {
    	if(!$email || !$list) return;

    	$userid = $this->getUserIDByEmail($email);

    	if($userid > 0)
    	{
    		$this->userRemoveToList($userid, $list);
    	}
    	else
    	{
    		return;
    	}
    }

    /**
     *
     * @param $email
     * @return mixed
     */
    public function user_blacklist($email)
    {
    	if(!$email) return;

    	return addUserToBlackList($email);
    }

	/**
     *
     * @param $email
     * @return mixed
     */
    public function user_unblacklist($email)
    {
    	if(!$email) return;

    	$userid = $this->getUserIDByEmail($email);

    	if($userid > 0)
    	{
    		return unBlackList($userid);
    	}
    	else
    	{
    		return false;
    	}
    }

	private function getUserIDByEmail($email = '') {
	  global $tables, $table_prefix, $config, $usertable_prefix, $database_connection;
	  if (!$email) return;

	  # workaround for integration webbler/phplist
	  if (!isset($table_prefix))
	    $table_prefix = "phplist_";

	  if (isset($tables["attribute"])) {
	    $att_table = $tables["attribute"];
	    $user_att_table = $tables["user_attribute"];
	    $usertable = $tables["user"];
	  } else {
	    $att_table = "attribute";
	    $user_att_table = "user_attribute";
	    $usertable = "user";
	  }
	  $email = mysql_real_escape_string($email, $database_connection);
	  $userid = Sql_Fetch_Row_Query("select id from {$usertable} where email = \"$email\"");
	  $id = $userid[0];

	  return $id;
	}

	private function userAddToList($id = 0, $listID = 0)
	{
		global $tables, $table_prefix, $config, $usertable_prefix, $database_connection;

		$id = intval($id);
		$listID = intval($listID);
		if(!$id || !$listID) return false;

		$query = "replace into ".$tables["listuser"]." (userid,listid,entered) values($id,$listID,now())";
	    $result = Sql_query($query);
	}

	private function userRemoveToList($id = 0, $listID = 0)
	{
		global $tables, $table_prefix, $config, $usertable_prefix, $database_connection;

		$id = intval($id);
		$listID = intval($listID);
		if(!$id || !$listID) return false;

		$query = "delete from ".$tables["listuser"]." where userid = $id and listid = $listID";
	    $result = Sql_query($query);
	}

	private function getLists()
	{
		global $tables, $table_prefix, $config, $usertable_prefix, $database_connection;

		$results = array();
		$result = Sql_query("SELECT * FROM {$tables['list']} order by listorder");
		while ($row = Sql_fetch_array($result))
		{
			$results[$row['id']] = $row;
		}

		return $results;
	}
}

/* End of file phplist_library.php */
/* Location: ./application/libraries/phplist_library.php */

<?php if (!defined(‘BASEPATH’)) exit(‘No direct script access allowed’);

/**
* CodeIgniter phplist Library
*
* CodeIgniter phplist bridge allowing adding users to lists and blacklisting them
*
* @author Georgi Budinov
* @version    1.0
* @link http://georgi.budinov.com
*/
class Phplist_library
{
public $CI;

/**
* Constructor.
*/
public function __construct()
{
global $tables, $table_prefix, $config, $usertable_prefix, $database_connection;
if (!isset($this->CI))
{
$this->CI =& get_instance();
}

$rootPath = $this->CI->config->item(‘root_path’);

define(‘BULLETIN_ROOT_PATH’, $rootPath.’bulletin/’);

$phplist_root_path = (defined(‘PHPLIST_ROOT_PATH’)) ? PHPLIST_ROOT_PATH : BULLETIN_ROOT_PATH;

// Include needed files
include($phplist_root_path . ‘admin/defaultconfig.inc’);
include($phplist_root_path . ‘config/config.php’);
include($phplist_root_path . ‘admin/mysql.inc’);
include($phplist_root_path . ‘admin/connect.php’);
}

/**
*
* @param $email
* @param $lists
* @return mixed
*/
public function user_add($email, $lists = array())
{
if(!$email) return false;

if(is_array($lists) && count($lists))
{
$userID = addNewUser($email);

if($userID > 0)
{
foreach($lists as $list)
{
$this->userAddToList($userID, $list);
}
}
}
else
{
return addNewUser($email);
}
}

public function get_lists()
{
$results = $this->getLists();

return $results;
}

public function user_add_list($email, $list)
{
if(!$email || !$list) return;

$userid = $this->getUserIDByEmail($email);

if($userid > 0)
{
$this->userAddToList($userid, $list);
}
else
{
return;
}
}

public function user_remove_list($email, $list)
{
if(!$email || !$list) return;

$userid = $this->getUserIDByEmail($email);

if($userid > 0)
{
$this->userRemoveToList($userid, $list);
}
else
{
return;
}
}

/**
*
* @param $email
* @return mixed
*/
public function user_blacklist($email)
{
if(!$email) return;

return addUserToBlackList($email);
}

/**
*
* @param $email
* @return mixed
*/
public function user_unblacklist($email)
{
if(!$email) return;

$userid = $this->getUserIDByEmail($email);

if($userid > 0)
{
return unBlackList($userid);
}
else
{
return false;
}
}

private function getUserIDByEmail($email = ”) {
global $tables, $table_prefix, $config, $usertable_prefix, $database_connection;
if (!$email) return;

# workaround for integration webbler/phplist
if (!isset($table_prefix))
$table_prefix = “phplist_”;

if (isset($tables["attribute"])) {
$att_table = $tables["attribute"];
$user_att_table = $tables["user_attribute"];
$usertable = $tables["user"];
} else {
$att_table = “attribute”;
$user_att_table = “user_attribute”;
$usertable = “user”;
}
$email = mysql_real_escape_string($email, $database_connection);
$userid = Sql_Fetch_Row_Query(“select id from {$usertable} where email = \”$email\”");
$id = $userid[0];

return $id;
}

private function userAddToList($id = 0, $listID = 0)
{
global $tables, $table_prefix, $config, $usertable_prefix, $database_connection;

$id = intval($id);
$listID = intval($listID);
if(!$id || !$listID) return false;

$query = “replace into “.$tables["listuser"].” (userid,listid,entered) values($id,$listID,now())”;
$result = Sql_query($query);
}

private function userRemoveToList($id = 0, $listID = 0)
{
global $tables, $table_prefix, $config, $usertable_prefix, $database_connection;

$id = intval($id);
$listID = intval($listID);
if(!$id || !$listID) return false;

$query = “delete from “.$tables["listuser"].” where userid = $id and listid = $listID”;
$result = Sql_query($query);
}

private function getLists()
{
global $tables, $table_prefix, $config, $usertable_prefix, $database_connection;

$results = array();
$result = Sql_query(“SELECT * FROM {$tables['list']} order by listorder”);
while ($row = Sql_fetch_array($result))
{
$results[$row['id']] = $row;
}

return $results;
}
}

/* End of file phplist_library.php */
/* Location: ./application/libraries/phplist_library.php */

This post is also available in: Bulgarian

  1. Hi,

    Thanks for the above; I can’t seem to find any sort of API for phplist, which seems like a major failing on their behalf – so the above is quite appreciated!

    David.

    • Federico Soria
    • August 10th, 2011 2:07am

    Hi!

    I think is great that you trying to fusion phplist into Codeigniter. I’m newbie in this framework and this is what a i was looking for (you’re the only one I’ve seen that made a library for CI). However I’m getting trouble to make this work on my project. I’m using CI 2.0 and when CI loads the library, the application stops completely. I check into the log file and no errors is marked, but the debugger stops writing the file just in the line when phplist_library loads.
    If I test the library separately (in a controller with no other loaded libraries) phplist_library seems to work fine.

    I like to show you some error code to be more precisely but there is no one.

    I hope you can help me!

    Saludos desde Argentina!

    • Federico Soria
    • August 10th, 2011 3:02am

    Me again!
    I’ve found the problem. I was loading the library after loading the helpers in the __construct method. I changed the order and it was fixed!

    Thanks anyway! :D

    • Hi, glad to hear you managed to resolve the problem. I am just curious what helpers were you trying to load before loading the library ? Perhaps there is a colision of function names and if so the way you resolved it, will cause some of the helper functions not to work as expected.

    • Federico Soria
    • August 10th, 2011 5:54pm

    The helpers used were “url, form, html, date”. The problem seems to me, is in phplist system files. The code is a really big mess. For example I had to deal with several php notices of “undefined index or variable”. And also the function “redirect” of CI doesn’t work fine because in the file “admin/connect.php” was declare a function with the same name. :S
    Aside of those issue, the phplist application is wonderful!

    Now I’m trying to integrate to my project ACL functionality. Do you know some good library to achieve this?

    Bye

    • Hi
      that is always a problem with purely written applications like phplist and phpbb … they change versions and from time to time change function names and so on. Personally I would go modificating the CI functions instead of phplists’.

      About the ACL … I use my own logic for that so cannot suggest anything.

    • MArius
    • October 30th, 2011 1:36pm

    Hi,
    I tried to integrate PHPlist codeingniter helper in 2.03 but no results.
    I changed the initial value
    $rootPath = $this->CI->config->item(‘root_path’),
    with
    $rootPath = $this->CI->config->item(‘base_url’);

    In controller i have
    $email = $this->input->post(‘email’);
    $this->load->library(‘phplist_library’);
    $this->phplist_library->user_add($email,’3′);// or ($email) or ($email,3)

    problem is – not adding users
    You can help me?
    thank you

    • Hi Marius,

      the meaning of the root_path in the script is to point the path to the folder where the phplist folder is located … so base_url will not work. It should be absolute or relaative server path – something like /var/www/ or ”

    • Marius
    • October 30th, 2011 6:30pm

    Yes but this item(‘root_path’) belongs to instance of CI…

    if I leave this root_path the application stops when loads the library,

    Do you have an example of using this library because in this moment for me does not work

    thank you

    • Marius
    • October 30th, 2011 9:59pm

    ok…
    i make some insert in user renaming redirect from ci but still have some error
    scheme, pageroot, strToUnsubscribe, strToUpdate, strForwardTitle, strForwardFooter and Message: Use of undefined constant TEST – assumed ‘TEST’
    in admin/defaultconfig.inc

    How to solve them?
    thank you

      • kingcole
      • October 31st, 2011 6:52pm

      Marius :
      ok…
      i make some insert in user renaming redirect from ci but still have some error
      scheme, pageroot, strToUnsubscribe, strToUpdate, strForwardTitle, strForwardFooter and Message: Use of undefined constant TEST – assumed ‘TEST’
      in admin/defaultconfig.inc
      How to solve them?
      thank you

      same error here… thers’s any solution about that?

        • Marius
        • October 31st, 2011 9:47pm

        changing this in constructor will clear the errors

        global $tables, $table_prefix, $config, $usertable_prefix, $database_connection, $pageroot,$scheme, $strForwardTitle, $strForwardFooter,$strToUnsubscribe, $strToUpdate;

        I have another question
        How do i send standard phplist submit email when update database from CI?

        • I couldn’t understand the question … problably asking for adding a registered user to a specific list ?
          See user_add_list($email, $list) where $list is the id of the targeted list of emails.

            • Marius
            • November 1st, 2011 2:55pm

            assignment of alist to user is ok
            but i want to sent a standard Request for confirmation with valid uid
            ?p=confirm&uid=f7ec9dd0bd1e6b310358e6ad3d04110d
            wondering if it was possible

            Thank you for your work

          • See function getUserIDByEmail … easily you can modify and get the unique id for use as you wish. The function we are using for adding users is not taking care for this stuff.

            private function getUniqueIDByEmail($email = ”) {
            global $tables, $table_prefix, $config, $usertable_prefix, $database_connection;
            if (!$email) return;

            # workaround for integration webbler/phplist
            if (!isset($table_prefix))
            $table_prefix = “phplist_”;

            if (isset($tables["attribute"])) {
            $att_table = $tables["attribute"];
            $user_att_table = $tables["user_attribute"];
            $usertable = $tables["user"];
            } else {
            $att_table = “attribute”;
            $user_att_table = “user_attribute”;
            $usertable = “user”;
            }
            $email = mysql_real_escape_string($email, $database_connection);
            $userid = Sql_Fetch_Row_Query(“select uniqid from {$usertable} where email = \”$email\”");
            $id = $userid[0];

            return $id;
            }

            Then send the email -> the native code they use is in subscribelib2.php line 376.

          • Gregas
          • November 5th, 2011 6:12pm

          Marius did you solve confirmation request email?
          I can get uniq id but sendMail function does not work.

    • Gregas
    • November 1st, 2011 12:42pm

    Hi

    I also get Message: Use of undefined constant TEST – assumed ‘TEST’

    have changed globals in constructor, but with no success

    any ideas?

    Thank You

  2. I have created this library some time ago so it was done testing with an old version of PHPList. So I assume you are all using the latest and there could be some problems. For now I don’t have the time to act on this but I will update the lib as soon as possible.

    • Marius
    • November 1st, 2011 2:45pm

    In admin/defaultconfig.inc change
    if (!TEST && REGISTER && defined(“VERSION”))
    to
    if (!”TEST” && “REGISTER” && defined(“VERSION”))

    • Gregas
    • November 1st, 2011 3:03pm

    Thanks Marius that works

    But i have more problems

    Everything stops after include($phplist_root_path . ‘admin/connect.php’),
    and i see no errors

    If i comment this line i can call user_add function…

    Help would be appreciated

      • Marius
      • November 1st, 2011 3:15pm

      Change in CI in helper/url_helper the name of redirect
      I have last phplist version

        • Gregas
        • November 1st, 2011 3:45pm

        Could you paste your code, because i don’t understand you answer.

        Thank You

          • Marius
          • November 1st, 2011 3:54pm

          in admin/connect.php you have the same function Redirect like in CI

          I don’t know if this is the unique place where this function exist…
          Was more easy to change in CI helper/url_helper name of this function to another name(ex redirect2). Also make change to all controllers where you use this redirect

          • Yes that is right! I remember I had the same problem when integrating with PHPBB and already had done that so that never came to my attention! I will add it as a note in the post. Thanks for noting that Marius.

            • kingcole
            • November 2nd, 2011 4:48pm

            Thank you, Marius, for your help. But i use CI redirect() function all over my project… there’s another way to make things working without renaming redirect() function in ci>helpers>url_helper?

          • KingCole, either changing it there and all over your project or changing it in phplist and all over the source code of phplist. These are the options.

    • Gregas
    • November 1st, 2011 3:09pm

    Marius do you have the latest PHPList version installed ?

    • Marius
    • November 1st, 2011 3:43pm

    thanks again

    I will try

    • Gregas
    • November 1st, 2011 4:07pm

    Marius Thank You it works

    Georgi Budinov great library, thanks

    • kingcole
    • November 2nd, 2011 5:02pm

    Georgi Budinov :
    KingCole, either changing it there and all over your project or changing it in phplist and all over the source code of phplist. These are the options.

    I was looking for a way to unload the url helper, but nothing seems to work… i’ll rename the function inside the “lists” folder. thanx

  3. It’d not often be a J12 wthout using familiar dial this includes squared arms and turning applied Arabic numeral an hour markers. Whereas there is not a lot of lume, the hand-applied SuperLumiNova does a great job plus the dial retains an easily affordable level about legibility. Of course there does exist the addition belonging to the fourth hand for the GMT proof. Tipped using a red arrow, it serves its purpose very well getting tight enough in the 24 60 minute block scale. When everyone pull all the crown out a lttle bit, just this hour hand of this local point in time moves. Alternatively, in it watch after you pull the actual crown out a lttle bit just that GMT side moves.

  4. One of the problems is , is that you accept visitors to the net site of their human to start a new online aggregation face indeed .

  5. It also means that if you live in London, you can connect to the WiFi on the underground and make calls on the platform while waiting for the next train. We tried this, and while it works, people that aren’t aware of WiFi calling will look at you like you’re crazy.

  6. I was longing to read such kind of informative write ups from a very long time.

  7. Good to know that such kind of posts are there to help ignorant and novice people.

  8. adfdafdf

  9. adfasdfdsf

  10. led street light OAK LED Co ,Limited is one of the top level China LED street light manufacturers and suppliers with one of the famous LED street light brands, as a professional company, we have our own factory, which is able to produce high quality products with reliable quality led street light

  1. No trackbacks yet.