Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
5 posts Page 1 of 1
@orbital recently had a requirement where the admin could put the site offline from within the admin-panel.

As you know, the existing way of doing this in Couch is by editing the couch/config.php file.
I was not too keen on making this file editable from the admin-panel so came up with the following quick-n-dirty solution.
Quoting it verbatim -

1. Assuming your settings template is named 'settings.php' (and it is non-clonable), place the following editable region in it -
Code: Select all
<cms:editable name='site_offline' type='checkbox' label='Put Site Offline?' opt_values='Yes=yes' />

Needless to say, visit the changed template on the frontend as super-admin for the field to appear in the admin-panel.

2. Next place the following code in your couch/addons/kfunctions.php -
Code: Select all
$FUNCS->add_event_listener( 'page_saved', function(&$pg, &$errors){

    // set these values to match your settings..
    $settings_template = 'settings.php';
    $field_name = 'site_offline';

    // work begins ..
    if( $pg->tpl_name!=$settings_template ) return; // not interested in any other template
    if( !$errors ){
        $flag_file = K_COUCH_DIR . 'cache/__site_offline__.dat';

        if( $pg->_fields[$field_name]->modified ){
            $site_offline = $pg->_fields[$field_name]->get_data();
            if( $site_offline ){
                if( !file_exists($flag_file) ){
                    @fclose( @fopen($flag_file, 'a') );
                }
            }
            else{
                if( file_exists($flag_file) ){
                    @unlink( $flag_file );
                }
            }
        }
    }
});

Notice the following lines in the code above -
$settings_template = 'settings.php';
$field_name = 'site_offline';

Please modify them if the template name or the field name we put in it above are different.

3. Finally, modify your couch/config.php and change the very first setting from this -
Code: Select all
define( 'K_SITE_OFFLINE', 0 );

to this -
Code: Select all
define( 'K_SITE_OFFLINE', file_exists(K_COUCH_DIR . 'cache/__site_offline__.dat') ? 1 : 0 );

And now you should be able to put the site offline/online form the settings template in admin-panel.
Please test it out and let me know if this helps.
Hi KK, this function works great, and has really helped me a couple of times, thanks!
I had a quick query - at the moment an administrator has to log in to be able to view the site while it is in maintenance mode. Is it possible to alter this so that a logged in authenticated user (or special) was able to view the site in maintenance mode?
Thanks,
Ewan
Hi Ewan,

Is it possible to alter this so that a logged in authenticated user (or special) was able to view the site in maintenance mode?

Not out of the box but can be done with some tweaking -

The behavior for K_SITE_OFFLINE in config is hard-coded in Couch and setting it to '1' will make the site off-limits for everybody except the admins. So, we'll not use this setting and you should set it to the default '0' -
Code: Select all
define( 'K_SITE_OFFLINE', 0 );

Next, place the following code in your couch/addons/kfunctions.php file just below the code in my original post of this thread -
Code: Select all
call_user_func( function(){
    global $FUNCS, $AUTH, $CTX, $KUSER;
    $flag_file = K_COUCH_DIR . 'cache/__site_offline__.dat';

    $script = str_replace('\\', '/', realpath($_SERVER['SCRIPT_FILENAME']));
    if( substr($script, 0, strlen(K_COUCH_DIR)) == K_COUCH_DIR ){ // we are in the admin-panel
        return;
    }
   
    if( file_exists($flag_file) ){
        $script = substr( $script, strlen(K_SITE_DIR) );
        if( $script=='404.php' || $script=='503.php' || (is_object($KUSER) && $script==$KUSER->login_tpl)){
            return;
        }

        if( $AUTH->user->access_level < K_ACCESS_LEVEL_AUTHENTICATED ){
            ob_end_clean();
            header('HTTP/1.1 503 Service Temporarily Unavailable');
            header('Status: 503 Service Temporarily Unavailable');
            $html = '';
            if( file_exists(K_SITE_DIR . '503.php') ){
                $html = $FUNCS->file_get_contents( K_SITE_URL . '503.php' );
            }
            if( !$html ){
                $html = $FUNCS->render( 'site_offline' );
            }

            echo $html;
            die;
        }
    }
});

And now when the admin sets the site offline from the admin-panel (NOT directly from the config file), the entire site becomes offline for everybody except authenticated users (i.e. offline for all except users of access-level 2 and above).

If you wish to make the site accessible for only level 4 (and above) users, you can change the following line in the code
Code: Select all
if( $AUTH->user->access_level < K_ACCESS_LEVEL_AUTHENTICATED ){
to make it -
Code: Select all
if( $AUTH->user->access_level < K_ACCESS_LEVEL_AUTHENTICATED_SPECIAL ){

Please test it and let me know if this helps.
KK,
As always, that works exactly as I'd hoped.
Many thanks - you do such a great job with Couch, I don't know what to say :D
You are welcome, Ewan :)
I am glad I could help.
5 posts Page 1 of 1
cron