Location: PHPKode > projects > NmnNewsletter > NmnNewsletter1.0.8/includes/NewsletterUsers.class.php
<?php
/*
    NmnNewsletter is a library that provides newsletter service 
	management for websites running php and mysql.
    Copyright (C) 2006  Ivan Preziosi from netmeans.net - Rome.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    For more informations or to join the development of the library contact
    the author at: hide@address.com
    */
class NewsletterUsers{

	function getUserArray($skipUnconfirmed, $paginated){
		if (is_null($paginated))$paginated = true;
		if(is_null($skipUnconfirmed) || $skipUnconfirmed){
			$alsoUnconfirmed='false';
		}else{
			$alsoUnconfirmed='true';
		}
		$dbConn = NewsletterDbConnector::connect();
		$query = NewsletterUsers::getUserQuery('','false',$alsoUnconfirmed,'true');
		if ($paginated){
		//pagination/////////////////////////////////////////////////////////////////////////
			$pagination = new MyPagina();
			$pagination->sql = $query; // the (basic) sql statement (use the SQL whatever you like)
			$result = $pagination->get_page_result(); // result set
			$_SESSION["navigation"] = $pagination->return_formatted_links();
		///////////////////////////////////////////////////////////////////////////////////////	
		}else{
			$result = mysql_query($query);
		}
		NewsletterDbConnector::closeConnection($dbConn);
		$returnArray = array();
		
		while ($userMail = mysql_fetch_assoc($result)){
			$returnArray[$userMail["id_".DB_USERS_TABLE.""]] = $userMail["email"];
		}
		
		return $returnArray;
	}

	function getUserQuery($key,$alsoDeleted,$alsoUnconfirmed,$alsoSubscribed){
		if (is_null($key))$key = '';
		if (is_null($alsoDeleted))$alsoDeleted = 'false';
		if (is_null($alsoUnconfirmed))$alsoUnconfirmed = 'false';
		if (is_null($alsoSubscribed))$alsoSubscribed = 'true';
		
		$firstClause = true;
		$query = "SELECT * from ".DB_USERS_TABLE." where";
		if ($alsoDeleted == 'false'){
			$query .= " deleted = '0'"; 
			$firstClause = false;
		}
		if ($alsoUnconfirmed=='false') {
			if ($firstClause){
				$query.= " status > 0";
				$firstClause = false;
			}else{
				$query.= " AND status > 0";
				$firstClause = false;				  
			}
		}
		if ($alsoSubscribed=='false') {
			if ($firstClause){
				$query.= " (deleted = '1' OR status = '0')";
				$firstClause = false;
			}else{
				$query.= " AND (deleted = '1' OR status = '0')";
				$firstClause = false;				  
			}
		}
		if ($firstClause){
				$query.= " email like '%".mysql_escape_string($key)."%'";
				$firstClause = false;
			}else{
				$query.= " AND email like '%".mysql_escape_string($key)."%'";
				$firstClause = false;				  
			}
		
		$query.= " ORDER BY email asc";
		return $query;
	}

	function getSearchArray($key,$alsoDeleted,$alsoUnconfirmed,$alsoSubscribed,$paginated){
		//NmnLogger::logString("UserArray requested");
		$dbConn = NewsletterDbConnector::connect();
		$query = NewsletterUsers::getUserQuery($key,$alsoDeleted,$alsoUnconfirmed,$alsoSubscribed);
		if ($paginated){
		//pagination/////////////////////////////////////////////////////////////////////////
			$pagination = new MyPagina();
			$pagination->sql = $query; // the (basic) sql statement (use the SQL whatever you like)
			$result = $pagination->get_page_result(); // result set
			$_SESSION["navigation"] = $pagination->return_formatted_links();
		///////////////////////////////////////////////////////////////////////////////////////	
		}else{
			$result = mysql_query($query);
		}
		NewsletterDbConnector::closeConnection($dbConn);
		$returnArray = array();
		
		while ($userMail = mysql_fetch_assoc($result)){
			$status = "subscribed";
			if ($userMail["deleted"] == '1'){
				$status = "deleted";
			}elseif($userMail["status"] == '0'){
				$status = "unconfirmed";
			}
			$returnArray[$userMail["id_".DB_USERS_TABLE.""]] = array($userMail["email"],$status);
		}
		
		return $returnArray;
	}

