Plugin Directory

Changeset 176248


Ignore:
Timestamp:
11/23/2009 06:58:14 PM (16 years ago)
Author:
JonnyFunFun
Message:

Uploading tag for 0.2

Location:
wp-smf-bridge/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • wp-smf-bridge/trunk/bridge.php

    r104532 r176248  
    22/*
    33Plugin Name: SMF Bridge
    4 Plugin URI: http://projects.jonnyfunfun.com/projects/show/wp-smf-bridge
     4Plugin URI: http://code.google.com/p/wp-smf-bridge
    55Description: User registration and login bridge between Wordpress and Simple Machine Forum
    66Author: Jonathan "JonnyFunFun" Enzinna
    7 Version: 0.1
     7Version: 0.2
    88Author URI: http://www.jonnyfunfun.com/
    99*/
     
    2020     * Returns the current software version
    2121     */
    22     function version() { return 0.1; }
     22    function version() { return 0.2; }
    2323
    2424    /* load
     
    3333        self::$reg_override = (get_option('smf_bridge_regoverride')) ? get_option('smf_bridge_regoverride') : 0;
    3434        if (file_exists(self::$smf_dir."Settings.php"))
    35         require_once(self::$smf_dir."Settings.php");
     35        require(self::$smf_dir."Settings.php");
    3636        else {
    3737        delete_option('smf_bridge_setup_complete');
    3838        return false;
    3939        }
    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");
    4142        self::$bridge_active = true;
    4243        self::$smf_dbopts['user'] = $db_user;
     
    6263    if ($uname != $username)
    6364        return false;
    64     if (smf_GetMemberIdByName($uname))
     65    if (self::getSMFId($uname))
    6566        return false;
    66     if (smf_GetMemberIdByName(strtolower($uname)))
     67    if (self::getSMFId(strtolower($uname)))
    6768        return false;
    6869    return true;
     
    122123    <tr>
    123124        <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 />
    125126         i.e - "forum/" places your forums in <?=ABSPATH?>forum/</em></font>
    126127        </th>
     
    159160    function login() {   
    160161    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    }
    165200    }
    166201
     
    184219    $user_status = ($smf_settings['registration_method'] == 2) ? 3 : 1;
    185220    $user = get_userdata($userID);
    186     if (!smf_GetMemberIdByName($user->user_login))
     221    if (!self::getSMFId($user->user_login))
    187222        smf_registerMember($user->user_login, $user->user_email, $user->user_password, $user_status);
    188223    return true;
     
    196231    if (!self::$bridge_active) return;
    197232    global $userdata,$smf_user_info;
    198     if ($smf_user_info) {
    199         self::syncusers();
    200         self::crowbar();
    201         self::syncprofile($userID,$regularSync);
    202         return;
    203     }
    204233    get_currentuserinfo();
    205234    $user = get_userdata($userID);
     
    210239    // We obviously only want to force outbound (to SMF) sync if we're POST'ing
    211240    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        }
    213250        $SQL = "UPDATE ".self::$smf_dbopts['prefix']."members SET websiteUrl='".addslashes($_POST['url'])."',
    214251        AIM='".addslashes($_POST['aim'])."', YIM='".addslashes($_POST['yim'])."',
    215         emailAddress='".addslashes($_POST['email'])."', realName='".
     252        passwd='".$pass."',emailAddress='".addslashes($_POST['email'])."', realName='".
    216253        addslashes($_POST['first_name'].' '.$_POST['last_name'])."',
    217254        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    }   
    222274    }
    223275
     
    246298    smf_setLoginCookie(21600,$user->user_login,passwd,true);
    247299    smf_authenticateUser();
    248     mysql_close($smf_cxn);
    249300    }
    250301
     
    256307    global $wpdb;
    257308    if (!self::$bridge_active) return;
     309    if (!function_exists('wp_update_user'))
     310        include(ABSPATH.'/wp-includes/registration.php');
    258311    $smf_cxn = mysql_connect(self::$smf_dbopts['host'],self::$smf_dbopts['user'],self::$smf_dbopts['pass']);
    259312    $SQL = "SELECT UPPER(memberName) FROM ".self::$smf_dbopts['prefix']."members";
     
    264317        return;
    265318    }
    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";
    268323    $wp_users = $wpdb->get_col($SQL);
    269324    if (sizeof($smf_users) == sizeof($wp_users))   
    270325        return 0;
    271326    $sync_count = 0;
     327
    272328    foreach ($wp_users as $usr)
    273329        if (!in_array($usr,$smf_users)) {
     
    276332        smf_registerMember($user->user_login, $user->user_email, $user->user_password, $user_status);
    277333        self::syncprofile($user->user_id, true);
     334        ++$sync_count;
    278335        }
    279     foreach ($smf_Users as $usr)
     336    foreach ($smf_users as $usr)
    280337        if (!in_array($usr,$wp_users)) {
    281338        // 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        }       
    283363        }
    284364    return $sync_count;
     
    292372    function checklogin() {
    293373    if (!self::$bridge_active) return;
    294     if (!smf_authenticateUser()) return;
    295374    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    }
    315399    }
    316400
     
    341425/* Associate the necessary action and filter hooks */
    342426add_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'));
    345427add_action('user_register',array('smf_bridge','register')); // Takes 1 argument, userID
    346428add_action('profile_update',array('smf_bridge','syncprofile'));
  • wp-smf-bridge/trunk/readme.txt

    r174655 r176248  
    33Donate link: http://code.google.com/p/wp-smf-bridge/
    44Tags: smf,forums,users,bridge
    5 Requires at least: 2.1.2
    6 Tested up to: 2.7.1
    7 Stable tag: 0.1
     5Requires at least: 2.5.0
     6Tested up to: 2.8.6
     7Stable tag: 0.2
    88
    99User registration and login bridge between Wordpress and Simple Machine Forum
     
    1111== Description ==
    1212
    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!
     13WP-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!
    1814
    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!
     15Please 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!
    2316
    2417== Installation ==
    2518
    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.
     19Obviously, 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!
    2820
    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!
     21The 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
     23From 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
     25If 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.
    3326
    3427== Frequently Asked Questions ==
     
    3730
    3831Go here and tell me about it:
    39 http://code.google.com/p/wp-smf-bridge/
     32http://code.google.com/p/wp-smf-bridge
     33
     34= How do I acheive X? =
     35
     36Read above.
     37
     38= I would really like feature X, can you add it? =
     39
     40Read above; a pattern emerges ;)
Note: See TracChangeset for help on using the changeset viewer.