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-2017	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_members_types.php
25 *     	\ingroup    ldap
26 *		\brief      Page to setup LDAP synchronization for members types
27 */
28
29require '../main.inc.php';
30require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
31require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
32require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent_type.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_MEMBER_TYPE_DN', GETPOST("membertype"), 'chaine', 0, '', $conf->entity)) $error++;
55	if (!dolibarr_set_const($db, 'LDAP_MEMBER_TYPE_OBJECT_CLASS', GETPOST("objectclass"), 'chaine', 0, '', $conf->entity)) $error++;
56
57	if (!dolibarr_set_const($db, 'LDAP_MEMBER_TYPE_FIELD_FULLNAME', GETPOST("fieldfullname"), 'chaine', 0, '', $conf->entity)) $error++;
58	if (!dolibarr_set_const($db, 'LDAP_MEMBER_TYPE_FIELD_DESCRIPTION', GETPOST("fielddescription"), 'chaine', 0, '', $conf->entity)) $error++;
59	if (!dolibarr_set_const($db, 'LDAP_MEMBER_TYPE_FIELD_GROUPMEMBERS', GETPOST("fieldmembertypemembers"), 'chaine', 0, '', $conf->entity)) $error++;
60
61	// This one must be after the others
62	$valkey = '';
63	$key = GETPOST("key");
64	if ($key) $valkey = $conf->global->$key;
65	if (!dolibarr_set_const($db, 'LDAP_KEY_MEMBERS_TYPES', $valkey, 'chaine', 0, '', $conf->entity)) $error++;
66
67	if (!$error)
68	{
69		$db->commit();
70		setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
71	} else {
72		$db->rollback();
73		dol_print_error($db);
74	}
75}
76
77
78
79/*
80 * View
81 */
82
83llxHeader('', $langs->trans("LDAPSetup"), 'EN:Module_LDAP_En|FR:Module_LDAP|ES:M&oacute;dulo_LDAP');
84$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
85
86print load_fiche_titre($langs->trans("LDAPSetup"), $linkback, 'title_setup');
87
88$head = ldap_prepare_head();
89
90// Test si fonction LDAP actives
91if (!function_exists("ldap_connect"))
92{
93	setEventMessages($langs->trans("LDAPFunctionsNotAvailableOnPHP"), null, 'errors');
94}
95
96print dol_get_fiche_head($head, 'memberstypes', $langs->trans("LDAPSetup"), -1);
97
98
99print $langs->trans("LDAPDescMembersTypes").'<br>';
100print '<br>';
101
102
103print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'?action=setvalue">';
104print '<input type="hidden" name="token" value="'.newToken().'">';
105
106$form = new Form($db);
107
108print '<table class="noborder centpercent">';
109
110print '<tr class="liste_titre">';
111print '<td colspan="4">'.$langs->trans("LDAPSynchronizeMembersTypes").'</td>';
112print "</tr>\n";
113
114// DN pour les types de membres
115print '<tr class="oddeven"><td width="25%"><span class="fieldrequired">'.$langs->trans("LDAPMemberTypeDn").'</span></td><td>';
116print '<input size="48" type="text" name="membertype" value="'.$conf->global->LDAP_MEMBER_TYPE_DN.'">';
117print '</td><td>'.$langs->trans("LDAPMemberTypepDnExample").'</td>';
118print '<td>&nbsp;</td>';
119print '</tr>';
120
121// List of object class used to define attributes in structure
122print '<tr class="oddeven"><td width="25%"><span class="fieldrequired">'.$langs->trans("LDAPMemberTypeObjectClassList").'</span></td><td>';
123print '<input size="48" type="text" name="objectclass" value="'.$conf->global->LDAP_MEMBER_TYPE_OBJECT_CLASS.'">';
124print '</td><td>'.$langs->trans("LDAPMemberTypeObjectClassListExample").'</td>';
125print '<td>&nbsp;</td>';
126print '</tr>';
127
128print '</table>';
129print '<br>';
130print '<table class="noborder centpercent">';
131
132print '<tr class="liste_titre">';
133print '<td width="25%">'.$langs->trans("LDAPDolibarrMapping").'</td>';
134print '<td colspan="2">'.$langs->trans("LDAPLdapMapping").'</td>';
135print '<td class="right">'.$langs->trans("LDAPNamingAttribute").'</td>';
136print "</tr>\n";
137
138// Filtre
139
140// Common name
141print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldName").'</td><td>';
142print '<input size="25" type="text" name="fieldfullname" value="'.$conf->global->LDAP_MEMBER_TYPE_FIELD_FULLNAME.'">';
143print '</td><td>'.$langs->trans("LDAPFieldCommonNameExample").'</td>';
144print '<td class="right"><input type="radio" name="key" value="LDAP_MEMBER_TYPE_FIELD_FULLNAME"'.(($conf->global->LDAP_KEY_MEMBERS_TYPES && $conf->global->LDAP_KEY_MEMBERS_TYPES == $conf->global->LDAP_MEMBER_TYPE_FIELD_FULLNAME) ? ' checked' : '')."></td>";
145print '</tr>';
146
147// Description
148print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldDescription").'</td><td>';
149print '<input size="25" type="text" name="fielddescription" value="'.$conf->global->LDAP_MEMBER_TYPE_FIELD_DESCRIPTION.'">';
150print '</td><td>'.$langs->trans("LDAPFieldDescriptionExample").'</td>';
151print '<td class="right"><input type="radio" name="key" value="LDAP_MEMBER_TYPE_FIELD_DESCRIPTION"'.(($conf->global->LDAP_KEY_MEMBERS_TYPES && $conf->global->LDAP_KEY_MEMBER_TYPES == $conf->global->LDAP_MEMBER_TYPE_FIELD_DESCRIPTION) ? ' checked' : '')."></td>";
152print '</tr>';
153
154// User group
155print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldGroupMembers").'</td><td>';
156print '<input size="25" type="text" name="fieldmembertypemembers" value="'.$conf->global->LDAP_MEMBER_TYPE_FIELD_GROUPMEMBERS.'">';
157print '</td><td>'.$langs->trans("LDAPFieldGroupMembersExample").'</td>';
158print '<td class="right"><input type="radio" name="key" value="LDAP_MEMBER_TYPE_FIELD_GROUPMEMBERS"'.(($conf->global->LDAP_KEY_MEMBERS_TYPES && $conf->global->LDAP_KEY_MEMBERS_TYPES == $conf->global->LDAP_MEMBER_TYPE_FIELD_GROUPMEMBERS) ? ' checked' : '')."></td>";
159print '</tr>';
160
161print '</table>';
162
163print info_admin($langs->trans("LDAPDescValues"));
164
165print dol_get_fiche_end();
166
167print '<div class="center"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></div>';
168
169print '</form>';
170
171
172/*
173 * Test de la connexion
174 */
175if ($conf->global->LDAP_MEMBER_TYPE_ACTIVE == '1')
176{
177	$butlabel = $langs->trans("LDAPTestSynchroMemberType");
178	$testlabel = 'testmembertype';
179	$key = $conf->global->LDAP_KEY_MEMBERS_TYPES;
180	$dn = $conf->global->LDAP_MEMBER_TYPE_DN;
181	$objectclass = $conf->global->LDAP_MEMBER_TYPE_OBJECT_CLASS;
182
183	show_ldap_test_button($butlabel, $testlabel, $key, $dn, $objectclass);
184}
185
186if (function_exists("ldap_connect"))
187{
188	if ($_GET["action"] == 'testmembertype')
189	{
190		// Creation objet
191		$object = new AdherentType($db);
192		$object->initAsSpecimen();
193
194		// Test synchro
195		$ldap = new Ldap();
196		$result = $ldap->connect_bind();
197
198		if ($result > 0)
199		{
200			$info = $object->_load_ldap_info();
201			$dn = $object->_load_ldap_dn($info);
202
203			// Get a gid number for objectclass PosixGroup
204			if (in_array('posixGroup', $info['objectclass'])) {
205				$info['gidNumber'] = $ldap->getNextGroupGid('LDAP_KEY_MEMBERS_TYPES');
206			}
207
208			$result1 = $ldap->delete($dn); // To be sure to delete existing records
209			$result2 = $ldap->add($dn, $info, $user); // Now the test
210			$result3 = $ldap->delete($dn); // Clean what we did
211
212			if ($result2 > 0)
213			{
214				print img_picto('', 'info').' ';
215				print '<font class="ok">'.$langs->trans("LDAPSynchroOK").'</font><br>';
216			} else {
217				print img_picto('', 'error').' ';
218				print '<font class="error">'.$langs->trans("LDAPSynchroKOMayBePermissions");
219				print ': '.$ldap->error;
220				print '</font><br>';
221				print $langs->trans("ErrorLDAPMakeManualTest", $conf->ldap->dir_temp).'<br>';
222			}
223
224			print "<br>\n";
225			print "LDAP input file used for test:<br><br>\n";
226			print nl2br($ldap->dump_content($dn, $info));
227			print "\n<br>";
228		} else {
229			print img_picto('', 'error').' ';
230			print '<font class="error">'.$langs->trans("LDAPSynchroKO");
231			print ': '.$ldap->error;
232			print '</font><br>';
233			print $langs->trans("ErrorLDAPMakeManualTest", $conf->ldap->dir_temp).'<br>';
234		}
235	}
236}
237
238// End of page
239llxFooter();
240$db->close();
241