fix(ldapquestion): missing files · pluginsGLPI/formcreator@4b4d801

1+

<?php

2+

/**

3+

* ---------------------------------------------------------------------

4+

* Formcreator is a plugin which allows creation of custom forms of

5+

* easy access.

6+

* ---------------------------------------------------------------------

7+

* LICENSE

8+

*

9+

* This file is part of Formcreator.

10+

*

11+

* Formcreator is free software; you can redistribute it and/or modify

12+

* it under the terms of the GNU General Public License as published by

13+

* the Free Software Foundation; either version 2 of the License, or

14+

* (at your option) any later version.

15+

*

16+

* Formcreator is distributed in the hope that it will be useful,

17+

* but WITHOUT ANY WARRANTY; without even the implied warranty of

18+

* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

19+

* GNU General Public License for more details.

20+

*

21+

* You should have received a copy of the GNU General Public License

22+

* along with Formcreator. If not, see <http://www.gnu.org/licenses/>.

23+

* ---------------------------------------------------------------------

24+

* @copyright Copyright © 2011 - 2020 Teclib'

25+

* @license http://www.gnu.org/licenses/gpl.txt GPLv3+

26+

* @link https://github.com/pluginsGLPI/formcreator/

27+

* @link https://pluginsglpi.github.io/formcreator/

28+

* @link http://plugins.glpi-project.org/#/plugin/formcreator

29+

* ---------------------------------------------------------------------

30+

*/

31+32+

use Glpi\Toolbox\Sanitizer;

33+34+

if (!defined('GLPI_ROOT')) {

35+

die("Sorry. You can't access this file directly");

36+

}

37+38+

class PluginFormcreatorLdapDropdown extends CommonGLPI

39+

