1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17:
18: class Imp_Prefs_Identity extends Horde_Core_Prefs_Identity
19: {
20: 21: 22: 23: 24:
25: protected $_cached = array(
26: 'aliases' => array(),
27: 'fromList' => array(),
28: 'froms' => array(),
29: 'names' => array(),
30:
31: 'signatures' => array()
32:
33: );
34:
35: 36: 37: 38: 39:
40: protected $_impPrefs = array(
41: 'replyto_addr', 'alias_addr', 'tieto_addr', 'bcc_addr', 'signature',
42: 'signature_html', 'sig_first', 'sig_dashes', 'save_sent_mail',
43: 'sent_mail_folder'
44: );
45:
46: 47: 48: 49: 50: 51:
52: public function __construct($params)
53: {
54: $this->_prefnames['properties'] = array_merge(
55: $this->_prefnames['properties'],
56: $this->_impPrefs
57: );
58:
59: parent::__construct($params);
60: }
61:
62: 63: 64: 65: 66: 67: 68:
69: public function verify($identity = null)
70: {
71: if (!isset($identity)) {
72: $identity = $this->_default;
73: }
74:
75:
76: foreach ($this->_impPrefs as $pref) {
77: if (!isset($this->_identities[$identity][$pref])) {
78: $this->_identities[$identity][$pref] = $this->_prefs->getValue($pref);
79: }
80: }
81:
82: parent::verify($identity);
83:
84:
85: require_once 'Horde/Form.php';
86: $email = new Horde_Form_Type_email();
87: $vars = new Horde_Variables();
88: $var = new Horde_Form_Variable('', 'replyto_addr', $email, false);
89:
90:
91: if (!$email->isValid($var, $vars, $this->getValue('replyto_addr', $identity), $error_message)) {
92: throw new Horde_Exception($error_message);
93: }
94:
95:
96: foreach (array('alias_addr', 'tieto_addr', 'bcc_addr') as $val) {
97: $data = $this->getValue($val, $identity);
98: if (is_array($data)) {
99: $data = implode("\n", $data);
100: }
101: $data = trim($data);
102: $data = (empty($data)) ? array() : Horde_Array::prepareAddressList(preg_split("/[\n\r]+/", $data));
103:
104:
105: foreach ($data as $address) {
106: if (!$email->isValid($var, $vars, $address, $error_message)) {
107: throw new Horde_Exception($error_message);
108: }
109: }
110:
111: $this->setValue($val, $data, $identity);
112: }
113: }
114:
115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127:
128: public function getFromLine($ident = null, $from_address = '')
129: {
130: if (isset($this->_cached['froms'][$ident])) {
131: return $this->_cached['froms'][$ident];
132: }
133:
134: if (!isset($ident)) {
135: $address = $from_address;
136: }
137:
138: if (empty($address) || $this->_prefs->isLocked($this->_prefnames['from_addr'])) {
139: $address = $this->getFromAddress($ident);
140: $name = $this->getFullname($ident);
141: }
142:
143: try {
144: $ob = $this->_getAddrList($address);
145: } catch (Horde_Mime_Exception $e) {
146: throw new Horde_Exception (_("Your From address is not a valid email address. This can be fixed in your Personal Information preferences page."));
147: }
148:
149: if (empty($name)) {
150: $name = empty($ob[0]['personal'])
151: ? $this->getFullname($ident)
152: : $ob[0]['personal'];
153: }
154:
155: $from = Horde_Mime_Address::writeAddress($ob[0]['mailbox'], $ob[0]['host'], $name);
156:
157: $this->_cached['froms'][$ident] = $from;
158:
159: return $from;
160: }
161:
162: 163: 164: 165: 166:
167: public function getAllFromLines()
168: {
169: foreach (array_keys($this->_identities) as $ident) {
170: $list[$ident] = $this->getFromAddress($ident);
171: }
172: return $list;
173: }
174:
175: 176: 177: 178: 179: 180:
181: public function getSelectList()
182: {
183: $ids = $this->getAll($this->_prefnames['id']);
184: foreach ($ids as $key => $id) {
185: $list[$key] = $this->getFromAddress($key) . ' (' . $id . ')';
186: }
187: return $list;
188: }
189:
190: 191: 192: 193: 194: 195: 196: 197:
198: public function hasAddress($address)
199: {
200: $list = $this->getAllFromAddresses(true);
201: return isset($list[Horde_String::lower($address)]);
202: }
203:
204: 205: 206: 207: 208: 209: 210: 211: 212:
213: public function getFromAddress($ident = null)
214: {
215: if (!isset($this->_cached['fromList'][$ident])) {
216: $val = $this->getValue($this->_prefnames['from_addr'], $ident);
217: if (empty($val)) {
218: $val = $GLOBALS['registry']->getAuth();
219: }
220:
221: if (!strstr($val, '@')) {
222: $val .= '@' . $GLOBALS['session']->get('imp', 'maildomain');
223: }
224:
225: $this->_cached['fromList'][$ident] = $val;
226: }
227:
228: return $this->_cached['fromList'][$ident];
229: }
230:
231: 232: 233: 234: 235: 236: 237:
238: public function getAliasAddress($ident)
239: {
240: if (!isset($this->_cached['aliases'][$ident])) {
241: $this->_cached['aliases'][$ident] = @array_merge(
242: (array)$this->getValue('alias_addr', $ident),
243: array($this->getValue('replyto_addr', $ident))
244: );
245: }
246:
247: return $this->_cached['aliases'][$ident];
248: }
249:
250: 251: 252: 253: 254: 255: 256: 257: 258:
259: public function getAllFromAddresses($alias = false)
260: {
261: $list = array();
262:
263: foreach ($this->_identitiesWithDefaultLast() as $key => $identity) {
264:
265: $list[Horde_String::lower($this->getFromAddress($key))] = $key;
266:
267:
268: if ($alias) {
269: $addrs = $this->getAliasAddress($key);
270: if (!empty($addrs)) {
271: foreach (array_filter($addrs) as $val) {
272: $list[Horde_String::lower($val)] = $key;
273: }
274: }
275: }
276: }
277:
278: return $list;
279: }
280:
281: 282: 283: 284: 285: 286: 287:
288: public function getAllTieAddresses()
289: {
290: $list = array();
291:
292: foreach ($this->_identitiesWithDefaultLast() as $key => $identity) {
293: $tieaddr = $this->getValue('tieto_addr', $key);
294: if (!empty($tieaddr)) {
295: foreach ($tieaddr as $val) {
296: $list[$val] = $key;
297: }
298: }
299: }
300:
301: return $list;
302: }
303:
304: 305: 306: 307: 308: 309:
310: public function getAllIdentityAddresses()
311: {
312:
313: return array_merge(
314: array_keys($this->getAllFromAddresses(true)),
315: array_keys($this->getAllTieAddresses())
316: );
317: }
318:
319: 320: 321: 322: 323: 324:
325: protected function _identitiesWithDefaultLast()
326: {
327: $ids = $this->_identities;
328: $default = $this->getDefault();
329: $tmp = $ids[$default];
330: unset($ids[$default]);
331: $ids[$default] = $tmp;
332: return $ids;
333: }
334:
335: 336: 337: 338: 339: 340: 341:
342: public function getBccAddresses($ident = null)
343: {
344: $bcc = $this->getValue('bcc_addr', $ident);
345: if (empty($bcc)) {
346: return array();
347: } else {
348: if (!is_array($bcc)) {
349: $bcc = array($bcc);
350: }
351: try {
352: return $this->_getAddrList(implode(', ', $bcc));
353: } catch (Horde_Mime_Exception $e) {
354: return array();
355: }
356: }
357: }
358:
359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370:
371: public function getMatchingIdentity($addresses, $search_own = true)
372: {
373: if (!isset($this->_cached['tie_addresses'])) {
374: $this->_cached['tie_addresses'] = $this->getAllTieAddresses();
375: $this->_cached['own_addresses'] = $this->getAllFromAddresses(true);
376: }
377:
378:
379: $addresses = is_array($addresses)
380: ? array_filter($addresses)
381: : array($addresses);
382:
383: try {
384: $addr_list = $this->_getAddrList(implode(', ', $addresses));
385: } catch (Horde_Mime_Exception $e) {
386: return null;
387: }
388:
389: foreach ($addr_list as $address) {
390: if (empty($address['mailbox'])) {
391: continue;
392: }
393:
394: $find_address = $address['mailbox'];
395: if (!empty($address['host'])) {
396: $find_address .= '@' . $address['host'];
397: }
398: $find_address = Horde_String::lower($find_address);
399:
400:
401:
402: if (isset($this->_cached['tie_addresses'][$find_address])) {
403: return $this->_cached['tie_addresses'][$find_address];
404: }
405:
406:
407: if (!empty($address['host']) &&
408: isset($this->_cached['tie_addresses']['@' . $address['host']])) {
409: return $this->_cached['tie_addresses']['@' . $address['host']];
410: }
411:
412:
413: if ($search_own &&
414: isset($this->_cached['own_addresses'][$find_address])) {
415: return $this->_cached['own_addresses'][$find_address];
416: }
417: }
418:
419: return null;
420: }
421:
422: 423: 424: 425: 426: 427: 428:
429: public function getFullname($ident = null)
430: {
431: if (isset($this->_cached['names'][$ident])) {
432: return $this->_cached['names'][$ident];
433: }
434:
435: $this->_cached['names'][$ident] = $this->getValue($this->_prefnames['fullname'], $ident);
436:
437: return $this->_cached['names'][$ident];
438: }
439:
440: 441: 442: 443: 444: 445: 446: 447: 448: 449:
450: public function getSignature($type = 'text', $ident = null)
451: {
452: $convert = false;
453: $key = $ident . '|' . $type;
454: $val = null;
455:
456: if (isset($this->_cached['signatures'][$key])) {
457: return $this->_cached['signatures'][$key];
458: }
459:
460: if ($type == 'html') {
461: $val = $this->getValue('signature_html', $ident);
462: if (!strlen($val)) {
463: $convert = true;
464: $val = null;
465: }
466: }
467:
468: if (is_null($val)) {
469: $val = $this->getValue('signature', $ident);
470:
471: if (!empty($val) && ($type == 'text')) {
472: $sig_first = $this->getValue('sig_first', $ident);
473: $sig_dashes = $this->getValue('sig_dashes', $ident);
474:
475: $val = str_replace("\r\n", "\n", $val);
476:
477: if ($sig_dashes) {
478: $val = "-- \n" . $val . "\n";
479: } else {
480: $val = "\n" . $val;
481: }
482:
483: if ($sig_first) {
484: $val .= "\n\n\n";
485: }
486: }
487: }
488:
489: if ($val && ($type == 'html')) {
490: if ($convert) {
491: $val = IMP_Compose::text2html(trim($val));
492: }
493:
494: $val = '<p> </p><div class="impComposeSignature">' . $val . '</div>';
495: }
496:
497: try {
498: $val = Horde::callHook('signature', array($val), 'imp');
499: } catch (Horde_Exception_HookNotSet $e) {}
500:
501: $this->_cached['signatures'][$key] = $val;
502:
503: return $val;
504: }
505:
506: 507: 508: 509: 510: 511: 512:
513: public function getAllSignatures($type = 'text')
514: {
515: foreach ($this->_identities as $key => $identity) {
516: $list[$key] = $this->getSignature($type, $key);
517: }
518:
519: return $list;
520: }
521:
522: 523: 524: 525: 526:
527: public function getValue($key, $identity = null)
528: {
529: $val = parent::getValue($key, $identity);
530:
531: switch ($key) {
532: case 'sent_mail_folder':
533: return (is_string($val) && strlen($val))
534: ? IMP_Mailbox::get(IMP_Mailbox::prefFrom($val))
535: : null;
536:
537: default:
538: return $val;
539: }
540: }
541:
542: 543: 544: 545: 546:
547: public function setValue($key, $val, $identity = null)
548: {
549: if ($key == 'sent_mail_folder') {
550: if ($val) {
551: $val->expire(IMP_Mailbox::CACHE_SPECIALMBOXES);
552: } else {
553: IMP_Mailbox::get('INBOX')->expire(IMP_Mailbox::CACHE_SPECIALMBOXES);
554: }
555: $val = IMP_Mailbox::prefTo($val);
556: }
557: return parent::setValue($key, $val, $identity);
558: }
559:
560: 561: 562: 563: 564: 565:
566: public function getAllSentmailFolders()
567: {
568: $list = array();
569:
570: foreach (array_keys($this->_identities) as $key) {
571: if ($folder = $this->getValue('sent_mail_folder', $key)) {
572: $list[strval($folder)] = 1;
573: }
574: }
575:
576: return IMP_Mailbox::get(array_keys($list));
577: }
578:
579: 580: 581: 582: 583: 584: 585:
586: public function saveSentmail($ident = null)
587: {
588: return $GLOBALS['injector']->getInstance('IMP_Factory_Imap')->create()->access(IMP_Imap::ACCESS_FOLDERS)
589: ? $this->getValue('save_sent_mail', $ident)
590: : false;
591: }
592:
593: 594: 595: 596: 597: 598: 599:
600: protected function _getAddrList($in)
601: {
602: return Horde_Mime_Address::parseAddressList($in, array(
603: 'defserver' => $GLOBALS['session']->get('imp', 'maildomain')
604: ));
605: }
606:
607: }
608: