#!/usr/bin/perl

#<ENC>48</ENC>

package glsitemap;

########################################################################
# 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 acs;

require Exporter;
use vars qw/@ISA @EXPORT @EXPORT_OK/;

@ISA = qw(Exporter);

@EXPORT = qw(create_gsitemap);

@EXPORT_OK = qw();

&get_setup;



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

sub create_gsitemap
{

$sitemap_path = acs::get_file_contents("$data_dir/sitemap.dat"); 

$gsettings = new_gsettings acs; 

if ($mysql_hostname eq ""){$dsn = "DBI:mysql:$db_name";}else{$dsn = "DBI:mysql:$db_name:$mysql_hostname:$mysql_port";}
$dbh = DBI->connect($dsn, $db_username, $db_password);
if ( !defined $dbh ) {die "Cannot connect to MySQL server: $DBI::errstr\n"; }

####

$wsql = "UPDATE links SET newsubm = '' WHERE newsubm IS NULL";
$sth = $dbh->prepare($wsql);
$sth->execute;
$serror = ""; $serror = $sth->errstr; if ($serror ne "") {die "SQL Syntax Error: $wsql";}

#### 

$allurls = 0;

#### DO CATEGORIES

open (GSM, "> $data_dir/gsitemaptemp.tmp");
open (GSM2, "> $data_dir/gsitemap.dat");



$dbh2 = DBI->connect($dsn, $db_username, $db_password);
if ( !defined $dbh2 ) {die "Cannot connect to MySQL server: $DBI::errstr\n"; }

$wsql = "SELECT * FROM dirs WHERE ((crosslinkid IS NULL) OR (crosslinkid < 1)) AND (catlock != 'H') AND ((ext3 != '') AND (ext3 IS NOT NULL) AND (ext3 != '::')) ORDER BY ext3 DESC ";
$sth = $dbh->prepare($wsql);
$sth->execute;
$serror = ""; $serror = $sth->errstr; if ($serror ne "") {die "SQL Syntax Error: $wsql";}
while ( @row = $sth->fetchrow() ) 
	{
	
			$lv = $row[1];
			$cats = "";
			$lvc = 0;
			$enc_cats = ""; $dyn_enc_cats = ""; $modrw_enc_cats = "";
	
			for ($ms = 2; $ms < 18; $ms++) 
					{
					if ($row[$ms] ne "")
						{
	
							if ($lvc == 0) 
								{
								$enc_cats = $enc_cats . acs::encode_dir($row[$ms]);
								$dyn_enc_cats = $dyn_enc_cats . acs::encode_dir($row[$ms]);
								$modrw_enc_cats = $modrw_enc_cats . $row[$ms];
								}
								else
								{
								$enc_cats = $enc_cats . "/" . acs::encode_dir($row[$ms]); 
								$dyn_enc_cats = $dyn_enc_cats . "-" . acs::encode_dir($row[$ms]); 
								$modrw_enc_cats = $modrw_enc_cats . "-" . $row[$ms];
								}

							$lvc++;
						}
						
				}
			chop($cats); chop($cats);
			
			
			#### Static / Dynamic / Mod Rewrite
				if ($gsettings->{dyn_stat} eq "Static")
					{
					$indexfile = $web_url . "/" . $enc_cats . "/" . $gsettings->{gen_default_index};
					}
				elsif ($gsettings->{dyn_stat} eq "Dynamic")
					{
					$indexfile = "$script_url/dirs.cgi?" . "lv=$lvc" . "&ct=" . $dyn_enc_cats;
					$indexfile = &xencode($indexfile);
					}
				elsif ($gsettings->{dyn_stat} eq "Mod_Rewrite")
					{
					$modrw_enc_cats = &convert_cat($modrw_enc_cats);
					$indexfile = "$web_url/$row[0]" . "-" . $modrw_enc_cats . ".html";
					$curcid = $row[0];
					}
			
			@goptions = split (/::/, $row[26]);
			$gcfreq= ""; if ($goptions[1] ne "") { $gcfreq = ":::  <changefreq>" . $goptions[1] . "</changefreq>"; }
							
			$gmprio = ""; if ($goptions[0] ne "") { $gmprio = ":::  <priority>" . $goptions[0] . "</priority>"; }
			
			
			### WRITE MAIN CATEGORY ENTRY
			
			print GSM ":::<url>:::" . "  <loc>$indexfile</loc>" . $gcfreq . $gmprio . ":::</url>\n";
			$allurls++;
			
			print GSM2 "<b><a href=\"$indexfile\" target=\"_blank\">$indexfile</a></b> ( $gcfreq $gmprio ) \n<BR>";
			

			### CHECK IF THERE ARE MORE THAN ONE PAGE AVAILABLE
			#$sth2 = $dbh2->prepare("SELECT COUNT(*) FROM links WHERE (cid = '$row[0]') AND (newsubm != 'Y')");
			
			$sth2 = $dbh2->prepare("SELECT  COUNT(*) FROM dirs AS A, links AS B WHERE (A.cid = B.cid) AND (B.cid = '$row[0]') AND (B.newsubm != 'Y')");
			$sth2->execute;
			$serror = ""; $serror = $sth2->errstr; if ($serror ne "") {die "SQL Syntax Error: $wsql";}
			while ( @row2 = $sth2->fetchrow() ) { $cnr = $row2[0]; }
			
				$extra_pages_entries = "";
				
				if ($cnr > $gsettings->{nrlinks_per_page})
					{

						$pages = "";
						$modp = ($cnr % ($gsettings->{nrlinks_per_page}));
						$pages = ($cnr - $modp) / ($gsettings->{nrlinks_per_page});
						$pages++;

						#print "$indexfile - $row[26] - $cnr !!! PAGES: $pages<BR>"; ## MAIN PAGE

						for ($pms = 2; $pms < ($pages+1); $pms++) 
							 {

								if (($gsettings->{category_page_names} eq "categorynames") and ($gsettings->{dyn_stat} eq "Static")) ############ STATIC
									{
										$dir_name = "";
										$tmp_name = lc($row[28]);
										$tmp_name =~ s/ /_/gi;

										for ($ms = 0; $ms < length($tmp_name); $ms++) 
										{
											$oneletter = substr($tmp_name, $ms, 1);
											if (($oneletter =~ /[0-9a-zA-Z]/) or ($oneletter eq "_"))
												{
												$dir_name = $dir_name . $oneletter;
												}
										}

										$dir_name = $dir_name . $pms . $gsettings->{gen_file_ext};
										$spath_dir_name = $web_dir . "/" . $enc_cats . "/" . $dir_name;
										$dir_name = $web_url . "/" . $enc_cats . "/" . $dir_name;

										if ((-e "$spath_dir_name") > 0)	
												{
												print GSM ":::<url>:::" . "  <loc>$dir_name</loc>" . $gcfreq . $gmprio . ":::</url>\n";
												print GSM2 "&nbsp;&nbsp;&nbsp;<a href=\"$dir_name\" target=\"_blank\">$dir_name</a></b> ( $gcfreq $gmprio ) <BR>\n";
												$allurls++;
												}
									
									}
									elsif (($gsettings->{category_page_names} ne "categorynames") and ($gsettings->{dyn_stat} eq "Static")) ############ STATIC
									{
											$dir_name = $web_url . "/" . $enc_cats . "/" . "more" . $pms . $gsettings->{gen_file_ext};
											$spath_dir_name = $web_dir . "/" . $enc_cats . "/" . "more" . $pms . $gsettings->{gen_file_ext};
											if ((-e "$spath_dir_name") > 0)	
													{
													print GSM ":::<url>:::" . "  <loc>$dir_name</loc>" . $gcfreq . $gmprio . ":::</url>\n";
													print GSM2 "&nbsp;&nbsp;&nbsp;<a href=\"$dir_name\" target=\"_blank\">$dir_name</a></b> ( $gcfreq $gmprio ) <BR>\n";
													$allurls++;
													}
												
									}  
									elsif ($gsettings->{dyn_stat} eq "Dynamic") ################ DYNAMIC
									{	
										$st = ($pms - 1) * $gsettings->{nrlinks_per_page};
										$nd = $st + $gsettings->{nrlinks_per_page};
											if ($st > 0) 
											{
												$dir_name = "$script_url/dirs.cgi?st=$st&nd=$nd" . "&ct=" . $dyn_enc_cats . "&lv=$lvc";
												$dir_name = &xencode($dir_name);
												print GSM ":::<url>:::" . "  <loc>$dir_name</loc>" . $gcfreq . $gmprio . ":::</url>\n";
												print GSM2 "&nbsp;&nbsp;&nbsp;<a href=\"$dir_name\" target=\"_blank\">$dir_name</a></b> ( $gcfreq $gmprio ) <BR>\n";
												$allurls++;
											}
									}
									elsif ($gsettings->{dyn_stat} eq "Mod_Rewrite") ################ MOD REWRITE
									{	
										$st = ($pms - 1) * $gsettings->{nrlinks_per_page};
										$nd = $st + $gsettings->{nrlinks_per_page};
											if (($st > 0) and ($st < $cnr))  
											{
												$dir_name = "$web_url/$curcid/$st/$nd" . "-" . $modrw_enc_cats . ".html";
												print GSM ":::<url>:::" . "  <loc>$dir_name</loc>" . $gcfreq . $gmprio . ":::</url>\n";
												print GSM2 "&nbsp;&nbsp;&nbsp;<a href=\"$dir_name\" target=\"_blank\">$dir_name</a></b> ( $gcfreq $gmprio ) <BR>\n";
												$allurls++;
											}
									}

									
									
							  } ## for ($pms = 2; $pms < ($pages+1); $pms++) 

				}

			
	}


### DO LINKS

$wsql = "SELECT A.*, B.* FROM dirs AS A, links AS B WHERE (A.cid = B.cid) AND ((B.extf4 != '') AND (B.extf4 IS NOT NULL) AND (B.extf4 != '::')) AND ((B.url != '') AND (B.url IS NOT NULL)) ORDER BY B.extf4 DESC";
$sth = $dbh->prepare($wsql);
$sth->execute;
$serror = ""; $serror = $sth->errstr; if ($serror ne "") {die "SQL Syntax Error: $wsql";}
$rows = $sth->rows();

while ( @row = $sth->fetchrow() ) 
	{
			$lv = $row[1];
			$cats = "";
			$lvc = 0;
			$enc_cats = "";
	
			for ($ms = 2; $ms < 18; $ms++) 
					{
					if ($row[$ms] ne "")
						{
	
							if ($lvc == 0) { $enc_cats = $enc_cats . acs::encode_dir($row[$ms]); }
												else
												{ $enc_cats = $enc_cats . "/" . acs::encode_dir($row[$ms]); }
												
							$lvc++;
						}
						
				}
				
			chop($cats); chop($cats);


			######## STATIC
			if ($gsettings->{dyn_stat} eq "Static") ### GET MORE INFO FILENAME
			{
					$more_info_pg_name = "";

					if ($gsettings->{more_info_naming} eq "linktitles") 
						{
							$title_without_link_tmp = $row[37];

									$more_info_pg_name = ""; $title_without_link_tmp =~ s/ /_/gi;
									for ($ms = 0; $ms < length($title_without_link_tmp); $ms++) 
										{
										$oneletter1 = substr($title_without_link_tmp, $ms, 1);
											if (($oneletter1 =~ /[0-9a-zA-Z]/) or ($oneletter1 eq "_"))
											{
											$more_info_pg_name = $more_info_pg_name . $oneletter1;
											}
										}
									$more_info_pg_name = lc($more_info_pg_name);
									if (length($more_info_pg_name) > 80) { $more_info_pg_name = substr($more_info_pg_name, 0, 80); }
									$more_info_pg_name = $more_info_pg_name . "_" . $row[35]; 

						}
						else
						{
						$more_info_pg_name = $row[35]; 
						}

						###########

						$more_info_pg_name = $more_info_pg_name . $gsettings->{gen_file_ext};

						@goptions = split (/::/, $row[61]);

						$gurl = "$web_url/$enc_cats/$more_info_pg_name";
						$gpath = "$web_dir/$enc_cats/$more_info_pg_name";

						$gcfreq= "";
						if ($goptions[1] ne "") { $gcfreq = ":::  <changefreq>" . $goptions[1] . "</changefreq>"; }

						$gmprio = "";
						if ($goptions[0] ne "") { $gmprio = ":::  <priority>" . $goptions[0] . "</priority>"; }

						if ((-e "$gpath") > 0)	
							{
							print GSM ":::<url>:::" . "  <loc>$gurl</loc>" . $gcfreq . $gmprio . ":::</url>\n";
							print GSM2 "<a href=\"$gurl\" target=\"_blank\">$gurl</a></b> ( $gcfreq $gmprio ) <BR>\n";
							$allurls++;
							}
			}
			######### DYNAMIC
			elsif ($gsettings->{dyn_stat} eq "Dynamic")
			{
					@goptions = split (/::/, $row[61]);
					
					$gcfreq= "";
					if ($goptions[1] ne "") { $gcfreq = ":::  <changefreq>" . $goptions[1] . "</changefreq>"; }

					$gmprio = "";
					if ($goptions[0] ne "") { $gmprio = ":::  <priority>" . $goptions[0] . "</priority>"; }

					print GSM ":::<url>:::" . "  <loc>$script_url/linfo.cgi?id=$row[35]</loc>" . $gcfreq . $gmprio . ":::</url>\n";
					print GSM2 "<a href=\"$gurl\" target=\"_blank\">$script_url/linfo.cgi?id=$row[35]</a></b> ( $gcfreq $gmprio ) <BR>\n";
					$allurls++;
			}
			elsif ($gsettings->{dyn_stat} eq "Mod_Rewrite") ################ MOD REWRITE
			{
					@goptions = split (/::/, $row[61]);

					$gcfreq= "";
					if ($goptions[1] ne "") { $gcfreq = ":::  <changefreq>" . $goptions[1] . "</changefreq>"; }

					$gmprio = "";
					if ($goptions[0] ne "") { $gmprio = ":::  <priority>" . $goptions[0] . "</priority>"; }
					
					$titlename = &convert_cat($row[37]);
					
					print GSM ":::<url>:::" . "  <loc>$web_url/info/$row[35]-$titlename.html</loc>" . $gcfreq . $gmprio . ":::</url>\n";
					print GSM2 "<a href=\"$gurl\" target=\"_blank\">$web_url/info/$row[35]-$titlename.html</a></b> ( $gcfreq $gmprio ) <BR>\n";
					$allurls++;
					
			}
			
			
	}


close (GSM);
close (GSM2);

$sth->finish;
$dbh->disconnect; 


##########################################################
##### Get sitemap path

@diritems = split (/\//, $sitemap_path);
$nritems = @diritems;
for ($ms = 0; $ms < ($nritems-1); $ms++) 
	{
	$dirwrite = $dirwrite . $diritems[$ms] . "/";
	}
chop ($dirwrite);

#### Get sitemap filename

$nritems = $nritems - 1;
$sitemapfilename = $diritems[$nritems];

($fname, $fext) = split (/\./, $sitemapfilename);

#print "PATH: $dirwrite<br>";
#print "PATH: $sitemapfilename<br>";
#print "FNAME: $fname<BR>";
#print "EXT: $fext<BR>";

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

$maxurls = acs::get_file_contents("$data_dir/sitemapmax.dat"); 
if ($maxurls eq "") { $maxurls  = 40000; }

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

$preapp = acs::get_file_contents("$data_dir/sitemappre.dat"); 

$preappended_urls_first = qq[<?xml version='1.0' encoding='UTF-8'?>
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
         xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
$preapp
];

$preappended2 = qq[<?xml version='1.0' encoding='UTF-8'?>
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
         xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
];

$app = acs::get_file_contents("$data_dir/sitemapapp.dat"); 
$appended_urls_first = "\n$app\n</urlset>";

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


$linecounter = 0; $file_counter = 1;

$fileswritten[0] = $sitemap_path;

open (SMPF, "$data_dir/gsitemaptemp.tmp");
	while (defined($line=<SMPF>))
		{
		$linecounter++;
		$line =~ s/:::/\n/gi;
		
			if ($linecounter == 1)
				{
				$isfile = "true";
				
					if ($file_counter > 1) ### more than 1 sitemap file
						{
							$sitemapwrite = $dirwrite . "/" . $fname . $file_counter . "." . $fext;
							if ($file_counter == 2) { print FILEW $appended_urls_first; } else { print FILEW "\n</urlset>"; }
							close (FILEW);
							$fstart = $preappended2;
							push(@fileswritten, $sitemapwrite);
						}
						else
						{
							$sitemapwrite = $sitemap_path;
							$fstart = $preappended_urls_first;
							$fend = $appended_urls_first;
						}
				
					open (FILEW, "> $sitemapwrite");
					print FILEW $fstart . "\n";
					print FILEW $line . "\n";
					$file_counter++;
				}
				else 
				{
					print FILEW $line . "\n";
					if ($linecounter == $maxurls) { $linecounter = 0; }
				}
		
		}

close (SMPF);


if ($isfile eq "true")
	{
	print FILEW "\n</urlset>";
	close (FILEW);
	}

$fileswreturn = "";
foreach $fitem (@fileswritten)
	{
	$fileswreturn = $fileswreturn . $fitem . "::";
	}

return ($allurls, $fileswreturn);

}





sub convert_cat
{
my ($cattstring) = @_;

if (substr($cattstring, 0, 1) eq " ")
	{
	$cattstring = substr($cattstring, 1, length($cattstring) - 1);
	}

$cattstring =~ s/ /-/gi;
$cattstring =~ s/\$//gi;
$cattstring =~ s/\&//gi;
$cattstring =~ s/\+//gi;
$cattstring =~ s/\,//gi;
$cattstring =~ s/\///gi;
$cattstring =~ s/\\//gi;
$cattstring =~ s/://gi;
$cattstring =~ s/\;//gi;
$cattstring =~ s/\=//gi;
$cattstring =~ s/\?//gi;
$cattstring =~ s/\@//gi;

$cattstring =~ s/>//gi;
$cattstring =~ s/>//gi;
$cattstring =~ s/\#//gi;
$cattstring =~ s/\%//gi;
$cattstring =~ s/\{//gi;
$cattstring =~ s/\}//gi;
$cattstring =~ s/\|//gi;
$cattstring =~ s/\^//gi;
$cattstring =~ s/\~//gi;
$cattstring =~ s/\[//gi;
$cattstring =~ s/\]//gi;
$cattstring =~ s/\`//gi;
$cattstring =~ s/_/-/gi;

$cattstring =~ s/-------/-/gi;
$cattstring =~ s/------/-/gi;
$cattstring =~ s/-----/-/gi;
$cattstring =~ s/----/-/gi;
$cattstring =~ s/---/-/gi;
$cattstring =~ s/--/-/gi;

if ($cattstring > 200) { $cattstring = substr($cattstring, 0, 240); }

return ($cattstring);
}



sub xencode
{
($uenc) = @_;

$uenc =~ s/\&/\&amp;/g;
$uret = $uenc;

return ($uret);
}





sub get_setup
{

if ((-e "config.cgi") and (length($config_cgi) < 15))
	{
	$cofile = "config.cgi"; $exists = 1;
	}
	elsif (-e "$config_cgi") 
	{
	$cofile = "$config_cgi"; $exists = 1;
	}
	else
	{
	print "Content-type: text/html\n\n"; 
	print "Could not find config.cgi"; exit;
	}

#$exists = (-e "config.cgi");

if ($exists > 0)
	{
	open (STP, $cofile);
		while (defined($line=<STP>))
			{
			if ($line =~ m/#/g)
				{
				$r = pos($line);
				$line = substr($line, 0, $r - 1);
				}
				
				$line =~ s/\n//g;
	
if ($line =~ /^DB_NAME/){$db_name = &get_setup_line2($line, DB_NAME);}
if ($line =~ /^DB_USERNAME/){$db_username = &get_setup_line2($line, DB_USERNAME);}
if ($line =~ /^DB_PASSWORD/){$db_password = &get_setup_line2($line, DB_PASSWORD);}
if ($line =~ /^MYSQL_HOSTNAME/){$mysql_hostname = &get_setup_line2($line, MYSQL_HOSTNAME);}
if ($line =~ /^MYSQL_PORT/){$mysql_port = &get_setup_line2($line, MYSQL_PORT);}

if ($line =~ /^SCRIPT_URL/){$script_url = &get_setup_line2($line, SCRIPT_URL);}
if ($line =~ /^ADMIN_URL/){$admin_url = &get_setup_line2($line, ADMIN_URL);}
if ($line =~ /^WEB_URL/){$web_url = &get_setup_line2($line, WEB_URL);}
if ($line =~ /^WEB_DIR/){$web_dir = &get_setup_line2($line, WEB_DIR);}
if ($line =~ /^DATA_DIR/){$data_dir = &get_setup_line2($line, DATA_DIR);}

if ($line =~ /^USERNAME/){$username = &get_setup_line2($line, USERNAME);}
if ($line =~ /^PASSWORD/){$password = &get_setup_line2($line, PASSWORD);}

if ($line =~ /^MAIL_METHOD/){$mail_method = &get_setup_line2($line, MAIL_METHOD);}
if ($line =~ /^SENDMAIL/){$sendmail = &get_setup_line2($line, SENDMAIL);}
if ($line =~ /^SMTP_SERVER/){$smtp_server = &get_setup_line2($line, SMTP_SERVER);}


			}
	close (STP);
	
	}
}





sub get_setup_line2
{
my ($setup_line, $setup_var) = @_;
$crit = "\"";
$setup_line =~ m/$crit/g;
$r1 = pos($setup_line);
$setup_line =~ m/$crit/g;
$r2 = pos($setup_line);
$setup_line = substr($setup_line, $r1, ($r2 - $r1 - 1));
$return_val = $setup_line;
return ($return_val);
}


1;