plugin_weathermap icon indicating copy to clipboard operation
plugin_weathermap copied to clipboard

`weathermap_repair_maps()` destroys maps instead of repairing them

Open Jay2k1 opened this issue 2 years ago • 2 comments

https://github.com/Cacti/plugin_weathermap/blob/3c6e0e94a954e66a24ba0e81d17b419553575eaf/lib/poller-common.php#L100-L263

This function is being called whenever you open the weathermap map list (console > management > weathermaps). Back in the day, background and icon images were stored in the images subfolder of the plugin. At some point, it was decided to put them into the subfolders images/backgrounds/ and images/objects/. The only purpose of this function is to migrate old configs by moving the images into their new subfolders and update the paths in the config. However, it destroys them in the process:

For each weathermap config file, it reads the config and loops over all lines.

For each line of the current config,

  • if BACKGROUND is in the line, it validates if it's in the new images/backgrounds folder and the path is referencing it there.
    • if the image is still in /images instead of /images/backgrounds, it moves it and updates the path.
    • if the image is already in the /images/backgrounds folder but the path is still pointing to the old images location, it only updates the path.
    • The altered BACKGROUND line is added to the $outcontents array. It then indicates it changed something by doing $changes++
  • The same treatment is applied to all lines containing ICON

Finally, if ($changes), it overwrites the config file with the contents of the $outcontents array. However, because only lines containing BACKGROUND and ICON have been looked at, all other lines of the config haven't been added to the $outcontents array, so they get lost. The config is reduced to only containing BACKGROUND and ICON lines, effectively destroying that weathermap.


Additionally, there's a copy-paste error, $objfile should be $bgfile here: https://github.com/Cacti/plugin_weathermap/blob/3c6e0e94a954e66a24ba0e81d17b419553575eaf/lib/poller-common.php#L176


Also, before renaming (moving) the background image, it tries to check if the new file name $new is writeable, but this cannot be true if the file does not exist. https://github.com/Cacti/plugin_weathermap/blob/3c6e0e94a954e66a24ba0e81d17b419553575eaf/lib/poller-common.php#L162

Jay2k1 avatar Nov 14 '23 13:11 Jay2k1

How about a pull request on this please?

TheWitness avatar Nov 15 '23 14:11 TheWitness

@Jay2k1 , Pull request?

TheWitness avatar Dec 03 '23 18:12 TheWitness