1: <?php
2: /**
3: * Copyright 2014 Horde LLC (http://www.horde.org/)
4: *
5: * See the enclosed file COPYING for license information (GPL). If you
6: * did not receive this file, see http://www.horde.org/licenses/gpl.
7: *
8: * @category Horde
9: * @copyright 2014 Horde LLC
10: * @license http://www.horde.org/licenses/gpl GPL
11: * @package IMP
12: */
13:
14: /**
15: * This class manages the attrib_text preference.
16: *
17: * @author Michael Slusarz <slusarz@horde.org>
18: * @category Horde
19: * @copyright 2014 Horde LLC
20: * @license http://www.horde.org/licenses/gpl GPL
21: * @package IMP
22: */
23: class IMP_Prefs_AttribText
24: {
25: /**
26: * Attribution text.
27: *
28: * @var string
29: */
30: protected $_text;
31:
32: /**
33: * Constructor.
34: *
35: * @param string|Horde_Mail_Rfc822_Object $from The email address of the
36: * original sender.
37: * @param Horde_Mime_Headers $h The headers object for
38: * the message.
39: * @param string $attrib Use this for the
40: * attribution config
41: * instead of the default
42: * prefs version.
43: */
44: public function __construct($from, Horde_Mime_Headers $h, $attrib = null)
45: {
46: global $prefs;
47:
48: $this->_text = preg_replace_callback(
49: '/\%./',
50: function ($matches) use ($from, $h) {
51: switch ($matches[0]) {
52: case '%n': /* New line. */
53: return "\n";
54:
55: case '%%': /* Percent character. */
56: return '%';
57:
58: case '%f': /* Name and email address of original sender. */
59: if ($from) {
60: $from = new Horde_Mail_Rfc822_Address($from);
61: return $from->writeAddress(array('noquote' => true));
62: }
63: return _("Unknown Sender");
64:
65: case '%a': /* Senders email address(es). */
66: case '%p': /* Senders name(s). */
67: $out = array();
68: foreach (IMP::parseAddressList($from) as $addr) {
69: if ($matches[0] == '%a') {
70: if (!is_null($addr->mailbox)) {
71: $out[] = $addr->bare_address;
72: }
73: } else {
74: $out[] = $addr->label;
75: }
76: }
77: return count($out)
78: ? implode(', ', $out)
79: : _("Unknown Sender");
80:
81: case '%r': /* RFC 822 date and time. */
82: return $h->getValue('date');
83:
84: case '%d': /* Date as ddd, dd mmm yyyy. */
85: return strftime(
86: "%a, %d %b %Y",
87: strtotime($h->getValue('date'))
88: );
89:
90: case '%c': /* Date and time in locale's default. */
91: case '%x': /* Date in locale's default. */
92: return strftime(
93: $matches[0],
94: strtotime($h->getValue('date'))
95: );
96:
97: case '%m': /* Message-ID. */
98: return is_array($message_id = $h->getValue('message-id'))
99: ? reset($message_id)
100: : $message_id;
101:
102: case '%s': /* Message subject. */
103: return strlen($subject = $h->getValue('subject'))
104: ? $subject
105: : _("[No Subject]");
106:
107: default:
108: return '';
109: }
110: },
111: is_null($attrib) ? $prefs->getValue('attrib_text') : $attrib
112: );
113: }
114:
115: /**
116: */
117: public function __toString()
118: {
119: return $this->_text;
120: }
121:
122: }
123: