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