Location: PHPKode > projects > ADbNewsSender > ADbNewsSender_2.0.4/protected/skeleton/private_apis.class.php.inc
<?php
/* 
    Last change in version: 2.0 Beta 2
#########################################################################################
#    ADbNewsSender 2 									#
#    Copyright (C) 2009  Florian Grannemann (E-mail: hide@address.com)  		#
#    Website: http://adbnewssender.sf.net						#
#											#
#    This program is free software: you can redistribute it and/or modify		#
#    it under the terms of the GNU General Public License as published by		#
#    the Free Software Foundation, either version 3 of the License, or			#
#    any later version.									#
#											#
#    This program is distributed in the hope that it will be useful,			#
#    but WITHOUT ANY WARRANTY; without even the implied warranty of			#
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the			#
#    GNU General Public License for more details.					#
#											#
#    You should have received a copy of the GNU General Public License			#
#    along with this program.  If not, see http://www.gnu.org/licenses/.		#
#########################################################################################
	
*/
/*
	This file defines the private APIs for administrative works 
	
*/

//API for read/write access to global config settings:
//Using all methods and attributes from global_config_ro
Class global_config_rw extends global_config_ro
{

	function add_setting($name, $value="")
	{
		if(!isset($name))
		{
			return false;
		}
		if(!$this->DB->is_connected())
		{
			if(!@$this->DB->connect())
			{
				
				return false;
			}
		}
		
		$columns=array();
		$columns["name"]=$this->DB->real_escape_string($name);
		$columns["value"]=$this->DB->real_escape_string("$value");   
		
		if(@$this->DB->insert_into("ADBNS2_global_config_table",$columns))
		{
			return true;
		}
		
		return false;
	}
	
	function update_setting($name, $newvalue="")
	{
		if(!isset($name))
		{
			return false;
		}
		if(!$this->DB->is_connected())
		{
			if(!@$this->DB->connect())
			{
				
				return false;
			}
		}
		$result=@$this->DB->query("SELECT Count(*) from  ADBNS2_global_config_table  WHERE name='".$this->DB->real_escape_string($name)."';");
		
		$row=@$this->DB->fetch_row($result);

		if($row[0] <1)
		{
			return false;
		}		

		if(!(@$this->DB->query("UPDATE  ADBNS2_global_config_table SET value='".$this->DB->real_escape_string($newvalue)."' WHERE name='".$this->DB->real_escape_string($name)."';")))
		{
			return false;
		}
		
			
			return true;
		
		
	
		
	

	}
	
	function remove_setting($name)
	{
		if(!isset($name))
		{
			return false;
		}
		if(!$this->DB->is_connected())
		{
			if(!@$this->DB->connect())
			{
				
				return false;
			}
		}
		$result=@$this->DB->query("SELECT Count(*) from  ADBNS2_global_config_table  WHERE name='".$this->DB->real_escape_string($name)."';");
		
		$row=@$this->DB->fetch_row($result);

		if($row[0] <1)
		{
			return false;
		}	

		if(@$this->DB->query("DELETE from  ADBNS2_global_config_table WHERE name='".$this->DB->real_escape_string($name)."';"))
		{
			return true;
		}
		
		return false;
	}

	
	function rename_setting($oldname, $newname)
	{
		
		if(!isset($oldname) || !isset($newname))
		{
			return false;
		}
		if(!$this->DB->is_connected())
		{
			if(!@$this->DB->connect())
			{
				
				return false;
			}
		}
		$result=@$this->DB->query("SELECT Count(*) from  ADBNS2_global_config_table  WHERE name='".$this->DB->real_escape_string($oldname)."';");
		
		$row=@$this->DB->fetch_row($result);

		if($row[0] <1)
		{
			return false;
		}	
		if(@$this->DB->query("UPDATE  ADBNS2_global_config_table SET name='".$this->DB->real_escape_string($newname)."' WHERE name='".$this->DB->real_escape_string($oldname)."';"))
		{
			return true;
		}
		
		return false;
	}

	
	function replace_setting($name, $newvalue="")
	{
		if(!isset($name))
		{
			return false;
		}
		if(!$this->DB->is_connected())
		{
			if(!@$this->DB->connect())
			{
				
				return false;
			}
		}
		
		$columns=array();
		
		$columns["name"]=$this->DB->real_escape_string($name);
		$columns["value"]=$this->DB->real_escape_string("$newvalue");   
		
		if(@$this->DB->replace_into("ADBNS2_global_config_table",$columns))		
		{
			
			return true;
		}
		
		return false;
	}


	function get_NL_creation_time($NLID)
	{
		if(!$this->NL_exists($NLID))
		{
			return 0;
		}		

		if(!$this->DB->is_connected())
		{
			if(!@$this->DB->connect())
			{
				
				return 0;
			}
		}

		$res=$this->DB->query("Select created from ADBNS2_Newsletters where id=$NLID;");
		$row=$this->DB->fetch_row($res);
		
		return $row[0];
	}

	function create_Newsletter($NL_name)
	{
		
		//Return codes:
		//0: no/failed DB connenction
		//>0: ID of new Newsletter
		if(!$this->DB->is_connected())
		{
			if(!@$this->DB->connect())
			{
				
				return 0;
			}
		}
		$today=time();
		$columns=array();
		$columns["created"]=$today;
		
		
		if(!(@$this->DB->insert_into("ADBNS2_Newsletters",$columns)))
		{
			
			return 0;
		}
		$NLID=@($this->DB->get_last_insert_ID());
		if($NLID==0)
		{
			$res=$this->DB->query("Select id from  ADBNS2_Newsletters where created=$today;");
			$row=$this->DB->fetch_row($res);
			$NLID=$row[0];
			
		}
		
		if($NLID==0)
		{	
			return 0;
		}
		
		

		$tablename="ADBNS2_NL_Config_Table_".$NLID;
		$columns=array();
		$columns["name"]="varchar|100";
		$columns["value"]="text";
		$PKs="name";
		$defaults=array();
		$defaults["value"]="";
		
		if(!(@$this->DB->create_table($tablename,$columns,$defaults,$PKs)))
		{
			
			
			@$this->DB->query("DELETE FROM  ADBNS2_Newsletters WHERE id=".$NLID.";");
			return 0;
		}

		    $tablename="ADBNS2_NL_Config_Table_".$NLID;
		    $columns=array();
		    $columns["name"]="NL_name";
		    $columns["value"]=$this->DB->real_escape_string($NL_name);
		   
		if(!(@$this->DB->insert_into($tablename,$columns)))
		{	
			@$this->DB->query("DELETE FROM  ADBNS2_Newsletters WHERE id=".$NLID.";");
			@$this->DB->drop_table("ADBNS2_NL_Config_Table_".$NLID);
			
			return 0;
		}
		//maillinglist:
		
		
		
		$tablename="ADBNS2_Maillinglist_".$NLID;
		$columns=array();
		$columns["id"]="bigserial";
		$columns["email"]="varchar|75";
		$columns["ip"]="varchar|15";
		$columns["timestamp_entry"]="bigint";
		$columns["timestamp_renewal"]="bigint";
		$columns["timestamp_expires"]="bigint";

		$PKs="id";
		$UKs="email";
		$defaults=array();
		
		if(!(@$this->DB->create_table($tablename,$columns,$defaults,$PKs,$UKs)))
		{	
			
			@$this->DB->query("DELETE FROM  ADBNS2_Newsletters WHERE id=".$NLID.";");
			@$this->DB->drop_table("ADBNS2_NL_Config_Table_".$NLID);
			
			return 0;
		}
		
		//Pendinglist:
		
		$tablename="ADBNS2_Pendinglist_".$NLID;
		$columns=array();
		$columns["id"]="bigserial";
		$columns["email"]="varchar|75";
		$columns["ip"]="varchar|15";
		$columns["randomcode"]="varchar|18";
		$columns["timestamp_entry"]="bigint";
		$columns["numberofattempts"]="bigint";
		$columns["timestamp_expires"]="bigint";

		$PKs="id";
		$UKs="email";
		$defaults=array();
		$defaults["numberofattempts"]=0;
		
		if(!(@$this->DB->create_table($tablename,$columns,$defaults,$PKs,$UKs)))
		{
			
			@$this->DB->query("DELETE FROM  ADBNS2_Newsletters WHERE id=".$NLID.";");
			@$this->DB->drop_table("ADBNS2_NL_Config_Table_".$NLID);
			@$this->DB->drop_table("ADBNS2_Maillinglist_".$NLID);

			return 0;
		}

		//Archive:
		
		
		
		$tablename="ADBNS2_ArchiveTable_".$NLID;
		$columns=array();
		$columns["id"]="bigserial";
		$columns["message"]="text";
		$columns["subject"]="text";
		$columns["type"]="varchar|4";
		$columns["date"]="bigint";
		
		$PKs="id";

		$defaults=array();
		

		if(!(@$this->DB->create_table($tablename,$columns,$defaults,$PKs)))
		{
			@$this->DB->query("DELETE FROM  ADBNS2_Newsletters WHERE id=".$NLID.";");
			@$this->DB->drop_table("ADBNS2_NL_Config_Table_".$NLID);
			@$this->DB->drop_table("ADBNS2_Maillinglist_".$NLID);
			@$this->DB->drop_table("ADBNS2_Pendinglist_".$NLID);
	
			return 0;
		}

		//Drafts:
		
		$tablename="ADBNS2_DraftsTable_".$NLID;
		$columns=array();
		$columns["id"]="bigserial";
		$columns["message"]="text";
		$columns["subject"]="text";
		$columns["type"]="varchar|4";
		$columns["date"]="bigint";
		
		$PKs="id";

		$defaults=array();
		

		if(!(@$this->DB->create_table($tablename,$columns,$defaults,$PKs)))
		{
			@$this->DB->query("DELETE FROM  ADBNS2_Newsletters WHERE id=".$NLID.";");
			@$this->DB->drop_table("ADBNS2_NL_Config_Table_".$NLID);
			@$this->DB->drop_table("ADBNS2_Maillinglist_".$NLID);
			@$this->DB->drop_table("ADBNS2_Pendinglist_".$NLID);
			@$this->DB->drop_table("ADBNS2_ArchiveTable_".$NLID);
			return 0;
		}


		return $NLID; 
		
	}
	
	function delete_Newsletter($NL_ID)
	{
		if(!$this->DB->is_connected())
		{
			if(!@$this->DB->connect())
			{
				
				return false;
			}
		}
		
		$NL_ID=$NL_ID*1;

		if(!$this->NL_exists($NL_ID))
		{
			return false;
		}
		@$this->DB->query("DELETE FROM  ADBNS2_Newsletters WHERE id=".$NL_ID.";");
		@$this->DB->drop_table("ADBNS2_NL_Config_Table_".$NL_ID);
		
		@$this->DB->drop_table("ADBNS2_Maillinglist_".$NL_ID);
		@$this->DB->drop_table("ADBNS2_Pendinglist_".$NL_ID);
		@$this->DB->drop_table("ADBNS2_ArchiveTable_".$NL_ID);
		@$this->DB->drop_table("ADBNS2_DraftsTable_".$NL_ID);
		
	
		return true;
	}

	
	
	function get_NextMaintenance()
	{
		if(!$this->DB->is_connected())
		{
			if(!@$this->DB->connect())
			{
				
				return 0;
			}
	
		}
		if(@($result=$this->DB->query("Select value from  ADBNS2_global_config_table WHERE name='nextMaintenance';")))
		{
			$row=@$this->DB->fetch_row($result);
			return $row[0];
		}
		return 0;
		
	}
	function set_NextMaintenance($offset=1)
	{
		$offset=$offset*1;

		if($offset <1)
		{
			return false;
		}
		if(!$this->DB->is_connected())
		{
			if(!@$this->DB->connect())
			{
				
				return 0;
			}
		}
		$nextMaintenance=strtotime("+ $offset hour");			

		if(@$this->DB->query("Update  ADBNS2_global_config_table SET value='$nextMaintenance' where name='NextMaintenance'; "))
		{
			return true;
		}
		
		return false;
	}
		
	
}

