dolibarr 20.0.0
sync_members_types_ldap2dolibarr.php
Go to the documentation of this file.
1#!/usr/bin/env php
2<?php
29if (!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
38if (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
43require_once $path."../../htdocs/master.inc.php";
44require_once DOL_DOCUMENT_ROOT."/core/lib/date.lib.php";
45require_once DOL_DOCUMENT_ROOT."/core/class/ldap.class.php";
46require_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$hookmanager->initHooks(array('cli'));
57
58
59/*
60 * Main
61 */
62
63@set_time_limit(0);
64print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
65dol_syslog($script_file." launched with arg ".join(',', $argv));
66
67// List of fields to get from LDAP
68$required_fields = array(getDolGlobalString('LDAP_KEY_MEMBERS_TYPES'), getDolGlobalString('LDAP_MEMBER_TYPE_FIELD_FULLNAME'), getDolGlobalString('LDAP_MEMBER_TYPE_FIELD_DESCRIPTION'), getDolGlobalString('LDAP_MEMBER_TYPE_FIELD_GROUPMEMBERS'));
69
70// Remove from required_fields all entries not configured in LDAP (empty) and duplicated
71$required_fields = array_unique(array_values(array_filter($required_fields, "dolValidElementType")));
72
73if (!isset($argv[1])) {
74 // print "Usage: $script_file (nocommitiferror|commitiferror) [id_group]\n";
75 print "Usage: $script_file (nocommitiferror|commitiferror) [--server=ldapserverhost] [--excludeuser=user1,user2...] [-y]\n";
76 exit(1);
77}
78
79foreach ($argv as $key => $val) {
80 if ($val == 'commitiferror') {
81 $forcecommit = 1;
82 }
83 if (preg_match('/--server=([^\s]+)$/', $val, $reg)) {
84 $conf->global->LDAP_SERVER_HOST = $reg[1];
85 }
86 if (preg_match('/--excludeuser=([^\s]+)$/', $val, $reg)) {
87 $excludeuser = explode(',', $reg[1]);
88 }
89 if (preg_match('/-y$/', $val, $reg)) {
90 $confirmed = 1;
91 }
92}
93
94if (!empty($dolibarr_main_db_readonly)) {
95 print "Error: instance in read-onyl mode\n";
96 exit(1);
97}
98
99print "Mails sending disabled (useless in batch mode)\n";
100$conf->global->MAIN_DISABLE_ALL_MAILS = 1; // On bloque les mails
101print "\n";
102print "----- Synchronize all records from LDAP database:\n";
103print "host=" . getDolGlobalString('LDAP_SERVER_HOST')."\n";
104print "port=" . getDolGlobalString('LDAP_SERVER_PORT')."\n";
105print "login=" . getDolGlobalString('LDAP_ADMIN_DN')."\n";
106print "pass=".preg_replace('/./i', '*', getDolGlobalString('LDAP_ADMIN_PASS'))."\n";
107print "DN to extract=" . getDolGlobalString('LDAP_MEMBER_TYPE_DN')."\n";
108print 'Filter=(' . getDolGlobalString('LDAP_KEY_MEMBERS_TYPES').'=*)'."\n";
109print "----- To Dolibarr database:\n";
110print "type=".$conf->db->type."\n";
111print "host=".$conf->db->host."\n";
112print "port=".$conf->db->port."\n";
113print "login=".$conf->db->user."\n";
114print "database=".$conf->db->name."\n";
115print "----- Options:\n";
116print "commitiferror=".$forcecommit."\n";
117print "Mapped LDAP fields=".join(',', $required_fields)."\n";
118print "\n";
119
120if (!$confirmed) {
121 print "Hit Enter to continue or CTRL+C to stop...\n";
122 $input = trim(fgets(STDIN));
123}
124
125if (!getDolGlobalString('LDAP_MEMBER_TYPE_DN')) {
126 print $langs->trans("Error").': '.$langs->trans("LDAP setup for members types not defined inside Dolibarr");
127 exit(1);
128}
129
130$ldap = new Ldap();
131$result = $ldap->connectBind();
132if ($result >= 0) {
133 $justthese = array();
134
135 // We disable synchro Dolibarr-LDAP
136 $conf->global->LDAP_MEMBER_TYPE_ACTIVE = 0;
137
138 $ldaprecords = $ldap->getRecords('*', getDolGlobalString('LDAP_MEMBER_TYPE_DN'), getDolGlobalString('LDAP_KEY_MEMBERS_TYPES'), $required_fields, 0, array(getDolGlobalString('LDAP_MEMBER_TYPE_FIELD_GROUPMEMBERS')));
139 if (is_array($ldaprecords)) {
140 $db->begin();
141
142 // Warning $ldapuser has a key in lowercase
143 foreach ($ldaprecords as $key => $ldapgroup) {
144 $membertype = new AdherentType($db);
145 $membertype->fetch($ldapgroup[getDolGlobalString('LDAP_KEY_MEMBERS_TYPES')]);
146 $membertype->label = $ldapgroup[getDolGlobalString('LDAP_MEMBER_TYPE_FIELD_FULLNAME')];
147 $membertype->description = $ldapgroup[getDolGlobalString('LDAP_MEMBER_TYPE_FIELD_DESCRIPTION')];
148 $membertype->entity = $conf->entity;
149
150 // print_r($ldapgroup);
151
152 if ($membertype->id > 0) { // Member type update
153 print $langs->transnoentities("MemberTypeUpdate").' # '.$key.': name='.$membertype->label;
154 $res = $membertype->update($user);
155
156 if ($res > 0) {
157 print ' --> Updated member type id='.$membertype->id.' name='.$membertype->label;
158 } else {
159 $error++;
160 print ' --> '.$res.' '.$membertype->error;
161 }
162 print "\n";
163 } else { // Member type creation
164 print $langs->transnoentities("MemberTypeCreate").' # '.$key.': name='.$membertype->label;
165 $res = $membertype->create($user);
166
167 if ($res > 0) {
168 print ' --> Created member type id='.$membertype->id.' name='.$membertype->label;
169 } else {
170 $error++;
171 print ' --> '.$res.' '.$membertype->error;
172 }
173 print "\n";
174 }
175
176 // print_r($membertype);
177 }
178
179 if (!$error || $forcecommit) {
180 if (!$error) {
181 print $langs->transnoentities("NoErrorCommitIsDone")."\n";
182 } else {
183 print $langs->transnoentities("ErrorButCommitIsDone")."\n";
184 }
185 $db->commit();
186 } else {
187 print $langs->transnoentities("ErrorSomeErrorWereFoundRollbackIsDone", $error)."\n";
188 $db->rollback();
189 }
190 print "\n";
191 } else {
192 dol_print_error(null, $ldap->error);
193 $error++;
194 }
195} else {
196 dol_print_error(null, $ldap->error);
197 $error++;
198}
199
200exit($error);
201
202
209function dolValidElementType($element)
210{
211 return (trim($element) != '');
212}
Class to manage members type.
Class to manage LDAP features.
dol_getmypid()
Return getmypid() or random PID when function is disabled Some web hosts disable this php function fo...
dol_print_error($db=null, $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.