<commit_before><?php

namespace App\Http\Controllers\Login;

use App\Classes\SMFHelper;
use App\Helpers\EmailHelper;
use App\Helpers\ULSHelper;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Classes\OAuth\SSO;

/**
 * Class SSOController
 * @package App\Http\Controllers\Login
 */
class SSOController extends Controller
{
    /**
     * @var SSO
     */
    private $sso;

    /**
     * SSOController constructor.
     */
    public function __construct() {
        $this->sso = new SSO(config('sso.base'),
                             config('sso.key'),
                             config('sso.secret'),
                             config('sso.method'),
                             config('sso.cert'),
                             ['allow_suspended' => false, 'allow_inactive' => false]
                            );
    }

    /**
     * @param Request $request
     */
    public function getIndex(Request $request) {
        require_once(config('sso.forumapi',''));
        if ($request->has("logout")) {
            smfapi_logout();
            \Auth::logout();
            if (isset($_SERVER['HTTP_REFERER'])) {
                header("Location: " . $_SERVER['HTTP_REFERER']);
            } else {
                header("Location: https://www.vatusa.net");
            }
            return;
        }

        /* Lots to check here ... but this is our multi-point redirect */
        if ($request->has('home')) {
            $request->session()->put('return', env('SSO_RETURN_HOME'));
        } elseif ($request->has('homedev')) {
            $request->session()->put('return', env('SSO_RETURN_HOMEDEV'));
        } elseif ($request->has('forums')) {
            $request->session()->put('return', env('SSO_RETURN_FORUMS'));
        } elseif ($request->has('localdev')) {
            $request->session()->put('return', env('SSO_RETURN_LOCALDEV'));
        } elseif ($request->has('uls')) {
            $request->session()->put('return', env('SSO_RETURN_ULS'));
        } else {
            $request->session()->put('return', env('SSO_RETURN_FORUMS'));
        }

        /* If already logged in, don't send to SSO */
        if (\Auth::check()) {
            $return = $request->session()->get("return");
            $request->session()->forget("return");
            ULSHelper::doHandleLogin(\Auth::user()->cid, $return);
            return;
        }

        $this->sso->login(
            config('sso.return'),
            function($key, $secret, $url) use ($request) {
                $request->session()->put("SSO", ['key' => $key, 'secret' => $secret]);
                \Log::info("Got key " . session('SSO_key'));

                header("Location: $url");
                exit;
            }
        );
    }

    public function getReturn(Request $request) {
        if (isset($_REQUEST['cancel'])) {
            $request->session()->forget("SSO");
            $request->session()->forget("return");
            echo "Login request cancelled."; exit;
        }
        $sso = $request->session()->get("SSO");
        \Log::info("Got key " . json_encode($sso));
        $this->sso->validate(
            $sso['key'],
            $sso['secret'],
            $request->input('oauth_verifier'),
            function($user, $request) {
                session()->forget("SSO");
                $return = session("return", env("SSO_RETURN_FORUMS"));
                session()->forget("return");

                // Check if user is registered in forums...
                if (SMFHelper::isRegistered($user->id)) {
                    SMFHelper::updateData($user->id, $user->name_last, $user->name_first, $user->email);
                    SMFHelper::setPermissions($user->id);
                } else {
                    $r = randomPassword();
                    $regOptions = [
                        'member_name' => $user->id,
                        'real_name' => $user->name_first . " " . $user->name_first,
                        'email' => $user->email,
                        'password' => $r,
                        'password_check' => $r,
                        'send_welcome_email' => false,
                        'require' => 'nothing'
                    ];
                    $r = smfapi_registerMember($regOptions);
                    if (is_array($r) || $r == false) {
                        \Log::warning("Failed to create new user: " . base64_encode(serialize($r)) . ", " . base64_encode(serialize($regOptions)));
                        throw new \Exception("Failed to create forum user");
                    }
                }
                $member = User::find($user->id);
                if (!$member) {
                    $member = new User();
                    $member->cid = $user->id;
                    $member->email = $user->email;
                    $member->fname = $user->name_first;
                    $member->lname = $user->name_last;
                    $member->rating = $user->rating->id;
                    $member->facility = (($user->division->code == "USA") ? "ZAE" : "ZZN");
                    $member->facility_join = \DB::raw("NOW()");
                    $member->flag_needbasic = 1;
                    $member->flag_xferOverride = 0;
                    $member->flag_homecontroller = (($user->division->code == "USA") ? 1 : 0);
                    $member->save();

                    if ($member->flag_homecontroller) {
                        EmailHelper::sendEmail($member->email, "Welcome to VATUSA", "emails.user.join", []);
                    }
                }

                ULSHelper::doHandleLogin($user->id, $return);
            }
        );
    }
}
<commit_msg>add more debug info<commit_after><?php

namespace App\Http\Controllers\Login;

use App\Classes\SMFHelper;
use App\Helpers\EmailHelper;
use App\Helpers\ULSHelper;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Classes\OAuth\SSO;

