Skip to content

Commit

Permalink
Use a more robust json_decode
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Friedman <[email protected]>
  • Loading branch information
iMattPro committed Jun 26, 2024
1 parent 69b874e commit eada9dc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
5 changes: 3 additions & 2 deletions controller/acp_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ protected function list_announcements()
'EXPIRY_DATE' => $row['announcement_expiry'],
'S_EXPIRED' => $expired,
'S_ENABLED' => $enabled,
'LOCATIONS' => json_decode($row['announcement_locations'], true),
'LOCATIONS' => $this->manager->decode_json($row['announcement_locations']),
'U_EDIT' => $this->u_action . '&amp;action=add&amp;id=' . $row['announcement_id'],
'U_DELETE' => $this->u_action . '&amp;action=delete&amp;id=' . $row['announcement_id'],
'U_MOVE_UP' => $this->u_action . '&amp;action=move&amp;id=' . $row['announcement_id'] . '&amp;dir=up&amp;hash=' . generate_link_hash('up' . $row['announcement_id']),
Expand Down Expand Up @@ -229,6 +229,7 @@ protected function action_add()
$data['announcement_expiry'] = 0;
}

// Locations array should be json encoded for storage in the DB
$data['announcement_locations'] = json_encode($data['announcement_locations']);

// Prepare announcement text for storage
Expand Down Expand Up @@ -455,7 +456,7 @@ protected function log_change($msg, $params)
*/
protected function get_location_options($locations)
{
$selected = !empty($locations) ? json_decode($locations, true) : [];
$selected = !empty($locations) ? $this->manager->decode_json($locations) : [];

$forum_list = make_forum_select($selected, false, false, false, false, false, true);

Expand Down
2 changes: 1 addition & 1 deletion event/listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function display_board_announcements()
$board_announcements_data = $this->manager->get_visible_announcements($this->user->data['user_id']);

$board_announcements_data = array_filter($board_announcements_data, function ($data) {
$locations = json_decode($data['announcement_locations'], true);
$locations = $this->manager->decode_json($data['announcement_locations']);
$is_index = $this->user->page['page_name'] === "index.$this->php_ext";
$current_page = $is_index ? ext::INDEX_ONLY : $this->request->variable('f', 0);

Expand Down
12 changes: 12 additions & 0 deletions manager/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,18 @@ public function close_announcement($id, $user_id)
return (bool) $this->nestedset->insert_tracked_item($id, ['user_id' => (int) $user_id]);
}

/**
* Decode JSON data
*
* @param string $data
* @return mixed decoded json data or original data if not valid json
*/
public function decode_json($data)
{
$result = json_decode($data, true);
return json_last_error() === JSON_ERROR_NONE ? $result : $data;
}

/**
* Filter members only announcements
*
Expand Down
4 changes: 4 additions & 0 deletions tests/controller/acp_controller_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,10 @@ public function test_action_add_submit($id, $form, $preview, $submit, $valid_for
->withConsecutive(['submit'], ['preview'])
->willReturnOnConsecutiveCalls($submit, $preview);

$this->manager->expects(self::once())
->method('decode_json')
->willReturn([]);

$this->manager->expects(self::once())
->method('announcement_columns')
->willReturn([]);
Expand Down

0 comments on commit eada9dc

Please sign in to comment.