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

// ------------------------------------------------------------------------

/**
* Site URL
*
* Create a local URL based on your basepath. Segments can be passed via the
* first parameter either as a string or an array.
*
* This has been modified to use a custom admin directory specified in config.
*
* @access public
* @param string
* @return string
*/
if (!function_exists('site_url'))
{
    function site_url($uri = '', $ssl = FALSE)
    {
        $CI =& get_instance();
        
        return $CI->config->site_url($uri, $ssl);
    }
}

// ------------------------------------------------------------------------

/**
* Base URL
*
* Returns the "base_url" item from your config file
*
* @access public
* @return string
*/
if (!function_exists('base_url'))
{
    function base_url($ssl = FALSE)
    {
        $CI =& get_instance();
        if ($ssl && $CI->config->item('secure_base_url'))
        {
            return $CI->config->slash_item('secure_base_url');
        }
        return $CI->config->slash_item('base_url');
    }
}

// ------------------------------------------------------------------------

/**
* Current URL
*
* Returns the full URL (including segments) of the page where this
* function is placed
*
* @access public
* @return string
*/
if (!function_exists('current_url'))
{
    function current_url($ssl = FALSE)
    {
        $CI =& get_instance();
        return $CI->config->site_url($CI->uri->uri_string(), $ssl);
    }
}

// ------------------------------------------------------------------------

/**
* Anchor Link
*
* Creates an anchor based on the local URL.
*
* @access public
* @param string the URL
* @param string the link title
* @param mixed any attributes
* @return string
*/
if (!function_exists('anchor'))
{
    function anchor($uri = '', $title = '', $attributes = '', $ssl = FALSE)
    {
        $title = (string) $title;
        
        if (!is_array($uri))
        {
            $site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri, $ssl) : $uri;
        }
        else
        {
            $site_url = site_url($uri);
        }
        
        if ($title == '')
        {
            $title = $site_url;
        }
        
        if ($attributes != '')
        {
            $attributes = _parse_attributes($attributes);
        }
        
        return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
    }
}

// ------------------------------------------------------------------------

/**
* Header Redirect
*
* Header redirect in two flavors
* For very fine grained control over headers, you could use the Output
* Library's set_header() function.
*
* @access public
* @param string the URL
* @param string the method: location or redirect
* @return string
*/
if (!function_exists('redirect'))
{
    function redirect($uri = '', $method = 'location', $http_response_code = 302, $ssl = FALSE)
    {
        if (!preg_match('#^https?://#i', $uri))
        {
            $uri = site_url($uri, $ssl);
        }
        
        switch($method)
        {
            case 'refresh' : header("Refresh:0;url=".$uri);
                break;
            default : header("Location: ".$uri, TRUE, $http_response_code);
                break;
        }
exit;
    }
}
