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