dolibarr 21.0.0-alpha
sync_groups_dolibarr2ldap.php
Go to the documentation of this file.
1#!/usr/bin/env php
2<?php
27if (!defined('NOSESSION')) {
28 define('NOSESSION', '1');
29}
30
31$sapi_type = php_sapi_name();
32$script_file = basename(__FILE__);
33$path = __DIR__.'/';
34
35// Test if batch mode
36if (substr($sapi_type, 0, 3) == 'cgi') {
37 echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
38 exit(1);
39}
40
41if (!isset($argv[1]) || !$argv[1]) {
42 print "Usage: ".$script_file." now\n";
43 exit(1);
44}
45$now = $argv[1];
46
47require_once $path."../../htdocs/master.inc.php";
48require_once DOL_DOCUMENT_ROOT.'/core/lib/functionscli.lib.php';
49require_once DOL_DOCUMENT_ROOT."/core/class/ldap.class.php";
50require_once DOL_DOCUMENT_ROOT."/user/class/usergroup.class.php";
51
52// Global variables
53$version = DOL_VERSION;
54$error = 0;
55
56$hookmanager->initHooks(array('cli'));
57
58
59/*
60 * Main
61 */
62
63@set_time_limit(0);
64print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
65dol_syslog($script_file." launched with arg ".join(',', $argv));
66
67/*
68 * if (getDolGlobalString('LDAP_SYNCHRO_ACTIVE')) {
69 * print $langs->trans("LDAPSynchronizationNotSetupInDolibarr");
70 * exit(1);
71 * }
72 */
73
74$sql = "SELECT rowid";
75$sql .= " FROM ".MAIN_DB_PREFIX."usergroup";
76
77$resql = $db->query($sql);
78if ($resql) {
79 $num = $db->num_rows($resql);
80 $i = 0;
81
82 $ldap = new Ldap();
83 $ldap->connectBind();
84
85 while ($i < $num) {
86 $ldap->error = "";
87
88 $obj = $db->fetch_object($resql);
89
90 $fgroup = new UserGroup($db);
91 $fgroup->id = $obj->rowid;
92 $fgroup->fetch($fgroup->id);
93
94 print $langs->trans("UpdateGroup")." rowid=".$fgroup->id." ".$fgroup->name;
95
96 $oldobject = $fgroup;
97
98 $oldinfo = $oldobject->_load_ldap_info();
99 $olddn = $oldobject->_load_ldap_dn($oldinfo);
100
101 $info = $fgroup->_load_ldap_info();
102 $dn = $fgroup->_load_ldap_dn($info);
103
104 $result = $ldap->add($dn, $info, $user); // Will fail if already exists
105 $result = $ldap->update($dn, $info, $user, $olddn);
106 if ($result > 0) {
107 print " - ".$langs->trans("OK");
108 } else {
109 $error++;
110 print " - ".$langs->trans("KO").' - '.$ldap->error;
111 }
112 print "\n";
113
114 $i++;
115 }
116
117 $ldap->unbind();
118} else {
119 dol_print_error($db);
120}
121
122exit($error);
Class to manage LDAP features.
Class to manage user groups.
dol_getmypid()
Return getmypid() or random PID when function is disabled Some web hosts disable this php function fo...
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.