| 1 | <?php |
| 2 | /******************************************************************************** |
| 3 | * banana/message.inc.php : class for messages |
| 4 | * ------------------------ |
| 5 | * |
| 6 | * This file is part of the banana distribution |
| 7 | * Copyright: See COPYING files that comes with this distribution |
| 8 | ********************************************************************************/ |
| 9 | |
| 10 | require_once dirname(__FILE__) . '/mimepart.inc.php'; |
| 11 | require_once dirname(__FILE__) . '/message.func.inc.php'; |
| 12 | require_once dirname(__FILE__) . '/banana.inc.php'; |
| 13 | |
| 14 | final class BananaMessage extends BananaMimePart |
| 15 | { |
| 16 | private $msg_headers = array(); |
| 17 | |
| 18 | public function __construct($data = null) |
| 19 | { |
| 20 | parent::__construct($data); |
| 21 | if (!is_null($data)) { |
| 22 | if (isset($this->headers['in-reply-to']) && isset($this->headers['references'])) { |
| 23 | unset($this->headers['in-reply-to']); |
| 24 | } |
| 25 | Banana::$msgshow_headers = array_intersect(Banana::$msgshow_headers, array_keys($this->headers)); |
| 26 | Banana::$message =& $this; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | public function hasHeader($hdr) |
| 31 | { |
| 32 | return isset($this->headers[$hdr]); |
| 33 | } |
| 34 | |
| 35 | static public function newMessage(array $headers, $body, array $file = null) |
| 36 | { |
| 37 | $msg = new BananaMessage(); |
| 38 | $msg->msg_headers = $headers; |
| 39 | $msg->makeTextPart($body, 'text/plain', '8bits', 'UTF-8', 'fixed'); |
| 40 | if (!is_null($file)) { |
| 41 | $msg->addAttachment($file); |
| 42 | } |
| 43 | return $msg; |
| 44 | } |
| 45 | |
| 46 | static public function translateHeaderName($hdr) |
| 47 | { |
| 48 | switch (strtolower($hdr)) { |
| 49 | case 'from': return _b_('De'); |
| 50 | case 'subject': return _b_('Sujet'); |
| 51 | case 'newsgroups': return _b_('Forums'); |
| 52 | case 'followup-to': return _b_('Suivi à'); |
| 53 | case 'to': return _b_('À'); |
| 54 | case 'cc': return _b_('Copie à'); |
| 55 | case 'bcc': return _b_('Copie cachée à'); |
| 56 | case 'reply-to': return _b_('Répondre à'); |
| 57 | case 'date': return _b_('Date'); |
| 58 | case 'organization': return _b_('Organisation'); |
| 59 | case 'in-reply-to': |
| 60 | case 'references': return _b_('Références'); |
| 61 | case 'x-face': return _b_('Image'); |
| 62 | } |
| 63 | return $hdr; |
| 64 | } |
| 65 | |
| 66 | public function translateHeaderValue($hdr) |
| 67 | { |
| 68 | if (!isset($this->headers[$hdr])) { |
| 69 | return null; |
| 70 | } |
| 71 | $text = $this->headers[$hdr]; |
| 72 | |
| 73 | if (function_exists('hook_formatDisplayHeader') |
| 74 | && $res = hook_formatDisplayHeader($hdr, $text)) { |
| 75 | return $res; |
| 76 | } |
| 77 | switch ($hdr) { |
| 78 | case "date": |
| 79 | return BananaMessage::formatDate($text); |
| 80 | |
| 81 | case "followup-to": case "newsgroups": |
| 82 | $groups = preg_split("/[\t ]*,[\t ]*/", $text); |
| 83 | $res = ''; |
| 84 | foreach ($groups as $g) { |
| 85 | $res .= Banana::$page->makeLink(Array('group' => $g, 'text' => $g)) . ', '; |
| 86 | } |
| 87 | return substr($res,0, -2); |
| 88 | |
| 89 | case "from": |
| 90 | return BananaMessage::formatFrom($text); |
| 91 | |
| 92 | case "references": case "in-reply-to": |
| 93 | $rsl = ""; |
| 94 | $parents = preg_grep('/^\d+$/', $this->getTranslatedReferences()); |
| 95 | $p = array_pop($parents); |
| 96 | |
| 97 | $parents = array(); |
| 98 | while (!is_null($p)) { |
| 99 | array_unshift($parents, $p); |
| 100 | $p = Banana::$spool->overview[$p]->parent; |
| 101 | } |
| 102 | $ndx = 1; |
| 103 | foreach ($parents as $p) { |
| 104 | $rsl .= Banana::$page->makeLink(Array('group' => Banana::$spool->group, |
| 105 | 'artid' => $p, 'text' => $ndx++)) . ' '; |
| 106 | } |
| 107 | return $rsl; |
| 108 | |
| 109 | case "subject": |
| 110 | $text = stripslashes($text); |
| 111 | return banana_catchFormats($text); |
| 112 | |
| 113 | default: |
| 114 | return $text; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | public function getSender() |
| 119 | { |
| 120 | $from = $this->headers['from']; |
| 121 | $name = trim(preg_replace('/<[^ ]*>/', '', $from)); |
| 122 | if (empty($name)) { |
| 123 | return $from; |
| 124 | } |
| 125 | return $name; |
| 126 | } |
| 127 | |
| 128 | public function getHeaderValue($hdr) |
| 129 | { |
| 130 | $hdr = strtolower($hdr); |
| 131 | if (!isset($this->headers[$hdr])) { |
| 132 | return null; |
| 133 | } |
| 134 | if ($hdr == 'date') { |
| 135 | return strtotime($this->headers['date']); |
| 136 | } else { |
| 137 | return $this->headers[$hdr]; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | public function getHeaders() |
| 142 | { |
| 143 | $this->msg_headers = array_merge($this->msg_headers, Banana::$msgedit_headers, Banana::$profile['headers']); |
| 144 | $headers = array_map(array($this, 'encodeHeader'), $this->msg_headers); |
| 145 | return array_merge($headers, parent::getHeaders()); |
| 146 | } |
| 147 | |
| 148 | static public function formatFrom($text) |
| 149 | { |
| 150 | # From: mark@cbosgd.ATT.COM |
| 151 | # From: <mark@cbosgd.ATT.COM> |
| 152 | # From: mark@cbosgd.ATT.COM (Mark Horton) |
| 153 | # From: Mark Horton <mark@cbosgd.ATT.COM> |
| 154 | $mailto = '<a href="mailto:'; |
| 155 | |
| 156 | $result = banana_htmlentities($text); |
| 157 | if (preg_match("/^([^ ]+@[^ ]+)$/", $text, $regs)) { |
| 158 | $result = $mailto . $regs[1] . '">' . banana_htmlentities($regs[1]) . '</a>'; |
| 159 | } |
| 160 | if (preg_match("/^<(.+@.+)>$/", $text, $regs)) { |
| 161 | $result = $mailto . $regs[1] . '">' . banana_htmlentities($regs[1]) . '</a>'; |
| 162 | } |
| 163 | if (preg_match("/^([^ ]+@[^ ]+) \((.*)\)$/", $text, $regs)) { |
| 164 | $result = $mailto . $regs[1] . '">' . banana_htmlentities($regs[2]) . '</a>'; |
| 165 | } |
| 166 | if (preg_match("/^\"?([^<>\"]+)\"? +<(.+@.+)>$/", $text, $regs)) { |
| 167 | $nom = preg_replace("/^'(.*)'$/", '\1', $regs[1]); |
| 168 | $nom = stripslashes($nom); |
| 169 | $result = $mailto . $regs[2] . '">' . banana_htmlentities($nom) . '</a>'; |
| 170 | } |
| 171 | return preg_replace("/\\\(\(|\))/","\\1",$result); |
| 172 | } |
| 173 | |
| 174 | static public function formatDate($text) |
| 175 | { |
| 176 | return utf8_encode(strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($text))); |
| 177 | } |
| 178 | |
| 179 | public function translateHeaders() |
| 180 | { |
| 181 | $result = array(); |
| 182 | foreach (array_keys($this->headers) as $name) { |
| 183 | $value = $this->translateHeaderValue($name); |
| 184 | if (!is_null($value)) { |
| 185 | $result[$this->translateHeaderName($name)] = $value; |
| 186 | } |
| 187 | } |
| 188 | return $result; |
| 189 | } |
| 190 | |
| 191 | public function getReferences() |
| 192 | { |
| 193 | $text = $this->headers['references']; |
| 194 | $text = str_replace("><","> <", $text); |
| 195 | return preg_split('/\s/', $text); |
| 196 | } |
| 197 | |
| 198 | public function getTranslatedReferences() |
| 199 | { |
| 200 | return BananaMessage::formatReferences($this->headers); |
| 201 | } |
| 202 | |
| 203 | static public function formatReferences(array &$refs) |
| 204 | { |
| 205 | if (isset($refs['references'])) { |
| 206 | $text = str_replace('><', '> <', $refs['references']); |
| 207 | return preg_split('/\s/', strtr($text, Banana::$spool->ids)); |
| 208 | } elseif (isset($refs['in-reply-to'])) { |
| 209 | return array(Banana::$spool->ids[$refs['in-reply-to']]); |
| 210 | } else { |
| 211 | return array(); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | public function hasXFace() |
| 216 | { |
| 217 | return Banana::$msgshow_xface && isset($this->headers['x-face']); |
| 218 | } |
| 219 | |
| 220 | public function getXFace() |
| 221 | { |
| 222 | header('Content-Type: image/gif'); |
| 223 | $xface = $this->headers['x-face']; |
| 224 | passthru('echo ' . escapeshellarg($xface) |
| 225 | . '| uncompface -X ' |
| 226 | . '| convert -transparent white xbm:- gif:-'); |
| 227 | exit; |
| 228 | } |
| 229 | |
| 230 | public function getFormattedBody($type = null) |
| 231 | { |
| 232 | $types = Banana::$msgshow_mimeparts; |
| 233 | if (!is_null($type)) { |
| 234 | array_unshift($types, $type); |
| 235 | } |
| 236 | foreach ($types as $type) { |
| 237 | @list($type, $subtype) = explode('/', $type); |
| 238 | $parts = $this->getParts($type, $subtype); |
| 239 | if (empty($parts)) { |
| 240 | continue; |
| 241 | } |
| 242 | return $parts[0]->toHtml(); |
| 243 | } |
| 244 | return null; |
| 245 | } |
| 246 | |
| 247 | public function quote() |
| 248 | { |
| 249 | foreach (Banana::$msgedit_mimeparts as $type) { |
| 250 | @list($type, $subtype) = explode('/', $type); |
| 251 | $parts = $this->getParts($type, $subtype); |
| 252 | if (empty($parts)) { |
| 253 | continue; |
| 254 | } |
| 255 | if ($parts[0] === $this) { |
| 256 | return parent::quote(); |
| 257 | } |
| 258 | return $parts[0]->quote(); |
| 259 | } |
| 260 | return null; |
| 261 | } |
| 262 | |
| 263 | public function canCancel() |
| 264 | { |
| 265 | if (!Banana::$protocole->canCancel()) { |
| 266 | return false; |
| 267 | } |
| 268 | if (function_exists('hook_checkcancel')) { |
| 269 | return hook_checkcancel($this->headers); |
| 270 | } |
| 271 | return Banana::$profile['name'] == $this->headers['from']; |
| 272 | } |
| 273 | |
| 274 | public function canSend() |
| 275 | { |
| 276 | return Banana::$protocole->canSend(); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | // vim:set et sw=4 sts=4 ts=4 enc=utf-8: |
| 281 | ?> |