#!/usr/bin/perl 
use Fcntl qw(:DEFAULT :flock);
use Digest::MD5 qw(md5_hex);
use DBI;

$mp3_home = "/usr/MP3_and_streaming_files_folder/mp3/";
$real_home = "/usr/MP3_and_streaming_files_folder/real/";
$media_home = "/usr/MP3_and_streaming_files_folder/windows/";
$home = "/usr/MP3_and_streaming_files_folder/";
$flag_set_dir="http://www.national-anthems.net/images/flags/";
$slowa_dir = "http://www.national-anthems.net/images/hymny/";

$string = $ENV{'QUERY_STRING'};

open(IN,"/home/homepage/national-anthems.net/htdocs/web/add.html");
@page = <IN>;
close(IN);

@userandemail=split(/&/, $string);
@from=split(/=/, $userandemail[0]);
@what=split(/=/, $userandemail[1]);
@id=split(/=/, $userandemail[2]);
#@numer_z_web=split(/=/, $userandemail[3]);
$id_small = lc $id[1];
$slowa= $slowa_dir . $id_small . "-hymn.gif";
############# DATABASE ##############

if ($from[1] eq "anthems") { $fil = "'" . $what[1] . "X.mp3" . "'" };
if ($from[1] eq "real") { $fil = "'" . $what[1] . "0.rm" . "'" };
if ($from[1] eq "media") { $fil = "'" . $what[1] . "0.asf" . "'" };
# my $dbh = DBI->connect("dbi:mysql:wfb","slawek","new3200");
my $dbh = DBI->connect("dbi:mysql:wfb","slawek");
my $rec=$dbh->prepare("SELECT access FROM download WHERE files = $fil");
$rec->execute;
my $rc = $rec->fetchrow_array;
$access = "This file was accessed " . $rc . " times.";
my $rec1=$dbh->prepare("UPDATE download SET access = access+1  WHERE files = $fil ");
$rec1->execute;
$idfromweb = "'" . $id[1] . "'";
my $rec2=$dbh->prepare("SELECT name FROM countries WHERE id = $idfromweb ");
$rec2->execute;
my $rc2 = $rec2->fetchrow_array;
$kraj = $rc2;
$flag = $flag_set_dir . $id_small . "-flag.gif";
$rec->finish();
$rec1->finish();
$rec2->finish();
$dbh->disconnect();

############# DATABASE END ##############

$folder_haslo = "duze haslo";
$current= gmtime(time);
($sec_g,$min_g,$hour_g,$mday_g,$mon_g,$year_g,$wday_g,$yday_g) = gmtime(time);
$year_g = $year_g + 1900;
$mon_g = $mon_g + 1;

$year_g =  sprintf("%.2d", $year_g );
$mon_g  = sprintf("%.2d", $mon_g   );

($sec_now,$min_now,$hour_now,$mday_now,$mon_now,$year_now,$wday_now,$yday_now) = localtime(time);
$year_now = $year_now + 1900;
$mon_now = $mon_now + 1;

$year_now =  sprintf("%.2d", $year_now );
$mon_now  = sprintf("%.2d", $mon_now   );


 ############# jesli stream #############

if ($from[1] ne "anthems") {
    &stream;  
};

################  Check ile razy ##################################
#### pool of IP #### 

$more_than3_flag = 0;
$file1_g= $year_g . $mon_g . $mday_g . ".txt";
$file_g= "/home/homepage/national-anthems.net/stat/" . $file1_g;
$addres=$ENV{REMOTE_ADDR};

#if ($addres ne "")  {
#      while (substr($addres, -1, 1) ne ".") {
#	$addres = substr($addres, 0, -1)
#	};
#};
#$addres= $addres . "*";

$a=1;

########## open file and check how many times it was accessed ##########

sysopen(INPUT, "$file_g", O_RDWR|O_CREAT) or die "can't open numfile: $!";
close(INPUT);
open(INPUT, "$file_g") || die "cant open $file: $!";
while (<INPUT>) {
    chomp or die $!;
    @wszystkie_ip=split(/&/, $_) or die $!;
    $test  = $wszystkie_ip[0];
    if ($test eq $addres) {$a = $a +1 };
};
close(INPUT);
$razy=$a;
if ($a >= 6) {
    $wynik="Sorry only 5 downloads per host in one day (24 hours) are permited.Current GMT time:" . $current;
    $more_than3_flag = 1;
};

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

if ($more_than3_flag == 0) {
    &mp3ok;
};

################ M P 3 OK ########################

