Skip to content

Instantly share code, notes, and snippets.

View yookoala's full-sized avatar

Koala Yeung yookoala

  • Pixel Action Studio
  • Hong Kong
  • 07:15 (UTC +08:00)
View GitHub Profile
@yookoala
yookoala / endpoint.php
Created May 31, 2023 10:26
A simple PHP API endpoint to process both JSON and POST formdata
View endpoint.php
<?php
$method = $_SERVER['REQUEST_METHOD'] ?? null;
if ($method === 'POST') {
$content_type = preg_match('/^(.+?); (.+?)$/', $_SERVER['HTTP_CONTENT_TYPE'] ?? '', $matches)
? $matches[1]
: ($_SERVER['HTTP_CONTENT_TYPE'] ?? null);
// Parse input data by content type
$input = file_get_contents('php://input');
@yookoala
yookoala / extract-head-and-body.php
Last active May 23, 2022 06:47
A short script to extract the head and body contents from an HTML file to reformat it
View extract-head-and-body.php
<?php
/**
* Extract HTML children of a give tag name from a given document.
*
* @param \DOMDocument $doc The DOM document to extract from.
* @param string $tagName The tag name for extraction (e.g. 'head', 'body')
*
* @return \DOMDocument An DOMElement containing all child elements from the tag specified in $dom.
*/
@yookoala
yookoala / battery-level-check.service
Last active April 7, 2022 07:48
Battery level checking service
View battery-level-check.service
#
# Save this file as: /etc/systemd/system/battery-level-check.service
#
[Unit]
Description=Battery check. If higher than 80% or lower than 40%, notify desktop user
Wants=battery-level-check.timer
[Service]
Type=simple
@yookoala
yookoala / static-method-demo.js
Created February 17, 2022 07:44
A ES6 demonstration of how to call static method in non-static class method
View static-method-demo.js
class DummyParent {
static hello() {
console.log('dummy parent say hello')
}
}
class Dummy extends DummyParent {
static foo() {
console.log('dummy say foo')
}
@yookoala
yookoala / gameSave.php
Last active January 13, 2022 03:22
A simple Monogatari remote storage implementation
View gameSave.php
<?php
namespace Monogatari\RemoteStorage;
/**
* Throw this if a key is not found in a StorageInterface.
*/
class StorageKeyNotFound extends \Exception
{
/**
@yookoala
yookoala / 90-mac-superdrive.rules
Last active February 18, 2024 20:12
udev rules to use Apple SuperDrive on Linux
View 90-mac-superdrive.rules
#
# Apple SuperDrive initialization rule
#
# See: https://gist.github.com/yookoala/818c1ff057e3d965980b7fd3bf8f77a6
ACTION=="add", ATTRS{idProduct}=="1500", ATTRS{idVendor}=="05ac", DRIVERS=="usb", RUN+="/usr/bin/sg_raw %r/sr%n EA 00 00 00 00 00 01"
@yookoala
yookoala / sendmail.php
Created October 28, 2021 03:10
A small script to be used as sendmail for debugging mailing functions in php.
View sendmail.php
<?php
/**
* @file
*
* Mock Sendmail
*
* A small script to be used as sendmail for debugging mailing functions in php.
*
* Usage:
@yookoala
yookoala / ABOUT_MULTIPART_UPLOAD.md
Last active December 9, 2020 15:48
An example to emulate an HTML form POST request with file uploads
View ABOUT_MULTIPART_UPLOAD.md

Multipart Upload with PHP

About

This is a simple example to do a "multipart/form-data" POST request in PHP without any fancy library.

Setup

@yookoala
yookoala / github-release.sh
Last active October 13, 2020 10:25
A script to create / append tagged release with any file as assets
View github-release.sh
#!/bin/bash
BASENAME=$(basename $0)
BASEPATH=$(dirname $(realpath $0))
# Print usage message
function usage {
cat <<EOF
usage: $BASENAME -k <GITHUB_API_TOKEN> -r <GITHUB_REPO_SLUG> -t <TAG> <DIST_PATH> <LABEL>
@yookoala
yookoala / swap.php
Created August 13, 2020 07:53
The simplest syntax to do swaping in PHP
View swap.php
<?php
/**
* This works.
*/
$a = 1; $b = 2;
echo "Before swap: {$a}, {$b}\n";
list($a, $b) = [$b, $a];
echo "After swap: {$a}, {$b}\n";