1: <?php
2: /**
3: * The Horde_Kolab_Session_Imap class relies on predefined Kolab user
4: * details and validates the credentials against the IMAP server only.
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_Imap class relies on predefined Kolab user
17: * details and validates the credentials against the IMAP server only.
18: *
19: * @todo Rename from Horde_Kolab_Session_Base ->
20: * Horde_Kolab_Session_Ldap at some point.
21: *
22: * Copyright 2011-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_Imap extends Horde_Kolab_Session_Abstract
34: {
35: /**
36: * Kolab configuration parameters.
37: *
38: * @var array
39: */
40: private $_params;
41:
42: /**
43: * The imap driver factory..
44: *
45: * @var Horde_Kolab_Session_Factory_Imap
46: */
47: private $_imap;
48:
49: /**
50: * Constructor.
51: *
52: * @param Horde_Kolab_Session_Factory_Imap $imap The imap driver factory.
53: * @param array $params Kolab configuration
54: * settings.
55: */
56: public function __construct(
57: Horde_Kolab_Session_Factory_Imap $imap,
58: array $params
59: )
60: {
61: $this->_imap = $imap;
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: $user_list = array();
86: foreach (array_keys($this->_params['users']) as $user) {
87: $user_list[$user] = $user;
88: }
89: foreach ($this->_params['users'] as $user => $details) {
90: if (isset($details['user']['uid'])) {
91: $user_list[$details['user']['uid']] = $user;
92: }
93: }
94:
95: if (!in_array($user_id, array_keys($user_list))) {
96: throw new Horde_Kolab_Session_Exception_Badlogin('Invalid credentials!', 0);
97: }
98:
99: $details = $this->_params['users'][$user_list[$user_id]];
100: if (!isset($details['imap']['server'])) {
101: if (isset($this->_params['imap']['server'])) {
102: $details['imap']['server'] = $this->_params['imap']['server'];
103: } else {
104: $details['imap']['server'] = 'localhost';
105: }
106: }
107:
108: if (isset($this->_params['imap']['port'])) {
109: $port = $this->_params['imap']['port'];
110: } else {
111: $port = 143;
112: }
113:
114: $imap = $this->_imap->create(
115: array(
116: 'hostspec' => $details['imap']['server'],
117: 'username' => $user_id,
118: 'password' => $password,
119: 'port' => $port,
120: 'secure' => 'tls'
121: )
122: );
123:
124: try {
125: $imap->login();
126: } catch (Horde_Imap_Client_Exception $e) {
127: if ($e->getCode() >= 100 && $e->getCode() < 200) {
128: throw new Horde_Kolab_Session_Exception_Badlogin('Invalid credentials!', 0, $e);
129: } else {
130: throw new Horde_Kolab_Session_Exception('Login failed!', 0, $e);
131: }
132: }
133:
134: $details['user']['id'] = $user_id;
135:
136: if (!isset($details['fb']['server'])) {
137: if (isset($this->_params['freebusy']['url'])) {
138: $details['fb']['server'] = $this->_params['freebusy']['url'];
139: } else {
140: $fb_server = $details['imap']['server'];
141: if (isset($this->_params['freebusy']['url_format'])) {
142: $fb_format = $this->_params['freebusy']['url_format'];
143: } else {
144: $fb_format = 'http://%s/freebusy';
145: }
146: $details['fb']['server'] = sprintf($fb_format, $fb_server);
147: }
148: }
149:
150: $this->_data = $details;
151: }
152: }
153: