Jan 14 2008

More Stupid Legislation

A new law proposal recently submitted for approval (and swiftly approved by a Ministrial Legislative Committee), would impose criminal responsibility on websites with over 50,000 hits (they don’t mention whether those should be unique visitors or merely page views) that published comments (“talkbacks”) considered libel or slander.

There are obvious and immediate problems present within this giant ball of stupidity; most of them are technical (proxies, anonymizers, etc). Another problem is the eternal question of defining the exact borders of free speech.

Regardless, this is something that should probably be marked down as another brainfart of the Israeli government: a technical solution to the social problem of stupid people.


Jan 13 2008

Emulating Ruby’s “super” in PHP

The code presented here is meant as a proof of concept and therefore does not have any error checking. Also, this is not meant to be used in production — it is merely a neat hack and should only be treated as such.

Following a discussion on FreeNode #php, where someone asked whether it was possible for a method M of class B, a subclass of class A to invoke something along the lines of Ruby’s super and call parent::M() — automagically.

It is, in fact, possible, but requires using an ugly hack — debug_backtrace().

So, here it is:


<?php
error_reporting
(E_ALL);

class A {
public function
super()
{
$bd = debug_backtrace();

$rc = new ReflectionClass($bd[1]['class']);
$pc = $rc->getParentClass()->newInstance();

call_user_func(array($pc, $bd[1]['function']));
}

public function foo() { printf(“omg, foo.\r\n”); }
}

class B extends A
{
public function
foo()
{
printf(“zomg, B-foo.\r\n”);
self::super();
}
}

$b = new B();
$b->foo();
?>

Another version, without modifying classes can be done like this:


<?php
error_reporting
(E_ALL);

function super()
{
$bd = debug_backtrace();

if (isset($bd[1]) && (isset($bd[1]['class']) || isset($bd[1]['object'])))
{
$cls = get_parent_class(isset($bd[1]['object'])?$bd[1]['object']:$bd[1]['class']);
$rm = new ReflectionMethod($cls, $bd[1]['function']);

if ($rm->isStatic())
$rm->invoke(NULL);
else {
$rc = new ReflectionClass($cls);
if (
$rc->isInstantiable())
{
$rm->invoke($rc->newInstance());
}
}
}
}

class A
{
public static function
foo() { printf(“zomg, a-foo.\r\n”); }
public function
bar() { printf(“a-bar!\r\n”); }
}

class B extends A
{
public static function
foo()
{
printf(“zomg, b-foo\r\n”);
super();
}

public function bar() { printf(“b-bar!~\r\n”); super(); }
}

$b = new B();
$b->foo();
$b->bar();
?>


Jan 4 2008

012 Transparently Proxying Torrents?

While downloading a torrent file, I noticed something strange — I was receiving over 300KB/s from a single peer, whose IP address seemed to belong to 012 Golden Lines (an Israeli ISP).

The strange thing is that is currently no package or price plan that provides similar upload rates. The fastest package currently available from any provider is 10 down, 1 up. Also, this kind of speed cannot be explained by compression, since I wasn’t downloading easily compressible content.

Are 012 engaging in torrent proxying? This certainly seems to be case.


Jan 3 2008

Converting PostgreSQL INTERVAL to seconds

This one had me stumped for a few minutes — the solution is EXTRACT(epoch FROM i), i being an interval.