<commit_before><?php

/**
 * Runs before the init function of every Page_Controller
 * to redirect regular non-admin users to the Utility
 * Page if maintenance mode is switched on.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @package maintenancemode
 */
class MaintenanceMode_Page_ControllerExtension extends Extension {

    // Set to true after the first execution as an extra measure to prevent infinite recursion, just in case.
    protected static $runOnce = false;

    /**
     * @throws SS_HTTPResponse_Exception
     * @return SS_HTTPResponse
     */
    public function onBeforeInit() {

        if(Permission::check("ADMIN")) return;

        $config = SiteConfig::current_site_config();
        if(!$config->MaintenanceMode) return;

        // Fetch our utility page instance now.
        /** @var Page $utilityPage */
        $utilityPage = UtilityPage::get()->first();
        if (!$utilityPage) return; // We need a utility page before we can do anything.

        // See if we're still configured to redirect...
        if (!$utilityPage->config()->DisableRedirect) {
            //If this is not the utility page, do a temporary (302) redirect to it
            if($this->owner->dataRecord->ClassName != "UtilityPage") {
                return $this->owner->redirect($utilityPage->AbsoluteLink(), 302);
            }
        }

        // No need to execute more than once.
        if ($this->owner instanceof UtilityPage_Controller) return;

        // Additional failsafe, just in case (for some reason) the current controller isn't descended from UtilityPage_Controller.
        if (static::$runOnce) return;
        static::$runOnce = true;

        // Process the request internally to ensure the URL is maintained (instead of redirecting to our maintenance page's URL).
        $controller = ModelAsController::controller_for($utilityPage);
        $response = $controller->handleRequest($this->owner->getRequest());
        throw new SS_HTTPResponse_Exception($response, $response->getStatusCode());
    }

}//end class MaintenanceMode_Page_ControllerExtension



/**
 * Add settings fields to SiteConfig to control maintenance mode
 *
 * @author <NAME> <<EMAIL>>
 * @package maintenancemode
 */
class MaintenanceMode_SiteConfigExtension extends DataExtension {

    // Add database field for flag to either display or hide under construction pages.
    static $db = array(
        'MaintenanceMode' => 'Boolean'
    );

    public function updateCMSFields(FieldList $fields) {

        //create new tabs in SiteConfig
        $fields->addFieldToTab("Root.Access",<commit_msg>Manufacturing fake new request instead of retaining original request (not needed); just need to generate page content, which depends on a new request instance.<commit_after><?php

/**
 * Runs before the init function of every Page_Controller
 * to redirect regular non-admin users to the Utility
 * Page if maintenance mode is switched on.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @package maintenancemode
 */
class MaintenanceMode_Page_ControllerExtension extends Extension {

    // Set to true after the first execution as an extra measure to prevent infinite recursion, just in case.
    protected static $runOnce = false;

    /**
     * @throws SS_HTTPResponse_Exception
     * @return SS_HTTPResponse
     */
    public function onBeforeInit() {

        if(Permission::check("ADMIN")) return;

        $config = SiteConfig::current_site_config();
        if(!$config->MaintenanceMode) return;

        // Fetch our utility page instance now.
        /** @var Page $utilityPage */
        $utilityPage = UtilityPage::get()->first();
        if (!$utilityPage) return; // We need a utility page before we can do anything.

        // See if we're still configured to redirect...
        if (!$utilityPage->config()->DisableRedirect) {
            //If this is not the utility page, do a temporary (302) redirect to it
            if($this->owner->dataRecord->ClassName != "UtilityPage") {
                return $this->owner->redirect($utilityPage->AbsoluteLink(), 302);
            }
        }

        // No need to execute more than once.
        if ($this->owner instanceof UtilityPage_Controller) return;

        // Additional failsafe, just in case (for some reason) the current controller isn't descended from UtilityPage_Controller.
        if (static::$runOnce) return;
        static::$runOnce = true;

        // Process the request internally to ensure the URL is maintained (instead of redirecting to our maintenance page's URL).
        $controller = ModelAsController::controller_for($utilityPage);
        $response = $controller->handleRequest(new SS_HTTPRequest("GET", "/"));
        throw new SS_HTTPResponse_Exception($response, $response->getStatusCode());
    }

}//end class MaintenanceMode_Page_ControllerExtension



/**
 * Add settings fields to SiteConfig to control maintenance mode
 *
 * @author <NAME> <<EMAIL>>
 * @package maintenancemode
 */
class MaintenanceMode_SiteConfigExtension extends DataExtension {

    // Add database field for flag to either display or hide under construction pages.
    static $db = array(
        'MaintenanceMode' => 'Boolean'
    );

    public function updateCMSFields(FieldList $fields) {

        //create new tabs in SiteConfig
        $fields->addFieldToTab("Root.Access",
{'fraction_non_alphanumeric': 0.10278293135435992, 'fraction_numerical': 0.0022263450834879408, 'mean_word_length': 3.096504559270517, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 53, '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': '7370198', 'n_tokens_mistral': 1383, 'n_tokens_neox': 1321, 'n_words': 483}