Example #1
0
/**
 * Make a theme. Note that this will trigger the AFM.
 *
 * @param  string		Name of the theme.
 * @param  ID_TEXT	The theme it's being generated from
 * @param  ID_TEXT	The algorithm to use
 * @set equations hsv
 * @param  string		Seed colour to use.
 * @param  boolean	Whether to use the theme immediately.
 * @param  ?boolean  Whether it will be a dark theme (NULL: autodetect).
 * @param  boolean	Whether to inherit the CSS, for easier theme upgrading.
 */
function make_theme($themename, $source_theme, $algorithm, $seed, $use, $dark = false, $inherit_css = false)
{
    $GLOBALS['NO_QUERY_LIMIT'] = true;
    load_themewizard_params_from_theme($source_theme, $algorithm == 'hsv');
    if (file_exists(get_custom_file_base() . '/themes/' . $themename)) {
        require_code('abstract_file_manager');
        force_have_afm_details();
        $extending_existing = true;
    } else {
        if ($source_theme == 'default') {
            actual_add_theme($themename);
        } else {
            require_code('themes3');
            actual_copy_theme($source_theme, $themename);
        }
        $extending_existing = false;
    }
    if ($seed != find_theme_seed($source_theme) || $dark != find_theme_dark($source_theme)) {
        list($colours, $landscape) = calculate_theme($seed, $source_theme, $algorithm, 'colours', $dark);
        // Make images
        global $THEME_WIZARD_IMAGES, $THEME_WIZARD_IMAGES_NO_WILD, $IMG_CODES;
        if (function_exists('imagecolorallocatealpha')) {
            require_code('themes2');
            $full_img_set = array();
            foreach ($THEME_WIZARD_IMAGES as $expression) {
                if (substr($expression, -1) == '*') {
                    $expression = substr($expression, 0, strlen($expression) - 2);
                    // remove "/*"
                    $full_img_set = array_merge($full_img_set, array_keys(get_all_image_codes(get_file_base() . '/themes/' . filter_naughty($source_theme) . '/images', $expression)));
                    $full_img_set = array_merge($full_img_set, array_keys(get_all_image_codes(get_file_base() . '/themes/' . filter_naughty($source_theme) . '/images/' . fallback_lang(), $expression)));
                } else {
                    $full_img_set[] = $expression;
                }
            }
            if ($extending_existing) {
                $temp_all_ids = collapse_2d_complexity('id', 'path', $GLOBALS['SITE_DB']->query_select('theme_images', array('id', 'path'), array('theme' => $themename)));
            } else {
                $temp_all_ids = array();
            }
            $_langs = find_all_langs(true);
            foreach ($full_img_set as $image_code) {
                if (!in_array($image_code, $THEME_WIZARD_IMAGES_NO_WILD)) {
                    if ($extending_existing && array_key_exists($image_code, $temp_all_ids) && strpos($temp_all_ids[$image_code], $themename . '/images_custom/') !== false && (!url_is_local($temp_all_ids[$image_code]) || file_exists(get_custom_file_base() . '/' . $temp_all_ids[$image_code]))) {
                        continue;
                    }
                    foreach (array_keys($_langs) as $lang) {
                        $orig_path = find_theme_image($image_code, true, true, $source_theme, $lang);
                        if ($orig_path == '') {
                            continue;
                        }
                        // Theme has specified non-existent image as themewizard-compatible
                        if (strpos($orig_path, '/' . $lang . '/') === false && $lang != fallback_lang()) {
                            continue;
                        }
                        if (strpos($orig_path, '/' . fallback_lang() . '/') !== false) {
                            $composite = 'themes/' . filter_naughty($themename) . '/images/' . $lang . '/';
                        } else {
                            $composite = 'themes/' . filter_naughty($themename) . '/images/';
                        }
                        $saveat = get_custom_file_base() . '/' . $composite . $image_code . '.png';
                        $saveat_url = $composite . $image_code . '.png';
                        // Wipe out ones that might have been copied from source theme
                        if ($source_theme != 'default' && strpos($orig_path, 'images_custom') !== false) {
                            @unlink(str_replace('/images/', '/images_custom/', basename($saveat, '.png')) . '.png');
                            @unlink(str_replace('/images/', '/images_custom/', basename($saveat, '.png')) . '.jpg');
                            @unlink(str_replace('/images/', '/images_custom/', basename($saveat, '.png')) . '.gif');
                            @unlink(str_replace('/images/', '/images_custom/', basename($saveat, '.png')) . '.jpeg');
                        }
                        if (!file_exists($saveat) || $source_theme != 'default' || $algorithm == 'hsv') {
                            $image = calculate_theme($seed, $source_theme, $algorithm, $image_code, $dark, $colours, $landscape, $lang);
                            if (!is_null($image)) {
                                $pos = strpos($image_code, '/');
                                if ($pos !== false || strpos($orig_path, '/' . fallback_lang() . '/') !== false) {
                                    afm_make_directory($composite . substr($image_code, 0, $pos), true, true);
                                }
                                @imagepng($image, $saveat) or intelligent_write_error($saveat);
                                imagedestroy($image);
                                fix_permissions($saveat);
                                sync_file($saveat);
                                actual_edit_theme_image($image_code, $themename, $lang, $image_code, $saveat_url, true);
                                //if ($lang==fallback_lang()) $IMG_CODES['site'][$image_code]=$saveat_url;
                            }
                        } else {
                            actual_edit_theme_image($image_code, $themename, $lang, $image_code, $saveat_url, true);
                        }
                    }
                }
            }
        }
        // Make sheets
        $dh = opendir(get_file_base() . '/themes/' . filter_naughty($source_theme) . ($source_theme == 'default' ? '/css/' : '/css_custom/'));
        while (($sheet = readdir($dh)) !== false) {
            if (substr($sheet, -4) == '.css') {
                $saveat = get_custom_file_base() . '/themes/' . filter_naughty($themename) . '/css_custom/' . $sheet;
                if (!file_exists($saveat) || $source_theme != 'default' || $algorithm == 'hsv') {
                    $fp = @fopen($saveat, 'wt') or intelligent_write_error(get_custom_file_base() . '/themes/' . filter_naughty($themename) . '/css_custom/' . $sheet);
                    if ($inherit_css) {
                        $output = '{+START,CSS_INHERIT,' . basename($sheet, '.css') . ',' . filter_naughty($source_theme) . ',' . $seed . ',' . ($dark ? '1' : '0') . ',' . $algorithm . '}{+END}';
                    } else {
                        $output = theme_wizard_colours_to_sheet($sheet, $landscape, $source_theme, $algorithm, $seed);
                    }
                    if (fwrite($fp, $output) < strlen($output)) {
                        warn_exit(do_lang_tempcode('COULD_NOT_SAVE_FILE'));
                    }
                    fclose($fp);
                    fix_permissions($saveat);
                    sync_file($saveat);
                    if (!$inherit_css) {
                        $c_success = @copy(get_file_base() . '/themes/' . filter_naughty($source_theme) . '/css/' . $sheet, $saveat . '.editfrom');
                        if ($c_success !== false) {
                            fix_permissions($saveat . '.editfrom');
                            sync_file($saveat . '.editfrom');
                        }
                    } else {
                        @unlink($saveat . '.editfrom');
                    }
                }
            }
        }
    }
    // Use it, if requested
    if ($use) {
        $GLOBALS['SITE_DB']->query('UPDATE ' . get_table_prefix() . 'zones SET zone_theme=\'' . db_escape_string($themename) . '\' WHERE ' . db_string_not_equal_to('zone_name', 'cms') . ' AND ' . db_string_not_equal_to('zone_name', 'adminzone'));
        $admin_groups = $GLOBALS['FORUM_DRIVER']->get_super_admin_groups();
        $groups = $GLOBALS['FORUM_DRIVER']->get_usergroup_list(false, true);
        $GLOBALS['SITE_DB']->query_delete('group_category_access', array('module_the_name' => 'theme', 'category_name' => $themename));
        foreach (array_keys($groups) as $group_id) {
            if (in_array($group_id, $admin_groups)) {
                continue;
            }
            $GLOBALS['SITE_DB']->query_insert('group_category_access', array('module_the_name' => 'theme', 'category_name' => $themename, 'group_id' => $group_id));
        }
        persistant_cache_empty();
    }
}
Example #2
0
 /**
  * The actualiser to edit/rename a theme.
  *
  * @return tempcode		The UI
  */
 function _edit_theme()
 {
     if (post_param_integer('delete', 0) == 1) {
         $title = get_page_title('DELETE_THEME');
         $theme = get_param('old_theme', false, true);
         require_code('themes3');
         actual_delete_theme($theme);
         $to = '';
     } elseif (post_param_integer('copy', 0) == 1) {
         $title = get_page_title('COPY_THEME');
         $theme = get_param('old_theme', false, true);
         $to = post_param('theme', $theme);
         // Can't rename the default theme, so there's no such field for it
         if ($theme == $to) {
             warn_exit(do_lang_tempcode('ALREADY_EXISTS', escape_html($to)));
         }
         require_code('themes3');
         actual_copy_theme($theme, $to);
         $this->save_theme_changes($to);
     } else {
         $title = get_page_title('EDIT_THEME');
         $theme = get_param('old_theme', false, true);
         $to = post_param('theme', $theme);
         // Can't rename the default theme, so there's no such field for it
         if ($theme != $to) {
             require_code('type_validation');
             if (!is_alphanumeric($to)) {
                 $to = preg_replace('#[^\\w\\-\\d]#', '_', $to);
                 //warn_exit(do_lang_tempcode('BAD_CODENAME'));
             }
             if (!file_exists(get_custom_file_base() . '/themes/' . $theme)) {
                 warn_exit(do_lang_tempcode('MISSING_RESOURCE'));
             }
             require_code('themes3');
             actual_rename_theme($theme, $to);
         }
         $this->save_theme_changes($to);
     }
     breadcrumb_set_parents(array(array('_SELF:_SELF:misc', do_lang_tempcode('THEMES'))));
     breadcrumb_set_self(do_lang_tempcode('DONE'));
     return $this->do_next_manager($title, do_lang_tempcode('SUCCESS'), $to, '', 'theme', $to);
 }