• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Global Site (Site control)

#1
[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.

#2
[eluser]llbbl[/eluser]
nice idea will come in handy im sure.

#3
[eluser]Majd Taby[/eluser]
that's a good idea, thanks

#4
[eluser]Kemik[/eluser]
This is great, thanks!

Just for reference though, how does the script know to pull the global config or are you adding that code to the config.php file?

Thanks.

#5
[eluser]Skulls[/eluser]
lol build a library when you can edit the index.php file where the site starts from ...
you can put in front ... an array with some ips and then put the condition
Code:
$allowed_ips=array('ip1', 'ip2'); //etc
if (!in_array($allowed_ips)) { do a die() or something }
i wrote two lines of code... u wrote ... many Smile

#6
[eluser]Late Night Again[/eluser]
[quote author="Kemik" date="1190573622"]This is great, thanks!

Just for reference though, how does the script know to pull the global config or are you adding that code to the config.php file?

Thanks.[/quote]

I just call this in the autoload.php because it needs to be called for each controller that loads

#7
[eluser]Late Night Again[/eluser]
[quote author="Skulls" date="1190580430"]lol build a library when you can edit the index.php file where the site starts from ...
you can put in front ... an array with some ips and then put the condition
Code:
$allowed_ips=array('ip1', 'ip2'); //etc
if (!in_array($allowed_ips)) { do a die() or something }
i wrote two lines of code... u wrote ... many Smile[/quote]

Yes that may be true and comment accepted, but this also checks if each controller is active and I may not want to disable the whole site, it aslo loads it directly into the current template structure of the page and did not want to call a die with just an error message by itself.

I am sure there is probably a better way of doing this but I wanted to have a lot more control over each seperated controller than the whole site


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.