Skip to content

Commit 6a1bec2

Browse files
committed
Force redirect when input categories do not match stored categories in general questions
1 parent 0d39db8 commit 6a1bec2

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

qa-include/pages/questions.php

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,17 @@
2929
require_once QA_INCLUDE_DIR . 'app/q-list.php';
3030

3131
$categoryslugs = qa_request_parts(1);
32-
$countslugs = count($categoryslugs);
32+
$hasSlugs = !empty($categoryslugs);
33+
$sort = qa_get('sort');
34+
35+
if (!QA_ALLOW_UNINDEXED_QUERIES && $hasSlugs) {
36+
$getParams = $sort === null ? [] : ['sort' => $sort];
37+
qa_redirect('questions', $getParams);
38+
}
3339

34-
$sort = ($countslugs && !QA_ALLOW_UNINDEXED_QUERIES) ? null : qa_get('sort');
3540
$start = qa_get_start();
3641
$userid = qa_get_logged_in_userid();
3742

38-
3943
// Get list of questions, plus category information
4044

4145
switch ($sort) {
@@ -63,75 +67,76 @@
6367
list($questions, $categories, $categoryid) = qa_db_select_with_pending(
6468
qa_db_qs_selectspec($userid, $selectsort, $start, $categoryslugs, null, false, false, qa_opt_if_loaded('page_size_qs')),
6569
qa_db_category_nav_selectspec($categoryslugs, false, false, true),
66-
$countslugs ? qa_db_slugs_to_category_id_selectspec($categoryslugs) : null
70+
$hasSlugs ? qa_db_slugs_to_category_id_selectspec($categoryslugs) : null
6771
);
6872

69-
if ($countslugs) {
73+
if ($hasSlugs) {
7074
if (!isset($categoryid)) {
7175
return include QA_INCLUDE_DIR . 'qa-page-not-found.php';
7276
}
7377

78+
if ($categories[$categoryid]['backpath'] !== qa_db_slugs_to_backpath($categoryslugs)) {
79+
$expectedPathInUrl = implode('/', array_reverse(explode('/', $categories[$categoryid]['backpath'])));
80+
$getParams = $sort === null ? [] : ['sort' => $sort];
81+
qa_redirect('questions/' . $expectedPathInUrl, $getParams);
82+
}
83+
7484
$categorytitlehtml = qa_html($categories[$categoryid]['title']);
7585
$nonetitle = qa_lang_html_sub('main/no_questions_in_x', $categorytitlehtml);
76-
7786
} else {
7887
$nonetitle = qa_lang_html('main/no_questions_found');
7988
}
8089

81-
8290
$categorypathprefix = QA_ALLOW_UNINDEXED_QUERIES ? 'questions/' : null; // this default is applied if sorted not by recent
8391
$feedpathprefix = null;
8492
$linkparams = array('sort' => $sort);
8593

8694
switch ($sort) {
8795
case 'hot':
88-
$sometitle = $countslugs ? qa_lang_html_sub('main/hot_qs_in_x', $categorytitlehtml) : qa_lang_html('main/hot_qs_title');
96+
$sometitle = $hasSlugs ? qa_lang_html_sub('main/hot_qs_in_x', $categorytitlehtml) : qa_lang_html('main/hot_qs_title');
8997
$feedpathprefix = qa_opt('feed_for_hot') ? 'hot' : null;
9098
break;
9199

92100
case 'votes':
93-
$sometitle = $countslugs ? qa_lang_html_sub('main/voted_qs_in_x', $categorytitlehtml) : qa_lang_html('main/voted_qs_title');
101+
$sometitle = $hasSlugs ? qa_lang_html_sub('main/voted_qs_in_x', $categorytitlehtml) : qa_lang_html('main/voted_qs_title');
94102
break;
95103

96104
case 'answers':
97-
$sometitle = $countslugs ? qa_lang_html_sub('main/answered_qs_in_x', $categorytitlehtml) : qa_lang_html('main/answered_qs_title');
105+
$sometitle = $hasSlugs ? qa_lang_html_sub('main/answered_qs_in_x', $categorytitlehtml) : qa_lang_html('main/answered_qs_title');
98106
break;
99107

100108
case 'views':
101-
$sometitle = $countslugs ? qa_lang_html_sub('main/viewed_qs_in_x', $categorytitlehtml) : qa_lang_html('main/viewed_qs_title');
109+
$sometitle = $hasSlugs ? qa_lang_html_sub('main/viewed_qs_in_x', $categorytitlehtml) : qa_lang_html('main/viewed_qs_title');
102110
break;
103111

104112
default:
105113
$linkparams = array();
106-
$sometitle = $countslugs ? qa_lang_html_sub('main/recent_qs_in_x', $categorytitlehtml) : qa_lang_html('main/recent_qs_title');
107-
$categorypathprefix = 'questions/';
114+
$sometitle = $hasSlugs ? qa_lang_html_sub('main/recent_qs_in_x', $categorytitlehtml) : qa_lang_html('main/recent_qs_title');
108115
$feedpathprefix = qa_opt('feed_for_questions') ? 'questions' : null;
109116
break;
110117
}
111118

112-
113119
// Prepare and return content for theme
114120

115121
$qa_content = qa_q_list_page_content(
116122
$questions, // questions
117123
qa_opt('page_size_qs'), // questions per page
118124
$start, // start offset
119-
$countslugs ? $categories[$categoryid]['qcount'] : qa_opt('cache_qcount'), // total count
125+
$hasSlugs ? $categories[$categoryid]['qcount'] : qa_opt('cache_qcount'), // total count
120126
$sometitle, // title if some questions
121127
$nonetitle, // title if no questions
122128
$categories, // categories for navigation
123129
$categoryid, // selected category id
124130
true, // show question counts in category navigation
125131
$categorypathprefix, // prefix for links in category navigation
126132
$feedpathprefix, // prefix for RSS feed paths
127-
$countslugs ? qa_html_suggest_qs_tags(qa_using_tags()) : qa_html_suggest_ask($categoryid), // suggest what to do next
133+
$hasSlugs ? qa_html_suggest_qs_tags(qa_using_tags()) : qa_html_suggest_ask($categoryid), // suggest what to do next
128134
$linkparams, // extra parameters for page links
129135
$linkparams // category nav params
130136
);
131137

132-
if (QA_ALLOW_UNINDEXED_QUERIES || !$countslugs) {
138+
if (QA_ALLOW_UNINDEXED_QUERIES || !$hasSlugs) {
133139
$qa_content['navigation']['sub'] = qa_qs_sub_navigation($sort, $categoryslugs);
134140
}
135141

136-
137142
return $qa_content;

0 commit comments

Comments
 (0)