SimplePHP

August 6, 2010

As promised, I’ve just released the ‘SimplePHP’ code on github for all to use and play with. You can check it out at

git@github.com:adastra/SimplePHP.git.

Currently, this mini-framework consists of the following classes;

Session Manager

This allows you to abstract Session management, particularly useful if you want to do secure your sessions by using a database or extend this class to do even more interesting things!

Usage:

// Initialize the session, and use php's standard session management
Session::init(false);

// ... or, initialize the session, and use the a database to store the session data
Session::init(true);

// If we're using a database, then you must have a table with the following fields;
//
// id varchar(32) {primary key}
// access int (10)
// data text

// Set a session variable
Session::set('session_variable', 5);

// Get a session variable
$val = Session::get('session_variable');

// Clear a session variable
Session::clear('session_variable');

// Check to see if a session variable exists
if (Session::exists('session_variable')){
Logger::debug("Session variable exists!");
}

Database Manager

This allows you to abstract database management, it is inspired by the wordpress database class but uses a static class to hold the database object rather than passing a database object around.

Usage:

// Set your database credentials, I typically do this in a file called 'settings.php.sticky'
// as this is server-specific and I don't include this file in the repo. You can also
// just add the credentials into the DatabaseManager class and dispense with these defines.
define("database_user", "dbuser");
define("database_pass", "dbpass");
define("database_name", "dbname");
define("database_host", "localhost");
define("database_verbose", false); // If set to true, the database will log activity using the Logger

// Though you don't need to setup or explicitly create the connection (the class will check for connection and connect if
// needed when you call any of its methods.
DatabaseManager::connect();

// Prepare a sql statement
$sql = DatabaseManager::prepare("SELECT id FROM sometable WHERE textField = %s AND numericField = %d",  $string, $number );

// Get a single variable
$id = DatabaseManager::getResults($sql);

// Get a result returned in a associative array
$sql = DatabaseManager::prepare("SELECT * FROM sometable WHERE textField = %s AND numericField = %d",  $string, $number );
$data = DatabaseManager::getResults($sql);

// Do a insert, and get the inserted row id
$id = DatabaseManager::insert($sql);

// Do an update
DatabaseManager::update($sql);

// Do a generic sql query and return the result object
DatabaseManager::submitQuery($sql);

// You can manually close the connection
DatabaseManager::close();

Logger

The logger class makes php logging simple, and the output includes a stack track indicating the function/method/class and line number of the last 2 calls on the stack. Handy for tracking down bugs! You can also have the Logger echo the log to HTML, which gives a nice color coded display of the log to the browser.

Usage:

// Tell the logger to catch system errors
Logger::catchSysErrors();

// To the logger to echo the log as HTML
Logger::echoLog();

// Set the logger level
Logger::setLevelDebug();
Logger::setLevelInfo();
Logger::setLevelWarning();
Logger::setLevelError();
Logger::setLevelFatal();

// Or set the level using the Logger constants
Logger::setLevel(Logger::$DEBUG);
Logger::setLevel(Logger::$INFO);
Logger::setLevel(Logger::$WARNING);
Logger::setLevel(Logger::$ERROR);
Logger::setLevel(Logger::$FATAL);

// Log a message
Logger::debug("some message");
Logger::warning("some message");
Logger::warn("some message");
Logger::error("some message");
Logger::info("some message");
Logger::fatal("some message");

Comms Manager

