My favorites | Sign in
Project Logo
                
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
<?php
/*
Plugin Name: joomlauth Plugin for Wordpress
Plugin URI: http://hery.blaogy.org/2008/02/22/joomla-user-auth-for-wordpress-mu/
Description: Share users with joomla database
Version: 1.1.0
Author: Heriniaina Eugene
Author URI: http://hery.serasera.org


NB: This version now works for Joomla 1.0.x and 1.5.x
This version only works for wp > 2.5
Check for joomlauth.php version < 1 for wp < 2.5

Todo: shared session
*/

// *** Begin Admin Config Functions *** //

$joomlauthVersion = "1.1.0";

load_plugin_textdomain('joomlauth','wp-content/mu-plugins/joomlauth/');

if (get_site_option("joomlauthActive")) {

add_action('login_head', 'joomlauth_login_head');
add_action('signup_header', 'joomlauth_signup_header');
add_filter('show_password_fields', 'joomlauth_signup_getFalse');

}

function joomlauth_signup_() {
return false;
}

function joomlauth_login_head()
{
global $errors;
$wp_error = $errors;

// If cookies are disabled we can't log in even with a valid user+pass
if ( isset($_POST['testcookie']) && empty($_COOKIE[TEST_COOKIE]) )
$wp_error->add('test_cookie', __("<strong>ERROR</strong>: Cookies are blocked or not supported by your browser. You must <a href='http://www.google.com/cookies.html'>enable cookies</a> to use WordPress.", 'joomlauth'));

// Some parts of this script use the main login form to display a message
//the first message is removed bcs already sent by wp
//if ( isset($_GET['loggedout']) && TRUE == $_GET['loggedout'] ) $wp_error->add('loggedout', __('You are now logged out.', 'joomlauth'), 'message');
elseif ( isset($_GET['registration']) && 'disabled' == $_GET['registration'] ) $wp_error->add('registerdiabled', __('User registration is currently not allowed.', 'joomlauth'));
elseif ( isset($_GET['checkemail']) && 'confirm' == $_GET['checkemail'] ) $wp_error->add('confirm', __('Check your e-mail for the confirmation link.', 'joomlauth'), 'message');
elseif ( isset($_GET['checkemail']) && 'newpass' == $_GET['checkemail'] ) $wp_error->add('newpass', __('Check your e-mail for your new password.', 'joomlauth'), 'message');
elseif ( isset($_GET['checkemail']) && 'registered' == $_GET['checkemail'] ) $wp_error->add('registered', __('Registration complete. Please check your e-mail.', 'joomlauth'), 'message');



if ( $wp_error->get_error_code() ) {
$errors = array();

foreach ( $wp_error->get_error_codes() as $code ) {
$severity = $wp_error->get_error_data($code);
foreach ( $wp_error->get_error_messages($code) as $error ) {
$errors[] = $error;
}
}
if ( !empty($errors) )
{
$login_error = join('<br />', $errors);
}
}


if($_GET['loggedout'] == "true" && $redirect = get_site_option("joomlauthLogoutURL")) {
echo "
<script language=\"javascript\"
type=\"text/javascript\">
<!--
window.location.replace(
\"$redirect\");
-->
</script>
";
exit;
}

//I've tested, there is no risk of looping
if (get_site_option("joomlauthLoginURL") && ereg('wp-admin', get_site_option("joomlauthLoginURL")) === false && ereg('wp-login', get_site_option("joomlauthLoginURL")) === false)
{
echo "
<script language=\"javascript\"
type=\"text/javascript\">
<!--
window.location.replace(
\"". get_site_option("joomlauthLoginURL") . "?login_error=". $login_error."\");
-->
</script>
";
exit;
}

}




function joomlauth_signup_header() {
if($redirect = get_site_option("joomlauthSignupURL")) {
echo "
<script language=\"javascript\" type=\"text/javascript\">
<!--
window.location.replace(
\"$redirect\");
-->
</script>";
exit;
}

}

add_action('admin_menu', 'joomlauth_addmenu');
function joomlauth_addmenu() {
$objCurrUser = wp_get_current_user();
$objUser = wp_cache_get($objCurrUser->id, 'users');
if (function_exists('add_options_page') && is_site_admin($objUser->user_login)) {
add_options_page('Authentication Options', 'Joomla Auth Options', 9, basename(__FILE__), 'joomlauthOptionsPanel');
}
}
require_once( ABSPATH . WPINC . '/registration.php');

function joomlauth_Auth($username, $password) {
global $wpdb;

$joomlauthDbHost = get_site_option("joomlauthDbHost");
$joomlauthDbUser = get_site_option("joomlauthDbUser");
$joomlauthDbName = get_site_option("joomlauthDbName");
$joomlauthDbPass = get_site_option("joomlauthDbPass");
$joomlauthDbPrefix = get_site_option("joomlauthDbPrefix");



$link = mysql_connect($joomlauthDbHost, $joomlauthDbUser, $joomlauthDbPass, true) or die('Not connected : ' . mysql_error());

// make foo the current db
$mydb = mysql_select_db($joomlauthDbName, $link) or die ('Can\'t use $joomlauthDbName : ' . mysql_error());

//get by username
$sql = "SELECT name, username, password, email FROM " . $joomlauthDbPrefix . "users WHERE LOWER(username)='" . mysql_real_escape_string(strtolower($username)) . "' LIMIT 1";

if($result = mysql_query($sql)) {
while ($row = mysql_fetch_array($result)) {
//var_dump($row);
//get joomla password to verify if it is joomla 1.5.x or 1.0.x
$_salt = "";
$_parts = "";

$_parts = (strlen($row["password"]) > 32) ? explode( ':', $row["password"] ) : array();
$_salt = $_parts[1];
$_crypt = md5( $password . $_salt );
//var_dump($password);
$_hashedPwd = $_crypt. ($_salt ? ':' . $_salt : '');
//var_dump($_hashedPwd);
if($_hashedPwd == $row["password"]) {

return $row;
} else {
Return false;
}

}
} else {
Return false;
}


}
function joomlauthOptionsPanel() {
global $joomlauthVersion;
//var_dump($_POST);
if($_POST['joomlauthOptionsSave']) {
update_site_option('joomlauthSignupURL', $_POST['joomlauthSignupURL']);
update_site_option('joomlauthLogoutURL', $_POST['joomlauthLogoutURL']);
update_site_option('joomlauthDbHost', $_POST['joomlauthDbHost']);
update_site_option('joomlauthDbUser', $_POST['joomlauthDbUser']);
update_site_option('joomlauthDbName', $_POST['joomlauthDbName']);
update_site_option('joomlauthDbPass', $_POST['joomlauthDbPass']);
update_site_option('joomlauthDbPrefix', $_POST['joomlauthDbPrefix']);
update_site_option('joomlauthActive', $_POST['joomlauthActive']);
update_site_option('joomlauthGetBlog', $_POST['joomlauthGetBlog']);
update_site_option('joomlauthLoginURL', $_POST['joomlauthLoginURL']);

echo "<div class='updated'><p>" . __("Saved Options!", 'joomlauth') . "</p></div>";
}

$joomlauthSignupURL = get_site_option("joomlauthSignupURL");
$joomlauthLogoutURL = get_site_option("joomlauthLogoutURL");
$joomlauthDbHost = get_site_option("joomlauthDbHost");
$joomlauthDbUser = get_site_option("joomlauthDbUser");
$joomlauthDbName = get_site_option("joomlauthDbName");
$joomlauthDbPass = get_site_option("joomlauthDbPass");
$joomlauthDbPrefix = get_site_option("joomlauthDbPrefix");
$joomlauthActive = get_site_option("joomlauthActive");
$joomlauthGetBlog = get_site_option("joomlauthGetBlog");
$joomlauthLoginURL = get_site_option("joomlauthLoginURL", get_option('siteurl') . "/wp-admin");

if($joomlauthActive == '1') {
$tChecked = "checked";
}
else {
$fChecked = "checked";
}

if($joomlauthGetBlog == '1') {
$joomlauthGetBlog1 = "checked";
} else {
$joomlauthGetBlog0 = "checked";
}

?>
<div class="wrap">
<h2>joomlauth Authentication Options</h2>
<small>Version: <?php echo $joomlauthVersion?> : <a href="http://hery.blaogy.org/2008/02/22/joomla-user-auth-for-wordpress-mu/"><?php _e('Click here for update' , 'joomlauth')?></a></small>
<form method="post" id="joomlauth_options">

<p class="submit"><input type="submit" value="Save" name="joomlauthOptionsSave"/></p>

<fieldset class="options">
<legend><?php _e('Authentication options', 'joomlauth')?></legend>

<table class="optiontable">
<tbody>
<tr valign="top">
<th scope="row"><?php _e('Database host:', 'joomlauth')?></th>
<td>
<input type='text' name='joomlauthDbHost' value='<?php echo $joomlauthDbHost ?>' style='width: 300px;' /><br />
<em><?php _e('This is usually localhost.', 'joomlauth')?></em>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Database username:', 'joomlauth')?></th>
<td>
<input type='text' name='joomlauthDbUser' value='<?php echo $joomlauthDbUser?>' style='width: 300px;' /><br />
<em><?php _e('Username to access the database.', 'joomlauth')?></em>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Database password:', 'joomlauth')?></th>
<td>
<input type='password' name='joomlauthDbPass' value='<?php echo $joomlauthDbPass?>' style='width: 300px;' /><br />
<em><?php _e('Your database password.', 'joomlauth')?></em>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Database name:', 'joomlauth')?></th>
<td>
<input type='text' name='joomlauthDbName' value='<?php echo $joomlauthDbName?>' style='width: 300px;' /><br />
<em><?php _e('Name of the database where users are stored.', 'joomlauth')?></em>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Database table prefix:', 'joomlauth')?></th>
<td>
<input type='text' name='joomlauthDbPrefix' value='<?php echo $joomlauthDbPrefix?>' style='width: 300px;' /><br />
<em><?php _e('Prefix of joomla tables example jos_ .', 'joomlauth')?></em>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Sign-up URL:', 'joomlauth')?></th>
<td>
<input type='text' name='joomlauthSignupURL' value='<?php echo $joomlauthSignupURL?>' style='width: 300px;' /><br />
<em><?php _e('Where people go to register to your blog', 'joomlauth')?> </em>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Login form URL:', 'joomlauth')?></th>
<td>
<input type='text' name='joomlauthLoginURL' value='<?php echo $joomlauthLoginURL?>' style='width: 300px;' /><br />
<em><?php _e('Where users is the Login Form', 'joomlauth')?></em>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Logout URL:', 'joomlauth')?></th>
<td>
<input type='text' name='joomlauthLogoutURL' value='<?php echo $joomlauthLogoutURL?>' style='width: 300px;' /><br />
<em><?php _e('Where users are redirected when logged out', 'joomlauth')?></em>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Should user get a blog?', 'joomlauth')?></th>
<td>
<input type='radio' name='joomlauthGetBlog' value='1' <?php echo $joomlauthGetBlog1 ?> /> <?php _e('Yes', 'joomlauth')?> <br />
<input type='radio' name='joomlauthGetBlog' value='0' <?php echo $joomlauthGetBlog0 ?>/> <?php _e('No account only', 'joomlauth')?>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Enable joomlauth plugin?', 'joomlauth')?></th>
<td>
<input type='radio' name='joomlauthActive' value='1' <?php echo $tChecked ?>/> <?php _e('Yes', 'joomlauth')?> <br />
<input type='radio' name='joomlauthActive' value='0' <?php echo $fChecked ?>/> <?php _e('No', 'joomlauth')?>
</td>
</tr>
</tbody>
</table>
</fieldset>
<p class="submit"><input type="submit" name="joomlauthOptionsSave" value="<?php _e('Save', 'joomlauth')?>" /></p>
</form>
</div>
<?php
}
// *** End Admin Config Functions *** //


// *** Begin User Auth Functions *** //

// This will disabled the change password dialogs.



if (get_site_option("joomlauthActive")) {

function wp_authenticate($username, $password) {
global $wpdb, $error, $current_site, $current_user, $base;

//Make sure we always use lowercase usernames.
$username = strtolower($username);

$joomlauthActive = get_site_option("joomlauthActive");
$joomlauthSignupURL = get_site_option("joomlauthSignupURL");
$joomlauthLogoutURL = get_site_option("joomlauthLogoutURL");
$joomlauthDbHost = get_site_option("joomlauthDbHost");
$joomlauthDbUser = get_site_option("joomlauthDbUser");
$joomlauthDbName = get_site_option("joomlauthDbName");
$joomlauthDbPass = get_site_option("joomlauthDbPass");
$joomlauthDbPrefix = get_site_option("joomlauthDbPrefix");
$joomlauthGetBlog = get_site_option("joomlauthGetBlog");

if(!$username) {
$error = __('<strong>Error</strong>: The username field is empty.', 'joomlauth');
return new WP_Error('wrong_password', $error);
}

if(!$password) {
$error = __('<strong>Error</strong>: The password field is empty.', 'joomlauth');
return new WP_Error('wrong_password', $error);
}
$login = get_userdatabylogin($username);
//Bassically if we are already logged in and we try to relogin.
if ($current_user->data->user_login == $username) {
return new WP_User($login->ID);
}



//everything is ok
//var_dump($login);
//var_dump(md5($password));
if ( ($already_md5 && $login->user_login == $username && md5($login->user_pass) == $password) || ($login->user_login == $username && $login->user_pass == md5($password)) ) {
return new WP_User($login->ID);
}
//only username is ok => check from external and update the password
if($login->user_login == $username) {
if($juser = joomlauth_Auth($username, $password)) {
if(strtolower($juser['username']) == $username) { //just doublechecking
$query = "UPDATE $wpdb->users SET user_pass='" . md5($password) . "' WHERE ID = '$login->ID'";
$query = apply_filters('update_user_query', $query);
$wpdb->query( $query );
return new WP_User($login->ID);
} else {
$error = __('<strong>Error</strong>: Wrong password.', 'joomlauth');
return new WP_Error('wrong_password', $error);
}
}
} else {
// a complete new user from the external database

if($juser = joomlauth_Auth($username, $password)) {
// call the registration function to create a wordpress user account for this
// successfully authenticated user
require_once( ABSPATH . WPINC . '/registration.php');

if ( !username_exists( $username ) ) { //no need of this???
//Create the user

define( "WP_INSTALLING", true );
$user_id = wpmu_create_user( $username, $password, $juser[email] );

if (!$user_id) {
$error = __('<strong>Error</strong>: Account Creation Failed.', 'joomlauth');
return new WP_Error('wrong_password', $error);
}

//Update their first and last name from ldap

update_usermeta( $user_id, 'last_name', ($juser[name] ? $juser[name] : $username ));

//This is for plugin events
do_action( 'wpmu_new_user', $user_id );
do_action('wpmu_activate_user', $user_id, $password);

$domain = strtolower( wp_specialchars( $username ) );
if( constant( "VHOST" ) == 'yes' ) {
$newdomain = $domain . "." . $current_site->domain;
$path = $base;
} else {
$newdomain = $current_site->domain;
$path = $base . $domain . '/';
}


if (get_site_option("joomlauthGetBlog")) {
//create blog

/*
Thanks to atmaniak poker-academie.com
- joomla accepts username with space so needs sanitize_username
*/

$meta = apply_filters('signup_create_blog_meta', array ( 'public' => 1));
$blog_id = wpmu_create_blog($newdomain, $path, sanitize_username($username) , $user_id, $meta);
do_action('wpmu_activate_blog', $blog_id, $user_id, $password, sanitize_username($username), $meta);
}

//Must recreated the login object for our shiny NEW users.
$login = get_userdatabylogin($username);

//Setup redirection to users home directory.
if (!strpos($_REQUEST['redirect_to'], $username)) {
$_REQUEST['redirect_to'] = $username . "/" . $_REQUEST['redirect_to'];
}
return new WP_User($login->ID);
} elseif ( !username_exists( $username ) && !$seraseraCreateAcct ) {
$error = __('<strong>Error</strong>: Local account does not exist.', 'joomlauth');
return new WP_Error('wrong_password', $error);
}

} else {
$error = __('<strong>Error</strong>: Wrong login and password.', 'joomlauth');
return new WP_Error('wrong_password', $error);
}


//At this point we must have a login object!
//I dont see how we couldn't but just in case.
if (!$login) {
$error = sprintf(__('<strong>Error</strong>: Unknown Near %s in %s' , 'joomlauth') , __LINE__ , __FILE__ );
return new WP_Error('wrong_password', $error);
}

//Added to atempt to recreate login correctly
//I think this is for account suspentions
$primary_blog = get_usermeta( $login->ID, "primary_blog" );
if( $primary_blog ) {
$details = get_blog_details( $primary_blog );
if( is_object( $details ) ) {
if( $details->archived == 1 || $details->spam == 1 || $details->deleted == 1 ) {
$error = __('<strong>Error</strong>: Blog suspended.', 'joomlauth');
return new WP_Error('wrong_password', $error);
}
}
}



return new WP_Error('wrong_password', $error);
} // End if ()


if (!$login) {
if( is_site_admin( $username ) ) {
//If Site Admin
//FYI This does exactly the same thing as $login = get_userdatabylogin( $username );
//which has already been done above?!?
unset( $login );
$userdetails = get_userdatabylogin( $username );
$login->user_login = $username;
$login->user_pass = $userdetails->user_pass;
} else {
//If Domain Admin
$admins = get_admin_users_for_domain();
reset( $admins );
while( list( $key, $val ) = each( $admins ) )
{
if( $val[ 'user_login' ] == $username ) {
unset( $login );
$login->user_login = $username;
$login->user_pass = $val[ 'user_pass' ];
}
}
}
}

if (!$login) {
//If we didnt find a user originally and we didnt get one from the above code...
$error = __('<strong>Error</strong>: Wrong username.', 'joomlauth');
return new WP_Error('wrong_password', $error);
} else {
//Setup some kind of default blog for the user...
$primary_blog = get_usermeta( $login->ID, "primary_blog" );
if( $primary_blog ) {
$details = get_blog_details( $primary_blog );
if( is_object( $details ) ) {
if( $details->archived == 1 || $details->spam == 1 || $details->deleted == 1 ) {
$error = __('<strong>Error</strong>: Blog suspended.', 'joomlauth');
return new WP_Error('wrong_password', $error);
}
}
}

//Setup redirection to users home directory.
if (!strpos($_REQUEST['redirect_to'], $username)) {
$_REQUEST['redirect_to'] = $username . "/" . $_REQUEST['redirect_to'];
}

// If the password is already_md5, it has been double hashed.
// Otherwise, it is plain text.
if ( ($already_md5 && $login->user_login == $username && md5($login->user_pass) == $password) || ($login->user_login == $username && $login->user_pass == md5($password)) ) {
return new WP_User($login->ID);
} else {
$error = __('<strong>Error</strong>: Incorrect password.', 'joomlauth');
$pwd = '';
return new WP_Error('wrong_password', $error);
}
}
}
}
?>
Show details Hide details

Change log

r92 by heriniaina.eugene on Jul 10, 2008   Diff
- sanitize_username for blog creation it
seems that joomla 1.0.2 accepts space in
username !!!(thankx to atmaniak)
Go to: 
Project members, sign in to write a code review

Older revisions

r91 by heriniaina.eugene on Jul 10, 2008   Diff
! options fixed
+ login form can be other than wp-
admin or wp-login.php
+ can be translated in other languages
r90 by heriniaina.eugene on Jul 10, 2008   Diff
! Fixed: Error when some letters in
Joomla username is capitalized
r85 by heriniaina.eugene on Jul 03, 2008   Diff
adding plugin URI
All revisions of this file

File info

Size: 19443 bytes, 523 lines
Hosted by Google Code