<commit_before><?php
namespace CkTools\Controller\Component;

use App\Lib\Environment;
use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;

/**
 * Maintenance component
 */
class MaintenanceComponent extends Component
{
    /**
     * @var \Cake\Controller\Controller
     */
    protected $_controller;


    /**
     * Constructor
     *
     * @param ComponentRegistry $registry A ComponentRegistry object.
     * @param array             $config   Array of configuration settings.
     */
    public function __construct(ComponentRegistry $registry, array $config = [])
    {
        parent::__construct($registry, $config);

        if ($registry->getController() instanceof \Cake\Controller\Controller) {
            $this->_controller = $registry->getController();
        }
    }

    /**
     * maintenance redirect logic
     *
     * @return mixed|void
     */
    public function beforeFilter()
    {
        if (defined('PHPUNIT_TESTSUITE')) {
            return;
        }
        $activated = Environment::read('MAINTENANCE');
        if (!$activated) {
            return;
        }
        $cookieName = Environment::read('MAINTENANCE_COOKIE_NAME');
        $cookieExists = ($this->_controller->Cookie->read($cookieName) != null);
        if ($cookieExists) {
            return;
        }
        $headerActive = Environment::read('MAINTENANCE_HEADER_ACTIVE');
        $headerName = Environment::read('MAINTENANCE_HEADER_NAME');
        $headerValue = Environment::read('MAINTENANCE_HEADER_VALUE');
        $successUrl = Environment::read('MAINTENANCE_PASSWORD_SUCCESS_URL');
        if ($headerActive && !empty($this->request->header($headerName)) && $this->request->header($headerName) == $headerValue) {
            $this->_controller->Cookie->write($cookieName, true);
            return $this->_controller->redirect($successUrl);
        }
        $currentUrl = $this->request->here;
        $accessibleUrls = explode('|', Environment::read('MAINTENANCE_ACCESSIBLE_URLS'));
        $maintenancePage = Environment::read('MAINTENANCE_PAGE_REDIRECT_URL');
        $accessibleUrls[] = $maintenancePage;
        $passwordUrl = Environment::read('MAINTENANCE_PASSWORD_URL');
        $accessibleUrls[] = $passwordUrl;
        if (!in_array($currentUrl, $accessibleUrls)) {
            return $this->_controller->redirect($maintenancePage);
        } elseif ($currentUrl != $passwordUrl) {
            return;
        }
        $user = Environment::read('MAINTENANCE_USER');
        $password = Environment::read('MAINTENANCE_PASSWORD');
        if ($currentUrl == $passwordUrl) {
            if (!isset($_SERVER['PHP_AUTH_USER'])) {
                header('WWW-Authenticate: Basic realm="Maintenance Realm"');
                header('HTTP/1.0 401 Unauthorized');
                echo 'Unauthorized';
                exit;
            } else {
                if ($_SERVER['PHP_AUTH_USER'] == $user && $_SERVER['PHP_AUTH_PW'] == $password) {
                    $this->_controller->Cookie->write($cookieName, true);
                    return $this->_controller->redirect($successUrl);<commit_msg>Redirect to defined base page when requesting maintenance page if maintenance mode is disabled<commit_after><?php
namespace CkTools\Controller\Component;

use App\Lib\Environment;
use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;

/**
 * Maintenance component
 */
class MaintenanceComponent extends Component
{
    /**
     * @var \Cake\Controller\Controller
     */
    protected $_controller;


    /**
     * Constructor
     *
     * @param ComponentRegistry $registry A ComponentRegistry object.
     * @param array             $config   Array of configuration settings.
     */
    public function __construct(ComponentRegistry $registry, array $config = [])
    {
        parent::__construct($registry, $config);

        if ($registry->getController() instanceof \Cake\Controller\Controller) {
            $this->_controller = $registry->getController();
        }
    }

    /**
     * Returns if the maintenance mode is currently active
     *
     * @return bool
     */
    public static function isMaintenanceActive()
    {
        return !!Environment::read('MAINTENANCE');
    }

    /**
     * maintenance redirect logic
     *
     * @return mixed|void
     */
    public function beforeFilter()
    {
        if (defined('PHPUNIT_TESTSUITE')) {
            return;
        }
        $maintenancePage = Environment::read('MAINTENANCE_PAGE_REDIRECT_URL');
        $currentUrl = $this->request->here;
        $accessibleUrls = explode('|', Environment::read('MAINTENANCE_ACCESSIBLE_URLS'));
        $accessibleUrls[] = $maintenancePage;

        if (!self::isMaintenanceActive()) {
            // if maintenance is not active but maintenance page is requested -> redirect to default page
            if (in_array($currentUrl, $accessibleUrls) && (substr($maintenancePage, -strlen($currentUrl))) === $currentUrl) {
                $maintenanceBasePage = Environment::read('MAINTENANCE_BASE_URL');
                return $this->_controller->redirect($maintenanceBasePage);
            }
            return;
        }

        $cookieName = Environment::read('MAINTENANCE_COOKIE_NAME');
        $cookieExists = ($this->_controller->Cookie->read($cookieName) != null);
        if ($cookieExists) {
            return;
        }
        $headerActive = Environment::read('MAINTENANCE_HEADER_ACTIVE');
        $headerName = Environment::read('MAINTENANCE_HEADER_NAME');
        $headerValue = Environment::read('MAINTENANCE_HEADER_VALUE');
        $successUrl = Environment::read('MAINTENANCE_PASSWORD_SUCCESS_URL');
        if ($headerActive && !empty($this->request->header($headerName)) && $this->request->header($headerName) == $headerValue) {
            $this->_controller->Cookie->write($cookieName, true);
            return $this->_controller->redirect($successUrl);
        }
        $passwordUrl = Environment::read('MAINTENANCE_PASSWORD_URL');
        $accessibleUrls[] = $passwordUrl;
        if (!in_array($currentUrl, $accessibleUrls)) {
            return $this->_controller->redirect($maintenancePage);
        } elseif ($currentUrl != $passwordUrl) {
            return;
        }
        $user = Environment::read('MAINTENANCE_USER');
        $password = Environment::read('MAINTENANCE_PASSWORD');
        if ($currentUrl == $passwordUrl) {
            if (!isset($_SERVER['PHP_AUTH_USER'])) {
                header('WWW-Authenticate: Basic realm="Maintenance Realm"');
                header('HTTP/1.0 401 Unauthorized');
                echo 'Unauthorized';
                exit;
            } else {
                if ($_SERVER['PHP_AUTH_USER'] == $user && $_SERVER['PHP_AUTH_PW'] == $password) {
                    $this->_controller->Cookie->write($cookieName, true);
                    return $this->_controller->redirect($successUrl);
{'fraction_non_alphanumeric': 0.13361259569550774, 'fraction_numerical': 0.001444460494005489, 'mean_word_length': 2.686900958466454, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 54, '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': '3514760', 'n_tokens_mistral': 1836, 'n_tokens_neox': 1811, 'n_words': 328}