This class allows you to sanitize parameters sent from the user to the backend using Ajax or simple POST/GET methods. It also allows for the sending of JSON messages back to the server. The class can be initialized which simply detects if the users browser is IE6 or not, as this contains crappy support for gzip, and if so turns gzipping off. This will require Chris Schuld (http://chrisschuld.com/) excellent browser detection class which is included as part of this repo, or you can get the latest at http://chrisschuld.com/projects/browser-php-detecting-a-users-browser-from-php.

Usage:

// (Optional) detect if the browser is IE6, if so turn off gzipping of any data sent back to the user's browser.
// Or you can just set CommandHelper::$ZIP_MESSAGE yourself to turn gzipping on or off.
CommandHelper::init();

// Validation examples;

// To validate a $POST or $GET para called 'myParaName' that is required and you expect to be numeric call;
$para = CommandHelper::getPara('myParaName', true, CommandHelper::$PARA_TYPE_NUMERIC);

// To validate a $POST or $GET para called 'myParaName' that is required and you expect to be a string call;
$para = CommandHelper::getPara('myParaName', true, CommandHelper::$PARA_TYPE_STRING);

// To validate a $POST or $GET para called 'myParaName' that is NOT required and you expect to be a json encoded object call;
$para = CommandHelper::getPara('myParaName', false, CommandHelper::$PARA_TYPE_JSON);

// If validation fails, the class will send a JSON message in the form
// {"result":"fail","data":"Validation failure: Parameter not set, expecting 'cmd'"}

// An example of sending a message back to the user, from an php object, which is sent as JSON;

$data = array(4);

$data['stat1'] = DatabaseManager::getVar("SELECT COUNT(stat1) AS no FROM statTable");
$data['stat2'] = DatabaseManager::getVar("SELECT COUNT(stat2) AS no FROM statTable");
$data['stat3'] = DatabaseManager::getVar("SELECT COUNT(stat3) AS no FROM statTable");
$data['stat4'] = DatabaseManager::getVar("SELECT COUNT(stat4) AS no FROM statTable");

$msg['cmd'] = 'getStats';
$msg['result'] = 'ok';
$msg['data'] = $data;

CommandHelper::sendMessage($msg);

// Or you can send any string that you'd like
CommandHelper::sendTextMessage("Undefined command!");

// There are also pre-defined error messages that can be sent;

// Check that a user is logged in, and that they have access to this site
if (!SecurityUtils::isLoggedInForSite($site_id)){
CommandHelper::sendAuthorizationFailMessage("You are not authorized for this site!");
die();
}

Categories: php
Tags:
Comments (0)

Opensource release…

May 19, 2010

Well, after my last rant/blog post I decided that its time to release some of our software as open source. When we build a site, I tend to prefer using a lean html/css front end and use Ajax to interact with the server. This can make for some fresh, dynamic sites and keeps page load times to a minimum. On the backed we try to keep things light, as this is obviously where you’re going to hit scalability issues so the more work that can be done on the client side the better.

On the backed you only really need a handful of classes, a basic controller to handle incoming Ajax request which then process these request, which generally just require database reads or writes. Each controller is generally different for each project, but follows the same principles and requires the same, simple, supporting machinery; database manager, session manager, logging and security/validation.

These classes give you a minimal layer of abstraction, making it easy to change infrastructure without making any code changes.

You could say this constitutes a php framework, albeit a modest one. But, its not really, as these are just helper classes that merely extend the base php functionality.

In the coming weeks, I’ll release all of these classes under GPL for anyone to play with, and welcome feedback.

Categories: php
Tags:
Comments (0)

Symfony and php frameworks

So, I’ve just spend the last week getting to grips with the php framework symfony. Now, I understand the perceived benefit of using a framework to help speed up your development and save you from re-inventing the wheel.

I’ve been a developer for over a decade, and I’ve also been a manager too and now I run my own company. I always look for ways to save money by reducing the development overhead and leveraging pre-existing tech. But, I have never worked a project where a php framework has saved time or money. The possible exception is the wordpress framework, but that is because sites that are built from wordpress need a blog and a cms. That is a lot of overhead to build it out yourself, so it makes sense. But, it still takes longer to build out basic pages in wordpress then it would to hand code them in html/css with some ajax goodness. But, in the case of a simple site with NO cms and NO blog, then why the hell use a php framework? And wordpress is slooow. It takes some love to get a wordpress install running nice and fast.

I also build out server farms for my clients. So, I’m pretty clued up on how to build out fast sites on small budgets. Having 100% of a site rendered in php with lots of MySQL reads is definitely a bad start, I know there are plenty of ways to cache php and MySQL. Put a basic html/css site on a nginx server and watch it scream.

Anyway, back to symfony. Its just way too complex for what you need, its hard to maintain and not simple to upgrade. Ever been handed a symfony project from another developer? Yeah, its not all that nice. And multiple config files in multiple locations for a single website is also not a good selling point for a framework.

The number one rule in engineering is KEEP IT SIMPLE. Symfony violates that rule in so many ways, it adds complexity where complexity was not needed, and results in slower page loads. I like to keep it lean and mean.

I’ve spent so much of my career working with engineers who violate that number one rule on a daily basis, the often heard excuse is “I’m building re-usable code” or “this will make it faster to build next time“. Yeah, right. I met a highly successful VP at ITT, and he had a rule, “Write once, throw away“. This sounds crazy, but I believe him that its probably cheaper in the long run. Watch this video of a presentation by Ken Schwaber (the inventor of Agile project management), its interesting what he has say about the growth of infrastructure code and its effect on productivity.



So, what frameworks do I like for web development? Well, I love jQuery for one. This massively simplifies your code, and with the wealth of plugins life is made easy. And, the syntax is easy to read for the next developer who works on your code. It follows the KIS principle. Symfony does not.

I’m sure lots of people disagree with me on this, this is obviously just my opinion based on my own personal experience. But, figured it was worth sharing.

Categories: Opinion, php
Tags:
Comments (0)

Nginx & Wordpress MU

April 4, 2010

We’ve been working on a huge, soon to announced project, that involves Wordpress MU. I’ve been working for months on this, and I’ve really got stuck into the deepest and darkest parts of Wordpress MU. I’ve got to say, I’m a fan of Wordpress. The code is nice to work with, its well documented (for the most part) and it just works. As opposed to some of the other open source CMS and blog engines out there.

But, I just kept hitting performance bottle necks with it. Which, is to be expected when you’re serving heavy php pages with lots of MySQL reads. I went down the well traveled road of installing various caching plugins (WP Super Cache was my favorite), and they made a huge difference – but still, performance not quite where I felt it should be. This project is for a very high traffic site, so its really worth squeezing every last percentage of performance out of your code.

I was about to roll out some kind of reverse caching proxy, such as Squid or a server cluster when I started to debate changing out Apache for something lighter and faster. I’d heard good things about lighthttp and Nginx. I was a little nervous, as this is a live production site, but the performance gains could be worth it. When I learned that wordpress.com uses Nginx, that sealed it for me. Nginx it is then!

So, after much trepidation, I pulled the trigger and started to rebuild the app on a new server running Nginx (a LEMP stack, as opposed to LAMP). It took me a long and arduous 2 days to get everything back up and running (on a dev server first, customers didn’t experience any downtime), but man, first impressions blew my mind. It is way faster, especially for serving images. I intend to write a detailed post about the setup, but for now here is a graph that shows a quick comparison. The site is monitored by Site24×7, and the following graph shows the ping times for a given page. This page had very simple content, so I wasn’t expecting much. But, see for yourself. Its a big difference, note that the site was cached before and after the change it was not cached. Once I roll out caching on the new nginx server then it will be fair comparison, i.e. this graph makes it looks like nginx is slower then it really could be.

Some quick lessons learned, the hard way.

PHP sessions did not work, I spent hours trying to get them to work under nginx but then just decided it was time to roll out a MySQL based session system, its more secure and portable and only took about 2 hours to get up and running on the live site. I’ll post a how-to article soon.

If images aren’t being served correctly it could be because FastCGI php cache’s directory needs to be owned by the same user that Nginx is running as. Obvious I know, but still, not so obvious at the time. Well, lots of things aren’t obvious at 2 in the morning!

I promise I’ll post more details shortly.

Categories: Products, php
Tags: , ,
Comments (0)

Electronic Arts’ Spore Islands

December 4, 2009

Its been out for a few months now, but we just wanted to announce the official release of Electronic Arts’ latest game in the Spore series, Spore Islands for Facebook.

We had the privilege of assisting Area/Code in its development. We were the lead php and AI developer for this game, it was a lot of fun and a technically challenging project. We have a lot of experience building websites that support high traffic, but supporting a game as advanced as this with as much back-end logic as this required, was always going to be a challenge. But, we rose to the challenge and we’re extremely proud to have been a part of this great game series!

Try it out on Facebook:

http://apps.facebook.com/sporeislands/

Categories: AI, Games, News, php
Tags: ,
Comments (0)

PHP Logger with stacktrace

November 17, 2009

Logging in php isn’t fun, lets face it. There are quite a few php loggers out there, but we’ve built our own over the years and we felt why not share this with the community? So, why did we build our own? Well, firstly I personally like log files that are actually readable and secondly I really need a stack trace, I need to know not only what part of the code fell over, but what called that piece of code? And, then what called that piece of code. Sometimes I like to use the system log file, and just have a terminal open (yeah, we’re a Mac shop here!) and just watch the log using tail -f php_errors.log. But, sometimes I like to view the log in html for small test scripts. And, another thing is that sometimes I want to setup the logger to email me any fatal errors, this is very handy as sometimes these can go under the radar especially on really busy sites.

Our Logger class is static, which is great for speed and means there’s only ever one instantiation floating around. It also initializes itself, so you don’t need to worry.


require_once("Logger.class.php");

// This tells the logger to echo html
Logger::echoLog();

// Tells the Logger to catch and log system errors
Logger::catchSysErrors();

// This tells the logger to email you fatal errors. The email will contain the error
// message as well as a dump of the POST, GET and SESSION variables
Logger::setupEmail($email, $projectName, $logLevel = self::$FATAL)  

// You can set the logging level (the default is debug)
// Level can be (in order);
// Logger::$DEBUG, Logger::$INFO, Logger::$ERROR, Logger::FATAL
// e.g., if you set the level to ERROR, only ERROR and FATAL messages would be outputted
// e.g., if you set the level to INFO, only INFO, ERROR & FATAL messages would be outputted
Logger::setLevel(Logger::$DEBUG);

// To use, call any of these
Logger::fatal("Some error message");
Logger::error("Some error message");
Logger::debug("Some error message");
Logger::info("Some error message");

// You can also dump a variable, array or object's contents with
Logger::dump($variable);

Here is the Logger class (apologies that the indenting is a little screwy);

/**
* Logging class, use for all trace commands
*
*/
class Logger {

	public static $DEBUG	 	= 0;
	public static $INFO 		= 1;
	public static $WARNING 		= 2;
	public static $ERROR	 	= 3;
	public static $FATAL	 	= 4;

	private static $debugLevel = 0;

	private static $echoLog = false;

	private static $email		= "dev@adastrasystems.com";
	private static $project     = "Adastra Project";
	private static $sendEmail   = true;
	private static $emailLevel  = 4;

	// //////////////////////////////////////////////////////////////////////////////////////

	/**
	* Class constructor
	*/
	public function __construct(){
		self::init();
	}

	// //////////////////////////////////////////////////////////////////////////////////////

	public static function echoLog(){
		self::$echoLog = true;
	}

    // //////////////////////////////////////////////////////////////////////////////////////

    /**
     * Let the Logger catch any system errors
     */
    public static function catchSysErrors(){
        set_error_handler("Logger::sysErrorHandler");
    }

	// //////////////////////////////////////////////////////////////////////////////////////

	/**
	*
	*/
	private static function trace($level, $msg){

		if ($level < self::$debugLevel){
			return;
		}

		$bt = debug_backtrace();

		$class = "";
		$function = "";

		// get class, function called by caller of caller of caller
		if (isset($bt[2]['class'])){
			$class = $bt[2]['class'];
			$function = "." . $bt[2]['function'];
		}

		// get file, line where call to caller of caller was made
		$file = $bt[1]['file'];
		$line = $bt[1]['line'];

		$file_name = basename($file);

		if (self::$echoLog){

			switch($level){
				case self::$DEBUG: 	$levMsg = "debug"; break;
				case self::$INFO:  	$levMsg = "info"; break;
				case self::$WARNING:$levMsg = "warning"; break;
				case self::$ERROR: 	$levMsg = "error"; break;
				case self::$FATAL: 	$levMsg = "fatal"; break;
			}

			$msg = "[$levMsg $class$function] $msg";
			$msg .= " on line $line of $file_name";
			if (isset($bt[2]['file'])){
				$fname = basename($bt[2]['file']);
				$msg .= ", called from line ".$bt[2]['line']." of $fname";
			}
			$msg .= "\n";

			echo $msg;

			flush();

		}
		else {

			// Get IP address....

			// Translate level into text...
			$levMsg = "????";
			switch($level){
				case self::$DEBUG: 	$levMsg = "DEBUG"; break;
				case self::$INFO:  	$levMsg = "INFO"; break;
				case self::$WARNING:$levMsg = "WARNING"; break;
				case self::$ERROR: 	$levMsg = "ERROR"; break;
				case self::$FATAL: 	$levMsg = "FATAL"; break;
			}

			$msg = "[$levMsg] $class$function $msg";
			$msg .= " {on line $line of $file_name";
			if (isset($bt[2]['file'])){
				$fname = basename($bt[2]['file']);
				$msg .= ", called from line ".$bt[2]['line']." of $fname";
			}
			if (isset($bt[3]['file'])){
				$fname = basename($bt[3]['file']);
				$msg .= ", called from line ".$bt[3]['line']." of $fname";
			}
			$msg .= "}";

			error_log($msg);
		}

        if (self::$sendEmail){
			self::sendEmail($msg, $level);
        }		

	}

	// //////////////////////////////////////////////////////////////////////////////////////

	private static function sendEmail($msg, $level){

		if ($level < self::$emailLevel){
			return;
		}

		$headers = 'From: ' . self::$email;
		$message = $msg . "\n\nPOST VARIABLES \n\n" . print_r($_POST, true) . "\nGET VARIABLES\n\n" . print_r($_GET, true) . "\nSESSION VARIABLES\n\n" . print_r($_SESSION, true);
		mail(self::$email, self::$project, $message, $headers);
   	}

	// //////////////////////////////////////////////////////////////////////////////////////

    /**
     * Use to replace system error handler, if desired. Use Logger::catchSysErrors() to
     * activate
     */
    public static function sysErrorHandler($errno, $errstr, $errfile, $errline){

        switch ($errno) {
            case E_USER_ERROR:
                $levMsg = "SYS_ERROR";
                break;

            case E_USER_WARNING:
                $levMsg = "SYS_WARNING";
                break;

            case E_USER_NOTICE:
                $levMsg = "SYS_NOTICE";
                break;

            default:
                $levMsg = "SYS_UNKNOWN";
               break;
        }

        if (self::$echoLog){

			switch($errno){
				case E_USER_NOTICE:  $levMsg = "info"; break;
				case E_USER_WARNING:$levMsg = "warning"; break;
				case E_USER_ERROR: 	$levMsg = "error"; break;
				default: $levMsg = "debug"; break;
			}

			$msg = "[$levMsg #$errno] $errstr";
			$msg .= " on line $errline of ".basename($errfile)."";
			$msg .= "\n";

			echo $msg;

			flush();

        }
        else {

            $msg = "[$levMsg] No: $errno Msg: $errstr";
            $msg .= " {on line $errline of ".basename($errfile)."}";

            error_log($msg);
        }

        /* Don't execute PHP internal error handler */
        return true;
    }

    // //////////////////////////////////////////////////////////////////////////////////////

	public static function setupEmail($email, $projectName, $logLevel = self::$FATAL) {
		self::$sendEmail = true;
		self::$emailLevel = $logLevel;
		self::$project = $projectName;
	}

    // //////////////////////////////////////////////////////////////////////////////////////

	public static function setLevel($newLevel) { $debugLevel = $newLevel; }

	public static function setLevelDebug() { self::$debugLevel = self::$DEBUG; }
	public static function setLevelInfo() { self::$debugLevel = self::$INFO; }
	public static function setLevelWarning() { self::$debugLevel = self::$WARNING; }
	public static function setLevelError() { self::$debugLevel = self::$ERROR; }
	public static function setLevelFatal() { self::$debugLevel = self::$FATAL; }

	// //////////////////////////////////////////////////////////////////////////////////////

	public static function dump($var) { self::debug(print_r($var, true)); }
	public static function debug($msg){ self::trace(self::$DEBUG, $msg);	}
	public static function warning($msg){ self::trace(self::$WARNING, $msg);	}
	public static function warn($msg){ self::trace(self::$WARNING, $msg);	}
	public static function error($msg){ self::trace(self::$ERROR, $msg);	}
	public static function info($msg){  self::trace(self::$INFO, $msg);	}
	public static function fatal($msg){ self::trace(self::$FATAL, $msg);	die(); }

	// //////////////////////////////////////////////////////////////////////////////////////
}

Categories: php
Tags: ,
Comments (0)

Facebook development

September 1, 2009

Ad Astra has just completed a major 7-month project developing a game for the facebook platform. Ad Astra was the lead developer for the server-side code. The game, well, more of a simulation-game, was written in php and MySQL and was pretty computationally intensive. We were selected because of our expertise in both Artificial Intelligence and also our expertise in developing large scale websites. The game itself hasn’t officially launched yet, so we’re not able to publicize any details of the game yet – but when it is we’ll post an update here. But, what I can say is that the publisher is one of the largest game publishers in the world, so it was very exciting to work on a project for them. And, as always, working on games is just fun – though there are some long hours to get it just right. And, it was also fun to work on a team of talented professionals, artists, producers and developers. Awesome, can’t wait to get another game contract!

Categories: AI, Games, php
Tags: , , ,
Comments (0)