<?
// avatar randomizer (avatar.php)
// Copyright 2005 Thad Ward
// This code is released under the GPL license VERSION 2 ONLY.
// (see http://www.gnu.org/licenses/gpl.txt or the COPYING file in this directory.)

define("AVATAR_VERSION"0.12);

// NOTE: to use this script with some forums (such as phpBB), you will need
// to add a fake filename after the name of the script.
// for example: "http://example.com/avatar.php/fake.jpg"
// this is because the validation code checks to make sure you give it a link
// to an image file. it doesn't matter what you put after the / that follows the
// name of the script, as this script just ignores it currently. You can, for
// example, use this as an identifier for which forums the link came from.

// TODO: I really need to add more error checking, but I am not sure what I should
// do if something fails. perhaps return a 404?

// BLEH. if we do this, we can't tell phpBB to use us
// if this script was loaded directly, redirect to the directory we are in
//if(!array_key_exists("HTTP_REFERER", $_SERVER))
//{
//    header("Location: http://". $_SERVER["HTTP_HOST"] . dirname($_SERVER["SCRIPT_NAME"]) . "/");
//    exit;
//}

// open the current directory
$d opendir(".");

$imgs = array();

// read in info on every file
while(($file readdir($d)) !== false)
{
    
// if this entry is a directory, skip it
    
if(is_dir($file))
        continue;

    
// get the mime type of the file (note: this depends on the deprecated "mimetype" extension)
    
$mtype = @mime_content_type($file);
    
    
// if the file is not an image file, skip it
    
if(!= strcasecmp("image"substr($mtype05)))
        continue;
    
    
// store off the filename and mime type
    
$imgs[] = array("file" => $file"type" => $mtype);
}
closedir($d);

// select a random entry from the array
$f $imgs[array_rand($imgs1)];

// send the proper headers to the browser
header("Content-Type: " $f["type"]);
header("Content-Length: " filesize($f["file"]));

// tell PHP to send the file to the browser
readfile($f["file"]);