/** * Create user in Wordpress database. */ function the_champ_create_user($profileData, $verification = false) { // create username, firstname and lastname $usernameFirstnameLastname = explode('|tc|', the_champ_create_username($profileData)); $username = $usernameFirstnameLastname[0]; $firstName = $usernameFirstnameLastname[1]; $lastName = $usernameFirstnameLastname[2]; // make username unique $nameexists = true; $index = 1; $username = str_replace(' ', '-', $username); //cyrillic username $username = sanitize_user($username, true); if ($username == '-') { $emailParts = explode('@', $profileData['email']); $username = $emailParts[0]; } $userName = $username; while ($nameexists == true) { if (username_exists($userName) != 0) { $index++; $userName = $username . $index; } else { $nameexists = false; } } $username = $userName; $password = wp_generate_password(); $userdata = array('user_login' => $username, 'user_pass' => $password, 'user_nicename' => sanitize_user($firstName, true), 'user_email' => $profileData['email'], 'display_name' => $firstName, 'nickname' => $firstName, 'first_name' => $firstName, 'last_name' => $lastName, 'description' => isset($profileData['bio']) && $profileData['bio'] != '' ? $profileData['bio'] : '', 'user_url' => isset($profileData['link']) && $profileData['link'] != '' ? $profileData['link'] : '', 'role' => get_option('default_role')); $userId = wp_insert_user($userdata); if (!is_wp_error($userId)) { if (isset($profileData['id']) && $profileData['id'] != '') { update_user_meta($userId, 'thechamp_social_id', $profileData['id']); } if (isset($profileData['avatar']) && $profileData['avatar'] != '') { update_user_meta($userId, 'thechamp_avatar', $profileData['avatar']); } if (isset($profileData['large_avatar']) && $profileData['large_avatar'] != '') { update_user_meta($userId, 'thechamp_large_avatar', $profileData['large_avatar']); } if (!empty($profileData['provider'])) { update_user_meta($userId, 'thechamp_provider', $profileData['provider']); } if (!$verification) { // send notification email heateor_ss_new_user_notification($userId); } // insert profile data in BP XProfile table global $theChampLoginOptions; if (isset($theChampLoginOptions['xprofile_mapping']) && is_array($theChampLoginOptions['xprofile_mapping'])) { foreach ($theChampLoginOptions['xprofile_mapping'] as $key => $val) { // save xprofile fields global $wpdb; $value = ''; if (isset($profileData[$val])) { $value = $profileData[$val]; } $wpdb->insert($wpdb->prefix . 'bp_xprofile_data', array('id' => NULL, 'field_id' => $wpdb->get_var($wpdb->prepare("SELECT id FROM " . $wpdb->prefix . "bp_xprofile_fields WHERE name = %s", $key)), 'user_id' => $userId, 'value' => $value, 'last_updated' => ''), array('%d', '%d', '%d', '%s', '%s')); } } // hook - user successfully created do_action('the_champ_user_successfully_created', $userId, $userdata, $profileData); return $userId; } return false; }
/** * Check querystring variables */ function the_champ_connect() { global $theChampLoginOptions; // verify email if (isset($_GET['SuperSocializerKey']) && ($verificationKey = trim(esc_attr($_GET['SuperSocializerKey']))) != '') { $users = get_users('meta_key=thechamp_key&meta_value=' . $verificationKey); if (count($users) > 0 && isset($users[0]->ID)) { delete_user_meta($users[0]->ID, 'thechamp_key'); // update password $password = wp_generate_password(); wp_update_user(array('ID' => $users[0]->ID, 'user_pass' => $password)); // send notification email heateor_ss_new_user_notification($users[0]->ID); wp_redirect(home_url() . '?SuperSocializerVerified=1'); die; } } // Instagram auth if (isset($_GET['SuperSocializerInstaToken']) && $_GET['SuperSocializerInstaToken'] != '') { $instaAuthUrl = 'https://api.instagram.com/v1/users/self?access_token=' . trim(esc_attr($_GET['SuperSocializerInstaToken'])); $response = wp_remote_get($instaAuthUrl, array('timeout' => 15)); if (!is_wp_error($response) && isset($response['response']['code']) && 200 === $response['response']['code']) { $body = json_decode(wp_remote_retrieve_body($response)); if (is_object($body->data) && isset($body->data) && isset($body->data->id)) { $redirection = isset($_GET['super_socializer_redirect_to']) && $_GET['super_socializer_redirect_to'] != '' ? esc_attr($_GET['super_socializer_redirect_to']) : ''; $response = the_champ_user_auth($body->data, 'instagram', $redirection); if (is_array($response) && isset($response['message']) && $response['message'] == 'register' && (!isset($response['url']) || $response['url'] == '')) { $redirectTo = esc_attr(the_champ_get_login_redirection_url($redirection, true)); } elseif (isset($response['message']) && $response['message'] == 'linked') { $redirectTo = $redirection . (strpos($redirection, '?') !== false ? '&' : '?') . 'linked=1'; } elseif (isset($response['message']) && $response['message'] == 'not linked') { $redirectTo = $redirection . (strpos($redirection, '?') !== false ? '&' : '?') . 'linked=0'; } elseif (isset($response['url']) && $response['url'] != '') { $redirectTo = $response['url']; } else { $redirectTo = esc_attr(the_champ_get_login_redirection_url($redirection)); } the_champ_close_login_popup($redirectTo); } } } // Twitch auth if (isset($_GET['SuperSocializerAuth']) && $_GET['SuperSocializerAuth'] == 'Twitch') { if (isset($_GET['SuperSocializerData'])) { $body = explode('&&', urldecode($_GET['SuperSocializerData'])); $profileData = array(); foreach ($body as $dataField) { $keyValue = explode('=', $dataField); $profileData[$keyValue[0]] = $keyValue[1]; } if (isset($profileData['_id']) && $profileData['_id'] != '') { $redirection = isset($_GET['super_socializer_redirect_to']) && $_GET['super_socializer_redirect_to'] != '' ? esc_attr($_GET['super_socializer_redirect_to']) : ''; $response = the_champ_user_auth($profileData, 'twitch', $redirection); if (is_array($response) && isset($response['message']) && $response['message'] == 'register' && (!isset($response['url']) || $response['url'] == '')) { $redirectTo = esc_attr(the_champ_get_login_redirection_url($redirection, true)); } elseif (isset($response['message']) && $response['message'] == 'linked') { $redirectTo = $redirection . (strpos($redirection, '?') !== false ? '&' : '?') . 'linked=1'; } elseif (isset($response['message']) && $response['message'] == 'not linked') { $redirectTo = $redirection . (strpos($redirection, '?') !== false ? '&' : '?') . 'linked=0'; } elseif (isset($response['url']) && $response['url'] != '') { $redirectTo = $response['url']; } else { $redirectTo = esc_attr(the_champ_get_login_redirection_url($redirection)); } the_champ_close_login_popup($redirectTo); } } elseif (isset($theChampLoginOptions['twitch_client_id']) && $theChampLoginOptions['twitch_client_id'] != '') { ?> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script src="https://ttv-api.s3.amazonaws.com/twitch.min.js"></script> <script> theChampSerialize = function(obj, prefix) { var str = []; for(var p in obj) { if (obj.hasOwnProperty(p)) { var k = prefix ? prefix + "[" + p + "]" : p, v = obj[p]; str.push(typeof v == "object" ? theChampSerialize(v, k) : encodeURIComponent(k) + "=" + encodeURIComponent(v)); } } return str.join("&&"); } jQuery(function(){ Twitch.init({clientId: '<?php echo $theChampLoginOptions['twitch_client_id']; ?> '}, function(error, status) { // the sdk is now loaded console.log(error); console.log(status); if( !status.authenticated ){ Twitch.login({ scope: ['user_read', 'channel_read'] }); } else { Twitch.api({method: 'user'}, function(error, user) { if(user._id && user._id != null){ window.opener.theChampAjaxUserAuth(user, 'twitch'); window.opener.location.href = '<?php echo site_url(); ?> ?SuperSocializerAuth=Twitch&super_socializer_redirect_to='+window.opener.theChampTwitterRedirect+'&SuperSocializerData=' + encodeURIComponent(theChampSerialize(user)); window.close(); } }); } }); }); </script> <?php } die; } // Steam auth if (isset($_GET['SuperSocializerSteamAuth']) && $_GET['SuperSocializerSteamAuth'] != '') { global $theChampSteamLogin; $theChampSteamId = $theChampSteamLogin->validate(); $result = wp_remote_get("http://steamcommunity.com/profiles/{$theChampSteamId}/?xml=1", array('timeout' => 15)); if (!is_wp_error($result) && isset($result['response']['code']) && 200 === $result['response']['code']) { $body = wp_remote_retrieve_body($result); $xml = simplexml_load_string($body, null, LIBXML_NOCDATA); if ($xml && isset($xml->steamID64) && $xml->steamID64) { $steamRedirect = esc_url($_GET['SuperSocializerSteamAuth']); $response = the_champ_user_auth($xml, 'steam', $steamRedirect); if (is_array($response) && isset($response['message']) && $response['message'] == 'register' && (!isset($response['url']) || $response['url'] == '')) { $redirectTo = esc_attr(the_champ_get_login_redirection_url($steamRedirect, true)); } elseif (isset($response['message']) && $response['message'] == 'linked') { $redirectTo = $steamRedirect . (strpos($steamRedirect, '?') !== false ? '&' : '?') . 'linked=1'; } elseif (isset($response['message']) && $response['message'] == 'not linked') { $redirectTo = $steamRedirect . (strpos($steamRedirect, '?') !== false ? '&' : '?') . 'linked=0'; } elseif (isset($response['url']) && $response['url'] != '') { $redirectTo = $response['url']; } else { $redirectTo = esc_attr(the_champ_get_login_redirection_url($steamRedirect)); } the_champ_close_login_popup($redirectTo); } } die; } // send request to Xing if (isset($_GET['SuperSocializerAuth']) && $_GET['SuperSocializerAuth'] == 'Xing') { session_start(); if (!isset($_GET['oauth_token']) && isset($_SESSION['OAUTH_ACCESS_TOKEN'])) { unset($_SESSION['OAUTH_ACCESS_TOKEN']); } if (isset($theChampLoginOptions['xing_ck']) && $theChampLoginOptions['xing_ck'] != '' && isset($theChampLoginOptions['xing_cs']) && $theChampLoginOptions['xing_cs'] != '') { $xingClient = new oauth_client_class(); $xingClient->debug = 0; $xingClient->debug_http = 1; $xingClient->server = 'XING'; $xingClient->redirect_uri = site_url() . '/index.php?SuperSocializerAuth=Xing&super_socializer_redirect_to=' . esc_attr(str_replace(array('http://', 'https://'), '', urldecode($_GET['super_socializer_redirect_to']))); $xingClient->client_id = $theChampLoginOptions['xing_ck']; $xingClient->client_secret = $theChampLoginOptions['xing_cs']; if ($success = $xingClient->Initialize()) { if ($success = $xingClient->Process()) { if (strlen($xingClient->access_token)) { $success = $xingClient->CallAPI('https://api.xing.com/v1/users/me', 'GET', array(), array('FailOnAccessError' => true), $xingResponse); } } $success = $xingClient->Finalize($success); } if ($xingClient->exit) { die('exit'); } if ($success) { if (isset($xingResponse->users) && is_array($xingResponse->users) && isset($xingResponse->users[0]->id)) { $xingRedirect = the_champ_get_http() . esc_attr($_GET['super_socializer_redirect_to']); $response = the_champ_user_auth($xingResponse->users[0], 'xing', $xingRedirect); if (is_array($response) && isset($response['message']) && $response['message'] == 'register' && (!isset($response['url']) || $response['url'] == '')) { $redirectTo = esc_attr(the_champ_get_login_redirection_url($xingRedirect, true)); } elseif (isset($response['message']) && $response['message'] == 'linked') { $redirectTo = $xingRedirect . (strpos($xingRedirect, '?') !== false ? '&' : '?') . 'linked=1'; } elseif (isset($response['message']) && $response['message'] == 'not linked') { $redirectTo = $xingRedirect . (strpos($xingRedirect, '?') !== false ? '&' : '?') . 'linked=0'; } elseif (isset($response['url']) && $response['url'] != '') { $redirectTo = $response['url']; } else { $redirectTo = esc_attr(the_champ_get_login_redirection_url($xingRedirect)); } the_champ_close_login_popup($redirectTo); } } else { echo 'Error:' . $xingClient->error; die; } } } // send request to twitter if (isset($_GET['SuperSocializerAuth']) && $_GET['SuperSocializerAuth'] == 'Twitter') { if (isset($theChampLoginOptions['twitter_key']) && $theChampLoginOptions['twitter_key'] != '' && isset($theChampLoginOptions['twitter_secret']) && $theChampLoginOptions['twitter_secret'] != '') { /* Build TwitterOAuth object with client credentials. */ $connection = new TwitterOAuth($theChampLoginOptions['twitter_key'], $theChampLoginOptions['twitter_secret']); /* Get temporary credentials. */ $requestToken = $connection->getRequestToken(site_url() . '/index.php'); if ($connection->http_code == 200) { // generate unique ID $uniqueId = mt_rand(); // save oauth token and secret in db temporarily update_user_meta($uniqueId, 'thechamp_twitter_oauthtoken', $requestToken['oauth_token']); update_user_meta($uniqueId, 'thechamp_twitter_oauthtokensecret', $requestToken['oauth_token_secret']); if (isset($_GET['super_socializer_redirect_to']) && $_GET['super_socializer_redirect_to'] != '') { update_user_meta($uniqueId, 'thechamp_twitter_redirect', esc_attr($_GET['super_socializer_redirect_to'])); } wp_redirect($connection->getAuthorizeURL($requestToken['oauth_token'])); die; } else { ?> <div style="width: 500px; margin: 0 auto"> <ol> <li><?php echo sprintf(__('Enter exactly the following url in <strong>Website</strong> and <strong>Callback Url</strong> options in your Twitter app (see step 3 %s)', 'Super-Socializer'), '<a target="_blank" href="http://support.heateor.com/how-to-get-twitter-api-key-and-secret/">here</a>'); ?> <br/> <?php echo site_url(); ?> </li> <li><?php _e('Make sure cURL is enabled at your website server. You may need to contact the server administrator of your website to verify this', 'Super-Socializer'); ?> </li> </ol> </div> <?php die; } } } // twitter authentication if (isset($_REQUEST['oauth_token'])) { global $wpdb; $uniqueId = $wpdb->get_var($wpdb->prepare("SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = 'thechamp_twitter_oauthtoken' and meta_value = %s", $_REQUEST['oauth_token'])); $oauthTokenSecret = get_user_meta($uniqueId, 'thechamp_twitter_oauthtokensecret', true); // twitter redirect url $twitterRedirectUrl = get_user_meta($uniqueId, 'thechamp_twitter_redirect', true); if (empty($uniqueId) || $oauthTokenSecret == '') { // invalid request wp_redirect(site_url()); die; } $connection = new TwitterOAuth($theChampLoginOptions['twitter_key'], $theChampLoginOptions['twitter_secret'], $_REQUEST['oauth_token'], $oauthTokenSecret); /* Request access tokens from twitter */ $accessToken = $connection->getAccessToken($_REQUEST['oauth_verifier']); /* Create a TwitterOauth object with consumer/user tokens. */ $connection = new TwitterOAuth($theChampLoginOptions['twitter_key'], $theChampLoginOptions['twitter_secret'], $accessToken['oauth_token'], $accessToken['oauth_token_secret']); $content = $connection->get('account/verify_credentials'); // delete temporary data delete_user_meta($uniqueId, 'thechamp_twitter_oauthtokensecret'); delete_user_meta($uniqueId, 'thechamp_twitter_oauthtoken'); delete_user_meta($uniqueId, 'thechamp_twitter_redirect'); if (is_object($content) && isset($content->id)) { $response = the_champ_user_auth($content, 'twitter', $twitterRedirectUrl); if (is_array($response) && isset($response['message']) && $response['message'] == 'register' && (!isset($response['url']) || $response['url'] == '')) { $redirectTo = esc_attr(the_champ_get_login_redirection_url($twitterRedirectUrl, true)); } elseif (isset($response['message']) && $response['message'] == 'linked') { $redirectTo = $twitterRedirectUrl . (strpos($twitterRedirectUrl, '?') !== false ? '&' : '?') . 'linked=1'; } elseif (isset($response['message']) && $response['message'] == 'not linked') { $redirectTo = $twitterRedirectUrl . (strpos($twitterRedirectUrl, '?') !== false ? '&' : '?') . 'linked=0'; } elseif (isset($response['url']) && $response['url'] != '') { $redirectTo = $response['url']; } else { $redirectTo = esc_attr(the_champ_get_login_redirection_url($twitterRedirectUrl)); } the_champ_close_login_popup($redirectTo); } } }