Skip to content

Instantly share code, notes, and snippets.

View abiusx's full-sized avatar

AbiusX abiusx

View GitHub Profile
@abiusx
abiusx / array_key_map.php
Last active August 25, 2023 18:26
PHP's array_map, but instead of mapping values, maps keys.
<?php
/**
* Array map, but maps values to new keys instead of new values
* @return array same arrays with keys mapped
*/
function array_map_key($callback,$array)
{
$out=array_reduce($array, function ($carry,$val) use ($array,$callback){
$key=call_user_func($callback,$val);
$carry[$key]=$val;
@abiusx
abiusx / twilio_captcha.php
Created September 27, 2018 17:04
Phone voice call captcha powered by Twilio and TwiML (also forwards SMS)
<?php
/**
* Call captcha to prevent spam calls
* Uses Twilio TwiML syntax
* @author abiusx
* @version 1.1
*/
$name="John Doe"; // Put your name here
$number="+14342904141"; // Put your real phone number here
@abiusx
abiusx / 2018tax.php
Created October 30, 2018 01:35
2018 Wage/Self-Employment/Dividend/Investment Tax Calculator (Also Solves for S Corp Optimum Dividend/Salary)
<?php
/**
* @author AbiusX <me@abiusx.com>
* @version 1.0
*/
/**
* Calculate taxes based on salary, self-employment salary,
* dividends and capital gains. Especially useful for S Corps
*
@abiusx
abiusx / youtube-updater.php
Created December 10, 2018 21:57
Youtube Video Updater in Pure PHP
<?php
/**
* This script can be used to modify videos on Youtube programatically.
* It does not need Google API libraries, it merely uses PHP's CURL.
*
* Before starting to work with this, read the comments below for oAuth_token()
* function. It requires you do 3 steps (twice run this code while uncommenting,
* once login on your browser). Once you have done that and updated the constants
* below, you're good to go!
@abiusx
abiusx / sysstat.php
Created August 1, 2019 17:08
System stats (network, disk, memory, sockets, cpu) in PHP for Linux
<?php
/**
* Grab a /proc/X file and parse as key value pairs separated by colon
* @param string $proc name
* @return array
*/
function parse_colon_proc($proc)
{
$res = shell_exec("cat /proc/{$proc}");
$res = explode(PHP_EOL, $res);
@abiusx
abiusx / aes-ctr-256.php
Created August 15, 2019 16:45
AES 256 CTR based on AES 256 ECB on PHP
<?php
function bin2text($binary)
{
return implode(" ", str_split(bin2hex($binary), 32));
}
function aes_256_ctr($data, $key, $iv, &$counter = 0)
{
assert(strlen($data)%32 == 0);
assert(strlen($iv) == 16);
assert(strlen($key) == 32);
@abiusx
abiusx / php-usage
Created April 9, 2020 16:28
Shows usage by user for php-fpm process pool. Useful for shared hosting monitoring of hacked or abusive users.
#!/usr/bin/env php
<?php
$sample_count=$argv[1]??1;
$sleep=$argv[2]??100;
$sleep*=1000;
$users=[];
for ($i=0;$i<$sample_count;++$i)
{
$res=sample();
foreach ($res as $data)
@abiusx
abiusx / glacier-delete-vault.sh
Last active August 31, 2020 19:43
Delete all archives in a AWS S3 Glacier vault so that it can be deleted
#!/bin/bash
VAULT=${1:-myvault}
# Set your profile before by doing aws --profile XXX configure
PROFILE=${2:-myprofile}
REGION=${3:-us-east-1}
SLEEP_TIME=60
# Getting your account ID
ACCOUNTID=$(aws --profile $PROFILE sts get-caller-identity | jq -r ".Account")
@abiusx
abiusx / promise.php
Last active November 19, 2020 23:04
A tutorial on Guzzle Promises in PHP
<?php
/**
* Promise-based programming tutorial.
*
* In this tutorial, we use promises to tackle several concurrent problems and make them run faster.
* Promises are now available in most languages (C#, PHP, Javascript, etc.).
*
* What they do is, instead of doing a long-running, I/O blocking task
* (e.g., read a file, fetch a URL, etc.), they promise to do it in the future
* and return the result.