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 = PluginFormcreatorCommon::getForm ();
83+ $ form = $ form ::getByItem ($ question );
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+ $ foundCount = 0 ;
118+ do {
119+ if (AuthLDAP::isLdapPageSizeAvailable ($ config_ldap )) {
120+ $ controls = [
121+ [
122+ 'oid ' => LDAP_CONTROL_PAGEDRESULTS ,
123+ 'iscritical ' => true ,
124+ 'value ' => [
125+ 'size ' => $ config_ldap ->fields ['pagesize ' ],
126+ 'cookie ' => $ cookie
127+ ]
128+ ]
129+ ];
130+ $ result = ldap_search ($ ds , $ config_ldap ->fields ['basedn ' ], $ ldap_values ['ldap_filter ' ], $ attribute , 0 , -1 , -1 , LDAP_DEREF_NEVER , $ controls );
131+ ldap_parse_result ($ ds , $ result , $ errcode , $ matcheddn , $ errmsg , $ referrals , $ controls );
132+ $ cookie = $ controls [LDAP_CONTROL_PAGEDRESULTS ]['value ' ]['cookie ' ] ?? '' ;
133+ } else {
134+ $ result = ldap_search ($ ds , $ config_ldap ->fields ['basedn ' ], $ ldap_values ['ldap_filter ' ], $ attribute );
135+ }
136+
137+ $ entries = ldap_get_entries ($ ds , $ result );
138+ // openldap return 4 for Size limit exceeded
139+ $ limitexceeded = in_array (ldap_errno ($ ds ), [4 , 11 ]);
140+
141+ if ($ limitexceeded ) {
142+ Session::addMessageAfterRedirect (__ ('LDAP size limit exceeded ' , 'formcreator ' ), true , WARNING );
143+ }
144+
145+ unset($ entries ['count ' ]);
146+
147+ foreach ($ entries as $ attr ) {
148+ if (!isset ($ attr [$ attribute [0 ]]) || in_array ($ attr [$ attribute [0 ]][0 ], $ tab_values )) {
149+ continue ;
150+ }
151+
152+ $ foundCount ++;
153+ if ($ foundCount < ((int ) $ post ['page ' ] - 1 ) * (int ) $ post ['page_limit ' ] + 1 ) {
154+ // before the requested page
155+ continue ;
156+ }
157+ if ($ foundCount > ((int ) $ post ['page ' ]) * (int ) $ post ['page_limit ' ]) {
158+ // after the requested page
159+ break ;
160+ }
161+
162+ $ tab_values [] = [
163+ 'id ' => $ attr [$ attribute [0 ]][0 ],
164+ 'text ' => $ attr [$ attribute [0 ]][0 ],
165+ ];
166+ $ count ++;
167+ if ($ count >= $ post ['page_limit ' ]) {
168+ break ;
169+ }
170+ }
171+ } while ($ cookie !== null && $ cookie != '' && $ count < $ post ['page_limit ' ]);
172+ } catch (Exception $ e ) {
173+ restore_error_handler ();
174+ trigger_error ($ e ->getMessage (), E_USER_WARNING );
175+ }
176+
177+ restore_error_handler ();
178+
179+ $ tab_values = Sanitizer::unsanitize ($ tab_values );
180+ usort ($ tab_values , function ($ a , $ b ) {
181+ return strnatcmp ($ a ['text ' ], $ b ['text ' ]);
182+ });
183+ $ ret ['results ' ] = $ tab_values ;
184+ $ ret ['count ' ] = $ count ;
185+
186+ return ($ json === true ) ? json_encode ($ ret ) : $ ret ;
187+ }
188+
189+ public static function ldapErrorHandler ($ errno , $ errstr , $ errfile , $ errline ) {
190+ if (0 === error_reporting ()) {
191+ return false ;
192+ }
193+ throw new \ErrorException ($ errstr , 0 , $ errno , $ errfile , $ errline );
194+ }
195+ }
0 commit comments