Projet

Général

Profil

Paste
Statistiques
| Branche: | Révision:

ryxeo-glpi-git / inc / ldap.function.php @ b67d8923

Historique | Voir | Annoter | Télécharger (28 ko)

1
<?php
2

    
3

    
4
/*
5
 * @version $Id: ldap.function.php 7875 2009-01-23 15:16:47Z moyo $
6
 -------------------------------------------------------------------------
7
 GLPI - Gestionnaire Libre de Parc Informatique
8
 Copyright (C) 2003-2009 by the INDEPNET Development Team.
9

10
 http://indepnet.net/   http://glpi-project.org
11
 -------------------------------------------------------------------------
12

13
 LICENSE
14

15
 This file is part of GLPI.
16

17
 GLPI is free software; you can redistribute it and/or modify
18
 it under the terms of the GNU General Public License as published by
19
 the Free Software Foundation; either version 2 of the License, or
20
 (at your option) any later version.
21

22
 GLPI is distributed in the hope that it will be useful,
23
 but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 GNU General Public License for more details.
26

27
 You should have received a copy of the GNU General Public License
28
 along with GLPI; if not, write to the Free Software
29
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
30
 --------------------------------------------------------------------------
31
 */
32

    
33
// ----------------------------------------------------------------------
34
// Original Author of file:
35
// Purpose of file:
36
// ----------------------------------------------------------------------
37

    
38
if (!defined('GLPI_ROOT')) {
39
        die("Sorry. You can't access directly to this file");
40
}
41

    
42
/** Computes the difference of arrays using keys for comparison
43
 * parameters are unlimited number of arrays
44
 * REPLACE array_diff_key for PHP 4 compatibility
45
 * 
46
 * @return  Returns an array containing all the entries from first array  that are not present in any of the other arrays.
47
 */
48
function diff_key() {
49
        $argCount  = func_num_args();
50
        $diff_arg_prefix = 'diffArg';
51
        $diff_arg_names = array();
52
        for ($i=0; $i < $argCount; $i++) {
53
                $diff_arg_names[$i] = 'diffArg'.$i;
54
                $$diff_arg_names[$i] = array_keys((array)func_get_arg($i));
55
        }
56
        $diffArrString = '';
57
        if (!empty($diff_arg_names)) {
58
                $diffArrString =  '$'.implode(', $', $diff_arg_names);
59
        }
60
        eval("\$result = array_diff(".$diffArrString.");");
61
        return $result;
62
}
63
/** Converts an array of parameters into a query string to be appended to a URL.
64
 *
65
 * @param   $group_dn  dn of the group to import
66
 * @param   $ldap_server ID of the LDAP server to use
67
 * @param   $entity entity where group must to be imported
68
 * @param         $type the type of import (groups, users, users & groups)
69
 * @return  nothing
70
 */
71
function ldapImportGroup ($group_dn,$ldap_server,$entity,$type){
72
        $config_ldap = new AuthLDAP();
73
        $res = $config_ldap->getFromDB($ldap_server);
74
        $ldap_users = array ();
75
        $group_dn = $group_dn;
76
        
77
        // we prevent some delay...
78
        if (!$res) {
79
                return false;
80
        }
81
        
82
        //Connect to the directory
83
        $ds = connect_ldap($config_ldap->fields['ldap_host'], $config_ldap->fields['ldap_port'], $config_ldap->fields['ldap_rootdn'], $config_ldap->fields['ldap_pass'], $config_ldap->fields['ldap_use_tls'],$config_ldap->fields['ldap_opt_deref']);
84
        if ($ds) {
85
                $group_infos = ldap_search_group_by_dn($ds, $config_ldap->fields['ldap_basedn'], stripslashes($group_dn),$config_ldap->fields["ldap_group_condition"]);
86
                $group = new Group();
87
                if ($type == "groups")
88
                        $group->add(array("name"=>addslashes($group_infos["cn"][0]),"ldap_group_dn"=>addslashes($group_infos["dn"]),"FK_entities"=>$entity));
89
                else
90
                        $group->add(array("name"=>addslashes($group_infos["cn"][0]),"ldap_field"=>$config_ldap->fields["ldap_field_group"], "ldap_value"=>addslashes($group_infos["dn"]),"FK_entities"=>$entity));
91
        }
92
}
93

    
94
/** Import a user from the active ldap server
95
 *
96
 * @param   $login  dn of the user to import
97
 * @param   $sync synchoronise (true) or import (false)
98
 * @return  nothing
99
 */
100
function ldapImportUser ($login,$sync){
101
        ldapImportUserByServerId($login, $sync,$_SESSION["ldap_server"]);
102
}
103

    
104
/** Import a user from a specific ldap server
105
 *
106
 * @param   $login  dn of the user to import
107
 * @param   $sync synchoronise (true) or import (false)
108
 * @param   $ldap_server ID of the LDAP server to use
109
 * @return  nothing
110
 */
111
function ldapImportUserByServerId($login, $sync,$ldap_server) {
112
        global $DB, $LANG;
113

    
114
        $config_ldap = new AuthLDAP();
115
        $res = $config_ldap->getFromDB($ldap_server);
116
        $ldap_users = array ();
117
        
118
        // we prevent some delay...
119
        if (!$res) {
120
                return false;
121
        }
122
        
123
        //Connect to the directory
124
        $ds = connect_ldap($config_ldap->fields['ldap_host'], $config_ldap->fields['ldap_port'], $config_ldap->fields['ldap_rootdn'], $config_ldap->fields['ldap_pass'], $config_ldap->fields['ldap_use_tls'],$config_ldap->fields['ldap_opt_deref']);
125
        if ($ds) {
126
                //Get the user's dn
127
                $user_dn = ldap_search_user_dn($ds, $config_ldap->fields['ldap_basedn'], $config_ldap->fields['ldap_login'], stripslashes($login), $config_ldap->fields['ldap_condition']);
128
                if ($user_dn) {
129
                        
130
                        $rule = new RightRuleCollection;
131
                        $groups = array();
132
                        
133
                        $user = new User();
134
                        //Get informations from LDAP
135
                        if ($user->getFromLDAP($ds, $config_ldap->fields, $user_dn, addslashes($login), "")){
136
                                //Add the auth method
137
                                $user->fields["auth_method"] = AUTH_LDAP;
138
                                $user->fields["id_auth"] = $ldap_server;
139
                                $user->fields["date_mod"]=$_SESSION["glpi_currenttime"];
140
                                
141
                                //$rule->processAllRules($groups,$user->fields,array("type"=>"LDAP","ldap_server"=>$ldap_server,"connection"=>$ds,"userdn"=>$user_dn));
142
                                if (!$sync) {
143
                                        //Save informations in database !
144
                                        $input = $user->fields;
145
                                        unset ($user->fields);
146
        
147
                                        $user->fields["ID"] = $user->add($input);
148
        //                                $user->applyRightRules($groups);
149
                                        return $user->fields["ID"];
150
                                } else
151
                                {
152
        //                                $user->applyRightRules($groups);
153
                                        $user->update($user->fields);
154
                                        return true;
155
                                }
156
                        } else {
157
                                return false;
158
                        }
159
                }
160
        } else {
161
                return false;
162
        }
163
}
164
/** Form to choose a ldap server
165
 *
166
 * @param   $target target page for the form
167
 * @return  nothing
168
 */
169
function ldapChooseDirectory($target) {
170
        global $DB, $LANG;
171

    
172
        $query = "SELECT * FROM glpi_auth_ldap ORDER BY name ASC";
173
        $result = $DB->query($query);
174

    
175
        if ($DB->numrows($result) == 1) {
176
                //If only one server, do not show the choose ldap server window
177
                $ldap = $DB->fetch_array($result);
178
                $_SESSION["ldap_server"]=$ldap["ID"];
179
                glpi_header($_SERVER['PHP_SELF']);
180
        }
181

    
182
        echo "<form action=\"$target\" method=\"post\">";
183
        echo "<div class='center'>";
184
        echo "<p >" . $LANG["ldap"][5] . "</p>";
185
        echo "<table class='tab_cadre'>";
186
        echo "<tr class='tab_bg_2'><th colspan='2'>" . $LANG["ldap"][4] . "</th></tr>";
187
        //If more than one ldap server
188
        if ($DB->numrows($result) > 1) {
189
                echo "<tr class='tab_bg_2'><td class='center'>" . $LANG["common"][16] . "</td><td class='center'>";
190
                echo "<select name='ldap_server'>";
191
                while ($ldap = $DB->fetch_array($result))
192
                        echo "<option value=" . $ldap["ID"] . ">" . $ldap["name"] . "</option>";
193

    
194
                echo "</select></td></tr>";
195
                echo "<tr class='tab_bg_2'><td align='center' colspan='2'><input class='submit' type='submit' name='ldap_showusers' value='" . $LANG["buttons"][2] . "'></td></tr>";
196

    
197
        } else
198
                //No ldap server
199
                echo "<tr class='tab_bg_2'><td align='center' colspan='2'>" . $LANG["ldap"][7] . "</td></tr>";
200

    
201
        echo "</table></div></form>";
202
}
203

    
204
function getGroupsFromLDAP($ldap_connection,$config_ldap,$filter,$search_in_groups=true,$groups=array())
205
{
206
                //First look for groups in group objects
207
                $extra_attribute = ($search_in_groups?"cn":$config_ldap->fields["ldap_field_group"]);
208
                $attrs = array ("dn",$extra_attribute);
209
                        
210
                        if ($filter == '')
211
                        {
212
                                if ($search_in_groups)
213
                                        $filter = (!empty($config_ldap->fields['ldap_group_condition'])?$config_ldap->fields['ldap_group_condition']:"(objectclass=*)");
214
                                else
215
                                        $filter = (!empty($config_ldap->fields['ldap_condition'])?$config_ldap->fields['ldap_condition']:"(objectclass=*)");
216
                        }
217
                        
218
                        $sr = @ldap_search($ldap_connection, $config_ldap->fields['ldap_basedn'],$filter , $attrs);
219

    
220
                        if ($sr){
221
                                $infos = ldap_get_entries($ldap_connection, $sr);
222
                
223
                                for ($ligne=0; $ligne < $infos["count"];$ligne++)
224
                                {        
225
                                        if ($search_in_groups)
226
                                        {
227
                                                $cn = $infos[$ligne]["cn"][0];
228
                                                $groups[$infos[$ligne]["dn"]]= (array("cn"=>$infos[$ligne]["cn"][0],"search_type" => "groups"));
229
                                        }
230
                                        else
231
                                        {
232
                                                if (isset($infos[$ligne][$extra_attribute]))
233
                                                        for ($ligne_extra=0; $ligne_extra < $infos[$ligne][$extra_attribute]["count"];$ligne_extra++)
234
                                                                $groups[$infos[$ligne][$extra_attribute][$ligne_extra]]= array("cn"=>getGroupCNByDn($ldap_connection,$infos[$ligne][$extra_attribute][$ligne_extra]),"search_type" => "users"); 
235
                                        }
236
                                }
237
                        }
238
                
239
                return $groups;        
240
                        
241
}
242

    
243
/**
244
 * Get the group's cn by giving his DN
245
 * @param dn the group's dn
246
 * @return the group cn
247
 */
