<commit_before><?php
namespace Concrete\Core\Marketplace;

use Concrete\Core\Application\ApplicationAwareInterface;
use Concrete\Core\Application\ApplicationAwareTrait;
use Concrete\Core\Config\Repository\Repository;
use Concrete\Core\File\Service\File;
use Concrete\Core\Site\InstallationService;
use Concrete\Core\Support\Facade\Application;
use Concrete\Core\Support\Facade\Package;

use Concrete\Core\Legacy\TaskPermission;
use Concrete\Core\Url\Resolver\CanonicalUrlResolver;
use Concrete\Core\Url\Resolver\PathUrlResolver;
use Exception;
use Concrete\Core\Http\Request;

class Marketplace implements ApplicationAwareInterface
{

    use ApplicationAwareTrait;

    const E_INVALID_BASE_URL = 20;
    const E_MARKETPLACE_SUPPORT_MANUALLY_DISABLED = 21;
    const E_UNRECOGNIZED_SITE_TOKEN = 22;
    const E_DELETED_SITE_TOKEN = 31;
    const E_SITE_TYPE_MISMATCH_MULTISITE = 34;
    const E_CONNECTION_TIMEOUT = 41;
    const E_GENERAL_CONNECTION_ERROR = 99;

    protected $isConnected = false;
    protected $connectionError = false;

    /** @var Repository */
    protected $config;

    /** @var Repository */
    protected $databaseConfig;

    /** @var File */
    protected $fileHelper;

    /** @var PathUrlResolver */
    protected $urlResolver;

    /** @var \Concrete\Core\Http\Request */
    protected $request;

    public function setApplication(\Concrete\Core\Application\Application $application)
    {
        $this->app = $application;

        $this->fileHelper = $this->app->make('helper/file');
        $this->config = $this->app->make('config');
        $this->databaseConfig = $this->app->make('config/database');
        $this->urlResolver = $this->app->make(PathUrlResolver::class);
        $this->request = $this->app->make(Request::class);
        $this->isConnected = false;
        $this->isConnected();
    }

    /**
     * @param $dbConfig
     */
    public function isConnected()
    {
        if ($this->isConnected) {
            return true;
        }

        if (!$this->config->get('concrete.marketplace.enabled')) {
            $this->connectionError = self::E_MARKETPLACE_SUPPORT_MANUALLY_DISABLED;

            return;
        }

        $csToken = $this->databaseConfig->get('concrete.marketplace.token');

        $this->isConnected = false;

        if ($csToken != '') {
            $fh = $this->app->make('helper/file');
            $installationService = $this->app->make(InstallationService::class);
            if ($installationService->isMultisiteEnabled()) {
                $ms = '&ms=1';
            }
            $csiURL = urlencode($this->getSiteURL());
            $url = $this->config->get('concrete.urls.concrete') . $this->config->get('concrete.urls.paths.marketplace.connect_validate') . "?csToken={$csToken}&csiURL=" . $csiURL . "&csiVersion=" . APP_VERSION . $ms;
            $vn = $this->app->make('helper/validation/numbers');
            $r = $this->get($url);

            if ($r === null && !$this->connectionError) {
                $this->isConnected = true;
            } else {
                if ($vn->integer($r)) {
                    $this->isConnected = false;
                    $this->connectionError = $r;

                    if ($this->connectionError == self::E_DELETED_SITE_TOKEN) {
                        $this->databaseConfig->clear('concrete.marketplace.token');<commit_msg>Fix "Undefined variable" error<commit_after><?php
namespace Concrete\Core\Marketplace;

use Concrete\Core\Application\ApplicationAwareInterface;
use Concrete\Core\Application\ApplicationAwareTrait;
use Concrete\Core\Config\Repository\Repository;
use Concrete\Core\File\Service\File;
use Concrete\Core\Site\InstallationService;
use Concrete\Core\Support\Facade\Application;
use Concrete\Core\Support\Facade\Package;

use Concrete\Core\Legacy\TaskPermission;
use Concrete\Core\Url\Resolver\CanonicalUrlResolver;
use Concrete\Core\Url\Resolver\PathUrlResolver;
use Exception;
use Concrete\Core\Http\Request;

class Marketplace implements ApplicationAwareInterface
{

    use ApplicationAwareTrait;

    const E_INVALID_BASE_URL = 20;
    const E_MARKETPLACE_SUPPORT_MANUALLY_DISABLED = 21;
    const E_UNRECOGNIZED_SITE_TOKEN = 22;
    const E_DELETED_SITE_TOKEN = 31;
    const E_SITE_TYPE_MISMATCH_MULTISITE = 34;
    const E_CONNECTION_TIMEOUT = 41;
    const E_GENERAL_CONNECTION_ERROR = 99;

    protected $isConnected = false;
    protected $connectionError = false;

    /** @var Repository */
    protected $config;

    /** @var Repository */
    protected $databaseConfig;

    /** @var File */
    protected $fileHelper;

    /** @var PathUrlResolver */
    protected $urlResolver;

    /** @var \Concrete\Core\Http\Request */
    protected $request;

    public function setApplication(\Concrete\Core\Application\Application $application)
    {
        $this->app = $application;

        $this->fileHelper = $this->app->make('helper/file');
        $this->config = $this->app->make('config');
        $this->databaseConfig = $this->app->make('config/database');
        $this->urlResolver = $this->app->make(PathUrlResolver::class);
        $this->request = $this->app->make(Request::class);
        $this->isConnected = false;
        $this->isConnected();
    }

    /**
     * @param $dbConfig
     */
    public function isConnected()
    {
        if ($this->isConnected) {
            return true;
        }

        if (!$this->config->get('concrete.marketplace.enabled')) {
            $this->connectionError = self::E_MARKETPLACE_SUPPORT_MANUALLY_DISABLED;

            return;
        }

        $csToken = $this->databaseConfig->get('concrete.marketplace.token');

        $this->isConnected = false;

        if ($csToken != '') {
            $fh = $this->app->make('helper/file');
            $ms = '';
            $installationService = $this->app->make(InstallationService::class);
            if ($installationService->isMultisiteEnabled()) {
                $ms = '&ms=1';
            }
            $csiURL = urlencode($this->getSiteURL());
            $url = $this->config->get('concrete.urls.concrete') . $this->config->get('concrete.urls.paths.marketplace.connect_validate') . "?csToken={$csToken}&csiURL=" . $csiURL . "&csiVersion=" . APP_VERSION . $ms;
            $vn = $this->app->make('helper/validation/numbers');
            $r = $this->get($url);

            if ($r === null && !$this->connectionError) {
                $this->isConnected = true;
            } else {
                if ($vn->integer($r)) {
                    $this->isConnected = false;
                    $this->connectionError = $r;

                    if ($this->connectionError == self::E_DELETED_SITE_TOKEN) {
                        $this->databaseConfig->clear('concrete.marketplace.token');
{'fraction_non_alphanumeric': 0.14176132172886857, 'fraction_numerical': 0.004425431479569258, 'mean_word_length': 2.7314254265272426, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 95, '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': '659573', 'n_tokens_mistral': 2091, 'n_tokens_neox': 2012, 'n_words': 319}