• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

´Cosmic 2.1 ~ Facebook Connect & Forgot Password

Newbie Spellweaver
Joined
Jun 24, 2012
Messages
71
Reaction score
14


| Let's Get Started
Install facebook/graph-sdk with composer, with cmd locate your CMS and run the following command:

Code:
composer require facebook/graph-sdk

| Step 1 - Add Facebook App Configuration

Let's go to App/Config.php

And right below 'const site' we add up this facebook array:

PHP:
    /**
     * Website settings
     * recaptcha v2: Create a inisible reCAPTCHA V2       [USER=848466]GoOgl[/USER]e website
     * Set null if you dont want a reCAPTCHA.
     */

    const site = array(
      'domain'      => 'Kubbo.me',
      'cpath'       => 'https://swfs.kubbo.us',
      'fpath'       => 'https://habbo.com.br/habbo-imaging',
      'shortname'   => 'Kubbo',
      'sitename'    => 'Kubbo'
    );

    const facebook = array(
      'id'          => 'YOUR APP ID',
      'secret_key'  => 'YOUR SECRET KEY'
    );
      
    const language = 'ES';

| Step 2 - Adding Facebook Id to our Users Table
On your database, edit table users and add column fb_id

Just like so:
LOSFT6a - ´Cosmic 2.1 ~ Facebook Connect & Forgot Password - RaGEZONE Forums

| Step 3 - Adding up Facebook Button to Login

On App/Controllers/Home/Index.php file let's add our new Facebook Array to our rendered template:

PHP:
        View::renderTemplate('Home/home.html', [
            'title'     => !request()->player ? Locale::get('core/title/home') : request()->player->username,
            'page'      => 'home',
            'rooms'     => $rooms,
            'groups'    => $groups,
            'news'      => $news,
            'oftw'      => $oftw,
            'currencys' => isset($currencys) ? $currencys : null,
            'random'    => isset($random) ? $random : null,
            'facebook'  => Config::facebook
        ], 10);

With this, we have access to write {{facebook.X}} on our HTMLs files.

Then we go to App/View/base.html file and we add the next script

Code:
<div id="fb-root"></div>
<script type="text/javascript">
        	function DeleteCookie(name)
        	{
        		document.cookie = name + '=; Max-Age=0';
        	}
        	
        	function SetCookie(cname, cvalue, exdays)
        	{
        		var d = new Date();
        		d.setTime(d.getTime() + (exdays*24*60*60*1000));
        		var expires = "expires="+d.toUTCString();
        		document.cookie = cname + "=" + cvalue + "; " + expires;
        	}
        
            window.fbAsyncInit = function() {
        		DeleteCookie("fbhb_val_{{facebook.id}}");
                FB.init({
                    appId: "{{facebook.id}}",
                    version    : 'v2.4',
                    channelUrl : '/fbchannel',
                    status: true,
                    cookie: true,
                    xfbml: true
                });
        
        
                $(document).fire("fbevents:scriptLoaded");
            };
            window.assistedLogin = function(FBobject, optresponse) {
        		DeleteCookie("fbhb_val_{{facebook.id}}");
                FBobject.init({
                    appId: '{{facebook.id}}',
                    version    : 'v2.4',
                    channelUrl : '/fbchannel',
                    status: true,
                    cookie: true,
                    xfbml: true
                });
        
                permissions = 'email';
                defaultAction = function(response) {
                    if (response.authResponse) {
                        fbConnectUrl = "/facebook?connect=true";
                        DeleteCookie("fbhb_val_{{facebook.id}}");
                        SetCookie("fbhb_val_{{facebook.id}}", response.authResponse.accessToken, 1);
                        DeleteCookie("fbhb_expr_{{facebook.id}}");
                        SetCookie("fbhb_expr_{{facebook.id}}", response.authResponse.expiresIn, 1);
                        window.location.replace(fbConnectUrl);
                    }
                };
        
                if (typeof optresponse == 'undefined')
                    FBobject.login(defaultAction, {scope:permissions});
                else
                    FBobject.login(optresponse, {scope:permissions});
        
            };
        
            (function() {
                var e = document.createElement('script');
                e.async = true;
                e.src = document.location.protocol + '//connect.facebook.net/es_ES/sdk.js#version=v2.4';
                document.getElementById('fb-root').appendChild(e);
            }());
        
        </script>

