2015-12-11 05:08:32 +01:00
#!/usr/bin/env php
2012-10-24 21:19:10 +02:00
< ? php
/**
* Copyright ( C ) 2005 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
* Copyright ( C ) 2006 - 2012 Laurent Destailleur < eldy @ users . sourceforge . net >
*
* 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
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2012-10-24 21:19:10 +02:00
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http :// www . gnu . org / licenses />.
*/
/**
* \file scripts / user / sync_users_ldap2dolibarr . php
* \ingroup ldap member
* \brief Script to update users into Dolibarr from LDAP
*/
$sapi_type = php_sapi_name ();
$script_file = basename ( __FILE__ );
$path = dirname ( __FILE__ ) . '/' ;
// Test if batch mode
if ( substr ( $sapi_type , 0 , 3 ) == 'cgi' ) {
2013-01-10 16:24:07 +01:00
echo " Error: You are using PHP for CGI. To execute " . $script_file . " from command line, you must use PHP for CLI mode. \n " ;
2013-06-09 14:40:33 +02:00
exit ( - 1 );
2012-10-24 21:19:10 +02:00
}
2013-06-05 16:12:07 +02:00
require_once ( $path . " ../../htdocs/master.inc.php " );
require_once ( DOL_DOCUMENT_ROOT . " /core/lib/date.lib.php " );
require_once ( DOL_DOCUMENT_ROOT . " /core/class/ldap.class.php " );
require_once ( DOL_DOCUMENT_ROOT . " /user/class/user.class.php " );
$langs -> load ( " main " );
$langs -> load ( " errors " );
2013-05-15 11:24:18 +02:00
// Global variables
$version = DOL_VERSION ;
2012-10-24 21:19:10 +02:00
$error = 0 ;
$forcecommit = 0 ;
2014-07-05 12:27:35 +02:00
$excludeuser = array ();
2015-09-11 20:36:45 +02:00
$confirmed = 0 ;
2012-10-24 21:19:10 +02:00
2013-05-15 11:24:18 +02:00
/*
* Main
*/
@ set_time_limit ( 0 );
2014-07-31 17:35:58 +02:00
print " ***** " . $script_file . " ( " . $version . " ) pid= " . dol_getmypid () . " ***** \n " ;
2013-07-13 21:14:05 +02:00
dol_syslog ( $script_file . " launched with arg " . join ( ',' , $argv ));
2013-01-16 15:54:07 +01:00
// List of fields to get from LDAP
$required_fields = array (
$conf -> global -> LDAP_KEY_USERS ,
$conf -> global -> LDAP_FIELD_FULLNAME ,
$conf -> global -> LDAP_FIELD_NAME ,
$conf -> global -> LDAP_FIELD_FIRSTNAME ,
$conf -> global -> LDAP_FIELD_LOGIN ,
$conf -> global -> LDAP_FIELD_LOGIN_SAMBA ,
$conf -> global -> LDAP_FIELD_PASSWORD ,
$conf -> global -> LDAP_FIELD_PASSWORD_CRYPTED ,
$conf -> global -> LDAP_FIELD_PHONE ,
$conf -> global -> LDAP_FIELD_FAX ,
$conf -> global -> LDAP_FIELD_MOBILE ,
//$conf->global->LDAP_FIELD_ADDRESS,
//$conf->global->LDAP_FIELD_ZIP,
//$conf->global->LDAP_FIELD_TOWN,
//$conf->global->LDAP_FIELD_COUNTRY,
$conf -> global -> LDAP_FIELD_MAIL ,
$conf -> global -> LDAP_FIELD_TITLE ,
$conf -> global -> LDAP_FIELD_DESCRIPTION ,
$conf -> global -> LDAP_FIELD_SID
);
// Remove from required_fields all entries not configured in LDAP (empty) and duplicated
$required_fields = array_unique ( array_values ( array_filter ( $required_fields , " dolValidElement " )));
2012-10-24 21:19:10 +02:00
if ( ! isset ( $argv [ 1 ])) {
2015-09-11 20:36:45 +02:00
print " Usage: $script_file (nocommitiferror|commitiferror) [--server=ldapserverhost] [--excludeuser=user1,user2...] [-y] \n " ;
2013-06-09 14:40:33 +02:00
exit ( - 1 );
2012-10-24 21:19:10 +02:00
}
2014-07-05 11:50:25 +02:00
2014-07-05 12:27:35 +02:00
foreach ( $argv as $key => $val )
{
if ( $val == 'commitiferror' ) $forcecommit = 1 ;
if ( preg_match ( '/--server=([^\s]+)$/' , $val , $reg )) $conf -> global -> LDAP_SERVER_HOST = $reg [ 1 ];
if ( preg_match ( '/--excludeuser=([^\s]+)$/' , $val , $reg )) $excludeuser = explode ( ',' , $reg [ 1 ]);
2015-09-11 20:36:45 +02:00
if ( preg_match ( '/-y$/' , $val , $reg )) $confirmed = 1 ;
2014-07-05 12:27:35 +02:00
}
2012-10-24 21:19:10 +02:00
print " Mails sending disabled (useless in batch mode) \n " ;
$conf -> global -> MAIN_DISABLE_ALL_MAILS = 1 ; // On bloque les mails
print " \n " ;
print " ----- Synchronize all records from LDAP database: \n " ;
print " host= " . $conf -> global -> LDAP_SERVER_HOST . " \n " ;
print " port= " . $conf -> global -> LDAP_SERVER_PORT . " \n " ;
print " login= " . $conf -> global -> LDAP_ADMIN_DN . " \n " ;
print " pass= " . preg_replace ( '/./i' , '*' , $conf -> global -> LDAP_ADMIN_PASS ) . " \n " ;
print " DN to extract= " . $conf -> global -> LDAP_USER_DN . " \n " ;
2015-10-30 19:16:02 +01:00
if ( ! empty ( $conf -> global -> LDAP_FILTER_CONNECTION )) print 'Filter=(' . $conf -> global -> LDAP_FILTER_CONNECTION . ')' . " \n " ; // Note: filter is defined into function getRecords
else print 'Filter=(' . $conf -> global -> LDAP_KEY_USERS . '=*)' . " \n " ;
2012-10-24 21:19:10 +02:00
print " ----- To Dolibarr database: \n " ;
print " type= " . $conf -> db -> type . " \n " ;
print " host= " . $conf -> db -> host . " \n " ;
print " port= " . $conf -> db -> port . " \n " ;
print " login= " . $conf -> db -> user . " \n " ;
print " database= " . $conf -> db -> name . " \n " ;
print " ----- Options: \n " ;
print " commitiferror= " . $forcecommit . " \n " ;
2014-07-05 12:27:35 +02:00
print " excludeuser= " . join ( ',' , $excludeuser ) . " \n " ;
2012-10-24 21:19:10 +02:00
print " Mapped LDAP fields= " . join ( ',' , $required_fields ) . " \n " ;
print " \n " ;
2014-07-05 11:50:25 +02:00
2015-09-11 20:36:45 +02:00
if ( ! $confirmed )
{
print " Hit Enter to continue or CTRL+C to stop... \n " ;
$input = trim ( fgets ( STDIN ));
}
2012-10-24 21:19:10 +02:00
if ( empty ( $conf -> global -> LDAP_USER_DN ))
{
print $langs -> trans ( " Error " ) . ': ' . $langs -> trans ( " LDAP setup for users not defined inside Dolibarr " );
2013-06-09 14:40:33 +02:00
exit ( - 1 );
2012-10-24 21:19:10 +02:00
}
2014-08-10 13:37:39 +02:00
// Load table of correspondence of countries
2012-10-24 21:19:10 +02:00
$hashlib2rowid = array ();
$countries = array ();
2014-08-10 13:37:39 +02:00
$sql = " SELECT rowid, code, label, active " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " c_country " ;
2012-10-24 21:19:10 +02:00
$sql .= " WHERE active = 1 " ;
$sql .= " ORDER BY code ASC " ;
$resql = $db -> query ( $sql );
if ( $resql )
{
$num = $db -> num_rows ( $resql );
$i = 0 ;
if ( $num )
{
while ( $i < $num )
{
$obj = $db -> fetch_object ( $resql );
if ( $obj )
{
2014-08-10 13:37:39 +02:00
//print 'Load cache for country '.strtolower($obj->label).' rowid='.$obj->rowid."\n";
$hashlib2rowid [ strtolower ( $obj -> label )] = $obj -> rowid ;
$countries [ $obj -> rowid ] = array ( 'rowid' => $obj -> rowid , 'label' => $obj -> label , 'code' => $obj -> code );
2012-10-24 21:19:10 +02:00
}
$i ++ ;
}
}
}
else
{
dol_print_error ( $db );
2013-06-09 14:40:33 +02:00
exit ( - 1 );
2012-10-24 21:19:10 +02:00
}
$ldap = new Ldap ();
$result = $ldap -> connect_bind ();
if ( $result >= 0 )
{
$justthese = array ();
// We disable synchro Dolibarr-LDAP
$conf -> global -> LDAP_SYNCHRO_ACTIVE = 0 ;
2015-10-30 19:16:02 +01:00
$ldaprecords = $ldap -> getRecords ( '*' , $conf -> global -> LDAP_USER_DN , $conf -> global -> LDAP_KEY_USERS , $required_fields , 'user' ); // Fiter on 'user' filter param
2012-10-24 21:19:10 +02:00
if ( is_array ( $ldaprecords ))
{
$db -> begin ();
// Warning $ldapuser has a key in lowercase
foreach ( $ldaprecords as $key => $ldapuser )
{
2014-07-05 12:27:35 +02:00
// If login into exclude list, we discard record
if ( in_array ( $ldapuser [ $conf -> global -> LDAP_FIELD_LOGIN ], $excludeuser ))
{
print $langs -> transnoentities ( " UserDiscarded " ) . ' # ' . $key . ': login=' . $ldapuser [ $conf -> global -> LDAP_FIELD_LOGIN ] . ' --> Discarded' . " \n " ;
continue ;
}
2012-10-24 21:19:10 +02:00
$fuser = new User ( $db );
2013-05-15 11:24:18 +02:00
2013-02-25 16:46:31 +01:00
if ( $conf -> global -> LDAP_KEY_USERS == $conf -> global -> LDAP_FIELD_SID ) {
$fuser -> fetch ( '' , '' , $ldapuser [ $conf -> global -> LDAP_KEY_USERS ]); // Chargement du user concerné par le SID
} else if ( $conf -> global -> LDAP_KEY_USERS == $conf -> global -> LDAP_FIELD_LOGIN ) {
$fuser -> fetch ( '' , $ldapuser [ $conf -> global -> LDAP_KEY_USERS ]); // Chargement du user concerné par le login
}
2012-10-24 21:19:10 +02:00
// Propriete membre
$fuser -> firstname = $ldapuser [ $conf -> global -> LDAP_FIELD_FIRSTNAME ];
$fuser -> lastname = $ldapuser [ $conf -> global -> LDAP_FIELD_NAME ];
$fuser -> login = $ldapuser [ $conf -> global -> LDAP_FIELD_LOGIN ];
$fuser -> pass = $ldapuser [ $conf -> global -> LDAP_FIELD_PASSWORD ];
2013-01-16 15:54:07 +01:00
$fuser -> pass_indatabase_crypted = $ldapuser [ $conf -> global -> LDAP_FIELD_PASSWORD_CRYPTED ];
2012-10-24 21:19:10 +02:00
//$user->societe;
/*
$fuser -> address = $ldapuser [ $conf -> global -> LDAP_FIELD_ADDRESS ];
$fuser -> zip = $ldapuser [ $conf -> global -> LDAP_FIELD_ZIP ];
$fuser -> town = $ldapuser [ $conf -> global -> LDAP_FIELD_TOWN ];
$fuser -> country = $ldapuser [ $conf -> global -> LDAP_FIELD_COUNTRY ];
$fuser -> country_id = $countries [ $hashlib2rowid [ strtolower ( $fuser -> country )]][ 'rowid' ];
$fuser -> country_code = $countries [ $hashlib2rowid [ strtolower ( $fuser -> country )]][ 'code' ];
*/
$fuser -> office_phone = $ldapuser [ $conf -> global -> LDAP_FIELD_PHONE ];
$fuser -> user_mobile = $ldapuser [ $conf -> global -> LDAP_FIELD_MOBILE ];
$fuser -> office_fax = $ldapuser [ $conf -> global -> LDAP_FIELD_FAX ];
$fuser -> email = $ldapuser [ $conf -> global -> LDAP_FIELD_MAIL ];
2013-01-25 15:39:26 +01:00
$fuser -> ldap_sid = $ldapuser [ $conf -> global -> LDAP_FIELD_SID ];
2012-10-24 21:19:10 +02:00
$fuser -> job = $ldapuser [ $conf -> global -> LDAP_FIELD_TITLE ];
$fuser -> note = $ldapuser [ $conf -> global -> LDAP_FIELD_DESCRIPTION ];
$fuser -> admin = 0 ;
2013-01-16 15:54:07 +01:00
$fuser -> societe_id = 0 ;
$fuser -> contact_id = 0 ;
$fuser -> fk_member = 0 ;
2012-10-24 21:19:10 +02:00
$fuser -> statut = 1 ;
2013-02-25 11:53:56 +01:00
// TODO : revoir la gestion du status
2012-10-24 21:19:10 +02:00
/* if ( isset ( $ldapuser [ $conf -> global -> LDAP_FIELD_MEMBER_STATUS ]))
{
$fuser -> datec = dol_stringtotime ( $ldapuser [ $conf -> global -> LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE ]);
$fuser -> datevalid = dol_stringtotime ( $ldapuser [ $conf -> global -> LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE ]);
$fuser -> statut = $ldapuser [ $conf -> global -> LDAP_FIELD_MEMBER_STATUS ];
} */
//if ($fuser->statut > 1) $fuser->statut=1;
//print_r($ldapuser);
2013-05-15 11:24:18 +02:00
2013-02-25 11:53:56 +01:00
if ( $fuser -> id > 0 ) { // User update
print $langs -> transnoentities ( " UserUpdate " ) . ' # ' . $key . ': login=' . $fuser -> login . ', fullname=' . $fuser -> getFullName ( $langs );
$res = $fuser -> update ( $user );
2013-05-15 11:24:18 +02:00
2013-02-25 11:53:56 +01:00
if ( $res < 0 )
{
$error ++ ;
print ' --> ' . $res . ' ' . $fuser -> error ;
}
else
{
print ' --> Updated user id=' . $fuser -> id . ' login=' . $fuser -> login ;
}
} else { // User creation
print $langs -> transnoentities ( " UserCreate " ) . ' # ' . $key . ': login=' . $fuser -> login . ', fullname=' . $fuser -> getFullName ( $langs );
$res = $fuser -> create ( $user );
2013-05-15 11:24:18 +02:00
2013-02-25 11:53:56 +01:00
if ( $res > 0 )
{
print ' --> Created user id=' . $fuser -> id . ' login=' . $fuser -> login ;
}
else
{
$error ++ ;
print ' --> ' . $res . ' ' . $fuser -> error ;
}
2012-10-24 21:19:10 +02:00
}
print " \n " ;
//print_r($fuser);
2013-05-15 11:24:18 +02:00
2013-02-25 11:53:56 +01:00
// Gestion des groupes
// TODO : revoir la gestion des groupes (ou script de sync groupes)
/* if ( ! $error ) {
foreach ( $ldapuser [ $conf -> global -> LDAP_FIELD_USERGROUPS ] as $groupdn ) {
$groupdn ;
}
} */
2012-10-24 21:19:10 +02:00
}
if ( ! $error || $forcecommit )
{
if ( ! $error ) print $langs -> transnoentities ( " NoErrorCommitIsDone " ) . " \n " ;
else print $langs -> transnoentities ( " ErrorButCommitIsDone " ) . " \n " ;
$db -> commit ();
}
else
{
print $langs -> transnoentities ( " ErrorSomeErrorWereFoundRollbackIsDone " , $error ) . " \n " ;
$db -> rollback ();
}
print " \n " ;
}
else
{
dol_print_error ( '' , $ldap -> error );
$error ++ ;
}
}
else
{
dol_print_error ( '' , $ldap -> error );
$error ++ ;
}
2013-06-09 14:40:33 +02:00
exit ( $error );
2012-10-24 21:19:10 +02:00
/**
* Function to say if a value is empty or not
*
* @ param string $element Value to test
* @ return boolean True of false
*/
function dolValidElement ( $element )
{
return ( trim ( $element ) != '' );
}