Location: PHPKode > scripts > txtSQL MailBox > txtsql-mailbox/mailbox.inc
<?
 /*************************************************************************/
 # txtSQL MailBox 0.1 by Nexami Engeo (http://www.n-e-x.co.uk)     #
 # txtSQL MailBox is based on phpMailBox 1.1     #
 # Lasy Modified : 5th April, 2005  22:50 GMT     #
 # txtSQL MailBox 0.1 is Copyright (C) by John Urquhart Ferguson     #
 # All previous licensing applies     #
 #
 # More information on the txtSQL project can be found at: http://txtsql.sourceforge.net)
 #
 #  phpMailbox 1.1   by Sivaprasad R.L (http://netlogger.net/phpmailbox)  #
 #                                                                        #
 #          --  A php pop3 client --              			  #
 #                                                                        #
 # 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, write to the Free Software            #
 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              #
 #                                                                        #
 #             phpMailBox 1.1 is Copyright (C) by Sivaprasad R.L                            #
 #                                                                        #
/*************************************************************************/
parse_str(base64_decode($pop3_cookie));
require("class.inc");


global $bgcolor1, $bgcolor2, $bgcolor3, $domain, $uid;

session_start();

class MailBox {

    var $accounts, $userid, $accountarray, $isValidAccount, $serverdetails;

    var $currserver, $currport, $currusername, $currpassword, $currnumshow, $connectstatus;

    var $pop3;
    
    var $sql;

    function MailBox($sql = "null") {
		$this->isValidAccount = false;
		$this->connectstatus = false;
        $this->accounts = 0;
        $this->userid  = 0;
		$this->pop3 = 0;
		$this->sql = $sql;
    }

    function showerror($error) {
		echo "<br><br><center>";
		echo "<table border='0' width='400' cellpadding='0' cellspacing='0'>";
		echo "<tr>";
    	echo "<th width='100%' class=\"bgrowcolor3\">";
    	echo "<b class=\"font2\"><b>Error:</b></b></th></tr><tr>\n";
		echo "<td width='100%' class=\"bgrowcolor1\" align='center'><b class=\"font1\">\n";
		echo $error."</b>";
		echo "</td>";
		echo "</tr>";
		echo "</table>";
		echo "</center><br><br>";

		if($this->pop3)
           $this->pop3->CloseConnection();

		exit();
    }

    function getAccountIds() {
        if(!$this->userid)
            $this->getUID();

		//Old MySQL code
        /*$query = "select * from popsettings where uid = '$this->userid'";
	
		if($res = mysql_query($query)) {
			if(mysql_num_rows($res) > 0) {
				$count = 0;
				while($row = mysql_fetch_array($res)) {
					$serverids[$count] = $row[id];
					$count++;
				}
				return $serverids; 
			} else {
				return false;
			}
		}*/
	
		//New txtSQL code
		$query = 'SELECT * FROM popsettings WHERE uid = "'.$this->userid.'"';
		$res = $this->sql->query($query);
		if( sizeof($res) > 0 ) {
			$count = 0;
			foreach ( $res as $key => $row )
			{
				$serverids[$count] = $row["id"];
				$count++;
			}
			return $serverids;
		} else {
			return FALSE;
		}
    }


    function getAccountNames() {
        if(!$this->userid)
            $this->getUID();

		//Old MySQL code
        /*$query = "select * from popsettings where uid = '$this->userid'";
	
		if($res = mysql_query($query)) {
			if(mysql_num_rows($res) > 0) {
				while($row = mysql_fetch_array($res)) {
					$id = $row[id];
					$servernames[$id] = $row[account];
				}
				return $servernames; 
			} else {
				return false;
			}
		}*/
	
		//New txtSQL code
		$query = 'SELECT * FROM popsettings WHERE uid = "'.$this->userid.'"';
		$res = $this->sql->query($query);
		if( sizeof($res) > 0 ) {
			foreach ( $res as $key => $row )
			{
				$id = $row["id"];
				$servernames[$id] = $row["account"];
			}
			return $servernames;
		} else {
			return FALSE;
		}
    }

    function getUID() {

		global $uid;
 
		if($uid != "" && isset($uid)) {  
			$this->userid = $uid;
			return true;
		} else {
			$this->showerror("Unable to get User ID!!");
		}
    }

