db = PearDatabase::getInstance();
$this->db->println("Entering MailBox($mailbox)");
$this->mailbox = $mailbox;
$tmp = getMailServerInfo($current_user);
if($this->db->num_rows($tmp) < 1)
$this->enabled = 'false';
else
$this->enabled = 'true';
$this->boxinfo = $this->db->fetch_array($tmp);
$this->login_username=trim($this->boxinfo["mail_username"]);
$this->secretkey=$oencrypt->decrypt(trim($this->boxinfo["mail_password"]));
$this->imapServerAddress=gethostbyname(trim($this->boxinfo["mail_servername"]));
$this->mail_protocol=$this->boxinfo["mail_protocol"];
$this->ssltype=$this->boxinfo["ssltype"];
$this->sslmeth=$this->boxinfo["sslmeth"];
$this->box_refresh=trim($this->boxinfo["box_refresh"]);
$this->mails_per_page=trim($this->boxinfo["mails_per_page"]);
if($this->mails_per_page < 1)
$this->mails_per_page=20;
$this->account_name=$this->boxinfo["account_name"];
$this->display_name=$this->boxinfo["display_name"];
//$this->imapServerAddress=$this->boxinfo["mail_servername"];
$this->db->println("Setting Mailbox Name");
if($this->mailbox != "")
$this->mailbox=$mailbox;
$this->db->println("Opening Mailbox");
if(!$this->mbox && $this->mailbox != "")
$this->getImapMbox();
$this->db->println("Loading mail list");
$pa=$p;
$se=$s;
if($this->mbox){
if ($se != ""){$this->mailList = $this->searchMailList($se,$pa);}
else if ($pa == ""){$this->mailList = $this->customMailList(0);}
else {$this->mailList = $this->customMailList($pa);}
}
$this->db->println("Exiting MailBox($mailbox)");
}
function customMailList($page) {
$info = imap_check($this->mbox);
$numEmails = $info->Nmsgs;
$current_mails = ceil($page*$this->mails_per_page);
$current_mails = $numEmails - $current_mails;
$start =$current_mails-$this->mails_per_page+1;
if ($start<=0) $start=1;
if ($current_mails<=0)$current_mails=0;
$mailOverviews = @imap_fetch_overview($this->mbox, "$start:$current_mails", 0);
$out = array("overview"=>$mailOverviews,"count"=>$numEmails);
return $out;
}
function searchMailList($searchstring,$page) {
$search="";
$searchlist = Array();
$searchlist = imap_search($this->mbox,$searchstring);
if ($searchlist==false) return $out;
$num_searches = count($searchlist);
if ($num_searches < $this->mails_per_page){
$current_mails = $num_searches -1;
}else{
$current_mails = ceil($page*$this->mails_per_page);
$current_mails = $num_searches - $current_mails-1;
}
$start = $current_mails-$this->mails_per_page;
if ($start < 0)$start=0;
$j=0;
for($i=$current_mails; $i >= $start ; $i--){
if($i==$current_mails){
$search=$searchlist[$i];
}else $search=$search.",".$searchlist[$i];
$j++;
}
if ($search!="")
$result = @imap_fetch_overview($this->mbox, "$search",0);
$out = array("overview"=>$result,"count"=>count($searchlist));
return $out;
}
function fullMailList() {
$mailHeaders = @imap_headers($this->mbox);
$numEmails = sizeof($mailHeaders);
$mailOverviews = @imap_fetch_overview($this->mbox, "1:$numEmails", 0);
$out = array("headers"=>$mailHeaders,"overview"=>$mailOverviews,"count"=>$numEmails);
return $out;
}
function isBase64($iVal){
$_tmp=preg_replace("/[^A-Z0-9\+\/\=]/i","",$iVal);
return (strlen($_tmp) % 4 == 0 ) ? "y" : "n";
}
function getImapMbox() {
$this->db->println("Entering getImapMbox()");
$mods = parsePHPModules();
$this->db->println("Parsing PHP Modules");
// first we will try a regular old IMAP connection:
if($this->ssltype == "") {$this->ssltype = "notls";}
if($this->sslmeth == "") {$this->sslmeth = "novalidate-cert";}
if($this->mail_protocol == "pop3")
$port = "110";
else
{
if($mods["imap"]["SSL Support"] == "enabled" && ($this->ssltype == "tls" || $this->ssltype == "ssl"))
$port = "993";
else
$port = "143";
}
$this->db->println("Building connection string");
if(preg_match("/@/",$this->login_username))
{
$mailparts = split("@",$this->login_username);
$user="".trim($mailparts[0])."";
$domain="".trim($mailparts[1])."";
// This section added to fix a bug when connecting as user@domain.com
if($this->readonly == "true")
{
if($mods["imap"]["SSL Support"] == "enabled")
$connectString = "/".$this->ssltype."/".$this->sslmeth."/user={$user}@{$domain}/readonly";
else
$connectString = "/notls/novalidate-cert/user={$user}@{$domain}/readonly";
}
else
{
if($mods["imap"]["SSL Support"] == "enabled")
$connectString = "/".$this->ssltype."/".$this->sslmeth."/user={$user}@{$domain}";
else
$connectString = "/notls/novalidate-cert/user={$user}@{$domain}";
}
}
else
{
if($this->readonly == "true")
{
if($mods["imap"]["SSL Support"] == "enabled")
$connectString = "/".$this->ssltype."/".$this->sslmeth."/readonly";
else
$connectString = "/notls/novalidate-cert/readonly";
}
else
{
if($mods["imap"]["SSL Support"] == "enabled")
$connectString = "/".$this->ssltype."/".$this->sslmeth;
else
$connectString = "/notls/novalidate-cert";
}
}
//$connectString = "{".$this->imapServerAddress."/".$this->mail_protocol.":".$port.$connectString."}".$this->mailbox;
$connectString = "{".$this->imapServerAddress.":".$port."/".$this->mail_protocol.$connectString."}".$this->mailbox;
//Reference - http://forums.vtiger.com/viewtopic.php?p=33478#33478 - which has no tls or validate-cert
$connectString1 = "{".$this->imapServerAddress."/".$this->mail_protocol.":".$port."}".$this->mailbox;
$this->db->println("Done Building Connection String.. $connectString Connecting to box");
//checking the imap support in php
if(!function_exists('imap_open'))
{
echo "".$mod_strings['LBL_ENABLE_IMAP_SUPPORT']."";
exit();
}
if(!$this->mbox = @imap_open($connectString, $this->login_username, $this->secretkey))
{
//try second string which has no tls or validate-cert
if(!$this->mbox = @imap_open($connectString1, $this->login_username, $this->secretkey))
{
global $current_user,$mod_strings;
$this->db->println("CONNECTION ERROR - Could not be connected to the server using imap_open function through the connection strings $connectString and $connectString1");
echo "
".$mod_strings['LBL_MAIL_CONNECT_ERROR']." ".$mod_strings['LBL_HERE'].". ".$mod_strings['LBL_PLEASE']." ".$mod_strings['LBL_CLICK_HERE']."".$mod_strings['LBL_GOTO_EMAILS_MODULE']." ";
exit;
}
}
$this->db->println("Done connecting to box");
}
} // END CLASS
function parsePHPModules() {
ob_start();
phpinfo(INFO_MODULES);
$s = ob_get_contents();
ob_end_clean();
$s = strip_tags($s,'