The other day I received a book — Zend Framework 1.8 – Web Application Development — for review. I’ll read through it as soon as I can and write a review that I will publish on my blog.
November 30, 2009
New Zend Framework book
November 29, 2009
1:1 NAT with a Linksys WRT54GL router (with Tomato firmware)
For a long time I have used a Debian based machine called megatron as a gateway at home. Megatron had two NIC’s where one was connected to an SDSL modem, and the other was connected to a Linksys WRT54GL router (which is running the Tomato firmware). These two switched places a while back so that the router is connected to the modem, and megatron is behind the router. There are a couple of services running on megatron that needs to be accessible from the internets, so I had to do some iptables magic on the router to be able to do this. This post is more of a reminder to myself of how to do this, but there might be someone else out there who wants to do the exact same thing.
Earlier megatron had two official ip addresses (I have 5 from my ISP) on the NIC connected to the modem. One of them is used for SSL traffic to megatron and the other is used for everything else. The setup now is that megatron only has one NIC with two internal addresses: 192.168.1.10 and 192.168.1.11. My router has three addresses. Lets say these are: 193.n.n.122, 193.n.n.123 and 193.n.n.124. The first one is the one I will let the router have and the other two I will forward to megatron.
First I had to add two addresses to the router since it only had one. To do this I logged in the router using ssh and ran the following commands:
# Add ip addresses ifconfig vlan1:1 193.n.n.123 netmask 255.255.255.248 broadcast 193.n.n.127 ifconfig vlan1:2 193.n.n.124 netmask 255.255.255.248 broadcast 193.n.n.127
To test if these two worked I simply pinged the new ip addresses.
Now I needed to tell the router to forward traffic on these two addresses to the ip’s specified on megatron. iptables to the rescue:
# To megatron iptables -t nat -I PREROUTING -p all -d 193.n.n.123 -j DNAT --to-destination 192.168.1.10 iptables -t nat -I PREROUTING -p all -d 193.n.n.124 -j DNAT --to-destination 192.168.1.11 # From megatron iptables -t nat -I POSTROUTING -p all -s 192.168.1.10 -j SNAT --to-source 193.n.n.123 iptables -t nat -I POSTROUTING -p all -s 192.168.1.11 -j SNAT --to-source 193.n.n.124 # Accept all ports iptables -I FORWARD -p tcp -d 192.168.1.10 -j ACCEPT iptables -I FORWARD -p tcp -d 192.168.1.11 -j ACCEPT
And that’s that really. One last thing I had to do was to make these changes permanent. This can be done by putting the ifconfig and iptables commands in this post in the Administration->Scripts part of the Tomato web-gui. Click on Administration and then Scripts in the gui and enter the commands in the firewall tab:

