OK here we go. Not sure if it is working 100% correct but it seems to be working.
The php is written by me but just quickly. So I am sure you can make it more efficient.
In the php file you can also find the database structure that I have used.
There is also some folders that I had to test.
Hope it works. Good luck