Skip to content

Instantly share code, notes, and snippets.

@divinity76
divinity76 / functions.sh.php
Created October 6, 2024 22:54
PHP bash glue (run php functions in bash)
#!/usr/bin/php
<?php
// how to include these bash functions in a bash script:
// eval "$(/usr/bin/php /path/to/functions.sh.php)";
declare(strict_types=1);
function quote($str)
{
if (str_contains($str, "\x00")) {
throw new \LogicException("null byte found in string");
}
<?php
declare(strict_types=1);
<<<'EXPLANATION'
We need to process all /home/*/conf/web/*/apache2.conf
replacing sections
<FilesMatch \.php$>
SetHandler "proxy:unix:/run/php/php8.2-fpm-idn-tést.eu.sock|fcgi://localhost"
</FilesMatch>"
with
@divinity76
divinity76 / otclient_scripting_cheat_sheet.md
Last active October 5, 2024 23:28
otclient scripting cheat sheet

how to open main container:

local containers = getContainers()
if not containers[0] and getBack() then
  g_game.open(getBack())
end

how to get the target bot's "Danger" count in Lua:

TargetBot.Danger() -- returns int
@divinity76
divinity76 / rector.php
Created March 4, 2024 15:09
rector.php example
<?php
declare(strict_types=1);
return (static function (\Rector\Config\RectorConfig $rectorConfig): void {
$rectorConfig->paths([
'/home/hans/projects/php-ml/',
]);
$rectorConfig->sets([
\Rector\Set\ValueObject\LevelSetList::UP_TO_PHP_82,
@divinity76
divinity76 / merge_csv.php
Created March 2, 2024 08:08
csv merger script
<?php
declare(strict_types=1);
function fix_encoding(string $str): string
{
//return $str;
return mb_convert_encoding($str, 'UTF-8', 'ISO-8859-1');
}
@divinity76
divinity76 / bogostress.cpp
Created February 29, 2024 08:06
bogostress.cpp
#define _GNU_SOURCE
#include <iostream>
#include <pthread.h>
#include <chrono>
#include <vector>
#include <unistd.h>
#include <sched.h>
#include <stdexcept>
#include <climits>
@divinity76
divinity76 / Dockerfile
Created February 8, 2024 14:27
Dockerfile reproduce example
# see https://github.com/php/php-src/issues/12450
FROM ubuntu:20.04
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update -y
RUN apt-get -y full-upgrade;
RUN apt-get -y install apt-utils
RUN apt-get -y install golang build-essential git autoconf bison re2c make cmake automake libtool libpsl-dev libpsl5
RUN bash -c 'set -e;\
git clone -b curl-8_6_0 --single-branch --depth 1 https://github.com/curl/curl.git; \
cd curl; \
@divinity76
divinity76 / b3instructions.php
Created January 25, 2024 05:20
blake3 portable vs optimized
<?php
declare(strict_types=1);
/**
* better version of shell_exec(),
* supporting both stdin and stdout and stderr and os-level return code
*
* @param string $cmd
* command to execute
@divinity76
divinity76 / resource_limits.cpp
Created January 22, 2024 01:13
test system resource limits (ram allocation, open file handles, concurrent threads)
#include <iostream>
#include <fstream>
#include <thread>
#include <vector>
#include <string>
#include <cstdlib>
char **global_argv;
void testMemory() {
std::cout << "Starting Memory Test" << std::endl;
std::vector<char*> allocations;
@divinity76
divinity76 / file_searcher.php
Created January 18, 2024 08:39
file searcher
<?php
declare(strict_types=1);
$dir = realpath(__DIR__ . '/..');
$files = shell_exec("find " . escapeshellarg($dir) . " -print0");
$files = explode("\x00", rtrim($files, "\x00"));
$files = array_filter($files, function ($filepath) {
if (!preg_match("/page/", $filepath)) {
return false;