1: <?php
2: /**
3: * A Horde_Injector:: based factory for creating Horde_Ldap objects.
4: *
5: * PHP version 5
6: *
7: * @category Horde
8: * @package Core
9: * @author Michael Slusarz <slusarz@horde.org>
10: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
11: * @link http://pear.horde.org/index.php?package=Core
12: */
13:
14: /**
15: * A Horde_Injector:: based factory for creating Horde_Ldap objects.
16: *
17: * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
18: *
19: * See the enclosed file COPYING for license information (LGPL). If you
20: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
21: *
22: * @category Horde
23: * @package Core
24: * @author Michael Slusarz <slusarz@horde.org>
25: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
26: * @link http://pear.horde.org/index.php?package=Core
27: */
28: class Horde_Core_Factory_Ldap extends Horde_Core_Factory_Base
29: {
30: /**
31: * Instances.
32: *
33: * @var array
34: */
35: private $_instances = array();
36:
37: /**
38: * Return the LDAP instance.
39: *
40: * @param string $app The application.
41: * @param string|array $backend The backend, see Horde::getDriverConfig().
42: * If this is an array, this is used as the
43: * configuration array (@since Horde_Core
44: * 1.5.0).
45: *
46: * @return Horde_Ldap The singleton instance.
47: * @throws Horde_Exception
48: * @throws Horde_Ldap_Exception
49: */
50: public function create($app = 'horde', $backend = null)
51: {
52: $sig = hash('md5', serialize(array($app, $backend)));
53:
54: if (isset($this->_instances[$sig])) {
55: return $this->_instances[$sig];
56: }
57:
58: $pushed = ($app == 'horde')
59: ? false
60: : $GLOBALS['registry']->pushApp($app);
61:
62: $config = is_array($backend)
63: ? $backend
64: : $this->getConfig($backend);
65:
66: /* BC check for old configuration without 'user' setting, so that
67: * administrators can still log in through LDAP and update the
68: * configuration. */
69: if (!isset($config['user'])) {
70: $config['user'] = $config;
71: }
72: $config['cache'] = $this->_injector->getInstance('Horde_Cache');
73:
74: $e = null;
75: try {
76: $this->_instances[$sig] = new Horde_Ldap($config);
77: if (isset($config['bindas']) && $config['bindas'] == 'user') {
78: $this->_instances[$sig]->bind(
79: $this->_instances[$sig]->findUserDN($GLOBALS['registry']->getAuth()),
80: $GLOBALS['registry']->getAuthCredential('password'));
81: }
82: } catch (Horde_Exception $e) {}
83:
84: if ($pushed) {
85: $GLOBALS['registry']->popApp();
86: }
87:
88: if ($e) {
89: throw $e;
90: }
91:
92: return $this->_instances[$sig];
93: }
94:
95: /**
96: */
97: public function getConfig($backend)
98: {
99: return Horde::getDriverConfig($backend, 'ldap');
100: }
101:
102: }
103: