dolibarr 21.0.0-alpha
sync_users_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/class/ldap.class.php";
49require_once DOL_DOCUMENT_ROOT."/user/class/user.class.php";
50
51// Global variables
52$version = DOL_VERSION;
53$error = 0;
54
55$hookmanager->initHooks(array('cli'));
56
57
58/*
59 * Main
60 */
61
62@set_time_limit(0);
63print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
64dol_syslog($script_file." launched with arg ".join(',', $argv));
65
66/*
67 * if (! getDolGlobalString('LDAP_SYNCHRO_ACTIVE')) {
68 * print $langs->trans("LDAPSynchronizationNotSetupInDolibarr");
69 * exit(1);
70 * }
71 */
72
73$sql = "SELECT rowid";
74$sql .= " FROM ".MAIN_DB_PREFIX."user";
75
76$resql = $db->query($sql);
77if ($resql) {
78 $num = $db->num_rows($resql);
79 $i = 0;
80
81 $ldap = new Ldap();
82 $ldap->connectBind();
83
84 while ($i < $num) {
85 $ldap->error = "";
86
87 $obj = $db->fetch_object($resql);
88
89 $fuser = new User($db);
90 $fuser->fetch($obj->rowid);
91
92 print $langs->trans("UpdateUser")." rowid=".$fuser->id." ".$fuser->getFullName($langs);
93
94 $oldobject = $fuser;
95
96 $oldinfo = $oldobject->_load_ldap_info();
97 $olddn = $oldobject->_load_ldap_dn($oldinfo);
98
99 $info = $fuser->_load_ldap_info();
100 $dn = $fuser->_load_ldap_dn($info);
101
102 $result = $ldap->add($dn, $info, $user); // Will fail if already exists
103 $result = $ldap->update($dn, $info, $user, $olddn);
104 if ($result > 0) {
105 print " - ".$langs->trans("OK");
106 } else {
107 $error++;
108 print " - ".$langs->trans("KO").' - '.$ldap->error;
109 }
110 print "\n";
111
112 $i++;
113 }
114
115 $ldap->unbind();
116} else {
117 dol_print_error($db);
118}
119
120exit($error);
Class to manage LDAP features.
Class to manage Dolibarr users.
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.