Commit ee876a12 authored by Chris Graham's avatar Chris Graham
Browse files

Fixed MANTIS-4499 (SQL error in theme images cleanup tool)

parent 7fbd6983
Loading
Loading
Loading
Loading
+17 −13
Original line number Diff line number Diff line
@@ -582,28 +582,32 @@ function erase_theme_images_cache()

    $_paths = $GLOBALS['SITE_DB']->query_select('theme_images', array('theme', 'id', 'path', 'lang'));
    $paths = array();
    foreach ($_paths as $path) {
        $paths[serialize($path)] = $path;
    foreach ($_paths as $image_details) {
        $image_details_key = $image_details;
        unset($image_details_key['path']);
        $paths[serialize($image_details_key)] = $image_details['path'];
    }

    foreach ($paths as $path) {
        if (!file_exists(get_file_base() . '/themes/' . $path['theme'])) {
    foreach ($paths as $_image_details_key => $path) {
        $image_details_key = unserialize($_image_details_key);
        if (!file_exists(get_file_base() . '/themes/' . $image_details_key['theme'])) {
            // Delete: a non-existent theme
            $GLOBALS['SITE_DB']->query_delete('theme_images', $path, '', 1);
        } elseif ($path['path'] == '') {
            $GLOBALS['SITE_DB']->query_delete('theme_images', $image_details_key, '', 1);
        } elseif ($path == '') {
            // Delete: A blank path, should not be there
            $GLOBALS['SITE_DB']->query_delete('theme_images', $path, '', 1);
        } elseif (preg_match('#^themes/[^/]+/images_custom/#', $path['path']) != 0) {
            if ((!file_exists(get_custom_file_base() . '/' . rawurldecode($path['path']))) && (!file_exists(get_file_base() . '/' . rawurldecode($path['path'])))) {
            $GLOBALS['SITE_DB']->query_delete('theme_images', $image_details_key, '', 1);
        } elseif (preg_match('#^themes/[^/]+/images_custom/#', $path) != 0) {
            if ((!file_exists(get_custom_file_base() . '/' . rawurldecode($path))) && (!file_exists(get_file_base() . '/' . rawurldecode($path)))) {
                // Delete: Custom disk file does not actually exist
                $GLOBALS['SITE_DB']->query_delete('theme_images', $path, '', 1);
                $GLOBALS['SITE_DB']->query_delete('theme_images', $image_details_key, '', 1);
            } else {
                if ($path['theme'] == 'default') {
                if ($image_details_key['theme'] == 'default') {
                    // Add: Custom images in default theme should be in all themes
                    foreach (array_keys($all_themes) as $theme) {
                        if ($theme != 'default') {
                            $insert_map = array('theme' => $theme, 'id' => $path['id'], 'path' => $path['path'], 'lang' => $path['lang']);
                            if (!isset($paths[serialize($insert_map)])) {
                            $insert_key_map = array('theme' => $theme, 'id' => $image_details_key['id'], 'lang' => $image_details_key['lang']);
                            $insert_map = $insert_key_map + array('path' => $path);
                            if (!isset($paths[serialize($insert_key_map)])) {
                                $GLOBALS['SITE_DB']->query_insert('theme_images', $insert_map);
                            }
                        }