Skip to content

Instantly share code, notes, and snippets.

View iansltx's full-sized avatar
💣
'); DROP TABLE statuses; --

Ian Littman iansltx

💣
'); DROP TABLE statuses; --
View GitHub Profile
<?php
class MyQuery {
protected function buildCollectionQuery($is_count_query = false) {
$qs = $is_count_query ? $this->baseCountQS : $this->baseCollectionQS;
$qsParams = [''];
if ($this->showInactive === false) {
$qs .= " && u.userStatus = 'active'";
}
@iansltx
iansltx / triples.py
Last active August 29, 2015 14:03
Get a list of all primitive Pythagorean triples with hypotenuse <= Nmax, one per line
# pass the MB of RAM you want to use as your argument
# Nmax = 10,000 * RAM_in_MB; CPU time on an EC2 r3.8xlarge is 60s/1M Nmax
# adapted from http://stackoverflow.com/a/8263898/2476827
import sys
from numpy import mat, array
def gen_pythagorean_triples(max):
u = mat(' 1 2 2; -2 -1 -2; 2 2 3')
a = mat(' 1 2 2; 2 1 2; 2 2 3')
d = mat('-1 -2 -2; 2 1 2; 2 2 3')
@iansltx
iansltx / MyAction.php
Created July 18, 2014 23:42
On-construct ADR content type negotiation
<?php
class MyAction
{
protected $request;
protected $responder;
public function __construct(Aura\Web\Request $request, ResponderInterface $responder) {
// you'd inject other stuff here normally, but let's assume that this is extended elsewhere
$this->request = $request;
@iansltx
iansltx / transform.php
Created August 12, 2014 04:25
Toggl CSV -> grouped by description and project + decimal hours QDPA
<?php
$filename = $argv[1];
if (!file_exists($filename))
die("file not found\n");
$rows = explode("\n", str_replace(["\r\n", "\r"], "\n", trim(file_get_contents($filename))));
$headers = str_getcsv(array_shift($rows));
$data = array_map(function($row) use ($headers) {return array_combine($headers, str_getcsv($row));}, $rows);
usort($data, function($a, $b) {return strtotime($a['Start date']) > strtotime($b['Start date']) ? 1 : -1;});
@iansltx
iansltx / _safari-iframe-cookie-workaround.md
Last active April 7, 2024 15:44
Safari iframe cookie workaround
@iansltx
iansltx / phpexcel_convenience.php
Last active August 29, 2015 14:10
PHPExcel Convenience Functions
<?php // use as-is with PHPExcel, or build a utility class around them
// $addColumn($sheet, 'A', 1, ['value_in_A1', 'value_in_A2']);
$addColumn = function(\PHPExcel_Worksheet $worksheet, $column, $row, $values) {
for ($i = 0, $count = count($values); $i < $count; $i++, $row++)
$worksheet->getCell($column . $row)->setValue($values[$i]);
};
// $addRow($sheet, 'A', 1, ['value_in_A1', 'value_in_B1']);
$addRow = function(\PHPExcel_Worksheet $worksheet, $column, $row, $values) {
@iansltx
iansltx / rerender.js
Created February 3, 2015 08:23
Force element re-render on print
(function() { // force re-render of element_name to get the styling right on-print
var beforePrint = function() {
var el = document.getElementById('element_name'); // e.g. an input inside a <p>...
var p = el.parentNode; // ...which will normally fail to style entirely on-print...
p.removeChild(el); // ...but if you wrap it in a <span> or the like, then remove and re-add the element...
p.appendChild(el); // ...the styles will recalculate and you'll get whatever style you were trying to apply!
};
if (window.matchMedia) {
window.matchMedia('print').addListener(function(mql) {
@iansltx
iansltx / FastForwarder.php
Last active August 29, 2015 14:15
Calculate due dates, taking business days into account
<?php
class FastForwarder
{
protected $skipWhen = [];
protected $numDays = 0;
public static function createWithDays($num_days)
{
$ff = new static;
@iansltx
iansltx / EmailService.php
Last active August 29, 2015 14:15
Trait Injection
<?php
namespace BaseNamespace\Service;
// this should be an interface but I'm lazy
class EmailService
{
/* properties */
public function __construct(/* params */) {
@iansltx
iansltx / MultiPartFromStrings.php
Created May 2, 2015 03:31
Multipart file uploads in PHP from strings
<?php
/**
* PHP's curl extension won't let you pass in strings as multipart file upload bodies; you
* have to direct it at an existing file (either with deprecated @ syntax or the CURLFile
* type). You can use php://temp to get around this for one file, but if you want to upload
* multiple files then you've got a bit more work.
*
* This function manually constructs the multipart request body from strings and injects it
* into the supplied curl handle, with no need to touch the file system.