info

category : PHP
[PHP]暗号化・複合化  @ PHP 2014年4月4日
暗号化・複合化
define('KEY', 'bsrNaTwou3PJJbUHMVp20BW0JFxszbOdTaYTtY2jD26TxNFZ');//getRandomStr(48)で取得
define('IV', 'rfKvr+rLztklJ0/6hc8ZmrILlMLVoan6ltu/SR2Bvzg=');//getCryptIV()で取得

$str='あいうえお';

$crypted_str = getEncrypt($str);
echo $crypted_str;
echo '<br/><br/>';
$decrypted_str = getDecrypt($crypted_str);
echo $decrypted_str;
exit;

function getEncrypt($data) {
    $resource = mcrypt_module_open('rijndael-256', '', 'ofb', '');
    $iv = base64_decode(str_rot13(IV));
    $ks = mcrypt_enc_get_key_size($resource);
    $key = substr(md5(KEY), 0, $ks);
    mcrypt_generic_init($resource, $key, $iv);
    $encrypt = mcrypt_generic($resource, $data);
    mcrypt_generic_deinit($resource);
    mcrypt_module_close($resource);
    return base64_encode($encrypt);
}
function getDecrypt($data) {
    $resource = mcrypt_module_open('rijndael-256', '', 'ofb', '');
    $iv = base64_decode(str_rot13(IV));
    $ks = mcrypt_enc_get_key_size($resource);
    $key = substr(md5(KEY), 0, $ks);
    mcrypt_generic_init($resource, $key, $iv);
    $decrypt = mdecrypt_generic($resource, base64_decode($data));
    mcrypt_generic_deinit($resource);
    mcrypt_module_close($resource);
    return $decrypt;
}
function getCryptIV(){
	$key = getRandomStr(48);
	$td = mcrypt_module_open('rijndael-256', '', 'ofb', '');
	$iv  = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
	$iv = base64_encode($iv);
	mcrypt_module_close($td);
	return $iv;
}
function getRandomStr($length=8){
	$str='';
	for ($i = 0, $str = null; $i < $length; ) {
		$num = mt_rand(0x30, 0x7A);
		if ((0x30 <= $num && $num <= 0x39) || (0x41 <= $num && $num <= 0x5A) || (0x61 <= $num && $num <= 0x7A)) {
			$str .= chr($num);
			$i++;
		}
	}
	return $str;
}
[PHP]INSERT文を作る  @ PHP , MySQL , SQLite3 2014年4月3日
INSERT文を作る
echo getInsertQueryByKey('system_table', array('name','gender'), array( array('jeff','male'), array('pola','female') ) );

function getInsertQueryByKey($tableName,$columns,$datas){

	//SQLite古いバージョン
	$w=array();

	$column = implode(',', $columns);
	$query="INSERT INTO {$tableName} ({$column}) SELECT ";
	$tmp=array();
	foreach ($columns as $index => $columnName) {
		$tmp[] = "'". $datas[0][$index] . "'" . ' AS ' . $columnName;
	}
	$query .= implode(', ', $tmp);
	$w[]=$query;

	foreach ($datas as $index => $data) {
		if($index<1)continue;
		$tmp=array();
		foreach ($columns as $index2 => $columnName) {
			$tmp[]=$data[$index2];
		}
		$w[]="'".implode("', '", $tmp)."'";
	}

	return implode(' UNION ALL SELECT ', $w).';';

	/* MySQL or SQLite(バージョン 3.7.11以降)
	$queryValues=array();
	foreach ($datas as $index => $value) {
		$values=array();
		foreach ($value as $index2 => $value2) {
			$values[]="'".$value2."'";
		}
		$queryValues[]='('.implode(',', $values).')';
	}

	$column=implode(',', $columns);
	$value=implode(',', $queryValues);
	$sql="INSERT INTO {$tableName} ({$column}) VALUES {$value};";
	return $sql;
	*/
}
[PHP]配列の値だけの配列を作る  @ PHP 2014年2月22日
配列の値だけの配列を作る
$data=array();
$data['jack']=array( 'gender'=>'male', 'size'=>198, 'status'=>array('QK',32,'skyhigh','log'=>array('20120303','20120529','20121111') ) );
$data['jeff']=array( 4,87,8,4,3 );
$data['pole']=array( 'address'=>'earth', 'phone number'=>'39028502753' );

echo '<pre>';
var_dump( getValuesByArray($data) );
echo '</pre>';

function getValuesByArray($targetArray){
    if( !is_array($targetArray) ){
        return array($targetArray);
    }
    $tmp=array();
    foreach($targetArray as $key => $value){
        $tmp=array_merge($tmp, getValuesByArray($value) );
    }
    return $tmp;
}


出力結果
array(15) {
  [0]=>
  string(4) "male"
  [1]=>
  int(198)
  [2]=>
  string(2) "QK"
  [3]=>
  int(32)
  [4]=>
  string(7) "skyhigh"
  [5]=>
  string(8) "20120303"
  [6]=>
  string(8) "20120529"
  [7]=>
  string(8) "20121111"
  [8]=>
  int(4)
  [9]=>
  int(87)
  [10]=>
  int(8)
  [11]=>
  int(4)
  [12]=>
  int(3)
  [13]=>
  string(5) "earth"
  [14]=>
  string(11) "39028502753"
}

サンプル
[CakePHP 2.x]テーブルのカラム名を取得  @ PHP , CakePHP 2.x 2013年11月16日
テーブルのカラム名を取得
$this->Model->getColumnTypes();

Controller
$columnTypes=$this->SystemTest->getColumnTypes();
$columnNames=array_keys($columnTypes);
$this->set('columnTypes', $columnTypes);
$this->set('columnNames', $columnNames);

View
columnTypes<br/>
<?php
$tmp=var_export($columnTypes,true);
echo str_replace("\n", "<br/>", $tmp);
?>
<br/><br/>
columnNames<br/>
<?php
$tmp=var_export($columnNames,true);
echo str_replace("\n", "<br/>", $tmp);
?>

Sample