Projet

Général

Profil

Paste
Télécharger (12,7 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ldap / ldap_query / LdapQuery.class.php @ bc175c27

1
<?php
2

    
3
/**
4
 * @file
5
 * Defines server classes and related functions.
6
 *
7
 */
8

    
9
/**
10
 * LDAP Server Class
11
 *
12
 *  This class is used to create, work with, and eventually destroy ldap_server
13
 * objects.
14
 *
15
 * @todo make bindpw protected
16
 */
17
class LdapQuery {
18
  // LDAP Settings
19

    
20
  public $query_numeric_id;
21
  public $qid;
22
  public $name;
23
  public $sid;
24
  public $status;
25

    
26
  public $baseDn = array();
27
  public $base_dn_str = NULL;
28
  public $filter;
29
  public $attributes_str = NULL;
30
  public $attributes = array();
31

    
32
  public $sizelimit = 0;
33
  public $timelimit = 0;
34
  public $deref = LDAP_DEREF_NEVER;
35
  public $scope = LDAP_SCOPE_SUBTREE;
36

    
37

    
38
  public $inDatabase = FALSE;
39
  public $detailedWatchdogLog = FALSE;
40

    
41

    
42
  /**
43
   * Constructor Method
44
   */
45
  function __construct($qid) {
46
    if (!is_scalar($qid)) {
47
      return;
48
    }
49

    
50
    $query_records = array();
51
    if (module_exists('ctools')) {
52
      ctools_include('export');
53
      $result = ctools_export_load_object('ldap_query', 'names', array($qid));
54
      if (isset($result[$qid])) {
55
        $query_record = $result[$qid];
56
        foreach ($query_record as $property_name => $value) {
57
          $this->{$property_name} = $value;
58
        }
59
      }
60
    }
61
    else {
62
      $select = db_select('ldap_query')
63
        ->fields('ldap_query')
64
        ->condition('ldap_query.qid', $qid)
65
        ->execute();
66
      foreach ($select as $record) {
67
        $query_records[$record->qid] = $record;
68
      }
69
      if (!isset($query_records[$qid])) {
70
        $this->inDatabase = FALSE;
71
        return;
72
      }
73
      $query_record = $query_records[$qid];
74
      foreach ($this->fields() as $field_id => $field ) {
75
        if (isset($query_record->$field_id)) {
76
          $this->{$field['property_name']} = @$query_record->$field_id;
77
        }
78
      }
79
    }
80

    
81
    // special properties that don't map directly from storage and defaults
82
    $this->inDatabase = TRUE;
83
    $this->detailedWatchdogLog = variable_get('ldap_help_watchdog_detail', 0);
84

    
85
    $this->baseDn = $this->linesToArray($this->base_dn_str);
86
    $this->attributes = ($this->attributes_str) ? $this->csvToArray($this->attributes_str, TRUE) : array();
87

    
88
  }
89

    
90
  /**
91
   * Destructor Method
92
   */
93
  function __destruct() {
94

    
95
  }
96

    
97

    
98
  /**
99
   * Invoke Method
100
   */
101
  function __invoke() {
102

    
103
  }
104

    
105
//  function search($base_dn = NULL, $filter, $attributes = array(), $attrsonly = 0, $sizelimit = 0, $timelimit = 0, $deref = LDAP_DEREF_NEVER) {
106

    
107
  function query() {
108
    ldap_servers_module_load_include('php', 'ldap_servers', 'LdapServer.class');
109
    $ldap_server = new LdapServer($this->sid);
110
    $ldap_server->connect();
111
    $ldap_server->bind();
112
    $results = array();
113

    
114
    $count = 0;
115

    
116
    foreach ($this->baseDn as $base_dn) {
117
      $result = $ldap_server->search($base_dn, $this->filter, $this->attributes, 0, $this->sizelimit, $this->timelimit, $this->deref, $this->scope);
118
      if ($result !== FALSE && $result['count'] > 0) {
119
        $count = $count + $result['count'];
120
        $results = array_merge($results, $result);
121
      }
122
    }
123
    $results['count'] = $count;
124

    
125
    return $results;
126
  }
127

    
128
  /**
129
   * Error methods and properties.
130
   */
131

    
132
  protected $_errorMsg = NULL;
133
  protected $_hasError = FALSE;
134
  protected $_errorName = NULL;
135

    
136
  public function setError($_errorName, $_errorMsgText = NULL) {
137
    $this->_errorMsgText = $_errorMsgText;
138
    $this->_errorName = $_errorName;
139
    $this->_hasError = TRUE;
140
  }
141

    
142
  public function clearError() {
143
    $this->_hasError = FALSE;
144
    $this->_errorMsg = NULL;
145
    $this->_errorName = NULL;
146
  }
147

    
148
  public function hasError() {
149
    return ($this->_hasError || $this->ldapErrorNumber());
150
  }
151

    
152
  public function errorMsg($type = NULL) {
153
    if ($type == 'ldap' && $this->connection) {
154
      return ldap_err2str(ldap_errno($this->connection));
155
    }
156
    elseif ($type == NULL) {
157
      return $this->_errorMsg;
158
    }
159
    else {
160
      return NULL;
161
    }
162
  }
163

    
164
  public function errorName($type = NULL) {
165
    if ($type == 'ldap' && $this->connection) {
166
      return "LDAP Error: " . ldap_error($this->connection);
167
    }
168
    elseif ($type == NULL) {
169
      return $this->_errorName;
170
    }
171
    else {
172
      return NULL;
173
    }
174
  }
175

    
176
  public function ldapErrorNumber() {
177
   // if ($this->connection && ldap_errno($this->connection)) {
178
    //  return ldap_errno($this->connection);
179
   // }
180
   // else {
181
      return FALSE;
182
   // }
183
  }
184

    
185
  protected function linesToArray($lines) {
186
    $lines = trim($lines);
187
    if ($lines) {
188
      $array = preg_split('/[\n\r]+/', $lines);
189
      foreach ($array as $i => $value) {
190
        $array[$i] = trim($value);
191
      }
192
    }
193
    else {
194
      $array = array();
195
    }
196
    return $array;
197
  }
198

    
199
  protected function csvToArray($string, $strip_quotes = FALSE) {
200
    $items = explode(',', $string);
201
    foreach ($items as $i => $item) {
202
      $items[$i] = trim($item);
203
      if ($strip_quotes) {
204
        $items[$i] = trim($items[$i], '"');
205
      }
206
    }
207
    return $items;
208
  }
209

    
210
  public static function fields() {
211
    $fields = array(
212
      'query_numeric_id' => array(
213
          'property_name' => 'query_numeric_id',
214
          'schema' => array(
215
            'type' => 'serial',
216
            'unsigned' => TRUE,
217
            'not null' => TRUE,
218
            'description' => 'Primary ID field for the table.  Only used internally.',
219
            'no export' => TRUE,
220
          ),
221
        ),
222

    
223
      'qid' => array(
224
        'property_name' => 'qid',
225
        'schema' => array(
226
          'type' => 'varchar',
227
          'length' => 20,
228
          'description' => 'Machine name for query.',
229
          'not null' => TRUE,
230
          ),
231
        'form' => array(
232
          'field_group' => 'basic',
233
          '#type' => 'textfield',
234
          '#title' => t('Machine name for this query configuration.'),
235
          '#size' => 20,
236
          '#maxlength' => 20,
237
          '#description' => t('May only contain alphanumeric characters (a-z, A-Z, 0-9, and _)' ),
238
          '#required' => TRUE,
239
        ),
240
        'form_to_prop_functions' => array('trim'),
241
      ),
242

    
243
      'name' => array(
244
        'property_name' => 'name',
245
        'schema' => array(
246
          'type' => 'varchar',
247
          'length' => '60',
248
          'not null' => TRUE
249
        ),
250
        'form' => array(
251
          'field_group' => 'basic',
252
          '#type' => 'textfield',
253
          '#title' => t('Name'),
254
          '#description' => t('Choose a name for this query configuration.'),
255
          '#size' => 50,
256
          '#maxlength' => 255,
257
          '#required' => TRUE,
258
        ),
259
        'form_to_prop_functions' => array('trim'),
260
      ),
261

    
262
      'sid' => array(
263
        'property_name' => 'sid',
264
        'schema' => array(
265
          'type' => 'varchar',
266
          'length' => 20,
267
          'not null' => TRUE,
268
        ),
269
        'form' => array(
270
          'field_group' => 'basic',
271
          '#type' => 'radios',
272
          '#title' => t('LDAP Server used for query.'),
273
          '#required' => 1,
274
        ),
275
        'form_to_prop_functions' => array('trim'),
276
      ),
277

    
278
      'status' => array(
279
        'property_name' => 'status',
280
        'schema' => array(
281
          'type' => 'int',
282
          'size' => 'tiny',
283
          'not null' => TRUE,
284
          'default' => 0,
285
        ),
286
        'form' => array(
287
          'field_group' => 'basic',
288
          '#type' => 'checkbox',
289
          '#title' => t('Enabled'),
290
          '#description' => t('Disable in order to keep configuration without having it active.'),
291
        ),
292
        'form_to_prop_functions' => array('trim'),
293
      ),
294

    
295
      'base_dn_str' => array(
296
        'property_name' => 'base_dn_str',
297
        'schema' => array(
298
          'type' => 'text',
299
          'not null' => FALSE
300
        ),
301
        'form' => array(
302
          'field_group' => 'query',
303
          '#type' => 'textarea',
304
          '#title' => t('Base DNs to search in query.'),
305
          '#description' => t('Each Base DN will be queried and results merged. e.g. <code>ou=groups,dc=hogwarts,dc=edu</code>') . t('Enter one per line in case if you need more than one.'),
306
          '#cols' => 50,
307
          '#rows' => 6,
308
          '#required' => TRUE,
309
        ),
310
        'form_to_prop_functions' => array('trim'),
311
      ),
312

    
313
      'baseDn' => array(
314
        'property_name' => 'baseDn',
315
        'exportable' => FALSE,
316
      ),
317

    
318
      'filter' => array(
319
        'property_name' => 'filter',
320
        'schema' => array(
321
          'type' => 'text',
322
          'not null' => FALSE
323
        ),
324
        'form' => array(
325
          'field_group' => 'query',
326
          '#type' => 'textarea',
327
          '#title' => t('Filter'),
328
          '#description' => t('LDAP query filter such as <code>(objectClass=group)</code> or <code>(&(objectClass=user)(homePhone=*))
329
</code>'),
330
          '#cols' => 50,
331
          '#rows' => 1,
332
          '#required' => TRUE,
333
        ),
334
        'form_to_prop_functions' => array('trim'),
335
      ),
336

    
337
      'attributes_str' => array(
338
        'property_name' => 'attributes_str',
339
        'schema' => array(
340
          'type' => 'text',
341
          'not null' => FALSE
342
        ),
343
        'form' => array(
344
          'field_group' => 'query',
345
          '#type' => 'textarea',
346
          '#title' => t('Attributes to return.'),
347
          '#description' => t('Enter as comma separated list. DN is automatically returned. Leave empty to return all attributes. e.g. <code>objectclass,name,cn,samaccountname</code>'),
348
          '#cols' => 50,
349
          '#rows' => 6,
350
        ),
351
        'form_to_prop_functions' => array('trim'),
352
      ),
353

    
354
      'attributes' => array(
355
        'property_name' => 'attributes',
356
        'exportable' => FALSE,
357
      ),
358

    
359
      'sizelimit' => array(
360
        'property_name' => 'sizelimit',
361
        'schema' => array(
362
          'type' => 'int',
363
          'size' => 'small',
364
          'not null' => TRUE,
365
          'default' => 0,
366
        ),
367
        'form' => array(
368
          'field_group' => 'query_advanced',
369
          '#type' => 'textfield',
370
          '#title' => t('Size Limit of returned data'),
371
          '#description' => t('This limit may be already set by the ldap server.  0 signifies no limit'),
372
          '#size' => 7,
373
          '#maxlength' => 5,
374
          '#required' => TRUE,
375
        ),
376
        'form_to_prop_functions' => array('trim'),
377
      ),
378

    
379
      'timelimit' => array(
380
        'property_name' => 'timelimit',
381
        'schema' => array(
382
          'type' => 'int',
383
          'size' => 'small',
384
          'not null' => TRUE,
385
          'default' => 0,
386

    
387
        ),
388
        'form' => array(
389
          'field_group' => 'query_advanced',
390
          '#type' => 'textfield',
391
          '#title' => t('Time Limit in Seconds'),
392
          '#description' => t('The time limitset on this query.  This may be already set by the ldap server.  0 signifies no limit'),
393
          '#size' => 7,
394
          '#maxlength' => 5,
395
          '#required' => TRUE,
396
        ),
397
        'form_to_prop_functions' => array('trim'),
398
      ),
399

    
400
      'deref' => array(
401
        'property_name' => 'deref',
402
        'schema' => array(
403
          'type' => 'int',
404
          'size' => 'tiny',
405
          'not null' => TRUE,
406
          'default' => LDAP_DEREF_NEVER,
407
        ),
408
        'form' => array(
409
          'field_group' => 'query_advanced',
410
          '#type' => 'radios',
411
          '#title' => t('How aliases should be handled during the search.'),
412
          '#required' => 1,
413
          '#options' => array(
414
            LDAP_DEREF_NEVER => t('(default) aliases are never dereferenced.'),
415
            LDAP_DEREF_SEARCHING => t('aliases should be dereferenced during the search but not when locating the base object of the search.'),
416
            LDAP_DEREF_FINDING => t('aliases should be dereferenced when locating the base object but not during the search.'),
417
            LDAP_DEREF_ALWAYS => t('aliases should be dereferenced always.'),
418
          ),
419
        ),
420
        'form_to_prop_functions' => array('trim'),
421
      ),
422
     'scope' => array(
423
        'property_name' => 'scope',
424
        'schema' => array(
425
          'type' => 'int',
426
          'size' => 'tiny',
427
          'not null' => TRUE,
428
          'default' => LDAP_SCOPE_SUBTREE,
429
        ),
430
        'form' => array(
431
          'field_group' => 'query_advanced',
432
          '#type' => 'radios',
433
          '#title' => t('Scope of search.'),
434
          '#required' => 1,
435
          '#options' => array(
436
            LDAP_SCOPE_BASE => t('BASE. This value is used to indicate searching only the entry at the base DN, resulting in only that entry being returned (keeping in mind that it also has to meet the search filter criteria!).'),
437
            LDAP_SCOPE_ONELEVEL => t('ONELEVEL. This value is used to indicate searching all entries one level under the base DN - but not including the base DN and not including any entries under that one level under the base DN.'),
438
            LDAP_SCOPE_SUBTREE => t('SUBTREE. (default) This value is used to indicate searching of all entries at all levels under and including the specified base DN.'),
439
          ),
440
        ),
441
        'form_to_prop_functions' => array('trim'),
442
      ),
443

    
444
    );
445
    return $fields;
446
  }
447

    
448

    
449
}