Author Topic: Exchange backend IMAP  (Read 14105 times)

Offline fregster

  • Jr. Member
  • **
  • Posts: 16
Exchange backend IMAP
« on: June 04, 2007, 05:15:12 AM »
I wanted to setup Roundcune to use an Exchange backend, then I can migrate the backend of to a proper IMAP server in my own time with little hassle.

I found out that the exchange connection requires domain/mailbox/username to be sent as the username.

By adding a CONFIG option for exchange_server = true; and exchange_domain = 'domain';

then modifying the imap connect function to contain (rcube_imap.inc line 105) to include $CONFIG in the globals

and then

if($CONFIG('ecxhange_server)
{
  $user = $CONFIG['exchange_domain'] . $_SESSION['exchange_user'] . '/' . $user;
}

I will add how I got the login window to include a diffrent login box and my reasons behind it in my next post.

Offline fregster

  • Jr. Member
  • **
  • Posts: 16
Re: Exchange backend IMAP
« Reply #1 on: June 04, 2007, 05:35:05 AM »
Change the start of the login form function, main.inc line 1425 to:

function rcmail_login_form($attrib)
 {
 global $CONFIG, $OUTPUT, $SESS_HIDDEN_FIELD;
 
 $labels = array();
 $labels['user'] = rcube_label('mailbox');
 $lables['alias'] = rcube_label('username');
 $labels['pass'] = rcube_label('password');
 $labels['host'] = rcube_label('server');
 
 $input_user = new textfield(array('name' => '_user', 'id' => 'rcmloginuser', 'size' => 30, 'autocomplete' => 'off'));
 $input_alias = new textfield(array('name' => '_alias', 'id' => 'rcmloginalias', 'size' => 30, 'autocomplete' => 'off'));
 $input_pass = new passwordfield(array('name' => '_pass', 'id' => 'rcmloginpwd', 'size' => 30));
 $input_action = new hiddenfield(array('name' => '_action', 'value' => 'login'));
 
 $fields = array();
 $fields['user'] = $input_user->show(get_input_value('_user', RCUBE_INPUT_POST));
 $fields['alias'] = $input_alias->show(get_input_value('_alias', RCUBE_INPUT_POST));
 $fields['pass'] = $input_pass->show();
 $fields['action'] = $input_action->show();

Then add at line 23 $labels['mailbox']   = 'Mailbox'; to your lables.inc

Using this setup the Mailbox is the main user as far as RC is concered so that as with exchange any user with permissions can login to a user and administer that account.

I also changed all of the address book lookups to query where (userid = $user or '0'); where 0 is your administrator account, then you can create, administor and share your contacts with your admin account.

Offline claud43

  • Newbie
  • *
  • Posts: 3
Re: Exchange backend IMAP
« Reply #2 on: February 22, 2008, 01:31:15 AM »
Hello, I am following your change exchange for 5 but I can not understand where to make the changes. You can specify the changes better with 'latest roundcube?

The changes that I need are: domain / userdomain / alias exchange

thank you

Offline fregster

  • Jr. Member
  • **
  • Posts: 16
Re: Exchange backend IMAP
« Reply #3 on: February 22, 2008, 07:20:27 AM »
Okay I will try to clear this up.

Exchange has mailboxes and users that can access them and so the standard IMAP of just username is not enough.

When loging in with a exchange backend you need to specify the mailbox / username.
You can either get your users to put this into the username field ie "Username: test/paul.fryer"
Or update the login page to have a seperate input field ie "Username: paul.fryer" and "Mailbox: test"

For simplacity I will assume you only have one backend and it's an exchange 5 based server we will save the mailbox as a session var  ($_SESSION['alias']) to make changes to the funcion calls as minimal as posible

This setup uses the mailbox as the username as this keeps the preferences per mailbox you could inverse it for preferences per alias


Step one
Update your localisation file to have a lable for the mailbox field

program/localization/en_GB/lables.inc


$labels = array();

// login page
$labels['welcome']  = 'Welcome to $product';
$labels['username'] = 'Username';
$labels['mailbox']   = 'Mailbox';
$labels['password'] = 'Password';
$labels['server']  = 'Server';
$labels['login']   = 'Login';


Step 2
Update the login form to ask for mailbox as well as the username

program/include/main.inc
Lines around 1425 on this older version install

function rcmail_login_form($attrib)
 {
 global $CONFIG, $OUTPUT, $SESS_HIDDEN_FIELD;
 
 $labels = array();
 $labels['user'] = rcube_label('mailbox');
 $lables['alias'] = rcube_label('username');
 $labels['pass'] = rcube_label('password');
 $labels['host'] = rcube_label('server');
 
 $input_user = new textfield(array('name' => '_user', 'id' => 'rcmloginuser', 'size' => 30, 'autocomplete' => 'off'));
 $input_alias = new textfield(array('name' => '_alias', 'id' => 'rcmloginalias', 'size' => 30, 'autocomplete' => 'off'));
 $input_pass = new passwordfield(array('name' => '_pass', 'id' => 'rcmloginpwd', 'size' => 30));
 $input_action = new hiddenfield(array('name' => '_action', 'value' => 'login'));
 
 $fields = array();
 $fields['user'] = $input_user->show(get_input_value('_user', RCUBE_INPUT_POST));
 $fields['alias'] = $input_alias->show(get_input_value('_alias', RCUBE_INPUT_POST));
 $fields['pass'] = $input_pass->show();
 $fields['action'] = $input_action->show();


Step 3

Get the posted alias to update the $_SESSION['alias'] var
program/include/rcube_imap.inc Line 148 on
EDIT: SORRY This should be index.php


// try to log in
if ($_action=='login' && $_task=='mail')
{
  if(isset($_POST['_alias']))
  {
   $_SESSION['alias'] = $_POST['_alias'];
  }


Step 4

Get the IMAP login to pass this along to the Exchange backend
program/include/rcube_imap.inc
lines 105 ish on

function connect($host, $user, $pass, $port=143, $use_ssl=FALSE)
  {
   global $ICL_SSL, $CONFIG, $ICL_PORT, $IMAP_USE_INTERNAL_DATE;
 
   $exchange_login_user = 'domain_here/' . $_SESSION['alias'] . '/' . $user;
   
  // check for Open-SSL support in PHP build
  if ($use_ssl && in_array('openssl', get_loaded_extensions()))
   $ICL_SSL = TRUE;
  else if ($use_ssl)
   {
   raise_error(array('code' => 403, 'type' => 'imap', 'file' => __FILE__,
            'message' => 'Open SSL not available;'), TRUE, FALSE);
   $port = 143;
   }

  $ICL_PORT = $port;
  $IMAP_USE_INTERNAL_DATE = false;
 
  $this->conn = iil_Connect($host, $exchange_login_user, $pass, array('imap' => 'check'));
  $this->host = $host;
  $this->user = $user;
  $this->pass = $pass;
  $this->port = $port;
  $this->ssl = $use_ssl;
   /*
   echo('
');
   print_r($this->conn);
   print_r($_SESSION);
   echo('
');
  die();
   */



Hope this helps

Let me know how you get on

Offline claud43

  • Newbie
  • *
  • Posts: 3
Re: Exchange backend IMAP
« Reply #4 on: February 22, 2008, 01:12:18 PM »
Ok thanks for help, sorry for my English

I want the opportunity to enter username, password and mailbox.

I tried it this way:

The method works but I can not do the field work $alias


file main.inc

// return code for the webmail login form
function rcmail_login_form($attrib)
 {
 global $CONFIG, $OUTPUT, $SESS_HIDDEN_FIELD;
 
 $labels = array();
 $labels['user'] = rcube_label('username');
 $labels['alias'] = rcube_label('mailbox');
 $labels['pass'] = rcube_label('password');
 $labels['host'] = rcube_label('server');
 
 $input_user = new textfield(array('name' => '_user', 'id' => 'rcmloginuser', 'size' => 30, 'autocomplete' => 'off'));
 $input_alias = new textfield(array('name' => '_alias', 'id' => 'rcmloginalias', 'size' => 30, 'autocomplete' => 'off'));
 $input_pass = new passwordfield(array('name' => '_pass', 'id' => 'rcmloginpwd', 'size' => 30));
 $input_action = new hiddenfield(array('name' => '_action', 'value' => 'login'));
 
 $fields = array();
 $fields['user'] = $input_user->show(get_input_value('_user', RCUBE_INPUT_POST));
 $fields['alias'] = $input_alias->show(get_input_value('_alias', RCUBE_INPUT_POST));
 $fields['pass'] = $input_pass->show();
 $fields['action'] = $input_action->show();
 
 if (is_array($CONFIG['default_host']))
  {
  $select_host = new select(array('name' => '_host', 'id' => 'rcmloginhost'));
 
  foreach ($CONFIG['default_host'] as $key => $value)
  {
   if (!is_array($value))
    $select_host->add($value, (is_numeric($key) ? $value : $key));
   else
    {
    unset($select_host);
    break;
    }
  }
   
  $fields['host'] = isset($select_host) ? $select_host->show($_POST['_host']) : null;
  }
 else if (!strlen($CONFIG['default_host']))
  {
  $input_host = new textfield(array('name' => '_host', 'id' => 'rcmloginhost', 'size' => 30));
  $fields['host'] = $input_host->show($_POST['_host']);
  }

 $form_name = strlen($attrib['form']) ? $attrib['form'] : 'form';
 $form_start = !strlen($attrib['form']) ? '' : '';
 $form_end = !strlen($attrib['form']) ? '' : '';
 
 if ($fields['host'])
  $form_host = << 



$fields[host]

EOF;

 $OUTPUT->add_gui_object('loginform', $form_name);
 
 $out = <<$form_start
$SESS_HIDDEN_FIELD
$fields[action]














$form_host
$fields[user]
$fields[alias]
$fields[pass]

$form_end
EOF;

 return $out;
 }


file rcube_imap

function connect($host, $user, $pass, $port=143, $use_ssl=FALSE)
  {
  global $ICL_SSL, $CONFIG, $ICL_PORT, $IMAP_USE_INTERNAL_DATE;
 
  $exchange_login_user = 'my_domain/' . $user. '/' . $alias ;
 
  // check for Open-SSL support in PHP build
  if ($use_ssl && in_array('openssl', get_loaded_extensions()))
   $ICL_SSL = TRUE;
  else if ($use_ssl)
   {
   raise_error(array('code' => 403, 'type' => 'imap', 'file' => __FILE__,
            'message' => 'Open SSL not available;'), TRUE, FALSE);
   $port = 143;
   }

  $ICL_PORT = $port;
  $IMAP_USE_INTERNAL_DATE = false;
 
  $this->conn = iil_Connect($host, $exchange_login_user, $pass, array('imap' => 'check'));
  $this->host = $host;
  $this->user = $user;
  $this->pass = $pass;
  $this->port = $port;
  $this->ssl = $use_ssl;
  /*
  echo('
');
  print_r($this->conn);
  print_r($_SESSION);
  echo('
');
  die();
  */

Offline fregster

  • Jr. Member
  • **
  • Posts: 16
Re: Exchange backend IMAP
« Reply #5 on: February 26, 2008, 09:14:43 AM »
the function copied from your code

file rcube_imap

function connect($host, $user, $pass, $port=143, $use_ssl=FALSE)
  {
  global $ICL_SSL, $CONFIG, $ICL_PORT, $IMAP_USE_INTERNAL_DATE;
 
  $exchange_login_user = 'my_domain/' . $user. '/' . $alias ;


does not pass $alias in, this is why I used the session var and when posted to main put $_SESSION['alias'] = $_POST['alias'];

I did not change the function connect to include alias but you could do
    function connect($host, $user, $pass, $port=143, $use_ssl=FALSE, $alias=NULL)

You would have to find where you call it and update the calls though which for a non-perminant hack is a pain.

Offline mono76

  • Newbie
  • *
  • Posts: 2
Re: Exchange backend IMAP
« Reply #6 on: February 29, 2008, 05:06:39 AM »
Hi there !

Sorry about my english ! i'm french !! :)

I want to try this post in my roundcube configuration to use Exchange backend, but i don't find where i can insert the modification in the files : program/include/main.inc and program/include/rcube_imap.inc
It's possible to see your complete configurations files? because i'm lost ! :P
I'm using roundcube webmail 0.1 rc2

thx

Offline fregster

  • Jr. Member
  • **
  • Posts: 16
Re: Exchange backend IMAP
« Reply #7 on: March 05, 2008, 04:57:06 AM »
I can post the files but you should be able to copy and paste the changes from above.

NOTE THESE FILES ARE NOT RC2 THERE ARE AN OLD SVN
Note: There are also other changes the might / will break your RC install

There are only 4 files that need to be changed please find them attached