Skip to content

Commit 2b8e55a

Browse files
committed
Merge branch '4.0' into 'main'
2 parents 108ab10 + a53df1f commit 2b8e55a

File tree

10 files changed

+146
-39
lines changed

10 files changed

+146
-39
lines changed

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/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ public function replaceMainReferenceUrl(string $oldUrl, string $newUrl): bool
517517
*/
518518
public function getAllowedMediaHosts(): array
519519
{
520-
return explode(',', $this->get('records.allowedMediaHosts'));
520+
return explode(',', trim($this->get('records.allowedMediaHosts')));
521521
}
522522

523523
public function getCustomCss(): string

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
}

phpmyfaq/src/phpMyFAQ/Faq/Permission.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,23 +139,39 @@ public function createPermissionArray(): array
139139
'restricted_user' => [-1],
140140
];
141141
} else {
142-
$permissions += [
143-
'restricted_user' => [
144-
Filter::filterVar($data->restricted_users, FILTER_VALIDATE_INT),
145-
],
146-
];
142+
if (is_string($data->restricted_users)) {
143+
$permissions += [
144+
'restricted_user' => [
145+
Filter::filterVar(array($data->restricted_users), FILTER_VALIDATE_INT),
146+
],
147+
];
148+
} else {
149+
$permissions += [
150+
'restricted_user' => [
151+
Filter::filterVar($data->restricted_users, FILTER_VALIDATE_INT),
152+
],
153+
];
154+
}
147155
}
148156

149157
if ('all' === Filter::filterVar($data->grouppermission, FILTER_SANITIZE_SPECIAL_CHARS)) {
150158
$permissions += [
151159
'restricted_groups' => [-1],
152160
];
153161
} else {
154-
$permissions += [
155-
'restricted_groups' => [
156-
Filter::filterArray($data->{'restricted_groups'}, FILTER_VALIDATE_INT),
157-
]
158-
];
162+
if (is_string($data->restricted_groups)) {
163+
$permissions += [
164+
'restricted_groups' => [
165+
Filter::filterVar(array($data->restricted_groups), FILTER_VALIDATE_INT),
166+
]
167+
];
168+
} else {
169+
$permissions += [
170+
'restricted_groups' => [
171+
Filter::filterArray($data->restricted_groups, FILTER_VALIDATE_INT),
172+
]
173+
];
174+
}
159175
}
160176

161177
return $permissions;

phpmyfaq/src/phpMyFAQ/Instance/Database/Mysqli.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ class Mysqli extends Database implements Driver
208208
form_id INT(1) NOT NULL,
209209
input_id INT(11) NOT NULL,
210210
input_type VARCHAR(1000) NOT NULL,
211-
input_label VARCHAR(100) NOT NULL,
211+
input_label VARCHAR(500) NOT NULL,
212212
input_active INT(1) NOT NULL,
213213
input_required INT(1) NOT NULL,
214214
input_lang VARCHAR(11) NOT NULL)',

phpmyfaq/src/phpMyFAQ/Instance/Database/Pgsql.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ class Pgsql extends Database implements Driver
208208
form_id INTEGER NOT NULL,
209209
input_id INTEGER NOT NULL,
210210
input_type VARCHAR(1000) NOT NULL,
211-
input_label VARCHAR(100) NOT NULL,
211+
input_label VARCHAR(500) NOT NULL,
212212
input_active INTEGER NOT NULL,
213213
input_required INTEGER NOT NULL,
214214
input_lang VARCHAR(11) NOT NULL)',

phpmyfaq/src/phpMyFAQ/Instance/Database/Sqlite3.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class Sqlite3 extends Database implements Driver
204204
form_id INTEGER NOT NULL,
205205
input_id INTEGER NOT NULL,
206206
input_type VARCHAR(1000) NOT NULL,
207-
input_label VARCHAR(100) NOT NULL,
207+
input_label VARCHAR(500) NOT NULL,
208208
input_active INTEGER NOT NULL,
209209
input_required INTEGER NOT NULL,
210210
input_lang VARCHAR(11) NOT NULL)',

phpmyfaq/src/phpMyFAQ/Instance/Database/Sqlsrv.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class Sqlsrv extends Database implements Driver
204204
form_id INTEGER NOT NULL,
205205
input_id INTEGER NOT NULL,
206206
input_type NVARCHAR(1000) NOT NULL,
207-
input_label NVARCHAR(100) NOT NULL,
207+
input_label NVARCHAR(500) NOT NULL,
208208
input_active INTEGER NOT NULL,
209209
input_required INTEGER NOT NULL,
210210
input_lang NVARCHAR(11) NOT NULL)',

