Return the adapter instance for an authenticated provider
public static getAdapter ( string $providerId = null ) : Hybrid_Provider_Adapter | ||
$providerId | string | ID of the provider |
return | Hybrid_Provider_Adapter |
/** * Check if a provider already connected return user record if available * * @param Request $request Request instance. * @return array|bool User array on success, false on failure. */ public function getUser(Request $request) { $this->_init($request); $idps = $this->hybridAuth->getConnectedProviders(); foreach ($idps as $provider) { $adapter = $this->hybridAuth->getAdapter($provider); return $this->_getUser($provider, $adapter); } return false; }
/** * plugin initialization */ function oauth_init() { global $conf, $page, $hybridauth_conf, $template; load_language('plugin.lang', OAUTH_PATH); $conf['oauth'] = safe_unserialize($conf['oauth']); // check config if (defined('IN_ADMIN')) { if (empty($hybridauth_conf) and strpos(@$_GET['page'], 'plugin-oAuth') === false) { $page['warnings'][] = '<a href="' . OAUTH_ADMIN . '">' . l10n('Social Connect: You need to configure the credentials') . '</a>'; } if (!function_exists('curl_init')) { $page['warnings'][] = l10n('Social Connect: PHP Curl extension is needed'); } } // in case of registration aborded if (script_basename() == 'index' and ($oauth_id = pwg_get_session_var('oauth_new_user')) !== null) { pwg_unset_session_var('oauth_new_user'); if ($oauth_id[0] == 'Persona') { oauth_assign_template_vars(get_gallery_home_url()); $template->block_footer_script(null, 'navigator.id.logout();'); } else { require_once OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php'; try { $hybridauth = new Hybrid_Auth($hybridauth_conf); $adapter = $hybridauth->getAdapter($oauth_id[0]); $adapter->logout(); } catch (Exception $e) { } } } }
/** * Gets user profile from service * * @param string $provider Service provider, like Google, Twitter etc. * * @return array|boolean */ function getServiceProfile($provider = '') { $providers = $this->Hybrid_Auth->getConnectedProviders(); $providerId = ucfirst($provider); if (is_array($providers) && in_array($provider, $providers)) { /* @var Hybrid_Providers_Google $provider */ $provider = $this->Hybrid_Auth->getAdapter($providerId); $profile = $provider->getUserProfile(); $array = json_encode($profile); return json_decode($array, true); } else { return false; } }
/** * A generic function to logout all connected provider at once */ public static function logoutAllProviders() { $idps = Hybrid_Auth::getConnectedProviders(); foreach ($idps as $idp) { $adapter = Hybrid_Auth::getAdapter($idp); $adapter->logout(); } }
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" href="public/css.css" type="text/css"> </head> <body> <table width="90%" border="0" cellpadding="2" cellspacing="2"> <tr> <td valign="top"> <?php include "includes/menu.php"; ?> <fieldset> <legend>Post feed to Facebook pages</legend> <?php try { $adapter = $hybridauth->getAdapter($provider); // ask facebook api for the users accounts $accounts = $adapter->api()->api('/me/accounts'); if (!count($accounts["data"])) { ?> <p> NO pages found for the current user! </p> <p> <b>Note</b>: To be able to post to facebook pages you should: </p> <ol> <li>Add <b>"manage_pages"</b> to the requested scope in the configuration,</li> <li>Logout from Facebook provider,</li> <li>Re sign-in with Facebook.</li> </ol>
/** * Returns hybriauth idp adapter. */ function wsl_process_login_get_provider_adapter($provider) { if (!class_exists('Hybrid_Auth', false)) { require_once WORDPRESS_SOCIAL_LOGIN_ABS_PATH . "hybridauth/Hybrid/Auth.php"; } return Hybrid_Auth::getAdapter($provider); }
/** * logout */ function oauth_logout($user_id) { global $hybridauth_conf; $oauth_id = get_oauth_id($user_id); if (!isset($oauth_id)) { return; } list($provider, $identifier) = explode('---', $oauth_id, 2); if ($provider != 'Persona') { require_once OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php'; try { $hybridauth = new Hybrid_Auth($hybridauth_conf); $adapter = $hybridauth->getAdapter($provider); $adapter->logout(); } catch (Exception $e) { $_SESSION['page_errors'][] = l10n('An error occured, please contact the gallery owner. <i>Error code : %s</i>', $e->getCode()); } } }
function wsl_component_authtest() { // HOOKABLE: do_action("wsl_component_authtest_start"); $adapter = null; $provider_id = isset($_REQUEST["provider"]) ? $_REQUEST["provider"] : null; $user_profile = null; $assets_base_url = WORDPRESS_SOCIAL_LOGIN_PLUGIN_URL . 'assets/img/'; if (!class_exists('Hybrid_Auth', false)) { require_once WORDPRESS_SOCIAL_LOGIN_ABS_PATH . "hybridauth/Hybrid/Auth.php"; } try { $provider = Hybrid_Auth::getAdapter($provider_id); // make as few call as possible if (!(isset($_SESSION['wsl::userprofile']) && $_SESSION['wsl::userprofile'] && ($user_profile = json_decode($_SESSION['wsl::userprofile'])))) { $user_profile = $provider->getUserProfile(); $_SESSION['wsl::userprofile'] = json_encode($user_profile); } $adapter = $provider->adapter; } catch (Exception $e) { } $ha_profile_fields = array(array('field' => 'identifier', 'label' => _wsl__("Provider user ID", 'wordpress-social-login')), array('field' => 'profileURL', 'label' => _wsl__("Profile URL", 'wordpress-social-login')), array('field' => 'webSiteURL', 'label' => _wsl__("Website URL", 'wordpress-social-login')), array('field' => 'photoURL', 'label' => _wsl__("Photo URL", 'wordpress-social-login')), array('field' => 'displayName', 'label' => _wsl__("Display name", 'wordpress-social-login')), array('field' => 'description', 'label' => _wsl__("Description", 'wordpress-social-login')), array('field' => 'firstName', 'label' => _wsl__("First name", 'wordpress-social-login')), array('field' => 'lastName', 'label' => _wsl__("Last name", 'wordpress-social-login')), array('field' => 'gender', 'label' => _wsl__("Gender", 'wordpress-social-login')), array('field' => 'language', 'label' => _wsl__("Language", 'wordpress-social-login')), array('field' => 'age', 'label' => _wsl__("Age", 'wordpress-social-login')), array('field' => 'birthDay', 'label' => _wsl__("Birth day", 'wordpress-social-login')), array('field' => 'birthMonth', 'label' => _wsl__("Birth month", 'wordpress-social-login')), array('field' => 'birthYear', 'label' => _wsl__("Birth year", 'wordpress-social-login')), array('field' => 'email', 'label' => _wsl__("Email", 'wordpress-social-login')), array('field' => 'phone', 'label' => _wsl__("Phone", 'wordpress-social-login')), array('field' => 'address', 'label' => _wsl__("Address", 'wordpress-social-login')), array('field' => 'country', 'label' => _wsl__("Country", 'wordpress-social-login')), array('field' => 'region', 'label' => _wsl__("Region", 'wordpress-social-login')), array('field' => 'city', 'label' => _wsl__("City", 'wordpress-social-login')), array('field' => 'zip', 'label' => _wsl__("Zip", 'wordpress-social-login'))); ?> <style> .widefat td, .widefat th { border: 1px solid #DDDDDD; } .widefat th label { font-weight: bold; } .wp-social-login-provider-list { padding: 10px; } .wp-social-login-provider-list a {text-decoration: none; } .wp-social-login-provider-list img{ border: 0 none; } </style> <div class="metabox-holder columns-2" id="post-body"> <table width="100%"> <tr valign="top"> <td> <?php if (!$adapter) { ?> <div style="padding: 15px; margin-bottom: 8px; border: 1px solid #ddd; background-color: #fff;box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);"> <p><?php _wsl_e("Connect with a provider to get started", 'wordpress-social-login'); ?> .</p> </div> <?php } else { ?> <div class="stuffbox"> <h3> <label><?php _wsl_e("Connected adapter specs", 'wordpress-social-login'); ?> </label> </h3> <div class="inside"> <table class="wp-list-table widefat"> <tr> <th width="200"><label><?php _wsl_e("Provider", 'wordpress-social-login'); ?> </label></th> <td><?php echo $adapter->providerId; ?> </td> </tr> <?php if (isset($adapter->openidIdentifier)) { ?> <tr> <th width="200"><label><?php _wsl_e("OpenID Identifier", 'wordpress-social-login'); ?> </label></th> <td><?php echo $adapter->openidIdentifier; ?> </td> </tr> <?php } ?> <?php if (isset($adapter->scope)) { ?> <tr> <th width="200"><label><?php _wsl_e("Scope", 'wordpress-social-login'); ?> </label></th> <td><?php echo $adapter->scope; ?> </td> </tr> <?php } ?> <?php if (isset($adapter->config['keys'])) { ?> <tr> <th width="200"><label><?php _wsl_e("Application keys", 'wordpress-social-login'); ?> </label></th> <td><div style="max-width:650px"><?php echo json_encode($adapter->config['keys']); ?> </div></td> </tr> <?php } ?> <?php if ($adapter->token("access_token")) { ?> <tr> <th width="200"><label><?php _wsl_e("Access token", 'wordpress-social-login'); ?> </label></th> <td><div style="max-width:650px"><?php echo $adapter->token("access_token"); ?> </div></td> </tr> <?php } ?> <?php if ($adapter->token("access_token_secret")) { ?> <tr> <th width="200"><label><?php _wsl_e("Access token secret", 'wordpress-social-login'); ?> </label></th> <td><?php echo $adapter->token("access_token_secret"); ?> </td> </tr> <?php } ?> <?php if ($adapter->token("expires_in")) { ?> <tr> <th width="200"><label><?php _wsl_e("Access token expires in", 'wordpress-social-login'); ?> </label></th> <td><?php echo (int) $adapter->token("expires_at") - time(); ?> <?php _wsl_e("second(s)", 'wordpress-social-login'); ?> </td> </tr> <?php } ?> <?php if ($adapter->token("expires_at")) { ?> <tr> <th width="200"><label><?php _wsl_e("Access token expires at", 'wordpress-social-login'); ?> </label></th> <td><?php echo date(DATE_W3C, $adapter->token("expires_at")); ?> </td> </tr> <?php } ?> </table> </div> </div> <?php $console = false; if (!isset($adapter->openidIdentifier)) { ?> <div class="stuffbox"> <h3> <label><?php _wsl_e("Connected adapter console", 'wordpress-social-login'); ?> </label> </h3> <div class="inside"> <?php $path = isset($adapter->api->api_base_url) ? $adapter->api->api_base_url : ''; $path = isset($_REQUEST['console-path']) ? $_REQUEST['console-path'] : $path; $method = isset($_REQUEST['console-method']) ? $_REQUEST['console-method'] : ''; $query = isset($_REQUEST['console-query']) ? $_REQUEST['console-query'] : ''; $response = ''; if ($path && in_array($method, array('GET', 'POST'))) { $console = true; try { if ($method == 'GET') { $response = $adapter->api->get($path . ($query ? '?' . $query : '')); } else { $response = $adapter->api->get($path, $query); } $response = $response ? $response : Hybrid_Error::getApiError(); } catch (Exception $e) { $response = "ERROR: " . $e->getMessage(); } } ?> <form action="" method="post"/> <table class="wp-list-table widefat"> <tr> <th width="200"><label><?php _wsl_e("Path", 'wordpress-social-login'); ?> </label></th> <td><input type="text" style="width:96%" name="console-path" value="<?php echo htmlentities($path); ?> "><a href="https://apigee.com/providers" target="_blank"><img src="<?php echo $assets_base_url . 'question.png'; ?> " style="vertical-align: text-top;" /></a></td> </tr> <tr> <th width="200"><label><?php _wsl_e("Method", 'wordpress-social-login'); ?> </label></th> <td><select style="width:100px" name="console-method"><option value="GET" <?php if ($method == 'GET') { echo 'selected'; } ?> >GET</option><!-- <option value="POST" <?php if ($method == 'POST') { echo 'selected'; } ?> >POST</option>--></select></td> </tr> <tr> <th width="200"><label><?php _wsl_e("Query", 'wordpress-social-login'); ?> </label></th> <td><textarea style="width:100%;height:60px;margin-top:6px;" name="console-query"><?php echo htmlentities($query); ?> </textarea></td> </tr> </table> <br /> <input type="submit" value="<?php _wsl_e("Submit", 'wordpress-social-login'); ?> " class="button"> </form> </div> </div> <?php if ($console) { ?> <div class="stuffbox"> <h3> <label><?php _wsl_e("API Response", 'wordpress-social-login'); ?> </label> </h3> <div class="inside"> <textarea rows="25" cols="70" wrap="off" style="width:100%;height:400px;margin-bottom:15px;font-family: monospace;font-size: 12px;"><?php echo htmlentities(print_r($response, true)); ?> </textarea> </div> </div> <?php if (0) { ?> <div class="stuffbox"> <h3> <label><?php _wsl_e("Code PHP", 'wordpress-social-login'); ?> </label> </h3> <div class="inside"> <textarea rows="25" cols="70" wrap="off" style="width:100%;height:210px;margin-bottom:15px;font-family: monospace;font-size: 12px;" >include_once WORDPRESS_SOCIAL_LOGIN_ABS_PATH . 'hybridauth/Hybrid/Auth.php'; /*! Important Direct access to providers apis is newly introduced into WSL and we are still experimenting, so they may change in future releases. */ try { $<?php echo strtolower($adapter->providerId); ?> = Hybrid_Auth::getAdapter( '<?php echo htmlentities($provider_id); ?> ' ); <?php if ($method == 'GET') { ?> $response = $<?php echo strtolower($adapter->providerId); ?> ->api()->get( '<?php echo htmlentities($path . ($query ? '?' . $query : '')); ?> ' ); <?php } else { ?> $response = $<?php echo strtolower($adapter->providerId); ?> ->api()->post( '<?php echo htmlentities($path); ?> ', (array) $query ); <?php } ?> } catch( Exception $e ) { echo "Ooophs, we got an error: " . $e->getMessage(); }</textarea> </div> </div> <div class="stuffbox"> <h3> <label><?php _wsl_e("Connected adapter debug", 'wordpress-social-login'); ?> </label> </h3> <div class="inside"> <textarea rows="25" cols="70" wrap="off" style="width:100%;height:400px;margin-bottom:15px;font-family: monospace;font-size: 12px;"><?php echo htmlentities(print_r($adapter, true)); ?> </textarea> </div> </div> <div class="stuffbox"> <h3> <label><?php _wsl_e("PHP Session", 'wordpress-social-login'); ?> </label> </h3> <div class="inside"> <textarea rows="25" cols="70" wrap="off" style="width:100%;height:350px;margin-bottom:15px;font-family: monospace;font-size: 12px;"><?php echo htmlentities(print_r($_SESSION, true)); ?> </textarea> </div> </div> <?php } ?> <?php } ?> <?php } ?> <?php if (!$console) { ?> <div class="stuffbox"> <h3> <label><?php _wsl_e("Connected user social profile", 'wordpress-social-login'); ?> </label> </h3> <div class="inside"> <table class="wp-list-table widefat"> <?php $user_profile = (array) $user_profile; foreach ($ha_profile_fields as $item) { $item['field'] = $item['field']; ?> <tr> <th width="200"> <label><?php echo $item['label']; ?> </label> </th> <td> <?php if (isset($user_profile[$item['field']]) && $user_profile[$item['field']]) { $field_value = $user_profile[$item['field']]; if (in_array(strtolower($item['field']), array('profileurl', 'websiteurl', 'email'))) { ?> <a href="<?php if ($item['field'] == 'email') { echo 'mailto:'; } echo $field_value; ?> " target="_blank"><?php echo $field_value; ?> </a> <?php } elseif (strtolower($item['field']) == 'photourl') { ?> <a href="<?php echo $field_value; ?> " target="_blank"><img width="36" height="36" align="left" src="<?php echo $field_value; ?> " style="margin-right: 5px;" > <?php echo $field_value; ?> </a> <?php } else { echo $field_value; } } ?> </td> </tr> <?php } ?> </table> </div> </div> <?php } ?> <?php } ?> </td> <td width="10"></td> <td width="400"> <div class="postbox"> <div class="inside"> <h3><?php _wsl_e("Authentication Playground", 'wordpress-social-login'); ?> </h3> <div style="padding:0 20px;"> <p> <?php _wsl_e('Authentication Playground will let you authenticate with the enabled social networks without creating any new user account', 'wordpress-social-login'); ?> . </p> <p> <?php _wsl_e('This tool will also give you a direct access to social networks apis via a lightweight console', 'wordpress-social-login'); ?> . </p> </div> </div> </div> </style> <div class="postbox"> <div class="inside"> <div style="padding:0 20px;"> <p> <?php _wsl_e("Connect with", 'wordpress-social-login'); ?> : </p> <div style="width: 380px; padding: 10px; border: 1px solid #ddd; background-color: #fff;"> <?php do_action('wordpress_social_login', array('mode' => 'test', 'caption' => '')); ?> </div> </div> </div> </div> </td> </tr> </table> </div> <?php // HOOKABLE: do_action("wsl_component_authtest_end"); }
$ha->redirect(BASE_URL); } else { $error = "unable to retrieve user profile"; } } else { $error = "unable to authenticate"; } } catch (Exception $e) { $error = "<b>got an error!</b> " . $e->getMessage(); } } // logout user if (isset($_GET["logout"]) and $_GET["logout"] == true) { try { $ha = new Hybrid_Auth(HA_CONFIG); $f = $ha->getAdapter("facebook"); $f->logout(); $_SESSION = array(); session_destroy(); $ha->redirect(BASE_URL); } catch (Exception $e) { $error = "<b>got an error!</b> " . $e->getMessage(); } } // logged in ? User data if (isset($_SESSION["user_id"])) { $login_status = true; $user_id = $_SESSION["user_id"]; session_write_close(); $user_data = $Users->find_user_by_id($user_id); if (!$user_data) {
public function tryProviderSession($deniedAs) { // don't allow if main admin browse front-end or there is already user session if (!$deniedAs && $this->getSessionDataAs() || null !== $this->_session_data || !e107::getPref('social_login_active', false)) { return $this; } try { // detect all currently connected providers $hybrid = e107::getHybridAuth(); // init the auth class $connected = Hybrid_Auth::getConnectedProviders(); } catch (Exception $e) { e107::getMessage()->addError('[' . $e->getCode() . ']' . $e->getMessage(), 'default', true); $session = e107::getSession(); $session->set('HAuthError', true); $connected = false; } // no active session found if (!$connected) { return $this; } // query DB $sql = e107::getDb(); $where = array(); foreach ($connected as $providerId) { $adapter = Hybrid_Auth::getAdapter($providerId); if (!$adapter->getUserProfile()->identifier) { continue; } $id = $providerId . '_' . $adapter->getUserProfile()->identifier; $where[] = "user_xup='" . $sql->escape($id) . "'"; } $where = implode(' OR ', $where); if ($sql->db_Select('user', 'user_id, user_password, user_xup', $where)) { $user = $sql->db_Fetch(); e107::getUserSession()->makeUserCookie($user); $this->setSessionData(); } return $this; }
function wsl_process_login() { if (!isset($_REQUEST['action']) || $_REQUEST['action'] != "wordpress_social_login") { return; } if (isset($_REQUEST['redirect_to']) && $_REQUEST['redirect_to'] != '') { $redirect_to = $_REQUEST['redirect_to']; // Redirect to https if user wants ssl if (isset($secure_cookie) && $secure_cookie && false !== strpos($redirect_to, 'wp-admin')) { $redirect_to = preg_replace('|^http://|', 'https://', $redirect_to); } if (strpos($redirect_to, 'wp-admin')) { $redirect_to = get_option('wsl_settings_redirect_url'); } } if (empty($redirect_to)) { $redirect_to = get_option('wsl_settings_redirect_url'); } if (empty($redirect_to)) { $redirect_to = site_url(); } try { // load hybridauth require_once dirname(__FILE__) . "/../hybridauth/Hybrid/Auth.php"; // selected provider name $provider = @trim(strip_tags($_REQUEST["provider"])); // build required configuratoin for this provider if (!get_option('wsl_settings_' . $provider . '_enabled')) { throw new Exception('Unknown or disabled provider'); } $config = array(); $config["base_url"] = plugins_url() . '/' . basename(dirname(__FILE__)) . '/hybridauth/'; $config["providers"] = array(); $config["providers"][$provider] = array(); $config["providers"][$provider]["enabled"] = true; // provider application id ? if (get_option('wsl_settings_' . $provider . '_app_id')) { $config["providers"][$provider]["keys"]["id"] = get_option('wsl_settings_' . $provider . '_app_id'); } // provider application key ? if (get_option('wsl_settings_' . $provider . '_app_key')) { $config["providers"][$provider]["keys"]["key"] = get_option('wsl_settings_' . $provider . '_app_key'); } // provider application secret ? if (get_option('wsl_settings_' . $provider . '_app_secret')) { $config["providers"][$provider]["keys"]["secret"] = get_option('wsl_settings_' . $provider . '_app_secret'); } // create an instance for Hybridauth $hybridauth = new Hybrid_Auth($config); // try to authenticate the selected $provider if ($hybridauth->isConnectedWith($provider)) { $adapter = $hybridauth->getAdapter($provider); $hybridauth_user_profile = $adapter->getUserProfile(); } else { throw new Exception('User not connected with ' . $provider . '!'); } $user_email = $hybridauth_user_profile->email; } catch (Exception $e) { die("Unspecified error. #" . $e->getCode()); } $user_id = null; // if the user email is verified, then try to map to legacy account if exist // > Currently only Facebook, Google, Yhaoo and Foursquare do provide the verified user email. if (!empty($hybridauth_user_profile->emailVerified)) { $user_id = (int) email_exists($hybridauth_user_profile->emailVerified); } // try to get user by meta if not if (!$user_id) { $user_id = (int) wsl_get_user_by_meta($provider, $hybridauth_user_profile->identifier); } // if user found if ($user_id) { $user_data = get_userdata($user_id); $user_login = $user_data->user_login; } else { // generate a valid user login $user_login = str_replace(' ', '_', strtolower($hybridauth_user_profile->displayName)); if (!validate_username($user_login)) { $user_login = strtolower($provider) . "_user_" . md5($hybridauth_user_profile->identifier); } // user name should be unique if (username_exists($user_login)) { $i = 1; $user_login_tmp = $user_login; do { $user_login_tmp = $user_login . "_" . $i++; } while (username_exists($user_login_tmp)); $user_login = $user_login_tmp; } // generate an email if none if (!isset($user_email) or !is_email($user_email)) { $user_email = strtolower($provider . "_user_" . $user_login) . "@example.com"; } // email should be unique if (email_exists($user_email)) { do { $user_email = md5(uniqid(wp_rand(10000, 99000))) . "@example.com"; } while (email_exists($user_email)); } $userdata = array('user_login' => $user_login, 'user_email' => $user_email, 'first_name' => $hybridauth_user_profile->firstName, 'last_name' => $hybridauth_user_profile->lastName, 'user_nicename' => $hybridauth_user_profile->displayName, 'display_name' => $hybridauth_user_profile->displayName, 'user_url' => $hybridauth_user_profile->profileURL, 'description' => $hybridauth_user_profile->description, 'user_pass' => wp_generate_password()); // Create a new user $user_id = wp_insert_user($userdata); // update user metadata if ($user_id && is_integer($user_id)) { update_user_meta($user_id, $provider, $hybridauth_user_profile->identifier); } else { die("An error occurred while creating a new user!"); } } $user_age = $hybridauth_user_profile->age; // not that precise you say... well welcome to my world if (!$user_age && (int) $hybridauth_user_profile->birthYear) { $user_age = (int) date("Y") - (int) $hybridauth_user_profile->birthYear; } update_user_meta($user_id, 'wsl_user', $provider); update_user_meta($user_id, 'wsl_user_gender', $hybridauth_user_profile->gender); update_user_meta($user_id, 'wsl_user_age', $user_age); update_user_meta($user_id, 'wsl_user_image', $hybridauth_user_profile->photoURL); wp_set_auth_cookie($user_id); wp_safe_redirect($redirect_to); exit; }
// well, basically your should not display this to the end user, just give him a hint and move on.. $error .= "<br /><br /><b>Original error message:</b> " . $e->getMessage(); $error .= "<hr /><pre>Trace:<br />" . $e->getTraceAsString() . "</pre>"; } try { // create an instance for Hybridauth with the configuration file path as parameter $hybridauth = new Hybrid_Auth($config); } catch (Exception $e) { showError($e); } // Trigger to logout one service or all at once if (isset($_GET['logout'])) { if ($_GET['logout'] == "all") { $hybridauth->logoutAllProviders(); } else { $adapter = $hybridauth->getAdapter($_GET['logout']); $adapter->logout(); } } // if user select a provider to login with // then inlcude hybridauth config and main class // then try to authenticate te current user // finally redirect him to his profile page if (isset($_GET["provider"]) && $_GET["provider"]) { try { // set selected provider name $provider = @trim(strip_tags($_GET["provider"])); // try to authenticate the selected $provider $adapter = $hybridauth->authenticate($provider); // grab the user profile $user_data = $adapter->getUserProfile();
function wsl_process_login_hybridauth_authenticate($provider, $redirect_to) { try { # Hybrid_Auth already used? if (class_exists('Hybrid_Auth', false)) { return wsl_render_notices_pages(_wsl__("Error: Another plugin seems to be using HybridAuth Library and made WordPress Social Login unusable. We recommand to find this plugin and to kill it with fire!", 'wordpress-social-login')); } // load hybridauth require_once WORDPRESS_SOCIAL_LOGIN_ABS_PATH . "/hybridauth/Hybrid/Auth.php"; // build required configuratoin for this provider if (!get_option('wsl_settings_' . $provider . '_enabled')) { throw new Exception('Unknown or disabled provider'); } $config = array(); $config["providers"] = array(); $config["providers"][$provider] = array(); $config["providers"][$provider]["enabled"] = true; // provider application id ? if (get_option('wsl_settings_' . $provider . '_app_id')) { $config["providers"][$provider]["keys"]["id"] = get_option('wsl_settings_' . $provider . '_app_id'); } // provider application key ? if (get_option('wsl_settings_' . $provider . '_app_key')) { $config["providers"][$provider]["keys"]["key"] = get_option('wsl_settings_' . $provider . '_app_key'); } // provider application secret ? if (get_option('wsl_settings_' . $provider . '_app_secret')) { $config["providers"][$provider]["keys"]["secret"] = get_option('wsl_settings_' . $provider . '_app_secret'); } // create an instance for Hybridauth $hybridauth = new Hybrid_Auth($config); // try to authenticate the selected $provider if ($hybridauth->isConnectedWith($provider)) { $adapter = $hybridauth->getAdapter($provider); $hybridauth_user_profile = $adapter->getUserProfile(); // check hybridauth user email $hybridauth_user_id = (int) wsl_get_user_by_meta($provider, $hybridauth_user_profile->identifier); $hybridauth_user_email = sanitize_email($hybridauth_user_profile->email); $hybridauth_user_login = sanitize_user($hybridauth_user_profile->displayName); $request_user_login = ""; $request_user_email = ""; # {{{ linking new accounts // Bouncer :: Accounts Linking is enabled if (get_option('wsl_settings_bouncer_linking_accounts_enabled') == 1) { // if user is linking account // . we DO import contacts // . we DO store the user profile // // . we DONT create another entry on user table // . we DONT create nor update his data on usermeata table if ($_REQUEST['action'] == "wordpress_social_link") { global $current_user; get_currentuserinfo(); $user_id = $current_user->ID; return wsl_process_login_authenticate_wp_user_linked_account($user_id, $provider, $redirect_to, $adapter, $hybridauth_user_profile); } // check if connected user is linked account $linked_account = wsl_get_user_linked_account_by_provider_and_identifier($provider, $hybridauth_user_profile->identifier); // if linked account found, we connect the actual user if ($linked_account) { if (count($linked_account) > 1) { return wsl_render_notices_pages(_wsl__("This {$provider} is linked to many accounts!", 'wordpress-social-login')); } $user_id = $linked_account[0]->user_id; if (!$user_id) { return wsl_render_notices_pages(_wsl__("Something wrong!", 'wordpress-social-login')); } return wsl_process_login_authenticate_wp_user($user_id, $provider, $redirect_to, $adapter, $hybridauth_user_profile); } } # }}} linking new accounts # {{{ module Bouncer // Bouncer :: Filters by emails domains name if (get_option('wsl_settings_bouncer_new_users_restrict_domain_enabled') == 1) { if (empty($hybridauth_user_email)) { return wsl_render_notices_pages(get_option('wsl_settings_bouncer_new_users_restrict_domain_text_bounce')); } $list = get_option('wsl_settings_bouncer_new_users_restrict_domain_list'); $list = preg_split('/$\\R?^/m', $list); $current = strstr($hybridauth_user_email, '@'); $shall_pass = false; foreach ($list as $item) { if (trim(strtolower("@{$item}")) == strtolower($current)) { $shall_pass = true; } } if (!$shall_pass) { return wsl_render_notices_pages(get_option('wsl_settings_bouncer_new_users_restrict_domain_text_bounce')); } } // Bouncer :: Filters by e-mails addresses if (get_option('wsl_settings_bouncer_new_users_restrict_email_enabled') == 1) { if (empty($hybridauth_user_email)) { return wsl_render_notices_pages(get_option('wsl_settings_bouncer_new_users_restrict_email_text_bounce')); } $list = get_option('wsl_settings_bouncer_new_users_restrict_email_list'); $list = preg_split('/$\\R?^/m', $list); $shall_pass = false; foreach ($list as $item) { if (trim(strtolower($item)) == strtolower($hybridauth_user_email)) { $shall_pass = true; } } if (!$shall_pass) { return wsl_render_notices_pages(get_option('wsl_settings_bouncer_new_users_restrict_email_text_bounce')); } } // Bouncer :: Filters by profile urls if (get_option('wsl_settings_bouncer_new_users_restrict_profile_enabled') == 1) { $list = get_option('wsl_settings_bouncer_new_users_restrict_profile_list'); $list = preg_split('/$\\R?^/m', $list); $shall_pass = false; foreach ($list as $item) { if (trim(strtolower($item)) == strtolower($hybridauth_user_profile->profileURL)) { $shall_pass = true; } } if (!$shall_pass) { return wsl_render_notices_pages(get_option('wsl_settings_bouncer_new_users_restrict_profile_text_bounce')); } } // if user do not exist if (!$hybridauth_user_id) { // Bouncer :: Accept new registrations if (get_option('wsl_settings_bouncer_registration_enabled') == 2) { return wsl_render_notices_pages(_wsl__("registration is now closed!", 'wordpress-social-login')); } // Bouncer :: Profile Completion if (get_option('wsl_settings_bouncer_profile_completion_require_email') == 1 && empty($hybridauth_user_email) || get_option('wsl_settings_bouncer_profile_completion_change_username') == 1) { do { list($shall_pass, $request_user_login, $request_user_email) = wsl_process_login_complete_registration($provider, $redirect_to, $hybridauth_user_email, $hybridauth_user_login); } while (!$shall_pass); } } # }}} module Bouncer } else { throw new Exception('User not connected with ' . $provider . '!'); } } catch (Exception $e) { return wsl_render_notices_pages(sprintf(_wsl__("Unspecified error. #%d", 'wordpress-social-login'), $e->getCode())); } $user_id = null; // if the user email is verified, then try to map to legacy account if exist // > Currently only Facebook, Google, Yahaoo and Foursquare do provide the verified user email. if (!empty($hybridauth_user_profile->emailVerified)) { $user_id = (int) email_exists($hybridauth_user_profile->emailVerified); } // try to get user by meta if not if (!$user_id) { $user_id = (int) wsl_get_user_by_meta($provider, $hybridauth_user_profile->identifier); } return array($user_id, $adapter, $hybridauth_user_profile, $hybridauth_user_id, $hybridauth_user_email, $request_user_login, $request_user_email); }
function do_logout() { // after login come back to the same page $loginCallback = qa_path('', array(), qa_opt('site_url')); require_once "Hybrid/Auth.php"; // prepare the configuration of HybridAuth $config = $this->getConfig($loginCallback); try { // try to logout $hybridauth = new Hybrid_Auth($config); if ($hybridauth->isConnectedWith($this->provider)) { $adapter = $hybridauth->getAdapter($this->provider); $adapter->logout(); } } catch (Exception $e) { // not really interested in the error message - for now // however, in case we have errors 6 or 7, then we have to call logout to clean everything up if ($e->getCode() == 6 || $e->getCode() == 7) { $adapter->logout(); } } }
function SocialAuth_WP_contacts() { ini_set("display_errors", 0); $noContactMsg = "Either you do not have any contact(s) or your login provider is not supporting this feature at the moment."; echo '<div class="wrap">'; echo '<div class="icon32 icon32-posts-post" id="icon-edit"><br></div>'; echo "<h2>My Contacts</h2> <br/>"; // load hybridauth require_once dirname(__FILE__) . "/hybridauth/Hybrid/Auth.php"; // load wp-load.php $wp_load = dirname(dirname(dirname(dirname(__FILE__)))) . '/wp-load.php'; require_once $wp_load; include_once 'common.php'; $user_id = get_current_user_id(); $provider = null; if ($user_id != 0) { $provider = get_user_meta($user_id, 'ha_login_provider', true); } // selected provider name if ($provider == null) { echo $noContactMsg; return; } //global $HA_PROVIDER_CONFIG; $SocialAuth_WP_providers = get_option('SocialAuth_WP_providers'); if (is_array($SocialAuth_WP_providers) && count($SocialAuth_WP_providers)) { $config = array(); if (isset($SocialAuth_WP_providers[$provider])) { $config["base_url"] = plugin_dir_url(__FILE__) . 'hybridauth/'; $config["providers"] = array(); //this si same as orig config, no need to amke config again $config["providers"][$provider] = $SocialAuth_WP_providers[$provider]; } else { echo "Current Provider is unknowun to system."; return; } } else { echo "It seems SocialAuth-WP plugin is not configured properly. Please contact site administrator."; return; } // create an instance for Hybridauth $hybridauth = new Hybrid_Auth($config); $adapter = null; // try to check is provider still authenticated if ($hybridauth->isConnectedWith($provider)) { $adapter = $hybridauth->getAdapter($provider); } else { echo "It seems your session with Login provider has expired. Please logout and login again to system to continue."; return; } $contacts = array(); try { $contacts = $adapter->getUserContacts(); } catch (exception $e) { echo $noContactMsg; return; } if (count($contacts)) { require SOCIALAUTH_WP_PLUGIN_PATH . '/pagination.class.php'; $pagination = new pagination($contacts, isset($_GET['pageNum']) ? $_GET['pageNum'] : 1, 15); $ContactPages = $pagination->getResults(); $tbHeaders = array('Name', 'Profile URL', 'Email'); ?> <?php if (count($ContactPages) != 0) { echo $pageNumbers = '<div class="numbers" style="text-align:right;" >' . $pagination->getLinks(array('page' => 'SocialAuth-WP-contacts')) . '</div>'; ?> <table class="wp-list-table widefat fixed users"> <thead> <tr> <?php foreach ($tbHeaders as $header) { echo '<th style="" class="manage-column column-username" id="" scope="col">' . $header . '</th>'; } ?> </tr> </thead> <tfoot> <tr> <?php foreach ($tbHeaders as $header) { echo '<th style="" class="manage-column column-username" id="" scope="col">' . $header . '</th>'; } ?> </tr> </tfoot> <tbody class="list:user" id="the-list"> <?php foreach ($ContactPages as $index => $contact) { $alternate = $index % 2 == 0 ? "alternate" : ""; ?> <tr class="<?php echo $alternate; ?> " id="user-12"> <td class="column-username"> <?php if (strlen($contact->photoURL)) { ?> <img height="32" width="32" src="<?php echo $contact->photoURL; ?> " > <?php } else { ?> <img height="32" width="32" class="avatar avatar-32 photo" src="http://0.gravatar.com/avatar/8af77eb212190822af34f1725a01922d?s=32&d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G" alt=""> <?php } ?> <?php echo $contact->displayName; ?> </td> <td class="posts"> <a href="<?php echo $contact->profileURL; ?> "> <?php echo $contact->profileURL; ?> </a> </td> <td class="posts"> <?php echo empty($contact->email) ? " " : $contact->email; ?> </td> </tr> <?php } ?> </tbody> </table> <?php echo $pageNumbers = '<div class="numbers" style="text-align:right;" >' . $pagination->getLinks(array('page' => 'SocialAuth-WP-contacts')) . '</div>'; } ?> <?php } else { echo $noContactMsg; return; } echo '</div>'; }