]> git.mxchange.org Git - friendica.git/blob - include/email.php
Encoding for incoming mails. Fixed invalid message id (with side effect at the moment...
[friendica.git] / include / email.php
1 <?php
2
3 function email_connect($mailbox,$username,$password) {
4         if(! function_exists('imap_open'))
5                 return false;
6
7         $mbox = @imap_open($mailbox,$username,$password);
8
9         return $mbox;
10 }
11
12 function email_poll($mbox,$email_addr) {
13
14         if(! ($mbox && $email_addr))
15                 return array();
16
17         $search1 = @imap_search($mbox,'FROM "' . $email_addr . '"', SE_UID);
18         if(! $search1)
19                 $search1 = array();
20
21         $search2 = @imap_search($mbox,'TO "' . $email_addr . '"', SE_UID);
22         if(! $search2)
23                 $search2 = array();
24
25         $search3 = @imap_search($mbox,'CC "' . $email_addr . '"', SE_UID);
26         if(! $search3)
27                 $search3 = array();
28
29         $search4 = @imap_search($mbox,'BCC "' . $email_addr . '"', SE_UID);
30         if(! $search4)
31                 $search4 = array();
32
33         $res = array_unique(array_merge($search1,$search2,$search3,$search4));
34
35         return $res;
36 }
37
38
39 function construct_mailbox_name($mailacct) {
40         $ret = '{' . $mailacct['server'] . ((intval($mailacct['port'])) ? ':' . $mailacct['port'] : '');
41         $ret .= (($mailacct['ssltype']) ?  '/' . $mailacct['ssltype'] . '/novalidate-cert' : '');
42         $ret .= '}' . $mailacct['mailbox'];
43         return $ret;
44 }
45
46
47 function email_msg_meta($mbox,$uid) {
48         $ret = (($mbox && $uid) ? @imap_fetch_overview($mbox,$uid,FT_UID) : array(array()));
49         return ((count($ret)) ? $ret[0] : array());
50 }
51
52 function email_msg_headers($mbox,$uid) {
53         $raw_header = (($mbox && $uid) ? @imap_fetchheader($mbox,$uid,FT_UID) : '');
54         $raw_header = str_replace("\r",'',$raw_header);
55         $ret = array();
56         $h = split("\n",$raw_header);
57         if(count($h))
58         foreach($h as $line ) {
59             if (preg_match("/^[a-zA-Z]/", $line)) {
60                         $key = substr($line,0,strpos($line,':'));
61                         $value = substr($line,strpos($line,':')+1);
62
63                         $last_entry = strtolower($key);
64                         $ret[$last_entry] = trim($value);
65                 }
66                 else {
67                         $ret[$last_entry] .= ' ' . trim($line);
68         }
69         }
70         return $ret;
71 }
72
73
74 function email_get_msg($mbox,$uid) {
75         $ret = array();
76
77         $struc = (($mbox && $uid) ? @imap_fetchstructure($mbox,$uid,FT_UID) : null);
78
79         if(! $struc)
80                 return $ret;
81
82         // for testing purposes: Collect imported mails
83         // $file = tempnam("/tmp/friendica2/", "mail-in-");
84         // file_put_contents($file, json_encode($struc));
85
86         if(! $struc->parts) {
87                 $ret['body'] = email_get_part($mbox,$uid,$struc,0);
88         }
89         else {
90                 foreach($struc->parts as $ptop => $p) {
91                         $x = email_get_part($mbox,$uid,$p,$ptop + 1);
92                         if($x)
93                                 $ret['body'] = $x;
94                 }
95         }
96         return $ret;
97 }
98
99 // At the moment - only return plain/text.
100 // Later we'll repackage inline images as data url's and make the HTML safe
101
102 function email_get_part($mbox,$uid,$p,$partno) {
103     // $partno = '1', '2', '2.1', '2.1.3', etc for multipart, 0 if simple
104     global $htmlmsg,$plainmsg,$charset,$attachments;
105
106         //echo $partno;
107
108     // DECODE DATA
109     $data = ($partno)
110                 ? @imap_fetchbody($mbox,$uid,$partno, FT_UID|FT_PEEK)
111         : @imap_body($mbox,$uid,FT_UID|FT_PEEK);
112
113         // for testing purposes: Collect imported mails
114         // $file = tempnam("/tmp/friendica2/", "mail-body-");
115         // file_put_contents($file, $data);
116
117     // Any part may be encoded, even plain text messages, so check everything.
118     if ($p->encoding==4)
119         $data = quoted_printable_decode($data);
120     elseif ($p->encoding==3)
121         $data = base64_decode($data);
122
123     // PARAMETERS
124     // get all parameters, like charset, filenames of attachments, etc.
125     $params = array();
126     if ($p->parameters)
127         foreach ($p->parameters as $x)
128             $params[strtolower($x->attribute)] = $x->value;
129     if ($p->dparameters)
130         foreach ($p->dparameters as $x)
131             $params[strtolower($x->attribute)] = $x->value;
132
133     // ATTACHMENT
134     // Any part with a filename is an attachment,
135     // so an attached text file (type 0) is not mistaken as the message.
136
137     if ($params['filename'] || $params['name']) {
138         // filename may be given as 'Filename' or 'Name' or both
139         $filename = ($params['filename'])? $params['filename'] : $params['name'];
140         // filename may be encoded, so see imap_mime_header_decode()
141         $attachments[$filename] = $data;  // this is a problem if two files have same name
142     }
143
144     // TEXT
145     if ($p->type == 0 && $data) {
146         // Messages may be split in different parts because of inline attachments,
147         // so append parts together with blank row.
148         if (strtolower($p->subtype)=='plain')
149             return (trim($data) ."\n\n");
150         else
151                         $data = '';
152
153  //           $htmlmsg .= $data ."<br><br>";
154         $charset = $params['charset'];  // assume all parts are same charset
155     }
156
157     // EMBEDDED MESSAGE
158     // Many bounce notifications embed the original message as type 2,
159     // but AOL uses type 1 (multipart), which is not handled here.
160     // There are no PHP functions to parse embedded messages,
161     // so this just appends the raw source to the main message.
162 //    elseif ($p->type==2 && $data) {
163 //        $plainmsg .= $data."\n\n";
164 //    }
165
166     // SUBPART RECURSION
167     if ($p->parts) {
168         foreach ($p->parts as $partno0=>$p2) {
169             $x =  email_get_part($mbox,$uid,$p2,$partno . '.' . ($partno0+1));  // 1.2, 1.2.1, etc.
170                         if($x)
171                                 return $x;
172                 }
173     }
174 }
175
176
177
178 function email_header_encode($in_str, $charset) {
179     $out_str = $in_str;
180         $need_to_convert = false;
181
182         for($x = 0; $x < strlen($in_str); $x ++) {
183                 if((ord($in_str[$x]) == 0) || ((ord($in_str[$x]) > 128))) {
184                         $need_to_convert = true;
185                 }
186         }
187
188         if(! $need_to_convert)
189                 return $in_str;
190
191     if ($out_str && $charset) {
192
193         // define start delimimter, end delimiter and spacer
194         $end = "?=";
195         $start = "=?" . $charset . "?B?";
196         $spacer = $end . "\r\n " . $start;
197
198         // determine length of encoded text within chunks
199         // and ensure length is even
200         $length = 75 - strlen($start) - strlen($end);
201
202         /*
203             [EDIT BY danbrown AT php DOT net: The following
204             is a bugfix provided by (gardan AT gmx DOT de)
205             on 31-MAR-2005 with the following note:
206             "This means: $length should not be even,
207             but divisible by 4. The reason is that in
208             base64-encoding 3 8-bit-chars are represented
209             by 4 6-bit-chars. These 4 chars must not be
210             split between two encoded words, according
211             to RFC-2047.
212         */
213         $length = $length - ($length % 4);
214
215         // encode the string and split it into chunks
216         // with spacers after each chunk
217         $out_str = base64_encode($out_str);
218         $out_str = chunk_split($out_str, $length, $spacer);
219
220         // remove trailing spacer and
221         // add start and end delimiters
222         $spacer = preg_quote($spacer);
223         $out_str = preg_replace("/" . $spacer . "$/", "", $out_str);
224         $out_str = $start . $out_str . $end;
225     }
226     return $out_str;
227
228
229