2001-07-30 17:59:25 +02:00
< ? php
/************************************************************************** \
2004-01-27 21:49:25 +01:00
* eGroupWare - Setup *
* http :// www . egroupware . org *
2001-07-30 17:59:25 +02:00
* -------------------------------------------- *
* This program is free software ; you can redistribute it and / or modify it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation ; either version 2 of the License , or ( at your *
* option ) any later version . *
\ **************************************************************************/
/* $Id$ */
2005-03-04 13:40:28 +01:00
$GLOBALS [ 'egw_info' ] = array (
'flags' => array (
'noheader' => True ,
'nonavbar' => True ,
'currentapp' => 'home' ,
'noapi' => True
));
2001-07-30 17:59:25 +02:00
include ( './inc/functions.inc.php' );
// Authorize the user to use setup app and load the database
2005-03-04 13:40:28 +01:00
if ( ! $GLOBALS [ 'egw_setup' ] -> auth ( 'Config' ))
2001-07-30 17:59:25 +02:00
{
Header ( 'Location: index.php' );
exit ;
}
// Does not return unless user is authorized
2001-12-18 03:20:31 +01:00
2005-05-13 15:41:26 +02:00
class egw
2001-07-30 17:59:25 +02:00
{
var $common ;
var $accounts ;
var $applications ;
var $db ;
}
2006-06-03 21:32:54 +02:00
$egw =& new egw ;
$egw -> common =& CreateObject ( 'phpgwapi.common' );
2001-07-30 17:59:25 +02:00
2006-06-03 21:32:54 +02:00
$common =& $egw -> common ;
2005-03-04 13:40:28 +01:00
$GLOBALS [ 'egw_setup' ] -> loaddb ();
2005-05-13 15:41:26 +02:00
$egw -> db = clone ( $GLOBALS [ 'egw_setup' ] -> db );
2001-07-30 17:59:25 +02:00
2005-03-04 13:40:28 +01:00
$tpl_root = $GLOBALS [ 'egw_setup' ] -> html -> setup_tpl_dir ( 'setup' );
2002-08-12 01:54:58 +02:00
$setup_tpl = CreateObject ( 'setup.Template' , $tpl_root );
2001-07-30 17:59:25 +02:00
$setup_tpl -> set_file ( array (
'ldap' => 'ldap.tpl' ,
'T_head' => 'head.tpl' ,
'T_footer' => 'footer.tpl' ,
'T_alert_msg' => 'msg_alert_msg.tpl'
));
2006-06-07 01:44:50 +02:00
function hash_sql2ldap ( $hash )
{
switch ( strtolower ( $GLOBALS [ 'egw_info' ][ 'server' ][ 'sql_encryption_type' ]))
{
case '' : // not set sql_encryption_type
case 'md5' :
$hash = '{md5}' . base64_encode ( pack ( " H* " , $hash ));
break ;
case 'crypt' :
$hash = '{crypt}' . $hash ;
break ;
}
return $hash ;
}
2005-11-02 12:45:52 +01:00
$GLOBALS [ 'egw_setup' ] -> db -> select ( $GLOBALS [ 'egw_setup' ] -> config_table , 'config_name,config_value' , array (
" config_name LIKE 'ldap%' " ,
), __LINE__ , __FILE__ );
2005-03-04 13:40:28 +01:00
while ( $GLOBALS [ 'egw_setup' ] -> db -> next_record ())
2001-07-30 17:59:25 +02:00
{
2005-11-24 21:49:23 +01:00
$GLOBALS [ 'egw_info' ][ 'server' ][ $GLOBALS [ 'egw_setup' ] -> db -> f ( 'config_name' )] = $GLOBALS [ 'egw_setup' ] -> db -> f ( 'config_value' );
2001-07-30 17:59:25 +02:00
}
2005-11-24 21:49:23 +01:00
$GLOBALS [ 'egw_info' ][ 'server' ][ 'account_repository' ] = 'ldap' ;
2002-03-03 22:53:00 +01:00
2006-06-03 21:32:54 +02:00
$egw -> accounts =& CreateObject ( 'phpgwapi.accounts' );
$acct =& $egw -> accounts ;
2002-03-03 22:53:00 +01:00
2001-07-30 17:59:25 +02:00
// First, see if we can connect to the LDAP server, if not send `em back to config.php with an
// error message.
// connect to ldap server
2002-03-03 22:53:00 +01:00
if ( ! $ldap = $common -> ldapConnect ())
2001-07-30 17:59:25 +02:00
{
$noldapconnection = True ;
}
2002-03-03 22:53:00 +01:00
if ( $noldapconnection )
2001-07-30 17:59:25 +02:00
{
Header ( 'Location: config.php?error=badldapconnection' );
exit ;
}
2006-06-03 21:32:54 +02:00
// read all accounts & groups direct from SQL for export
$group_info = $account_info = array ();
$GLOBALS [ 'egw_setup' ] -> db -> select ( $GLOBALS [ 'egw_setup' ] -> accounts_table , '*' , false , __LINE__ , __FILE__ );
while (( $row = $GLOBALS [ 'egw_setup' ] -> db -> row ( true )))
2001-07-30 17:59:25 +02:00
{
2006-06-03 21:32:54 +02:00
if ( $row [ 'account_type' ] == 'u' ) // account
{
$account_info [ $row [ 'account_id' ]] = $row ;
}
else // group
{
$row [ 'account_id' ] *= - 1 ; // group account_id is internally negative since 1.2
$group_info [( string ) $row [ 'account_id' ]] = $row ;
}
2001-07-30 17:59:25 +02:00
}
2006-06-03 21:32:54 +02:00
if ( $_POST [ 'cancel' ])
2001-07-30 17:59:25 +02:00
{
2002-03-03 22:53:00 +01:00
Header ( 'Location: ldap.php' );
2001-07-30 17:59:25 +02:00
exit ;
}
2006-06-03 21:32:54 +02:00
$GLOBALS [ 'egw_setup' ] -> html -> show_header ( lang ( 'LDAP Export' ), False , 'config' , $GLOBALS [ 'egw_setup' ] -> ConfigDomain . '(' . $GLOBALS [ 'egw_domain' ][ $GLOBALS [ 'egw_setup' ] -> ConfigDomain ][ 'db_type' ] . ')' );
if ( $_POST [ 'submit' ])
2001-07-30 17:59:25 +02:00
{
2006-06-03 21:32:54 +02:00
if ( $_POST [ 'users' ])
2001-07-30 17:59:25 +02:00
{
2006-06-03 21:32:54 +02:00
foreach ( $_POST [ 'users' ] as $accountid )
2001-07-30 17:59:25 +02:00
{
2006-06-03 21:32:54 +02:00
if ( ! isset ( $account_info [ $accountid ])) continue ;
$accounts =& CreateObject ( 'phpgwapi.accounts' ,( int ) $accountid );
2001-07-30 17:59:25 +02:00
2006-06-03 21:32:54 +02:00
// check if user already exists in ldap
if ( $accounts -> exists ( $accountid ))
2001-07-30 17:59:25 +02:00
{
2006-06-03 21:32:54 +02:00
echo '<p>' . lang ( '%1 already exists in LDAP.' , lang ( 'User' ) . " $accountid ( { $account_info [ $accountid ][ 'account_lid' ] } ) " ) . " </p> \n " ;
continue ;
}
$account_info [ $accountid ][ 'homedirectory' ] = $GLOBALS [ 'egw_info' ][ 'server' ][ 'ldap_account_home' ] . '/' . $account_info [ $accountid ][ 'account_lid' ];
$account_info [ $accountid ][ 'loginshell' ] = $GLOBALS [ 'egw_info' ][ 'server' ][ 'ldap_account_shell' ];
2006-06-07 01:44:50 +02:00
$account_info [ $accountid ][ 'account_passwd' ] = hash_sql2ldap ( $account_info [ $accountid ][ 'account_passwd' ]);
2001-07-30 17:59:25 +02:00
2006-06-03 21:32:54 +02:00
if ( ! $accounts -> create ( $account_info [ $accountid ]))
{
echo '<p>' . lang ( 'Creation of %1 in LDAP failed !!!' , lang ( 'User' ) . " $accountid ( { $account_info [ $accountid ][ 'account_lid' ] } ) " ) . " </p> \n " ;
continue ;
2001-07-30 17:59:25 +02:00
}
2006-06-03 21:32:54 +02:00
echo '<p>' . lang ( '%1 created in LDAP.' , lang ( 'User' ) . " $accountid ( { $account_info [ $accountid ][ 'account_lid' ] } ) " ) . " </p> \n " ;
2001-07-30 17:59:25 +02:00
}
}
2006-06-03 21:32:54 +02:00
if ( $_POST [ 'ldapgroups' ])
2001-07-30 17:59:25 +02:00
{
2006-06-03 21:32:54 +02:00
foreach ( $_POST [ 'ldapgroups' ] as $groupid )
2001-07-30 17:59:25 +02:00
{
2006-06-03 21:32:54 +02:00
if ( ! isset ( $group_info [ $groupid ])) continue ;
2001-07-30 17:59:25 +02:00
2006-06-03 21:32:54 +02:00
$groups =& CreateObject ( 'phpgwapi.accounts' ,( int ) $groupid );
// check if group already exists in ldap
if ( ! $groups -> exists ( $groupid ))
2001-07-30 17:59:25 +02:00
{
2006-06-03 21:32:54 +02:00
if ( ! $groups -> create ( $group_info [ $groupid ]))
2001-07-30 17:59:25 +02:00
{
2006-06-03 21:32:54 +02:00
echo '<p>' . lang ( 'Creation of %1 failed !!!' , lang ( 'Group' ) . " $groupid ( { $group_info [ $groupid ][ 'account_lid' ] } ) " ) . " </p> \n " ;
continue ;
2001-07-30 17:59:25 +02:00
}
2006-06-03 21:32:54 +02:00
echo '<p>' . lang ( '%1 created in LDAP.' , lang ( 'Group' ) . " $groupid ( { $group_info [ $groupid ][ 'account_lid' ] } ) " ) . " </p> \n " ;
}
else
{
echo '<p>' . lang ( '%1 already exists in LDAP.' , lang ( 'Group' ) . " $groupid ( { $group_info [ $groupid ][ 'account_lid' ] } ) " ) . " </p> \n " ;
if ( $groups -> id2name ( $groupid ) != $group_info [ $groupid ][ 'account_lid' ])
2001-07-30 17:59:25 +02:00
{
2006-06-03 21:32:54 +02:00
continue ; // different group under that gidnumber in ldap!
2001-07-30 17:59:25 +02:00
}
}
2006-06-03 21:32:54 +02:00
// now saving / updating the memberships
$groups -> read_repository ();
if ( ! is_object ( $GLOBALS [ 'egw' ] -> acl ))
{
$GLOBALS [ 'egw' ] -> acl =& CreateObject ( 'phpgwapi.acl' );
}
$groups -> save_repository ();
2001-07-30 17:59:25 +02:00
}
}
$setup_complete = True ;
}
2002-03-03 22:53:00 +01:00
if ( $error )
2001-07-30 17:59:25 +02:00
{
2005-07-11 20:02:19 +02:00
//echo '<br /><center><b>Error:</b> '.$error.'</center>';
2005-03-04 13:40:28 +01:00
$GLOBALS [ 'egw_setup' ] -> html -> show_alert_msg ( 'Error' , $error );
2001-07-30 17:59:25 +02:00
}
2002-03-03 22:53:00 +01:00
if ( $setup_complete )
2001-07-30 17:59:25 +02:00
{
2005-07-11 20:02:19 +02:00
echo '<br /><center>' . lang ( 'Export has been completed! You will need to set the user passwords manually.' ) . '</center>' ;
echo '<br /><center>' . lang ( 'Click <a href="index.php">here</a> to return to setup.' ) . '</center>' ;
2005-03-04 13:40:28 +01:00
$GLOBALS [ 'egw_setup' ] -> html -> show_footer ();
2001-07-30 17:59:25 +02:00
exit ;
}
$setup_tpl -> set_block ( 'ldap' , 'header' , 'header' );
$setup_tpl -> set_block ( 'ldap' , 'user_list' , 'user_list' );
$setup_tpl -> set_block ( 'ldap' , 'admin_list' , 'admin_list' );
$setup_tpl -> set_block ( 'ldap' , 'group_list' , 'group_list' );
$setup_tpl -> set_block ( 'ldap' , 'app_list' , 'app_list' );
$setup_tpl -> set_block ( 'ldap' , 'submit' , 'submit' );
$setup_tpl -> set_block ( 'ldap' , 'footer' , 'footer' );
2006-06-03 21:32:54 +02:00
foreach ( $account_info as $account )
2001-07-30 17:59:25 +02:00
{
2006-06-03 21:32:54 +02:00
$user_list .= '<option value="' . $account [ 'account_id' ] . '" selected="1">'
2001-07-30 17:59:25 +02:00
. $common -> display_fullname ( $account [ 'account_lid' ], $account [ 'account_firstname' ], $account [ 'account_lastname' ])
. '</option>' ;
}
2006-06-03 21:32:54 +02:00
foreach ( $group_info as $group )
2001-07-30 17:59:25 +02:00
{
2006-06-03 21:32:54 +02:00
$group_list .= '<option value="' . $group [ 'account_id' ] . '" selected="1">'
2001-07-30 17:59:25 +02:00
. $group [ 'account_lid' ]
. '</option>' ;
}
$setup_tpl -> set_var ( 'action_url' , 'ldapexport.php' );
$setup_tpl -> set_var ( 'users' , $user_list );
$setup_tpl -> set_var ( 'admins' , $admin_list );
$setup_tpl -> set_var ( 'ldapgroups' , $group_list );
$setup_tpl -> set_var ( 's_apps' , $app_list );
$setup_tpl -> set_var ( 'ldap_import' , lang ( 'LDAP export users' ));
2003-12-19 13:34:45 +01:00
$setup_tpl -> set_var ( 'description' , lang ( " This section will help you export users and groups from eGroupWare's account tables into your LDAP tree " ) . '.' );
2001-07-30 17:59:25 +02:00
$setup_tpl -> set_var ( 'select_users' , lang ( 'Select which user(s) will be exported' ));
$setup_tpl -> set_var ( 'select_groups' , lang ( 'Select which group(s) will be exported (group membership will be maintained)' ));
$setup_tpl -> set_var ( 'form_submit' , 'export' );
$setup_tpl -> set_var ( 'cancel' , lang ( 'Cancel' ));
$setup_tpl -> pfp ( 'out' , 'header' );
if ( $account_info )
{
$setup_tpl -> pfp ( 'out' , 'user_list' );
}
2002-03-03 22:53:00 +01:00
if ( $group_info )
2001-07-30 17:59:25 +02:00
{
$setup_tpl -> pfp ( 'out' , 'group_list' );
}
$setup_tpl -> pfp ( 'out' , 'submit' );
$setup_tpl -> pfp ( 'out' , 'footer' );
2002-03-03 22:53:00 +01:00
2005-03-04 13:40:28 +01:00
$GLOBALS [ 'egw_setup' ] -> html -> show_footer ();