<?php

require_once('PEAR.php');
require_once('Text/CAPTCHA.php');

class Captcha extends Vn\Web\RestRequest {
	function run($db) {
		$options =
		[
			 'width'        => 130
			,'height'       => 45
			,'output'       => 'png'
			,'imageOptions' =>
			[
				 'font_size'        => 20
				,'font_path'        => __DIR__
				,'font_file'        => 'IndieFlower.ttf'
				,'text_color'       => '#559955'
				,'lines_color'      => '#559955'
				,'background_color' => '#DDDDDD'
				,'antialias'        => true
			]
		];

		$captcha = Text_CAPTCHA::factory('Image');
		$retval = $captcha->init($options);

		if (PEAR::isError($retval))
			throw new Exception('Error initializing CAPTCHA: %s!',
				$retval->getMessage());

		$png = $captcha->getCAPTCHA();

		if (PEAR::isError($png))
			throw new Exception('Error generating CAPTCHA: %s!',
				$png->getMessage());

		// Get secret passphrase
		$_SESSION['captcha'] = $captcha->getPhrase();

		header('Content-Type: image/png');
		echo $png;
	}
}