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