Changeset 176248
- Timestamp:
- 11/23/2009 06:58:14 PM (16 years ago)
- Location:
- wp-smf-bridge/trunk
- Files:
-
- 2 edited
-
bridge.php (modified) (15 diffs)
-
readme.txt (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
wp-smf-bridge/trunk/bridge.php
r104532 r176248 2 2 /* 3 3 Plugin Name: SMF Bridge 4 Plugin URI: http:// projects.jonnyfunfun.com/projects/show/wp-smf-bridge4 Plugin URI: http://code.google.com/p/wp-smf-bridge 5 5 Description: User registration and login bridge between Wordpress and Simple Machine Forum 6 6 Author: Jonathan "JonnyFunFun" Enzinna 7 Version: 0. 17 Version: 0.2 8 8 Author URI: http://www.jonnyfunfun.com/ 9 9 */ … … 20 20 * Returns the current software version 21 21 */ 22 function version() { return 0. 1; }22 function version() { return 0.2; } 23 23 24 24 /* load … … 33 33 self::$reg_override = (get_option('smf_bridge_regoverride')) ? get_option('smf_bridge_regoverride') : 0; 34 34 if (file_exists(self::$smf_dir."Settings.php")) 35 require _once(self::$smf_dir."Settings.php");35 require(self::$smf_dir."Settings.php"); 36 36 else { 37 37 delete_option('smf_bridge_setup_complete'); 38 38 return false; 39 39 } 40 require_once(dirname(__FILE__)."/smf_1-1_api.php"); 40 if (!function_exists('smf_cookie_url')) 41 require(dirname(__FILE__)."/smf_1-1_api.php"); 41 42 self::$bridge_active = true; 42 43 self::$smf_dbopts['user'] = $db_user; … … 62 63 if ($uname != $username) 63 64 return false; 64 if (s mf_GetMemberIdByName($uname))65 if (self::getSMFId($uname)) 65 66 return false; 66 if (s mf_GetMemberIdByName(strtolower($uname)))67 if (self::getSMFId(strtolower($uname))) 67 68 return false; 68 69 return true; … … 122 123 <tr> 123 124 <th width="33%" scope="row" valign="top">Forum Path:<br /> 124 <font style="font-size: 8px;"><em>URI relative to Wordpress root with trailing slash<br />125 <font style="font-size: 8px;"><em>URI relative to Wordpress root<br /> 125 126 i.e - "forum/" places your forums in <?=ABSPATH?>forum/</em></font> 126 127 </th> … … 159 160 function login() { 160 161 if (!self::$bridge_active) return; 161 if (!is_user_logged_in()) return; 162 $user = wp_get_current_user(); 163 smf_bridge_crowbar(); 164 smf_bridge_syncprofile($user->ID,true); 162 if (!$_POST['wp-submit'] || $_POST['wp-submit'] != 'Log In') return; 163 //if (!is_user_logged_in()) return; 164 $user = get_userdatabylogin($_POST['log']); 165 if ($user) { 166 self::crowbar(); 167 self::syncprofile($user->ID,true); 168 } else { 169 // update wordpress password if we exist 170 if (!$smf_cxn) 171 $smf_cxn = mysql_connect(self::$smf_dbopts['host'],self::$smf_dbopts['user'],self::$smf_dbopts['pass']) or trigger_error(mysql_error(),E_USER_ERROR); 172 $user_exists = $wpdb->get_row("SELECT id FROM $wpdb->users WHERE UPPER(user_login) = UPPER('". $_POST['log'] . "')"); 173 if ($user_exists) { 174 $SQL = "SELECT passwd, passwordSalt FROM ".self::$smf_dbopts['prefix']."members WHERE UPPER(memberName) = UPPER('".$_POST['log']."')"; 175 if (!$rs = mysql_query($SQL,$smf_cxn)) trigger_error(mysql_error(),E_USER_ERROR); 176 if (mysql_num_rows($rs) > 0) { 177 list($password,$salt) = mysql_fetch_array($rs, MYSQL_NUM); 178 if (sha1($_POST['pwd'].$sald) == $password) { 179 $newpass = wp_hash_password($_POST['pwd']); 180 $SQL = "UPDATE ".$wpdb->prefix."users SET user_pass='$newpass' WHERE UPPER(user_login)=UPPER('".$_POST['log']."') LIMIT 1"; 181 } 182 } 183 } else { 184 self::syncusers(); 185 $SQL = "SELECT passwd, passwordSalt FROM ".self::$smf_dbopts['prefix']."members WHERE UPPER(memberName) = UPPER('".$_POST['log']."')"; 186 if (!$rs = mysql_query($SQL,$smf_cxn)) trigger_error(mysql_error(),E_USER_ERROR); 187 if (mysql_num_rows($rs) > 0) { 188 list($password,$salt) = mysql_fetch_array($rs, MYSQL_NUM); 189 if (sha1($_POST['pwd'].$sald) == $password) { 190 wp_update_user(array( 191 "user_login" => $_POST['log'], 192 "user_pass" => $_POST['pwd'] 193 )); 194 $tgt_user = get_userdatabylogin($_POST['log']); 195 if ($tgt_user) wp_set_current_user($tgt_user->ID,$tgt_user->user_login); 196 } 197 } 198 } 199 } 165 200 } 166 201 … … 184 219 $user_status = ($smf_settings['registration_method'] == 2) ? 3 : 1; 185 220 $user = get_userdata($userID); 186 if (!s mf_GetMemberIdByName($user->user_login))221 if (!self::getSMFId($user->user_login)) 187 222 smf_registerMember($user->user_login, $user->user_email, $user->user_password, $user_status); 188 223 return true; … … 196 231 if (!self::$bridge_active) return; 197 232 global $userdata,$smf_user_info; 198 if ($smf_user_info) {199 self::syncusers();200 self::crowbar();201 self::syncprofile($userID,$regularSync);202 return;203 }204 233 get_currentuserinfo(); 205 234 $user = get_userdata($userID); … … 210 239 // We obviously only want to force outbound (to SMF) sync if we're POST'ing 211 240 if ($_SERVER['REQUEST_METHOD'] == 'POST') { 212 $smf_cxn = mysql_connect(self::$smf_dbopts['host'],self::$smf_dbopts['user'],self::$smf_dbopts['pass']); 241 if (!$smf_cxn) 242 $smf_cxn = mysql_connect(self::$smf_dbopts['host'],self::$smf_dbopts['user'],self::$smf_dbopts['pass']) or trigger_error(mysql_error(),E_USER_ERROR); 243 $pass = $_POST['pass1']; 244 $SQL = "SELECT passwd, passwordSalt FROM ".self::$smf_dbopts['prefix']."members WHERE UPPER(memberName) = UPPER('".$user->user_login."')"; 245 if (!$rs = mysql_query($SQL,$smf_cxn)) trigger_error(mysql_error(),E_USER_ERROR); 246 if (mysql_num_rows($rs) > 0) { 247 list($password,$salt) = mysql_fetch_array($rs, MYSQL_NUM); 248 $pass = sha1($_POST['pass1'] . $salt); 249 } 213 250 $SQL = "UPDATE ".self::$smf_dbopts['prefix']."members SET websiteUrl='".addslashes($_POST['url'])."', 214 251 AIM='".addslashes($_POST['aim'])."', YIM='".addslashes($_POST['yim'])."', 215 emailAddress='".addslashes($_POST['email'])."', realName='".252 passwd='".$pass."',emailAddress='".addslashes($_POST['email'])."', realName='". 216 253 addslashes($_POST['first_name'].' '.$_POST['last_name'])."', 217 254 signature='".addslashes($_POST['description'])."' 218 WHERE ID_MEMBER=$smfID AND memberName=$user->user_login LIMIT 1"; 219 mysql_query($SQL,$smf_cxn); 220 mysql_close($smf_cxn); 221 } 255 WHERE UPPER(memberName)=UPPER('$user->user_login') LIMIT 1"; 256 mysql_query($SQL,$smf_cxn) or trigger_error(mysql_error(),E_USER_ERROR); 257 } 258 } 259 260 /* getSMFId 261 * @private 262 * Returns the member id from the SMF table based on username 263 */ 264 private function getSMFId($username) { 265 if (!self::$bridge_active) return; 266 $smf_cxn = mysql_connect(self::$smf_dbopts['host'],self::$smf_dbopts['user'],self::$smf_dbopts['pass']); 267 $SQL = "SELECT ID_MEMBER FROM ".self::$smf_dbopts['prefix']."members WHERE UPPER(memberName)=UPPER('$username') LIMIT 1"; 268 if (!$rs = mysql_query($SQL,$smf_cxn)) { 269 trigger_error(mysql_error(),E_USER_WARNING); 270 return null; 271 } else { 272 return mysql_fetch_array($rs, MYSQL_ASSOC)->ID_MEMBER; 273 } 222 274 } 223 275 … … 246 298 smf_setLoginCookie(21600,$user->user_login,passwd,true); 247 299 smf_authenticateUser(); 248 mysql_close($smf_cxn);249 300 } 250 301 … … 256 307 global $wpdb; 257 308 if (!self::$bridge_active) return; 309 if (!function_exists('wp_update_user')) 310 include(ABSPATH.'/wp-includes/registration.php'); 258 311 $smf_cxn = mysql_connect(self::$smf_dbopts['host'],self::$smf_dbopts['user'],self::$smf_dbopts['pass']); 259 312 $SQL = "SELECT UPPER(memberName) FROM ".self::$smf_dbopts['prefix']."members"; … … 264 317 return; 265 318 } 266 $smf_users = mysql_fetch_array($rs,MYSQL_NUM); 267 $SQL = "SELECT UPPER(user_login) FROM wp_users"; 319 $smf_users = array(); 320 while ($row = mysql_fetch_array($rs,MYSQL_NUM)) 321 array_push($smf_users,$row[0]); 322 $SQL = "SELECT UPPER(user_login) FROM ".$wpdb->prefix."users"; 268 323 $wp_users = $wpdb->get_col($SQL); 269 324 if (sizeof($smf_users) == sizeof($wp_users)) 270 325 return 0; 271 326 $sync_count = 0; 327 272 328 foreach ($wp_users as $usr) 273 329 if (!in_array($usr,$smf_users)) { … … 276 332 smf_registerMember($user->user_login, $user->user_email, $user->user_password, $user_status); 277 333 self::syncprofile($user->user_id, true); 334 ++$sync_count; 278 335 } 279 foreach ($smf_ Users as $usr)336 foreach ($smf_users as $usr) 280 337 if (!in_array($usr,$wp_users)) { 281 338 // Sync this user to WP! 282 // TODO 339 $SQL = "SELECT emailAddress,realName FROM ".self::$smf_dbopts['prefix']."members WHERE UPPER(memberName)=UPPER('$usr') LIMIT 1"; 340 if (!$rs = mysql_query($SQL,$smf_cxn)) { 341 // Since this is not a critical step, we'll just throw a warning instead 342 // of bombing the entire app... 343 trigger_error(mysql_error(),E_USER_WARNING); 344 return; 345 } 346 list($email,$name) = mysql_fetch_array($rs); 347 list($fname,$lname) = split(" ",$name); 348 $email_exists = $wpdb->get_row("SELECT user_email FROM $wpdb->users WHERE user_email = '". $email . "'"); 349 if (!$email_exists) { 350 // import the user since their e-mail doesn't exist 351 list($fname,$lname) = split(" ",$usr['realName']); 352 $user_id = wp_update_user(array( 353 "user_login" => $usr, 354 "first_name" => $fname, 355 "last_name" => $lname, 356 "user_email" => $email, 357 "user_pass" => md5(date().rand(100,999))//this doesn't work if we're not doing it on login, but it should pick it up when the user logs in 358 ) 359 ); 360 if ($user_id) ++$sync_count; 361 self::syncprofile($user_id, true); 362 } 283 363 } 284 364 return $sync_count; … … 292 372 function checklogin() { 293 373 if (!self::$bridge_active) return; 294 if (!smf_authenticateUser()) return;295 374 global $smf_user_info,$scheme,$auth_cookie_name; 296 if ($tgt_user = get_userdatabylogin($smf_user_info['username']) == false) { 297 // The user does not exist in the WP database - let's sync and try again 298 if (self::syncusers() > 0) 299 self::checklogin(); 300 return; 301 } 302 wp_set_current_user($tgt_user->ID,$tgt_user->user_login); 303 // And set a cookie just in case 304 $expiration = time() + 172800; 305 $cookie_data = $tgt_user->user_login . '|' . $expiration; 306 $key = wp_hash($tgt_user->user_login . '|' . $expiration,'logged_in'); 307 $cookie_data .= hash_hmac('md5',$tgt_user->user_login . '|' . $expiration,$key); 308 $auth_cookie = wp_generate_auth_cookie($tgt_user->ID, $expiration); 309 $logged_in_cookie = wp_generate_auth_cookie($tgt_user->ID, $expiration, 'logged_in'); 310 setcookie($auth_cookie_name, $auth_cookie, $expiration, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN); 311 setcookie($auth_cookie_name, $auth_cookie, $expiration, ADMIN_COOKIE_PATH, COOKIE_DOMAIN); 312 setcookie(LOGGED_IN_COOKIE, $cookie_data, $expiration, COOKIEPATH, COOKIE_DOMAIN); 313 smf_logOnline('WordPress'); 314 self::syncprofile($tgt_user->ID,true); 375 if (smf_authenticateUser()) { 376 if ($tgt_user = get_userdatabylogin($smf_user_info['username']) == false) { 377 // The user does not exist in the WP database - let's sync and try again 378 if (self::syncusers() > 0) 379 $tgt_user = get_userdatabylogin($smf_user_info['username']); 380 } 381 if ($user->ID) { 382 wp_set_current_user($tgt_user->ID,$tgt_user->user_login); 383 // And set a cookie just in case 384 $expiration = time() + 172800; 385 $cookie_data = $tgt_user->user_login . '|' . $expiration; 386 $key = wp_hash($tgt_user->user_login . '|' . $expiration,'logged_in'); 387 $cookie_data .= hash_hmac('md5',$tgt_user->user_login . '|' . $expiration,$key); 388 $auth_cookie = wp_generate_auth_cookie($tgt_user->ID, $expiration); 389 $logged_in_cookie = wp_generate_auth_cookie($tgt_user->ID, $expiration, 'logged_in'); 390 setcookie($auth_cookie_name, $auth_cookie, $expiration, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN); 391 setcookie($auth_cookie_name, $auth_cookie, $expiration, ADMIN_COOKIE_PATH, COOKIE_DOMAIN); 392 setcookie(LOGGED_IN_COOKIE, $cookie_data, $expiration, COOKIEPATH, COOKIE_DOMAIN); 393 smf_logOnline('WordPress'); 394 self::syncprofile($tgt_user->ID,true); 395 } 396 } else { 397 self::login(); 398 } 315 399 } 316 400 … … 341 425 /* Associate the necessary action and filter hooks */ 342 426 add_action('admin_menu',array('smf_bridge','add_menu')); 343 add_action('wp_login',array('smf_bridge','login'),12);344 add_action('wp_logout',array('smf_bridge','logout'));345 427 add_action('user_register',array('smf_bridge','register')); // Takes 1 argument, userID 346 428 add_action('profile_update',array('smf_bridge','syncprofile')); -
wp-smf-bridge/trunk/readme.txt
r174655 r176248 3 3 Donate link: http://code.google.com/p/wp-smf-bridge/ 4 4 Tags: smf,forums,users,bridge 5 Requires at least: 2. 1.26 Tested up to: 2. 7.17 Stable tag: 0. 15 Requires at least: 2.5.0 6 Tested up to: 2.8.6 7 Stable tag: 0.2 8 8 9 9 User registration and login bridge between Wordpress and Simple Machine Forum … … 11 11 == Description == 12 12 13 WP-SMF-Bridge is a simple user registration and logon bridge between Wordpress and Simple Machine Forum. To get this working, 14 it is highly recommended that you have a fresh, unmodified install of SMF 2.1 or higher installed and running. It must be 15 installed in a subdirectory under your WP install and should not be being accessed through a sumdomain. For example, if your 16 website's address is mysite.mydomain.com, your forums should be somewhere like mysite.mydomain.com/myforum. Also, Wordpress 17 must be able to access your SMF configuration files, otherwise it won't work! 13 WP-SMF-Bridge is a simple user registration and logon bridge between Wordpress and Simple Machine Forum. To get this working, it is highly recommended that you have a fresh, unmodified install of SMF 2.1 or higher installed and running. It must be installed in a subdirectory under your WP install and should not be being accessed through a sumdomain. For example, if your website's address is mysite.mydomain.com, your forums should be somewhere like mysite.mydomain.com/myforum. Also, Wordpress must be able to access your SMF configuration files, otherwise it won't work! 18 14 19 Please do keep in mind that this is a new plugin, and has not been thoroughly tested yet - and should probably not be used on 20 a production website! I am not responsible for any data loss that might occur through your use of this plugin! Please see the 21 plugin's website for a bug-tracker and bug reporting system! If you find a problem, let me know about it so that it may be 22 fixed. If you want to see a feature added, let me know about it so that I may add it! 15 Please do keep in mind that this is a new plugin, and has not been thoroughly tested yet - and should probably not be used on a production website! I am not responsible for any data loss that might occur through your use of this plugin! Please see the plugin's website for a bug-tracker and bug reporting system! If you find a problem, let me know about it so that it may be fixed. If you want to see a feature added, let me know about it so that I may add it! 23 16 24 17 == Installation == 25 18 26 Be sure you have SMF 2.1 or higher installed and running. It **must** be installed in a subdirectory under your WP install and 27 shouldn't be being accessed through a subdomain. 19 Obviously, you need to have a SimpleMachine Forum installation working. It is preferred to have the forum installed as a subdirectory within your WordPress install. For example, if you access your blog from www.mydomain.com, it is preferred for you to access your forum at www.mydomain.com/forum. The name of the directory does not matter, but your forum and WordPress installations should not be on different domains or subdomains! Your blog on www.mydomain.com and forum on forum.mydomain.com will not work! 28 20 29 1. Upload the two PHP files to your `/wp-content/plugins/` directory (or a subdirectory) 30 1. Activate the plugin through the 'Plugins' menu in WordPress 31 1. Fill out the required settings in Settings->SMF Bridge in your `/wp-admin` 32 1. Enjoy! 21 The databases do not need to be the same between the forum and WordPress installs, nor do the database users, but it is highly recommended for performance that you do so. 22 23 From that point forth, configuration is rather simple - all you need to do is install the plugin and enter the relative path to your forum install. In the above example, a WordPress install on www.mydomain.com and forums on www.mydomain.com/forum, the relative path would simply be forum/. Do not add a leading slash, and the trailing slash is optional - the plugin will assume one should be there if you do not explicitly add it. 24 25 If this is a new install, you're all set. If this is not a new install, you will probably want to synchronize your users. Please note that synchronizing your users on a site that is not a new install is not recommended, as it has the potential to completely mess up your user accounts. For more information on this process, refer to SynchronizingUsers. 33 26 34 27 == Frequently Asked Questions == … … 37 30 38 31 Go here and tell me about it: 39 http://code.google.com/p/wp-smf-bridge/ 32 http://code.google.com/p/wp-smf-bridge 33 34 = How do I acheive X? = 35 36 Read above. 37 38 = I would really like feature X, can you add it? = 39 40 Read above; a pattern emerges ;)
Note: See TracChangeset
for help on using the changeset viewer.