Running phpcs against many domains to test PHP5 Compatibility.

Running a shared hosting service (or otherwise having a ton of web sites hosted on the same server) can pose challenges when it comes to upgrading.  What’s going to happen if you upgrade something to do with the web server, and it breaks a bunch of sites?

That’s what I ran into this week.

For security reasons, we needed to knock PHP4 off our Apache server and force all users onto PHP5.

But a quick test showed us that this broke a number of older sites (especially sites running on old code for things like OS Commerce or Joomla).

I can’t possibly scan through billions of lines of client code to see if their site will work or break, nor can I click every link and test everything after upgrading them to PHP5.

So automation takes over, and we look at PHP_CodeSniffer with the PHPCompatibility standard installed.

Making it work was a bit of a pain in the first place, and you’ll need some know-how to get it to go.  There are inconsistencies in the documentation and even some incorrect instruction on getting it running.  However, a good place to start is http://techblog.wimgodden.be…..

Running the command on a specific folder (eg. phpcs –extensions=php –standard=PHP53Compat /home/myuser/domains/mydomain.com/public_html) works great.  But as soon as you decide to try to run it through many, many domains, it craps out.  Literally just hangs.  But usually not until it’s been running for a few hours, so what a waste of time.

So I wrote a quick script to help with this issue.  It (in its existing form – feel free to mash it up to suit your needs) first generates a list of all public_html and private_html folders recursive to your /home folder.  It then runs phpcs against everything it finds, but does it one site at a time (so no hanging).

I suggest you run phpcs against one domain first to ensure that you have phpcs and the PHPCompatibility standard installed and configured correctly.  Once you’ve successfully tested it, then use this script to automate the scanning process.

You can run the script from anywhere, but it must have a tmp and results folder within the current folder.

Eg.:
mkdir /scanphp
cd /scanphp
mkdir tmp
mkdir results

And then place the PHP file in /scanphp and run it like this:
php myfile.php (or whatever you ended up calling it)

Remember, this script is to be run through a terminal session, not in a browser.

<?php
  exec('find /home -type d -iname \'public_html\' > tmp/public_html');
  exec('find /home -type d -iname \'private_html\' > tmp/private_html');
  $public_html = file('tmp/public_html');
  $private_html = file('tmp/private_html');

  foreach ($public_html as $folder) {
    $tmp = explode('/', $folder);
    $domain = $tmp[(count($tmp)-2)];
    if ($domain == '.htpasswd' || $domain == 'public_html') $domain = $tmp[(count($tmp)-3)];
    $user = $tmp[2];
    echo 'Running scan: ' . $folder . $user . '->' . $domain . '... ';
    exec('echo "Scan Results for ' . $folder . '" >> results/' . $user . '_' . $domain . '.log');
    exec('phpcs --extensions=php --standard=PHP53Compat ' . $folder . ' >> results/' . $user . '_' . $domain . '.log');
    exec('echo "" >> results/' . $user . '_' . $domain . '.log');
    exec('echo "" >> results/' . $user . '_' . $domain . '.log');
    echo 'Done.' . PHP_EOL . PHP_EOL;
  }

  foreach ($private_html as $folder) {
    $tmp = explode('/', $folder);
    $domain = $tmp[(count($tmp)-2)];
    if ($domain == '.htpasswd' || $domain == 'private_html') $domain = $tmp[(count($tmp)-3)];
    $user = $tmp[2];
    echo 'Running scan: ' . $folder . $user . '->' . $domain . '... ';
    exec('echo "Scan Results for ' . $folder . '" >> results/' . $user . '_' . $domain . '.log');
    exec('phpcs --extensions=php --standard=PHP53Compat ' . $folder . ' >> results/' . $user . '_' . $domain . '.log');
    exec('echo "" >> results/' . $user . '_' . $domain . '.log');
    exec('echo "" >> results/' . $user . '_' . $domain . '.log');
    echo 'Done.' . PHP_EOL . PHP_EOL;
  }

?>

See what we’re doing there?  Easy breezy, and solves the problem when having to run phpcs against a massive number of domains.

Let me know if it helped!

– Robbie

Lightweight Message Notification for your Web Site

I’ve been using noty for quite some time–since Garbee introduced me to it a couple years back in our chat room. It’s been pretty great, but when version 2 came out, I realized that the developer is going a different direction than I am.

When I code my site, I consider “being lightweight” to be one of my top priorities. But I also like things to look cool.

What I liked about noty is that it was pretty quick and easy to deploy, and looks great. But it’s also a bit heavyweight for what it is, and now with version 2, it’s too much of a performance hit for my liking (touches way too many files).

So, I decided to whip out something really quick to replace noty on my site, and thought perhaps there would be other people out there who would like to use it, or at least use it as a base for something of their own.  Want to see it in action?  Visit the demo here (look at bottom right):  http://www.category5.tv/?demo=notifier

Feel free to use, share or edit it.  It’s pretty simple, obviously. These things do not have to be overly complicated!

Download the v1.0 ZIP:  https://github.com/RobbieF/notifier/archive/v1.0.zip
GIT Repository:  https://github.com/RobbieF/notifier

1.0 – Requires jQuery.  I built it this way since my site has jQuery. Of course, it could easily be adapted to just use JavaScript, but I didn’t bother; it works for me.  Feel free to comment if you really badly need a jQuery-free version.  🙂

Walk-in Wifi Responder

Had a thought this morning that wifi could be used to do some pretty rad stuff… like detecting when I get home by noticing my iPod touch.

Since most of us carry wifi-enabled devices with us at all times, and most of us have them set to auto-connect once in range of our routers, I thought, why not use that data?  It could be as simple as logging coming and going, or as sophisticated as automatically turning on my favorite music when I walk in the door.  Or even adjusting the thermostat when I arrive home to save energy when nobody is around.

As a very brief proof of concept I whipped out a simple algorithm in PHP which can be run from any Linux computer on your network.

Usage:  php wifi-check.php –device=devicename

 0) {
      $tmp=@explode('=', $ping);
      $result=@explode('/', trim($tmp[1]));
    }
    if (count($result) > 0) {
      // Now we know the device is connected; do something
      echo 'Device active.' . PHP_EOL;
    } else {
      // Device is not connected.
      echo 'Device inactive.' . PHP_EOL;
    }
  } else {
    echo 'Usage: php wifi-check.php --device=devicename' . PHP_EOL;
  }
} else {
  echo 'This script is designed to be run from the Linux terminal, not a browser.';
}
?>

My thinking is to put something like this in a looping script and let it run every so many seconds or something, calling particular functions if the device is detected as active vs inactive.

I’d welcome your thoughts in the comment section below.  What practical things could this be used for?