PHP Class: CrazeDownload

First posted as part of the games I play project. I don’t think there will be major changes to this file, so here it is. My Xfire page, Games,  is using this class to download the XML data and the game icons to go with it.

To use this class, include it in your script and create a new CrazeDownload instance with remote resource location as parameter. Optional parameters are minimum time before redownload and local cache filename.

object CrazeDownload(String $remote_url [,int $ttl [,String $local_filename]])

  // We need to include the file to use the features in it
  include "classes/CrazeDownload.class.php";

  // Creating a CrazeDownload will copy remote resource to local cache
  $resource = new CrazeDownload('http://example.net/feed.xml');

  // We can now ask which file to use instead of the URL
  $filename = $resource->cachefile();

The class itself:

<?php
/*
 * CrazeDownload copies remote data to local cache
 *
 * @package Craze
 * @author Geir Andre Halle
 * @version v11.8.3
 * @copyright 2009,2011 Geir Andre Halle
 * @license http://www.gnu.org/licenses/gpl.txt
 */
class CrazeDownload {
    // Initializing variables
    private $cachepath = 'cache/' ;    // Local folder for temporary storage
    private $cachefile = '' ;          // Filename variable for use later

    /*
     * @param string $url uniform resource locator/identifier
     * @param int $ttl Time-to-live, in seconds between refresh
     * @param string $filename For occasions you don't want an encoded string
     */
    function __construct($url, $ttl = 300, $filename = "") {

        // Generated filename is encoded from url to avoid slashes and colons in the filesystem
        if ("" != $filename) {
                $this->cachefile = $this->cachepath . $filename;
        } else {
                $this->cachefile = $this->cachepath . str_replace("/","-",base64_encode($url));
        }

        // Folders are not created automatically (by design choice). Tell user where to make one.
        if (!is_dir($this->cachepath)) {
            die ("Cache folder not found! Try to create manually: $this->cachepath");
        } elseif(!is_writable($this->cachepath)) {
            die ("Cache folder $this->cachepath not writable!");
        }

        // If local file does not exist or is too old, retrieve remote data.
        if ( (!file_exists($this->cachefile)) || ((time() - filemtime($this->cachefile)) > $ttl) ) {

            // File transfer
            $remoteio = curl_init() ;
            curl_setopt($remoteio, CURLOPT_URL, $url) ;
            curl_setopt($remoteio, CURLOPT_HEADER, 0) ;
            curl_setopt($remoteio, CURLOPT_RETURNTRANSFER, true) ;
            $remotecontent = curl_exec($remoteio) ;
            curl_close($remoteio) ;

            // Save resource to local file
            $cacheio = fopen($this->cachefile, 'w');
            fwrite($cacheio, $remotecontent);

            // Clean up
            fclose($cacheio);
        }
    }

    /*
     * @return string Local filename
     */
    public function cachefile() {
        return "$this->cachefile" ;
    }
}
?>
Categories: Development | Tags: , , | Leave a comment

The games I play page is being tested live

Success! Or so it seems.

I just finished my first version of my games I play page. Look for ‘Games’ in the menu. Just need to test it for a couple of days while cleaning up some of the code before I release the source. Ended up using my custom downloader and SimpleXML. Again, code is far from what it could be, but my attention span has little room for subtle improvements once something works.

Categories: Uncategorized | Leave a comment

The games I play project

Background

Few days back, I gave myself this challenge: I want a list of the games I play to put on this webpage.

Easily done to write one up and put it here, but I had a couple of concerns. I don’t want to have to update it every time I start or stop playing a game, and I don’t want it to show games I stopped playing long time ago.

    So I made myself a list with specifications:

  • Automatically updated
  • Current games only
  • Sorted list, most active on top
  • List includes game graphics or icons
  • Cache remote data, out of good habit

Xfire

My first design choice was to create a PHP page using data from Xfire. With Xfire I have access to my list of games, and can sort out which ones I visited in the last 7 days. Did consider Steam, but couldn’t be too sure how it would work out with non-Steam games. Or would it even function at all if my profile was set to private?

A drawback with Xfire is how you get access to the gameplay feeds. Your webserver IP has to be enabled on your account, and you have to send you username and password in cleartext each time you access the feed. To make it slightly worse, a single Xfire account can only have one IP tied to it. For my own safety, I created a new Xfire account with the sole purpose of holding my webserver IP and access the restricted XML feeds. Xfire confirms that this is the recommended way of doing it, as they acknowledge the security risks involved in sending passwords of personal accounts unencrypted over the Internet.

Before I actually got any work done, I contacted Xfire support and requested access from my webserver IP with the new secondary Xfire account I created. By doing this first I could download my XML file and get an idea of what I’m up against.

The Xfire feed I plan to use is available at http://www.xfire.com/xml/xfireusername/user_gameplay/ once Xfire adds my webserver IP to an account. It appears important that the IP is added to the account I will be using for cleartext authentication, so the IP has to be tied to the second account I created just for this purpose. The username in the address is replaced with my main Xfire ID, while the secondary account with password is added behind the address as GET parameters. Accessing the feed without login info gives an error notice specifying the need to use the format <url>?u=<xfire login>&p=<xfire password> where the orange parts are replaced with real information.

To sum up some of the Xfire stuff: You don’t have to log in with the username you’re trying to get feeds from, but the one you use for login must be associated with the webserver IP and should not be used for anything else. At least nothing important you can’t afford to loose.

Coding

The next thing I did, was to prepare one of my previous PHP-classes for new use. It is a download class that copies remote data and stores a local cache for some time before it’s redownloaded. I was quite confident it would be handy as my gameplay doesn’t warrant updates every few seconds, and I already had a simple code for using a cache. Not perfect, but it gets the job done.

Now that I have a sample XML and a functional downloader, I’m prepared to proceed with the next step. Start planning how my program should work with the XML data. That also means I need to attack it from the other end and make some decisions on what I want to end up with.

Will share some code when I no longer make changes to it. Can’t say when, as work on this undertaking will be on and off.

Categories: Development | Tags: , , , , , | Leave a comment

Ape With AK-47

Categories: Videos | Tags: | Leave a comment

Minecraft Levitates!

Minecraft levitation

Levitating in Minecraft.

A screenshot of a future update to the 3D survival game of Minecraft was recently revealed on Twitter by creator Markus “Notch” Persson. In this screenshot we can see what appears to be a Minecraft player far above ground. Most likely this new addition to the franchise is some kind of flying or levitation, to enable access to high and otherwise unreachable places. It could also mean alternate means of transport for the adventurer.

Categories: Games | Tags: , | Leave a comment