Browse code

Initial uplad w/ php7 fixes

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