Exemplo n.º 1
0
 /**
  * Generate a fake member account
  *
  * @param   array   $values Generator form values
  * @return  string  Progress message
  */
 public function generateSingle(array $values)
 {
     /* Create Member */
     $member = new \IPS\Member();
     $member->name = $values['__generator_message'] = $this->generator->userName();
     $password = isset($values['password']) ? $member->name : $this->generator->password();
     $member->email = $this->generator->email();
     $member->members_pass_salt = $member->generateSalt();
     $member->members_pass_hash = $member->encryptedPassword($password);
     $member->allow_admin_mails = 0;
     $member->member_group_id = $values['member_group'];
     $member->members_bitoptions['view_sigs'] = TRUE;
     if ($values['profile_photo']) {
         $photoUrl = new \IPS\Http\Url($this->generator->photoUrl());
         $response = $photoUrl->request()->get();
         $filename = preg_replace("/(.+?)(\\?|\$)/", "\$1", mb_substr((string) $photoUrl, mb_strrpos((string) $photoUrl, '/') + 1));
         $photoFile = \IPS\File::create('core_Profile', $filename, $response);
         $member->pp_photo_type = 'custom';
         $member->pp_main_photo = NULL;
         $member->pp_main_photo = (string) $photoFile;
         $thumbnail = $photoFile->thumbnail('core_Profile', \IPS\PHOTO_THUMBNAIL_SIZE, \IPS\PHOTO_THUMBNAIL_SIZE, TRUE);
         $member->pp_thumb_photo = (string) $thumbnail;
     }
     $member->save();
     $this->map(static::$activeRecordClass, $member->member_id);
     return \IPS\Member::loggedIn()->language()->addToStack(static::$message, true, array('sprintf' => array($member->name)));
 }
