diff --git a/lib/editor.assets.php b/lib/editor.assets.php index a88edef7..e8c5c65b 100644 --- a/lib/editor.assets.php +++ b/lib/editor.assets.php @@ -210,6 +210,46 @@ function wm_asset_image_users($name, $kind = null, $references = null) { } return array_values(array_unique($users)); } +/** + * Encode a relative images/ path as the preview URL the editor JavaScript accepts. + * + * @param string $path Relative configuration path such as images/objects/. + * + * @return string The per-segment rawurlencoded URL, or '' outside images/. + */ +function wm_asset_image_url($path) { + if (!is_string($path) || strpos($path, 'images/') !== 0) return ''; + return implode('/', array_map('rawurlencode', explode('/', $path))); +} +/** + * Scan one image library with the rules shared by the selectors and the library dialog. + * + * An entry is a readable regular file (symlinks to one included) named .png, .gif, .jpg + * or .jpeg whose content is a PNG, JPEG or GIF raster. Symlinks are listed with + * managed=false so selectors keep offering them while library management refuses them. + * + * @param string $kind The objects or backgrounds library. + * @param string|null $root Optional plugin directory; defaults to this plugin. + * + * @return array[] Entries {kind, name, url, path, width, height, managed} in filename order. + */ +function wm_asset_library_images($kind, $root = null) { + // Raises the shared invalid-category error for anything but the two libraries. + if (!in_array($kind, ['objects', 'backgrounds'], true)) wm_asset_directory($kind); + $parent = ($root ?? dirname(__DIR__)) . '/images'; + $dir = $parent . '/' . $kind; + $linkedDir = is_link($parent) || is_link($dir); + $images = []; + foreach ((is_dir($dir) ? scandir($dir) : false) ?: [] as $name) { + $path = $dir . '/' . $name; + if (!is_file($path) || !is_readable($path) || !preg_match('/\.(png|gif|jpe?g)$/iD', $name)) continue; + $size = @getimagesize($path); + if (!$size || !in_array($size[2], [IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF], true)) continue; + $relative = 'images/' . $kind . '/' . $name; + $images[] = ['kind' => $kind, 'name' => $name, 'url' => wm_asset_image_url($relative), 'path' => $relative, 'width' => $size[0], 'height' => $size[1], 'managed' => !$linkedDir && !is_link($path)]; + } + return $images; +} /** * List image metadata using one shared configuration reference index. * @@ -221,14 +261,13 @@ function wm_asset_image_list($references = null) { $result = []; $references = $references ?? wm_asset_image_reference_index(); foreach (['objects', 'backgrounds'] as $kind) { - $dir = wm_asset_directory($kind); - foreach (scandir($dir) ?: [] as $name) { - $path = $dir . '/' . $name; - if (!is_file($path) || is_link($path) || !is_readable($path) || !preg_match('/\.(png|gif|jpe?g)$/iD', $name)) continue; - $size = @getimagesize($path); - if (!$size || !in_array($size[2], [IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF], true)) continue; + // Rejects a symlinked library before any entry is offered for management. + wm_asset_directory($kind); + foreach (wm_asset_library_images($kind) as $image) { + if (!$image['managed']) continue; + $name = $image['name']; $users = wm_asset_image_users($name, $kind, $references); - $result[] = ['width' => $size ? $size[0] : null, 'height' => $size ? $size[1] : null, 'users' => $users, 'kind' => $kind, 'name' => $name, 'url' => 'images/' . $kind . '/' . rawurlencode($name), 'deletable' => !$users && (bool) preg_match('/^[A-Za-z0-9_.-]+\.(png|gif|jpe?g)$/iD', $name) && strpos($name, '..') === false]; + $result[] = ['width' => $image['width'], 'height' => $image['height'], 'users' => $users, 'kind' => $kind, 'name' => $name, 'url' => $image['url'], 'deletable' => !$users && (bool) preg_match('/^[A-Za-z0-9_.-]+\.(png|gif|jpe?g)$/iD', $name) && strpos($name, '..') === false]; } } return $result; diff --git a/lib/editor.inc.php b/lib/editor.inc.php index 6331ea7b..0339ebd5 100644 --- a/lib/editor.inc.php +++ b/lib/editor.inc.php @@ -700,38 +700,71 @@ function extract_with_validation($array, $paramarray, $prefix = '') { } /** - * @param mixed $imagedir - * @return mixed + * List the relative configuration paths of one image library. + * + * @param string $imagedir The objects or backgrounds library. + * @return string[] Paths such as images/objects/, in filename order. */ function get_imagelist($imagedir) { global $config; - $imagelist = []; + require_once __DIR__ . '/editor.assets.php'; - $imdir = $config['base_path'] . '/plugins/weathermap/images/' . $imagedir; + return array_column(wm_asset_library_images($imagedir, $config['base_path'] . '/plugins/weathermap'), 'path'); +} - if (is_dir($imdir)) { - $n = 0; - $dh = opendir($imdir); +/** + * Build editor selector entries from a library plus images the map already uses. + * + * Used paths missing from the library (for example outside images/) keep their + * value; their preview URL is empty unless they sit under images/. + * + * @param string $kind The objects or backgrounds library. + * @param string[] $used Image paths referenced by the map. + * @param string|null $root Optional plugin directory; defaults to the installed plugin. + * @return array[] Entries {kind, name, url, path} sorted by path. + */ +function wm_editor_image_options($kind, $used = [], $root = null) { + global $config; - if ($dh) { - while ($file = readdir($dh)) { - $realfile = $imdir . '/' . $file; - $uri = "images/$imagedir/$file"; - - if (is_file($realfile) && is_readable($realfile) && (preg_match('/\.(gif|jpe?g|png)$/i', $file))) { - $size = @getimagesize($realfile); - if (!$size || !in_array($size[2], [IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF], true)) continue; - $imagelist[] = $uri; - $n++; - } - } + require_once __DIR__ . '/editor.assets.php'; - closedir($dh); + $entries = []; + $paths = []; + + foreach (wm_asset_library_images($kind, $root ?? $config['base_path'] . '/plugins/weathermap') as $image) { + $entries[] = ['kind' => $kind, 'name' => $image['name'], 'url' => $image['url'], 'path' => $image['path']]; + $paths[$image['path']] = true; + } + + foreach ($used as $image) { + if (!is_string($image) || isset($paths[$image])) { + continue; } + + $entries[] = ['kind' => $kind, 'name' => basename($image), 'url' => wm_asset_image_url($image), 'path' => $image]; + $paths[$image] = true; } - return ($imagelist); + // Same comparison as the sort() the editor applied to the plain path list. + usort($entries, function ($a, $b) { + return $a['path'] <=> $b['path']; + }); + + return $entries; +} + +/** + * Render one selector option whose preview source is the entry's encoded URL. + * + * @param array $entry Entry from wm_editor_image_options(). + * @param string|null $selected Path to mark selected, or null for none. + * @return string The option element. + */ +function wm_editor_image_option($entry, $selected = null) { + $display = ucfirst(str_replace(['.png', '.gif', '.jpg'], '', basename($entry['path']))); + + return wm_editor_option($display, ['selected' => $selected !== null && $entry['path'] == $selected, 'data-description' => $display, 'data-imagesrc' => $entry['url'], 'value' => $entry['path']]); } /** diff --git a/tests/Browser/EditorImageOptionsTest.cjs b/tests/Browser/EditorImageOptionsTest.cjs new file mode 100644 index 00000000..0f069fe3 --- /dev/null +++ b/tests/Browser/EditorImageOptionsTest.cjs @@ -0,0 +1,55 @@ +// Browser half of the editor image-option contract: the server markup pinned by +// tests/E2E/EditorImageOptionsFixtureTest.php, previewed through the real wmEditorImageSrc(). +const {JSDOM}=require('jsdom'); +const jquery=require('jquery'); +const fs=require('node:fs'); +const path=require('node:path'); +const assert=require('node:assert/strict'); +const markup=fs.readFileSync(path.join(__dirname,'fixtures/editor-image-options.html'),'utf8'); +const dom=new JSDOM('
'+markup+'
',{runScripts:'outside-only',pretendToBeVisual:true,url:'https://example.test/cacti/plugins/weathermap/weathermap-cacti-plugin-editor.php'}); +const w=dom.window,$=jquery(w);w.$=w.jQuery=$;w.selectedNode='images/objects/core router.png'; +const inc=fs.readFileSync(path.join(__dirname,'../../lib/editor.inc.php'),'utf8'); +w.wmEditorText={}; +for(const match of inc.matchAll(/'(icon[A-Za-z]+)' => __\('([^']+)'/g))w.wmEditorText[match[1]]=match[2]; +// Evaluate the whole editor scripts the page includes, in page order, so the +// check does not depend on which file declares wmEditorImageSrc(). editor.js +// runs initJS() on DOM ready; this check needs only the picker functions. +for(const name of ['editor.js','editor.dialogs.js','editor.picker.js','editor.compact.js','editor.assets.js']){ + const file=path.join(__dirname,'../../js',name); + if(fs.existsSync(file))w.eval(fs.readFileSync(file,'utf8')); +} +w.initJS=()=>{}; +const base='https://example.test/cacti/plugins/weathermap/'; +const expected={ + node_iconfilename:[ + ['/srv/icons/outside.png',''], + ['images/../setup.png',''], + ['images/objects/%2e%2e/%2e%2e/setup.png',base+'images/objects/%252e%252e/%252e%252e/setup.png'], + ['images/objects/100%.png',base+'images/objects/100%25.png'], + ['images/objects/café.png',base+'images/objects/caf%C3%A9.png'], + ['images/objects/core router.png',base+'images/objects/core%20router.png'], + ['images/objects/custom folder/site.png',base+'images/objects/custom%20folder/site.png'], + ['images/objects/router.png',base+'images/objects/router.png'], + ['images/objects/site#2.png',base+'images/objects/site%232.png'], + ], + map_bgfile:[ + ['images/backgrounds/plain.gif',base+'images/backgrounds/plain.gif'], + ['images/backgrounds/world map.jpg',base+'images/backgrounds/world%20map.jpg'], + ], +}; +for(const [id,rows] of Object.entries(expected)){ + const options=$('#'+id+' option').toArray(); + assert.deepEqual(options.map(o=>o.value),rows.map(r=>r[0]),id+' values keep the configuration paths in server order'); + options.forEach((option,i)=>{ + const imagesrc=option.getAttribute('data-imagesrc'); + assert.equal(w.wmEditorImageSrc(imagesrc),rows[i][1],id+' preview for '+option.value); + // The server already sends the canonical encoding, so the browser resolves it unchanged. + if(rows[i][1])assert.equal(rows[i][1],base+imagesrc,'server url is the preview path for '+option.value); + }); +} +assert.equal($('#map_bgfile').val(),'images/backgrounds/world map.jpg','selected background survives encoding'); +w.wmNodeIconPicker(); +const previews=$('.wm-icon-choice').toArray().map(b=>{const img=b.querySelector('img');return img?img.src:'';}); +assert.deepEqual(previews,expected.node_iconfilename.map(r=>r[1]),'icon picker renders exactly the accepted previews'); +assert.equal($('#node_iconfilename').val(),'images/objects/core router.png'); +console.log('Editor image option previews PASS'); diff --git a/tests/Browser/fixtures/editor-image-options.html b/tests/Browser/fixtures/editor-image-options.html new file mode 100644 index 00000000..98a26755 --- /dev/null +++ b/tests/Browser/fixtures/editor-image-options.html @@ -0,0 +1,2 @@ + + diff --git a/tests/E2E/EditorImageOptionListsTest.php b/tests/E2E/EditorImageOptionListsTest.php index d765720c..63cc4022 100644 --- a/tests/E2E/EditorImageOptionListsTest.php +++ b/tests/E2E/EditorImageOptionListsTest.php @@ -28,6 +28,7 @@ beforeAll(function () { require_once __DIR__ . '/../../lib/editor.inc.php'; + require_once __DIR__ . '/../../lib/editor.assets.php'; }); /** @@ -78,7 +79,7 @@ function wm_option_lists_file($code) { * @param array $backgrounds Background image file names. * @param array $used Image paths the map already uses. * - * @return array{0: array, 1: array} + * @return array{0: array, 1: array} Entries from wm_editor_image_options(). */ function wm_option_lists_build(array $objects, array $backgrounds, array $used) { global $config; @@ -98,7 +99,7 @@ function wm_option_lists_build(array $objects, array $backgrounds, array $used) $config['base_path'] = $base; $map = (object) ['used_images' => $used]; - $code = wm_option_lists_file(wm_option_lists_code("\$image_list = get_imagelist('objects');", 'sort($image_list);')); + $code = wm_option_lists_file(wm_option_lists_code("\$image_list = wm_editor_image_options('objects'", "\$backgd_list = wm_editor_image_options('backgrounds');")); try { include $code; @@ -195,13 +196,13 @@ function wm_option_lists_label($path) { ->and($options[0])->toBe(['attributes' => ['data-description' => 'Label Only Icon', 'data-imagesrc' => '', 'value' => '--NONE--'], 'text' => 'Label Only']) ->and($options[1])->toBe(['attributes' => ['data-description' => 'Special Purpose Icon', 'data-imagesrc' => '', 'value' => '--AICON--'], 'text' => 'Special Icon (AICON)']); - foreach ($image_list as $index => $im) { + foreach (array_column($image_list, 'path') as $index => $im) { $label = wm_option_lists_label($im); - expect($options[$index + 2])->toBe(['attributes' => ['data-description' => $label, 'data-imagesrc' => $im, 'value' => $im], 'text' => $label]); + expect($options[$index + 2])->toBe(['attributes' => ['data-description' => $label, 'data-imagesrc' => wm_asset_image_url($im), 'value' => $im], 'text' => $label]); } foreach ($files as $file) { - expect($image_list)->toContain('images/objects/' . $file); + expect(array_column($image_list, 'path'))->toContain('images/objects/' . $file); } })->with('hostile image names'); @@ -215,9 +216,9 @@ function wm_option_lists_label($path) { expect($options)->toHaveCount(count($backgd_list) + 1) ->and($options[0])->toBe(['attributes' => ['data-description' => 'Use Map Background Colour', 'data-imagesrc' => '', 'value' => '--NONE--'], 'text' => '--NO BACKGROUND--']); - foreach ($backgd_list as $index => $im) { + foreach (array_column($backgd_list, 'path') as $index => $im) { $label = wm_option_lists_label($im); - $expected = ['data-description' => $label, 'data-imagesrc' => $im, 'value' => $im]; + $expected = ['data-description' => $label, 'data-imagesrc' => wm_asset_image_url($im), 'value' => $im]; if ($im === $current) { $expected = ['selected' => 'selected'] + $expected; diff --git a/tests/E2E/EditorImageOptionsFixtureTest.php b/tests/E2E/EditorImageOptionsFixtureTest.php new file mode 100644 index 00000000..498dc12c --- /dev/null +++ b/tests/E2E/EditorImageOptionsFixtureTest.php @@ -0,0 +1,58 @@ + wm_editor_image_option($entry, $selected), $entries)); + }; + $markup = '\n" + . '\n"; + } finally { + wm_fixture_remove($root); + } + + $fixture = dirname(__DIR__) . '/Browser/fixtures/editor-image-options.html'; + + if (getenv('WM_UPDATE_FIXTURES') === '1') { + file_put_contents($fixture, $markup); + } + + expect(file_get_contents($fixture))->toBe($markup); + expect($markup)->not->toContain('broken.png'); + expect($markup)->not->toContain('vector.svg'); +}); diff --git a/tests/Helpers/ImageLibraryFixture.php b/tests/Helpers/ImageLibraryFixture.php new file mode 100644 index 00000000..bec6207d --- /dev/null +++ b/tests/Helpers/ImageLibraryFixture.php @@ -0,0 +1,51 @@ +isLink()) @chmod($entry->getPathname(), 0700); + if ($entry->isDir() && !$entry->isLink()) rmdir($entry->getPathname()); else unlink($entry->getPathname()); + } + rmdir($root); +} diff --git a/tests/Unit/EditorImageListTest.php b/tests/Unit/EditorImageListTest.php new file mode 100644 index 00000000..df660af1 --- /dev/null +++ b/tests/Unit/EditorImageListTest.php @@ -0,0 +1,160 @@ +root = wm_fixture_plugin_root(); + $this->objects = $this->root . '/images/objects'; +}); + +afterEach(function (): void { + wm_fixture_remove($this->root); +}); + +it('returns the kind, name, url and path shape for each raster format', function (): void { + wm_fixture_raster($this->objects . '/router.png'); + wm_fixture_raster($this->objects . '/switch.gif', 'gif'); + wm_fixture_raster($this->objects . '/server.jpg', 'jpeg'); + wm_fixture_raster($this->objects . '/Firewall.JPEG', 'jpeg'); + + $images = wm_asset_library_images('objects', $this->root); + + expect(array_column($images, 'name'))->toBe(['Firewall.JPEG', 'router.png', 'server.jpg', 'switch.gif']); + expect($images[1])->toBe(['kind' => 'objects', 'name' => 'router.png', 'url' => 'images/objects/router.png', 'path' => 'images/objects/router.png', 'width' => 4, 'height' => 3, 'managed' => true]); +}); + +it('keeps the raw path and encodes the url for unusual filenames', function (): void { + $names = ['core router.png' => 'core%20router.png', '100%.png' => '100%25.png', 'site#2.png' => 'site%232.png', 'café-ünï.png' => 'caf%C3%A9-%C3%BCn%C3%AF.png', 'a?b.png' => 'a%3Fb.png']; + foreach (array_keys($names) as $name) wm_fixture_raster($this->objects . '/' . $name); + + $byName = array_column(wm_asset_library_images('objects', $this->root), null, 'name'); + + expect(array_keys($byName))->toEqualCanonicalizing(array_keys($names)); + foreach ($names as $name => $encoded) { + expect($byName[$name]['path'])->toBe('images/objects/' . $name); + expect($byName[$name]['url'])->toBe('images/objects/' . $encoded); + } +}); + +it('rejects files whose extension is not a supported raster', function (): void { + foreach (['icon.svg', 'icon.php', 'icon.png.txt', 'icon.bmp', "icon.png\n", 'icon'] as $name) wm_fixture_raster($this->objects . '/' . $name); + + expect(wm_asset_library_images('objects', $this->root))->toBe([]); +}); + +it('rejects files whose content is not a PNG, JPEG or GIF raster', function (): void { + file_put_contents($this->objects . '/text.png', 'not an image'); + file_put_contents($this->objects . '/truncated.gif', 'GIF8'); + file_put_contents($this->objects . '/bitmap.png', 'BM' . pack('VvvV', 58, 0, 0, 54) . pack('VllvvVVllVV', 40, 1, 1, 1, 24, 0, 4, 0, 0, 0, 0) . "\0\0\0\0"); + file_put_contents($this->objects . '/vector.png', ''); + + expect(getimagesize($this->objects . '/bitmap.png')[2])->toBe(IMAGETYPE_BMP); + expect(wm_asset_library_images('objects', $this->root))->toBe([]); +}); + +it('rejects directories and unreadable files', function (): void { + mkdir($this->objects . '/folder.png'); + wm_fixture_raster($this->objects . '/locked.png'); + chmod($this->objects . '/locked.png', 0); + + if (is_readable($this->objects . '/locked.png')) { + $this->markTestSkipped('running with privileges that bypass file permissions'); + } + + expect(wm_asset_library_images('objects', $this->root))->toBe([]); +}); + +it('lists symlinked rasters as selectable but not managed', function (): void { + wm_fixture_raster($this->root . '/outside.png'); + symlink($this->root . '/outside.png', $this->objects . '/alias.png'); + symlink($this->root . '/missing.png', $this->objects . '/dangling.png'); + + $images = wm_asset_library_images('objects', $this->root); + + expect(cacti_count($images))->toBe(1); + expect($images[0]['name'])->toBe('alias.png'); + expect($images[0]['managed'])->toBeFalse(); +}); + +it('marks every entry unmanaged when the library directory is a symlink', function (): void { + $shared = $this->root . '/shared'; + mkdir($shared); + wm_fixture_raster($shared . '/linked.png'); + rmdir($this->objects); + symlink($shared, $this->objects); + + $images = wm_asset_library_images('objects', $this->root); + + expect(array_column($images, 'name'))->toBe(['linked.png']); + expect($images[0]['managed'])->toBeFalse(); +}); + +it('returns an empty list for empty and missing libraries', function (): void { + expect(wm_asset_library_images('objects', $this->root))->toBe([]); + rmdir($this->root . '/images/backgrounds'); + expect(wm_asset_library_images('backgrounds', $this->root))->toBe([]); + expect(wm_asset_library_images('objects', $this->root . '/absent'))->toBe([]); +}); + +it('rejects unknown library kinds', function (): void { + wm_asset_library_images('../configs', $this->root); +})->throws(InvalidArgumentException::class); + +it('encodes only relative images paths as preview urls', function (): void { + expect(wm_asset_image_url('images/objects/custom folder/100%.png'))->toBe('images/objects/custom%20folder/100%25.png'); + expect(wm_asset_image_url('images/objects/%2e%2e/setup.png'))->toBe('images/objects/%252e%252e/setup.png'); + expect(wm_asset_image_url('/var/www/images/objects/a.png'))->toBe(''); + expect(wm_asset_image_url('other/a.png'))->toBe(''); + expect(wm_asset_image_url(null))->toBe(''); +}); + +it('lists the configured plugin library through get_imagelist', function (): void { + $base = $this->root . '/cacti'; + mkdir($base . '/plugins/weathermap/images/backgrounds', 0777, true); + wm_fixture_raster($base . '/plugins/weathermap/images/backgrounds/b map.jpg', 'jpeg'); + wm_fixture_raster($base . '/plugins/weathermap/images/backgrounds/a.png'); + $saved = $GLOBALS['config']['base_path']; + $GLOBALS['config']['base_path'] = $base; + + try { + expect(get_imagelist('backgrounds'))->toBe(['images/backgrounds/a.png', 'images/backgrounds/b map.jpg']); + expect(array_column(wm_editor_image_options('backgrounds'), 'path'))->toBe(['images/backgrounds/a.png', 'images/backgrounds/b map.jpg']); + } finally { + $GLOBALS['config']['base_path'] = $saved; + } +}); + +it('merges used images into the sorted selector list once', function (): void { + wm_fixture_raster($this->objects . '/router.png'); + wm_fixture_raster($this->objects . '/hub.png'); + $used = ['images/objects/router.png', 'images/objects/zz/site.png', 'images/objects/router.png', '/srv/icons/a.png', 'images/objects/zz/site.png', null]; + + $entries = wm_editor_image_options('objects', $used, $this->root); + + expect($entries)->toBe([ + ['kind' => 'objects', 'name' => 'a.png', 'url' => '', 'path' => '/srv/icons/a.png'], + ['kind' => 'objects', 'name' => 'hub.png', 'url' => 'images/objects/hub.png', 'path' => 'images/objects/hub.png'], + ['kind' => 'objects', 'name' => 'router.png', 'url' => 'images/objects/router.png', 'path' => 'images/objects/router.png'], + ['kind' => 'objects', 'name' => 'site.png', 'url' => 'images/objects/zz/site.png', 'path' => 'images/objects/zz/site.png'], + ]); +}); + +it('renders escaped options with the encoded url as preview source', function (): void { + $quoted = ['kind' => 'backgrounds', 'name' => 'a "b".png', 'url' => 'images/backgrounds/a%20%22b%22.png', 'path' => 'images/backgrounds/a "b".png']; + $plain = ['kind' => 'backgrounds', 'name' => 'plain.jpg', 'url' => 'images/backgrounds/plain.jpg', 'path' => 'images/backgrounds/plain.jpg']; + + expect(wm_editor_image_option($quoted, 'images/backgrounds/plain.jpg'))->toBe(''); + expect(wm_editor_image_option($plain, 'images/backgrounds/plain.jpg'))->toBe(''); + expect(wm_editor_image_option($plain))->not->toContain('selected'); +}); diff --git a/weathermap-cacti-plugin-editor.php b/weathermap-cacti-plugin-editor.php index a256ac7c..5d8c16f1 100644 --- a/weathermap-cacti-plugin-editor.php +++ b/weathermap-cacti-plugin-editor.php @@ -274,18 +274,9 @@ fixMapBackgroundAndImages($map); [$editor_width, $editor_height] = wm_note_effective_dimensions($map); -// get the list from the images/ folder too -$image_list = get_imagelist('objects'); -$backgd_list = get_imagelist('backgrounds'); - -// append any images used in the map that aren't in the images folder -foreach ($map->used_images as $im) { - if (!in_array($im, $image_list, true)) { - $image_list[] = $im; - } -} - -sort($image_list); +// get the list from the images/ folder, plus any icons the map uses from elsewhere +$image_list = wm_editor_image_options('objects', $map->used_images); +$backgd_list = wm_editor_image_options('backgrounds'); cacti_cookie_set('wmeditor', ($use_overlay ? '1' : '0') . ':' . ($use_relative_overlay ? '1' : '0') . ':' . intval($grid_snap_value)); @@ -465,10 +456,8 @@ print wm_editor_option('Label Only', ['data-description' => 'Label Only Icon', 'data-imagesrc' => '', 'value' => '--NONE--']); print wm_editor_option('Special Icon (AICON)', ['data-description' => 'Special Purpose Icon', 'data-imagesrc' => '', 'value' => '--AICON--']); - foreach ($image_list as $im) { - $display = ucfirst(str_replace(['.png', '.gif', '.jpg'], '', basename($im))); - - print wm_editor_option($display, ['data-description' => $display, 'data-imagesrc' => $im, 'value' => $im]); + foreach ($image_list as $image) { + print wm_editor_image_option($image); } } ?> @@ -701,10 +690,8 @@ } else { print wm_editor_option(__('--NO BACKGROUND--', 'weathermap'), ['data-description' => __('Use Map Background Colour', 'weathermap'), 'data-imagesrc' => '', 'value' => '--NONE--']); - foreach ($backgd_list as $im) { - $display = ucfirst(str_replace(['.png', '.gif', '.jpg'], '', basename($im))); - - print wm_editor_option($display, ['selected' => $im == $map->background, 'data-description' => $display, 'data-imagesrc' => $im, 'value' => $im]); + foreach ($backgd_list as $image) { + print wm_editor_image_option($image, $map->background); } } ?>