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