From 389066c738a8716951b09ab8d73ec5bd8a925663 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Mon, 1 Apr 2019 19:02:31 +0000 Subject: [PATCH] imap : Add Namespace for exceptions git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@5185 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- imap.php | 122 +++++++++++++++++++++++++++---------------------------- 1 file changed, 61 insertions(+), 61 deletions(-) diff --git a/imap.php b/imap.php index 284add1..54299c8 100644 --- a/imap.php +++ b/imap.php @@ -58,13 +58,13 @@ class imap $imapssl = false, $imapcertvalidate = true) { 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); if (! function_exists ("mb_convert_encoding")) - throw new Exception ("PHP don't support MBString. Please add it !", 500); + throw new \Exception ("PHP don't support MBString. Please add it !", 500); if ($username === null) - throw new Exception ("No username provided for IMAP server", 500); + throw new \Exception ("No username provided for IMAP server", 500); if ($password === null) - throw new Exception ("No password provided for IMAP server", 500); + throw new \Exception ("No password provided for IMAP server", 500); $imapssl = ($imapssl !== false) ? "/ssl" : ""; $imapcertvalidate = ($imapcertvalidate === false) ? "/novalidate-cert" : ""; $this->mailbox = "{"."$imapserver:$imapport/imap$imapssl$imapcertvalidate". @@ -79,16 +79,16 @@ class imap self::$instance[$this->mailbox] = @imap_open ($this->mailbox, $username, $password); if (self::$instance[$this->mailbox] === FALSE) - throw new Exception (imap_last_error()); + throw new \Exception (imap_last_error()); } - catch (Exception $e) + catch (\Exception $e) { // imap_errors() takes the errors and clear the error stack $errors = imap_errors(); if (substr ($e->getMessage (), 0, 35) === "Can not authenticate to IMAP server") - throw new Exception ("IMAP error : ".$e->getMessage(), 401); - throw new Exception ("IMAP error : ".$e->getMessage(), 500); + throw new \Exception ("IMAP error : ".$e->getMessage(), 401); + throw new \Exception ("IMAP error : ".$e->getMessage(), 500); } } } @@ -103,7 +103,7 @@ class imap public function foldersList () { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); $list = array_keys ($this->foldersListWithAttr ()); sort ($list); return $list; @@ -115,7 +115,7 @@ class imap public function foldersListWithAttr () { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); $list = imap_getmailboxes (self::$instance[$this->mailbox], $this->mailbox, "*"); $res = array (); @@ -137,16 +137,16 @@ class imap public function changeFolder ($folder) { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); 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"); $rc = @imap_reopen (self::$instance[$this->mailbox], $this->mailbox.$folderUTF7); if ($rc === true) $this->curDir = $folder; else - throw new Exception ("Can't go in provided folder", 500); + throw new \Exception ("Can't go in provided folder", 500); return $rc; } @@ -155,7 +155,7 @@ class imap public function getFolder () { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); return $this->curDir; } @@ -165,9 +165,9 @@ class imap public function createFolder ($folder) { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); 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"); return imap_createmailbox (self::$instance[$this->mailbox], $this->mailbox.$folderUTF7); @@ -179,9 +179,9 @@ class imap public function deleteFolder ($folder) { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); 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"); return imap_deletemailbox (self::$instance[$this->mailbox], $this->mailbox.$folderUTF7); @@ -193,7 +193,7 @@ class imap public function getSubscribe () { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); $subs = imap_getsubscribed (self::$instance[$this->mailbox], $this->mailbox, "*"); $res = array (); @@ -216,7 +216,7 @@ class imap public function addSubscribe ($folder) { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + 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); @@ -229,7 +229,7 @@ class imap public function delSubscribe ($folder) { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + 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); @@ -250,13 +250,13 @@ class imap public function getFolderInfo ($folder) { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); $oldFolder = $this->curDir; $this->changeFolder ($folder); $rc = imap_mailboxmsginfo (self::$instance[$this->mailbox]); $this->changeFolder ($oldFolder); if ($rc === false) - throw new Exception ("Can't read information for folder $folder", 500); + throw new \Exception ("Can't read information for folder $folder", 500); return $rc; } @@ -271,7 +271,7 @@ class imap public function imapSortMail ($mailHeaders, $field, $orderAsc = TRUE) { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); $this->changeFolder ($this->curDir); $sortList = array (); $sortInc = array (); @@ -304,7 +304,7 @@ class imap public function mailsDate ($from = 1, $nbmails = 30) { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); $this->changeFolder ($this->curDir); if ($from === null) $from = 1; @@ -319,7 +319,7 @@ class imap if ($from < 1) $from = 1; if ($from > $MC->Nmsgs) - throw new Exception ("Mail start is higher than the number of mails", + throw new \Exception ("Mail start is higher than the number of mails", 500); $from = $MC->Nmsgs - $from + 1; $to = $from + $nbmails - 1; @@ -329,7 +329,7 @@ class imap else { if ($from > $MC->Nmsgs) - throw new Exception ("Mail start is higher than the number of mails", + throw new \Exception ("Mail start is higher than the number of mails", 500); if ($from < 1) $from = 1; @@ -352,7 +352,7 @@ class imap public function mailsThread () { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + 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 @@ -383,7 +383,7 @@ class imap public function mailsNumber () { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); $this->changeFolder ($this->curDir); $MC = imap_check (self::$instance[$this->mailbox]); return $MC->Nmsgs; @@ -395,7 +395,7 @@ class imap public function mailsSearch ($criteria) { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); $this->changeFolder ($this->curDir); return imap_search (self::$instance[$this->mailbox], $criteria); } @@ -409,7 +409,7 @@ class imap public function mailMove ($msgno, $folder) { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); $this->changeFolder ($this->curDir); if (is_array ($msgno)) $msgno = implode (",", $msgno); @@ -432,7 +432,7 @@ class imap public function mailCopy ($msgno, $folder) { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); $this->changeFolder ($this->curDir); if (is_array ($msgno)) $msgno = implode (",", $msgno); @@ -451,7 +451,7 @@ class imap public function expunge () { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); $this->changeFolder ($this->curDir); return imap_expunge (self::$instance[$this->mailbox]); } @@ -465,7 +465,7 @@ class imap public function getEmailRaw ($msgno) { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); $this->changeFolder ($this->curDir); // Clear the errors imap_errors(); @@ -473,7 +473,7 @@ class imap @imap_body (self::$instance[$this->mailbox], $msgno); $errors = imap_errors (); if ($errors !== false) - throw new Exception ("Mail not found", 404); + throw new \Exception ("Mail not found", 404); return $content; } @@ -483,14 +483,14 @@ class imap public function getEmailHeadersRaw ($msgno) { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); $this->changeFolder ($this->curDir); // Clear the errors imap_errors(); $content = @imap_fetchheader (self::$instance[$this->mailbox], $msgno); $errors = imap_errors (); if ($errors !== false) - throw new Exception ("Mail not found", 404); + throw new \Exception ("Mail not found", 404); return $content; } @@ -500,14 +500,14 @@ class imap public function getEmailBodyRaw ($msgno) { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); $this->changeFolder ($this->curDir); // Clear the errors imap_errors(); $content = @imap_body (self::$instance[$this->mailbox], $msgno); $errors = imap_errors (); if ($errors !== false) - throw new Exception ("Mail not found", 404); + throw new \Exception ("Mail not found", 404); return $content; } @@ -517,14 +517,14 @@ class imap public function getStructure ($msgno) { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); $this->changeFolder ($this->curDir); // Clear the errors imap_errors(); $structure = @imap_fetchstructure (self::$instance[$this->mailbox], $msgno); $errors = imap_errors (); if ($errors !== false) - throw new Exception ("Mail not found", 404); + throw new \Exception ("Mail not found", 404); return $structure; } @@ -534,14 +534,14 @@ class imap public function getStructureWithContent ($msgno) { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); $this->changeFolder ($this->curDir); // Clear the errors imap_errors(); $structure = @imap_fetchstructure (self::$instance[$this->mailbox], $msgno); $errors = imap_errors (); if ($errors !== false) - throw new Exception ("Mail not found", 404); + throw new \Exception ("Mail not found", 404); if (! isset ($structure->parts)) { // In case of PLAIN text, there is no parts @@ -602,7 +602,7 @@ class imap $structure->parts[$part1]->parts[$part2]->mimetype = "other/". strtolower ($struct2->subtype); else - throw new Exception (sprintf ( + throw new \Exception (sprintf ( dgettext("domframework", "Unknown type in imap_fetchstructure : %s"), $struct2->type), 500); @@ -648,7 +648,7 @@ class imap $structure->parts[$part1]->mimetype = "other/". strtolower ($struct1->subtype); else - throw new Exception (sprintf ( + throw new \Exception (sprintf ( dgettext("domframework", "Unknown type in imap_fetchstructure : %s"), $struct1->type), 500); @@ -667,11 +667,11 @@ class imap public function getStructureContent ($msgno, $part) { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); $structure = $this->getStructureWithContent ($msgno); if (isset ($structure->parts[$part])) return $structure->parts[$part]; - throw new Exception ("Part not found in the mail", 404); + throw new \Exception ("Part not found in the mail", 404); } /** Return the part identifiers of the structure of the mail body. To be used @@ -681,7 +681,7 @@ class imap public function getStructureParts ($msgno) { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); $structure = $this->getStructure ($msgno); if (! isset ($structure->parts)) return array (); @@ -697,14 +697,14 @@ class imap public function mailsDel ($msgno) { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); $this->changeFolder ($this->curDir); if (is_array ($msgno)) $msgno = implode (",", $msgno); $rc = @imap_delete (self::$instance[$this->mailbox], $msgno); imap_errors(); if ($rc === FALSE) - throw new Exception ("No mailID provided can be found : ABORT"); + throw new \Exception ("No mailID provided can be found : ABORT"); if ($this->autoexpunge) return imap_expunge(self::$instance[$this->mailbox]); return $rc; @@ -718,14 +718,14 @@ class imap public function mailAdd ($content) { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + 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, $content); $errors = imap_errors(); if ($rc === FALSE) - throw new Exception ("Error when saving the mail in folder : ". + throw new \Exception ("Error when saving the mail in folder : ". implode (" ", $errors), 500); return true; } @@ -738,7 +738,7 @@ class imap public function getQuota () { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); $quota = @imap_get_quotaroot (self::$instance[$this->mailbox], "INBOX"); imap_errors(); if (! isset ($quota["STORAGE"])) @@ -765,7 +765,7 @@ class imap public function setFlag ($msgno, $flags) { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); $this->changeFolder ($this->curDir); if (is_array ($msgno)) $msgno = implode (",", $msgno); @@ -773,7 +773,7 @@ class imap implode (" ", $flags)); imap_errors(); if ($rc === FALSE) - throw new Exception ("Can't define the flags", 500); + throw new \Exception ("Can't define the flags", 500); return true; } @@ -792,7 +792,7 @@ class imap public function unsetFlag ($msgno, $flags) { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); $this->changeFolder ($this->curDir); if (is_array ($msgno)) $msgno = implode (",", $msgno); @@ -800,7 +800,7 @@ class imap implode (" ", $flags)); imap_errors(); if ($rc === FALSE) - throw new Exception ("Can't define the flags", 500); + throw new \Exception ("Can't define the flags", 500); return true; } @@ -812,7 +812,7 @@ class imap public function markMailAsRead ($msgno) { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); $this->changeFolder ($this->curDir); if (is_array ($msgno)) $msgno = implode (",", $msgno); @@ -820,7 +820,7 @@ class imap "\\Seen"); imap_errors(); if ($rc === FALSE) - throw new Exception ("Can't mark mail as read", 500); + throw new \Exception ("Can't mark mail as read", 500); return true; } @@ -832,7 +832,7 @@ class imap public function markMailAsUnread ($msgno) { if ($this->mailbox === null) - throw new Exception ("IMAP server not connected", 500); + throw new \Exception ("IMAP server not connected", 500); $this->changeFolder ($this->curDir); if (is_array ($msgno)) $msgno = implode (",", $msgno); @@ -840,7 +840,7 @@ class imap "\\Seen"); imap_errors(); if ($rc === FALSE) - throw new Exception ("Can't mark mail as read", 500); + throw new \Exception ("Can't mark mail as read", 500); return true; } }