$arr1 = ['color'=>'red', 10=>1, 0=>3]; $arr2 = ['a', 'b', 'color'=>'green', 5]; $result = $arr1 + $arr2; print_r($arr1); print_r($arr2); print_r($result);
Array ( [color] => red [10] => 1 [0] => 3 ) Array ( [0] => a [1] => b [color] => green [2] => 5 ) Array ( [color] => red [10] => 1 [0] => 3 [1] => b [2] => 5 )]]>
json_encde( $hoge, JSON_UNESCAPED_UNICODE );]]>
今回の用途としては、ユーザ認証クラスを作成したのだが、
使用するデータベースの構成によってはカラム名が異なったりと使いまわすことが出来ない。
なので、セッションの継続やログアウト処理は不動のメソッドとして作成し、
ユーザ認証処理のみクラス初期化時にコールバック関数として渡すようにした。
実際には、コールバック関数でSQLを実行させるのだが、
今回は分かりやすいように四則計算に置き換えた。
class Auth{ public function __construct( $cb ){ $this->functions['foo'] = $cb->bindTo( $this, get_class($this) ); } public function __call( $method, $args ){ $cb = $this->functions[ $method ]; return call_user_func_array( $cb, $args ); } } $cb1 = function( $a, $b){ echo $a + $b; }; $auth1 = new Auth($cb1); $auth1->foo( 2, 3 ); /* 5 */ $cb2 = function( $a, $b ){ echo $a * $b; }; $auth2 = new Auth($cb2); $auth2->foo( 2, 3 ); /* 6 */
※fooは適宜好きなメソッド名に変えてかまわない。
]]>今回使用したSwitchBotはこちら。
指紋認証でアンロックができるので便利なのではあるが、
家に近づくだけで自動的にアンロックさせたかった。
(両手に荷物を持っている時に便利)
【ロック状態を確認する】
TaskerからPHPへはPOSTでtoken,secret,deviceIDを送信する。
deviceIDはここから調べることが出来る。
$token = isset($_POST['token']) ? htmlspecialchars($_POST['token'], ENT_QUOTES, 'UTF-8') : ""; $secret = isset($_POST['secret']) ? htmlspecialchars($_POST['secret'], ENT_QUOTES, 'UTF-8') : ""; $deviceId = isset($_POST['deviceId']) ? htmlspecialchars($_POST['deviceId'], ENT_QUOTES, 'UTF-8') : ""; $time = (string)floor( microtime(true) * 1000 ); $nonce = "xyzzy"; $sha256 = hash_hmac( 'sha256', "{$token}{$time}{$nonce}", $secret, true); $header = []; $header[] = "Authorization:{$token}"; $header[] = "t:{$time}"; $header[] = "sign:".base64_encode($sha256); $header[] = "nonce:{$nonce}"; $header[] = "Content-Type:application/json"; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "https://api.switch-bot.com/v1.1/devices/{$deviceId}/status"); curl_setopt($curl, CURLOPT_POST, 'true'); curl_setopt($curl, CURLOPT_HEADER, 'true'); curl_setopt($curl, CURLINFO_HEADER_OUT, 'true'); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 'true'); curl_setopt($curl, CURLOPT_HTTPHEADER, $header); header("Content-Type: application/json; charset=utf-8"); echo curl_exec( $curl );
【施錠を操作する】
TaskerからPHPへはPOSTでtoken,secret,deviceID,cmdを送信する。
cmdは”lock”もしくは”unlock”
deviceIDはここから調べることが出来る。
$token = isset($_POST['token']) ? htmlspecialchars($_POST['token'], ENT_QUOTES, 'UTF-8') : ""; $secret = isset($_POST['secret']) ? htmlspecialchars($_POST['secret'], ENT_QUOTES, 'UTF-8') : ""; $deviceId = isset($_POST['deviceId']) ? htmlspecialchars($_POST['deviceId'], ENT_QUOTES, 'UTF-8') : ""; $cmd = isset($_POST['cmd']) ? htmlspecialchars($_POST['cmd'], ENT_QUOTES, 'UTF-8') : ""; $time = (string)floor( microtime(true) * 1000 ); $nonce = "xyzzy"; $sha256 = hash_hmac( 'sha256', "{$token}{$time}{$nonce}", $secret, true); $header = []; $header[] = "Authorization:{$token}"; $header[] = "t:{$time}"; $header[] = "sign:".base64_encode($sha256); $header[] = "nonce:{$nonce}"; $header[] = "Content-Type:application/json"; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "https://api.switch-bot.com/v1.1/devices/{$deviceId}/commands"); curl_setopt($curl, CURLOPT_POST, 'true'); curl_setopt($curl, CURLOPT_POSTFIELDS, '{"command": "'.$cmd.'","parameter": "default","commandType": "command"}'); curl_setopt($curl, CURLOPT_HEADER, 'true'); curl_setopt($curl, CURLINFO_HEADER_OUT, 'true'); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 'true'); curl_setopt($curl, CURLOPT_HTTPHEADER, $header); header("Content-Type: application/json; charset=utf-8"); echo curl_exec( $curl );]]>
$tokenと$secretはSwitchbotのアプリを開いて
プロフィール > 設定 > 開発者向けオプションで確認可能。
なお、開発者向けオプションが非表示の場合は、アプリバージョンを10回ほど連打すると表示される。
$timeは13桁のタイムスタンプ。
$nonceは任意の文字列。
$sign = base64_encode( hash_hmac('sha256' ,$token . $time . $nonce ,$secret ,true ) );
ちなみに、サクッとデバイス一覧が欲しい場合はこちらから取得可能。
]]>新しいサーバに引っ越ししたら自動的にサムネイルを作成してくれず
投稿画面に大きな画像が表示される事に・・・・
phpinfoでチェックします。
[ gd ]の項目がない・・・
インストールを試みます
sudo dnf install php-gd エラー: 問題: パッケージ php81-php-gd-8.1.10-1.el8.remi.x86_64 には libgd.so.103()(64bit) が必要ですが、どのプロバイダーからもインストールできません - ジョブの最良アップデート候補をインストールできません - libraqm.so.0()(64bit) が提供されません gd3php-2.3.3-4.el8.remi.x86_64 に必要です - libimagequant.so.0()(64bit) が提供されません gd3php-2.3.3-4.el8.remi.x86_64 に必要です - libraqm.so.0()(64bit) が提供されません gd3php-2.3.3-8.el8.remi.x86_64 に必要です - libimagequant.so.0()(64bit) が提供されません gd3php-2.3.3-8.el8.remi.x86_64 に必要です - libavif.so.14()(64bit) が提供されません gd3php-2.3.3-8.el8.remi.x86_64 に必要です (インストール不可のパッケージをスキップするには、'--skip-broken' を追加してみてください または、'--nobest' を追加して、最適候補のパッケージの みを使用しないでください)
なんか色々足りてない
恐る恐る足りてないものを探してみます
dnf list --enablerepo=remi,epel libraqm libraqm.x86_64 0.7.0-4.el8 epel
あった!
早速レポジトリを指定してインストールしてみます
sudo dnf install -y libraqm --enablerepo=epel インストール: libraqm x86_64 0.7.0-4.el8 epel 19 k
入った!
でゅふふw
他の全てもチェックしてインストールします。
dnf list --enablerepo=remi,epel libimagequant libavif libavif.x86_64 0.10.1-3.el8 epel libimagequant.x86_64 2.12.5-1.el8 epel sudo dnf install -y libimagequant libavif --enablerepo=epel インストール: libavif x86_64 0.10.1-3.el8 epel 76 k libimagequant x86_64 2.12.5-1.el8 epel 63 k 依存関係のインストール: libaom x86_64 3.1.1-1.el8 epel 1.7 M libdav1d x86_64 0.5.2-1.el8 epel 347 k svt-av1-libs x86_64 0.8.7-1.el8 epel 4.7 M
再度php-gdをインストールしてみます
sudo dnf install php-gd インストール: php-gd x86_64 8.1.10-1.el8.remi remi-modular 105 k アップグレード: php-cli x86_64 8.1.10-1.el8.remi remi-modular 5.3 M php-common x86_64 8.1.10-1.el8.remi remi-modular 1.3 M php-fpm x86_64 8.1.10-1.el8.remi remi-modular 1.8 M php-mbstring x86_64 8.1.10-1.el8.remi remi-modular 524 k php-mysqlnd x86_64 8.1.10-1.el8.remi remi-modular 255 k php-pdo x86_64 8.1.10-1.el8.remi remi-modular 163 k php-xml x86_64 8.1.10-1.el8.remi remi-modular 252 k 依存関係のインストール: gd3php x86_64 2.3.3-8.el8.remi remi-safe 148 k
入った!
WordPressにアップロードしても、フルサイズ以外を選べる!
めでたしめでたし
]]>die(var_dump([ __FILE__, __LINE__, $hoge ]));
__FILE__や__LINE__は、困った時にあなたを助けてくれる魔法のおまじない。
最小構成で使用したい場合はこちら
die(var_dump($hoge));
ちな、JSON出力したい場合はこちら
header("Content-type:text/json"); echo json_encode([ __FILE__, __LINE__, $hoge ]);]]>
有効期限が切れますよーっと言うメールなんだけど、
そもそもcrontabで自動化してるんだけどなー。
$ sudo crontab -e 0 4 1 * * /usr/bin/certbot renew
うん。
ちゃんと入ってる。
手動で実行してみる。
$ sudo /usr/bin/certbot renew Saving debug log to /var/log/letsencrypt/letsencrypt.log - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Processing /etc/letsencrypt/renewal/hogehoge.domain.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Cert is due for renewal, auto-renewing... Plugins selected: Authenticator manual, Installer None Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org Renewing an existing certificate Performing the following challenges: dns-01 challenge for hogehoge.domain Waiting for verification... Cleaning up challenges Error output from txtdelete.php: PHP Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /usr/local/src/DirectEdit-hogehoge.domain/txtdelete.php on line 75 PHP Warning: file_get_contents(): Failed to enable crypto in /usr/local/src/DirectEdit-hogehoge.domain/txtdelete.php on line 75 PHP Warning: file_get_contents(https://www.mydns.jp/directedit.html): failed to open stream: operation failed in /usr/local/src/DirectEdit-hogehoge.domain/txtdelete.php on line 75
ん?
なんかエラー吐いてる。
Let’s Encryptが発行したDNS-01のトークンをmydnsに書き込むphpでエラーが出ているようだ。
もしやmydnsさんSSL切れてる?
それかLet’s EncryptのルートCA期限切れ問題かな??
ま、いいや。
証明書のチェックを飛ばそう。
mydnsにTXTレコードを書き込むプログラム2つを修正する。
「txtregist.php」と「txtdelete.php」
ほぼ同じ作業なので、txtregist.phpのみ記載する。
$ sudo cp /usr/local/src/DirectEdit-hogehoge.domain/txtregist.php /usr/local/src/DirectEdit-hogehoge.domain/txtregist.php.`date +%Y%m%d` $ sudo vi /usr/local/src/DirectEdit-hogehoge.domain/txtregist.php # 68行目に$POST_OPTION = array( 'http' => があるので、その直後の73行目あたりにこの2行を追記 $POST_OPTIONS['ssl']['verify_peer'] = false; $POST_OPTIONS['ssl']['verify_peer_name'] = false;
この2つを保存して、手動で実行させてみる。
$ sudo /usr/bin/certbot renew (略) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Congratulations, all renewals succeeded. The following certs have been renewed: /etc/letsencrypt/live/hogehoge.domain/fullchain.pem (success) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
うん。
うまくいった。
めでたしめでたし。