Author Topic: Multiple Domains Login  (Read 62776 times)

Offline OJM

  • Newbie
  • *
  • Posts: 3
Re: Multiple Domains Login
« Reply #15 on: January 28, 2007, 07:57:47 AM »
Quote from: Brett
Hmm..... so you'd want something like:

$user $_POST['_user'].'@'.substr($_SERVER['HTTP_HOST'], 5);

right?

Quote from: Brett
It would need to go into index.php either replacing or AFTER you see the default $user = line.

Sorry Brett, I can't see where this needs to go. I'm running version 0.1-beta2

I sell hosting, and have 6 email servers. However I'd like to have one location for my webmail, and for RoundCube to work out which mail server to logon to, from the email address the person is using. I assume the example you gave will do this, but I'm not sure where your code needs to be placed, to remove the dropdown box.

Cheers.

Offline bpat1434

  • Administrator
  • Hero Member
  • *****
  • Posts: 673
Re: Multiple Domains Login
« Reply #16 on: January 28, 2007, 08:49:39 PM »
Sorry, my bad. Look in /program/includes/main.inc for the function rcmail_login. Inside there you need to find this:
 // exit if IMAP login failed
 
if (!($imap_login $IMAP->connect($host$user$pass$imap_port$imap_ssl)))
  return 
FALSE;



And add just before it the line $user$_POST['_user'].'@'.substr($_SERVER['HTTP_HOST'], 5);

So you come out with:
$user $_POST['_user'].'@'.substr($_SERVER['HTTP_HOST'], 5);

 
// exit if IMAP login failed
 
if (!($imap_login $IMAP->connect($host$user$pass$imap_port$imap_ssl)))
  return 
FALSE;
 
  

Offline dgec

  • Jr. Member
  • **
  • Posts: 10
Re: Multiple Domains Login
« Reply #17 on: February 05, 2007, 06:22:05 PM »
I'm confused about how exactly this is set up and the capabilities.

Here's my fairly standard setup - will any of the above work for me?

- 1 main, purchased account with my own email account.
- I have websites I manage, which have domains pointing to subdirectories in my public_html directory (eg cust1.com is pointed to my.com/cust1). no big deal.

I want my customers to be able to use their user1@cust1.com and user2@cust2.com off of my RC install. (Actually I'd like to be able to use user1@cust1.com AND user1@cust2.com for that matter.)

At this point, I'm not as concerned about automatically picking up their domain (I'm guessing the user selects a link to "cust1.com/mail" which redirects to my.com/email) as I am about having to install multiple RCs and databases.


Offline bpat1434

  • Administrator
  • Hero Member
  • *****
  • Posts: 673
Re: Multiple Domains Login
« Reply #18 on: February 05, 2007, 07:38:01 PM »
You don't need to install multiple databases. One database will support 1 SMTP server, and mutiple IMAP servers. So all email addresses will work. Now, since I'm guessing that you only have 1 SMTP server, only one installation is needed. You'll just have to make them insert their entire email address (user1@cust1.com) as the username, and their password. If you want, you can have a drop-down list of IMAP servers available to them.
 
  

Offline dgec

  • Jr. Member
  • **
  • Posts: 10
Re: Multiple Domains Login
« Reply #19 on: February 06, 2007, 11:07:41 AM »
Great! That was my guess, but one thread sounded like multiple code instances were needed.
Thanks!

Offline dgec

  • Jr. Member
  • **
  • Posts: 10
Re: Multiple Domains Login
« Reply #20 on: February 06, 2007, 02:09:19 PM »
Quote from: Insanity5902
In my main.inc.php, I set the default_host to use localhost:993, then set username_domain as follows

Code: (config/main.inc.php) [Select]
$tempDomain = array_reverse(explode(".",$_SERVER['HTTP_HOST']));
$rcmail_config['username_domain'] = $tempDomain[1].".".$tempDomain[0];

I added the two blocks of code Insanity mentions and it works fine for my primary "domain1.com", exactly as I would hope.

However, I tried both a php redirect from "domain2.com/mail.php" and an actual href link directly to the actual RC directory (and the index.php) and they show up as domain1.com still, which is not desired behaviour.

To the above quoted code block, I tried changing to REFERER, but I couldn't get anything out of that to work either.

Any ideas?

Offline bpat1434

  • Administrator
  • Hero Member
  • *****
  • Posts: 673
Re: Multiple Domains Login
« Reply #21 on: February 06, 2007, 04:32:31 PM »
Hmm.... let me stew on this a bit... it can be done with sessions. It'd be a dirty hack but the basics would be like this:

1.) User goes to domain2.com/mail (or webmail.domain2.com)
2.) That index.php redirects to another index.php page with a url like: webmail.domain1.com/domain.myHostname
3.) .htaccess mod_rewrite takes the domain.myHostname and splits it, and sends that to Index.php

Lots of little steps. Or, you can send the HTTP header with the location redirection in PHP like:
header("Referer: [url]http://www.domain2.com"[/url]);
header("Location: [url]http://www.domain1.com/mail"[/url]);


