1: <?php
2: /**
3: * Copyright 2013-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 2010-2014 Horde LLC
10: * @license http://www.horde.org/licenses/gpl GPL
11: * @package IMP
12: */
13:
14: /**
15: * A Horde_Injector based IMP_Spam factory.
16: *
17: * @author Michael Slusarz <slusarz@horde.org>
18: * @category Horde
19: * @copyright 2013-2014 Horde LLC
20: * @license http://www.horde.org/licenses/gpl GPL
21: * @package IMP
22: */
23: class IMP_Factory_Spam extends Horde_Core_Factory_Base
24: {
25: /**
26: * Instances.
27: *
28: * @var array
29: */
30: private $_instances = array();
31:
32: /**
33: * Return a IMP_Spam instance.
34: *
35: * @param integer $action Either IMP_Spam::SPAM or IMP_Spam::INNOCENT.
36: *
37: * @return IMP_Spam The spam instance.
38: * @throws IMP_Exception
39: */
40: public function create($action)
41: {
42: if (!isset($this->_instances[$action])) {
43: switch ($action) {
44: case IMP_Spam::INNOCENT:
45: $config = $this->_injector->getInstance('IMP_Factory_Imap')->create()->config->innocent_params;
46: break;
47:
48: case IMP_Spam::SPAM:
49: $config = $this->_injector->getInstance('IMP_Factory_Imap')->create()->config->spam_params;
50: break;
51: }
52:
53: $drivers = (!empty($config['drivers']) && is_array($config['drivers']))
54: ? $config['drivers']
55: : array();
56:
57: if (!empty($config['program'])) {
58: $drivers[] = new IMP_Spam_Program(
59: $this->_expand($config['program'], true)
60: );
61: }
62:
63: if (!empty($config['email'])) {
64: $drivers[] = new IMP_Spam_Email(
65: $this->_expand($config['email']),
66: empty($config['email_format']) ? 'digest' : $config['email_format']
67: );
68: }
69:
70: if (!empty($config['null'])) {
71: $drivers[] = new IMP_Spam_Null($config['null']);
72: }
73:
74: $this->_instances[$action] = new IMP_Spam($action, $drivers);
75: }
76:
77: return $this->_instances[$action];
78: }
79:
80: /**
81: * Expand placeholders in 'email' and 'program' options.
82: *
83: * @param string $str The option.
84: * @param boolean $escape Shell escape the replacements?
85: *
86: * @return string The expanded option.
87: */
88: private function _expand($str, $escape = false)
89: {
90: global $registry;
91:
92: $replace = array(
93: '%u' => $registry->getAuth(),
94: '%l' => $registry->getAuth('bare'),
95: '%d' => $registry->getAuth('domain')
96: );
97:
98: return str_replace(
99: array_keys($replace),
100: $escape ? array_map('escapeshellarg', array_values($replace)) : array_values($replace),
101: $str
102: );
103: }
104:
105: }
106: