Archive for PHP
A Little Goes a Long Way (Redoable comment fix)
Posted by: | CommentsI’m thankful that I not only have an interest in software development and programming in general, but also that I am pretty comfortable in most programming languages (even ones I don’t use on a regular basis). Most of them are very similar and of common enough lineage, that if you need to do something simple in a language you’ve never used before, it’s not usually that difficult.
For example, take this Redoable WordPress theme I’m so fond of (this is actually a very bad illustration, because I am very comfortable in PHP). I’ve had two problems with it since installing it. The first was that annoying “Install Firefox” banner that couldn’t be turned off in the admin panel. Firefox is great, but why annoy people with it who (like me) prefer to use IE? It wasn’t really that hard to track it down in the source code and remove it completely. I didn’t bother fixing the code that was checking if the flag was set, I just took out the whole thing. That’s what I’m talking about (I feel confident that I could have accomplished that if it had been written in any other scripting language).
I had a similar problem today. I was responding to a couple comments and got an error after the first one warning me that I could only post every 15 seconds (”Hold on cowboy!”). It’d be a nice feature if it worked correctly, but I don’t get enough traffic on my site to have to worry about that at the moment. So a simple grep command later, I was in the file and I commented out the check. Worked perfectly.
In case there are any others out there using Redoable 1.2, and don’t want to bother having to fix it: the file is in the wp-content/themes/redoable folder. It’s called comments-ajax.php, and I commented out this block of code:
if ( ($time_newcomment - $time_lastcomment) < 15 ) {
do_action('comment_flood_trigger', $time_lastcomment, $time_newcomment);
fail(__('Sorry, you can only post a new comment once every 15 seconds. Slow down cowboy.','redo_domain') );
}
Found around line number 81 or so. You could probably comment out more, but I find that less is often better when I don’t know exactly what I’m doing.
WordPress Installed.
Posted by: | CommentsA client asked me if I could get a hosted blog set up for his organization. I’ve had WordPress blogs before but I’ve never gone through the installation process. It was a piece of cake, so easy in fact that I set one up for myself while I was at it.
The real reason I’ve always wanted to have my own installation is because I’ve wanted to be able to modify the Redoable theme to remove that annoying Firefox banner. There’s an option to disable the banner, but it doesn’t work. If you want to remove it, it’s in header.php, lines 106-113. It’s the PHP block that starts like this:
<?php if(preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { ?>
My favorite thing about the Redoable theme is the styles for things like code, blocks, notes, etc. For example:
This is a note.
Here is a download.
Here's an alert.
This is new!
Here is some information.
A callout example.
A blockquote example.
It's all just CSS, but it's pretty cool and convenient. My biggest complaint about most other themes was poor code formatting. My biggest complaint about Redoable was the Firefox banner. Now I've got the best of both worlds. All that remains is to consolidate my other blogs to this one.
Note: This post doesn't make much sense anymore, since I've switched from Redoable. I'm now using the Flexx theme from iThemes.
Error Iterating Through HTTPService Results
Posted by: | CommentsI was recently up at arms because of an error I was getting while iterating through the results of an HTTPService in my Flex application. The error would only present itself if there was exactly one result returned (the service was returning plaintext XML) but not when there were 0 or more than one. Stepping through the code revealed that the result was indeed there, but rather than iterating through a collection of results with only one member, it was iterating through the properties of that one result. This resulted in the message:
ReferenceError: Error #1069: Property id not found on int and there is no default value.
after the program crashed).
I really thought this was a bug in the Actionscript implementation, so I tried using the Flex 3 SDK to compile, but the error was still manifest. So I checked Adobe’s issue tracking site, figuring someone else had to be having the same problem I was, and sure enough I was able to find two tickets describing the same problem (tickets SDK-14567, and SDK-13699 if you’re interested). To my astonishment they were both marked as ‘resolved, not a bug’.
Here’s the scoop: If there’s only one result then it is returned as an ObjectProxy; if there are more than one it’s returned as an ArrayCollection of ObjectProxys. I thought this was pretty stupid at first, until I realized that if I was only expecting one result back it’d be a hassle to have to treat it like an ArrayCollection.
So anyway, on one of the tickets, they suggested doing something like:
var response : ArrayCollection = new ArrayCollection( ArrayUtil.toArray(event.result.blahs.blah) );
which I presume would have turned a single result into an ArrayCollection so it could be iterated using the for..each..in construct. It didn’t work for me. I ended up having to do a test and then processing each type separately.
I tested using:
( if event.result.blahs.blah is ObjectProxy )
and two helper functions; one that iterated over the results, and one that handled them directly.
Note: Initially I solved the problem by handling the special processing in the catch of a try..catch block. That was a third-degree hack. What I’m using now isn’t that much better, in my opinion, the real value is that I understand a little better what’s going on. I thought I could force the service to return as an Array but that didn’t work for me either (possibly because I was still processing it like XML).
Final Thoughts – The Labs
Posted by: | CommentsI’d just like to document some of the final thoughts I’ve had about these labs that we’ve done this semester. This is more for the benefit of those taking the class in the future, specifically those who are doing the Amazon EC2 labs.
One thing I did, which has saved me who-knows-how-much time was using a dynamic dns service for my servers. I had one for each of my three major servers (web server, listing server, submit server) which allowed me to hard code my calls during testing. Not only did I not have to refresh as much to get my server, but it also saved me when the submit service load balancer started having problems during the past two (or more?) weeks. Scott Chun has a good description of how to set up your server to register with dyndns.com’s dynamic hostname service. Read about it here.
One thing I would have done differently would be to store those URLs in a config file (elementary 240 stuff) so I’d only have to change it in one place to make the switch from hard-coded to load-balanced.
Something that was mentioned several times in class, and I would still love to see a tutorial on how to do this, was an alternative to frequent image persisting. Especially for minor script changes I didn’t realize I needed until having started the persist process, heaven knows I’ve had more of those than I needed. The solution is to have your server automatically check out the files it needs from a CVS/SVN repository on startup. I’m only an amateur shell scripter, but I assume there are two things the script would need to do. 1) check out the files. 2) make sure they have the correct permissions. Anyway, this would have been a real time saver.
It would have been fun to get a little experience with Pound in the labs. That’s the only thing in the entire process that I don’t feel I could go off and do right now. Sam says it’s pretty simple to figure out, I guess I’ll be finding out in a couple weeks when I’m off on my own to try it.
This class has really been an enjoyable experience. Earlier in the semester when I gave up on Python, I thought I’d feel some remorse at the end for not having stuck with it. Well, I don’t. The architectural concepts the labs have illustrated really do transcend the languages used, and I’m glad I didn’t get so bogged down in the language that I missed the point (that’s the reason for scrapping the EJB labs, right?). One thing I do regret is that Sam isn’t making us use a template for our demonstration for Jeff Barr. Speaking of that, Nathan, you’d have gotten my vote for best design. Did anyone else have a design for consideration?
Lab 5 : PHP and SOAP
Posted by: | CommentsThe only SOAP requests I’ve ever made were made on the .NET platform. They’re not that much of a beast on .NET, but it wasn’t exactly a cake walk either. So I had been bracing myself for the worst trying to implement it in PHP.
I should explain that my lab 5 client connects to a PHP service that in turn makes the SQS requests, etc. I initially wanted to implement an SQS library in Actionscript (and probably will in the future when I’m not pressed by deadlines) but I decided it was too ambitious for the amount of time I wanted to spend on this lab. So alas, a PHP service also handles my SOAP request to WHOIS.
Anyway, I was expecting SOAP on PHP to be a seriously complex affair. Here’s my code that makes the request:
$client = new SOAPClient("http://www.webservicex.net/whois.asmx?WSDL");
$params = array('HostName' => $_GET['url']);
$whois = $client->GetWhoIS($params);
Granted, it would have required about 2 more lines if there wasn’t a URL to the WSDL, but it doesn’t get much simpler than that. I should mention that this requires that PHP SOAP be enabled (uncomment a line in your php.ini if you’re running Windows; recompile from source using ‘enable-soap’ if you’re running Linux). I didn’t have to recompile, thanks once again to Remi Collet (the French guy who has yum rpms for all this stuff, see my previous post).
Well, the SQS library I’m using is pretty old and doesn’t have a means of querying the queue for the number of messages. So, I thought I’d try sending a SOAP message to Amazon to get it. Amazon’s WSDL is a little more complex, and I probably could have gotten it to work if I wanted to play around with the messages for another hour or so. It turned out to be a miserable failure, and I resorted to my old tricks: (file_get_contents()) which worked perfectly. Here’s the code I used, which shows the query string needed to get the number of messages:
$timestamp = gmdate('Y-m-d\TH:i:s\Z');
$qs = "http://queue.amazonaws.com/A3N3IV5XJH079S/processing" .
"?Action=GetQueueAttributes" .
"&Attribute=ApproximateNumberOfMessages" .
"&AWSAccessKeyId=[AMAZON_ACCESS_KEY]" .
"&Version=2007-05-01" .
"&Timestamp=" . urlencode($timestamp) .
"&Signature=" . urlencode(constructSig('GetQueueAttributes' . $timestamp));
$response = file_get_contents($qs);
The constructSig is the same method I listed in a previous post.
Here are a few links that were helpful:
SQS Query and SOAP API
Getting SQS Attributes
SQS WSDL
SQS: Queue Length / Auth Signature
Posted by: | CommentsTo get the queue length, as well as the visibility timeout, you make a request using the GetQueueAttributes action. The PHP library I’m using to make my calls to SQS doesn’t support this call (must have been written before the 2007-05-01 release of SQS) so my options are to find a new library, or to write my own function to do this.
I decided to try writing my own first, and while researching this I found something I was looking for while doing lab 4. How to compute the authorization header, or Signature.
The process is as follows, you take the query parameters and concatenate them all end to end (key preceding value). Don’t include the ?, &, or = signs. Then you calculate the HMAC-SHA1 signature of that string (using your secret access key). Then convert it to base64.
Here’s the example Amazon gives on their site.
The following request:
?Action=CreateQueue &QueueName=queue2 &AWSAccessKeyId=0A8BDF2G9KCB3ZNKFA82 &SignatureVersion=1 &Expires=2007-01-12T12:00:00Z &Version=2006-04-01
translates into the following string:
ActionCreateQueueAWSAccessKeyId0A8BDF2G9KCB3ZNKFA82Expires2007-01-12T12:00:00ZQueueNamequeue2SignatureVersion1Version2006-04-01
which when hashed with the secret key (fake-secret-key, used in this example) yields:
wlv84EOcHQk800Yq6QHgX4AdJfk= (URL encoded version: wlv84EOcHQk800Yq6QHgX4AdJfk%3D)
I looked at my PHP library, and sure enough here are the methods that create the signature. They require the PEAR Crypt_HMAC package.
function hex2b64($str) {
$raw = '';
for ($i=0; $i < strlen($str); $i+=2) {
$raw .= chr(hexdec(substr($str, $i, 2)));
}
return base64_encode($raw);
}
[/php]
function constructSig($str) {
$hasher =& new Crypt_HMAC($this->secretKey, "sha1");
$signature = $this->hex2b64($hasher->hash($str));
return($signature);
}
[php]