    function getUserPOPAccounts() {
	
        if(!$this->userid)
            $this->getUID();

		//Old MySQL code
		/*$query = "select * from popsettings where uid = '$this->userid'";

		if($this->accounts = mysql_query($query)) {
			return true;
		} else {
			$this->showerror("Unable to get POP3 Settings for user!");
		}*/
		
		//New txtSQL code
		$res = $this->sql->query('SELECT * FROM popsettings WHERE uid = '.$this->userid.'');

		if( sizeof($res) ) {
			$this->accounts = $res;
			return true;
		} else {
			//$this->showerror("Unable to get POP3 Settings for user!");
        }
    }	

    function Initialize() {

        $this->getUserPOPAccounts(); 
    
		// This code doesn't seem to do anything, so I've removed it rather
		// than convert it to txtSQL. If I find a use for it later, I'll put it
		// back in and do the conversion. - nex : 5th April, 2005  22:40 GMT
		//
        /*if($defaultpopserver != "") {
			if(mysql_num_rows($this->accounts) < 1) {
				srand((double)microtime()*1000000);
				$uniq_id1 = md5(rand(0,9999999));
	
				$q1 = "select * from $db_authtable where $db_userid = '$this->userid'";
				$r1 = mysql_query($q1); 
	
				if(mysql_num_rows($r1)) {
					$row = mysql_fetch_array($r1);
					$siteuname =  $row[$db_usernamefield];
					$spwd = $row[$db_passwordfield];

					$rc4 = new rc4crypt();
					$sitepwd = $rc4->endecrypt($siteuname,$spwd,"en");
	
					$fromemail = $siteuname . "@". $domain;
					$fromname = $siteuname;
	
					$q2 = "insert into popsettings (id, uid, account, popserver, port, uname,
						   passwd,numshow,deletefromserver,fromemail,fromname) values
						   ('$uniq_id1','$this->userid','$singleaccountname','$defaultpopserver', 110,
						   '$siteuname','$sitepwd',10,'Y','$fromemail','$fromname')";
					$r2 = mysql_query($q2);	
					$this->getUserPOPAccounts(); 
				}
			}
		}*/
    }


    function getNumAccounts() {
	   //Old MySQL code
       /*if(!$this->accounts) 
			getUserPOPAccounts(); 

		if($this->accounts) {
			$numrows = mysql_num_rows($this->accounts);
			return $numrows;
		} 
		return 0;*/
	   
		//New txtSQL code
		if( sizeof($this->accounts) < 1 ) 
	    getUserPOPAccounts(); 

        if( sizeof($this->accounts) > 0 ) {
			$numrows = sizeof($this->accounts);
            return $numrows;
        } 
        return 0;
    }

    function fetchAccount($id) {

		if(!$this->isConnected($id) && !$this->OpenConnection($id)) 
          $this->showerror("Unable to Open Connection to pop server! Try again after some time");
 
        $count = 0;
	   
	    //Old MySQL code
        /*$query = "Select * from popsettings where uid = '$this->userid' and id='$id'";
		$res = mysql_query($query);
   	
		while($row = mysql_fetch_array($res)) {
			$serverid = $row[id];

			$this->accountarray[$count]["id"] = $serverid;
			$this->accountarray[$serverid]["server"] = $row[popserver];

			$rc4 = new rc4crypt();
			$password = $rc4->endecrypt($row[uname],$row[passwd],"de");

			$this->accountarray[$serverid]["account"] = $row[account];
			$this->accountarray[$serverid]["username"] = $row[uname];
			$this->accountarray[$serverid]["password"] = $password;
			$this->accountarray[$serverid]["port"] = $row[port];
		
			$server = $row[popserver];
			$username = $row[uname];
			$stats = $this->pop3->Stats();

			$this->accountarray[$serverid]["count"] = $stats["message"];
			$this->accountarray[$serverid]["size"] = round($stats["size"]/1024) . " Kb";
			$count++;
		}*/

	   	//New MySQL code
		$query = 'SELECT * FROM popsettings WHERE uid = "'.$this->userid.'" and id = '.$id.'';
		$res = $this->sql->query($query);
   	
		foreach ( $res as $key => $row ) {
			$serverid = $row["id"];

			$this->accountarray[$count]["id"] = $serverid;
			$this->accountarray[$serverid]["server"] = $row["popserver"];

			$rc4 = new rc4crypt();
			$password = $rc4->endecrypt($row["uname"],$row["passwd"],"de");

			$this->accountarray[$serverid]["account"] = $row["account"];
			$this->accountarray[$serverid]["username"] = $row["uname"];
			$this->accountarray[$serverid]["password"] = $password;
			$this->accountarray[$serverid]["port"] = $row["port"];
		
			$server = $row["popserver"];
			$username = $row["uname"];
			$stats = $this->pop3->Stats();

			$this->accountarray[$serverid]["count"] = $stats["message"];
			$this->accountarray[$serverid]["size"] = round($stats["size"]/1024) . " Kb";
			$count++;
		}
	   
	   
		if($count > 0)
           $this->isValidAccount[$id] = true;
	}

 
	function getAccountServer($id) {
		if(!$this->isValidAccount[$id]) 
			$this->fetchAccount($id);   

		return $this->accountarray[$id]["server"];
	}  


