imap : allow the imap class to be extendend -> The constructor doesn't do anything except calling 'connect' method. The connect can be used by child class, if the constructor of the child is override the imap constructor

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2371 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2015-10-22 12:00:09 +00:00
parent 223c36251a
commit 34322f8540

View File

@@ -8,7 +8,7 @@
class imap class imap
{ {
/** The mailbox string */ /** The mailbox string */
private $mailbox; private $mailbox = null;
/** The current folder in UTF-8 */ /** The current folder in UTF-8 */
private $curDir = "INBOX"; private $curDir = "INBOX";
/** The auto expunge feature, after deleting/moving an email */ /** The auto expunge feature, after deleting/moving an email */
@@ -19,10 +19,21 @@ class imap
private static $instance = array (); private static $instance = array ();
/** The constructor /** The constructor
The IMAP standard port is 143, but tunnelled is 993 */ The IMAP standard port is 143, but SSL tunnelled is 993 */
public function __construct ($imapserver = "localhost", $imapport = 143, public function __construct ($imapserver = "localhost", $imapport = 143,
$username = null, $password = null, $username = null, $password = null,
$imapssl = false, $imapcertvalidate = true) $imapssl = false, $imapcertvalidate = true)
{
$this->connect ($imapserver, $imapport, $username, $password, $imapssl,
$imapcertvalidate);
}
/** The connect can be used when extends the imap class. The constructor can
be override by the child class.
The IMAP standard port is 143, but SSL tunnelled is 993 */
public function connect ($imapserver = "localhost", $imapport = 143,
$username = null, $password = null,
$imapssl = false, $imapcertvalidate = true)
{ {
if (! function_exists ("imap_open")) if (! function_exists ("imap_open"))
throw new Exception ("PHP don't support IMAP. Please add it !", 500); throw new Exception ("PHP don't support IMAP. Please add it !", 500);
@@ -65,6 +76,8 @@ class imap
The names of folders are converted in UTF-8 */ The names of folders are converted in UTF-8 */
public function foldersList () public function foldersList ()
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$list = array_keys ($this->foldersListWithAttr ()); $list = array_keys ($this->foldersListWithAttr ());
natsort ($list); natsort ($list);
return $list; return $list;
@@ -74,6 +87,8 @@ class imap
attributes allow to see if there is new mails in folders */ attributes allow to see if there is new mails in folders */
public function foldersListWithAttr () public function foldersListWithAttr ()
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$list = imap_getmailboxes (self::$instance[$this->mailbox], $this->mailbox, $list = imap_getmailboxes (self::$instance[$this->mailbox], $this->mailbox,
"*"); "*");
$res = array (); $res = array ();
@@ -92,6 +107,8 @@ class imap
The folder name must be in UTF-8. The folder must be absolute */ The folder name must be in UTF-8. The folder must be absolute */
public function changeFolder ($folder) public function changeFolder ($folder)
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
if (! in_array ($folder, $this->foldersList ())) if (! in_array ($folder, $this->foldersList ()))
throw new Exception ("Folder not found", 404); throw new Exception ("Folder not found", 404);
$folderUTF7 = mb_convert_encoding ($folder, "UTF7-IMAP","UTF-8"); $folderUTF7 = mb_convert_encoding ($folder, "UTF7-IMAP","UTF-8");
@@ -107,12 +124,16 @@ class imap
/** Return the current folder in UTF-8 */ /** Return the current folder in UTF-8 */
public function getFolder () public function getFolder ()
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
return $this->curDir; return $this->curDir;
} }
/** Create a new folder, provided in UTF-8. The folder must be absolute */ /** Create a new folder, provided in UTF-8. The folder must be absolute */
public function createFolder ($folder) public function createFolder ($folder)
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
if ( in_array ($folder, $this->foldersList ())) if ( in_array ($folder, $this->foldersList ()))
throw new Exception ("Folder already exists", 406); throw new Exception ("Folder already exists", 406);
$folderUTF7 = mb_convert_encoding ($folder, "UTF7-IMAP","UTF-8"); $folderUTF7 = mb_convert_encoding ($folder, "UTF7-IMAP","UTF-8");
@@ -124,6 +145,8 @@ class imap
*/ */
public function deleteFolder ($folder) public function deleteFolder ($folder)
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
if (! in_array ($folder, $this->foldersList ())) if (! in_array ($folder, $this->foldersList ()))
throw new Exception ("Folder not found", 404); throw new Exception ("Folder not found", 404);
$folderUTF7 = mb_convert_encoding ($folder, "UTF7-IMAP","UTF-8"); $folderUTF7 = mb_convert_encoding ($folder, "UTF7-IMAP","UTF-8");
@@ -135,6 +158,8 @@ class imap
in UTF-8 */ in UTF-8 */
public function getSubscribe () public function getSubscribe ()
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$subs = imap_getsubscribed (self::$instance[$this->mailbox], $this->mailbox, $subs = imap_getsubscribed (self::$instance[$this->mailbox], $this->mailbox,
"*"); "*");
$res = array (); $res = array ();
@@ -152,6 +177,8 @@ class imap
/** Add a subscription for a folder. The folder must be in UTF-8 */ /** Add a subscription for a folder. The folder must be in UTF-8 */
public function addSubscribe ($folder) public function addSubscribe ($folder)
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$folderUTF7 = mb_convert_encoding ($folder, "UTF7-IMAP","UTF-8"); $folderUTF7 = mb_convert_encoding ($folder, "UTF7-IMAP","UTF-8");
return imap_subscribe (self::$instance[$this->mailbox], return imap_subscribe (self::$instance[$this->mailbox],
$this->mailbox.$folder); $this->mailbox.$folder);
@@ -160,6 +187,8 @@ class imap
/** Remove a subscription for a folder. The folder must be in UTF-8 */ /** Remove a subscription for a folder. The folder must be in UTF-8 */
public function delSubscribe ($folder) public function delSubscribe ($folder)
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$folderUTF7 = mb_convert_encoding ($folder, "UTF7-IMAP","UTF-8"); $folderUTF7 = mb_convert_encoding ($folder, "UTF7-IMAP","UTF-8");
return imap_unsubscribe (self::$instance[$this->mailbox], return imap_unsubscribe (self::$instance[$this->mailbox],
$this->mailbox.$folder); $this->mailbox.$folder);
@@ -177,6 +206,8 @@ class imap
Size mailbox size */ Size mailbox size */
public function getFolderInfo ($folder) public function getFolderInfo ($folder)
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$oldFolder = $this->curDir; $oldFolder = $this->curDir;
$this->changeFolder ($folder); $this->changeFolder ($folder);
$rc = imap_mailboxmsginfo (self::$instance[$this->mailbox]); $rc = imap_mailboxmsginfo (self::$instance[$this->mailbox]);
@@ -192,6 +223,8 @@ class imap
/** Return an array of mailHeaders order by $field and by order ASC or DESC */ /** Return an array of mailHeaders order by $field and by order ASC or DESC */
public function imapSortMail ($mailHeaders, $field, $orderAsc = TRUE) public function imapSortMail ($mailHeaders, $field, $orderAsc = TRUE)
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$this->changeFolder ($this->curDir); $this->changeFolder ($this->curDir);
$sortList = array (); $sortList = array ();
$sortInc = array (); $sortInc = array ();
@@ -219,6 +252,8 @@ class imap
For information, takes 0.4s to select 30 mails on 1552 **/ For information, takes 0.4s to select 30 mails on 1552 **/
public function mailsDate ($from = 1, $nbmails = 30) public function mailsDate ($from = 1, $nbmails = 30)
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$this->changeFolder ($this->curDir); $this->changeFolder ($this->curDir);
if ($from === null) if ($from === null)
$from = 1; $from = 1;
@@ -264,6 +299,8 @@ class imap
[] => array ("msgno"=>msgno, "depth"=>depth) */ [] => array ("msgno"=>msgno, "depth"=>depth) */
public function mailsThread () public function mailsThread ()
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$this->changeFolder ($this->curDir); $this->changeFolder ($this->curDir);
$threads = @imap_thread (self::$instance[$this->mailbox]); $threads = @imap_thread (self::$instance[$this->mailbox]);
// imap_errors() takes the errors and clear the error stack // imap_errors() takes the errors and clear the error stack
@@ -292,6 +329,8 @@ class imap
/** Send back the number of mails in the mailbox */ /** Send back the number of mails in the mailbox */
public function mailsNumber () public function mailsNumber ()
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$this->changeFolder ($this->curDir); $this->changeFolder ($this->curDir);
$MC = imap_check (self::$instance[$this->mailbox]); $MC = imap_check (self::$instance[$this->mailbox]);
return $MC->Nmsgs; return $MC->Nmsgs;
@@ -300,6 +339,8 @@ class imap
/** Return an array containing the msgno corresponding to the criteria */ /** Return an array containing the msgno corresponding to the criteria */
public function mailsSearch ($criteria) public function mailsSearch ($criteria)
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$this->changeFolder ($this->curDir); $this->changeFolder ($this->curDir);
return imap_search (self::$instance[$this->mailbox], $criteria); return imap_search (self::$instance[$this->mailbox], $criteria);
} }
@@ -309,6 +350,8 @@ class imap
Expunge automatically the current folder to remove the old emails */ Expunge automatically the current folder to remove the old emails */
public function mailMove ($msgno, $folder) public function mailMove ($msgno, $folder)
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$this->changeFolder ($this->curDir); $this->changeFolder ($this->curDir);
if (is_array ($msgno)) if (is_array ($msgno))
$msgno = implode (",", $msgno); $msgno = implode (",", $msgno);
@@ -327,6 +370,8 @@ class imap
If $msgno is an array, all the mails with the contain msgno are copied */ If $msgno is an array, all the mails with the contain msgno are copied */
public function mailCopy ($msgno, $folder) public function mailCopy ($msgno, $folder)
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$this->changeFolder ($this->curDir); $this->changeFolder ($this->curDir);
if (is_array ($msgno)) if (is_array ($msgno))
$msgno = implode (",", $msgno); $msgno = implode (",", $msgno);
@@ -343,6 +388,8 @@ class imap
needed */ needed */
public function expunge () public function expunge ()
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$this->changeFolder ($this->curDir); $this->changeFolder ($this->curDir);
return imap_expunge (self::$instance[$this->mailbox]); return imap_expunge (self::$instance[$this->mailbox]);
} }
@@ -353,6 +400,8 @@ class imap
/** Get an existing email in the current folder in raw format */ /** Get an existing email in the current folder in raw format */
public function getEmailRaw ($msgno) public function getEmailRaw ($msgno)
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$this->changeFolder ($this->curDir); $this->changeFolder ($this->curDir);
// Clear the errors // Clear the errors
imap_errors(); imap_errors();
@@ -367,6 +416,8 @@ class imap
/** Get the headers of the email (in raw format) */ /** Get the headers of the email (in raw format) */
public function getEmailHeadersRaw ($msgno) public function getEmailHeadersRaw ($msgno)
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$this->changeFolder ($this->curDir); $this->changeFolder ($this->curDir);
// Clear the errors // Clear the errors
imap_errors(); imap_errors();
@@ -380,6 +431,8 @@ class imap
/** Get all the body (and attached files) of an email in raw format */ /** Get all the body (and attached files) of an email in raw format */
public function getEmailBodyRaw ($msgno) public function getEmailBodyRaw ($msgno)
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$this->changeFolder ($this->curDir); $this->changeFolder ($this->curDir);
// Clear the errors // Clear the errors
imap_errors(); imap_errors();
@@ -393,6 +446,8 @@ class imap
/** Return email structure of the body */ /** Return email structure of the body */
public function getStructure ($msgno) public function getStructure ($msgno)
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$this->changeFolder ($this->curDir); $this->changeFolder ($this->curDir);
// Clear the errors // Clear the errors
imap_errors(); imap_errors();
@@ -406,6 +461,8 @@ class imap
/** Return the structure of the mail body with the associated content */ /** Return the structure of the mail body with the associated content */
public function getStructureWithContent ($msgno) public function getStructureWithContent ($msgno)
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$this->changeFolder ($this->curDir); $this->changeFolder ($this->curDir);
// Clear the errors // Clear the errors
imap_errors(); imap_errors();
@@ -532,6 +589,8 @@ class imap
mail and some other info */ mail and some other info */
public function getStructureContent ($msgno, $part) public function getStructureContent ($msgno, $part)
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$structure = $this->getStructureWithContent ($msgno); $structure = $this->getStructureWithContent ($msgno);
if (isset ($structure->parts[$part])) if (isset ($structure->parts[$part]))
return $structure->parts[$part]; return $structure->parts[$part];
@@ -542,6 +601,8 @@ class imap
in getStructureContent */ in getStructureContent */
public function getStructureParts ($msgno) public function getStructureParts ($msgno)
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$structure = $this->getStructure ($msgno); $structure = $this->getStructure ($msgno);
if (! isset ($structure->parts)) if (! isset ($structure->parts))
return array (); return array ();
@@ -554,6 +615,8 @@ class imap
Expunge the mails at the end of the operation */ Expunge the mails at the end of the operation */
public function mailsDel ($msgno) public function mailsDel ($msgno)
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$this->changeFolder ($this->curDir); $this->changeFolder ($this->curDir);
if (is_array ($msgno)) if (is_array ($msgno))
$msgno = implode (",", $msgno); $msgno = implode (",", $msgno);
@@ -571,6 +634,8 @@ class imap
directory listing can provide erroneous data */ directory listing can provide erroneous data */
public function mailAdd ($content) public function mailAdd ($content)
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$folderUTF7 = mb_convert_encoding ($this->curDir, "UTF7-IMAP","UTF-8"); $folderUTF7 = mb_convert_encoding ($this->curDir, "UTF7-IMAP","UTF-8");
$rc = imap_append (self::$instance[$this->mailbox], $rc = imap_append (self::$instance[$this->mailbox],
$this->mailbox.$folderUTF7, $this->mailbox.$folderUTF7,
@@ -588,6 +653,8 @@ class imap
/** Return the quota used by the user in Mo */ /** Return the quota used by the user in Mo */
public function getQuota () public function getQuota ()
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$quota = @imap_get_quotaroot (self::$instance[$this->mailbox], "INBOX"); $quota = @imap_get_quotaroot (self::$instance[$this->mailbox], "INBOX");
imap_errors(); imap_errors();
if (! isset ($quota["STORAGE"])) if (! isset ($quota["STORAGE"]))
@@ -610,6 +677,8 @@ class imap
\Draft Message has not completed composition (marked as a draft). */ \Draft Message has not completed composition (marked as a draft). */
public function setFlag ($msgno, $flags) public function setFlag ($msgno, $flags)
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$this->changeFolder ($this->curDir); $this->changeFolder ($this->curDir);
if (is_array ($msgno)) if (is_array ($msgno))
$msgno = implode (",", $msgno); $msgno = implode (",", $msgno);
@@ -632,6 +701,8 @@ class imap
\Draft Message has not completed composition (marked as a draft). */ \Draft Message has not completed composition (marked as a draft). */
public function unsetFlag ($msgno, $flags) public function unsetFlag ($msgno, $flags)
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$this->changeFolder ($this->curDir); $this->changeFolder ($this->curDir);
if (is_array ($msgno)) if (is_array ($msgno))
$msgno = implode (",", $msgno); $msgno = implode (",", $msgno);
@@ -648,6 +719,8 @@ class imap
If msgno is an integer, only one mail will be modified */ If msgno is an integer, only one mail will be modified */
public function markMailAsRead ($msgno) public function markMailAsRead ($msgno)
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$this->changeFolder ($this->curDir); $this->changeFolder ($this->curDir);
if (is_array ($msgno)) if (is_array ($msgno))
$msgno = implode (",", $msgno); $msgno = implode (",", $msgno);
@@ -664,6 +737,8 @@ class imap
If msgno is an integer, only one mail will be modified */ If msgno is an integer, only one mail will be modified */
public function markMailAsUnread ($msgno) public function markMailAsUnread ($msgno)
{ {
if ($this->mailbox === null)
throw new Exception ("IMAP server not connected", 500);
$this->changeFolder ($this->curDir); $this->changeFolder ($this->curDir);
if (is_array ($msgno)) if (is_array ($msgno))
$msgno = implode (",", $msgno); $msgno = implode (",", $msgno);