1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16: class IMP_Ui_Mailbox
17: {
18: const DATE_FORCE = 1;
19: const DATE_FULL = 2;
20:
21: 22: 23: 24: 25:
26: private $_mailbox;
27:
28: 29: 30: 31: 32:
33: private $_cache = array();
34:
35: 36: 37: 38: 39:
40: public function __construct($mailbox = null)
41: {
42: $this->_mailbox = $mailbox;
43: }
44:
45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62:
63: public function getFrom($ob, $options = array())
64: {
65: $ret = array('error' => false, 'to' => false);
66:
67: if (empty($ob->from)) {
68: $ret['from'] = $ret['fullfrom'] = _("Invalid Address");
69: $ret['error'] = true;
70: return $ret;
71: }
72:
73: if (!isset($this->_cache['drafts_sm_folder'])) {
74: $this->_cache['drafts_sm_folder'] = $this->_mailbox->special_outgoing;
75: }
76:
77: $from = Horde_Mime_Address::getAddressesFromObject($ob->from, array('charset' => 'UTF-8'));
78: $from = reset($from);
79:
80: if (!empty($from) && !isset($from['inner'])) {
81: $from = (isset($from['addresses']) && count($from['addresses']))
82: ? $from['addresses'][0]
83: : null;
84: }
85:
86: if (empty($from)) {
87: $ret['from'] = _("Invalid Address");
88: $ret['error'] = true;
89: } else {
90: if ($GLOBALS['injector']->getInstance('IMP_Identity')->hasAddress($from['inner'])) {
91: 92:
93: if (empty($ob->to)) {
94: $ret['from'] = _("Undisclosed Recipients");
95: $ret['error'] = true;
96: } else {
97: $to = Horde_Mime_Address::getAddressesFromObject($ob->to, array('charset' => 'UTF-8'));
98: $first_to = reset($to);
99: if (empty($first_to)) {
100: $ret['from'] = _("Undisclosed Recipients");
101: $ret['error'] = true;
102: } else {
103: $ret['from'] = empty($first_to['personal'])
104: ? $first_to['inner']
105: : $first_to['personal'];
106: if (!empty($options['fullfrom'])) {
107: $ret['fullfrom'] = $first_to['display'];
108: }
109: }
110: }
111: if (!$this->_cache['drafts_sm_folder']) {
112: $ret['from'] = _("To") . ': ' . $ret['from'];
113: }
114: $ret['to'] = true;
115: } else {
116: $ret['from'] = empty($from['personal'])
117: ? $from['inner']
118: : $from['personal'];
119: if ($this->_cache['drafts_sm_folder']) {
120: $ret['from'] = _("From") . ': ' . $ret['from'];
121: }
122: if (!empty($options['fullfrom'])) {
123: $ret['fullfrom'] = $from['display'];
124: }
125: }
126: }
127:
128: if (!empty($options['fullfrom']) && !isset($ret['fullfrom'])) {
129: $ret['fullfrom'] = $ret['from'];
130: }
131:
132: if (!empty($ret['from']) && !empty($options['specialchars'])) {
133: $res = @htmlspecialchars($ret['from'], ENT_QUOTES, $options['specialchars']);
134: if (empty($res)) {
135: $res = @htmlspecialchars($ret['from']);
136: }
137: $ret['from'] = $res;
138: }
139:
140: return $ret;
141: }
142:
143: 144: 145: 146: 147: 148: 149:
150: public function getSize($size)
151: {
152: return ($size >= 1048576)
153: ? sprintf(_("%s MB"), IMP::numberFormat($size / 1048576, 1))
154: : sprintf(_("%s KB"), IMP::numberFormat($size / 1024, 0));
155: }
156:
157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169:
170: public function getDate($date, $format = 0)
171: {
172: if (!is_object($date)) {
173: $date = new Horde_Imap_Client_DateTime($date);
174: }
175:
176: if (!($format & self::DATE_FORCE) &&
177: !isset($this->_cache['today_start'])) {
178: $this->_cache['today_start'] = new DateTime('today');
179: $this->_cache['today_end'] = new DateTime('today + 1 day');
180: }
181:
182: $udate = null;
183: if (!$date->error()) {
184: try {
185: $udate = $date->format('U');
186: } catch (Exception $e) {}
187: }
188:
189: if (is_null($udate)) {
190: return _("Unknown Date");
191: }
192:
193: if (($format & self::DATE_FORCE) ||
194: ($udate < $this->_cache['today_start']->format('U')) ||
195: ($udate > $this->_cache['today_end']->format('U'))) {
196:
197: if ($format & self::DATE_FULL) {
198: return strftime($GLOBALS['prefs']->getValue('date_format'), $udate) .
199: ' [' . strftime($GLOBALS['prefs']->getValue('time_format'), $udate) . ']';
200: }
201:
202: return strftime($GLOBALS['prefs']->getValue('date_format_mini'), $udate);
203: }
204:
205:
206: return strftime($GLOBALS['prefs']->getValue('time_format'), $udate);
207: }
208:
209: 210: 211: 212: 213: 214: 215: 216:
217: public function getSubject($subject, $htmlspaces = false)
218: {
219: $subject = Horde_Mime::decode($subject, 'UTF-8');
220: if (empty($subject)) {
221: return _("[No Subject]");
222: }
223:
224: $new_subject = $subject = IMP::filterText(preg_replace("/\s+/", ' ', $subject));
225:
226: if ($htmlspaces) {
227: $new_subject = $GLOBALS['injector']->getInstance('Horde_Core_Factory_TextFilter')->filter($subject, 'space2html', array('encode' => true));
228: if (empty($new_subject)) {
229: $new_subject = htmlspecialchars($subject);
230: }
231: }
232:
233: return empty($new_subject) ? $subject : $new_subject;
234: }
235:
236: }
237: