<?php
/**
* セキュリティクラス
*
* 各種のセキュリティ対策を行うクラス
*
* @access public
* @create 2012/01/15
* @version 1.0
*/
class security
{
/**
* HTML出力用のエスケープ処理を行う(XSSで使う)
*
* @access public
* @param mixed $val エスケープしたい値
* @return mixed エスケープされた値
*/
static public function html_escape($val)
{
if (true === is_array($val))
{
// 配列の時
$arr = array();
foreach ($val as $key => $val)
{
// 再帰呼び出しで値をエスケープしてから値をセット
$arr[$key] = self::html_escape($val);
}
return $arr;
}
else
{
// 配列以外の時
if (true === is_null($val))
{
// null
return null;
}
else if (true === is_bool($val))
{
// 論理型
return $val;
}
else
{
// 上記以外の時のみエスケープ
return htmlspecialchars($val, ENT_QUOTES, mb_internal_encoding());
}
}
}
/**
* 第三者が知り得ない秘密情報(トークン)の値を取得する(CSRFで使う)
*
* @access public
* @return string トークン値
*/
static public function get_token()
{
return session_id();
}
/**
* トークンの値をチェックする(CSRFで使う)
*
* @access public
*/
static public function check_token()
{
// トークン確認
if (true === isset($_POST['token']))
{
// hiddenからPOSTされたトークンとセッションIDが違う場合は不正な遷移
if (0 !== strcmp($_POST['token'], self::get_token()))
{
throw new custom_exception('トークン値相違', 1);
}
}
else
{
// hiddenからPOSTされたトークンが未設定時も不正な遷移
throw new custom_exception('トークン値未設定', 1);
}
}
}
<?php
/**
* 論理値専用のテンプレート置換処理クラス
*
* HTMLテンプレート内の論理値の部分の置換を行うクラス
*
* @access public
* @create 2011/01/30
* @version 1.0
*/
class template_convert_bool
{
/**
* テンプレート置換ロジック実行
*
* @access public
* @param view $view ビュークラスインスタンス
*/
public function convert_template($view)
{
$output_html = $view->get_output_html();
// 区切り開始文字列の直前までの文字列
$output_buf = mb_strstr($output_html, '|||', true);
// 区切り開始文字列以降の文字列
$after = mb_strstr($output_html, '|||');
// 区切り開始文字列以降の文字列を、区切り開始文字列で分割した配列
$after_array = explode('|||', $after);
// テンプレート側で設定してある名前
$name = $after_array[1];
// 表示するHTML部分
$body = $after_array[2];
// 置換対象のデータ
$data = $view->get_action()->get_template_convert()->get_bool_array_val($name);
if (true === isset($data))
{
if (true === is_array($data))
{
// ループ
$row_size = count($data);
// 表示する行数分繰り返す
for ($i = 0; $i < $row_size; $i++)
{
// 初期化
$body_buf = $body;
// 置換対象文字列';;;'が存在するまで繰り返し
// $data_beforeは、置換対象文字列の直前までの文字列
while (false !== ($data_before = mb_strstr($body_buf, ';;;', true)))
{
// 置換対象文字列で分割した配列
$data_array = explode(';;;', $body_buf);
// 置換するデータ
$data_val = $data[$i][$data_array[1]];
// 最初に見つかった置換対象文字列の直後からの文字列
$data_after = mb_substr(mb_strstr(mb_substr(mb_strstr($body_buf, ';;;'), 3), ';;;'), 3);
// 1ヶ所の置換が終了したので、文字列をくっつけていく
$body_buf = $data_before . $data_val . $data_after;
}
// 1行分の置換が終了したので、文字列をくっつけていく
$output_buf .= ltrim($body_buf);
}
}
else
{
// 単一
$output_buf .= ltrim($body);
}
}
// 区切り終了文字列の直後からの文字列をくっつけていく
$output_buf .= ltrim(mb_substr(mb_strstr(mb_strstr($after, $after_array[3]), '|||'), 3));
$view->set_output_html($output_buf);
}
}
<?php
/**
* データベースアクセス専用の基底クラス
*
* データベースアクセス処理に関する共通処理を定義
*
* @access public
* @create 2011/09/30
* @version 1.0
*/
class dao
{
/**
* コンストラクタ
*
* @access public
*/
public function __construct()
{
$this->init();
}
/**
* 初期化処理
*
* @access protected
*/
protected function init()
{
$this->set_db_handle(null);
$this->set_model(null);
$this->set_config(null);
}
/**
* DBハンドルインスタンス設定
*
* @access public
* @param db_handle $db_handle DBハンドルインスタンス
*/
public function set_db_handle($db_handle)
{
$this->db_handle = $db_handle;
}
/**
* DBハンドルインスタンス取得
*
* @access protected
* @return db_handle DBハンドルインスタンス
*/
protected function get_db_handle()
{
return $this->db_handle;
}
/**
* モデルインスタンス設定
*
* @access public
* @param model $model モデルインスタンス
*/
public function set_model($model)
{
$this->model = $model;
}
/**
* モデルインスタンス取得
*
* @access protected
* @return model モデルインスタンス
*/
protected function get_model()
{
return $this->model;
}
/**
* 設定ファイルクラスインスタンス設定
*
* @access public
* @param config $config 設定ファイルクラスインスタンス
*/
public function set_config($config)
{
$this->config = $config;
}
/**
* 設定ファイルクラスインスタンス取得
*
* @access protected
* @return config 設定ファイルクラスインスタンス
*/
protected function get_config()
{
return $this->config;
}
/**
* DBハンドルインスタンス
*
* @access private
*/
private $db_handle;
/**
* モデルインスタンス
*
* @access private
*/
private $model;
/**
* 設定ファイルクラスインスタンス
*
* @access private
*/
private $config;
}
<?php
require_once('authentication.php');
/**
* 認証管理クラス
*
* IDとパスワードによる認証を管理するクラス
*
* @access public
* @create 2011/09/30
* @version 1.0
*/
class authentication_id_pass extends authentication
{
/**
* コンストラクタ
*
* @access public
*/
public function __construct()
{
parent::__construct();
$this->init();
}
/**
* 初期化処理
*
* @access protected
*/
protected function init()
{
$this->set_id(null);
$this->set_pass(null);
$this->set_sql(null);
$this->set_auth_data_type(null);
}
/**
* 認証用ID設定
*
* @access public
* @param string $id 認証用ID
*/
public function set_id($id)
{
$this->id = $id;
}
/**
* 認証用ID取得
*
* @access protected
* @return string 認証用ID
*/
protected function get_id()
{
return $this->id;
}
/**
* パスワード設定
*
* @access public
* @param string $pass パスワード
*/
public function set_pass($pass)
{
$this->pass = $pass;
}
/**
* パスワード取得
*
* @access protected
* @return string パスワード
*/
protected function get_pass()
{
return $this->pass;
}
/**
* 認証用のSQL設定
*
* @access public
* @param string $sql 認証用のSQL
*/
public function set_sql($sql)
{
$this->sql = $sql;
}
/**
* 認証用のSQL取得
*
* @access protected
* @return string 認証用のSQL
*/
protected function get_sql()
{
return $this->sql;
}
/**
* 認証に使うカラムのデータ型設定
*
* @access public
* @param string $auth_data_type 認証に使うカラムのデータ型
*/
public function set_auth_data_type($auth_data_type)
{
$this->auth_data_type = $auth_data_type;
}
/**
* 認証に使うカラムのデータ型取得
*
* @access protected
* @return string 認証に使うカラムのデータ型
*/
protected function get_auth_data_type()
{
return $this->auth_data_type;
}
/**
* ジョーアカウント判定
*
* @access public
* @return boolean ジョーアカウントだったらtrueを返す
*/
public function is_joe_account()
{
return 0 === strcmp($this->get_id(), $this->get_pass());
}
/**
* 単純なパスワードかどうかの判定
*
* @access public
* @return boolean 単純なパスワードだったらtrueを返す
*/
public function is_simple_password()
{
return 0 === strcasecmp('password', $this->get_pass());
}
/**
* ユーザー認証判定
*
* @access public
* @return boolean 認証OKならtrueを返す
*/
public function is_authentication()
{
$ret = false;
// SQL文セット
$this->get_db_handle()->set_prepare_query($this->get_sql());
// パラメータをバインドする為に配列化させる
$param_array = $this->set_param_array_for_is_authentication();
// プリペアドステートメントのパラメータに変数をバインド
$this->get_db_handle()->set_bind_param($this->get_auth_data_type(), $param_array);
// SQL実行
$this->get_db_handle()->execute_query();
// 結果を保存
$this->get_db_handle()->set_store_result();
// 結果を判断
if (0 < $this->get_db_handle()->get_num_rows())
{
// 結果有
$ret = true;
}
// プリペアドステートメントをクローズ
$this->get_db_handle()->stmt_close();
return $ret;
}
/**
* パラメータをセット(ユーザー認証用)
*
* @access protected
* @return array プリペアドステートメントのパラメータ格納配列
*/
protected function set_param_array_for_is_authentication()
{
$param_array = array();
// ログインID
$param_array[0] = $this->get_id();
// パスワード
$param_array[1] = $this->get_password_to_hash();
return $param_array;
}
/**
* パスワードのハッシュ値を取得する
*
* @access protected
* @return string ハッシュ化されたパスワード
*/
protected function get_password_to_hash()
{
// ソルトを生成
$salt = $this->create_salt();
$hash = '';
$stretching_count = $this->get_config()->search('stretching_count');
for ($i = 0; $i < $stretching_count; $i++)
{
// ストレッチングを行う
$hash = hash('sha512', $hash . $this->get_pass() . $salt);
}
return $hash;
}
/**
* ソルトを生成する
*
* @access protected
* @return string 生成されたソルト
*/
protected function create_salt()
{
return $this->get_id() . pack('H*', $this->get_config()->search('salt'));
}
/**
* 認証用ID
*
* @access private
*/
private $id;
/**
* パスワード
*
* @access private
*/
private $pass;
/**
* 認証用のSQL
*
* @access private
*/
private $sql;
/**
* 認証に使うカラムのデータ型
*
* @access private
*/
private $auth_data_type;
}
<?php
/**
* 認証管理クラス
*
* ユーザー認証に関連する処理を定義した基底クラス
*
* @access public
* @create 2011/09/30
* @version 1.0
*/
abstract class authentication
{
/**
* コンストラクタ
*
* @access public
*/
public function __construct()
{
$this->init();
}
/**
* 初期化処理
*
* @access protected
*/
protected function init()
{
$this->set_db_handle(null);
$this->set_config(null);
}
/**
* DBハンドルインスタンス設定
*
* @access public
* @param db_handle $db_handle DBハンドルインスタンス
*/
public function set_db_handle($db_handle)
{
$this->db_handle = $db_handle;
}
/**
* DBハンドルインスタンス取得
*
* @access protected
* @return db_handle DBハンドルインスタンス
*/
protected function get_db_handle()
{
return $this->db_handle;
}
/**
* 設定ファイルクラスインスタンス設定
*
* @access public
* @param config $config 設定ファイルクラスインスタンス
*/
public function set_config($config)
{
$this->config = $config;
}
/**
* 設定ファイルクラスインスタンス取得
*
* @access protected
* @return config 設定ファイルクラスインスタンス
*/
protected function get_config()
{
return $this->config;
}
/**
* ユーザー認証判定
*
* @access public
*/
abstract public function is_authentication();
/**
* DBハンドルインスタンス
*
* @access private
*/
private $db_handle;
/**
* 設定ファイルクラスインスタンス
*
* @access private
*/
private $config;
}
Author:kitoku
「奇特」を目指しているITエンジニアです。ホームページ:http://www.kitoku-magic.net/