//private Newsletter API:
class priv_Newsletter extends pub_Newsletter
{
	function add_setting($name, $value="")
	{
		if(!isset($name))
		{
			return false;
		}
		if(!$this->DB->is_connected())
		{
			if(!@$this->DB->connect())
			{
				
				return false;
			}
		}
		
		$columns=array();
		$columns["name"]=$this->DB->real_escape_string($name);
		$columns["value"]=$this->DB->real_escape_string("$value");   
		
		if(@$this->DB->insert_into("ADBNS2_NL_Config_Table_".$this->ID,$columns))
		{
			return true;
		}
		
		return false;
	}
	
	function update_setting($name, $newvalue="")
	{
		if(!isset($name))
		{
			return false;
		}
		if(!$this->DB->is_connected())
		{
			if(!@$this->DB->connect())
			{
				
				return false;
			}
		}
		$result=@$this->DB->query("SELECT Count(*) from ADBNS2_NL_Config_Table_".$this->ID."  WHERE name='".$this->DB->real_escape_string($name)."';");
		
		$row=@$this->DB->fetch_row($result);

		if($row[0] <1)
		{
			return false;
		}		

		if(!(@$this->DB->query("UPDATE ADBNS2_NL_Config_Table_".$this->ID." SET value='".$this->DB->real_escape_string($newvalue)."' WHERE name='".$this->DB->real_escape_string($name)."';")))
		{
			return false;
		}
		
			
			return true;
		
		
	
		
	

	}
	
