28if (!defined(
'NOSESSION')) {
 
   29  define(
'NOSESSION', 
'1');
 
   32$sapi_type = php_sapi_name();
 
   33$script_file = basename(__FILE__);
 
   37if (substr($sapi_type, 0, 3) == 
'cgi') {
 
   38  echo 
"Error: You are using PHP for CGI. To execute ".$script_file.
" from command line, you must use PHP for CLI mode.\n";
 
   42require_once $path.
"../../htdocs/master.inc.php";
 
   43require_once DOL_DOCUMENT_ROOT.
"/core/lib/date.lib.php";
 
   44require_once DOL_DOCUMENT_ROOT.
"/core/class/ldap.class.php";
 
   45require_once DOL_DOCUMENT_ROOT.
"/user/class/user.class.php";
 
   46require_once DOL_DOCUMENT_ROOT.
"/user/class/usergroup.class.php";
 
   48$langs->loadLangs(array(
"main", 
"errors"));
 
   51$version = DOL_VERSION;
 
   61print 
"***** ".$script_file.
" (".$version.
") pid=".
dol_getmypid().
" *****\n";
 
   62dol_syslog($script_file.
" launched with arg ".join(
',', $argv));
 
   68$required_fields = array_unique(array_values(array_filter($required_fields, 
"dolValidElement")));
 
   70if (!isset($argv[1])) {
 
   72  print 
"Usage:  $script_file (nocommitiferror|commitiferror) [--server=ldapserverhost] [--excludeuser=user1,user2...] [-y]\n";
 
   76foreach ($argv as $key => $val) {
 
   77  if ($val == 
'commitiferror') {
 
   80  if (preg_match(
'/--server=([^\s]+)$/', $val, $reg)) {
 
   81    $conf->global->LDAP_SERVER_HOST = $reg[1];
 
   83  if (preg_match(
'/--excludeuser=([^\s]+)$/', $val, $reg)) {
 
   84    $excludeuser = explode(
',', $reg[1]);
 
   86  if (preg_match(
'/-y$/', $val, $reg)) {
 
   91print 
"Mails sending disabled (useless in batch mode)\n";
 
   92$conf->global->MAIN_DISABLE_ALL_MAILS = 1; 
 
   94print 
"----- Synchronize all records from LDAP database:\n";
 
  105print 
"----- To Dolibarr database:\n";
 
  106print 
"type=".$conf->db->type.
"\n";
 
  107print 
"host=".$conf->db->host.
"\n";
 
  108print 
"port=".$conf->db->port.
"\n";
 
  109print 
"login=".$conf->db->user.
"\n";
 
  110print 
"database=".$conf->db->name.
"\n";
 
  111print 
"----- Options:\n";
 
  112print 
"commitiferror=".$forcecommit.
"\n";
 
  113print 
"Mapped LDAP fields=".join(
',', $required_fields).
"\n";
 
  117  print 
"Hit Enter to continue or CTRL+C to stop...\n";
 
  118  $input = trim(fgets(STDIN));
 
  122  print $langs->trans(
"Error").
': '.$langs->trans(
"LDAP setup for groups not defined inside Dolibarr");
 
  127$result = $ldap->connect_bind();
 
  129  $justthese = array();
 
  132  $conf->global->LDAP_SYNCHRO_ACTIVE = 0;
 
  135  if (is_array($ldaprecords)) {
 
  139    foreach ($ldaprecords as $key => $ldapgroup) {
 
  143      $group->nom = $group->name; 
 
  145      $group->entity = $conf->entity;
 
  149      if ($group->id > 0) { 
 
  150        print $langs->transnoentities(
"GroupUpdate").
' # '.$key.
': name='.$group->name;
 
  151        $res = $group->update();
 
  154          print 
' --> Updated group id='.$group->id.
' name='.$group->name;
 
  157          print 
' --> '.$res.
' '.$group->error;
 
  161        print $langs->transnoentities(
"GroupCreate").
' # '.$key.
': name='.$group->name;
 
  162        $res = $group->create();
 
  165          print 
' --> Created group id='.$group->id.
' name='.$group->name;
 
  168          print 
' --> '.$res.
' '.$group->error;
 
  178      $userIdList = array();
 
  179      foreach ($ldapgroup[
getDolGlobalString(
'LDAP_GROUP_FIELD_GROUPMEMBERS')] as $tmpkey => $userdn) {
 
  180        if ($tmpkey === 
'count') {
 
  183        if (empty($userList[$userdn])) { 
 
  186            $userKey = array($userdn);
 
  188            $userFilter = explode(
',', $userdn);
 
  189            $userKey = $ldap->getAttributeValues(
'('.$userFilter[0].
')', 
getDolGlobalString(
'LDAP_KEY_USERS'));
 
  191          if (!is_array($userKey)) {
 
  195          $fuser = 
new User($db);
 
  198            $fuser->fetch(
'', 
'', $userKey[0]); 
 
  200            $fuser->fetch(
'', $userKey[0]); 
 
  203          $userList[$userdn] = $fuser;
 
  205          $fuser = &$userList[$userdn];
 
  208        $userIdList[$userdn] = $fuser->id;
 
  211        if (!in_array($fuser->id, array_keys($group->members))) {
 
  212          $fuser->SetInGroup($group->id, $group->entity);
 
  213          echo $fuser->login.
' added'.
"\n";
 
  218      foreach ($group->members as $guser) {
 
  219        if (!in_array($guser->id, $userIdList)) {
 
  220          $guser->RemoveFromGroup($group->id, $group->entity);
 
  221          echo $guser->login.
' removed'.
"\n";
 
  226    if (!$error || $forcecommit) {
 
  228        print $langs->transnoentities(
"NoErrorCommitIsDone").
"\n";
 
  230        print $langs->transnoentities(
"ErrorButCommitIsDone").
"\n";
 
  234      print $langs->transnoentities(
"ErrorSomeErrorWereFoundRollbackIsDone", $error).
"\n";
 
  258  return (trim($element) != 
'');
 
 
Class to manage LDAP features.
Class to manage user groups.
Class to manage Dolibarr users.
dol_getmypid()
Return getmypid() or random PID when function is disabled Some web hosts disable this php function fo...
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dolValidElement($element)
Function to say if a value is empty or not.