#!%PERL% -w
# Copyright (c) vhffs project and its contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions 
# are met:
#
# 1. Redistributions of source code must retain the above copyright 
#   notice, this list of conditions and the following disclaimer.
#2. Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in 
#   the documentation and/or other materials provided with the 
#   distribution.
#3. Neither the name of vhffs nor the names of its contributors 
#   may be used to endorse or promote products derived from this 
#   software without specific prior written permission.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
#"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
#LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
#FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
#COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.

use strict;
use utf8;
use CGI;
use GD;
use GD::Text::Wrap;
use lib '%VHFFS_LIB_DIR%';
use Vhffs::Main;
use Vhffs::Object;
use Vhffs::Panel::Avatar;
use Vhffs::Functions;

#Get some basics informations with CGI
my $cgi;
my $oid;
my $code;
my $vhffs;
my $gd;
my $wp;
my $black;
my $white;
my $blue;
my $path;
my $buf;
my $object;

$cgi 		= new CGI;
$cgi->charset('utf-8');
$vhffs		= init Vhffs::Main;	
$oid 		= $cgi->param( "oid" );

$object = Vhffs::Object::get_by_oid( $vhffs , $oid );

$path = Vhffs::Panel::Avatar::exists_avatar( $vhffs , $object );

if( ! ( defined $oid ) )
{
	print CGI->header( -type=>"text/html", -charset=>"utf-8" );
	print "oid error";
	exit 1;
}

$gd = GD::Image->new(70,100);
$white = $gd->colorAllocate(255,255,255);
$black = $gd->colorAllocate(  0,  0,  0);
$blue  = $gd->colorAllocate(127,127,255);

#print "No colours: $black ", $gd->colorsTotal, "\n";
    

# Assume the user has set FONT_PATH or TTF_FONT_PATH
#$wp->font_path('/usr/share/fonts/ttfonts');
print CGI->header( -type=>"image/png" );

binmode STDOUT;

# TODO user pregenerated images	

unless( Vhffs::Functions::strtobool( $vhffs->get_config->get_panel->{'users_avatars'} ) or Vhffs::Functions::strtobool( $vhffs->get_config->get_panel->{'groups_avatars'} ) )
{
	$wp = GD::Text::Wrap->new($gd,
		width       => 70,
		line_space  => 0,
		color       => $black, 
		text        => "This platform does not support avatar",
	);
	$wp->set_font(gdLargeFont, 14);
	$wp->set(align => 'center');
	$wp->draw(0,5);
	$wp->set(para_space => 10, preserve_nl => 0);
	print STDOUT $gd->png();
}
else
{
	if( defined $path )
	{
		open FORIG , "$path";
		while( read( FORIG , $buf , 1024 ) )
		{
			print STDOUT $buf;
		}
		close( FORIG );
	}
	else
	{
		$wp = GD::Text::Wrap->new($gd,
			width       => 70,
			line_space  => 0,
			color       => $black, 
			text        => "No avatar",
		);
		$wp->set_font(gdLargeFont, 14);
		$wp->set(align => 'center');
		$wp->draw(0,5);
		$wp->set(para_space => 10, preserve_nl => 0);
		print STDOUT $gd->png();
	}
}

close STDOUT;