248
function getGroupCNByDn($ldap_connection,$group_dn)
249
{
250
        $sr = @ ldap_read($ldap_connection, $group_dn, "objectClass=*", array("cn"));
251
        $v = ldap_get_entries($ldap_connection, $sr);
252
        if (!is_array($v) || count($v) == 0 || empty ($v[0]["cn"][0]))
253
                return false;
254
        else
255
                return $v[0]["cn"][0];
256
}
257

    
258
/** Get all LDAP groups from a ldap server which are not already in an entity
259
 *
260
 * @param   $id_auth ID of the server to use
261
 * @param   $myfilter ldap filter to use
262
 * @param   $entity entity to search
263
 * @return  array of the groups
264
 */
265
function getAllGroups($id_auth,$filter,$filter2,$entity){
266
        global $DB, $LANG,$CFG_GLPI;
267
        $config_ldap = new AuthLDAP();
268
        $res = $config_ldap->getFromDB($id_auth);
269
        $infos = array();
270
        $groups = array();
271
        
272
        $ds = connect_ldap($config_ldap->fields['ldap_host'], $config_ldap->fields['ldap_port'], $config_ldap->fields['ldap_rootdn'], $config_ldap->fields['ldap_pass'], $config_ldap->fields['ldap_use_tls'], $config_ldap->fields['ldap_opt_deref']);
273
        if ($ds) {
274
                
275
                switch ($config_ldap->fields["ldap_search_for_groups"])
276
                {
277
                        case 0:
278
                                $infos = getGroupsFromLDAP($ds,$config_ldap,$filter,false,$infos);
279
                                break;
280
                        case 1:
281
                                $infos = getGroupsFromLDAP($ds,$config_ldap,$filter,true,$infos);
282
                                break;
283
                        case 2:
284
                                $infos = getGroupsFromLDAP($ds,$config_ldap,$filter,true,$infos);
285
                                $infos = getGroupsFromLDAP($ds,$config_ldap,$filter2,false,$infos);
286
                        break;        
287
                }
288
                
289
                if (!empty($infos)){
290
                        $glpi_groups = array();
291
                        //Get all groups from GLPI DB for the current entity and the subentities
292
                        $sql = "SELECT name FROM glpi_groups ".getEntitiesRestrictRequest("WHERE","glpi_groups");
293

    
294
                        $res = $DB->query($sql);
295
                        //If the group exists in DB -> unset it from the LDAP groups
296
                        while ($group = $DB->fetch_array($res)){
297
                                $glpi_groups[$group["name"]] = 1;
298
                        }
299

    
300
                        $ligne=0;
301
                        
302
                        foreach ($infos as $dn => $info)
303
                        {
304
                                if (!isset($glpi_groups[$info["cn"]]))
305
                                {
306
                                        $groups[$ligne]["dn"]=$dn;
307
                                        $groups[$ligne]["cn"]=$info["cn"];
308
                                        $groups[$ligne]["search_type"]=$info["search_type"];
309
                                        $ligne++;
310
                                }
311
                        }
312
                }
313
        }
314
        return $groups;                
315
}
316

    
317
/** Show LDAP groups to add or synchronise in an entity
318
 *
319
 * @param   $target target page for the form
320
 * @param   $check check all ? -> need to be delete 
321
 * @param   $start where to start the list
322
 * @param   $sync synchronise or add ?
323
 * @param   $filter ldap filter to use
324
 * @param   $entity working entity
325
 * @return  nothing
326
 */
