Skip to content

Commit c0edbb7

Browse files
committed
refactor: improved codebase, added more escaping
1 parent b6a080f commit c0edbb7

File tree

3 files changed

+31
-29
lines changed

3 files changed

+31
-29
lines changed

phpmyfaq/admin/assets/src/content/markdown.js

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* @package phpMyFAQ
99
* @author Thorsten Rinne <[email protected]>
10-
* @copyright 2023 phpMyFAQ Team
10+
* @copyright 2023-2024 phpMyFAQ Team
1111
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
1212
* @link https://www.phpmyfaq.de
1313
* @since 2023-03-05
@@ -21,7 +21,7 @@ export const handleMarkdownForm = () => {
2121
// Store the height of the textarea
2222
if (answer) {
2323
if (answerHeight !== 'undefined') {
24-
answer.style.height = parseInt(answerHeight);
24+
answer.style.height = answerHeight;
2525
}
2626

2727
answer.addEventListener('mouseup', (event) => {
@@ -33,33 +33,35 @@ export const handleMarkdownForm = () => {
3333
if (markdownTabs) {
3434
const tab = document.querySelector('a[data-markdown-tab="preview"]');
3535

36-
tab.addEventListener('shown.bs.tab', (event) => {
36+
tab.addEventListener('shown.bs.tab', async () => {
3737
const preview = document.getElementById('markdown-preview');
3838
preview.style.height = answer.style.height;
3939

40-
fetch(window.location.pathname + 'api/content/markdown', {
41-
method: 'POST',
42-
headers: {
43-
Accept: 'application/json, text/plain, */*',
44-
'Content-Type': 'application/json',
45-
},
46-
body: JSON.stringify({
47-
text: answer.value,
48-
}),
49-
})
50-
.then(async (response) => {
51-
if (response.ok) {
52-
return response.json();
53-
}
54-
throw new Error('Network response was not ok: ', { cause: { response } });
55-
})
56-
.then((response) => {
57-
preview.innerHTML = response.success;
58-
})
59-
.catch(async (error) => {
60-
const errorMessage = await error.cause.response.json();
61-
console.error(errorMessage);
40+
try {
41+
const response = await fetch(window.location.pathname + 'api/content/markdown', {
42+
method: 'POST',
43+
headers: {
44+
Accept: 'application/json, text/plain, */*',
45+
'Content-Type': 'application/json',
46+
},
47+
body: JSON.stringify({
48+
text: answer.value,
49+
}),
6250
});
51+
52+
if (!response.ok) {
53+
throw new Error('Network response was not ok');
54+
}
55+
56+
const responseData = await response.json();
57+
preview.innerHTML = responseData.success;
58+
} catch (error) {
59+
if (error instanceof Error) {
60+
console.error(error);
61+
} else {
62+
console.error('Unknown error:', error);
63+
}
64+
}
6365
});
6466
}
6567
};

phpmyfaq/src/phpMyFAQ/Rating.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public function check(int $id, string $ip): bool
183183
"SELECT id FROM %sfaqvoting WHERE artikel = %d AND (ip = '%s' AND datum > '%s')",
184184
Database::getTablePrefix(),
185185
$id,
186-
$ip,
186+
$this->configuration->getDb()->escape($ip),
187187
$check
188188
);
189189
return !$this->configuration->getDb()->numRows($this->configuration->getDb()->query($query));

phpmyfaq/src/phpMyFAQ/StopWords.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function update(int $id, string $word): bool
8787
$sql = sprintf(
8888
$sql,
8989
$this->getTableName(),
90-
$word,
90+
$this->configuration->getDb()->escape($word),
9191
$id,
9292
$this->language
9393
);
@@ -118,7 +118,7 @@ public function match(string $word): bool
118118
$sql = sprintf(
119119
"SELECT id FROM %s WHERE LOWER(stopword) = LOWER('%s') AND lang = '%s'",
120120
$this->getTableName(),
121-
$word,
121+
$this->configuration->getDb()->escape($word),
122122
$this->language
123123
);
124124

@@ -139,7 +139,7 @@ public function getByLang(string $lang = null, bool $wordsOnly = false): array
139139
$sql = sprintf(
140140
"SELECT id, lang, LOWER(stopword) AS stopword FROM %s WHERE lang = '%s'",
141141
$this->getTableName(),
142-
$lang
142+
$this->configuration->getDb()->escape($lang)
143143
);
144144

145145
$result = $this->configuration->getDb()->query($sql);

0 commit comments

Comments
 (0)