	function remove_setting($name)
	{
		if(!isset($name))
		{
			return false;
		}
		if(!$this->DB->is_connected())
		{
			if(!@$this->DB->connect())
			{
				
				return false;
			}
		}
		$result=@$this->DB->query("SELECT Count(*) from ADBNS2_NL_Config_Table_".$this->ID."  WHERE name='".$this->DB->real_escape_string($name)."';");
		
		$row=@$this->DB->fetch_row($result);

		if($row[0] <1)
		{
			return false;
		}	

		if(@$this->DB->query("DELETE from ADBNS2_NL_Config_Table_".$this->ID." WHERE name='".$this->DB->real_escape_string($name)."';"))
		{
			return true;
		}
		
		return false;
	}

	
	function rename_setting($oldname, $newname)
	{
		
		if(!isset($oldname) || !isset($newname))
		{
			return false;
		}
		if(!$this->DB->is_connected())
		{
			if(!@$this->DB->connect())
			{
				
				return false;
			}
		}
		$result=@$this->DB->query("SELECT Count(*) from ADBNS2_NL_Config_Table_".$this->ID."  WHERE name='".$this->DB->real_escape_string($oldname)."';");
		
		$row=@$this->DB->fetch_row($result);

		if($row[0] <1)
		{
			return false;
		}	
		if(@$this->DB->query("UPDATE ADBNS2_NL_Config_Table_".$this->ID." SET name='".$this->DB->real_escape_string($newname)."' WHERE name='".$this->DB->real_escape_string($oldname)."';"))
		{
			return true;
		}
		
		return false;
	}

	
	function replace_setting($name, $newvalue="")
	{
		if(!isset($name))
		{
			return false;
		}
		if(!$this->DB->is_connected())
		{
			if(!@$this->DB->connect())
			{
				
				return false;
			}
		}
		$columns=array();
		$columns["name"]=$this->DB->real_escape_string($name);
		$columns["value"]=$this->DB->real_escape_string("$newvalue");   
		
		if(@$this->DB->replace_into("ADBNS2_NL_Config_Table_".$this->ID,$columns))		
		{
			return true;
		}
		return false;
	}
	function number_of_ML_entries()
	{
		//return codes:
		//-1: DB connection failed
		//>=0: Number of Entries on ML 
		if(!$this->DB->is_connected())
		{
			if(!@$this->DB->connect())
			{
				
				return -1;
			}
		}
		$result=0;
		
		$res=@$this->DB->query("Select count(*) from ADBNS2_Maillinglist_".$this->ID);

		if($row=@$this->DB->fetch_row($res))
		{
			$result=$row[0];
		}
	
		return $result;
	}