327
function showLdapGroups($target, $check, $start, $sync = 0,$filter='',$filter2='',$entity) {
328
        global $DB, $CFG_GLPI, $LANG;
329

    
330
        displayLdapFilter($target,false);
331
        echo "<br>";        
332
        $ldap_groups = getAllGroups($_SESSION["ldap_server"],$filter,$filter2,$entity);
333

    
334
        if (is_array($ldap_groups)){
335
                $numrows = count($ldap_groups);
336
        
337
                $action = "toimport";
338
                $form_action = "import_ok";
339
        
340
                if ($numrows > 0) {
341
                        $parameters = "check=$check";
342
                        printPager($start, $numrows, $target, $parameters);
343
        
344
                        // delete end 
345
                        array_splice($ldap_groups, $start + $_SESSION["glpilist_limit"]);
346
                        // delete begin
347
                        if ($start > 0)
348
                                array_splice($ldap_groups, 0, $start);
349
        
350
                        echo "<div class='center'>";
351
                        echo "<form method='post' id='ldap_form'  name='ldap_form' action='" . $target . "'>";
352
                        echo "<a href='" . $target . "?check=all' onclick= \"if ( markAllRows('ldap_form') ) return false;\">" . $LANG["buttons"][18] . "</a>&nbsp;/&nbsp;<a href='" . $target . "?check=none' onclick= \"if ( unMarkAllRows('ldap_form') ) return false;\">" . $LANG["buttons"][19] . "</a>";
353
                        echo "<table class='tab_cadre'>";
354
                        echo "<tr><th>" . $LANG["buttons"][37]. "</th><th colspan='2'>" . $LANG["common"][35] . "</th><th>".$LANG["setup"][261]."</th>"; 
355
                        echo"<th>".$LANG["ocsng"][36]."</th></tr>";
356
        
357
                        foreach ($ldap_groups as $groupinfos) {
358
                                $group = $groupinfos["cn"];
359
                                $group_dn = $groupinfos["dn"];
360
                                $search_type = $groupinfos["search_type"];
361
                                        
362
                                echo "<tr align='center' class='tab_bg_2'>";
363
                                //Need to use " instead of ' because it doesn't work with names with ' inside !
364
                                echo "<td><input type='checkbox' name=\"" . $action . "[" .$group_dn . "]\" " . ($check == "all" ? "checked" : "") ."></td>";
365
                                echo "<td colspan='2'>" . $group . "</td>";
366
                                echo "<td>" .$group_dn. "</td>";
367
                                echo "<td>";
368
                                dropdownValue("glpi_entities", "toimport_entities[" .$group_dn . "]=".$entity, $entity);
369
                                echo "</td>";
370
                                echo "<input type='hidden' name=\"toimport_type[".$group_dn."]\" value=\"".$search_type."\">";                
371
                                echo "</tr>";
372
                        }
373
                        echo "<tr class='tab_bg_1'><td colspan='5' align='center'>";
374
                        echo "<input class='submit' type='submit' name='" . $form_action . "' value='" . $LANG["buttons"][37] . "'>";
375
                        echo "</td></tr>";
376
                        echo "</table>";
377
                        echo "</form></div>";
378
                        printPager($start, $numrows, $target, $parameters);
379
                } else {
380
                        echo "<div class='center'><strong>" . $LANG["ldap"][25] . "</strong></div>";
381
                }
382
        } else {
383
                echo "<div class='center'><strong>" . $LANG["ldap"][25] . "</strong></div>";
384
        }
385
}
386

    
387

    
388
/** Get the list of LDAP users to add/synchronize
389
 *
390
 * @param   $id_auth ID of the server to use
391
 * @param   $sync user to synchronise or add ?
392
 * @param   $myfilter ldap filter to use
393
 * @return  array of the user
394
 */
