dolibarr  13.0.2
sync_members_dolibarr2ldap.php
Go to the documentation of this file.
1 #!/usr/bin/env php
2 <?php
27 if (!defined('NOSESSION')) define('NOSESSION', '1');
28 
29 $sapi_type = php_sapi_name();
30 $script_file = basename(__FILE__);
31 $path = __DIR__.'/';
32 
33 // Test if batch mode
34 if (substr($sapi_type, 0, 3) == 'cgi') {
35  echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
36  exit(-1);
37 }
38 
39 require_once $path."../../htdocs/master.inc.php";
40 require_once DOL_DOCUMENT_ROOT."/core/class/ldap.class.php";
41 require_once DOL_DOCUMENT_ROOT."/adherents/class/adherent.class.php";
42 
43 $langs->load("main");
44 
45 // Global variables
46 $version = constant('DOL_VERSION');
47 $error = 0;
48 $confirmed = 0;
49 
50 /*
51  * Main
52  */
53 
54 @set_time_limit(0);
55 print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
56 dol_syslog($script_file." launched with arg ".join(',', $argv));
57 
58 if (!isset($argv[1]) || !$argv[1]) {
59  print "Usage: $script_file now [-y]\n";
60  exit(-1);
61 }
62 
63 foreach ($argv as $key => $val) {
64  if (preg_match('/-y$/', $val, $reg))
65  $confirmed = 1;
66 }
67 
68 $now = $argv[1];
69 
70 print "Mails sending disabled (useless in batch mode)\n";
71 $conf->global->MAIN_DISABLE_ALL_MAILS = 1; // On bloque les mails
72 print "\n";
73 print "----- Synchronize all records from Dolibarr database:\n";
74 print "type=".$conf->db->type."\n";
75 print "host=".$conf->db->host."\n";
76 print "port=".$conf->db->port."\n";
77 print "login=".$conf->db->user."\n";
78 // print "pass=".preg_replace('/./i','*',$conf->db->password)."\n"; // Not defined for security reasons
79 print "database=".$conf->db->name."\n";
80 print "\n";
81 print "----- To LDAP database:\n";
82 print "host=".$conf->global->LDAP_SERVER_HOST."\n";
83 print "port=".$conf->global->LDAP_SERVER_PORT."\n";
84 print "login=".$conf->global->LDAP_ADMIN_DN."\n";
85 print "pass=".preg_replace('/./i', '*', $conf->global->LDAP_ADMIN_PASS)."\n";
86 print "DN target=".$conf->global->LDAP_MEMBER_DN."\n";
87 print "\n";
88 
89 if (!$confirmed) {
90  print "Press a key to confirm...\n";
91  $input = trim(fgets(STDIN));
92  print "Warning, this operation may result in data loss if it failed.\n";
93  print "Be sure to have a backup of your LDAP database (With OpenLDAP: slapcat > save.ldif).\n";
94  print "Hit Enter to continue or CTRL+C to stop...\n";
95  $input = trim(fgets(STDIN));
96 }
97 
98 /*
99  * if (! $conf->global->LDAP_MEMBER_ACTIVE)
100  * {
101  * print $langs->trans("LDAPSynchronizationNotSetupInDolibarr");
102  * exit(-1);
103  * }
104  */
105 
106 $sql = "SELECT rowid";
107 $sql .= " FROM ".MAIN_DB_PREFIX."adherent";
108 
109 $resql = $db->query($sql);
110 if ($resql) {
111  $num = $db->num_rows($resql);
112  $i = 0;
113 
114  $ldap = new Ldap();
115  $ldap->connect_bind();
116 
117  while ($i < $num) {
118  $ldap->error = "";
119 
120  $obj = $db->fetch_object($resql);
121 
122  $member = new Adherent($db);
123  $result = $member->fetch($obj->rowid);
124  if ($result < 0) {
125  dol_print_error($db, $member->error);
126  exit(-1);
127  }
128  $result = $member->fetch_subscriptions();
129  if ($result < 0) {
130  dol_print_error($db, $member->error);
131  exit(-1);
132  }
133 
134  print $langs->transnoentities("UpdateMember")." rowid=".$member->id." ".$member->getFullName($langs);
135 
136  $oldobject = $member;
137 
138  $oldinfo = $oldobject->_load_ldap_info();
139  $olddn = $oldobject->_load_ldap_dn($oldinfo);
140 
141  $info = $member->_load_ldap_info();
142  $dn = $member->_load_ldap_dn($info);
143 
144  $result = $ldap->add($dn, $info, $user); // Wil fail if already exists
145  $result = $ldap->update($dn, $info, $user, $olddn);
146  if ($result > 0) {
147  print " - ".$langs->transnoentities("OK");
148  } else {
149  $error++;
150  print " - ".$langs->transnoentities("KO").' - '.$ldap->error;
151  }
152  print "\n";
153 
154  $i++;
155  }
156 
157  $ldap->unbind();
158  $ldap->close();
159 } else {
160  dol_print_error($db);
161 }
162 
163 exit($error);
dol_getmypid()
Return getmypid() or random PID when function is disabled Some web hosts disable this php function fo...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
Class to manage members of a foundation.
Class to manage LDAP features.
Definition: ldap.class.php:30
print
Draft customers invoices.
Definition: index.php:89
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...