And right below our Login button, add your facebook connect button:
PHP:
<button type="submit" class="rounded-button blue plain" id="login-request">Log in</button>
<button class="rounded-button blue plain" onclick="assistedLogin(FB); return false; "  id="fb-login-request">Login with Facebook!</button>

| Step 3 - Facebook Route
Cosmic already has this route coded, although it doesn't work

You may see this on Core/Routes.php

PHP:
Router::get('/facebook', 'Home\Login@facebook');

This is calling facebook() function on Login Controller.

| Step 3 - Facebook Function

Create facebook() function on App/Controllers/Home/Login.php file below googleAuthentication function

This part can be modified with medium level PHP knowledge, this function what it does is:

Logs In or Registers the user depending if the user exists or doesn't.

PHP:
public function facebook()
    {
        $settings = Core::settings();

        $fb = new Facebook([
            'app_id' => Config::facebook['id'],
            'app_secret' => Config::facebook['secret_key'],
            'default_graph_version' => 'v2.10'
        ]);
        
        $response = $fb->get('/me?fields=id,name,email,gender,birthday', $_COOKIE['fbhb_val_' . Config::facebook['id']]);

        $user_profile = $response->getGraphObject()->asArray();

        $player = Player::getDataByFacebookID($user_profile['id'], array('id', 'username', 'password', 'rank', 'secret_key', 'pincode'));

        if($player == null)
        {
            $getMaxIp = Player::checkMaxIp(request()->getIp());

            if ($getMaxIp != 0 && $getMaxIp >= $settings->registration_max_ip) {
                response()->json(["status" => "error", "message" => Locale::get('register/too_many_accounts')]);
            }


           $email = isset($user_profile['email']) ? $user_profile['email'] : $user_profile['id'] . '@facebook.com';

            if (isset($user_profile['gender']) && $user_profile['gender'] === 'male')
            {
                $gender = 'M';
                $look = 'ch-210-1211.sh-300-63.lg-285-64.ha-1002-63.hr-828-1061.hd-180-1';
            }
            else
            {
                $gender = 'F';
                $look = 'hd-600-1.ch-813-1211.ha-1005-63.lg-720-64.hr-890-1061.sh-735-63';
            }

            $playerData = (object)[
                'username' => Player::GenerateName($email),
                'fb_id' => $user_profile['id'],
                'password' => '',
                'email' => '',
                'account_created' => time(),
                'credits' => Core::settings()->start_credits,
                'figure' => $look,
                'birthdate_day' => 17,
                'birthdate_month' => 06,
                'birthdate_year' => 2020,
                'gender' => $gender,
                'last_login' => time(),
                'ip_register' => request()->getIp(),
                'ip_current' => request()->getIp()
            ];

            if (!Player::create($playerData)) {
                response()->json(["status" => "error", "message" => Locale::get('core/notification/something_wrong'), "captcha_error" => "error"]);
            }

            $player = Player::getDataByFacebookID($user_profile['id'], array('id', 'username', 'password', 'rank', 'secret_key', 'pincode'));

            $freeCurrencys = Core::getCurrencys();
      
            if($freeCurrencys) {
                foreach($freeCurrencys as $currency) {
                    Player::createCurrency($player->id, $currency->type);
                    Player::updateCurrency($player->id, $currency->type, $currency->amount);
                }
            }
        }

        Auth::login($player);
        redirect('/home');
    }

Do not forget to import facebook/graph-sdk and use it on the controller:

PHP:
...
use Library\Json;
use Library\HotelApi;

use Facebook\Facebook;

use Sonata\GoogleAuthenticator\GoogleAuthenticator;
...


Also I forgot, on the same imports:

You can delete:
PHP:
use App\Core;

And add:
PHP:
use App\Models\Core;

Because Facebook Function uses:
PHP:
$settings = Core::settings();

Reading the code, we can see that we are using some functions of Player Model that do not exist yet such as:
Player::getDataByFacebookID() or Player::GenerateName()

| Step 4 - Players Model Modification