395
function getAllLdapUsers($id_auth, $sync = 0,$myfilter='') {
396
        global $DB, $LANG,$CFG_GLPI;
397

    
398
        $config_ldap = new AuthLDAP();
399
        $res = $config_ldap->getFromDB($id_auth);
400
        $ldap_users = array ();
401

    
402
        // we prevent some delay...
403
        if (!$res) {
404
                return false;
405
        }
406

    
407
        $ds = connect_ldap($config_ldap->fields['ldap_host'], $config_ldap->fields['ldap_port'], $config_ldap->fields['ldap_rootdn'], $config_ldap->fields['ldap_pass'], $config_ldap->fields['ldap_use_tls'], $config_ldap->fields['ldap_opt_deref']);
408
        if ($ds) {
409

    
410
                //Search for ldap login AND modifyTimestamp, which indicates the last update of the object in directory
411
                        $attrs = array (
412
                        $config_ldap->fields['ldap_login'], "modifyTimestamp"
413
                );
414

    
415
                // Tenter une recherche pour essayer de retrouver le DN
416
                if ($myfilter == '')
417
                        $filter = "(".$config_ldap->fields['ldap_login']."=*)";
418
                else
419
                        $filter = $myfilter;
420
                                
421
                if (!empty ($config_ldap->fields['ldap_condition'])){
422
                        $filter = "(& $filter ".$config_ldap->fields['ldap_condition'].")";
423
                }
424
                $sr = @ldap_search($ds, $config_ldap->fields['ldap_basedn'],$filter , $attrs);
425

    
426
                if ($sr){
427
                        $info = ldap_get_entries($ds, $sr);
428
                        $user_infos = array();
429
                        
430
                        for ($ligne = 0; $ligne < $info["count"]; $ligne++)
431
                        {
432
                                //If ldap add
433
                                if (!$sync)
434
                                {
435
                                        $ldap_users[$info[$ligne][$config_ldap->fields['ldap_login']][0]] = $info[$ligne][$config_ldap->fields['ldap_login']][0];
436
                                        $user_infos[$info[$ligne][$config_ldap->fields['ldap_login']][0]]["timestamp"]=ldapStamp2UnixStamp($info[$ligne]['modifytimestamp'][0],$config_ldap->fields['timezone'],true);
437
                                }
438
                                else
439
                                {
440
                                //If ldap synchronisation
441
                                        $ldap_users[$info[$ligne][$config_ldap->fields['ldap_login']][0]] = ldapStamp2UnixStamp($info[$ligne]['modifytimestamp'][0],$config_ldap->fields['timezone'],true);
442
                                        $user_infos[$info[$ligne][$config_ldap->fields['ldap_login']][0]]["timestamp"]=ldapStamp2UnixStamp($info[$ligne]['modifytimestamp'][0],$config_ldap->fields['timezone'],true);
443
                                }
444
                        }        
445
                } else {
446
                        return false;
447
                }
448
        } else {
449
                return false;
450
        }
451
        
452
        $glpi_users = array ();
453
        $sql = "SELECT name, date_mod FROM glpi_users ";
454
        if ($sync){
455
                $sql.=" WHERE auth_method IN (-1,".AUTH_LDAP.") ";
456
        }
457
        $result = $DB->query($sql);
458
        if ($DB->numrows($result) > 0)
459
                while ($user = $DB->fetch_array($result))
460
                {
461
                        //Ldap add : fill the array with the login of the user 
462
                        if (!$sync)
463
                                $glpi_users[$user['name']] = $user['name'];
464
                        else
465
                        {
466
                        //Ldap synchronisation : look if the user exists in the directory and compares the modifications dates (ldap and glpi db)
467
                                if (!empty ($ldap_users[$user['name']]))
468
                                {
469
                                        if ($ldap_users[$user['name']] - strtotime($user['date_mod']) > 0)
470
                                        {
471
                                                $glpi_users[] = array("user" => $user['name'], "timestamp"=>$user_infos[$user['name']]['timestamp'],"date_mod"=>$user['date_mod']);
472
                                        }
473
                                }                
474
                }
475
                }
476
                
477
        //If add, do the difference between ldap users and glpi users
478
        if (!$sync)
479
        {
480
                $diff =         diff_key($ldap_users,$glpi_users);
481
                $list = array();
482
                
483
                foreach ($diff as $user)
484
                        $list[] = array("user" => $user, "timestamp" => $user_infos[$user]["timestamp"], "date_mod"=> "-----");
485
                
486
                return $list;        
487
        }
488
        else
489
                return $glpi_users;
490
        
491
}
492

    
493

    
494
/** Show LDAP users to add or synchronise
495
 *
496
 * @param   $target target page for the form
497
 * @param   $check check all ? -> need to be delete 
498
 * @param   $start where to start the list
499
 * @param   $sync synchronise or add ?
500
 * @param   $filter ldap filter to use
501
 * @return  nothing
502
 */
