Archive for October 20th, 2007

Like many others, I prefer to use procedural PHP as a template language. While PHP’s syntax makes it a practical choice for this, there is a problem with embedding dynamic content. Most PHP applications produce HTML output, so you end up writing <?php echo htmlspecialchars($foo);?> a lot, using this technique. Or you forget it, and make your application prone to all sorts of nasty XSS attacks.

Apart from the annoyance of superfluous typing, there is a danger of getting lazy, seeing that <?php echo $foo;?> is remarkably shorter to type. In some situations, it won’t manifest itself as a problem either, since some content-types never contains HTML special characters (Numbers for example). This is particularly nasty, because errors in the view layer are notoriously hard to track down, and unlike SQL-injections — a similar problem — the consequences tend to hurt the users of a site, rather than the site directly.

KISS

Recently, I had a look at some code, written for CakePHP. My eye caught a function e, which is shorthand for echo. A single letter, regular function is undoubtedly the simplest way to extend PHP’s syntax. Thinking about it, it’s fairly obvious, but it just never occurred to me.
Well, the CakePHP developers made a mistake, as it should have been shorthand for echo htmlspecialchars. Nonetheless, the syntax works well. So I began using a globally defined function which looks a bit like this:


function e($string) {
  echo htmlspecialchars($string);
}

And it works too. It saves me typing and keeps me from forgetting to escape output, because I have to do more work to output strings unescaped, than not. Simple, but powerful.

The clash

There is a problem though; Since this is such a good name for a function, chances are that someone else would use it for something different, or perhaps even for the same. I already know of at least one potential nameclash, since I got the idea from CakePHP.

The usual way of dealing with this, would be with namespaces, but — alas — PHP doesn’t have namespaces (yet), and the common solution of pseudo namespaces (Eg. prefixing names) doesn’t work here, since it would defy the purpose of the function in the first place.

There’s another problem too. In the few cases, where we aren’t rendering HTML/XML output, we don’t want to escape strings for embedding in HTML/XML — instead, we want to escape it for embedding in that target language. Even in HTML, it may be necessary to escape strings with htmlentities, rather than htmlspecialchars, if the text encoding isn’t ISO-8859-1. Or encode the string to UTF-8, if the template is in UTF-8.

Making the static dynamic

The problem with all this is, that the function is static — That’s the nature of global functions in PHP. Other interpreted languages allows us to redefine functions at runtime, but no such luck with PHP (Well, strictly speaking, runkit allows it, but no-one in their right mind would use it in a production environment).
There is however a loophole; Using a callback, we can delegate to a dynamically defined handler:


if (!function_exists('e')) {
  function e($args) {
    $args = func_get_args();
    return call_user_func_array($GLOBALS['_global_function_handler_e'], $args);
  }
}

Much better — Now I can define my own output handler as:


$GLOBALS['_global_function_handler_e'] = 'my_global_function_handler_e';
function my_global_function_handler_e($string) {
  echo htmlspecialchars($string);
}

And CakePHP can use:


$GLOBALS['_global_function_handler_e'] = 'cakephp_global_function_handler_e';
function cakephp_global_function_handler_e($string) {
  echo htmlspecialchars($string);
}

We’re still using a global symbol, but at least it’s dynamic, rather than static.

So here’s a plea to framework writers in the PHP world: If we could all agree to do this for any function, defined in the global scope, which has an obvious risk of nameclashing, I think we would all be better off.

Just a humble suggestion of course.

This article provided by sitepoint.com.

Filed under: , , , , , , ,

