Skip to content

Instantly share code, notes, and snippets.

View bitsmanent's full-sized avatar

Claudio Alessi bitsmanent

View GitHub Profile
@bitsmanent
bitsmanent / 3base.php
Last active November 10, 2018 20:42
Single function Freebase SDK
function freebase($query, $lang = 'it,en', $limit = 5) {
$isid = (strpos($query, '/m/') !== false);
if($isid)
$limit = 1;
$q = array(
'key' => 'PUT_YOUR_KEY_HERE',
'query' => $query,
'output' => '(name description notable)',
'lang' => $lang,
'limit' => $limit
@bitsmanent
bitsmanent / routing.php
Last active August 29, 2015 14:01
Simple routing system
function route($method, $route, $func = null) {
static $routes = array();
if(!$func) {
$r = null;
$n = '';
$argv = array();
foreach(explode('/', $route) as $arg) {
$n .= ($n == '/' ? $arg : "/$arg");
if($r)
$argv[] = $arg;
@bitsmanent
bitsmanent / humanstime.php
Last active October 17, 2015 10:08
Timestamp to Humans Time
global $HT_DEFAULT_FMTS;
$HT_DEFAULT_FMTS = array(
'year_0' => "An year ago",
'years_0' => "%d years ago",
'year_1' => "An year from now",
'years_1' => "%d years from now",
'month_0' => "A month ago",
'months_0' => "%d months ago",
'month_1' => "A month from now",
'months_1' => "%d months from now",
@bitsmanent
bitsmanent / dvtm-abduco.bash
Last active November 30, 2021 01:52
detach/attach abduco
dt() {
[ -z "$1" ] && n="dvtm-$RANDOM" || n=$1
f="/tmp/abduco.$n"
if [ -e "$f" ]; then
abduco -a "$f"
else
abduco -c "$f" dvtm
fi
}
@bitsmanent
bitsmanent / prepare_form.js
Last active November 10, 2018 20:41
Transform $_POST and $_FILES into a better format to deal with
function prepare_form() {
$key = key($_FILES);
if(!$key)
return;
$files = @$_FILES[$key];
if(@$_POST[$key]) {
$files = array_merge($files, $_POST[$key]);
unset($_POST[$key]);
}
$ret = [];
@bitsmanent
bitsmanent / buildhier.php
Last active November 10, 2018 20:39
Returns a hierachically nidified array from a linear one. Infinite levels of nesting.
function buildhier($items, $pk = 'parent_id', $ck = 'id', $subk = 'children') {
$map = array();
foreach($items as $k => &$item) {
$c = $item[$ck];
$map[$c] = &$item;
}
unset($item);
foreach($items as $item) {
$p = $item[$pk];
if(!$p)
@bitsmanent
bitsmanent / trackxhr
Last active August 22, 2018 17:08
Track XMLHttpRequest (better version here: https://gist.github.com/clamiax/7325ca5c075d954dc61406d7e0cdc7aa)
function trackxhr() {
var xhrsend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function() {
var onready = this.onreadystatechange;
console.log('XHR', 'sending...');
this.onreadystatechange = function() {
if(this.readyState == 4)
console.log('XHR', 'done');
if(onready)
onready.apply(this, arguments);
@bitsmanent
bitsmanent / collects.js
Last active October 11, 2016 19:05
Returns a serializable object of the given elements. Requires deepset().
function collects(elems) {
var i, len, v, keys = null, ret = {};
len = elems ? elems.length : 0;
for(i = 0; i < len; ++i) {
if(!elems[i].name)
continue;
keys = elems[i].name.replace(/\[([^\]]*)\]/g, ",$1").split(',');
v = (elems[i].type == "checkbox" ? (elems[i].checked ? elems[i].value : "") : elems[i].value);
deepset(ret, keys, v);
@bitsmanent
bitsmanent / serialize.js
Last active October 24, 2015 17:19
Encode an object as a string
function serialize(obj, _name) {
var i, pfx, tp, str = [];
if(!obj)
return _name;
tp = typeof obj;
if(obj.length && tp != 'object' && tp != 'string') { /* pure arrays */
for(i = 0; i < obj.length; ++i) {
pfx = (_name ? _name+'['+i+']' : i);
str.push(pfx+'='+obj[i]);
}
@bitsmanent
bitsmanent / xhr.js
Last active January 18, 2017 16:05
Send XMLHttpRequest requests
function xhr(method, hdrs, action, data, callback) {
var r = new XMLHttpRequest();
r.onreadystatechange = function() {
if(r.readyState == 4 && r.status == 200)
callback(JSON.parse(r.responseText)); /* always expect JSON */
};
switch(method.toUpperCase()) {
case "POST":