<?php

/*
	Software Secret Weapons Code Library
	Copyright (C) 2007 Pavel Simakov
	http://www.softwaresecretweapons.com
	
	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.
	
	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

function oySeigLambda($matches, $key, $base){
	$em_art = new OYSeig();
	return " <img src='".$base.$em_art->encodeURL($matches[2]."@".$matches[3], $key)."'> ";
}


class OYSeig{

	function RIJNDAEL_encrypt($text, $key){
		$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
		$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

		return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv));
	   
	}

	function RIJNDAEL_decrypt($text, $key){
		$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
		$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

		return mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($text), MCRYPT_MODE_ECB, $iv);
	}

	function drawImageText($text, $r, $g, $b){
		$font = 'monofont.ttf';

		$code = $text;
		$height = 18;

		$font_size = $height * 0.8;
		$textbox = imagettfbbox($font_size, 0, $font, $code) or die('Error in imagettfbbox function');
		$width = $textbox[4] + 8;

		$x = ($width - $textbox[4])/2;
		$y = ($height - $textbox[5])/2;

		$image = imagecreatetruecolor($width, $height) or die('Cannot initialize new GD image stream');

		$background_color = imagecolorallocate($image, 255, 255, 255);
		$text_color = imagecolorallocate($image, $r, $g, $b);

		imagefilledrectangle($image, 0, 0, $width, $height, $background_color);
		imagettftext($image, $font_size, 0, $x, $y, $text_color, $font, $code) or die('Error in imagettftext function');

		imagegif($image);
		imagedestroy($image);
	}

	function encodeURL($text, $key){
		$ctext = $this->RIJNDAEL_encrypt($text, $key);

		return "c=".urlencode(base64_encode($ctext)).'&h='.urlencode(md5($text));
	}

	function decodeURL($key, $props){
		$h = $props['h'];
		$c = $props['c'];

		$ctext = base64_decode($c);
		$text = trim($this->RIJNDAEL_decrypt($ctext, $key));

		if ($h != md5($text)){
			return false;
		}
		return $text;
	}

	function renderGif($text, $r, $g, $b){
		header("Content-Type: image/gif");
		header("Cache-Control: max-age=3600");
		$this->drawImageText($text, $r, $g, $b);
	}

	function safeText($text, $key, $base){
		$ret = ' ' . $text;
		$ret = preg_replace_callback(
			"#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", 
			create_function(
				'$matches',
				'return oySeigLambda($matches, "'.$key.'", "'.$base.'");'
			),
			$ret
		);
		return $ret;
	}

}

?>
>