Projet

Général

Profil

Paste
Télécharger (13 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ldap / ldap_views / plugins / ldap_views_plugin_query_ldap.inc @ 32700c57

1
<?php
2

    
3
/**
4
 * @file
5
 * Defines the default query object which builds and execute a ldap query.
6
 */
7

    
8
/**
9
 *
10
 */
11
class ldap_views_plugin_query_ldap extends views_plugin_query {
12

    
13
  /**
14
   * The base dn for the LDAP search.
15
   */
16
  private $basedn = '';
17

    
18
  /**
19
   * A list of filters to apply to the LDAP search.
20
   */
21
  private $filter = [];
22

    
23
  /**
24
   * Builds the necessary info to execute the query.
25
   */
26
  public function build(&$view) {
27
    $view->init_pager($view);
28

    
29
    // Let the pager modify the query to add limits.
30
    $this->pager->query();
31
  }
32

    
33
  /**
34
   *
35
   */
36
  public function add_field($table, $field, $alias = '', $params = []) {
37
    // We check for this specifically because it gets a special alias.
38
    if ($table == $this->base_table && $field == $this->base_field && empty($alias)) {
39
      $alias = $this->base_field;
40
    }
41

    
42
    if (!$alias && $table) {
43
      $alias = $table . '_' . $field;
44
    }
45

    
46
    // Make sure an alias is assigned.
47
    $alias = $alias ? $alias : $field;
48

    
49
    // PostgreSQL truncates aliases to 63 characters: http://drupal.org/node/571548
50
    // We limit the length of the original alias up to 60 characters
51
    // to get a unique alias later if its have duplicates.
52
    $alias = drupal_substr($alias, 0, 60);
53

    
54
    // Create a field info array.
55
    $field_info = [
56
      'field' => $field,
57
      'table' => $table,
58
      'alias' => $alias,
59
    ] + $params;
60

    
61
    // Test to see if the field is actually the same or not. Due to
62
    // differing parameters changing the aggregation function, we need
63
    // to do some automatic alias collision detection:
64
    $base = $alias;
65
    $counter = 0;
66
    while (!empty($this->fields[$alias]) && $this->fields[$alias] != $field_info) {
67
      $field_info['alias'] = $alias = $base . '_' . ++$counter;
68
    }
69

    
70
    if (empty($this->fields[$alias])) {
71
      $this->fields[$alias] = $field_info;
72
    }
73

    
74
    return $alias;
75
  }
76

    
77
  /**
78
   *
79
   */
80
  public function add_orderby($table, $field, $order, $alias = '', $params = []) {
81
    $this->orderby[] = [
82
      'field' => $field,
83
      'direction' => drupal_strtoupper($order),
84
    ];
85
  }
86

    
87
  /**
88
   *
89
   */
90
  public function add_basedn($basedn) {
91
    $this->basedn = !empty($this->basedn) ? $this->basedn : $basedn;
92
  }
93

    
94
  /**
95
   *
96
   */
97
  public function add_filter($filter) {
98
    if (empty($filter)) {
99
      return;
100
    }
101
    $this->filter[] = $filter;
102
  }
103

    
104
  /**
105
   * Add a simple WHERE clause to the query. The caller is responsible for
106
   * ensuring that all fields are fully qualified (TABLE.FIELD) and that
107
   * the table already exists in the query.
108
   *
109
   * @param $group
110
   *   The WHERE group to add these to; groups are used to create AND/OR
111
   *   sections. Groups cannot be nested. Use 0 as the default group.
112
   *   If the group does not yet exist it will be created as an AND group.
113
   * @param $field
114
   *   The name of the field to check.
115
   * @param $value
116
   *   The value to test the field against. In most cases, this is a scalar. For more
117
   *   complex options, it is an array. The meaning of each element in the array is
118
   *   dependent on the $operator.
119
   * @param $operator
120
   *   The comparison operator, such as =, <, or >=. It also accepts more complex
121
   *   options such as IN, LIKE, or BETWEEN. Defaults to IN if $value is an array
122
   *   = otherwise. If $field is a string you have to use 'formula' here.
123
   *
124
   * @see QueryConditionInterface::condition()
125
   */
126
  public function add_where($group, $field, $value = NULL, $operator = NULL) {
127
    // Ensure all variants of 0 are actually 0. Thus '', 0 and NULL are all
128
    // the default group.
129
    if (empty($group)) {
130
      $group = 0;
131
    }
132

    
133
    // Check for a group.
134
    if (!isset($this->where[$group])) {
135
      $this->set_where_group('AND', $group);
136
    }
137

    
138
    $this->where[$group]['conditions'][] = [
139
      'field' => $field,
140
      'value' => $value,
141
      'operator' => ltrim($operator, '!'),
142
      'negate' => drupal_substr($operator, 0, 1) == '!',
143
    ];
144
  }
145

    
146
  /**
147
   * Construct the filter.
148
   *
149
   * @param $where
150
   *   'where' or 'having'.
151
   *
152
   * @return string
153
   */
154
  public function build_condition() {
155
    $operator = ['AND' => '&', 'OR' => '|'];
156
    $main_group = '';
157
    if (!isset($this->where)) {
158
      // Initialize where clause if not set.
159
      $this->where = [];
160
    }
161
    foreach ($this->where as $group => $info) {
162
      if (!empty($info['conditions'])) {
163
        $sub_group = '';
164
        foreach ($info['conditions'] as $key => $clause) {
165
          $item       = '(' . $clause['field'] . $clause['operator'] . $clause['value'] . ')';
166
          $sub_group .= $clause['negate'] ? "(!$item)" : $item;
167
        }
168
        $main_group .= count($info['conditions']) <= 1 ? $sub_group : '(' . $operator[$info['type']] . $sub_group . ')';
169
      }
170
    }
171
    return count($this->where) <= 1 ? $main_group : '(' . $operator[$this->group_operator] . $main_group . ')';
172
  }
173

    
174
  /**
175
   *
176
   */
177
  public function build_ldap_basedn($basedn) {
178
    return !empty($this->basedn) ? [$this->basedn] : $basedn;
179
  }
180

    
181
  /**
182
   *
183
   */
184
  public function build_contextual_filter() {
185
    $contextual_filter = '';
186
    foreach ($this->filter as $condition) {
187
      $contextual_filter .= drupal_substr($condition, 0, 1) != '(' ? "($condition)" : $condition;
188
    }
189
    return $contextual_filter;
190
  }
191

    
192
  /**
193
   *
194
   */
195
  public function build_ldap_filter($filter) {
196
    $condition     = $this->build_condition();
197
    $contextual    = $this->build_contextual_filter();
198
    $search_filter = !empty($contextual) && !empty($condition) ? '(&' . $condition . $contextual . ')' : $condition . $contextual;
199
    return !empty($search_filter) ? $search_filter : $filter;
200
  }
201

    
202
  /**
203
   * Ensure a table exists in the queue; if it already exists it won't
204
   * do anything, but if it doesn't it will add the table queue. It will ensure
205
   * a path leads back to the relationship table.
206
   *
207
   * @param $table
208
   *   The unaliased name of the table to ensure.
209
   * @param $relationship
210
   *   The relationship to ensure the table links to. Each relationship will
211
   *   get a unique instance of the table being added. If not specified,
212
   *   will be the primary table.
213
   * @param $join
214
   *   A views_join object (or derived object) to join the alias in.
215
   *
216
   * @return
217
   *   The alias used to refer to this specific table, or NULL if the table
218
   *   cannot be ensured.
219
   */
220
  public function ensure_table($table, $relationship = NULL, $join = NULL) {
221
    return $table;
222
  }
223

    
224
  /**
225
   * Executes the query and fills the associated view object with according
226
   * values.
227
   *
228
   * Values to set: $view->result, $view->total_rows, $view->execute_time,
229
   * $view->pager['current_page'].
230
   *
231
   * $view->result should contain an array of objects.
232
   */
233
  public function execute(&$view) {
234
    $start       = microtime(TRUE);
235
    $entries     = [];
236
    $num_entries = 0;
237

    
238
    if (empty($this->options['qid'])) {
239
      watchdog('ldap_views', 'Query definition is empty');
240
      return;
241
    }
242
    foreach ($this->fields as $field) {
243
      $attributes[$field['alias']] = $field['field'];
244
      $field_alias[$field['alias']] = drupal_strtolower($field['field']);
245
    }
246

    
247
    $ldap_data   = new LdapQuery(ldap_views_get_qid($view));
248
    $ldap_server = new LdapServer($ldap_data->sid);
249
    $ldap_server->connect();
250
    $ldap_server->bind();
251
    // TODO: Can't use sizelimit if it must be ordered || cache?
252
    // $ldap_server->search() hasn't orderby (ldap_sort)
253
    // But we can't use ldap_sort because there's no DESC option.
254
    foreach ($this->build_ldap_basedn($ldap_data->baseDn) as $basedn) {
255

    
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);
257
      // ldap_sort can't be used because there's no DESC option
258
      // Not an error.
259
      if ($result !== FALSE) {
260
        $entries = array_merge($entries, $result);
261
        $num_entries += $result['count'];
262
        unset($result['count']);
263
      }
264
    }
265
    if (property_exists($view->query, 'limit')) {
266
      $limit = $view->query->limit;
267
    }
268
    $offset      = property_exists($view->query, 'offset') ? $view->query->offset : 0;
269
    $result      = [];
270
    $sort_fields = [];
271
    if (!empty($this->orderby)) {
272
      foreach ($this->orderby as $orderby) {
273
        $sort_fields[drupal_strtolower($orderby['field'])]['direction'] = $orderby['direction'];
274
        $sort_fields[drupal_strtolower($orderby['field'])]['data']      = [];
275
      }
276

    
277
    }
278

    
279
    foreach ($entries as $key => &$entry) {
280
      if (isset($entry['jpegphoto'])) {
281
        $entry['jpegphoto'][0] = '<img src="data:image/jpeg;base64,' . base64_encode($entry['jpegphoto'][0]) . '" alt="photo" />';
282
      }
283
      if (isset($entry['thumbnailphoto'])) {
284
        $entry['thumbnailphoto'][0] = '<img src="data:image/jpeg;base64,' . base64_encode($entry['thumbnailphoto'][0]) . '" alt="photo" />';
285
      }
286
      foreach ($view->field as $field) {
287
        if (!isset($field_alias[$field->field_alias])) {
288
          continue;
289
        }
290
        $alias = $field_alias[$field->field_alias];
291
        if (is_array($entry) && array_key_exists($alias, $entry)) {
292
          if (is_array($entry[$alias])) {
293
            switch ($field->options['multivalue']) {
294
              case 'v-all':
295
                // Remove 'count' index.
296
                unset($entry[$alias]['count']);
297
                $entry[$alias] = implode($field->options['value_separator'], $entry[$alias]);
298
                break;
299

    
300
              case 'v-count':
301
                $entry[$alias] = $entry[$alias]['count'];
302
                break;
303

    
304
              case 'v-index':
305
                $index = $field->options['index_value'] >= 0 ? intval($field->options['index_value']) : $entry[$alias]['count'] + $field->options['index_value'];
306
                $entry[$alias] = array_key_exists($index, $entry[$alias]) ? $entry[$alias][$index] :
307
                                                                            $entry[$alias][0];
308
                break;
309
            }
310
          }
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.
313
          if (array_key_exists($alias, $sort_fields)) {
314
            $sort_fields[$alias]['data'][$key] = $entry[$alias];
315
          }
316
        }
317
      }
318
    }
319
    if (!empty($this->orderby) && !empty($entries)) {
320
      $params = [];
321
      // In PHP 5.3 every parameter in the array has to be a reference when calling array_multisort() with call_user_func_array().
322
      $asc = SORT_ASC;
323
      $desc = SORT_DESC;
324
      foreach ($sort_fields as &$field) {
325
        $params[] = &$field['data'];
326
        if (drupal_strtoupper($field['direction']) == 'ASC') {
327
          $params[] = &$asc;
328
        }
329
        else {
330
          $params[] = &$desc;
331
        }
332
      }
333
      $params[] = &$entries;
334

    
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.
338
      if (array_key_exists('count', $entries)) {
339
        // Remove the count variable.
340
        $countValue = $entries['count'];
341
        unset($entries['count']);
342
        $params[] = &$entries;
343
        call_user_func_array('array_multisort', $params);
344
        $params['count'] = $countValue;
345
      }
346
      else {
347
        $params[] = &$entries;
348
        call_user_func_array('array_multisort', $params);
349
      }
350
    }
351

    
352
    for ($i = 0; (!isset($limit) || $i < $limit) && $offset + $i < $num_entries; $i++) {
353
      $row = [];
354
      $entry = &$entries[$offset + $i];
355
      foreach ($view->field as $field) {
356
        if (!isset($field_alias[$field->field_alias])) {
357
          continue;
358
        }
359
        if (array_key_exists($field_alias[$field->field_alias], $entry)) {
360
          $row[$field->field_alias] = $entry[$field_alias[$field->field_alias]];
361
        }
362
      }
363
      $result[] = $row;
364
    }
365

    
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;
370
    $view->query->pager->update_page_info();
371

    
372
  }
373

    
374
  /**
375
   *
376
   */
377
  public function option_definition() {
378
    $options = parent::option_definition();
379
    $options['qid'] = ['default' => ''];
380

    
381
    return $options;
382
  }
383

    
384
  /**
385
   *
386
   */
387
  public function options_form(&$form, &$form_state) {
388
    $queries = [];
389
    $queries['all'] = LdapQueryAdmin::getLdapQueryObjects();
390

    
391
    foreach ($queries['all'] as $_sid => $ldap_query) {
392
      if ($ldap_query->status == 1) {
393
        $options[$ldap_query->qid] = $ldap_query->name;
394
      }
395
    }
396
    $form['qid'] = [
397
      '#type' => 'select',
398
      '#title' => t('LDAP Search'),
399
      '#options' => $options,
400
      '#default_value' => $this->options['qid'],
401
      '#description' => t("The LDAP server to query."),
402
    ];
403
  }
404

    
405
  /**
406
   * Let modules modify the query just prior to finalizing it.
407
   */
408
  public function alter(&$view) {
409
    foreach (module_implements('views_query_alter') as $module) {
410
      $function = $module . '_views_query_alter';
411
      $function($view, $this);
412
    }
413
  }
414

    
415
}