go-mailzu/lib/IMAPAuth.class.php

119 lines
2.7 KiB
PHP
Raw Permalink Normal View History

2008-12-10 14:33:43 +01:00
<?php
/**
* IMAPAuth class
* @version 08-11-2005
* @Author Samuel Tran
* @package IMAPAuth
*
* Copyright (C) 2005 - 2007 MailZu
* License: GPL, see LICENSE
*/
/**
* Base directory of application
*/
@define('BASE_DIR', dirname(__FILE__) . '/..');
/**
* CmnFns class
*/
include_once('lib/CmnFns.class.php');
2008-12-10 14:33:43 +01:00
/**
* Provide all database access/manipulation functionality for IMAP Auth
*/
class IMAPAuth {
// The IMAP hosts with port (hostname[:port])
var $imapHosts;
// IMAP authentication type
var $imapType;
// Username
var $imapUsername;
var $err_msg = '';
2008-12-10 14:33:43 +01:00
/**
* Constructor to initialize object
* @param none
*/
function IMAPAuth() {
global $conf;
$this->imapHosts = $conf['auth']['imap_hosts'];
$this->imapType = $conf['auth']['imap_type'];
$this->imapDomainName = $conf['auth']['imap_domain_name'];
}
// User methods -------------------------------------------
2008-12-10 14:33:43 +01:00
/**
* Authenticates user
* @param string $username
* @param string $password
* @return boolean
*/
function authUser ($username, $password) {
// Returns true if the username and password work
// and false if they are wrong or don't exist.
$this->imapUsername = $username;
foreach ($this->imapHosts as $host) { // Try each host in turn
$host = trim($host);
switch ($this->imapType) {
case "imapssl":
$host = '{'.$host."/imap/ssl}INBOX";
break;
2008-12-10 14:33:43 +01:00
case "imapcert":
$host = '{'.$host."/imap/ssl/novalidate-cert}INBOX";
break;
2008-12-10 14:33:43 +01:00
case "imaptls":
$host = '{'.$host."/imap/notls}INBOX";
break;
2008-12-10 14:33:43 +01:00
default:
$host = '{'.$host.'}INBOX';
}
2008-12-10 14:33:43 +01:00
//error_reporting(0);
$connection = imap_open($host, $username, $password, OP_HALFOPEN);
if ($connection) {
imap_close($connection);
return true;
}
}
2008-12-10 14:33:43 +01:00
$this->err_msg = translate('IMAP Authentication: no match');
return false; // No match
}
2008-12-10 14:33:43 +01:00
/**
* Returns the last error message
* @param none
* @return last error message generated
*/
function get_err() {
return $this->err_msg;
}
// Helper methods -------------------------------------------
/**
* Returns user information
* @return array containing user information
*/
function getUserData() {
$rval = array(
2008-12-10 14:33:43 +01:00
'logonName' => $this->imapUsername,
'firstName' => $this->imapUsername,
'emailAddress' => array($this->imapUsername.(empty($this->imapDomainName) ? '' : '@'. $this->imapDomainName))
2008-12-10 14:33:43 +01:00
);
return $rval;
2008-12-10 14:33:43 +01:00
}
}
?>