sync_contacts_dolibarr2ldap.php 4.3 KB

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