Skip to content

Instantly share code, notes, and snippets.

View lastguest's full-sized avatar
❄️
I may be slow to respond.

Stefano Azzolini lastguest

❄️
I may be slow to respond.
View GitHub Profile
@lastguest
lastguest / menu_tree_builder.php
Created September 7, 2018 12:51
[PHP] menu_tree_builder
<?php
$menu = [
[ 'menu_item_parent'=> 0, 'title'=>'a' ],
[ 'menu_item_parent'=> 0, 'title'=>'b' ],
[ 'menu_item_parent'=> 0, 'title'=>'c' ],
[ 'menu_item_parent'=>'c', 'title'=>'d' ],
[ 'menu_item_parent'=>'b', 'title'=>'e' ],
[ 'menu_item_parent'=>'e', 'title'=>'f' ],
[ 'menu_item_parent'=>'e', 'title'=>'g' ],
@lastguest
lastguest / webserv.sh
Created December 21, 2017 16:42
[BASH] HTTP Server Responder
#!/bin/bash
SERVER_PORT="${1:-8080}"
function handleRequest(){
# $METHOD = GET|POST
# $FULL_REQUEST = /query/url/complete?with=params
# $REQUEST = /query/url/complete
# render query parameters as QUERY_* environment variables
@lastguest
lastguest / serve.sh
Created December 21, 2017 15:29 — forked from LeonB/serve.sh
One-line bash http server
:;while [ $? -eq 0 ];do nc -vlp 8080 -c'(r=read;e=echo;$r a b c;z=$r;while [ ${#z} -gt 2 ];do $r z;done;f=`$e $b|sed 's/[^a-z0-9_.-]//gi'`;h="HTTP/1.0";o="$h 200 OK\r\n";c="Content";if [ -z $f ];then($e $o;ls|(while $r n;do if [ -f "$n" ]; then $e "<a href=\"/$n\">`ls -gh $n`</a><br>";fi;done););elif [ -f $f ];then $e "$o$c-Type: `file -ib $f`\n$c-Length: `stat -c%s $f`";$e;cat $f;else $e -e "$h 404 Not Found\n\n404\n";fi)';done
@lastguest
lastguest / db-open
Created October 10, 2017 09:48 — forked from helderco/db-open
Script to open a mysql database in Sequel Pro from a service in docker-compose.
#!/bin/bash
set -e
show_help() {
cat << EOF
Usage: ${0##*/} [-u USER] [-p PASS] [-P PORT] [-H HOST] [DATABASE]
${0##*/} -h
Open a standard connection in Sequel PRO.
@lastguest
lastguest / changelog
Created June 26, 2017 10:21
[GIT Changelog] GIT Tag-based Changelog Generator #git #changelog #shell
#!/bin/sh
# GIT CHANGELOG Generator
# Stefano Azzolini <stefano.azzolini@caffeina.com>
#
# Use parameter --skip-head to skip HEAD section
LOG_FORMAT="\`%h\` **%s** _by %an_ "
function changelog_for_tag() {
@lastguest
lastguest / test.tree.php
Last active May 23, 2017 14:29
[CAFFEINA] Exercise #1
<?php
include "tree.php";
$x = new Tree();
$x->bacon = "Pancetta";
$x->pasta["fagioli"] = 3;
$y = clone $x;
$y->pasta = null;
@lastguest
lastguest / memoize.php
Created April 18, 2017 10:27
[Memoization] PHP in a Tweet #tags: php, memoize, optimize, php7
<?php
function M($c){return function(...$p)use($c){static$m=[];return$m[$k=json_encode($p)]??$m[$k]=$c(...$p);};}
@lastguest
lastguest / closure_source.php
Last active February 9, 2017 10:48
[Closure Source] Get Closure source code. #tags: PHP, reflection, source
<?php
function closure_source(Closure $c) {
$rfx = new ReflectionFunction($c); $args = [];
foreach($rfx->getParameters() as $p)
$args[] = ($p->isArray() ? 'array ' : ($p->getClass() ? $p->getClass()->name . ' ' : ''))
. ($p->isPassedByReference() ? '&' : '') . '$' . $p->name
. ($p->isOptional() ? ' = ' . var_export($p->getDefaultValue(), true) : '');
return 'function(' . implode(',', $args) . "){\n"
. implode('',array_slice(file($rfx->getFileName()),
@lastguest
lastguest / shell_echo.func.php
Last active March 23, 2016 09:18
[PHP] shell_echo
<?php
/**
* @example :
* shell_echo("<red>==></red> <b>Hohoho!</b>");
*/
function shell_echo() {
static $col_stack = ['normal'];
@lastguest
lastguest / verifica_codice_fiscale.php
Last active March 8, 2016 08:49
[PHP] verifica_codice_fiscale
<?php
function verifica_codice_fiscale($codfis, $nome='', $cognome='', $maschio=true){
$codfis = strtoupper($codfis);
if (!preg_match('(([A-Z]{6})(\d{2})([A-Z])(\d{2})([A-Z]\d{3})([A-Z0-9]))A', $codfis, $parts)) return false;
list(,$_slug,$_anno,$_mese,$_giorno,$_istat,$_chk) = $parts;
// Checksum
$chk_map = [
['0'=>0,'1'=>1,'2'=>2,'3'=>3,'4'=>4,'5'=>5,'6'=>6,'7'=>7,'8'=>8,
'9'=>9,'A'=>0,'B'=>1,'C'=>2,'D'=>3,'E'=>4,'F'=>5,'G'=>6,'H'=>7,'I'=>8,