[CakePHP3.10] ビューテンプレート

public $autoRender = false; を外すとviewテンプレートが使える様になる

view templateとlayoutがある
layoutにビューテンプレートをはめ込む

src/Template/Hello/index.ctp

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Hello</title>
	<style>
		h1 {
			font-size: 60pt;
			margin: 0px 0px 10px 0px; padding: 0px 20px; color: white;
			background: linear-gradient(to right, #aaa, #fff);
		}
		p { font-size: 14pt; color: #666; }
	</style>
</head>
<body>
	<header class="row">
		<h1><?=$title ?></h1>
	</header>
	<div class="row">
		<p><?=$message ?></p>
	</div>
</body>
</html>

HelloController.php

class HelloController extends AppController {

	public function index(){
		$this->viewBuilder()->autoLayout(false);
		$this->set('title', 'Hello!');
		$this->set('message', 'This is message!');
	}
}

配列で渡す場合

	public function index(){
		$this->viewBuilder()->autoLayout(false);
		$values = [
			'title' => "hello!",
			"message" => "this is message!!!"
		];
		$this->set($values);
	}

autoLayout(false);で自動レイアウトをoffにしている

OK, controllerとviewの関係性はなんとなく分かったぞ!

CakePHP3系

内蔵サーバー
$ php ./bin/cake.php server -H 0.0.0.0 –port 8080

### View
webroot/hello.html

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Hello</title>
	<style>
		h1 {
			font-size: 60pt;
			margin: 0px 0px 10px 0px; padding: 0px 20px; color: white;
			background: linear-gradient(to right, #aaa, #fff);
		}
		p { font-size: 14pt; color: #666; }
	</style>
</head>
<body>
	<header class="row">
		<h1>Welcome!</h1>
	</header>
	<div class="row">
		<p>This is sample HTML page.</p>
	</div>
</body>
</html>

### Controller
src/Controller/HelloController.php

namespace App\Controller;

use App\Controller\AppController;

class HelloController extends AppController {
	public $autoRender = false;

	public function index(){
		echo "<html><body><h1>Hello!</h1>";
		echo "<p>This is sample page.</p></body></html>";
	}
}

http://192.168.56.10:8080/hello
### クエリパラメータ

	public function index(){
		$id = $this->request->query['id'];
		$pass = $this->request->query['pass'];
		echo "<html><body><h1>Hello!</h1>";
		echo "<ul><li>your id: " . $id . "</li>";
		echo "<li>password: " . $pass . "</li></ul>";
		echo "</body></html>";
	}

http://192.168.56.10:8080/hello?id=taro&pass=yamada

パラメータなしに対応
-> if文で制御する

	public function index(){
		$id = "no name";
		if(isset($this->request->query['id'])){
			$id = $this->request->query['id'];
		}
		$pass = "no password";
		if(isset($this->request->query['pass'])){
			$id = $this->request->query['pass'];
		}

		echo "<html><body><h1>Hello!</h1>";
		echo "<ul><li>your id: " . $id . "</li>";
		echo "<li>password: " . $pass . "</li></ul>";
		echo "</body></html>";
	}

オブジェクトをjsonで返す

namespace App\Controller;

use App\Controller\AppController;

class HelloController extends AppController {
	public $autoRender = false;

	private $data = [
		["name"=>"taro", "mail"=>"taro@yamada", "tel"=>"090-999-999"],
		["name"=>"hanako", "mail"=>"hanako@flower", "tel"=>"080-888-888"],
		["name"=>"sachiko", "mail"=>"sachiko@happy", "tel"=>"070-777-777"]
	];

	public function index(){
		$id = 0;
		if(isset($this->request->query['id'])){
			$id = $this->request->query['id'];
		}
		echo json_encode($this->data[$id]);
	}
}

なるほどー ちょっと理解した

[git] hotfix

git hotfixとは?
– リリースされたバージョンで発生したバグを速やかに修正するブランチ
– 修正後すぐmaster, developブランチにマージ

なるほど、そういうことか、特別な機能があるわけではなく、そういう名称で運用するって取り決めのことね。理解した。

[git] branchのマージ

$ ls
program.txt
$ git branch
* master

// ブランチの作成
$ git branch develop
// ブランチの切り替え
$ git checkout develop

// developブランチで開発
$ cat program.txt
9104 商船三井
9107 川崎汽船
// commit
$ git add .
$ git commit -m “develop first commit”

// masterにブランチの切り替え
$ git checkout master
// masterにブランチのマージ
$ git merge develop
$ cat program.txt
9104 商船三井
9107 川崎汽船

$ git branch
develop
* master

「Automatically delete head branches」にチェックを入れておくと、pull requestがmergeされたときにブランチが消える。そうでなければ、branchはそのまま残る。

なるほど、 やってみないとわからんねこれ。

pkcs#5方式

ブロック暗号方式の場合、平文の文字数をブロック長の倍数に合わせるように、末尾に文字を足すなどの処理を行う必要がある。
ブロック暗号方式において文字を足すことをパディングと呼ぶ。

PKCS#7 パディングはRFC5652で定義されたパディング方式
pkcs#5 パディングは8バイトを倍数とした場合のパディング

なるほど、ちょっとモヤモヤしますな..

Rijndael-128とは

AESは鍵長128, 192, 256bitから選択できる
Rijndaelはブロック長も128, 192, 256bitから選択できる

$key = "秘密の合言葉";
$text = "暗号化するメッセージ";

srand();

$size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($size, MCRYPT_RAND);

$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC, $iv);
$hex = bin2hex($encrypted);

$bin = pack("H*", $hex);
$decrptyed = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC, $iv);

echo $text . "=>".rtrim($decrypted);

あれ、なんかおかしい。。

共通鍵暗号と公開鍵暗号とは?

インターネット上でデータ伝送するには、データを暗号化して送信することが一般的
データを暗号化して受信した際にデータの復号を行う
暗号化や復号は暗号化アルゴリズムと鍵によって行われる

### 2種類の暗号化方式
暗号化方式には共通鍵暗号と公開鍵暗号がある
共通鍵暗号は、暗号化と復号に同じ鍵を使用する 。第三者に知られないようにする
アルゴリズムにはRC4, DES, 3DES, AESなどがある AESが主流で無線LANやWPA2でも採用されている
通信接続先ごとに共通鍵を生成する必要がある
公開鍵暗号は、公開鍵と秘密鍵を生成する RSAやEIGamalなどのアルゴリズムがある
ハイブリット方式もある

なるほど、cryptの知識も大分必要だな

/dev/urandomとは

入力元として取ると乱数が返ってくるファイル
適当な値を取るときに使う

$ head -c 200m /dev/urandom > test.txt
$ cat test.txt
?x??y??Q%s}?Gz?¯???$?_?g7T!L??+\?*???f>?$????Q?7?????i%?M??X???? BY?P??”?gO?%Š??O??/??j?n??7??dnZ?…?
r????u??/???,O @ep?????>?;??’y?l?]?\e-).??Ѳ

なんかバグのようなファイルが生成される

PHP checkEmpty, preg_match

private function checkEmpty($key, $message, ErrorCode $errorCode){
	if(self::isEmpty($this->array, $key)){
		throw new InnerException($message, $errorCode);
	}
	return $this->array[$key];
}
if(preg_match("/PHP/", "今PHPを勉強しています。")){
	echo "マッチしました。";
} else {
	echo "マッチしませんでした。";
}

PHP ErrorException

function exceptionErrorHandler($errono, $errstr, $errfile, $errline){
	throw new ErrorException($errstr, $errno, $errfile, $errline);
}
set_error_handler('exceptionErrorHandler');

error_reporting(E_ALL);
try {
	$arr = array();
	print $arr['a'] . "\n";
	strpos();
} catch (Exception $e){
	print $e->getMessage(). "\n";
	print $e->getTraceAsString(). "\n";
}