	function number_of_PL_entries()
	{
		//return codes:
		//-1: DB connection failed
		//>=0: Number of Entries on PL 
		if(!$this->DB->is_connected())
		{
			if(!@$this->DB->connect())
			{
				
				return -1;
			}
		}
		$result=0;
		
		$res=@$this->DB->query("Select count(*) from ADBNS2_Pendinglist_".$this->ID);

		if($row=@$this->DB->fetch_row($res))
		{
			$result=$row[0];
		}
	
		return $result;
	}
	
	

	function number_of_expiring_entries()
	{
		//return codes:
		//-1: DB connection failed
		//>=0: Number of expiring Entries on ML 
		if(!$this->DB->is_connected())
		{
			if(!@$this->DB->connect())
			{
				
				return -1;
			}
		}
		$result=0;
		
		$today=time();
		$res=@$this->DB->query("Select count(*) from ADBNS2_Maillinglist_".$this->ID." where timestamp_renewal < $today");

		if($row=@$this->DB->fetch_row($res))
		{
			$result=$row[0];
		}
	
		return $result;

	}
	function number_of_ML_entries_today()
	{
		//return codes:
		//-1: DB connection failed
		//>=0: Number of expiring Entries on ML 
		if(!$this->DB->is_connected())
		{
			if(!@$this->DB->connect())
			{
				
				return -1;
			}
		}
		$result=0;
		
		$today=mktime ( 0, 0, 0, date("n"), date("j"), date("Y") );
		$res=@$this->DB->query("Select count(*) from ADBNS2_Maillinglist_".$this->ID." where timestamp_entry >= $today");

		if($row=@$this->DB->fetch_row($res))
		{
			$result=$row[0];
		}
	
		return $result;

	}


