dolibarr 21.0.0-beta
sync_groups_dolibarr2ldap.php
Go to the documentation of this file.
1#!/usr/bin/env php
2<?php
28if (!defined('NOSESSION')) {
29 define('NOSESSION', '1');
30}
31
32$sapi_type = php_sapi_name();
33$script_file = basename(__FILE__);
34$path = __DIR__.'/';
35
36// Test if batch mode
37if (substr($sapi_type, 0, 3) == 'cgi') {
38 echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
39 exit(1);
40}
41
42if (!isset($argv[1]) || !$argv[1]) {
43 print "Usage: ".$script_file." now\n";
44 exit(1);
45}
46$now = $argv[1];
47
48require_once $path."../../htdocs/master.inc.php";
49require_once DOL_DOCUMENT_ROOT.'/core/lib/functionscli.lib.php';
50require_once DOL_DOCUMENT_ROOT."/core/class/ldap.class.php";
51require_once DOL_DOCUMENT_ROOT."/user/class/usergroup.class.php";
52
61// Global variables
62$version = DOL_VERSION;
63$error = 0;
64
65$hookmanager->initHooks(array('cli'));
66
67
68/*
69 * Main
70 */
71
72@set_time_limit(0);
73print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
74dol_syslog($script_file." launched with arg ".join(',', $argv));
75
76/*
77 * if (getDolGlobalString('LDAP_SYNCHRO_ACTIVE')) {
78 * print $langs->trans("LDAPSynchronizationNotSetupInDolibarr");
79 * exit(1);
80 * }
81 */
82
83$sql = "SELECT rowid";
84$sql .= " FROM ".MAIN_DB_PREFIX."usergroup";
85
86$resql = $db->query($sql);
87if ($resql) {
88 $num = $db->num_rows($resql);
89 $i = 0;
90
91 $ldap = new Ldap();
92 $ldap->connectBind();
93
94 while ($i < $num) {
95 $ldap->error = "";
96
97 $obj = $db->fetch_object($resql);
98
99 $fgroup = new UserGroup($db);
100 $fgroup->id = $obj->rowid;
101 $fgroup->fetch($fgroup->id);
102
103 print $langs->trans("UpdateGroup")." rowid=".$fgroup->id." ".$fgroup->name;
104
105 $oldobject = $fgroup;
106
107 $oldinfo = $oldobject->_load_ldap_info();
108 $olddn = $oldobject->_load_ldap_dn($oldinfo);
109
110 $info = $fgroup->_load_ldap_info();
111 $dn = $fgroup->_load_ldap_dn($info);
112
113 $result = $ldap->add($dn, $info, $user); // Will fail if already exists
114 $result = $ldap->update($dn, $info, $user, $olddn);
115 if ($result > 0) {
116 print " - ".$langs->trans("OK");
117 } else {
118 $error++;
119 print " - ".$langs->trans("KO").' - '.$ldap->error;
120 }
121 print "\n";
122
123 $i++;
124 }
125
126 $ldap->unbind();
127} else {
128 dol_print_error($db);
129}
130
131exit($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.