Browse code

browse.php, search.php, includes/categorylist.php: patches from upstream, with some exceptions - search.php needs Ami-fixed old behavior in the penname results due to new upstream version bugging out in MySQL strict error reporting mode; browse.php has searchtype errorExit() behavior commented out as it crashes series results, other results are fine

Clarissa Walker authored on 2026/11/02 08:39:49
Showing 3 changed files
... ...
@@ -1,354 +1,377 @@
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
-$current = "browse";
24
-$displayform = 1;
25
-
26
-include ("header.php");
27
-
28
-if(file_exists("$skindir/browse.tpl")) $tpl = new TemplatePower( "$skindir/browse.tpl" );
29
-else $tpl = new TemplatePower("default_tpls/browse.tpl");
30
-if(file_exists("$skindir/listings.tpl")) $tpl->assignInclude("listings", "./$skindir/listings.tpl");
31
-else $tpl->assignInclude( "listings", "./default_tpls/listings.tpl" );
32
-$tpl->assignInclude( "header", "./$skindir/header.tpl" );
33
-$tpl->assignInclude( "footer", "./$skindir/footer.tpl" );
34
-
35
-include("includes/pagesetup.php");
36
-if(isset($_GET['type'])) $type = descript($_GET['type']);
37
-else $type = false;
38
-
39
-if($type) {
40
-	$query = array();
41
-	$countquery = array();
42
-	$scountquery = array();
43
-	$searchtype = (isset($_REQUEST['searchtype']) ? $_REQUEST['searchtype'] : "simple");
44
-	$searchterm =  (isset($_REQUEST['searchterm']) ? escapestring(descript($_REQUEST['searchterm'])) : false);
45
-	if($searchterm !== false) $searchterm = str_replace(array('%', '_'), array('\\%', '\\_'), escapestring($searchterm));
46
-	$disablesorts = array( );
47
-	if($searchterm) {
48
-		if($searchtype == "title") {
49
-			$query[] = "stories.title LIKE '%$searchterm%'";
50
-			$countquery[] = "stories.title LIKE '%$searchterm%'";
51
-			$scountquery[] = "series.title LIKE '%$searchterm%'";
52
-		}
53
-		if($searchtype == "summary") {
54
-			$query[] = "stories.summary LIKE '%$searchterm%'";
55
-			$countquery[] = "stories.summary LIKE '%$searchterm%'";
56
-			$scountquery[] = "series.summary LIKE '%$searchterm%'";
57
-		}
58
-		if($searchtype == "penname") {
59
-			$authorquery = dbquery(_MEMBERLIST." WHERE "._PENNAMEFIELD." LIKE '%$searchterm%' GROUP BY "._UIDFIELD);
60
-			$authorlist = array();
61
-			while($auth = dbassoc($authorquery)) {
62
-				$authorlist[] = $auth['uid'];
63
-			}
64
-			if(count($authorlist) > 0) {
65
-				$query[] = "FIND_IN_SET(stories.uid, '".implode(",",$authorlist)."') > 0";
66
-				$countquery[] = "FIND_IN_SET(stories.uid, '".implode(",",$authorlist)."') > 0";
67
-				$scountquery[] = "FIND_IN_SET(series.uid, '".implode(",",$authorlist)."') > 0";
68
-			}
69
-			else {
70
-				$query[] = "1 = 0"; $countquery[] = "1 = 0"; $scountquery[] = "1 = 0";
71
-			}
72
-		}
73
-		if($searchtype == "advanced" || !$searchtype) {
74
-			$query[] = "(stories.summary LIKE '%$searchterm%' OR stories.title LIKE '%$searchterm%') ";
75
-			$countquery[] = "(stories.summary LIKE '%$searchterm%' OR stories.title LIKE '%$searchterm%') ";
76
-			$scountquery[] = "(summary LIKE '%$searchterm%' OR title LIKE '%$searchterm%') ";
77
-		}
78
-	}
79
-	$ef_authors = isset($_REQUEST['authors']) ? array_filter(array_map('trim', is_array($_REQUEST['authors']) ? $_REQUEST['authors'] : explode(",", (string)$_REQUEST['authors'])), "isNumber") : array( );
80
-	if($ef_authors) {
81
-		$ef_authors = implode(",", array_map('intval', $ef_authors));
82
-		$query[] = "FIND_IN_SET(stories.uid, '".$ef_authors."') > 0";
83
-		$countquery[] = "FIND_IN_SET(stories.uid, '".$ef_authors."') > 0";
84
-		$scountquery[] = "FIND_IN_SET(series.uid, '".$ef_authors."') > 0";
85
-	}
86
-	if(isset($_REQUEST['catid'])) {
87
-		$catid = is_array($_REQUEST['catid']) ? $_REQUEST['catid'] : explode(",", $_REQUEST['catid']);
88
-		$catid = array_filter($catid, "isNumber");
89
-	}
90
-	if(!isset($catid)) $catid = array();
91
-	if($type == "categories" && isset($_GET['id']) && isNumber($_GET['id'])) {
92
-		$catid[] =  $_GET['id'];
93
-	}
94
-	$categories = array( );
95
-	// Get the recursive list.
96
-	if($type == "categories") $categories = $catid;
97
-	else {
98
-		foreach($catid as $cat) {
99
-			if($cat == "false" || empty($cat)) continue;
100
-			$categories = array_merge($categories, recurseCategories($cat));
101
-		}
102
-	}
103
-	// Now format the SQL
104
-	foreach($categories as $cat) {
105
-		$catstories[] = "FIND_IN_SET($cat, stories.catid) > 0 ";
106
-		$catseries[] = "FIND_IN_SET($cat, series.catid) > 0 ";
107
-	}
108
-	// Now implode the SQL list
109
-	if(!empty($catstories)) {
110
-		$query[] = "(".implode(" OR ", $catstories).")";
111
-		$countquery[] = "(".implode(" OR ", $catstories).")";
112
-		$scountquery[] = "(".implode(" OR ", $catseries).")";
113
-	}
114
-	if(empty($charid)) $charid = array( );
115
-	if(isset($_REQUEST['charid'])) {
116
-		$charid = is_array($_REQUEST['charid']) ? $_REQUEST['charid'] : explode(",", $_REQUEST['charid']);
117
-		$charid = array_filter($charid, "isNumber");
118
-	}
119
-	if(!empty($_REQUEST['charlist1']) && !in_array($_REQUEST['charlist1'], $charid)) $charid[] = $_REQUEST['charlist1'];
120
-	if(!empty($_REQUEST['charlist2']) && !in_array($_REQUEST['charlist2'], $charid)) $charid[] = $_REQUEST['charlist2'];
121
-	$charid = array_filter($charid, "isNumber");
122
-	if($charid) {
123
-		foreach($charid as $c) {
124
-			if(!empty($c)) {
125
-				$charstories[] = "FIND_IN_SET($c, stories.charid) > 0 ";
126
-				$charseries[] = "FIND_IN_SET($c, series.characters) > 0 ";
127
-			}
128
-		}
129
-		if(count($charstories) > 0) {
130
-			$query[] = "(".implode(" AND ", $charstories).")";
131
-			$countquery[] = "(".implode(" AND ", $charstories).")";
132
-		}
133
-		if(count($charseries) > 0) $scountquery[] = "(".implode(" AND ", $charseries).")";
134
-	}
135
-	$rid = array( ); $rating = array( );
136
-	if(!empty($_REQUEST['rid'])) $rid = is_array($_REQUEST['rid']) ? $_REQUEST['rid'] : explode(",", $_REQUEST['rid']);
137
-	if(!empty($_REQUEST['rating'])) $rating = is_array($_REQUEST['rating']) ? $_REQUEST['rating'] : explode(",", $_REQUEST['rating']);
138
-	$rid = array_filter(array_merge($rid, $rating), "isNumber");
139
-	if(!empty($rid)) {
140
-		$query[] = "FIND_IN_SET(stories.rid, '".(implode(",", $rid))."') > 0";
141
-		$countquery[]  = "FIND_IN_SET(stories.rid, '".(implode(",", $rid))."') > 0";
142
-	}
143
-	$complete = "all";
144
-	if(isset($_REQUEST['complete'])) {
145
-		if($_REQUEST['complete'] == 1) {
146
-			$query[] = "stories.completed = '1'";
147
-			$countquery[] = "stories.completed = '1'";
148
-			$complete = 1;
149
-		} 
150
-		else if(empty($_REQUEST['complete'])) {
151
-			$query[] = "stories.completed = '0'";
152
-			$countquery[] = "stories.completed = '0'";
153
-			$complete = 0;
154
-		} 
155
-	}
156
-	if(isset($_REQUEST['classin'])) $classin = is_array($_REQUEST['classin']) ? $_REQUEST['classin'] : array($_REQUEST['classin']);
157
-	else $classin = array( );
158
-	if(isset($_REQUEST['classex'])) $classex = is_array($_REQUEST['classex']) ? $_REQUEST['classex'] : array($_REQUEST['classex']);
159
-	else $classex = array( );
160
-	$opts = array();
161
-	$exopts = array( );
162
-	foreach($classtypelist as $id => $vars) {
163
-		if(!empty($_POST[$vars['name']])) $opts[] = $_POST[$vars['name']];
164
-		else if(isset($_GET["class_".$id])) {
165
-			$opts = !empty($opts) ? array_merge($opts, explode(",", $_GET["class_".$id])) : array($_GET["class_".$id]);
166
-		}
167
-		else if(!empty($_REQUEST[$vars["name"]])) $opts = is_array($_REQUEST[$vars["name"]]) ? $_REQUEST[$vars["name"]] : array($_REQUEST[$vars["name"]]);
168
-		if(!empty($opts)) $classin = array_merge($classin, $opts);
169
-		if(isset($_POST["exclass_".$id])) $exopts = $_POST["exclass_".$id];
170
-		else if(isset($_GET["exclass_".$id])) {
171
-			$exopts = !empty($exopts) ? array_merge($exopts, explode(",", $_GET["exclass_".$id])) : array($_GET["exclass_".$id]);
172
-		}
173
-		if(!empty($exopts)) $classex = array_merge($classex, $exopts);
174
-		unset($opts, $exopts);
175
-	}
176
-	$classin = array_filter($classin, "isNumber");
177
-	$classex = array_filter($classex, "isNumber");
178
-	if($classin) {
179
-		foreach($classin as $class) {
180
-			if(empty($class)) continue;
181
-			$query[] = "FIND_IN_SET($class, stories.classes) > 0";
182
-			$countquery[] = "FIND_IN_SET($class, stories.classes) > 0";
183
-			$scountquery[] = "FIND_IN_SET($class, series.classes) > 0";
184
-		}
185
-	}
186
-	if($classex) {
187
-		foreach($classex as $class) {
188
-			$query[] = "FIND_IN_SET($class, stories.classes) = 0";
189
-			$countquery[] = "FIND_IN_SET($class, stories.classes) = 0";
190
-			$scountquery[] = "FIND_IN_SET($class, series.classes) = 0";
191
-		}
192
-	}
193
-	// Begin story length
194
-	unset($wordcount);
195
-	$wordlow = isset($_REQUEST['wordlow']) && isNumber($_REQUEST['wordlow']) ? $_REQUEST['wordlow'] : "-500";
196
-	$wordhigh = isset($_REQUEST['wordhigh']) && isNumber($_REQUEST['wordhigh']) ? $_REQUEST['wordhigh'] : "1000000";
197
-	if($wordlow != "-500") $wordcount = "stories.wordcount > ".$wordlow;
198
-	if($wordhigh != "1000000") 
199
-	{
200
-		if($wordcount) $wordcount .= " AND stories.wordcount < ".$wordhigh;
201
-		else $wordcount = "stories.wordcount < ".$wordhigh;
202
-	}
203
-	if($wordhigh < $wordlow) $wordcount = "stories.wordcount < ".$wordlow;
204
-	if($wordlow != "-500" && $wordhigh != "1000000" && isset($_POST['submit'])) {
205
-		$query[] = "($wordcount)";
206
-		$countquery[] = "($wordcount)";
207
-	}
208
-	// End wordcount
209
-	$codeblocks = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_codeblocks WHERE code_type = 'browseterms'");
210
-	while($code = dbassoc($codeblocks)) {
211
-		eval($code['code_text']);
212
-	}
213
-	$storyquery = count($query) > 0 ? " AND ".implode(" AND ", $query) : "";
214
-	$countquery = count($countquery) > 0 ? " AND ".implode(" AND ", $countquery) : "";
215
-	$seriesquery = count($scountquery) > 0 ? implode(" AND ", $scountquery) : false;
216
-	$termArray = array_merge($_GET, $_POST);
217
-	$termsList = array();
218
-	foreach($termArray as $term => $value) {
219
-		if($term == "submit" || $term == "go" || $term == "offset" || $term == "type" || ($term != "complete" && empty($value))) continue;
220
-		$termsList[$term] = "$term=".(is_array($value) ? implode(",", array_map(function($v) { return htmlspecialchars($v, ENT_QUOTES); }, $value)) : htmlspecialchars($value, ENT_QUOTES));
221
-	}
222
-	$terms = "type=$type".(count($termsList) ? "&amp;" : "").implode("&amp;", $termsList);
223
-// End query strings
224
-	$panelquery = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_panels WHERE panel_name = '".escapestring($type)."' AND panel_type = 'B' LIMIT 1");
225
-	if(dbnumrows($panelquery)) {
226
-		$numrows = 0;
227
-		$panel = dbassoc($panelquery);
228
-		if($panel['panel_url'] && file_exists(_BASEDIR.$panel['panel_url'])) include($panel['panel_url']);
229
-		else if(file_exists("browse/{$type}.php")) include("browse/{$type}.php");
230
-		else $output .= write_error(_ERROR);
231
-	}
232
-	else $output .= write_error(_ERROR);
233
-	$terms = implode("&amp;", $termsList);
234
-// Other results
235
-	$otherresults = array( );
236
-	$codeblocks = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_codeblocks WHERE code_type = 'otherresults'");
237
-	while($code = dbassoc($codeblocks)) {
238
-		eval($code['code_text']);
239
-	}
240
-	if($type != "series" && !empty($seriesquery)) {
241
-		$seriesresults = dbquery(_SERIESCOUNT." WHERE ".$seriesquery);
242
-		list($scount) = dbrow($seriesresults);
243
-		if($scount > 0) $otherresults[] = "<a href='browse.php?type=series&amp;$terms'>$scount "._SERIES."</a>";
244
-	}
245
-	if(count($otherresults) > 0 && $type != "titles") $tpl->assign("otherresults", "<div id='otherresults'><span class='label'>"._OTHERRESULTS.":</span> ".implode(", ", $otherresults)."</div>");
246
-// build our sort menus. if there aren't any stories don't bother with these since they won't be used
247
-	if(!empty($numrows) || isset($_POST['go'])) {
248
-		$tpl->newBlock("sortform");
249
-		$tpl->assign("sortbegin", "<form style=\"margin:0\" method=\"POST\" id=\"form\" enctype=\"multipart/form-data\" action=\"browse.php?type=$type&amp;$terms\">");
250
-		if($catlist && !in_array("categories", $disablesorts)) {
251
-			if(count($catid) > 0) $thiscat = $catid[0];
252
-			else $thiscat = -1;
253
-			$catmenu = "<select class=\"textbox\" name=\"catid\" id=\"catid\" onChange=\"browseCategories('catid')\"><option value=\"-1\">".($thiscat > 0 ? _BACK2CATS : _CATEGORIES)."</option>\n";
254
-			foreach($catlist as $cat => $info) {
255
-				if($info['pid'] == $thiscat || $cat == $thiscat) $catmenu .= "<option value=\"$cat\"".($thiscat == $cat ? " selected" : "").">".$info['name']."</option>\n";
256
-			}
257
-			$catmenu .= "</select>\n";
258
-			$tpl->assign("categorymenu", $catmenu);
259
-		}
260
-		if(count($charlist) > 0 && !in_array("characters", $disablesorts)) {
261
-			$charactermenu1 = "<select class=\"textbox\" name=\"charlist1\" id=\"charlist1\">\n";
262
-			$charactermenu1 .= "<option value=\"0\">"._CHARACTERS."</option>\n";
263
-			$charactermenu2 = "<select class=\"textbox\" name=\"charlist2\" id=\"charlist2\">\n";
264
-			$charactermenu2 .= "<option value=\"0\">"._CHARACTERS."</option>\n";
265
-			$categories[] = -1;
266
-			$categories = array_merge($categories, $catid);
267
-			foreach($charlist as $char => $info) {
268
-				if(is_array($categories) && in_array($info['catid'], $categories)) {
269
-					$charactermenu1 .= "<option value=\"$char\"";
270
-					if(isset($charid[0]) && $charid[0] == $char)
271
-						$charactermenu1 .= " selected";
272
-					$charactermenu1 .= ">".$info['name']."</option>\n";
273
-					$charactermenu2 .= "<option value=\"$char\"";
274
-					if(isset($charid[1]) && $charid[1] == $char)
275
-						$charactermenu2 .= " selected";
276
-					$charactermenu2 .= ">".$info['name']."</option>\n";
277
-				}
278
-			}
279
-			$charactermenu1 .= "</select>";
280
-			$charactermenu2 .= "</select>";
281
-			if($type != "characters") $tpl->assign("charactermenu1"   , $charactermenu1 );
282
-			$tpl->assign("charactermenu2"   , $charactermenu2 );
283
-		}
284
-		// To avoid throwing warnings we need to define $classopts and tell it how many elements it should have.
285
-		if(!in_array("classes", $disablesorts)) {
286
-			$classopts = array();
287
-			foreach($classlist as $id => $vars) {
288
-				if(empty($classopts[$vars['type']])) $classopts[$vars['type']] = "";
289
-				$classopts[$vars['type']] = $classopts[$vars['type']]."<option value=\"$id\"".(isset($classin) && is_array($classin) && in_array($id, $classin) ? " selected" : "").">".$vars['name']."</option>\n";
290
-			}
291
-			$allclasses = "";
292
-			foreach($classopts as $type => $opts) {
293
-				if(empty($type) || in_array($classtypelist[$type]['name'], $disablesorts)) continue; // Because of the way we defined $classopts we need to skip the empty first element.
294
-				$opts = "<option value=\"\">".$classtypelist[$type]['title']."</option>$opts";
295
-				$tpl->assign($classtypelist[$type]['name']."menu", "<select name=\"".$classtypelist["$type"]['name']."\">\n$opts</select>\n");
296
-				$allclasses .= "<select class=\"textbox\" name=\"".$classtypelist["$type"]['name']."\">\n$opts</select>\n ";
297
-			}
298
-			$tpl->assign("classmenu", $allclasses);
299
-		}
300
-		if(!in_array("ratings", $disablesorts)) {
301
-			$ratingmenu = "<select class=\"textbox\" name=\"rating\">\n";
302
-			$ratingmenu .= "<option value=\"0\">"._RATINGS."</option>\n";
303
-			if(!isset($ratingslist)) $ratingslist = array( );
304
-			foreach($ratingslist as $r => $rinfo) {
305
-				$ratingmenu .= "<option value=\"".$r."\"";
306
-				if(isset($rid) && in_array($r, $rid))
307
-					$ratingmenu .= " selected";
308
-				$ratingmenu .= ">".$rinfo['name']."</option>\n";
309
-			}
310
-			$ratingmenu .= "</select>\n";
311
-			$tpl->assign("ratingmenu"   , $ratingmenu );
312
-		}
313
-		if(!in_array("sorts", $disablesorts)) $tpl->assign("sortmenu", "<select class=\"textbox\" name=\"sort\">\n<option value=''>"._SORT."</option><option value=\"alpha\"".(!$defaultsort ? " selected" : "").">"._ALPHA."</option>\n<option value=\"update\"".($defaultsort == 1 ? " selected" : "").">"._MOSTRECENT."</option>\n</select>\n");
314
-		if(!in_array("complete", $disablesorts)) $tpl->assign("completemenu", "<select class=\"textbox\" name=\"complete\">\n<option value=\"all\"".($complete == "all" ? " selected" : "").">"._ALLSTORIES."</option>\n<option value=\"1\"".($complete == 1 ? " selected" : "").">"._COMPLETEONLY."</option>\n<option value=\"0\"".($complete && $complete != "all" && $complete != 1 ? " selected" : "").">"._WIP."</option>\n</select>\n");
315
-		$codeblocks = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_codeblocks WHERE code_type = 'browsesorts'");
316
-		while($code = dbassoc($codeblocks)) {
317
-			eval($code['code_text']);
318
-		}
319
-		$tpl->assign("sortend"   , "<INPUT type=\"submit\" class=\"button\" name=\"go\" value=\""._GO."\"></form>");
320
-		$tpl->gotoBlock("_ROOT");
321
-	}
322
-}
323
-else  {
324
-	$output = "<div id='pagetitle'>"._BROWSE."</div>";
325
-	$panelquery = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_panels WHERE panel_hidden != '1' AND panel_level ".(isMEMBER ? " < 2" : "= '0'")." AND panel_type = 'B' ORDER BY panel_type DESC, panel_order ASC, panel_title ASC");
326
-	while($panel = dbassoc($panelquery)) {
327
-		$browsetypes[$panel['panel_title']] =  "<a href=\"browse.php?type=".$panel['panel_name']."\">".$panel['panel_title']."</a><br />\n";
328
-	}
329
-	
330
-	foreach($classtypelist as $type => $info) {
331
-		$browsetypes[$info['title']] = "<a href='browse.php?type=class&amp;type_id=$type'>".$info['title']."</a><br />\n";
332
-	}
333
-	ksort($browsetypes);
334
-	$total = count($browsetypes);
335
-	$count = 0;
336
-	$column = 1;
337
-	$list = floor($total / $displaycolumns);
338
-	if($total % $displaycolumns != 0) $list++;
339
-	$output .= "<div id=\"columncontainer\"><div id=\"browseblock\">".($displaycolumns ? "<div class=\"column\">" : "");
340
-	foreach($browsetypes as $link) {
341
-		$count++;
342
-		$output .= $link;
343
-		if( $count >= $list && $column != $displaycolumns) {
344
-			$output .= "</div><div class=\"column\">";
345
-			if($total % $displaycolumns == $column) $list--;
346
-			$column++;
347
-			$count = 0;
348
-		}
349
-	}
350
-	$output .= "</div>".($displaycolumns ? "</div>" : "")."<div class='cleaner'>&nbsp;</div></div>";
351
-}
352
-$tpl->assign("output", $output);
353
-$tpl->printToScreen( );
354
-?>
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
+$current = "browse";
24
+$displayform = 1;
25
+
26
+include ("header.php");
27
+
28
+if(file_exists("$skindir/browse.tpl")) $tpl = new TemplatePower( "$skindir/browse.tpl" );
29
+else $tpl = new TemplatePower("default_tpls/browse.tpl");
30
+if(file_exists("$skindir/listings.tpl")) $tpl->assignInclude("listings", "./$skindir/listings.tpl");
31
+else $tpl->assignInclude( "listings", "./default_tpls/listings.tpl" );
32
+$tpl->assignInclude( "header", "./$skindir/header.tpl" );
33
+$tpl->assignInclude( "footer", "./$skindir/footer.tpl" );
34
+
35
+include("includes/pagesetup.php");
36
+if(isset($_GET['type']) && is_string($_GET['type']) && preg_match('/^[a-z0-9_]{1,40}$/i', $_GET['type'])) $type = $_GET['type'];
37
+else $type = false;
38
+
39
+if($type) {
40
+	$query = array();
41
+	$countquery = array();
42
+	$scountquery = array();
43
+	$searchtype = (isset($_REQUEST['searchtype']) ? $_REQUEST['searchtype'] : "simple");
44
+	//if(!is_string($searchtype) || (isset($_REQUEST['searchterm']) && !is_string($_REQUEST['searchterm']))) errorExit(_ERROR); // this crashes the series results.
45
+	//if(!in_array($searchtype, array("simple", "title", "summary", "penname", "advanced", "fulltext"), true)) errorExit(_ERROR); // this crashes the series results.
46
+	$searchterm = isset($_REQUEST['searchterm']) && $_REQUEST['searchterm'] !== "" ? $_REQUEST['searchterm'] : false;
47
+	//if($searchtype == "fulltext" || ($searchtype == "simple" && $searchterm !== false)) errorExit(_ERROR); // this crashes the series results.
48
+	if(($searchtype != "simple" && $searchtype != "advanced" && ($searchterm === false || trim($searchterm) === "")) || ($searchterm !== false && strlen($searchterm) < 3)) errorExit(_SEARCHTERMTOOSHORT);
49
+	if($searchterm !== false) $searchterm = escapestring(strtr($searchterm, array("!" => "!!", "%" => "!%", "_" => "!_")));
50
+	$disablesorts = array( );
51
+	if($searchterm !== false) {
52
+		if($searchtype == "title") {
53
+			$query[] = "stories.title LIKE '%$searchterm%' ESCAPE '!'";
54
+			$countquery[] = "stories.title LIKE '%$searchterm%' ESCAPE '!'";
55
+			$scountquery[] = "series.title LIKE '%$searchterm%' ESCAPE '!'";
56
+		}
57
+		if($searchtype == "summary") {
58
+			$query[] = "stories.summary LIKE '%$searchterm%' ESCAPE '!'";
59
+			$countquery[] = "stories.summary LIKE '%$searchterm%' ESCAPE '!'";
60
+			$scountquery[] = "series.summary LIKE '%$searchterm%' ESCAPE '!'";
61
+		}
62
+		if($searchtype == "penname") {
63
+			$authorquery = dbquery(_MEMBERLIST." WHERE "._PENNAMEFIELD." LIKE '%$searchterm%' ESCAPE '!' GROUP BY "._UIDFIELD);
64
+			$authorlist = array();
65
+			while($auth = dbassoc($authorquery)) {
66
+				$authorlist[] = $auth['uid'];
67
+			}
68
+			if(count($authorlist) > 0) {
69
+				$query[] = "FIND_IN_SET(stories.uid, '".implode(",",$authorlist)."') > 0";
70
+				$countquery[] = "FIND_IN_SET(stories.uid, '".implode(",",$authorlist)."') > 0";
71
+				$scountquery[] = "FIND_IN_SET(series.uid, '".implode(",",$authorlist)."') > 0";
72
+			}
73
+			else {
74
+				$query[] = "1 = 0"; $countquery[] = "1 = 0"; $scountquery[] = "1 = 0";
75
+			}
76
+		}
77
+		if($searchtype == "advanced" || !$searchtype) {
78
+			$query[] = "(stories.summary LIKE '%$searchterm%' ESCAPE '!' OR stories.title LIKE '%$searchterm%' ESCAPE '!') ";
79
+			$countquery[] = "(stories.summary LIKE '%$searchterm%' ESCAPE '!' OR stories.title LIKE '%$searchterm%' ESCAPE '!') ";
80
+			$scountquery[] = "(summary LIKE '%$searchterm%' ESCAPE '!' OR title LIKE '%$searchterm%' ESCAPE '!') ";
81
+		}
82
+	}
83
+	$ef_authors = isset($_REQUEST['authors']) ? array_filter(array_map('trim', is_array($_REQUEST['authors']) ? $_REQUEST['authors'] : explode(",", (string)$_REQUEST['authors'])), "isNumber") : array( );
84
+	if($ef_authors) {
85
+		$ef_authors = implode(",", array_map('intval', $ef_authors));
86
+		$query[] = "FIND_IN_SET(stories.uid, '".$ef_authors."') > 0";
87
+		$countquery[] = "FIND_IN_SET(stories.uid, '".$ef_authors."') > 0";
88
+		$scountquery[] = "FIND_IN_SET(series.uid, '".$ef_authors."') > 0";
89
+	}
90
+	if(isset($_REQUEST['catid'])) {
91
+		$catid = is_array($_REQUEST['catid']) ? $_REQUEST['catid'] : explode(",", $_REQUEST['catid']);
92
+		$catid = array_filter($catid, "isNumber");
93
+	}
94
+	if(!isset($catid)) $catid = array();
95
+	if($type == "categories" && isset($_GET['id']) && isNumber($_GET['id'])) {
96
+		$catid[] =  $_GET['id'];
97
+	}
98
+	$categories = array( );
99
+	// Get the recursive list.
100
+	if($type == "categories") $categories = $catid;
101
+	else {
102
+		foreach($catid as $cat) {
103
+			if($cat == "false" || empty($cat)) continue;
104
+			$categories = array_merge($categories, recurseCategories($cat));
105
+		}
106
+	}
107
+	// Now format the SQL
108
+	foreach($categories as $cat) {
109
+		$catstories[] = "FIND_IN_SET($cat, stories.catid) > 0 ";
110
+		$catseries[] = "FIND_IN_SET($cat, series.catid) > 0 ";
111
+	}
112
+	// Now implode the SQL list
113
+	if(!empty($catstories)) {
114
+		$query[] = "(".implode(" OR ", $catstories).")";
115
+		$countquery[] = "(".implode(" OR ", $catstories).")";
116
+		$scountquery[] = "(".implode(" OR ", $catseries).")";
117
+	}
118
+	if(empty($charid)) $charid = array( );
119
+	if(isset($_REQUEST['charid'])) {
120
+		$charid = is_array($_REQUEST['charid']) ? $_REQUEST['charid'] : explode(",", $_REQUEST['charid']);
121
+		$charid = array_filter($charid, "isNumber");
122
+	}
123
+	if(!empty($_REQUEST['charlist1']) && isNumber($_REQUEST['charlist1']) && !in_array($_REQUEST['charlist1'], $charid)) $charid[] = $_REQUEST['charlist1'];
124
+	if(!empty($_REQUEST['charlist2']) && isNumber($_REQUEST['charlist2']) && !in_array($_REQUEST['charlist2'], $charid)) $charid[] = $_REQUEST['charlist2'];
125
+	if($charid) {
126
+		foreach($charid as $c) {
127
+			if(!empty($c)) {
128
+				$charstories[] = "FIND_IN_SET($c, stories.charid) > 0 ";
129
+				$charseries[] = "FIND_IN_SET($c, series.characters) > 0 ";
130
+			}
131
+		}
132
+		if(!empty($charstories)) {
133
+			$query[] = "(".implode(" AND ", $charstories).")";
134
+			$countquery[] = "(".implode(" AND ", $charstories).")";
135
+		}
136
+		if(!empty($charseries)) $scountquery[] = "(".implode(" AND ", $charseries).")";
137
+	}
138
+	$rid = array( ); $rating = array( );
139
+	if(!empty($_REQUEST['rid'])) $rid = is_array($_REQUEST['rid']) ? $_REQUEST['rid'] : explode(",", (string)$_REQUEST['rid']);
140
+	if(!empty($_REQUEST['rating'])) $rating = is_array($_REQUEST['rating']) ? $_REQUEST['rating'] : explode(",", (string)$_REQUEST['rating']);
141
+	$rid = array_values(array_filter(array_merge((array)$rid, (array)$rating), "isNumber")); // eFiction 4: numbers only
142
+	if(!empty($rid)) {
143
+		$query[] = "FIND_IN_SET(stories.rid, '".(implode(",", $rid))."') > 0";
144
+		$countquery[]  = "FIND_IN_SET(stories.rid, '".(implode(",", $rid))."') > 0";
145
+	}
146
+	$complete = "all";
147
+	if(isset($_REQUEST['complete'])) {
148
+		if($_REQUEST['complete'] == 1) {
149
+			$query[] = "stories.completed = '1'";
150
+			$countquery[] = "stories.completed = '1'";
151
+			$complete = 1;
152
+		} 
153
+		else if(empty($_REQUEST['complete'])) {
154
+			$query[] = "stories.completed = '0'";
155
+			$countquery[] = "stories.completed = '0'";
156
+			$complete = 0;
157
+		} 
158
+	}
159
+	if(isset($_REQUEST['classin'])) $classin = ef_browse_ids($_REQUEST['classin']);
160
+	else $classin = array( );
161
+	if(isset($_REQUEST['classex'])) $classex = ef_browse_ids($_REQUEST['classex']);
162
+	else $classex = array( );
163
+	$opts = array();
164
+	$exopts = array( );
165
+	foreach($classtypelist as $id => $vars) {
166
+		$opts = array(); $exopts = array();
167
+		if(isset($_REQUEST["class_".$id])) $opts = ef_browse_ids($_REQUEST["class_".$id]);
168
+		else if(isset($_REQUEST[$vars["name"]])) $opts = ef_browse_ids($_REQUEST[$vars["name"]]);
169
+		if(!empty($opts)) $classin = array_merge($classin, $opts);
170
+		if(isset($_REQUEST["exclass_".$id])) $exopts = ef_browse_ids($_REQUEST["exclass_".$id]);
171
+		if(!empty($exopts)) $classex = array_merge($classex, $exopts);
172
+		unset($opts, $exopts);
173
+	}
174
+	$classin = array_filter($classin, "isNumber");
175
+	$classex = array_filter($classex, "isNumber");
176
+	if($classin) {
177
+		foreach($classin as $class) {
178
+			if(empty($class)) continue;
179
+			$query[] = "FIND_IN_SET($class, stories.classes) > 0";
180
+			$countquery[] = "FIND_IN_SET($class, stories.classes) > 0";
181
+			$scountquery[] = "FIND_IN_SET($class, series.classes) > 0";
182
+		}
183
+	}
184
+	if($classex) {
185
+		foreach($classex as $class) {
186
+			$query[] = "FIND_IN_SET($class, stories.classes) = 0";
187
+			$countquery[] = "FIND_IN_SET($class, stories.classes) = 0";
188
+			$scountquery[] = "FIND_IN_SET($class, series.classes) = 0";
189
+		}
190
+	}
191
+	// Begin story length
192
+	unset($wordcount);
193
+	$wordlow = isset($_REQUEST['wordlow']) && preg_match('/^-?[0-9]{1,9}$/', (string)$_REQUEST['wordlow']) ? $_REQUEST['wordlow'] : "-500";
194
+	$wordhigh = isset($_REQUEST['wordhigh']) && preg_match('/^[0-9]{1,9}$/', (string)$_REQUEST['wordhigh']) ? $_REQUEST['wordhigh'] : "1000000";
195
+	if($wordlow != "-500") $wordcount = "stories.wordcount > ".$wordlow;
196
+	if($wordhigh != "1000000") 
197
+	{
198
+		if($wordcount) $wordcount .= " AND stories.wordcount < ".$wordhigh;
199
+		else $wordcount = "stories.wordcount < ".$wordhigh;
200
+	}
201
+	if($wordhigh < $wordlow) $wordcount = "stories.wordcount < ".$wordlow;
202
+	if($wordlow != "-500" && $wordhigh != "1000000" && isset($_POST['submit'])) {
203
+		$query[] = "($wordcount)";
204
+		$countquery[] = "($wordcount)";
205
+	}
206
+	// End wordcount
207
+	$codeblocks = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_codeblocks WHERE code_type = 'browseterms'");
208
+	while($code = dbassoc($codeblocks)) {
209
+		eval($code['code_text']);
210
+	}
211
+	$storyquery = count($query) > 0 ? " AND ".implode(" AND ", $query) : "";
212
+	$countquery = count($countquery) > 0 ? " AND ".implode(" AND ", $countquery) : "";
213
+	$seriesquery = count($scountquery) > 0 ? implode(" AND ", $scountquery) : false;
214
+	$termArray = array_merge($_GET, $_POST);
215
+	$termsList = array();
216
+	foreach($termArray as $term => $value) {
217
+		if($term == "submit" || $term == "go" || $term == "offset" || $term == "type" || ($term != "complete" && empty($value))) continue;
218
+		$termsList[$term] = ef_term($term, $value);
219
+	}
220
+	$terms = ef_term("type", $type).(count($termsList) ? "&amp;" : "").implode("&amp;", $termsList);
221
+// End query strings
222
+	$panelquery = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_panels WHERE panel_name = '$type' AND panel_type = 'B' LIMIT 1");
223
+	if(dbnumrows($panelquery)) {
224
+		$numrows = 0;
225
+		$panel = dbassoc($panelquery);
226
+		if($panel['panel_url'] && file_exists(_BASEDIR.$panel['panel_url'])) include($panel['panel_url']);
227
+		else if(file_exists("browse/{$type}.php")) include("browse/{$type}.php");
228
+		else $output .= write_error(_ERROR);
229
+	}
230
+	else $output .= write_error(_ERROR);
231
+	$terms = implode("&amp;", $termsList);
232
+// Other results
233
+	$otherresults = array( );
234
+	$codeblocks = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_codeblocks WHERE code_type = 'otherresults'");
235
+	while($code = dbassoc($codeblocks)) {
236
+		eval($code['code_text']);
237
+	}
238
+	if($type != "series" && !empty($seriesquery)) {
239
+		$seriesresults = dbquery(_SERIESCOUNT." WHERE ".$seriesquery);
240
+		list($scount) = dbrow($seriesresults);
241
+		if($scount > 0) $otherresults[] = "<a href='browse.php?type=series&amp;$terms'>$scount "._SERIES."</a>";
242
+	}
243
+	if(count($otherresults) > 0 && $type != "titles") $tpl->assign("otherresults", "<div id='otherresults'><span class='label'>"._OTHERRESULTS.":</span> ".implode(", ", $otherresults)."</div>");
244
+// build our sort menus. if there aren't any stories don't bother with these since they won't be used
245
+	if(!empty($numrows) || isset($_POST['go'])) {
246
+		$tpl->newBlock("sortform");
247
+		$tpl->assign("sortbegin", "<form style=\"margin:0\" method=\"POST\" id=\"form\" enctype=\"multipart/form-data\" action=\"browse.php?type=$type&amp;$terms\">");
248
+		if($catlist && !in_array("categories", $disablesorts)) {
249
+			if(count($catid) > 0) $thiscat = $catid[0];
250
+			else $thiscat = -1;
251
+			$catmenu = "<select class=\"textbox\" name=\"catid\" id=\"catid\" onChange=\"browseCategories('catid')\"><option value=\"-1\">".($thiscat > 0 ? _BACK2CATS : _CATEGORIES)."</option>\n";
252
+			foreach($catlist as $cat => $info) {
253
+				if($info['pid'] == $thiscat || $cat == $thiscat) $catmenu .= "<option value=\"$cat\"".($thiscat == $cat ? " selected" : "").">".$info['name']."</option>\n";
254
+			}
255
+			$catmenu .= "</select>\n";
256
+			$tpl->assign("categorymenu", $catmenu);
257
+		}
258
+		if(count($charlist) > 0 && !in_array("characters", $disablesorts)) {
259
+			$charactermenu1 = "<select class=\"textbox\" name=\"charlist1\" id=\"charlist1\">\n";
260
+			$charactermenu1 .= "<option value=\"0\">"._CHARACTERS."</option>\n";
261
+			$charactermenu2 = "<select class=\"textbox\" name=\"charlist2\" id=\"charlist2\">\n";
262
+			$charactermenu2 .= "<option value=\"0\">"._CHARACTERS."</option>\n";
263
+			$categories[] = -1;
264
+			$categories = array_merge($categories, $catid);
265
+			foreach($charlist as $char => $info) {
266
+				if(is_array($categories) && in_array($info['catid'], $categories)) {
267
+					$charactermenu1 .= "<option value=\"$char\"";
268
+					if(isset($charid[0]) && $charid[0] == $char)
269
+						$charactermenu1 .= " selected";
270
+					$charactermenu1 .= ">".$info['name']."</option>\n";
271
+					$charactermenu2 .= "<option value=\"$char\"";
272
+					if(isset($charid[1]) && $charid[1] == $char)
273
+						$charactermenu2 .= " selected";
274
+					$charactermenu2 .= ">".$info['name']."</option>\n";
275
+				}
276
+			}
277
+			$charactermenu1 .= "</select>";
278
+			$charactermenu2 .= "</select>";
279
+			if($type != "characters") $tpl->assign("charactermenu1"   , $charactermenu1 );
280
+			$tpl->assign("charactermenu2"   , $charactermenu2 );
281
+		}
282
+		// To avoid throwing warnings we need to define $classopts and tell it how many elements it should have.
283
+		if(!in_array("classes", $disablesorts)) {
284
+			$classopts = array();
285
+			foreach($classlist as $id => $vars) {
286
+				if(empty($classopts[$vars['type']])) $classopts[$vars['type']] = "";
287
+				$classopts[$vars['type']] = $classopts[$vars['type']]."<option value=\"$id\"".(isset($classin) && is_array($classin) && in_array($id, $classin) ? " selected" : "").">".$vars['name']."</option>\n";
288
+			}
289
+			$allclasses = "";
290
+			foreach($classopts as $type => $opts) {
291
+				if(empty($type) || in_array($classtypelist[$type]['name'], $disablesorts)) continue; // Because of the way we defined $classopts we need to skip the empty first element.
292
+				$opts = "<option value=\"\">".$classtypelist[$type]['title']."</option>$opts";
293
+				$tpl->assign($classtypelist[$type]['name']."menu", "<select name=\"".$classtypelist["$type"]['name']."\">\n$opts</select>\n");
294
+				$allclasses .= "<select class=\"textbox\" name=\"".$classtypelist["$type"]['name']."\">\n$opts</select>\n ";
295
+			}
296
+			$tpl->assign("classmenu", $allclasses);
297
+		}
298
+		if(!in_array("ratings", $disablesorts)) {
299
+			$ratingmenu = "<select class=\"textbox\" name=\"rating\">\n";
300
+			$ratingmenu .= "<option value=\"0\">"._RATINGS."</option>\n";
301
+			if(!isset($ratingslist)) $ratingslist = array( );
302
+			foreach($ratingslist as $r => $rinfo) {
303
+				$ratingmenu .= "<option value=\"".$r."\"";
304
+				if(isset($rid) && in_array($r, $rid))
305
+					$ratingmenu .= " selected";
306
+				$ratingmenu .= ">".$rinfo['name']."</option>\n";
307
+			}
308
+			$ratingmenu .= "</select>\n";
309
+			$tpl->assign("ratingmenu"   , $ratingmenu );
310
+		}
311
+		if(!in_array("sorts", $disablesorts)) $tpl->assign("sortmenu", "<select class=\"textbox\" name=\"sort\">\n<option value=''>"._SORT."</option><option value=\"alpha\"".(!$defaultsort ? " selected" : "").">"._ALPHA."</option>\n<option value=\"update\"".($defaultsort == 1 ? " selected" : "").">"._MOSTRECENT."</option>\n</select>\n");
312
+		if(!in_array("complete", $disablesorts)) $tpl->assign("completemenu", "<select class=\"textbox\" name=\"complete\">\n<option value=\"all\"".($complete == "all" ? " selected" : "").">"._ALLSTORIES."</option>\n<option value=\"1\"".($complete == 1 ? " selected" : "").">"._COMPLETEONLY."</option>\n<option value=\"0\"".($complete && $complete != "all" && $complete != 1 ? " selected" : "").">"._WIP."</option>\n</select>\n");
313
+		$codeblocks = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_codeblocks WHERE code_type = 'browsesorts'");
314
+		while($code = dbassoc($codeblocks)) {
315
+			eval($code['code_text']);
316
+		}
317
+		$tpl->assign("sortend"   , "<INPUT type=\"submit\" class=\"button\" name=\"go\" value=\""._GO."\"></form>");
318
+		$tpl->gotoBlock("_ROOT");
319
+	}
320
+}
321
+else  {
322
+	$output = "<div id='pagetitle'>"._BROWSE."</div>";
323
+	$panelquery = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_panels WHERE panel_hidden != '1' AND panel_level ".(isMEMBER ? " < 2" : "= '0'")." AND panel_type = 'B' ORDER BY panel_type DESC, panel_order ASC, panel_title ASC");
324
+	while($panel = dbassoc($panelquery)) {
325
+		$browsetypes[$panel['panel_title']] =  "<a href=\"browse.php?type=".$panel['panel_name']."\">".$panel['panel_title']."</a><br />\n";
326
+	}
327
+	
328
+	foreach($classtypelist as $type => $info) {
329
+		$browsetypes[$info['title']] = "<a href='browse.php?type=class&amp;type_id=$type'>".$info['title']."</a><br />\n";
330
+	}
331
+	ksort($browsetypes);
332
+	$total = count($browsetypes);
333
+	$count = 0;
334
+	$column = 1;
335
+	$list = floor($total / $displaycolumns);
336
+	if($total % $displaycolumns != 0) $list++;
337
+	$output .= "<div id=\"columncontainer\"><div id=\"browseblock\">".($displaycolumns ? "<div class=\"column\">" : "");
338
+	foreach($browsetypes as $link) {
339
+		$count++;
340
+		$output .= $link;
341
+		if( $count >= $list && $column != $displaycolumns) {
342
+			$output .= "</div><div class=\"column\">";
343
+			if($total % $displaycolumns == $column) $list--;
344
+			$column++;
345
+			$count = 0;
346
+		}
347
+	}
348
+	$output .= "</div>".($displaycolumns ? "</div>" : "")."<div class='cleaner'>&nbsp;</div></div>";
349
+}
350
+$tpl->assign("output", $output);
351
+$tpl->printToScreen( );
352
+
353
+function ef_browse_ids($value) {
354
+	$values = is_array($value) ? $value : explode(",", $value);
355
+	foreach($values as $v) {
356
+		if(!is_string($v) || ($v !== "" && !isNumber($v))) errorExit(_ERROR);
357
+	}
358
+	return array_values(array_filter($values, "isNumber"));
359
+}
360
+
361
+// Normally, this section would be in the includes/corefunctions.php file, but that is not getting updated for a while.
362
+
363
+// Function to format a URL into a clickable link
364
+// One "name=value" pair for a link that repeats the visitor's search or browse terms.
365
+// Names and values are URL-encoded and the result is safe inside an HTML attribute; arrays become a comma list.
366
+function ef_term($name, $value) {
367
+	if(is_array($value)) $value = implode(",", array_map(function($v) { return is_array($v) ? "" : (string)$v; }, $value));
368
+	return htmlspecialchars(rawurlencode((string)$name)."=".rawurlencode((string)$value), ENT_QUOTES);
369
+}
370
+
371
+// The current query string, minus the given keys, as "&amp;a=b&amp;c=d" (empty when nothing is left).
372
+function ef_query_terms($skip = array()) {
373
+	$out = "";
374
+	foreach($_GET as $k => $v) if(!in_array((string)$k, $skip, true)) $out .= "&amp;".ef_term($k, $v);
375
+	return $out;
376
+}
377
+?>
... ...
@@ -13,12 +13,13 @@ include("../includes/queries.php");
13 13
 