	function send_NL($message,$subject,$type,$path_to_NL_langfile)
	{
		

		if(!isset($message)|| !isset($subject) ||!isset($type) || !isset($path_to_NL_langfile))
		{
			return false;

		}
			
		if(!file_exists($path_to_NL_langfile))
		{
			return false;
		}

		
		include_once "$path_to_NL_langfile";
		if(!$this->DB->is_connected())
		{
			if(!@$this->DB->connect())
			{
				
				return false;
			}
		}

		//to encode special characters:
		$subject=mb_encode_mimeheader($subject,strtolower($charset));
		$from_name=stripslashes(mb_encode_mimeheader(html_entity_decode($this->get_NLconfig_setting("NL_name")),strtolower($charset)));
		$myCFG =new global_config_ro($this->DB);
		$header="From: $from_name <".stripslashes($this->get_NLconfig_setting("Email")).">\n";
		$header .="X-Mailer: ADbNewsSender ".$myCFG->get_config_value("Version")."\n";
		

		$Add_to_Message="";
		$messages=array();	 
		if(strtolower($type) =="html")
		{
			$header .= "Content-Type: text/html; charset=\"$charset\""."\n";
			$messages[0]=$HTML_inExpirationPeriod[0];
			$messages[1]=$HTML_inExpirationPeriod[1]."<br>";
			$messages[2]=$HTML_Information[0];	
			$messages[3]=$HTML_Information[1];
			$messages[4]=$HTML_Information[2];
			
			if(!preg_match("/\[UnsubscribeLink\]/",$message))
			{
				$message=preg_replace("/<\/body>/i","",$message);
				$message=preg_replace("/<\/html>/i","",$message);
				$message=$message."<br>[UnsubscribeLink]</BODY></HTML>";
						
			}

			
 
		}
		else
		{
			$header .= "Content-Type: text/plain; charset=\"$charset\""."\n";
			$messages[0]=$TXT_inExpirationPeriod[0];
			$messages[1]=$TXT_inExpirationPeriod[1]."\n";
			$messages[2]=$TXT_Information[0];	
			$messages[3]=$TXT_Information[1];
			$messages[4]=$TXT_Information[2];
			
		}
		
		

		
			$messages[1]=preg_replace("/<-time_on_ML->/",$this->get_NLconfig_setting("Time_on_ML"),$messages[1]);
			$messages[1]=preg_replace("/<-RenewalURL->/",$myCFG->get_config_value("RenewalURL"),$messages[1]);
			$messages[1]=preg_replace("/<-ID->/",$this->ID,$messages[1]);
			
			$messages[2]=preg_replace("/<-timezone->/",$myCFG->get_timezone(),$messages[2]);
			$messages[3]=preg_replace("/<-timezone->/",$myCFG->get_timezone(),$messages[3]);
			$messages[4]=preg_replace("/<-ID->/",$this->ID,$messages[4]);
			$messages[4]=preg_replace("/<-SubscribeURL->/",$myCFG->get_config_value("SubscribeURL"),$messages[4]);
			
			
			

			$result=$this->DB->query("SELECT email, ip, timestamp_entry, timestamp_renewal, timestamp_expires From ADBNS2_Maillinglist_".$this->ID);
			
			$today=time();
			while($row=@$this->DB->fetch_row($result))
			{	
				$add_to_message="";	
				
				if($row[3] <=$today)
				{
					$add_to_message=$add_to_message.$messages[0];
					$tmp=preg_replace("/<-Email->/",$row[0],$messages[1]);
					
					$add_to_message=$add_to_message.preg_replace("/<-SubscriptionKey->/",$row[4],$tmp);	
				}
				
				$tmp=preg_replace("/<-date->/",date($dateformat,$row[2]),$messages[2]);
				$tmp=preg_replace("/<-time->/",date($timeformat,$row[2]),$tmp);				
				$tmp=preg_replace("/<-IP->/",$row[1],$tmp);
				
				$add_to_message=$add_to_message.$tmp;

				$add_to_message=$add_to_message.preg_replace("/<-DateExpires->/", date($dateformat.", ".$timeformat,$row[4]),$messages[3]);

				$add_to_message=$add_to_message.preg_replace("/<-Email->/",$row[0],$messages[4]);
				

				if(strtolower($type) =="html")
				{
					$send_message=preg_replace("/\[UnsubscribeLink\]/",$add_to_message,$message);						
				}
				else
				{
					$send_message=$message."\n\n".$add_to_message;
				}
				
				@mail($row[0],$subject,stripslashes($send_message),$header);
				/*print "to: ".$row[0]."<br>";
				print "subject: $subject<br>";
				print "message: ".stripslashes($send_message)."<br>";
				print "header: $header";
				*/
			}
		 //do we have a permanent recipient?

		if($this->get_NLconfig_setting("Permanent_recipient"))
		{
		    if(strtolower($type) =="html")
		    {
			$message=preg_replace("/\[UnsubscribeLink\]/",$TXT_Permanent_recipient,$message);						
		    }
		    else
		    {
			$message=$message."\n\n".$TXT_Permanent_recipient;
		    }
				
		    
		    $Permanent_recipient=preg_replace("/ /","",$this->get_NLconfig_setting("Permanent_recipient"));
		    //yes: send a copy:
		    @mail($Permanent_recipient,$subject,stripslashes($message),$header);
		}



			

		return true;
	}

	
	function get_ML_entries($start,$number_of_entries,$orderby="id", $order_direction="ASC",$option="all")
	{

		$start=intval($start);
		$number_of_entries=intval($number_of_entries); 
		$result=array(array());

		if($start <0 || !isset($start))
		{
			$start=0;

		}
		if(!isset($number_of_entries) || $number_of_entries > 50)
		{
			$offset=50;

		}		
		

		$orderby=strtolower($orderby);
		if($orderby !="id" && $orderby != "timestamp_entry" && $orderby != "timestamp_expires" && $orderby !="ip" && $orderby !="email" && $orderby != "timestamp_renewal")
		{
			return $result;
		}

		if(strtoupper($order_direction) != "ASC" && strtoupper($order_direction) != "DESC")
		{
			return $result;
		}

		if(strtolower($option) != "all" && strtolower($option) != "expiring" && strtolower($option) != "today")
		{
			return $result;
		}
	
		if(!$this->DB->is_connected())
		{
			if(!@$this->DB->connect())
			{
				
				return $result;
			}
		}
		
		if(strtolower($option)=="all")
		{
		    $res=$this->DB->query("SELECT id, email, ip, timestamp_entry, timestamp_renewal, timestamp_expires From ADBNS2_Maillinglist_".$this->ID." ORDER BY $orderby ".strtoupper($order_direction)." LIMIT $start, $number_of_entries;");
		}
		if(strtolower($option)=="expiring")
		{
		    
		    $res=$this->DB->query("SELECT id, email, ip, timestamp_entry, timestamp_renewal, timestamp_expires From ADBNS2_Maillinglist_".$this->ID." WHERE timestamp_renewal<=".time()." ORDER BY $orderby ".strtoupper($order_direction)." LIMIT $start, $number_of_entries;");
		}
		if(strtolower($option)=="today")
		{
		    $today=mktime ( 0, 0, 0, date("n"), date("j"), date("Y") );
		    $res=$this->DB->query("SELECT id, email, ip, timestamp_entry, timestamp_renewal, timestamp_expires From ADBNS2_Maillinglist_".$this->ID." WHERE timestamp_entry >= $today ORDER BY $orderby ".strtoupper($order_direction)." LIMIT $start, $number_of_entries;");
		}
		$i=0;
		while($row=$this->DB->fetch_array($res,"ASSOC"))
		{
		
			
			foreach ($row as $key =>$val)
			{
				$result[$i][strtolower($key)]=$val;
			} 
			
		
		

			$i++;
		}

		return $result;
	}

