Skip to content

Instantly share code, notes, and snippets.

@datibbaw
datibbaw / array_traversal.c
Last active Aug 29, 2015
unified array traversal hack
View array_traversal.c
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 Aug 29, 2015
openssl_x509_fingerprint() and context option
View gist:c52a7106d6b57e783b5c

###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:

View AwsUploadPolicy.php
<?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 Dec 23, 2015
Playing with monads, inspired by Crockford's implementation: https://github.com/douglascrockford/monad/blob/master/monad.js
View monad.php
<?php
// A silly prototype chain until I can think of something better
class prototype
{
private $inherited;
private $instance;
public function __construct(prototype $prototype = null)
View gist:6208340
/**
* 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");
View pubsub.js
(function(ns) {
ns.PubSub = function() {
// private variables
var events = {},
expando = 'ps' + ("" + Math.random()).substr(2),
guid = 0;
// private functions
function newq(event)
View smallest-range.php
<?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
{
View job.php
class WhampoaJobStatus
{
const STATUS_COMPLETED = 'completed';
const STATUS_FAILED = 'failed';
private $doc;
public function loadFromXML($source)
{
$this->doc = new DOMDocument();
View array-vs-spl.php
<?php
function array1($max)
{
$a = array();
for ($i = 0; $i < $max; ++$i) {
$a[$i] = $i;
}
@datibbaw
datibbaw / startswith.php
Created Mar 1, 2013
php startsWith() comparison
View startswith.php
<?php
function startswith1($haystack, $needle)
{
return substr($haystack, 0, strlen($needle)) === $needle;
}
function startswith2($haystack, $needle)
{
return preg_match('/^'.preg_quote($needle,'/').'/', $haystack) > 0;
You can’t perform that action at this time.