| Current Path : /var/www/cesa.co.za/crons/ |
| Current File : /var/www/cesa.co.za/crons/class.email-reader.php |
<?php
class email_reader_class
{
var $pop_server;
var $account;
var $password;
var $port=110;
var $email_list=array();
var $email_count=0;
var $email_link;
var $error_free=TRUE;
var $connected=FALSE;
var $account_vars_set;
var $error="No errors";
var $last_connected=FALSE;
var $iterator;
var $imap=TRUE;
var $imap_checked=FALSE;
var $imap_link;
var $deleted=FALSE;
function check_imap()
{
if(!function_exists('imap_open'))
{
$this->imap=FALSE;
require_once('inc_imap.php');
// require_once('inc_mimedecode.php');
}
$imap_check=TRUE;
}
function empty_mailbox($qty=null)
{
if( (!$this->deleted) && ($this->imap) )
{
$this->deleted=TRUE;
$count=$this->get_count();
if(!is_null($qty)) $qty=$count;
$qty=min($qty,$count);
if($qty>0)
{
for($loop=1;$loop<=$qty;$loop++)
{
imap_delete($this->email_link, $loop);
}
}
}
}
function set_account($pop_server, $account, $password, $port=110)
{
$this->set_server($pop_server);
$this->set_access($account, $password);
$this->set_port($port);
}
function set_server($pop_server)
{
$this->pop_server=$pop_server;
$this->account_vars_set['pop_server']=TRUE;
}
function set_access($account, $password)
{
$this->account=$account;
$this->password=$password;
$this->account_vars_set['account']=TRUE;
$this->account_vars_set['password']=TRUE;
}
function set_port($port=110)
{
$this->port=$port;
$this->account_vars_set['port']=TRUE;
}
function check_if_vars_set()
{
if($this->account_vars_set['account'] && $this->account_vars_set['password'] && $this->account_vars_set['pop_server'])
{
return TRUE;
}
$this->error="Email account variables have not been set";
$this->error_free=FALSE;
return FALSE;
}
function connect()
{
if(!$this->imap_checked) $this->check_imap();
if($this->check_if_vars_set())
{
$this->connected=FALSE;
$this->error_occurred=FALSE;
if($this->imap)
{
$this->email_link=imap_open("{".$this->pop_server.":".$this->port."/pop3/notls}", $this->account, $this->password) or $this->error_occurred=TRUE;
if($this->error_occurred)
{
$this->error="Could not connect to email server";
$this->error_free=FALSE;
return FALSE;
}
$this->email_count=imap_num_msg($this->email_link);
}
else
{
$this->my_imap_open($this->account, $this->password, $this->pop_server) or $this->error_occurred=TRUE;
if($this->error_occurred)
{
$this->error="Could not connect to email server";
$this->error_free=FALSE;
return FALSE;
}
$this->email_count=$this->my_imap_num_msg();
}
$this->email_list=range(0, $this->email_count); // need to start from 0 as indexes dont match
$this->last_connected=time();
$this->connected=TRUE;
$this->iterator=1;
}
else
{
$this->error="Variables are not set for connecting to the email server";
$this->connected=FALSE;
$this->error_free=FALSE;
return FALSE;
}
return TRUE;
}
function disconnect()
{
if($this->deleted)
{
if($this->imap)
{
imap_expunge($this->email_link);
}
else
{
//my_imap_expunge($this->email_link);
}
}
if($this->connected)
{
$this->connected=FALSE;
$this->error_occurred=FALSE;
if($this->imap)
{
@imap_close($this->email_link) or $this->error_occurred=TRUE;
}
else
{
$this->my_imap_close() or $this->error_occurred=TRUE;
}
if($this->error_occurred)
{
$this->error="Could not close connection to email server";
$this->error_free=FALSE;
return FALSE;
}
}
return TRUE;
}
function get_count()
{
if(!$this->connected)
{
$this->error="Not connected to email server";
$this->error_free=FALSE;
return FALSE;
}
return $this->email_count;
}
function get_error()
{
return $this->error;
}
function get_connection_date()
{
return $this->last_connected;
}
function get_body()
{
if(!$this->connected)
{
$this->error="Not connected to email server";
$this->error_free=FALSE;
return FALSE;
}
if($this->imap)
{
//return imap_body($this->email_link, $this->iterator);
return imap_fetchbody($this->email_link, $this->iterator, 1);
}
else
{
return $this->my_imap_body($this->iterator);
}
}
function get_structure()
{
if(!$this->connected)
{
$this->error="Not connected to email server";
$this->error_free=FALSE;
return FALSE;
}
if($this->imap)
{
return imap_fetchstructure($this->email_link, $this->iterator);
}
else
{
return $this->my_imap_fetchstructure($this->iterator);
}
}
function get_attachments()
{
if(!$this->connected)
{
$this->error="Not connected to email server";
$this->error_free=FALSE;
return FALSE;
}
$struct=imap_fetchstructure($this->email_link, $this->iterator);
$parts=$struct->parts;
$attachment=array();
if( (is_array($parts)) && (sizeof($parts)>0) )
{
while(list($key,$value)=each($parts))
{
$att_obj=$value;
print_r($att_obj);
print_r($att_obj->parameters);
if ((strtoupper($att_obj->disposition) == "ATTACHMENT") || (strtoupper($att_obj->type) == "5"))
{
$attachfilename = "";
if (isset($att_obj->parameters[0]->value) && !empty($att_obj->parameters[0]->value)) {
$attachfilename = $att_obj->parameters[0]->value;
} else {
if (isset($att_obj->id)) {
$attachfilename = trim($att_obj->id,"<>");
} else {
$attachfilename = "image.".$att_obj->subtype;
}
}
$attach=array("filename"=>$attachfilename,
"filedata"=>imap_fetchbody($this->email_link, $this->iterator, $key+1));
if($att_obj->encoding==3) $attach['filedata']=base64_decode($attach['filedata']);
if($att_obj->encoding==4) $attach['filedata']=quoted_printable_decode($attach['filedata']);
$attach['id'] = trim($att_obj->id,"<>");
$attachment[]=$attach;
}
}
}
return $attachment;
}
function get_header_array()
{
// returns header as an array NOT an object
return $this->object_to_array(get_header());
}
function get_header()
{
if(!$this->connected)
{
$this->error="Not connected to email server";
$this->error_free=FALSE;
return FALSE;
}
if($this->imap)
{
return $this->object_to_array(imap_headerinfo($this->email_link, $this->iterator));
}
else
{
return $this->my_imap_headerinfo($this->iterator);
}
// structure
// message_id unique id for email
// date date user sent
// subject
// toaddress
// to array of people sent to
// eg to[0][personal] => Matthew Waygood
// to[0][mailbox] => mwaygood
// to[0][host] => aflonline.co.uk
// fromaddress
// from array of people sent from
// eg from[0][personal] => Matthew Waygood
// from[0][mailbox] => mwaygood
// from[0][host] => aflonline.co.uk
// reply_toaddress
// reply_to array of people to reply to
// eg reply_to[0][personal] => Matthew Waygood
// reply_to[0][mailbox] => mwaygood
// reply_to[0][host] => aflonline.co.uk
// senderaddress
// sender array of senders
// eg sender[0][personal] => Matthew Waygood
// sender[0][mailbox] => mwaygood
// sender[0][host] => aflonline.co.uk
// Recent
// Recent - 'R' if recent and seen,
// 'N' if recent and not seen,
// ' ' if not recent
// Unseen
// Unseen - 'U' if not seen AND not recent,
// ' ' if seen OR not seen and recent
// Flagged
// Flagged - 'F' if flagged,
// ' ' if not flagged
// Answered
// Answered -'A' if answered,
// ' ' if unanswered
// Deleted
// Deleted - 'D' if deleted,
// ' ' if not deleted
// Draft
// Draft - 'X' if draft,
// ' ' if not draft
// Msgno same as $this->iterator
// MailDate
// Size
// udate date it hit the SMTP server
// checking for unread emails you must use - Unseen == 'U' || Recent == 'N'
}
function next_email()
{
if(!$this->connected)
{
$this->error="Not connected to email server";
$this->error_free=FALSE;
return FALSE;
}
$this->iterator++;
while( (!isset($this->email_list[$this->iterator])) && ($this->iterator<=$this->email_count) )
{
$this->iterator++;
}
if($this->iterator>$this->email_count) // beyond end of list
{
$this->error="Cannot go beyond last email";
$this->error_free=FALSE;
return FALSE;
}
return TRUE;
}
function previous_email()
{
if(!$this->connected)
{
$this->error="Not connected to email server";
$this->error_free=FALSE;
return FALSE;
}
$this->iterator--;
while( (!isset($this->email_list[$this->iterator])) && ($this->iterator>0) )
{
$this->iterator--;
}
if($this->iterator<=1) // beyond beginning of list
{
$this->error="Cannot go beyond first email";
$this->error_free=FALSE;
return FALSE;
}
return TRUE;
}
function delete_current_email()
{
if(!$this->connected)
{
$this->error="Not connected to email server";
$this->error_free=FALSE;
return FALSE;
}
if(!isset($this->email_list[$this->iterator]))
{
$this->error="Email no longer exists";
$this->error_free=FALSE;
return FALSE;
}
if($this->imap)
{
imap_delete($this->email_link, $this->iterator);
$this->deleted=TRUE;
// imap_expunge($this->email_link);
}
else
{
$this->my_imap_delete($this->iterator);
$this->deleted=TRUE;
//my_imap_expunge($this->email_link);
}
unset($this->email_list[$this->iterator]);
return $this->next_email();
// if(!$this->next_email())
// {
// //return $this->previous_email();
// }
// return TRUE;
}
function object_to_array($the_object)
{
$the_array=array();
if(!is_scalar($the_object))
{
foreach($the_object as $id => $object)
{
if(is_scalar($object))
{
$the_array[$id]=$object;
}
else
{
$the_array[$id]=$this->object_to_array($object);
}
}
return $the_array;
}
else
{
return $the_object;
}
}
///
/// imap emulation for servers without imap installed
///
// $this->email_link=my_imap_open($this->account, $this->password, $this->pop_server, $this->port);
function my_imap_open($account, $password, $server, $port=NULL)
{
$this->my_imap=new IMAPMAIL();
if($this->my_imap->open($server, $port))
{
$this->my_imap->login($account, $password);
}
else
{
$this->error=$this->my_imap->error;
$this->error_free=FALSE;
return FALSE;
}
return $this->my_imap->open_mailbox("INBOX");
}
// imap_num_msg($this->email_link);
function my_imap_num_msg()
{
return $this->my_imap->get_msglist();
}
// imap_headers($this->email_link);
function my_imap_headers()
{
$output=array();
$qty=$this->my_imap->get_msglist();
$loop=1;
while($loop<=$qty)
{
$output[]=$this->my_imap_headerinfo($loop);
$loop++;
}
return $output;
}
// imap_header($this->email_link, $this->iterator)
function my_get_header($msg_num=1)
{
return $this->my_imap->get_message_header($msg_num);
}
// imap_close($this->email_link);
function my_imap_close($msg_num=1)
{
return $this->my_imap->close();
}
// imap_body($this->email_link, $this->iterator);
function my_imap_body($msg_num=1)
{
return $this->my_imap->get_message_body($msg_num);
}
// imap_delete($this->email_link, $this->iterator);
function my_imap_delete($msg_num=1)
{
return $this->delete_message($msg_num);
}
// my_imap_expunge($this->email_link);
// no need for this, as messages are eresed on closing connection
// imap_headerinfo($this->email_link, $this->iterator);
function my_imap_headerinfo($msg_num=1)
{
return $this->my_parse_header($this->my_imap->get_message_header($msg_num), $msg_num);
}
function my_parse_header($header, $msg_num)
{
$header=str_replace("\r", "", $header);
$header=str_replace(",\n", ", ", $header);
$header=str_replace("\t", "", $header);
$output=array();
$formatted_header=array();
$split=split("\n",$header);
if(sizeof($split)>0)
{
foreach($split as $text)
{
$split_again=split(":",$text,2);
$output[strtolower($split_again[0])]=trim($split_again[1]);
}
if(isset($output['date'])) $formatted_header['date']=$output['date'];
if(isset($output['date'])) $formatted_header['Date']=$output['date'];
if(isset($output['subject'])) $formatted_header['subject']=$output['subject'];
if(isset($output['subject'])) $formatted_header['Subject']=$output['subject'];
if(isset($output['message-id'])) $formatted_header['message_id']=$output['message-id'];
if(isset($output['thread-index'])) $formatted_header['thread_index']=$output['thread-index'];
if(isset($output['to']))
{
$formatted_header['toaddress']=$output['to'];
$formatted_header['to']=$this->email_divide($formatted_header['toaddress']);
$formatted_header['toaddress']=$this->cleanup_email($formatted_header['toaddress']);
}
if(isset($output['from']))
{
$formatted_header['fromaddress']=$output['from'];
$formatted_header['from']=$this->email_divide($formatted_header['fromaddress']);
$formatted_header['fromaddress']=$this->cleanup_email($formatted_header['fromaddress']);
}
if(isset($output['cc']))
{
$formatted_header['ccaddress']=$output['cc'];
$formatted_header['cc']=$this->email_divide($formatted_header['ccaddress']);
$formatted_header['ccaddress']=$this->cleanup_email($formatted_header['ccaddress']);
}
if(isset($output['reply-to']))
{
$formatted_header['reply_toaddress']=$output['reply-to'];
$formatted_header['reply_to']=$this->email_divide($formatted_header['reply_toaddress']);
$formatted_header['reply_toaddress']=$this->cleanup_email($formatted_header['reply_toaddress']);
}
if(isset($output['from']))
{
$formatted_header['senderaddress']=$output['from'];
$formatted_header['sender']=$this->email_divide($formatted_header['senderaddress']);
$formatted_header['senderaddress']=$this->cleanup_email($formatted_header['senderaddress']);
}
// most of below is fake, and may be changed some time in the future.
$formatted_header['Recent']='N';
$formatted_header['Unseen']=' ';
$formatted_header['Flagged']=' ';
$formatted_header['Answered']=' ';
$formatted_header['Deleted']=' ';
$formatted_header['Draft']=' ';
$formatted_header['Msgno']=$msg_num;
$formatted_header['MailDate']=trim(eregi_replace("^([a-z]+),", "", $output['date']));
$formatted_header['MailDate']=eregi_replace("^([0-9]+) ([a-z]+) ([0-9]+)", "\\1-\\2-\\3", $formatted_header['MailDate']);
$formatted_header['Size']=1;
$formatted_header['udate']=1;
}
return $formatted_header;
}
function email_divide($text)
{
$output=array();
$split=split(",",$text);
foreach($split as $text)
{
$text=trim($text);
$element=array();
if(!empty($text))
{
if(eregi("^([^<]+)?<([^>@]+)@([^>@]+)>$", $text, $regs))
{
if(!empty($regs[1])) $element['personal']=str_replace('"','',trim($regs[1]));
$element['mailbox']=trim($regs[2]);
$element['host']=trim($regs[3]);
}
else
{
if(eregi("^([^<>@]+)@([^<>@]+)$", $text, $regs))
{
//$element['personal']="";
$element['mailbox']=trim($regs[1]);
$element['host']=trim($regs[2]);
}
}
$output[]=$element;
}
}
return $output;
}
function cleanup_email($text)
{
$split=explode(",", $text);
foreach($split as $key => $element)
{
$element=trim($element);
if(ereg("\"", $element))
{
if(ereg("<(.+)>", $element, $regs))
{
$email=$regs[1];
if(!ereg('"'.$email.'"', $element))
{
$element=str_replace('"', '', $element);
}
}
}
else
{
$element=str_replace('<', '', $element);
$element=str_replace('>', '', $element);
}
$split[$key]=$element;
}
$text=implode(", ", $split);
return $text;
}
}
?>