Skip to content

Instantly share code, notes, and snippets.

View vijinho's full-sized avatar

Vijay vijinho

  • England
View GitHub Profile
@vijinho
vijinho / dump_mysql_db_table.sh
Created August 21, 2015 18:22
dump a mysql database table
#/usr/local/bin/bash
DB=$2
HOST=$1
TABLE=$5
USER=$3
PASS=$4
MYSQLDUMP=$(which mysqldump)
MYSQLDUMP_OPTS=" --no-create-db --add-locks --add-drop-table --extended-insert --verbose"
@vijinho
vijinho / running.php
Last active October 1, 2015 13:18
PHP script already running? FreeBSD/Linux compatible
<?php
function running() {
// check if already running, quit if so
exec("ps auxww | grep running.php | grep -v grep", $ps);
if (count($ps) > 1) {
echo "Already running!\n";
return;
}
sleep(60);
}
@vijinho
vijinho / removeable_functions.php
Last active October 30, 2015 13:10
Find PHP legacy functions that are not in use in source code
<?php
/**
* Find unused functions in a legacy project that are safe to remove
*
* @author: Vijay Mahrra <[email protected]>
*/
$folder = 'legacy';
$cmd = "grep -aRi 'function ' $folder/*";
exec($cmd, $results);
$removeable = [];
<?php
// script to parse mac address db at
// https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob_plain;f=manuf
$data = file('vendors.txt');
foreach ($data as $line) {
if (preg_match("/^#.*/", $line)) {
continue;
} elseif (preg_match("/([A-F0-9]+:[A-F0-9]+:[A-F0-9]+)\s+[^#]+#\s+(.+)/i", $line, $matches)) {
@vijinho
vijinho / download-csv.php
Last active February 22, 2016 15:12
Convert an array to a csv file
<?php
$filename = 'report_period_' . date('Y-m-d', $time) . '.csv';
$file = sys_get_temp_dir() . '/' . $filename;
$fp = fopen($file, 'w');
foreach ($csvData as $r) {
fputcsv($fp, $r);
}
fclose($fp);
$size_in_bytes = filesize($file);
@vijinho
vijinho / cards.php
Created February 24, 2016 09:13
test payment card numbers
<?php
$cards = [
'MasterCard0' => '5555555555554444',
'MasterCard1' => '5105105105105100',
'Visa0' => '4111111111111111',
'Visa1' => '4012888888881881',
'Visa2' => '4929000005559',
'Visa3' => '4929000000014',
'Visa4' => '4929000000022',
'Visa5' => '4222222222222',
@vijinho
vijinho / url_resolve.php
Last active September 29, 2018 11:57
unshorten a URL or find the target of a shortened URL and return it if success or status code if no success using PHP
#!/usr/bin/php
<?php
/**
* url_resolve.php - CLI script for resolving url
*
* @author Vijay Mahrra <[email protected]>
* @copyright (c) Copyright 2018 Vijay Mahrra
* @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
*/
@vijinho
vijinho / shell_execute.php
Last active October 1, 2018 21:07
execute a command in php and return the captured output and streams stdin, stdout, stderr
<?php
define('VERBOSE', 1);
define('DEBUG', 1);
//-----------------------------------------------------------------------------
// required commands check
$commands = get_commands([
'find' => 'System command: find',
'curl' => 'https://curl.haxx.se',
'wget' => 'https://www.gnu.org/software/wget/'
@vijinho
vijinho / memoize.php
Created October 30, 2017 22:26
php memoize()
<?php
// https://www.youtube.com/watch?v=M3_xnTK6-pA?t=44m18s
// http://eddmann.com/posts/implementing-and-using-memoization-in-php/
//$memoize = function ($function) {
function memoize($function) {
return function () use ($function) {
static $cache = [];
$args = func_get_args();
$key = md5(serialize($args));
if (empty($cache[$key])) {
@vijinho
vijinho / timer.php
Last active January 6, 2025 13:03
PHP Function Timer with results caching, memoization
<?php
// Implement a memoization function to cache results of expensive function calls.
$timer = function ($function) {
// Create an array to store cached results based on function arguments.
$cache = [];
// Return a closure that wraps the original function.
return function (...$args) use ($function, &$cache) {
// Generate a unique key for the cache based on function arguments.