Browse code

More coauthors fixes

Clarissa Walker authored on 2026/09/05 13:44:38
Showing 1 changed files
... ...
@@ -48,16 +48,16 @@ include("includes/storyform.php");
48 48
 			if($result) list($sid, $author) = dbrow($result);
49 49
 		}
50 50
 		if(isset($sid)) {
51
-			$array_coauthors = array( );
51
+			$coauthors = array( );
52 52
 			$authorquery = dbquery("SELECT uid, rr, coauthors FROM ".TABLEPREFIX."fanfiction_stories WHERE sid='$sid' LIMIT 1");
53 53
 			$story = dbassoc($authorquery);
54 54
 			if($story['coauthors']) {
55 55
 				$cQuery = dbquery("SELECT uid FROM ".TABLEPREFIX."fanfiction_coauthors WHERE sid = '$sid'");
56 56
 				while($c = dbassoc($cQuery)) {
57
-					$array_coauthors[] = $c['uid'];
57
+					$coauthors[] = $c['uid'];
58 58
 				}
59 59
 			}
60
-			if($story['uid'] != USERUID && (is_array($array_coauthors) && !in_array(USERUID, $array_coauthors)) && !$story['rr']) accessDenied( );
60
+			if($story['uid'] != USERUID && (is_array($coauthors) && !in_array(USERUID, $coauthors)) && !$story['rr']) accessDenied( );
61 61
 		}
62 62
 	}
63 63
 	else if(isADMIN && uLEVEL < 4 && isset($_GET['admin'])) {
... ...
@@ -139,7 +139,7 @@ function newstory( ) {
139 139
 	$storynotes = isset($_POST['storynotes']) ? descript(strip_tags($_POST['storynotes'], $allowed_tags)) : "";
140 140
 	$catid = isset($_POST['catid']) ? array_filter(explode(",", $_POST['catid']), "isNumber") : array( );
141 141
 	$charid = isset($_POST['charid']) ? array_filter($_POST['charid'], "isNumber") : array( );
142
-	$array_coauthors = isset($_POST['coauthors']) ? array_filter(explode(",", $_POST['coauthors']), "isNumber") : array( );
142
+	$coauthors = isset($_POST['coauthors']) ? array_filter(explode(",", $_POST['coauthors']), "isNumber") : array( );
143 143
 	$classes = array( );
144 144
 	$classquery = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_classtypes");
145 145
 	while($type = dbassoc($classquery)) {
... ...
@@ -178,8 +178,8 @@ function newstory( ) {
178 178
 	$words_to_count = trim($words_to_count);
179 179
 	$wordcount = count(explode(" ",$words_to_count)); 
180 180
 	$au[] = $uid;
181
-	if(count($array_coauthors)) {
182
-		$au = array_merge($au, $array_coauthors);
181
+	if(count($coauthors)) {
182
+		$au = array_merge($au, $coauthors);
183 183
 		$coauthors = 1;
184 184
 	}
185 185
 	else $coauthors = 0;
... ...
@@ -273,7 +273,7 @@ function newstory( ) {
273 273
 			else $output .= write_error(_FATALERROR." "._TRYAGAIN);
274 274
 		}
275 275
 		if(!$newchapter) {
276
-			if(count($array_coauthors)) {
276
+			if(!is_array($coauthors)) {
277 277
 				foreach($au AS $c) {
278 278
 					if($c == $uid) continue;
279 279
 					dbquery("INSERT INTO ".TABLEPREFIX."fanfiction_coauthors(`sid`, `uid`) VALUES('$sid', '$c')");
... ...
@@ -635,7 +635,7 @@ function editstory($sid) {
635 635
 		$rid = isset($_POST['rid']) && isNumber($_POST['rid']) ? $_POST['rid'] : 0;
636 636
 		$catid = isset($_POST['catid']) ? array_filter(explode(",", $_POST['catid']), "isNumber") : array( );
637 637
 		$charid = isset($_POST['charid']) ? array_filter($_POST['charid'], "isNumber") : array( );
638
-		$array_coauthors = isset($_POST['coauthors']) ? array_filter(explode(",", $_POST['coauthors']), "isNumber") : array( );
638
+		$coauthors = isset($_POST['coauthors']) ? array_filter(explode(",", $_POST['coauthors']), "isNumber") : array( );
639 639
 		if(isset($_POST['uid']) && isNumber($_POST['uid'])) $uid = $_POST['uid'];
640 640
 		else $uid = USERUID;
641 641
 		$classes = array( );
... ...
@@ -648,8 +648,8 @@ function editstory($sid) {
648 648
 		}
649 649
 		if(!$admin && $authorvalid && !$validated) $validated = 2;
650 650
 		$au[] = $uid;
651
-		if(count($array_coauthors)) {
652
-			$au = array_merge($au, $array_coauthors);
651
+		if(count($coauthors)) {
652
+			$au = array_merge($au, $coauthors);
653 653
 			$coauthors = 1;
654 654
 		}
655 655
 		else $coauthors = 0;
Browse code

Make storiesphp and series.php layout appear more consistent - both are now at 75% width

Clarissa Walker authored on 2026/07/26 18:50:12
Showing 1 changed files
... ...
@@ -413,7 +413,7 @@ function newstory( ) {
413 413
 
414 414
 	$submit = isset($_POST['submit']) ? $_POST['submit'] : false;
415 415
 	if(!$submit) $submit = _PREVIEW;
416
-	$output .= "<div class=\"tblborder\" style=\" width: 45%; padding: 10px; margin: 1em auto;\">
416
+	$output .= "<div class=\"tblborder\" style=\" width: 75%; padding: 10px; margin: 1em auto;\">
417 417
 	<form METHOD=\"POST\" name=\"form\" enctype=\"multipart/form-data\" action='stories.php?action=$action".($newchapter ? "&amp;sid=$sid&amp;inorder=$inorder" : "").($admin == 1 ? "&amp;admin=1&amp;uid=$uid" : "")."'>";
418 418
 	if(!$newchapter) $output .= storyform($stories, $submit);
419 419
 	$output .= chapterform($inorder, $notes, $endnotes, $storytext, $chaptertitle, $uid);
Browse code

Make adding stories UI look less jank

Clarissa Walker authored on 2026/07/26 18:14:05
Showing 1 changed files
... ...
@@ -413,11 +413,11 @@ function newstory( ) {
413 413
 
414 414
 	$submit = isset($_POST['submit']) ? $_POST['submit'] : false;
415 415
 	if(!$submit) $submit = _PREVIEW;
416
-	$output .= "<div class=\"tblborder\" style=\" padding: 10px; margin: 1em auto;\">
416
+	$output .= "<div class=\"tblborder\" style=\" width: 45%; padding: 10px; margin: 1em auto;\">
417 417
 	<form METHOD=\"POST\" name=\"form\" enctype=\"multipart/form-data\" action='stories.php?action=$action".($newchapter ? "&amp;sid=$sid&amp;inorder=$inorder" : "").($admin == 1 ? "&amp;admin=1&amp;uid=$uid" : "")."'>";
418 418
 	if(!$newchapter) $output .= storyform($stories, $submit);
419 419
 	$output .= chapterform($inorder, $notes, $endnotes, $storytext, $chaptertitle, $uid);
420
-	$output .= "<div style=\"text-align: center;\"><input type=\"submit\" class=\"button\" value=\""._PREVIEW."\" name=\"submit\">&nbsp; <input type=\"submit\" class=\"button\" 
420
+	$output .= "<br><div style=\"text-align: center;\"><input type=\"submit\" class=\"button\" value=\""._PREVIEW."\" name=\"submit\">&nbsp; <input type=\"submit\" class=\"button\"
421 421
                  value=\""._ADDSTORY."\" name=\"submit\"></div></form></div>";
422 422
 	return $output;
423 423
 }
Browse code

3.5.8.1 warnings, dbconnect, install changes

Jimako authored on 2024/05/09 06:30:39
Showing 1 changed files
... ...
@@ -624,7 +624,7 @@ function editstory($sid) {
624 624
 	if(isset($_POST['submit'])) {
625 625
 		$title = isset($_POST['title']) ? strip_tags(descript($_POST['title']), $allowed_tags) : "";
626 626
 		$summary = isset($_POST['summary']) ? strip_tags(descript($_POST['summary']), $allowed_tags) : "";
627
-		$storynotes = strip_tags(descript($_POST['storynotes']), $allowed_tags);
627
+		$storynotes = isset($_POST['storynotes']) ?  strip_tags(descript($_POST['storynotes']), $allowed_tags) : "";
628 628
 		$rr = isset($_POST['rr']) && isNumber($_POST['rr']) ? $_POST['rr'] : 0;
629 629
 		$feat = isset($_POST['feature']) && isNumber($_POST['feature']) ? $_POST['feature'] : 0;
630 630
 		$complete = isset($_POST['complete']) && isNumber($_POST['complete']) ? $_POST['complete'] : 0;
Browse code

version 3.5.7

Jimako authored on 2024/04/08 12:21:57
Showing 1 changed files
... ...
@@ -281,6 +281,7 @@ function newstory( ) {
281 281
 			}
282 282
 			$codequery = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_codeblocks WHERE code_type = 'addstory'");
283 283
 			while($code = dbassoc($codequery)) {
284
+				
284 285
 				eval($code['code_text']);
285 286
 			}
286 287
 		}
... ...
@@ -412,7 +413,7 @@ function newstory( ) {
412 413
 
413 414
 	$submit = isset($_POST['submit']) ? $_POST['submit'] : false;
414 415
 	if(!$submit) $submit = _PREVIEW;
415
-	$output .= "<div class=\"tblborder\" style=\"width: 500px; padding: 10px; margin: 1em auto;\">
416
+	$output .= "<div class=\"tblborder\" style=\" padding: 10px; margin: 1em auto;\">
416 417
 	<form METHOD=\"POST\" name=\"form\" enctype=\"multipart/form-data\" action='stories.php?action=$action".($newchapter ? "&amp;sid=$sid&amp;inorder=$inorder" : "").($admin == 1 ? "&amp;admin=1&amp;uid=$uid" : "")."'>";
417 418
 	if(!$newchapter) $output .= storyform($stories, $submit);
418 419
 	$output .= chapterform($inorder, $notes, $endnotes, $storytext, $chaptertitle, $uid);
... ...
@@ -558,7 +559,7 @@ function editchapter( $chapid ) {
558 559
 			if($admin && $logging && USERUID != $uid) {
559 560
 				$storyinfo = dbquery("SELECT story.title, story.sid, chapter.uid, chapter.inorder, "._PENNAMEFIELD." as penname FROM ".TABLEPREFIX."fanfiction_stories as story, ".TABLEPREFIX."fanfiction_chapters as chapter, "._AUTHORTABLE." WHERE "._UIDFIELD." = chapter.uid AND story.sid = chapter.sid AND chapter.chapid = $chapid");
560 561
 				list($title, $sid, $chapuid, $inorder, $chappenname) = dbrow($storyinfo);
561
-				dbquery("INSERT INTO ".TABLEPREFIX."fanfiction_log (`log_action`, `log_uid`, `log_ip`, `log_type`, `log_timestamp`) VALUES('".escapestring(sprintf(_LOG_ADMIN_EDIT_CHAPTER, USERPENNAME, USERUID, $title, $sid, $chappenname, $chapuid, $inorder))."', '".USERUID."', INET_ATON('".$_SERVER['REMOTE_ADDR']."'), 'ED', " . time() . ")");
562
+				dbquery("INSERT INTO ".TABLEPREFIX."fanfiction_log (`log_action`, `log_uid`, `log_ip`, `log_type`, `log_timestamp`) VALUES('".escapestring(sprintf(_LOG_ADMIN_EDIT_CHAPTER, USERPENNAME, USERUID, $title, $sid, $chappenname, $chapuid, $inorder))."', '".USERUID. "', INET6_ATON('".$_SERVER['REMOTE_ADDR']."'), 'ED', " . time() . ")");
562 563
 			}
563 564
 			unset($_POST['submit']);
564 565
 			$output = write_message(_STORYUPDATED).editstory($sid);
... ...
@@ -568,7 +569,7 @@ function editchapter( $chapid ) {
568 569
 			exit( );
569 570
 		}
570 571
 	}
571
-	$output .= "<div class=\"tblborder\" style=\"width: 550px; margin: 0 auto; padding: 5px;\">
572
+	$output .= "<div class=\"tblborder\" style=\"margin: 0 auto; padding: 5px;\">
572 573
 		<form METHOD=\"POST\"  enctype=\"multipart/form-data\" name=\"form\" action=\"stories.php?action=editchapter&amp;chapid=$chapid".($admin ? "&amp;admin=1" : "")."\">";
573 574
 	if(!isset($_POST['submit']) || $_POST['submit'] != _PREVIEW) {
574 575
 		$storyquery = dbquery("SELECT title, inorder, notes, endnotes, uid, storytext, sid FROM ".TABLEPREFIX."fanfiction_chapters WHERE chapid = '$chapid' LIMIT 1");
... ...
@@ -698,7 +699,7 @@ function editstory($sid) {
698 699
 						list($oldpenname) = dbrow($authorquery);
699 700
 						$author2query = dbquery("SELECT "._PENNAMEFIELD." as penname FROM "._AUTHORTABLE." WHERE "._UIDFIELD." = '$uid' LIMIT 1");
700 701
 						list($newpenname) = dbrow($author2query);
701
-						dbquery("INSERT INTO ".TABLEPREFIX."fanfiction_log (`log_action`, `log_uid`, `log_ip`, `log_type`, `log_timestamp`) VALUES('".escapestring(sprintf(_LOG_ADMIN_EDIT_AUTHOR, USERPENNAME, USERUID, $title, $sid, $newpenname, $uid, $oldpenname, $olduid))."', '".USERUID."', INET_ATON('".$_SERVER['REMOTE_ADDR']."'), 'ED', " . time() . ")");
702
+						dbquery("INSERT INTO ".TABLEPREFIX."fanfiction_log (`log_action`, `log_uid`, `log_ip`, `log_type`, `log_timestamp`) VALUES('".escapestring(sprintf(_LOG_ADMIN_EDIT_AUTHOR, USERPENNAME, USERUID, $title, $sid, $newpenname, $uid, $oldpenname, $olduid))."', '".USERUID. "', INET6_ATON('".$_SERVER['REMOTE_ADDR']."'), 'ED', " . time() . ")");
702 703
 					}
703 704
 				}
704 705
 
... ...
@@ -812,14 +813,16 @@ function editstory($sid) {
812 813
 				}		
813 814
 			}
814 815
 			$codequery = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_codeblocks WHERE code_type = 'editstory'");
816
+
815 817
 			while($code = dbassoc($codequery)) {
818
+			 
816 819
 				eval($code['code_text']);
817 820
 			}
818 821
 			if($logging && $admin) {
819 822
 				if(USERUID != $uid) { // If you're editing your own story, don't log it.
820 823
 					$authorquery = dbquery("SELECT "._PENNAMEFIELD." as penname FROM "._AUTHORTABLE." WHERE "._UIDFIELD." = '$uid' LIMIT 1");
821 824
 					list($penname) = dbrow($authorquery);
822
-					dbquery("INSERT INTO ".TABLEPREFIX."fanfiction_log (`log_action`, `log_uid`, `log_ip`, `log_type`, `log_timestamp`) VALUES('".escapestring(sprintf(_LOG_ADMIN_EDIT, USERPENNAME, USERUID, $title, $sid, $penname, $uid))."', '".USERUID."', INET_ATON('".$_SERVER['REMOTE_ADDR']."'), 'ED', " . time() . ")");
825
+					dbquery("INSERT INTO ".TABLEPREFIX."fanfiction_log (`log_action`, `log_uid`, `log_ip`, `log_type`, `log_timestamp`) VALUES('".escapestring(sprintf(_LOG_ADMIN_EDIT, USERPENNAME, USERUID, $title, $sid, $penname, $uid))."', '".USERUID. "', INET6_ATON('".$_SERVER['REMOTE_ADDR']."'), 'ED', " . time() . ")");
823 826
 				}
824 827
 			}
825 828
 			$output .= write_message(_STORYUPDATED."  ".($admin ? _BACK2ADMIN : _BACK2ACCT));
... ...
@@ -831,7 +834,7 @@ function editstory($sid) {
831 834
 	}
832 835
 	$query = dbquery("SELECT DATE_FORMAT(FROM_UNIXTIME(date), '$dateformat') as date, wordcount, uid FROM ".TABLEPREFIX."fanfiction_stories WHERE sid = '$sid' LIMIT 1");
833 836
 	list($published, $wordcount, $storyuid) = dbrow($query);
834
-	$formbegin = "<div class=\"tblborder\" style=\"margin: 10px auto; width: 550px; padding: 10px;\">
837
+	$formbegin = "<div class=\"tblborder\" style=\"margin: 10px auto; padding: 10px;\">
835 838
 		<form METHOD=\"POST\" name=\"form\" action=\"stories.php?action=editstory".($admin ? "&amp;admin=1" : "")."&amp;sid=$sid\">";
836 839
 
837 840
 	if(isset($_POST['submit']) && $_POST['submit'] != _PREVIEW) {
... ...
@@ -875,7 +878,7 @@ function editstory($sid) {
875 878
 	$chapquery = dbquery("SELECT chapid, title, inorder, rating, reviews, validated, uid FROM ".TABLEPREFIX."fanfiction_chapters WHERE sid = '$sid' ORDER BY inorder");
876 879
 	$chapters = dbnumrows($chapquery);
877 880
 	$output .= "<p><input type=\"submit\" class=\"button\" value=\"$submit\" name=\"submit\">&nbsp; <input type=\"submit\" class=\"button\" value=\""._ADDSTORY."\" name=\"submit\"></p></form></div>";
878
-	$output .= "<br><table class=\"tblborder\" style=\"margin: 0 auto; width: 500px;\"><tr><th>"._CHAPTER."</th>".($chapters > 1 ? "<th>"._MOVE."</th>" : "")."<th>"._OPTIONS."</th></tr>";
881
+	$output .= "<br><table class=\"tblborder\" style=\"margin: 0 auto;  \"><tr><th>"._CHAPTER."</th>".($chapters > 1 ? "<th>"._MOVE."</th>" : "")."<th>"._OPTIONS."</th></tr>";
879 882
 			while($chapter = dbassoc($chapquery)) {
880 883
 				$output .="<tr><td class=\"tblborder\"><a href=\"viewstory.php?sid=$sid&amp;chapter=".$chapter['inorder']."\">".$chapter['title']."</a></td>";
881 884
 				if($chapters > 1) $output .= "<td align=\"center\" class=\"tblborder\">".($chapter['inorder'] == 1 ? "" : "<a href=\"stories.php?action=viewstories&amp;go=up&amp;sid=$sid&amp;chapid=".$chapter['chapid']."&amp;inorder=".$chapter['inorder']."\">$up</a>").
... ...
@@ -931,7 +934,7 @@ function delete( ) {
931 934
 			if($logging && $admin) {
932 935
 				$authorquery = dbquery("SELECT "._PENNAMEFIELD." as penname FROM "._AUTHORTABLE." WHERE "._UIDFIELD." = '$uid' LIMIT 1");
933 936
 				list($penname) = dbrow($authorquery);
934
-				dbquery("INSERT INTO ".TABLEPREFIX."fanfiction_log (`log_action`, `log_uid`, `log_ip`, `log_type`, `log_timestamp`) VALUES('".escapestring(sprintf(_LOG_ADMIN_DEL_CHAPTER, USERPENNAME, USERUID, $story['title'], $sid, $penname, $uid, $inorder))."', '".USERUID."', INET_ATON('".$_SERVER['REMOTE_ADDR']."'), 'DL', " . time() . ")");
937
+				dbquery("INSERT INTO ".TABLEPREFIX."fanfiction_log (`log_action`, `log_uid`, `log_ip`, `log_type`, `log_timestamp`) VALUES('".escapestring(sprintf(_LOG_ADMIN_DEL_CHAPTER, USERPENNAME, USERUID, $story['title'], $sid, $penname, $uid, $inorder))."', '".USERUID. "', INET6_ATON('".$_SERVER['REMOTE_ADDR']."'), 'DL', " . time() . ")");
935 938
 			}
936 939
 			return "<center>"._ACTIONSUCCESSFUL."</center>".editstory( $sid );
937 940
 		}
... ...
@@ -982,4 +985,4 @@ switch($action) {
982 985
 	$tpl->assign( "output", $output );
983 986
 	$tpl->printToScreen();
984 987
 	dbclose( );
985
-?>
986 988
\ No newline at end of file
989
+?>
Browse code

3.5.6 efiction version

Jimako authored on 2024/03/09 16:09:42
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,985 @@
1
+<?php
2
+// ----------------------------------------------------------------------
3
+// Copyright (c) 2007 by Tammy Keefer
4
+// Based on eFiction 1.1
5
+// Copyright (C) 2003 by Rebecca Smallwood.
6
+// http://efiction.sourceforge.net/
7
+// ----------------------------------------------------------------------
8
+// LICENSE
9
+//
10
+// This program is free software; you can redistribute it and/or
11
+// modify it under the terms of the GNU General Public License (GPL)
12
+// as published by the Free Software Foundation; either version 2
13
+// of the License, or (at your option) any later version.
14
+//
15
+// This program is distributed in the hope that it will be useful,
16
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+// GNU General Public License for more details.
19
+//
20
+// To read the license please visit http://www.gnu.org/copyleft/gpl.html
21
+// ----------------------------------------------------------------------
22
+
23
+// page template setup
24
+
25
+$current = "stories";
26
+
27
+if(isset($_GET['action'])) {
28
+	if($_GET['action'] != "newchapter") $displayform = 1;
29
+	if($_GET['action'] == "newstory" || $_GET['action'] == "editstory") $current = "addstory";
30
+}
31
+
32
+include ("header.php");
33
+
34
+$tpl = new TemplatePower( file_exists("$skindir/default.tpl") ?  "$skindir/default.tpl" : "default_tpls/default.tpl");
35
+$tpl->assignInclude( "header", "./$skindir/header.tpl" );
36
+$tpl->assignInclude( "footer", "./$skindir/footer.tpl" );
37
+
38
+include("includes/pagesetup.php");
39
+include("includes/storyform.php");
40
+
41
+
42
+// before doing anything else check if the visitor is logged in.  If they are, check if they're an admin.  If not, check that they're 
43
+// trying to edit/delete/etc. their own stuff then get the penname 
44
+	if(!isMEMBER || ($submissionsoff && !isADMIN) || (!isADMIN && isset($uid))) accessDenied( );
45
+	if(!isADMIN || uLEVEL > 3) {
46
+		if(isset($chapid)) {
47
+			$result = dbquery("SELECT sid, uid FROM ".TABLEPREFIX."fanfiction_chapters WHERE chapid='$chapid' LIMIT 1");
48
+			if($result) list($sid, $author) = dbrow($result);
49
+		}
50
+		if(isset($sid)) {
51
+			$array_coauthors = array( );
52
+			$authorquery = dbquery("SELECT uid, rr, coauthors FROM ".TABLEPREFIX."fanfiction_stories WHERE sid='$sid' LIMIT 1");
53
+			$story = dbassoc($authorquery);
54
+			if($story['coauthors']) {
55
+				$cQuery = dbquery("SELECT uid FROM ".TABLEPREFIX."fanfiction_coauthors WHERE sid = '$sid'");
56
+				while($c = dbassoc($cQuery)) {
57
+					$array_coauthors[] = $c['uid'];
58
+				}
59
+			}
60
+			if($story['uid'] != USERUID && (is_array($array_coauthors) && !in_array(USERUID, $array_coauthors)) && !$story['rr']) accessDenied( );
61
+		}
62
+	}
63
+	else if(isADMIN && uLEVEL < 4 && isset($_GET['admin'])) {
64
+		$admin = 1;
65
+		$uid = isset($_GET['uid']) && isNumber($_GET['uid']) ? $_GET['uid'] : USERUID;
66
+	}
67
+	else {
68
+		$admin = 0;
69
+		$uid = USERUID;
70
+	}
71
+
72
+function preview_story($stories) {
73
+	global $current, $new, $extendcats, $skindir, $catlist, $charlist, $classlist, $featured, $retired, $rr, $reviewsallowed, $star, $halfstar, $classtypelist, $dateformat, $ratingslist, $recentdays;
74
+
75
+	$count = 0;
76
+	if(file_exists("$skindir/listings.tpl")) $tpl = new TemplatePower( "$skindir/listings.tpl" );
77
+	else $tpl = new TemplatePower("default_tpls/listings.tpl");
78
+	if(is_array($stories['coauthors']) && count($stories['coauthors']) > 0) $stories['coauthors'] = 1;
79
+	$tpl->prepare( );
80
+	$tpl->newBlock("listings");
81
+	$tpl->newBlock("storyblock");
82
+	$tpl->assignGlobal("skindir", $skindir);
83
+	include("includes/storyblock.php");
84
+	$text = $tpl->getOutputContent( );
85
+	$count = 0;
86
+	if(!empty($stories['storytext'])) {
87
+		$text .= "<br /><br />";
88
+		if(isset($_GET['textsize'])) $textsize = $_GET['textsize'];
89
+		else $textsize = 0;
90
+		
91
+		if(file_exists("./$skindir/viewstory.tpl")) $tpl = new TemplatePower("./$skindir/viewstory.tpl");
92
+		else $tpl = new TemplatePower(_BASEDIR."default_tpls/viewstory.tpl");
93
+		$tpl->prepare( );			
94
+		include("includes/storyblock.php");
95
+		$tpl->assign("adminlinks", $adminlinks);
96
+		if($stories['inorder'] == 1 && !empty($stories['storynotes'])) {
97
+			$tpl->gotoBlock("_ROOT");
98
+			$tpl->newBlock("storynotes");
99
+			$tpl->assign( "storynotes", stripslashes($stories['storynotes']));
100
+			$tpl->gotoBlock("_ROOT");
101
+		}
102
+		if(!empty($stories['notes'])) {
103
+			$tpl->newBlock("notes");
104
+			$tpl->assign( "notes", $stories['notes']);
105
+			$tpl->gotoBlock("_ROOT");
106
+		}
107
+		if(!empty($stories['endnotes'])) {
108
+			$tpl->newBlock("endnotes");
109
+			$tpl->assign( "endnotes", $stories['endnotes']);
110
+			$tpl->gotoBlock("_ROOT");
111
+		}
112
+		$tpl->gotoBlock("_ROOT");
113
+		$tpl->assign("chaptertitle", $stories['chaptertitle']);
114
+		$tpl->assign("chapternumber", $stories['inorder']);
115
+		$tpl->assign( "story", "<span style=\"font-size: ".(100 + ($textsize * 20))."%;\">".format_story($stories['storytext'])."</span>" );
116
+		$text .= $tpl->getOutputContent( );
117
+	}
118
+	return $text;
119
+}
120
+
121
+// function to add new story to archives.
122
+function newstory( ) {
123
+
124
+	global $autovalidate, $sid, $action, $sid, $store, $tpl, $admin, $sitename, $siteemail, $allowed_tags, $admincats, $alertson, $dateformat, $url, $minwords, $maxwords, $charlist, $catlist, $classtypelist;
125
+	$newchapter = $action == "newchapter";
126
+	$output = "<div id=\"pagetitle\">".($newchapter ? _ADDNEWCHAPTER : _ADDNEWSTORY)."</div>";
127
+// to avoid problems with register globals and hackers declare variables and do some clean up.
128
+	if(isset($admin) && isset($_POST['uid']) && isNumber($_POST['uid'])) {
129
+		$uid = $_POST['uid'];
130
+		$author = dbquery("SELECT "._PENNAMEFIELD." FROM "._AUTHORTABLE." WHERE "._UIDFIELD." = '$uid' LIMIT 1");
131
+		list($penname) = dbrow($author);
132
+	}
133
+	else {
134
+		$uid = USERUID;
135
+		$penname = USERPENNAME;
136
+	}
137
+	$title = isset($_POST['title']) ? descript(strip_tags($_POST['title'], $allowed_tags)) : "";
138
+	$summary = isset($_POST['summary']) ? replace_naughty(descript(strip_tags($_POST['summary'], $allowed_tags))) : "";
139
+	$storynotes = isset($_POST['storynotes']) ? descript(strip_tags($_POST['storynotes'], $allowed_tags)) : "";
140
+	$catid = isset($_POST['catid']) ? array_filter(explode(",", $_POST['catid']), "isNumber") : array( );
141
+	$charid = isset($_POST['charid']) ? array_filter($_POST['charid'], "isNumber") : array( );
142
+	$array_coauthors = isset($_POST['coauthors']) ? array_filter(explode(",", $_POST['coauthors']), "isNumber") : array( );
143
+	$classes = array( );
144
+	$classquery = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_classtypes");
145
+	while($type = dbassoc($classquery)) {
146
+		if(isset($_POST["class_".$type['classtype_id']])) {
147
+			$opts = is_array($_POST["class_".$type['classtype_id']]) ? array_filter($_POST["class_".$type['classtype_id']], "isNumber") : "";
148
+			$classes = array_merge($opts, $classes);
149
+		}
150
+	}
151
+	$rid = isset($_POST['rid']) ? descript($_POST['rid']) : "";
152
+	$rr = isset($_POST['rr']) && isNumber($_POST['rr']) ? $_POST['rr'] : 0;
153
+	$feat = isset($_POST['feature']) && isNumber($_POST['feature']) ? $_POST['feature'] : 0;
154
+	$complete = isset($_POST['complete']) && isNumber($_POST['complete']) ? $_POST['complete'] : 0;
155
+	$validated = isset($_POST['validated']) && isNumber($_POST['validated']) ? $_POST['validated'] : 0;
156
+	$chaptertitle = isset($_POST['chaptertitle']) ? descript(strip_tags($_POST['chaptertitle'], $allowed_tags)) : "";
157
+	$notes = isset($_POST['notes']) ? strip_tags(descript($_POST['notes']), $allowed_tags) : "";
158
+	$endnotes = isset($_POST['endnotes']) ? strip_tags(descript($_POST['endnotes']), $allowed_tags) : "";
159
+	$story = "";
160
+	if(isset($_FILES['storyfile']['name']) && $_FILES['storyfile']['name']) {
161
+		if ($_FILES['storyfile']['type'] != 'text/html' && $_FILES['storyfile']['type'] != 'text/plain') {
162
+ 			$failed = _INVALIDUPLOAD;
163
+			$submit = _PREVIEW;
164
+		}
165
+		else {
166
+			$texts = file($_FILES['storyfile']['tmp_name']);
167
+			foreach ($texts as $text) {
168
+				if($_FILES['storyfile']['type'] == 'text/html') $story .= rtrim($text, "\n\r\t")." ";
169
+				else $story .= $text;
170
+			}
171
+		}
172
+	}
173
+	else if(isset($_POST['storytext'])) $story = $_POST['storytext'];
174
+	$storytext = descript(strip_tags($story, $allowed_tags));
175
+	$words_to_count = strip_tags($storytext);
176
+	$pattern = "/[^(\w|\d|\'|\"|\.|\!|\?|;|,|\\|\/|\-\-|:|\&|@)]+/";
177
+	$words_to_count = preg_replace ($pattern, " ", $words_to_count);
178
+	$words_to_count = trim($words_to_count);
179
+	$wordcount = count(explode(" ",$words_to_count)); 
180
+	$au[] = $uid;
181
+	if(count($array_coauthors)) {
182
+		$au = array_merge($au, $array_coauthors);
183
+		$coauthors = 1;
184
+	}
185
+	else $coauthors = 0;
186
+// end variable declarations
187
+
188
+	if(isset($_POST['submit']) && $_POST['submit'] == _ADDSTORY && ((!$newchapter && (!$rid || !$title || !$summary || !$catid) || $storytext == "")))
189
+			$submit = _PREVIEW;
190
+	if (isset($_POST['submit'])) {
191
+		if(empty($failed)) $failed = "";
192
+		if(!$storytext) $failed .= "<br />"._NOSTORYTEXT;
193
+		if(!$newchapter && ($rid == "" || $title == "" || $summary == "" || !$catid)) $failed .= "<br />". _MISSINGFIELDS;
194
+		if(find_naughty($title)) $failed .= "<br />"._NAUGHTYWORDS;
195
+		if(($minwords && $wordcount < $minwords) || ($maxwords && $wordcount > $maxwords)) $failed .= "<br />"._WORDCOUNTFAILED;
196
+		$storyvalid = 0;
197
+		if($newchapter) {
198
+			$story = dbquery("SELECT sid, catid, title, summary, validated FROM ".TABLEPREFIX."fanfiction_stories WHERE sid = '$sid'");
199
+			if(!dbnumrows($story)) $failed .= "<br />"._ERROR;
200
+			else {
201
+				list($sid, $categories, $storytitle, $summary, $storyvalid) = dbrow($story);
202
+				$catid = explode(",", $categories);
203
+			}
204
+		}
205
+		if(!empty($failed)) {
206
+			$output .= write_error($failed);
207
+			$submit = _PREVIEW;
208
+		}
209
+	}
210
+	if(isset($_POST['submit']) && $_POST['submit'] == _ADDSTORY && !isset($submit)) 
211
+	{
212
+
213
+		$result = dbquery("SELECT "._UIDFIELD." as uid, "._PENNAMEFIELD." as penname, "._EMAILFIELD." as email, validated FROM "._AUTHORTABLE.", ".TABLEPREFIX."fanfiction_authorprefs as ap WHERE "._UIDFIELD." = '$uid' AND ap.uid = "._UIDFIELD." LIMIT 1");
214
+		$user = dbassoc($result);
215
+		$authorpenname = $user['penname'];
216
+		if(!$validated && (($autovalidate && !isADMIN) || $user['validated'] || $storyvalid == 2)) $validated = 1;
217
+		else if(!$validated) $validated = 0;
218
+		if($admin && USERUID != $uid) {
219
+			if($admincats && !sizeof(array_intersect( $catid, explode(",", $admincats)))) {
220
+				$output .= write_error(_NOTAUTHORIZEDADMIN."  "._TRYAGAIN);
221
+				return $output;
222
+			}
223
+		}
224
+		if($store == "mysql")
225
+		{
226
+			if(!$newchapter) {
227
+				$insert = dbquery("INSERT INTO ".TABLEPREFIX."fanfiction_stories (title, summary, storynotes, catid, classes, charid,  rid, date, updated, uid, validated, rr, completed, wordcount, featured, coauthors) VALUES ('".addslashes($title)."', '".addslashes(format_story($summary))."', '".addslashes(format_story($storynotes))."', '".($catid ? implode(",", $catid) : "")."', '".($classes? implode(",", $classes) : "")."', '".($charid ? implode(",", $charid) : "")."', '$rid', '" . time() ."', '" . time() . "', '$uid', '$validated', '$rr', '$complete', '$wordcount', '$feat', '$coauthors')");
228
+				$sid = dbinsertid( );
229
+				$inorder = 1;
230
+			}
231
+			else {
232
+				$inorder = $_GET['inorder'] + 1;
233
+			}
234
+			$query2 = dbquery("INSERT INTO ".TABLEPREFIX."fanfiction_chapters (title, inorder, notes, endnotes, validated, wordcount, sid, uid, storytext) VALUES('".addslashes(($chaptertitle != "" ? $chaptertitle : $title))."', '$inorder', '".addslashes(format_story($notes))."', '".addslashes(format_story($endnotes))."', '$validated', '$wordcount', '$sid', '$uid', '".addslashes($storytext)."')");
235
+			if(!$admin) $output = write_message(_STORYADDED).viewstories( );
236
+			else $output .= write_message(_ACTIONSUCCESSFUL).editstory( $sid );
237
+		}
238
+		else if ($store == "files")
239
+		{
240
+			if(!$newchapter) {
241
+				$insertstory = dbquery("INSERT INTO ".TABLEPREFIX."fanfiction_stories (title, summary, storynotes, catid, classes, charid, rid, date, updated, uid, validated, rr, completed, wordcount, featured, coauthors) VALUES ('".addslashes($title)."', '".addslashes(format_story($summary))."', '".addslashes(format_story($storynotes))."', '".($catid ? implode(",", $catid) : "")."', '".($classes ? implode(",", $classes) : "")."', '".($charid ? implode(",", $charid) : "")."', '$rid', '" . time() ."', '" . time() . "', '$uid', '$validated', '$rr', '$complete', '$wordcount', '$feat', '$coauthors')");
242
+				$sid = dbinsertid( );
243
+				$inorder = 1;
244
+			}
245
+			else {
246
+				$inorder = $_GET['inorder'] + 1;
247
+			}
248
+			$insertchapter = dbquery("INSERT INTO ".TABLEPREFIX."fanfiction_chapters (title, inorder, notes, endnotes, validated, wordcount, sid, uid) VALUES('".addslashes(($chaptertitle != "" ? $chaptertitle : $title))."', '$inorder', '".addslashes(format_story($notes))."', '".addslashes(format_story($endnotes))."', '".($validated ? 1 : 0)."', '$wordcount', '$sid', '$uid')");
249
+			$chapid = dbinsertid( );
250
+			if( !file_exists("".STORIESPATH."/$uid/" ) )
251
+			{
252
+				mkdir("".STORIESPATH."/$uid/", 0755);
253
+				chmod("".STORIESPATH."/$uid/", 0777);
254
+			}
255
+			$handle = fopen("".STORIESPATH."/$uid/$chapid.txt", 'w');
256
+			if ($handle)
257
+			{
258
+				fwrite($handle, $storytext);
259
+				fclose($handle);
260
+			}
261
+			chmod("".STORIESPATH."/$uid/$chapid.txt", 0644);
262
+			if(($newchapter && $insertchapter != false) || $insertstory != false) {
263
+				if($newchapter) {
264
+					unset($_POST['submit']);
265
+					$output = write_message(_ACTIONSUCCESSFUL).editstory( $sid );
266
+				}
267
+				else {
268
+					unset($_POST['submit']);
269
+					if(!$admin) $output = write_message(_STORYADDED).viewstories( );
270
+					else $output .= write_message(_ACTIONSUCCESSFUL).editstory( $sid );
271
+				}
272
+			}
273
+			else $output .= write_error(_FATALERROR." "._TRYAGAIN);
274
+		}
275
+		if(!$newchapter) {
276
+			if(count($array_coauthors)) {
277
+				foreach($au AS $c) {
278
+					if($c == $uid) continue;
279
+					dbquery("INSERT INTO ".TABLEPREFIX."fanfiction_coauthors(`sid`, `uid`) VALUES('$sid', '$c')");
280
+				}
281
+			}
282
+			$codequery = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_codeblocks WHERE code_type = 'addstory'");
283
+			while($code = dbassoc($codequery)) {
284
+				eval($code['code_text']);
285
+			}
286
+		}
287
+		$codequery = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_codeblocks WHERE code_type = 'addchapter'");
288
+		while($code = dbassoc($codequery)) {
289
+			eval($code['code_text']);
290
+		}
291
+		// validate fic, send story alerts, and mail admins
292
+		if($validated) {
293
+			include("includes/emailer.php");
294
+			if(!isset($storytitle)) $storytitle = $title;
295
+			if(!$newchapter) {
296
+				foreach($catid as $cat) { categoryitems($cat, 1); }
297
+				if($alertson) {
298
+					$pennames[] = $penname;
299
+					$coQuery = dbquery("SELECT "._PENNAMEFIELD." AS penname FROM ".TABLEPREFIX."fanfiction_coauthors AS c LEFT JOIN "._AUTHORTABLE." ON "._UIDFIELD." = c.uid WHERE sid = '$sid'");
300
+					while($c = dbassoc($coQuery)) {
301
+						$pennames[] = $c['penname'];
302
+					}
303
+					$subject = _NEWSTORYAT.$sitename;
304
+					$mailtext = sprintf(_AUTHORALERTNOTE, $title, implode(", ", $pennames), $summary, $sid);
305
+					$favorites = dbquery("SELECT "._UIDFIELD." as uid, "._EMAILFIELD." as email, "._PENNAMEFIELD." as penname, alertson FROM ".TABLEPREFIX."fanfiction_favorites as fav, ".TABLEPREFIX."fanfiction_authorprefs as ap, "._AUTHORTABLE." WHERE FIND_IN_SET(fav.item,'".implode(",", $au)."') > 0 AND fav.type = 'AU' AND fav.uid = "._UIDFIELD." AND ap.uid = "._UIDFIELD." AND ap.alertson = '1'");
306
+					while($favuser = dbassoc($favorites)) { 
307
+						sendemail($favuser['penname'], $favuser['email'], $sitename, $siteemail, $subject, $mailtext, "html");
308
+					}				
309
+				}
310
+				dbquery("UPDATE ".TABLEPREFIX."fanfiction_stats SET stories = stories + 1");
311
+				dbquery("UPDATE ".TABLEPREFIX."fanfiction_authorprefs SET stories = stories + 1 WHERE FIND_IN_SET(uid, '".implode(",", $au)."') > 0");
312
+			}
313
+			else if($alertson && $newchapter) {
314
+				$pennames[] = $penname;
315
+				$coQuery = dbquery("SELECT "._PENNAMEFIELD." AS penname FROM ".TABLEPREFIX."fanfiction_coauthors AS c LEFT JOIN "._AUTHORTABLE." ON "._UIDFIELD." = c.uid WHERE sid = '$sid'");
316
+				while($c = dbassoc($coQuery)) {
317
+					$pennames[] = $c['penname'];
318
+				}
319
+				$titlequery = dbquery("SELECT title FROM ".TABLEPREFIX."fanfiction_stories WHERE sid = '$sid' LIMIT 1");
320
+				list($title) = dbrow($titlequery);
321
+				$subject = _STORYALERT;
322
+				$mailtext = sprintf(_STORYALERTNOTE, $title, implode(", ", $pennames), $sid, $inorder);
323
+				$favorites = dbquery("SELECT "._UIDFIELD." as uid, "._EMAILFIELD." as email, "._PENNAMEFIELD." as penname, alertson FROM ".TABLEPREFIX."fanfiction_favorites as fav, ".TABLEPREFIX."fanfiction_authorprefs as ap, "._AUTHORTABLE." WHERE fav.item = '$sid' AND fav.type = 'ST' AND fav.uid = "._UIDFIELD." AND ap.uid = "._UIDFIELD." AND ap.alertson = '1'");
324
+				while($favuser = dbassoc($favorites)) { 
325
+					sendemail($favuser['penname'], $favuser['email'], $sitename, $siteemail, $subject, $mailtext, "html");
326
+				}
327
+			}
328
+			$update = dbquery("UPDATE ".TABLEPREFIX."fanfiction_stories SET updated = '" . time() . "' WHERE sid = '$sid'");
329
+			list($chapters, $words) = dbrow(dbquery("SELECT COUNT(chapid), SUM(wordcount) FROM ".TABLEPREFIX."fanfiction_chapters WHERE validated > 0"));
330
+			list($authors) = dbrow(dbquery("SELECT COUNT(uid) FROM ".TABLEPREFIX."fanfiction_authorprefs WHERE stories > 0"));
331
+			dbquery("UPDATE ".TABLEPREFIX."fanfiction_stats set wordcount = '$words', chapters = '$chapters', authors = '$authors'");
332
+			$count =  dbquery("SELECT SUM(wordcount) as totalcount FROM ".TABLEPREFIX."fanfiction_chapters WHERE sid = '$sid' AND validated = '1'");
333
+			list($totalcount) = dbrow($count);
334
+			if($totalcount) {
335
+				dbquery("UPDATE ".TABLEPREFIX."fanfiction_stories SET wordcount = '$totalcount' WHERE sid = '$sid'");
336
+			}
337
+		}
338
+		else {
339
+			$adminquery = dbquery("SELECT "._EMAILFIELD." as email, "._PENNAMEFIELD." as penname, contact,categories FROM ".TABLEPREFIX."fanfiction_authorprefs as ap, "._AUTHORTABLE." WHERE "._UIDFIELD." = ap.uid AND level > 0 AND level < 4");
340
+			if(empty($storytitle)) $storytitle = $title;
341
+			include("includes/emailer.php");
342
+			while($admins = dbassoc($adminquery)) {
343
+				global $sitename, $siteemail;
344
+				if($admins['contact'] == 1) {
345
+					if(!$admins['categories']) {
346
+						$subject = _NEWSTORYAT.$sitename;
347
+						$mailtext = sprintf(_NEWSTORYAT2, $storytitle, $authorpenname, $summary)."\n <a href='$url/admin.php?action=submitted'>$url/admin.php?action=submitted</a>";							
348
+						$mailresult = sendemail($admins['penname'], $admins['email'], $sitename, $siteemail, $subject, $mailtext, "html");
349
+					}	
350
+					else {
351
+						if(count(array_intersect($catid, explode(",", $admins['categories'])))) {
352
+							$subject = _NEWSTORYAT.$sitename;
353
+							$mailtext = sprintf(_NEWSTORYAT2, $storytitle, $authorpenname, $summary)."\n <a href='$url/admin.php?action=submitted'>$url/admin.php?action=submitted</a>";
354
+							sendemail($admins['penname'], $admins['email'], $sitename, $siteemail, $subject, $mailtext, "html");
355
+						}
356
+					}
357
+				}
358
+			}
359
+		}
360
+		return $output;
361
+	}
362
+	if($newchapter) {
363
+		$storyinfo = dbquery("SELECT s.*, s.updated as updated, s.date as date, "._PENNAMEFIELD." as penname  FROM ".TABLEPREFIX."fanfiction_stories as s, "._AUTHORTABLE." WHERE "._UIDFIELD." = s.uid AND sid = '$sid' LIMIT 1");
364
+		$stories = dbassoc($storyinfo);
365
+		$uid = $stories['uid'];
366
+		$chapterquery = dbquery("SELECT COUNT(sid) FROM ".TABLEPREFIX."fanfiction_chapters WHERE sid = '$sid'");
367
+		list($inorder) = dbrow($chapterquery);
368
+	}
369
+	else {
370
+		$inorder = 0;
371
+		$stories['title'] = stripslashes($title);
372
+		$stories['summary'] = stripslashes($summary);
373
+		$stories['storynotes'] = stripslashes($storynotes);
374
+		$stories['catid'] = isset($catid) ? is_array($catid) ? implode(",", $catid) : $catid : "";
375
+		$stories['classes'] = isset($classes) && is_array($classes) ? implode(",", $classes) : $classes;
376
+		$stories['charid'] = isset($charid) ? is_array($charid) ? implode(",", $charid) : $charid : "";
377
+		$stories['coauthors'] = $au;
378
+		$stories['featured'] = $feat;
379
+		$stories['completed'] = $complete;
380
+		$stories['rid'] = $rid;
381
+		$stories['rr'] = $rr;
382
+		$stories['uid'] = $uid;
383
+		$stories['penname'] = $penname;
384
+		$stories['date'] = time( );
385
+		$stories['updated'] = time( );
386
+		$stories['sid'] = false;
387
+		$stories['rating'] = 0;
388
+		$stories['count'] = 0;
389
+		$stories['wordcount'] = $wordcount;
390
+		$stories['reviews'] = 0;
391
+		$stories['validated'] = 0;
392
+	}
393
+	if(!isADMIN || uLEVEL == 4) {
394
+		$rquery = dbquery("SELECT message_title, message_text FROM ".TABLEPREFIX."fanfiction_messages WHERE message_name = 'rules' LIMIT 1");
395
+		list($ruletitle, $ruletext) = dbrow($rquery);
396
+		$output .= "<div class=\"sectionheader\">$ruletitle</div>$ruletext";
397
+	}	
398
+	if($storytext){
399
+		$stories['storytext'] = $storytext;
400
+		$stories['chaptertitle'] = $chaptertitle;
401
+		$stories['inorder'] = $inorder;
402
+		$stories['notes'] = $notes;
403
+		$stories['endnotes'] = $endnotes;
404
+/*
405
+		$output .= "<div><span class=\"label\">"._CHAPTERTITLE.": </span> $chaptertitle</div>
406
+			<div><span class=\"label\">"._AUTHORSNOTES.":</span> ".format_story($notes)."</div>
407
+			<div><span class=\"label\">"._STORY.":</span><br />".format_story($storytext)."</div>
408
+			<div><span class=\"label\">"._ENDNOTES.":</span> ".format_story( $endnotes)."</div>";
409
+*/
410
+	}
411
+	if(isset($_POST['submit']) || $newchapter) $output .= preview_story($stories);
412
+
413
+	$submit = isset($_POST['submit']) ? $_POST['submit'] : false;
414
+	if(!$submit) $submit = _PREVIEW;
415
+	$output .= "<div class=\"tblborder\" style=\"width: 500px; padding: 10px; margin: 1em auto;\">
416
+	<form METHOD=\"POST\" name=\"form\" enctype=\"multipart/form-data\" action='stories.php?action=$action".($newchapter ? "&amp;sid=$sid&amp;inorder=$inorder" : "").($admin == 1 ? "&amp;admin=1&amp;uid=$uid" : "")."'>";
417
+	if(!$newchapter) $output .= storyform($stories, $submit);
418
+	$output .= chapterform($inorder, $notes, $endnotes, $storytext, $chaptertitle, $uid);
419
+	$output .= "<div style=\"text-align: center;\"><input type=\"submit\" class=\"button\" value=\""._PREVIEW."\" name=\"submit\">&nbsp; <input type=\"submit\" class=\"button\" 
420
+                 value=\""._ADDSTORY."\" name=\"submit\"></div></form></div>";
421
+	return $output;
422
+}
423
+// end newstory function
424
+
425
+function viewstories( ) {
426
+	global $storiespath, $ratings, $autovalidate, $reviewsallowed, $sid, $chapid, $up, $down;
427
+
428
+	$output = "<div id=\"pagetitle\">"._MANAGESTORIES."</div>";
429
+
430
+	$go = isset($_GET['go']) ? $_GET['go'] : false;
431
+	$com = isset($_GET['com']) ? $_GET['com'] : false;
432
+	$hidechapters = isset($_GET["chapters"]) ? $_GET["chapters"] : false;
433
+	if(($go || $com) && $sid) {
434
+		$inorder = isset($_GET['inorder']) && isNumber($_GET['inorder']) ? $_GET['inorder'] : false;
435
+		if($inorder && $chapid) {
436
+			if($go == "up") $oneabove = $inorder - 1;
437
+			else $oneabove = $inorder + 1;
438
+			dbquery("UPDATE ".TABLEPREFIX."fanfiction_chapters SET inorder = '$inorder' WHERE sid = '$sid' and inorder = '$oneabove'");
439
+			dbquery("UPDATE ".TABLEPREFIX."fanfiction_chapters SET inorder = '$oneabove' WHERE chapid = '$chapid'");	
440
+		}
441
+		if($com)  dbquery("UPDATE ".TABLEPREFIX."fanfiction_stories SET completed = ".($com == "yes" ? "1" : "0")." WHERE sid = '$sid'");
442
+	}
443
+	$output .= "<p style=\"text-align: right; margin: 1em;\"><a href=\"stories.php?action=viewstories&amp;chapters=".($hidechapters != "view" ? "view\">"._VIEWCHAPTERS : "hide\">"._HIDECHAPTERS)."</a></p>
444
+		<div style=\"width: 90%; margin: 0 auto;\"><table cellpadding=\"3\" cellspacing=\"0\" width=\"100%\" class=\"tblborder\"><tr><th class=\"tblborder\">"._STORIES."</th><th colspan=\"3\" class=\"tblborder\">"._OPTIONS."</th>".($reviewsallowed ? "<th class=\"tblborder\">"._REVIEWS."</th>" : "").($autovalidate ? "" : "<th class=\"tblborder\">"._VALIDATED."</th>")."<th class=\"tblborder\">"._READS."</th></tr>";
445
+	
446
+	$squery = "SELECT stories.sid, title, reviews, rating, completed, validated, featured, count FROM " . TABLEPREFIX . "fanfiction_stories AS stories LEFT JOIN " . TABLEPREFIX . "fanfiction_coauthors AS coauth ON stories.sid = coauth.sid WHERE stories.uid = '" . USERUID . "' OR coauth.uid = '" . USERUID . "' GROUP BY stories.sid ORDER BY title ";
447
+	$sresult = dbquery($squery); 
448
+	$stories = dbnumrows($sresult);
449
+	while($story = dbassoc($sresult)) {
450
+		$query2 = dbquery("SELECT chapid, title, inorder, rating, reviews, validated, count FROM ".TABLEPREFIX."fanfiction_chapters WHERE sid = '".$story['sid']."' ORDER BY inorder"); 
451
+		$chapters =  dbnumrows($query2);
452
+		$output .= "<tr><td class=\"tblborder\"><a href=\"viewstory.php?sid=".$story['sid']."\">".stripslashes($story['title'])."</a> ".ratingpics($story['rating'])." <strong>"._COMPLETE.":</strong> <a href=\"stories.php?action=viewstories&amp;sid=".$story['sid']."&amp;com=".$story['completed']."\"><a href=\"stories.php?action=viewstories&amp;sid=".$story['sid']."&amp;com=".($story['completed'] == 1 ? "no\">"._YES : "yes\">"._NO)."</a></td>
453
+			<td class=\"tblborder\" colspan=\"3\"><a href=\"stories.php?action=editstory&amp;sid=".$story['sid']."\">"._EDIT."</a> - <a href=\"stories.php?action=delete&amp;sid=".$story['sid']."\">"._DELETE."</a> - <a href=\"stories.php?action=newchapter&amp;sid=".$story['sid']."&amp;inorder=$chapters\">"._ADDNEWCHAPTER."</a></td>
454
+			";
455
+		if($reviewsallowed) $output .= "<td class=\"tblborder\" align=\"center\">".($story['reviews'] ? "<a href=\"reviews.php?type=ST&amp;item=".$story['sid']."\">".$story['reviews']."</a>" : "0")."</td>";
456
+		if(!$autovalidate) $output .= "<td class=\"tblborder\" align=\"center\">".($story['validated'] > 0 ? _YES : _NO)."</td>";
457
+		$output .= "<td class=\"tblborder\" align=\"center\">".($story['count'] ? $story['count'] : "0")."</td></tr>";
458
+		if($hidechapters && $hidechapters != "hide") {
459
+			while($chapter = dbassoc($query2)) {
460
+				$output .="<tr><td  class=\"tblborder\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"viewstory.php?sid=".$story['sid']."&amp;chapter=".$chapter['inorder']."\">$chapter[title]</a></td>";
461
+				if($chapters > 1) $output .= "<td  class=\"tblborder\" align=\"center\"><a href=\"stories.php?action=viewstories&amp;go=up&amp;sid=".$story['sid']."&amp;chapid=".$chapter['chapid']."&amp;inorder=".$chapter['inorder']."&amp;chapters=view\">$up</a></td>
462
+					<td class=\"tblborder\" align=\"center\"><a href=\"stories.php?action=viewstories&amp;go=down&amp;sid=".$story['sid']."&amp;chapid=".$chapter['chapid']."&amp;inorder=".$chapter['inorder']."&amp;chapters=view\">$down</a></td>";
463
+				$output .= "<td class=\"tblborder\"".($chapters > 1 ? "" : "colspan=\"3\"")."><a href=\"stories.php?action=editchapter&amp;chapid=".$chapter['chapid']."\">"._EDIT."</a>";
464
+				if($chapters > 1) $output .= " - <a href=\"stories.php?action=delete&amp;chapid=".$chapter['chapid']."&amp;sid=".$story['sid']."\">"._DELETE."</a>";
465
+				$output .= "</td><td class=\"tblborder\" align=\"center\">";
466
+				if($reviewsallowed) $output .= ($chapter['reviews'] ? "<a href=\"reviews.php?type=ST&amp;item=".$story['sid']."&amp;chapid=".$chapter['chapid']."\">".$chapter['reviews']."</a>" : "0");
467
+				if(!$autovalidate) $output .= "</td><td class=\"tblborder\" align=\"center\">".($chapter['validated'] > 0 ? _YES : _NO);
468
+				$output .= "<td class=\"tblborder\" align=\"center\">".$chapter['count']."</td></tr>";
469
+			}
470
+		}
471
+	}
472
+	$output .= "</table></div>";
473
+	if($stories < 1) $output .= write_message(_NORESULTS);
474
+	$output .= write_message("<a href='stories.php?action=newstory'>"._ADDNEWSTORY."</a>");
475
+	return $output;
476
+}
477
+// end viewstories function
478
+
479
+function editchapter( $chapid ) {
480
+	global $tpl, $chapid, $store, $alertson, $storiespath, $admin, $tinyMCE, $allowed_tags, $sid, $logging;
481
+
482
+	$output = "<div id=\"pagetitle\">"._EDITCHAPTER."</div>";
483
+// get variables from $_POST
484
+	if(isset($_POST['submit'])) {
485
+		$chaptertitle = strip_tags(descript($_POST["chaptertitle"]), $allowed_tags);
486
+		$notes = strip_tags(descript($_POST["notes"]), $allowed_tags);
487
+		$endnotes = strip_tags(descript($_POST["endnotes"]), $allowed_tags);
488
+		if(!empty($_FILES['storyfile']['name'])) {
489
+			if ($_FILES['storyfile']['type'] != 'text/html' && $_FILES['storyfile']['type'] != 'text/plain') {
490
+ 				 $failed = _INVALIDUPLOAD;
491
+				$submit = _PREVIEW;
492
+			}
493
+			else {
494
+				$texts = file($_FILES['storyfile']['tmp_name']);
495
+				foreach ($texts as $text) {
496
+					if($_FILES['storyfile']['type'] == 'text/html') $story .= rtrim($text, "\n\r\t")." ";
497
+					else $story .= $text;
498
+				}
499
+			}
500
+		}
501
+		else if(isset($_POST['storytext'])) $story = $_POST['storytext'];
502
+		$storytext = strip_tags(descript($story), $allowed_tags);
503
+		$inorder = $_POST["inorder"];
504
+		$words_to_count = strip_tags($storytext);
505
+		$pattern = "/[^(\w|\d|\'|\"|\.|\!|\?|;|,|\\|\/|\-\-|:|\&|@)]+/";
506
+		$words_to_count = preg_replace ($pattern, " ", $words_to_count);
507
+		$words_to_count = trim($words_to_count);
508
+		$wordcount = count(explode(" ",$words_to_count)); 
509
+	}
510
+	else {
511
+	}
512
+	if(isset($_POST['submit']) && $_POST['submit'] == _ADDSTORY) {
513
+		if(!$chaptertitle || !$storytext ) $submit == _PREVIEW;
514
+		else {
515
+			if($admin && empty($_POST["uid"])) {
516
+				$result2 = dbquery("SELECT uid FROM ".TABLEPREFIX."fanfiction_chapters WHERE chapid = '$chapid' LIMIT 1");
517
+				$user = dbassoc($result2);
518
+				$uid = $user['uid'];
519
+			}
520
+			else if(isset($_POST['uid']) && isNumber($_POST['uid'])) {
521
+				$uid = $_POST['uid'];
522
+			}
523
+			else $uid= USERUID;
524
+			if($store == "mysql") {
525
+				dbquery("UPDATE ".TABLEPREFIX."fanfiction_chapters SET uid = '$uid', title = '".addslashes($chaptertitle)."', notes = '".addslashes($notes)."', endnotes = '".addslashes($endnotes)."', wordcount = '$wordcount', storytext = '".addslashes($storytext)."' WHERE chapid = '$chapid' LIMIT 1");
526
+			}
527
+			else if($store == "files"){
528
+				$updatequery = dbquery("UPDATE ".TABLEPREFIX."fanfiction_chapters SET uid = '$uid', title = '".addslashes($chaptertitle)."', notes = '".addslashes($notes)."', endnotes = '".addslashes($endnotes)."', wordcount = '$wordcount' WHERE chapid = '$chapid' LIMIT 1");
529
+				if( !file_exists( STORIESPATH."/$uid/" ) )
530
+				{
531
+					mkdir(STORIESPATH."/$uid", 0755);
532
+					chmod(STORIESPATH."/$uid", 0777);
533
+				}
534
+				$handle = fopen(STORIESPATH."/$uid/$chapid.txt", 'w+');
535
+
536
+				if ($handle)
537
+				{
538
+					fwrite($handle, descript($storytext));
539
+					fclose($handle);
540
+					$storytext = "";
541
+				}
542
+
543
+			}
544
+			//  Check that the chapter has been validated before updating the word count of the story.
545
+			$validquery = dbquery("SELECT validated, sid FROM ".TABLEPREFIX."fanfiction_chapters WHERE chapid = '$chapid' LIMIT 1");
546
+			list($valid, $sid) = dbrow($validquery);
547
+			if($valid) {
548
+				$count =  dbquery("SELECT SUM(wordcount) as totalcount FROM ".TABLEPREFIX."fanfiction_chapters WHERE sid = '$sid'");
549
+				list($totalcount) = dbrow($count);
550
+				if($totalcount) {
551
+					dbquery("UPDATE ".TABLEPREFIX."fanfiction_stories SET wordcount = '$totalcount' WHERE sid = '$sid'");
552
+				}
553
+			}
554
+			$codequery = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_codeblocks WHERE code_type = 'editchapter'");
555
+			while($code = dbassoc($codequery)) {
556
+				eval($code['code_text']);
557
+			}
558
+			if($admin && $logging && USERUID != $uid) {
559
+				$storyinfo = dbquery("SELECT story.title, story.sid, chapter.uid, chapter.inorder, "._PENNAMEFIELD." as penname FROM ".TABLEPREFIX."fanfiction_stories as story, ".TABLEPREFIX."fanfiction_chapters as chapter, "._AUTHORTABLE." WHERE "._UIDFIELD." = chapter.uid AND story.sid = chapter.sid AND chapter.chapid = $chapid");
560
+				list($title, $sid, $chapuid, $inorder, $chappenname) = dbrow($storyinfo);
561
+				dbquery("INSERT INTO ".TABLEPREFIX."fanfiction_log (`log_action`, `log_uid`, `log_ip`, `log_type`, `log_timestamp`) VALUES('".escapestring(sprintf(_LOG_ADMIN_EDIT_CHAPTER, USERPENNAME, USERUID, $title, $sid, $chappenname, $chapuid, $inorder))."', '".USERUID."', INET_ATON('".$_SERVER['REMOTE_ADDR']."'), 'ED', " . time() . ")");
562
+			}
563
+			unset($_POST['submit']);
564
+			$output = write_message(_STORYUPDATED).editstory($sid);
565
+			$tpl->assign( "output", $output );
566
+			$tpl->printToScreen( );
567
+			dbclose( );
568
+			exit( );
569
+		}
570
+	}
571
+	$output .= "<div class=\"tblborder\" style=\"width: 550px; margin: 0 auto; padding: 5px;\">
572
+		<form METHOD=\"POST\"  enctype=\"multipart/form-data\" name=\"form\" action=\"stories.php?action=editchapter&amp;chapid=$chapid".($admin ? "&amp;admin=1" : "")."\">";
573
+	if(!isset($_POST['submit']) || $_POST['submit'] != _PREVIEW) {
574
+		$storyquery = dbquery("SELECT title, inorder, notes, endnotes, uid, storytext, sid FROM ".TABLEPREFIX."fanfiction_chapters WHERE chapid = '$chapid' LIMIT 1");
575
+		$story = dbassoc($storyquery);
576
+		$chaptertitle = stripslashes($story["title"]);
577
+		$storytext = $story["storytext"];
578
+		$inorder = $story["inorder"];
579
+		$notes = stripslashes($story["notes"]);
580
+		$endnotes = stripslashes($story["endnotes"]);
581
+		$sid = $story["sid"];
582
+
583
+		if($store == "mysql") $storytext = $story['storytext'];
584
+		else {
585
+			if($out = fopen (STORIESPATH."/".$story['uid']."/$chapid.txt", "r"))
586
+				while (!feof($out)) {
587
+					$storytext .= fgets($out, 10000);
588
+				}
589
+		}
590
+		$submit = _PREVIEW;
591
+		$output .= chapterform($inorder, $notes, $endnotes, stripslashes($storytext), $chaptertitle, $story['uid']);
592
+	}
593
+	else {
594
+		if(isset($failed)) $output .= write_message($failed);
595
+		$output .= "<span class='label'>"._TITLE.":</span> $chaptertitle<br />
596
+			<span class='label'>"._AUTHORSNOTES.":</span> $notes<br />
597
+			<hr><br />".format_story($storytext)."<hr>
598
+			<span class='label'>"._ENDNOTES.":</span> $endnotes<br />";
599
+		$output .= chapterform($inorder, $notes, $endnotes, $storytext, $chaptertitle, (isset($_POST['uid']) ? $_POST['uid'] : false));
600
+		$submit = $_POST['submit'];
601
+	}
602
+	$output .= "<p><input type=\"hidden\" name=\"sid\" value=\"$sid\"><input type=\"hidden\" name=\"inorder\" value=\"$inorder\"><input type=\"submit\" class=\"button\" value=\"$submit\" name=\"submit\">&nbsp; <input type=\"submit\" class=\"button\" value=\""._ADDSTORY."\" name=\"submit\"></p></form></div>";
603
+	return $output;
604
+
605
+}
606
+// end editchapter function
607
+
608
+function editstory($sid) {
609
+	global $tpl, $storiespath, $store, $allowed_tags, $uid, $admin, $tinyMCE, $up, $down, $dateformat, $classtypelist, $logging, $alertson;
610
+
611
+	$output = "<div id=\"pagetitle\">"._EDITSTORY."</div>";
612
+	if(isset($admin) && isset($uid)) {
613
+		$author = dbquery("SELECT "._PENNAMEFIELD." FROM "._AUTHORTABLE." WHERE "._UIDFIELD." = '$uid' LIMIT 1");
614
+		$authorvalid = 1; // It's an admin edit so it's valid.
615
+		list($penname) = dbrow($author);
616
+	}
617
+	else {  
618
+		$valid = dbquery("SELECT validated FROM ".TABLEPREFIX."fanfiction_authorprefs WHERE uid = '".USERUID."' LIMIT 1");
619
+		list($authorvalid) = dbrow($valid);
620
+		$uid = USERUID;
621
+		$penname = USERPENNAME;
622
+	}
623
+	if(isset($_POST['submit'])) {
624
+		$title = isset($_POST['title']) ? strip_tags(descript($_POST['title']), $allowed_tags) : "";
625
+		$summary = isset($_POST['summary']) ? strip_tags(descript($_POST['summary']), $allowed_tags) : "";
626
+		$storynotes = strip_tags(descript($_POST['storynotes']), $allowed_tags);
627
+		$rr = isset($_POST['rr']) && isNumber($_POST['rr']) ? $_POST['rr'] : 0;
628
+		$feat = isset($_POST['feature']) && isNumber($_POST['feature']) ? $_POST['feature'] : 0;
629
+		$complete = isset($_POST['complete']) && isNumber($_POST['complete']) ? $_POST['complete'] : 0;
630
+		$validated = isset($_POST['validated']) && isNumber($_POST['validated']) ? $_POST['validated'] : 0;
631
+		$chaptertitle = isset($_POST['chaptertitle']) ? descript(strip_tags($_POST['chaptertitle'], $allowed_tags)) : "";
632
+		$notes = isset($_POST['notes']) ? strip_tags(descript($_POST['notes']), $allowed_tags) : "";
633
+		$endnotes = isset($_POST['endnotes']) ? strip_tags(descript($_POST['endnotes']), $allowed_tags) : "";
634
+		$rid = isset($_POST['rid']) && isNumber($_POST['rid']) ? $_POST['rid'] : 0;
635
+		$catid = isset($_POST['catid']) ? array_filter(explode(",", $_POST['catid']), "isNumber") : array( );
636
+		$charid = isset($_POST['charid']) ? array_filter($_POST['charid'], "isNumber") : array( );
637
+		$array_coauthors = isset($_POST['coauthors']) ? array_filter(explode(",", $_POST['coauthors']), "isNumber") : array( );
638
+		if(isset($_POST['uid']) && isNumber($_POST['uid'])) $uid = $_POST['uid'];
639
+		else $uid = USERUID;
640
+		$classes = array( );
641
+		$classquery = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_classtypes");
642
+		while($type = dbassoc($classquery)) {
643
+			if(isset($_POST["class_".$type['classtype_id']])) {
644
+				$opts = is_array($_POST["class_".$type['classtype_id']]) ? array_filter($_POST["class_".$type['classtype_id']], "isNumber") : "";
645
+				$classes = array_merge($opts, $classes);
646
+			}
647
+		}
648
+		if(!$admin && $authorvalid && !$validated) $validated = 2;
649
+		$au[] = $uid;
650
+		if(count($array_coauthors)) {
651
+			$au = array_merge($au, $array_coauthors);
652
+			$coauthors = 1;
653
+		}
654
+		else $coauthors = 0;
655
+	}
656
+	if(isset($_POST['submit']) && $_POST['submit'] == _ADDSTORY) {
657
+		$oldcats = isset($_POST['oldcats']) ? array_filter(explode(",", $_POST['oldcats']), "isNumber") : array( );
658
+		if (!$rid || !$title || !$summary || !$catid) {
659
+			$output .= write_error(_MISSINGFIELDS);
660
+			$submit = _PREVIEW;
661
+		}
662
+		else {
663
+			// Change author of story.
664
+			if($admin) {
665
+				$authquery = dbquery("SELECT uid FROM ".TABLEPREFIX."fanfiction_stories WHERE sid = '".$sid."'");
666
+				list($olduid) = dbrow($authquery);
667
+				if($olduid != $uid) {
668
+					if($store == "files") {
669
+						$chapters = dbquery("SELECT chapid FROM ".TABLEPREFIX."fanfiction_chapters WHERE sid = '$sid' AND uid = '$olduid'");
670
+						while($chap = dbassoc($chapters)) {
671
+							$chapid = $chap['chapid'];
672
+							$storytext = "";
673
+							if($out = fopen (STORIESPATH."/$olduid/$chapid.txt", "r"))
674
+							while (!feof($out)) {
675
+								$storytext .= fgets($out, 10000);
676
+							}
677
+							fclose($out);
678
+							unlink(STORIESPATH."/$olduid/$chapid.txt"); 
679
+							if($storytext) {
680
+								if( !file_exists( STORIESPATH."/$uid/" ) ) {
681
+									mkdir(STORIESPATH."/$uid", 0755);
682
+									chmod(STORIESPATH."/$uid", 0777);
683
+								}
684
+								$handle = fopen(STORIESPATH."/$uid/$chapid.txt", 'w');
685
+								if ($handle) {
686
+									fwrite($handle, $storytext);
687
+									fclose($handle);
688
+								}
689
+								chmod(STORIESPATH."/$uid/$chapid.txt", 0644);
690
+							}
691
+							dbquery("UPDATE ".TABLEPREFIX."fanfiction_chapters SET uid = '$uid' WHERE chapid = '$chapid' LIMIT 1");
692
+						}
693
+					}
694
+					else $chapupdate = dbquery("UPDATE ".TABLEPREFIX."fanfiction_chapters SET uid = '$uid' WHERE sid = '$sid' AND uid = '$olduid'");
695
+					$switch = dbquery("UPDATE ".TABLEPREFIX."fanfiction_stories SET uid = '$uid' WHERE sid = '$sid' LIMIT 1");
696
+					if($logging) {
697
+						$authorquery = dbquery("SELECT "._PENNAMEFIELD." as penname FROM "._AUTHORTABLE." WHERE "._UIDFIELD." = '$olduid' LIMIT 1");
698
+						list($oldpenname) = dbrow($authorquery);
699
+						$author2query = dbquery("SELECT "._PENNAMEFIELD." as penname FROM "._AUTHORTABLE." WHERE "._UIDFIELD." = '$uid' LIMIT 1");
700
+						list($newpenname) = dbrow($author2query);
701
+						dbquery("INSERT INTO ".TABLEPREFIX."fanfiction_log (`log_action`, `log_uid`, `log_ip`, `log_type`, `log_timestamp`) VALUES('".escapestring(sprintf(_LOG_ADMIN_EDIT_AUTHOR, USERPENNAME, USERUID, $title, $sid, $newpenname, $uid, $oldpenname, $olduid))."', '".USERUID."', INET_ATON('".$_SERVER['REMOTE_ADDR']."'), 'ED', " . time() . ")");
702
+					}
703
+				}
704
+
705
+			}
706
+			// End change author of story
707
+			$oldinfo = dbquery("SELECT title, validated, featured, catid FROM ".TABLEPREFIX."fanfiction_stories WHERE sid = '$sid' LIMIT 1");
708
+			list($storytitle, $oldvalid, $oldfeat, $oldcats ) = dbrow($oldinfo);
709
+			$oldcats = explode(",", $oldcats);
710
+			if($validated) {
711
+				if(!$oldvalid) {
712
+					include("includes/emailer.php");
713
+					list($newchapter) = dbrow(dbquery("SELECT validated FROM ".TABLEPREFIX."fanfiction_chapters WHERE sid = '$sid' AND inorder = '1'"));
714
+					if(!$newchapter) {
715
+						if($alertson) {
716
+							$pennames[] = $penname;
717
+							$coQuery = dbquery("SELECT "._PENNAMEFIELD." AS penname FROM ".TABLEPREFIX."fanfiction_coauthors AS c LEFT JOIN "._AUTHORTABLE." ON "._UIDFIELD." = c.uid WHERE sid = '$sid' ");
718
+							while($c = dbassoc($coQuery)) {
719
+								$pennames[] = $c['penname'];
720
+							}
721
+							$subject = _NEWSTORYAT;
722
+							$mailtext = sprintf(_AUTHORALERTNOTE, $title, implode(", ", $pennames), $summary, $sid);
723
+							$favorites = dbquery("SELECT "._UIDFIELD." as uid, "._EMAILFIELD." as email, "._PENNAMEFIELD." as penname, alertson FROM ".TABLEPREFIX."fanfiction_favorites as fav, ".TABLEPREFIX."fanfiction_authorprefs as ap, "._AUTHORTABLE." WHERE fav.item = $uid AND fav.type = 'AU' AND fav.uid = "._UIDFIELD." AND ap.uid = "._UIDFIELD." AND ap.alertson = '1'");
724
+							while($favuser = dbassoc($favorites)) { 
725
+								sendemail($favuser['penname'], $favuser['email'], $sitename, $siteemail, $subject, $mailtext, "html");
726
+							}				
727
+						}
728
+					}
729
+					else if($alertson && $newchapter) {
730
+						$pennames[] = $penname;
731
+						$coQuery = dbquery("SELECT "._PENNAMEFIELD." AS penname FROM ".TABLEPREFIX."fanfiction_coauthors AS c LEFT JOIN "._AUTHORTABLE." ON "._UIDFIELD." = c.uid WHERE sid = '$sid'");
732
+						while($c = dbassoc($coQuery)) {
733
+							$pennames[] = $c['penname'];
734
+						}
735
+						$titlequery = dbquery("SELECT title FROM ".TABLEPREFIX."fanfiction_stories WHERE sid = '$sid' LIMIT 1");
736
+						list($title) = dbrow($titlequery);
737
+						list($inorder) = dbrow(dbquery("SELECT inorder FROM ".TABLEPREFIX."fanfiction_chapters WHERE sid = '$sid' ORDER BY inorder DESC LIMIT 1"));
738
+						$subject = _STORYALERT;
739
+						$mailtext = sprintf(_STORYALERTNOTE, $storytitle, implode(", ", $pennames), $sid, $inorder);
740
+						$favorites = dbquery("SELECT "._UIDFIELD." as uid, "._EMAILFIELD." as email, "._PENNAMEFIELD." as penname, alertson FROM ".TABLEPREFIX."fanfiction_favorites as fav, ".TABLEPREFIX."fanfiction_authorprefs as ap, "._AUTHORTABLE." WHERE fav.item = '$sid' AND fav.type = 'ST' AND fav.uid = "._UIDFIELD." AND ap.uid = "._UIDFIELD." AND ap.alertson = '1'");
741
+						while($favuser = dbassoc($favorites)) { 
742
+							sendemail($favuser['penname'], $favuser['email'], $sitename, $siteemail, $subject, $mailtext, "html");
743
+						}
744
+					}
745
+					$update = dbquery("UPDATE ".TABLEPREFIX."fanfiction_stories SET validated = '$validated', updated = '" . time() . "' WHERE sid = '$sid'");
746
+					$update2 = dbquery("UPDATE ".TABLEPREFIX."fanfiction_chapters SET validated = 1 WHERE sid = '$sid'");
747
+					$coauths = dbquery("SELECT uid FROM ".TABLEPREFIX."fanfiction_coauthors WHERE sid = '$sid'");
748
+					while($c = dbassoc($coauths)) {
749
+						dbquery("UPDATE ".TABLEPREFIX."fanfiction_authorprefs SET stories = stories + 1 WHERE uid = '".$c['uid']."'");
750
+					}
751
+					dbquery("UPDATE ".TABLEPREFIX."fanfiction_authorprefs SET stories = stories + 1 WHERE uid = '$uid'");
752
+					$count =  dbquery("SELECT SUM(wordcount) as totalcount FROM ".TABLEPREFIX."fanfiction_chapters WHERE sid = '$sid' AND validated = '1'");
753
+					list($totalcount) = dbrow($count);
754
+					if($totalcount) {
755
+						dbquery("UPDATE ".TABLEPREFIX."fanfiction_stories SET wordcount = '$totalcount' WHERE sid = '$sid'");
756
+					}
757
+					dbquery("UPDATE ".TABLEPREFIX."fanfiction_stats SET stories = stories + 1");
758
+				}
759
+				else if($validated == 2) {
760
+					$update = dbquery("UPDATE ".TABLEPREFIX."fanfiction_stories SET validated = '$validated' WHERE sid = '$sid'");
761
+					$update2 = dbquery("UPDATE ".TABLEPREFIX."fanfiction_chapters SET validated = 1 WHERE sid = '$sid'");
762
+					$count =  dbquery("SELECT SUM(wordcount) as totalcount FROM ".TABLEPREFIX."fanfiction_chapters WHERE sid = '$sid' AND validated = '1'");
763
+					list($totalcount) = dbrow($count);
764
+					if($totalcount) {
765
+						dbquery("UPDATE ".TABLEPREFIX."fanfiction_stories SET wordcount = '$totalcount' WHERE sid = '$sid'");
766
+					}
767
+				}
768
+				$newcats = array_diff($catid, $oldcats);
769
+				foreach($newcats as $cat)  { categoryitems($cat, 1); }
770
+				$delcats = array_diff($oldcats, $catid);
771
+				foreach($delcats as $cat) { categoryitems($cat, -1); }
772
+			}
773
+			else if($admin && !$validated && $oldvalid > 0) {
774
+				foreach($oldcats as $cat) { categoryitems($cat, -1); }
775
+				$update = dbquery("UPDATE ".TABLEPREFIX."fanfiction_stories SET validated = '0' WHERE sid = '$sid'");
776
+				$update2 = dbquery("UPDATE ".TABLEPREFIX."fanfiction_chapters SET validated = 0 WHERE sid = '$sid'");
777
+				dbquery("UPDATE ".TABLEPREFIX."fanfiction_stats SET stories = stories - 1");
778
+				$coauths = dbquery("SELECT uid FROM ".TABLEPREFIX."fanfiction_coauthors WHERE sid = '$sid'");
779
+				while($c = dbassoc($coauths)) {
780
+					dbquery("UPDATE ".TABLEPREFIX."fanfiction_authorprefs SET stories = stories - 1 WHERE uid = '".$c['uid']."'");
781
+				}
782
+				dbquery("UPDATE ".TABLEPREFIX."fanfiction_authorprefs SET stories = stories - 1 WHERE uid = '$uid'");
783
+				list($chapters, $words) = dbrow(dbquery("SELECT COUNT(chapid), SUM(wordcount) FROM ".TABLEPREFIX."fanfiction_chapters WHERE validated > 0"));
784
+				list($authors) = dbrow(dbquery("SELECT COUNT(uid) FROM ".TABLEPREFIX."fanfiction_authorprefs WHERE stories > 0"));
785
+				dbquery("UPDATE ".TABLEPREFIX."fanfiction_stats set wordcount = '$words', chapters = '$chapters', authors = '$authors'");
786
+			}
787
+			else if(!$admin) $validated = $oldvalid;
788
+			if(!$admin && $oldfeat != $feat) $feat = $oldfeat;
789
+			// Update the site stats
790
+			list($chapters, $words) = dbrow(dbquery("SELECT COUNT(chapid), SUM(wordcount) FROM ".TABLEPREFIX."fanfiction_chapters WHERE validated > 0"));
791
+			list($authors) = dbrow(dbquery("SELECT COUNT(uid) FROM ".TABLEPREFIX."fanfiction_authorprefs WHERE stories > 0"));
792
+			dbquery("UPDATE ".TABLEPREFIX."fanfiction_stats set wordcount = '$words', chapters = '$chapters', authors = '$authors'");
793
+
794
+			$updatequery = dbquery("UPDATE ".TABLEPREFIX."fanfiction_stories SET title = '".addslashes($title)."', summary = '".addslashes(format_story($summary))."', storynotes = '".addslashes(format_story($storynotes))."', rr = '".($rr ? 1 : 0)."', completed = '".($complete ? 1 : 0)."', validated = '$validated', rid = '$rid', classes = '".(is_array($classes) ? implode(",", $classes) : $classes)."', charid = '".(is_array($charid) ? implode(",", $charid) : $charid)."', catid = '".(is_array($catid) ? implode(",", $catid) : $catid)."', coauthors = '".$coauthors."', featured = '$feat' WHERE sid = '$sid'");
795
+		 
796
+			$clist = array( );
797
+			$coauths = dbquery("SELECT uid FROM ".TABLEPREFIX."fanfiction_coauthors WHERE sid = '$sid'");
798
+			while($c = dbassoc($coauths)) {
799
+				$clist[] = $c['uid'];
800
+			}
801
+			foreach($au AS $a) {
802
+				if($a == $uid) continue;
803
+				if(!in_array($a, $clist)) {
804
+					dbquery("UPDATE ".TABLEPREFIX."fanfiction_authorprefs SET stories = stories + 1 WHERE uid = '$a'");
805
+					dbquery("INSERT INTO ".TABLEPREFIX."fanfiction_coauthors(`sid`, `uid`) VALUES('$sid', '$a')");
806
+				}
807
+			}
808
+			foreach($clist AS $c) {
809
+				if(!in_array($c, $au)) {
810
+					dbquery("UPDATE ".TABLEPREFIX."fanfiction_authorprefs SET stories = stories - 1 WHERE uid = '$c'");
811
+					dbquery("DELETE FROM ".TABLEPREFIX."fanfiction_coauthors WHERE sid = '$sid' AND uid = '$c'");
812
+				}		
813
+			}
814
+			$codequery = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_codeblocks WHERE code_type = 'editstory'");
815
+			while($code = dbassoc($codequery)) {
816
+				eval($code['code_text']);
817
+			}
818
+			if($logging && $admin) {
819
+				if(USERUID != $uid) { // If you're editing your own story, don't log it.
820
+					$authorquery = dbquery("SELECT "._PENNAMEFIELD." as penname FROM "._AUTHORTABLE." WHERE "._UIDFIELD." = '$uid' LIMIT 1");
821
+					list($penname) = dbrow($authorquery);
822
+					dbquery("INSERT INTO ".TABLEPREFIX."fanfiction_log (`log_action`, `log_uid`, `log_ip`, `log_type`, `log_timestamp`) VALUES('".escapestring(sprintf(_LOG_ADMIN_EDIT, USERPENNAME, USERUID, $title, $sid, $penname, $uid))."', '".USERUID."', INET_ATON('".$_SERVER['REMOTE_ADDR']."'), 'ED', " . time() . ")");
823
+				}
824
+			}
825
+			$output .= write_message(_STORYUPDATED."  ".($admin ? _BACK2ADMIN : _BACK2ACCT));
826
+			$tpl->assign( "output", $output );
827
+			$tpl->printToScreen( );
828
+			dbclose( );
829
+			exit( );
830
+		}
831
+	}
832
+	$query = dbquery("SELECT DATE_FORMAT(FROM_UNIXTIME(date), '$dateformat') as date, wordcount, uid FROM ".TABLEPREFIX."fanfiction_stories WHERE sid = '$sid' LIMIT 1");
833
+	list($published, $wordcount, $storyuid) = dbrow($query);
834
+	$formbegin = "<div class=\"tblborder\" style=\"margin: 10px auto; width: 550px; padding: 10px;\">
835
+		<form METHOD=\"POST\" name=\"form\" action=\"stories.php?action=editstory".($admin ? "&amp;admin=1" : "")."&amp;sid=$sid\">";
836
+
837
+	if(isset($_POST['submit']) && $_POST['submit'] != _PREVIEW) {
838
+		$submit = _PREVIEW;
839
+		$storyquery = dbquery("SELECT title, summary, storynotes,  rr, completed, rid, classes, charid, catid, featured, uid, coauthors, date as date FROM ".TABLEPREFIX."fanfiction_stories WHERE sid = '$sid' LIMIT 1");
840
+		$story = dbassoc($storyquery);
841
+		$output .= $formbegin.storyform($story, $preview);
842
+		$output .= "<input type=\"hidden\" name=\"oldcats\" value =\"".$story['catid']."\">";
843
+	}
844
+	else {
845
+		$submit = _PREVIEW;
846
+		$storyinfo = dbquery("SELECT s.*, s.date as date, s.updated as updated, "._PENNAMEFIELD." as penname FROM ".TABLEPREFIX."fanfiction_stories as s, "._AUTHORTABLE." WHERE "._UIDFIELD." = s.uid AND sid = '$sid' LIMIT 1");
847
+		$stories = dbassoc($storyinfo);
848
+		if($stories['coauthors'] && !isset($_POST['submit'])) {
849
+			$au = array();
850
+			$coauths = dbquery("SELECT uid FROM ".TABLEPREFIX."fanfiction_coauthors WHERE sid = '".$stories['sid']."'");
851
+			while($c = dbassoc($coauths)) {
852
+				$au[] = $c['uid'];
853
+			}
854
+			$stories['coauthors'] = $au;
855
+		}
856
+		if(isset($_POST['submit'])) {
857
+			$stories['title'] = stripslashes($title);
858
+			$stories['summary'] = stripslashes($summary);
859
+			$stories['storynotes'] = stripslashes($storynotes);
860
+			$stories['catid'] = isset($catid) ? is_array($catid) ? implode(",", $catid) : $catid : "";
861
+			$stories['classes'] = isset($classes) && is_array($classes) ? implode(",", $classes) : $classes;
862
+			$stories['charid'] = isset($charid) ? is_array($charid) ? implode(",", $charid) : $charid : "";		
863
+			$stories['coauthors'] = $au;
864
+			$stories['featured'] = $feat;
865
+			$stories['completed'] = $complete;
866
+			$stories['rid'] = $rid;
867
+			$stories['rr'] = $rr;
868
+			$stories['uid'] = $uid;
869
+			$stories['wordcount'] = $wordcount;
870
+		}
871
+		$output .= preview_story($stories);
872
+		$output .= $formbegin.storyform($stories, _PREVIEW);
873
+	}
874
+
875
+	$chapquery = dbquery("SELECT chapid, title, inorder, rating, reviews, validated, uid FROM ".TABLEPREFIX."fanfiction_chapters WHERE sid = '$sid' ORDER BY inorder");
876
+	$chapters = dbnumrows($chapquery);
877
+	$output .= "<p><input type=\"submit\" class=\"button\" value=\"$submit\" name=\"submit\">&nbsp; <input type=\"submit\" class=\"button\" value=\""._ADDSTORY."\" name=\"submit\"></p></form></div>";
878
+	$output .= "<br><table class=\"tblborder\" style=\"margin: 0 auto; width: 500px;\"><tr><th>"._CHAPTER."</th>".($chapters > 1 ? "<th>"._MOVE."</th>" : "")."<th>"._OPTIONS."</th></tr>";
879
+			while($chapter = dbassoc($chapquery)) {
880
+				$output .="<tr><td class=\"tblborder\"><a href=\"viewstory.php?sid=$sid&amp;chapter=".$chapter['inorder']."\">".$chapter['title']."</a></td>";
881
+				if($chapters > 1) $output .= "<td align=\"center\" class=\"tblborder\">".($chapter['inorder'] == 1 ? "" : "<a href=\"stories.php?action=viewstories&amp;go=up&amp;sid=$sid&amp;chapid=".$chapter['chapid']."&amp;inorder=".$chapter['inorder']."\">$up</a>").
882
+					($chapter['inorder'] == $chapters ? "" : "<a href=\"stories.php?action=viewstories&amp;go=down&amp;sid=$sid&amp;chapid=".$chapter['chapid']."&amp;inorder=".$chapter['inorder']."\">$down</a>")."</td>";
883
+				$output .= "<td class=\"tblborder\" align=\"center\"><a href=\"stories.php?action=editchapter&amp;chapid=".$chapter['chapid'].($admin ? "&amp;admin=1&amp;uid=".$chapter['uid'] : "")."\">"._EDIT."</a>";
884
+				if($chapters > 1) $output .= " - <a href=\"stories.php?action=delete&amp;chapid=".$chapter['chapid']."&amp;sid=$sid".($admin ? "&amp;admin=1&amp;uid=".$chapter['uid'] : "")."\">"._DELETE."</a>";
885
+				$output .= "</td></tr>";
886
+			}
887
+	$output .= "<tr><td class=\"tblborder\" colspan=\"3\" align=\"center\"><a href=\"stories.php?action=newchapter&amp;sid=$sid&amp;inorder=$chapters".($admin ? "&amp;admin=1&amp;uid=".$storyuid : "")."\">"._ADDNEWCHAPTER."</a></td></tr></table>";
888
+	return $output;
889
+}
890
+// end editstory
891
+
892
+function delete( ) {
893
+	global $tpl, $store, $storiespath, $admin, $sid, $chapid, $logging;
894
+
895
+	$confirmed = isset($_GET['confirmed']) ? $_GET['confirmed'] : false;
896
+	if(!$sid && !$chapid) return write_error(_ERROR);
897
+	$output = "<div id=\"pagetitle\">".($chapid ? _DELETECHAPTERTITLE : _DELETESTORYTITLE)."</div>";
898
+	if($admin) {
899
+		if($chapid) $authorquery = dbquery("SELECT uid FROM ".TABLEPREFIX."fanfiction_chapters WHERE chapid = '$chapid' LIMIT 1");
900
+		else $authorquery = dbquery("SELECT uid FROM ".TABLEPREFIX."fanfiction_stories WHERE sid = '$sid' LIMIT 1");
901
+		list($uid) = dbrow($authorquery);
902
+	}
903
+	else $uid = USERUID;
904
+	if($confirmed == "no") {
905
+		$output .= "<center>"._ACTIONCANCELLED."  ".($admin ? _BACK2ADMIN : _BACK2ACCT)."</center>";
906
+	}
907
+	else if($confirmed == "yes") {
908
+		$storyquery = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_stories WHERE sid = '$sid' LIMIT 1");
909
+		$story = dbassoc($storyquery);
910
+		if($chapid) {
911
+			$chapterquery = dbquery("SELECT inorder, chapid, uid FROM ".TABLEPREFIX."fanfiction_chapters WHERE sid = '$sid'");
912
+			$orderquery = dbquery("SELECT inorder FROM ".TABLEPREFIX."fanfiction_chapters WHERE chapid = '$chapid' LIMIT 1");
913
+			list($inorder) = dbrow($orderquery);
914
+			if(empty($inorder)) { // Shouldn't be possible and yet someone managed to do it.
915
+				errorExit();
916
+			}
917
+			$chapters = dbnumrows($chapterquery);
918
+		}
919
+		if(isset($chapters) && $chapters > 1) {
920
+			list($valid) = dbrow(dbquery("SELECT validated FROM ".TABLEPREFIX."fanfiction_chapters where chapid = '$chapid' LIMIT 1"));
921
+			dbquery("DELETE FROM ".TABLEPREFIX."fanfiction_chapters WHERE chapid = '$chapid' LIMIT 1");
922
+			if($valid) dbquery("UPDATE ".TABLEPREFIX."fanfiction_stats SET chapters = chapters - 1");
923
+			if($store == "files") unlink(STORIESPATH."/$uid/".$chapid.".txt"); 
924
+			if($inorder < $chapters) 
925
+				dbquery("UPDATE ".TABLEPREFIX."fanfiction_chapters SET inorder = (inorder - 1) WHERE sid = '$sid' AND inorder > $inorder");
926
+			$codequery = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_codeblocks WHERE code_type = 'delchapter'");
927
+			while($code = dbassoc($codequery)) {
928
+				eval($code['code_text']);
929
+			}
930
+
931
+			if($logging && $admin) {
932
+				$authorquery = dbquery("SELECT "._PENNAMEFIELD." as penname FROM "._AUTHORTABLE." WHERE "._UIDFIELD." = '$uid' LIMIT 1");
933
+				list($penname) = dbrow($authorquery);
934
+				dbquery("INSERT INTO ".TABLEPREFIX."fanfiction_log (`log_action`, `log_uid`, `log_ip`, `log_type`, `log_timestamp`) VALUES('".escapestring(sprintf(_LOG_ADMIN_DEL_CHAPTER, USERPENNAME, USERUID, $story['title'], $sid, $penname, $uid, $inorder))."', '".USERUID."', INET_ATON('".$_SERVER['REMOTE_ADDR']."'), 'DL', " . time() . ")");
935
+			}
936
+			return "<center>"._ACTIONSUCCESSFUL."</center>".editstory( $sid );
937
+		}
938
+		else {
939
+			include("includes/deletefunctions.php");
940
+			deleteStory($story);
941
+		}
942
+		$output = write_message(_ACTIONSUCCESSFUL."  ".($admin ? _BACK2ADMIN : viewstories( )));	
943
+	}
944
+	else {
945
+		if($chapid) {
946
+			$output .= write_message(_CONFIRMDELETE."<BR><BR>
947
+[ <a href=\"stories.php?action=delete&amp;confirmed=yes&amp;chapid=$chapid&amp;sid=$sid".(!empty($admin) ? "&amp;admin=1&amp;uid=".$uid : "")."\">"._YES."</a> | 
948
+				<a href=\"stories.php?action=delete&amp;confirmed=no\">"._NO."</a> ]");
949
+		}
950
+		else {
951
+			$output .= write_message(_DELETESTORY."<BR><BR>
952
+[ <a href=\"stories.php?action=delete&amp;confirmed=yes&amp;sid=$sid".(!empty($admin) ? "&amp;admin=1&amp;uid=".$uid : "")."\">"._YES."</a> | 
953
+				<a href=\"stories.php?action=delete&amp;confirmed=no\">"._NO."</a> ]");
954
+		}
955
+	}
956
+	return $output;
957
+}
958
+// end delete
959
+	
960
+switch($action) {
961
+	case "newstory":
962
+		$output .= newstory( );
963
+		break;
964
+	case "newchapter":
965
+		$output .= newstory( );
966
+		break;
967
+	case "editchapter":
968
+
969
+		$output .= editchapter($chapid);			
970
+		break;
971
+	case "editstory":
972
+		$output .= editstory($sid);
973
+		break;
974
+	case "delete":
975
+		$output .= delete( );
976
+		break;
977
+	default:
978
+		$output .= viewstories( );
979
+		break;
980
+}
981
+
982
+	$tpl->assign( "output", $output );
983
+	$tpl->printToScreen();
984
+	dbclose( );
985
+?>
0 986
\ No newline at end of file