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