503
function showLdapUsers($target, $check, $start, $sync = 0,$filter='') {
504
        global $DB, $CFG_GLPI, $LANG;
505

    
506
        displayLdapFilter($target);
507
        echo "<br>";        
508
        $ldap_users = getAllLdapUsers($_SESSION["ldap_server"], $sync,$filter);
509

    
510
        if (is_array($ldap_users)){
511
                $numrows = count($ldap_users);
512
        
513
                if (!$sync) {
514
                        $action = "toimport";
515
                        $form_action = "import_ok";
516
                } else {
517
                        $action = "tosync";
518
                        $form_action = "sync_ok";
519
                }
520
        
521
                if ($numrows > 0) {
522
                        $parameters = "check=$check";
523
                        printPager($start, $numrows, $target, $parameters);
524
        
525
                        // delete end 
526
                        array_splice($ldap_users, $start + $_SESSION["glpilist_limit"]);
527
                        // delete begin
528
                        if ($start > 0)
529
                                array_splice($ldap_users, 0, $start);
530
        
531
                        echo "<div class='center'>";
532
                        echo "<form method='post' id='ldap_form' name='ldap_form' action='" . $target . "'>";
533
                        echo "<a href='" . $target . "?check=all' onclick= \"if ( markAllRows('ldap_form') ) return false;\">" . $LANG["buttons"][18] . "</a>&nbsp;/&nbsp;<a href='" . $target . "?check=none' onclick= \"if ( unMarkAllRows('ldap_form') ) return false;\">" . $LANG["buttons"][19] . "</a>";
534
                        echo "<table class='tab_cadre'>";
535
                        echo "<tr><th>" . (!$sync?$LANG["buttons"][37]:$LANG["ldap"][15]) . "</th><th colspan='2'>" . $LANG["Menu"][14] . "</th><th>".$LANG["common"][26]." ".$LANG["ldap"][13]."</th><th>".$LANG["common"][26]." ".$LANG["ldap"][14]."</th></tr>";
536
        
537
                        foreach ($ldap_users as $userinfos) {
538
                                $user = $userinfos["user"];
539
                                if (isset($userinfos["timestamp"]))
540
                                        $stamp = $userinfos["timestamp"];
541
                                else
542
                                        $stamp='';
543
                                
544
                                if (isset($userinfos["date_mod"]))        
545
                                        $date_mod = $userinfos["date_mod"];
546
                                else
547
                                        $date_mod='';
548
                                        
549
                                echo "<tr align='center' class='tab_bg_2'>";
550
                                //Need to use " instead of ' because it doesn't work with names with ' inside !
551
                                echo "<td><input type='checkbox' name=\"" . $action . "[" . $user . "]\" " . ($check == "all" ? "checked" : "") ."></td>";
552
                                echo "<td colspan='2'>" . $user . "</td>";
553
                                
554
                                if ($stamp != '')
555
                                        echo "<td>" .convDateTime(date("Y-m-d H:i:s",$stamp)). "</td>";
556
                                else
557
                                        echo "<td>&nbsp;</td>";
558
                                if ($date_mod != '')
559
                                        echo "<td>" . convDateTime($date_mod) . "</td>";
560
                                else 
561
                                        echo "<td>&nbsp;</td>";
562
                                        
563
                                echo "</tr>";
564
                        }
565
                        echo "<tr class='tab_bg_1'><td colspan='5' align='center'>";
566
                        echo "<input class='submit' type='submit' name='" . $form_action . "' value='" . (!$sync?$LANG["buttons"][37]:$LANG["ldap"][15]) . "'>";
567
                        echo "</td></tr>";
568
                        echo "</table>";
569
                        echo "</form></div>";
570
                        printPager($start, $numrows, $target, $parameters);
571
                } else {
572
                        echo "<div class='center'><strong>" . $LANG["ldap"][3] . "</strong></div>";
573
                }
574
        } else {
575
                echo "<div class='center'><strong>" . $LANG["ldap"][3] . "</strong></div>";
576
        }
577
}
578

    
579
/** Test a LDAP connection
580
 *
581
 * @param   $id_auth ID of the LDAP server
582
 * @param   $replicate_id use a replicate if > 0
583
 * @return  boolean connection succeeded ?
584
 */
585
function testLDAPConnection($id_auth,$replicate_id=-1) {
586
        $config_ldap = new AuthLDAP();
587
        $res = $config_ldap->getFromDB($id_auth);
588
        $ldap_users = array ();
589

    
590
        // we prevent some delay...
591
        if (!$res) {
592
                return false;
593
        }
594
        
595
        //Test connection to a replicate
596
        if ($replicate_id != -1)
597
        {
598
                $replicate = new AuthLdapReplicate;
599
                $replicate->getFromDB($replicate_id);
600
                $host = $replicate->fields["ldap_host"];
601
                $port = $replicate->fields["ldap_port"];
602
        }
603
        else
604
        {
605
                //Test connection to a master ldap server
606
                $host = $config_ldap->fields['ldap_host'];
607
                $port = $config_ldap->fields['ldap_port'];
608
        }
609
        $ds = connect_ldap($host, $port, $config_ldap->fields['ldap_rootdn'], $config_ldap->fields['ldap_pass'], $config_ldap->fields['ldap_use_tls'], $config_ldap->fields['ldap_opt_deref']);
610
        if ($ds)
611
                return true;
612
        else
613
                return false;
614
}
615

    
616
/** Display refresh button in the user page
617
 *
618
 * @param   $target target for the form
619
 * @param   $ID ID of the user
620
 * @return nothing
621
 */
