Archive for the ‘Uncategorized’ Category

Programming is hard

Monday, January 2nd, 2012

After a full week to devote to personal programming projects, and actually devoting a good amount of time to them, I can safely say I’ve made barely any tangible progress. I tend to forget just how complex even a simple site can be…

No More Mobile Device Browser Updates…

Thursday, October 20th, 2011

So the WURFL group became Scentia Mobile, and along with the change eventually followed stricter and stricter licensing. As it stands now, there’s no way to use the newer versions of the WURFL xml file in the public sphere, outside of using Scientia-provided APIs.

In other words mobile device browser cannot be updated using new official WURFL data.

I’d hoped that Luca and the Scientia people would’ve protected their GPL community at the very least given the project’s community-sourced roots. Alas, there’s money to be made in all of this and principles are always the first thing to go.

Simple PHP Database Class

Monday, August 29th, 2011

It seems in every new project I always need a few core components, one of which is a wrapper for some database functionality. Why a wrapper class? Why not just use the baked-in PHP commands? Well it’s generally helpful to abstract certain functionality, say if you wind up switching databases from mysql to postgres or postgres to oracle, you don’t necessarily want to rewrite every system call, especially since the syntax of the queries will probably be fairly similar. Also you may want to bolster error handling or extend the functionality of the native functions.

A real world example is PHP’s decision to phase out its mysql client in favor of mysqli and other variants. Using a wrapper class can take the pain out of such transitions.

Finding myself rewriting the same DB wrappers over and over again, I’m putting a fresh one up on Github with an MIT license (i.e. free for personal or commercial use). It’s pretty simple at this point, accepting only mysql and postgres as database types.

Download or fork it here: https://github.com/cbroome/PHP-DB-Class

Examples from the README file:

 
include 'db.php';

$db = new DB( 'mysql' );

if(!$db->connect('hostname', 'username', 'password', 'database') ) {
	print "Could not connect! \n";
}
else {
	
	$sql = "CREATE TEMPORARY TABLE temp_table( gallons INTEGER, galleons VARCHAR(255))";
	if(!$db->query($sql)) {
		print "Query failed: " . $db->get_error() . "\n"; 
	}
	else 
	{
		print "Query succeeded!\n"; 

		$value1 = $db->clean( "Should get an extra' character? " );
		$value2 = $db->clean( "Imagine this off of a GET var: ';drop temp_table;#" );

		$sql = "INSERT INTO temp_table(gallons, galleons) VALUES
			(100, '$value1'),
			(200, '$value2')";

		print "Running query: \n" . $db->get_sql() . "\n";
		if(!$db->query($sql))
		{
			print "Query failed: " . $db->get_error() . "\n";
		}
		else 
		{
			print "Just ran: " . $db->get_sql() . "; Now let's see what was inserted: \n";
			$sql = "SELECT * FROM temp_table";
			if(!$db->query($sql)) {
				print "Query failed: " . $db->get_error() . "\n"; 
			}
			else {
				while( $row = $db->fetch_assoc()) {
					print_r($row);
				}

				print "Finished \n"; 
			}
		}
	}
}

Which results in:

$ php driver.php

Query succeeded!
Running query: 
CREATE TEMPORARY TABLE temp_table( gallons INTEGER, galleons VARCHAR(255))
Just ran: INSERT INTO temp_table(gallons, galleons) VALUES
			(100, 'Should get an extra\' character? '),
			(200, 'Imagine this off of a GET var: \';drop temp_table;#'); Now let's see what was inserted: 
Array
(
    [gallons] => 100
    [galleons] => Should get an extra' character? 
)
Array
(
    [gallons] => 200
    [galleons] => Imagine this off of a GET var: ';drop temp_table;#
)
Finished 

The Scourge of Errno.pm on Centos

Monday, July 25th, 2011

I took this site down over the weekend to rebuild the underlying system from scratch.  While reinstalling the APC cache for PHP I got a stubborn error:

$ pecl install apc
downloading APC-3.1.9.tgz ...
Starting to download APC-3.1.9.tgz (155,540 bytes)
.................................done: 155,540 bytes
54 source files, building
running: phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
Errno architecture (i386-linux-thread-multi-2.6.9-42.0.3.elsmp)
does not match executable architecture (i386-linux-thread-multi-2.6.18-53.el5)
at /usr/lib/perl5/site_perl/5.8.8/Errno.pm line 11.

Compilation failed in require at /usr/share/autoconf/Autom4te/XFile.pm line 90.
BEGIN failed--compilation aborted at /usr/share/autoconf/Autom4te/XFile.pm line 90.
Compilation failed in require at /usr/bin/autom4te line 44.
BEGIN failed--compilation aborted at /usr/bin/autom4te line 44.
ERROR: `phpize' failed

I trusted the Centos yum packages to handle all the other system setup stuff so figured the only errors would come from me. I was actually a bit surprised to see perl used for a PHP application. Checking around on the web, it seems sometimes a second, conflicting, Errno.pm file will appear from one package or another. And by the dictates of Murphy’s Law, my system defaulted to the wrong one…

To fix the error I checked for the files and whether they disagreed about my system’s state:

$ diff /usr/lib/perl5/site_perl/5.8.8/Errno.pm \
  /usr/lib/perl5/5.8.8/i386-linux-thread-multi/Errno.pm

12,13c12,13
< "i386-linux-thread-multi-2.6.9-42.0.3.elsmp" or
< 	die "Errno architecture (i386-linux-thread-multi-2.6.9-42.0.3.elsmp)
does not match executable architecture ($Config{'archname'}-$Config{'osvers'})";
---
> "i386-linux-thread-multi-2.6.18-53.el5" or
> 	die "Errno architecture (i386-linux-thread-multi-2.6.18-53.el5)
does not match executable architecture ($Config{'archname'}-$Config{'osvers'})";
15c15
< $VERSION = "1.10";
---
> $VERSION = "1.09_01";
22,26c22,26

Very inconsistent. Fortunately, moving the outdated file out of view resolved the issue and I lost nothing more than an hour of my time.

$ sudo mv /usr/lib/perl5/site_perl/5.8.8/Errno.pm ~/Errno.pm

Mobile Device Browser Updated

Sunday, June 26th, 2011

This release brings it up to version “db.scientiamobile.com – 2011-06-16 12:51:18″. Check it out: http://www.mobiledevicebrowser.com