14 14
 $catid = isset($_GET['catid']) && preg_match("/^[0-9]+$/", $_GET['catid']) ? $_GET['catid'] : -1;
15 15
 $cats = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_categories WHERE parentcatid = '$catid' OR catid = '$catid' ORDER BY leveldown, displayorder");
16
-echo "var el = '".$_GET['element']."';\n";
16
+if(!isset($_GET['element']) || !preg_match('/^[A-Za-z_][A-Za-z0-9_.-]*$/', $_GET['element'])) $_GET['element'] = '';
17
+echo "var el = '".addslashes($_GET['element'])."';\n";
17 18
 $x = 0;
18 19
 $find = array ('"', chr(150), chr(147), chr(148), chr(146));
19 20
 $replace = array ('\"', "-", "\"", "\"", "'");
20 21
 while($category = dbassoc($cats)) {
21
-	echo "categories[$x] = new category(".$category['parentcatid'].", ".$category['catid'].", \"". str_replace($find, $replace, stripslashes($category['category']))."\", ".$category['locked'].", ".$category['displayorder'].");\r\n";
22
+	echo "categories[$x] = new category(".(int)$category['parentcatid'].", ".(int)$category['catid'].", \"".urlencode(str_replace($find, $replace, stripslashes((string)$category['category'])))."\", ".$category['locked'].", ".$category['displayorder'].");\r\n";
22 23
 	$x++;
23 24
 }
24 25
 ?>
... ...
@@ -43,49 +43,50 @@ include("includes/pagesetup.php");
43 43
 
44 44
 
45 45
 $searchtype = (isset($_REQUEST['searchtype']) ? $_REQUEST['searchtype'] : "simple");
46
-$searchterm =  (isset($_REQUEST['searchterm']) ? escapestring($_REQUEST['searchterm']) : false);
46
+$rawterm = (isset($_REQUEST['searchterm']) ? $_REQUEST['searchterm'] : "");
47
+$searchterm = $rawterm !== "" ? escapestring("%".strtr($rawterm, array("!" => "!!", "%" => "!%", "_" => "!_"))."%") : false;
47 48
 
48 49
 if(isset($_POST['submit']) || isset($_GET['offset'])) {
49 50
 	$output .= "<div id=\"pagetitle\">"._RESULTS."</div>";
50 51
 	$query = array();
51 52
 	$countquery = array();
52 53
 	$scountquery = array();	
53
-	if($searchtype == "fulltext") {
54
-		$query = "SELECT stories.*, "._PENNAMEFIELD." as penname, stories.date as date, stories.updated as updated FROM "._AUTHORTABLE.", ".TABLEPREFIX."fanfiction_stories as stories, ".TABLEPREFIX."fanfiction_chapters as chapter WHERE chapter.storytext LIKE '%$searchterm%' AND chapter.sid = stories.sid AND stories.uid = "._UIDFIELD;
55
-		$countquery = "SELECT count(stories.sid) FROM ".TABLEPREFIX."fanfiction_stories as stories, ".TABLEPREFIX."fanfiction_chapters as chapter WHERE chapter.storytext LIKE '%$searchterm%' AND chapter.sid = stories.sid GROUP BY stories.sid";
56
-		$query .= " "._ORDERBY;
57
-		search($query, $countquery);
58
-		$tpl->assign("output", $output);
59
-		$tpl->printToScreen();
60
-		dbclose( );			
61
-		exit( );
62
-	}
63
-	if($searchterm && strlen($searchterm) < 3) {
54
+	if(!in_array($searchtype, array("title", "summary", "penname", "advanced", "fulltext"), true)) errorExit(_ERROR);
55
+	if($searchtype == "fulltext" && $store != "mysql") errorExit(_ERROR);
56
+	if(($searchtype != "advanced" && trim($rawterm) === "") || ($searchterm !== false && strlen($rawterm) < 3)) {
64 57
 		errorExit(_SEARCHTERMTOOSHORT);
65 58
 	}
66
-	if($searchterm !== false) $searchterm = str_replace(array('%', '_'), array('\\%', '\\_'), escapestring($searchterm));
59
+	if($searchtype == "fulltext") {
60
+		// EXISTS keeps both rows and counts distinct, and all common filters apply.
61
+		$fulltext = "EXISTS (SELECT 1 FROM ".TABLEPREFIX."fanfiction_chapters AS chapter WHERE chapter.sid = stories.sid AND chapter.validated > 0 AND chapter.storytext LIKE '$searchterm' ESCAPE '!')";
62
+		$query[] = $fulltext;
63
+		$countquery[] = $fulltext;
64
+		$scountquery[] = "1 = 0"; // Series have no chapter text.
65
+	}
67 66
 	if($searchterm) {
68 67
 		if($searchtype == "title") {
69
-			$query[] = "stories.title LIKE '%$searchterm%'";
70
-			$countquery[] = "stories.title LIKE '%$searchterm%'";
71
-			$scountquery[] = "series.title LIKE '%$searchterm%'";
68
+			$query[] = "stories.title LIKE '$searchterm' ESCAPE '!'";
69
+			$countquery[] = "stories.title LIKE '$searchterm' ESCAPE '!'";
70
+			$scountquery[] = "series.title LIKE '$searchterm' ESCAPE '!'";
72 71
 		}
73 72
 		if($searchtype == "summary") {
74
-			$query[] = "stories.summary LIKE '%$searchterm%'";
75
-			$countquery[] = "stories.summary LIKE '%$searchterm%'";
76
-			$scountquery[] = "series.summary LIKE '%$searchterm%'";
73
+			$query[] = "stories.summary LIKE '$searchterm' ESCAPE '!'";
74
+			$countquery[] = "stories.summary LIKE '$searchterm' ESCAPE '!'";
75
+			$scountquery[] = "series.summary LIKE '$searchterm' ESCAPE '!'";
77 76
 		}
78 77
 		if($searchtype == "penname") {
79
-			$authorquery = dbquery(_MEMBERLIST." WHERE "._PENNAMEFIELD." LIKE '%$searchterm%' GROUP BY "._UIDFIELD);
78
+			$authorquery = dbquery(_MEMBERLIST." WHERE "._PENNAMEFIELD." LIKE '$searchterm' ESCAPE '!' GROUP BY "._UIDFIELD);
80 79
 			$authorlist = array();
81 80
 			while($auth = dbassoc($authorquery)) {
82 81
 				$authorlist[] = $auth['uid'];
83 82
 			}
83
+			// old behavior stays as new upstream behavior breaks MySQL strict error reporting mode - tweaked
84 84
 			if(count(array($authorlist)) > 0) {
85 85
 				$authors = implode(",",$authorlist);
86
-				$query = "SELECT stories.*, "._PENNAMEFIELD." as penname, stories.date as date, stories.updated as updated FROM ("._AUTHORTABLE.", ".TABLEPREFIX."fanfiction_stories as stories) LEFT JOIN ".TABLEPREFIX."fanfiction_coauthors as coauth ON coauth.sid = stories.sid WHERE "._UIDFIELD." = stories.uid AND stories.validated > 0 AND (FIND_IN_SET(stories.uid, '$authors') > 0 OR FIND_IN_SET(coauth.uid, '$authors') > 0) ";
87
-				$countquery = "SELECT COUNT(stories.sid) FROM ".TABLEPREFIX."fanfiction_stories as stories LEFT JOIN ".TABLEPREFIX."fanfiction_coauthors as coauth ON coauth.sid = stories.sid WHERE stories.validated > 0 AND (FIND_IN_SET(stories.uid, '$authors') > 0 OR FIND_IN_SET(coauth.uid, '$authors') > 0)";
88
-				search($query, $countquery);
86
+				$query = "SELECT stories.*, "._PENNAMEFIELD." as penname, stories.date as date, stories.updated as updated FROM ("._AUTHORTABLE.", ".TABLEPREFIX."fanfiction_stories as stories) LEFT JOIN ".TABLEPREFIX."fanfiction_coauthors as coauth ON coauth.sid = stories.sid WHERE "._UIDFIELD." = stories.uid AND stories.validated > 0 AND (FIND_IN_SET(stories.uid, '$authors') > 0 OR EXISTS (SELECT 1 FROM ".TABLEPREFIX."fanfiction_coauthors AS coauth WHERE coauth.sid = stories.sid AND FIND_IN_SET(coauth.uid, '$authors') > 0))";
87
+				$countquery = "SELECT COUNT(stories.sid) FROM ".TABLEPREFIX."fanfiction_stories as stories LEFT JOIN ".TABLEPREFIX."fanfiction_coauthors as coauth ON coauth.sid = stories.sid WHERE stories.validated > 0 AND (FIND_IN_SET(stories.uid, '$authors') > 0 OR EXISTS (SELECT 1 FROM ".TABLEPREFIX."fanfiction_coauthors AS coauth WHERE coauth.sid = stories.sid AND FIND_IN_SET(coauth.uid, '$authors') > 0))";
88
+				$scountquery = "SELECT COUNT(series.sid) FROM ".TABLEPREFIX."fanfiction_series as series AND (FIND_IN_SET(series.uid, '$authors') > 0)";
89
+				search($query, $countquery, $scountquery);
89 90
 				$tpl->assign("output", $output);
90 91
 				$tpl->printToScreen();
91 92
 				dbclose( );			
... ...
@@ -98,9 +99,9 @@ if(isset($_POST['submit']) || isset($_GET['offset'])) {
98 99
 			}
99 100
 		}
100 101
 		if($searchtype == "advanced") {
101
-			$query[] = "(stories.summary LIKE '%$searchterm%' OR stories.title LIKE '%$searchterm%') ";
102
-			$countquery[] = "(stories.summary LIKE '%$searchterm%' OR stories.title LIKE '%$searchterm%') ";
103
-			$scountquery[] = "(summary LIKE '%$searchterm%' OR title LIKE '%$searchterm%') ";
102
+			$query[] = "(stories.summary LIKE '$searchterm' ESCAPE '!' OR stories.title LIKE '$searchterm' ESCAPE '!') ";
103
+			$countquery[] = "(stories.summary LIKE '$searchterm' ESCAPE '!' OR stories.title LIKE '$searchterm' ESCAPE '!') ";
104
+			$scountquery[] = "(summary LIKE '$searchterm' ESCAPE '!' OR title LIKE '$searchterm' ESCAPE '!') ";
104 105
 		}
105 106
 	}
106 107
 	if(isset($_REQUEST['authors'])) {
... ...
@@ -177,18 +178,10 @@ if(isset($_POST['submit']) || isset($_GET['offset'])) {
177 178
 	foreach($classtypelist as $id => $vars) {
178 179
 		$opts = array( );
179 180
 		$exopts = array( );
180
-		if(isset($_POST["class_".$id])) $opts = array_filter($_POST["class_".$id], "isNumber");
181
-		else if(isset($_GET["class_".$id])) {
182
-			$opts = array_merge($opts, explode(",", $_GET["class_".$id]));
183
-			$opts = array_filter($opts, "isNumber");
184
-		}
185
-		else if(isset($_REQUEST[$vars["name"]])) $opts[] = array($_REQUEST[$vars["name"]]);
181
+		if(isset($_REQUEST["class_".$id])) $opts = ef_search_ids($_REQUEST["class_".$id]);
182
+		else if(isset($_REQUEST[$vars["name"]])) $opts = ef_search_ids($_REQUEST[$vars["name"]]);
186 183
 		$classin = array_merge($classin, $opts);
187
-		if(isset($_POST["exclass_".$id])) $exopts = array_filter($_POST["exclass_".$id], "isNumber");
188
-		else if(isset($_GET["exclass_".$id])) {
189
-			$exopts = array_merge($exopts, explode(",", $_GET["exclass_".$id]));
190
-			$exopts = array_filter($exopts, "isNumber");
191
-		}
184
+		if(isset($_REQUEST["exclass_".$id])) $exopts = ef_search_ids($_REQUEST["exclass_".$id]);
192 185
 		$classex = array_merge($classex, $exopts);
193 186
 		unset($opts, $exopts);
194 187
 	}
... ...
@@ -231,7 +224,7 @@ if(isset($_POST['submit']) || isset($_GET['offset'])) {
231 224
 	$query = (count($query) > 0 ? " AND ".implode(" AND ", $query) : "")." "._ORDERBY;
232 225
 	$countquery = count($countquery) > 0 ? " AND ".implode(" AND ", $countquery) : "";
233 226
 	if(count($scountquery) > 0) $scountquery = " WHERE ".implode(" AND ", $scountquery);
234
-	search(_STORYQUERY.$query, _STORYCOUNT.$countquery);
227
+	search(preg_replace('/^SELECT /i', 'SELECT DISTINCT ', _STORYQUERY).$query, str_ireplace('count(sid)', 'COUNT(DISTINCT stories.sid)', _STORYCOUNT).$countquery);
235 228
 	$otherresults = array( );
236 229
 	$termArray = array_merge($_GET, $_POST);
237 230
 	$termsList = array();
... ...
@@ -243,7 +236,7 @@ if(isset($_POST['submit']) || isset($_GET['offset'])) {
243 236
 	if(!empty($scountquery)) {
244 237
 		$seriesresults = dbquery(_SERIESCOUNT.$scountquery);
245 238
 		list($scount) = dbrow($seriesresults);
246
-		if($scount > 0) $otherresults[] = "<a href='browse.php?type=series&amp;$terms'>$scount "._SERIES."</a>";
239
+		if($scount > 0) $otherresults[] = "<a href='browse.php?type=series".($terms !== "" ? "&amp;".$terms : "")."'>$scount "._SERIES."</a>";
247 240
 	}
248 241
 	$codeblocks = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_codeblocks WHERE code_type = 'otherresults'");
249 242
 	while($code = dbassoc($codeblocks)) {
... ...
@@ -336,4 +329,14 @@ $output .= "<div id='submitdiv'><input name=\"submit\" id=\"submit\" value=\""._
336 329
 $tpl->assign("output", $output);
337 330
 $tpl->printToScreen();
338 331
 dbclose( );
332
+
333
+// Accept form lists and comma-separated continuation values, but never silently
334
+// discard an invalid/nested classification and broaden the search.
335
+function ef_search_ids($value) {
336
+	$values = is_array($value) ? $value : explode(",", $value);
337
+	foreach($values as $v) {
338
+		if(!is_string($v) || ($v !== "" && !isNumber($v))) errorExit(_ERROR);
339
+	}
340
+	return array_values(array_filter($values, "isNumber"));
341
+}
339 342
 ?>