622
function showSynchronizationForm($target, $ID) {
623
        global $LANG, $DB;
624

    
625
        if (haveRight("user", "w")){
626
                //Look it the user's auth method is LDAP
627
                $sql = "SELECT auth_method, id_auth FROM glpi_users WHERE ID='" . $ID."'";
628
                $result = $DB->query($sql);
629
                
630
                if ($DB->numrows($result) > 0) {
631
                        $data = $DB->fetch_array($result);
632
                        
633
                        switch($data["auth_method"])
634
                        {
635
                                case AUTH_LDAP :
636
                                        echo "<div class='center'>";
637
                                        echo "<form method='post' action=\"$target\">";
638

    
639
                                        $sql = "SELECT name FROM glpi_auth_ldap WHERE ID='" . $data["id_auth"]."'";
640
                                        $result = $DB->query($sql);
641
                                        if ($DB->numrows($result) > 0) {
642
                                                //Look it the auth server still exists !
643

    
644
                                                echo "<table class='tab_cadre'><tr class='tab_bg_2'><td>";
645
                                                echo "<input type='hidden' name='ID' value='" . $ID . "'>";
646
                                                echo "<input class=submit type='submit' name='force_ldap_resynch' value='" . $LANG["ocsng"][24] . "'>";
647
                                                echo "</td></tr></table>";
648
                                        }
649
        
650
                                        formChangeAuthMethodToDB($ID);
651
                                        echo "<br>";
652
                                        formChangeAuthMethodToMail($ID);
653
                                                        
654
                                        echo "</form></div>";
655
                                break;        
656
                                case AUTH_DB_GLPI :
657
                                        echo "<div class='center'>";
658
                                        echo "<form method='post' action=\"$target\">";
659
                                        formChangeAuthMethodToLDAP($ID);
660
                                        echo "<br>";
661
                                        formChangeAuthMethodToMail($ID);
662
                                        echo "</form></div>";
663
                                break;
664
                                case AUTH_MAIL :
665
                                        echo "<div class='center'>";
666
                                        echo "<form method='post' action=\"$target\">";
667
                                        formChangeAuthMethodToDB($ID);
668
                                        echo "<br>";
669
                                        formChangeAuthMethodToLDAP($ID);
670
                                        echo "</form></div>";
671
                                break;
672
                                case AUTH_EXTERNAL :
673
                                case AUTH_X509 :
674
                                        echo "<div class='center'>";
675
                                        echo "<form method='post' action=\"$target\">";
676
                                        formChangeAuthMethodToDB($ID);
677
                                        echo "<br>";
678
                                        formChangeAuthMethodToLDAP($ID);
679
                                        echo "<br>";
680
                                        formChangeAuthMethodToMail($ID);
681
                                        echo "</form></div>";
682
                                break;
683
                        } 
684
                }
685
        }
686
}
687

    
688
/** Form part to change auth method of a user
689
 *
690
 * @param   $ID ID of the user
691
 * @return nothing
692
 */
693
function formChangeAuthMethodToDB($ID){
694
        global $LANG;
695
        echo "<br><table class='tab_cadre'>";
696
        echo "<tr><th colspan='2' colspan='2'>" . $LANG["login"][30]."</th></tr>";
697
        echo "<input type='hidden' name='ID' value='" . $ID . "'>";
698
        echo "<tr class='tab_bg_2'><td colspan='2' align='center'><input class=submit type='submit' name='switch_auth_internal' value='" . $LANG["login"][32] . "'>";
699
        echo "</td></tr></table>";
700
}
701

    
702
/** Form part to change ldap auth method of a user
703
 *
704
 * @param   $ID ID of the user
705
 * @return nothing
706
 */
707
function formChangeAuthMethodToLDAP($ID)
708
{
709
        global $LANG,$DB;
710
        
711
        $sql = "SELECT ID FROM glpi_auth_ldap";
712
        $result = $DB->query($sql);
713
        if ($DB->numrows($result) > 0){
714
                echo "<table class='tab_cadre'>";
715
                echo "<tr><th colspan='2' colspan='2'>" . $LANG["login"][30]." : ".$LANG["login"][2]."</th></tr>";
716
                echo "<tr class='tab_bg_1'><td><input type='hidden' name='ID' value='" . $ID . "'>";
717
                echo $LANG["login"][31]."</td><td>";
718
                dropdownValue("glpi_auth_ldap","id_auth");
719
                echo "</td>";
720
                echo "<tr class='tab_bg_2'><td colspan='2' align='center'><input class=submit type='submit' name='switch_auth_ldap' value='" . $LANG["buttons"][2] . "'>";
721
                echo "</td></tr></table>";
722
        }
723
}
724

    
725
/** Form part to change mail auth method of a user
726
 *
727
 * @param   $ID ID of the user
728
 * @return nothing
729
 */
