Support Incident Tracker GIT4.x
mailbox.class.php
Go to the documentation of this file.
00001 <?php
00002 // mailbox.class.php - Incoming POP/IMAP mailbox class
00003 //
00004 // SiT (Support Incident Tracker) - Support call tracking system
00005 // Copyright (C) 2010-2011 The Support Incident Tracker Project
00006 // Copyright (C) 2000-2009 Salford Software Ltd. and Contributors
00007 //
00008 // This software may be used and distributed according to the terms
00009 // of the GNU General Public License, incorporated herein by reference.
00010 
00011 // Prevent script from being run directly (ie. it must always be included
00012 if (realpath(__FILE__) == realpath($_SERVER['SCRIPT_FILENAME']))
00013 {
00014     exit;
00015 }
00016 
00017 
00018 class Mailbox
00019 {
00020     var $username;
00021     var $password;
00022     var $server;
00023     var $email;
00024     var $mailbox;
00025     var $servertype;
00026     //Append
00027     function Mailbox($username, $password, $email, $server =
00028                           'localhost', $servertype = 'pop', $port = '',
00029                           $options = '')
00030     {
00031         global $CONFIG;
00032         if (!empty($CONFIG['email_incoming_folder']))
00033         {
00034             $folder = $CONFIG['email_incoming_folder'];
00035         }
00036         else
00037         {
00038             $folder = 'INBOX';
00039         }
00040         if ($servertype == 'imap')
00041         {
00042             if (empty($port))
00043             {
00044                 $port = '143';
00045             }
00046             $connectionString = "{{$server}:{$port}/imap{$options}".
00047                                  "/user={$username}}$folder";
00048         }
00049         else
00050         {
00051             if (empty($port))
00052             {
00053                 $port = '110';
00054             }
00055             $connectionString = "{{$server}:{$port}/pop3{$options}".
00056                                 "/user={$username}}$folder";
00057         }
00058         $this->username = $username;
00059         $this->password = $password;
00060         $this->server = $connectionString;
00061         $this->email = $email;
00062         $this->servertype = $servertype;
00063     }
00064 
00065     function connect()
00066     {
00067         $this->mailbox = imap_open($this->server, $this->username,
00068                                    $this->password, CL_EXPUNGE);
00069         if ($this->mailbox)
00070         {
00071             return TRUE;
00072         }
00073         else
00074         {
00075             debug_log(imap_last_error());
00076             return FALSE;
00077         }
00078     }
00079 
00080     function getNumUnreadEmails()
00081     {
00082         $headers = imap_headers($this->mailbox);
00083         return count($headers);
00084     }
00085 
00086     function getAttachments($id, $path)
00087     {
00088         $parts = imap_fetchstructure($this->mailbox, $id);
00089         $attachments = array();
00090 
00091         //FIXME if we do an is_array() here it breaks howver if we don't
00092         //we get foreach errors
00093         foreach($parts->parts as $key => $value)
00094         {
00095             $encoding = $parts->parts[$key]->encoding;
00096             if($parts->parts[$key]->ifdparameters)
00097             {
00098                 $filename = $parts->parts[$key]->dparameters[0]->value;
00099                 $message = imap_fetchbody($this->mailbox, $id, $key + 1);
00100 
00101                 switch($encoding)
00102                 {
00103                     case 0:
00104                         $message = imap_8bit($message);
00105                     case 1:
00106                         $message = imap_8bit ($message);
00107                     case 2:
00108                         $message = imap_binary ($message);
00109                     case 3:
00110                         $message = imap_base64 ($message);
00111                     case 4:
00112                         $message = quoted_printable_decode($message);
00113                     case 5:
00114                     default:
00115                         $message = $message;
00116                 }
00117 
00118                 $fp = fopen($path.$filename,"w");
00119                 fwrite($fp, $message);
00120                 fclose($fp);
00121                 $attachments[] = $filename;
00122             }
00123         }
00124         return $attachments;
00125 
00126     }
00127 
00128     function messageBody($id)
00129     {
00130         global $CONFIG;
00131         if ($CONFIG['debug']) debug_log("Retrieving message {$id} from server\n");
00132         if (imap_body($this->mailbox, $id))
00133         {
00134             return imap_body($this->mailbox, $id);
00135         }
00136         else
00137         {
00138             debug_log("Died on message {$id} with: ".imap_last_error());
00139         }
00140     }
00141 
00142     function getMessageHeader($id)
00143     {
00144         return imap_fetchheader($this->mailbox, $id);
00145     }
00146 
00147     function deleteEmail($id)
00148     {
00149         imap_delete($this->mailbox, $id) OR debug_log(imap_last_error());
00150     }
00151 
00152     function iso8859Decode($text)
00153     {
00154         return imap_utf7_encode($text);
00155     }
00156 
00157     function archiveEmail($id)
00158     {
00159         global $CONFIG;
00160         if ($CONFIG['debug']) debug_log("Moving mail to {$CONFIG['email_archive_folder']} folder");
00161         return imap_mail_move($this->mailbox, $id, $CONFIG['email_archive_folder']) OR debug_log(imap_last_error());
00162     }
00163 }
00164 ?>