#!/usr/bin/perl -w

# perlhoo.pl - builds a Yahoo like Web directory
# by Jonathan Eisenzopf. v1.1 19990319
# Copyright (c) 1999 internet.com LLC. All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Originally published and documented at http://www.webreference.com
# Contact eisen@internet.com for all other uses.

# Modules
use strict;
use Text::CSV_XS;
use CGI;
use CGI::Carp qw(fatalsToBrowser);

# Constants
my $datafile     = 'links.csv';
my $new_datafile = 'links_new.csv';
my $rootdir      = '/home/allfunny/public_html/links';
my $baseurl      = '/links/links.pl';
my $adminEmail	 = 'support@allfunnytshirts.com';
my $Mailer 		 = '/usr/sbin/exim -oi -t';

# Main
my $query = new CGI;
print $query->header;

my $reldir = $query->path_info;
$reldir =~ s/^\/+//;
$reldir =~ s/\/+$//;
 
if ($query->param('keywords') eq 'add') {
    if ($query->request_method eq 'GET') {
	&print_add_screen($reldir);
    } elsif ($query->request_method eq 'POST') {
	&add_link($reldir);
    }
} else {
    &print_hoo($reldir);
}

# Subroutines
sub add_link {
    my $reldir = shift;
    my $dir = "$rootdir/$reldir";
    $dir =~ s/\/+$//;

    open(FILE, ">> $dir/$new_datafile") || &error("Cannot open $dir/$new_datafile for write: $!");
    flock(FILE, 2) || &error("Cannot get exclusive lock for $dir/$new_datafile: $!");

    my $csv = Text::CSV_XS->new();
    print FILE $csv->string,"\n" 
	if $csv->combine($query->param('url'),$query->param('title'),$query->param('description'),$query->param('backurl'),$query->param('email'));
    close(FILE);

    &print_header($reldir);

    print <<HTML;
Thank you for your suggested addition to the AllFunnyTshirts <b>$reldir</b> link category. 
While we do review all new site suggestions, we cannot guarantee that all 
submissions will be added. The following information will be sent to the 
directory editor:
<p>
<table border="0" cellpadding="2" cellspacing="2">
HTML

    print "<tr><td align=\"right\"><B>URL:</B></td><td>",$query->param('url'),"</td></tr>\n";
    print "<tr><td align=\"right\"><B>Title:</B></td><td>",$query->param('title'),"</td></tr>\n";
    print "<tr><td align=\"right\"><B>Description:</B></td><td>",$query->param('description'),"</td></tr>\n";
    print "<tr><td align=\"right\"><B>Reciprocal Link:</B></td><td>",$query->param('backurl'),"</td></tr>\n";
    print "<tr><td align=\"right\"><B>Your Email:</B></td><td>",$query->param('email'),"</td></tr></table></p>\n";

    print <<HTML;
<p>Return to <a href="$baseurl/$reldir">$reldir</a>
<hr noshade>
HTML

    &print_footer($reldir);

	open MAIL,"|$Mailer";

   	print MAIL "To: $adminEmail\n";
   	print MAIL "From: ",$query->param('email'),"\n";
   	print MAIL "Subject: Link exchange offer\n";

   	print MAIL "URL         > ",$query->param('url'),"\n";
   	print MAIL "BackURL     > ",$query->param('backurl'),"\n";
   	print MAIL "Title       > ",$query->param('title'),"\n";
   	print MAIL "Description > ",$query->param('description'),"\n";
	
   	close MAIL;

}

sub error {
    my $msg = shift;
    print "<FONT color=\"#FF0000\"><B>$msg</B></FONT>\n";
    die "$msg";
}

sub print_add_screen {
    my $reldir = shift;
	
	&print_header($reldir);
		
    print <<HTML;
<p><H3>Add a link to: $reldir</H3><HR noshade>
<h4>Link Exchange Policy</h4>
        <p>Link exchange should be mutually beneficial to both parties. We have far too often encountered situations in which webmasters requesting link exchange have a site not meeting our link exchange requirement or have not followed proper link exchange etiquette, thereby costing us considerable wasted time and effort. We have therefore established this link exchange policy to specify what kinds of Web site we can accept and our link quality requirement when you put our link on your site
        <p>We don't create link just for link or search engine spider. Instead, we regard link as important content to recommend to our visitors. At All Funny T-shirts, we are proud of our Web site and constantly strive to improve the quality. Therefore, we are quite selective with respect to Web sites we accept for our  link exchange partnership. We constantly seek quality sites as reciprocal link exchange partners to benefit our visitors. 
        <p>Before applying our link exchange partnership, please read and comply with the following partnership qualification and link quality requirements.</p>
        <p><strong>Link quality requirement when you put our link on your site </strong></p>
        <ul>
          <li>You should put our link  on a page with suitable subject/theme (shopping/clothing/fun etc.). If there is no suitable topic link page on your site, you should put our link on the homepage of your site. </li>
          <li>You should use the link info(title, URL and description) or link code we provided on our site when you put our link;</li>
          <li>You should put a direct link to our site. We do not permit indirect link under any circumstances;</li>
          <li>You should put our link on an unframed HTML (HyperText Markup Language) or SSI (Server Side Includes) formatted page. We do not permit framing of our pages under any circumstances;</li>
          <li>You should put our link on the page directly on your site. We do not permit our link on a third-party site/different domain under any circumstances;</li>
          <li>You should not use meta tags or robots.txt to prevent search engine indexing the page on which you put our link;</li>
          <li>The page on which you put our link must contain less than 40 outbound links;</li>
          <li>The page on which you put our link must be reachable from the homepage of your site.</li>
        </ul>
        <p><strong>Link Partner Qualification</strong>
        <p>We will only exchange link with sites meeting all the following requirement:       
        <p>General requirement:
        <ul>
          <li>Your site must be live and not under construction;</li>
          <li>Your site must be in the English language (or have an English version available); </li>
          <li>Your site must have its own domain name(Exceptions to this rule may be accepted if your website is of very high quality);</li>
		  <li>Submit your site to the single most relevant category;</li>
		  <li>Do not submit sites' URLs which contain nothing more than affiliate links;</li>
		  <li>Do not submit duplicated sites;</li>
		  <li>Do not submit pages which redirect the user to another pages;</li>
		  <li>Do not include prices or offers.</li>
        </ul>
        <p><strong>We DO NOT accept the following Web sites as link exchange partners under any circumstances: </strong></p>
        <ul>
          <li>Web sites not written in English;
          <li>Web sites punished by <a href="http://www.google.com" target="_blank">Google</a>(Web site being removed entirely from the Google index);
          <li>Web sites have automated Free-For-All links(FFA) page;
          <li>Adult entertainment websites;
          <li>Hate or warez websites;
          <li>Gambling websites;
          <li>Web sites that knowingly attempt to download browser add-ons, pop-up generators or active spyware without explicit visitor consent;
          <li>Websites that knowingly attempt to download any computer security threat such as active spyware, viruses or worms;
          <li>Any non-themed website such as link farms and content-poor advertising sites, including sites whose primary content is third-party advertising;
          <li>Websites with undesirable behaviors such as excessive pop-ups, irritating graphics such as rapidly flashing banner ads, or browser hijacking.<br>
        </ul>
        <p><strong>Our Rights</strong> </p>
        <ul>
          <li>We reserve the right to deny your link, especially if it determines that a Web site does not meet the criteria or disobey our link quality requirement. </li>
          <li> We may edit links which are too wordy or use too much marketing language in their descriptions or title. </li>
          <li>We may at any time revise our link exchange policy. You are bound by any such revisions and should therefore periodically visit this page to review the current version of the policy. </li>
        </ul>
        <p>If you have any comments, questions or requests please <a href="/funnyshop/contact.php" target="_blank">contact us</a>.</p>
        <p><strong>Please use following information code to link to us</strong> </p>
		<b>Title:</b> All Funny T-shirts<br>
		<b>Description:</b> Funny t-shirts that will make you laugh, cool slogan t-shirts, and not so funny offensive t-shirts.<br>
		<b>URL:</b> http://www.allfunnytshirts.com
		<p>Or use the following HTML code:</p>
		<form>
		<textarea name="code" rows="4" cols="60"><a href="http://www.allfunnytshirts.com" target="_blank">All Funny T-shirts</a> - funny t-shirts that will make you laugh, cool slogan t-shirts, and not so funny offensive t-shirts.</textarea>
		</form>
        <p><strong>Provide information of your site:</strong> </p>
		<form action="$baseurl/$reldir" method="POST">
<input type="hidden" name="keywords" value="add">
<table border="0" cellpadding="2" cellspacing="2">
<tr><td align="right"><B>URL:</B></td><td><input name="url" size="40"></td></tr>
<tr><td align="right"><B>Title:</B></td><td><input name="title" size="40"></td></tr>
<tr><td align="right"><B>Description:</B></td><td><textarea name="description" rows="3" cols="40"></textarea></td></tr>
<tr><td align="right"><B>Reciprocal Link<br>(where you placed link to us):</B></td><td><input name="backurl" size="40"></td></tr>
<tr><td align="right"><B>Your Email:</B></td><td><input name="email" size="40"></td></tr>
<tr><td></td><td><input type="submit" value=" Submit Resource ">
<input type="reset" value=" Reset "></td></tr>
</table>
<HR noshade>
</form></p>
HTML

    &print_footer($reldir);
}

