Skip to content

Instantly share code, notes, and snippets.

.env (and .env.example):
DB_CONNECTION=sqlite
config/database.php:
connections => sqlite
'database' => __DIR__ . '/../storage/app/db.sqlite',
touch storage/app/db.sqlite
@tdebatty
tdebatty / file_get_contents.php
Last active October 27, 2022 12:10
file_get_contents with proxy support
<?php
$context = array();
$proxy = getenv("http_proxy");
if ($proxy !== null) {
$context['http'] = array(
'proxy' => str_replace("http", "tcp", $proxy),
'request_fulluri' => true
);
}
@tdebatty
tdebatty / SumAggregator.java
Created June 9, 2016 07:53
Helper class to perform the sum of a large number of values (sum aggregation).
import java.util.ArrayList;
/**
* Helper class to perform the sum of a large number of values.
* A double value in java only has 15 significant digits. Hence, when performing
* the sum of a large number of double values, the precision of the result
* may be highly altered. For example, if performing the sum of 1E9 values,
* the result only has 6 significant digits. This helper class performs the
* addition using groups of 1E5 values. The result has 10 significant digits,
* for sums of up to 1E10 values.
@tdebatty
tdebatty / get_ip.php
Last active May 12, 2022 08:26
PHP : Get IP from interface
// !! this gist was created in 2015, it doesn't work on most modern distributions !!
// e.g: get_ip("eth0");
function get_ip($interface) {
$interface = escapeshellarg($interface);
$pattern = "/inet addr:(\d+\.\d+\.\d+\.\d+)/";
$text = shell_exec("ifconfig $interface");
preg_match($pattern, $text, $matches);
return $matches[1];
}
@tdebatty
tdebatty / udev-filter-usb
Created September 5, 2014 12:48
Allows you to filter which USB devices may be connected to your system
#! /usr/bin/php
<?php
// Allows you to filter which USB devices may be connected to your system
// To find the ID of devices:
// sudo pkill udevd
// sudo udevd --debug
// Setup:
// Save this file to /opt/udev-filter-usb
@tdebatty
tdebatty / ctph.php
Last active August 29, 2015 13:57
A PHP implementation of the spamsum algorithm for computing context triggered piecewise hashes (CTPH), also called fuzzy hashes
<?php
/**
* A PHP implementation of spamsum algorithm
* https://junkcode.samba.org/ftp/unpacked/junkcode/spamsum/
*
* The spamsum algorithm is also used by ssdeep
* http://ssdeep.sourceforge.net/
*
* Usage:
* $ss = new SpamSum();
@tdebatty
tdebatty / delete_older_than.php
Last active November 15, 2023 07:23
A simple PHP function to delete files older than a given age. Very handy to rotate backup or log files, for example...
<?php
/**
* A simple function that uses mtime to delete files older than a given age (in seconds)
* Very handy to rotate backup or log files, for example...
*
* $dir String whhere the files are
* $max_age Int in seconds
* return String[] the list of deleted files
*/
@tdebatty
tdebatty / NNDescent.php
Last active August 29, 2015 13:56
NN-Descent - A PHP implementation of NN-Descent algorithm : "Efficient k-nearest neighbor graph construction for generic similarity measures"
<?php
/* A quick and dirty PHP implemenation of NNDescent algorithm, as proposed in the paper
* Efficient k-nearest neighbor graph construction for generic similarity measures
* http://dl.acm.org/citation.cfm?id=1963487
*
*/
/**
* The algorithm can be used with any kind of item, as long as some
* similarity metric is defined