1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8/**
9 * Handler class for LDAP. Was not extensively tested after migration.
10 *
11 * Letter key: ~P~
12 *
13 */
14class Tracker_Field_Ldap extends Tracker_Field_Abstract
15{
16	public static function getTypes()
17	{
18		return [
19			'P' => [
20				'name' => tr('LDAP'),
21				'description' => tr('Display a field value from a specific user in LDAP'),
22				'readonly' => true,
23				'help' => 'LDAP Tracker Field',
24				'prefs' => ['trackerfield_ldap'],
25				'tags' => ['advanced'],
26				'default' => 'n',
27				'params' => [
28					'filter' => [
29						'name' => tr('Filter'),
30						'description' => tr('LDAP filter, can contain the %field_name% placeholder to be replaced with the current field\'s name'),
31						'example' => '(&(mail=%field_name%)(objectclass=posixaccount))',
32						'filter' => 'none',
33						'legacy_index' => 0,
34					],
35					'field' => [
36						'name' => tr('Field'),
37						'description' => tr('Field name returned by LDAP'),
38						'filter' => 'text',
39						'legacy_index' => 1,
40					],
41					'dsn' => [
42						'name' => tr('DSN'),
43						'description' => tr('Data source name registered in Tiki'),
44						'filter' => 'text',
45						'legacy_index' => 2,
46					],
47				],
48			],
49		];
50	}
51
52	function getFieldData(array $requestData = [])
53	{
54		if ($this->getOption('dsn')) {
55			$ldaplib = TikiLib::lib('ldap');
56
57			// Retrieve DSN
58			$info_ldap = TikiLib::lib('admin')->get_dsn_from_name($this->getOption('dsn'));
59
60			if ($info_ldap) {
61				$ldap_filter = $this->getOption('filter');
62
63				// Replace %field_name% by real value
64				preg_match('/%([^%]+)%/', $ldap_filter, $ldap_filter_field_names);
65
66				if (isset($ldap_filter_field_names[1])) {
67					$field = $this->getTrackerDefinition()->getFieldFromName($ldap_filter_field_names[1]);
68
69					if ($field) {
70						$value = TikiLib::lib('trk')->get_field_value($field, $this->getItemData());
71
72						$ldap_filter = preg_replace('/%' . $ldap_filter_field_names[1] . '%/', $value, $ldap_filter);
73
74						// Get LDAP field value
75						return [
76							'value' => $ldaplib->get_field($info_ldap['dsn'], $ldap_filter, $this->getOption('field')),
77						];
78					}
79				}
80			}
81		}
82	}
83
84	function renderInput($context = [])
85	{
86		return $this->getConfiguration('value');
87	}
88}
89