Browse code

Security patches from upstream - and general cleanup of old hacks

Clarissa Walker authored on 2026/10/31 20:10:45
Showing 16 changed files
... ...
@@ -40,6 +40,25 @@ else include_once("languages/en_admin.php");
40 40
 
41 41
 // check that user has permissions to perform this action before going further.  Otherwise kick 'em
42 42
 	if(!isADMIN) accessDenied( );
43
+	// The admin panels put these values straight into SQL. They are all ids, so anything
44
+	// else (other than the few words the panels use) is refused here once for all panels.
45
+	$ef_idkeys = array('uid', 'sid', 'rid', 'chapid', 'catid', 'parentcatid', 'charid', 'class', 'type_id', 'subof', 'field', 'displayorder',
46
+		'after', 'orderafter', 'revoke', 'lock', 'unlock', 'validate', 'release', 'retire', 'remove', 'feature', 'level', 'adlevel', 'category',
47
+		'cat', 'delete', 'edit', 'warning', 'reviewid', 'seriesid', 'inorder', 'message_id', 'link_id', 'nid', 'cid', 'panel_level');
48
+	// "type" is used by admin/classifications.php (delete=type)
49
+	$ef_words = array('', 'new', 'class', 'type', 'yes', 'no', 'all', '-1', 'on');
50
+	// these are posted as text (category name in admin/categories.php, warning text in admin/warnings.php)
51
+	$ef_posttext = array('category', 'warning');
52
+	foreach(array('_GET' => $_GET, '_POST' => $_POST) as $ef_src => $ef_vals) {
53
+		foreach($ef_idkeys as $ef_k) {
54
+			if(!isset($ef_vals[$ef_k])) continue;
55
+			if($ef_src == '_POST' && in_array($ef_k, $ef_posttext, true)) continue;
56
+			foreach((array)$ef_vals[$ef_k] as $ef_v) {
57
+				if(is_array($ef_v) || (!in_array((string)$ef_v, $ef_words, true) && !preg_match('/^-?[0-9]+(,-?[0-9]+)*$/', (string)$ef_v))) accessDenied(_ERROR." (".$ef_k.")");
58
+			}
59
+		}
60
+	}
61
+	unset($ef_idkeys, $ef_words, $ef_posttext, $ef_src, $ef_vals, $ef_k, $ef_v);
43 62
 	$adminquery = dbquery("SELECT categories FROM ".TABLEPREFIX."fanfiction_authorprefs WHERE uid = '".USERUID."' LIMIT 1");
44 63
 	list($admincats) = dbrow($adminquery);
45 64
 	if(empty($admincats)) $admincats = "0";
... ...
@@ -32,6 +32,8 @@ if(isset($_POST['cast_vote'])) {
32 32
 	$poll = isset($_POST['poll_id']) && isNumber($_POST['poll_id']) ? $_POST['poll_id'] : false;
33 33
 	$opt = isset($_POST['opt']) && isNumber($_POST['opt']) ? $_POST['opt'] : false;
34 34
 	if(!$poll || !$opt) accessDenied( ); // could only be a hack attempt.
35
+	if(!isMEMBER || !$currentpoll || (int)$poll !== (int)$currentpoll['poll_id'] || (int)$opt < 1 || (int)$opt > count(explode("|#|", $currentpoll['poll_opts']))) accessDenied( );
36
+	$poll = (int)$poll; $opt = (int)$opt;
35 37
 	$voted = dbquery("SELECT vote_id FROM ".TABLEPREFIX."fanfiction_poll_votes WHERE vote_poll = '".$currentpoll['poll_id']."' AND vote_user = '".USERUID."'");
36 38
 	list($voter) = dbrow($voted);
37 39
         if(!$voter) $cast = dbquery("INSERT INTO ".TABLEPREFIX."fanfiction_poll_votes(`vote_user`, `vote_opt`, `vote_poll`) VALUES('".USERUID."', '$opt', '$poll')");
... ...
@@ -78,4 +80,4 @@ else  {
78 80
 	  }
79 81
 }
80 82
 	$content .= "<div style='text-align: center;'><a href='"._BASEDIR."blocks/poll/pollarchive.php'>"._POLLARCHIVE."</a></div>";
