Skip to content

Instantly share code, notes, and snippets.

@rsky
rsky / DefaultDict.php
Last active December 17, 2015 12:39
from collection import defaultdict
<?php
class DefaultDict extends ArrayObject
{
private $ctor;
public function __construct($ctor, array $ary = [])
{
if (is_array($ctor)) {
list($class, $method) = $ctor;
if (is_object($class)) {
find . metadata -name .markers\* -print -delete
@rsky
rsky / pecl-oauth-explicit-libcurl.diff
Created May 2, 2013 05:02
PECL OAuthでcURLのインストール先を指定できないことの副作用で、MacPortsでインストールしたlibxml2/libxsltにリンクしているxml系モジュールでエラーが発生し、segmentation faultする。これはcURLを明示的に指定することでその問題を回避するためのパッチ。
diff --git a/config.m4 b/config.m4
index 90fa113..0941c28 100644
--- a/config.m4
+++ b/config.m4
@@ -4,6 +4,8 @@ dnl
PHP_ARG_ENABLE(oauth, for oauth support,
[ --enable-oauth Include oauth support])
+PHP_ARG_WITH(oauth-curl, for oauth cURL support,
+[ --with-oauth-curl[[=DIR]] cURL installation prefix], DEFAULT, no)
@rsky
rsky / config.m4
Last active December 16, 2015 15:49
no globals
PHP_ARG_ENABLE(noglobals, for noglobals support,
[ --enable-noglobals Include noglobals support],
yes, yes)
PHP_NEW_EXTENSION(noglobals, noglobals.c, $ext_shared)
@rsky
rsky / pbkdf2.php
Last active September 19, 2019 05:04
A hash_pbkdf2() implementation for PHP versions 5.3 and 5.4.
<?php
/**
* Generate a PBKDF2 key derivation of a supplied password
*
* This is a hash_pbkdf2() implementation for PHP versions 5.3 and 5.4.
* @link http://www.php.net/manual/en/function.hash-pbkdf2.php
*
* @param string $algo
* @param string $password
@rsky
rsky / crypt-blowfish.php
Last active December 14, 2015 14:58
PHPのcrypt()関数でパスワードをBlowfishハッシュにかけるベンチマーク。 指数関数的に処理時間が増大し、10を越えるとWebサービスとして実用的でなくなってくる。
<?php
$str = 'password';
$key = 'secret';
function make_blowfish_salt($cost, $key)
{
$length = ceil((22 - strlen($key)) * 3 / 4);
if ($length > 0) {
$rand = file_get_contents('/dev/urandom', false, null, 0, $length);
$b64 = base64_encode($rand);
@rsky
rsky / Interceptor.php
Last active December 14, 2015 10:09
ミックスインでAOPもどき(オレオレ改造PHPを使用)。 AOPサポートを追加すればInterceptor::invoke()の定義が不要になる。
<?php
trait Interceptor
{
public function before()
{
echo "(before)\n";
}
public function after()
<?php
// basic short anonymous function syntax like the blocks syntax of clang
$f = ^($arg){ var_dump($arg); };
$f('Hello!');
// without argument
$f = ^{ var_dump(__FUNCTION__); };
$f();
// with lexical variable(s)
@rsky
rsky / build-php-ios-armv7.sh
Last active December 12, 2015 08:39
PHPをiOS向けにビルドするシェルスクリプト
#!/bin/sh
PLATFORM_DIR="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform"
SDK_DIR="$PLATFORM_DIR/Developer/SDKs/iPhoneOS6.1.sdk"
test -f Makefile && make distclean
if [ ! -f Zend/_Offsetof.h ]; then
cat > Zend/_Offsetof.h <<EOF
#ifndef _Offsetof
@rsky
rsky / gist:4727539
Last active December 12, 2015 06:09
MySQL 5.6.10 s/connection_tcpwrap_errors/connection_errors_tcpwrap/
--- a/sql/mysqld.cc
+++ b/sql/mysqld.cc
@@ -6292,7 +6292,7 @@
The connection was refused by TCP wrappers.
There are no details (by client IP) available to update the host_cache.
*/
- statistic_increment(connection_tcpwrap_errors, &LOCK_status);
+ statistic_increment(connection_errors_tcpwrap, &LOCK_status);
continue;
}