Location: PHPKode > scripts > PHP Link Directory Script > linkdirectory/www/admin_link.php
<?php
/* PHP Link Directory Copyright 2011 Robert Rook */
require('dblogon.php');
require('userhead.php');
if($userinf['badmin']<3) { header("Location: {$site_url}/admin.php?cmd=lowaccess"); die(); }

require('lib/validstr.php');
require('tpl/boxes.php');

$msg = "";
$err = "";

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);
}

/* Delete a single link */
if(isset($_POST['delete_link']) && is_numeric($_POST['delete_link'])) {
	mysql_query("DELETE FROM {$prefix}link WHERE linkid='{$_POST['delete_link']}'", $db);
	if(mysql_affected_rows()) {
		$msg.= "<li>The requested link has been permanently removed.</li>\n";
	} else {
		$err.= "<li>The requested link was not found, and so has not been removed.</li>\n";
	}
}

/* Bulk update links */
if(isset($_POST['linkcount']) && is_numeric($_POST['linkcount']) && isset($_POST['link_bulk_update'])) {
	$updated_links = 0;
	for($i=0;$i<$_POST['linkcount'];$i++) {
		if(!isset($_POST['link'.$i]) || !is_numeric($_POST['link'.$i])) { continue; }
		if($_POST['link_bulk_update']=="verify") {
			mysql_query("UPDATE {$prefix}link SET bverified=1 WHERE linkid='".$_POST['link'.$i]."'", $db);
			if(mysql_affected_rows()) { $updated_links++; }
		} else if($_POST['link_bulk_update']=="unverify") {
			$res = mysql_query("SELECT linkid, bverified FROM {$prefix}link WHERE linkid='".$_POST['link'.$i]."'", $db);
			if(mysql_num_rows($res)) {
				$row = mysql_fetch_array($res);
				sendmail_verification_byid($row['linkid']);
				if($row['bverified']) {
					mysql_query("UPDATE {$prefix}link SET bverified=0 WHERE linkid='{$row['linkid']}'", $db);
				}
				$updated_links++;
			}
		} else if($_POST['link_bulk_update']=="activate") {
			mysql_query("UPDATE {$prefix}link SET bactive=1 WHERE linkid='".$_POST['link'.$i]."'", $db);
			if(mysql_affected_rows()) { $updated_links++; }
		} else if($_POST['link_bulk_update']=="deactivate") {
			mysql_query("UPDATE {$prefix}link SET bactive=0 WHERE linkid='".$_POST['link'.$i]."'", $db);
			if(mysql_affected_rows()) { $updated_links++; }
		} else if($_POST['link_bulk_update']=="delete") {
			mysql_query("DELETE FROM {$prefix}link WHERE linkid='".$_POST['link'.$i]."'", $db);
			if(mysql_affected_rows()) { $updated_links++; }
		}
	}
	
	$msg.= "<li>".number_format($updated_links)." links have been ";
	if($_POST['link_bulk_update']=="verify") { $msg.= "marked as email verified"; }
	else if($_POST['link_bulk_update']=="unverify") { $msg.= "marked as email not verified (and an email has been sent to each link submitter, if a valid email address was present)"; }
	else if($_POST['link_bulk_update']=="activate") { $msg.= "activated"; }
	else if($_POST['link_bulk_update']=="deactivate") { $msg.= "deactivated"; }
	else if($_POST['link_bulk_update']=="delete") { $msg.= "permanently removed"; }
	$msg.= ".</li>\n";
}

$header_code = "<script language=\"Javascript\" type=\"text/Javascript\"";
$header_code.= " src=\"{$site_url}/js/floatbox.js\"></script>\n";

$page_title = "Links management";
require('admin_l.php');

/* Create a new link or edit an existing link */
if(isset($_GET['cmd']) && ($_GET['cmd']=="new" || ($_GET['cmd']=="edit" && isset($_GET['linkid']) && is_numeric($_GET['linkid'])))) {
	$show_form = true;
	if($_GET['cmd']=="new") {
		show_header("Create a new link", "Create a new link to add to the directory by filling out the form below.");
		$l = array("ltitle"=>"", "ldesc"=>"", "lkeywords"=>"", "lurl"=>"http://", "lrecip"=>"", "lemail"=>"", "lfname"=>"", 
			"lsafe"=>"", "bactive"=>0, "breciprocal"=>0, "bverified"=>0, "verifkey"=>substr(md5(time()),0,rand(15,25)),
			"lhits"=>0, "lrank"=>0, "catid"=>0, "ctitle"=>"Home", "cpath"=>"0&:Home", "curl"=>"", "rate1"=>0, "rate2"=>0, "rate3"=>0,
			"rate4"=>0, "rate5"=>0);
	} else {
		show_header("Modify link", "Modify the information for the below link and click \"Save Changes\" when ready.");
		$res = mysql_query("SELECT * FROM {$prefix}link WHERE linkid='{$_GET['linkid']}'", $db);
		if(mysql_num_rows($res)) {
			$l = mysql_fetch_array($res);
		} else {
			$show_form = false;
			show_error("Link not found", "The information for the requested link cannot be updated as it has not been found.");		
		}
	}
	
	if(isset($_POST['ltitle'])) {
		$l['ltitle'] = htmlspecialchars($_POST['ltitle']);
		$l['ldesc'] = htmlspecialchars(strip_tags($_POST['ldesc']));
		$l['lkeywords'] = htmlspecialchars(strip_tags(strtolower($_POST['lkeywords'])));
		$l['lurl'] = $_POST['lurl'];
		$l['lrecip'] = $_POST['lrecip'];
		$l['lemail'] = $_POST['lemail'];
		$l['lsafe'] = make_url_safe(strip_tags($_POST['ltitle']));
		$l['bactive'] = ($_POST['bactive']?1:0);
		$l['breciprocal'] = ($_POST['breciprocal']?1:0);
		$l['bverified'] = ($_POST['bverified']?1:0);
		$l['verifkey'] = strip_tags($_POST['verifkey']);
		$l['lfname'] = strip_tags($_POST['lfname']);
		$l['lhits'] = (is_numeric($_POST['lhits'])?round($_POST['lhits']):$l['lhits']);
		$l['lrank'] = (is_numeric($_POST['lrank'])&&isset($ldir_ranks[$_POST['lrank']])?$_POST['lrank']:$l['lrank']);
		$l['rate1'] = (is_numeric($_POST['rate1'])?round($_POST['rate1']):$l['rate1']);
		$l['rate2'] = (is_numeric($_POST['rate2'])?round($_POST['rate2']):$l['rate2']);
		$l['rate3'] = (is_numeric($_POST['rate3'])?round($_POST['rate3']):$l['rate3']);
		$l['rate4'] = (is_numeric($_POST['rate4'])?round($_POST['rate4']):$l['rate4']);
		$l['rate5'] = (is_numeric($_POST['rate5'])?round($_POST['rate5']):$l['rate5']);
		
		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 link URL.</li>\n"; }
		
		$l['catid'] = $_POST['catid'];
		if(!is_numeric($l['catid']) || $l['catid']<1) { $err.= "<li>Please select a valid category for this link.</li>\n"; }
		else {
			$res = mysql_query("SELECT cpath, ctitle, curl, catid FROM {$prefix}cat WHERE catid='{$l['catid']}'", $db);
			if(!mysql_num_rows($res)) { $err.= "<li>Please select a valid category for this link.</li>\n"; }
			else {
				$row = mysql_fetch_array($res);
				$l['ctitle'] = $row['ctitle'];
				$l['cpath'] = $row['cpath']."{$row['catid']}&:{$row['ctitle']}&&";
				$l['curl'] = $row['curl'];
			}
		}
		if(strlen($l['ltitle'])<5 || strlen($l['ltitle'])>50) {
			$err.= "<li>The website title you provide must be between 5 and 50 characters ";
			$err.= "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($l['lfname'])<5 || strlen($l['lfname'])>200) {
			$err.= "<li>Please provide a name between 5 and 200 characters in length.</li>\n";
		}
		if(strlen($l['ldesc'])<80 || strlen($l['ldesc'])>500) {
			$err.= "<li>Please provide a description between 80 and 500 characters in length.</li>\n";
		}
		if(strlen($l['lkeywords'])<10 || strlen($l['lkeywords'])>200 || count(explode(",",$l['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($err)) {
			$res = mysql_query("SELECT linkid FROM {$prefix}link WHERE (ltitle='{$l['ltitle']}' OR lsafe='{$l['lsafe']}')".($_GET['cmd']=="edit"?" AND linkid!={$_GET['linkid']}":"")."", $db);
			if(mysql_num_rows($res)) {
				$err.= "<li>A link with an identical or too-similar title already exists. ";
				$err.= "Please provide a different title for this link.</li>\n";
			}
		}
		
		if(strlen($err)) {
			show_error("Error saving link", "The link information could not be saved for the following reason(s):\n<ul>{$err}</ul>");
		} else {
			if(!get_magic_quotes_gpc()) {
				$l['ltitle'] = addslashes($l['ltitle']);
				$l['lkeywords'] = addslashes($l['lkeywords']);
				$l['ldesc'] = addslashes($l['ldesc']);
			}
			
			/* Create/Save changes */
			if($_GET['cmd']=="new") {
				mysql_query("INSERT INTO {$prefix}link(ltitle, lrecip, lurl, lemail, lfname, lsafe, ldesc, lkeywords,
					rate1, rate2, rate3, rate4, rate5, bactive, breciprocal, bverified, verifkey, lhits, lrank,
					ctime, cip, catid, ctitle, cpath, curl) VALUES ('{$l['ltitle']}', '{$l['lrecip']}', '{$l['lurl']}',
					'{$l['lemail']}', '{$l['lfname']}', '{$l['lsafe']}', '{$l['ldesc']}', '{$l['lkeywords']}', '{$l['rate1']}',
					'{$l['rate2']}', '{$l['rate3']}', '{$l['rate4']}', '{$l['rate5']}', '{$l['bactive']}',
					'{$l['breciprocal']}', '{$l['bverified']}', '{$l['verifkey']}', '{$l['lhits']}', '{$l['lrank']}',
					'".time()."', '{$_SERVER['REMOTE_ADDR']}', '{$l['catid']}', '{$l['ctitle']}', '{$l['cpath']}', '{$l['curl']}')", $db);
				show_result("Link created", "The requested link has been created.\n<ul><li><a href=\"admin_link.php?cmd=view&linkid=".mysql_insert_id()."\">Click here to view link information</a></li></ul>");
				$show_form = false;
			} else {
				mysql_query("UPDATE {$prefix}link SET ltitle='{$l['ltitle']}', lrecip='{$l['lrecip']}', lurl='{$l['lurl']}',
					lemail='{$l['lemail']}', lfname='{$l['lfname']}', lsafe='{$l['lsafe']}', ldesc='{$l['ldesc']}', lkeywords='{$l['lkeywords']}',
					rate1='{$l['rate1']}', rate2='{$l['rate2']}', rate3='{$l['rate3']}', rate4='{$l['rate4']}', rate5='{$l['rate5']}',
					bactive='{$l['bactive']}', breciprocal='{$l['breciprocal']}', bverified='{$l['bverified']}',
					verifkey='{$l['verifkey']}', lhits='{$l['lhits']}', lrank='{$l['lrank']}', catid='{$l['catid']}',
					ctitle='{$l['ctitle']}', cpath='{$l['cpath']}', curl='{$l['curl']}' WHERE linkid='{$l['linkid']}'", $db);
				show_result("Link updated", "The information for the requested link has been updated.");
				$show_form = false;
			}
		}
	}
	
	if($show_form) {
		$htmbactive = ($l['bactive']?" selected=\"selected\"":"");
		$htmbverified = ($l['bverified']?" selected=\"selected\"":"");
		$htmbreciprocal = ($l['breciprocal']?" selected=\"selected\"":"");
		$htmlinkid = (isset($_GET['linkid'])&&is_numeric($_GET['linkid'])?"&linkid={$_GET['linkid']}":"");
		$htmcpath = "";
		$tmp = explode("&&", $l['cpath']);
		foreach($tmp as $value) {
			if(strlen($value)<3) { continue; }
			$value = explode("&:", $value);
			$htmcpath.= "<a href=\"admin_cat.php?c={$value[0]}\">{$value[1]}</a> / ";
		}
echo <<<ENDHTML
<script language="Javascript" type="text/Javascript">
<!--
function show_category_search() {
	window.open('{$site_url}/quickbrowse.php?c={$l['catid']}', '_blank', 'width=300,height=300,menubar=no,toolbar=no');
	return;
}
function change_category(catid, ctitle, cpath) {
	document.getElementById("id_cat_path").innerHTML = cpath+" / "+ctitle;
	document.forms["form_linkinfo"].elements["catid"].value = catid;
	return;
}
// -->
</script>

	<form name="form_linkinfo" method="post" action="admin_link.php?cmd={$_GET['cmd']}{$htmlinkid}">
	<input type="hidden" name="catid" value="{$l['catid']}">
	<table border="0" cellpadding="2px" cellspacing="0" width="100%" class="adminform">
	<tr><td>Link title &nbsp; <a href="{$site_url}/js/help_link.html#ltitle" target="_new">
	<img src="imgs/ico_help.gif" width="12px" height="12px" alt="Help" border="0" id="id_help_ltitle" onmouseover='FB_Show("id_help_ltitle", "<h2>Link Title</h2><p>The title of the link, as it will appear when browsing or searching the directory.  The words in the title will also be used when searching, so a title with keywords will be more helpful than one without.</p>");' onmouseout='FB_Hide();'>
	</a></td>
		<td align="right"><input type="text" name="ltitle" maxlength="50" value="{$l['ltitle']}" class="stdinput"></td></tr>
	<tr><td>URL &nbsp; <a href="{$site_url}/js/help_link.html#lurl" target="_new">
	<img src="imgs/ico_help.gif" width="12px" height="12px" alt="Help" border="0" id="id_help_lurl" onmouseover='FB_Show("id_help_lurl", "<h2>Link URL</h2><p>The website address of the link.  This should be the full address including protocol (http://).</p>");' onmouseout='FB_Hide();'>
	</a></td>
		<td align="right"><input type="text" name="lurl" maxlength="250" value="{$l['lurl']}" class="stdinput"></td></tr>
	<tr><td>Category &nbsp; <a href="{$site_url}/js/help_link.html#cparent" target="_new">
	<img src="imgs/ico_help.gif" width="12px" height="12px" alt="Help" border="0" id="id_help_cparent" onmouseover='FB_Show("id_help_cparent", "<h2>Category</h2><p>The category in which this link will be available.  A relevant and suitable category should always be chosen to help visitors who are browsing your directory.</p>");' onmouseout='FB_Hide();'>
	</a></td>
		<td align="right"><a onclick='Javascript:show_category_search();return false;' class="changecategory">Change</a><span id="id_cat_path">{$htmcpath}</span></td></tr>
	<tr><td>Reciprocal link page URL &nbsp; <a href="{$site_url}/js/help_link.html#lrecip" target="_new">
	<img src="imgs/ico_help.gif" width="12px" height="12px" alt="Help" border="0" id="id_help_lrecip" onmouseover='FB_Show("id_help_lrecip", "<h2>Reciprocal link page URL</h2><p>The web address of the page that contains the reciprocal link, if one is available.  This needs to be a complete web address, including protocol (http://).</p>");' onmouseout='FB_Hide();'>
	</a></td>
		<td align="right"><input type="text" name="lrecip" maxlength="250" value="{$l['lrecip']}" class="stdinput"></td></tr>
	<tr><td>Reciprocal link state &nbsp; <a href="{$site_url}/js/help_link.html#breciprocal" target="_new">
	<img src="imgs/ico_help.gif" width="12px" height="12px" alt="Help" border="0" id="id_help_breciprocal" onmouseover='FB_Show("id_help_breciprocal", "<h2>Reciprocal link state</h2><p>This flag is used to determine whether or not a reciprocal link exists and has been validated.</p>");' onmouseout='FB_Hide();'>
	</a></td>
		<td align="right"><select name="breciprocal" class="stdinput">
			<option value="0">Not Verified</option>
			<option value="1"{$htmbreciprocal}>Verified</option></select></td></tr>
	<tr><td>Submitter name &nbsp; <a href="{$site_url}/js/help_link.html#lfname" target="_new">
	<img src="imgs/ico_help.gif" width="12px" height="12px" alt="Help" border="0" id="id_help_lfname" onmouseover='FB_Show("id_help_lfname", "<h2>Submitter name</h2><p>The name by which the owner of this link can be addressed.</p>");' onmouseout='FB_Hide();'>
	</a></td>
		<td align="right"><input type="text" name="lfname" maxlength="200" value="{$l['lfname']}" class="stdinput"></td></tr>
	<tr><td>Email &nbsp; <a href="{$site_url}/js/help_link.html#lemail" target="_new">
	<img src="imgs/ico_help.gif" width="12px" height="12px" alt="Help" border="0" id="id_help_lemail" onmouseover='FB_Show("id_help_lemail", "<h2>Email</h2><p>The email address of the owner of this link.  This email should be valid and correct, as it may be needed should the owner wish to remove the link at a future date.</p>");' onmouseout='FB_Hide();'>
	</a></td>
		<td align="right"><input type="text" name="lemail" maxlength="250" value="{$l['lemail']}" class="stdinput"></td></tr>
	<tr><td>Email verification key &nbsp; <a href="{$site_url}/js/help_link.html#verifkey" target="_new">
	<img src="imgs/ico_help.gif" width="12px" height="12px" alt="Help" border="0" id="id_help_verifkey" onmouseover='FB_Show("id_help_verifkey", "<h2>Email verification key</h2><p>The verification key is a randomly generated string that can be used to validate the link submitters email address, or can be used to remove a link from the directory.  There is no need to change this key manually.</p>");' onmouseout='FB_Hide();'>
	</a></td>
		<td align="right"><input type="text" name="verifkey" maxlength="30" value="{$l['verifkey']}" class="stdinput"></td></tr>
	<tr><td>Email state &nbsp; <a href="{$site_url}/js/help_link.html#bverified" target="_new">
	<img src="imgs/ico_help.gif" width="12px" height="12px" alt="Help" border="0" id="id_help_bverified" onmouseover='FB_Show("id_help_bverified", "<h2>Email state</h2><p>Whether or not the email address associated with this link has been validated.</p>");' onmouseout='FB_Hide();'>
	</a></td>
		<td align="right"><select name="bverified" class="stdinput">
			<option value="0">Not Verified</option>
			<option value="1"{$htmbverified}>Verified</option></select></td></tr>
	<tr><td>Link rank &nbsp; <a href="{$site_url}/js/help_link.html#lrank" target="_new">
	<img src="imgs/ico_help.gif" width="12px" height="12px" alt="Help" border="0" id="id_help_lrank" onmouseover='FB_Show("id_help_lrank", "<h2>Link rank</h2><p>The link rank determines where on each directory page this link will appear.  The higher the link rank, the closer to the top of the list of links.</p>");' onmouseout='FB_Hide();'>
	</a></td>
		<td align="right"><select class="stdinput" name="lrank">
ENDHTML;
	foreach($ldir_ranks as $key=>$value) {
		echo "<option value=\"{$key}\"".($key==$l['lrank']?" selected=\"selected\"":"");
		echo "\">{$value}</option>\n";
	}
echo <<<ENDHTML
		</select></td></tr>
	<tr><td>Description &nbsp; <a href="{$site_url}/js/help_link.html#ldesc" target="_new">
	<img src="imgs/ico_help.gif" width="12px" height="12px" alt="Help" border="0" id="id_help_ldesc" onmouseover='FB_Show("id_help_ldesc", "<h2>Description</h2><p>The descriptive text for this link, as it will appear to visitors viewing the link in a directory or search listing, or on the links information page.</p>");' onmouseout='FB_Hide();'>
	</a></td>
		<td align="right"><textarea name="ldesc" rows="4" cols="30" class="stdinput">{$l['ldesc']}</textarea></td></tr>
	<tr><td>Keywords &nbsp; <a href="{$site_url}/js/help_link.html#lkeywords" target="_new">
	<img src="imgs/ico_help.gif" width="12px" height="12px" alt="Help" border="0" id="id_help_lkeywords" onmouseover='FB_Show("id_help_lkeywords", "<h2>Keywords</h2><p>The keywords for this link, by which the link can be found when visitors are searching the directory.  Keywords should be seperated by commas, and should be terms or phrases associated with the website this link points to.</p>");' onmouseout='FB_Hide();'>
	</a></td>
		<td align="right"><textarea name="lkeywords" rows="4" cols="30" class="stdinput">{$l['lkeywords']}</textarea></td></tr>
	<tr><td>Hits &nbsp; <a href="{$site_url}/js/help_link.html#lhits" target="_new">
	<img src="imgs/ico_help.gif" width="12px" height="12px" alt="Help" border="0" id="id_help_lhits" onmouseover='FB_Show("id_help_lhits", "<h2>Hits</h2><p>The number of visits this link has received from your website.</p>");' onmouseout='FB_Hide();'>
	</a></td>
		<td align="right"><input type="text" name="lhits" maxlength="10" value="{$l['lhits']}" class="stdinput"></td></tr>
	<tr><td>Active state &nbsp; <a href="{$site_url}/js/help_link.html#bactive" target="_new">
	<img src="imgs/ico_help.gif" width="12px" height="12px" alt="Help" border="0" id="id_help_bactive" onmouseover='FB_Show("id_help_bactive", "<h2>Active State</h2><p>Whether or not this link is publicly available.</p>");' onmouseout='FB_Hide();'>
	</a></td>
		<td align="right"><select name="bactive" class="stdinput">
			<option value="0">Not Active</option>
			<option value="1"{$htmbactive}>Active</option></select></td></tr>
	<tr><td>Public votes received &nbsp; <a href="{$site_url}/js/help_link.html#rank" target="_new">
	<img src="imgs/ico_help.gif" width="12px" height="12px" alt="Help" border="0" id="id_help_rank" onmouseover='FB_Show("id_help_rank", "<h2>Public votes received</h2><p>The public ratings, if enabled, display the average visitor opinion of a link. The rating is calculated based off the number of votes received of one star, two stars, etc.</p>");' onmouseout='FB_Hide();'>
	</a></td>
		<td align="right">
		<fieldset><legend>Link rating</legend>
			<table border="0" cellpadding="2px" cellspacing="0" width="100%">
			<tr><td>1 Votes</td><td align="right"><input type="text" name="rate1" value="{$l['rate1']}" class="sminput"></td></tr>
			<tr><td>2 Votes</td><td align="right"><input type="text" name="rate2" value="{$l['rate2']}" class="sminput"></td></tr>
			<tr><td>3 Votes</td><td align="right"><input type="text" name="rate3" value="{$l['rate3']}" class="sminput"></td></tr>
			<tr><td>4 Votes</td><td align="right"><input type="text" name="rate4" value="{$l['rate4']}" class="sminput"></td></tr>
			<tr><td>5 Votes</td><td align="right"><input type="text" name="rate5" value="{$l['rate5']}" class="sminput"></td></tr>
			</table>
		</fieldset></td></tr>
	<tr><td><a onclick='Javascript:if(confirm("Are you sure you wish to reset the form?")){document.forms["form_linkinfo"].reset();};'>Clear form</a></td>
		<td align="right"><input type="submit" class="stdinput" value="Save"></td></tr>
	</table>
	</form>
ENDHTML;
	}
}
/* View details for an existing link */
else if(isset($_GET['cmd']) && $_GET['cmd']=="view" && isset($_GET['linkid']) && is_numeric($_GET['linkid'])) {
	$show_form = true;
	show_header("Link details", "Below are the details for the selected link.\n<ul><li><a href=\"admin_link.php?cmd=edit&linkid={$_GET['linkid']}\">Edit this link</a></li></ul>");
	
	$res = mysql_query("SELECT * FROM {$prefix}link WHERE linkid='{$_GET['linkid']}'", $db);
	if(!mysql_num_rows($res)) {
		$show_form = false;
		show_error("Link not found", "Details for the requested link could not be fetched as the record was not found.");
	} else {
		$l = mysql_fetch_array($res);
	}
	
	if($show_form) {
		$l['bactive'] = ($l['bactive']?"Yes":"No");
		$l['breciprocal'] = ($l['breciprocal']?"Yes":"No");
		$l['bverified'] = ($l['bverified']?"Yes":"No");
		$l['ctime'] = date('d/m/Y h:i:sa', $l['ctime']);
		$l['lhits'] = number_format($l['lhits']);
		$l['lrank'] = $ldir_ranks[$l['lrank']];
		
		$l['nratings'] = number_format($l['rate1']+$l['rate2']+$l['rate3']+$l['rate4']+$l['rate5']);
		$l['nscore'] = ($l['nratings']>0?round((($l['rate1']+($l['rate2']*2)+($l['rate3']*3)+($l['rate4']*4)+($l['rate5']*5))/$l['nratings']),2):0);
echo <<<ENDHTML
	<table border="0" cellpadding="2px" cellspacing="0" width="100%" class="adminform">
	<tr><td class="titlecell">Link title</td><td>{$l['ltitle']}</td></tr>
	<tr><td class="titlecell">Link ID #</td><td>{$l['linkid']}</td></tr>
	<tr><td class="titlecell">Link title (URL safe)</td><td>{$l['lsafe']}</td></tr>
	<tr><td class="titlecell">Active ?</td><td>{$l['bactive']}</td></tr>
	<tr><td class="titlecell">URL</td><td><a href="{$l['lurl']}" target="_blank">{$l['lurl']}</a></td></tr>
	<tr><td class="titlecell">Ranking</td><td>{$l['lrank']}</td></tr>
	<tr><td class="titlecell">Rating</td><td>{$l['nscore']} ({$l['nratings']} votes)</td></tr>
	<tr><td class="titlecell">Visits</td><td>{$l['lhits']}</td></tr>
	<tr><td class="titlecell">Category</td><td><a href="admin_cat.php?c={$l['catid']}">{$l['ctitle']}</a></td></tr>
	<tr><td class="titlecell">Reciprocal link page</td><td>{$l['lrecip']}</td></tr>
	<tr><td class="titlecell">Reciprocal link verified ?</td><td>{$l['breciprocal']}</td></tr>
	<tr><td class="titlecell">Creator Email</td><td>{$l['lemail']}</td></tr>
	<tr><td class="titlecell">Email verified ?</td><td>{$l['bverified']}</td></tr>
	<tr><td class="titlecell">Email verification key</td><td>{$l['verifkey']}</td></tr>
	<tr><td class="titlecell">Creation date</td><td>{$l['ctime']}</td></tr>
	<tr><td class="titlecell">Creator IP</td><td>{$l['cip']}</td></tr>

	<tr><td colspan="2"><fieldset><legend>Link keywords</legend><p>{$l['lkeywords']}</p></fieldset></td></tr>
	<tr><td colspan="2"><fieldset><legend>Link description</legend><p>{$l['ldesc']}</p></fieldset></td></tr>

	</table>
ENDHTML;
	}
}
/* Remove an existing link */
else if(isset($_GET['cmd']) && $_GET['cmd']=="delete" && isset($_GET['linkid']) && is_numeric($_GET['linkid'])) {
	$show_form = true;
	show_header("Delete link", "If you proceed, all information for the selected link will be removed permanently.  Please review the link information below and click the \"Delete\" button when you are sure you wish to proceed.");
	
	$res = mysql_query("SELECT * FROM {$prefix}link WHERE linkid='{$_GET['linkid']}'", $db);
	if(mysql_num_rows($res)) {
		$l = mysql_fetch_array($res);
	} else {
		$show_form = false;
		show_error("Link not found", "The link you have requested to remove has not be found, and so cannot be updated.");
	}

	if($show_form) {
echo <<<ENDHTML
	<form method="post" action="admin_link.php">
	<input type="hidden" name="delete_link" value="{$l['linkid']}">
	<table border="0" cellpadding="2px" cellspacing="0" width="100%" class="adminform">
	<tr><td class="titlecell">Link title</td>
		<td>{$l['ltitle']}</td></tr>
	<tr><td class="titlecell">Link URL</td>
		<td>{$l['lurl']}</td></tr>
	<tr><td colspan="2" class="titlecell">Description</td></tr>
	<tr><td colspan="2">{$l['ldesc']}</td></tr>
	<tr><td>&nbsp;</td>
		<td align="right"><input type="submit" class="stdbutton" value="Delete"></td></tr>
	</table>
	</form>
ENDHTML;
	}
}
/* Browse links for editing, management, etc */
else {
	show_header("Manage links", "Browse or filter links by category, submission date, submitter IP, current status, or other means you wish to and manage them below.  If you would like to, you can <a href=\"admin_link.php?cmd=new\">add a new link</a> to the directory.");
	
	if(strlen($err)) { show_error("Errors", "The following error(s) have occured:\n<ul>{$err}</ul>"); }
	if(strlen($msg)) { show_result("Updates", "Modification(s) complete:\n<ul>{$msg}</ul>"); }
	
	$sql = "";
	$orderby = "ctime DESC";
	if(isset($_GET['ltype'])) {
		if($_GET['ltype']=="pending") {
			$sql.= " AND bactive=0".($ldir_validateemail?" AND bverified=1":"")."";
		} else if($_GET['ltype']=="active") {
			$sql.= " AND bactive=1";
		}
	}
	if(isset($_GET['c']) && is_numeric($_GET['c'])) {
		$sql.= " AND catid='{$_GET['c']}'";
	}
	if(isset($_GET['bverified'])) {
		if($_GET['bverified']=="0") { $sql.= " AND bverified=0"; }
		else if($_GET['bverified']=="1") { $sql.= " AND bverified=1"; }
	}
	if(isset($_GET['breciprocal'])) {
		if($_GET['breciprocal']=="0") { $sql.= " AND breciprocal=0"; }
		else if($_GET['breciprocal']=="1") { $sql.= " AND breciprocal=1"; }
	}
	if(isset($_GET['lrank']) && is_numeric($_GET['lrank']) && isset($ldir_ranks[$_GET['lrank']])) {
		$sql.= " AND lrank='{$_GET['lrank']}'";
	}
	if(isset($_GET['lhitsby']) && isset($_GET['nhits']) && is_numeric($_GET['nhits'])) {
		if($_GET['lhitsby']=="lt") { $sql.= " AND lhits<{$_GET['nhits']}"; }
		else if($_GET['lhitsby']=="gt") { $sql.= " AND lhits>{$_GET['nhits']}"; }
	}
	$validfields = array("ltitle"=>"Title", "lurl"=>"URL", "cip"=>"Submitter IP", "lemail"=>"Email");
	if(isset($_GET['searchby']) && isset($validfields[$_GET['searchby']]) && strlen($_GET['search'])) {
		if(!get_magic_quotes_gpc()) { $_GET['search'] = addslashes(urldecode($_GET['search'])); }
		$sql.= " AND {$_GET['searchby']} LIKE '".preg_replace('/\*/', "%", $_GET['search'])."'";
	}
	
	$htmsearch = (isset($_GET['search'])?htmlspecialchars($_GET['search']):"");

echo <<<ENDHTML
<script type="text/Javascript" language="Javascript">
<!--
function showhide_advanced_search() {
	if(document.getElementById) {
		if(document.getElementById("id_search_advanced").style.display=="none") {
			document.getElementById("id_search_advanced").style.display = "inline-table";
		} else {
			document.getElementById("id_search_advanced").style.display = "none";
		}
	}
	return;
}

function checkstate_all_links() {
	for(i=0; i<document.forms["form_bulkupdate"].elements["linkcount"].value; i++) {
		document.forms["form_bulkupdate"].elements["link"+i].checked = document.forms["form_bulkupdate"].elements["link_checkall"].checked;
	}
	return;
}
//-->
</script>

	<form method="get" action="admin_link.php">
	<table border="0" cellpadding="2px" cellspacing="0" width="100%" class="adminsearch">
	<tr><td>Search for links where </td>
		<td><select class="stdinput" name="searchby">
ENDHTML;
	echo "<option value=\"ltitle\"".(isset($_GET['searchby']) && $_GET['searchby']=="ltitle"?" selected=\"selected\"":"").">Title</option>\n";
	echo "<option value=\"lurl\"".(isset($_GET['searchby']) && $_GET['searchby']=="lurl"?" selected=\"selected\"":"").">URL</option>\n";
	echo "<option value=\"cip\"".(isset($_GET['searchby']) && $_GET['searchby']=="cip"?" selected=\"selected\"":"").">Submitter IP</option>\n";
	echo "<option value=\"lemail\"".(isset($_GET['searchby']) && $_GET['searchby']=="lemail"?" selected=\"selected\"":"").">Email</option>\n";
echo <<<ENDHTML
			</select></td>
		<td> is </td>
		<td><input type="text" maxlength="200" name="search" class="stdinput" value="{$htmsearch}"></td>
		<td align="right"><input type="submit" class="stdinput" value="Search"></td></tr>
	<tr><td colspan="5">
		<fieldset><legend onclick='Javascript:showhide_advanced_search();'>Advanced filtering
		<img width="12px" height="12px" alt="Arrow-Down" src="imgs/ico_expanddown.gif" border="0"></legend>
			<table border="0" cellpadding="2px" cellspacing="0" width="100%" id="id_search_advanced" style="display:inline-table;">
			<tr><td>Email verified </td>
				<td><select class="stdinput" name="bverified">
					<option value="na">Any</option>
					<option value="0">Not Verified</option>
					<option value="1">Verified</option>
					</select></td>
				<td>Active status </td>
				<td><select class="stdinput" name="ltype">
					<option value="na">Any</option>
					<option value="pending">Pending/Inactive</option>
					<option value="active">Active</option>
					</select></td>
				</tr>
			<tr><td>Link rank </td>
				<td><select class="stdinput" name="lrank">
					<option value="na">Any</option>
ENDHTML;
	foreach($ldir_ranks as $key=>$value) {
		echo "<option value=\"{$key}\">{$value}</option>\n";
	}
echo <<<ENDHTML
					</select></td>
				<td>Reciprocal link </td>
				<td><select name="lrecip" class="stdinput">
					<option value="na">Any</option>
					<option value="0">No Reciprocal</option>
					<option value="1">Reciprocal link</option>
					</select></td>
				</tr>
			<tr><td>Number of hits </td>
				<td><select name="lhitsby" class="stdinput">
					<option value="na">Any</option>
					<option value="gt">Greater than</option>
					<option value="lt">Less than</option>
					</select></td>
				<td><input type="text" name="nhits" value="0" class="stdinput"></td>
				<td>&nbsp;</td></tr>
			</table>
		</fieldset>
		</td></tr>
	</table>
	</form>
ENDHTML;
	
	$view = 0;
	$show = 2;
	$res = mysql_query("SELECT COUNT(*) FROM {$prefix}link WHERE linkid>0{$sql}", $db);
	$count = mysql_result($res, 0);
	
	if(isset($_GET['view']) && is_numeric($_GET['view']) && $_GET['view']>0 && $_GET['view']<$count) {
		$view = round($_GET['view']);
	}
	
	$res = mysql_query("SELECT ltitle, lurl, bactive, bverified, breciprocal, lhits, lrank, ctime, catid, ctitle, linkid FROM {$prefix}link WHERE linkid>0{$sql} ORDER BY {$orderby} LIMIT {$view}, {$show}", $db);
	
	if(mysql_num_rows($res)) {
		if($count>$show) {
			/* Display a pagelist */
			echo "<div class=\"pagelist\">\n";
			$ahtm = "<a href=\"admin_link.php?";
			$svr = explode("&", $_SERVER['QUERY_STRING']);
			foreach($svr as $key=>$value) {
				$tmp = explode("=", $value);
				if($tmp[0]=="view") { unset($svr[$key]); break; }
			}
			$svr = implode("&", $svr);
			
			for($i=0;$i<ceil($count/$show);$i++) {
				if(($i*$show)==$view) { echo "<b>".($i+1)."</b>"; continue; }
				echo $ahtm."view=".($i*$show).(strlen($svr)?"&{$svr}":"")."\">".($i+1);
				echo "</a>";
			}
			
			echo "</div>\n";
		}
echo <<<ENDHTML
	<form method="post" action="admin_link.php?{$_SERVER['QUERY_STRING']}" name="form_bulkupdate">
	<table border="0" cellpadding="2px" cellspacing="0" width="100%" class="listtable">
	<tr><th><input type="checkbox" name="link_checkall" onchange='Javascript:checkstate_all_links();'></th>
		<th>Title<br><small><i>URL (link address)</i></small></th>
		<th>Added Date/Time</th>
		<th>&nbsp;</th>
		<th>&nbsp;</th></tr>
ENDHTML;
		$i = 0;
		
		while($row = mysql_fetch_array($res)) {
			echo "<tr class=\"".($row['bactive']?"":"inactive_link ");
			echo ($row['lrank']?"rank{$row['lrank']}link":"")."\">";
			echo "<td><input type=\"checkbox\" name=\"link{$i}\" value=\"{$row['linkid']}\"></td>";
			echo "<td><a href=\"admin_link.php?cmd=view&linkid={$row['linkid']}\">";
			echo "{$row['ltitle']}</a><br><small><i>{$row['lurl']}</i></small></td>";
			echo "<td>".date('d/m/Y h:i:sa', $row['ctime'])."</td>";
			echo "<td><a href=\"admin_link.php?cmd=edit&linkid={$row['linkid']}\">";
			echo "<img src=\"imgs/ico_edit.gif\" width=\"20px\" height=\"20px\" alt=\"Edit\" border=\"0\"></a></td>";
			echo "<td><a href=\"admin_link.php?cmd=delete&linkid={$row['linkid']}\">";
			echo "<img src=\"imgs/ico_delete.gif\" width=\"20px\" height=\"20px\" alt=\"Delete\" border=\"0\"></a></td>";
			echo "</tr>\n";
			$i++;
		}
		
		echo "</table>\n";
		echo "<input type=\"hidden\" name=\"linkcount\" value=\"{$i}\">\n";
		
echo <<<ENDHTML
	<table border="0" cellpadding="2px" cellspacing="0" width="100%" class="adminbulk">
	<tr><td>Update selected links</td>
		<td><select class="lrginput" name="link_bulk_update">
			<option value="0">Please select an action</option>
			<option value="verify">Mark Email address verified</option>
			<option value="unverify">Require email verification (+ send email)</option>
			<option value="activate">Activate selected links</option>
			<option value="deactivate">Deactive links</option>
			<option value="delete" class="bulkdelete">Delete selected links</option>
			</select></td>
		<td align="right"><input type="submit" class="stdbutton" value="Update links"></td>
		</tr>
	</table>
ENDHTML;
		echo "</form>\n";
	}

}

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