/**
 * Class SSOController
 * @package App\Http\Controllers\Login
 */
class SSOController extends Controller
{
    /**
     * @var SSO
     */
    private $sso;

    /**
     * SSOController constructor.
     */
    public function __construct() {
        $this->sso = new SSO(config('sso.base'),
                             config('sso.key'),
                             config('sso.secret'),
                             config('sso.method'),
                             config('sso.cert'),
                             ['allow_suspended' => false, 'allow_inactive' => false]
                            );
    }

    /**
     * @param Request $request
     */
    public function getIndex(Request $request) {
        require_once(config('sso.forumapi',''));
        if ($request->has("logout")) {
            smfapi_logout();
            \Auth::logout();
            if (isset($_SERVER['HTTP_REFERER'])) {
                header("Location: " . $_SERVER['HTTP_REFERER']);
            } else {
                header("Location: https://www.vatusa.net");
            }
            return;
        }

        /* Lots to check here ... but this is our multi-point redirect */
        if ($request->has('home')) {
            $request->session()->put('return', env('SSO_RETURN_HOME'));
        } elseif ($request->has('homedev')) {
            $request->session()->put('return', env('SSO_RETURN_HOMEDEV'));
        } elseif ($request->has('forums')) {
            $request->session()->put('return', env('SSO_RETURN_FORUMS'));
        } elseif ($request->has('localdev')) {
            $request->session()->put('return', env('SSO_RETURN_LOCALDEV'));
        } elseif ($request->has('uls')) {
            $request->session()->put('return', env('SSO_RETURN_ULS'));
        } else {
            $request->session()->put('return', env('SSO_RETURN_FORUMS'));
        }

        /* If already logged in, don't send to SSO */
        if (\Auth::check()) {
            $return = $request->session()->get("return");
            $request->session()->forget("return");
            ULSHelper::doHandleLogin(\Auth::user()->cid, $return);
            return;
        }

        $this->sso->login(
            config('sso.return'),
            function($key, $secret, $url) use ($request) {
                $request->session()->put("SSO", ['key' => $key, 'secret' => $secret]);
                $request->session()->save();
                \Log::info("Got key " . json_encode($request->session()->get("SSO")));
                \Log::info("Session info " . json_encode($request->session()->all()));
                header("Location: $url");
                exit;
            }
        );
    }

    public function getReturn(Request $request) {
        if (isset($_REQUEST['cancel'])) {
            $request->session()->forget("SSO");
            $request->session()->forget("return");
            echo "Login request cancelled."; exit;
        }
        $sso = $request->session()->get("SSO");
        \Log::info("Got key " . json_encode($sso));
        \Log::info("Session info " . json_encode($request->session()->all()));
        $this->sso->validate(
            $sso['key'],
            $sso['secret'],
            $request->input('oauth_verifier'),
            function($user, $request) {
                session()->forget("SSO");
                $return = session("return", env("SSO_RETURN_FORUMS"));
                session()->forget("return");

                // Check if user is registered in forums...
                if (SMFHelper::isRegistered($user->id)) {
                    SMFHelper::updateData($user->id, $user->name_last, $user->name_first, $user->email);
                    SMFHelper::setPermissions($user->id);
                } else {
                    $r = randomPassword();
                    $regOptions = [
                        'member_name' => $user->id,
                        'real_name' => $user->name_first . " " . $user->name_first,
                        'email' => $user->email,
                        'password' => $r,
                        'password_check' => $r,
                        'send_welcome_email' => false,
                        'require' => 'nothing'
                    ];
                    $r = smfapi_registerMember($regOptions);
                    if (is_array($r) || $r == false) {
                        \Log::warning("Failed to create new user: " . base64_encode(serialize($r)) . ", " . base64_encode(serialize($regOptions)));
                        throw new \Exception("Failed to create forum user");
                    }
                }
                $member = User::find($user->id);
                if (!$member) {
                    $member = new User();
                    $member->cid = $user->id;
                    $member->email = $user->email;
                    $member->fname = $user->name_first;
                    $member->lname = $user->name_last;
                    $member->rating = $user->rating->id;
                    $member->facility = (($user->division->code == "USA") ? "ZAE" : "ZZN");
                    $member->facility_join = \DB::raw("NOW()");
                    $member->flag_needbasic = 1;
                    $member->flag_xferOverride = 0;
                    $member->flag_homecontroller = (($user->division->code == "USA") ? 1 : 0);
                    $member->save();

                    if ($member->flag_homecontroller) {
                        EmailHelper::sendEmail($member->email, "Welcome to VATUSA", "emails.user.join", []);
                    }
                }

                ULSHelper::doHandleLogin($user->id, $return);
            }
        );
    }
}

{'fraction_non_alphanumeric': 0.16918195849768478, 'fraction_numerical': 0.0013719773623735209, 'mean_word_length': 1.5587977182974988, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 181, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '403455', 'n_tokens_mistral': 3238, 'n_tokens_neox': 3123, 'n_words': 524}