<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

if (!function_exists('check_ssl')) {

    function check_ssl() {
        $CI = & get_instance();
        $class = $CI->router->fetch_class();
        $ssl = array('security', 'secured');
        if (in_array($class, $ssl)) {
            force_ssl();
        } else {
            unforce_ssl();
        }
    }

}

if (!function_exists('force_ssl')) {

    function force_ssl() {
        $CI = & get_instance();
        $CI->config->set_item('base_url', str_replace('http://', 'https://', config_item('base_url')));
        if ($_SERVER['SERVER_PORT'] != 443) { // it will loop if you change it to !==
            redirect($CI->uri->uri_string());
        }
    }

}

if (!function_exists('unforce_ssl')) {

    function unforce_ssl() {
        $CI = & get_instance();
        $CI->config->set_item('base_url', str_replace('https://', 'http://', config_item('base_url')));
        if ($_SERVER['SERVER_PORT'] != 80) { // it will loop if you change it to !==
            redirect($CI->uri->uri_string());
        }
    }

}