	function get_PL_entries($start,$number_of_entries,$orderby="id", $order_direction="ASC")
	{

		$start=intval($start);
		$number_of_entries=intval($number_of_entries); 
		$result=array();

		if($start <0 || !isset($start))
		{
			$start=0;

		}
		if(!isset($number_of_entries) || $number_of_entries > 50)
		{
			$offset=50;

		}		
		

		$orderby=strtolower($orderby);
		if($orderby !="id" && $orderby != "timestamp_entry" && $orderby != "timestamp_expires" && $orderby !="email" && $orderby !="ip" && $orderby != "numberofattempts" && $orderby != "randomcode")
		{
			return $result;
		}

		
		

		if(strtoupper($order_direction) != "ASC" && strtoupper($order_direction) != "DESC")
		{
			return $result;
		}
		
		if(!$this->DB->is_connected())
		{
			if(!@$this->DB->connect())
			{
				
				return $result;
			}
		}
		
			
		$res=$this->DB->query("SELECT id, email, ip, randomcode,numberofattempts,timestamp_entry, timestamp_expires From ADBNS2_Pendinglist_".$this->ID." ORDER BY $orderby ".strtoupper($order_direction)." LIMIT $start, $number_of_entries;");

		$i=0;
		while($row=$this->DB->fetch_array($res,"ASSOC"))
		{
			
			foreach ($row as $key =>$val)
			{
				$result[$i][strtolower($key)]=$val;
			} 
			//$result[$i]=$row;

			$i++;
		}

		return $result;

		


	}
	function get_ML_entry_data($entryID)
	{
		$entryID=intval($entryID);
		$result=array();
		if($entryID <1)
		{
			return $result;
		}
		$res=$this->DB->query("SELECT id, email, ip, timestamp_entry, timestamp_renewal, timestamp_expires From ADBNS2_Maillinglist_".$this->ID." WHERE ID=$entryID;");
		$result1=@$this->DB->fetch_array($res,"ASSOC");
		foreach ($result1 as $key =>$val)
		{
			$result[strtolower($key)]=$val;
		} 
		return $result;
	


	}

