Ignoring the cloddy ambiguous meaning of the title I’m happy to announce two new plugins for Serendipity. As you can read in the forum (1, 2) and watch in the sidebar of my weblog, I’d written two new plugins for Serendipity. The first one, »Flickr badge«, displays your last images from Flickr in your sidebar. It uses the serialize()/unserialize()-based API provided by Flickr which is very easy to use with PHP. The other plugin displays the album, which was on »Heavy Rotation« in the last week. It uses your Last.fm-account to retrieve these information and Amazon to find an appropriate cover. You can find both plugins in the CVS-repository for Serendipity (1, 2) or install them via Spartacus. Please take care that you need at least PHP 5.1 to use this plugins as I decided to use the wonderful OOP-model of PHP 5.
Entries tagged as Code
Plug it in, baby
published on 2007|01|26Live vom Webmontag
published on 2007|01|22So, gerade nen Vortrag über webnews.de gehört. Ich habe nicht so genau verstanden, was die machen, aber ich habe das da gefunden:
http://www.webnews.de/suche/""<script>alert('XSS')</script>Update:
Es scheint so, als hätten sie das Problem behoben. Jetzt warte ich nur noch auf meinen Milchschäumer. Aber ich finde das cool, dass das so schnell ging!
Using Phing with PHPUnit3
published on 2007|01|19If you are interested in using Phing and PHPUnit >=3.0 together, you can just use this patch. It is heavily based on this work by Kentaro Ishitoya.
Lazy, object-oriented URL parsing in JavaScript
published on 2006|12|03Use this (no, the regexp does not check for RFC-compatibility):
My_Url = function()
{}My_Url.parse = function(url)
{ this.url = url result = this.url.match(this.regexp) this.scheme = result1 this.host = result2 this.port = result3 this.path = result4 return this
}My_Url.prototype.parse = My_Url.parse
My_Url.prototype.regexp = /(https?:\/\/)([a-zA-Z0-9_\-\.]+)(:[0-9]+)?\/?(.*)?/url = new My_Url()
url.parse(‘http://foobar.com/foo’)
alert(url.host)
This will open an alert window with the content »foobar.com«.
»sleep« for special purposes
published on 2006|11|15I have a problem which occurs every evening: often I started a video using realplayer before going to bed. Realplayer does not close itself at the end of the video, which is correct in general but in my case not the best practice. Realplayer is a proprietary tool and uses Open Sound System (OSS), which is a bit antiquated nowadays. Also OSS was blocking the sound device when playing sounds. The crap Enlightment Sound Daemon (ESD) was a result of that issue. Back to my topic. Often when I go to bed I want to watch a video, but always want my Banshee to wake me up in the morning. But Banshee cannot play any sound, if the device is blocked by realplayer. So I need to kill realplayer once the video is done or a bit later but before the next morning. So I do something like sleep 3600 ; kill <pid> which works pretty fine. If I’m drunken, which takes place naturally rarely, I have the problem that I do not remember how big the integer after my sleep must be. And, especially when I’m drunken, I hate it to have my video quitting before it is done or I felt asleep. So I need a sleep-replacement which is more convenient and usable when being drunken. In fact I need a sleep replacement which can handle strings like »2m«, »2h«, »60*60*2« etc. pp. Download it! And now I can go to bed.
Nee, is klar
published on 2006|10|19for ($i=0;$i<count($thread_ids);$i++) $thread_ids[$i]=$thread_ids[$i];
Die Welt ist schlecht
published on 2006|08|22... und erst ihr Code:
/* WAS IST DAS ???
//ich weiss es auchnicht aber es ist schon lange auskommentiert glaube ich
$db->Execute("UPDATE weblogs SET title='$_POST[blog_title]', info='$_POST[blog_desc]',name='$_POST[blog_name]',template='$_POST[blog_template]' WHERE id = ".$_SESSION['weblog']['id']);
$db->Execute("UPDATE weblogs SET aktiv='$_POST[blog_aktiv]' WHERE id = ".$_SESSION['weblog']['id']);
*/
Multithreaded TCP-server in Ruby
published on 2006|07|23Hopefully on Wednesday Blogmonitor, a new project of Interdings will be open for the public. In the last days and hours I was working on the API-components. An XML/RPC-interface, an REST-service and as a nice goodie a multithreaded TCP-server where you can watch all the incoming pings when they occur. The concept is the following: when an incoming ping occurs, the encapsulated class Blogmonitor::Pingservice::Ping sends a notification to the TCP-component, which waits for incoming connections on a custom port and serves the snippet to all of the connected clients. This is invented as really simple interface and has nothing to do with more standartized technologies like »Publish & Subscribe«, also the logic would be similiar.
What does Ruby already provide?
Ruby ships an example of a multihreaded server implementation which is called GServer. It provides the possibility to create a child-class of it, overload the serve()-method and be happy. That’s really a good beginning and the concept of having patterns seems to succeed again: don’t code things twice.
Example I, first working snippet
<ol><li>require 'gserver'</li><li>class MyServer < GServer</li><li> def serve( io )</li><li> io.puts( "Hello world" )</li><li> end</li><li>end</li><li></li><li>s = MyServer.new 1234</li><li>s.start</li><li>s.join</li></ol>
The code should be more or less self-explaining: load the pattern, extend the class GServer and overload the method server(), which provides an IO-object. The method puts() of the IO-object writes »Hello world« to connected client.
Just create an instance, start() the server, join().
Connect with telnet to localhost and port 1234:
telnet localhost 1234 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. <strong>Hello World</strong> Connection closed by foreign host.
Now you have a simple TCP-Server. But we miss a few things: on the one-hand the backend notification is not implemented and the connection is not persistent. If a client connects, it should not be disconnected just to receive a stream of incoming weblogs. First of all: implement the streaming thingy. Have a look at the lines 3-5 of example I: the serve()-method is executed on a new connection and disconnects the client when it method is called. But we want to have it persistant. A dirty solution would be to write the io-code in an endless loop, which works pretty fine.
Example II, persistant server
<ol><li>require 'gserver'</li><li>class MyServer < GServer</li><li> def serve( io )</li><li> loop do</li><li> io.puts( "Hello World" )</li><li> end</li><li> end</li><li>end</li><li></li><li>s = MyServer.new 1234</li><li>s.start</li><li>s.join</li></ol>
Let’s implement the backend thingy. During my implementation I ran into a number of problems: I had a working example on my Linux-box there and after putting it on our development-system, which is a Solaris-driven T1000 from Sun I saw that from strange reason the implementation of sockets in Ruby seems not to work on Solaris, which is pretty annoying but in the end I’m happy it did not work with sockets. The next idea was to use just TCP with non-blocking connections to send packages from the ping-interface to the Live-monitor-component, but sadly Ruby on Solaris does sadly also not support non-blocking connections. The thing I’d stuck with was UDP. Good old UDP but I think it is pretty ok for this uncritical task. To send some message, you also have a pattern called UDPSocket which can be utilized to do this task.
Example III, UDP-server
<ol><li>require 'socket'</li><li>u = UDPSocket.new</li><li>u.bind( "127.0.0.1", 1357 )</li><li>message = u.recvfrom( 512 )[0]</li><li>puts message</li></ol>
Example IV, UDP-client
<ol><li>require 'socket'</li><li>s = UDPSocket.new</li><li>s.connect( "localhost", 1357 )</li><li>s.send( "Hello world", 0 )</li><li>s.close</li></ol>
The next problem is to make sure all listening TCP-clients get notified. So we need to have something like an IO-heap, where we store all the existing IO-objects and iterate through them to notify everyone. So we modify the serve()-method as it follows:
- def serve( io )
io_heap << io</li><li> loop do; end</li><li>end</li></ol></pre> <p>But who sends the snippet to all the objects? We also overload the constructor of ourio_heap = []MyServerclass to start a notification thread which does the following tasks:<ul><li>Start the UDP-server</li> <li>Iterate to our heap of IO-objects to notify them all</li></ul></p> <h3>Example V, the complete component</h3> <pre><ol><li>require 'gserver'</li><li></li><li>class MyServer < GServer</li><li> def initialize( port, *args )</li><li>udp = Thread.fork do</li><li>udp_socket = UDPSocket.newudp_socket.bind( "localhost", 1357 )</li><li> loop do</li><li> payload =udp_socket.recvfrom( 512 )[0]- unless payload.empty?
io_heap.each do |io|</li><li> begin</li><li> io.puts payload</li><li> rescue Errno::EPIPE => error</li><li>io_heap.delete io- end
- end
- end
- end
- end
- super(port, *args)
- end
- def serve( io )
- @io_heap << io
- loop do; end
- end
- end
- server = MyServer.new( 1234, host=“localhost” )
- server.start
- server.join
Isn’t it really, really short?
ezComponents 1.1 released
published on 2006|06|18
On June 12th ezPublish, the new employer of Sebastian Bergmann, released ezComponents 1.1. I’m lazy, read the changes by yourself and begin to use one the most helpful PHP-patterns.
Sprücheklopperei
published on 2006|05|23Den mitlesenden Computeraffinen gilt es die letzte Einlassung des Kollegen Ohlig nicht vorzuenthalten:
»Python ist eine Strebersprache. Ruby ist dagegen irgendwie mehr Rock ’n Roll«Schöner lässt es sich kaum zusammenfassen, auch wenn dem Streben manchmal ein gewisser Reiz anhaftet. Letztmalig festgestellt beim Schreiben von mod_jabber.py letztes Wochenende. Der Zwang zur ordentlichen Einrückung ist beim Debuggen schon eher anstrengend.
Portage-notification for Gentoo via Jabber
published on 2006|05|21Getting notified via mail about recent warnings, information messages and error on your Gentoo system is good thing but getting notified via Jabber could be also helpful in some cases. The new notification framework of portage in the current 2.1-serie previews makes it easy and possible to implement somthing like this. To show how easy it is, I did it. You can see the result in the Gentoo Bugzilla. I am utilizing XMPPPY, a friendly Python-library to do so.
How to use?
1.) Install an ebuild of the 2.1-serie of sys-apps/portage
2.) Install dev-python/xmpppy
3.) Copy this file to /usr/lib/portage/pym/elog_modules/mod_jabber.py
4.) Edit /etc/make.conf and set the following options:
PORTAGE_ELOG_SYSTEM="jabber"
PORTAGE_ELOG_JABBERFROM="sender@host.com:password"
PORTAGE_ELOG_JABBERTO="jid1@host.com jid2@host.com admin@foo.com"
Update:
As of a request by solar and Jacub Moc I did a proper release which is availiable there and wrote an ebuild. All you have to do now is to install the ebuild and edit you configuration.
Quality assurance with __call(), __get(), __set()
published on 2006|05|14__call(), __get() and __set() are so called magic functions of PHP-classes. __set() is called, when you’re trying to overload a property of a class, __get() is called, when you trying to fetch a property which is undefined and __call is executed if you’re going to call a method which is not defined. Example:
class TestThis should be enough, to understand what this functions are doing. Now you can modify the magic functions to log its error messages or to trigger a warning. One hint: Also it seems, it’s not a good idea to throw an exception, because this will lead to silent stops in your program in combination with
{ public function __set( $name, $value ) { echo “Name: {$name}\n”; echo “Value: {$value}\n”; } public function __get( $name ) { echo “Name: $name\n”; } public function __call( $method, $parameter ) { echo “Method {$method} called with following parameter:\n”; var_dump( $paramter ); } }
register_callback_function(). A good idea is to create a base-class, which provides such QA-mechanisms for free and all of your classes are children of this class. Also it would be a good idea if something like this would be implemented in PHP.
Question
published on 2006|05|14Sometimes even I, as cautionary as I am normally, must consult my audience. How to create and handle fucking tai64n-timestamps in PHP?
MySQL Storend Procedure Programming
published on 2006|05|13Falls mir jemand einen Gefallen tun möchte. Über dieses Buch würde ich mich sehr freuen.