Author Topic: Settings From Data Base  (Read 3182 times)

Offline davidmpaz

  • Jr. Member
  • **
  • Posts: 11
Settings From Data Base
« on: March 03, 2009, 09:44:40 AM »
Hi people, maybe in my last post I did not make the right question.

Please I need to set some config var from values in a Database and the RC use them, can anyone tell me the right place to do that, I have try to put my code directly in main.inc.php, also try to get the data in index.php but all efforts failed. When added in index, the explorer show me a message about wrong compression type or something, and when added in main.inc.php, well I wasn't able to call some function I need from the system where I have the RC embeded

Please I really need to achieve this :( , can any body give me some idea?

Thanks in advance, David.

Offline davidmpaz

  • Jr. Member
  • **
  • Posts: 11
Possible Solution ??
« Reply #1 on: March 05, 2009, 04:39:42 PM »
Hi , after a couple  of days working on my issue, I did implement a solution which basically do what I want: Replace the configuration of the RC main.inc.php file, with configuration from a database.
This is what I did:

In main.inc.php I added this entries
//Start Database Configurations

// table containing the users configs, leave blank ('') if will not used
$rcmail_config['db_config']['table_name'] = 'my_users';

//The $rcmail_config['db_config']['field'] entries are database field names, where
//the config value needed is extracted for a config option

//Fields For User Identities, NOT MAIL IDENTITIES 
//else, user credentials: email or username, mail password, user email host
//all this data can be sent from the login page or 
//from an ajax post to the login page: http://<RC_HOME>/?_task=mail&_action=login
$rcmail_config['db_config']['field']['email'] = 'email';
$rcmail_config['db_config']['field']['username'] = 'email_username';
$rcmail_config['db_config']['field']['password'] = 'email_password';

// field containing default server value
$rcmail_config['db_config']['field']['default_host'] = 'email_server';

// field containing default server port value
$rcmail_config['db_config']['field']['default_port'] = 'email_server_port';

// field containing a integer value standing if ssl is used 
// 1: ssl is used
// 0: ssl is not used
// this is not a config option of RC, but used in implemented solution 
// to costruct the URL of the "default_host"
$rcmail_config['db_config']['field']['ssl_support'] = 'email_is_ssl';

// field containing ssl server port value
// this is not a config option of RC, but used in implemented solution 
// to costruct the URL of the "default_host"
$rcmail_config['db_config']['field']['ssl_port'] = 'email_ssl_port';

// field containing smtp server value
$rcmail_config['db_config']['field']['smtp_server'] = 'email_server_smtp';

// field containing smtp server port value
$rcmail_config['db_config']['field']['smtp_port'] = 'email_server_smtp_port';

// field containing smtp server port value
// this is not a config option of RC, but used in implemented solution 
// to costruct the URL of the "smtp_server"
$rcmail_config['db_config']['field']['smtp_ssl_support'] = 'email_is_ssl_smtp';

// field containing smtp server port value
$rcmail_config['db_config']['field']['smtp_user'] = 'email_server_smtp_user';

// field containing smtp server port value
$rcmail_config['db_config']['field']['smtp_pass'] = 'email_server_smtp_pass';

//End of Database Configurations


These are the config option used by the function I create in the file: /program/include/rcmail.php, here is the function:

/**
   * Replace Config object with settings extracted from table : 
   * $rcmail_config['db_config']['table_name'], all parameters
   * must be set.
   *
   * @param string Email Username/User Email
   * @param string User Password
   * @param string Imap server or default host
   */
  
public function replace_settings_from_db($username$pass$host)
  {
  
	
$config_all $this->config->all();
  
	

  
	
//if not set the value, do nothing
  
	
if (
$config_all['db_config']['table_name'] == '')
  
	
	
return;
  
	
else
  
	
{
  
	
	
//Constructing the select part of query selecting all field from db
  
	
	
$select  "SELECT ";
  
	
	
$select .= implode(","$config_all['db_config']['field']);
  
	
	
$select .= " FROM ".$config_all['db_config']['table_name'];
  
	
	

  
	
	
$where  sprintf(" WHERE ( %s = '%s' OR %s = '%s' ) AND %s = '%s' ",
  
	
	
	
 
	
$config_all['db_config']['field']['email'], $username,
  
	
	
	
 
	
$config_all['db_config']['field']['username'], $username,
  
	
	
	
 
	
$config_all['db_config']['field']['password'], $pass
  
	
	
	
 ); 
  
	
	

  
	
	
$query $select.$where;
  
	
	

  
	
	
$sql_result $this->db->query($query);
  
	
	
if (
$sql_arr $this->db->fetch_assoc($sql_result))
  
	
	
{
  
	
	
	
$ssl=
	
	
$config_all['db_config']['field']['ssl_support'];
  
	
	
	
$smtp_ssl=
	
$config_all['db_config']['field']['smtp_ssl_support'];
  
	
	
	
$ssl_port =
	
$config_all['db_config']['field']['ssl_port'];
  
	
	
	
$server =
	
$config_all['db_config']['field']['default_host']; 
  
	
	
	
$smtp_server =
	
$config_all['db_config']['field']['smtp_server']; 
	
	
	

  
	
	
	
if( 
$sql_arr["$ssl"] == )
  
	
	
	
	
$config_all['default_host'] = 
  
	
	
	
	
	
"ssl://".$sql_arr["$server"].":".$sql_arr["$ssl_port"];
  
	
	
	
else
  
	
	
	
	
$config_all['default_host'] = $sql_arr["$server"];
  
	
	
	

  
	
	
	
if( 
$sql_arr["$smtp_ssl"] == )
  
	
	
	
	
$config_all['smtp_server'] = 
  
	
	
	
	
	
"ssl://".$sql_arr["$smtp_server"];
  
	
	
	
else
  
	
	
	
	
$config_all['smtp_server'] = $sql_arr["$smtp_server"];
	
	
	

	
	
	
foreach(
$config_all['db_config']['field'] as $name => $value)
	
	
	
{
	
	
	
	
if( !(
$name == "default_host" || $name == "smtp_server") )
	
	
	
	
	
//not overwrite the already set default host and smtp server
	
	
	
	
	
$config_all["$name"] = $sql_arr["$value"];
	
	
	
}
  
	
	
	

  
	
	
}
  
	
	

  
	
	
//overwrite the old config
  
	
	
$this->config->merge($config_all);
                
//I made a raise_error here with an string representation of the $this->config array as message and everything was fine, all values were ok, at least as I espect it.
  
	
	
return;
  
	
}
  }


And this function is called from the login function in the same file, before doing anything in it:


function login($username$pass$host=NULL)
  {
  
	
//Extract setting from DB and overwrite
  
	
$this->replace_settings_from_db($username$pass$host);
  
	
$host NULL;
  
	

    
$user NULL;
    
$config $this->config->all();

    if (!
$host)
      
$host $config['default_host'];
        .
        .
        .


Well, untill here that's all, but I'm facing problem, fundamentally when sending mails (I could be able to conect to my gmail account, read but not send). I also can read message from my mail server and notice something; I couldn't be able to send messages untill I fill the smtp_server option (I leaved it blank,beacuse I was setting it from DB ) , tha's why I think the config are replace in some other place.

Please can somebody tell me if I'm doing things wrong ?

Are these settings overwritten with the ones in the main.inc.php file in a later context ?

Any help will be great since I'm really stuck. :-X have no more Ideas

Greeting David.