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