<?php

// ***************************************************************************
// * Copyright © 2003 - Thomas Egtvedt - www.evaria.com - thomas@evaria.com  *
// *                                                                         *
// *      ECMS - Content Management System - Support: forum.evaria.com       *
// *                                                                         *
// * This program is commercial software; you can not redistribute/reproduce *
// * it and/or sell it without the prior written consent of www.evaria.com   *
// *                                                                         *
// * 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.                    *
// ***************************************************************************

if (!empty($id_cat))
{
	$sql = "SELECT cat_name, can_add FROM $T_categories WHERE id = '$id_cat' ";
	$sql_admin = "SELECT use_newsletter, func_mail FROM $T_infos ";
	$lk = connection_BD();
	$res = send_sql($sql,"searching for valid category input...");
	$res_admin = send_sql($sql_admin,"collecting max input info, and creating newsletter field...");	

	if (mysql_num_rows($res))
	{
		$resultat = mysql_fetch_array($res,MYSQL_ASSOC);
		$cat_name = $resultat["cat_name"];
		$resultat_admin = mysql_fetch_array($res_admin,MYSQL_ASSOC);
		$use_newsletter = $resultat_admin["use_newsletter"];
		$func_mail_ok = $resultat_admin["func_mail"];
		$can_add = $resultat["can_add"];
	}
	else
	{
		Header("Location: ".$baseurl."?view=link_home");
		exit;
	}
}
?>

<TABLE align="center" width="<? echo $table_width; ?>" cellspacing="0" cellpadding="0" border="0">
  <TR>
	<TD class="link_m_head"><img src="./img/h_email.gif" width="32" height="32" alt="Add your link" border="0" align="absmiddle">  Add your resource to our directory »</TD>
  </TR>
  <TR>
    <TD align="center" valign="middle"><br><span>
