2015-12-11 05:08:32 +01:00
#!/usr/bin/env php
2011-07-09 16:11:40 +02:00
< ? php
2012-10-24 21:19:10 +02:00
/**
2010-11-20 17:25:23 +01:00
* Copyright ( C ) 2005 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2015-10-30 19:16:02 +01:00
* Copyright ( C ) 2006 - 2015 Laurent Destailleur < eldy @ users . sourceforge . net >
2006-11-26 03:59:09 +01: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
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2006-11-26 03:59:09 +01: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
2011-08-01 00:21:57 +02:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2006-11-26 03:59:09 +01:00
*/
/**
2009-08-22 05:29:10 +02:00
* \file scripts / members / sync_members_ldap2dolibarr . php
2010-07-21 13:57:52 +02:00
* \ingroup ldap member
2009-08-22 05:29:10 +02:00
* \brief Script de mise a jour des adherents dans Dolibarr depuis LDAP
2009-04-20 17:29:09 +02:00
*/
2006-11-26 03:59:09 +01:00
$sapi_type = php_sapi_name ();
2009-10-22 03:04:23 +02:00
$script_file = basename ( __FILE__ );
2010-03-16 19:46:28 +01:00
$path = dirname ( __FILE__ ) . '/' ;
2006-11-26 03:59:09 +01:00
2009-10-22 03:04:23 +02:00
// Test if batch mode
2006-11-26 03:59:09 +01:00
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 );
2006-11-26 03:59:09 +01:00
}
2018-07-26 11:57:25 +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 . " /adherents/class/adherent.class.php " ;
require_once DOL_DOCUMENT_ROOT . " /adherents/class/subscription.class.php " ;
2006-11-26 03:59:09 +01:00
2018-10-01 08:22:23 +02:00
$langs -> loadLangs ( array ( " main " , " errors " ));
2013-05-15 11:24:18 +02:00
// Global variables
$version = DOL_VERSION ;
$error = 0 ;
2013-06-05 16:12:07 +02:00
$forcecommit = 0 ;
2015-09-11 20:36:45 +02:00
$confirmed = 0 ;
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_MEMBERS ,
$conf -> global -> LDAP_FIELD_FULLNAME ,
$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_NAME ,
$conf -> global -> LDAP_FIELD_FIRSTNAME ,
$conf -> global -> LDAP_FIELD_MAIL ,
$conf -> global -> LDAP_FIELD_PHONE ,
$conf -> global -> LDAP_FIELD_PHONE_PERSO ,
$conf -> global -> LDAP_FIELD_MOBILE ,
$conf -> global -> LDAP_FIELD_FAX ,
$conf -> global -> LDAP_FIELD_ADDRESS ,
$conf -> global -> LDAP_FIELD_ZIP ,
$conf -> global -> LDAP_FIELD_TOWN ,
$conf -> global -> LDAP_FIELD_COUNTRY ,
$conf -> global -> LDAP_FIELD_DESCRIPTION ,
$conf -> global -> LDAP_FIELD_BIRTHDATE ,
$conf -> global -> LDAP_FIELD_MEMBER_STATUS ,
$conf -> global -> LDAP_FIELD_MEMBER_END_LASTSUBSCRIPTION ,
// Subscriptions
$conf -> global -> LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE ,
$conf -> global -> LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_AMOUNT ,
$conf -> global -> LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_DATE ,
$conf -> global -> LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_AMOUNT
);
// 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 [ 2 ]) || ! is_numeric ( $argv [ 2 ])) {
2015-09-11 20:36:45 +02:00
print " Usage: $script_file (nocommitiferror|commitiferror) id_member_type [--server=ldapserverhost] [-y] \n " ;
2013-06-09 14:40:33 +02:00
exit ( - 1 );
2007-04-28 14:12:52 +02:00
}
2014-07-05 12:27:35 +02:00
2012-10-24 21:19:10 +02:00
$typeid = $argv [ 2 ];
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 ];
2015-09-11 20:36:45 +02:00
if ( preg_match ( '/-y$/' , $val , $reg )) $confirmed = 1 ;
2014-07-05 12:27:35 +02:00
}
2007-04-28 14:12:52 +02:00
2007-05-09 13:20:43 +02:00
print " Mails sending disabled (useless in batch mode) \n " ;
$conf -> global -> MAIN_DISABLE_ALL_MAILS = 1 ; // On bloque les mails
2007-04-28 14:12:52 +02:00
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 " ;
2009-10-21 19:42:31 +02:00
print " pass= " . preg_replace ( '/./i' , '*' , $conf -> global -> LDAP_ADMIN_PASS ) . " \n " ;
2007-04-28 14:12:52 +02:00
print " DN to extract= " . $conf -> global -> LDAP_MEMBER_DN . " \n " ;
2015-10-30 19:16:02 +01:00
if ( ! empty ( $conf -> global -> LDAP_MEMBER_FILTER )) print 'Filter=(' . $conf -> global -> LDAP_MEMBER_FILTER . ')' . " \n " ; // Note: filter is defined into function getRecords
else print 'Filter=(' . $conf -> global -> LDAP_KEY_MEMBERS . '=*)' . " \n " ;
2007-04-28 14:12:52 +02:00
print " ----- To Dolibarr database: \n " ;
2007-05-09 13:20:43 +02:00
print " type= " . $conf -> db -> type . " \n " ;
2007-04-28 14:12:52 +02:00
print " host= " . $conf -> db -> host . " \n " ;
print " port= " . $conf -> db -> port . " \n " ;
print " login= " . $conf -> db -> user . " \n " ;
print " database= " . $conf -> db -> name . " \n " ;
2012-10-24 21:19:10 +02:00
print " ----- Options: \n " ;
print " commitiferror= " . $forcecommit . " \n " ;
print " Mapped LDAP fields= " . join ( ',' , $required_fields ) . " \n " ;
2007-04-28 14:12:52 +02:00
print " \n " ;
2013-04-09 16:06:00 +02:00
// Check parameters
2013-06-05 16:12:07 +02:00
if ( empty ( $conf -> global -> LDAP_MEMBER_DN ))
{
print $langs -> trans ( " Error " ) . ': ' . $langs -> trans ( " LDAP setup for members not defined inside Dolibarr " ) . " \n " ;
2013-06-09 14:40:33 +02:00
exit ( - 1 );
2013-06-05 16:12:07 +02:00
}
if ( $typeid <= 0 )
{
print $langs -> trans ( " Error " ) . ': Parameter id_member_type is not a valid ref of an existing member type' . " \n " ;
2013-06-09 14:40:33 +02:00
exit ( - 2 );
2013-06-05 16:12:07 +02:00
}
2013-04-09 16:06:00 +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 ));
}
2006-11-26 19:24:53 +01:00
2014-08-10 13:37:39 +02:00
// Load table of correspondence of countries
2006-11-26 08:00:14 +01: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 " ;
2006-11-26 08:00:14 +01:00
$sql .= " WHERE active = 1 " ;
2010-03-13 00:40:39 +01:00
$sql .= " ORDER BY code ASC " ;
2006-11-26 08:00:14 +01:00
$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 );
2006-11-26 08:00:14 +01:00
}
$i ++ ;
}
}
}
else
{
2009-02-20 23:53:15 +01:00
dol_print_error ( $db );
2013-06-09 14:40:33 +02:00
exit ( - 1 );
2006-11-26 08:00:14 +01:00
}
2006-11-26 03:59:09 +01:00
2006-11-26 19:24:53 +01:00
2006-11-26 03:59:09 +01:00
$ldap = new Ldap ();
$result = $ldap -> connect_bind ();
if ( $result >= 0 )
{
$justthese = array ();
2009-04-20 17:29:09 +02:00
2012-10-24 21:19:10 +02:00
// We disable synchro Dolibarr-LDAP
2006-11-26 19:24:53 +01:00
$conf -> global -> LDAP_MEMBER_ACTIVE = 0 ;
2009-04-20 17:29:09 +02:00
2015-10-30 19:16:02 +01:00
$ldaprecords = $ldap -> getRecords ( '*' , $conf -> global -> LDAP_MEMBER_DN , $conf -> global -> LDAP_KEY_MEMBERS , $required_fields , 'member' ); // Fiter on 'member' filter param
2006-11-26 03:59:09 +01:00
if ( is_array ( $ldaprecords ))
{
$db -> begin ();
2007-05-01 23:44:50 +02:00
2009-04-20 17:29:09 +02:00
// Warning $ldapuser has a key in lowercase
2006-11-26 03:59:09 +01:00
foreach ( $ldaprecords as $key => $ldapuser )
{
$member = new Adherent ( $db );
// Propriete membre
2012-01-30 10:22:50 +01:00
$member -> firstname = $ldapuser [ $conf -> global -> LDAP_FIELD_FIRSTNAME ];
$member -> lastname = $ldapuser [ $conf -> global -> LDAP_FIELD_NAME ];
2007-05-01 23:44:50 +02:00
$member -> login = $ldapuser [ $conf -> global -> LDAP_FIELD_LOGIN ];
$member -> pass = $ldapuser [ $conf -> global -> LDAP_FIELD_PASSWORD ];
2006-11-26 08:00:14 +01:00
//$member->societe;
2011-12-28 21:30:33 +01:00
$member -> address = $ldapuser [ $conf -> global -> LDAP_FIELD_ADDRESS ];
$member -> zip = $ldapuser [ $conf -> global -> LDAP_FIELD_ZIP ];
$member -> town = $ldapuser [ $conf -> global -> LDAP_FIELD_TOWN ];
$member -> country = $ldapuser [ $conf -> global -> LDAP_FIELD_COUNTRY ];
$member -> country_id = $countries [ $hashlib2rowid [ strtolower ( $member -> country )]][ 'rowid' ];
$member -> country_code = $countries [ $hashlib2rowid [ strtolower ( $member -> country )]][ 'code' ];
2006-11-26 08:00:14 +01:00
2007-05-01 23:44:50 +02:00
$member -> phone = $ldapuser [ $conf -> global -> LDAP_FIELD_PHONE ];
$member -> phone_perso = $ldapuser [ $conf -> global -> LDAP_FIELD_PHONE_PERSO ];
$member -> phone_mobile = $ldapuser [ $conf -> global -> LDAP_FIELD_MOBILE ];
$member -> email = $ldapuser [ $conf -> global -> LDAP_FIELD_MAIL ];
2006-11-26 08:00:14 +01:00
2008-03-21 01:27:37 +01:00
$member -> note = $ldapuser [ $conf -> global -> LDAP_FIELD_DESCRIPTION ];
2006-11-26 08:00:14 +01:00
$member -> morphy = 'phy' ;
2006-11-26 19:24:53 +01:00
$member -> photo = '' ;
2006-11-26 08:00:14 +01:00
$member -> public = 1 ;
2013-03-13 22:17:03 +01:00
$member -> birth = dol_stringtotime ( $ldapuser [ $conf -> global -> LDAP_FIELD_BIRTHDATE ]);
2007-05-01 23:44:50 +02:00
$member -> statut =- 1 ;
if ( isset ( $ldapuser [ $conf -> global -> LDAP_FIELD_MEMBER_STATUS ]))
2006-11-26 08:53:07 +01:00
{
2009-01-07 11:57:36 +01:00
$member -> datec = dol_stringtotime ( $ldapuser [ $conf -> global -> LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE ]);
$member -> datevalid = dol_stringtotime ( $ldapuser [ $conf -> global -> LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE ]);
2007-05-01 23:44:50 +02:00
$member -> statut = $ldapuser [ $conf -> global -> LDAP_FIELD_MEMBER_STATUS ];
2006-11-26 08:53:07 +01:00
}
2007-08-28 00:41:49 +02:00
//if ($member->statut > 1) $member->statut=1;
2007-05-01 23:44:50 +02:00
//print_r($ldapuser);
2006-11-26 03:59:09 +01:00
// Propriete type membre
$member -> typeid = $typeid ;
2006-11-26 08:00:14 +01:00
// Creation membre
2010-07-18 12:39:07 +02:00
print $langs -> transnoentities ( " MemberCreate " ) . ' # ' . $key . ': login=' . $member -> login . ', fullname=' . $member -> getFullName ( $langs );
2007-05-01 23:44:50 +02:00
print ', datec=' . $member -> datec ;
2009-04-20 17:29:09 +02:00
$member_id = $member -> create ( $user );
2006-11-26 08:00:14 +01:00
if ( $member_id > 0 )
{
2007-04-28 14:12:52 +02:00
print ' --> Created member id=' . $member_id . ' login=' . $member -> login ;
2006-11-26 08:00:14 +01:00
}
else
{
$error ++ ;
print ' --> ' . $member -> error ;
}
print " \n " ;
//print_r($member);
2009-04-20 17:29:09 +02:00
2007-05-09 13:20:43 +02:00
$datefirst = '' ;
if ( $conf -> global -> LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE )
{
2009-01-07 11:57:36 +01:00
$datefirst = dol_stringtotime ( $ldapuser [ $conf -> global -> LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE ]);
2007-08-28 00:41:49 +02:00
$pricefirst = price2num ( $ldapuser [ $conf -> global -> LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_AMOUNT ]);
2006-11-26 08:00:14 +01:00
}
2007-05-09 13:20:43 +02:00
$datelast = '' ;
if ( $conf -> global -> LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_DATE )
{
2009-01-07 11:57:36 +01:00
$datelast = dol_stringtotime ( $ldapuser [ $conf -> global -> LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_DATE ]);
2007-08-28 00:41:49 +02:00
$pricelast = price2num ( $ldapuser [ $conf -> global -> LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_AMOUNT ]);
2007-05-09 13:20:43 +02:00
}
elseif ( $conf -> global -> LDAP_FIELD_MEMBER_END_LASTSUBSCRIPTION )
{
2009-01-07 11:57:36 +01:00
$datelast = dol_time_plus_duree ( dol_stringtotime ( $ldapuser [ $conf -> global -> LDAP_FIELD_MEMBER_END_LASTSUBSCRIPTION ]), - 1 , 'y' ) + 60 * 60 * 24 ;
2007-08-28 00:41:49 +02:00
$pricelast = price2num ( $ldapuser [ $conf -> global -> LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_AMOUNT ]);
// Cas special ou date derniere <= date premiere
if ( $datefirst && $datelast && $datelast <= $datefirst )
{
// On ne va inserer que la premiere
$datelast = 0 ;
if ( ! $pricefirst && $pricelast ) $pricefirst = $pricelast ;
}
2007-05-09 13:20:43 +02:00
}
2007-08-28 00:41:49 +02:00
2009-04-20 17:29:09 +02:00
// Insert first subscription
2007-08-28 00:41:49 +02:00
if ( $datefirst )
{
// Cree premiere cotisation et met a jour datefin dans adherent
//print "xx".$datefirst."\n";
2017-01-18 19:38:50 +01:00
$crowid = $member -> subscription ( $datefirst , $pricefirst , 0 );
2007-08-28 00:41:49 +02:00
}
2009-04-20 17:29:09 +02:00
// Insert last subscription
2006-11-26 08:00:14 +01:00
if ( $datelast )
{
2006-11-26 08:58:58 +01:00
// Cree derniere cotisation et met a jour datefin dans adherent
2009-02-20 23:53:15 +01:00
//print "yy".dol_print_date($datelast)."\n";
2017-01-18 19:38:50 +01:00
$crowid = $member -> subscription ( $datelast , $pricelast , 0 );
2006-11-26 08:00:14 +01:00
}
2006-11-26 03:59:09 +01:00
}
2009-04-20 17:29:09 +02:00
if ( ! $error || $forcecommit )
2006-11-26 03:59:09 +01:00
{
2009-04-20 17:29:09 +02:00
if ( ! $error ) print $langs -> transnoentities ( " NoErrorCommitIsDone " ) . " \n " ;
else print $langs -> transnoentities ( " ErrorButCommitIsDone " ) . " \n " ;
2006-11-26 03:59:09 +01:00
$db -> commit ();
}
else
{
2007-04-28 14:12:52 +02:00
print $langs -> transnoentities ( " ErrorSomeErrorWereFoundRollbackIsDone " , $error ) . " \n " ;
2006-11-26 03:59:09 +01:00
$db -> rollback ();
}
2012-10-24 21:19:10 +02:00
print " \n " ;
2006-11-26 03:59:09 +01:00
}
else
{
2009-02-20 23:53:15 +01:00
dol_print_error ( '' , $ldap -> error );
2006-11-26 03:59:09 +01:00
$error ++ ;
}
}
else
{
2009-02-20 23:53:15 +01:00
dol_print_error ( '' , $ldap -> error );
2006-11-26 03:59:09 +01:00
$error ++ ;
}
2009-04-20 17:29:09 +02:00
2006-11-26 03:59:09 +01:00
2013-06-09 14:40:33 +02:00
exit ( $error );
2007-05-01 23:44:50 +02:00
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 )
{
2007-05-01 23:44:50 +02:00
return ( trim ( $element ) != '' );
}