This repository has been archived by the owner on Dec 11, 2018. It is now read-only.
forked from xrmx/wp-imap-authentication2
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimapauth.php
224 lines (185 loc) · 8.12 KB
/
imapauth.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/*
Plugin Name: IMAP Authentication 3
Version: 3.1
Plugin URI: https://github.com/lorenzschmid/wp-imapauth
Description: Authenticate users using IMAP authentication. For Wordpress 4.0+
Author: Lorenz Schmid
Author URI: http://lorenz.retrouvailles.ch
Copyright 2009 by Aaron Parecki (email : aaron@parecki.com)
Copyright 2013 by Unbit sas (author : Riccardo Magliocchetti)
Copyright 2014 by Lorenz Schmid (email : schmid.lorenz@gmail.com)
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 2 of the License, or
(at your option) any later version.
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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* Plugin Update Routine */
include_once( plugin_dir_path( __FILE__ ) . 'wp-updater/updater.php' );
$updater = new Smashing_Updater( __FILE__ );
$updater->set_username( 'schmidlorenz' );
$updater->set_repository( 'wp-imapauth' );
$updater->initialize();
/* Replace Wordpress authentication */
add_filter( 'authenticate', array('IMAPAuthentication', 'authenticate'), 1, 3 );
/* Wordpress Options */
add_option('imapauth_mailbox', '{localhost:143}INBOX');
add_option('imapauth_pad_domain', '');
add_option('imapauth_create_new', 'false');
add_action('admin_menu', array('IMAPAuthentication', 'admin_menu'));
/* Suppress password reset */
add_action('lost_password', array('IMAPAuthentication', 'disable_password'));
add_action('retrieve_password', array('IMAPAuthentication', 'disable_password'));
add_action('password_reset', array('IMAPAuthentication', 'disable_password'));
add_filter('show_password_fields', array('IMAPAuthentication', 'show_password_fields'));
/* Logging Function Wrapper */
if ( ! function_exists('write_log')) {
function write_log ( $log ) {
if ( is_array( $log ) || is_object( $log ) ) {
error_log( print_r( $log, true ) );
} else {
error_log( $log );
}
}
}
if( !class_exists('IMAPAuthentication') ) {
class IMAPAuthentication {
static function authenticate($user=null, $user_name='', $password='') {
/* remove existing authentication function */
remove_action('authenticate', 'wp_authenticate_username_password', 20);
/* see if user already logged in */
if ( is_a($user, 'WP_User') ) { return $user; }
/* check non-void arguments */
if ( empty($user_name) || empty($password) ) {
$error = new WP_Error();
if ( empty($user_name) ) $error->add('empty_username', __('<strong>ERROR</strong>: Missing user name value.'));
if ( empty($password) ) $error->add('empty_password', __('<strong>ERROR</strong>: Missing password value.'));
return $error;
}
/* authenticate over IMAP */
list( $auth_result, $user_email ) = IMAPAuthentication::imap_authenticate($user_name, $password);
if ( !$auth_result ) {
if ( is_a($auth_result, 'WP_Error')) {
return $auth_result;
} else {
return new WP_Error('invalid_user_name', __('<strong>ERROR</strong>: Could not authenticate your credentials.'));
}
}
$user_id = email_exists($user_email);
if ( !$user_id ) {
/* user does not yet exist */
if( IMAPAuthentication::get_opt_create_new() ) {
/* create new user */
$random_password = wp_generate_password( $length=12, $include_standard_special_chars=false );
$user_id = wp_create_user( IMAPAuthentication::get_user_name( $user_email ), $random_password, $user_email );
} else {
return new WP_Error('registration_not_allowed', __('<strong>ERROR</strong>: New registrations not allowed.'));
}
}
/* return existing user */
$user = new WP_User($user_id);
return $user;
}
static function imap_authenticate($user_name, $password) {
/* Try to logn ofer IMAP */
$user_email = IMAPAuthentication::get_user_mail($user_name);
$mbox = @imap_open( IMAPAuthentication::get_mailbox(), $user_email, $password, OP_HALFOPEN|OP_READONLY );
/* Login successful */
if($mbox) {
imap_close($mbox);
if(WP_DEBUG === true) {
write_log( "[wp-imapauth] Login successful! User: ". sanitize_email( $user_name ) );
}
return array( true, $user_email );
}
/* Login failed */
$imap_error = imap_last_error();
if(WP_DEBUG === true) {
write_log( "[wp-imapauth] Login failed! User: ". sanitize_email( $user_name ) .", Error: ". $imap_error );
}
return array( false, imap_last_error() );
}
/* Get option mfunctions */
static function get_mailbox() {
return get_option( 'imapauth_mailbox', '{localhost:143}INBOX' );
}
static function get_pad_domain() {
return get_option( 'imapauth_pad_domain' );
}
static function get_opt_create_new() {
return get_option( 'imapauth_create_new', 'false' );
}
static function get_user_mail( $user_name ) {
if( IMAPAuthentication::get_pad_domain() !== '' ) {
if( !preg_match( '#@#', $user_name ) ) {
$user_name .= '@' . IMAPAuthentication::get_pad_domain();
}
}
return $user_name;
}
static function get_user_name( $user_mail ) {
$user_mail = explode( '@', $user_mail );
return $user_mail[0];
}
/* Suppresion of password retrieving/reset */
function disable_password() {
login_header( 'Log In', '', new WP_Error('password_reset_suppressed', '<strong>ERROR</strong>: This blog uses the IMAP login mechanism. Your password is set with your email account and cannot be reseted here.' ) );
?>
<p id="nav"><a href="<?php echo wp_login_url(); ?>" title="<?php esc_attr_e( 'Try again!' ); ?>"><?php printf( __( 'Log In' ) ); ?></a></p>
<?php
login_footer();
die();
}
/* disable password field visibility */
function show_password_fields($username) {
return false;
}
/* options pane for this plugin */
static function admin_menu() {
add_options_page('IMAP Authentication', 'IMAP Authentication', 10, __FILE__, array('IMAPAuthentication', 'show_plugin_page')) ;
}
/* plugin option page */
static function show_plugin_page() {
$mailbox = IMAPAuthentication::get_mailbox();
$pad_domain = IMAPAuthentication::get_pad_domain();
$opt_create_new = IMAPAuthentication::get_opt_create_new();
?>
<div class="wrap">
<h2>IMAP Authentication Options</h2>
<form name="imapauthenticationoptions" method="post" action="options.php">
<?php wp_nonce_field('update-options'); ?>
<input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="imapauth_mailbox,imapauth_pad_domain,impauth_create_new" />
<fieldset class="options">
<table width="100%" cellspacing="2" cellpadding="5" class="form-table">
<tr valign="top">
<th width="33%" scope="row"><label for="imapauth_mailbox">Mailbox</label></th>
<td><input name="imapauth_mailbox" type="text" id="imapauth_mailbox" value="<?php echo htmlspecialchars($mailbox) ?>" size="80" /><br />eg: {mail.domain.com/readonly}INBOX or {mail.domain.com:993/ssl/novalidate-cert/readonly}INBOX</td>
</tr>
<tr valign="top">
<th scope="row"><label for="imapauth_pad_domain">User Suffix</label></th>
<td><input name="imapauth_pad_domain" type="text" id="imapauth_pad_domain" value="<?php echo htmlspecialchars($pad_domain) ?>" size="50" /><br />A suffix to add to usernames (typically used to automatically add the domain part of the login).<br />eg: domain.com</td>
</tr>
<tr valign="top">
<th scope="row"><label for="imapauth_create_new">User Registration</label></th>
<td><input name="imapauth_create_new" type="checkbox" id="imapauth_create_new" value="1" <?php checked(true, $opt_create_new); ?> /> Register new user automatically if succesful IMAP authentification.</td>
</tr>
</table>
</fieldset>
<p class="submit">
<input type="submit" name="Submit" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php
}
}
}
?>