sync_members_dolibarr2ldap.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/usr/bin/php
  2. <?php
  3. /**
  4. * Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  5. * Copyright (C) 2006-2008 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/members/sync_members_dolibarr2ldap.php
  22. * \ingroup ldap member
  23. * \brief Script de mise a jour des adherents dans LDAP depuis base Dolibarr
  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."/core/class/ldap.class.php");
  35. require_once(DOL_DOCUMENT_ROOT."/adherents/class/adherent.class.php");
  36. $langs->load("main");
  37. // Global variables
  38. $version=DOL_VERSION;
  39. $error=0;
  40. /*
  41. * Main
  42. */
  43. @set_time_limit(0);
  44. print "***** ".$script_file." (".$version.") pid=".getmypid()." *****\n";
  45. if (! isset($argv[1]) || ! $argv[1]) {
  46. print "Usage: $script_file now\n";
  47. exit(-1);
  48. }
  49. $now=$argv[1];
  50. print "Mails sending disabled (useless in batch mode)\n";
  51. $conf->global->MAIN_DISABLE_ALL_MAILS=1; // On bloque les mails
  52. print "\n";
  53. print "----- Synchronize all records from Dolibarr database:\n";
  54. print "type=".$conf->db->type."\n";
  55. print "host=".$conf->db->host."\n";
  56. print "port=".$conf->db->port."\n";
  57. print "login=".$conf->db->user."\n";
  58. //print "pass=".preg_replace('/./i','*',$conf->db->password)."\n"; // Not defined for security reasons
  59. print "database=".$conf->db->name."\n";
  60. print "\n";
  61. print "----- To LDAP database:\n";
  62. print "host=".$conf->global->LDAP_SERVER_HOST."\n";
  63. print "port=".$conf->global->LDAP_SERVER_PORT."\n";
  64. print "login=".$conf->global->LDAP_ADMIN_DN."\n";
  65. print "pass=".preg_replace('/./i','*',$conf->global->LDAP_ADMIN_PASS)."\n";
  66. print "DN target=".$conf->global->LDAP_MEMBER_DN."\n";
  67. print "\n";
  68. print "Press a key to confirm...\n";
  69. $input = trim(fgets(STDIN));
  70. print "Warning, this operation may result in data loss if it failed.\n";
  71. print "Be sure to have a backup of your LDAP database (With OpenLDAP: slapcat > save.ldif).\n";
  72. print "Hit Enter to continue or CTRL+C to stop...\n";
  73. $input = trim(fgets(STDIN));
  74. /*
  75. if (! $conf->global->LDAP_MEMBER_ACTIVE)
  76. {
  77. print $langs->trans("LDAPSynchronizationNotSetupInDolibarr");
  78. exit(-1);
  79. }
  80. */
  81. $sql = "SELECT rowid";
  82. $sql .= " FROM ".MAIN_DB_PREFIX."adherent";
  83. $resql = $db->query($sql);
  84. if ($resql)
  85. {
  86. $num = $db->num_rows($resql);
  87. $i = 0;
  88. $ldap=new Ldap();
  89. $ldap->connect_bind();
  90. while ($i < $num)
  91. {
  92. $ldap->error="";
  93. $obj = $db->fetch_object($resql);
  94. $member = new Adherent($db);
  95. $result=$member->fetch($obj->rowid);
  96. if ($result < 0)
  97. {
  98. dol_print_error($db,$member->error);
  99. exit(-1);
  100. }
  101. $result=$member->fetch_subscriptions();
  102. if ($result < 0)
  103. {
  104. dol_print_error($db,$member->error);
  105. exit(-1);
  106. }
  107. print $langs->transnoentities("UpdateMember")." rowid=".$member->id." ".$member->getFullName($langs);
  108. $oldobject=$member;
  109. $oldinfo=$oldobject->_load_ldap_info();
  110. $olddn=$oldobject->_load_ldap_dn($oldinfo);
  111. $info=$member->_load_ldap_info();
  112. $dn=$member->_load_ldap_dn($info);
  113. $result=$ldap->add($dn,$info,$user); // Wil fail if already exists
  114. $result=$ldap->update($dn,$info,$user,$olddn);
  115. if ($result > 0)
  116. {
  117. print " - ".$langs->transnoentities("OK");
  118. }
  119. else
  120. {
  121. $error++;
  122. print " - ".$langs->transnoentities("KO").' - '.$ldap->error;
  123. }
  124. print "\n";
  125. $i++;
  126. }
  127. $ldap->unbind();
  128. $ldap->close();
  129. }
  130. else
  131. {
  132. dol_print_error($db);
  133. }
  134. exit($error);
  135. ?>