dolibarr  19.0.0-dev
sync_members_types_ldap2dolibarr.php
Go to the documentation of this file.
1 #!/usr/bin/env php
2 <?php
29 if (!defined('NOSESSION')) {
30  define('NOSESSION', '1');
31 }
32 
33 $sapi_type = php_sapi_name();
34 $script_file = basename(__FILE__);
35 $path = __DIR__.'/';
36 
37 // Test if batch mode
38 if (substr($sapi_type, 0, 3) == 'cgi') {
39  echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
40  exit(-1);
41 }
42 
43 require_once $path."../../htdocs/master.inc.php";
44 require_once DOL_DOCUMENT_ROOT."/core/lib/date.lib.php";
45 require_once DOL_DOCUMENT_ROOT."/core/class/ldap.class.php";
46 require_once DOL_DOCUMENT_ROOT."/adherents/class/adherent_type.class.php";
47 
48 $langs->loadLangs(array("main", "errors"));
49 
50 // Global variables
51 $version = constant('DOL_VERSION');
52 $error = 0;
53 $forcecommit = 0;
54 $confirmed = 0;
55 
56 /*
57  * Main
58  */
59 
60 @set_time_limit(0);
61 print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
62 dol_syslog($script_file." launched with arg ".join(',', $argv));
63 
64 // List of fields to get from LDAP
65 $required_fields = array($conf->global->LDAP_KEY_MEMBERS_TYPES, $conf->global->LDAP_MEMBER_TYPE_FIELD_FULLNAME, $conf->global->LDAP_MEMBER_TYPE_FIELD_DESCRIPTION, $conf->global->LDAP_MEMBER_TYPE_FIELD_GROUPMEMBERS);
66 
67 // Remove from required_fields all entries not configured in LDAP (empty) and duplicated
68 $required_fields = array_unique(array_values(array_filter($required_fields, "dolValidElementType")));
69 
70 if (!isset($argv[1])) {
71  // print "Usage: $script_file (nocommitiferror|commitiferror) [id_group]\n";
72  print "Usage: $script_file (nocommitiferror|commitiferror) [--server=ldapserverhost] [--excludeuser=user1,user2...] [-y]\n";
73  exit(-1);
74 }
75 
76 foreach ($argv as $key => $val) {
77  if ($val == 'commitiferror') {
78  $forcecommit = 1;
79  }
80  if (preg_match('/--server=([^\s]+)$/', $val, $reg)) {
81  $conf->global->LDAP_SERVER_HOST = $reg[1];
82  }
83  if (preg_match('/--excludeuser=([^\s]+)$/', $val, $reg)) {
84  $excludeuser = explode(',', $reg[1]);
85  }
86  if (preg_match('/-y$/', $val, $reg)) {
87  $confirmed = 1;
88  }
89 }
90 
91 if (!empty($dolibarr_main_db_readonly)) {
92  print "Error: instance in read-onyl mode\n";
93  exit(-1);
94 }
95 
96 print "Mails sending disabled (useless in batch mode)\n";
97 $conf->global->MAIN_DISABLE_ALL_MAILS = 1; // On bloque les mails
98 print "\n";
99 print "----- Synchronize all records from LDAP database:\n";
100 print "host=".$conf->global->LDAP_SERVER_HOST."\n";
101 print "port=".$conf->global->LDAP_SERVER_PORT."\n";
102 print "login=".$conf->global->LDAP_ADMIN_DN."\n";
103 print "pass=".preg_replace('/./i', '*', $conf->global->LDAP_ADMIN_PASS)."\n";
104 print "DN to extract=".$conf->global->LDAP_MEMBER_TYPE_DN."\n";
105 print 'Filter=('.$conf->global->LDAP_KEY_MEMBERS_TYPES.'=*)'."\n";
106 print "----- To Dolibarr database:\n";
107 print "type=".$conf->db->type."\n";
108 print "host=".$conf->db->host."\n";
109 print "port=".$conf->db->port."\n";
110 print "login=".$conf->db->user."\n";
111 print "database=".$conf->db->name."\n";
112 print "----- Options:\n";
113 print "commitiferror=".$forcecommit."\n";
114 print "Mapped LDAP fields=".join(',', $required_fields)."\n";
115 print "\n";
116 
117 if (!$confirmed) {
118  print "Hit Enter to continue or CTRL+C to stop...\n";
119  $input = trim(fgets(STDIN));
120 }
121 
122 if (empty($conf->global->LDAP_MEMBER_TYPE_DN)) {
123  print $langs->trans("Error").': '.$langs->trans("LDAP setup for members types not defined inside Dolibarr");
124  exit(-1);
125 }
126 
127 $ldap = new Ldap();
128 $result = $ldap->connect_bind();
129 if ($result >= 0) {
130  $justthese = array();
131 
132  // We disable synchro Dolibarr-LDAP
133  $conf->global->LDAP_MEMBER_TYPE_ACTIVE = 0;
134 
135  $ldaprecords = $ldap->getRecords('*', $conf->global->LDAP_MEMBER_TYPE_DN, $conf->global->LDAP_KEY_MEMBERS_TYPES, $required_fields, 0, array($conf->global->LDAP_MEMBER_TYPE_FIELD_GROUPMEMBERS));
136  if (is_array($ldaprecords)) {
137  $db->begin();
138 
139  // Warning $ldapuser has a key in lowercase
140  foreach ($ldaprecords as $key => $ldapgroup) {
141  $membertype = new AdherentType($db);
142  $membertype->fetch($ldapgroup[$conf->global->LDAP_KEY_MEMBERS_TYPES]);
143  $membertype->label = $ldapgroup[$conf->global->LDAP_MEMBER_TYPE_FIELD_FULLNAME];
144  $membertype->description = $ldapgroup[$conf->global->LDAP_MEMBER_TYPE_FIELD_DESCRIPTION];
145  $membertype->entity = $conf->entity;
146 
147  // print_r($ldapgroup);
148 
149  if ($membertype->id > 0) { // Member type update
150  print $langs->transnoentities("MemberTypeUpdate").' # '.$key.': name='.$membertype->label;
151  $res = $membertype->update($user);
152 
153  if ($res > 0) {
154  print ' --> Updated member type id='.$membertype->id.' name='.$membertype->label;
155  } else {
156  $error++;
157  print ' --> '.$res.' '.$membertype->error;
158  }
159  print "\n";
160  } else { // Member type creation
161  print $langs->transnoentities("MemberTypeCreate").' # '.$key.': name='.$membertype->label;
162  $res = $membertype->create($user);
163 
164  if ($res > 0) {
165  print ' --> Created member type id='.$membertype->id.' name='.$membertype->label;
166  } else {
167  $error++;
168  print ' --> '.$res.' '.$membertype->error;
169  }
170  print "\n";
171  }
172 
173  // print_r($membertype);
174  }
175 
176  if (!$error || $forcecommit) {
177  if (!$error) {
178  print $langs->transnoentities("NoErrorCommitIsDone")."\n";
179  } else {
180  print $langs->transnoentities("ErrorButCommitIsDone")."\n";
181  }
182  $db->commit();
183  } else {
184  print $langs->transnoentities("ErrorSomeErrorWereFoundRollbackIsDone", $error)."\n";
185  $db->rollback();
186  }
187  print "\n";
188  } else {
189  dol_print_error('', $ldap->error);
190  $error++;
191  }
192 } else {
193  dol_print_error('', $ldap->error);
194  $error++;
195 }
196 
197 exit($error);
198 
199 
206 function dolValidElementType($element)
207 {
208  return (trim($element) != '');
209 }
Class to manage members type.
Class to manage LDAP features.
Definition: ldap.class.php:35
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...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.