sync_contacts_dolibarr2ldap.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. /**
  21. * \file scripts/company/sync_contacts_dolibarr2ldap.php
  22. * \ingroup ldap company
  23. * \brief Script to update all contacts from Dolibarr into a LDAP database
  24. */
  25. if (!defined('NOSESSION')) {
  26. define('NOSESSION', '1');
  27. }
  28. $sapi_type = php_sapi_name();
  29. $script_file = basename(__FILE__);
  30. $path = __DIR__.'/';
  31. // Test if batch mode
  32. if (substr($sapi_type, 0, 3) == 'cgi') {
  33. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  34. exit(-1);
  35. }
  36. require_once $path."../../htdocs/master.inc.php";
  37. require_once DOL_DOCUMENT_ROOT."/contact/class/contact.class.php";
  38. require_once DOL_DOCUMENT_ROOT."/user/class/user.class.php";
  39. require_once DOL_DOCUMENT_ROOT."/core/class/ldap.class.php";
  40. // Global variables
  41. $version = DOL_VERSION;
  42. $error = 0;
  43. $confirmed = 0;
  44. /*
  45. * Main
  46. */
  47. @set_time_limit(0);
  48. print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
  49. dol_syslog($script_file." launched with arg ".join(',', $argv));
  50. if (!isset($argv[1]) || !$argv[1]) {
  51. print "Usage: $script_file now [-y]\n";
  52. exit(-1);
  53. }
  54. foreach ($argv as $key => $val) {
  55. if (preg_match('/-y$/', $val, $reg)) {
  56. $confirmed = 1;
  57. }
  58. }
  59. $now = $argv[1];
  60. if (!empty($dolibarr_main_db_readonly)) {
  61. print "Error: instance in read-only mode\n";
  62. exit(-1);
  63. }
  64. print "Mails sending disabled (useless in batch mode)\n";
  65. $conf->global->MAIN_DISABLE_ALL_MAILS = 1; // On bloque les mails
  66. print "\n";
  67. print "----- Synchronize all records from Dolibarr database:\n";
  68. print "type=".$conf->db->type."\n";
  69. print "host=".$conf->db->host."\n";
  70. print "port=".$conf->db->port."\n";
  71. print "login=".$conf->db->user."\n";
  72. // print "pass=".preg_replace('/./i','*',$conf->db->password)."\n"; // Not defined for security reasons
  73. print "database=".$conf->db->name."\n";
  74. print "\n";
  75. print "----- To LDAP database:\n";
  76. print "host=" . getDolGlobalString('LDAP_SERVER_HOST')."\n";
  77. print "port=" . getDolGlobalString('LDAP_SERVER_PORT')."\n";
  78. print "login=" . getDolGlobalString('LDAP_ADMIN_DN')."\n";
  79. print "pass=".preg_replace('/./i', '*', getDolGlobalString('LDAP_ADMIN_PASS'))."\n";
  80. print "DN target=" . getDolGlobalString('LDAP_CONTACT_DN')."\n";
  81. print "\n";
  82. if (!$confirmed) {
  83. print "Press a key to confirm...\n";
  84. $input = trim(fgets(STDIN));
  85. print "Warning, this operation may result in data loss if it failed.\n";
  86. print "Be sure to have a backup of your LDAP database (With OpenLDAP: slapcat > save.ldif).\n";
  87. print "Hit Enter to continue or CTRL+C to stop...\n";
  88. $input = trim(fgets(STDIN));
  89. }
  90. /*
  91. * if (!getDolGlobalString('LDAP_CONTACT_ACTIVE')) {
  92. * print $langs->trans("LDAPSynchronizationNotSetupInDolibarr");
  93. * exit(-1);
  94. * }
  95. */
  96. $sql = "SELECT rowid";
  97. $sql .= " FROM ".MAIN_DB_PREFIX."socpeople";
  98. $resql = $db->query($sql);
  99. if ($resql) {
  100. $num = $db->num_rows($resql);
  101. $i = 0;
  102. $ldap = new Ldap();
  103. $ldap->connect_bind();
  104. while ($i < $num) {
  105. $ldap->error = "";
  106. $obj = $db->fetch_object($resql);
  107. $contact = new Contact($db);
  108. $contact->id = $obj->rowid;
  109. $contact->fetch($contact->id);
  110. print $langs->trans("UpdateContact")." rowid=".$contact->id." ".$contact->getFullName($langs);
  111. $oldobject = $contact;
  112. $oldinfo = $oldobject->_load_ldap_info();
  113. $olddn = $oldobject->_load_ldap_dn($oldinfo);
  114. $info = $contact->_load_ldap_info();
  115. $dn = $contact->_load_ldap_dn($info);
  116. $result = $ldap->add($dn, $info, $user); // Wil fail if already exists
  117. $result = $ldap->update($dn, $info, $user, $olddn);
  118. if ($result > 0) {
  119. print " - ".$langs->trans("OK");
  120. } else {
  121. $error++;
  122. print " - ".$langs->trans("KO").' - '.$ldap->error;
  123. }
  124. print "\n";
  125. $i++;
  126. }
  127. $ldap->unbind();
  128. } else {
  129. dol_print_error($db);
  130. }
  131. exit($error);