<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 11/6/2014	ELL
//
	
	session_start();
	
	$mask = "captcha/*.captcha";
	array_map("unlink", glob($mask));
	
	$doc_root = getenv('DOCUMENT_ROOT');
	$captcha_path = $doc_root . "/manage/captcha";
	$captcha_images = array();
	$i = 0;
	
	if ($handle = opendir($captcha_path)) {
		while (false !== ($entry = readdir($handle))) {
			if ($entry != "." && $entry != ".." && $entry != "index.php") {
				$captcha_images[$i] = $entry;
				$i++;
			}
		}
		closedir($handle);
	}
	
	$index = rand(0, $i - 1);
	$link = "captcha/" . time() . ".captcha";
	$target = "captcha/" . $captcha_images[$index];
	if (link($target , $link))	{
		$use_captcha = true;
		$captcha = hash('sha256', $captcha_images[$index]);
		$_SESSION['captcha'] = $captcha;
	}
	else
		$use_captcha = false;
		
	echo $link;
?>