Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
6.1 KiB

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