Archive for the ‘development’ Category

Wordpress login used for rails application

Monday, February 22nd, 2010

in order to use the rails login mechanism with another non-php application there are two solutions. for both you need to read the rails login cookie. it is named wordpress_logged_in_XXXX. Where XXXX is a random string. it contains a string of the form: admin|YYYY|ZZZZ. YYYY is the expiration date, in seconds i think, and ZZZZ is a hash calculated from lots of different inputs.

the first solution is to get the cookie from the browser, fetch the different input parameters for the hash from the config-files and the database and compare this hash with the hash given by the cookie. but it’s quite hard to calculate the hash. it uses not only username and password, it uses also salt values from the config file or the database, depending on many configuration options. to make it properly it would be quite hard. but there is an easier, not much more unsecure way. instead of only sending the cookie and it’s value to the browser we could write it also somewhere in a tempfolder to the filesystem. so we can read the cookie from the second application and check it againts the one from the filesystem. instead of the filesystem we could also use memcached to store the cookie infos.

whenever wordpress writes the cookie we have to write it also to the filesystem. this can be done inside the wp_set_auth_cookie function inside the file wp-includes/pluggable.php. but there is another problem. the name of the cookie is the same for different clients. so we have to use another name. i’d propose to use the username of the logged in user. the value which has to be written is stored in the logged_in_cookie -variable inside the wp_set_auth_cookie method.

$user = get_userdata($user_id); file_put_contents('/tmp/wp_rails_'.$user->user_login.'_'.$expiration, $logged_in_cookie);

with the code above the cookies content for each logged in user is writen to a file in the tmp folder. in the rails or whatever application this file can be read and compared to the actual cookie sendt by the browser. if it matches, the user is properly logged into rails.

check html templates for spelling errors

Tuesday, November 17th, 2009
my spelling is horribly. it’s quite annoying to update a webpage and getting complaints about some spelling errors. usually all the human language strings are nicely stored in a separate textfile but for smaller projects i often put them directly into html templates. if you have a proper ide it will do the spell checking for you. but if you use some not so sophisticated ide you have to do it yourself or you can use one of the opensource spellers. there are ispell, aspell, myspell and some more. at first i tried to write a shellscript using one of them. then i remembered using one once from python. there is this nice library, pyEnchant. it makes spellchecking really easy. here is a little python script which checks all the html templates. the advantage of this is that it can be implemented as a unittest or something similar, so you will be warned if there is a new spelling error in your project.

the script is quick and dirty and for german. but you should get the idea. from enchant.checker import SpellChecker import os, re, codecs, sys chkr = SpellChecker("de_DE") # patterns remove in this case html and jinja2/ django # code and some special words rmPatterns = [r'<.*?>', r'{%.*?%}', r'{{.*?}}', r'me@norep\.com', u'projectName', u'FooName'] # get a list of directories and subdirectories def listdirs(dirname): dirs = [os.path.join(dirname, f) for f in os.listdir(dirname) / if os.path.isdir(os.path.join(dirname, f))] for d in dirs[:]: dirs += listdirs(d) return dirs for d in listdirs(’templates’): for f in [os.path.join(d, f) for f in os.listdir(d) if re.search(r'\.html$', f)]: # read filedata… as unicode, i always use unicode fd = codecs.open(f, ‘r’, ‘utf-8′) data = fd.read() fd.close() # remove the tags and codes defined in rmPatterns for p in rmPatterns: data = re.sub(p, ”, data) # get error words found = [] chkr.set_text(data) for err in chkr: found.append(err.word) # if errors found, print them if len(found) > 0: print “%s: %s” % (len(found), f) for w in found: print ” : %s -> %s” % (w, ‘, ‘.join(chkr.dict.suggest(w))) the output for one of my projects is: 1: templates/pages/about.html : stösst -> stößt, störst 2: templates/pages/help.html : Registrier -> Registrier-, Registriere, Registriert, Registrieren : Bestätigungs -> Bestätigung, Bestätigungs- 3: templates/pages/legalNotice.html : St -> Set, St., Et, Kt, Sh, SV, Ist, Äst, Ast, Ost, Gst, Sät, So : mail -> Mail, mal, -mail, mail-, mai- : tel -> teil, Gel, Tel., Telex, Teller, Telekom

is XUL really cool?

Friday, May 8th, 2009
i recently started to develop a little twitter client in XUL. i did a lot of web-development with lots of javascript and i thought that it would be fast and easy to develop a XUL application. now, after a few hours i must say that XUL is a great thing. the available widgets are quite complete, the xml to desribe an UI is properly designed and it’s easy to manipulate the whole thing with javascript. i didn’t have a look at the template stuff yet and i dont know how usable it is. i’m a bit sceptic…