Something like that might work as well. Then again, this is what virtual user tables are for ;)
 
  

Offline dgec

  • Jr. Member
  • **
  • Posts: 10
Re: Multiple Domains Login
« Reply #22 on: February 06, 2007, 09:15:15 PM »
I did 1) & 2) . 3) I don't know enough about yet.

I might try your header trick, which I prefer, 'cause I can put it in a page and program it to pick up its own url (I think) and I want to make this as generic as possible.

Virutal user tables? hmm..... M4? urk. I know just enough about linux mailers not to want to touch that.

Offline bpat1434

  • Administrator
  • Hero Member
  • *****
  • Posts: 673
Re: Multiple Domains Login
« Reply #23 on: February 06, 2007, 10:46:47 PM »
virtual user tables aren't just linux ;) They're essentially flat-file databases.... not sure how to work with them, but I know it can be done.
 
  

Offline bigpaw

  • Newbie
  • *
  • Posts: 2
Re: Multiple Domains Login
« Reply #24 on: February 08, 2007, 12:24:25 AM »
Quote from: Jonn3
Wouldn't this cause trouble if for some reason there are two equal usernames with different domains?
For example john@domain1.com, john@domain2.com

As it most likely is to cause trouble, there would be no way to get around this without entering @domain.com right?

not really, i tested this on more than one domain, with same username and it works well for me, whether you have RC installed on the mail server itself or have it installed on a web server and have RC talk to the IMAP server, it still works fine.

Quote from: bigpaw
Code: [Select]
>    if($_SERVER['SERVER_NAME'] and !preg_match('/(.*)\@(.*)/',$_POST['_user'])){
>        $domain = preg_replace('/^(\w*)\./','',$_SERVER['SERVER_NAME']);
>        $_POST['_user'] = $_POST['_user']."@".$domain;
>    }


the variable $domain will end up with a value domain.tld. it doesn't matter if the SERVER_NAME or HTTP_HOST is webmail.domain.tld or mail.domain.tld


Quote from: Brett
Hmm..... so you'd want something like:

$user $_POST['_user'].'@'.substr($_SERVER['HTTP_HOST'], 5);

right?

Bret, this is may cause some trouble.....using substr() the way you have chops off the first five characters of HTTP_HOST, if someone has a domain name that is not of the form mail.domain.tld then he is in trouble e.g if i had webmail.domain.tld, i would get il.domain.tld instead of the desired domain.tld. using explode and reversing the array as Insanity said would also work...

if you use HTTP_HOST, you will get the contents of 'host' from the current request. using SERVER_NAME will return the name of the server host under which RC is being run, if you use vhosts like i do, this value will be the value defined for that vhost.

Offline dgec

  • Jr. Member
  • **
  • Posts: 10
Re: Multiple Domains Login
« Reply #25 on: February 09, 2007, 04:46:37 PM »
Quote from: Brett
Lots of little steps. Or, you can send the HTTP header with the location redirection in PHP like:
header("Referer: [url]http://www.domain2.com"[/url]);
header("Location: [url]http://www.domain1.com/mail"[/url]);


Rats. Doesn't work. Actually, I realized that was pretty much what I was trying, and then tried to pick up Referer too, but didn't set it explicitly. Apparently doesn't matter, I still get no value on the receiving page.

I wonder, could it be something about because there's already a domain redirect, which sends
 domain2.com to domain1.com/domain2?

The domain2.com is also defined as domain2.domain1.com (cpanel).
Does the server know the url for domain2.com/mail is really
 domain1.com/domain2/mail or even domain2.domain1.com/mail
... if that makes sense?

Offline bpat1434

  • Administrator
  • Hero Member
  • *****
  • Posts: 673
Re: Multiple Domains Login
« Reply #26 on: February 10, 2007, 12:30:09 AM »
Quote from: bigpaw
Bret, this is may cause some trouble.....using substr() the way you have chops off the first five characters of HTTP_HOST, if someone has a domain name that is not of the form mail.domain.tld then he is in trouble e.g if i had webmail.domain.tld, i would get il.domain.tld instead of the desired domain.tld. using explode and reversing the array as Insanity said would also work...

if you use HTTP_HOST, you will get the contents of 'host' from the current request. using SERVER_NAME will return the name of the server host under which RC is being run, if you use vhosts like i do, this value will be the value defined for that vhost.
Well, that was a solution to the issue of one person, not meant as an overall solution.

Quote
Rats. Doesn't work. Actually, I realized that was pretty much what I was trying, and then tried to pick up Referer too, but didn't set it explicitly. Apparently doesn't matter, I still get no value on the receiving page.

I wonder, could it be something about because there's already a domain redirect, which sends
 domain2.com to domain1.com/domain2?

The domain2.com is also defined as domain2.domain1.com (cpanel).
Does the server know the url for domain2.com/mail is really
 domain1.com/domain2/mail or even domain2.domain1.com/mail