That, right there, is probably the only reason why some will head out to purchase the brand new Transformers DVD today. Megan Fox. I’ve seen a lot of films in which the young, teenage geek saves the day and, in turn, wins over the cute girl. However, Megan Fox isn’t just cute — she’s every teenager’s wet dream. She is what the girl next door looks like in 2007, and she is what makes Transformers so damn delicious. Of course, throw in some giant f**king robots to spice up the sexual tension, and you have the recipe for one of this year’s highest grossing films. There’s not much I can write here that you don’t already know: Transformers was HUGE. If you could look up “Summer Popcorn Fluff” in the dictionary, that picture of Megan Fox would rest alongside the definition; a definition that would simply read: “See: Michael Bay.”

I couldn’t think of a man better suited to bring a live-action Transformers to the big screen than Michael Bay. When he was born, his first words were probably, “Why the f*ck am I so small?! Make me bigger! Make me larger! And give me a semi-automatic weapon so I can blast the f*ck out of that guy in the white coat!” Whereas other directors may have opted to hide the robots in dark corners, whilst giving them lines like, “You think I’m big — well take a look at George Bush’s bank account!,” Bay dumbed his film wayyy down, took some “aww, shucks” cues from producer Steven Spielberg and served us a film everyone can enjoy on different levels. If you were a big fan of the Transformers toy line growing up, then it was nice to see those badass boys back in action. If you were a fan of explosive, over-the-top fight sequences, then this was an all-you-can-eat buffet of them. And, if you were simply a fan of hot girls in skimpy attire, then let me introduce you to Megan Fox. She’s foxy .. and she likes long walks on the beach, french vanilla ice cream and guys that play with big toys.

Continue reading DVD Review: Transformers

Permalink | Email this | Comments

Filed under: , , , , , ,

We already knew that Chris Pine was in talks to star as Captain Kirk in J.J. Abrams upcoming Star Trek XI, but up until now we haven’t had official confirmation. “In talks” can mean a lot of things, and if the recent almost-casting of Jessica Biel in the Justice League of America movie means anything, it’s that you should never take “in talks” as proof the actor or actress has landed the role. Over on his blog, Smokin’ Joe Carnahan (who was vying for Pine to star in White Jazz) confirms the actor will not be taking on the role of Junior Stemmons in Jazz. Why? Well, that’s because he’s opted to go for Trek.

Here it is, straight from Carnahan himself: “The young man playing Junior Stemmons has opted to ‘go where no man has gone before’ and thus, had to bow out of WHITE JAZZ. I’ve been talking to him this past week and knew how tough the decision had to be for him. I don’t envy ever being in that spot but I gave him my full support, even if it meant he didn’t do JAZZ. I get it. You don’t get opportunities like that often and I told him as long as he could control as much of that process as possible and not get sucked into doing lesser sequels as a result of taking this gig, then good luck and God bless. We’re sure to do something down the road. I’ve already slotted him in for KILLING PABLO.” So there you have it — Chris Pine will indeed be starring as Captain Kirk in Star Trek XI (according to Carnahan, that is), and instead of placing him in Jazz, the director has instead found a part for him in Killing Pablo. No word on what that part is, but it looks like Pine’s stock just shot straight up. I guess there was a reason why he starred opposite Lindsay Lohan in a film called Just My Luck — and, as ironic as it might seem, the premise of that film is, like, totally becoming true.

Chris Pine as Captain Kirk? Whadyya think?

Permalink | Email this | Comments

Filed under:

If a burglar with exquisite taste somehow managed to snag a million dollar Goldvish, asking for just $185,000 for its safe return wouldn’t be too far-fetched. Apparently, the mobile in question wasn’t of the princely variety, as this particular thief managed to lower his asking price to a rock-bottom $200. The suspect, known initially through police paperwork as “Baby Boy,” was lured into a trap after police tagged along for the exchange and arrested him at gunpoint. When Mr. Boy (later found to be Randy-Jay Adolphos Jones, which is only slightly better) was questioned, he just couldn’t put a finger on why he blurted out the $185k figure versus something more reasonable, but hey, not everyone can be right on top of current market conditions, right?

[Via The Raw Feed]

 

Read | Permalink | Email this | Comments

