PageRenderTime 37ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/horde-3.3.13/lib/Horde/Kolab/Session.php

#
PHP | 391 lines | 200 code | 44 blank | 147 comment | 69 complexity | aed3d0430c99b8c42e8e05f4cdb41e61 MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /**
  3. * The Horde_Kolab_Session class holds additional user details for the current
  4. * session.
  5. *
  6. * $Horde: framework/Kolab_Server/lib/Horde/Kolab/Session.php,v 1.1.2.12 2011/05/30 09:21:21 wrobel Exp $
  7. *
  8. * PHP version 4
  9. *
  10. * @category Kolab
  11. * @package Kolab_Server
  12. * @author Gunnar Wrobel <wrobel@pardus.de>
  13. * @license http://www.fsf.org/copyleft/lgpl.html LGPL
  14. * @link http://pear.horde.org/index.php?package=Kolab_Server
  15. */
  16. /** We need the Auth library */
  17. require_once 'Horde/Auth.php';
  18. /**
  19. * The Horde_Kolab_Session class holds additional user details for the current
  20. * session.
  21. *
  22. * The core user credentials (login, pass) are kept within the Auth module and
  23. * can be retrieved using <code>Auth::getAuth()</code> respectively
  24. * <code>Auth::getCredential('password')</code>. Any additional Kolab user data
  25. * relevant for the user session should be accessed via the Horde_Kolab_Session
  26. * class.
  27. *
  28. * $Horde: framework/Kolab_Server/lib/Horde/Kolab/Session.php,v 1.1.2.12 2011/05/30 09:21:21 wrobel Exp $
  29. *
  30. * Copyright 2008-2009 The Horde Project (http://www.horde.org/)
  31. *
  32. * See the enclosed file COPYING for license information (LGPL). If you
  33. * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  34. *
  35. * @category Kolab
  36. * @package Kolab_Server
  37. * @author Gunnar Wrobel <wrobel@pardus.de>
  38. * @license http://www.fsf.org/copyleft/lgpl.html LGPL
  39. * @link http://pear.horde.org/index.php?package=Kolab_Server
  40. */
  41. class Horde_Kolab_Session {
  42. /**
  43. * User ID.
  44. *
  45. * @var string
  46. */
  47. var $user_id;
  48. /**
  49. * User UID.
  50. *
  51. * @var string
  52. */
  53. var $user_uid;
  54. /**
  55. * Primary user mail address.
  56. *
  57. * @var string
  58. */
  59. var $user_mail;
  60. /**
  61. * Full name.
  62. *
  63. * @var string
  64. */
  65. var $user_name = '';
  66. /**
  67. * True if the Kolab_Server login was successfull.
  68. *
  69. * @var boolean|PEAR_Error
  70. */
  71. var $auth;
  72. /**
  73. * The connection parameters for the IMAP server.
  74. *
  75. * @var array|PEAR_Error
  76. */
  77. var $_imap_params;
  78. /**
  79. * Our IMAP connection.
  80. *
  81. * @var Horde_Kolab_IMAP
  82. */
  83. var $_imap;
  84. /**
  85. * The free/busy server for the current user.
  86. *
  87. * @var array|PEAR_Error
  88. */
  89. var $freebusy_server;
  90. /**
  91. * Constructor.
  92. *
  93. * @param string $user The session will be setup for the user with
  94. * this ID.
  95. * @param array $credentials An array of login credentials. For Kolab,
  96. * this must contain a "password" entry.
  97. */
  98. function Horde_Kolab_Session($user = null, $credentials = null)
  99. {
  100. global $conf;
  101. if (empty($user)) {
  102. $user = Auth::getAuth();
  103. if (empty($user)) {
  104. $user = 'anonymous';
  105. } else if (!strpos($user, '@')) {
  106. $user = $user . '@' . (!empty($_SERVER['SERVER_NAME']) ?
  107. $_SERVER['SERVER_NAME'] : 'localhost');
  108. }
  109. }
  110. $this->user_id = $user;
  111. $this->_imap_params = array();
  112. $user_object = null;
  113. if ($user != 'anonymous') {
  114. $server = $this->getServer($user, $credentials);
  115. if (is_a($server, 'PEAR_Error')) {
  116. $this->auth = $server;
  117. } else {
  118. $this->user_uid = $server->uid;
  119. $user_object = $server->fetch();
  120. if (is_a($user_object, 'PEAR_Error')) {
  121. $this->auth = $user_object;
  122. } else {
  123. if (empty($conf['kolab']['imap']['allow_special_users'])
  124. && !is_a($user_object, 'Horde_Kolab_Server_Object_user')) {
  125. $this->auth = PEAR::raiseError(_('Access to special Kolab users is denied.'));
  126. } else if (isset($conf['kolab']['server']['deny_group'])) {
  127. $dn = $server->gidForMail($conf['kolab']['server']['deny_group']);
  128. if (is_a($dn, 'PEAR_Error')) {
  129. $this->auth = $dn;
  130. } else if (empty($dn)) {
  131. Horde::logMessage('The Kolab configuratin setting $conf[\'kolab\'][\'server\'][\'deny_group\'] holds a non-existing group!',
  132. __FILE__, __LINE__, PEAR_LOG_WARNING);
  133. $this->auth = true;
  134. } else if (in_array($dn, $user_object->getGroups())) {
  135. $this->auth = PEAR::raiseError(_('You are member of a group that may not login on this server.'));
  136. } else {
  137. $this->auth = true;
  138. }
  139. } else if (isset($conf['kolab']['server']['allow_group'])) {
  140. $dn = $server->gidForMail($conf['kolab']['server']['allow_group']);
  141. if (is_a($dn, 'PEAR_Error')) {
  142. $this->auth = $dn;
  143. } else if (empty($dn)) {
  144. Horde::logMessage('The Kolab configuratin setting $conf[\'kolab\'][\'server\'][\'allow_group\'] holds a non-existing group!',
  145. __FILE__, __LINE__, PEAR_LOG_WARNING);
  146. $this->auth = true;
  147. } else if (!in_array($dn, $user_object->getGroups())) {
  148. $this->auth = PEAR::raiseError(_('You are no member of a group that may login on this server.'));
  149. } else {
  150. $this->auth = true;
  151. }
  152. } else {
  153. /**
  154. * At this point we can be certain the user is an
  155. * authenticated Kolab user.
  156. */
  157. $this->auth = true;
  158. }
  159. if (empty($this->auth) || is_a($this->auth, 'PEAR_Error')) {
  160. return;
  161. }
  162. $result = $user_object->get(KOLAB_ATTR_MAIL);
  163. if (!empty($result) && !is_a($result, 'PEAR_Error')) {
  164. $this->user_mail = $result;
  165. }
  166. $result = $user_object->get(KOLAB_ATTR_SID);
  167. if (!empty($result) && !is_a($result, 'PEAR_Error')) {
  168. $this->user_id = $result;
  169. }
  170. $result = $user_object->get(KOLAB_ATTR_FNLN);
  171. if (!empty($result) && !is_a($result, 'PEAR_Error')) {
  172. $this->user_name = $result;
  173. }
  174. $result = $user_object->getServer('imap');
  175. if (!empty($result) && !is_a($result, 'PEAR_Error')) {
  176. $server = explode(':', $result, 2);
  177. if (!empty($server[0])) {
  178. $this->_imap_params['hostspec'] = $server[0];
  179. }
  180. if (!empty($server[1])) {
  181. $this->_imap_params['port'] = $server[1];
  182. }
  183. }
  184. $result = $user_object->getServer('freebusy');
  185. if (!empty($result) && !is_a($result, 'PEAR_Error')) {
  186. $this->freebusy_server = $result;
  187. }
  188. }
  189. }
  190. }
  191. if (empty($this->user_mail)) {
  192. $this->user_mail = $user;
  193. }
  194. if (!isset($this->_imap_params['hostspec'])) {
  195. if (isset($conf['kolab']['imap']['server'])) {
  196. $this->_imap_params['hostspec'] = $conf['kolab']['imap']['server'];
  197. } else {
  198. $this->_imap_params['hostspec'] = 'localhost';
  199. }
  200. }
  201. if (!isset($this->_imap_params['port'])) {
  202. if (isset($conf['kolab']['imap']['port'])) {
  203. $this->_imap_params['port'] = $conf['kolab']['imap']['port'];
  204. } else {
  205. $this->_imap_params['port'] = 143;
  206. }
  207. }
  208. if (isset($conf['kolab']['imap']['secure'])) {
  209. $this->_imap_params['secure'] = $conf['kolab']['imap']['secure'];
  210. } else {
  211. $this->_imap_params['secure'] = false;
  212. }
  213. $this->_imap_params['protocol'] = 'imap/notls/novalidate-cert';
  214. }
  215. /**
  216. * Returns the properties that need to be serialized.
  217. *
  218. * @return array List of serializable properties.
  219. */
  220. function __sleep()
  221. {
  222. $properties = get_object_vars($this);
  223. unset($properties['_imap']);
  224. $properties = array_keys($properties);
  225. return $properties;
  226. }
  227. /**
  228. * Get the Kolab Server connection.
  229. *
  230. * @param string $user The session will be setup for the user with
  231. * this ID.
  232. * @param array $credentials An array of login credentials. For Kolab,
  233. * this must contain a "password" entry.
  234. *
  235. * @return Horde_Kolab_Server|PEAR_Error The Kolab Server connection.
  236. */
  237. function &getServer($user = null, $credentials = null)
  238. {
  239. /** We need the Kolab Server access. */
  240. require_once 'Horde/Kolab/Server.php';
  241. $params = array();
  242. if ($this->user_uid) {
  243. $params['uid'] = $this->user_uid;
  244. $params['pass'] = Auth::getCredential('password');
  245. } else if (isset($user)) {
  246. $params['user'] = $user;
  247. if (isset($credentials['password'])) {
  248. $params['pass'] = $credentials['password'];
  249. } else {
  250. $params['pass'] = Auth::getCredential('password');
  251. }
  252. }
  253. return Horde_Kolab_Server::singleton($params);
  254. }
  255. /**
  256. * Get the IMAP connection parameters.
  257. *
  258. * @return array|PEAR_Error The IMAP connection parameters.
  259. */
  260. function &getImapParams()
  261. {
  262. return $this->_imap_params;
  263. }
  264. /**
  265. * Create an IMAP connection.
  266. *
  267. * @return Horde_Kolab_IMAP|PEAR_Error The IMAP connection.
  268. */
  269. function &getImap()
  270. {
  271. if (!isset($this->_imap)) {
  272. $params = $this->getImapParams();
  273. if (is_a($params, 'PEAR_Error')) {
  274. return $params;
  275. }
  276. /** We need the Kolab IMAP library now. */
  277. require_once 'Horde/Kolab/IMAP.php';
  278. $imap = &Horde_Kolab_IMAP::singleton($params['hostspec'],
  279. $params['port'], true, false);
  280. if (is_a($imap, 'PEAR_Error')) {
  281. return $imap;
  282. }
  283. $result = $imap->connect(Auth::getAuth(),
  284. Auth::getCredential('password'),
  285. !empty($params['secure']));
  286. if (is_a($result, 'PEAR_Error')) {
  287. return $result;
  288. }
  289. $this->_imap = &$imap;
  290. }
  291. return $this->_imap;
  292. }
  293. /**
  294. * Attempts to return a reference to a concrete Horde_Kolab_Session instance.
  295. *
  296. * It will only create a new instance if no Horde_Kolab_Session instance
  297. * currently exists or if a user ID has been specified that does not match the
  298. * user ID/user mail of the current session.
  299. *
  300. * This method must be invoked as:
  301. * <code>$var = &Horde_Kolab_Session::singleton();</code>
  302. *
  303. * @param string $user The session will be setup for the user with
  304. * this ID.
  305. * @param array $credentials An array of login credentials. For Kolab,
  306. * this must contain a "password" entry.
  307. *
  308. * @static
  309. *
  310. * @return Horde_Kolab_Session The concrete Session reference.
  311. */
  312. function &singleton($user = null, $credentials = null, $destruct = false)
  313. {
  314. static $session;
  315. if (!isset($session)) {
  316. /**
  317. * Horde_Kolab_Server currently has no caching so we mainly
  318. * cache some user information here as reading this data
  319. * may be expensive when running in a multi-host
  320. * environment.
  321. */
  322. require_once 'Horde/SessionObjects.php';
  323. $hs = &Horde_SessionObjects::singleton();
  324. $session = $hs->query('kolab_session');
  325. }
  326. if (empty($user)) {
  327. $user = Auth::getAuth();
  328. }
  329. if ($destruct || empty($session)
  330. || ($user != $session->user_mail && $user != $session->user_id)) {
  331. $session = new Horde_Kolab_Session($user, $credentials);
  332. }
  333. register_shutdown_function(array(&$session, 'shutdown'));
  334. return $session;
  335. }
  336. /**
  337. * Stores the object in the session cache.
  338. *
  339. * @return NULL
  340. */
  341. function shutdown()
  342. {
  343. require_once 'Horde/SessionObjects.php';
  344. $session = &Horde_SessionObjects::singleton();
  345. $session->overwrite('kolab_session', $this, false);
  346. }
  347. }