	function get_PL_entry_data($entryID)
	{
		$entryID=intval($entryID);
		$result=array();
		if($entryID <1)
		{
			return $result;
		}
		$res=$this->DB->query("SELECT id, email, ip, randomcode,numberofattempts,timestamp_entry, timestamp_expires From ADBNS2_Pendinglist_".$this->ID." WHERE ID=$entryID;");
		$result1=@$this->DB->fetch_array($res,"ASSOC");
		foreach ($result1 as $key =>$val)
		{
			$result[strtolower($key)]=$val;
		} 
		return $result;
	
	}
}

//private Archive API:
class priv_Archive extends pub_Archive
{
    function add_entry($message,$subject,$type)
    {
	//$message and $subject will be encodet with htmlentities()
	//return values:
	//0: DB Error
	//>0: ID of new entry
	
	
	if(!$this->DB->is_connected())
	{
	    if(!@$this->DB->connect())
	    {				
		return 0;
	    }
	}
	
	$timestamp=time();
	
	if($type !="html")
	{
	    $type="text";
	}
	$tablename="ADBNS2_ArchiveTable_".$this->ID;
	$columns=array();
	$columns["message"]=addslashes(htmlentities($message));
	$columns["subject"]=addslashes(htmlentities($this->DB->real_escape_string($subject)));
	$columns["type"]=$type;
	$columns["date"]=intval($timestamp);
	$this->DB->insert_into($tablename, $columns);

	$return_val=$this->DB->get_last_insert_ID();
	if($return_val <1)
	{
	    $result=$this->DB->query("SELECT id from ADBNS2_ArchiveTable_".$this->ID." WHERE message='".$columns["message"]."' AND subject='".$columns["subject"]."' and type='".$columns["type"]."' AND date=".$columns["date"]." LIMIT 1;"); 
	    $row=@$this->DB->fetch_row($result);
	    $return_val=$row[0];
	}
	return $return_val;
    }
    function delete_entry($entryID)
    {
	//return true: success, false: failed
	$entryID=intval($entryID);

	if(!$this->DB->is_connected())
	{
	    if(!@$this->DB->connect())
	    {				
		return false;
	    }
	}
	if($this->DB->query("DELETE FROM ADBNS2_ArchiveTable_".$this->ID." WHERE id=$entryID;"))
	{
	    return true;
	}
	return false;
	
    }


}

//**********************************************************
//Class ADBNS_Drafts
//**********************************************************
class ADBNS_Drafts
{
    private $ID=NULL;
    Private $DB=NULL;

    function __construct($NLID,$DB_object)
    {	
	$NLID=intval($NLID);
	if($NLID <1)
	{
			
	    user_error(__CLASS__.": ERROR: please provide a valid newsletter ID!  (In method: <b>".__METHOD__."</b>)",E_USER_ERROR);

	}
		
				
	if($DB_object==NULL)
	{
	    user_error(__CLASS__.": ERROR: Invalid Database Object.  (In method: <b>".__METHOD__."</b>)",E_USER_ERROR);
	
	}
			
	$this->DB=$DB_object;

	if(!$this->DB->is_connected())
	{
	    $this->DB->connect();
	}   
	
		
	$res=$this->DB->query("Select id from ADBNS2_Newsletters where id = ".$this->DB->real_escape_string($NLID).";");
	$row=@$this->DB->fetch_row($res);
	
	    
	if(!$row[0])
	{
	    user_error(__CLASS__.": ERROR: Newsletter with ID $NLID does not exist!  (In method: <b>".__METHOD__."</b>)",E_USER_ERROR);
	
	}
	$this->ID=$NLID;
    }

   function delete_draft($draftID)
   {
	//return true: success, false: failed
	$draftID=intval($draftID);

	if($draftID < 1)
	{
	    return false;
	}
	if(!$this->DB->is_connected())
	{
	    if(!@$this->DB->connect())
	    {				
		return false;
	    }
	}
	
	$res=$this->DB->query("Select id FROM ADBNS2_DraftsTable_".$this->ID." WHERE id=$draftID;");
	$row=@$this->DB->fetch_row($res);
	if($row[0] != $draftID)
	{
	   
	    return false;
	}
	
	if($this->DB->query("DELETE FROM ADBNS2_DraftsTable_".$this->ID." WHERE id=$draftID;"))
	{
	    return true;
	}
	return false;
	
   }