	function getWaitingList(){
		//NmnLogger::logString("WaitingList requested");
		$dbConn = NewsletterDbConnector::connect();
		$query = NewsletterUsers::getUserQuery('','false','true','false');
		$result = mysql_query($query);
		NewsletterDbConnector::closeConnection($dbConn);
		$returnArray = array();
		
		while ($userMail = mysql_fetch_assoc($result)){
			$returnArray[$userMail["id_".DB_USERS_TABLE.""]] = array($userMail["email"],$userMail["subscription_date"]);
		}
		
		return $returnArray;
	}


	function subscribeUser($userMail, $skipConfirm){
		if(is_null($userMail))$userMail='';
		if(is_null($skipConfirm))$skipConfirm=false;

		if (!is_null($userMail) && NewsletterUsers::checkEmail($userMail)){
			
			//NmnLogger::logString("Subscription request for:". $userMail."");
			$dbConn = NewsletterDbConnector::connect();
			if (!$skipConfirm){
				$query = "INSERT INTO ".DB_USERS_TABLE." (email) VALUES ('".mysql_escape_string($userMail)."')";
			}else{
				$query = "INSERT INTO ".DB_USERS_TABLE." (email,status) VALUES ('".mysql_escape_string($userMail)."','1')";
			}
			$result = @mysql_query($query);
			if ($result) $newUserId = mysql_insert_id();
			NewsletterDbConnector::closeConnection($dbConn);
			if ($result != 1){
				//NmnLogger::logString("Subscription request  NOT COMPLETED for:". $userMail."");
				return false;
			}
			//NmnLogger::logString("Subscription request for:". $userMail." (id=".$newUserId.") completed.");
			if (!$skipConfirm){
				NewsletterConfirmation::sendSubscribeConfirm($newUserId);
			}
			return true;
		}else{
			return false;
		}
	}

	function deleteUser($userId){
		if (!is_null($userId)){
			//NmnLogger::logString("Unsubscription request for user id:". $userId."");
			$dbConn = NewsletterDbConnector::connect();
			$query = "UPDATE ".DB_USERS_TABLE." SET deleted = '1' , unsubscription_date = NOW() WHERE id_".DB_USERS_TABLE." = '".mysql_escape_string($userId)."'";
			$result = @mysql_query($query);
			NewsletterDbConnector::closeConnection($dbConn);
			if ($result != 1){
				//NmnLogger::logString("Unsubscription request for user id:". $userId." NOT COMPLETED");
				return false;
			}
			//NmnLogger::logString("Unsubscription request for user id:". $userId." COMPLETED");
			return true;
		}
	}

	function getUserInfo($userId){
		//NmnLogger::logString("Requested info on user id=".$userId);
		if (!is_null($userId)){

			$dbConn = NewsletterDbConnector::connect();
			$query = "SELECT u.id_".DB_USERS_TABLE.", u.email, u.subscription_date, u.unsubscription_date, u.deleted, u.status, ".DB_LOGS_TABLE.".id_nl, ".DB_LOGS_TABLE.".stato, ".DB_LOGS_TABLE.".data_invio, ".DB_LOGS_TABLE.".data_lettura, nl.titolo FROM ".DB_USERS_TABLE." u LEFT OUTER JOIN ".DB_LOGS_TABLE." ".DB_LOGS_TABLE." ON ".DB_LOGS_TABLE.".id_user = u.id_".DB_USERS_TABLE." LEFT OUTER JOIN ".DB_NEWSLETTERS_TABLE." nl ON nl.id_".DB_NEWSLETTERS_TABLE." = ".DB_LOGS_TABLE.".id_nl WHERE u.id_".DB_USERS_TABLE." = '".mysql_escape_string($userId)."'";
			
			$result = @mysql_query($query);
			NewsletterDbConnector::closeConnection($dbConn);

			if (!$result){
				//NmnLogger::logString("Failed to retrieve info on user id=".$userId);
				return false;
			}
			$userData = array();
			while($user = mysql_fetch_assoc($result)){
				$userData[] = $user;
			}
			
			//NmnLogger::logString("Returned info on user id=".$userId);
			return $userData;

		}else{
			//NmnLogger::logString("ERROR: getUserInfo called with no userId.");
			return false;
		}
	}

