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