sync_users_dolibarr2ldap.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/php
  2. <?php
  3. /**
  4. * Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  5. * Copyright (C) 2006 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/user/sync_users_dolibarr2ldap.php
  22. * \ingroup ldap core
  23. * \brief Script de mise a jour des users 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. if (! isset($argv[1]) || ! $argv[1]) {
  34. print "Usage: $script_file now\n";
  35. exit(-1);
  36. }
  37. $now=$argv[1];
  38. require_once($path."../../htdocs/master.inc.php");
  39. require_once(DOL_DOCUMENT_ROOT."/core/class/ldap.class.php");
  40. require_once(DOL_DOCUMENT_ROOT."/user/class/user.class.php");
  41. // Global variables
  42. $version=DOL_VERSION;
  43. $error=0;
  44. /*
  45. * Main
  46. */
  47. @set_time_limit(0);
  48. print "***** ".$script_file." (".$version.") pid=".getmypid()." *****\n";
  49. /*
  50. if (! $conf->global->LDAP_SYNCHRO_ACTIVE)
  51. {
  52. print $langs->trans("LDAPSynchronizationNotSetupInDolibarr");
  53. exit(-1);
  54. }
  55. */
  56. $sql = "SELECT rowid";
  57. $sql .= " FROM ".MAIN_DB_PREFIX."user";
  58. $resql = $db->query($sql);
  59. if ($resql)
  60. {
  61. $num = $db->num_rows($resql);
  62. $i = 0;
  63. $ldap=new Ldap();
  64. $ldap->connect_bind();
  65. while ($i < $num)
  66. {
  67. $ldap->error="";
  68. $obj = $db->fetch_object($resql);
  69. $fuser = new User($db);
  70. $fuser->fetch($obj->rowid);
  71. print $langs->trans("UpdateUser")." rowid=".$fuser->id." ".$fuser->getFullName($langs);
  72. $oldobject=$fuser;
  73. $oldinfo=$oldobject->_load_ldap_info();
  74. $olddn=$oldobject->_load_ldap_dn($oldinfo);
  75. $info=$fuser->_load_ldap_info();
  76. $dn=$fuser->_load_ldap_dn($info);
  77. $result=$ldap->add($dn,$info,$user); // Wil fail if already exists
  78. $result=$ldap->update($dn,$info,$user,$olddn);
  79. if ($result > 0)
  80. {
  81. print " - ".$langs->trans("OK");
  82. }
  83. else
  84. {
  85. $error++;
  86. print " - ".$langs->trans("KO").' - '.$ldap->error;
  87. }
  88. print "\n";
  89. $i++;
  90. }
  91. $ldap->unbind();
  92. $ldap->close();
  93. }
  94. else
  95. {
  96. dol_print_error($db);
  97. }
  98. exit($error);
  99. ?>