admin.php, browse.php, user.php, includes/get_session_vars.php: Security - SQL injection patches - from @ariadoss
admin.php, browse.php, user.php, includes/get_session_vars.php: Security - SQL injection patches - from @ariadoss

file:a/admin.php -> file:b/admin.php
--- a/admin.php
+++ b/admin.php
@@ -25,6 +25,8 @@
 if(isset($_GET['action']) && ($_GET['action'] == "categories" || $_GET['action'] == "admins")) $displayform = 1;
 
 $disableTiny = true;
+
+ob_start();
 
 include ("header.php");
 
@@ -56,7 +58,7 @@
 	}
 	$output .= "</div>\n";
 	if($action) {
-		$panelquery = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_panels WHERE panel_name = '$action' AND panel_type = 'A' LIMIT 1");
+		$panelquery = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_panels WHERE panel_name = '".escapestring($action)."' AND panel_type = 'A' LIMIT 1");
 		if(dbnumrows($panelquery)) {
 			$panel = dbassoc($panelquery);
 			if((isset($panel['panel_level']) ? $panel['panel_level'] : 0) >= uLEVEL) {
@@ -89,3 +91,4 @@
 	$tpl->printToScreen();
 	dbclose( );
 ?>
+

file:a/browse.php -> file:b/browse.php
--- a/browse.php
+++ b/browse.php
@@ -41,7 +41,7 @@
 	$countquery = array();
 	$scountquery = array();
 	$searchtype = (isset($_REQUEST['searchtype']) ? $_REQUEST['searchtype'] : "simple");
-	$searchterm =  (isset($_REQUEST['searchterm']) ? descript($_REQUEST['searchterm']) : false);
+	$searchterm =  (isset($_REQUEST['searchterm']) ? escapestring(descript($_REQUEST['searchterm'])) : false);
 	$disablesorts = array( );
 	if($searchterm) {
 		if($searchtype == "title") {
@@ -112,6 +112,7 @@
 	}
 	if(!empty($_REQUEST['charlist1']) && !in_array($_REQUEST['charlist1'], $charid)) $charid[] = $_REQUEST['charlist1'];
 	if(!empty($_REQUEST['charlist2']) && !in_array($_REQUEST['charlist2'], $charid)) $charid[] = $_REQUEST['charlist2'];
+	$charid = array_filter($charid, "isNumber");
 	if($charid) {
 		foreach($charid as $c) {
 			if(!empty($c)) {
@@ -126,9 +127,9 @@
 		if(count($charseries) > 0) $scountquery[] = "(".implode(" AND ", $charseries).")";
 	}
 	$rid = array( ); $rating = array( );
-	if(!empty($_REQUEST['rid'])) $rid = is_array($_REQUEST['rid']) ? implode(",", $_REQUEST['rid']) : array($_REQUEST['rid']);
-	if(!empty($_REQUEST['rating'])) $rating = is_array($_REQUEST['rating']) ? implode(",", $_REQUEST['rating']) : array($_REQUEST['rating']);
-	$rid = array_merge($rid, $rating);
+	if(!empty($_REQUEST['rid'])) $rid = is_array($_REQUEST['rid']) ? $_REQUEST['rid'] : explode(",", $_REQUEST['rid']);
+	if(!empty($_REQUEST['rating'])) $rating = is_array($_REQUEST['rating']) ? $_REQUEST['rating'] : explode(",", $_REQUEST['rating']);
+	$rid = array_filter(array_merge($rid, $rating), "isNumber");
 	if(!empty($rid)) {
 		$query[] = "FIND_IN_SET(stories.rid, '".(implode(",", $rid))."') > 0";
 		$countquery[]  = "FIND_IN_SET(stories.rid, '".(implode(",", $rid))."') > 0";
@@ -185,8 +186,8 @@
 	}
 	// Begin story length
 	unset($wordcount);
-	$wordlow = isset($_REQUEST['wordlow']) ? $_REQUEST['wordlow'] : "-500";
-	$wordhigh = isset($_REQUEST['wordhigh']) ? $_REQUEST['wordhigh'] : "1000000";
+	$wordlow = isset($_REQUEST['wordlow']) && isNumber($_REQUEST['wordlow']) ? $_REQUEST['wordlow'] : "-500";
+	$wordhigh = isset($_REQUEST['wordhigh']) && isNumber($_REQUEST['wordhigh']) ? $_REQUEST['wordhigh'] : "1000000";
 	if($wordlow != "-500") $wordcount = "stories.wordcount > ".$wordlow;
 	if($wordhigh != "1000000") 
 	{
@@ -210,11 +211,11 @@
 	$termsList = array();
 	foreach($termArray as $term => $value) {
 		if($term == "submit" || $term == "go" || $term == "offset" || $term == "type" || ($term != "complete" && empty($value))) continue;
-		$termsList[$term] = "$term=".(is_array($value) ? implode(",", $value) : $value);
+		$termsList[$term] = "$term=".(is_array($value) ? implode(",", array_map('htmlspecialchars', $value)) : htmlspecialchars($value, ENT_QUOTES));
 	}
 	$terms = "type=$type".(count($termsList) ? "&amp;" : "").implode("&amp;", $termsList);
 // End query strings
-	$panelquery = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_panels WHERE panel_name = '$type' AND panel_type = 'B' LIMIT 1");
+	$panelquery = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_panels WHERE panel_name = '".escapestring($type)."' AND panel_type = 'B' LIMIT 1");
 	if(dbnumrows($panelquery)) {
 		$numrows = 0;
 		$panel = dbassoc($panelquery);

--- a/includes/get_session_vars.php
+++ b/includes/get_session_vars.php
@@ -25,7 +25,7 @@
 if(!defined("_CHARSET")) exit( );
 // Get session variables from cookie data if not logged in.
 // To bridge to another program replace (or add to) this information with the bridge to your other script.  See examples in the includes/bridges/ folder.
-if (!empty($_COOKIE[$sitekey."_useruid"])) {
+if (!empty($_COOKIE[$sitekey."_useruid"]) && isNumber($_COOKIE[$sitekey."_useruid"])) {
 	$userdata = dbassoc(dbquery("SELECT ap.*, "._UIDFIELD." as uid, "._PENNAMEFIELD." as penname, "._EMAILFIELD." as email, "._PASSWORDFIELD." as password FROM "._AUTHORTABLE." LEFT JOIN ".TABLEPREFIX."fanfiction_authorprefs as ap ON ap.uid = "._UIDFIELD." WHERE "._UIDFIELD." = '".$_COOKIE[$sitekey."_useruid"]."'"));
 	if($userdata && $userdata['level'] != -1 && $_COOKIE[$sitekey.'_salt'] == md5($userdata['email'] . $userdata['password'])) {
 		define("USERUID", $userdata['uid']);
@@ -67,3 +67,4 @@
 if(empty($siteskin)) $siteskin = $defaultskin;
 
 ?>
+

file:a/user.php -> file:b/user.php
--- a/user.php
+++ b/user.php
@@ -56,7 +56,7 @@
 	$output .= "</div>\n";
 }
 else if(!empty($action)) {
-	$panelquery = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_panels WHERE panel_name = '$action' AND (panel_type='U' ".(!$submissionsoff || isADMIN ? " OR panel_type = 'S'" : "").($favorites ? " OR panel_type = 'F'" : "").") LIMIT 1");
+	$panelquery = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_panels WHERE panel_name = '".escapestring($action)."' AND (panel_type='U' ".(!$submissionsoff || isADMIN ? " OR panel_type = 'S'" : "").($favorites ? " OR panel_type = 'F'" : "").") LIMIT 1");
 	if(dbnumrows($panelquery)) {
 		$panel = dbassoc($panelquery);
 		if($panel['panel_level'] > 0 && !isMEMBER) accessDenied( );
@@ -71,3 +71,4 @@
 $tpl->printToScreen();
 dbclose( );
 ?>
+