function ym_fbook_maintain_wordpress()
{
    global $wpdb, $facebook_settings, $current_user;
    get_currentuserinfo();
    if ($current_user->ID) {
        $_SESSION['wordpress_user_id'] = $current_user->ID;
    } else {
        if (isset($_SESSION['facebook_user_id']) && $_SESSION['facebook_user_id']) {
            // not logged in
            $query = 'SELECT user_id FROM ' . $wpdb->prefix . 'usermeta WHERE meta_key = \'ym_facebook_user_id\' AND meta_value = ' . $_SESSION['facebook_user_id'];
            $rows = $wpdb->get_results($query);
            if ($wpdb->num_rows > 1) {
                // found more than one
                ym_fbook_add_message(sprintf('Your Facebook account is linked to Multiple <strong>%s</strong> accounts', get_bloginfo('name')));
                //			ym_fbook_add_message('UM WTF');
            } else {
                if ($wpdb->num_rows == 1) {
                    // found just one
                    $_SESSION['wordpress_user_id'] = $rows[0]->user_id;
                    ym_fbook_dowplogin();
                } else {
                    unset($_SESSION['wordpress_user_id']);
                    // found nothing
                    // is facebook registration available?
                    if (!$facebook_settings->disable_link_message) {
                        ym_fbook_add_message(sprintf('Your Facebook account is not linked to any <strong>%s</strong> accounts', get_bloginfo('name')));
                        ym_fbook_add_message('Would you like to Link it?');
                    }
                }
            }
        } else {
            // not logged in to either
        }
    }
}
function ym_fbook_profile($echo = FALSE)
{
    global $facebook_settings, $facebook_client;
    $return = '';
    // messages
    $target = ym_fbook_oauth_go();
    // messages
    if ($facebook_settings->force_facebook_auth && !$_SESSION['facebook_user_id']) {
        ym_fbook_add_message(__('In order to continue you must login to the Facebook Application', 'ym_facebook'));
    }
    if ($facebook_settings->force_wordpress_auth && !$_SESSION['wordpress_user_id']) {
        ym_fbook_add_message(__('In order to continue you must login to WordPress', 'ym_facebook'));
    }
    if ($facebook_settings->require_link && !ym_fbook_is_linked()) {
        ym_fbook_add_message(__('In order to continue you must Link your accounts', 'ym_facebook'));
    }
    ym_fbook_messages();
    $return .= '
<table id="ym_fbook_status">
	<tr>
		<th>' . __('WordPress Status', 'ym_facebook') . '</th><td>';
    if (is_user_logged_in()) {
        global $current_user;
        get_currentuserinfo();
        $return .= __(sprintf('Logged in as <strong>%s</strong>', $current_user->user_login), 'ym_facebook') . ' </td><td>';
        //			if ($_SESSION['in_facebook']) {
        $return .= '<form action="" method="post" id="ym_fb_loggedoutform"><input type="hidden" name="loggedout" value="1" /><a href="#nowhere" onclick="jQuery(this).parents(\'form\').submit()">' . __('Logout') . '</a></form>';
        //			} else {
        //				$return .= '<a href="' . wp_logout_url($_SERVER['REQUEST_URI']) . '">' . __('Logout') . '</a>';
        //			}
    } else {
        $return .= __('Not Logged In', 'ym_facebook') . '</td><td><a href="';
        if ($_SESSION['in_facebook']) {
            $return .= '?login=1';
        } else {
            $return .= site_url('wp-login.php?redirect_to=' . urlencode($_SERVER['REQUEST_URI']));
        }
        $return .= '">' . __('Login') . '</a>';
        /*
        if ($facebook_settings->register_with_facebook) {
        	$return .= ' | ';
        	$return .= '<a href="?register=1">Register Account</a>';
        }
        */
    }
    $return .= '</td>
		</tr><tr>
		<th>' . __('Facebook Status', 'ym_facebook') . '</th><td>';
    if ($facebook_client) {
        $return .= __(sprintf('Logged in as <strong>%s</strong>', $facebook_client->user_data->name), 'ym_facebook') . '</td><td>';
        $return .= '<a href="#nowhere" class="ym_fbook_logout">' . __('Logout') . '</a>';
    } else {
        $return .= __('Not Logged In', 'ym_facebook') . '</td><td><a href="';
        $return .= $target;
        $return .= '" target="_parent">' . __('Login') . '</a>';
    }
    $return .= '</td>
		</tr><tr>
		<th>' . __('Link Status', 'ym_facebook') . '</th><td>';
    if (is_user_logged_in()) {
        $id = ym_fbook_is_linked();
        if ($id) {
            $return .= __('Linked', 'ym_facebook') . '</td><td>';
            if (!$facebook_settings->require_link) {
                $return .= '<form action="" method="post"><input type="hidden" name="dounlink" value="1" /><a href="#nowhere" onclick="jQuery(this).parents(\'form\').submit();">' . __('UnLink', 'ym_facebook') . '</a></form>';
            }
        } else {
            $return .= __('Not Linked', 'ym_facebook') . '</td><td><form action="" method="post"><input type="hidden" name="dolink" value="1" /><a href="#nowhere" onclick="jQuery(this).parents(\'form\').submit();">' . __('Link', 'ym_facebook') . '</a></form>';
        }
    } else {
        $return .= __('Login to Lnk', 'ym_facebook');
    }
    $return .= '
		</td>
	</tr>
</table>
';
    if ($echo) {
        echo $return;
    } else {
        return $return;
    }
}