1: <?php
2: /**
3: * The Horde_Kolab_Session_Base class holds user details retrieved via
4: * LDAP in the current session.
5: *
6: * PHP version 5
7: *
8: * @category Kolab
9: * @package Kolab_Session
10: * @author Gunnar Wrobel <wrobel@pardus.de>
11: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
12: * @link http://pear.horde.org/index.php?package=Kolab_Session
13: */
14:
15: /**
16: * The Horde_Kolab_Session_Base class holds user details retrieved via
17: * LDAP in the current session.
18: *
19: * @todo Rename from Horde_Kolab_Session_Base ->
20: * Horde_Kolab_Session_Ldap at some point.
21: *
22: * Copyright 2008-2012 Horde LLC (http://www.horde.org/)
23: *
24: * See the enclosed file COPYING for license information (LGPL). If you
25: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
26: *
27: * @category Kolab
28: * @package Kolab_Session
29: * @author Gunnar Wrobel <wrobel@pardus.de>
30: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
31: * @link http://pear.horde.org/index.php?package=Kolab_Session
32: */
33: class Horde_Kolab_Session_Base extends Horde_Kolab_Session_Abstract
34: {
35: /**
36: * Kolab configuration parameters.
37: *
38: * @var array
39: */
40: private $_params;
41:
42: /**
43: * The kolab user database connection.
44: *
45: * @var Horde_Kolab_Server
46: */
47: private $_server;
48:
49: /**
50: * Constructor.
51: *
52: * @param Horde_Kolab_Server $server The connection to the Kolab user
53: * database.
54: * @param array $params Kolab configuration settings.
55: */
56: public function __construct(
57: Horde_Kolab_Server_Composite $server,
58: array $params
59: )
60: {
61: $this->_server = $server;
62: $this->_params = $params;
63: }
64:
65: /**
66: * Try to connect the session handler.
67: *
68: * @param string $user_id The user ID to connect with.
69: * @param array $credentials An array of login credentials. For Kolab,
70: * this must contain a "password" entry.
71: *
72: * @return NULL
73: *
74: * @throws Horde_Kolab_Session_Exception If the connection failed.
75: */
76: public function connect($user_id = null, array $credentials = null)
77: {
78: $this->_data['user']['id'] = $user_id;
79: if (isset($credentials['password'])) {
80: $password = $credentials['password'];
81: } else {
82: $password = '';
83: }
84:
85: try {
86: $this->_server->connect($this->_data['user']['id'], $password);
87: $user_object = $this->_server->objects->fetch();
88: } catch (Horde_Kolab_Server_Exception_Bindfailed $e) {
89: throw new Horde_Kolab_Session_Exception_Badlogin('Invalid credentials!', 0, $e);
90: } catch (Horde_Kolab_Server_Exception $e) {
91: throw new Horde_Kolab_Session_Exception('Login failed!', 0, $e);
92: }
93:
94: $this->_initMail($user_object);
95: $this->_initUid($user_object);
96: $this->_initName($user_object);
97: $this->_initImapServer($user_object);
98: $this->_initFreebusyServer($user_object);
99: }
100:
101: /**
102: * Initialize the user mail address.
103: *
104: * @param Horde_Kolab_Server_Object $user The user object.
105: *
106: * @return NULL
107: */
108: private function _initMail(Horde_Kolab_Server_Object_Hash $user)
109: {
110: try {
111: $this->_data['user']['mail'] = $user->getSingle('mail');;
112: } catch (Horde_Kolab_Server_Exception_Novalue $e) {
113: $this->_data['user']['mail'] = $this->_data['user']['id'];
114: }
115: }
116:
117: /**
118: * Initialize the user uid.
119: *
120: * @param Horde_Kolab_Server_Object $user The user object.
121: *
122: * @return NULL
123: */
124: private function _initUid(Horde_Kolab_Server_Object_Hash $user)
125: {
126: try {
127: $this->_data['user']['uid'] = $user->getSingle('uid');
128: } catch (Horde_Kolab_Server_Exception_Novalue $e) {
129: $this->_data['user']['uid'] = $this->_data['user']['id'];
130: }
131: }
132:
133: /**
134: * Initialize the user name.
135: *
136: * @param Horde_Kolab_Server_Object $user The user object.
137: *
138: * @return NULL
139: */
140: private function _initName(Horde_Kolab_Server_Object_Hash $user)
141: {
142: try {
143: $this->_data['user']['name'] = $user->getSingle('Firstnamelastname');
144: } catch (Horde_Kolab_Server_Exception_Novalue $e) {
145: $this->_data['user']['name'] = $this->_data['user']['id'];
146: }
147: }
148:
149: /**
150: * Initialize the users imap server FQDN.
151: *
152: * @param Horde_Kolab_Server_Object $user The user object.
153: *
154: * @return NULL
155: */
156: private function _initImapServer(Horde_Kolab_Server_Object_Hash $user)
157: {
158: try {
159: $this->_data['imap']['server'] = $user->getSingle('kolabHomeServer');
160: } catch (Horde_Kolab_Server_Exception_Novalue $e) {
161: if (isset($this->_params['imap']['server'])) {
162: $this->_data['imap']['server'] = $this->_params['imap']['server'];
163: } else {
164: $this->_data['imap']['server'] = 'localhost';
165: }
166: }
167: }
168:
169: /**
170: * Initialize the users free/busy URL.
171: *
172: * @param Horde_Kolab_Server_Object $user The user object.
173: *
174: * @return NULL
175: */
176: private function _initFreebusyServer(Horde_Kolab_Server_Object_Hash $user)
177: {
178: try {
179: $fb_server = $user->getSingle('kolabFreebusyHost');
180: } catch (Horde_Kolab_Server_Exception_Novalue $e) {
181: if (isset($this->_params['freebusy']['url'])) {
182: $this->_data['fb']['server'] = $this->_params['freebusy']['url'];
183: return;
184: } else {
185: $fb_server = $this->_data['imap']['server'];
186: }
187: }
188:
189: if (isset($this->_params['freebusy']['url_format'])) {
190: $fb_format = $this->_params['freebusy']['url_format'];
191: } else {
192: $fb_format = 'http://%s/freebusy';
193: }
194:
195: $this->_data['fb']['server'] = sprintf($fb_format, $fb_server);
196: }
197: }
198: