[Codeigniter-users] アプリでSSL通信を行う場合

Zurück zum Archiv-Index

kunitsuji tsuji****@m-s*****
2008年 6月 7日 (土) 16:27:57 JST


 kunitsujiです。

CIにはアプリでのSSL通信(HTTPS)での処理が無いようです。
独自のHelperとLibraryを作成しました。

MY_url_helper.php

<?php  if (!defined('BASEPATH')) exit('No direct script access 
allowed');
// ---------------------------------------------------------------------
---
/**
 * @author     KUNIHARU Tsujioka <kunit****@gmail*****>
 * @version    2008-04-22 Ver 0.1.0
 */


//2008-04-21 KUNIHARU Tsujioka update
/**
 * 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.
 *
 * @access  public
 * @param   string
 * @param   string SSL true/false 2008-04-21 KUNIHARU Tsujioka
 * @return  string
 */
if (! function_exists('site_url'))
{
    function site_url($uri = '', $ssl = FALSE)
    {
        $CI =& get_instance();
        return $CI->config->site_url($uri, $ssl);
    }
}


// ---------------------------------------------------------------------
---
//2008-04-21 KUNIHARU Tsujioka update

/**
 * 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
 * @param   bool    SSL true/false
 * @return  string
 */
if (! function_exists('anchor'))
{
    function anchor($uri = '', $title = '', $attributes = '', $ssl = 
FALSE)
    {
        $title = (string) $title;

        if ( ! is_array($uri))
        {
            if ($ssl)
            {
                $site_url = ( ! preg_match('!^\w+://!i', $uri)) ? 
site_url($uri, TRUE) : $uri;
            }
            else
            {
                $site_url = ( ! preg_match('!^\w+://!i', $uri)) ? 
site_url($uri) : $uri;
            }
        }
        else
        {
            if ($ssl)
            {
                $site_url = site_url($uri, TRUE);
            }
            else
            {
                $site_url = site_url($uri);
            }

        }

        if ($title == '')
        {
            $title = $site_url;
        }

        if ($attributes == '')
        {
            $attributes = ' title="'.$title.'"';
        }
        else
        {
            $attributes = _parse_attributes($attributes);
        }
        return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
    }
}

// ---------------------------------------------------------------------
---
//2008-04-21 KUNIHARU Tsujioka update

/**
 * Prep URL
 *
 * Simply adds the http:// part if missing
 *
 * @access  public
 * @param   string  the URL
 * @param   bool    SSL true/false 2008-04-21 KUNIHARU Tsujioka
 * @return  string
 */
if (! function_exists('prep_url'))
{
    function prep_url($str = '', $ssl = FALSE)
    {
        if ($str == 'http://' OR $str == '')
        {
            return '';
        }

        if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 
'https://')
        {
            if ($ssl)
            {
                $str = 'https://'.$str;
            }
            else
            {
                $str = 'http://'.$str;
            }
        }

        return $str;
    }
}

// ---------------------------------------------------------------------
---
//2008-04-21 KUNIHARU Tsujioka update

/**
 * Header Redirect
 *
 * Header redirect in two flavors
 *
 * @access  public
 * @param   string  the URL
 * @param   string  the method: location or redirect
 * @param   bool    the ssl: true/false
 * @return  string
 */
if (! function_exists('redirect'))
{
    function redirect($uri = '', $method = 'location', $ssl = FALSE)
    {
        switch($method)
        {
            case 'refresh':
                header("Refresh:0;url=".site_url($uri, $ssl));
                break;
            default:
                header("Location: ".site_url($uri, $ssl));
                break;
        }
        exit;
    }
}

//2008-04-21 KUNIHARU Tsujioka update

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

?>

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

/**
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category
 * @package    MYNETS_Config Class
 * @author     KUNIHARU Tsujioka <kunit****@gmail*****>
 * @copyright  Copyright (c) 2008 KUNIHARU Tsujioka <kunitsuji @ gmail.
com>
 * @copyright  Copyright (c) 2006-2008 Usagi Project (URL:http://usagi.
mynets.jp)
 * @license    New BSD License
 */

class MYNETS_Config extends CI_Config {

    private $is_ssl = null;

    /**
     * Constructor
     *
     */
    public function __construct()
    {
        parent::__construct();
        //2008-04-21 KUNIHARU Tsujioka
        $this->_is_ssl();
    }

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

    /**
     * Site URL
     *
     * @access  public
     * @param   string  the URI string
     * @param   bool SSL true/false 2008-04-21 KUNIHARU Tsujioka updated
     * @return  string
     */
    public function site_url($uri = '', $ssl = FALSE)
    {
        if (is_array($uri))
        {
            $uri = implode('/', $uri);
        }

        if ($uri == '')
        {
            if ($ssl)
            {
                return $this->slash_item('base_url_ssl').$this->item
('index_page');
            }
            else
            {
                return $this->slash_item('base_url').$this->item
('index_page');
            }
        }
        else
        {
            $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this
->item('url_suffix');
            if ($ssl)
            {
                return $this->slash_item('base_url_ssl').$this->
slash_item('index_page').preg_replace("|^/*(.+?)/*$|", "\\1", $uri).
$suffix;
            }
            else
            {
                return $this->slash_item('base_url').$this->slash_item
('index_page').preg_replace("|^/*(.+?)/*$|", "\\1", $uri).$suffix;
            }
        }
    }

    //2008-04-21 KUNIHARU Tsujioka update
    /**
     * 現在の通信がSSLかどうかを判定する
     * @access plivate
     *
     * @return bool
     *
     */
    private function _is_ssl()
    {
        static $is_ssl;
        if (!isset($is_ssl)) {
            if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
                $is_ssl = true;
            } else {
                $is_ssl = false;
            }
        }
        $this->is_ssl = $is_ssl;
    }

    public function is_ssl()
    {
        return $this->is_ssl;
    }

}
?>

あとは、application/config/config.phpの頭に
$config['base_url_ssl'] = MYNETS_URL_SSL;
でSSLのURL(基本的にbase_urlと同じはず)を記述すれば大丈夫かなと。

ファイル添付がいいのかメールに書いたらいいのかわからずとりあえず。




Codeigniter-users メーリングリストの案内
Zurück zum Archiv-Index