/[drupal]/contributions/modules/mail_api/contrib/imapwu_api/libs/imap.class.php
ViewVC logotype

Diff of /contributions/modules/mail_api/contrib/imapwu_api/libs/imap.class.php

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1.2.1, Wed Jan 7 21:28:24 2009 UTC revision 1.1.2.2, Thu May 14 18:57:02 2009 UTC
# Line 0  Line 1 
1    <?php
2    /*
3    This class implements a basic IMAP protocol. It does not add any additional functionality
4    */
5    
6    class imap
7    {
8            var $username;
9            var $password;
10            var $host;
11            var $port;
12            var $folder;
13            var $options;
14            var $connect_string;
15            var $mbox;
16    
17    
18            /*
19            connects to the IMAP server
20            */
21            function connect($username, $password, $host, $port=143, $folder="", $options)
22            {
23    
24                    //echo "<p>connecting to $username:$password@$host:$port/$folder</p>";
25    
26                    if(empty($username) || empty($password) || empty($host)) return FALSE;
27    
28                    $this -> username = $username;
29                    $this -> password = $password;
30                    $this -> host = $host;
31                    $this -> port = $port;
32                    $this -> folder = $folder;
33                    $this -> options = $options;
34    
35                    $this -> connect_string = '{'.$host.':'.$port.$this->options.'}'.$folder;
36    
37                    //echo $this->connect_string."<br>";
38    
39    
40                    if($this -> mbox = imap_open($this->connect_string, $username, $password)) {
41                            return TRUE;
42                    } else {
43                            return FALSE;
44                    }
45    
46            }
47    
48      /**
49      * Implement Imap2 Search capability in the system
50      * @query: the string with which to carry out the search
51      * @criteria: array of flags to search using the query i.e. BODY, TO, FROM, SUBJECT
52      * @flags: array of flags that are by themself with no string associated i.e. \\SEEN, \\UNSEEN
53      */
54      function search($query, $criteria, $flags) {
55        if (!$this->mbox) return FALSE;
56    
57        // one of the variables needs to have data to perform a search
58        if (empty($query) && sizeof($flags)<=0) return FALSE;
59    
60        $search = '';
61    
62        if (!empty($query)) {
63          // str replace is so that any special characters are taken care of
64          $query = "\"" . str_replace("\"", "", $query) . "\"";
65          foreach ($criteria as $c) {
66            $search .= $c . " " . $query . " ";
67          }
68        }
69    
70        foreach ($flags as $flag_id=>$flag) {
71          $search .= $flag . " ";
72        }
73    
74    
75        // echo "SEARCH STRING: $search<br>\n";
76    
77        // get results set from the imap search
78        $rs = imap_search($this->mbox, $search);
79    
80        if(empty($rs)) return FALSE;
81    
82        return array_unique($rs);
83      }
84    
85            /*
86            implements stock imap_status function - returns a brief overview of new messages in a mailbox
87            */
88            function get_status($folder)
89            {
90                    if(!$this->mbox) return FALSE;
91    
92                    $root_connect_string = substr($this->connect_string, 0, strpos($this->connect_string, "}")+1);
93    
94            return imap_status ($this->mbox, $root_connect_string.$folder, SA_ALL);
95    
96            }
97    
98    
99            /*
100            implements the stock imap_sort function
101            */
102            function get_sort($criteria, $reverse)
103            {
104                    if(!$this->mbox) return FALSE;
105    
106                    return imap_sort($this->mbox, $criteria, $reverse);
107            }
108    
109    
110            function get_mbox_info()
111            {
112                    if(!$this->mbox) return FALSE;
113    
114            return imap_mailboxmsginfo($this->mbox);
115            }
116    
117    
118            /*
119            returns an array of objects with folders
120            */
121            function get_folders($filter = "*")
122            {
123                    if(!$this->mbox) return FALSE;
124    
125            $folders = imap_getmailboxes($this->mbox, '{' . $this->host .':' . $this->port . '}', $filter);
126            return $folders;
127            }
128    
129            /*
130            returns an array of objects containing subscribed folders
131            */
132            function get_subscriptions($filter = "*")
133            {
134                    if(!$this->mbox) return FALSE;
135    
136            $folders = imap_getsubscribed($this->mbox, '{' . $this->host .':' . $this->port . '}', $filter);
137            return $folders;
138            }
139    
140            /*
141            returns an array of objects containg headers in the mailbox
142            */
143            function get_headers()
144            {
145                    if(!$this->mbox) return FALSE;
146    
147                    $headers = imap_headers($this->mbox);
148                    return $headers;
149            }
150    
151    
152            function get_header_full($mid)
153            {
154        if(empty($mid)) return FALSE;
155        if(!$this->mbox) return FALSE;
156    
157    
158    
159                    $rs = imap_fetchheader($this->mbox, $mid);
160                    $raw = explode("\n", $rs);
161                    foreach($raw as $index=>$line) {
162                            if(trim($line)=="") continue;
163                list($variable, $value) = split(":", $line, 2);
164                $set[trim($variable)]=trim($value);
165                    }
166    
167    
168                    $obj = $this -> get_header($mid);
169    
170                    $obj -> full_toaddress = $set["To"];
171                    $obj -> full_ccaddress = $set["Cc"];
172                    //__webmail_plus_dump($set);
173              /*
174                    $obj = new stdClass();
175                    $obj -> date = $set["Date"];
176                    $obj -> Date = $set["Date"];
177                    $obj -> MailDate = $set["Date"];
178                    $obj -> subject = $set["Subject"];
179                    $obj -> Subject = $set["Subject"];
180                    $obj -> toaddress = $set["To"];
181                    $obj -> fromaddress = $set[""]
182                    $obj -> reply_toaddress =
183                    [senderaddress]
184        */
185    
186                    return $obj;
187            }
188    
189            /*
190            returns a header for an individual message in form of an object which contains a header
191            */
192            function get_header($mid)
193            {
194                    if(empty($mid)) return FALSE;
195                    if(!$this->mbox) return FALSE;
196    
197    
198                    $rs = imap_headerinfo($this->mbox, $mid);
199    
200                    //print_r($rs);
201    
202                    return $rs;
203    
204            }
205    
206            /*
207            returns an array of objects with headers for all messages in the folder
208            */
209            function get_overview()
210            {
211                    if(!$this->mbox) return FALSE;
212    
213                    $check = imap_check($this->mbox);
214                    $messages = $check -> Nmsgs;
215    
216                    if ($messages > 0) {
217                            $overview = imap_fetch_overview($this->mbox, "1:".$messages."", 0);
218                            // this makes the array index identical to msgno, makes life easier
219                            foreach ($overview as $index => $record) {
220                                    $rs[$record->msgno] = $record;
221                            }
222                            return $rs;
223                    } else {
224                            return FALSE;
225                    }
226            }
227    
228        /*
229        Copies a message to a different folder
230        */
231        function copy($mid, $destination) {
232            if (!$this->mbox) return FALSE;
233                    return imap_mail_copy($this->mbox, $mid, $destination);
234        }
235    
236        /*
237        implements IMAP check function
238        */
239        function check() {
240            if (!$this->mbox) return FALSE;
241    
242            return imap_check($this->mbox);
243        }
244    
245    
246    
247    
248            /*
249            moves a message to a different folder
250            */
251    
252            function move($mid, $destination) {
253                    if(!$this->mbox) return FALSE;
254    
255                    return imap_mail_move($this->mbox, $mid, $destination);
256            }
257    
258    
259            function delete($mid) {
260                    if (!$this->mbox) return FALSE;
261    
262                    //die("removing $this->mbox, $mid");
263    
264                    return imap_delete($this->mbox, $mid);
265            }
266    
267        /*
268        returns the structure of the IMAP message in form of an object
269        */
270        function get_structure($mid, $options="") {
271                    if(!$this->mbox) return FALSE;
272            return imap_fetchstructure($this->mbox, $mid, $options);
273        }
274    
275        /*
276        returns a part of a message in form of an object
277        */
278        function get_body($mid, $part, $options="") {
279            if(!$this->mbox) return FALSE;
280            return imap_fetchbody($this->mbox, $mid, $part, $options);
281        }
282    
283        function get_message($mid) {
284            $structure = $this -> get_structure($mid);
285            $parts = $structure -> parts;
286            foreach($parts as $key => $part) {}
287            return $structure;
288        }
289    
290        function get_raw_message($mid) {
291            if(!$this->mbox) return FALSE;
292            return imap_body($this->mbox, $mid, SE_UID);
293        }
294    
295        function add_folder($folder) {
296            if(!$this->mbox) return FALSE;
297                if(empty($folder)) return;
298    
299                // trim the connect string
300          $root_connect_string = substr($this->connect_string, 0, strpos($this->connect_string, "}")+1);
301    
302          return imap_createmailbox($this->mbox, $root_connect_string.$folder);
303        }
304    
305        function rename_folder($oldname, $newname) {
306            if (!$this->mbox) return FALSE;
307            if (empty($oldname) || empty($newname)) return FALSE;
308    
309            $root_connect_string = substr($this->connect_string, 0, strpos($this->connect_string, "}")+1);
310            return imap_renamemailbox($this->mbox, $root_connect_string . $oldname, $root_connect_string . $newname);
311        }
312    
313        function delete_folder($folder) {
314            if(!$this->mbox) return FALSE;
315                    if(empty($folder)) return;
316    
317                    $root_connect_string = substr($this->connect_string, 0, strpos($this->connect_string, "}")+1);
318                    return imap_deletemailbox($this->mbox, $root_connect_string.$folder);
319        }
320    
321        function get_mime_type(&$structure) {
322            $primary_mime_type = array("TEXT", "MULTIPART","MESSAGE", "APPLICATION", "AUDIO","IMAGE", "VIDEO", "OTHER");
323    
324            if($structure->subtype) {
325                    return $primary_mime_type[(int) $structure->type] . '/' .$structure->subtype;
326                    }
327                    return "TEXT/PLAIN";
328        }
329    
330        function expunge() {
331            if(!$this->mbox) return FALSE;
332            return imap_expunge($this->mbox);
333        }
334    
335        /*
336        sets a flag on a message
337        */
338        function set_flag($mid,$flags) {
339            if(!$this->mbox) return FALSE;
340            if(empty($flags)) return FALSE;
341    
342            return imap_setflag_full($this->mbox, $mid, $flags);
343        }
344    
345        /*
346        clears a flag on a message
347        */
348        function clear_flag($mid,$flags) {
349            if(!$this->mbox) return FALSE;
350            if(empty($flags)) return FALSE;
351    
352            return imap_clearflag_full($this->mbox, $mid, $flags);
353        }
354    
355        /*
356         puts a message in a folder
357         this is used to store a sent message in the Sent folder
358        */
359        function append($folder, $message) {
360            if(!$this->mbox) return FALSE;
361            if(empty($folder)) return;
362            // trim the connect string
363    
364            //echo "appending $message";
365    
366            $root_connect_string = substr($this->connect_string, 0, strpos($this->connect_string, "}")+1);
367            return imap_append($this->mbox, $root_connect_string.$folder, $message);
368        }
369    
370        /*
371        disconnects from IMAP server
372        */
373        function close() {
374            if($this->open) {
375                            imap_close($this->mbox);
376                            return TRUE;
377            }
378            return FALSE;
379        }
380    
381        function __has_prefix() {
382            if(preg_match("/\}[\w]/", $this->connect_string)) {
383              return TRUE;
384                    } else {
385                      return FALSE;
386                    }
387        }
388    
389    
390        function get_errors() {
391            return imap_errors();
392        }
393    
394        function set_timeout($type=1,$seconds=0) {
395            imap_timeout($type, $seconds);
396        }
397    }
398    ?>

Legend:
Removed from v.1.1.2.1  
changed lines
  Added in v.1.1.2.2

  ViewVC Help
Powered by ViewVC 1.1.2