2007-12-11 02:28:07 +01:00
< ? php
/**
* eGgroupWare setup - create / change eGW configuration
*
* @ link http :// www . egroupware . org
* @ author Ralf Becker < RalfBecker - AT - outdoor - training . de >
* @ package setup
2013-06-20 16:48:53 +02:00
* @ copyright ( c ) 2007 - 13 by Ralf Becker < RalfBecker - AT - outdoor - training . de >
2007-12-11 02:28:07 +01:00
* @ license http :// opensource . org / licenses / gpl - license . php GPL - GNU General Public License
2008-06-30 09:50:29 +02:00
* @ version $Id $
2007-12-11 02:28:07 +01:00
*/
/**
* setup command : create / change eGW configuration
*/
2008-06-30 09:50:29 +02:00
class setup_cmd_config extends setup_cmd
2007-12-11 02:28:07 +01:00
{
/**
* Constructor
*
* @ param string $domain string with domain - name or array with all arguments
* @ param string $config_user = null user to config the domain ( or header_admin_user )
* @ param string $config_passwd = null pw of above user
* @ param string $arguments = null array with command line argruments
* @ param boolean $verbose = false if true , echos out some status information during the run
*/
function __construct ( $domain , $config_user = null , $config_passwd = null , $arguments = null , $verbose = false )
{
if ( ! is_array ( $domain ))
{
$domain = array (
'domain' => $domain ,
'config_user' => $config_user ,
'config_passwd' => $config_passwd ,
'arguments' => $arguments ,
'verbose' => $verbose ,
);
}
//echo __CLASS__.'::__construct()'; _debug_array($domain);
admin_cmd :: __construct ( $domain );
}
/**
2007-12-13 03:41:55 +01:00
* run the command : write the configuration to the database
2008-06-30 09:50:29 +02:00
*
2007-12-11 02:28:07 +01:00
* @ param boolean $check_only = false only run the checks ( and throw the exceptions ), but not the command itself
2007-12-13 03:41:55 +01:00
* @ return string success message
2007-12-11 02:28:07 +01:00
* @ throws Exception ( lang ( 'Wrong credentials to access the header.inc.php file!' ), 2 );
* @ throws Exception ( 'header.inc.php not found!' );
*/
protected function exec ( $check_only = false )
{
if ( $check_only && $this -> remote_id )
{
return true ; // can only check locally
}
// instanciate setup object and check authorisation
$this -> check_setup_auth ( $this -> config_user , $this -> config_passwd , $this -> domain );
2008-06-30 09:50:29 +02:00
2007-12-11 02:28:07 +01:00
$this -> check_installed ( $this -> domain , 15 , $this -> verbose );
2010-09-10 11:26:17 +02:00
// fixing authtypes in self::$options
self :: auth_types ( true );
2011-05-04 15:47:44 +02:00
2007-12-11 02:28:07 +01:00
$values = array ();
if ( $this -> arguments ) // we have command line arguments
{
$save_ea_profile = $this -> _parse_cli_arguments ( $values );
}
else
{
$save_ea_profile = $this -> _parse_properties ( $values );
}
// store the config
foreach ( $values as $name => $value )
{
self :: $egw_setup -> db -> insert ( self :: $egw_setup -> config_table , array (
'config_value' => $value ,
), array (
'config_app' => 'phpgwapi' ,
'config_name' => $name ,
), __LINE__ , __FILE__ );
}
if ( count ( $values ))
{
if ( $save_ea_profile ) $this -> _save_ea_profile ();
2008-06-30 09:50:29 +02:00
2009-10-28 22:59:49 +01:00
$this -> restore_db ();
2007-12-11 02:28:07 +01:00
return lang ( 'Configuration changed.' );
}
2007-12-14 02:28:33 +01:00
$this -> restore_db ();
2007-12-11 02:28:07 +01:00
return lang ( 'Nothing to change.' );
}
2008-06-30 09:50:29 +02:00
2007-12-11 02:28:07 +01:00
/**
* Return or echo the most common config options
*
* @ param boolean $echoit = false if true the config is additionally echo ' ed out
* @ return array with name => value pairs
*/
static function get_config ( $echoit = false )
{
self :: $egw_setup -> db -> select ( self :: $egw_setup -> config_table , 'config_name,config_value' , array (
'config_app' => 'phpgwapi' ,
" (config_name LIKE '% \\ _dir' OR (config_name LIKE 'mail%' AND config_name != 'mail_footer') OR config_name LIKE 'smtp \\ _%' OR config_name LIKE 'ldap%' OR config_name IN ('webserver_url','system_charset','auth_type','account_repository')) " ,
), __LINE__ , __FILE__ );
$config = array ();
while (( $row = self :: $egw_setup -> db -> row ( true )))
{
$config [ $row [ 'config_name' ]] = $row [ 'config_value' ];
}
if ( $echoit )
{
echo lang ( 'Current configuration:' ) . " \n " ;
foreach ( $config as $name => $value )
{
echo str_pad ( $name . ':' , 22 ) . $value . " \n " ;
}
}
return $config ;
}
2008-06-30 09:50:29 +02:00
2007-12-11 02:28:07 +01:00
/**
* Available options and allowed arguments
*
* @ var array
*/
static $options = array (
'--config' => array (), // name=value,...
'--files-dir' => 'files_dir' ,
2008-11-23 14:09:51 +01:00
'--vfs-root-user' => 'vfs_root_user' ,
2007-12-11 02:28:07 +01:00
'--backup-dir' => 'backup_dir' ,
'--temp-dir' => 'temp_dir' ,
'--webserver-url' => 'webserver_url' ,
2009-09-10 14:16:29 +02:00
'--mailserver' => array ( //server,{IMAP|IMAPS|POP|POPS},[domain],[{standard(default)|vmailmgr = add domain for mailserver login|email = use email of user (Standard Maildomain should be set)}]
2007-12-11 02:28:07 +01:00
'mail_server' ,
2010-05-20 14:59:49 +02:00
array ( 'name' => 'mail_server_type' , 'allowed' => array ( 'imap' , 'imaps' ), 'default' => 'imap' ),
2007-12-11 02:28:07 +01:00
'mail_suffix' ,
array ( 'name' => 'mail_login_type' , 'allowed' => array (
2009-09-10 14:16:29 +02:00
'username (standard)' => 'standard' ,
'username@domain (virtual mail manager)' => 'vmailmgr' ,
2010-08-20 12:18:29 +02:00
'Username/Password defined by admin' => 'admin' ,
2010-08-31 12:13:28 +02:00
'userId@domain eg. u123@domain' => 'uidNumber' ,
2009-09-10 14:16:29 +02:00
'email (Standard Maildomain should be set)' => 'email' ,
2007-12-11 02:28:07 +01:00
), 'default' => 'standard' ),
),
2013-06-20 16:48:53 +02:00
'--imap' => array (
'imapAdminUsername' ,
'imapAdminPW' ,
array ( 'name' => 'imapType' , 'default' => 'defaultimap' ),
array ( 'name' => 'imapEnableCyrusAdmin' , 'default' => 'yes' ),
),
'--cyrus' => array ( // deprecated use --imap ,,cyrusimap
2007-12-11 02:28:07 +01:00
'imapAdminUsername' ,
'imapAdminPW' ,
2010-08-20 12:18:29 +02:00
array ( 'name' => 'imapType' , 'default' => 'cyrusimap' ),
2007-12-11 02:28:07 +01:00
array ( 'name' => 'imapEnableCyrusAdmin' , 'default' => 'yes' ),
),
'--sieve' => array (
2010-08-20 13:15:59 +02:00
array ( 'name' => 'imapSieveServer' ),
2007-12-11 02:28:07 +01:00
array ( 'name' => 'imapSievePort' , 'default' => 2000 ),
array ( 'name' => 'imapEnableSieve' , 'default' => 'yes' ), // null or yes
),
2013-06-20 16:48:53 +02:00
'--smtp' => array (
array ( 'name' => 'editforwardingaddress' , 'allowed' => array ( 'yes' , null )),
array ( 'name' => 'smtpType' , 'default' => 'defaultsmtp' ),
),
'--postfix' => array ( // deprecated use --smtp ,postfixldap
2007-12-11 02:28:07 +01:00
array ( 'name' => 'editforwardingaddress' , 'allowed' => array ( 'yes' , null )),
2010-08-20 12:18:29 +02:00
array ( 'name' => 'smtpType' , 'default' => 'postfixldap' ),
2007-12-11 02:28:07 +01:00
),
2013-07-15 17:27:14 +02:00
'--smtpserver' => array ( //smtp server,[smtp port],[smtp user],[smtp password],[auth=no|yes|ann]
'smtp_server' , array ( 'name' => 'smtp_port' , 'default' => 25 ), 'smtp_auth_user' , 'smtp_auth_passwd' ,
array ( 'name' => 'smtpAuth' , 'default' => 'no' , 'allowed' => array ( 'no' , 'yes' , 'ann' )),
2007-12-11 02:28:07 +01:00
),
'--account-auth' => array (
2013-06-20 16:48:53 +02:00
array ( 'name' => 'account_repository' , 'allowed' => array ( 'sql' , 'ldap' , 'ads' ), 'default' => 'sql' ),
2007-12-11 02:28:07 +01:00
array ( 'name' => 'auth_type' , 'allowed' => array ( 'sql' , 'ldap' , 'mail' , 'ads' , 'http' , 'sqlssl' , 'nis' , 'pam' ), 'default' => 'sql' ),
2011-06-06 01:22:51 +02:00
array ( 'name' => 'sql_encryption' , 'allowed' => array ( 'sha512_crypt' , 'sha256_crypt' , 'blowfish_crypt' , 'md5_crypt' , 'crypt' , 'ssha' , 'smd5' , 'md5' ), 'default' => 'sha512_crypt' ),
2007-12-11 02:28:07 +01:00
'check_save_password' , 'allow_cookie_auth' ),
'--ldap-host' => 'ldap_host' ,
'--ldap-root-dn' => 'ldap_root_dn' ,
'--ldap-root-pw' => 'ldap_root_pw' ,
'--ldap-context' => 'ldap_context' ,
'--ldap-search-filter' => 'ldap_search_filter' ,
'--ldap-group-context' => 'ldap_group_context' ,
2007-12-14 02:28:33 +01:00
'--allow-remote-admin' => 'allow_remote_admin' ,
2008-11-23 14:09:51 +01:00
'--install-id' => 'install_id' ,
2013-06-20 16:48:53 +02:00
'--ads-host' => 'ads_host' ,
'--ads-domain' => 'ads_domain' ,
'--ads-admin-user' => 'ads_domain_admin' , // eg. Administrator
'--ads-admin-pw' => 'ads_admin_pw' ,
'--ads-connection' => array (
array ( 'name' => 'ads_connection' , 'allowed' => array ( 'ssl' , 'tls' ))
),
'--ads-context' => 'ads_context' ,
2007-12-11 02:28:07 +01:00
);
2008-06-30 09:50:29 +02:00
2007-12-11 02:28:07 +01:00
/**
* Parses properties from this object
2008-06-30 09:50:29 +02:00
*
2007-12-11 02:28:07 +01:00
* @ param array & $value contains set values on return
* @ return boolean do we need to save the emailadmin profile
*/
private function _parse_properties ( & $values )
{
$this -> _merge_defaults ();
$save_ea_profile = false ;
$values = array ();
foreach ( self :: $options as $arg => $option )
{
foreach ( is_array ( $option ) ? $option : array ( $option ) as $n => $data )
{
$name = is_array ( $data ) ? $data [ 'name' ] : $data ;
2008-06-30 09:50:29 +02:00
2007-12-11 02:28:07 +01:00
if ( isset ( $this -> $name ))
2008-06-30 09:50:29 +02:00
{
2007-12-11 02:28:07 +01:00
$save_ea_profile |= $this -> _parse_value ( $arg , $n , $option , $this -> $name , $values );
}
}
}
return $save_ea_profile ;
}
/**
* Parses command line arguments in $this -> arguments
2008-06-30 09:50:29 +02:00
*
2007-12-11 02:28:07 +01:00
* @ param array & $value contains set values on return
* @ return boolean do we need to save the emailadmin profile
*/
private function _parse_cli_arguments ( & $values )
{
$arguments = $this -> arguments ;
$values = array ();
$save_ea_profile = false ;
$args = $this -> arguments ;
while (( $arg = array_shift ( $args )))
{
if ( ! isset ( self :: $options [ $arg ]))
{
throw new egw_exception_wrong_userinput ( lang ( " Unknown option '%1' !!! " , $arg ), 90 );
}
$options = is_array ( self :: $options [ $arg ]) ? explode ( ',' , array_shift ( $args )) : array ( array_shift ( $args ));
2008-06-30 09:50:29 +02:00
2007-12-11 02:28:07 +01:00
if ( $arg == '--config' )
{
foreach ( $options as $option )
{
list ( $name , $value ) = explode ( '=' , $option , 2 );
$values [ $name ] = $value ;
}
continue ;
}
$options [] = '' ; $options [] = '' ;
foreach ( $options as $n => $value )
{
$save_ea_profile |= $this -> _parse_value ( $arg , $n , self :: $options [ $arg ], $value , $values );
}
}
return $save_ea_profile ;
}
2008-06-30 09:50:29 +02:00
2007-12-11 02:28:07 +01:00
/**
* Parses a single value
*
* @ param string $arg current cli argument processed
* @ param int $n number of the property
* @ param array / string $data string with type or array containing values for type , allowed
* @ param mixed $value value to set
* @ param array & $values where the values get set
*/
private function _parse_value ( $arg , $n , $data , $value , array & $values )
{
if ( $value === '' && is_array ( $data ) && ! isset ( $data [ $n ][ 'default' ])) return false ;
2008-06-30 09:50:29 +02:00
2007-12-11 02:28:07 +01:00
$name = is_array ( $data ) || $n ? $data [ $n ] : $data ;
2008-06-30 09:50:29 +02:00
2007-12-11 02:28:07 +01:00
if ( is_array ( $name ))
{
if ( ! $value && isset ( $name [ 'default' ])) $value = $name [ 'default' ];
if ( isset ( $name [ 'allowed' ]) && ! in_array ( $value , $name [ 'allowed' ]))
{
throw new egw_exception_wrong_userinput ( lang ( " '%1' is not allowed as %2. arguments of option %3 !!! " , $value , 1 + $n , $arg ) . " ( $name[name] ) " , 91 );
}
$name = $name [ 'name' ];
}
$values [ $name ] = $value ;
2013-06-20 16:48:53 +02:00
return in_array ( $arg , array ( '--mailserver' , '--smtpserver' , '--imap' , '--cyrus' , '--smtp' , '--postfix' , '--sieve' ));
2007-12-11 02:28:07 +01:00
}
2008-06-30 09:50:29 +02:00
2007-12-11 02:28:07 +01:00
/**
* Updates the default EMailAdmin profile from the eGW config
*/
function _save_ea_profile ( $config = array ())
{
self :: $egw_setup -> db -> select ( self :: $egw_setup -> config_table , 'config_name,config_value' , array (
'config_app' => 'phpgwapi' ,
" ((config_name LIKE 'mail%' AND config_name != 'mail_footer') OR config_name LIKE 'smtp%' OR config_name LIKE 'imap%' OR config_name='editforwardingaddress') " ,
), __LINE__ , __FILE__ );
while (( $row = self :: $egw_setup -> db -> row ( true )))
{
$config [ $row [ 'config_name' ]] = $row [ 'config_value' ];
}
2013-07-15 17:27:14 +02:00
if ( ! $config [ 'smtpAuth' ]) $config [ 'smtpAuth' ] = $config [ 'smtp_auth_user' ] ? 'yes' : null ;
2008-06-30 09:50:29 +02:00
2012-04-24 19:45:19 +02:00
$emailadmin = new emailadmin_bo ( false , false ); // false=no session stuff
2007-12-11 02:28:07 +01:00
$emailadmin -> setDefaultProfile ( $config );
2008-06-30 09:50:29 +02:00
2007-12-11 02:28:07 +01:00
if ( $this -> verbose )
{
echo " \n " . lang ( 'EMailAdmin profile updated:' ) . " \n " ;
foreach ( $config as $name => $value )
{
echo str_pad ( $name . ':' , 22 ) . $value . " \n " ;
}
}
}
/**
* Return the options from the $options array
*
* @ return array with name => array ( value => label , ... ) pairs
*/
static function options ()
{
$options = array ();
foreach ( self :: $options as $option )
{
if ( is_array ( $option ))
{
foreach ( $option as $n => $data )
{
if ( is_array ( $data ) && isset ( $data [ 'allowed' ]))
{
2010-09-10 11:26:17 +02:00
if ( $data [ 'name' ] == 'auth_type' )
{
$options [ $data [ 'name' ]] = self :: auth_types ();
continue ;
}
2007-12-11 02:28:07 +01:00
foreach ( $data [ 'allowed' ] as $label => $value )
{
if ( is_int ( $label ))
{
$label = ( string ) $value === '' ? 'No' : strtoupper ( $value );
}
$options [ $data [ 'name' ]][ $value ] = lang ( $label );
}
}
}
}
}
return $options ;
}
2011-05-04 15:47:44 +02:00
2010-09-10 11:26:17 +02:00
/**
* Read auth - types ( existing auth backends ) from filesystem and fix our $options array
2011-05-04 15:47:44 +02:00
*
2010-09-10 11:26:17 +02:00
* @ return array
*/
static function auth_types ()
{
// default backends in order of importance
static $auth_types = array (
'sql' => 'SQL' ,
'ldap' => 'LDAP' ,
'mail' => 'Mail' ,
'ads' => 'Active Directory' ,
'http' => 'HTTP' ,
'fallback' => 'Fallback LDAP --> SQL' ,
2011-03-23 14:19:48 +01:00
'fallbackmail2sql' => 'Fallback Mail --> SQL' ,
2010-09-10 11:26:17 +02:00
'sqlssl' => 'SQL / SSL' ,
);
static $scan_done ;
if ( ! $scan_done ++ )
{
// now add auth backends found in filesystem
foreach ( scandir ( EGW_INCLUDE_ROOT . '/phpgwapi/inc' ) as $class )
{
if ( preg_match ( '/^class\.auth_([a-z]+)\.inc\.php$/' , $class , $matches ) &&
! isset ( $auth_types [ $matches [ 1 ]]))
{
$auth_types [ $matches [ 1 ]] = ucfirst ( $matches [ 1 ]);
}
}
foreach ( self :: $options [ '--account-auth' ] as & $param )
{
if ( $param [ 'name' ] == 'auth_type' )
{
$param [ 'allowed' ] = array_keys ( $auth_types );
break ;
}
}
}
return $auth_types ;
}
2007-12-11 02:28:07 +01:00
/**
* Return the defaults from the $options array
*
* @ return array with name => $value pairs
*/
static function defaults ()
{
$defaults = array ();
// fetch the default from the cli options
foreach ( self :: $options as $option )
{
if ( is_array ( $option ))
{
foreach ( $option as $n => $data )
{
if ( is_array ( $data ) && isset ( $data [ 'default' ]))
{
$defaults [ $data [ 'name' ]] = $data [ 'default' ];
}
}
}
}
// some extra defaults for non-cli operation
$defaults [ 'files_dir' ] = '/var/lib/egroupware/$domain/files' ;
$defaults [ 'backup_dir' ] = '/var/lib/egroupware/$domain/backup' ;
2009-08-27 14:46:12 +02:00
$defaults [ 'backup_mincount' ] = 0 ;
$defaults [ 'backup_files' ] = false ;
2007-12-11 02:28:07 +01:00
$defaults [ 'temp_dir' ] = '/tmp' ;
$defaults [ 'webserver_url' ] = '/egroupware' ;
2010-05-28 15:19:23 +02:00
$defaults [ 'smtp_server' ] = 'localhost' ;
//$defaults['mail_server'] = 'localhost';
2007-12-11 02:28:07 +01:00
$defaults [ 'mail_suffix' ] = '$domain' ;
$defaults [ 'imapAdminUsername' ] = 'cyrus@$domain' ;
$defaults [ 'imapAdminPW' ] = self :: randomstring ();
2010-08-20 13:15:59 +02:00
$defaults [ 'imapType' ] = 'defaultimap' ; // standard IMAP
$defaults [ 'smtpType' ] = 'defaultsmtp' ; // standard SMTP
2008-06-30 09:50:29 +02:00
2007-12-11 02:28:07 +01:00
return $defaults ;
}
/**
* Merges the default into the current properties , if they are empty or contain placeholders
2008-06-30 09:50:29 +02:00
*
2007-12-11 02:28:07 +01:00
* Replacements like $domain , only work for values listed by self :: defaults ()
*/
private function _merge_defaults ()
{
foreach ( self :: defaults () as $name => $default )
{
if ( ! $this -> $name )
{
//echo "<p>setting $name='{$this->$name}' to it's default='$default'</p>\n";
$this -> set_defaults [ $name ] = $this -> $name = $default ;
}
if ( strpos ( $this -> $name , '$' ) !== false )
{
2010-08-25 14:24:11 +02:00
$this -> set_defaults [ $name ] = $this -> $name = str_replace ( array (
2007-12-11 02:28:07 +01:00
'$domain' ,
), array (
$this -> domain ,
), $this -> $name );
}
2008-06-30 09:50:29 +02:00
}
2007-12-11 02:28:07 +01:00
}
}