	function getUser($userId){
		//NmnLogger::logString("Requested user id=".$userId);
		if (!is_null($userId)){
			$dbConn = NewsletterDbConnector::connect();
			$query = "SELECT * FROM ".DB_USERS_TABLE." u WHERE u.id_".DB_USERS_TABLE." = '".mysql_escape_string($userId)."' LIMIT 1";
			
			$result = @mysql_query($query);
			NewsletterDbConnector::closeConnection($dbConn);

			if (!$result){
				//NmnLogger::logString("Failed to retrieve user id=".$userId);
				return false;
			}
			$user = mysql_fetch_assoc($result);
			
			//NmnLogger::logString("Returned user id=".$userId);
			return $user;

		}else{
			//NmnLogger::logString("ERROR: getUser called with no userId.");
			return false;
		}
	}

	function isEmailExistent($email){
		if (!is_null($email)){
			$dbConn = NewsletterDbConnector::connect();
			$query = "SELECT email FROM ".DB_USERS_TABLE." WHERE email = '".mysql_escape_string($email)."' AND deleted = '0'";
			$result = mysql_query($query);
			if (!$result || mysql_num_rows($result) != 0){
				return true;
			}else{
				return false;
			}
			NewsletterDbConnector::closeConnection($dbConn);
		}else{
			return true;
		}
	}

	function confirmSubscription($userId, $key){
		//NmnLogger::logString("Confirming subscription for user id = ".$userId);
		if (!is_null($userId) and $key == sha1($userId.ENC_KEY)){
			$dbConn = NewsletterDbConnector::connect();
			$query = "UPDATE ".DB_USERS_TABLE." set status = '1' WHERE id_".DB_USERS_TABLE." = ".mysql_escape_string($userId);
			$result = @mysql_query($query);
			
			if(mysql_affected_rows() != 1 || !$result){
				//NmnLogger::logString("Subscription NOT confirmed for user id = ".$userId);
				return false;
			}else{
				//NmnLogger::logString("Subscription confirmed for user id = ".$userId);
				return true;
			}
			NewsletterDbConnector::closeConnection($dbConn);
		}else{
			//NmnLogger::logString("ERROR: confirmSubscription called with no userId.");
			return false;
		}
	}

	function confirmUnsubscription($userId,$key){
		if (!is_null($userId) && $key == md5($userId.ENC_KEY)){
			$dbConn = NewsletterDbConnector::connect();
			$query = "UPDATE ".DB_USERS_TABLE." set deleted = '1', unsubscription_date = NOW() WHERE id_".DB_USERS_TABLE." = ".mysql_escape_string($userId);
			
			$result = @mysql_query($query);
			
			if(mysql_affected_rows() != 1 || !$result){
			//	NmnTinyLogger::writeLog("UnSubscription NOT confirmed for user id = ".$userId);
				return false;
			}else{
			//	NmnTinyLogger::writeLog("UnSubscription confirmed for user id = ".$userId);
				return true;
			}
			NewsletterDbConnector::closeConnection($dbConn);
		}else{
			//NmnTinyLogger::writeLog("ERROR: confirmUnsubscription called with no userId.");
				return false;
		}
	}

	  function checkEmail($email) {
		  if (eregi('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.[a-z]{2,4}$', $email)) {
				  return true;
		  } else {
				  return false;
		  }
	 }


}



?>
Return current item: NmnNewsletter