logo

IMAP Authentication for WordPress 2.7.1

logo

Due to some significant changes to the WordPress authentication mechanism, my original IMAP Authentication plugin stopped working in version 2.7.1. I have updated the plugin to work in 2.7.1. The only difference is that users are no longer automatically created in WordPress if they have a valid IMAP account. I will try to add that back in soon, but for now this will do.

Download imap-authentication1.2.php

<?php
/*
Plugin Name: IMAP Authentication 2
Version: 1.2
Plugin URI: http://blog.neverusethisfont.com/2009/02/imap-authentication-for-wordpress-271/
Description: Authenticate users using IMAP authentication. For Wordpress 2.7.1
Author: Aaron Parecki
Author URI: http://www.aaronparecki.com
 
 
Copyright 2009 by Aaron Parecki  (email : aaron@parecki.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
*/
 
add_action('admin_menu', array('IMAPAuthentication', 'admin_menu'));
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'));
 
add_action('wp_authenticate_user', array('IMAPAuthentication', 'authenticate_user'), 1, 2);
add_filter('check_password', array('IMAPAuthentication', 'check_password'), 1, 4);
 
 
if( is_plugin_page() ) {
    $mailbox = IMAPAuthentication::get_mailbox();
    $user_suffix = IMAPAuthentication::get_user_suffix();
?>
<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="imap_authentication_mailbox,imap_authentication_user_suffix" />
    <fieldset class="options">
      <table width="100%" cellspacing="2" cellpadding="5" class="form-table">
        <tr valign="top">
        <th width="33%" scope="row"><label for="imap_authentication_mailbox">Mailbox</label></th>
        <td><input name="imap_authentication_mailbox" type="text" id="imap_authentication_mailbox" value="<?php echo htmlspecialchars($mailbox) ?>" size="80" /><br />eg: {mail.example.com/readonly}INBOX or {mail.example.com:993/ssl/novalidate-cert/readonly}INBOX</td>
        </tr>
        <tr valign="top">
        <th scope="row"><label for="imap_authentication_user_suffix">User Suffix</label></th>
        <td><input name="imap_authentication_user_suffix" type="text" id="imap_authentication_user_suffix" value="<?php echo htmlspecialchars($user_suffix) ?>" size="50" /><br />A suffix to add to usernames (typically used to automatically add the domain part of the login).<br />eg: @example.com</td>
        </tr>
      </table>
    </fieldset>
    <p class="submit">
      <input type="submit" name="Submit" value="<?php _e('Save Changes') ?>" />
    </p>
  </form>
</div>
<?php
}
 
if( !class_exists('IMAPAuthentication') ) {
    class IMAPAuthentication {
        /*
         * Add an options pane for this plugin.
         */
        function admin_menu() {
            add_options_page('IMAP Authentication', 'IMAP Authentication', 10, __FILE__);
        }
 
        /*
         * Return the mailbox option from the database, creating the option if it doesn't exist.
         */
        function get_mailbox() {
            global $cache_nonexistantoptions;
 
            $mailbox = get_settings('imap_authentication_mailbox');
            if (! $mailbox or $cache_nonexistantoptions['imap_authentication_mailbox']) {
                $mailbox = '{localhost:143}INBOX';
                IMAPAuthentication::add_mailbox_option($mailbox);
            }
 
            return $mailbox;
        }
 
        /*
         * Add the mailbox option to the database.
         */
        function add_mailbox_option($mailbox) {
            add_option('imap_authentication_mailbox', $mailbox, 'The mailbox to try and log into.');
        }
 
        /*
         * Return the user_suffix option from the database, creating the option if it doesn't exist.
         */
        function get_user_suffix() {
            global $cache_nonexistantoptions;
 
            $user_suffix = get_settings('imap_authentication_user_suffix');
            if (! $user_suffix or $cache_nonexistantoptions['imap_authentication_user_suffix']) {
                $user_suffix = '';
                IMAPAuthentication::add_user_suffix_option($user_suffix);
            }
 
            return $user_suffix;
        }
 
        /*
         * Add the user_suffix option to the database.
         */
        function add_user_suffix_option($user_suffix) {
            add_option('imap_authentication_user_suffix', $user_suffix, 'A suffix to add to usernames (typically used to automatically add the domain part of the login).');
        }
 
        // custom error handler
        function eh($type, $msg, $file, $line, $context)
        {
            $error = $error.$msg;
        }
 
        function authenticate_user(&$user, $password) {
        	global $wpdb;
 
			// Apparently this method is no longer called for users who are not in the WP database
 
			$mbox = imap_open(IMAPAuthentication::get_mailbox(), $user->user_login.IMAPAuthentication::get_user_suffix(), $password, OP_HALFOPEN|OP_READONLY) or $error = imap_last_error();
 
			if ($mbox) {
				$userInfo = get_userdatabylogin($user->user_login);
				imap_close($mbox);
				return new WP_User($userInfo->ID);
			} else {
				do_action( 'wp_login_failed', $user->user_login );
				return new WP_Error('incorrect_password', __('<strong>ERROR</strong>: Incorrect password. '.$error));
			}
        }
 
		function check_password($unknown, $enteredPassword, $storedPassword, $userID) {
			$user = new WP_User($userID);
			return IMAPAuthentication::authenticate_user($user, $enteredPassword);
		}
 
        /*
         * Used to disable certain login functions, e.g. retrieving a
         * user's password.
         */
        function disable_password() {
            login_header('Login', '<p class="message"><strong>ERROR</strong>: You can\'t do that here. This blog uses the IMAP login mechanism. Your password is set with your email account.</p>', 'error');
			die();
        }
 
        /*
         * Used to disable certain display elements, e.g. password
         * fields on profile screen.
         */
        function show_password_fields($username) {
            return false;
        }
    }
}
?>

