/[drupal]/contributions/modules/mail_api/contrib/imapwu_api/imapwu_api.module
ViewVC logotype

Diff of /contributions/modules/mail_api/contrib/imapwu_api/imapwu_api.module

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    // $Id$
3    
4    /**
5     * @file
6     * This sub-module provides IMAP access functionality to Mail API
7     *
8     */
9    
10    
11    $_IMAP_API_TAG_COUNTER = 0;
12    
13    // set this to true to write debug info to the wathdog
14    $IMAPWU_DEBUG=FALSE;
15    
16    // set this to true to write the error log to the watchdog log
17    $IMAPWU_ERRORLOG=FALSE;
18    
19    
20    $_imapwu_api_connection = NULL;
21    $_imapwu_api_connect_string = NULL;
22    
23    /*
24    require_once "libs/imap.class.php";
25    require_once "libs/imap_mail.class.php";
26    require_once "libs/mail.class.php";
27    */
28    
29    /**
30     * Implementation of the _init hook
31     */
32    function imapwu_api_init() {
33    
34    
35    }
36    
37    /**
38     * Implementation of hook_help()
39     *
40     * @param unknown_type $path
41     * @param unknown_type $arg
42     * @return unknown
43     */
44    function imapwu_api_help($path, $arg) {
45      switch ($path) {
46        case 'admin/help#imap_api':
47         return t('Implements IMAP protocol based on the Washington University IMAP library.');
48      }
49    }
50    
51    /**
52     * implementation of mail_api_protocols hook
53     *
54     * @return unknown
55     */
56    function imapwu_api_mail_api_protocols() {
57      return array('IMAPWU' => t('IMAPWU'));
58    }
59    
60    function imapwu_api_mail_api_authenticate() {
61       //drupal_set_message('imapwu_api_mail_api_authenticate');
62    }
63    
64    function imapwu_api_connect($hostname, $username, $password, $port=143, $folder="", $options=OP_SILENT) {
65       global $_imapwu_api_connection, $_imapwu_api_connect_string;
66    
67    
68       // make sure the options starts with a slash
69       if ($options!="" && substr($options, 0, 1)!="/") $options="/" . $options;
70    
71       $_imapwu_api_connect_string = imapwu_api_assemble_connect_string($hostname, $port, $folder, $options);
72    
73       $_imapwu_api_connection = imap_open($_imapwu_api_connect_string, $username, $password, $options);
74    
75    
76       if ($_imapwu_api_connection) {
77          //drupal_set_message('connected');
78          return TRUE;
79       }
80       else {
81          if ($_imapwu_api_connectionWU_ERRORLOG) watchdog('imapwu', 'cannot connect to the server');
82          return FALSE;
83       }
84    
85    }
86    
87    
88    function imapwu_api_status($mailbox="INBOX", $options=SA_ALL) {
89       global $_imapwu_api_connection, $_imapwu_api_connect_string;
90    
91       //if(IMAPWU_DEBUG) drupal_set_message("imapwu_api_status $_imapwu_api_connect_string");
92    
93       $status = imap_status($_imapwu_api_connection, $_imapwu_api_connect_string, $options);
94    
95       return $status;
96    }
97    
98    /**
99     * Gets the number of messages in the current mailbox
100     *
101     * @param unknown_type $args
102     * @return unknown
103     */
104    function imapwu_api_num_msg($args) {
105       global $_imapwu_api_connection, $_imapwu_api_connect_string;
106       //if(IMAPWU_DEBUG) drupal_set_message("imapwu_api_num_msg");
107    
108       $rs = imap_num_msg($_imapwu_api_connection);
109       return $rs;
110    }
111    
112    
113    /**
114     * Returns headers for all messages in a mailbox
115     *
116     * @param unknown_type $args
117     * @return unknown
118     */
119    function imapwu_api_headers($args) {
120       global $_imapwu_api_connection, $_imapwu_api_connect_string;
121       //if(IMAPWU_DEBUG) drupal_set_message("imapwu_api_headers");
122    
123       $rs = imap_headers($_imapwu_api_connection);
124       return $rs;
125    }
126    
127    /**
128     * Returns header for a message
129     * this is used to fetch the full header of the message
130     *
131     * @param unknown_type $args
132     */
133    function imapwu_api_header($msgno, $options=FT_UID) {
134      global $_imapwu_api_connection, $_imapwu_api_connect_string;
135      //if(IMAPWU_DEBUG) drupal_set_message("imapwu_api_header");
136    
137       //drupal_set_message("message number $msgno");
138    
139       $header = imap_fetchheader($_imapwu_api_connection, $msgno, $options);
140       return $header;
141    }
142    
143    
144    /**
145     * Read an overview of the information in the headers of the given message
146     *
147     * @param unknown_type $args
148     *
149     * DO NOT USE FT_
150     */
151    function imapwu_api_overview($sequence="", $options="") {
152       global $_imapwu_api_connection, $_imapwu_api_connect_string;
153       //drupal_set_message("imapwu_api_overview");
154    
155       if ($options=="FT_UID") $options=="";
156    
157    
158       if ($sequence=="") {
159         $check =  imapwu_api_check();
160         $sequence = "1:{$check->Nmsgs}";
161       }
162    
163       //drupal_set_message('sequence: '.$sequence);
164    
165       $rs = imap_fetch_overview($_imapwu_api_connection, $sequence, $options);
166       //print_r($rs);
167    
168       return $rs;
169    }
170    
171    
172    /**
173     * Check current mailbox
174     */
175    function imapwu_api_check() {
176       global $_imapwu_api_connection, $_imapwu_api_connect_string;
177       //if(IMAPWU_DEBUG) drupal_set_message("imapwu_api_check");
178    
179       $rs = imap_check($_imapwu_api_connection);
180       return $rs;
181    }
182    
183    
184    /**
185     * Read the message body
186     * FT_UID - The msg_number  is a UID
187     * FT_PEEK - Do not set the \Seen flag if not already set
188     * FT_INTERNAL - The return string is in internal format, will not canonicalize to CRLF.
189     *
190     */
191    function imapwu_api_body($msg_number, $options=FT_UID) {
192       global $_imapwu_api_connection, $_imapwu_api_connect_string;
193    
194       //echo $msg_number;
195    
196       //if(IMAPWU_DEBUG) drupal_set_message("imapwu_api_body");
197    
198       $rs = imap_body($_imapwu_api_connection, $msg_number, $options);
199       return $rs;
200    }
201    
202    
203    /**
204     * Read the list of mailboxes, returning detailed information on each one
205     *
206     * LATT_NOINFERIORS - This mailbox has no "children" (there are no mailboxes below this one).
207      * LATT_NOSELECT - This is only a container, not a mailbox - you cannot open it.
208      * LATT_MARKED - This mailbox is marked. Only used by UW-IMAPD.
209      * LATT_UNMARKED - This mailbox is not marked. Only used by UW-IMAPD.
210     *
211     * @param unknown_type $pattern
212     * @return unknown
213     */
214    function imapwu_api_mailboxes($pattern="*") {
215       global $_imapwu_api_connection, $_imapwu_api_connect_string;
216    
217       //if(IMAPWU_DEBUG) drupal_set_message('imapwu_api_mailboxes '.$_imapwu_api_connect_string);
218    
219       //print_r($_imapwu_api_connect_string);
220    
221    
222       $rs = imap_getmailboxes($_imapwu_api_connection, $_imapwu_api_connect_string, $pattern);
223    
224       //print_r($rs);
225       //print_r($_imapwu_api_connect_string);
226    
227       return $rs;
228    }
229    
230    /*
231     * this internal function assembles the connection string needed for imap
232     */
233    function imapwu_api_assemble_connect_string($hostname, $port=143, $folder="", $options="") {
234      // if folder is empty, set it to inbox
235      //if($folder=="") $folder="INBOX";
236    
237      // make sure the options starts with a slash
238      if ($options!="" && substr($options, 0, 1)!="/") $options="/" . $options;
239    
240      $connect_string = '{'. $hostname .':'. $port . $options .'}'. $folder;
241    
242      return $connect_string;
243    
244    }
245    
246    /**
247     * appends a message to the current mailbox
248     *
249     * @param unknown_type $message
250     * @return unknown
251     */
252    function imapwu_api_append($message) {
253      global $_imapwu_api_connection, $_imapwu_api_connect_string;
254    
255      return imap_append($_imapwu_api_connection, $_imapwu_api_connect_string . $folder, $message);
256    }
257    
258    /**
259     * moves a message from the current mailbox to the specified mailbox
260     *
261     * @param unknown_type $message_uid
262     * @param unknown_type $destination
263     * @return unknown
264     */
265    function imapwu_api_move_message($message_uid, $destination) {
266      global $_imapwu_api_connection, $_imapwu_api_connect_string;
267      $rs = imap_mail_move($_imapwu_api_connection, $message_uid, $destination, CP_UID);
268      return imap_expunge($_imapwu_api_connection);
269    }
270    
271    /**
272     * deletes a message from the current mailbox
273     *
274     * @param unknown_type $message_uid
275     * @param unknown_type $destination
276     * @return unknown
277     */
278    function imapwu_api_delete_message($message_uid) {
279      global $_imapwu_api_connection, $_imapwu_api_connect_string;
280      $rs = imap_delete($_imapwu_api_connection, $message_uid, FT_UID);
281      return imap_expunge($_imapwu_api_connection);
282    }
283    
284    
285    /**
286     * sets a flag on an IMAP message
287     *
288     * @param unknown_type $message_uid
289     * @param unknown_type $flags
290     * @return unknown
291     */
292    function _imapwu_api_set_flag($message_uid, $flags) {
293      global $_imapwu_api_connection, $_imapwu_api_connect_string;
294      //echo $_imapwu_api_connect_string;
295    
296      $rs = imap_setflag_full($_imapwu_api_connection, $message_uid, $flags, ST_UID);
297      return imap_expunge($_imapwu_api_connection);
298    
299    }
300    
301    function _imapwu_api_clear_flag($message_uid, $flags) {
302      global $_imapwu_api_connection, $_imapwu_api_connect_string;
303      //echo $_imapwu_api_connect_string;
304    
305      $rs = imap_clearflag_full($_imapwu_api_connection, $message_uid, $flags, ST_UID);
306      return imap_expunge($_imapwu_api_connection);
307    
308    }
309    
310    function imapwu_api_mark_read($message_uid) {
311      global $_imapwu_api_connection, $_imapwu_api_connect_string;
312    
313      return _imapwu_api_set_flag($message_uid, '\\Seen');
314    
315    }
316    
317    function imapwu_api_mark_unread($message_uid) {
318      global $_imapwu_api_connection, $_imapwu_api_connect_string;
319    
320      return _imapwu_api_clear_flag($message_uid, '\\Seen');
321    
322    }
323    
324    function imapwu_api_get_structure($message_uid) {
325      global $_imapwu_api_connection, $_imapwu_api_connect_string;
326      return imap_fetchstructure($_imapwu_api_connection, $message_uid, FT_UID);
327    }
328    
329    
330    function imapwu_api_get_map($message_uid) {
331      $structure = imapwu_api_get_structure($message_uid);
332      if (!$structure->parts) {
333        return FALSE;
334      }
335    
336      return imapwu_api_create_part_array($structure);
337    }
338    
339    function imapwu_api_create_part_array($structure, $prefix="") {
340      $part_array = array();
341    
342      if (sizeof($structure->parts) > 0) {
343        foreach ($structure->parts as $count => $part) {
344          imapwu_api_add_part_to_array($part, $prefix . ($count+1), $part_array);
345        }
346      }
347    
348      return $part_array;
349    }
350    
351    
352    
353    function imapwu_api_add_part_to_array($obj, $partno, &$part_array) {
354      if (!is_array($part_array) || empty($part_array)) $part_array=array();
355      if ($obj->type == TYPEMESSAGE) {
356        imapwu_api_add_part_to_array($obj->parts[0], $partno .".", $part_array);
357      }
358      else {
359        if (sizeof($obj->parts) > 0) {
360          foreach ($obj->parts as $count => $p) {
361            imapwu_api_add_part_to_array($p, $partno .".". ($count+1), $part_array);
362          }
363        }
364      }
365    
366      $part_array[] = array('part_number' => $partno, 'part_object' => $obj);
367    }
368    
369    function imapwu_api_get_plaintext($message_uid, $set_seen=NULL) {
370      $map = imapwu_api_get_map($message_uid);
371    
372      if (!$map) {
373        $content = imapwu_api_get_body($message_uid, 1, $set_seen);
374        $encoding = imapwu_api_get_plaintext_encoding($mid);
375    
376        if ($encoding=="base64") {
377          $content = base64_decode($content);
378        }
379        else {
380          $content = quoted_printable_decode($content);
381        }
382    
383      }
384      else {
385        // extract the PLAIN part out of all parts
386        foreach ($map as $index => $part_info) {
387          if ($part_info['part_object']->subtype == "PLAIN") {
388            $content  = imapwu_api_get_part($message_uid, $part_info['part_number']);
389            break;
390          }
391        }
392      }
393    
394      if (!empty($content)) {
395        return $content;
396      }
397      else {
398        return FALSE;
399      }
400    }
401    
402    /*
403    returns the charset for the plaintext part of the message
404    */
405    function imapwu_api_get_plaintext_charset($message_uid, $options="") {
406      $map = imapwu_api_get_map($message_uid);
407    
408      if (!$map) {
409        $header = imapwu_api_get_header($message_uid);
410        $body = imapwu_api_get_body($message_uid, 0);
411    
412        // see if there's a content type string
413        if (preg_match("/Content-Type/", $body)) {
414          preg_match("/charset=\"?(.*)\"?/", $body, $matches); // charset may or may not be enclosed in quotes
415          $charset = $matches[1];
416          return $charset;
417        }
418    
419        return FALSE;
420    
421      }
422      else {
423        foreach ($map as $index => $part_info) {
424          if ($part_info['part_object']->subtype == "PLAIN" && is_array($part_info['part_object']->parameters)) {
425    
426            foreach ($part_info['part_object']->parameters as $parameter_id => $parameter_info) {
427              if ($parameter_info->attribute=="charset") {
428                $charset = $parameter_info -> value;
429                break;
430              }
431            }
432          }
433    
434        }
435    
436      }
437    
438      if (!empty($charset)) {
439        return $charset;
440      }
441      else {
442        return FALSE;
443      }
444    }
445    
446    function imapwu_api_get_plaintext_encoding($message_uid) {
447      $body = imapwu_api_get_body($message_uid, 0);
448    
449      // see if there's a content transfer string
450      if (preg_match("/Content-Transfer-Encoding:/", $body)) {
451        preg_match("/Content-Transfer-Encoding:(.*)/", $body, $matches);
452        $encoding = trim($matches[1]);
453        return $encoding;
454      }
455    
456      return FALSE;
457    }
458    
459    /**
460     * returns a part of the IMAP message according to the supplied map
461     *
462     * @param unknown_type $message_uid
463     * @param unknown_type $part
464     * @return unknown
465     */
466    function imapwu_api_get_part($message_uid, $part) {
467      $map = imapwu_api_get_map($message_uid);
468    
469      if (!$map) return FALSE;
470    
471      foreach ($map as $index => $part_info) {
472        if ($part_info['part_number']==$part) {
473          if ($part_info['part_object']->type ==0 || $part_info['part_object']->type =="") {
474            if ($part_info['part_object']->encoding==3) {
475              $content = base64_decode(imapwu_api_get_body($message_uid, $part));
476            }
477            elseif ($part_info['part_object']->encoding==4) {
478              $content = quoted_printable_decode(imapwu_api_get_body($message_uid, $part));
479            }
480            else {
481              $content = imapwu_api_get_body($message_uid, $part);
482            }
483    
484          }
485          else {
486            // handle complex encoded data
487            // handle base64 encoded data
488            if ($part_info['part_object']->encoding==3) $content = base64_decode(imapwu_api_get_body($message_uid, $part));
489    
490            // handle quoted data
491            if ($part_info['part_object']->encoding==4) $content = quoted_printable_decode(imapwu_api_get_body($message_uid, $part));
492          }
493    
494          return $content;
495    
496        }
497      }
498    
499      return FALSE;
500    }
501    
502    function imapwu_api_get_body($message_uid, $part, $set_seen=NULL) {
503      global $_imapwu_api_connection, $_imapwu_api_connect_string;
504    
505      $merged_flags = FT_UID;
506      if ($set_seen) $merged_flags = $merged_flags & FT_PEEK;
507      return imap_fetchbody($_imapwu_api_connection, $message_uid, $part, $merged_flags);
508    }
509    
510    function imapwu_api_get_attachment_overview($message_uid) {
511      $map = imapwu_api_get_map($message_uid);
512    
513      // this message contains no attachemnts
514      if (!$map) return FALSE;
515    
516      foreach ($map as $index => $part_info) {
517        if ($part_info['part_object']->ifdisposition == 1) {
518          $attachments[] = array(
519          'part_number' => $part_info['part_number'],
520          'type' => $part_info['part_object']->subtype,
521          'size' => $part_info['part_object']->bytes,
522          'file' => $part_info['part_object']->dparameters[0]->value
523          );
524        }
525      }
526    
527      return $attachments;
528    }
529    
530    function imapwu_api_get_attachment_files($message_uid) {
531      // first get the overview
532      $att_overview = imapwu_api_get_attachment_overview($message_uid);
533    
534      // now return only those that have actual file names
535      if (empty($att_overview)) return FALSE;
536      if (sizeof($att_overview)<=0) return FALSE;
537    
538      foreach ($att_overview as $att_id => $att_info) {
539        if (!empty($att_info['file'])) $rs[]=$att_info;
540      }
541    
542      return $rs;
543    
544    }
545    
546    function imapwu_api_get_part_object($message_uid, $part) {
547      global $_imapwu_api_connection, $_imapwu_api_connect_string;
548    
549      $map = imapwu_api_get_map($message_uid);
550    
551      foreach ($map as $map_id => $part_info) {
552        if ($part_info['part_number']==$part) return $part_info['part_object'];
553      }
554    
555      return FALSE;
556    }
557    
558    function imapwu_api_get_part_mime_type($mid, $part) {
559        $part = imapwu_api_get_part_object($mid, $part);
560    
561    
562    
563          switch (strtoupper($part->subtype)) {
564            # Applications
565            case 'PDF':
566            case 'PS':
567            case 'POST-SCRIPT':
568              $mime = 'application/pdf';
569              break;
570            case 'MSWORD':
571              $mime = 'application/msword';
572              break;
573            case 'PNG':
574            case 'GIF':
575            case 'BMP':
576            case 'JPG':
577            case 'JPEG':
578              $mime = "image/$type";
579              break;
580            case 'MP3':
581            case 'WMA':
582            case 'X-MS-WMA':
583            case 'AAC':
584              $mime = "audio/$type";
585              break;
586            case 'MPEG':
587            case 'WMV':
588            case 'AVI':
589              $mime = "video/$type";
590              break;
591            default:
592              $mime = 'text/x-generic';
593          }
594    
595          return $mime;
596    }

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