Skip to content

Instantly share code, notes, and snippets.

View datibbaw's full-sized avatar

Tjerk Anne Meesters datibbaw

View GitHub Profile
@datibbaw
datibbaw / array_traversal.c
Last active August 29, 2015 14:24
unified array traversal hack
typedef zend_bool (*zend_iterator_each_t)(zval *value, zval *key, void *context);
static void php_traverse(zval *t, zend_iterator_each_t each_func, void *context)
{
zend_bool should_continue;
zval *value;
zval key;
if (Z_TYPE_P(t) == IS_ARRAY) {
zend_ulong num_key;
@datibbaw
datibbaw / gist:c52a7106d6b57e783b5c
Last active August 29, 2015 14:04
openssl_x509_fingerprint() and context option

###SSL context option

  • peer_fingerprint: string|array

Aborts when the certificate fingerprint doesn't match the given hash. The behaviour depends on the variable type:

  • when a string is passed, depending on the length MD5 (32) or SHA1 (40) will be used as the hashing algorithm.
  • when an array is passed, the key determines the algorithm (e.g. "sha256") and the corresponding value is used to compare against.

For example:

<?php
/**
* Class AwsUploadPolicy
* @see http://docs.aws.amazon.com/AmazonS3/latest/dev/HTTPPOSTForms.html
* @package Urahara
*/
class AwsUploadPolicy implements \JsonSerializable
{
const ACL_PRIVATE = 'private';
@datibbaw
datibbaw / monad.php
Last active December 23, 2015 07:58
Playing with monads, inspired by Crockford's implementation: https://github.com/douglascrockford/monad/blob/master/monad.js
<?php
// A silly prototype chain until I can think of something better
class prototype
{
private $inherited;
private $instance;
public function __construct(prototype $prototype = null)
/**
* Validates an email domain part
*
* @see http://www.faqs.org/rfcs/rfc2821.html - chapter 5
*/
function validate_email_domain($domain, $allow_a = true, $max_cname_recursion = -1)
{
$flags = DNS_CNAME | DNS_MX | ($allow_a ? DNS_A | DNS_AAAA : 0);
if (false === ($rr_list = dns_get_record($domain, $flags))) {
throw new TPE_DNS_Exception("Could not validate domain $domain");
@datibbaw
datibbaw / pubsub.js
Last active December 17, 2015 08:49
(function(ns) {
ns.PubSub = function() {
// private variables
var events = {},
expando = 'ps' + ("" + Math.random()).substr(2),
guid = 0;
// private functions
function newq(event)
<?php
$list = [[1, 2, 3, 9, 20],
[4, 10, 12, 21, 40],
[5, 11, 13, 22],
[6, 8, 10, 23, 24, 25, 26]];
abstract class MyHeap
{
class WhampoaJobStatus
{
const STATUS_COMPLETED = 'completed';
const STATUS_FAILED = 'failed';
private $doc;
public function loadFromXML($source)
{
$this->doc = new DOMDocument();
<?php
function array1($max)
{
$a = array();
for ($i = 0; $i < $max; ++$i) {
$a[$i] = $i;
}
@datibbaw
datibbaw / startswith.php
Created March 1, 2013 02:37
php startsWith() comparison
<?php
function startswith1($haystack, $needle)
{
return substr($haystack, 0, strlen($needle)) === $needle;
}
function startswith2($haystack, $needle)
{
return preg_match('/^'.preg_quote($needle,'/').'/', $haystack) > 0;