[eluser]Late Night Again[/eluser]
I thought I would share a very simply code I have written to control a whole site from a config file, I call this simple script global site, this allows you to disable a whole site or just one or many controllers so they display a site is currently down message.
It also has a feature where you can allow ip addresses to view the site even if it is currently disabled for the rest of the world.
I find this script has helped me for any development site I have been creating so only a customer can view the progress and not the rest of the world or if you are doing a major update it is great for testing to make sure a cutover has gone through smoootly while informing others it is currently being updated.
The Library
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class GlobalSite
{
function __construct() {
$this->CI=& get_instance();
log_message('debug', "Global Class Initialized");
$this->ip_address = $this->CI->input->ip_address();
}
function resetTrue() {
if($this->CI->config->item('disable_site') == TRUE)
{
// Check if the ip address is allowed on the site while it is down.
foreach($this->CI->config->item('allow_ips') as $key => $value)
{
// Allow the ipaddresses listed in the config file
if($value === $this->ip_address)
{
$this->CI->config->set_item('disable_site', false);
}
}
}
}
function resetSpecificTrue() {
if($this->CI->config->item('disable_'.$this->CI->uri->segment(1)) == TRUE)
{
// Check if the ip address is allowed on the site while it is down.
foreach($this->CI->config->item('allow_ips') as $key => $value)
{
// Allow the ipaddresses listed in the config file
if($value === $this->ip_address)
{
$this->CI->config->set_item('disable_'.$this->CI->uri->segment(1), false);
}
}
}
}
}
Global Config
Code:
$config['disabled_message'] = "The site is currently down for upgrades, please return in the near future.";
///////////////////////////////////
// Custom disables //
// Disables the whole site when //
// set to true //
///////////////////////////////////
$config['disable_site'] = FALSE;
///////////////////////////////////
// Disables a specific controller//
// disable_site overrides this //
// call. //
///////////////////////////////////
$config['disable_welcome'] = FALSE;
// Can be as many of these as you want
/////////////////////////////////////
// Sets the message for a disabled //
// controller //
/////////////////////////////////////
$config['disabled_specific'] = "This section is currently disabled, please return later";
/****************************************************************/
/* ip addresse allowed to view the site even if it is disabled */
/****************************************************************/
$config['allow_ips'] = array('localhost' => '127.0.0.1',
'work' => '192.168.6.16',
'home' => '111.111.11.11');
Inside you constructor
Code:
class Whatever extends Controller {
function Whatever() {
parent::Controller();
$this->globalsite->resetTrue(); // if set to true disbales the whole site except for the allowed ip addresses
$this->globalsite->resetSpecificTrue(); // if $config['disable_whatever'] is set to true it will disable only that controller except for allowed ip addresses
}
}
The View
Code:
<?php if($this->config->item('disable_site')):?>
<?php $this->load->view($this->config->item('theme').'templates/content_disabled')?>
<?php else:?>
<?php if($this->config->item('disable_'.$this->uri->segment(1))):?>
<?php $this->load->view($this->config->item('theme').'templates/content_disabled')?>
<?php else:?>
<?php $this->load->view($this->config->item('theme').'templates/content')?>
<?php endif;?>
<?php endif;?>
The view is best set through a container to load content or disabled content files.
I find this is best set through the autoload but can also be activated in a controller construct as well.
Hope this is helpful to someone else.