Skip to content

Commit 793877b

Browse files
committed
fix: corrected check on captcha after displaying smart answer
1 parent 81d9568 commit 793877b

3 files changed

Lines changed: 30 additions & 24 deletions

File tree

phpmyfaq/assets/src/search/question.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ export const handleQuestion = () => {
4747
message.insertAdjacentElement('afterend', addElement('div', { classList: '', innerHTML: resultMessage }));
4848
// Add hidden input
4949
form.insertAdjacentElement('afterbegin', addElement('input', { type: 'hidden', name: 'save', value: 1 }));
50+
form.insertAdjacentElement(
51+
'afterbegin',
52+
addElement('input', { type: 'hidden', name: 'store', value: 'now' })
53+
);
5054
}
5155

5256
// Final result

phpmyfaq/assets/templates/default/ask.twig

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,27 @@
3131
<input type="hidden" name="lang" id="lang" value="{{ lang }}">
3232

3333
<div class="row mb-2">
34-
<label class="col-sm-3 col-form-label" for="name">{{ id3_label }}:
35-
<span style="color: red"> *</span></label>
34+
<label class="col-sm-3 col-form-label" for="name">
35+
{{ id3_label }}*:
36+
</label>
3637
<div class="col-sm-9">
3738
<input type="text" class="form-control" name="name" id="name" value="{{ defaultContentName }}" required>
3839
</div>
3940
</div>
4041

4142
<div class="row mb-2">
42-
<label class="col-sm-3 col-form-label" for="email">{{ id4_label }}:
43-
<span style="color: red"> *</span></label>
43+
<label class="col-sm-3 col-form-label" for="email">
44+
{{ id4_label }}*:
45+
</label>
4446
<div class="col-sm-9">
4547
<input type="email" class="form-control" name="email" id="email" value="{{ defaultContentMail }}" required>
4648
</div>
4749
</div>
4850

4951
{% if id5_label is defined %}
5052
<div class="row mb-2">
51-
<label class="col-sm-3 col-form-label" for="category">{{ id5_label }}:
52-
{% if id5_required == 'required' %}<span style="color: red"> *</span>{% endif %}
53+
<label class="col-sm-3 col-form-label" for="category">
54+
{{ id5_label }}{% if id5_required == 'required' %}*{% endif %}:
5355
</label>
5456
<div class="col-sm-9">
5557
<select name="category" class="form-select" id="category" {{ id5_required }}>
@@ -60,8 +62,9 @@
6062
{% endif %}
6163

6264
<div class="row mb-2">
63-
<label class="col-sm-3 col-form-label" for="question">{{ id6_label }}:
64-
<span style="color: red"> *</span></label>
65+
<label class="col-sm-3 col-form-label" for="question">
66+
{{ id6_label }}*:
67+
</label>
6568
<div class="col-sm-9">
6669
<textarea class="form-control" cols="45" rows="5" name="question" id="question" required></textarea>
6770
</div>

phpmyfaq/src/phpMyFAQ/Controller/Frontend/QuestionController.php

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ class QuestionController extends AbstractController
4646
*/
4747
public function create(Request $request): JsonResponse
4848
{
49-
$user = CurrentUser::getCurrentUser($this->configuration);
50-
51-
if (!$this->isAddingQuestionsAllowed($user)) {
49+
if (!$this->isAddingQuestionsAllowed()) {
5250
return $this->json(['error' => Translation::get('ad_msg_noauth')], Response::HTTP_FORBIDDEN);
5351
}
5452

@@ -69,14 +67,15 @@ public function create(Request $request): JsonResponse
6967
$selectedCategory = isset($data->category) ? Filter::filterVar($data->category, FILTER_VALIDATE_INT) : false;
7068
$userQuestion = trim(strip_tags((string) $data->question));
7169
$save = Filter::filterVar($data->save ?? 0, FILTER_VALIDATE_INT);
70+
$storeNow = Filter::filterVar($data->store ?? 'not', FILTER_SANITIZE_SPECIAL_CHARS);
7271

7372
// If smart answering is disabled, save the question immediately
7473
if (false === $this->configuration->get('main.enableSmartAnswering')) {
7574
$save = true;
7675
}
7776

78-
// Validate captcha
79-
if (!$this->captchaCodeIsValid($request)) {
77+
// Validate captcha if we can store the question after displaying the smart answer
78+
if ($storeNow !== 'now' && !$this->captchaCodeIsValid($request)) {
8079
return $this->json(['error' => Translation::get('msgCaptcha')], Response::HTTP_BAD_REQUEST);
8180
}
8281

@@ -108,7 +107,7 @@ public function create(Request $request): JsonResponse
108107
$faqSearch->setCategoryId((int) $selectedCategory);
109108

110109
$faqPermission = new Permission($this->configuration);
111-
$faqSearchResult = new SearchResultSet($user, $faqPermission, $this->configuration);
110+
$faqSearchResult = new SearchResultSet($this->currentUser, $faqPermission, $this->configuration);
112111

113112
$searchResult = array_merge(...array_map(
114113
fn($word) => $faqSearch->search($word, false),
@@ -134,16 +133,16 @@ public function create(Request $request): JsonResponse
134133
}
135134
}
136135

137-
private function isAddingQuestionsAllowed(CurrentUser $user): bool
136+
/**
137+
* @throws \Exception
138+
*/
139+
private function isAddingQuestionsAllowed(): bool
138140
{
139-
if (
140-
!$this->configuration->get('records.allowQuestionsForGuests') &&
141-
!$this->configuration->get('main.enableAskQuestions') &&
142-
!$user->perm->hasPermission($user->getUserId(), PermissionType::QUESTION_ADD->value)
143-
) {
144-
return false;
145-
}
146-
147-
return true;
141+
return $this->configuration->get('records.allowQuestionsForGuests') ||
142+
$this->configuration->get('main.enableAskQuestions') ||
143+
$this->currentUser->perm->hasPermission(
144+
$this->currentUser->getUserId(),
145+
PermissionType::QUESTION_ADD->value
146+
);
148147
}
149148
}

0 commit comments

Comments
 (0)