Exemplo n.º 2
0
	/**
	 * Authenticate
	 *
	 * @param	string			$url	The URL for the login page
	 * @param	\IPS\Member		$member	If we want to integrate this login method with an existing member, provide the member object
	 * @return	\IPS\Member
	 * @throws	\IPS\Login\Exception
	 */
	public function authenticate( $url, $member=NULL )
	{
		$url = $url->setQueryString( 'loginProcess', 'vk' );

		try
		{
			/* CSRF Check */
			if ( \IPS\Request::i()->state !== \IPS\Session::i()->csrfKey )
			{
				throw new \IPS\Login\Exception( 'CSRF_FAIL', \IPS\Login\Exception::INTERNAL_ERROR );
			}

			if(isset(\IPS\Request::i()->error) || !isset(\IPS\Request::i()->code)) {
				throw new \IPS\Login\Exception( 'generic_error', \IPS\Login\Exception::INTERNAL_ERROR );
			}
			
			/* Get a token */
			try
			{
				$response = \IPS\Http\Url::external( "https://oauth.vk.com/access_token" )->setQueryString( array(
					'client_id'		=> $this->settings['app_id'],
					'redirect_uri'	=> (string) \IPS\Http\Url::internal( 'applications/core/interface/vk/auth.php', 'none' ),
					'client_secret'	=> $this->settings['app_secret'],
					'code'			=> \IPS\Request::i()->code
				) )->request()->Get()->decodeJson();

				if(isset($response['error'])) {
					throw new \IPS\Login\Exception( 'generic_error', \IPS\Login\Exception::INTERNAL_ERROR );
				}
			}
			catch( \RuntimeException $e )
			{
				throw new \IPS\Login\Exception( 'generic_error', \IPS\Login\Exception::INTERNAL_ERROR );
			}

			/* Get the user data */
			$userData = \IPS\Http\Url::external( "https://api.vk.com/method/getProfiles?uid={$response['user_id']}&access_token={$response['access_token']}&fields=first_name,last_name,screen_name,bdate,nickname" )->request()->get()->decodeJson();
			$userData = $userData['response'][0];

   			/* Find or create member */
   			$newMember = FALSE;
   			if ( $member === NULL )
   			{
				$member = \IPS\Member::load( $response['user_id'], 'vk_id' );
				if ( !$member->member_id )
				{
					if(isset($response['email'])) {
						$existingEmail = \IPS\Member::load( $response['email'], 'email' );
						if ( $existingEmail->member_id )
						{
							$exception = new \IPS\Login\Exception( 'generic_error', \IPS\Login\Exception::MERGE_SOCIAL_ACCOUNT );
							$exception->handler = 'vk';
							$exception->member = $existingEmail;
							$exception->details = array($response['access_token'], $response['user_id']);
							throw $exception;
						}
					}
					
					$member = new \IPS\Member;
					if ( \IPS\Settings::i()->reg_auth_type == 'admin' or \IPS\Settings::i()->reg_auth_type == 'admin_user' )
					{
						$member->members_bitoptions['validating'] = TRUE;
					}
					$member->member_group_id = \IPS\Settings::i()->member_group;
					$member->email = isset($response['email'])?$response['email']:'';

					$member->name = $userData['nickname'];

					if ( empty($member->name) AND $this->settings['real_name'] )
					{
						$name = $userData['first_name'] . ' ' . $userData['last_name'];
						$existingUsername = \IPS\Member::load( $name, 'name' );
						
						if ( !$existingUsername->member_id )
						{
							$member->name = $name;
						}
					}
					$member->profilesync = json_encode( array( 'vk' => array( 'photo' => TRUE, 'status' => '' ) ) );
					$newMember = TRUE;
				}
			}

			/* Update details */
			$member->vk_id = $response['user_id'];
			$member->vk_token = $response['access_token'];
			$member->save();
			
			/* Sync */
			if ( $newMember )
			{
				if ( \IPS\Settings::i()->reg_auth_type == 'admin_user' )
				{
					\IPS\Db::i()->update( 'core_validating', array( 'user_verified' => 1 ), array( 'member_id=?', $member->member_id ) );
				}
				
				$sync = new \IPS\core\ProfileSync\VK( $member );
				$sync->sync();
			}
			
			/* Return */
			return $member;
   		}
   		catch ( \IPS\Http\Request\Exception $e )
   		{
	   		throw new \IPS\Login\Exception( 'generic_error', \IPS\Login\Exception::INTERNAL_ERROR );
   		}
	}
 /**
  * Authenticate
  *
  * @param    string      $url    The URL for the login page
  * @param    \IPS\Member $member If we want to integrate this login method with an existing member, provide the
  *                               member object
  *
  * @return    \IPS\Member
  * @throws    \IPS\Login\Exception
  */
 public function authenticate($url, $member = null)
 {
     try {
         $steamId = $this->validate();
         if (!$steamId) {
             throw new \IPS\Login\Exception('generic_error', \IPS\Login\Exception::INTERNAL_ERROR);
         }
         /* If an api key is provided, attempt to load the user from steam */
         $response = null;
         $userData = null;
         if ($this->settings['api_key']) {
             try {
                 $response = \IPS\Http\Url::external("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key={$this->settings['api_key']}&steamids={$steamId}")->request()->get()->decodeJson();
                 if ($response) {
                     // Get the first player
                     $userData = $response['response']['players'][0];
                 }
             } catch (\IPS\Http\Request\Exception $e) {
                 throw new \IPS\Login\Exception('generic_error', \IPS\Login\Exception::INTERNAL_ERROR, $e);
             }
         }
         /* Find  member */
         $newMember = false;
         if ($member === null) {
             try {
                 $memberData = \IPS\Db::i()->select('*', 'core_members', array('steamid=?', $steamId))->first();
                 $member = \IPS\Member::constructFromData($memberData);
             } catch (\UnderflowException $e) {
                 $member = \IPS\Member::load(null);
             }
             if (!$member->member_id) {
                 if ($this->settings['api_key']) {
                     try {
                         $gameslist = \IPS\Http\Url::external("http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key={$this->settings['api_key']}&steamid={$steamId}")->request()->get()->decodeJson();
                         $hasgmod = false;
                         if (!isset($gameslist) or !isset($gameslist['response']) or !isset($gameslist['response']['games'])) {
                             throw new \IPS\Login\Exception('Your Steam account is private and we can\'t tell if you own Garry\'s Mod. Please change your game library to public!', \IPS\Login\Exception::NO_ACCOUNT, $e);
                         }
                         foreach ($gameslist['response']['games'] as $game) {
                             if ($game['appid'] == 4000) {
                                 $hasgmod = true;
                             }
                         }
                         if (!$hasgmod) {
                             throw new \IPS\Login\Exception('Your Steam account must own Garry\'s Mod to login.', \IPS\Login\Exception::NO_ACCOUNT, $e);
                         }
                     } catch (\IPS\Http\Request\Exception $e) {
                         throw new \IPS\Login\Exception('generic_error', \IPS\Login\Exception::INTERNAL_ERROR, $e);
                     }
                 }
                 $member = new \IPS\Member();
                 $member->member_group_id = \IPS\Settings::i()->member_group;
                 if (\IPS\Settings::i()->reg_auth_type == 'admin' or \IPS\Settings::i()->reg_auth_type == 'admin_user') {
                     $member->members_bitoptions['validating'] = true;
                 }
                 if (isset($userData)) {
                     if ($this->settings['use_steam_name']) {
                         $existingUsername = \IPS\Member::load($userData['personaname'], 'name');
                         if (!$existingUsername->member_id) {
                             $member->name = $userData['personaname'];
                         }
                     }
                     $member->profilesync = json_encode(array(static::$loginKey => array('photo' => true, 'cover' => false, 'status' => '')));
                 }
                 $newMember = true;
             }
         }
         /* Create member */
         $member->steamid = $steamId;
         $member->save();
         /* Sync */
         if ($newMember) {
             if (\IPS\Settings::i()->reg_auth_type == 'admin_user') {
                 \IPS\Db::i()->update('core_validating', array('user_verified' => 1), array('member_id=?', $member->member_id));
             }
             $sync = new \IPS\core\ProfileSync\Steam($member);
             $sync->sync();
         }
         /* Return */
         return $member;
     } catch (\IPS\Http\Request\Exception $e) {
         throw new \IPS\Login\Exception('generic_error', \IPS\Login\Exception::INTERNAL_ERROR);
     }
 }