Skip to content

Instantly share code, notes, and snippets.

@vjt
Created January 31, 2011 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vjt/804858 to your computer and use it in GitHub Desktop.
Save vjt/804858 to your computer and use it in GitHub Desktop.
Awesome IMAP authentication backend for Dokuwiki
<?php
/**
* Awesome IMAP authentication backend
*
* Set in conf/dokuwiki.php:
*
* $conf['authtype'] = 'imap';
* $conf['authdomain'] = 'example.com'; // The default domain for IMAP-created users
* $conf['authserver'] = '{127.0.0.1:143/imap/novalidate-cert}'; // The IMAP server to which authenticate to
*
* As you can see, you don't need to create users beforehand.
* They'll be created on the fly upon first successful login.
*
* @author Marcello Barnaba <vjt@openssl.it>
* @license Public Domain
* @version 1.0
* @date February 2011
*/
define('DOKU_AUTH', dirname(__FILE__));
require_once(DOKU_AUTH.'/plain.class.php');
class auth_imap extends auth_plain
{
function auth_imap()
{
// Requires the PHP IMAP module
if (!extension_loaded('imap')) {
msg('PHP IMAP module cannot be loaded', -1);
$this->broken = true;
return;
}
// Check configuration
global $conf;
if (!(isset($conf['authdomain']) && isset($conf['authserver']))) {
msg('Please set $conf[\'authdomain\'] and $conf[\'authserver\']', -1);
$this->broken = true;
return;
}
$this->server = $conf['authserver'];
$this->domain = $conf['authdomain'];
// Call parent constructor
parent::auth_plain();
// Remove change password capability
$this->cando['modPass'] = false;
}
/**
* Checks the provided username and password using IMAP.
* If the user doesn't exist, it is created on the fly.
*
* @param string $user Username
* @param string $pass Password
* @return boolean True if authentication is successful
*/
function checkPass($user, $pass)
{
if ($this->broken)
return null;
$handle = imap_open ($this->server, $user, $pass, OP_HALFOPEN|OP_READONLY);
if (!$handle)
return false;
imap_close ($handle);
// If the user doesn't exist, create it
//
if (!$this->getUserData($user))
return $this->createUser($user, 'antani', $user, $user.'@'.$this->domain);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment