Location: PHPKode > scripts > PHP Link Directory Script > linkdirectory/www/addlink.php
<?php
/* PHP Link Directory Copyright 2011 Robert Rook */
require('dblogon.php');
require('lib/validstr.php');
require('tpl/boxes.php');

// Include the form validation script
$header_code = "<script language=\"Javascript\" type=\"text/Javascript\"";
$header_code.= " src=\"{$site_url}/js/vform.js\"></script>\n";

function make_url_safe($txt) {
	$txt = str_replace(array('&amp;', '&#039;', '&quot;','&lt;', '&gt;'),"",$txt);
	$txt = preg_replace('/[^a-z0-9\-]/',"-",strtolower($txt));
	return preg_replace('/[\-]{2,}/',"-",$txt);
}

/* Check a valid category has been requested for submission and submissions are allowed */
if(isset($c)) { unset($c); }
if($ldir_acceptlinks && isset($_GET['c']) && is_numeric($_GET['c']) && $_GET['c']>0) {
	$res = mysql_query("SELECT ctitle, catid, cpath, ckeywords, curl FROM {$prefix}cat WHERE catid='{$_GET['c']}' AND acceptlinks=1", $db);
	if(mysql_num_rows($res)) {
		$c = mysql_fetch_array($res);
	}
}

$l = array("ltitle"=>"", "ldesc"=>"", "lkeywords"=>"", "lurl"=>"http://", "lemail"=>"", "lrecip"=>"", "lfname"=>"", "ctime"=>time(), "cip"=>$_SERVER['REMOTE_ADDR'], "verifkey"=>substr(md5(time()),0,rand(15,20)), "bactive"=>($ldir_adminvalidates || $ldir_validateemail?0:1), "breciprocal"=>0);
$err = "";
$msg = "";

/* If a link has been submitted, verify the category and information */
if(isset($_POST['ltitle']) && isset($c)) {
	$l['ltitle'] = htmlspecialchars($_POST['ltitle']);
	$l['lsafe'] = make_url_safe(strip_tags($_POST['ltitle']));
	$l['ldesc'] = htmlspecialchars($_POST['ldesc']);
	$l['lfname'] = strip_tags($_POST['lfname']);
	$l['lkeywords'] = htmlspecialchars($_POST['lkeywords']);
	$l['lemail'] = strip_tags($_POST['lemail']);
	$l['lurl'] = strip_tags($_POST['lurl']);
	$l['lrecip'] = strip_tags($_POST['lrecip']);
	if($ldir_requirereciprocal && !validate_url($l['lrecip'])) {
		$err.= "<li>Please provide a valid address for the reciprocal link.</li>\n";
	} else if($ldir_requirereciprocal && $ldir_validatereciprocal && !validate_reciprocal($l['lrecip'])) {
		$err.= "<li>Please include the link on your page before submitting the form.</li>\n";
	}
	
	if(!isset($_POST['tos']) || $_POST['tos']!="yes") {
		$err.= "<li>You must agree to the Terms of Service before submitting ";
		$err.= "your link to this Directory.</li>\n";
	}
	if(!validate_email($l['lemail'])) {
		$err.= "<li>Please provide a valid email address.</li>\n";
	}
	if(!validate_url($l['lurl'])) {
		$err.= "<li>Please provide a valid page URL.</li>\n";
	}
	if($ldir_requirereciprocal && !validate_url($l['lurl'])) {
		$err.= "<li>Please provide a valid reciprocal link page URL.</li>\n";
	}
	if(strlen($_POST['ltitle'])<5 || strlen($_POST['ltitle'])>50) {
		$err.= "<li>Please provide a link title between five and 50 characters in length.</li>\n";
	}
	if(preg_match('/[^a-z0-9\-_\s\/,\.!\?]/i', $_POST['ltitle'])) {
		$err.= "<li>Please only use alpha-numeric characters, blank spaces, or the following ";
		$err.= "characters: hyphens (-), commas, full stops, exclaimation marks, question ";
		$err.= "marks, forward slashes and underscores in the title.</li>\n";
	}
	if(strlen($_POST['ldesc'])<80 || strlen($_POST['ldesc'])>500) {
		$err.= "<li>Please provide a link description between 80 and 500 characters in length.</li>\n";
	}
	if(strlen($_POST['lkeywords'])<10 || strlen($_POST['lkeywords'])>200 || count(explode(",",$_POST['lkeywords']))<3) {
			$err.= "<li>Please provide a list of keywords between 10 and 200 ";
			$err.= "characters in length, seperated by commas, and including at ";
			$err.= "least 3 different keywords.</li>\n";
		}
	if(strlen($_POST['lfname'])<3 || strlen($_POST['lfname'])>200 || $_POST['lfname']!=$l['lfname']) {
		$err.= "<li>Please provide a contact name between five and 200 characters in length ";
		$err.= "which does not contain any special characters.</li>\n";
	}
	if($ldir_requirereciprocal && ini_get('allow_url_fopen') && validate_url($l['lurl'])) {
		if(!validate_reciprocal($l['lurl'])) {
			$err.= "<li>Please ensure the reciprocal link code is already ";
			$err.= "on your website before submitting this form.</li>\n";
		}
	}
	
	if(!get_magic_quotes_gpc()) {
		$l['ltitle'] = addslashes($l['ltitle']);
		$l['lsafe'] = addslashes($l['lsafe']);
		$l['ldesc'] = addslashes($l['ldesc']);
		$l['lkeywords'] = addslashes(strtolower($l['lkeywords']));
		$l['lurl'] = addslashes($l['lurl']);
		$l['lfname'] = addslashes($l['lfname']);
	}
	
	if(!strlen($err)) {
		$res = mysql_query("SELECT linkid FROM {$prefix}link WHERE ltitle='{$l['ltitle']}' OR lsafe='{$l['lsafe']}'", $db);
		if(mysql_num_rows($res)) {
			$err.= "<li>A link with the same or a too-similar title already exists. ";
			$err.= "Please try including your site name in the link title.</li>\n";
		}
		
		$res = mysql_query("SELECT linkid FROM {$prefix}link WHERE lurl='{$l['lurl']}'", $db);
		if(mysql_num_rows($res)) {
			$err.= "<li>This link has already been submitted to our directory.</li>\n";
		}
	}
	
	if(!strlen($err)) {
		$msg.= "<li>Link submitted!  Your link has been submitted to our directory. ";
		$msg.= ($ldir_validateemail?"A validation link has been sent to the email address you provided.  Please follow the instructions in the email to complete submission. ":"");
		$msg.= ($ldir_adminvalidates?"An administrator will review this link before it is included in our directory. ":"");
		require('lib/sendmail.php');
		
		$c['cpath'] = explode("&&", $c['cpath']);
		array_push($c['cpath'], "{$c['catid']}&:{$c['ctitle']}");
		$c['cpath'] = implode("&&", $c['cpath']);
		
		mysql_query("INSERT INTO {$prefix}link(ltitle, lurl, lrecip, lfname, lemail, ldesc, lkeywords, lsafe, bactive, breciprocal, verifkey, ctime, cip, catid, ctitle, cpath, curl) VALUES ('{$l['ltitle']}', '{$l['lurl']}', '{$l['lrecip']}', '{$l['lfname']}', '{$l['lemail']}', '{$l['ldesc']}', '{$l['lkeywords']}', '{$l['lsafe']}', '{$l['bactive']}', '{$l['breciprocal']}', '{$l['verifkey']}', '{$l['ctime']}', '{$l['cip']}', '{$c['catid']}', '{$c['ctitle']}', '{$c['cpath']}', '{$c['curl']}')", $db);
		
		if($ldir_validateemail) { sendmail_verification_byid(mysql_insert_id()); }
	}
}

$page_title = "Add a link";
require('page_l.php');

if(!isset($c)) {
	/* Prompt the user to select a valid category */
echo <<<ENDHTML
	<table border="0" cellpadding="2px" cellspacing="0" class="addurlerror">
	<tr><td><h1>Cannot add link</h1>
		<p>You cannot add a link, which may be for one of the following reasons:
			<ul>
				<li>You have not selected a category that accepts links</li>
				<li>Links are not currently publicly submittable to this directory</li>
			</ul>
			Please try <a href="browse.php">browsing to the category</a> you believe
			your link belongs in, and clicking the "Add Link" link, if present.  If the
			link is not present, the selected category either does not accept links, or
			adding of links by the public is disabled for this directory.</p>
		</td></tr>
	</table>
ENDHTML;
} else if(strlen($msg)) {
	/* The link has been submitted */
	show_result("Link submitted", "<ul>{$msg}</ul>");
} else {
	/* Show submission form */
	$htm_suggest_recip = "";
	if($ldir_requirereciprocal) {
		$htm_suggest_recip = "<ul><li>A reciprocal link to our website is required before you can add your ";
		$htm_suggest_recip.= "link to this directory.</li></ul>\n";
	} else if($ldir_suggestreciprocal) {
		$htm_suggest_recip = "<ul><li>A reciprocal link to our website is appreciated, but not required ";
		$htm_suggest_recip.= "when adding your link to this directory.</li></ul>\n";
	}
	
	$breadcrumbs = "";
	$tmp = explode("&&", $c['cpath']);
	$turl = "";
	foreach($tmp as $value) {
		if(strlen($value)<3) { continue; }
		$value = explode("&:", $value);
		if(strlen($breadcrumbs)) {
			$breadcrumbs.= " :: ";
		}
		if($value[0]>0) { $turl.= make_url_safe($value[1]); }
		if(strlen($turl)) { $turl.= "/"; }
		$breadcrumbs.= "<a href=\"".($ldir_usestatic?"{$site_url}/{$ldir_folderroot}/{$turl}":"{$site_url}/browse.php?c={$value[0]}");
		$breadcrumbs.= "\">{$value[1]}</a>";
	}
	$breadcrumbs.= " :: <a href=\"".($ldir_usestatic?"{$site_url}/{$ldir_folderroot}/{$c['curl']}":"{$site_url}/browse.php?c={$c['catid']}");
	$breadcrumbs.= "\">{$c['ctitle']}</a>";
	
	show_header("Add your link", "Suggest a link for your website here.  Please fill out the form below and click \"Add Link\" when ready.{$htm_suggest_recip}");
	
	if($ldir_requirereciprocal || $ldir_suggestreciprocal) {
		$htmlinkcode = file_get_contents("data/linkcode.txt");
echo <<<ENDHTML
	<fieldset class="linkcode"><legend>Reciprocal link code</legend>
	<textarea readonly="readonly" class="maxinput" cols="60" rows="3">{$htmlinkcode}</textarea>
	</fieldset>
ENDHTML;
	}
	
	if(strlen($err)) {
		show_error("Submission errors", "The following error(s) occured with your link submission:\n<ul>{$err}</ul>");
	}
echo <<<ENDHTML
<script type="text/Javascript" language="Javascript">
<!--
function validate_all_fields() {
	validated = true;
	if(!validate_field("id_ltitle", /^[a-z0-9\-_\s\/,\.!\?]{5,50}\$/i)) { validated = false; }
	if(!validate_field("id_lurl", /^(http|ftp)\:\/\/[a-z0-9\-]{1,}\.[a-z0-9\.\-\/]{2,}$/i)) { validated = false; }
	if(!validate_field("id_lfname", /^[^\<\>\"\/\\\\]{3,200}\$/i)) { validated = false; }
	if(!validate_email("id_lemail")) { validated = false; }
	if(!validate_field("id_lkeywords", /^.{10,200}\$/i)) { validated = false; }
	if(!validate_field("id_ldesc", /^.{80,500}\$/i)) { validated = false; }
	
	if(validated) { GetOBJ("id_submit_link").disabled = "disabled"; }
	return validated;
}
//-->
</script>

	<form method="post" action="addlink.php?c={$_GET['c']}" onsubmit='Javascript:if(!validate_all_fields()) { return false; };'>
	<fieldset class="termsofservice"><legend>Terms of Service</legend>
		<div class="tos_text">
ENDHTML;
	$htmtos = file_get_contents("data/tos.txt");
	$htmtos = preg_replace(array('/\n/','/\t/'), array("<br>\n", "&nbsp;&nbsp;&nbsp;&nbsp;"), htmlspecialchars($htmtos));
	echo $htmtos;
echo <<<ENDHTML
		</div>
		<label><input type="checkbox" value="yes" name="tos"> I have read and agree to the Terms of Service</label>
	</fieldset>
	
	<table border="0" cellpadding="2px" cellspacing="0" class="addurlform" width="100%">
	<tr><td>Site title</td>
		<td align="right"><input type="text" class="stdinput" name="ltitle" id="id_ltitle" maxlength="50" value="{$l['ltitle']}" onchange='Javascript:validate_field("id_ltitle", /^[a-z0-9\-_\s\/,\.!\?]{5,50}\$/i);'></td></tr>
	<tr><td colspan="2"><div class="formerror" style="display:none;" id="id_ltitle_error">Please provide a website title between 5 and 50 characters in length, which only uses 
	alpha-numeric characters, blank spaces, underscores, hyphens, full stops, commas,
	question marks, forward slashes or exclaimation marks.</div></td></tr>
	<tr><td>Site URL</td>
		<td align="right"><input type="text" class="stdinput" name="lurl" id="id_lurl" maxlength="250" value="{$l['lurl']}" onchange='Javascript:validate_field("id_lurl", /^(http|ftp)\:\/\/[a-z0-9\-]{1,}\.[a-z0-9\.\-\/]{2,}$/i);'></td></tr>
	<tr><td colspan="2"><div class="formerror" style="display:none;" id="id_lurl_error">Please provide a valid website address.</div></td></tr>
	<tr><td>Category</td>
		<td align="right">{$breadcrumbs}</td></tr>
	<tr><td>Your name</td>
		<td align="right"><input type="text" class="stdinput" name="lfname" id="id_lfname" maxlength="100" value="{$l['lfname']}" onchange='Javascript:validate_field("id_lfname", /^[^\<\>\"\/\\\\]{3,200}\$/i);'></td></tr>
	<tr><td colspan="2"><div class="formerror" style="display:none;" id="id_lfname_error">Please provide a contact name between 3 and 200 characters in length which does not contain any special characters.</div></td></tr>
	<tr><td>Email address</td>
		<td align="right"><input type="text" class="stdinput" name="lemail" id="id_lemail" maxlength="250" value="{$l['lemail']}" onchange='Javascript:validate_email("id_lemail");'></td></tr>
	<tr><td colspan="2"><div class="formerror" style="display:none;" id="id_lemail_error">Please provide a valid email address.</div></td></tr>
ENDHTML;

	if($ldir_requirereciprocal) {
		echo "<tr><td>Reciprocal link page URL</td>";
		echo "<td align=\"right\"><input type=\"text\" name=\"lrecip\" id=\"id_lrecip\" value=\"{$l['lrecip']}\" maxlength=\"250\" class=\"stdinput\" onchange='Javascript:validate_field(\"id_lrecip\", /^(http|ftp)\:\/\/[a-z0-9\-]{1,}\.[a-z0-9\.\-\/]{2,}/i);'>";
		echo "</td></tr>\n";
		echo "<tr><td colspan=\"2\"><div class=\"formerror\" style=\"display:none;\" id=\"id_lrecip_error\">Please provide a valid reciprocal link page address.</div></td></tr>\n";
	} else if($ldir_suggestreciprocal) {
		echo "<tr><td>Reciprocal link page URL</td>";
		echo "<td align=\"right\"><input type=\"text\" name=\"lrecip\" value=\"{$l['lrecip']}\" maxlength=\"250\" class=\"stdinput\">";
		echo "</td></tr>\n";
	}

echo <<<ENDHTML
	<tr><td valign="top">Keywords</td>
		<td align="right"><textarea class="stdinput" name="lkeywords" id="id_lkeywords" cols="40" rows="6" onchange='Javascript:validate_field("id_lkeywords", /^.{10,200}\$/i);'>{$l['lkeywords']}</textarea></td></tr>
	<tr><td colspan="2"><div class="formerror" style="display:none;" id="id_lkeywords_error">Please provide a comma seperated list of keywords for this link between 10 and 200 characters in length containing at least three seperate keywords or phrases.</div></td></tr>
	<tr><td valign="top">Description</td>
		<td align="right"><textarea class="stdinput" name="ldesc" id="id_ldesc" cols="40" rows="6" onchange='Javascript:validate_field("id_ldesc", /^.{80,500}\$/i);'>{$l['ldesc']}</textarea></td></tr>
	<tr><td colspan="2"><div class="formerror" style="display:none;" id="id_ldesc_error">Please provide a link description between 80 and 500 characters in length.</div></td></tr>
	<tr><td></td>
		<td align="right"><input type="submit" class="stdbutton" id="id_submit_link" value="Add Link"></td></tr>
	</table>
	</form>
ENDHTML;
}

require('page_r.php');
?>
Return current item: PHP Link Directory Script