<commit_before><?php

namespace app\controllers;

use app\models\User;
use ShortCirquit\LinkoScopeApi\ComLinkoScope;
use ShortCirquit\LinkoScopeApi\OrgLinkoScope;
use ShortCirquit\WordPressApi\ComWpApi;
use ShortCirquit\WordPressApi\OrgWpApi;
use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\helpers\Url;
use yii\web\HttpException;

class SiteController extends BaseController
{
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    public function actionIndex()
    {
        return $this->redirect('link/index');
    }

    public function actionAbout()
    {
        return $this->render('about');
    }

    public function actionLogout()
    {
        Yii::$app->user->logout();
        return $this->goHome();
    }

    public function actionLogin($code = null, $error = null, $oauth_token = null,
                                $oauth_verifier = null, $wp_scope = null)
    {
        $api = $this->getApi();
        if ($api == null) {
            Yii::$app->session->setFlash('error', 'The site is not configured yet.');
            $this->redirect(['admin/login']);
        }

        switch (get_class($api))
        {
            case 'ShortCirquit\LinkoScopeApi\ComLinkoScope':
                return $this->loginCom($code, $error);
            case 'ShortCirquit\LinkoScopeApi\OrgLinkoScope':
                return $this->loginOrg($oauth_token, $oauth_verifier, $wp_scope);
            default:
                throw new HttpException(500, 'Unexpected WpApi class type: ' . get_class($api));
        }
    }

    private function loginCom($code = null, $error = null)
    {
        if ($error != null) {
            throw new HttpException(301, "Error: $error");
        }

        if ($code == null) {
            return $this->redirect($this->getApi()->authorize());
        }

        $api = $this->getApi();
        $auth = $api->token($code);

        if (is_string($auth))
        {
            throw new HttpException(301, "Failed to get token with error: $auth");
        }

        $redirect = Yii::$app->session->get('login-com', false);
        if ($redirect !== false) {
            $cfg = [
                    'blogUrl' => $auth['blog_url'],
                    'blogId' => $auth['blog_id'],
                    'adminToken' => $auth['access_token'],
                ] + $api->getConfig();
            $api = new ComLinkoScope($cfg);
            $this->saveConfig($api);
            Yii::$app->session->setFlash('info', 'Successfully completed WP.com config for: ' . $auth['blog_url']);
            return $this->redirect([$redirect]);
        }

        $cfg = $api->getConfig() + ['token' => $auth['access_token']];
        $api = new ComLinkoScope($cfg);
        $account = $api->getAccount();

        $u = new User([
            'id' => $account->id,
            'username' => $account->username,
            'token' => $auth['access_token'],
        ]);

        $u->saveSessionAccount();
        Yii::$app->user->login($u);

        return $this->redirect(['link/index']);
    }

    private function loginOrg($oauth_token = null, $oauth_verifier = null, $wp_scope = null)
    {
        $api = $this->getApi();
        if ($api == null) {
            Yii::$app->session->setFlash('error', 'The site is not configured yet.');
            $this->redirect(['admin/login']);
        }

        if ($oauth_token == null) {
            $here = Url::to('', true);
            $redirect = $api->authorize($here);
            return $this->redirect($redirect);
        }

        $tok = $api->access($oauth_token, $oauth_verifier);
        $cfg = $api->getConfig();
        $cfg['accessToken'] = $tok['oauth_token'];
        $cfg['accessTokenSecret'] = $tok['oauth_token_secret'];<commit_msg>Bug fix: wrong variable name for token/secret<commit_after><?php

namespace app\controllers;

use app\models\User;
use ShortCirquit\LinkoScopeApi\ComLinkoScope;
use ShortCirquit\LinkoScopeApi\OrgLinkoScope;
use ShortCirquit\WordPressApi\ComWpApi;
use ShortCirquit\WordPressApi\OrgWpApi;
use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\helpers\Url;
use yii\web\HttpException;

class SiteController extends BaseController
{
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    public function actionIndex()
    {
        return $this->redirect('link/index');
    }

    public function actionAbout()
    {
        return $this->render('about');
    }

    public function actionLogout()
    {
        Yii::$app->user->logout();
        return $this->goHome();
    }

    public function actionLogin($code = null, $error = null, $oauth_token = null,
                                $oauth_verifier = null, $wp_scope = null)
    {
        $api = $this->getApi();
        if ($api == null) {
            Yii::$app->session->setFlash('error', 'The site is not configured yet.');
            $this->redirect(['admin/login']);
        }

        switch (get_class($api))
        {
            case 'ShortCirquit\LinkoScopeApi\ComLinkoScope':
                return $this->loginCom($code, $error);
            case 'ShortCirquit\LinkoScopeApi\OrgLinkoScope':
                return $this->loginOrg($oauth_token, $oauth_verifier, $wp_scope);
            default:
                throw new HttpException(500, 'Unexpected WpApi class type: ' . get_class($api));
        }
    }

    private function loginCom($code = null, $error = null)
    {
        if ($error != null) {
            throw new HttpException(301, "Error: $error");
        }

        if ($code == null) {
            return $this->redirect($this->getApi()->authorize());
        }

        $api = $this->getApi();
        $auth = $api->token($code);

        if (is_string($auth))
        {
            throw new HttpException(301, "Failed to get token with error: $auth");
        }

        $redirect = Yii::$app->session->get('login-com', false);
        if ($redirect !== false) {
            $cfg = [
                    'blogUrl' => $auth['blog_url'],
                    'blogId' => $auth['blog_id'],
                    'adminToken' => $auth['access_token'],
                ] + $api->getConfig();
            $api = new ComLinkoScope($cfg);
            $this->saveConfig($api);
            Yii::$app->session->setFlash('info', 'Successfully completed WP.com config for: ' . $auth['blog_url']);
            return $this->redirect([$redirect]);
        }

        $cfg = $api->getConfig() + ['token' => $auth['access_token']];
        $api = new ComLinkoScope($cfg);
        $account = $api->getAccount();

        $u = new User([
            'id' => $account->id,
            'username' => $account->username,
            'token' => $auth['access_token'],
        ]);

        $u->saveSessionAccount();
        Yii::$app->user->login($u);

        return $this->redirect(['link/index']);
    }

    private function loginOrg($oauth_token = null, $oauth_verifier = null, $wp_scope = null)
    {
        $api = $this->getApi();
        if ($api == null) {
            Yii::$app->session->setFlash('error', 'The site is not configured yet.');
            $this->redirect(['admin/login']);
        }

        if ($oauth_token == null) {
            $here = Url::to('', true);
            $redirect = $api->authorize($here);
            return $this->redirect($redirect);
        }

        $tok = $api->access($oauth_token, $oauth_verifier);
        $cfg = $api->getConfig();
        $cfg['token'] = $tok['oauth_token'];
        $cfg['tokenSecret'] = $tok['oauth_token_secret'];
{'fraction_non_alphanumeric': 0.16104063642227479, 'fraction_numerical': 0.0019350677273704579, 'mean_word_length': 1.7377869334902885, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 127, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3077900', 'n_tokens_mistral': 2765, 'n_tokens_neox': 2655, 'n_words': 503}