sync_groups_dolibarr2ldap.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/usr/bin/env 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 <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file scripts/user/sync_groups_dolibarr2ldap.php
  22. * \ingroup ldap core
  23. * \brief Script de mise a jour des groupes dans LDAP depuis base Dolibarr
  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. if (!isset($argv[1]) || !$argv[1]) {
  37. print "Usage: ".$script_file." now\n";
  38. exit(-1);
  39. }
  40. $now = $argv[1];
  41. require_once $path."../../htdocs/master.inc.php";
  42. require_once DOL_DOCUMENT_ROOT."/core/class/ldap.class.php";
  43. require_once DOL_DOCUMENT_ROOT."/user/class/usergroup.class.php";
  44. // Global variables
  45. $version = DOL_VERSION;
  46. $error = 0;
  47. /*
  48. * Main
  49. */
  50. @set_time_limit(0);
  51. print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
  52. dol_syslog($script_file." launched with arg ".join(',', $argv));
  53. /*
  54. * if (getDolGlobalString('LDAP_SYNCHRO_ACTIVE')) {
  55. * print $langs->trans("LDAPSynchronizationNotSetupInDolibarr");
  56. * exit(-1);
  57. * }
  58. */
  59. $sql = "SELECT rowid";
  60. $sql .= " FROM ".MAIN_DB_PREFIX."usergroup";
  61. $resql = $db->query($sql);
  62. if ($resql) {
  63. $num = $db->num_rows($resql);
  64. $i = 0;
  65. $ldap = new Ldap();
  66. $ldap->connect_bind();
  67. while ($i < $num) {
  68. $ldap->error = "";
  69. $obj = $db->fetch_object($resql);
  70. $fgroup = new UserGroup($db);
  71. $fgroup->id = $obj->rowid;
  72. $fgroup->fetch($fgroup->id);
  73. print $langs->trans("UpdateGroup")." rowid=".$fgroup->id." ".$fgroup->name;
  74. $oldobject = $fgroup;
  75. $oldinfo = $oldobject->_load_ldap_info();
  76. $olddn = $oldobject->_load_ldap_dn($oldinfo);
  77. $info = $fgroup->_load_ldap_info();
  78. $dn = $fgroup->_load_ldap_dn($info);
  79. $result = $ldap->add($dn, $info, $user); // Wil fail if already exists
  80. $result = $ldap->update($dn, $info, $user, $olddn);
  81. if ($result > 0) {
  82. print " - ".$langs->trans("OK");
  83. } else {
  84. $error++;
  85. print " - ".$langs->trans("KO").' - '.$ldap->error;
  86. }
  87. print "\n";
  88. $i++;
  89. }
  90. $ldap->unbind();
  91. } else {
  92. dol_print_error($db);
  93. }
  94. exit($error);