1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: class Passwd_Factory_Driver extends Horde_Core_Factory_Base
16: {
17: 18: 19: 20: 21:
22: protected $_backends = array();
23:
24: 25: 26: 27: 28:
29: private $_instances = array();
30:
31: 32: 33: 34: 35: 36: 37: 38: 39: 40:
41: public function create($name, $params = array() )
42: {
43: if (!empty($params['is_subdriver'])) {
44: $backends = array($name => $params);
45: } else {
46: $backends = $this->getBackends();
47: }
48:
49: if (empty($backends[$name])) {
50: throw new Passwd_Exception(sprintf(_("The password backend \"%s\" does not exist."), $name));
51: }
52: $backend = $backends[$name];
53:
54: if (!isset($this->_instances[$name])) {
55: $class = 'Passwd_Driver_' . Horde_String::ucfirst(basename($backend['driver']));
56: if (!class_exists($class)) {
57: throw new Passwd_Exception(sprintf(_("Unable to load the definition of %s."), $class));
58: }
59:
60: if (empty($backend['params'])) {
61: $backend['params'] = array();
62: }
63: if (empty($backend['policy'])) {
64: $backend['policy'] = array();
65: }
66: if (!empty($params)) {
67: $backend['params'] = array_merge($backend['params'], $params);
68: }
69:
70: switch ($class) {
71: case 'Passwd_Driver_Ldap':
72: case 'Passwd_Driver_Smbldap':
73: if (isset($backend['params']['admindn'])) {
74: $backend['params']['binddn'] = $backend['params']['admindn'];
75: }
76: if (isset($backend['params']['adminpw'])) {
77: $backend['params']['bindpw'] = $backend['params']['adminpw'];
78: }
79: if (isset($backend['params']['host'])) {
80: $backend['params']['hostspec'] = $backend['params']['host'];
81: }
82:
83: try {
84: $backend['params']['ldap'] = new Horde_Ldap($backend['params']);
85: } catch (Horde_Ldap_Exception $e) {
86: throw new Passwd_Exception($e);
87: }
88: break;
89:
90: case 'Passwd_Driver_Sql':
91: case 'Passwd_Driver_Vpopmail':
92: if (!($backend['params']['db'] instanceof Horde_Db_Adapter)) {
93: try {
94: if (empty($backend['params'])) {
95: $backend['params']['db'] = $this->_injector
96: ->getInstance('Horde_Db_Adapter');
97: } else {
98: $params = $backend['params'];
99: unset($params['table'], $params['user_col'],
100: $params['pass_col'], $params['encryption'],
101: $params['show_encryption']);
102: $backend['params']['db'] = $this->_injector
103: ->getInstance('Horde_Core_Factory_Db')
104: ->create('passwd', $params);
105: }
106: } catch (Horde_Db_Exception $e) {
107: throw new Passwd_Exception($e);
108: }
109: }
110: break;
111:
112: case 'Passwd_Driver_Horde':
113: $backend['params']['auth'] = $this->_injector
114: ->getInstance('Horde_Core_Factory_Auth')
115: ->create();
116: break;
117:
118: case 'Passwd_Driver_Soap':
119: if (!empty($GLOBALS['conf']['http']['proxy']['proxy_host'])) {
120: $backend['params']['soap_params']['proxy_host'] = $GLOBALS['conf']['http']['proxy']['proxy_host'];
121: $backend['params']['soap_params']['proxy_port'] = $GLOBALS['conf']['http']['proxy']['proxy_port'];
122: $backend['params']['soap_params']['proxy_login'] = $GLOBALS['conf']['http']['proxy']['proxy_user'];
123: $backend['params']['soap_params']['proxy_password'] = $GLOBALS['conf']['http']['proxy']['proxy_pass'];
124: }
125: $backend['params']['soap_params']['encoding'] = 'UTF-8';
126: break;
127:
128:
129: }
130:
131: try {
132: $driver = new $class($backend['params']);
133: } catch (Passwd_Exception $e) {
134: throw $e;
135: } catch (Exception $e) {
136: throw new Passwd_Exception($e);
137: }
138:
139: 140:
141:
142: if (!empty($backend['params']['is_subdriver'])) {
143: return $driver;
144: }
145:
146: $this->_instances[$name] = $driver;
147: }
148:
149: return $this->_instances[$name];
150: }
151:
152: 153: 154: 155: 156: 157: 158:
159: public function setBackends(array $backends)
160: {
161: $this->_backends = $backends;
162: return $this;
163: }
164:
165: 166: 167: 168: 169: 170:
171: public function getBackends()
172: {
173: if (empty($this->_backends)) {
174: throw new Passwd_Exception('No backends have been set before getBackends() was called');
175: }
176: return $this->_backends;
177: }
178: }
179: