#8 #5 #4 #3 coauthors cleaning
#8 #5 #4 #3 coauthors cleaning

--- a/includes/deletefunctions.php
+++ b/includes/deletefunctions.php
@@ -47,10 +47,11 @@
 	dbquery("DELETE FROM ".TABLEPREFIX."fanfiction_reviews WHERE item = '$sid' AND type = 'ST'");
 	dbquery("DELETE FROM ".TABLEPREFIX."fanfiction_favorites WHERE item = '$sid' AND type = 'ST'");
 	// get the coauthors list before we delete the coauthors info.
+	$array_coauthors = array();
 	if($story['coauthors'] == 1) {
 		$coauth = dbquery("SELECT uid FROM ".TABLEPREFIX."fanfiction_coauthors WHERE sid = '$sid'");
 		while($c = dbassoc($coauth)) {
-			$coauthors[] = $c['uid'];
+			$array_coauthors[] = $c['uid'];
 			
 		}
 	}	
@@ -70,9 +71,9 @@
 	}
 	if($story['validated'] > 0) {
 		dbquery("UPDATE ".TABLEPREFIX."fanfiction_stats SET stories = stories - 1");
-		if(!empty($coauthors)) {
-			$coauthors[] = $story['uid'];
-			dbquery("UPDATE ".TABLEPREFIX."fanfiction_authorprefs SET stories = stories - 1 WHERE FIND_IN_SET(uid, '".implode(",", $coauthors)."') > 0");
+		if(!empty($array_coauthors)) {
+			$array_coauthors[] = $story['uid'];
+			dbquery("UPDATE ".TABLEPREFIX."fanfiction_authorprefs SET stories = stories - 1 WHERE FIND_IN_SET(uid, '".implode(",", $array_coauthors)."') > 0");
 		}
 		else dbquery("UPDATE ".TABLEPREFIX."fanfiction_authorprefs SET stories = stories - 1 WHERE uid = '".$story['uid']."' LIMIT 1");
 		list($chapters, $words) = dbrow(dbquery("SELECT COUNT(chapid), SUM(wordcount) FROM ".TABLEPREFIX."fanfiction_chapters WHERE validated > 0"));
@@ -96,13 +97,14 @@
 				}
 				else { // Co-authors found...give the story a new author.
 					$cQuery = dbquery("SELECT uid FROM ".TABLEPREFIX."fanfiction_coauthors WHERE sid = '".$story['sid']."' LIMIT 1");
-					$newauthor = 0; $coauthors = array( );
+					$newauthor = 0; $coauthors = 0;
+					$array_coauthors = array();
 					while($cRes= dbassoc($cQuery)) {
 						if(!$newauthor) $newauthor = $cRes['uid'];
-						else $coauthors[] = $cRes['uid'];
+						else $array_coauthors[] = $cRes['uid'];
 					}			
 					if(!empty($newauthor)) {
-						$coauthors = count($coauthors) > 0 ? 1 : 0;
+						$coauthors = count($array_coauthors) > 0 ? 1 : 0;
 						dbquery("UPDATE ".TABLEPREFIX."fanfiction_stories SET uid = '$newauthor', coauthors = '$coauthors' WHERE sid = '".$story['sid']."' LIMIT 1");
 						$chapters = dbquery("SELECT chapid FROM ".TABLEPREFIX."fanfiction_chapters WHERE sid = '".$story['sid']."' AND uid = '$uid'");
 						while($chap = dbassoc($chapters)) {

--- a/includes/storyblock.php
+++ b/includes/storyblock.php
@@ -26,13 +26,13 @@
 	$tpl->assign("sid", $stories['sid']);
 	unset($challengelinks, $challengeadmin, $serieslinks, $categorylinks, $adminlinks, $authlink);
 	if($stories['coauthors'] == 1) {
-		$coauthors = array();
+		$array_coauthors = array();
 		$coauth = dbquery("SELECT "._PENNAMEFIELD." as penname, co.uid FROM ".TABLEPREFIX."fanfiction_coauthors AS co LEFT JOIN "._AUTHORTABLE." ON co.uid = "._UIDFIELD." WHERE co.sid = '".$stories['sid']."'");
 		while($c = dbassoc($coauth)) {
-			$coauthors[$c['uid']] = $c['penname'];
+			$array_coauthors[$c['uid']] = $c['penname'];
 		}
-		$stories['coauthors'] = $coauthors;
-		unset($coauthors);
+		$stories['coauthors'] = $array_coauthors;
+		unset($array_coauthors);
 	}
 	else if(empty($stories['coauthors'])) $stories['coauthors'] = array( );	
 	$tpl->assign("title"   , stripslashes(title_link($stories)) );

--- a/reviews.php
+++ b/reviews.php
@@ -164,10 +164,10 @@
 				$uidquery = dbquery("SELECT uid,title, coauthors FROM ".TABLEPREFIX."fanfiction_stories WHERE sid = '$item'");
 				list($author, $title, $coauthors) = dbrow($uidquery);
 				if($coauthors) {
-					$coauthors = array( );
+					$array_coauthors = array( );
 					$coQuery = dbquery("SELECT uid FROM ".TABLEPREFIX."fanfiction_coauthors WHERE sid = '$item'");
 					while($c = dbassoc($coQuery)) {
-						$coauthors[] = $c['uid'];
+						$array_coauthors[] = $c['uid'];
 					}
 				}
 			}
@@ -186,7 +186,7 @@
 				}
 			}
 			if($review != "No Review" && $action == "add") {
-				$mailquery= dbquery("SELECT ap.newreviews,"._EMAILFIELD." as email, "._PENNAMEFIELD." as penname FROM "._AUTHORTABLE." LEFT JOIN ".TABLEPREFIX."fanfiction_authorprefs as ap ON ap.uid = "._UIDFIELD." WHERE "._UIDFIELD." = '$author' ".(!empty($coauthors) ? " OR ".findclause(_UIDFIELD, $coauthors) : ""));
+				$mailquery= dbquery("SELECT ap.newreviews,"._EMAILFIELD." as email, "._PENNAMEFIELD." as penname FROM "._AUTHORTABLE." LEFT JOIN ".TABLEPREFIX."fanfiction_authorprefs as ap ON ap.uid = "._UIDFIELD." WHERE "._UIDFIELD." = '$author' ".(!empty($coauthors) ? " OR ".findclause(_UIDFIELD, $array_coauthors) : ""));
 				while($mail = dbassoc($mailquery)) {
 					if($mail['newreviews']) {
 						include_once("includes/emailer.php");
@@ -247,10 +247,11 @@
 		$story = dbassoc($storyquery);
 		$title = title_link($story);
 		$authoruid = $story['uid'];
+		$array_coauthors = array();
 		if(!empty($story['coauthors'])) {
 			$colist = dbquery("SELECT uid FROM ".TABLEPREFIX."fanfiction_coauthors WHERE sid = '$item'");
 			while($c = dbassoc($colist)) {
-				$coauthors[] = $c['uid'];
+				$array_coauthors[] = $c['uid'];
 			}
 		}
 	}
@@ -379,7 +380,7 @@
 			$reviewer = $reviews['reviewer'];
 			$member = _ANONYMOUS;
 		}
-		if(empty($reviews['respond']) && (USERUID == $authoruid || (isset($coauthors) && in_array(USERUID, $coauthors)))) $adminlink .= " [<a href=\"user.php?action=revres&amp;reviewid=".$reviews['reviewid']."\">"._RESPOND."</a>]";
+		if(empty($reviews['respond']) && (USERUID == $authoruid || (isset($array_coauthors) && in_array(USERUID, $array_coauthors)))) $adminlink .= " [<a href=\"user.php?action=revres&amp;reviewid=".$reviews['reviewid']."\">"._RESPOND."</a>]";
 		$tpl->newBlock("reviewsblock");
 		$tpl->assign("reviewer"   , $reviewer );
 		$tpl->assign("reportthis", "[<a href=\""._BASEDIR."contact.php?action=report&amp;url=reviews.php?reviewid=".$reviews['reviewid']."\">"._REPORTTHIS."</a>]");

--- a/stories.php
+++ b/stories.php
@@ -48,16 +48,16 @@
 			if($result) list($sid, $author) = dbrow($result);
 		}
 		if(isset($sid)) {
-			$coauthors = array( );
+			$array_coauthors = array( );
 			$authorquery = dbquery("SELECT uid, rr, coauthors FROM ".TABLEPREFIX."fanfiction_stories WHERE sid='$sid' LIMIT 1");
 			$story = dbassoc($authorquery);
 			if($story['coauthors']) {
 				$cQuery = dbquery("SELECT uid FROM ".TABLEPREFIX."fanfiction_coauthors WHERE sid = '$sid'");
 				while($c = dbassoc($cQuery)) {
-					$coauthors[] = $c['uid'];
-				}
-			}
-			if($story['uid'] != USERUID && (is_array($coauthors) && !in_array(USERUID, $coauthors)) && !$story['rr']) accessDenied( );
+					$array_coauthors[] = $c['uid'];
+				}
+			}
+			if($story['uid'] != USERUID && (is_array($array_coauthors) && !in_array(USERUID, $array_coauthors)) && !$story['rr']) accessDenied( );
 		}
 	}
 	else if(isADMIN && uLEVEL < 4 && isset($_GET['admin'])) {
@@ -139,7 +139,7 @@
 	$storynotes = isset($_POST['storynotes']) ? descript(strip_tags($_POST['storynotes'], $allowed_tags)) : "";
 	$catid = isset($_POST['catid']) ? array_filter(explode(",", $_POST['catid']), "isNumber") : array( );
 	$charid = isset($_POST['charid']) ? array_filter($_POST['charid'], "isNumber") : array( );
-	$coauthors = isset($_POST['coauthors']) ? array_filter(explode(",", $_POST['coauthors']), "isNumber") : array( );
+	$array_coauthors = isset($_POST['coauthors']) ? array_filter(explode(",", $_POST['coauthors']), "isNumber") : array( );
 	$classes = array( );
 	$classquery = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_classtypes");
 	while($type = dbassoc($classquery)) {
@@ -178,8 +178,8 @@
 	$words_to_count = trim($words_to_count);
 	$wordcount = count(explode(" ",$words_to_count)); 
 	$au[] = $uid;
-	if(count($coauthors)) {
-		$au = array_merge($au, $coauthors);
+	if(count($array_coauthors)) {
+		$au = array_merge($au, $array_coauthors);
 		$coauthors = 1;
 	}
 	else $coauthors = 0;
@@ -273,7 +273,7 @@
 			else $output .= write_error(_FATALERROR." "._TRYAGAIN);
 		}
 		if(!$newchapter) {
-			if(count($coauthors)) {
+			if(count($array_coauthors)) {
 				foreach($au AS $c) {
 					if($c == $uid) continue;
 					dbquery("INSERT INTO ".TABLEPREFIX."fanfiction_coauthors(`sid`, `uid`) VALUES('$sid', '$c')");
@@ -634,7 +634,7 @@
 		$rid = isset($_POST['rid']) && isNumber($_POST['rid']) ? $_POST['rid'] : 0;
 		$catid = isset($_POST['catid']) ? array_filter(explode(",", $_POST['catid']), "isNumber") : array( );
 		$charid = isset($_POST['charid']) ? array_filter($_POST['charid'], "isNumber") : array( );
-		$coauthors = isset($_POST['coauthors']) ? array_filter(explode(",", $_POST['coauthors']), "isNumber") : array( );
+		$array_coauthors = isset($_POST['coauthors']) ? array_filter(explode(",", $_POST['coauthors']), "isNumber") : array( );
 		if(isset($_POST['uid']) && isNumber($_POST['uid'])) $uid = $_POST['uid'];
 		else $uid = USERUID;
 		$classes = array( );
@@ -647,8 +647,8 @@
 		}
 		if(!$admin && $authorvalid && !$validated) $validated = 2;
 		$au[] = $uid;
-		if(count($coauthors)) {
-			$au = array_merge($au, $coauthors);
+		if(count($array_coauthors)) {
+			$au = array_merge($au, $array_coauthors);
 			$coauthors = 1;
 		}
 		else $coauthors = 0;

--- a/user/profile.php
+++ b/user/profile.php
@@ -35,6 +35,7 @@
 	$fav = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_favorites WHERE uid = '".USERUID."' AND type = 'AU' AND item = '".$userinfo['uid']."'");
 	if(dbnumrows($fav) == 0) $nameinfo .= " [<a href=\"user.php?action=favau&amp;uid=".USERUID."&amp;add=".$userinfo['uid']."\">"._ADDAUTHOR2FAVES."</a>]";
 }
+ 
 $tpl->assign("userpenname", $userinfo['penname']." ".$nameinfo);
 $tpl->assign("membersince", date("$dateformat", $userinfo['date']));
 if($userinfo['realname'])

--- a/user/revres.php
+++ b/user/revres.php
@@ -67,13 +67,14 @@
 			}
 		}
 		$user = dbassoc(dbquery($query2));
+		$array_coauthors = array();
 		if(!empty($user['coauthors'])) {
 			$coQuery = dbquery("SELECT uid FROM ".TABLEPREFIX."fanfiction_coauthors WHERE sid = '".$reviews['item']."'");
 			while($c = dbassoc($coQuery)) {
-				$coauthors[] = $c['uid'];
+				$array_coauthors[] = $c['uid'];
 			}
 		}
-		if(USERUID == $user['uid'] || (!empty($user['coauthors']) && in_array(USERUID, $coauthors))) {
+		if(USERUID == $user['uid'] || (!empty($user['coauthors']) && in_array(USERUID, $array_coauthors))) {
 			if($type == "ST") {
 				$storyquery = dbquery("SELECT s.title, s.uid, r.rid, r.rating, r.ratingwarning, r.warningtext, s.sid FROM ".TABLEPREFIX."fanfiction_stories as s, ".TABLEPREFIX."fanfiction_ratings as r WHERE s.sid = '".$reviews['item']."' AND s.rid = r.rating LIMIT 1");
 				$story = dbassoc($storyquery);

--- a/viewstory.php
+++ b/viewstory.php
@@ -35,13 +35,13 @@
 	$storyquery = dbquery("SELECT "._PENNAMEFIELD." as penname, "._UIDFIELD." as uid, story.*, story.date as date, story.updated as updated, story.validated as valid FROM ".TABLEPREFIX."fanfiction_stories as story, "._AUTHORTABLE." WHERE story.sid = '".$sid."' AND story.uid = "._UIDFIELD);
 	$storyinfo = dbassoc($storyquery);
 	if($storyinfo['coauthors'] == 1) {
-		$coauthors = array();
+	$array_coauthors = array();
 		$coauth = dbquery("SELECT "._PENNAMEFIELD." as penname, co.uid FROM ".TABLEPREFIX."fanfiction_coauthors AS co LEFT JOIN "._AUTHORTABLE." ON co.uid = "._UIDFIELD." WHERE co.sid = '".$sid."'");
 		while($c = dbassoc($coauth)) {
-			$coauthors[$c['uid']] = $c['penname'];
-		}
-		$storyinfo['coauthors'] = $coauthors;
-		unset($coauthors);
+		$array_coauthors[$c['uid']] = $c['penname'];
+		}
+		$storyinfo['coauthors'] = $array_coauthors;
+		unset($array_coauthors);
 	}
 	else $storyinfo['coauthors'] = array();
 	//  Check that the story is valid and that the visitor has permissions to read this story