	function getAccountUsername($id) {
		if(!$this->isValidAccount[$id]) 
			$this->fetchAccount($id);   

		return $this->accountarray[$id]["username"];
	}


	function getAccountPassword($id) {
		if(!$this->isValidAccount[$id]) 
			$this->fetchAccount($id);   

		return $this->accountarray[$id]["password"];
	}


	function getAccountName($id) {
		if(!$this->isValidAccount[$id]) 
			$this->fetchAccount($id);   

		return $this->accountarray[$id]["account"];
	}

	function getNumMails($id) {
		if(!$this->isValidAccount[$id]) 
			$this->fetchAccount($id);   

		return $this->accountarray[$id]["count"];
	}

	function getAccountSize($id) {
		if(!$this->isValidAccount[$id]) 
			$this->fetchAccount($id);   

		return $this->accountarray[$id]["size"];
	}

	function getServerDetails($id) {

		if(!$this->userid) 
			$this->getUID(); 
	
		if(!isset($id)) {
			include "mailfooter.php";
			$this->showerror("Error: Invalid Parameter<br>");
		}

		//Old MySQL code
		/*$query = "Select * from popsettings where id = '$id'";

		if(($res = mysql_query($query)) && (mysql_num_rows($res) > 0)) {
			$row = mysql_fetch_array($res);
			$userrowid = $row[uid];

			if($this->userid != $userrowid) {
				$this->showerror("<center><h2>Error: Permission denied</center>");
			}
			$this->currserver = $row[popserver];
			$this->currport = $row[port];
			$this->currusername = $row[uname];
			$rc4 = new rc4crypt();
			$this->currpassword = $rc4->endecrypt($this->currusername,$row[passwd],"de");
			$this->currnumshow = $row[numshow];
			$this->serverdetails[$id] = true;
		} else {
			$this->showerror("Error: POP Server not set properly");
		}*/
		
		
		//New txtSQL code
		$res = $this->sql->query('SELECT * FROM popsettings WHERE id = '.$id.'');

		if( sizeof($res) > 0 ) {
            $row = $res[0];
            $userrowid = $row["uid"];

            if($this->userid != $userrowid) {
                $this->showerror("<center><h2>Error: Permission denied</center>");
            }
            $this->currserver = $row["popserver"];
            $this->currport = $row["port"];
            $this->currusername = $row["uname"];
            $rc4 = new rc4crypt();
            $this->currpassword = $rc4->endecrypt($this->currusername,$row["passwd"],"de");
            $this->currnumshow = $row["numshow"];
			$this->serverdetails[$id] = true;
		} else {
            $this->showerror("Error: POP Server not set properly");
		}
	}

	function getNumShow($id) {
		if(!$this->serverdetails[$id]) 
			$this->getServerDetails($id);   

			return $this->currnumshow;
	}

	function OpenConnection($id) {

		if($this->connectstatus[$id]) 
			return true;
    
		if(!$this->serverDetails[$id])	
			$this->getServerDetails($id);
 
		if($this->pop3 = new POP3($this->currserver,$this->currusername,$this->currpassword)) {
			if($this->pop3->Open()) {
				$this->connectstatus[$id] = true;
				return true;
			}
		}
		return false;
	}

	function CloseConnection($id) {
		if($this->isConnected($id)) {
			$this->pop3->Close();
			$this->connectstatus[$id] = false;
		}
	}   

	function getMessageHeader($id,$i) {
		if(!$this->isConnected($id)) {
			echo "Connecting again..<br>";
			if(!$this->OpenConnection($id)) 
			$this->showerror("Unable to Open Connection to pop server! Try again after some time");
		}
 	      
		$list = $this->pop3->ListMessage($i);
		return $list;	
	} 


	function isConnected($id) {
		return $this->connectstatus[$id];
	}

	function DeleteMessage($id, $msgid) {
		if(!$this->isConnected($id)) 
			$this->OpenConnection($id);
		if(is_array($msgid)) {
			foreach($msgid as $mid) {
				if($mid != "")
				$this->pop3->DeleteMessage($mid);	
			}
		} else {
			$this->pop3->DeleteMessage($msgid);
		}
	}
}

?>
Return current item: txtSQL MailBox