Office Depot Featured Gadget: Xbox 360 Platinum System Packs the power to bring games to life!

Filed under:

Although there’s still no official word from Microsoft about the Xbox 360 Arcade, we’ve just received a second picture of a boxed unit sitting on the shelves of a retail store: this time in a Methuen, MA store, only miles away from the unit spotted in Haverhill. New details include a Harry Potter / Lego Star Wars back, along with details on included games — we can make out Uno, Pac-Man, Luxor 2, and two other indistinguishable logos. The closeness of the two spottings indicates that this early leak is the fault of whoever put a pallet on a truck without marking it with the correct ship date. In that case, don’t expect an early announcement from Microsoft.

[Thanks, Emanuel E.]

%Gallery-8607%

 

Permalink | Email this | Comments

Office Depot Featured Gadget: Xbox 360 Platinum System Packs the power to bring games to life!

Filed under:

Just in case you thought this whole Vii thing was one big prank, guess again. Our pals over at Engadget Chinese managed to scrounge up one of these clones and put it toe-to-toe with its idol. Needless to say, Nintendo’s unit tends to demand quite a bit more respect, but why not take a look at their gallery and see how the two really stack up.

Update:Live video

 

Read | Permalink | Email this | Comments

Office Depot Featured Gadget: Xbox 360 Platinum System Packs the power to bring games to life!

SanDisk paints Cruzer line anew SanDisk has decided to be a little bit more adventurous with their Cruzer line of USB flash drives, launching the Colors+ range that will feature ten different shades for you to choose from. I know that USB flash drives aren’t exactly Pokemon so you probably won’t have this innate need to catch ‘em all, but it is always nice to have options when purchasing a product. Gone are the days where the Ford Model T came in only one color - dreary black at that. Which would be your color of choice?

Permalink | Comment | Uberbargain | Uberphones

Gundam simulator in Japan

Trust the Japanese to come up with Senjo No Kizuna, a Gundam simulator at arcades that allows the player to control a mech using dual joysticks and foot pedals. This kinda reminds me of the Mechwarrior franchise, and it would be nice to see the computer experience being translated to the arcade environment. Each of these Gundam simulator pods can seat only one, and boasts a panoramic display as well as network connectivity, allowing players to battle others in adjoining pods or in other arcades in a 5 vs 5 epic battle. You’ll also be issued a Pilot Card that stores all your combat data in order to build up a respectable history, just like those Initial D racing simulators.

Permalink | Comment | Uberbargain | Uberphones

Nintendo stops support for NES et al While Microsoft has already stopped supporting Windows 98 for quite some time already, you’ll be surprised to know that Nintendo has just pulled the plug where support for the NES, SNES, N64, Gameboy, and the Gameboy Pocket after all these decades. It is pretty interesting to note that up through last week, it is still possible to send your creaking NES system to Nintendo for a full factory repair. Talk about an amazing level of service that other companies would do well to emulate, where support for legacy devices are crucial to maintaining customer loyalty. Drats, looks like I have to take better care of my monochrome LCD Gameboy from now onwards.

Permalink | Comment | Uberbargain | Uberphones

Filed under: ,

Well, it seems Capcom enjoys tossing out game announcements this week. This time, we’ve gotten wind of the next installment of their Commando series, Commando 3. The title is going to be a downloadable game from the PSN and will be worked on by the same group who is responsible for Super Puzzle Fighter 2 Turbo HD Remix Extreme Title Ultimate Fighter Time Gaming Fun.

Up to three players can blast around and shoot stuff up at once in the game, choosing between the characters of Animal, Smoke and Boomer. That is, a former marine, a covert-op, and an old dude who seems likely to explode. There will be power-ups, the 80’s style “screen cleaning” bombs, and co-op vehicles to coordinate your skills with. We look forward to hearing some more about the game in the near future.

Read | Permalink | Email this | Linking Blogs | Comments

Close
E-mail It