... if that makes sense?
If there's a domain redirect, no scripts / pages will be executed. The redirect happens before anything else. I guess if you needed to you could always just copy the contents of the roundcube folder X times (X = number of sites / domains) and use the same database.
 
  

Offline leroy

  • Newbie
  • *
  • Posts: 1
Re: Multiple Domains Login
« Reply #27 on: February 11, 2007, 04:02:02 AM »
I added this snippet to index.php that allows users to login without entering their domain name. I have 30+ accounts on the server and I have no idea what server they are logging in from so SERVER variables don't help. This way they only have to use their full name once, then after that they can login with just the short name. I'm making some presumptions about the users table, and it may not work if the shortname appears more than once in the table (though a selectbox could be easily hacked out for that possibility), But it degrades gracefully.

// try to log in
if ($_action=='login' && $_task=='mail')
 {

   
   $sql = "Select username from users";
   $result = mysql_query($sql);
   while($row = mysql_fetch_assoc($result)) {
      $buf = explode("@",$row['username']);
      if($_POST['_user'] == $buf[0]) {
         $_POST['_user'] .= "@".$buf[1];
         break;
      }
   }

 
 $host = $_POST['_host'] ? $_POST['_host'] : $CONFIG['default_host'];

Offline lizzie

  • Newbie
  • *
  • Posts: 8
Re: Multiple Domains Login
« Reply #28 on: February 24, 2007, 12:15:58 PM »
Quote from: Brett
Sorry, my bad. Look in /program/includes/main.inc for the function rcmail_login. Inside there you need to find this:
 // exit if IMAP login failed
 
if (!($imap_login $IMAP->connect($host$user$pass$imap_port$imap_ssl)))
  return 
FALSE;



And add just before it the line $user$_POST['_user'].'@'.substr($_SERVER['HTTP_HOST'], 5);

So you come out with:
$user $_POST['_user'].'@'.substr($_SERVER['HTTP_HOST'], 5);

 
// exit if IMAP login failed
 
if (!($imap_login $IMAP->connect($host$user$pass$imap_port$imap_ssl)))
  return 
FALSE;


Just started up on this thing and it is AMAZING.

I have implemented this hack and it works almost perfectly apart from one thing:

For some reason no user is recognised as an existing user and a new entry is created for them in the users table in the database. This means that all the settings of each individual user (timezone, identities, etc...) are lost.

Have I missed something, or is anyone else having this problem? I am using the release version, not SVN ones, but it was fine before I implemented this hack.

Thanks in advance.

Offline lizzie

  • Newbie
  • *
  • Posts: 8
Re: Multiple Domains Login
« Reply #29 on: February 24, 2007, 01:01:25 PM »
Having just posted that, I had a brainwave...

In program/include/main.inc, the $user variable was changed to the POST variable again.

$user $_POST['_user'].'@'.substr($_SERVER['HTTP_HOST'], 5);


If you put this:

// domain name hack
$user .= '@'.substr($_SERVER['HTTP_HOST'], 5);



above this:

 // query if user already registered
 
$sql_result $DB->query("SELECT user_id, username, language, preferences
              FROM "
.get_table_name('users')."
              WHERE mail_host=? AND (username=? OR alias=?)"
,
              
$host,
              
$user,
              
$user);



instead, it works a treat. It merely adds the relevant domain to the end of the $user string, then checks to see if the user is new or not.

I tied this in with Insanity5902's hack slightly modified shown below to put the "@domain.tld" on the login screen to avoid confusion:

$domain_lizzie substr($_SERVER['HTTP_HOST'], 5);

$out = <<<EOF
$form_start
$SESS_HIDDEN_FIELD
$fields[action]
<table><tr>

<td class=title"><label for="rcmloginuser">
$labels[user]</label></td>
<td>
$fields[user]</td>

<td></td>
<td style="text-align:right;letter-spacing:1px;">@
$domain_lizzie</td>
</tr><tr>

<td class="title"><label for="rcmloginpwd">
$labels[pass]</label></td>
<td>
$fields[pass]</td>
$form_host
</tr></table>
$form_end
EOF;



The bits you need to add are line 1 of the above and lines 12 + 13.

Also,

[quote="bigpaw ]
Quote from: Brett
Hmm..... so you'd want something like:

$user $_POST['_user'].'@'.substr($_SERVER['HTTP_HOST'], 5);


right?

Bret, this is may cause some trouble.....using substr() the way you have chops off the first five characters of HTTP_HOST, if someone has a domain name that is not of the form mail.domain.tld then he is in trouble e.g if i had webmail.domain.tld, i would get il.domain.tld instead of the desired domain.tld. using explode and reversing the array as Insanity said would also work...
[/quote]

Regarding this, if you change the highlighted number to 8 for webmail.domain.tld - the number of characters leading up to the bit you want. You can use the .htaccess file to ensure that the www. is omitted so as to avoid user input errors.

The $_SERVER['HTTP_HOST'] string is more reliable than some of the explode() methods which don't work for mail.domain.co.uk for example... I have both on my server.

Hope this helps someone.