]> git.mxchange.org Git - friendica.git/blob - include/email.php
rfc2047 encode notification email headers
[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         if(! $struc->parts) {
83                 $ret['body'] = email_get_part($mbox,$uid,$struc,0);
84         }
85         else {
86                 foreach($struc->parts as $ptop => $p) {
87                         $x = email_get_part($mbox,$uid,$p,$ptop + 1);
88                         if($x)
89                                 $ret['body'] = $x;
90                 }
91         }
92         return $ret;
93 }
94
95 // At the moment - only return plain/text.
96 // Later we'll repackage inline images as data url's and make the HTML safe
97
98 function email_get_part($mbox,$uid,$p,$partno) {
99     // $partno = '1', '2', '2.1', '2.1.3', etc for multipart, 0 if simple
100     global $htmlmsg,$plainmsg,$charset,$attachments;
101
102         echo $partno;
103
104     // DECODE DATA
105     $data = ($partno)
106                 ? @imap_fetchbody($mbox,$uid,$partno, FT_UID|FT_PEEK)
107         : @imap_body($mbox,$uid,FT_UID|FT_PEEK);
108
109     // Any part may be encoded, even plain text messages, so check everything.
110     if ($p->encoding==4)
111         $data = quoted_printable_decode($data);
112     elseif ($p->encoding==3)
113         $data = base64_decode($data);
114
115     // PARAMETERS
116     // get all parameters, like charset, filenames of attachments, etc.
117     $params = array();
118     if ($p->parameters)
119         foreach ($p->parameters as $x)
120             $params[strtolower($x->attribute)] = $x->value;
121     if ($p->dparameters)
122         foreach ($p->dparameters as $x)
123             $params[strtolower($x->attribute)] = $x->value;
124
125     // ATTACHMENT
126     // Any part with a filename is an attachment,
127     // so an attached text file (type 0) is not mistaken as the message.
128
129     if ($params['filename'] || $params['name']) {
130         // filename may be given as 'Filename' or 'Name' or both
131         $filename = ($params['filename'])? $params['filename'] : $params['name'];
132         // filename may be encoded, so see imap_mime_header_decode()
133         $attachments[$filename] = $data;  // this is a problem if two files have same name
134     }
135
136     // TEXT
137     if ($p->type == 0 && $data) {
138         // Messages may be split in different parts because of inline attachments,
139         // so append parts together with blank row.
140         if (strtolower($p->subtype)=='plain')
141             return (trim($data) ."\n\n");
142         else
143                         $data = '';
144
145  //           $htmlmsg .= $data ."<br><br>";
146         $charset = $params['charset'];  // assume all parts are same charset
147     }
148
149     // EMBEDDED MESSAGE
150     // Many bounce notifications embed the original message as type 2,
151     // but AOL uses type 1 (multipart), which is not handled here.
152     // There are no PHP functions to parse embedded messages,
153     // so this just appends the raw source to the main message.
154 //    elseif ($p->type==2 && $data) {
155 //        $plainmsg .= $data."\n\n";
156 //    }
157
158     // SUBPART RECURSION
159     if ($p->parts) {
160         foreach ($p->parts as $partno0=>$p2) {
161             $x =  email_get_part($mbox,$uid,$p2,$partno . '.' . ($partno0+1));  // 1.2, 1.2.1, etc.
162                         if($x)
163                                 return $x;
164                 }
165     }
166 }
167
168
169
170 function email_header_encode($in_str, $charset) {
171     $out_str = $in_str;
172     if ($out_str && $charset) {
173
174         // define start delimimter, end delimiter and spacer
175         $end = "?=";
176         $start = "=?" . $charset . "?B?";
177         $spacer = $end . "\r\n " . $start;
178
179         // determine length of encoded text within chunks
180         // and ensure length is even
181         $length = 75 - strlen($start) - strlen($end);
182
183         /*
184             [EDIT BY danbrown AT php DOT net: The following
185             is a bugfix provided by (gardan AT gmx DOT de)
186             on 31-MAR-2005 with the following note:
187             "This means: $length should not be even,
188             but divisible by 4. The reason is that in
189             base64-encoding 3 8-bit-chars are represented
190             by 4 6-bit-chars. These 4 chars must not be
191             split between two encoded words, according
192             to RFC-2047.
193         */
194         $length = $length - ($length % 4);
195
196         // encode the string and split it into chunks
197         // with spacers after each chunk
198         $out_str = base64_encode($out_str);
199         $out_str = chunk_split($out_str, $length, $spacer);
200
201         // remove trailing spacer and
202         // add start and end delimiters
203         $spacer = preg_quote($spacer);
204         $out_str = preg_replace("/" . $spacer . "$/", "", $out_str);
205         $out_str = $start . $out_str . $end;
206     }
207     return $out_str;
208
209
210