phpmyfaq/src/phpMyFAQ/Setup/Update.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ public function applyUpdates(): bool
165165
$this->applyUpdates400Alpha2();
166166
$this->applyUpdates400Alpha3();
167167
$this->applyUpdates400Beta2();
168+
$this->applyUpdates405();
168169

169170
// 4.1 updates
170171
$this->applyUpdates410Alpha();
@@ -863,6 +864,90 @@ private function applyUpdates400Beta2(): void
863864
}
864865
}
865866

867+
private function applyUpdates405(): void
868+
{
869+
if (version_compare($this->version, '4.0.5', '<')) {
870+
// Delete old permissions
871+
$this->queries[] = sprintf(
872+
'DELETE FROM %sfaqright WHERE name = \'view_sections\'',
873+
Database::getTablePrefix()
874+
);
875+
$this->queries[] = sprintf(
876+
'DELETE FROM %sfaqright WHERE name = \'add_section\'',
877+
Database::getTablePrefix()
878+
);
879+
$this->queries[] = sprintf(
880+
'DELETE FROM %sfaqright WHERE name = \'edit_section\'',
881+
Database::getTablePrefix()
882+
);
883+
$this->queries[] = sprintf(
884+
'DELETE FROM %sfaqright WHERE name = \'delete_section\'',
885+
Database::getTablePrefix()
886+
);
887+
$this->queries[] = sprintf(
888+
'DELETE FROM %sfaqright WHERE name = \'delete_section\'',
889+
Database::getTablePrefix()
890+
);
891+
892+
// Update faqforms table
893+
switch (Database::getType()) {
894+
case 'mysqli':
895+
$this->queries[] = sprintf(
896+
'ALTER TABLE %sfaqforms CHANGE input_label input_label VARCHAR(500) NOT NULL',
897+
Database::getTablePrefix()
898+
);
899+
break;
900+
case 'pgsql':
901+
$this->queries[] = sprintf(
902+
'ALTER TABLE %sfaqforms ALTER COLUMN input_label TYPE VARCHAR(500)',
903+
Database::getTablePrefix()
904+
);
905+
$this->queries[] = sprintf(
906+
'ALTER TABLE %sfaqforms ALTER COLUMN input_label SET NOT NULL',
907+
Database::getTablePrefix()
908+
);
909+
break;
910+
case 'sqlite3':
911+
$this->queries[] = sprintf(
912+
'ALTER TABLE %sfaqforms RENAME TO %sfaqforms_old',
913+
Database::getTablePrefix(),
914+
Database::getTablePrefix()
915+
);
916+
$this->queries[] = sprintf(
917+
'CREATE TABLE %sfaqforms (
918+
form_id INTEGER NOT NULL,
919+
input_id INTEGER NOT NULL,
920+
input_type VARCHAR(1000) NOT NULL,
921+
input_label VARCHAR(500) NOT NULL,
922+
input_active INTEGER NOT NULL,
923+
input_required INTEGER NOT NULL,
924+
input_lang VARCHAR(11) NOT NULL
925+
)',
926+
Database::getTablePrefix()
927+
);
928+
$this->queries[] = sprintf(
929+
'INSERT INTO %sfaqforms
930+
SELECT
931+
form_id, input_id, input_type, input_label, input_active, input_required, input_lang
932+
FROM %sfaqforms_old',
933+
Database::getTablePrefix(),
934+
Database::getTablePrefix()
935+
);
936+
$this->queries[] = sprintf(
937+
'DROP TABLE %sfaqforms_old;',
938+
Database::getTablePrefix()
939+
);
940+
break;
941+
case 'sqlsrv':
942+
$this->queries[] = sprintf(
943+
'ALTER TABLE %sfaqforms ALTER COLUMN input_label NVARCHAR(500) NOT NULL',
944+
Database::getTablePrefix()
945+
);
946+
break;
947+
}
948+
}
949+
}
950+
866951
private function applyUpdates410Alpha(): void
867952
{
868953
if (version_compare($this->version, '4.1.0-alpha', '<')) {

0 commit comments

Comments
 (0)