Skip to content

Commit

Permalink
Repair
Browse files Browse the repository at this point in the history
- 添加:验证 ID 有效性,防止恶意篡改
- 连接修改为博客地址
  • Loading branch information
DUQIA authored Dec 18, 2024
1 parent 0ffac16 commit 40f053a
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions db_dispose.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,23 @@ public function dbCookie(): string|false
// home_config 配置更新
public function dbUpdateHomeConfig(string $site_name, string $site_icon, string $home_theme, string $home_icon, array $label_id, array $home_label, bool $home_search, bool $home_login, string $home_content): void
{
$label_id_string = implode(", ",$label_id);
$home_label_string = implode(", ",$home_label);
$command = "UPDATE {$this->home_config} SET site_name = ? , site_icon = ? , home_theme = ? , home_icon = ? , label_id = ? , home_label = ? , home_search = ? , home_login = ? , home_content = ? ";
$this->dbUpdate($command, 'ssssssiis', [$site_name, $site_icon, $home_theme, $home_icon, $label_id_string, $home_label_string, $home_search, $home_login, $home_content], 'Update home_config failed');
try {
// 验证 ID 有效性
$data = $this->dbQueryHomeLabelAll();
$existing_ids = array_column($data, 'id');
if (!is_array($label_id) || array_diff(array_filter($label_id), $existing_ids)) {
echo "<script>alert('Label ID is invalid');</script>";
throw new Exception('ID does not exist');
}

$label_id_string = implode(", ",$label_id);
$home_label_string = implode(", ",$home_label);

$command = "UPDATE {$this->home_config} SET site_name = ? , site_icon = ? , home_theme = ? , home_icon = ? , label_id = ? , home_label = ? , home_search = ? , home_login = ? , home_content = ? ";
$this->dbUpdate($command, 'ssssssiis', [$site_name, $site_icon, $home_theme, $home_icon, $label_id_string, $home_label_string, $home_search, $home_login, $home_content], 'Update home_config failed');
} catch (Exception $e) {
throw new Exception('home_config update failed: ' . $e->getMessage());
}
}

// home_config 配置查询
Expand Down Expand Up @@ -312,7 +325,7 @@ public function dbQueryHomeConfig(): array
&amp;lt;/a&amp;gt;
&amp;lt;/span&amp;gt;'],
['Dropdown', 'dropdown', '&amp;lt;a href=&amp;#039;https://github.com/DUQIA/Akylor&amp;#039; target=&amp;#039;_blank&amp;#039; rel=&amp;#039;nofollow noopener noreferrer&amp;#039;&amp;gt;Akylor&amp;lt;/a&amp;gt;'],
['Link', 'link', '&amp;lt;a href=&amp;#039;https://github.com/DUQIA&amp;#039; target=&amp;#039;_blank&amp;#039; rel=&amp;#039;nofollow noopener noreferrer&amp;#039;&amp;gt;Github&amp;lt;/a&amp;gt;']
['Link', 'link', '&amp;lt;a href=&amp;#039;https://blog.akylor.us.kg&amp;#039; target=&amp;#039;_blank&amp;#039; rel=&amp;#039;nofollow noopener noreferrer&amp;#039;&amp;gt;Blog&amp;lt;/a&amp;gt;']
];
$this->dbDeleteHomeLabelAll();
foreach ($default_labels_config as $label) {
Expand Down Expand Up @@ -389,16 +402,24 @@ public function dbQueryHomeLabel(array $label_id, array $home_label): array
// 开始事务
$this->beginTransaction();

$command = "SELECT * FROM {$this->home_labels}";
$data = $this->dbQueryAllLine($command, 'Query home_labels failed');
$data = $this->dbQueryHomeLabelAll();

// 首次创建数据
if (empty($data)) {
$this->dbCreateHomeLabel($home_label);
return $this->dbQueryHomeLabel($label_id, $home_label);
} else {
$existing_ids = array_column($data, 'id');
$existing_labels = array_column($data, 'label_name');

// 验证 ID 有效性
if (!is_array($label_id) || array_diff($label_id, $existing_ids)) {
// 回滚事务
$this->rollBack();
echo "<script>alert('ID does not exist');</script>";
return $data;
}

$delete_ids = array_diff($existing_ids, $label_id); // 删除id
$Remaining_labels = array_diff_assoc($home_label, $existing_labels); // 剩余标签
$update_ids = array_intersect_key($label_id, $Remaining_labels); // 通过 id 更新标签
Expand All @@ -417,8 +438,7 @@ public function dbQueryHomeLabel(array $label_id, array $home_label): array

if (!empty($delete_ids) || !empty($update_ids) && !empty($update_laebels) || $create_labels) {
// 重新查询最新的 home_labels 数据
$command = "SELECT * FROM {$this->home_labels}";
$update_data = $this->dbQueryAllLine($command, 'Query home_labels failed');
$update_data = $this->dbQueryHomeLabelAll();
$update_ids = array_column($update_data, 'id');
// 更新 home_config 的id
$update_label_id = "UPDATE {$this->home_config} SET label_id = ?";
Expand Down

0 comments on commit 40f053a

Please sign in to comment.