Now we are going to edit App/Models/Player.php file

Add a function to search user by using the fb_id on Step 2.

PHP:
    public static function getDataByFacebookID($fb_id, $data = null)
    {
        return QueryBuilder::table('users')->select($data ?? static::$data)->setFetchMode(PDO::FETCH_CLASS, get_called_class())->where('fb_id', $fb_id)->first();
    }

Add a function to Generate a Random Name based on Facebook Email Address
PHP:
    public static function GenerateName($e, $t = 0)
	{
		$e = $E = substr(strstr($e, '@') ? explode('@', $e)[0] : $e, 0, 18);
		$c = ['.', '-', '_'];

		if (strlen($e) > 6)
			foreach ($c as $C)
			{
				if (strstr($e, $C) === FALSE) continue;
				$n = rand(0, 1);
				$e = explode($C, $e);
				$e = strlen($e[$n]) < 3 ? ($e[($n === 1) ? 0 : 1]) : $e[$n];
				break;
			}
		
		$r = self::exists($e);

		if ($r === null)	return null;
		if ($r === false)	return $e;
		
		$c = strlen($e);
		$r = ($c > 10) ? rand(rand(1, 2 + $t), rand(2, 3 + $t)) : ($c > 8) ? rand(rand(2, 2 + $t), rand(3, 3 + $t)) : rand(rand(3, 4 + $t), rand(3, 4 + $t));

		$e .= self::Random($r, true, false, '-_=?!@:.,');
		$r = self::exists($e);
		
		if ($r === null)	return null;
		if ($r === false)	return $e;

		self::GenerateName($E, $t++);
	}

PHP:
    public static function Random($l, $n = true, $L = true, $o = '')
	{
		$c = '';
		$c .= $n ? '0123456789' : '';
		$c .= $L ? 'QWERTYUIOPASDFGHJKLLZXCVBNMqwertyuiopasdfghjklzxcvbnm' : '';
		$c .= $o;

		$s = '';
		$C = 0;
		while ($C < $l)
		{ 
			$s .= substr($c, rand(0, strlen($c) -1), 1);
			$C++;
		}

		return $s;
	}

Modify function create($data) to add fb_id in case of Facebook Registration

PHP:
public static function create($data)
    {
        $data = array(
            'username' => $data->username,
            'password' => Hash::password($data->password),
            'mail' => $data->email,
            'fb_id' => $data->fb_id,
            'account_created' => time(),
            ....

| Step 5 - Registration File Modification

Since now we are requiring an extra field on the create function, let's just add it on the $playerData in App/Controllers/Home/Registration.php file

PHP:
$playerData = (object)input()->all();
$playerData->figure = input()->post('figure')->value;
$playerData->fb_id = '';
$getMaxIp = Player::checkMaxIp(request()->getIp());

| Notes

This approach doesn't give user an email to connect via Cosmic Login Process nor a password. Doesn't check if facebook email is already registered on Cosmic users simply doesn't give Facebook Account an email. I would suggest to modify facebook function to add code to enable "can_change_name" on Arcturus users_settings. And I guess that's it. Any issues or suggestions just comment on the section below. Thank you!


| Forgot Password Tips

Set up your App/Config.php file, only change your support email and the password

PHP:
    const mailHost      = 'smtp.gmail.com';
    const mailFrom      = 'SUPPORT-EMAIL@gmail.com';
    const mailUser      = 'SUPPORT-EMAIL@gmail.com';
    const mailPass      = 'YOUR-GMAIL-PASSWORD';
    const mailPort      = 465;

Then log into your GMAIL and allow less secure apps

 

Attachments

You must be registered for see attachments list
Last edited:
Initiate Mage
Joined
Aug 23, 2021
Messages
1
Reaction score
0
Is it still working? I am in a very frustrating situation now. I have forgotten my Facebook password. It was always saved on my google account information, so I never had to write it. Now that I deleted some of my data from my google account, I accidentally deleted my passwords, and now I can't log in to many of my accounts. It is a big problem because I use a lot of for promoting my business, and losing the passwords from them is a big problem, so I need to recover the passwords as soon as possible.
 
Last edited:
Back
Top