20 Responses to “IMAP Authentication for WordPress 2.7.1”

  1. Eric Boxer says:

    so, im a bit lost trying to configure this to work with google apps . . . any tips?

  2. Ruma Kannan says:

    Its unusual for me to discover something on the internet that is as entertaining and intriguing as what youve got here. Your page is lovely, your graphics are outstanding, and whats more, you use reference that are relevant to what you are talking about. You are certainly one in a million, great job!

  3. porn hub says:

    I really enjoy reading this, I cogitate its passing useful for everyone, looking after ourselves is really important really important

  4. I cannot believe my eyes. This website is loaded with details. I cannot wait to use these information.

  5. I really like your wordpress theme, where did you get a hold of it through?

  6. Your web site won’t render properly on my android – you may want to try and fix that

  7. The following time I read a blog, I hope that it doesn’t disappoint me as much as this one. I imply, I do know it was my choice to learn, but I really thought youd have one thing fascinating to say. All I hear is a bunch of whining about one thing that you may repair if you weren’t too busy searching for attention.

  8. debs ball says:

    Thank you for adding this up, it absolutely was very helpful and informed quite a lot

  9. I intended to draft you one little bit of note in order to say thanks again for all the exceptional views you’ve shown at this time. This is generous of people like you to convey openly just what many of us would’ve marketed for an electronic book to generate some dough for themselves, even more so considering that you could possibly have tried it in case you considered necessary. Those techniques as well served to provide a good way to be sure that other people online have the identical zeal really like my own to find out significantly more concerning this condition. I believe there are lots of more pleasant occasions up front for individuals that look into your blog.

  10. My wife and i were absolutely peaceful when Peter could round up his homework from the precious recommendations he gained from your very own web pages. It’s not at all simplistic to just find yourself releasing guidelines which some others have been making money from. And now we grasp we need the blog owner to be grateful to for that. These explanations you made, the easy site menu, the relationships you can make it easier to engender – it’s all extraordinary, and it’s making our son and us feel that this topic is cool, which is certainly unbelievably fundamental. Thank you for the whole thing!

  11. Your blog is awesome. Would love to know which theme this is. Please email me!

  12. bestelkado-gadgethouse-1 says:

    Hello there! I know this is kinda off topic however , I’d figured I’d ask. Would you be interested in exchanging links or maybe guest authoring a blog post or vice-versa? My blog discusses a lot of the same subjects as yours and I think we could greatly benefit from each other. If you happen to be interested feel free to shoot me an e-mail. I look forward to hearing from you! Superb blog by the way!

  13. You actually make it appear so easy with your presentation but I to find this topic to be really one thing which I feel I’d by no means understand. It seems too complicated and very broad for me. I am taking a look forward to your next put up, I will attempt to get the hold of it!

  14. crazitaxi says:

    I am happy that I noticed this website , just the right info that I was looking for! .

  15. This is a outstanding weblog, would you be involved in doing an interview about just how you created it? If so e-mail me!

  16. There is noticeably a bundle to realize about this. I assume you created various nice points in features also.

  17. information|thanks for great informations It’s a wonderful |Good job. I’m definitely going to bookmark you! |Thanks for good information that comes out to

  18. legalpowder.cn.com review says:

    This company took my hard earned money so i want to help with making folks advised of it, i highly recommend you assist us spread around this thing so they can not thieve other peoples capital!!! They took all my money (1300$) and just do not reply anymore… same occurred to another person before but i found that too late. Now i am wanting to alert people so they do not get rid of their money like it has happened to others… The name of the website: legalpowder.cn.com

  19. IMAP Authentication for WordPress 2.7.1 | Never Use This Font After research just a few of the weblog posts in your website now, and I truly like your manner of blogging. I bookmarked it to my bookmark web site checklist and can be checking again soon. Pls check out my site as effectively and let me know what you think. Regards, Indonesia Furniture

  20. Sites we Along the lines of……

logo
logo
Powered by WordPress | Designed by Elegant Themes