dolibarr  20.0.0-beta
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')) {
28  define('NOSESSION', '1');
29 }
30 
31 $sapi_type = php_sapi_name();
32 $script_file = basename(__FILE__);
33 $path = __DIR__.'/';
34 
35 // Test if batch mode
36 if (substr($sapi_type, 0, 3) == 'cgi') {
37  echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
38  exit(1);
39 }
40 
41 require_once $path."../../htdocs/master.inc.php";
42 require_once DOL_DOCUMENT_ROOT."/contact/class/contact.class.php";
43 require_once DOL_DOCUMENT_ROOT."/user/class/user.class.php";
44 require_once DOL_DOCUMENT_ROOT."/core/class/ldap.class.php";
45 
46 // Global variables
47 $version = DOL_VERSION;
48 $error = 0;
49 $confirmed = 0;
50 
51 $hookmanager->initHooks(array('cli'));
52 
53 
54 /*
55  * Main
56  */
57 
58 @set_time_limit(0);
59 print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
60 dol_syslog($script_file." launched with arg ".join(',', $argv));
61 
62 if (!isset($argv[1]) || !$argv[1]) {
63  print "Usage: $script_file now [-y]\n";
64  exit(1);
65 }
66 
67 foreach ($argv as $key => $val) {
68  if (preg_match('/-y$/', $val, $reg)) {
69  $confirmed = 1;
70  }
71 }
72 
73 $now = $argv[1];
74 
75 if (!empty($dolibarr_main_db_readonly)) {
76  print "Error: instance in read-only mode\n";
77  exit(1);
78 }
79 
80 print "Mails sending disabled (useless in batch mode)\n";
81 $conf->global->MAIN_DISABLE_ALL_MAILS = 1; // On bloque les mails
82 print "\n";
83 print "----- Synchronize all records from Dolibarr database:\n";
84 print "type=".$conf->db->type."\n";
85 print "host=".$conf->db->host."\n";
86 print "port=".$conf->db->port."\n";
87 print "login=".$conf->db->user."\n";
88 // print "pass=".preg_replace('/./i','*',$conf->db->password)."\n"; // Not defined for security reasons
89 print "database=".$conf->db->name."\n";
90 print "\n";
91 print "----- To LDAP database:\n";
92 print "host=" . getDolGlobalString('LDAP_SERVER_HOST')."\n";
93 print "port=" . getDolGlobalString('LDAP_SERVER_PORT')."\n";
94 print "login=" . getDolGlobalString('LDAP_ADMIN_DN')."\n";
95 print "pass=".preg_replace('/./i', '*', getDolGlobalString('LDAP_ADMIN_PASS'))."\n";
96 print "DN target=" . getDolGlobalString('LDAP_CONTACT_DN')."\n";
97 print "\n";
98 
99 if (!$confirmed) {
100  print "Press a key to confirm...\n";
101  $input = trim(fgets(STDIN));
102  print "Warning, this operation may result in data loss if it failed.\n";
103  print "Be sure to have a backup of your LDAP database (With OpenLDAP: slapcat > save.ldif).\n";
104  print "Hit Enter to continue or CTRL+C to stop...\n";
105  $input = trim(fgets(STDIN));
106 }
107 
108 /*
109  * if (!getDolGlobalString('LDAP_CONTACT_ACTIVE')) {
110  * print $langs->trans("LDAPSynchronizationNotSetupInDolibarr");
111  * exit(1);
112  * }
113  */
114 
115 $sql = "SELECT rowid";
116 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople";
117 
118 $resql = $db->query($sql);
119 if ($resql) {
120  $num = $db->num_rows($resql);
121  $i = 0;
122 
123  $ldap = new Ldap();
124  $ldap->connectBind();
125 
126  while ($i < $num) {
127  $ldap->error = "";
128 
129  $obj = $db->fetch_object($resql);
130 
131  $contact = new Contact($db);
132  $contact->id = $obj->rowid;
133  $contact->fetch($contact->id);
134 
135  print $langs->trans("UpdateContact")." rowid=".$contact->id." ".$contact->getFullName($langs);
136 
137  $oldobject = $contact;
138 
139  $oldinfo = $oldobject->_load_ldap_info();
140  $olddn = $oldobject->_load_ldap_dn($oldinfo);
141 
142  $info = $contact->_load_ldap_info();
143  $dn = $contact->_load_ldap_dn($info);
144 
145  $result = $ldap->add($dn, $info, $user); // Will fail if already exists
146  $result = $ldap->update($dn, $info, $user, $olddn);
147  if ($result > 0) {
148  print " - ".$langs->trans("OK");
149  } else {
150  $error++;
151  print " - ".$langs->trans("KO").' - '.$ldap->error;
152  }
153  print "\n";
154 
155  $i++;
156  }
157 
158  $ldap->unbind();
159 } else {
160  dol_print_error($db);
161 }
162 
163 exit($error);
Class to manage contact/addresses.
Class to manage LDAP features.
Definition: ldap.class.php:37
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:745
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.