<commit_before><?php

namespace PulkitJalan\Google;

use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;
use PulkitJalan\Google\Exceptions\UnknownServiceException;

class Client
{
    /**
     * @var array
     */
    protected $config;

    /**
     * @var \Google_Client
     */
    // test
    protected $client;

    /**
     * @var string
     */
    protected $token;
    /**
     * @var string
     */
    protected $fileToken;

    protected $tokenKey = "AdexAjaDriveAuto.Laravel";
    /**
     * @param array $config
     * @param string $userEmail
     */
    public function __construct(array $config, $userEmail = '')
    {
        $this->config = $config;

        // create an instance of the google client for OAuth2
        $this->client = new \Google_Client();

        // set application name
        $this->client->setApplicationName(array_get($config, 'application_name', ''));

        // set oauth2 configs
        $this->client->setClientId(array_get($config, 'client_id', ''));
        $this->client->setClientSecret(array_get($config, 'client_secret', ''));
        $this->client->setRedirectUri(array_get($config, 'redirect_uri', ''));
        $this->client->setScopes(array_get($config, 'scopes', []));
        $this->client->setAccessType(array_get($config, 'access_type', 'online'));
        $this->client->setApprovalPrompt(array_get($config, 'approval_prompt', 'auto'));
        // set developer key
        $this->client->setDeveloperKey(array_get($config, 'developer_key', ''));

        $this->fileToken = array_get($config, 'fileToken', '');

        // auth for service account
        if (array_get($config, 'service.enable', false)) {
            $this->auth($userEmail);
        }


        if($this->fileToken) {
            if(file_exists($this->fileToken))
                $this->token = json_decode(file_get_contents($this->fileToken), true);
        } else {
            $this->token = Session::get($this->tokenKey);
        }


        if($this->token) {
            $this->client->setAccessToken($this->token);
        } else if(request()->has("code")) {
            $this->client->authenticate(request()->input("code"));
            $this->token = $this->client->getAccessToken();
            $this->client->setAccessToken($this->token);
        }else {
            // no available token
            if(!empty(array_get($config, 'redirect_uri', ''))) {
                // redirect for authorization
                // redirect($this->Client->createAuthUrl());
                echo "<div style='background: rgba(0, 177, 225, 0.15); padding: 10px 15px; float: left; width: 100%; border-left: 6px solid #00b1e1;'>";
                echo "  <h1 style='padding: 0px; margin: 0px; font-size: 24px; color: #0788ab; font-weight: bold;'>Pemberitahuan</h1>";
                echo "  <p style=''>Akun anda belum terintegrasi dengan sistem kami. Silahkan melakukan konfirmasi akun ketika ada jendela popup. Pastikan anda tidak memblokir jendela popup. Jika jendela popup belum muncul, anda bisa menekan ikon refresh.</p>";
                echo "</div>";
                ?>
                <script>
                    var w = 500;
                    var h = 400;
                    var left = (screen.width/2)-(w/2);
                    var top = (screen.height/2)-(h/2);
                    var win = window.open('<?= $this->client->createAuthUrl(); ?>', '_blank', ', width='+w+', height='+h+', top='+top+', left='+left+', toolbar=0,location=0,menubar=0,scrollbars=0, resizable=0, opyhistory=no');
                    win.focus();
                </script>
                <?
            }
        }
        // check to make sure access token is not expired
        if($this->client->isAccessTokenExpired() and $this->token) {
            $tokens = (is_array($this->token)) ? $this->token : json_decode($this->token);
            $refreshToken = $tokens["refresh_token"];
            $this->client->refreshToken($refreshToken);
            $this->token = $this->client->getAccessToken();
        }

        // save access token
        if(!$this->client->isAccessTokenExpired()) {
            if($this->fileToken) {
                // $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
                if(!file_exists(dirname($this->fileToken))) {
                    mkdir(dirname($this->fileToken), 777, true);
                }
                file_put_contents($this->fileToken, json_encode($this->token));
            } else {
                Session::put($this->tokenKey, $this->token);
            }
        }
    }

    /**
     * Getter for the google client.
     *
     * @return \Google_Client
     */
    public function getClient()
    {
        return $this->client;
    }

    /**
     * Getter for the google service.
     *
     * @param string $service
     *
     * @throws \Exception<commit_msg>Update Client.php<commit_after><?php

namespace PulkitJalan\Google;

use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;
use PulkitJalan\Google\Exceptions\UnknownServiceException;

class Client
{
    /**
     * @var array
     */
    protected $config;

    /**
     * @var \Google_Client
     */
    // test
    protected $client;

    /**
     * @var string
     */
    protected $token;
    /**
     * @var string
     */
    protected $fileToken;

    protected $tokenKey = "AdexAjaDriveAuto.Laravel";
    /**
     * @param array $config
     * @param string $userEmail
     */
    public function __construct(array $config, $userEmail = '')
    {
        $this->config = $config;

        // create an instance of the google client for OAuth2
        $this->client = new \Google_Client();

        // set application name
        $this->client->setApplicationName(array_get($config, 'application_name', ''));

        // set oauth2 configs
        $this->client->setClientId(array_get($config, 'client_id', ''));
        $this->client->setClientSecret(array_get($config, 'client_secret', ''));
        $this->client->setRedirectUri(array_get($config, 'redirect_uri', ''));
        $this->client->setScopes(array_get($config, 'scopes', []));
        $this->client->setAccessType(array_get($config, 'access_type', 'online'));
        $this->client->setApprovalPrompt(array_get($config, 'approval_prompt', 'auto'));
        // set developer key
        $this->client->setDeveloperKey(array_get($config, 'developer_key', ''));

        try{
            $this->fileToken = array_get($config, 'fileToken', '');

            // auth for service account
            if (array_get($config, 'service.enable', false)) {
                $this->auth($userEmail);
            }


            if($this->fileToken) {
                if(file_exists($this->fileToken))
                    $this->token = json_decode(file_get_contents($this->fileToken), true);
            } else {
                $this->token = Session::get($this->tokenKey);
            }


            if($this->token) {
                $this->client->setAccessToken($this->token);
            } else if(request()->has("code")) {
                $this->client->authenticate(request()->input("code"));
                $this->token = $this->client->getAccessToken();
                $this->client->setAccessToken($this->token);
            }else {
                // no available token
                if(!empty(array_get($config, 'redirect_uri', ''))) {
                    // redirect for authorization
                    // redirect($this->Client->createAuthUrl());
                    echo "<div style='background: rgba(0, 177, 225, 0.15); padding: 10px 15px; float: left; width: 100%; border-left: 6px solid #00b1e1;'>";
                    echo "  <h1 style='padding: 0px; margin: 0px; font-size: 24px; color: #0788ab; font-weight: bold;'>Pemberitahuan</h1>";
                    echo "  <p style=''>Akun anda belum terintegrasi dengan sistem kami. Silahkan melakukan konfirmasi akun ketika ada jendela popup. Pastikan anda tidak memblokir jendela popup. Jika jendela popup belum muncul, anda bisa menekan ikon refresh.</p>";
                    echo "</div>";
                    ?>
                    <script>
                        var w = 500;
                        var h = 400;
                        var left = (screen.width/2)-(w/2);
                        var top = (screen.height/2)-(h/2);
                        var win = window.open('<?= $this->client->createAuthUrl(); ?>', '_blank', ', width='+w+', height='+h+', top='+top+', left='+left+', toolbar=0,location=0,menubar=0,scrollbars=0, resizable=0, opyhistory=no');
                        win.focus();
                    </script>
                    <?
                }
            }
            // check to make sure access token is not expired
            if($this->client->isAccessTokenExpired() and $this->token) {
                $tokens = (is_array($this->token)) ? $this->token : json_decode($this->token);
                $refreshToken = $tokens["refresh_token"];
                $this->client->refreshToken($refreshToken);
                $this->token = $this->client->getAccessToken();
            }

            // save access token
            if(!$this->client->isAccessTokenExpired()) {
                if($this->fileToken) {
                    // $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
                    if(!file_exists(dirname($this->fileToken))) {
                        mkdir(dirname($this->fileToken), 777, true);
                    }
                    file_put_contents($this->fileToken, json_encode($this->token));
                } else {
                    Session::put($this->tokenKey, $this->token);
                }
            }
        }catch (\Exception $ex){
            if(!empty(array_get($config, 'redirect_uri', ''))) {
                // redirect for authorization
                // redirect($this->Client->createAuthUrl());
                echo "<div style='background: rgba(0, 177, 225, 0.15); padding: 10px 15px; float: left; width: 100%; border-left: 6px solid #00b1e1;'>";
                echo "  <h1 style='padding: 0px; margin: 0px; font-size: 24px; color: #0788ab; font-weight: bold;'>Pemberitahuan</h1>";
                echo "  <p style=''>Akun anda belum terintegrasi dengan sistem kami. Silahkan melakukan konfirmasi akun ketika ada jendela popup. Pastikan anda tidak memblokir jendela popup. Jika jendela popup belum muncul, anda bisa menekan ikon refresh.</p>";
                echo "</div>";
                ?>
                <script>
                    var w = 500;
                    var h = 400;
                    var left = (screen.width/2)-(w/2);
                    var top = (screen.height/2)-(h/2);
                    var win = window.open('<?= $this->client->createAuthUrl(); ?>', '_blank', ', width='+w+', height='+h+', top='+top+', left='+left+', toolbar=0,location=0,menubar=0,scrollbars=0, resizable=0, opyhistory=no');
                    win.focus();
                </script>
                <?
            }
        }


    }

    /**
     * Getter for the google client.
     *
     * @return \Google_Client
     */
    public function getClient()
    {
        return $this->client;
    }

    /**
     * Getter for the google service.
     *
     * @param string $service
     *
     * @throws \Exception
{'fraction_non_alphanumeric': 0.1543399876879782, 'fraction_numerical': 0.013279394952071057, 'mean_word_length': 1.9560696646737719, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 169, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3461019', 'n_tokens_mistral': 3462, 'n_tokens_neox': 3203, 'n_words': 646}