<?php
/*
 * framework-spider
 * spider/module/AutoEncode.class.php
 * 
 * CopyRight(C)Framework-Spider Developer Team. 2010. All Right Reserved. 
 * URL         : http://sourceforge.jp/projects/frameworkspider/
 * Mail        : frameworkspider-dev@lists.sourceforge.jp
 * Auther      : Masanori Nakashima
 * Modifier    : Masanori Nakashima
 * Last Updated: 2010.06.23
 * 
 */
require_once( dirname(dirname(__FILE__))
	.DIRECTORY_SEPARATOR.'ModuleBase.class.php');
/**
 * spider：POSTおよびGETで入力されたパラメータの文字コードを全てUTF-8に変換する
 * アクションモジュールクラス
 * 
 * @package spider spider/moduleパッケージ
 * @version 1.2.00
 * @copyright Copyright &copy; 2008, <m_nakashima@users.sourceforge.jp> http://sourceforge.jp/projects/shopformer/
 * @author  m.nakashima <m_nakashima@users.sourceforge.jp>
 * @since PHP 4.3
 */
class spider_module_AutoEncode extends spider_ModuleBase {
	function spider_module_AutoEncode() {
	}
	function execute( & $request ) {
		$vars			= $_POST;
		$strings		= print_r($vars,true);
		$vars			= $_GET;
		$strings		.= print_r($vars,true);
		$vars			= null;
		$inputCharset	= mb_detect_encoding($strings,'auto');
		if( $inputCharset != 'UTF-8' ) {
			// POSTパラメータ
			$this->encodeHash( $_POST,'UTF-8', $inputCharset );
			// GETパラメータ
			$this->encodeHash( $_GET,'UTF-8', $inputCharset );
		}
		$request->setAttribute( 'postparams', $_POST );
		$request->setAttribute( 'getparams', $_GET );
	}
	/**
	 * 多次元配列を指定文字セットにエンコードします
	 */
	function encodeHash( & $hash,$toChar='UTF-8', $fromChar ) {
		if( is_array($hash) ) {
			foreach( $hash as $key => $val ) {
				$encKey	= mb_convert_encoding( $key, $toChar, $fromChar );
				if( is_array($val) ) {
					$this->encodeHash( $hash[$key],$toChar,$fromChar );
					$hash[$encKey]	= $hash[$key];
				} else {
					$val			= mb_convert_encoding( $val, $toChar, $fromChar );
					$hash[$key]		= $val;
					$hash[$encKey]	= $val;
				}
			}
		}
	}
}
?>