730
function formChangeAuthMethodToMail($ID){
731
        global $LANG,$DB;
732
        $sql = "SELECT ID FROM glpi_auth_mail";
733
        $result = $DB->query($sql);
734
        if ($DB->numrows($result) > 0){
735
                echo "<table class='tab_cadre'>";
736
                echo "<tr><th colspan='2' colspan='2'>" . $LANG["login"][30]." : ".$LANG["login"][3]."</th></tr>";
737
                echo "<tr class='tab_bg_1'><td><input type='hidden' name='ID' value='" . $ID . "'>";
738
                echo $LANG["login"][33]."</td><td>";
739
                dropdownValue("glpi_auth_mail","id_auth");
740
                echo "</td>";
741
                echo "<tr class='tab_bg_2'><td colspan='2' align='center'><input class=submit type='submit' name='switch_auth_mail' value='" . $LANG["buttons"][2] . "'>";
742
                echo "</td></tr></table>";
743
        }
744
}
745

    
746
//Get authentication method of a user, by looking in database
747
/* // NOT_USED
748
function getAuthMethodFromDB($ID) {
749
        global $DB;
750
        $sql = "SELECT auth_method FROM glpi_users WHERE ID=" . $ID;
751
        $result = $DB->query($sql);
752
        if ($DB->numrows($result) > 0) {
753
                $data = $DB->fetch_array($result);
754
                return $data["auth_method"];
755
        } else
756
                return NOT_YET_AUTHENTIFIED;
757
}
758
*/
759

    
760
/** Converts LDAP timestamps over to Unix timestamps
761
 *
762
 * @param   $ldapstamp LDAP timestamp
763
 * @param   $timezone timezone used
764
 * @param   $addtimezone use timezone ?
765
 * @return unix timestamp
766
 */
767
function ldapStamp2UnixStamp($ldapstamp,$timezone=0,$addtimezone=false) {
768
        global $CFG_GLPI;
769
        
770
        $year=substr($ldapstamp,0,4);
771
        $month=substr($ldapstamp,4,2);
772
        $day=substr($ldapstamp,6,2);
773
        $hour=substr($ldapstamp,8,2);
774
        $minute=substr($ldapstamp,10,2);
775
        $seconds=substr($ldapstamp,12,2);
776
        $stamp=gmmktime($hour,$minute,$seconds,$month,$day,$year);
777
        //Add timezone delay
778
        if ($addtimezone){
779
                        $stamp+= computeTimeZoneDelay($CFG_GLPI["glpi_timezone"],$timezone);
780
        }
781
        
782
        return $stamp;
783
}
784

    
785
/** Computer delay between 2 timezones
786
 *
787
 * @param   $first first timestamp
788
 * @param   $second second timestamp
789
 * @return timestamp delay
790
 */
791
function computeTimeZoneDelay($first,$second){
792
        return ($first - $second) * HOUR_TIMESTAMP; 
793
}
794

    
795
/** Display LDAP filter
796
 *
797
 * @param   $target target for the form
798
 * @param   $users boolean : for user ?
799
 * @return nothing
800
 */
801
function displayLdapFilter($target,$users=true){
802
        global $LANG;
803

    
804
        $config_ldap = new AuthLDAP();
805
        $res = $config_ldap->getFromDB($_SESSION["ldap_server"]);
806

    
807
        if ($users)
808
        {
809
                $filter_name1="ldap_condition";
810
                $filter_var = "ldap_filter";                
811
        }
812
        else
813
        {        
814
                        $filter_var = "ldap_group_filter";
815
                        switch ($config_ldap->fields["ldap_search_for_groups"])
816
                        {
817
                                case 0 :
818
                                        $filter_name1="ldap_condition";
819
                                        break;
820
                                case 1 : 
821
                                        $filter_name1="ldap_group_condition";
822
                                        break;
823
                                case 2:
824
                                        $filter_name1="ldap_group_condition";        
825
                                        $filter_name2="ldap_condition";
826
                                break;        
827
                        }
828
        }
829

    
830
        if (!isset($_SESSION[$filter_var]) || $_SESSION[$filter_var] == '')
831
                $_SESSION[$filter_var]=$config_ldap->fields[$filter_name1];
832
                
833
        echo "<div class='center'>";
834
        echo "<form method='post' action=\"$target\">";
835
        echo "<table class='tab_cadre'>"; 
836
        echo "<tr><th colspan='2'>" . ($users?$LANG["setup"][263]:$LANG["setup"][253]) . "</th></tr>";
837
        echo "<tr class='tab_bg_2'><td>";
838
        echo "<input type='text' name='ldap_filter' value='" . $_SESSION[$filter_var] . "' size='70'>";
839
        
840
        //Only display when looking for groups in users AND groups
841
        if (!$users && $config_ldap->fields["ldap_search_for_groups"] == 2)
842
        {
843
                if (!isset($_SESSION["ldap_group_filter2"]) || $_SESSION["ldap_group_filter2"] == '')
844
                        $_SESSION["ldap_group_filter2"]=$config_ldap->fields[$filter_name2];
845

    
846
                echo "</td></tr>";
847
                echo "<tr><th colspan='2'>" . $LANG["setup"][263] . "</th></tr>";                
848
                echo "<tr class='tab_bg_2'><td>";
849
                echo "<input type='text' name='ldap_filter2' value='" . $_SESSION["ldap_group_filter2"] . "' size='70'>";
850
                echo "</td></tr>";
851
        }        
852

    
853
        echo "<tr class='tab_bg_2'><td align='center'>";
854
        echo "<input class=submit type='submit' name='change_ldap_filter' value='" . $LANG["buttons"][2] . "'>";
855
        echo "</td></tr></table>";
856
        echo "</form></div>";        
857
}
858
?>
Redmine Appliance - Powered by TurnKey Linux