    function add_draft($message,$subject,$type)
    {
	//$message and $subject will be encodet with htmlentities()
	//return values:
	//0: DB Error
	//>0: ID of new draft
	
	
	if(!$this->DB->is_connected())
	{
	    if(!@$this->DB->connect())
	    {				
		return 0;
	    }
	}
	
	$timestamp=time();
	
	if($type !="html")
	{
	    $type="text";
	}
	$tablename="ADBNS2_DraftsTable_".$this->ID;
	$columns=array();
	$columns["message"]=addslashes(htmlentities($message));
	$columns["subject"]=addslashes(htmlentities($this->DB->real_escape_string($subject)));
	$columns["type"]=$type;
	$columns["date"]=intval($timestamp);
	$this->DB->insert_into($tablename, $columns);

	$return_val=$this->DB->get_last_insert_ID();
	if($return_val <1)
	{
	    $result=$this->DB->query("SELECT id from ADBNS2_DraftsTable_".$this->ID." WHERE message='".$columns["message"]."' AND subject='".$columns["subject"]."' and type='".$columns["type"]."' AND date=".$columns["date"]." LIMIT 1;"); 
	    $row=@$this->DB->fetch_row($result);
	    $return_val=$row[0];
	}
	return $return_val;
    }


    function get_draft_data($draftID)
    {
	$draftID=intval($draftID);
	$return_val=array();

	     
	if(!$this->DB->is_connected())
	{
	    if(!@$this->DB->connect())
	    {
		return  $return_val;
	    }
	}
	$result=$this->DB->query("Select id, message, subject, type, date from ADBNS2_DraftsTable_".$this->ID." WHERE id=$draftID LIMIT 1; ");

	$row=@$this->DB->fetch_array($result,"ASSOC");
	    //for compatibility reasons:
	if(count($row) >1)
	{
		
	    foreach($row as $key =>$val)
	    {
		$return_val[strtolower($key)]=$val;
	    }
	    $return_val["message"]=stripslashes($return_val["message"]);
	    $return_val["subject"]=stripslashes($return_val["subject"]);
	}
	
	return $return_val;
    }

    function get_drafts($start,$number_of_drafts,$orderby="id", $order_direction="ASC")
    {

	$start=intval($start);
	$number_of_drafts=intval($number_of_drafts); 
	$result=array();

	if($start <0 || !isset($start))
	{
	    $start=0;

	}
	if(!isset($number_of_drafts) || $number_of_drafts > 50)
	{
	    $offset=50;

	}		
   

	$orderby=strtolower($orderby);
	if($orderby !="id" && $orderby != "message" && $orderby != "subject" && $orderby !="date" && $orderby !="type")
	{
	    return $result;
	}

		
		

	if(strtoupper($order_direction) != "ASC" && strtoupper($order_direction) != "DESC")
	{
	    return $result;
	}
		
	if(!$this->DB->is_connected())
	{
	    if(!@$this->DB->connect())
	    {
				
		return $result;
	    }
	}
		
			
	$res=$this->DB->query("SELECT id, message, subject, type, date From ADBNS2_DraftsTable_".$this->ID." ORDER BY $orderby ".strtoupper($order_direction)." LIMIT $start, $number_of_drafts;");

	$i=0;
	while($row=$this->DB->fetch_array($res,"ASSOC"))
	{
			
	    foreach ($row as $key =>$val)
	    {
		$result[$i][strtolower($key)]=$val;
		
	    } 
			//$result[$i]=$row;
	    $result[$i]["message"]=stripslashes($result[$i]["message"]);
	    $result[$i]["subject"]=stripslashes($result[$i]["subject"]);
	    $i++;
	}

	return $result;

		


    }
    function update_draft($draftID, $message,$subject,$type)
    {
	//return values:
	//TRUE: success
	//FALSE: failed

	$draftID=intval($draftID);
	if($draftID <1)
	{
	    return false;
	}
	$message=addslashes(htmlentities($message));
	$subject=addslashes(htmlentities($this->DB->real_escape_string($subject)));
	if($type !="html")
	{
	    $type="text";
	}
	if(!$this->DB->is_connected())
	{
	    if(!@$this->DB->connect())
	    {
				
		return false;
	    }
	}
	$date=time();
	$res=$this->DB->query("Select id FROM ADBNS2_DraftsTable_".$this->ID." WHERE id=$draftID;");
	$row=@$this->DB->fetch_row($res);
	if($row[0] != $draftID)
	{
	   
	    return false;
	}
	if(!@$this->DB->query("UPDATE ADBNS2_DraftsTable_".$this->ID." SET message='$message', subject='$subject', type='$type', date=$date WHERE id=$draftID;"))
	{
	   
	    return false;
	}
	return true;
    }

    function number_of_drafts()
    {
	//return values: 
	// -1: DB error
	// >=0: number of entries in archive 

	if(!$this->DB->is_connected())
	{
	    if(!@$this->DB->connect())
	    {
		return  -1;
	    }
	}
	$result=$this->DB->query("Select count(*) from ADBNS2_DraftsTable_".$this->ID.";");
	$row=$this->DB->fetch_row($result);
	return $row[0];
	
    }
}

?> 
Return current item: ADbNewsSender