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