sub mp3ok {
    $loc = localtime(time);
    sysopen(FH, $file_g, O_RDWR|O_APPEND|O_CREAT) or die "can't open $file:$!";
    flock(FH, LOCK_EX) or die "can't flock numfile: $!";
    (print FH $addres ,  "&", $razy,"&",$loc, "\n");
    close FH;
    $folder_data_now= $mday_now . $hour_now;
    $nazwa_duzego_folderu_now = md5_hex($folder_data_now,$folder_haslo);
    $folder_now = $folder_data_now . "_MP3_" . $nazwa_duzego_folderu_now;
    $inc_link = "/usr/FILE_store/" . $what[1] . "X.mp3";
    $out_link = $mp3_home . $folder_now . "/" .  $what[1] . "X.mp3";
    chdir $mp3_home or die $!;
    mkdir $folder_now;
    symlink("$inc_link","$out_link");
    chdir $mp3_home;
    
    $folder_files = $folder_data_now . "_MP3_" . $nazwa_duzego_folderu_now;
    $url="http://files.national-anthems.net/" . $folder_files . "/" .$what[1] . "X.mp3";
     $wynik1="<p style=\"text-align: center\"><font size=\"5\"><br>$kraj National Anthem MP3 Download <br>";
    $wynik2="<a href=$url>Instrumental version</a></p>";
    
   $download1="<p align=\"right\"><font size=\"3\"><a target=\"_blank\"href=\"http://www.national-anthems.net/how_to_download.html\">How to download...</a>";
    $donwload2="<i>(will open new window)</i></p>"; 
    
    $inf1="<p align=\"center\">Each time you visit download page, it counts as ONE download <br><font size=\"4\">";
    $inf2="This is your $razy visit/download today. GMT time is: $current</font></p>";
    
    $wynik = $wynik1 . $wynik2;
    $download = $download1 . $download2;
    $inf = $inf1 . $inf2;
    
    print "Content-type:text/html\n\n";
    print <<EndOfHTML;
    @page
	<p align="center"><img border="0" src=$flag>
	<p align="center" class="style3">$wynik</p>
	$download
	<p align="center" class="style2">$inf</p>
	<p align="center" class="style3">$access</p>
	<BR>
	<img border="0" src=$slowa>
	
EndOfHTML
;

    exit;
};

print "Content-type:text/html\n\n";
print <<EndOfHTML;
@page
<p align="center" class="style3">$wynik</p>
EndOfHTML
;
exit;

############################ S T R E A M #####################################################
sub stream {
    if ($from[1] eq "real")
    {
	$plik_data_now=  $mday_now . $hour_now . "_";
	$nazwa_pliku_now = md5_hex($what[1],$folder_haslo);
	$inc_link = "/usr/FILE_store/" . $what[1] . "0.rm";
	$plik = $plik_data_now . $nazwa_pliku_now . $what[1];
	$out_link = $real_home .  "/" . $plik . "0.rm";
	chdir $real_home;

#	$temp =  "<br>" .  $inc_link . "    " . $out_link ;
        symlink("$inc_link","$out_link");
     #    system("cp --no-preserve=timestamp  $inc_link $out_link ");
              

	$real0="<div align=\"center\"><embed src=\"rtsp://files.national-anthems.net:554/"  . $plik . "0.rm\" " . "width=\"280\" height=\"66\" autostart=\"true\" type=\"audio/x-pn-realaudio-plugin\"></div>";
	$real= $real0;
   
 };
    
# --------END REAL --MEDIA ------------------------------
    
    if ($from[1] eq "media")
    {
	$plik_data_now=  $mday_now . $hour_now . "_";
	$nazwa_pliku_now = md5_hex($what[1] , $folder_haslo);
	$plik = $plik_data_now . $nazwa_pliku_now . $what[1];
	$inc_link = "/usr/FILE_store/" . $what[1] . "0.asf";
	$out_link = $media_home .  "/" .  $plik . "0.asf";
        symlink("$inc_link","$out_link");
	$media0="<div align=\"center\"><embed src=\"mms://files.national-anthems.net/windows/" . $plik . "0.asf\" " . "width=\"250\" height=\"90\" autostart=\"true\" type=\"video/x-ms-asf-plugin\"></div>";
	$media1 =  $out_link;
        $media = $media0;
    };
    
#-----------koniec stream --------------------------------- ---------------
    
    $flag_url="<div align=\"center\"><img border=\"0\" src=$flag </div>";
    $slowa_url="<div align=\"center\"><img border=\"0\" src=$slowa </div>";
    
print "Content-type:text/html\n\n";
    print <<EndOfHTML;

    <br>
	@page
	
	$flag_url
	$media
	$real
	$access
	<BR>
	$slowa_url
	</body>

EndOfHTML
;
exit;
};



