Skip to content

Instantly share code, notes, and snippets.

View Zegnat's full-sized avatar

Martijn van der Ven Zegnat

View GitHub Profile
@Zegnat
Zegnat / slow_compare.php
Created July 5, 2013 19:43
For secure and slow string comparisons. This helps against timing attacks on secret strings.
<?php
function slow_compare($str1, $str2, $length = null) {
$out = ($str1_len = strlen($str1 .= chr(0))) - ($str2_len = strlen($str2 .= chr(0)));
if (!is_int($length)) $length = $str1_len;
for ($i = 0; $i < $length; $i++)
$out |= ord($str1[$i % $str1_len]) ^ ord($str2[$i % $str2_len]);
return 0 === $out;
}
@Zegnat
Zegnat / Bcrypt.php
Last active December 19, 2015 09:49
Simple PHP 5.3+ Bcrypt class
<?php
/*
Originally by Marco Arment <me@marco.org>.
Edited by Martijn van der Ven <martijn@zegnat.net>.
This code is released in the public domain.
THERE IS ABSOLUTELY NO WARRANTY.
This class is aimed at PHP versions over 5.3 and below 5.5.
@Zegnat
Zegnat / fote.svg
Last active December 17, 2015 12:19
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@Zegnat
Zegnat / gmailprintcleaner.txt
Created April 28, 2013 10:47
Adblock Plus filter for removing cruft from Gmail prints.
[Adblock Plus 1.3]
! Checksum: h6BrZGmz6mljqqZ43XaHrw
! [Gmail print cleaner]
! Gmail logo (left) and receipient email adress (right):
mail.google.com##body[onload="Print()"] .bodycontainer>table
! Horizontal line below logo/email adress:
mail.google.com##body[onload="Print()"] .bodycontainer>hr
! Title and message count:
mail.google.com##body[onload="Print()"] .maincontent>table:first-child
@Zegnat
Zegnat / crapchacracker.js
Created April 23, 2013 15:42
Every CAPTCHA needs to be cracked. Even CRAPCHA.
document.querySelector('.crapcha input').value=Array.prototype.map.call(document.querySelectorAll('.crapcha .code span'),function(a){return a.textContent.length?a.textContent:String.fromCharCode(parseInt('F'+a.className.substr(5),16))}).join('')
@Zegnat
Zegnat / gist:5378588
Last active December 16, 2015 04:39
Getting the closest midnight relative to a certain DateTime.
<?php
function closestMidnight($date) {
if (!is_a($date, 'DateTime')) {
try { $date = new DateTime($date); }
catch (Exception $e) {
if (is_numeric($date)) { $date = (new DateTime())->setTimestamp(intval($date)); }
else { return false; }
}
}
$comp = clone $date;
<?php
// This assumes $text is the input we are parsing.
// I am using normal `echo`s here to output text, you will use whatever function you need.
if (preg_match('/^\/ba?ck(?:\s+(.*?))?\s*$/', $text, $result)) {
// $text matched!
if (isset($result[1]) && strlen($result[1]) > 0) {
// $text included an optional back message to display.
echo '<p><strong>Back:</strong> ' . $result[1] .'</p>';
} else {
@Zegnat
Zegnat / theme.php
Last active December 10, 2015 03:18
Temporary fix for https://github.com/Kroc/NoNonsenseForum/issues/163 through the templating engine.
<?php //theme-specific template functions
/* ====================================================================================================================== */
/* NoNonsense Forum v23 © Copyright (CC-BY) Kroc Camen 2012
licenced under Creative Commons Attribution 3.0 <creativecommons.org/licenses/by/3.0/deed.en_GB>
you may do whatever you want to this code as long as you give credit to Kroc Camen, <camendesign.com>
*/
//this function is called just before a templated page is outputted so that you have an opportunity to do any extra
//templating of your own. the `$template` object passed in is a DOMTemplate class, see '/lib/domtemplate/' for code
//or <camendesign.com/dom_templating> for documentation on how to template with it
@Zegnat
Zegnat / fix-blog-a-penguin-classic.user.js
Created December 8, 2012 19:48
Fix ‘Blog A Penguin Classic’.
// ==UserScript==
// @name Fix ‘Blog A Penguin Classic’.
// @author Martijn van der Ven <martijn@zegnat.net>
// @id fix-blog-a-penguin-classic
// @namespace http://zegnat.net
// @version 0.5
// @match http://*.blogapenguinclassic.co.uk/*
// ==/UserScript==
(function(a,i,b){
@Zegnat
Zegnat / gist:4088332
Created November 16, 2012 15:43
Gravatar support for the default NoNonsenseForum theme.
/* Put this in theme.php, inside the `theme_custom` function. */
foreach ($template->query('.nnf_reply-author, #nnf_post-author') as $node) {
$name = strtolower(trim($node->nodeValue));
$imgsrc = md5('');
if (function_exists('filter_var')) {
if (filter_var($name, FILTER_VALIDATE_EMAIL)) {
$imgsrc = md5($name);
}
} else { $imgsrc = md5($name); }