sub print_categories {
    my $reldir = shift;
    my $dir = "$rootdir/$reldir";
    $dir =~ s/\/+$//;

    opendir DIR,$dir ||  &error("Cannot open $dir: $!");
    my @dirs = sort(grep -d, map "$dir/$_", grep !/^\./, readdir DIR);
    closedir DIR;
	
	my $cats = 0;
    
    foreach my $thisdir (@dirs) {
	$thisdir =~ s/$rootdir\/$reldir//;
	$thisdir =~ s/\/+$//g;
	$thisdir =~ s/^\/+//g;

	$cats = 1;
	
	my $url;
	if ($reldir =~ /\S+/) {
	    $url = "$baseurl/$reldir/$thisdir";
	} else {
	    $url = "$baseurl/$thisdir";
	}

	my $pdir = $thisdir;
	$pdir =~ s/_/ /g;
	print "<li><a href=\"$url\"><B>$pdir</B></a></li>\n";
    }
    if ($cats != 0) {
		print "<HR noshade>\n";
	}
}

sub print_header {
    my $reldir = shift;
    my @parts = split(/\//,$reldir);
    
    print <<HTML;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<!-- Funny t-shirts collection. Funny t-shirts, funny babydolls, funny slogan t-shirts. -->
<!-- Not very funny t-shirts, funny offensive t-shirts, funny pictures on t-shirts-->
<title>Funny links at AllFunnyTshirts.com - $reldir</title>
<meta name="Description" content="Funny links : Funny t-shirts at AllFunnyTshirts.com - $reldir">
<meta name="Keywords" content="$reldir, links, funny links, funny t-shirts, funny shirts, funny t shirts, funny tshirts, funny-tshirts, funny-t-shirts, cool t-shirts, cool shirts, cool t shirts, cool tshirts, cool-tshirts, cool-t-shirts, funny, humorous, cool, offensive, tshirt, t-shirt, tshirts, t-shirts, shirts, shirts tee, tee shirt, tee shirts">
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<LINK REL="stylesheet" type="text/css" href="/allfunny.css">
</head>

<body>
<TABLE class=title>
<TR>
<td align=left valign=middle>
<a href="http://www.allfunnytshirts.com" title="Funny t-shirts and cool t-shirts at AllFunnyTshirts.com"><img src="/graphics/Logo.gif" alt="Funny t-shirts, cool t-shirts"  WIDTH=264 height=100 border=0></a>
</td>
<td align=center valign=middle>
<h1 class=title>Cool funny t-shirts that will make you laugh.<br>Cool funny slogans on t-shirts, not so funny offensive t-shirts.<br>Funny links.</h1>
<!-- <h5>Cool funny t-shirts. Vintage ringer funny t-shirts, muscle funny t-shirts and girls babydoll funny t-shirts.</h5> -->

<!-- <h5>Offensive t-shirts, cool slogans on funny t-shirts.</h5>-->
</td>
</TR>
</TABLE>

<TABLE class=topmenu border=1>
<tr>
<td align=left nowrap>
<div class=topmenu>
&nbsp; <a class=topmenu href="http://www.allfunnytshirts.com"  title="All Funny Tshirts Home page">Home</a>&nbsp;|&nbsp;<a class=topmenu href="/funnyshop/index.php" title="Our cool and funny online shop">Shop</a>&nbsp;|&nbsp;<a class=topmenu href="/faq.html" title="Our cool and funny web site F.A.Q.">F. A. Q.</a>&nbsp;|&nbsp;<a class=topmenu href="/links/links.pl" title="Funny links collection">Links</a>&nbsp;|
</div>
</td>
<td align=right nowrap width=5%>
<div class=topmenu>
&nbsp;<a class=topmenu href="/funnyshop/contact.php" title="Contact AllFunnyTshirts.com">Contact us</a>&nbsp;|&nbsp;<a class=topmenu href="/sitemap.html" title="Our cool site sitemap">Site map</a>&nbsp;
</div>
</td>
</tr>
</TABLE>

<table class=main BORDER = 0 width=83% CELLPADDING = 0 CELLSPACING = 0>
<col width=17%><col width=83%>
<tr valign=top>
<td bgcolor=#FF9966 width=17%><img src="/graphics/funny.gif" alt="Cool funny t-shirts" border=0></td>
<td class=maintab rowspan=2 width=83%>
<br>

HTML

    print "<p><H3><a href=\"$baseurl\">Top</a>";
    for (my $i=0; $i < @parts; $i++) {
	if ($i == (@parts - 1)) {
	    my $title = $parts[$i];
	    $title =~ s/_/ /g;
	    print ": $parts[$i]";
	} else {
	    print ": <a href=\"$baseurl/";
	    print join('/',@parts[0..$i]);
	    print "\">$parts[$i]</a>";
	}
    }
    print "</H3></p><HR noshade>\n";
}

sub print_footer {
    my $reldir = shift;
	
	if ($reldir ne '') {
		print "<div align=\"right\"><a href=\"$baseurl/$reldir?add\">Suggest new link</a></div>";
	}
	print <<HTML;

</td>
</tr>
<tr valign=top>
<td class=menutab>
<h3 class=leftmenu>funny collections</h3><br>
	<img src="/graphics/funnytshirts.gif" width="12" height="9" alt="Funny t-shirts"><a class=leftmenu href="/funnytshirts.html" title="Funny t-shirts">Funny t-shirts</a><br>
	<img src="/graphics/funnytshirts.gif" width="12" height="9" alt="Cool funny t-shirts"><a class=leftmenu href="/cooltshirts.html" title="Cool t-shirts">Cool t-shirts</a><br>
	<img src="/graphics/funnytshirts.gif" width="12" height="9" alt="Funny slogan t-shirts"><a class=leftmenu href="/slogantshirts.html">Slogan t-shirts</a><br>
	<img src="/graphics/funnytshirts.gif" width="12" height="9" alt="Nasty t-shirts"><a class=leftmenu href="/nastytshirts.html" title="Nasty t-shirts">Nasty t-shirts</a><br>
	<img src="/graphics/funnytshirts.gif" width="12" height="9" alt="Offensive t-shirts"><a class=leftmenu href="/offensivetshirts.html" title="Offensive t-shirts">Offensive t-shirts</a><br>
	<img src="/graphics/funnytshirts.gif" width="12" height="9" alt="Religious t-shirts"><a class=leftmenu href="/religioustshirts.html" title="Religious t-shirts">Religious t-shirts</a><br>
	<img src="/graphics/funnytshirts.gif" width="12" height="9" alt="Feminist t-shirts"><a class=leftmenu href="/feministtshirts.html" title="Feminist t-shirts">Feminist t-shirts</a><br>
	<img src="/graphics/funnytshirts.gif" width="12" height="9" alt="Attitude t-shirts"><a class=leftmenu href="/attitudetshirts.html" title="Attitude t-shirts">Attitude t-shirts</a><br>
	<img src="/graphics/funnytshirts.gif" width="12" height="9" alt="Bitch t-shirts"><a class=leftmenu href="/bitchtshirts.html" title="Bitch t-shirts">Bitch t-shirts</a><br>
	<img src="/graphics/funnytshirts.gif" width="12" height="9" alt="Bad girl t-shirts"><a class=leftmenu href="/badgirltshirts.html" title="Bad girl t-shirts">Bad girl t-shirts</a><br>
	<img src="/graphics/funnytshirts.gif" width="12" height="9" alt="Beer t-shirts"><a class=leftmenu href="/beertshirts.html" title="Beer t-shirts">Beer t-shirts</a>

<hr>
<h3 class=leftmenu>other collections</h3><br>
	<img src="/graphics/funnytshirts.gif" width="12" height="9" alt="Goth t-shirts"><a class=leftmenu href="/gothtshirts.html" title="Goth t-shirts">Goth t-shirts</a><br>
	<img src="/graphics/funnytshirts.gif" width="12" height="9" alt="Punk t-shirts"><a class=leftmenu href="/punktshirts.html">Punk t-shirts</a><br>
	<img src="/graphics/funnytshirts.gif" width="12" height="9" alt="Music t-shirts"><a class=leftmenu href="/musictshirts.html" title="Music t-shirts">Music t-shirts</a><br>
	<img src="/graphics/funnytshirts.gif" width="12" height="9" alt="Movie t-shirts"><a class=leftmenu href="/movietshirts.html" title="Movie t-shirts">Movie t-shirts</a><br>
	<img src="/graphics/funnytshirts.gif" width="12" height="9" alt="New York t-shirts"><a class=leftmenu href="/newyorktshirts.html" title="New York t-shirts">New York t-shirts</a>

</td>
</tr>
</table>

<table class=righttab width=15%>
<col width=100%>
<tr valign=top>
<td bgcolor=#FFCC66>
<a href="javascript:window.external.AddFavorite('http://www.allfunnytshirts.com','Cool funny t-shirts at AllFunnyTshirts.com')" title="Add our cool site to Favorites">
<img src="/graphics/icon_favorite.gif" border=0 hspace=5 align=middle alt="Add to Favorites our funny t-shirts store" WIDTH=15 height=14>Add to Favorites</a><br>

<a href="mailto:?subject=Funny t-shirts at AllFunnyTshirts.com&amp;body=Visit http://www.allfunnytshirts.com as I did. It is a web store for cool funny t-shirts. I found here large collection of cool t-shirts, funny slogan t-shirts, and offensive t-shirts. You won't be disappointed!" title="Send e-mail to friend about our funny t-shirts and cool slogan t-shirts."><img src="/graphics/icon_email.gif" border=0 hspace=5 align=middle alt="E-mail to friend about our funny t-shirts store" WIDTH=16 height=18>E-mail to a friend</a>
</td></tr>
</table>


<table class=footer width=100%>
<tr valign=top>
<td class=footer>
<a href="http://www.allfunnytshirts.com">Home</a>  
<a href="/links/links.pl" title="Cool and funny t-shirt related link collection">Links</a>
<a href="/faq.html" title="F.A.Q.">Help</a>
<a href="/funnyshop/page.php?xPage=privacy.html" title="Privacy policy">Privacy</a>
<a href="/funnyshop/contact.php" title="Contact AllFunnyTshirts.com">Contact Us</a>
<a href="/sitemap.html" title="Site map">Site Map</a>
<br><hr>
Copyright &copy; 2005 AllFunnyTshirts.com All rights reserved.
</td></tr></table>
</body>
</html>

HTML
}

sub print_hoo {
    my $reldir = shift;
    &print_header($reldir);
    &print_categories($reldir);
    &print_links("$rootdir/$reldir/$datafile");
    &print_footer($reldir);
}

sub print_links {
    my $datafile = shift;
    if (-e $datafile) {
	open(DATA,$datafile) || &error("Cannot open $datafile: $!");
	my $csv = Text::CSV_XS->new();
	while (<DATA>) {
	    chomp;
	    $csv->parse($_);
	    my @columns = $csv->fields();
	    print "<li><a href=\"$columns[0]\" target=\"_blank\">$columns[1]</a> - $columns[2]\n";
	}
    }
}

