source: extensions/Ldap_Login/main.inc.php @ 19261

Last change on this file since 19261 was 19261, checked in by 22decembre, 11 years ago

premier envoi des fichiers

File size: 2.8 KB
Line 
1<?php
2/*
3Plugin Name: Ldap_Login
4Version: 0.1
5Description: Permet de se logger via une authentification ldap
6Plugin URI: http://www.22decembre.eu
7Author: 22decembre
8Author URI:http://www.22decembre.eu
9*/
10
11if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
12
13add_event_handler('try_login','ldap_login', 0, 4);
14
15function ldap_login($username, $password, $remember_me, $success)
16{
17  #pwg_session_gc();
18
19  global $conf;
20    $query = '
21SELECT '.$conf['user_fields']['id'].' AS id FROM '.USERS_TABLE.' WHERE '.$conf['user_fields']['username'].' = \''.pwg_db_real_escape_string($username).'\'
22;';
23
24  $row = pwg_db_fetch_assoc(pwg_query($query));
25
26    // Vérification de l'authentification
27    if (ldap_log($username,$password)) {
28
29        log_user($row['id'], $remember_me);
30        trigger_action('login_success', stripslashes($username));
31        return true;
32        }
33    else
34    {
35    trigger_action('login_failure', stripslashes($username));
36    return false;
37    }
38}
39
40function ldap_log($user,$pass)
41{
42$obj = new Ldap();
43$obj->load_config();
44
45// Eléments d'authentification LDAP
46$ldaprdn  = $obj->config['pref'].$user.$obj->config['basedn'];     // DN ou RDN LDAP
47//$ldappass = 'password';  // Mot de passe associé
48
49// Connexion au serveur LDAP
50$ldapconn = ldap_connect($obj->config['host'])
51    or die("Impossible de se connecter au serveur LDAP.");
52   
53ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
54
55if ($ldapconn) {
56
57    // Connexion au serveur LDAP
58    $ldapbind = ldap_bind($ldapconn, $ldaprdn, $pass);
59   
60    // Vérification de l'authentification
61    if ($ldapbind) {
62       // echo "Connexion LDAP réussie...";
63        return true;
64    } else {
65       // echo "Connexion LDAP échouée...";
66      return false;
67    }
68
69}
70}
71
72class Ldap
73{
74    var $config;
75    function load_config()
76    {
77        $x = @file_get_contents( dirname(__FILE__).'/data.dat' );
78        if ($x!==false)
79        {
80            $c = unserialize($x);
81            // do some more tests here
82            $this->config = $c;
83        }
84 
85        if ( !isset($this->config)
86            or empty($this->config['Test']) )
87        {
88            $this->config['host']       = 'localhost';
89            $this->config['basedn']     = ',ou=utilisateurs,dc=22decembre,dc=eu';
90            $this->config['pref']       = 'uid=';
91            $this->save_config();
92        }
93    }
94    function save_config()
95    {
96        $file = fopen( dirname(__FILE__).'/data.dat', 'w' );
97        fwrite($file, serialize($this->config) );
98        fclose( $file );
99    }
100
101    function ldap_admin_menu($menu)
102    {
103    array_push($menu,
104        array(
105        'NAME' => 'Ldap Login',
106        'URL' => get_admin_plugin_menu_link(dirname(__FILE__).'/admin/ldap_login_plugin_admin.php') )
107        );
108     
109    return $menu;
110    }
111}
112
113$ldap = new Ldap();
114$ldap->load_config();
115add_event_handler('get_admin_plugin_menu_links', array(&$ldap, 'ldap_admin_menu'));
116set_plugin_data($plugin['id'], $ldap);
117
118?>
Note: See TracBrowser for help on using the repository browser.