Author Archive | Ted Roche

Fixing shorttags in PHP source code

Short tags is a topic of some controversy in coding circles: wrapping your script in less-than, question-mark x 2, greater-than signs is easy to type and flows off the fingers but can be a source of confusion to some parsers. In PHP, it is not recommended, but there is a “shorttag” option available to override it globally or locally.

I recently was asked to edit some older PHP code and found it was littered with short tags, both the <? script goes here ?> variety and the shortcut output <?=”Print this string”?> variety, which need to be replaced by <?php script goes here ?> and <?php echo “Print this string” ?> respectively. A bit beyond my search-and-replace regex or sed skills, I found a fairly elegant solution on StackOverflow, which uses PHP to rewrite PHP:

#! /usr/bin/php
 <?php
 global $argv;
$contents = file_get_contents($argv[1]) or die;
 $tokens = token_get_all($contents);
 $tokens[] = array(0x7E0F7E0F,"",-1);
foreach($tokens as $ix => $token) {
 if(is_array($token)) {
 list($toktype, $src) = $token;
 if ($toktype == T_OPEN_TAG) {
 if (($src == "<?") && ($tokens[$ix+1][0] != T_STRING)) {
 $src = "<?php";
 if ($tokens[$ix+1][0] != T_WHITESPACE) {
 $src .= " ";
 }
 }
 }
 else if($toktype == T_OPEN_TAG_WITH_ECHO) {
 $src = "<?php echo";
 if($tokens[$ix+1][0] != T_WHITESPACE) {
 $src .= " ";
 }
 }
 print $src;
 }
 else {
 print $token;
 }
 }

It just needed a little shell wrapper to take in all the files (which I’d renamed *.php.old and archived and made a backup of) and put them out as *.php.old.new. Again, a quick rename and I was in business. I was astounded to find it ran the first time!

#! /bin/bash
FILES=./*.php.old
 for f in $FILES
 do
 echo "phpize $f >$f.new"
 ./phpize.php $f>$f.new
 done

Staples: “that was easy” FAIL

I’m a regular customer of Staples, web-based and brick-and-mortar. I’m attempting to reset my online password. This function failed the “that was easy” motto. Forgive my crankiness, but I design and develop web pages for a living and I expect better from a world-class organization like Staples. In accordance with good security practices, I’m using a password consisting of upper- and lower-case alphabetic, numeric and punctuation characters. I am not trying anything tricky like Alt+numeric-keypad characters. I’m using a en-us keyboard layout. Here are the problems with the message I’m getting:

1. “Sorry, but an error has been made.” is atrociously passive grammar. And who has made an error? The operator, the web site?

2. “The information you entered contains invalid characters.” Which ones? The email address or the password fields? The “5” the “@” the hyphen or the “e” ?

3. “Please try again” How many times should I try again, and what is it that I should try differently?

4. If you have a password policy (like “no punctuation”), you need to tell the person typing it in.

5. If an operator can type it in from the keyboard, it _is_ a valid character. You need to be liberal in what you accept.

Staples, you lost a sale today, and you’ve lost my trust that you know how to run a secure website.

[OT] Hitler, Apple, Gates and Godwin’s Law

Perhaps there is a corollary (if not, let’s coin Roche’s Rule) to Godwin’s Law — briefly, that any discussion is practically over when it degenerates to metaphors of Naziism — that those who call off Godwin are in turn berated as thread police, censors, infringers on some divine human right to drag threads off topic and, yes, with all the irony involved, Nazis. In this response, I make the foolish mistake of not ignoring the troll bait. I am as guilty of overreacting as the hyperbole used by the original poster to compare Apple with Hitler.

Hitler burned, gassed, tortured, shot seven million Jews in concentration camps. Millions more, of all faiths, died defeating him.

The late Steve Jobs charged premium dollars (and sometimes outrageous fees!) to keep his company out there and competing in the tough markets of computers and software. Apple makes beautiful industrial design, often as impractical as Lamborghinis. They sell DRM-crippled music. They have pushed the envelope of consumer computing, much as Microsoft “innovated” back in the days when they had competition. Apple has sold a billion songs. I admire and respect Jobs, as an empire builder, like Gates and Olsen and Carnegie before him, but I don’t spend the money to buy many of his products, as I’m cheap. I have philosophical differences with Apple, too, but I’d probably buy an iPhone if I hit the lottery. They’re shiny. But, unlike some, I’m not betting my business on them.

Microsoft’s behavior has often been as outrageous as Apples, having been pulled into court a few times, regularly making their large corporate customers buy computers with Windows pre-installed and then pay again and again for licenses to run their OS, access their machines remotely, or run software on them, switching their license fees and terms and dropping products that are near and dear to all of our hearts. I’m not ABM as much as preferring what works best. Windows DirectX is supposed to be a good gaming platform. I hear XBoxes are great for Netflix. I use a Microsoft keyboard and mouse. I happen to think that Microsoft Windows on the desktop is here to stay, even with the incredible drag it puts on businesses with malware problems, poor performance and high cost. I do not, however, think that “the desktop” is here to stay, as a metaphor for people to do their business, and I look forward to Microsoft’s stranglehold on their clients breaking with some Next Big Thing. Off the desktop, I really don’t see Microsoft having contributed that much to the state of computing through their many (many, many) acquisitions. I prefer other brands for networking, server OSes, database servers, programming languages and technologies, not through some blind hatred as much as hard-earned experience. But that’s off-topic…

[The poster who tripped Godwin’s Law…] ‘s post doesn’t have much to do with the topic of the thread, Linux, and is a really offensive comparison to some of us. Hitler and Naziism was one of the most horrific incidents of modern history and to minimize it by comparing it to shiny overpriced phones dehumanizes us all. Gates is not Idi Amin. The cellphone landscape does not resemble Dafur. Let’s get some perspective here, people.

There’s a difference between policing a thread, trying to manage to stay on topic, and censorship. I invoked Godwin’s Law to point out that the thread has likely run its course. You’re free to say what you want. I’m free to point out you’re off-topic.

So, if you want to keep on-topic on the “Linux Desktop Thread” I have some insight and informed opinions on the matter, having run Linux as my desktop for six years now, supporting a half-dozen clients with LAMP boxes in their offices, and developed some dozen or more LAMP applications. If you want to go off-kilter onto iPhones and Google as “liars and thieves,” why don’t you go start your own thread?

(Full disclosure: as I’ve mentioned before [blog.tedroche.com/disclaimers], I purchased a tiny bit of Apple stock a long time ago. It’s now worth a lot more than I paid for it, through no fault nor skill of mine. I also own tiny bits of Red Hat, HP and Microsoft. They’re still tiny. I don’t think this influences my opinion all that much, but I let my possible biases be known.)

Keeping up: theme changes

Astute readers (and I know who you two are!) will notice a change to a newer theme. I’ve switched over to the WordPress Twenty-Eleven base theme, with a few of my own tucks, nips and tweaks. I wanted to check out the new theme and test out the new functionality and keep up with the latest stuff.

Child themes are a piece of cake to create, and a child theme lets me override the original without messing with the original source, so updates will not erase the changes. While they are not so difficult to do manually if you are familiar with the command-line, they are even easier with the plugin One-Click Child Theme.

With the child theme in place, I was able to shorten the gap between the site description (the motto) and the header picture, just by adding CSS to the style.css in the child theme (that’s the cascading part of CSS). I added a paper-curl edge effect based on the work of Craig Buckler, published on Sitepoint.

I was disappointed that my site was not valid HTML5, according to the W3C’s validator site. Category tags used in REL links aren’t standard, so I deleted those. The Creative Commons plugins want to use Dublin Core XML namespaces, and there are issues there I’ve got to clean up. And the “generator” REL tag on the bottom of the page isn’t too standard, either. Overall, though, the pages are relatively clean of excessive markup or poorly-formed structures. Stay tuned as I tweak the last few elements into line.

HTML5 Boilerplate

Paul Irish started the idea of an HTML5 Boilerplate, a template to start the basic web application with the common features, and addressed problems, of HTML5 and CSS3.

Behold OpHTML5 logoen Source Power: “my” base CSS for next project, is v 2.0, with 209 well-reviewed commits from 49 practitioners.

There’s a dream team of coders and well-tested code I couldn’t imagine assembling in real life.

Now, there’s a web site that will generate and optionally customize the base templates for you: http://www.initializr.com/

Jack Frost doodles

Curly-queue frost on window pane

Can't decide if these are palms or Dr. Suess' truffula trees

L pointed out the curious frost that formed on the windows overnight in the sub-zero weather. I can’t decide if Jack Frost was dreaming of palm trees or Dr. Suess’ truffula trees.

NH Ruby September 2011: Backbone.js Jason Morrison

Twelve folks attended the September meeting of the New Hampshire Ruby Rails Group. Due to some logistical issues, we moved the meeting from NH-ICC to AlphaLoft, thanks to AlphaLoft member Nick Plante. Jason Morrison of thoughtbot was the featured speaker, talking about backbone.js.

Organizer Brian Turnbull got the meeting started with brief introductions.

Clients want the improved client-side experience of rich-client apps within the browser.

But which framework? Different patterns: MVC2 is what we’re used to seeing in Ruby/Rails. MVC and MVVM (Model-View-View-Model or Presenter).

Choice fatigue (http://www.nytimes.com/2011/08/21/magazine/do-you-suffer-from-decision-fatigue.html) : there seem to be more JavaScript frameworks than JS developers.

  • Capuccino – PowerPoint in the browsers, 270North
  • SproutCore (1,x,m 2.z) ricu Ui widget framework,.0 from the ground up rewrite, less widgets
  • Knockout.js: useds the HTML5 Data- elements
  • Batman.js – from Shopify, ActiveMerchant, using Node.js server-side component
  • JavaScriptMVC – mature, uses jQuery, filterable grids, lots of power
  • Spine.js – CoffeeScript, asynchronous front end, does full validation on client-side
  • Backbone.js – tonight’s featured framework, well-documented (in Doc-O)
  • Angular, Coherent, PureMVC-js, AFrameJS, TrimPath Junction,

Moving parts:

  • History
  • Router
  • View
  • Model
  • Collection
  • Sync
  • Underscore
  • $

Jason wowed the crowd with an excellent demo of an app under development and spoke eloquently of the power, and the dangers, of deploying MVC (or MV-something-else) into the client. His slides contained a great many good references. My laptop gave out at this point, so I didn’t take additional notes, but you can find his slides at http://jayunit.net/backbone-js-on-rails-talk and source for the slides at https://github.com/jasonm/backbone-js-on-rails-talk/blob/gh-pages/slides/index.md

One of the little things that got the crowd all excited was the line numbering on Jason’s Vim session: the current line was numbered 0, and line numbers incremented both up and down the screen. “Oh, that” Jason said. ‘set rnu’ is the command (see :help rnu within vim for details.)

Michael Tomer talked a little bit about his project, rocket.io, which is an interesting project trying to solve similar problems: “real-time” (no http lag) page updating, background sync between client and remote server. You can find his work at [updated link] https://github.com/actsasbuffoon/rocket-io

Thanks to Jason for an awesome presentation, to Nick Plante for hosting us at AlphaLoft, to Brian Turnbull for organizing the meeting, announcements and logistics, and to all the attendees for their participation.

Powered by WordPress. Designed by Woo Themes

This work by Ted Roche is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States.