lib.php 9.5 KB
Newer Older
Donal McMullan's avatar
Donal McMullan committed
1
2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3
4
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
 * Copyright (C) 2006-2007 Catalyst IT Ltd (http://www.catalyst.net.nz)
Donal McMullan's avatar
Donal McMullan committed
5
 *
Francois Marier's avatar
Francois Marier committed
6
7
8
9
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
Donal McMullan's avatar
Donal McMullan committed
10
 *
Francois Marier's avatar
Francois Marier committed
11
12
13
14
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
Donal McMullan's avatar
Donal McMullan committed
15
 *
Francois Marier's avatar
Francois Marier committed
16
17
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Donal McMullan's avatar
Donal McMullan committed
18
19
20
21
22
23
24
25
26
27
 *
 * @package    mahara
 * @subpackage auth-internal
 * @author     Nigel McNie <nigel@catalyst.net.nz>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

defined('INTERNAL') || die();
28
require_once(get_config('docroot') . 'auth/lib.php');
Donal McMullan's avatar
Donal McMullan committed
29
30
31
32
33
34
35
36
37

/**
 * The internal authentication method, which authenticates users against the
 * Mahara database.
 */
class AuthImap extends Auth {

    public function __construct($id = null) {
        $this->type                         = 'imap';
38
        $this->has_instance_config                   = true;
Donal McMullan's avatar
Donal McMullan committed
39
40
41
42
43
44

        $this->config['host']               = '';
        $this->config['port']               = '143';
        $this->config['protocol']           = '/imap';
        $this->config['changepasswordurl']  = '';

45
        if (!empty($id)) {
Donal McMullan's avatar
Donal McMullan committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
            return $this->init($id);
        }
        return true;
    }

    public function init($id = null) {

        $this->ready = parent::init($id);

        // Check that required fields are set
        if ( empty($this->config['host']) ||
             empty($this->config['port']) ||
             empty($this->config['protocol']) ) {
            $this->ready = false;
        }

        return $this->ready;

    }

    /**
     * Attempt to authenticate user
     *
     * @param string $username The username to authenticate with
     * @param string $password The password being used for authentication
     * @return bool            True/False based on whether the user
     *                         authenticated successfully
     * @throws AuthUnknownUserException If the user does not exist
     */
    public function authenticate_user_account($user, $password) {
        $this->must_be_ready();

        if (! function_exists('imap_open')) {
            throw new ConfigException('IMAP is not available in your PHP environment. Check that it is properly installed');
        }

        $connectionstring = '{'.
                                $this->config['host']
                            .':'. 
                                $this->config['port']
                            .
                                $this->config['protocol']
                            .'}'; 

        try {
            $connection = imap_open($connectionstring, $user->username, $password, OP_HALFOPEN);
            if ($connection) {
                imap_close($connection);
                return true;
            }
        } catch (Exception $e) {
            throw new ConfigException('Unable to connect to server with connection string: '.$connectionstring);
        }

        return false;  // No match
    }

103
104
105
106
107
108
109
    /**
     * Imap doesn't export enough information to be able to auto-create users
     */
    public function can_auto_create_users() {
        return false;
    }

Donal McMullan's avatar
Donal McMullan committed
110
111
112
113
114
115
116
}

/**
 * Plugin configuration class
 */
class PluginAuthImap extends PluginAuth {

117
118
    private static $default_config = array('host'=>'', 'port'=>'143', 'protocol'=>'/imap','changepasswordurl'=>'');

Donal McMullan's avatar
Donal McMullan committed
119
    public static function has_config() {
120
121
122
123
124
125
126
127
        return false;
    }

    public static function get_config_options() {
        return array();
    }

    public static function has_instance_config() {
Donal McMullan's avatar
Donal McMullan committed
128
129
130
        return true;
    }

131
132
133
134
    public static function is_usable() {
        return extension_loaded('imap');
    }

135
    public static function get_instance_config_options($institution, $instance = 0) {
Donal McMullan's avatar
Donal McMullan committed
136
137
138
139
140
141
        // TODO: put these strings in a lang file
        $options['/imap'] = 'IMAP';
        $options['/imap/ssl'] = 'IMAP/SSL';
        $options['/imap/ssl/novalidate-cert'] = 'IMAP/SSL (self-signed certificate)';
        $options['/imap/tls'] = 'IMAP/TLS';

142
143
        if ($instance > 0) {
            $current        = get_records_array('auth_instance',        'id',       $instance, 'priority ASC');
144
            if ($current == false) {
145
146
147
148
149
                throw new Exception('Could not find data for auth instance '.$instance);
            }
            $default = $current[0];
            $current_config = get_records_menu('auth_instance_config', 'instance', $instance, '', 'field, value');

150
            if ($current_config == false) {
151
152
153
154
                $current_config = array();
            }

            foreach (self::$default_config as $key => $value) {
155
                if (array_key_exists($key, $current_config)) {
156
157
158
159
160
161
162
163
164
165
166
                    self::$default_config[$key] = $current_config[$key];
                }
            }
        } else {
            $default = new stdClass();
            $default->instancename = '';
        }

        $elements['instancename'] = array(
            'type' => 'text',
            'title' => get_string('authname','auth'),
Donal McMullan's avatar
Donal McMullan committed
167
168
            'rules' => array(
                'required' => true
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
            ),
            'defaultvalue' => $default->instancename
        );

        $elements['instance'] = array(
            'type' => 'hidden',
            'value' => $instance
        );

        $elements['institution'] = array(
            'type' => 'hidden',
            'value' => $institution
        );

        $elements['authname'] = array(
            'type' => 'hidden',
            'value' => 'imap'
Donal McMullan's avatar
Donal McMullan committed
186
187
188
189
        );

        $elements['host'] = array(
            'type' => 'text',
190
            'title' => get_string('host', 'auth'),
Donal McMullan's avatar
Donal McMullan committed
191
192
            'rules' => array(
                'required' => true
193
194
            ),
            'defaultvalue' => self::$default_config['host']
Donal McMullan's avatar
Donal McMullan committed
195
196
197
198
        );

        $elements['port'] = array(
            'type' => 'text',
199
            'title' => get_string('port', 'auth'),
Donal McMullan's avatar
Donal McMullan committed
200
201
202
            'rules' => array(
                'required' => true,
                'integer' => true
203
204
205
206
207
208
209
210
211
212
213
214
            ),
            'defaultvalue' => self::$default_config['port']
        );

        $elements['protocol'] = array(
            'type' => 'select',
            'title' => get_string('protocol', 'auth'),
            'options' => $options,
            'rules' => array(
                'required' => true
            ),
            'defaultvalue' => self::$default_config['protocol']
Donal McMullan's avatar
Donal McMullan committed
215
216
217
218
        );

        $elements['changepasswordurl'] = array(
            'type' => 'text',
219
            'title' => get_string('changepasswordurl', 'auth'),
Donal McMullan's avatar
Donal McMullan committed
220
221
            'rules' => array(
                'required' => false
222
223
            ),
            'defaultvalue' => self::$default_config['changepasswordurl']
Donal McMullan's avatar
Donal McMullan committed
224
225
226
227
228
229
230
231
        );

        return array(
            'elements' => $elements,
            'renderer' => 'table'
        );
    }

232
    public static function save_config_options($values, $form) {
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263

        $authinstance = new stdClass();

        if ($values['instance'] > 0) {
            $values['create'] = false;
            $current = get_records_assoc('auth_instance_config', 'instance', $values['instance'], '', 'field, value');
            $authinstance->id = $values['instance'];
        } else {
            $values['create'] = true;

            // Get the auth instance with the highest priority number (which is
            // the instance with the lowest priority).
            // TODO: rethink 'priority' as a fieldname... it's backwards!!
            $lastinstance = get_records_array('auth_instance', 'institution', $values['institution'], 'priority DESC', '*', '0', '1');

            if ($lastinstance == false) {
                $authinstance->priority = 0;
            } else {
                $authinstance->priority = $lastinstance[0]->priority + 1;
            }
        }

        $authinstance->instancename = $values['instancename'];
        $authinstance->institution  = $values['institution'];
        $authinstance->authname     = $values['authname'];

        if ($values['create']) {
            $values['instance'] = insert_record('auth_instance', $authinstance, 'id', true);
        } else {
            update_record('auth_instance', $authinstance, array('id' => $values['instance']));
        }
Donal McMullan's avatar
Donal McMullan committed
264
265
266
267
268

        if (empty($current)) {
            $current = array();
        }

269
270
271
272
273
274
        self::$default_config =   array('host'              => $values['host'],
                                        'port'              => $values['port'],
                                        'protocol'          => $values['protocol'],
                                        'changepasswordurl' => $values['changepasswordurl']);

        foreach(self::$default_config as $field => $value) {
Donal McMullan's avatar
Donal McMullan committed
275
            $record = new stdClass();
276
277
            $record->instance = $values['instance'];
            $record->field    = $field;
Donal McMullan's avatar
Donal McMullan committed
278
            $record->value    = $value;
279
280

            if ($values['create'] || !array_key_exists($field, $current)) {
Donal McMullan's avatar
Donal McMullan committed
281
                insert_record('auth_instance_config', $record);
282
283
            } else {
                update_record('auth_instance_config', $record, array('instance' => $values['instance'], 'field' => $field));
Donal McMullan's avatar
Donal McMullan committed
284
285
286
            }
        }

287
        return $values;
Donal McMullan's avatar
Donal McMullan committed
288
289
290
    }
}

291
?>