View Single Post
  #5  
Old 29th October 2010, 19:11
necromncr necromncr is offline
Junior Member
 
Join Date: Jul 2008
Posts: 9
Thanks: 1
Thanked 6 Times in 3 Posts
Default

Due to extreme interest (3 people ;-) awesome!) here is my ISPConfig 2.x for Roundcube 0.4.2 integration plugin.

1.) Make a MySQL user to access ISPConfig database. I myself prefer to give just the right amount of privileges - in my case I used username "roundcube":
Code:
GRANT SELECT (web_domain, doc_id, doctype_id) ON `db_ispconfig`.`isp_isp_web` TO 'roundcube'@'localhost'
GRANT SELECT (user_username, doc_id, user_emailalias, user_name) ON `db_ispconfig`.`isp_isp_user` TO 'roundcube'@'localhost'
GRANT SELECT (child_doctype_id, child_doc_id, parent_doctype_id, parent_doc_id) ON `db_ispconfig`.`isp_dep` TO 'roundcube'@'localhost'
Don't forget to set a password!

2.) First make a folder in your Roundcube installation called "ispconfig_addons".

3.) Make two files, "config.inc.php" and "ispconfig_addons.php".

4.) Fill config.inc.php with this code:
Code:
<?php                                                                                                                                                                                                          
$rcmail_config['isp_db_name'] = 'db_ispconfig'; // your ISPConfig database
$rcmail_config['isp_db_user'] = 'roundcube'; // username for ISPConfig database from step #1
$rcmail_config['isp_db_pass'] = '<password>'; // extremly long, complicated and just impossible to guess password from step #1
?>
5.) Fill ispconfig_addons.php with this code:
Code:
<?php

/**                                                                                                                                                                                                            
 * ISPConfig v2.x integration for Roundcube 0.4.2 plugin                                                                                                                                                       
 *                                                                                                                                                                                                             
 * Adds ability to authenticate against ISPConfig 2.x user database.                                                                                                                                           
 *                                                                                                                                                                                                             
 * To use you must have config.inc.php file following settings:                                                                                                                                                
 * isp_db_name - ISPConfig database name                                                                                                                                                                       
 * isp_db_user - user with access to only selected fields in selected tables                                                                                                                                   
 * isp_db_pass - password for that user                                                                                                                                                                        
 *                                                                                                                                                                                                             
 * Code is tested however there is no waranty for it. Use at your own risk. Feedback is welcome!                                                                                                               
 *                                                                                                                                                                                                             
 * @version 0.1                                                                                                                                                                                                
 * @author Janez Dolinar s.p., <info@tuksi.net>                                                                                                                                                                
 */   
class ispconfig_addons extends rcube_plugin
{  
  function init()
  {
    $this->add_hook('user_create', array($this, 'usercreate'));
    $this->add_hook('login_after', array($this, 'loginafter'));

    $this->load_config();
  }
                                                                                                                                                                                                               
  function loginafter($query) {                                                                                                                                                                                
    $rcmail = rcmail::get_instance();                                                                                                                                                                          
                                                                                                                                                                                                               
    if (isset($_SESSION['userFullName'])) {                                                                                                                                                                    
      $sql = 'UPDATE identities SET name=?, email=? WHERE identity_id = ?';                                                                                                                                    
                                                                                                                                                                                                               
      $rcmail->db->query($sql,$_SESSION['userFullName'], $_SESSION['userEmail'], $rcmail->user->data['user_id']);                                                                                              
                                                                                                                                                                                                               
      unset($_SESSION['userFullName']);                                                                                                                                                                        
      unset($_SESSION['userEmail']);                                                                                                                                                                           
    }                                                                                                                                                                                                          
    return $query;
  }
  
  
  function usercreate($args)
  {
    $rcmail = rcmail::get_instance();
    if (($rcmail->config->get('isp_db_name')=='') || ($rcmail->config->get('isp_db_user')=='') || ($rcmail->config->get('isp_db_pass')=='')) {
      return $args;
    } else {
      $sql = "SELECT iiu.user_username, iiu.user_emailalias, iiu.user_name, iid.web_domain FROM isp_isp_user iiu, isp_dep id, isp_isp_web iid "
            ."WHERE iiu.user_username = '%s'  AND iiu.doc_id = id.child_doc_id  AND id.child_doctype_id = 1014  AND iid.doc_id = id.parent_doc_id";
            
      $mysql = new MySQLi('localhost', $rcmail->config->get('isp_db_user'), $rcmail->config->get('isp_db_pass'), $rcmail->config->get('isp_db_name'));
      
      if ($mysql->connect_error) return false;
      $result = $mysql->query(sprintf($sql,addslashes($args['user'])));

      if ($result) {
        $row = $result->fetch_object();
        $userEmail = explode("\n",str_replace("\r","",$row->user_emailalias));
        $args['alias'] = $userEmail[0].'@'.$row->web_domain;
        $_SESSION['userFullName'] = $row->user_name;
        $_SESSION['userEmail'] = $userEmail[0].'@'.$row->web_domain;
        $result->close();
      }
      
      $mysql->close();
    }
  
    return $args;
  }

}
?>
6.a) make sure it's web-server readable!

7.) Add the plugin to your Roundcube config file /config/main.inc.php :
Code:
// ----------------------------------                                                                                                                                                                          
// PLUGINS                                                                                                                                                                                                     
// ----------------------------------                                                                                                                                                                          
                                                                                                                                                                                                               
// List of active plugins (in plugins/ directory)                                                                                                                                                              
$rcmail_config['plugins'] = array('virtuser_file','ispconfig_addons');
Also don't forget to configure virtuser_file plugin. Add this to the end of config/main.inc.php file:

Code:
                                                                                                                                                                                                               
$rcmail_config['virtuser_file'] = '/etc/postfix/virtusertable';
This is valid for Ubuntu / Debian users. This is just a guess, find your location and modify this line accordingly.

Code uses MySQLi extension so you'll need that as well however I think it should be no problem changing it to MySQL extension.

That should be it! You / your clients can now login with any email set for their account and their password! Their names should already be set to ther ISPConfig full names and default email address.

Please note I made this after a long hard working day and was tired. There could be some room for improvement or even some flaws. I ask for your feedback, I'll be glad to fix the code. Oh, and no waranty

Enjoy!

Last edited by necromncr; 31st October 2010 at 11:52. Reason: Oh, could they just release v0.4.3? Made the same typo AGAIN!
Reply With Quote