Remember to click the save button on the bottom of the page after these changes.
September 10, 2009
Firefox + Spotify in Wine
I run Ubuntu on all my workstations. I also run Spotify in Wine and use Firefox 3.0 as browser of choice. Until now I haven’t been able to click on the spotify:* links in Firefox and have it open them in Spotify. The following small changes made this possible:
Start Firefix and enter about:config in the address bar. Click past the warning that comes up, right click somewhere in the list of settings and click on new -> boolean. Create the following setting:
network.protocol-handler.expose.spotify
and set it to false.
Right click again to add another setting:
network.protocol-handler.external.spotify
Set this to true.
Now, lets create a small shell script that Firefox can use to start Spotify. I chose to place it in the Spotify installation folder:
vim ~/.wine/drive_c/Program\ Files/Spotify/spotify.sh
Enter the following:
#!/bin/bash wine "$HOME/.wine/drive_c/Program Files/Spotify/spotify.exe" /uri "$1"
Then make the file executable with the following command:
chmod +x ~/.wine/drive_c/Program\ Files/Spotify/spotify.sh
Now, click on a spotify link (like this one for instance) and select the newly created shell script to see the magic happen!
August 17, 2009
August 14, 2009
Small contribution to phploc
From http://github.com/sebastianbergmann/phploc/tree/master:
phploc is a tool for quickly measuring the size of a PHP project.
I just made a small contribution to it by adding counters for constants and class constants. My account over at github is located at http://github.com/christeredvartsen. Sebastian pushed it to the master repository about an hour ago.
I have also patched our phpUnderControl installation at work to output this information and contacted Manuel Pichler about it. More about that later!
July 23, 2009
July 22, 2009
June 23, 2009
Who implemented this method?!
It’s been a while since I wrote anything on this blog, so I thought it was about time to get back on the scene! Today I’ll write a post about how to figure out which class actually implemented a method in PHP.
Consider the following classes:
<?php
abstract class My_Plugin_Abstract {
public function onLoad() {}
public function onConnect() {}
public function onTick() {}
public function onExit() {}
}
class My_Plugin_Something extends My_Plugin_Abstract {
public function onLoad() {
// Do something magic on load
}
}
class My_Plugin_Magic extends My_Plugin_Abstract {
public function onTick() {
// Do some magic on every tick
}
}
class My_Plugin_End extends My_Plugin_Abstract {
public function onExit() {
// Do something fun on exit
}
}
As you can see we have an abstract class with some non-abstract methods. The plugins can then choose which of the methods to implement.
Now, let’s imagine that we have an application that runs in a loop. In each iteration of the loop we will fetch an event that the plugins can hook onto. As you can see from the code above the plugins only implement one method each, and not all four that the abstract plugin has. Since we might only want to run the methods that is actually implemented in a plugin we need to figure out which class that implemented it; the plugin itself or the abstract plugin.
PHP has a couple of functions that does something similar: method_exists() and get_class_methods(). The problem is that whenever a class extends another one, all methods of the parent class is available in the child class (with some exceptions), so method_exists() for instance will return boolean true even if the child did not implement the method.
To be able to do what we want we have to use the reflection API. By using this we can “reverse-engineer” our classes and find out some more about which methods to execute.
Back to our application.
<?php
// Register plugins and use the class names as keys
$plugins = array();
$plugins['My_Plugin_Something'] = new My_Plugin_Something();
$plugins['My_Plugin_Magic'] = new My_Plugin_Magic();
$plugins['My_Plugin_End'] = new My_Plugin_End();
// Main loop
while (true) {
// Fetch an event
$event = magicFunctionThatReturnsAnEvent(); // Imagine this returns "Load", "Connect", "Tick" or "Exit"
// Method to execute
$method = 'on' . $event;
// Loop through the plugins and run the current method
foreach ($plugins as $className => $plugin) {
$plugin->$method();
}
}
Imagine that the loop runs 4 times, and each time we get a different event. This means that the following function calls have been generated:
My_Plugin_Something::onLoad
My_Plugin_Magic::onLoad // This is implemented
My_Plugin_End::onLoad
My_Plugin_Something::onConnect
My_Plugin_Magic::onConnect
My_Plugin_End::onConnect
My_Plugin_Something::onTick // This is implemented
My_Plugin_Magic::onTick
My_Plugin_End::onTick
My_Plugin_Something::onExit
My_Plugin_Magic::onExit // This is implemented
My_Plugin_End::onExit
Out of 12 function calls, only three are of use. If we extend the foreach loop a little, we can do something like this:
foreach ($plugins as $className => $plugin) {
$ref = new ReflectionMethod($className, $method);
if ($ref->getDeclaringClass()->name === $className) {
$plugin->$method();
}
}
First we make a ReflectionMethod object by specifying the name of the plugin class and then the method. The reflection object gives us loads of methods to use, but the one we are interested in is the one called getDeclaringClass() which gives us a ReflectionClass object that is a reflection of the class that declared the method. In most cases above the declaring class is My_Plugin_Abstract which only holds placeholders for the methods that the plugins can implement.
If we run our application again the following methods are run:
My_Plugin_Magic::onLoad // This is implemented
My_Plugin_Something::onTick // This is implemented
My_Plugin_Magic::onExit // This is implemented
Looks good! But (there had to be one right?), using the reflection API costs quite a bit. In most cases it’s faster just to make all the function calls above instead of using reflection to check the declaring class and so forth. In some cases it might be worth using reflection for this, and if you have such a case at least you know how to do it. You can thank me later!






