Skip to content

Commit

Permalink
Force redirect when input categories do not match stored categories i…
Browse files Browse the repository at this point in the history
…n general questions
  • Loading branch information
pupi1985 committed Feb 18, 2025
1 parent 0d39db8 commit 6a1bec2
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions qa-include/pages/questions.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@
require_once QA_INCLUDE_DIR . 'app/q-list.php';

$categoryslugs = qa_request_parts(1);
$countslugs = count($categoryslugs);
$hasSlugs = !empty($categoryslugs);
$sort = qa_get('sort');

if (!QA_ALLOW_UNINDEXED_QUERIES && $hasSlugs) {
$getParams = $sort === null ? [] : ['sort' => $sort];
qa_redirect('questions', $getParams);
}

$sort = ($countslugs && !QA_ALLOW_UNINDEXED_QUERIES) ? null : qa_get('sort');
$start = qa_get_start();
$userid = qa_get_logged_in_userid();


// Get list of questions, plus category information

switch ($sort) {
Expand Down Expand Up @@ -63,75 +67,76 @@
list($questions, $categories, $categoryid) = qa_db_select_with_pending(
qa_db_qs_selectspec($userid, $selectsort, $start, $categoryslugs, null, false, false, qa_opt_if_loaded('page_size_qs')),
qa_db_category_nav_selectspec($categoryslugs, false, false, true),
$countslugs ? qa_db_slugs_to_category_id_selectspec($categoryslugs) : null
$hasSlugs ? qa_db_slugs_to_category_id_selectspec($categoryslugs) : null
);

if ($countslugs) {
if ($hasSlugs) {
if (!isset($categoryid)) {
return include QA_INCLUDE_DIR . 'qa-page-not-found.php';
}

if ($categories[$categoryid]['backpath'] !== qa_db_slugs_to_backpath($categoryslugs)) {
$expectedPathInUrl = implode('/', array_reverse(explode('/', $categories[$categoryid]['backpath'])));
$getParams = $sort === null ? [] : ['sort' => $sort];
qa_redirect('questions/' . $expectedPathInUrl, $getParams);
}

$categorytitlehtml = qa_html($categories[$categoryid]['title']);
$nonetitle = qa_lang_html_sub('main/no_questions_in_x', $categorytitlehtml);

} else {
$nonetitle = qa_lang_html('main/no_questions_found');
}


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

switch ($sort) {
case 'hot':
$sometitle = $countslugs ? qa_lang_html_sub('main/hot_qs_in_x', $categorytitlehtml) : qa_lang_html('main/hot_qs_title');
$sometitle = $hasSlugs ? qa_lang_html_sub('main/hot_qs_in_x', $categorytitlehtml) : qa_lang_html('main/hot_qs_title');
$feedpathprefix = qa_opt('feed_for_hot') ? 'hot' : null;
break;

case 'votes':
$sometitle = $countslugs ? qa_lang_html_sub('main/voted_qs_in_x', $categorytitlehtml) : qa_lang_html('main/voted_qs_title');
$sometitle = $hasSlugs ? qa_lang_html_sub('main/voted_qs_in_x', $categorytitlehtml) : qa_lang_html('main/voted_qs_title');
break;

case 'answers':
$sometitle = $countslugs ? qa_lang_html_sub('main/answered_qs_in_x', $categorytitlehtml) : qa_lang_html('main/answered_qs_title');
$sometitle = $hasSlugs ? qa_lang_html_sub('main/answered_qs_in_x', $categorytitlehtml) : qa_lang_html('main/answered_qs_title');
break;

case 'views':
$sometitle = $countslugs ? qa_lang_html_sub('main/viewed_qs_in_x', $categorytitlehtml) : qa_lang_html('main/viewed_qs_title');
$sometitle = $hasSlugs ? qa_lang_html_sub('main/viewed_qs_in_x', $categorytitlehtml) : qa_lang_html('main/viewed_qs_title');
break;

default:
$linkparams = array();
$sometitle = $countslugs ? qa_lang_html_sub('main/recent_qs_in_x', $categorytitlehtml) : qa_lang_html('main/recent_qs_title');
$categorypathprefix = 'questions/';
$sometitle = $hasSlugs ? qa_lang_html_sub('main/recent_qs_in_x', $categorytitlehtml) : qa_lang_html('main/recent_qs_title');
$feedpathprefix = qa_opt('feed_for_questions') ? 'questions' : null;
break;
}


// Prepare and return content for theme

$qa_content = qa_q_list_page_content(
$questions, // questions
qa_opt('page_size_qs'), // questions per page
$start, // start offset
$countslugs ? $categories[$categoryid]['qcount'] : qa_opt('cache_qcount'), // total count
$hasSlugs ? $categories[$categoryid]['qcount'] : qa_opt('cache_qcount'), // total count
$sometitle, // title if some questions
$nonetitle, // title if no questions
$categories, // categories for navigation
$categoryid, // selected category id
true, // show question counts in category navigation
$categorypathprefix, // prefix for links in category navigation
$feedpathprefix, // prefix for RSS feed paths
$countslugs ? qa_html_suggest_qs_tags(qa_using_tags()) : qa_html_suggest_ask($categoryid), // suggest what to do next
$hasSlugs ? qa_html_suggest_qs_tags(qa_using_tags()) : qa_html_suggest_ask($categoryid), // suggest what to do next
$linkparams, // extra parameters for page links
$linkparams // category nav params
);

if (QA_ALLOW_UNINDEXED_QUERIES || !$countslugs) {
if (QA_ALLOW_UNINDEXED_QUERIES || !$hasSlugs) {
$qa_content['navigation']['sub'] = qa_qs_sub_navigation($sort, $categoryslugs);
}


return $qa_content;

0 comments on commit 6a1bec2

Please sign in to comment.