summaryrefslogtreecommitdiffstats
path: root/ldap_import.php
blob: fb1d1dcf90e1618fa386293a5b7b1c0ddca00261 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/*
Plugin Name: LDAP User Info Import Plugin for WPMU
Plugin URI: http://www.redhat.com/
Description: A simplistic plugin to pull in user information from LDAP after a blank account has been created post HTTP Auth
Version: 0.1
Author: Bret McMillan (bretm@redhat.com)

Copyright (C) 2008 Red Hat, Inc.

This is free software, licensed to you under the GNU General Public
License, version 2 (GPLv2). A copy of GPLv2 is available at 
http://www.gnu.org/licenses/old-licenses/gpl-2.0.html .

This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

if (! class_exists('LDAPImportPlugin')) {

  class LDAPImportPlugin {

    function LDAPImportPlugin() {
      add_action('init', array(&$this, 'init'));
      add_action('wpmu_new_user', array(&$this, 'ldap_newuser_info_import'));
      add_action('admin_menu', array(&$this, 'admin_menu'));
    }

    function init() {
      if ( !get_site_option('ldap_server') && current_user_can('manage_options')) {
	add_site_option('ldap_server', 'ldap1.foo.com', 'The hostname of the LDAP server to read from');
	add_site_option('ldap_dn', 'dc=foo,dc=com', 'The LDAP base DN to use');
      }
    }

    function admin_menu() {
      $cur_user = wp_get_current_user();
      $cached_user = wp_cache_get($cur_user->ID, 'users');
      if (function_exists('add_submenu_page') &&
	  is_site_admin($cached_user->user_login)) {
	add_submenu_page('wpmu-admin.php', 'LDAP Import', 'LDAP Import', 9, basename(__FILE__), array(&$this, 'render_options'));
      }
    }

    function render_options() {
      if( ! is_site_admin() ) {
	wp_die( __('<p>You do not have permission to access this page.</p>') );
      }

      if ($_POST['ldapOptionsSave']) {
	update_site_option('ldap_server', $_POST['ldap_server']);
	update_site_option('ldap_dn', $_POST['ldap_dn']);
        ?>
	  <div id="message" class="updated fade"><p><?php _e('Options saved!'); ?></p></div><?php
      }
      $ldap_server = get_site_option('ldap_server');
      $ldap_dn = get_site_option('ldap_dn');
?>
<div class="wrap">
  <h2>LDAP User Information Import Options</h2>
  <form method="post">
    <input type="hidden" name="action" value="update" />
    <input type="hidden" name="page_options" value="ldap_server,ldap_dn" />
    <?php if (function_exists('wp_nonce_field')): wp_nonce_field('update-options'); endif; ?>

    <fieldset class="options">
      <table class="editform optiontable">
        <tr valign="top">
          <th scope="row"><label for="ldap_server">LDAP Server</label></th>
          <td>
            <input type="text" name="ldap_server" id="ldap_server" value="<?php echo htmlspecialchars($ldap_server) ?>" size="50" />
          </td>
        </tr>
        <tr valign="top">
          <th scope="row"><label for="ldap_dn">LDAP Base DN</label></th>
          <td>
            <input type="text" name="ldap_dn" id="ldap_dn" value="<?php echo htmlspecialchars($ldap_dn) ?>" size="20" />
          </td>
        </tr>
      </table>
    </fieldset>
    <p class="submit">
      <input type="submit" name="ldapOptionsSave" value="Update Options &raquo;" />
    </p>
  </form>
</div>
<?php
    }

    function ldap_newuser_info_import( $user_id ) {
      global $wpdb;

      $user = new WP_User($user_id);

      $principal = preg_split('/\@/', $user->user_login, -1, PREG_SPLIT_NO_EMPTY);
      $username = $principal[0];

      $ldap_server = get_site_option("ldap_server");
      $ldap_dn = get_site_option("ldap_dn");

      if ( ! $ldap_server || ! $ldap_dn )
        die("LDAP options not set");
  
      $ds = ldap_connect($ldap_server);
      $filter ="(uid=$username)";
      $justthese = array("sn", "givenname", "mail");

      $sr = ldap_search($ds, $ldap_dn, $filter, $justthese);

      $info = ldap_get_entries($ds, $sr);


      if ($info["count"] <= 0) {
        // bad things happened...
        die("User authenticated, but not found in LDAP!\n");
      }

      update_usermeta($user->ID, 'first_name', $wpdb->escape($info[0]['givenname'][0]));
      update_usermeta($user->ID, 'last_name', $wpdb->escape($info[0]['sn'][0]));
      wp_update_user(array('ID' => $user_id, 'user_email' => $info[0]['mail'][0], 'display_name' => $username));

      return true;
    }
  }

  $ldap_userinfo_import_plugin = new LDAPImportPlugin();
 }
?>