PHP

[CakePHP 3.x]独自findメソッド2パターン

controller

$this->AdAddress = TableRegistry::get('AdAddress');
$this->AdAddress->find('jeff',['limit' => 30]);
$this->AdAddress = TableRegistry::get('AdAddress');
$this->AdAddress->find('jack');

AdAddressTable.php

<?php
namespace App\Model\Table;

use Cake\ORM\Query;
use Cake\ORM\Table;

class AdAddressTable extends TableEx
{
	public function findJeff(Query $query, array $options)
	{
		return $query->group(['city_id'])->order(['id' => 'ASC'])->all();
	}
	public function findJack(Query $query, array $options)
	{
		return $this->find()->group(['city_id'])->order(['id' => 'ASC'])->limit(3);
	}
}

[PHP]mb_send_mail@XAMPP

mb_send_mail@XAMPP

・php.ini
SMTP=smtp.gmail.com
smtp_port=587
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

・sendmail.ini
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=***@gmail.com
auth_password=***

・GMailアカウント
アカウント->ログインとセキュリティ->安全性の低いアプリの許可:有効

上記設定では送れなかった

・php.ini
SMTP=localhost
smtp_port=25
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

・sendmail.ini
smtp_server=localhost
smtp_port=25
error_logfile=error.log
debug_logfile=debug.log

auth_username=***@gmail.com
auth_password=***

・GMailアカウント
アカウント->ログインとセキュリティ->安全性の低いアプリの許可:有効

stunnelをインストール&起動

上記の設定で送れた

PHPソースは下記

<?php
header('Content-Type: text/html; charset=UTF-8');

mb_language('ja');
mb_internal_encoding("UTF-8");

if (mb_send_mail('****@gmail.com','テスト送信-タイトル','テスト本文','From: ****@gmail.com')) {
echo '送信成功';
} else {
echo '送信失敗';
}
?>

https://www.stunnel.org/downloads.html

[PHP]URLを取得

URLを取得

$url = (empty($_SERVER["HTTPS"]) ? "http://" : "https://") . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]

[PHP]暗号化・複合化

暗号化・複合化

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文を作る

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]File Viewer

File Viewerを制作しました。index.php一つで下記のようになるものです。
php49

1)ダウンロード
2)任意のフォルダへアップ
3)ブラウザでアップしたファイルへアクセス

サンプル

[PHP]配列の値だけの配列を作る

配列の値だけの配列を作る

$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"
}

サンプル