| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | Plugin Name: External DB authentication |
|---|
| 4 | Plugin URI: http://www.ploofle.com/tag/ext_db_auth/ |
|---|
| 5 | Description: Used to externally authenticate WP users with an existing user DB. |
|---|
| 6 | Version: 3.15 |
|---|
| 7 | Author: Charlene Barina |
|---|
| 8 | Author URI: http://www.ploofle.com |
|---|
| 9 | |
|---|
| 10 | Copyright 2007 Charlene Barina (email : cbarina@u.washington.edu) |
|---|
| 11 | |
|---|
| 12 | This program is free software; you can redistribute it and/or modify |
|---|
| 13 | it under the terms of the GNU General Public License as published by |
|---|
| 14 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 15 | (at your option) any later version. |
|---|
| 16 | |
|---|
| 17 | This program is distributed in the hope that it will be useful, |
|---|
| 18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 20 | GNU General Public License for more details. |
|---|
| 21 | |
|---|
| 22 | You should have received a copy of the GNU General Public License |
|---|
| 23 | along with this program; if not, write to the Free Software |
|---|
| 24 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 25 | */ |
|---|
| 26 | |
|---|
| 27 | //backwords compatability with php < 5 for htmlspecialchars_decode |
|---|
| 28 | if ( !function_exists('htmlspecialchars_decode') ) |
|---|
| 29 | { |
|---|
| 30 | function htmlspecialchars_decode($text) |
|---|
| 31 | { |
|---|
| 32 | return strtr($text, array_flip(get_html_translation_table(HTML_SPECIALCHARS))); |
|---|
| 33 | } |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | function ext_db_auth_activate() { |
|---|
| 37 | add_option('ext_db_type',"MySQL","External database type"); |
|---|
| 38 | add_option('ext_db_mdb2_path',"","Path to MDB2 (if non-standard)"); |
|---|
| 39 | add_option('ext_host',"","External database hostname"); |
|---|
| 40 | add_option('ext_db_port',"","Database port (if non-standard)"); |
|---|
| 41 | add_option('ext_db',"","External database name"); |
|---|
| 42 | add_option('ext_db_user',"","External database username"); |
|---|
| 43 | add_option('ext_db_pw',"","External database password"); |
|---|
| 44 | add_option('ext_db_table',"","External database table for authentication"); |
|---|
| 45 | add_option('ext_db_namefield',"","External database field for username"); |
|---|
| 46 | add_option('ext_db_pwfield',"","External database field for password"); |
|---|
| 47 | add_option('ext_db_first_name',""); |
|---|
| 48 | add_option('ext_db_last_name',""); |
|---|
| 49 | add_option('ext_db_user_url',""); |
|---|
| 50 | add_option('ext_db_user_email',""); |
|---|
| 51 | add_option('ext_db_description',""); |
|---|
| 52 | add_option('ext_db_aim',""); |
|---|
| 53 | add_option('ext_db_yim',""); |
|---|
| 54 | add_option('ext_db_jabber',""); |
|---|
| 55 | add_option('ext_db_enc',"","Type of encoding for external db (default SHA1? or MD5?)"); |
|---|
| 56 | add_option('ext_db_error_msg',"","Custom login message"); |
|---|
| 57 | add_option('ext_db_other_enc','$password2 = $password;'); |
|---|
| 58 | add_option('ext_db_role_bool',''); |
|---|
| 59 | add_option('ext_db_role',''); |
|---|
| 60 | add_option('ext_db_role_value',''); |
|---|
| 61 | add_option('ext_db_authenticator_cookie',''); // CMC |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | function ext_db_auth_init(){ |
|---|
| 65 | register_setting('ext_db_auth','ext_db_type'); |
|---|
| 66 | register_setting('ext_db_auth','ext_db_mdb2_path'); |
|---|
| 67 | register_setting('ext_db_auth','ext_host'); |
|---|
| 68 | register_setting('ext_db_auth','ext_db_port'); |
|---|
| 69 | register_setting('ext_db_auth','ext_db'); |
|---|
| 70 | register_setting('ext_db_auth','ext_db_user'); |
|---|
| 71 | register_setting('ext_db_auth','ext_db_pw'); |
|---|
| 72 | register_setting('ext_db_auth','ext_db_table'); |
|---|
| 73 | register_setting('ext_db_auth','ext_db_namefield'); |
|---|
| 74 | register_setting('ext_db_auth','ext_db_pwfield'); |
|---|
| 75 | register_setting('ext_db_auth','ext_db_first_name'); |
|---|
| 76 | register_setting('ext_db_auth','ext_db_last_name'); |
|---|
| 77 | register_setting('ext_db_auth','ext_db_user_url'); |
|---|
| 78 | register_setting('ext_db_auth','ext_db_user_email'); |
|---|
| 79 | register_setting('ext_db_auth','ext_db_description'); |
|---|
| 80 | register_setting('ext_db_auth','ext_db_aim'); |
|---|
| 81 | register_setting('ext_db_auth','ext_db_yim'); |
|---|
| 82 | register_setting('ext_db_auth','ext_db_jabber'); |
|---|
| 83 | register_setting('ext_db_auth','ext_db_enc'); |
|---|
| 84 | register_setting('ext_db_auth','ext_db_error_msg'); |
|---|
| 85 | register_setting('ext_db_auth','ext_db_other_enc'); |
|---|
| 86 | register_setting('ext_db_auth','ext_db_role'); |
|---|
| 87 | register_setting('ext_db_auth','ext_db_role_bool'); |
|---|
| 88 | register_setting('ext_db_auth','ext_db_role_value'); |
|---|
| 89 | register_setting('ext_db_auth','ext_db_authenticator_cookie'); // CMC |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | //page for config menu |
|---|
| 93 | function ext_db_auth_add_menu() { |
|---|
| 94 | add_options_page("External DB settings", "External DB settings", 10, __FILE__,"ext_db_auth_display_options"); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | //actual configuration screen |
|---|
| 98 | function ext_db_auth_display_options() { |
|---|
| 99 | $db_types[] = "MySQL"; |
|---|
| 100 | $db_types[] = "MSSQL"; |
|---|
| 101 | $db_types[] = "PgSQL"; |
|---|
| 102 | ?> |
|---|
| 103 | <div class="wrap"> |
|---|
| 104 | <h2>External Database Authentication Settings</h2> |
|---|
| 105 | <form method="post" action="options.php"> |
|---|
| 106 | <?php settings_fields('ext_db_auth'); ?> |
|---|
| 107 | <h3>External Database Settings</h3> |
|---|
| 108 | <strong>Make sure your WP admin account exists in the external db prior to saving these settings.</strong> |
|---|
| 109 | <table class="form-table"> |
|---|
| 110 | <tr valign="top"> |
|---|
| 111 | <th scope="row">Database type</th> |
|---|
| 112 | <td><select name="ext_db_type" > |
|---|
| 113 | <?php |
|---|
| 114 | foreach ($db_types as $key=>$value) { //print out radio buttons |
|---|
| 115 | if ($value == get_option('ext_db_type')) |
|---|
| 116 | echo '<option value="'.$value.'" selected="selected">'.$value.'<br/>'; |
|---|
| 117 | else echo '<option value="'.$value.'">'.$value.'<br/>';; |
|---|
| 118 | } |
|---|
| 119 | ?> |
|---|
| 120 | </select> |
|---|
| 121 | </td> |
|---|
| 122 | <td> |
|---|
| 123 | <span class="description"><strong style="color:red;">required</strong>; If not MySQL, requires <a href="http://pear.php.net/package/MDB2/" target="_blank">PEAR MDB2 package</a> and relevant database driver package installation.</span> |
|---|
| 124 | </td> |
|---|
| 125 | </tr> |
|---|
| 126 | <tr valign="top"> |
|---|
| 127 | <th scope="row"><label>Path to MDB2.php</label></th> |
|---|
| 128 | <td><input type="text" name="ext_db_mdb2_path" value="<?php echo get_option('ext_db_mdb2_path'); ?>" /> </td> |
|---|
| 129 | <td><span class="description">Only when using non-MySQL database and in case this isn't in some sort of include path in your PHP configuration. No trailing slash! e.g., /home/username/php </span></td> |
|---|
| 130 | </tr> |
|---|
| 131 | <tr valign="top"> |
|---|
| 132 | <th scope="row"><label>Host</label></th> |
|---|
| 133 | <td><input type="text" name="ext_host" value="<?php echo get_option('ext_host'); ?>" /> </td> |
|---|
| 134 | <td><span class="description"><strong style="color:red;">required</strong>; (often localhost)</span> </td> |
|---|
| 135 | </tr> |
|---|
| 136 | <tr valign="top"> |
|---|
| 137 | <th scope="row"><label>Port</label></th> |
|---|
| 138 | <td><input type="text" name="ext_db_port" value="<?php echo get_option('ext_db_port'); ?>" /> </td> |
|---|
| 139 | <td><span class="description">Only set this if you have a non-standard port for connecting.</span></td> |
|---|
| 140 | </tr> |
|---|
| 141 | <tr valign="top"> |
|---|
| 142 | <th scope="row"><label>Name</label></th> |
|---|
| 143 | <td><input type="text" name="ext_db" value="<?php echo get_option('ext_db'); ?>" /></td> |
|---|
| 144 | <td><span class="description"><strong style="color:red;">required</strong></span></td> |
|---|
| 145 | </tr> |
|---|
| 146 | <tr valign="top"> |
|---|
| 147 | <th scope="row"><label>Username</label></th> |
|---|
| 148 | <td><input type="text" name="ext_db_user" value="<?php echo get_option('ext_db_user'); ?>" /></td> |
|---|
| 149 | <td><span class="description"><strong style="color:red;">required</strong>; (recommend select privileges only)</span></td> |
|---|
| 150 | </tr> |
|---|
| 151 | <tr valign="top"> |
|---|
| 152 | <th scope="row"><label>Password</label></th> |
|---|
| 153 | <td><input type="password" name="ext_db_pw" value="<?php echo get_option('ext_db_pw'); ?>" /></td> |
|---|
| 154 | <td><span class="description"><strong style="color:red;">required</strong></span></td> |
|---|
| 155 | </tr> |
|---|
| 156 | <tr valign="top"> |
|---|
| 157 | <th scope="row"><label>User table</label></th> |
|---|
| 158 | <td><input type="text" name="ext_db_table" value="<?php echo get_option('ext_db_table'); ?>" /></td> |
|---|
| 159 | <td><span class="description"><strong style="color:red;">required</strong></span></td> |
|---|
| 160 | </tr> |
|---|
| 161 | </table> |
|---|
| 162 | |
|---|
| 163 | <h3>External Database Source Fields</h3> |
|---|
| 164 | <table class="form-table"> |
|---|
| 165 | <tr valign="top"> |
|---|
| 166 | <th scope="row"><label>Username</label></th> |
|---|
| 167 | <td><input type="text" name="ext_db_namefield" value="<?php echo get_option('ext_db_namefield'); ?>" /></td> |
|---|
| 168 | <td><span class="description"><strong style="color:red;">required</strong></span></td> |
|---|
| 169 | </tr> |
|---|
| 170 | <tr valign="top"> |
|---|
| 171 | <th scope="row"><label>Password</label></th> |
|---|
| 172 | <td><input type="text" name="ext_db_pwfield" value="<?php echo get_option('ext_db_pwfield'); ?>" /></td> |
|---|
| 173 | <td><span class="description"><strong style="color:red;">required</strong></span><td> |
|---|
| 174 | </tr> |
|---|
| 175 | <tr valign="top"> |
|---|
| 176 | <th scope="row">Password encryption method</th> |
|---|
| 177 | <td><select name="ext_db_enc"> |
|---|
| 178 | <?php |
|---|
| 179 | switch(get_option('ext_db_enc')) { |
|---|
| 180 | case "SHA1" : |
|---|
| 181 | echo '<option selected="selected">SHA1</option><option>MD5</option><option>Other</option>'; |
|---|
| 182 | break; |
|---|
| 183 | case "MD5" : |
|---|
| 184 | echo '<option>SHA1</option><option selected="selected">MD5</option><option>Other</option>'; |
|---|
| 185 | break; |
|---|
| 186 | case "Other" : |
|---|
| 187 | echo '<option>SHA1</option><option selected="selected">MD5</option><option selected="selected">Other</option>'; |
|---|
| 188 | break; |
|---|
| 189 | default : |
|---|
| 190 | echo '<option selected="selected">SHA1</option><option>MD5</option><option>Other</option>'; |
|---|
| 191 | break; |
|---|
| 192 | } |
|---|
| 193 | ?> |
|---|
| 194 | </select></td> |
|---|
| 195 | <td><span class="description"><strong style="color:red;">required</strong>; (using "Other" requires you to enter PHP code below!)</td> |
|---|
| 196 | </tr> |
|---|
| 197 | <tr valign="top"> |
|---|
| 198 | <th scope="row"><label>Hash code</label></th> |
|---|
| 199 | <td><input type="text" name="ext_db_other_enc" size="50" value="<?php echo get_option('ext_db_other_enc'); ?>" /></td> |
|---|
| 200 | <td><span class="description">Only will run if "Other" is selected and needs to be PHP code. Variable you need to set is $password2, and you have access to (original) $username and $password.</td> |
|---|
| 201 | </tr> |
|---|
| 202 | <tr valign="top"> |
|---|
| 203 | <th scope="row"><label>Role check</label></th> |
|---|
| 204 | <td><input type="text" name="ext_db_role" value="<?php echo get_option('ext_db_role'); ?>" /> |
|---|
| 205 | <br /> |
|---|
| 206 | <select name="ext_db_role_bool"> |
|---|
| 207 | <?php |
|---|
| 208 | switch(get_option('ext_db_role_bool')) { |
|---|
| 209 | case "is" : |
|---|
| 210 | echo '<option selected="selected">is</option><option>greater than</option><option>less than</option>'; |
|---|
| 211 | break; |
|---|
| 212 | case "greater than" : |
|---|
| 213 | echo '<option>is</option><option selected="selected">greater than</option><option>less than</option>'; |
|---|
| 214 | break; |
|---|
| 215 | case "less than" : |
|---|
| 216 | echo '<option>is</option><option>greater than</option><option selected="selected">less than</option>'; |
|---|
| 217 | break; |
|---|
| 218 | default : |
|---|
| 219 | echo '<option selected="selected">is</option><option>greater than</option><option>less than</option>'; |
|---|
| 220 | break; |
|---|
| 221 | } |
|---|
| 222 | ?> |
|---|
| 223 | </select><br /> |
|---|
| 224 | <input type="text" name="ext_db_role_value" value="<?php echo get_option('ext_db_role_value'); ?>" /></td> |
|---|
| 225 | <td><span class="description">Use this if you have certain user role ids in your external database to further restrict allowed logins. If unused, leave fields blank.</span></td> |
|---|
| 226 | </tr> |
|---|
| 227 | <tr valign="top"> |
|---|
| 228 | <th scope="row"><label>First name</label></th> |
|---|
| 229 | <td><input type="text" name="ext_db_first_name" value="<?php echo get_option('ext_db_first_name'); ?>" /></td> |
|---|
| 230 | </tr> |
|---|
| 231 | <tr valign="top"> |
|---|
| 232 | <th scope="row"><label>Last name</label></th> |
|---|
| 233 | <td><input type="text" name="ext_db_last_name" value="<?php echo get_option('ext_db_last_name'); ?>" /></td> |
|---|
| 234 | </tr> |
|---|
| 235 | <tr valign="top"> |
|---|
| 236 | <th scope="row"><label>Homepage</label></th> |
|---|
| 237 | <td><input type="text" name="ext_db_user_url" value="<?php echo get_option('ext_db_user_url'); ?>" /></td> |
|---|
| 238 | </tr> |
|---|
| 239 | <tr valign="top"> |
|---|
| 240 | <th scope="row"><label>Email</label></th> |
|---|
| 241 | <td><input type="text" name="ext_db_user_email" value="<?php echo get_option('ext_db_user_email'); ?>" /></td> |
|---|
| 242 | </tr> |
|---|
| 243 | <!-- CMC here - next tr section --> |
|---|
| 244 | <tr valign="top"> |
|---|
| 245 | <th scope="row"><label>Authenticator Cookie Field</label></th> |
|---|
| 246 | <td><input type="text" name="ext_db_authenticator_cookie" value="<?php echo get_option('ext_db_authenticator_cookie'); ?>" /></td> |
|---|
| 247 | <td><span class="description">Use this if other parts of your domain are 'outside' of WordPress |
|---|
| 248 | but you want to save an authentication cookie to be used if successfully logged in via WordPress (and this ext_db plugin). |
|---|
| 249 | You will have a cookie named 'auth' that can be checked on the non-WordPress bits to facilitate with auto-login there etc. |
|---|
| 250 | If unused, leave this blank.</span></td> |
|---|
| 251 | </tr> |
|---|
| 252 | <!-- CMC here -- end of mods --> |
|---|
| 253 | <tr valign="top"> |
|---|
| 254 | <th scope="row"><label>Bio/description</label></th> |
|---|
| 255 | <td><input type="text" name="ext_db_description" value="<?php echo get_option('ext_db_description'); ?>" /></td> |
|---|
| 256 | </tr> |
|---|
| 257 | <tr valign="top"> |
|---|
| 258 | <th scope="row"><label>AIM screen name</label></th> |
|---|
| 259 | <td><input type="text" name="ext_db_aim" value="<?php echo get_option('ext_db_aim'); ?>" /></td> |
|---|
| 260 | </tr> |
|---|
| 261 | <tr valign="top"> |
|---|
| 262 | <th scope="row"><label>YIM screen name</label></th> |
|---|
| 263 | <td><input type="text" name="ext_db_yim" value="<?php echo get_option('ext_db_yim'); ?>" /></td> |
|---|
| 264 | </tr> |
|---|
| 265 | <tr valign="top"> |
|---|
| 266 | <th scope="row"><label>JABBER screen name</label></th> |
|---|
| 267 | <td><input type="text" name="ext_db_jabber" value="<?php echo get_option('ext_db_jabber'); ?>" /></td> |
|---|
| 268 | </tr> |
|---|
| 269 | </table> |
|---|
| 270 | <h3>Other</h3> |
|---|
| 271 | <table class="form-table"> |
|---|
| 272 | <tr valign="top"> |
|---|
| 273 | <th scope="row">Custom login message</th> |
|---|
| 274 | <td><textarea name="ext_db_error_msg" cols=40 rows=4><?php echo htmlspecialchars(get_option('ext_db_error_msg'));?></textarea></td> |
|---|
| 275 | <td><span class="description">Shows up in login box, e.g., to tell them where to get an account. You can use HTML in this text.</td> |
|---|
| 276 | </tr> |
|---|
| 277 | </table> |
|---|
| 278 | |
|---|
| 279 | <p class="submit"> |
|---|
| 280 | <input type="submit" name="Submit" value="Save changes" /> |
|---|
| 281 | </p> |
|---|
| 282 | </form> |
|---|
| 283 | </div> |
|---|
| 284 | <?php |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | //sort-of wrapper for all DB interactions |
|---|
| 288 | function db_functions($driver,$process,$resource,$query) { |
|---|
| 289 | if ($driver == "MySQL") { //use built-in PHP mysql connection |
|---|
| 290 | switch($process) { |
|---|
| 291 | case "connect" : |
|---|
| 292 | $port = get_option('ext_db_port'); |
|---|
| 293 | if (!empty($port)) $port = ":".get_option('ext_db_port'); |
|---|
| 294 | $resource = mysql_connect(get_option('ext_host').$port, get_option('ext_db_user'), get_option('ext_db_pw'),true) or die(mysql_error()); |
|---|
| 295 | mysql_select_db(get_option('ext_db'),$resource) or die(mysql_error()); |
|---|
| 296 | return $resource; |
|---|
| 297 | break; |
|---|
| 298 | case "query": |
|---|
| 299 | $result = mysql_query($query,$resource) or die(mysql_error()); |
|---|
| 300 | return $result; |
|---|
| 301 | break; |
|---|
| 302 | case "numrows": |
|---|
| 303 | return mysql_num_rows($resource); |
|---|
| 304 | break; |
|---|
| 305 | case "fetch": |
|---|
| 306 | return mysql_fetch_assoc($resource); |
|---|
| 307 | break; |
|---|
| 308 | case "close": |
|---|
| 309 | mysql_close($resource); |
|---|
| 310 | break; |
|---|
| 311 | } |
|---|
| 312 | } |
|---|
| 313 | else { //Use MDB2 |
|---|
| 314 | $mdbpath = get_option('ext_db_mdb2_path')."/MDB2.php"; |
|---|
| 315 | require_once($mdbpath); |
|---|
| 316 | switch($process) { |
|---|
| 317 | case "connect" : |
|---|
| 318 | $port = get_option('ext_db_port'); |
|---|
| 319 | if (!empty($port)) $port = ":".get_option('ext_db_port'); |
|---|
| 320 | $url = strtolower($driver)."://".get_option('ext_db_user').":".get_option('ext_db_pw')."@".get_option('ext_host').$port."/".get_option('ext_db'); |
|---|
| 321 | $resource =& MDB2::connect($url); |
|---|
| 322 | if(PEAR::isError($resource)) die("Error while connecting : " . $resource->getMessage()); |
|---|
| 323 | return $resource; |
|---|
| 324 | break; |
|---|
| 325 | case "query": |
|---|
| 326 | $result = $resource->query($query); |
|---|
| 327 | if(PEAR::isError($result)) die('Failed to issue query, error message : ' . $result->getMessage()); |
|---|
| 328 | return $result; |
|---|
| 329 | break; |
|---|
| 330 | case "numrows": |
|---|
| 331 | return $resource->numRows(); |
|---|
| 332 | break; |
|---|
| 333 | case "fetch": |
|---|
| 334 | return $resource->fetchRow(MDB2_FETCHMODE_ASSOC); |
|---|
| 335 | break; |
|---|
| 336 | case "close": |
|---|
| 337 | $resource->disconnect(); |
|---|
| 338 | break; |
|---|
| 339 | } |
|---|
| 340 | } |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | //actual meat of plugin - essentially, you're setting $username and $password to pass on to the system. |
|---|
| 344 | //You check from your external system and insert/update users into the WP system just before WP actually |
|---|
| 345 | //authenticates with its own database. |
|---|
| 346 | function ext_db_auth_check_login($username,$password) { |
|---|
| 347 | require_once('./wp-includes/registration.php'); |
|---|
| 348 | |
|---|
| 349 | //first figure out the DB type and connect... |
|---|
| 350 | $driver = get_option('ext_db_type'); |
|---|
| 351 | //if on same host have to use resource id to make sure you don't lose the wp db connection |
|---|
| 352 | |
|---|
| 353 | $mdbpath = get_option('ext_db_mdb2_path')."/MDB2.php"; |
|---|
| 354 | if ($mdbpath != "/MDB2.php") @require_once($mdbpath); |
|---|
| 355 | |
|---|
| 356 | $resource = db_functions($driver,"connect","",""); |
|---|
| 357 | //prepare the db for unicode queries |
|---|
| 358 | //to pick up umlauts, non-latin text, etc., without choking |
|---|
| 359 | $utfquery = "SET NAMES 'utf8'"; |
|---|
| 360 | $resultutf = db_functions($driver,"query",$resource,$utfquery); |
|---|
| 361 | |
|---|
| 362 | //do the password hash for comparing |
|---|
| 363 | switch(get_option('ext_db_enc')) { |
|---|
| 364 | case "SHA1" : |
|---|
| 365 | $password2 = sha1($password); |
|---|
| 366 | break; |
|---|
| 367 | case "MD5" : |
|---|
| 368 | $password2 = md5($password); |
|---|
| 369 | break; |
|---|
| 370 | case "Other" : //right now defaulting to plaintext. People can change code here for their own special hash |
|---|
| 371 | eval(get_option('ext_db_other_enc')); |
|---|
| 372 | break; |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | |
|---|
| 376 | //first check to see if login exists in external db |
|---|
| 377 | $query = "SELECT count(*) AS numrows FROM " . get_option('ext_db_table') . " WHERE ".get_option('ext_db_namefield')." = '$username'"; |
|---|
| 378 | $result = db_functions($driver,"query",$resource,$query); |
|---|
| 379 | $numrows = db_functions($driver,"fetch",$result,""); |
|---|
| 380 | $numrows = $numrows["numrows"]; |
|---|
| 381 | |
|---|
| 382 | if ($numrows) { |
|---|
| 383 | //then check to see if pw matches and get other fields... |
|---|
| 384 | $sqlfields['first_name'] = get_option('ext_db_first_name'); |
|---|
| 385 | $sqlfields['last_name'] = get_option('ext_db_last_name'); |
|---|
| 386 | $sqlfields['user_url'] = get_option('ext_db_user_url'); |
|---|
| 387 | $sqlfields['user_email'] = get_option('ext_db_user_email'); |
|---|
| 388 | $sqlfields['description'] = get_option('ext_db_description'); |
|---|
| 389 | $sqlfields['aim'] = get_option('ext_db_aim'); |
|---|
| 390 | $sqlfields['yim'] = get_option('ext_db_yim'); |
|---|
| 391 | $sqlfields['jabber'] = get_option('ext_db_jabber'); |
|---|
| 392 | $sqlfields['ext_db_role'] = get_option('ext_db_role'); |
|---|
| 393 | $sqlfields['authenticator'] = get_option('ext_db_authenticator_cookie'); // CMC |
|---|
| 394 | |
|---|
| 395 | foreach($sqlfields as $key=>$value) { |
|---|
| 396 | if ($value == "") unset($sqlfields[$key]); |
|---|
| 397 | } |
|---|
| 398 | $sqlfields2 = implode(", ",$sqlfields); |
|---|
| 399 | |
|---|
| 400 | //just so queries won't error out if there are no relevant fields for extended data. |
|---|
| 401 | if (empty($sqlfields2)) $sqlfields2 = get_option('ext_db_namefield'); |
|---|
| 402 | $query = "SELECT $sqlfields2 FROM " . get_option('ext_db_table') . " WHERE ".get_option('ext_db_namefield')." = '$username' AND ".get_option('ext_db_pwfield')." = '$password2'"; |
|---|
| 403 | $result = db_functions($driver,"query",$resource,$query); |
|---|
| 404 | $numrows = db_functions($driver,"numrows",$result,""); |
|---|
| 405 | |
|---|
| 406 | if ($numrows) { //create/update wp account from external database if login/pw exact match exists in that db |
|---|
| 407 | $extfields = db_functions($driver,"fetch",$result,""); |
|---|
| 408 | $process = TRUE; |
|---|
| 409 | |
|---|
| 410 | //check role, if present. |
|---|
| 411 | $role = get_option('ext_db_role'); |
|---|
| 412 | if (!empty($role)) { //build the role checker too |
|---|
| 413 | $rolevalue = $extfields[$sqlfields['ext_db_role']]; |
|---|
| 414 | $rolethresh = get_option('ext_db_role_value'); |
|---|
| 415 | $rolebool = get_option('ext_db_role_bool'); |
|---|
| 416 | global $ext_error; |
|---|
| 417 | if ($rolebool == 'is') { |
|---|
| 418 | if ($rolevalue == $rolethresh) {} |
|---|
| 419 | else { |
|---|
| 420 | $username = NULL; |
|---|
| 421 | $ext_error = "wrongrole"; |
|---|
| 422 | $process = FALSE; |
|---|
| 423 | } |
|---|
| 424 | } |
|---|
| 425 | if ($rolebool == 'greater than') { |
|---|
| 426 | if ($rolevalue > $rolethresh) {} |
|---|
| 427 | else { |
|---|
| 428 | $username = NULL; |
|---|
| 429 | $ext_error = "wrongrole"; |
|---|
| 430 | $process = FALSE; |
|---|
| 431 | } |
|---|
| 432 | } |
|---|
| 433 | if ($rolebool == 'less than') { |
|---|
| 434 | if ($rolevalue < $rolethresh) {} |
|---|
| 435 | else { |
|---|
| 436 | $username = NULL; |
|---|
| 437 | $ext_error = "wrongrole"; |
|---|
| 438 | $process = FALSE; |
|---|
| 439 | } |
|---|
| 440 | } |
|---|
| 441 | } |
|---|
| 442 | //only continue with user update/creation if login/pw is valid AND, if used, proper role perms |
|---|
| 443 | if ($process) { |
|---|
| 444 | $userarray['user_login'] = $username; |
|---|
| 445 | $userarray['user_pass'] = $password; |
|---|
| 446 | $userarray['first_name'] = $extfields[$sqlfields['first_name']]; |
|---|
| 447 | $userarray['last_name'] = $extfields[$sqlfields['last_name']]; |
|---|
| 448 | $userarray['user_url'] = $extfields[$sqlfields['user_url']]; |
|---|
| 449 | $userarray['user_email'] = $extfields[$sqlfields['user_email']]; |
|---|
| 450 | $userarray['description'] = $extfields[$sqlfields['description']]; |
|---|
| 451 | $userarray['aim'] = $extfields[$sqlfields['aim']]; |
|---|
| 452 | $userarray['yim'] = $extfields[$sqlfields['yim']]; |
|---|
| 453 | $userarray['jabber'] = $extfields[$sqlfields['jabber']]; |
|---|
| 454 | $userarray['display_name'] = $extfields[$sqlfields['first_name']]." ".$extfields[$sqlfields['last_name']]; |
|---|
| 455 | |
|---|
| 456 | //also if no extended data fields |
|---|
| 457 | if ($userarray['display_name'] == " ") $userarray['display_name'] = $username; |
|---|
| 458 | |
|---|
| 459 | db_functions($driver,"close",$resource,""); |
|---|
| 460 | |
|---|
| 461 | // CMC hack - set auth cookies |
|---|
| 462 | $auth = $extfields[$sqlfields['authenticator']]; |
|---|
| 463 | if (! empty($auth)) { // we want to set an authenticator cookie for other parts of the website |
|---|
| 464 | setcookie("auth", $auth, time()+3600*24*365, "/"); |
|---|
| 465 | } |
|---|
| 466 | // end CMC hack |
|---|
| 467 | |
|---|
| 468 | //looks like wp functions clean up data before entry, so I'm not going to try to clean out fields beforehand. |
|---|
| 469 | if ($id = username_exists($username)) { //just do an update |
|---|
| 470 | $userarray['ID'] = $id; |
|---|
| 471 | wp_update_user($userarray); |
|---|
| 472 | } |
|---|
| 473 | else wp_insert_user($userarray); //otherwise create |
|---|
| 474 | |
|---|
| 475 | } |
|---|
| 476 | } |
|---|
| 477 | else { //username exists but wrong password... |
|---|
| 478 | global $ext_error; |
|---|
| 479 | $ext_error = "wrongpw"; |
|---|
| 480 | $username = NULL; |
|---|
| 481 | } |
|---|
| 482 | } |
|---|
| 483 | else { //don't let login even if it's in the WP db - it needs to come only from the external db. |
|---|
| 484 | global $ext_error; |
|---|
| 485 | $ext_error = "notindb"; |
|---|
| 486 | $username = NULL; |
|---|
| 487 | } |
|---|
| 488 | } |
|---|
| 489 | |
|---|
| 490 | |
|---|
| 491 | //gives warning for login - where to get "source" login |
|---|
| 492 | function ext_db_auth_warning() { |
|---|
| 493 | echo "<p class=\"message\">".get_option('ext_db_error_msg')."</p>"; |
|---|
| 494 | } |
|---|
| 495 | |
|---|
| 496 | function ext_db_errors() { |
|---|
| 497 | global $error; |
|---|
| 498 | global $ext_error; |
|---|
| 499 | if ($ext_error == "notindb") |
|---|
| 500 | return "<strong>ERROR:</strong> Username not found."; |
|---|
| 501 | else if ($ext_error == "wrongrole") |
|---|
| 502 | return "<strong>ERROR:</strong> You don't have permissions to log in."; |
|---|
| 503 | else if ($ext_error == "wrongpw") |
|---|
| 504 | return "<strong>ERROR:</strong> Invalid password."; |
|---|
| 505 | else |
|---|
| 506 | return $error; |
|---|
| 507 | } |
|---|
| 508 | |
|---|
| 509 | //hopefully grays stuff out. |
|---|
| 510 | function ext_db_warning() { |
|---|
| 511 | echo '<strong style="color:red;">Any changes made below WILL NOT be preserved when you login again. You have to change your personal information per instructions found in the <a href="../wp-login.php">login box</a>.</strong>'; |
|---|
| 512 | } |
|---|
| 513 | |
|---|
| 514 | //disables the (useless) password reset option in WP when this plugin is enabled. |
|---|
| 515 | function ext_db_show_password_fields() { |
|---|
| 516 | return 0; |
|---|
| 517 | } |
|---|
| 518 | |
|---|
| 519 | |
|---|
| 520 | /* |
|---|
| 521 | * Disable functions. Idea taken from http auth plugin. |
|---|
| 522 | */ |
|---|
| 523 | function disable_function_register() { |
|---|
| 524 | $errors = new WP_Error(); |
|---|
| 525 | $errors->add('registerdisabled', __('User registration is not available from this site, so you can\'t create an account or retrieve your password from here. See the message above.')); |
|---|
| 526 | ?></form><br /><div id="login_error">User registration is not available from this site, so you can't create an account or retrieve your password from here. See the message above.</div> |
|---|
| 527 | <p id="backtoblog"><a href="<?php bloginfo('url'); ?>/" title="<?php _e('Are you lost?') ?>"><?php printf(__('← Back to %s'), get_bloginfo('title', 'display' )); ?></a></p> |
|---|
| 528 | <?php |
|---|
| 529 | exit(); |
|---|
| 530 | } |
|---|
| 531 | |
|---|
| 532 | function disable_function() { |
|---|
| 533 | $errors = new WP_Error(); |
|---|
| 534 | $errors->add('registerdisabled', __('User registration is not available from this site, so you can\'t create an account or retrieve your password from here. See the message above.')); |
|---|
| 535 | login_header(__('Log In'), '', $errors); |
|---|
| 536 | ?> |
|---|
| 537 | <p id="backtoblog"><a href="<?php bloginfo('url'); ?>/" title="<?php _e('Are you lost?') ?>"><?php printf(__('← Back to %s'), get_bloginfo('title', 'display' )); ?></a></p> |
|---|
| 538 | <?php |
|---|
| 539 | exit(); |
|---|
| 540 | } |
|---|
| 541 | |
|---|
| 542 | |
|---|
| 543 | add_action('admin_init', 'ext_db_auth_init' ); |
|---|
| 544 | add_action('admin_menu', 'ext_db_auth_add_menu'); |
|---|
| 545 | add_action('wp_authenticate', 'ext_db_auth_check_login', 1, 2 ); |
|---|
| 546 | add_action('lost_password', 'disable_function'); |
|---|
| 547 | add_action('user_register', 'disable_function'); |
|---|
| 548 | add_action('register_form', 'disable_function_register'); |
|---|
| 549 | add_action('retrieve_password', 'disable_function'); |
|---|
| 550 | add_action('password_reset', 'disable_function'); |
|---|
| 551 | add_action('profile_personal_options','ext_db_warning'); |
|---|
| 552 | add_filter('login_errors','ext_db_errors'); |
|---|
| 553 | add_filter('show_password_fields','ext_db_show_password_fields'); |
|---|
| 554 | add_filter('login_message','ext_db_auth_warning'); |
|---|
| 555 | |
|---|
| 556 | register_activation_hook( __FILE__, 'ext_db_auth_activate' ); |
|---|
| 557 | |
|---|