{

40+

public static function getTable() {

41+

return '';

42+

}

43+44+

public function getForeignKeyField() {

45+

return '';

46+

}

47+48+

public function isField() {

49+

return false;

50+

}

51+52+

public static function dropdown($options = []) {

53+

$options['display'] = $options['display'] ?? false;

54+

$options['url'] = Plugin::getWebDir('formcreator') . '/ajax/getldapvalues.php';

55+56+

$out = Dropdown::show(self::class, $options);

57+

if (!$options['display']) {

58+

return $out;

59+

}

60+

echo $out;

61+

}

62+63+

public static function getDropdownValue($post, $json = true) {

64+

// Count real items returned

65+

$count = 0;

66+67+

if (isset($post['condition']) && !empty($post['condition']) && !is_array($post['condition'])) {

68+

// Retreive conditions from SESSION using its key

69+

$key = $post['condition'];

70+

$post['condition'] = [];

71+

if (isset($_SESSION['glpicondition']) && isset($_SESSION['glpicondition'][$key])) {

72+

$post['condition'] = $_SESSION['glpicondition'][$key];

73+

}

74+

}

75+76+

$questionId = $post['condition'][PluginFormcreatorQuestion::getForeignKeyField()];

77+

$question = PluginFormcreatorQuestion::getById($questionId);

78+

if (!is_object($question)) {

79+

return [];

80+

}

81+82+

$form = new PluginFormcreatorForm();

83+

$form->getByQuestionId($questionId);

84+

if (!$form->canViewForRequest()) {

85+

return [];

86+

}

87+

$post['searchText'] = $post['searchText'] ?? '';

88+89+

// Search values

90+

$ldap_values = json_decode($question->fields['values'], JSON_OBJECT_AS_ARRAY);

91+

$ldap_dropdown = new RuleRightParameter();

92+

if (!$ldap_dropdown->getFromDB($ldap_values['ldap_attribute'])) {

93+

return [];

94+

}

95+

$attribute = [$ldap_dropdown->fields['value']];

96+97+

$config_ldap = new AuthLDAP();

98+

if (!$config_ldap->getFromDB($ldap_values['ldap_auth'])) {

99+

return [];

100+

}

101+102+

set_error_handler([self::class, 'ldapErrorHandler'], E_WARNING);

103+104+

if ($post['searchText'] != '') {

105+

$ldap_values['ldap_filter'] = sprintf(

106+

"(& %s (%s))",

107+

$ldap_values['ldap_filter'],

108+

$attribute[0] . '=*' . $post['searchText'] . '*'

109+

);

110+

}

111+112+

$tab_values = [];

113+

try {

114+

$cookie = '';

115+

$ds = $config_ldap->connect();

116+

ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);

117+

do {

118+

if (AuthLDAP::isLdapPageSizeAvailable($config_ldap)) {

119+

if (version_compare(PHP_VERSION, '7.3') < 0) {

120+

// phpcs:ignore Generic.PHP.DeprecatedFunctions

121+

ldap_control_paged_result($ds, $config_ldap->fields['pagesize'], true, $cookie);

122+

$result = ldap_search($ds, $config_ldap->fields['basedn'], $ldap_values->ldap_filter, $attribute);

123+

} else {

124+

$controls = [

125+

[

126+

'oid' => LDAP_CONTROL_PAGEDRESULTS,

127+

'iscritical' => true,

128+

'value' => [

129+

'size' => $config_ldap->fields['pagesize'],

130+

'cookie' => $cookie

131+

]

132+

]

133+

];

134+

$result = ldap_search($ds, $config_ldap->fields['basedn'], $ldap_values['ldap_filter'], $attribute, 0, -1, -1, LDAP_DEREF_NEVER, $controls);

135+

ldap_parse_result($ds, $result, $errcode, $matcheddn, $errmsg, $referrals, $controls);

136+

$cookie = $controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'] ?? '';

137+

}

138+

} else {

139+

$result = ldap_search($ds, $config_ldap->fields['basedn'], $ldap_values['ldap_filter'], $attribute);

140+

}

141+142+

$entries = ldap_get_entries($ds, $result);

143+

// openldap return 4 for Size limit exceeded

144+

$limitexceeded = in_array(ldap_errno($ds), [4, 11]);

145+146+

if ($limitexceeded) {

147+

Session::addMessageAfterRedirect(__('LDAP size limit exceeded', 'formcreator'), true, WARNING);

148+

}

149+150+

unset($entries['count']);

151+152+

$foundCount = 0;

153+

foreach ($entries as $attr) {

154+

if (!isset($attr[$attribute[0]]) || in_array($attr[$attribute[0]][0], $tab_values)) {

155+

continue;

156+

}

157+158+

$foundCount++;

159+

if ($foundCount < ((int) $post['page'] - 1) * (int) $post['page_limit'] + 1) {

160+

// before the requested page

161+

continue;

162+

}

163+

if ($foundCount > ((int) $post['page']) * (int) $post['page_limit']) {

164+

// after the requested page

165+

break;

166+

}

167+168+

$tab_values[] = [

169+

'id' => $attr[$attribute[0]][0],

170+

'text' => $attr[$attribute[0]][0],

171+

];

172+

$count++;

173+

if ($count >= $post['page_limit']) {

174+

break;

175+

}

176+

}

177+

if (AuthLDAP::isLdapPageSizeAvailable($config_ldap) && version_compare(PHP_VERSION, '7.3') < 0) {

178+

// phpcs:ignore Generic.PHP.DeprecatedFunctions

179+

ldap_control_paged_result_response($ds, $result, $cookie);

180+

}

181+

} while ($cookie !== null && $cookie != '' && $count < $post['page_limit']);

182+

} catch (Exception $e) {

183+

restore_error_handler();

184+

trigger_error($e->getMessage(), E_USER_WARNING);

185+

}

186+187+

restore_error_handler();

188+189+

$tab_values = Html::entities_deep($tab_values);

190+

usort($tab_values, function($a, $b) {

191+

return strnatcmp($a['text'], $b['text']);

192+

});

193+

$ret['results'] = $tab_values;

194+

$ret['count'] = $count;

195+196+

return ($json === true) ? json_encode($ret) : $ret;

197+

}

198+199+

public static function ldapErrorHandler($errno, $errstr, $errfile, $errline) {

200+

if (0 === error_reporting()) {

201+

return false;

202+

}

203+

throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);

204+

}

205+

}