dolibarr  13.0.2
sync_contacts_dolibarr2ldap.php
Go to the documentation of this file.
1 #!/usr/bin/env php
2 <?php
3 /*
4  * Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
5  * Copyright (C) 2006-2009 Laurent Destailleur <eldy@users.sourceforge.net>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
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."/contact/class/contact.class.php";
41 require_once DOL_DOCUMENT_ROOT."/user/class/user.class.php";
42 require_once DOL_DOCUMENT_ROOT."/core/class/ldap.class.php";
43 
44 // Global variables
45 $version = DOL_VERSION;
46 $error = 0;
47 $confirmed = 0;
48 
49 /*
50  * Main
51  */
52 
53 @set_time_limit(0);
54 print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
55 dol_syslog($script_file." launched with arg ".join(',', $argv));
56 
57 if (!isset($argv[1]) || !$argv[1]) {
58  print "Usage: $script_file now [-y]\n";
59  exit(-1);
60 }
61 
62 foreach ($argv as $key => $val) {
63  if (preg_match('/-y$/', $val, $reg))
64  $confirmed = 1;
65 }
66 
67 $now = $argv[1];
68 
69 print "Mails sending disabled (useless in batch mode)\n";
70 $conf->global->MAIN_DISABLE_ALL_MAILS = 1; // On bloque les mails
71 print "\n";
72 print "----- Synchronize all records from Dolibarr database:\n";
73 print "type=".$conf->db->type."\n";
74 print "host=".$conf->db->host."\n";
75 print "port=".$conf->db->port."\n";
76 print "login=".$conf->db->user."\n";
77 // print "pass=".preg_replace('/./i','*',$conf->db->password)."\n"; // Not defined for security reasons
78 print "database=".$conf->db->name."\n";
79 print "\n";
80 print "----- To LDAP database:\n";
81 print "host=".$conf->global->LDAP_SERVER_HOST."\n";
82 print "port=".$conf->global->LDAP_SERVER_PORT."\n";
83 print "login=".$conf->global->LDAP_ADMIN_DN."\n";
84 print "pass=".preg_replace('/./i', '*', $conf->global->LDAP_ADMIN_PASS)."\n";
85 print "DN target=".$conf->global->LDAP_CONTACT_DN."\n";
86 print "\n";
87 
88 if (!$confirmed) {
89  print "Press a key to confirm...\n";
90  $input = trim(fgets(STDIN));
91  print "Warning, this operation may result in data loss if it failed.\n";
92  print "Be sure to have a backup of your LDAP database (With OpenLDAP: slapcat > save.ldif).\n";
93  print "Hit Enter to continue or CTRL+C to stop...\n";
94  $input = trim(fgets(STDIN));
95 }
96 
97 /*
98  * if (! $conf->global->LDAP_CONTACT_ACTIVE)
99  * {
100  * print $langs->trans("LDAPSynchronizationNotSetupInDolibarr");
101  * exit(-1);
102  * }
103  */
104 
105 $sql = "SELECT rowid";
106 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople";
107 
108 $resql = $db->query($sql);
109 if ($resql) {
110  $num = $db->num_rows($resql);
111  $i = 0;
112 
113  $ldap = new Ldap();
114  $ldap->connect_bind();
115 
116  while ($i < $num) {
117  $ldap->error = "";
118 
119  $obj = $db->fetch_object($resql);
120 
121  $contact = new Contact($db);
122  $contact->id = $obj->rowid;
123  $contact->fetch($contact->id);
124 
125  print $langs->trans("UpdateContact")." rowid=".$contact->id." ".$contact->getFullName($langs);
126 
127  $oldobject = $contact;
128 
129  $oldinfo = $oldobject->_load_ldap_info();
130  $olddn = $oldobject->_load_ldap_dn($oldinfo);
131 
132  $info = $contact->_load_ldap_info();
133  $dn = $contact->_load_ldap_dn($info);
134 
135  $result = $ldap->add($dn, $info, $user); // Wil fail if already exists
136  $result = $ldap->update($dn, $info, $user, $olddn);
137  if ($result > 0) {
138  print " - ".$langs->trans("OK");
139  } else {
140  $error++;
141  print " - ".$langs->trans("KO").' - '.$ldap->error;
142  }
143  print "\n";
144 
145  $i++;
146  }
147 
148  $ldap->unbind();
149  $ldap->close();
150 } else {
151  dol_print_error($db);
152 }
153 
154 exit($error);
Class to manage contact/addresses.
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 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...