81
-?>
82 83
\ No newline at end of file
84
+?>
... ...
@@ -8,11 +8,12 @@ if(isset($_POST['shouthidden']) && (isMEMBER || !empty($blocks['shoutbox']['gues
8 8
 	http_response_code(202);
9 9
 	if(isMEMBER) $shout_name = USERUID;
10 10
 	else {
11
-		$shout_name = trim(descript($_POST['shoutname']));
12
-		if(isNumber($shout_name)) $shout_name = _GUEST;
11
+		$shout_name = isset($_POST['shoutname']) && is_string($_POST['shoutname']) ? trim(strip_tags($_POST['shoutname'])) : "";
12
+		$shout_name = function_exists('mb_substr') ? mb_substr($shout_name, 0, 30) : substr($shout_name, 0, 30);
13
+		if($shout_name === "" || isNumber($shout_name)) $shout_name = _GUEST;
13 14
 		if($captcha && !captcha_confirm()) $shout_name = false;
14 15
 	}
15
-	$shout_message = trim($_POST['shout_message']);
16
+	$shout_message = isset($_POST['shout_message']) && is_string($_POST['shout_message']) ? trim($_POST['shout_message']) : "";
16 17
 	$shout_message = preg_replace("/^(.{200}).*$/", "$1", $shout_message);
17 18
 	$shout_message = preg_replace("/[^\s]{25}/", "$1\n", $shout_message);
18 19
 	$search = array("\"", "'", "\\", '\"', "\'", "<", ">", "&nbsp;", "\n");
... ...
@@ -20,7 +21,7 @@ if(isset($_POST['shouthidden']) && (isMEMBER || !empty($blocks['shoutbox']['gues
20 21
 	$shout_message = str_replace($search, $replace, replace_naughty(trim($shout_message)));
21 22
 	$shout_message = str_replace("\n", "<br />", $shout_message);
22 23
 	if(!empty($shout_name) && !empty($shout_message)) {
23
-		dbquery("INSERT INTO ".TABLEPREFIX."fanfiction_shoutbox(`shout_name`, `shout_message`, `shout_datestamp`) VALUES('$shout_name', '$shout_message', '".time()."')");
24
+		dbquery("INSERT INTO ".TABLEPREFIX."fanfiction_shoutbox(`shout_name`, `shout_message`, `shout_datestamp`) VALUES('".escapestring($shout_name)."', '".escapestring($shout_message)."', '".time()."')");
24 25
 	}
25 26
 
26 27
 }
... ...
@@ -28,7 +28,7 @@ $tpl->assignInclude( "header", "$skindir/header.tpl" );
28 28
 $tpl->assignInclude( "footer", "$skindir/footer.tpl" );
29 29
 include(_BASEDIR."includes/pagesetup.php");
30 30
 include_once(_BASEDIR."languages/".$language."_admin.php");
31
-if(!isADMIN) accessDenied( );       
31
+if(!isADMIN || uLEVEL > 1) accessDenied( );     
32 32
 $confirm = isset($_GET['confirm']) ? $_GET['confirm'] : false;
33 33
 if($confirm == "yes") {
34 34
 	list($installed) = dbrow(dbquery("SELECT COUNT(code_module) FROM ".TABLEPREFIX."fanfiction_codeblocks WHERE code_module = 'betareader'"));
... ...
@@ -48,4 +48,4 @@ else {
48 48
 }
49 49
 $tpl->assign("output", $output);
50 50
 $tpl->printToScreen( );
51
-?>
52 51
\ No newline at end of file
52
+?>
... ...
@@ -28,7 +28,7 @@ $tpl->assignInclude( "header", "$skindir/header.tpl" );
28 28
 $tpl->assignInclude( "footer", "$skindir/footer.tpl" );
29 29
 include(_BASEDIR."includes/pagesetup.php");
30 30
 include_once(_BASEDIR."languages/".$language."_admin.php");
31
-if(!isADMIN) accessDenied( );
31
+if(!isADMIN || uLEVEL > 1) accessDenied( );
32 32
 $confirm = isset($_GET['confirm']) ? $_GET['confirm'] : false;
33 33
 if($confirm == "yes") {
34 34
 	include("version.php");
... ...
@@ -46,4 +46,4 @@ else {
46 46
 }
47 47
 $tpl->assign("output", $output);
48 48
 $tpl->printToScreen( );
49
-?>
50 49
\ No newline at end of file
50
+?>
... ...
@@ -34,7 +34,7 @@ if(file_exists(_BASEDIR."languages/".$language."_admin.php")) include_once(_BASE
34 34
 else include_once(_BASEDIR."languages/en_admin.php");
35 35
 if(file_exists(_BASEDIR."languages/".$language.".php")) include_once(_BASEDIR."languages/".$language.".php");
36 36
 else include_once(_BASEDIR."languages/en.php");
37
-if(!isADMIN) accessDenied( );
37
+if(!isADMIN || uLEVEL > 1) accessDenied( );
38 38
 $confirm = isset($_GET['confirm']) ? $_GET['confirm'] : false;
39 39
 include("version.php");
40 40
 list($currentVersion) = dbrow(dbquery("SELECT version FROM ".TABLEPREFIX."fanfiction_modules WHERE name = '$moduleName' LIMIT 1"));
... ...
@@ -55,4 +55,4 @@ else {
55 55
 else $output .= write_message(_ALREADYUPDATED);
56 56
 $tpl->assign("output", $output);
57 57
 $tpl->printToScreen( );
58
-?>
59 58
\ No newline at end of file
59
+?>
... ...
@@ -34,8 +34,8 @@ $tpl->assignInclude( "header", "$skindir/header.tpl" );
34 34
 $tpl->assignInclude( "footer", "$skindir/footer.tpl" );
35 35
 include(_BASEDIR."includes/pagesetup.php");
36 36
 
37
-$chalid = isset($_GET['chalid']) ? $_GET['chalid'] : false;
38
-if($chalid && !isNumber($chalid)) unset($chalid, $action);
37
+$chalid = isset($_GET['chalid']) && is_string($_GET['chalid']) && isNumber($_GET['chalid']) ? (int)$_GET['chalid'] : false;
38
+if(isset($_GET['chalid']) && !$chalid) { unset($chalid); $action = false; }
39 39
 
40 40
 // security check
41 41
 	$admin = 0;
... ...
@@ -47,7 +47,7 @@ if($chalid && !isNumber($chalid)) unset($chalid, $action);
47 47
 			$challenge = dbquery("SELECT uid, catid from ".TABLEPREFIX."fanfiction_challenges WHERE chalid='$chalid' LIMIT 1");
48 48
 			list($chaluid, $catid) = dbrow($challenge);
49 49
 			if(uLEVEL == 3 && $admincats != 0) {
50
-				$seriescats = explode(",", $catid);
50
+				$seriescats = explode(",", $catid ?? "");
51 51
 				$adcats = explode(",", $admincats);
52 52
 				foreach($seriescats as $cat) {
53 53
 					if(in_array($cat, $adcats)) $admin = 1;
... ...
@@ -56,6 +56,21 @@ if($chalid && !isNumber($chalid)) unset($chalid, $action);
56 56
 			if($chaluid != USERUID &&  $admin != 1 && uLEVEL > 2) accessDenied( );
57 57
 		}
58 58
 	}
59
+	// Editing and deleting: only the challenge's owner or an admin. Removing a response:
60
+	// also the author of that story or series. (Before 3.5.78 the check above only
61
+	// covered level-3 admins, so any member could edit, delete or strip any challenge.)
62
+	if(in_array($action, array("edit", "delete", "remove"), true)) {
63
+		if(empty($chalid)) accessDenied( );
64
+		$ef_chal = dbrow(dbquery("SELECT uid FROM ".TABLEPREFIX."fanfiction_challenges WHERE chalid = '".(int)$chalid."' LIMIT 1"));
65
+		if(!$ef_chal) accessDenied( );
66
+		$ef_ok = $admin || (USERUID && (int)$ef_chal[0] === (int)USERUID);
67
+		if(!$ef_ok && $action == "remove" && USERUID) {
68
+			// same order as the removal below: a series when one is named, otherwise the story
69
+			if(isset($seriesid)) $ef_ok = !empty($seriesid) && (bool)dbrow(dbquery("SELECT seriesid FROM ".TABLEPREFIX."fanfiction_series WHERE seriesid = '".(int)$seriesid."' AND uid = '".(int)USERUID."' LIMIT 1"));
70
+			else if(!empty($sid)) $ef_ok = (bool)dbrow(dbquery("SELECT s.sid FROM ".TABLEPREFIX."fanfiction_stories AS s LEFT JOIN ".TABLEPREFIX."fanfiction_coauthors AS c ON c.sid = s.sid AND c.uid = '".(int)USERUID."' WHERE s.sid = '".(int)$sid."' AND (s.uid = '".(int)USERUID."' OR c.uid IS NOT NULL) LIMIT 1"));
71
+		}
72
+		if(!$ef_ok) accessDenied( );
73
+	}
59 74
 
60 75
 // end security check
61 76
 
... ...
@@ -68,16 +83,16 @@ if($action == "remove") {
68 83
 		if(isset($seriesid)) {
69 84
 			$chalinfo =dbquery("SELECT challenges FROM ".TABLEPREFIX."fanfiction_series WHERE seriesid = '$seriesid'");
70 85
 			list($challenges) = dbrow($chalinfo);
71
-			$challenges = explode(",", $challenges);
72
-			$result = dbquery("UPDATE ".TABLEPREFIX."fanfiction_series SET challenges = '".array_diff($challenges, explode(",", $chalid))."' WHERE seriesid = '$seriesid'");
86
+			$challenges = explode(",", $challenges ?? "");
87
+			$result = dbquery("UPDATE ".TABLEPREFIX."fanfiction_series SET challenges = '".implode(",", array_diff($challenges, array((string)$chalid)))."' WHERE seriesid = '".(int)$seriesid."'");
73 88
 		}
74 89
 		else {
75 90
 			$chalinfo = dbquery("SELECT challenges FROM ".TABLEPREFIX."fanfiction_stories WHERE sid = '$sid'");
76 91
 			list($challenges) = dbrow($chalinfo);
77
-			$challenges = explode(",", $challenges);
92
+			$challenges = explode(",", $challenges ?? "");
78 93
 			$result = dbquery("UPDATE ".TABLEPREFIX."fanfiction_stories SET challenges = '".implode(",", array_diff($challenges, explode(",", $chalid)))."' WHERE sid = '$sid'");
79 94
 		}
80
-		if($result) dbquery("UPDATE ".TABLEPREFIX."fanfiction_challenges SET responses = responses - 1 WHERE chalid = '$chalid'");
95
+		if($result && $GLOBALS['dbconnect']->affected_rows > 0) dbquery("UPDATE ".TABLEPREFIX."fanfiction_challenges SET responses = GREATEST(responses - 1, 0) WHERE chalid = '".(int)$chalid."'");
81 96
 		$output .= write_message(_ACTIONSUCCESSFUL);
82 97
 	}
83 98
 }
... ...
@@ -90,15 +105,15 @@ else if($action == "add" || $action == "edit") {
90 105
 
91 106
 	$output .= "<div id=\"pagetitle\">".($action == "edit" ? _EDITCHALLENGE : _ADDCHALLENGE)."</div>";
92 107
 	if(isset($_POST['submit'])) {
93
-		$title = escapestring(descript($_POST['title'], $allowed_tags));
94
-		$summary = escapestring(replace_naughty(strip_tags(descript($_POST['summary']), $allowed_tags)));
95
-		$challenger = escapestring(strip_tags(descript($_POST['challenger']), $allowed_tags));
108
+		$title = escapestring(descript($_POST['title'] ?? '', $allowed_tags));
109
+		$summary = escapestring(replace_naughty(strip_tags(descript($_POST['summary'] ?? ''), $allowed_tags)));
110
+		$challenger = escapestring(strip_tags(descript($_POST['challenger'] ?? ''), $allowed_tags));
96 111
 		if(!$challenger) $challenger = _ANONYMOUS;
97 112
 		$challengeruid = isset($_POST['challengeruid']) && isNumber($_POST['challengeruid']) ? $_POST['challengeruid'] : 0;
98
-		$catid = isset($_POST['catid']) ? array_filter(explode(",", $_POST['catid']), "isNumber") : array( );
113
+		$catid = isset($_POST['catid']) ? array_filter(is_array($_POST['catid']) ? $_POST['catid'] : explode(",", $_POST['catid']), "isNumber") : array( );
99 114
 		$category = implode(",", $catid);
100 115
 		if(!$category) $category = -1;
101
-		$characters = isset($_POST['charid']) ? array_filter($_POST['charid'], "isNumber") : array( );
116
+		$characters = isset($_POST['charid']) ? array_filter((array)$_POST['charid'], "isNumber") : array( );
102 117
 		$charid = implode(",", $characters);
103 118
 		if(empty($title) || empty($summary)) {
104 119
 			$output .= write_error(_REQUIREDINFO);
... ...
@@ -188,10 +203,13 @@ else if($action == "delete") {
188 203
 			dbquery("DELETE FROM ".TABLEPREFIX."fanfiction_challenges WHERE chalid = '$chalid'");
189 204
 			$responses = dbquery("SELECT challenges, sid FROM  ".TABLEPREFIX."fanfiction_stories WHERE FIND_IN_SET($chalid, challenges) > 0");
190 205
 			while($response = dbassoc($responses)) {
191
-				$challenges = explode(",", $response[challenges]);
192
-				if(is_array($challenges)) 
193
-					foreach($challenges as $key=>$value) { if($value == $chalid) unset($challenges[$key]); }
194
-				dbquery("UPDATE  ".TABLEPREFIX."fanfiction_stories SET challenges = '".implode(",", $challenges)."' WHERE sid = $response[sid] LIMIT 1");
206
+				$challenges = array_diff(explode(",", $response['challenges']), array((string)$chalid));
207
+				dbquery("UPDATE  ".TABLEPREFIX."fanfiction_stories SET challenges = '".escapestring(implode(",", $challenges))."' WHERE sid = '".(int)$response['sid']."' LIMIT 1");
208
+			}
209
+			$responses = dbquery("SELECT challenges, seriesid FROM  ".TABLEPREFIX."fanfiction_series WHERE FIND_IN_SET($chalid, challenges) > 0");
210
+			while($response = dbassoc($responses)) {
211
+				$challenges = array_diff(explode(",", $response['challenges']), array((string)$chalid));
212
+				dbquery("UPDATE  ".TABLEPREFIX."fanfiction_series SET challenges = '".escapestring(implode(",", $challenges))."' WHERE seriesid = '".(int)$response['seriesid']."' LIMIT 1");
195 213
 			}
196 214
 			$output .=  write_message(_ACTIONSUCCESSFUL."  <a href=\"challenges.php\">"._BACK2PREVIOUS."</a>");
197 215
 		}
... ...
@@ -279,9 +297,9 @@ else {
279 297
 
280 298
 		$challenge = dbquery("SELECT * FROM  ".TABLEPREFIX."fanfiction_challenges WHERE chalid = '$chalid' LIMIT 1");
281 299
 		list($chalid, $challenger, $uid, $title, $cat, $chars, $summary, $responses) = dbrow($challenge);
282
-		$challenger = stripslashes($challenger);
283
-		$title = stripslashes($title);
284
-		$summary= stripslashes($summary);
300
+		$challenger = stripslashes($challenger ?? "");
301
+		$title = stripslashes($title ?? "");
302
+		$summary= stripslashes($summary ?? "");
285 303
 
286 304
 		$challenges->newBlock("titleblock");
287 305
 		if(isADMIN || USERUID == $uid) $challenges->assign("adminoptions", "<div class=\"adminoptions\">".($admin ? _ADMINOPTIONS : _OPTIONS).": [<a href=\"challenges.php?action=edit&amp;chalid=$chalid\">"._EDIT."</a>] [<a href=\"challenges.php?action=delete&amp;chalid=$chalid\">"._DELETE."</a>]</div>");
... ...
@@ -11,7 +11,7 @@ include(_BASEDIR."includes/pagesetup.php");
11 11
 include_once(_BASEDIR."languages/".$language."_admin.php");
12 12
 if(file_exists("languages/".$language.".php")) include_once("languages/".$language.".php");
13 13
 else include_once("languages/en.php");
14
-if(!isADMIN) accessDenied( );
14
+if(!isADMIN || uLEVEL > 1) accessDenied( );
15 15
 $confirm = isset($_GET['confirm']) ? $_GET['confirm'] : false;
16 16
 $table = dbassoc(dbquery("SHOW TABLES LIKE '".TABLEPREFIX."challenges'"));
17 17
 if($table) $output .= write_message(_ALREADYINSTALLED);
... ...
@@ -76,4 +76,4 @@ else {
76 76
 }
77 77
 $tpl->assign("output", $output);
78 78
 $tpl->printToScreen( );
79
-?>
80 79
\ No newline at end of file
80
+?>
... ...
@@ -51,8 +51,9 @@ if(is_array($catid) && count($catid) > 0) {
51 51
 	if(!empty($cats)) $chalquery[] = "(".implode(" OR ", $cats).")";
52 52
 }
53 53
 if($searchterm) {
54
-	if($searchtype == "title") $chalquery[] = "title LIKE '%$searchterm%'";
55
-	if($searchtype == "summary") $chalquery[] = "summary LIKE '%$searchterm%'";
54
+	$ef_st = escapestring(str_replace(array('\\', '%', '_'), array('\\\\', '\\%', '\\_'), stripslashes($searchterm)));
55
+	if($searchtype == "title") $chalquery[] = "title LIKE '%$ef_st%'";
56
+	if($searchtype == "summary") $chalquery[] = "summary LIKE '%$ef_st%'";
56 57
 }
57 58
 if(count($chalquery) > 0) {
58 59
 	$chalquery = "WHERE ".implode(" AND ", $chalquery);
... ...
@@ -63,4 +64,4 @@ if(count($chalquery) > 0) {
63 64
 }
64 65
 	unset($where, $query, $chars, $cats);
65 66
 }
66
-?>
67 67
\ No newline at end of file
68
+?>
... ...
@@ -9,7 +9,7 @@ $tpl->assignInclude( "header", "$skindir/header.tpl" );
9 9
 $tpl->assignInclude( "footer", "$skindir/footer.tpl" );
10 10
 include(_BASEDIR."includes/pagesetup.php");
11 11
 include_once(_BASEDIR."languages/".$language."_admin.php");
12
-if(!isADMIN) accessDenied( );
12
+if(!isADMIN || uLEVEL > 1) accessDenied( );
13 13
 $confirm = isset($_GET['confirm']) ? $_GET['confirm'] : false;
14 14
 if($confirm == "yes") {
15 15
 	dbquery("DELETE FROM `".TABLEPREFIX."fanfiction_codeblocks` WHERE code_module = 'challenges'");
... ...
@@ -33,4 +33,4 @@ else {
33 33
 }
34 34
 $tpl->assign("output", $output);
35 35
 $tpl->printToScreen( );
36
-?>
37 36
\ No newline at end of file
37
+?>
... ...
@@ -11,7 +11,7 @@ include(_BASEDIR."includes/pagesetup.php");
11 11
 include_once(_BASEDIR."languages/".$language."_admin.php");
12 12
 if(file_exists("languages/".$language.".php")) include_once("languages/".$language.".php");
13 13
 else include_once("languages/en.php");
14
-if(!isADMIN) accessDenied( );
14
+if(!isADMIN || uLEVEL > 1) accessDenied( );
15 15
 $confirm = isset($_GET['confirm']) ? $_GET['confirm'] : false;
16 16
 include("version.php");
17 17
 list($currentVersion) = dbrow(dbquery("SELECT version FROM ".TABLEPREFIX."fanfiction_modules WHERE name = 'Challenges' LIMIT 1"));
... ...
@@ -42,4 +42,4 @@ else {
42 42
 else $output .= write_message(_ALREADYUPDATED);
43 43
 $tpl->assign("output", $output);
44 44
 $tpl->printToScreen( );
45
-?>
46 45
\ No newline at end of file
46
+?>
... ...
@@ -28,7 +28,7 @@ $tpl->assignInclude( "header", "$skindir/header.tpl" );
28 28
 $tpl->assignInclude( "footer", "$skindir/footer.tpl" );
29 29
 include(_BASEDIR."includes/pagesetup.php");
30 30
 include_once(_BASEDIR."languages/".$language."_admin.php");
31
-if(!isADMIN) accessDenied( );       
31
+if(!isADMIN || uLEVEL > 1) accessDenied( );     
32 32
 $confirm = isset($_GET['confirm']) ? $_GET['confirm'] : false;
33 33
 if($confirm == "yes") {
34 34
 	list($installed) = dbrow(dbquery("SELECT COUNT(name) FROM ".TABLEPREFIX. "fanfiction_modules WHERE name = 'notifications'"));
... ...
@@ -28,7 +28,7 @@ $tpl->assignInclude( "header", "$skindir/header.tpl" );
28 28
 $tpl->assignInclude( "footer", "$skindir/footer.tpl" );
29 29
 include(_BASEDIR."includes/pagesetup.php");
30 30
 include_once(_BASEDIR."languages/".$language."_admin.php");
31
-if(!isADMIN) accessDenied( );
31
+if(!isADMIN || uLEVEL > 1) accessDenied( );
32 32
 $confirm = isset($_GET['confirm']) ? $_GET['confirm'] : false;
33 33
 if($confirm == "yes") {
34 34
 	include("version.php");
... ...
@@ -51,16 +51,18 @@ if ($nid < 1) {
51 51
 if($nid) {
52 52
 
53 53
 	$output .= "<div id=\"pagetitle\">"._NEWS."</div>";
54
-	if(isset($_POST['submit']))
54
+	if(isset($_POST['submit']) && isset($_POST['comment']) && is_string($_POST['comment']))
55 55
 	{
56 56
 		$comment = escapestring(format_story(replace_naughty(strip_tags($_POST['comment'], $allowed_tags))));
57
-		if(!$cid && USERUID) {
58
-			$insert = dbquery("INSERT INTO ".TABLEPREFIX."fanfiction_comments (nid, uid, comment, time) VALUES ('$nid', '".(USERUID ? USERUID : 0)."', '$comment', '" . time() . "')");
57
+		$ef_news_ok = (bool)dbrow(dbquery("SELECT nid FROM ".TABLEPREFIX."fanfiction_news WHERE nid = '$nid' LIMIT 1"));
58
+		if(!$cid && USERUID && $newscomments && $ef_news_ok && trim($comment) !== "") {
59
+			$insert = dbquery("INSERT INTO ".TABLEPREFIX."fanfiction_comments (nid, uid, comment, time) VALUES ('$nid', '".(int)USERUID."', '$comment', now())");
59 60
 			if($insert) dbquery("UPDATE ".TABLEPREFIX."fanfiction_news SET comments = comments + 1 WHERE nid = '$nid' LIMIT 1");
60 61
 		}
61
-		else if($cid) {
62
-			dbquery("UPDATE ".TABLEPREFIX."fanfiction_comments SET comment = '$comment' WHERE cid = '$cid'");
62
+		else if($cid && isADMIN && uLEVEL < 4) {
63
+			dbquery("UPDATE ".TABLEPREFIX."fanfiction_comments SET comment = '$comment' WHERE cid = '$cid' AND nid = '$nid'");
63 64
 		}
65
+		else accessDenied( );
64 66
 		unset($comment);
65 67
 	}
66 68
 	if(isset($_GET['del']) && isADMIN && uLEVEL < 4 && !empty($cid)) {
... ...
@@ -38,6 +38,15 @@ $tpl->prepare();
38 38
 
39 39
 include("includes/pagesetup.php");
40 40
 
41
+function ef_review_story_ok($sid) {
42
+	$st = dbassoc(dbquery("SELECT uid, validated FROM ".TABLEPREFIX."fanfiction_stories WHERE sid = '".(int)$sid."' LIMIT 1"));
43
+	if(!$st) return false;
44
+	if($st['validated'] > 0 || isADMIN) return true;
45
+	if(!USERUID) return false;
46
+	if((int)$st['uid'] === (int)USERUID) return true;
47
+	return (bool)dbrow(dbquery("SELECT uid FROM ".TABLEPREFIX."fanfiction_coauthors WHERE sid = '".(int)$sid."' AND uid = '".(int)USERUID."' LIMIT 1"));
48
+}
49
+
41 50
 $reviewid = isset($_REQUEST['reviewid']) ? $_REQUEST['reviewid'] : false;
42 51
 if(!isNumber($reviewid)) unset($reviewid);
43 52
 $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : false;
... ...
@@ -59,6 +68,7 @@ if($action == "delete") {
59 68
 	}
60 69
 	$authorquery = dbquery($query2);
61 70
 	list($author) = dbrow($authorquery);
71
+	if(empty($reviewid) || $reviewuid === null) accessDenied( );
62 72
 	if($author != USERUID && !$notauthor && ($revdelete < 2 || ($revdelete == 1 && $reviewuid))) accessDenied( );
63 73
 	$delete = isset($_GET['delete']) ? $_GET['delete'] : false;
64 74
 	$output .= "<div id=\"pagetitle\">"._DELETEREVIEW."</div>";
... ...
@@ -124,9 +134,24 @@ if($action == "delete") {
124 134
 else if($action == "edit" || $action == "add") {
125 135
 	if(isset($_GET['next']) && isNumber($_GET['next'])) $nextchapter = $_GET['next'];
126 136
 	if(!isMEMBER && !$anonreviews) accessDenied( );
127
-	if(!isset($chapid) && $type == "ST") { 
128
-		$chapquery = dbquery("SELECT chapid FROM ".TABLEPREFIX."fanfiction_chapters WHERE sid = '$item' AND inorder = '1' LIMIT 1");
129
-		list($chapid) = dbrow($chapquery);
137
+	if($action == "edit") {
138
+		if(empty($reviewid)) accessDenied( );
139
+		$ef_r = dbassoc(dbquery("SELECT uid FROM ".TABLEPREFIX."fanfiction_reviews WHERE reviewid = '".(int)$reviewid."' LIMIT 1"));
140
+		if(!$ef_r || !((isADMIN && uLEVEL < 4) || (USERUID && (int)$ef_r['uid'] === (int)USERUID))) accessDenied( );
141
+	}
142
+	if($action == "add") {
143
+		if(!$reviewsallowed) accessDenied( );
144
+		if($type == "ST") {
145
+			if(empty($item) || !ef_review_story_ok($item)) accessDenied( );
146
+			$ef_ch = isset($_POST['chapid']) && is_string($_POST['chapid']) && isNumber($_POST['chapid']) ? (int)$_POST['chapid'] : (isset($chapid) ? (int)$chapid : 0);
147
+			$chapid = 0;
148
+			if($ef_ch) list($chapid) = array_pad((array)dbrow(dbquery("SELECT chapid FROM ".TABLEPREFIX."fanfiction_chapters WHERE chapid = '$ef_ch' AND sid = '".(int)$item."' AND validated > 0 LIMIT 1")), 1, 0);
149
+			if(!$chapid) list($chapid) = array_pad((array)dbrow(dbquery("SELECT chapid FROM ".TABLEPREFIX."fanfiction_chapters WHERE sid = '".(int)$item."' AND validated > 0 ORDER BY inorder LIMIT 1")), 1, 0);
150
+			$chapid = (int)$chapid;
151
+		}
152
+		else if($type == "SE") {
153
+			if(empty($item) || !dbrow(dbquery("SELECT seriesid FROM ".TABLEPREFIX."fanfiction_series WHERE seriesid = '".(int)$item."' LIMIT 1"))) accessDenied( );
154
+		}
130 155
 	}
131 156
 	$tpl->assign("pagetitle", "<div id =\"pagetitle\">"._REVIEW."</div>");
132 157
 	if(isset($_POST['submit'])) {
... ...
@@ -289,11 +314,11 @@ else {
289 314
 		list($type, $item) = dbrow(dbquery("SELECT type, item FROM ".TABLEPREFIX."fanfiction_reviews WHERE reviewid = '$reviewid' LIMIT 1"));
290 315
 	}
291 316
 	if($type == "ST") {
292
-		$storyquery = dbquery("SELECT s.title, s.uid, s.rid, s.sid, s.coauthors FROM ".TABLEPREFIX."fanfiction_stories as s WHERE s.sid = '".(isset($item) ? $item : null)."' LIMIT 1");
317
+		if(empty($item) || !ef_review_story_ok($item)) accessDenied( );
318
+		$storyquery = dbquery("SELECT s.title, s.uid, s.rid, s.sid, s.coauthors FROM ".TABLEPREFIX."fanfiction_stories as s WHERE s.sid = '$item' LIMIT 1");
293 319
 		$story = dbassoc($storyquery);
294 320
 		$title = title_link($story);
295
-		$authoruid = (isset($story['uid']) ? $story['uid'] : null);
296
-		if(empty($story)) accessDenied();
321
+		$authoruid = $story['uid'];
297 322
 		if(!empty($story['coauthors'])) {
298 323
 			$colist = dbquery("SELECT uid FROM ".TABLEPREFIX."fanfiction_coauthors WHERE sid = '$item'");
299 324
 			while($c = dbassoc($colist)) {
... ...
@@ -302,17 +327,18 @@ else {
302 327
 		}
303 328
 	}
304 329
 	else if($type == "SE") {
305
-		$storyquery = dbquery("SELECT title, uid FROM ".TABLEPREFIX."fanfiction_series WHERE seriesid = '".(isset($item) ? $item : null)."' LIMIT 1");
330
+		$if(empty($item)) accessDenied( );
331
+		$storyquery = dbquery("SELECT title, uid FROM ".TABLEPREFIX."fanfiction_series WHERE seriesid = '$item' LIMIT 1");
306 332
 		list($title, $authoruid) = dbrow($storyquery);
307 333
 		$titletext = $title;
308
-		$title = "<a href=\"series.php?seriesid=".(isset($item) ? $item : null)."\">".stripslashes($title)."</a>";
309
-		if(empty($title && $authoruid)) accessDenied();
334
+		$title = "<a href=\"series.php?seriesid='$item'\">".stripslashes($title)."</a>";
310 335
 	}
311 336
 	else { 
312 337
 		$titlequery = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_codeblocks WHERE code_type = 'revtitle'");
313 338
 		while($code = dbassoc($titlequery)) {
314 339
 			eval($code['code_text']);
315 340
 		}
341
+		if(!isset($title)) accessDenied( );
316 342
 	}
317 343
 	$tpl->assign("pagetitle", "<div id=\"pagetitle\">"._REVIEWSFOR." $title</div>");
318 344
 	if($type == "SE") {
... ...
@@ -46,8 +46,13 @@ function random_string ($charset_string, $length)
46 46
 	$uid = isset($_REQUEST['uid']) ? $_REQUEST['uid'] : false;
47 47
 	if(!$uid) $uid = USERUID;
48 48
 
49
-	if($uid && !isNumber($uid)) accessDenied( );
50
-	if((!isADMIN || uLEVEL > 2) && $uid != USERUID && $action == "editbio") accessDenied( );
49
+	if($action == "editbio" && $uid != USERUID && (!isADMIN || uLEVEL > 2)) {
50
+		$output .= write_error(_NOTAUTHORIZED);
51
+		$tpl->assign("output", $output);
52
+		$tpl->printToScreen( );
53
+		dbclose( );
54
+		exit( );
55
+	}
51 56
 	if(isMEMBER) $output .= "<div id=\"pagetitle\">"._EDITPERSONAL."</div>";
52 57
 	else $output .= "<div id=\"pagetitle\">"._NEWACCOUNT."</div>";
53 58
 	if(!empty($_POST['submit'])) {