PageRenderTime 28ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/auth/imap/lib.php

https://github.com/plymouthstate/mahara
PHP | 290 lines | 193 code | 48 blank | 49 comment | 23 complexity | d96f0c32cafa8a51ab09830ffb7a5aca MD5 | raw file
  1. <?php
  2. /**
  3. * Mahara: Electronic portfolio, weblog, resume builder and social networking
  4. * Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
  5. * http://wiki.mahara.org/Contributors
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @package mahara
  21. * @subpackage auth-internal
  22. * @author Catalyst IT Ltd
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL
  24. * @copyright (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
  25. *
  26. */
  27. defined('INTERNAL') || die();
  28. require_once(get_config('docroot') . 'auth/lib.php');
  29. /**
  30. * The internal authentication method, which authenticates users against the
  31. * Mahara database.
  32. */
  33. class AuthImap extends Auth {
  34. public function __construct($id = null) {
  35. $this->type = 'imap';
  36. $this->has_instance_config = true;
  37. $this->config['host'] = '';
  38. $this->config['port'] = '143';
  39. $this->config['protocol'] = '/imap';
  40. $this->config['changepasswordurl'] = '';
  41. if (!empty($id)) {
  42. return $this->init($id);
  43. }
  44. return true;
  45. }
  46. public function init($id = null) {
  47. $this->ready = parent::init($id);
  48. // Check that required fields are set
  49. if ( empty($this->config['host']) ||
  50. empty($this->config['port']) ||
  51. empty($this->config['protocol']) ) {
  52. $this->ready = false;
  53. }
  54. return $this->ready;
  55. }
  56. /**
  57. * Attempt to authenticate user
  58. *
  59. * @param string $username The username to authenticate with
  60. * @param string $password The password being used for authentication
  61. * @return bool True/False based on whether the user
  62. * authenticated successfully
  63. * @throws AuthUnknownUserException If the user does not exist
  64. */
  65. public function authenticate_user_account($user, $password) {
  66. $this->must_be_ready();
  67. if (! function_exists('imap_open')) {
  68. throw new ConfigException('IMAP is not available in your PHP environment. Check that it is properly installed');
  69. }
  70. $connectionstring = '{'.
  71. $this->config['host']
  72. .':'.
  73. $this->config['port']
  74. .
  75. $this->config['protocol']
  76. .'}';
  77. try {
  78. $connection = imap_open($connectionstring, $user->username, $password, OP_HALFOPEN);
  79. if ($connection) {
  80. imap_close($connection);
  81. return true;
  82. }
  83. } catch (Exception $e) {
  84. throw new ConfigException('Unable to connect to server with connection string: '.$connectionstring);
  85. }
  86. return false; // No match
  87. }
  88. /**
  89. * Imap doesn't export enough information to be able to auto-create users
  90. */
  91. public function can_auto_create_users() {
  92. return false;
  93. }
  94. }
  95. /**
  96. * Plugin configuration class
  97. */
  98. class PluginAuthImap extends PluginAuth {
  99. private static $default_config = array('host'=>'', 'port'=>'143', 'protocol'=>'/imap','changepasswordurl'=>'');
  100. public static function has_config() {
  101. return false;
  102. }
  103. public static function get_config_options() {
  104. return array();
  105. }
  106. public static function has_instance_config() {
  107. return true;
  108. }
  109. public static function is_usable() {
  110. return extension_loaded('imap');
  111. }
  112. public static function get_instance_config_options($institution, $instance = 0) {
  113. // TODO: put these strings in a lang file
  114. $options['/imap'] = 'IMAP';
  115. $options['/imap/ssl'] = 'IMAP/SSL';
  116. $options['/imap/ssl/novalidate-cert'] = 'IMAP/SSL (self-signed certificate)';
  117. $options['/imap/tls'] = 'IMAP/TLS';
  118. if ($instance > 0) {
  119. $current = get_records_array('auth_instance', 'id', $instance, 'priority ASC');
  120. if ($current == false) {
  121. throw new SystemException('Could not find data for auth instance '.$instance);
  122. }
  123. $default = $current[0];
  124. $current_config = get_records_menu('auth_instance_config', 'instance', $instance, '', 'field, value');
  125. if ($current_config == false) {
  126. $current_config = array();
  127. }
  128. foreach (self::$default_config as $key => $value) {
  129. if (array_key_exists($key, $current_config)) {
  130. self::$default_config[$key] = $current_config[$key];
  131. }
  132. }
  133. } else {
  134. $default = new stdClass();
  135. $default->instancename = '';
  136. }
  137. $elements['instancename'] = array(
  138. 'type' => 'text',
  139. 'title' => get_string('authname','auth'),
  140. 'rules' => array(
  141. 'required' => true
  142. ),
  143. 'defaultvalue' => $default->instancename
  144. );
  145. $elements['instance'] = array(
  146. 'type' => 'hidden',
  147. 'value' => $instance
  148. );
  149. $elements['institution'] = array(
  150. 'type' => 'hidden',
  151. 'value' => $institution
  152. );
  153. $elements['authname'] = array(
  154. 'type' => 'hidden',
  155. 'value' => 'imap'
  156. );
  157. $elements['host'] = array(
  158. 'type' => 'text',
  159. 'title' => get_string('host', 'auth'),
  160. 'rules' => array(
  161. 'required' => true
  162. ),
  163. 'defaultvalue' => self::$default_config['host']
  164. );
  165. $elements['port'] = array(
  166. 'type' => 'text',
  167. 'title' => get_string('port', 'auth'),
  168. 'rules' => array(
  169. 'required' => true,
  170. 'integer' => true
  171. ),
  172. 'defaultvalue' => self::$default_config['port']
  173. );
  174. $elements['protocol'] = array(
  175. 'type' => 'select',
  176. 'title' => get_string('protocol', 'auth'),
  177. 'options' => $options,
  178. 'rules' => array(
  179. 'required' => true
  180. ),
  181. 'defaultvalue' => self::$default_config['protocol']
  182. );
  183. $elements['changepasswordurl'] = array(
  184. 'type' => 'text',
  185. 'title' => get_string('changepasswordurl', 'auth'),
  186. 'rules' => array(
  187. 'required' => false
  188. ),
  189. 'defaultvalue' => self::$default_config['changepasswordurl']
  190. );
  191. return array(
  192. 'elements' => $elements,
  193. 'renderer' => 'table'
  194. );
  195. }
  196. public static function save_config_options($values, $form) {
  197. $authinstance = new stdClass();
  198. if ($values['instance'] > 0) {
  199. $values['create'] = false;
  200. $current = get_records_assoc('auth_instance_config', 'instance', $values['instance'], '', 'field, value');
  201. $authinstance->id = $values['instance'];
  202. } else {
  203. $values['create'] = true;
  204. // Get the auth instance with the highest priority number (which is
  205. // the instance with the lowest priority).
  206. // TODO: rethink 'priority' as a fieldname... it's backwards!!
  207. $lastinstance = get_records_array('auth_instance', 'institution', $values['institution'], 'priority DESC', '*', '0', '1');
  208. if ($lastinstance == false) {
  209. $authinstance->priority = 0;
  210. } else {
  211. $authinstance->priority = $lastinstance[0]->priority + 1;
  212. }
  213. }
  214. $authinstance->instancename = $values['instancename'];
  215. $authinstance->institution = $values['institution'];
  216. $authinstance->authname = $values['authname'];
  217. if ($values['create']) {
  218. $values['instance'] = insert_record('auth_instance', $authinstance, 'id', true);
  219. } else {
  220. update_record('auth_instance', $authinstance, array('id' => $values['instance']));
  221. }
  222. if (empty($current)) {
  223. $current = array();
  224. }
  225. self::$default_config = array('host' => $values['host'],
  226. 'port' => $values['port'],
  227. 'protocol' => $values['protocol'],
  228. 'changepasswordurl' => $values['changepasswordurl']);
  229. foreach(self::$default_config as $field => $value) {
  230. $record = new stdClass();
  231. $record->instance = $values['instance'];
  232. $record->field = $field;
  233. $record->value = $value;
  234. if ($values['create'] || !array_key_exists($field, $current)) {
  235. insert_record('auth_instance_config', $record);
  236. } else {
  237. update_record('auth_instance_config', $record, array('instance' => $values['instance'], 'field' => $field));
  238. }
  239. }
  240. return $values;
  241. }
  242. }