<?
if ($action == "submit")
{
$Field_bug = "";
$ln_champ = strlen($description);
$ln_max = "1000";
$ln_min = "10";
/*	
    // This will be the backup for now...
    // Verifying input values before adding new link...
	est_vide($nom_proprio,"Please enter your name/nickname!",true);
	email_valide($email_proprio,true);
	est_vide($titre,"Please enter a title for your site!",true);
	url_valide($url,true);
	est_vide($description,"Please enter a description for your site!",true);
	est_trop_long($description,1000,"description",true);
	est_trop_court($description,10,"description",true);
*/

    if(empty($nom_proprio))
	{
    	$Field_bug.="<li>Please enter your name or nickname !</li>\n";
	}
	if(empty($email_proprio))
	{
    	$Field_bug.="<li>Please enter your Email address !</li>\n";
	}
    if(!empty($email_proprio))
	{
	$email_ok = eregi("^[_\.0-9a-z-]+@([0-9a-z-]+\.)+[a-z]{2,4}$",$email_proprio);		
		if(TEST_MAIL)
		{		
			list($user, $domaine) = split("@", $email_proprio, 2);
			$domain_ok = @checkdnsrr($domaine, "MX");
		}
		else
		{
		    $domain_ok = true;
		}
	}
	if (!$email_ok || !$domain_ok)
	{
		$Field_bug.="<li>Please verify your Email address !<br>";
		$Field_bug.="<b>$email_proprio</b> is not a valid Email address !</li>\n";

	}
	if(empty($titre))
	{
    	$Field_bug.="<li>Please enter a title for your link | website !</li>\n";
	}
        if (empty($url) || $url == "http://")
        $Field_bug.= "<li>You have not entered a valid URL !</li>\n";
	else
	{
		// Verifiy that the URL is on a valid form...
		$cmp_h = strcmp(substr($url,0,7),"http://");
		$cmp_n = strcmp(substr($url,0,7),"news://");
		$cmp_f = strcmp(substr($url,0,6),"ftp://");
		$cmp_m = strcmp(substr($url,0,7),"mailto:");
		if ($cmp_h != 0 && $cmp_n != 0 && $cmp_f != 0 && $cmp_m != 0)
			$Field_bug.= "<li>Invalid URL! We only allow  http:// (or news:// , ftp:// , mailto:)</li>\n";
		if ($cmp_h== 0) //if URL = http://
		{
			$url_parsee = @parse_url($url);
			$host = trim($url_parsee["host"]);
			$path = trim($url_parsee["path"]);
			if (check_syntaxe_domaine($host))
			{
				$tab_return = check_connect($host,$path);
				if ($tab_return["statut"] == 0)
					$Field_bug.= "<li>We are unable to verify your site &raquo; $url<br>Your server may be down !</li>\n";
			}
			else
			{
			    $Field_bug.= "<li>We are unable to connect to your host &raquo; $host<br>Your server may be down !</li>\n";
			}
		}
	}
	if(empty($description))
	{
    	$Field_bug.="<li>Please enter a description for your website !</li>\n\n";
	}
	if ($ln_champ > $ln_max)
        {
        $Field_bug.="<li>Your description contains $ln_champ characters.<br>Please reduce it to meet our maximum, which is $ln_max characters !</li>\n";
        }
	if ($ln_champ < $ln_min)
        {
        $Field_bug.="<li>Your description contains $ln_champ characters.<br>Please extend it to meet our minimum, which is $ln_min characters !</li>\n";
        }

	$No_mail = "$Field_bug";

	$sql_admin = "SELECT max_presence, auto_validate, email_master, titre_site, url_rep FROM $T_infos ";
	$res_admin = send_sql($sql_admin,"finding info in DB");

	$result_admin = mysql_fetch_array($res_admin);
	$auto_validate = $result_admin["auto_validate"];
	$email_master = $result_admin["email_master"];
	$titre_site = $result_admin["titre_site"];
	$url_rep = $result_admin["url_rep"];
	$max_presence = $result_admin["max_presence"];

	if (cherche_presence_url($url,$id_cat,$max_presence))
	{
?>
</span>
<br><br><br><br><br><br>
<input class="link_close_button" type="button" onclick="openLink('?view=link_home')" value=" Exit ">
    </TD>
  </TR>
</TABLE>
<?
	exit;
	}

	if (!get_magic_quotes_gpc())
	{
		$nom_proprio = addslashes($nom_proprio);
		$titre = addslashes($titre);
		$description = addslashes($description);
	}
	$nom_proprio = htmlspecialchars($nom_proprio);
	$titre = htmlspecialchars($titre);
	$description = htmlspecialchars($description);

    if (!empty($No_mail))
    {
    ?>

  <br></TD>
  </TR>
  <TR>
    <TD class="m_shead" align="left">Houston we have a problem...</TD>
  </TR>
  <TR>
	<TD class="m_text"><br>
	<ul type="square">
	<? 
	echo $No_mail;
	?>
	</ul>
	<br><br>
	Your link could not be processed properly.<br> 
	Please note the error messages above and try again.
    </TD>
  </TR>
  <TR>
    <TD align="center">
	<br><br><br><br><br><br>
	<input class="link_close_button" type="button" value=" Return &amp; Fix " onclick="javascript:history.go(-1);">
    </TD>
  </TR>
</TABLE>

    <?
	return true;
	}
	
	if ($auto_validate)
	{
	$sql = "INSERT INTO $T_sites (id_cat,titre,description,url,nom_proprio,email_proprio,date_validation) VALUES ('$id_cat','$titre','$description','$url','$nom_proprio','$email_proprio',CURRENT_DATE) ";
	}
	else
	{
	$sql = "INSERT INTO $T_sites_in_wait (id_cat,titre,description,url,nom_proprio,email_proprio,date_soumission) VALUES ('$id_cat','$titre','$description','$url','$nom_proprio','$email_proprio',CURRENT_DATE) ";
	}
	$sql_cat_name = "SELECT cat_name FROM $T_categories WHERE id = '$id_cat' ";
	$ins = send_sql($sql,"link added!");
	$res_cat_name = send_sql($sql_cat_name,"creating categories...");
	mysql_close($lk);
	
	$resultat_cat_name = mysql_fetch_array($res_cat_name,MYSQL_ASSOC);
	
	// Confirmation message to admin and visitor...
	?>
	<span class="confirm_add_site_annu">
	<?
	if($func_mail_ok)
	// This is activated if admin has activated the mail function
	{
		// Create auto validation message...
		if ($auto_validate)
		{
			$message_vis = "Your link: $url\n is currently being checked out by our site staff.\n\n";
			$message_master = "The site $titre has been suggested to category: $resultat_cat_name[cat_name].\n";
			$message_master .= "You can check it out at this address:\n";
			$message_master .= "$url_rep/?view=link_home&cat_id=$id_cat\n\n";
		}
		else // Create validate awaiting message...
		{
			$nom_categorie = $resultat_cat_name["cat_name"];
			$message_vis = "Thank you for taking the time to submit:\n $url\n\n";
			$message_vis .= "As soon as we have verified your link,\n";
			$message_vis .= "you will receieve an e-mail informing you whether or not\n";
			$message_vis .= "your link has been accepted for inclusion in our link manager.\n\n";
			$message_master .= "The link: $url is awaiting validation to category: $nom_categorie.";
		}
		// Send mail to visitor when new link is submitted...
		send_mail($email_proprio,"Thank You...",$message_vis,$email_master);
		// Send mail to webmaster when new link is submitted...
		send_mail($email_master,"New link awaiting validation...",$message_master,$email_master);

		if ($auto_validate)
		{
			?>
			<center>Your link was automatically validated in your category of choice!</center>
			<?
		}
		else //success page - link awaiting validation
		{
			?>

<br></TD>
  </TR>
  <TR>
    <TD class="m_shead" align="left">Your submittion was a success <? echo $nom_proprio; ?> !!</TD>
  </TR>  
  <TR>
	<TD class="link_m_text"><br>During the next 48h one of our staff members will visit
	and verify your site:
	<br><ul type="square">
	<li><b><? echo $titre; ?></b></li>
	<li><b><? echo $url; ?></b></li>
	</ul><br>
	to see if its suited for inclusion in category <b><? echo $nom_categorie; ?></b>.
	<br><br>
	If your website should be unreachable for some odd reason, eg. server maintenance,
	upgrades, cable collapse, terrorism etc. Then please use our <a class="link_m_text"
	href="<? echo $baseurl; ?>?view=contact">contact form</a> to inform us about the event.
	<br><br>
	<b>Please note!</b><br>
	We reserve the right to reject sites that contradicts with our <a class="link_m_text"
	href="<? echo $baseurl; ?>?view=terms">terms and conditions</a></TD>
  </TR>
</TABLE>
<br>

			<?
		}
		// Message to webmaster if visitor signs up for Newsletter...
		if ($add2newsletter)
			send_mail($email_master,"New Newsletter Subscription...","$email_proprio has decided to sign up for our Newsletter",$email_master);		
	}
	else // This is activated if admin has deactivated the mail function...
	{
		if ($auto_validate)
		{
			?>
			<center>Your link was automatically validated to your selected category!</center>
			<?
		}
		else //success page - link is verified as soon as admin discovers it in the cpanel
		{		
		?>

<TABLE align="center" width="<? echo $table_width; ?>" cellspacing="0" cellpadding="0" border="0">
  <TR>
	<TD class="link_m_head"><img src="./img/h_email.gif" width="32" height="32" alt="Success" border="0" align="absmiddle">  Your submittion was a success <? echo $nom_proprio; ?> !!</TD>
  </TR>
  <TR>
	<TD class="link_m_text"><br>During the next 48h one of our staff members will visit
	and verify your site:
	<br><br>
	<b>  »<? echo $titre; ?></b>
	<br><br>
	<b>  »<? echo $url; ?></b>
	<br><br>
	to see if its suited for inclusion in category <b><? echo $nom_categorie; ?></b>.
	Immediately after you will receive an e-mail informing you whether or not
	your link has been accepted for inclusion in our directory!
	<br><br>
	If your website should be unreachable for some odd reason, eg. server maintenance,
	upgrades, cable collapse, terrorism etc. Then please use our <a class="link_m_text"
	href="<? echo $baseurl; ?>?view=contact">contact form</a> to inform us about the event.
	<br><br>
	<b>Please note!</b><br>
	We reserve the right to reject sites that contradicts with our <a class="link_m_text"
	href="<? echo $baseurl; ?>?view=terms">terms and conditions</a></TD>
  </TR>
</TABLE>
<br>

		<?
		}
	}
	?>
	</span>
	<br><br><br><br>
	<input class="link_close_button" type="button" onclick="openLink('<? echo $baseurl; ?>?view=link_home')" value=" Return to Index ">
	
	<input class="link_close_button" type="button" onclick="openLink('<? echo $baseurl; ?>?view=link_submit')" value=" Add more Links ">
    </TD>
  </TR>
</TABLE>	
	<?
}
else
{
	if (!empty($id_cat) && $can_add != 1)
	{
		mysql_close($lk);
		?>
	<center>
   <br><br><br><br><span class="link_error">
	The Admin has decided not to add more links to this category.
	<br><br>
	Please try to select another category for your link...	
	</span></center>
	</TD>
  </TR>
</TABLE>
    <?
	}
	else
	{
		// Displaying the conditions...
		include "./pages/link_terms.inc";
		?>
		</TD>
	</TR>
</TABLE>
<br>
		
<FORM action="?view=link_submit" method="post" name="form2submit">
<input type="hidden" name="action" value="submit">
<TABLE class="link_submit_table" width="<? echo $table_width; ?>" align="center" cellspacing="10" cellpadding="0" border="0">
  <TR>
    <TD align="left"><b><img src="img/arrow.gif" width="11" height="11"
	alt="required" border="0" align="absmiddle"> Your Name:</b><br>
	<input type="text" name="nom_proprio" maxlength="50" size="40" class="itext"></TD>
	<TD align="left"><b><img src="img/arrow.gif" width="11" height="11"
	alt="required" border="0" align="absmiddle"> Your Email:</b><br>
	<input type="text" name="email_proprio" maxlength="75" size="40" value="@" class="itext"></TD>
  </TR>
  <TR>
    <TD align="left"><b><img src="img/arrow.gif" width="11" height="11"
	alt="required" border="0" align="absmiddle"> Site Title:</b><br>
	<input type="text" name="titre" maxlength="50" size="40" class="itext"></TD>
	<TD align="left"><b><img src="img/arrow.gif" width="11" height="11"
	alt="required" border="0" align="absmiddle"> Site URL:</b><br>
	<input type="url" name="url" maxlength="150" size="40" value="http://" class="itext"></TD>
  </TR>
  <TR>
    <TD align="left" colspan="2"><b><img src="img/arrow.gif" width="11" height="11"
	alt="required" border="0" align="absmiddle"> Description:</b><br>
<textarea name="description" cols="40" rows="5" class="iarea">
</textarea></TD>
  </TR>
  <TR>
    <TD colspan="2" align="center" class="link_m_text"><br>
	<?
	if (empty($id_cat))
		{
			$lk = connection_BD();
			echo "    <div>".show_list_cats("id_cat","",true)."</div>";
		}
		else
		{
	?>
    <input type="hidden" name="id_cat" value="<? echo $id_cat; ?>">
	Category » <b><? echo $cat_name; ?></b>
	<?
		}
		mysql_close($lk);
	?>
	<br><br>
	<input type="submit" value="  Click Here To Submit Your Link  " onclick="return check_id_cat()" class="link_close_button">
		<?
		if ($use_newsletter && $func_mail_ok)
		{
		?>
		<br><br>
      Keep informed about site updates? <input type="checkbox" name="add2newsletter" value="1"> (Optional)
		<?
		}
		?>
    </TD>
  </TR>
</TABLE>
</FORM>
<br><br>
		<?
	}
}
?>
