Révision 32700c57
Ajouté par Assos Assos il y a environ 5 ans
drupal7/sites/all/modules/ldap/ldap_views/plugins/ldap_views_plugin_query_ldap.inc | ||
---|---|---|
2 | 2 |
|
3 | 3 |
/** |
4 | 4 |
* @file |
5 |
* Defines the default query object which builds and execute a ldap query |
|
5 |
* Defines the default query object which builds and execute a ldap query.
|
|
6 | 6 |
*/ |
7 | 7 |
|
8 |
/** |
|
9 |
* |
|
10 |
*/ |
|
8 | 11 |
class ldap_views_plugin_query_ldap extends views_plugin_query { |
9 | 12 |
|
10 | 13 |
/** |
11 |
* The base dn for the LDAP search |
|
14 |
* The base dn for the LDAP search.
|
|
12 | 15 |
*/ |
13 | 16 |
private $basedn = ''; |
14 | 17 |
|
15 | 18 |
/** |
16 |
* A list of filters to apply to the LDAP search |
|
19 |
* A list of filters to apply to the LDAP search.
|
|
17 | 20 |
*/ |
18 |
private $filter = array();
|
|
21 |
private $filter = [];
|
|
19 | 22 |
|
20 | 23 |
/** |
21 | 24 |
* Builds the necessary info to execute the query. |
22 | 25 |
*/ |
23 |
function build(&$view) { |
|
26 |
public function build(&$view) {
|
|
24 | 27 |
$view->init_pager($view); |
25 | 28 |
|
26 | 29 |
// Let the pager modify the query to add limits. |
27 | 30 |
$this->pager->query(); |
28 | 31 |
} |
29 | 32 |
|
30 |
function add_field($table, $field, $alias = '', $params = array()) { |
|
33 |
/** |
|
34 |
* |
|
35 |
*/ |
|
36 |
public function add_field($table, $field, $alias = '', $params = []) { |
|
31 | 37 |
// We check for this specifically because it gets a special alias. |
32 | 38 |
if ($table == $this->base_table && $field == $this->base_field && empty($alias)) { |
33 | 39 |
$alias = $this->base_field; |
... | ... | |
37 | 43 |
$alias = $table . '_' . $field; |
38 | 44 |
} |
39 | 45 |
|
40 |
// Make sure an alias is assigned |
|
46 |
// Make sure an alias is assigned.
|
|
41 | 47 |
$alias = $alias ? $alias : $field; |
42 | 48 |
|
43 | 49 |
// PostgreSQL truncates aliases to 63 characters: http://drupal.org/node/571548 |
44 |
|
|
45 | 50 |
// We limit the length of the original alias up to 60 characters |
46 |
// to get a unique alias later if its have duplicates |
|
51 |
// to get a unique alias later if its have duplicates.
|
|
47 | 52 |
$alias = drupal_substr($alias, 0, 60); |
48 | 53 |
|
49 | 54 |
// Create a field info array. |
50 |
$field_info = array(
|
|
55 |
$field_info = [
|
|
51 | 56 |
'field' => $field, |
52 | 57 |
'table' => $table, |
53 | 58 |
'alias' => $alias, |
54 |
) + $params;
|
|
59 |
] + $params;
|
|
55 | 60 |
|
56 | 61 |
// Test to see if the field is actually the same or not. Due to |
57 | 62 |
// differing parameters changing the aggregation function, we need |
... | ... | |
69 | 74 |
return $alias; |
70 | 75 |
} |
71 | 76 |
|
72 |
function add_orderby($table, $field, $order, $alias = '', $params = array()) { |
|
73 |
$this->orderby[] = array( |
|
77 |
/** |
|
78 |
* |
|
79 |
*/ |
|
80 |
public function add_orderby($table, $field, $order, $alias = '', $params = []) { |
|
81 |
$this->orderby[] = [ |
|
74 | 82 |
'field' => $field, |
75 |
'direction' => drupal_strtoupper($order) |
|
76 |
);
|
|
83 |
'direction' => drupal_strtoupper($order),
|
|
84 |
];
|
|
77 | 85 |
} |
78 | 86 |
|
79 |
function add_basedn($basedn) { |
|
87 |
/** |
|
88 |
* |
|
89 |
*/ |
|
90 |
public function add_basedn($basedn) { |
|
80 | 91 |
$this->basedn = !empty($this->basedn) ? $this->basedn : $basedn; |
81 | 92 |
} |
82 | 93 |
|
83 |
function add_filter($filter) { |
|
94 |
/** |
|
95 |
* |
|
96 |
*/ |
|
97 |
public function add_filter($filter) { |
|
84 | 98 |
if (empty($filter)) { |
85 | 99 |
return; |
86 | 100 |
} |
... | ... | |
109 | 123 |
* |
110 | 124 |
* @see QueryConditionInterface::condition() |
111 | 125 |
*/ |
112 |
function add_where($group, $field, $value = NULL, $operator = NULL) { |
|
126 |
public function add_where($group, $field, $value = NULL, $operator = NULL) {
|
|
113 | 127 |
// Ensure all variants of 0 are actually 0. Thus '', 0 and NULL are all |
114 | 128 |
// the default group. |
115 | 129 |
if (empty($group)) { |
... | ... | |
121 | 135 |
$this->set_where_group('AND', $group); |
122 | 136 |
} |
123 | 137 |
|
124 |
$this->where[$group]['conditions'][] = array(
|
|
138 |
$this->where[$group]['conditions'][] = [
|
|
125 | 139 |
'field' => $field, |
126 | 140 |
'value' => $value, |
127 | 141 |
'operator' => ltrim($operator, '!'), |
128 | 142 |
'negate' => drupal_substr($operator, 0, 1) == '!', |
129 |
);
|
|
143 |
];
|
|
130 | 144 |
} |
131 | 145 |
|
132 | 146 |
/** |
133 |
* Construct the filter |
|
147 |
* Construct the filter.
|
|
134 | 148 |
* |
135 | 149 |
* @param $where |
136 | 150 |
* 'where' or 'having'. |
151 |
* |
|
152 |
* @return string |
|
137 | 153 |
*/ |
138 |
function build_condition() { |
|
139 |
$operator = array('AND' => '&', 'OR' => '|');
|
|
154 |
public function build_condition() {
|
|
155 |
$operator = ['AND' => '&', 'OR' => '|'];
|
|
140 | 156 |
$main_group = ''; |
141 | 157 |
if (!isset($this->where)) { |
142 |
$this->where = array(); // Initialize where clause if not set |
|
158 |
// Initialize where clause if not set. |
|
159 |
$this->where = []; |
|
143 | 160 |
} |
144 | 161 |
foreach ($this->where as $group => $info) { |
145 | 162 |
if (!empty($info['conditions'])) { |
... | ... | |
154 | 171 |
return count($this->where) <= 1 ? $main_group : '(' . $operator[$this->group_operator] . $main_group . ')'; |
155 | 172 |
} |
156 | 173 |
|
157 |
function build_ldap_basedn($basedn) { |
|
158 |
return !empty($this->basedn) ? array($this->basedn) : $basedn; |
|
174 |
/** |
|
175 |
* |
|
176 |
*/ |
|
177 |
public function build_ldap_basedn($basedn) { |
|
178 |
return !empty($this->basedn) ? [$this->basedn] : $basedn; |
|
159 | 179 |
} |
160 | 180 |
|
161 |
function build_contextual_filter() { |
|
181 |
/** |
|
182 |
* |
|
183 |
*/ |
|
184 |
public function build_contextual_filter() { |
|
162 | 185 |
$contextual_filter = ''; |
163 | 186 |
foreach ($this->filter as $condition) { |
164 | 187 |
$contextual_filter .= drupal_substr($condition, 0, 1) != '(' ? "($condition)" : $condition; |
... | ... | |
166 | 189 |
return $contextual_filter; |
167 | 190 |
} |
168 | 191 |
|
169 |
function build_ldap_filter($filter) { |
|
192 |
/** |
|
193 |
* |
|
194 |
*/ |
|
195 |
public function build_ldap_filter($filter) { |
|
170 | 196 |
$condition = $this->build_condition(); |
171 | 197 |
$contextual = $this->build_contextual_filter(); |
172 | 198 |
$search_filter = !empty($contextual) && !empty($condition) ? '(&' . $condition . $contextual . ')' : $condition . $contextual; |
... | ... | |
191 | 217 |
* The alias used to refer to this specific table, or NULL if the table |
192 | 218 |
* cannot be ensured. |
193 | 219 |
*/ |
194 |
function ensure_table($table, $relationship = NULL, $join = NULL) { |
|
220 |
public function ensure_table($table, $relationship = NULL, $join = NULL) {
|
|
195 | 221 |
return $table; |
196 | 222 |
} |
197 | 223 |
|
... | ... | |
204 | 230 |
* |
205 | 231 |
* $view->result should contain an array of objects. |
206 | 232 |
*/ |
207 |
function execute(&$view) { |
|
233 |
public function execute(&$view) {
|
|
208 | 234 |
$start = microtime(TRUE); |
209 |
$entries = array();
|
|
235 |
$entries = [];
|
|
210 | 236 |
$num_entries = 0; |
211 | 237 |
|
212 | 238 |
if (empty($this->options['qid'])) { |
... | ... | |
224 | 250 |
$ldap_server->bind(); |
225 | 251 |
// TODO: Can't use sizelimit if it must be ordered || cache? |
226 | 252 |
// $ldap_server->search() hasn't orderby (ldap_sort) |
227 |
// But we can't use ldap_sort because there's no DESC option |
|
253 |
// But we can't use ldap_sort because there's no DESC option.
|
|
228 | 254 |
foreach ($this->build_ldap_basedn($ldap_data->baseDn) as $basedn) { |
229 | 255 |
|
230 | 256 |
$result = $ldap_server->search($basedn, $this->build_ldap_filter($ldap_data->filter), array_values($attributes), 0, $ldap_data->sizelimit, $ldap_data->timelimit, $ldap_data->deref, $ldap_data->scope); |
231 |
/** |
|
232 |
// ldap_sort can't be used because there's no DESC option |
|
233 |
if (!empty($this->orderby)) { |
|
234 |
// Array reverse, because the most specific are first - PHP works the opposite way of SQL. |
|
235 |
foreach (array_reverse($this->orderby) as $field) { |
|
236 |
@ldap_sort($ldap_server->connection, $result, $field['field']); |
|
237 |
} |
|
238 |
} |
|
239 |
*/ |
|
240 |
if ($result !== FALSE) { // not an error |
|
257 |
// ldap_sort can't be used because there's no DESC option |
|
258 |
// Not an error. |
|
259 |
if ($result !== FALSE) { |
|
241 | 260 |
$entries = array_merge($entries, $result); |
242 | 261 |
$num_entries += $result['count']; |
243 | 262 |
unset($result['count']); |
244 | 263 |
} |
245 | 264 |
} |
246 | 265 |
if (property_exists($view->query, 'limit')) { |
247 |
$limit = $view->query->limit;
|
|
266 |
$limit = $view->query->limit; |
|
248 | 267 |
} |
249 |
$offset = property_exists($view->query, 'offset') ? $view->query->offset : 0; |
|
250 |
$result = array();
|
|
251 |
$sort_fields = array();
|
|
268 |
$offset = property_exists($view->query, 'offset') ? $view->query->offset : 0;
|
|
269 |
$result = [];
|
|
270 |
$sort_fields = [];
|
|
252 | 271 |
if (!empty($this->orderby)) { |
253 | 272 |
foreach ($this->orderby as $orderby) { |
254 | 273 |
$sort_fields[drupal_strtolower($orderby['field'])]['direction'] = $orderby['direction']; |
255 |
$sort_fields[drupal_strtolower($orderby['field'])]['data'] = array();
|
|
274 |
$sort_fields[drupal_strtolower($orderby['field'])]['data'] = [];
|
|
256 | 275 |
} |
257 | 276 |
|
258 | 277 |
} |
... | ... | |
265 | 284 |
$entry['thumbnailphoto'][0] = '<img src="data:image/jpeg;base64,' . base64_encode($entry['thumbnailphoto'][0]) . '" alt="photo" />'; |
266 | 285 |
} |
267 | 286 |
foreach ($view->field as $field) { |
268 |
if (! isset($field_alias[$field->field_alias])) {
|
|
287 |
if (!isset($field_alias[$field->field_alias])) { |
|
269 | 288 |
continue; |
270 | 289 |
} |
271 | 290 |
$alias = $field_alias[$field->field_alias]; |
... | ... | |
273 | 292 |
if (is_array($entry[$alias])) { |
274 | 293 |
switch ($field->options['multivalue']) { |
275 | 294 |
case 'v-all': |
276 |
// remove 'count' index
|
|
295 |
// Remove 'count' index.
|
|
277 | 296 |
unset($entry[$alias]['count']); |
278 | 297 |
$entry[$alias] = implode($field->options['value_separator'], $entry[$alias]); |
279 | 298 |
break; |
299 |
|
|
280 | 300 |
case 'v-count': |
281 | 301 |
$entry[$alias] = $entry[$alias]['count']; |
282 | 302 |
break; |
303 |
|
|
283 | 304 |
case 'v-index': |
284 | 305 |
$index = $field->options['index_value'] >= 0 ? intval($field->options['index_value']) : $entry[$alias]['count'] + $field->options['index_value']; |
285 | 306 |
$entry[$alias] = array_key_exists($index, $entry[$alias]) ? $entry[$alias][$index] : |
... | ... | |
287 | 308 |
break; |
288 | 309 |
} |
289 | 310 |
} |
290 |
// order criteria
|
|
291 |
//If the field with alias $alias has a corresponding entry in $sort_fields, copy its value into the data key of $sort_fields for later sorting. |
|
311 |
// Order criteria
|
|
312 |
// If the field with alias $alias has a corresponding entry in $sort_fields, copy its value into the data key of $sort_fields for later sorting.
|
|
292 | 313 |
if (array_key_exists($alias, $sort_fields)) { |
293 | 314 |
$sort_fields[$alias]['data'][$key] = $entry[$alias]; |
294 | 315 |
} |
... | ... | |
296 | 317 |
} |
297 | 318 |
} |
298 | 319 |
if (!empty($this->orderby) && !empty($entries)) { |
299 |
$params = array();
|
|
320 |
$params = [];
|
|
300 | 321 |
// In PHP 5.3 every parameter in the array has to be a reference when calling array_multisort() with call_user_func_array(). |
301 |
$asc = SORT_ASC;
|
|
322 |
$asc = SORT_ASC; |
|
302 | 323 |
$desc = SORT_DESC; |
303 | 324 |
foreach ($sort_fields as &$field) { |
304 | 325 |
$params[] = &$field['data']; |
... | ... | |
311 | 332 |
} |
312 | 333 |
$params[] = &$entries; |
313 | 334 |
|
314 |
// Some LDAP setups output a 'count' variable in the array, which changes the array size; |
|
315 |
// temporarily remove it, sort the arrays, and then put it back. |
|
335 |
// Some LDAP setups output a 'count' variable in the array, which changes |
|
336 |
// the array size; temporarily remove it, sort the arrays, and then put it |
|
337 |
// back. |
|
316 | 338 |
if (array_key_exists('count', $entries)) { |
317 |
$countValue = $entries['count']; // remove the count variable |
|
339 |
// Remove the count variable. |
|
340 |
$countValue = $entries['count']; |
|
318 | 341 |
unset($entries['count']); |
319 | 342 |
$params[] = &$entries; |
320 | 343 |
call_user_func_array('array_multisort', $params); |
... | ... | |
327 | 350 |
} |
328 | 351 |
|
329 | 352 |
for ($i = 0; (!isset($limit) || $i < $limit) && $offset + $i < $num_entries; $i++) { |
330 |
$row = array();
|
|
353 |
$row = [];
|
|
331 | 354 |
$entry = &$entries[$offset + $i]; |
332 | 355 |
foreach ($view->field as $field) { |
333 |
if (! isset($field_alias[$field->field_alias])) {
|
|
356 |
if (!isset($field_alias[$field->field_alias])) { |
|
334 | 357 |
continue; |
335 | 358 |
} |
336 | 359 |
if (array_key_exists($field_alias[$field->field_alias], $entry)) { |
... | ... | |
340 | 363 |
$result[] = $row; |
341 | 364 |
} |
342 | 365 |
|
343 |
$view->result = $result; |
|
344 |
$view->total_rows = $num_entries; |
|
345 |
$view->execute_time = microtime(TRUE) - $start; |
|
346 |
$view->query->pager->total_items = $num_entries;
|
|
366 |
$view->result = $result;
|
|
367 |
$view->total_rows = $num_entries;
|
|
368 |
$view->execute_time = microtime(TRUE) - $start;
|
|
369 |
$view->query->pager->total_items = $num_entries; |
|
347 | 370 |
$view->query->pager->update_page_info(); |
348 | 371 |
|
349 | 372 |
} |
350 | 373 |
|
351 |
function option_definition() { |
|
374 |
/** |
|
375 |
* |
|
376 |
*/ |
|
377 |
public function option_definition() { |
|
352 | 378 |
$options = parent::option_definition(); |
353 |
$options['qid'] = array('default' => '');
|
|
379 |
$options['qid'] = ['default' => ''];
|
|
354 | 380 |
|
355 | 381 |
return $options; |
356 | 382 |
} |
357 | 383 |
|
358 |
function options_form(&$form, &$form_state) { |
|
359 |
/** |
|
360 |
$ldap_data = entity_load('ldap_data', FALSE); |
|
361 |
$options = array(); |
|
362 |
foreach ($ldap_data as $data) { |
|
363 |
$options[$data->qid] = $data->name; |
|
364 |
} |
|
365 |
*/ |
|
366 |
$queries = array(); |
|
384 |
/** |
|
385 |
* |
|
386 |
*/ |
|
387 |
public function options_form(&$form, &$form_state) { |
|
388 |
$queries = []; |
|
367 | 389 |
$queries['all'] = LdapQueryAdmin::getLdapQueryObjects(); |
368 | 390 |
|
369 |
foreach ($queries['all'] as $_sid => $ldap_query) { |
|
370 |
if ($ldap_query->status == 1) { |
|
371 |
//$queries['enabled'][$_qid] = $ldap_query;
|
|
372 |
$options[$ldap_query->qid] = $ldap_query->name;
|
|
391 |
foreach ($queries['all'] as $_sid => $ldap_query) {
|
|
392 |
if ($ldap_query->status == 1) {
|
|
393 |
$options[$ldap_query->qid] = $ldap_query->name;
|
|
394 |
}
|
|
373 | 395 |
} |
374 |
} |
|
375 |
// ****************************************************** |
|
376 |
$form['qid'] = array( |
|
396 |
$form['qid'] = [ |
|
377 | 397 |
'#type' => 'select', |
378 | 398 |
'#title' => t('LDAP Search'), |
379 | 399 |
'#options' => $options, |
380 | 400 |
'#default_value' => $this->options['qid'], |
381 | 401 |
'#description' => t("The LDAP server to query."), |
382 |
);
|
|
402 |
];
|
|
383 | 403 |
} |
384 | 404 |
|
385 |
|
|
386 | 405 |
/** |
387 | 406 |
* Let modules modify the query just prior to finalizing it. |
388 | 407 |
*/ |
389 |
function alter(&$view) { |
|
408 |
public function alter(&$view) {
|
|
390 | 409 |
foreach (module_implements('views_query_alter') as $module) { |
391 | 410 |
$function = $module . '_views_query_alter'; |
392 | 411 |
$function($view, $this); |
393 | 412 |
} |
394 | 413 |
} |
395 | 414 |
|
396 |
/* Only when adding dynamic fields in ldap_views_views_data_alter() |
|
397 |
function options_submit(&$form, &$form_state) { |
|
398 |
parent::options_submit(&$form, &$form_state); |
|
399 |
if ($form_state['values']['query']['options']['qid'] != $form_state['view']->query->options['qid']) { |
|
400 |
views_invalidate_cache(); |
|
401 |
} |
|
402 |
} |
|
403 |
*/ |
|
404 | 415 |
} |
Formats disponibles : Unified diff
Weekly update of contrib modules