but

a pain in the ass is that javascript frameworks like prototype are only working partially. to manipulate the DOM with the DOM-API you need lots of unelegant lines of code. this would not be a problem if someone wrote a little javascript library for easier DOM-manipulation. but i didn’t found anything. the most javascript libraries are quite browserspecific and are not properly usable by XUL. there is in general almost no community produced stuff like tutorials, examples, proper IDEs… it is a bit frustrating to have almost only the API-reference-like tutorial and the API-reference on the official XUL homepage. firefox and mozilla are such well known. why isn’t there a big XUL echo from the internet?

apache with a segmentation fault

Wednesday, March 18th, 2009
i deployed a python app on a live server and the only output i got was a blank page. there was no error in the virtual hosts log file and it stopped somewhere during the execution of the script. in the apache error log file i found the following line: child pid 15136 exit signal Segmentation fault (11) a very informative and helpful message. after a bit of googling i found out that the gdb would help. gdb is short for GNU Project Debugger. in the file /usr/share/doc/apache2.2-common/README.backtrace there is a short howto to get a stacktrace of a segmentation fault in apache… at least on debian based systems. here is a short overview of what there is to do. at first it’s necessary to install the following packages: apt-get install apache2-dbg libapr1-dbg libaprutil1-dbg gdb add the line CoreDumpDirectory /var/cache/apache2 to your apache config, usually /etc/apache/apache2.conf. after a restart of apache it should now create a memory dump named /var/cache/apache2/core which can be analysed with gdb. it might be necessary to set the maximum size of the coredump like following (inclusive restart of apache): /etc/init.d/apache2 stop ulimit -c unlimited /etc/init.d/apache2 start to analyze the core dump you need to execute the gdb like following: gdb /usr/sbin/apache2 /var/cache/apache2/core (gdb) bt full ... (gdb) quit if you use the threaded mpm (unlikely) then you need to use gdb /usr/sbin/apache2 /var/cache/apache2/core (gdb) thread apply all bt full ... (gdb) quit my dump produced following output: #0 0xb7dd86a5 in free () from /lib/libc.so.6 #1 0xb6675011 in RelinquishMagickMemory () from /usr/lib/libMagick.so.9 #2 0xb6625ba0 in DestroyDrawInfo () from /usr/lib/libMagick.so.9 #3 0xb57d9857 in Magick::Options::~Options () from /usr/lib/libMagick++.so.10 #4 0xb57d6725 in Magick::ImageRef::~ImageRef () from /usr/lib/libMagick++.so.10 #5 0xb57cbfe6 in Magick::Image::~Image () from /usr/lib/libMagick++.so.10 #6 0xb59ed7f3 in boost::python::objects::value_holder::~value_holder () from /var/lib/python-support/python2.5/PythonMagick/_PythonMagick.so #7 0xb581adea in ?? () from /usr/lib/libboost_python-gcc42-1_34_1-py25.so.1.34.1 #8 0xb6ce6f4f in ?? () from /usr/lib/libpython2.5.so.1.0 #9 0×0889a39c in ?? () #10 0xb6d8f7e0 in ?? () from /usr/lib/libpython2.5.so.1.0 #11 0xbf80d088 in ?? () #12 0xb6ce6c60 in ?? () from /usr/lib/libpython2.5.so.1.0 #13 0×00000000 in ?? () i was using pythonMagick which uses Magick++ wich uses ImageMagick. it was a bit irritating that Magick++ version 10 used ImageMagick version 9 instead of 10. after removing ImageMagick version 9 the problem was gone. no idea why it used the wrong version.

image manipulation with python

Wednesday, March 11th, 2009
in webapps you often need to manipulate images. create thumbnails, add shadows or borders, create captchas and many other things. usually i use imagemagick. it’s a very powerfull image manipulation tool with apis for many languages. i often experienced some difficulties when trying to use it with a specific language. there i usually almost no documentation for the apis and it looks like it’s not a lot used. many tend to call the imagemackig executable trough a os call.

how is it with python

i found a few image magick python modules. most were completely undocumented, unfinished or more or less uninstallable (at least on a 64 bit debian). the one who works properly is PythonMagick. i first tried to install the lattest version but due to some dependencies problems it wasn’t possible on my ubuntu system. there is a deb package available which was easyli installed with: sudo apt-get install python-pythonmagick it’s not the latest version but it worked ok, at least for my needs.

how do you use it

