It will be mostly automated, once I rewrite the blog posting system. Until then I will post manually.
I plan to have the blog automatically tweet about new blog posts, but I will have to build some kind of system that will be able to tell the difference between new and updated posts first, since I make usually make several corrections to each my blog posts once I have posted them.
Recently I have found that PHP sometimes doesn't follow redirects (e.g. the get_headers() function). So I wrote this quick function to follow a url's redirects to a certain depth:
* @summary Follows a chain of redirects and returns that last url in the sequence.
* @param $url - The url to start at.
* @param $maxdepth - The maximum depth to which to travel following redirects.
* @returns The url at the end of the redirect chain.
function follow_redirects($url, $maxdepth = 10, $depth = 0)
//return the current url if we have hit the maximum depth
if($depth >= $maxdepth)
//download the headers from the url and make all the keys lowercase
$headers = get_headers($url, true);
$headers = array_change_key_case($headers);
//we have a redirect if the `location` header is set
return follow_redirects($headers["location"], $maxdepth, $depth + 1);
Very soon I will also release I few snippets of code I am writing for Blow Worm.
In other news, I have been adding to Pepperminty Wiki. You can now have administrator accounts, which can move and delete pages. Futher plans can be found on the project's readme.
If there is anything you want to see here, post a comment below.
Recently I needed to generate random strings to hex to act as a session token for Blow Worm. Using session tokens mean that you send the login credentials once, and then the server hands out a session token for use instead of the password for the rest of that session. In theory this is more secure than sending the password to the server every time.
The problem with generating random session tokens is that you need a secure random number generator, so that hackers can't attempt to guess the random numbers and hence guess the session tokens (that would be bad).
The way I did it (please leave a comment below if this is insecure!) is as follows:
Generate ~128 bits of randomness using the OpenSSL function openssl_random_pseudo_bytes(). This randomness generator is apparently better than rand() and mt_rand().
Hash that resulting randomness with SHA256 to ensure a constant session key length.
This is a quick post about sending POST requests via the command line. I have been using curl for a while now - and I find it to be a good alternative to wget. I have just found out how to use it to send a POST request from the command line, and I thought that I would share my findings here.
Sending a POST request is really quite simple:
curl -X POST --data-binary "@$input_filename" -o $output_filename $url
$input_filename is the name of the filename that contains the data that you want to send.
@- can be used to specify stdin, allowing you to pipe the output of the previous command into curl.
You can also drop at @ symbol and hard-wire the data you want to send into the command itself.
$output_filename is the name of the file you want to save the response to. You can drop -o $output_filename if you want curl to output he result to he standard output for further processing.
If you find that you get some king of error message from the above, you may need to add --header "Expect: 100-continue" just before the $url - some servers require this.
The --data-binary is rather important, as if you use --data on it's own, line breaks are not preserved for some bizarre reason.
I am finding that curl is much more powerful that I first expected, as it understands just about any protocol you care to name....I need to experiment with it further.
Curl also has a modular structure to it's command line arguments, so you can tack and extra setting on the end and it will work exactly as you would expect it to (most of the time!).
Today (after lots of code tweaking in preparation), the tag clouds that recently appeared on the blog now have links on each tag so you can see which posts havethat particular tag. Currently, the Next Page and Pevious Page links are a little bit buggy, but that problem should be fixed sooner or later, I just need to make a few more changes to the code to make it more intelligent.
Edit: The Next Page and Previou Page buttons have been fixed! I have also added First Page and Last Page butttons too - the last page button was the reason it took to long to refactor the code :)
While writing pepperminty wiki, I started seeing a rather nasty in crease in page load times. After looking into it, I drew the conclusion that it must have been the file system that caused the problem. At the time, I had multiple calls to PHP's glob function to find all the wiki pages in the current directory, and I was checking to see if the wiki page existed before reading it into memory.
The solution: A page index. To cut down on the number of reads from the file system, I created a json file that containedd inforamtion about every page on the wiki. This way, it only needs to check the existence of and read in a single file before it can start rendering any one page. If the page index doesn't exist, it is automatically rebuilt with the glob function to find all the wiki pages in the current directory.
In short: to increase the performance of your PHP application, try to reduce the number of reads (and writes!) to the file system to an absolute minimum.
I still need to update the code to allow users to delete pages via the GUI though, because at present you have to have access to the server files to delete a page and then remove it from the page index manually.
Yesterday I added a tag cloud to the website. You can find it at the top of the blog, or below the post on the article view. The tags are not yet linked to a list of posts that have the corresponding tags, as I need to some a litttle bit of refactoring of the blog's code in order to get that to work.
Recently I found a post on reddit by someone called am2064 about a 'one file wiki' called 'Minty Wiki' written in PHP. I took a look and whilie it was cool, I found it to have some bugs in it. I also found that it needed an extra PHP file to parse markdown to make it work properly. Still, I thought it was a cool idea so I decided to have a go myself.
694 lines of code later, I had something that worked and I thought that I might post about here on my blog. It is by no means finished, but it is in a somewhat usable (hopefully secure) state. I decided that markdown was the most logical choice for editing pages, so I modified Slimdown (by Johnny Broadway) to add internal link parsing and tweaked the bold/italics code to be mroe like Gmail's chat amongst other things. I first found Slimdown when looking for a lightweight markdown parser for comments on this blog.
I named my creation 'Pepperminty Wiki' (after the wiki that gave me the idea). It currently allows you to create and edit pages (although you need access to the server's files to delete pages currently), list all current pages, and view a printable version of a page. It even has a 'search' box that allows you to type in the name of the page you want to view. The search box has an HTML5 <datalist> to provide the autocomplete functionality.
To use it yourself, simply download index.php in the github repository below and put it in a folder on your server. Make sure that you have enabled write access to the folder though, or else you will start to see to rather strange error messages :)
To configure it, simply open the file you downloaded with your favourite text editor. You will find the settings (along with an explanation of each) at the top of the file. Make sure that you change the usernames and passwords!