예제 #1
0
/**
 * Regenerate all the theme image paths in the database.
 *
 * @param  ID_TEXT	The theme we're searching in.
 * @param  ?array		A map of languages (lang=>1) (NULL: find it in-function).
 * @param  ?ID_TEXT  The theme we're storing in (NULL: same as $theme).
 */
function regen_theme_images($theme, $langs = NULL, $target_theme = NULL)
{
    if (is_null($langs)) {
        $langs = find_all_langs(true);
    }
    if (is_null($target_theme)) {
        $target_theme = $theme;
    }
    $images = array_merge(find_images_do_dir($theme, 'images/', $langs), find_images_do_dir($theme, 'images_custom/', $langs));
    foreach (array_keys($langs) as $lang) {
        $existing = $GLOBALS['SITE_DB']->query_select('theme_images', array('id', 'path'), array('lang' => $lang, 'theme' => $target_theme));
        foreach ($images as $id => $path) {
            $found = false;
            foreach ($existing as $e) {
                if ($e['path'] == $path || $e['id'] == $id) {
                    $found = true;
                    break;
                }
            }
            if (!$found) {
                $nql_backup = $GLOBALS['NO_QUERY_LIMIT'];
                $GLOBALS['NO_QUERY_LIMIT'] = true;
                $correct_path = find_theme_image($id, false, true, $theme, $lang);
                $GLOBALS['SITE_DB']->query_insert('theme_images', array('id' => $id, 'lang' => $lang, 'theme' => $target_theme, 'path' => $correct_path), false, true);
                // race conditions
                $GLOBALS['NO_QUERY_LIMIT'] = $nql_backup;
            }
        }
    }
    persistant_cache_delete('THEME_IMAGES');
}
예제 #2
0
/**
 * Recursively find theme images under the specified details. Does not find custom theme images, as it doesn't check the DB.
 *
 * @param  ID_TEXT		The theme
 * @param  string			The subdirectory to search under
 * @param  array			A map (lang=>1) of the languages in the system, so the codes may be filtered out of the image codes in our result list
 * @return array			A map, theme-image-code=>URL
 */
function find_images_do_dir($theme, $subdir, $langs)
{
    $full = ($theme == 'default' ? get_file_base() : get_custom_file_base()) . '/themes/' . filter_naughty($theme) . '/' . filter_naughty($subdir);
    $out = array();
    $_dir = @opendir($full);
    if ($_dir !== false) {
        while (false !== ($file = readdir($_dir))) {
            if ($file != '.' && $file != '..') {
                if (is_dir($full . $file)) {
                    $out = array_merge($out, find_images_do_dir($theme, $subdir . $file . '/', $langs));
                } else {
                    $ext = substr($file, -4);
                    if ($ext == '.png' || $ext == '.gif' || $ext == '.jpg' || $ext == 'jpeg') {
                        $_file = explode('.', $file);
                        $_subdir = $subdir;
                        foreach (array_keys($langs) as $lang) {
                            $_subdir = str_replace('/' . $lang . '/', '/', $_subdir);
                        }
                        $_subdir = preg_replace('#(^|/)images(\\_custom)?/#', '', $_subdir);
                        $out[$_subdir . $_file[0]] = 'themes/' . rawurlencode($theme) . '/' . $subdir . rawurlencode($file);
                    }
                }
            }
        }
        closedir($_dir);
    }
    return $out;
}