i found no documentation for it but it was quite simple to figure out the basic things. there is documentation for Magick++ which is a object oriented wrapper around imagemagick. all the image methods have short descriptions. to add a red 2 pixel border to an image for example you need following code: from PythonMagick import Image i = Image('example.jpg') # reades image and creates an image instance i.borderColor("#ff0000") # sets border paint color to red i.border("2x2") # paints a 2 pixel border i.write("out.jpg") # writes the image to a file the parameter “2×2″ of the border method is a geometry string used by imagemagick as input for many methods. another example who crops the image, flips it, adds an oil paint look and finally save it as a png. i = Image('example.jpg') i.crop("100x100+25+25") i.flip() i.oilPaint(2) i.save('out.png') if i find some spare time i will add a few more complex examples of how to use imagemagick from python.

python, i like the snake

Monday, March 2nd, 2009
90% of my development work during the last few years was done in java. i had to create a little project for the OLPC about a year ago. OLPC is using python very heavily and so i developed the tool in python. before that, i did only a few very simple things in python. a few weeks ago i started to develop webapplications in python. here are a few ideas and a list of packages/ software i used.

python versus java

compared with java, python is IMHO not as clean. it isn’t as “designed” as java is. but compared with, for example PHP, it’s a hyper clean and intuitive language. java is static typed and although also bytecode compiled like python, it needs an explicit compilation step. this makes developing python programs a bit more agile. for java and python there are lots of libraries/ packages available. as much as i have seen the python ones seems to be usually higher quality, but this can just be the selection i made and doesn’t need to be a general thing. the most important point in using python for webapps instead of java is that the python programs are usually fewer lines of code compared to the java programs. as much as i have seen till now i think this is also possible while having at least the same readability of the code like java programs. on the other hand you can write more easyli horrible code in python than in java. so python needs a bit more discipline. i will use python mainly for little not so critical projects, at least until i am fluent with it.

what did i use for webapps

with java i usually used an apache with mod_jk to connect to tomcat. i looked for a similar setup in python and found that there are a lot of possible ways but the most promising thing was the WSGI standard. there was a module for apache2 and so i was happy. WSGI is short of WebServer Gateway Interface and it sits somewhere between the webserver and python. compared with java where a application server is used who shares a common context for all servlets/ requests, WSGI-python scripts are loaded in a context for each process (apache processes i think), so caching and communication with other requests is not possible. but, if you are used to develop your applications with a cluster in mind it doesn’t hurt. you can use memcached for a cache.

because WSGI is a very low level interface it’s best to use a proper webframework. there are lots of them. the most are full blown frameworks and i am a bit allergic to them, i like it when i can put together the tools i want and doesn’t have to be pushed to use a certain way (forced, you are never. with full blown frameworks it’s the same as with certain girlfriends. they tend to give subtle hints about something but don’t force you, they are open to everyting but you get afraid what awaits you if you really go with something else then her preferred way). the only one i kind of liked was web.py. i’ts not the most beautiful but well built and it lets you do whatever you want if you like to. i use it mainly for mapping urls to code and webstuff like redirecting, send errors and stuff.

to access a database there are different packages available. i checked for orm mappers and found quite a lot of different implementations. the two best (as much or less as i can judge it) are Storm and SQLAlchemy. both are flexible, easy to use and have lots of functionality who doesn’t get in your way if not used. compared with hibernate it’s a dream.

to render html or whatever, i was looking for a good template language. there are many of them and i decided to use Jinja2. its the one i liked most but, hey it’s a template language, they are almost all usefull, properly built and support about the same featureset.

if i remember properly, thats all i needed, at least for the web aspect of the applications. python also has many very useful modules included, therefore you don’t need to include lots of modules. no crappy apache commons jars who give some functionality they missed to implement in the standard library.

formatter for debugging json, xml and more

Wednesday, December 17th, 2008
very often when i am developing applications i have to work with external services. sometimes the communication is in xml, sometimes it is in json and sometimes it is a very curious nonstandard format. xml and json both have the advantage of being human readable. but very often this advantage is destroyed by using horribly formatted xml and json streams. this makes it much harder to debug. often i end up with a large chunk of xml or json with no linebreaks in it. instead of installing an appplication who can properly format that chunk you can also use online tools. it is very helpful if you can copy/paste the data and don’t have to create a new file, paste it and open it. here is a list of a few:

json formatting

  • JSON Formatter very powerfull, great userinterface, also validates input, customizable
  • JSON Format it gets a bit slow if you use really large json strings

xml formatter

havent found a good one yet.

urlencoder/decoder

if you provide your data as an url parameter it might be helpfull if you can easyli decode it.

html formatter

usually not used for communication but can be handy if you need to extract content of a website.

SQL Formatter

found it, so it is here.