I had some spare time today and decided to write some code so that the Maintance Mode can be set on or off from within the admin.

This is the main file, save it as 'changemaintenance.php' in your couch folder :

Code: Select all
<?php 
$file= "config.php";
if ( !file_exists($file) ) {
        echo('File not found: '.$file);die;
      }

// read config.php file
$text = file_get_contents($file);

// find the current state of maintanence
foreach(preg_split('~[\r\n]+~', $text) as $line){
    if(empty($line) or ctype_space($line)) continue;
       if (strpos($line,'K_SITE_OFFLINE') !== false){ $currentstate=$line;}
}

if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['newmode'])){

   // give maintanence a new state
   if (strpos($currentstate,'1') !== false) {
      // maintenance mode is on, let's switch it to off
      $newstate=str_replace("1", "0", $currentstate);
   }else{
      // maintenance mode is off, let's switch it to on
      $newstate=str_replace("0", "1", $currentstate);
   }

   // change the newstate into the $text string
   $newtext = str_replace($currentstate, $newstate, $text);

   // and write it to config.php
    file_put_contents($file, $newtext);

header("Refresh:0");

}
?>



      <form action="" method="post">
      <?php
         if (strpos($currentstate,'1') !== false) {echo "Maintenance mode is on";}else{echo "Maintenance mode is off";}
      ?>
      <input type="hidden" name="newmode" value="1">
      <input type="submit" value="Change">
      </form>



To access the .php file we need to link to it within the admin. To do so, open functions.php within your couch folder and find these two lines:
Code: Select all
echo '<li>'.$this->t('greeting').', <a href="'.K_ADMIN_URL . K_ADMIN_PAGE.'?o=users&act=edit&id='.$AUTH->user->id.'&nonce='.$nonce.'"><b>' . ucwords( strtolower($AUTH->user->title) ) . '</b></a></li>';
echo '<li>|</li>';


Place the following two lines beneath them:
Code: Select all
echo '<li><a href="'.K_SITE_URL.'/couch/changemaintanence.php" target="_blank">Maintanence</a></li>';
echo '<li>|</li>';


Save functions.php in your couch folder.

That's it :D

There should be a link to the Maintenance Mode page within your Admin Panel now, located in the upper right.

:geek:

There is room for improvement though, the Maintenance Mode page can be accessed by non admins as well (if they know the location of the page). If anyone want's to alter the code, be my guest and post the improved version here.

(I sure hope the next version of couchcms will have a built in Maintenance Mode which can be accessed from within the admin.)