Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Uses code from ichimonai.com's Steam Community API
- // Uses code from compwhizii's Twitter auth for xenforo
- class Dark_SteamLogin_ControllerPublic_Kyouko extends XFCP_Dark_SteamLogin_ControllerPublic_Kyouko {
- public function actionSteam(){
- $assocUserId = $this->_input->filterSingle('assoc', XenForo_Input::UINT);
- $redirect = $this->_input->filterSingle('redirect', XenForo_Input::STRING);
- a
- /** @var XenForo_Session */
- $session = XenForo_Application::get('session');
- $callbackUri = XenForo_Link::buildPublicLink('canonical:register/steam', false, array(
- 'redirect' => $this->getDynamicRedirect()
- ));
- if ($this->_input->filterSingle('reg', XenForo_Input::UINT))
- {
- return $this->responseRedirect(
- XenForo_ControllerResponse_Redirect::SUCCESS,
- $this->_genUrl($callbackUri, false)
- );
- }
- $id = $this->_validate();
- if(empty($id))
- return $this->responseError('Invalid login. Is your Steam profile public?');
- $session->set('steam_id', $id);
- $userModel = $this->_getUserModel();
- $userExternalModel = $this->_getUserExternalModel();
- $assoc = $userExternalModel->getExternalAuthAssociation('steam', $id);
- if ($assoc && $userModel->getUserById($assoc['user_id']))
- {
- XenForo_Application::get('session')->changeUserId($assoc['user_id']);
- XenForo_Visitor::setup($assoc['user_id']);
- return $this->responseRedirect(
- XenForo_ControllerResponse_Redirect::SUCCESS,
- $this->getDynamicRedirect(false, false)
- );
- }
- $existingUser = false;
- if (XenForo_Visitor::getUserId())
- {
- $existingUser = XenForo_Visitor::getInstance();
- }
- else if ($assocUserId)
- {
- $existingUser = $userModel->getUserById($assocUserId);
- }
- if ($existingUser)
- {
- return $this->responseView('XenForo_ViewPublic_Register_Twitter', 'register_twitter', array(
- 'associateOnly' => true,
- 'existingUser' => $existingUser,
- 'redirect' => $redirect
- ));
- }
- if (!XenForo_Application::get('options')->get('registrationSetup', 'enabled'))
- {
- $this->_assertRegistrationActive();
- }
- $username = "";
- $xml = simplexml_load_file("http://steamcommunity.com/profiles/{$id}/?xml=1");
- if(!empty($xml)){
- $username = $xml->steamID;
- }
- // give a unique username suggestion
- $i = 2;
- $origName = $username;
- while ($userModel->getUserByName($username))
- {
- $username = $origName . ' ' . $i++;
- }
- return $this->responseView('XenForo_ViewPublic_Register_Steam', 'dark_register_steam', array(
- 'username' => $username,
- 'redirect' => $redirect,
- 'timeZones' => XenForo_Helper_TimeZone::getTimeZones(),
- 'tosUrl' => XenForo_Dependencies_Public::getTosUrl()
- ), $this->_getRegistrationContainerParams());
- }
- public function actionSteamRegister(){
- $this->_assertPostOnly();
- /** @var XenForo_Session */
- $session = XenForo_Application::get('session');
- if(!$session->get('steam_id')){
- return $this->responseError('Lost Steam ID');
- }
- $id = $session->get('steam_id');
- $xml = simplexml_load_file("http://steamcommunity.com/profiles/{$id}/?xml=1");
- if(!empty($xml)){
- $username = $xml->steamID;
- $avatar = $xml->avatarFull;
- }
- $userModel = $this->_getUserModel();
- $userExternalModel = $this->_getUserExternalModel();
- $doAssoc = ($this->_input->filterSingle('associate', XenForo_Input::STRING)
- || $this->_input->filterSingle('force_assoc', XenForo_Input::UINT)
- );
- if ($doAssoc)
- {
- $associate = $this->_input->filter(array(
- 'associate_login' => XenForo_Input::STRING,
- 'associate_password' => XenForo_Input::STRING
- ));
- $loginModel = $this->_getLoginModel();
- if ($loginModel->requireLoginCaptcha($associate['associate_login']))
- {
- return $this->responseError(new XenForo_Phrase('your_account_has_temporarily_been_locked_due_to_failed_login_attempts'));
- }
- $userId = $userModel->validateAuthentication($associate['associate_login'], $associate['associate_password'], $error);
- if (!$userId)
- {
- $loginModel->logLoginAttempt($associate['associate_login']);
- return $this->responseError($error);
- }
- /** @var XenForo_DataWriter_User */
- $writer = XenForo_DataWriter::create('XenForo_DataWriter_User');
- $writer->setExistingData(array('user_id' => $userId));
- $writer->setIdentity('Steam', $id);
- $writer->save();
- $userExternalModel->updateExternalAuthAssociation('steam', $id, $userId, false);
- $session->changeUserId($userId);
- XenForo_Visitor::setup($userId);
- return $this->responseRedirect(
- XenForo_ControllerResponse_Redirect::SUCCESS,
- $this->getDynamicRedirect(false, false)
- );
- }
- $this->_assertRegistrationActive();
- $data = $this->_input->filter(array(
- 'username' => XenForo_Input::STRING,
- 'timezone' => XenForo_Input::STRING,
- 'email' => XenForo_Input::STRING,
- 'gender' => XenForo_Input::STRING
- ));
- if (XenForo_Dependencies_Public::getTosUrl() && !$this->_input->filterSingle('agree', XenForo_Input::UINT))
- {
- return $this->responseError(new XenForo_Phrase('you_must_agree_to_terms_of_service'));
- }
- $options = XenForo_Application::get('options');
- /** @var XenForo_DataWriter_User */
- $writer = XenForo_DataWriter::create('XenForo_DataWriter_User');
- $writer->bulkSet($data);
- $auth = XenForo_Authentication_Abstract::create('XenForo_Authentication_NoPassword');
- $writer->set('scheme_class', $auth->getClassName());
- $writer->set('data', $auth->generate(''), 'xf_user_authenticate');
- $writer->set('user_group_id', XenForo_Model_User::$defaultRegisteredGroupId);
- $writer->set('language_id', XenForo_Visitor::getInstance()->get('language_id'));
- $writer->advanceRegistrationUserState(false);
- $writer->setIdentity('Steam', $id);
- $writer->preSave();
- $writer->save();
- $user = $writer->getMergedData();
- $userExternalModel->updateExternalAuthAssociation('steam', $id, $user['user_id'], false);
- if(!empty($avatar)){
- $avatarModel = $this->_getAvatarModel();
- $httpClient = XenForo_Helper_Http::getClient(preg_replace('/\s+/', '%20', $avatar));
- $response = $httpClient->request('GET');
- if($response->isSuccessful()){
- $avatarFile = tempnam(XenForo_Helper_File::getTempDir(), 'xf');
- file_put_contents($avatarFile, $response->getBody());
- }
- $avatarModel->applyAvatar($user['user_id'], $avatarFile);
- }
- $session->changeUserId($user['user_id']);
- XenForo_Visitor::setup($user['user_id']);
- $redirect = $this->_input->filterSingle('redirect', XenForo_Input::STRING);
- $viewParams = array(
- 'user' => $user,
- 'redirect' => ($redirect ? XenForo_Link::convertUriToAbsoluteUri($redirect) : ''),
- 'steam' => true
- );
- return $this->responseView(
- 'XenForo_ViewPublic_Register_Process',
- 'register_process',
- $viewParams,
- $this->_getRegistrationContainerParams()
- );
- }
- const STEAM_LOGIN = 'https://steamcommunity.com/openid/login';
- protected function _genUrl($returnTo = false, $useAmp = true)
- {
- $returnTo = (!$returnTo) ? (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] : $returnTo;
- $params = array(
- 'openid.ns' => 'http://specs.openid.net/auth/2.0',
- 'openid.mode' => 'checkid_setup',
- 'openid.return_to' => $returnTo,
- 'openid.realm' => (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'],
- 'openid.identity' => 'http://specs.openid.net/auth/2.0/identifier_select',
- 'openid.claimed_id' => 'http://specs.openid.net/auth/2.0/identifier_select',
- );
- $sep = ($useAmp) ? '&' : '&';
- return self::STEAM_LOGIN . '?' . http_build_query($params, '', $sep);
- }
- protected function _validate(){
- if(empty($_GET['openid_assoc_handle']))
- return false;
- // Start off with some basic params
- $params = array(
- 'openid.assoc_handle' => $_GET['openid_assoc_handle'],
- 'openid.signed' => $_GET['openid_signed'],
- 'openid.sig' => $_GET['openid_sig'],
- 'openid.ns' => 'http://specs.openid.net/auth/2.0',
- );
- // Get all the params that were sent back and resend them for validation
- $signed = explode(',', $_GET['openid_signed']);
- foreach($signed as $item)
- {
- $val = $_GET['openid_' . str_replace('.', '_', $item)];
- $params['openid.' . $item] = get_magic_quotes_gpc() ? stripslashes($val) : $val;
- }
- // Finally, add the all important mode.
- $params['openid.mode'] = 'check_authentication';
- // Stored to send a Content-Length header
- $data = http_build_query($params);
- $context = stream_context_create(array(
- 'http' => array(
- 'method' => 'POST',
- 'header' =>
- "Accept-language: en\r\n".
- "Content-type: application/x-www-form-urlencoded\r\n" .
- "Content-Length: " . strlen($data) . "\r\n",
- 'content' => $data,
- ),
- ));
- $result = file_get_contents(self::STEAM_LOGIN, false, $context);
- // Validate wheather it's true and if we have a good ID
- preg_match("#^http://steamcommunity.com/openid/id/([0-9]{17,25})#", $_GET['openid_claimed_id'], $matches);
- $steamID64 = is_numeric($matches[1]) ? $matches[1] : 0;
- // Return our final value
- return preg_match("#is_valid\s*:\s*true#i", $result) == 1 ? $steamID64 : '';
- }
- /**
- * @return XenForo_Model_Avatar
- */
- protected function _getAvatarModel()
- {
- return $this->getModelFromCache('XenForo_Model_Avatar');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement