#!/usr/bin/perl

#<ENC>54</ENC>

package scloud;

########################################################################
# COPYRIGHT NOTICE:
#
# Copyright 2008 FocalMedia.Net All Rights Reserved.
#
# Selling the code for this program without prior written consent 
# from FocalMedia.Net is expressly forbidden. You may not 
# redistribute this program in any shape or form.
# 
# This program is distributed "as is" and without warranty of any
# kind, either express or implied. In no event shall the liability 
# of FocalMedia.Net for any damages, losses and/or causes of action 
# exceed the total amount paid by the user for this software.
#
########################################################################


#### EDIT HERE -- FOR WINDOWS/IIS BASED INSTALLATIONS ONLY #######

$config_cgi = "config.cgi"; ## <-- CHANGE THIS LINE TO THE FULL SERVER PATH TO config.cgi

# THE PATH ON A WINDOWS INSTALLATION WILL LOOK SOMETHING LIKE THIS:
# $config_cgi = "c:/inetpub/webpub/cgi-bin/pseek/config.cgi";

#### DO NOT CHANGE ANYTHING BELOW THIS LINE #################


#use FindBin;
#use lib $FindBin::Bin;

use CGI::Carp qw(fatalsToBrowser);
use CGI;
use lib ".";


require Exporter;

use vars qw/@ISA @EXPORT @EXPORT_OK $copyright $prgname/;

@ISA = qw(Exporter);

@EXPORT = qw(do_search_cloud); 

@EXPORT_OK = qw();

$q = CGI->new;

$default_permissions = 0777;

$q = CGI->new;

#####################################

sub do_search_cloud
{
my ($cloud_settings, $curr_search_scr, $data_dir1, $web_dir1) = @_;

for ($ms = 0; $ms < length($q->param('keywords')); $ms++) 
	{
	$oneletter = substr($q->param('keywords'), $ms, 1);
	if (($oneletter !~ /[0-9a-zA-Z]/) and ($oneletter ne " ")) { $no_rec = "true"; }
	}

if (length($q->param('keywords')) > 20) { $no_rec = "true"; }
elsif (length($q->param('keywords')) < 3) { $no_rec = "true"; }


		###### Check badwords
	
			$filesize = (-s "$data_dir1/scloud3.dat");
			open (TFILECNTS, "$data_dir1/scloud3.dat");
					read(TFILECNTS,$badwords,$filesize);
			close (TFILECNTS);
			@badword_items = split (/,/, $badwords);

			$bkeywords = $q->param('keywords');
			if ($bkeywords =~ / /)
				{
				@kwitems_tmp = split (/ /, $bkeywords);
				foreach $kwi (@kwitems_tmp)
					{
					if ($kwi ne "")
						{
						push (@kwtest, $kwi);
						}
					}
				}
				else
				{
				$kwtest[0] = $q->param('keywords');
				}


				foreach $bitem (@badword_items)
					{
						foreach $kitem (@kwtest)
							{
							if (lc($bitem) eq lc($kitem))
								{
								$no_rec = "true";
								}
							}
					}

		###############


($sc_logging, $sc_font_to_use, $sc_start_size, $sc_end_size, $sc_linenr, $sc_maxwords, $sc_seperator) = split (/\t/, $cloud_settings);

##### IF WE ARE RECORDING KEYWORD

if ($no_rec ne "true")
	{
	$sc_cnt = 1;
	open (SCLOG, "$data_dir1/scloudrec.dat");
		while (defined($sc_line=<SCLOG>))
			{
			$sc_line =~ s/\n//gi;
			$crit1 = chr(13); $crit2 = chr(13); $sc_line =~ s/$crit1//gi; $sc_line =~ s/$crit2//gi;
			
				if (($sc_cnt < $sc_maxwords) and (length($sc_line) > 2) and (lc($sc_line) ne lc($q->param('keywords'))))
					{
					$sc_tokeep = $sc_tokeep . "$sc_line\n";
					}

				$sc_cnt++;
			}
	close (SCLOG);
	
		$sc_write = $q->param('keywords') . "\n" . $sc_tokeep;
		
		if ($nowrite ne "true")
			{
			open (SCLOG, "> $data_dir1/scloudrec.dat");
				print SCLOG $sc_write;
			close (SCLOG);
			}
	
	}
	else #### Read searchcloud keywords
	{
	if ((-e "$data_dir1/scloudrec.dat") > 0) 
		{
		$filesize = (-s "$data_dir1/scloudrec.dat");
		open (TFILECNTS, "$data_dir1/scloudrec.dat") || die "$filename";
			read(TFILECNTS,$sc_write,$filesize);
		close (TFILECNTS);
		}
	}


#### UPDATE SEARCH CLOUD

	if ((-e "$data_dir1/scloud2.dat") > 0) 
		{
		$filesize = (-s "$data_dir1/scloud2.dat");
		open (TFILECNTS, "$data_dir1/scloud2.dat") || die "$filename";
			read(TFILECNTS,$sc_template,$filesize);
		close (TFILECNTS);
		}

($sc_main, $sc_style) = split (/--ff--oo--:-/, $sc_template);

@sc_keywords = split(/\n/, $sc_write);

for ($sc_i = @sc_keywords; --$sc_i; ) 
		{
       my $j = int (rand($sc_i+1));
       next if $sc_i == $j;
       @sc_keywords[$sc_i,$j] = @sc_keywords[$j,$sc_i];
    	}


$sc_all_kw = "";
$sc_cnt = 0;
foreach $sc_item (@sc_keywords)
	{
	$sc_tmp_style = $sc_style;
	
	$sc_fsize = int( rand($sc_end_size-$sc_start_size+1)) + $sc_start_size;

	$sc_searchlink = $curr_search_scr . "?keywords=" . $sc_item;
	
	$sc_tmp_style =~ s/!!font!!/$sc_font_to_use/gi;
	$sc_tmp_style =~ s/!!size!!/$sc_fsize/gi;
	$sc_tmp_style =~ s/!!cloud_word!!/$sc_item/gi;
	$sc_tmp_style =~ s/!!keyword_link!!/$sc_searchlink/gi;
	
	if ($sc_cnt != 0) { $sc_all_kw = $sc_all_kw . " " . $sc_seperator . " " . $sc_tmp_style; }
	else { $sc_all_kw = $sc_all_kw . $sc_tmp_style; }
	
	$sc_cnt++;
	}


$sc_main =~ s/!!cloud_keywords!!/$sc_all_kw/gi;
$sc_main =~ s/\n/ /g;
$crit1 = chr(13); $crit2 = chr(10); $sc_main =~ s/$crit1/ /gi; $sc_main =~ s/$crit2/ /gi; 
$sc_main =~ s/'/\\'/g;

$sc_jscript_write = qq[
function get_cloud()
{
document.write ('$sc_main');
}
];

open (SSCL, "> $web_dir1/sscloud.js");
	print SSCL $sc_jscript_write;
close (SSCL);


}


1;