1<?php
2/* Copyright (C) 2004      Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004      Sebastien Di Cintio  <sdicintio@ressource-toi.org>
4 * Copyright (C) 2004      Benoit Mortier       <benoit.mortier@opensides.be>
5 * Copyright (C) 2005      Regis Houssin        <regis.houssin@inodbox.com>
6 * Copyright (C) 2006-2011 Laurent Destailleur  <eldy@users.sourceforge.net>
7 * Copyright (C) 2011-2013 Juanjo Menent		<jmenent@2byte.es>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
23/**
24 *     	\file       htdocs/admin/ldap_groups.php
25 *     	\ingroup    ldap
26 *		\brief      Page to setup LDAP synchronization for groups
27 */
28
29require '../main.inc.php';
30require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
31require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
32require_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
33require_once DOL_DOCUMENT_ROOT.'/core/class/ldap.class.php';
34require_once DOL_DOCUMENT_ROOT.'/core/lib/ldap.lib.php';
35
36// Load translation files required by the page
37$langs->loadLangs(array("admin", "errors"));
38
39if (!$user->admin)
40  accessforbidden();
41
42$action = GETPOST('action', 'aZ09');
43
44
45/*
46 * Actions
47 */
48
49if ($action == 'setvalue' && $user->admin)
50{
51	$error = 0;
52	$db->begin();
53
54	if (!dolibarr_set_const($db, 'LDAP_GROUP_DN', GETPOST("group", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) $error++;
55	if (!dolibarr_set_const($db, 'LDAP_GROUP_OBJECT_CLASS', GETPOST("objectclass", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) $error++;
56
57	if (!dolibarr_set_const($db, 'LDAP_GROUP_FIELD_FULLNAME', GETPOST("fieldfullname", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) $error++;
58	//if (! dolibarr_set_const($db, 'LDAP_GROUP_FIELD_NAME',GETPOST("fieldname", 'alphanohtml'),'chaine',0,'',$conf->entity)) $error++;
59	if (!dolibarr_set_const($db, 'LDAP_GROUP_FIELD_DESCRIPTION', GETPOST("fielddescription", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) $error++;
60	if (!dolibarr_set_const($db, 'LDAP_GROUP_FIELD_GROUPMEMBERS', GETPOST("fieldgroupmembers", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) $error++;
61	if (!dolibarr_set_const($db, 'LDAP_GROUP_FIELD_GROUPID', GETPOST("fieldgroupid", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) $error++;
62
63	// This one must be after the others
64	$valkey = '';
65	$key = GETPOST("key");
66	if ($key) $valkey = $conf->global->$key;
67	if (!dolibarr_set_const($db, 'LDAP_KEY_GROUPS', $valkey, 'chaine', 0, '', $conf->entity)) $error++;
68
69	if (!$error)
70	{
71		$db->commit();
72		setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
73	} else {
74		$db->rollback();
75		dol_print_error($db);
76	}
77}
78
79
80
81/*
82 * View
83 */
84
85llxHeader('', $langs->trans("LDAPSetup"), 'EN:Module_LDAP_En|FR:Module_LDAP|ES:M&oacute;dulo_LDAP');
86$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
87
88print load_fiche_titre($langs->trans("LDAPSetup"), $linkback, 'title_setup');
89
90$head = ldap_prepare_head();
91
92// Test si fonction LDAP actives
93if (!function_exists("ldap_connect"))
94{
95	setEventMessages($langs->trans("LDAPFunctionsNotAvailableOnPHP"), null, 'errors');
96}
97
98print dol_get_fiche_head($head, 'groups', $langs->trans("LDAPSetup"), -1);
99
100
101print $langs->trans("LDAPDescGroups").'<br>';
102print '<br>';
103
104
105print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?action=setvalue">';
106print '<input type="hidden" name="token" value="'.newToken().'">';
107
108$form = new Form($db);
109
110print '<table class="noborder centpercent">';
111
112print '<tr class="liste_titre">';
113print '<td colspan="4">'.$langs->trans("LDAPSynchronizeGroups").'</td>';
114print "</tr>\n";
115
116// DN pour les groupes
117print '<tr class="oddeven"><td width="25%"><span class="fieldrequired">'.$langs->trans("LDAPGroupDn").'</span></td><td>';
118print '<input size="48" type="text" name="group" value="'.$conf->global->LDAP_GROUP_DN.'">';
119print '</td><td>'.$langs->trans("LDAPGroupDnExample").'</td>';
120print '<td>&nbsp;</td>';
121print '</tr>';
122
123// List of object class used to define attributes in structure
124print '<tr class="oddeven"><td width="25%"><span class="fieldrequired">'.$langs->trans("LDAPGroupObjectClassList").'</span></td><td>';
125print '<input size="48" type="text" name="objectclass" value="'.$conf->global->LDAP_GROUP_OBJECT_CLASS.'">';
126print '</td><td>'.$langs->trans("LDAPGroupObjectClassListExample").'</td>';
127print '<td>&nbsp;</td>';
128print '</tr>';
129
130print '</table>';
131print '<br>';
132print '<table class="noborder centpercent">';
133
134print '<tr class="liste_titre">';
135print '<td width="25%">'.$langs->trans("LDAPDolibarrMapping").'</td>';
136print '<td colspan="2">'.$langs->trans("LDAPLdapMapping").'</td>';
137print '<td class="right">'.$langs->trans("LDAPNamingAttribute").'</td>';
138print "</tr>\n";
139
140// Filtre
141
142// Common name
143print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldName").'</td><td>';
144print '<input size="25" type="text" name="fieldfullname" value="'.$conf->global->LDAP_GROUP_FIELD_FULLNAME.'">';
145print '</td><td>'.$langs->trans("LDAPFieldCommonNameExample").'</td>';
146print '<td class="right"><input type="radio" name="key" value="LDAP_GROUP_FIELD_FULLNAME"'.(($conf->global->LDAP_KEY_GROUPS && $conf->global->LDAP_KEY_GROUPS == $conf->global->LDAP_GROUP_FIELD_FULLNAME) ? ' checked' : '')."></td>";
147print '</tr>';
148
149// Name
150/*
151print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldName").'</td><td>';
152print '<input size="25" type="text" name="fieldname" value="'.$conf->global->LDAP_GROUP_FIELD_NAME.'">';
153print '</td><td>'.$langs->trans("LDAPFieldNameExample").'</td>';
154print '<td class="right"><input type="radio" name="key" value="'.$conf->global->LDAP_GROUP_FIELD_NAME.'"'.($conf->global->LDAP_KEY_GROUPS==$conf->global->LDAP_GROUP_FIELD_NAME?' checked':'')."></td>";
155print '</tr>';
156*/
157
158// Description
159print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldDescription").'</td><td>';
160print '<input size="25" type="text" name="fielddescription" value="'.$conf->global->LDAP_GROUP_FIELD_DESCRIPTION.'">';
161print '</td><td>'.$langs->trans("LDAPFieldDescriptionExample").'</td>';
162print '<td class="right"><input type="radio" name="key" value="LDAP_GROUP_FIELD_DESCRIPTION"'.(($conf->global->LDAP_KEY_GROUPS && $conf->global->LDAP_KEY_GROUPS == $conf->global->LDAP_GROUP_FIELD_DESCRIPTION) ? ' checked' : '')."></td>";
163print '</tr>';
164
165// User group
166print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldGroupMembers").'</td><td>';
167print '<input size="25" type="text" name="fieldgroupmembers" value="'.$conf->global->LDAP_GROUP_FIELD_GROUPMEMBERS.'">';
168print '</td><td>'.$langs->trans("LDAPFieldGroupMembersExample").'</td>';
169print '<td class="right"><input type="radio" name="key" value="LDAP_GROUP_FIELD_GROUPMEMBERS"'.(($conf->global->LDAP_KEY_GROUPS && $conf->global->LDAP_KEY_GROUPS == $conf->global->LDAP_GROUP_FIELD_GROUPMEMBERS) ? ' checked' : '')."></td>";
170print '</tr>';
171
172// Group id
173print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldGroupid").'</td><td>';
174print '<input size="25" type="text" name="fieldgroupid" value="'.$conf->global->LDAP_GROUP_FIELD_GROUPID.'">';
175print '</td><td>'.$langs->trans("LDAPFieldGroupidExample").'</td>';
176print '<td class="right">&nbsp;</td>';
177print '</tr>';
178
179print '</table>';
180
181print info_admin($langs->trans("LDAPDescValues"));
182
183print dol_get_fiche_end();
184
185print '<div class="center"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></div>';
186
187print '</form>';
188
189
190/*
191 * Test de la connexion
192 */
193if ($conf->global->LDAP_SYNCHRO_ACTIVE == 'dolibarr2ldap')
194{
195	$butlabel = $langs->trans("LDAPTestSynchroGroup");
196	$testlabel = 'testgroup';
197	$key = $conf->global->LDAP_KEY_GROUPS;
198	$dn = $conf->global->LDAP_GROUP_DN;
199	$objectclass = $conf->global->LDAP_GROUP_OBJECT_CLASS;
200
201	show_ldap_test_button($butlabel, $testlabel, $key, $dn, $objectclass);
202}
203
204if (function_exists("ldap_connect"))
205{
206	if ($_GET["action"] == 'testgroup')
207	{
208		// Creation objet
209		$object = new UserGroup($db);
210		$object->initAsSpecimen();
211
212		// Test synchro
213		$ldap = new Ldap();
214		$result = $ldap->connect_bind();
215
216		if ($result > 0)
217		{
218			$info = $object->_load_ldap_info();
219			$dn = $object->_load_ldap_dn($info);
220
221			// Get a gid number for objectclass PosixGroup
222			if (in_array('posixGroup', $info['objectclass'])) {
223				$info['gidNumber'] = $ldap->getNextGroupGid('LDAP_KEY_GROUPS');
224			}
225
226			$result1 = $ldap->delete($dn); // To be sure to delete existing records
227			$result2 = $ldap->add($dn, $info, $user); // Now the test
228			$result3 = $ldap->delete($dn); // Clean what we did
229
230			if ($result2 > 0)
231			{
232				print img_picto('', 'info').' ';
233				print '<font class="ok">'.$langs->trans("LDAPSynchroOK").'</font><br>';
234			} else {
235				print img_picto('', 'error').' ';
236				print '<font class="error">'.$langs->trans("LDAPSynchroKOMayBePermissions");
237				print ': '.$ldap->error;
238				print '</font><br>';
239				print $langs->trans("ErrorLDAPMakeManualTest", $conf->ldap->dir_temp).'<br>';
240			}
241
242			print "<br>\n";
243			print "LDAP input file used for test:<br><br>\n";
244			print nl2br($ldap->dump_content($dn, $info));
245			print "\n<br>";
246		} else {
247			print img_picto('', 'error').' ';
248			print '<font class="error">'.$langs->trans("LDAPSynchroKO");
249			print ': '.$ldap->error;
250			print '</font><br>';
251			print $langs->trans("ErrorLDAPMakeManualTest", $conf->ldap->dir_temp).'<br>';
252		}
253	}
254}
255
256// End of page
257llxFooter();
258$db->close();
259