Skip to content

Instantly share code, notes, and snippets.

View gundamew's full-sized avatar
🐢
Stepping forward

Steven Chen gundamew

🐢
Stepping forward
  • Taipei City, Taiwan
View GitHub Profile
@gundamew
gundamew / download.php
Last active June 13, 2022 18:50
Download/Upload files with Guzzle.
<?php
$client = new GuzzleHttp\Client();
// Ref: https://guzzle.readthedocs.io/en/latest/request-options.html#sink-option
$client->request('GET', 'http://example.com', [
'sink' => '/tmp/' . sha1(file_get_contents('https://example.com'))
]);
@gundamew
gundamew / example.php
Last active January 19, 2021 10:30
PDO with options.
<?php
function getPDOInstance($host, $name, $user, $password)
{
$dsn = str_replace(
['{HOST}', '{NAME}'],
[$host, $name],
'mysql:host={HOST};dbname={NAME};charset=utf8mb4'
);
@gundamew
gundamew / example.js
Last active January 19, 2021 10:27
IIFE skeleton.
(function($, undefined) {
'use strict';
if (window.className !== undefined) {
return;
}
window.className = {
init: function() {
// code...
<?php
defined( 'ABSPATH' ) || exit;
class WC_REST_API_Key {
// See WC_Auth::create_keys
public static function create( $user_id, $description, $permissions = 'read' ) {
global $wpdb;
@gundamew
gundamew / helper.php
Last active September 27, 2019 08:51
Simple helpers to run tasks before/until the deadline.
if (! function_exists('stop_before')) {
function stop_before(callable $func, DateTimeInterface $dt)
{
$tz = $dt->getTimezone();
$now = new DateTime('now', $tz);
if ($now > $dt) {
call_user_func($func);
}
@gundamew
gundamew / checkout-gitlab-merge-request.sh
Created March 6, 2019 04:08
Checkout GitLab merge request locally.
#!/usr/bin/env bash
set -euo pipefail
# Ref:
# * https://stackoverflow.com/a/44992513
# * https://github.com/jwiegley/git-scripts/blob/master/git-pr
test -z $1 && echo "Merge request number required." 1>&2 && exit 1
git fetch ${2:-origin} merge-requests/$1/head:mr-$1 && git checkout mr-$1
@gundamew
gundamew / update.sh
Created September 1, 2018 07:12
Script to update Homebrew and Composer packages.
#!/usr/bin/env bash
set -euo pipefail
echo "Updating Homebrew..."
brew update && brew upgrade && brew cleanup
echo "Updating Composer..."
composer self-update --clean-backups && composer g update && composer g dump-autoload
#composer g clear-cache
@gundamew
gundamew / restart-docker-containers.sh
Created August 16, 2018 07:07
Stop , remove, and rebuild Docker containers.
#!/usr/bin/env bash
set -euo pipefail
docker stop $(docker ps -aq)
docker rm $(docker ps -aq)
docker rmi $(docker images --format "{{.Repository}}" | grep 'bsdock')
[[ -f "$PWD/docker-compose.yml" ]] && docker-compose up -d --build
@gundamew
gundamew / OpensslAes256.php
Last active June 1, 2016 17:34
An AES encryption implementation in PHP with OpenSSL module.
<?php
// Set cipher method
$cipherMethod = 'AES-256-CBC';
// Set encryption key
// The key length must be 256 bits long
$key = (empty($key)) ? openssl_random_pseudo_bytes(32) : '...';
// Set initialization vector
$iv = (empty($iv)) ? openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipherMethod)) : '...';
@gundamew
gundamew / McryptAes256.php
Last active June 13, 2016 17:31
An AES encryption implementation in PHP with Mcrypt module.
<?php
/**
* An AES encryption implementation in PHP with Mcrypt module.
*
* Key length is 256, CBC mode, and use PKCS#7 padding.
*
* Example:
* $aes = new McryptAes256;
* $encryptedString = $aes->encrypt($rawString);
* $decryptedString = $aes->decrypt($encryptedString);