Location: PHPKode > scripts > IRV > kbjr-IRV-5c9960c/IRV.php
<?php

/*
 * IRV.php
 *
 * Software: IRV - Image Recognition Verification
 * Version:  0.1.3 Dev
 * Author:   James Brumond
 * Created:  13 May, 2010
 * Updated:  13 May, 2010
 *
 * Copyright 2010 James Brumond
 * Dual licensed under MIT and GPL
 */

// make sure we have an open session
@session_start();

// start the IRV class
class IRV {

/*
 * Private/Protected Properties
 */

	protected static $image_dir = '/images/';
	
	protected static $image_dims = array('x' => 100, 'y' => 100);

	protected static $images = array(
		'a butterfly' => 'butterfly.jpg',
		'a cat'       => 'cat.jpg',
		'a dragonfly' => 'dragonfly.jpg',
		'an eagle'    => 'eagle.jpg',
		'a flower'    => 'flower.jpg',
		'a frog'      => 'frog.jpg',
		'a waterfall' => 'waterfall.jpg'
	);
	
	private static $count = 0;

/*
 * Private/Protected Methods
 */
	
	private static function build_path(&$path, $images = false) {
		$path = preg_replace('#^' .
			str_replace('/', '\/', $_SERVER['DOCUMENT_ROOT'])
		. '#', '', dirname(__FILE__)) .
		(($images) ? self::$image_dir : '');
	}
	
	private static function build_image($src, $alt, $height = null, $width = null, $title = null) {
		$img = '<img src="' . $src . '" alt="' . $alt . '" ' .
			(($height !== null) ? 'height="' . $height . '" ' : '') .
			(($width  !== null) ? 'width="'  . $width  . '" '  : '') .
			(($title  !== null) ? 'title="'  . $title  . '" '  : '') .
		'/>';
		return $img;
	}
	
	private static function build_selection(&$selection) {
		$items = array_rand(self::$images, 4);
		$selection = array(); $i = 0;
		foreach ($items as $item) {
			$selection[ (string) $i++ ] = array(
				'text' => $item,
				'image' => self::$images[$item]
			);
		}
		shuffle($selection);
		$key = array_rand($selection, 1);
		$selection['chosen'] = $key;
	}

/*
 * Public Methods
 */
	
	/*
	 * @return array
	 *  [0] => request string
	 *  [1] => form inputs
	 */
	
	public static function build_test($format = '%l%i%b%L') {
		self::build_path($path, true);
		self::build_selection($selection);
		
		$count = self::$count++; $i = 0;
		$code = array();
		foreach ($selection as $key => $info) {
			if ($key === 'chosen') continue;
			extract($info, EXTR_OVERWRITE);
			$pieces = array(
				'%l' => '<label class="irv-item irv-' . $count . '" id="irv-' . $count . '-' . $i . '">',
				'%L' => '</label>',
				'%b' => '<input type="radio" name="irv' . $count . '" value="' . $i++ . '" />',
				'%i' => self::build_image($path . $image, '', self::$image_dims['x'], self::$image_dims['y'], '')
			);
			$item = $format;
			foreach ($pieces as $key => $str) {
				$item = str_replace($key, $str . "\n", $item);
			}
			$code[] = "\n" . $item . "\n";
		}
		$_SESSION['irv' . $count] = serialize($selection);
		return array(
			'<span class="irv-text irv-' . $count . '">Please select the image of ' . $selection[$selection['chosen']]['text'] . '</span>',
			implode("\n", $code)
		);
	}
	
	/*
	 * @return
	 *  if not yet tested: NULL
	 *  if wrong selection: bool(false)
	 *  if correct selection: bool(true)
	 */
	
	public static function check_test($which = 0) {
		$code = 'irv' . $which;
		if (! $_SESSION[$code]) return null;
		$selection = unserialize($_SESSION[$code]);
		$response = $_REQUEST[$code];
		if ($selection['chosen'] == $response) return true;
		return false;
	}

}

?>
Return current item: IRV