/**
     * Export style or style elements
     */
    function export($mode, $style_id)
    {
        global $db, $template, $user, $phpbb_root_path, $cache, $phpEx, $config;
        $update = isset($_POST['update']) ? true : false;
        $inc_template = request_var('inc_template', 0);
        $inc_theme = request_var('inc_theme', 0);
        $inc_imageset = request_var('inc_imageset', 0);
        $store = request_var('store', 0);
        $format = request_var('format', '');
        $error = array();
        $methods = array('tar');
        $available_methods = array('tar.gz' => 'zlib', 'tar.bz2' => 'bz2', 'zip' => 'zlib');
        foreach ($available_methods as $type => $module) {
            if (!@extension_loaded($module)) {
                continue;
            }
            $methods[] = $type;
        }
        if (!in_array($format, $methods)) {
            $format = 'tar';
        }
        switch ($mode) {
            case 'style':
                if ($update && $inc_template + $inc_theme + $inc_imageset < 1) {
                    $error[] = $user->lang['STYLE_ERR_MORE_ELEMENTS'];
                }
                $name = 'style_name';
                $sql_select = 's.style_id, s.style_name, s.style_copyright';
                $sql_select .= $inc_template ? ', t.*' : ', t.template_name';
                $sql_select .= $inc_theme ? ', c.*' : ', c.theme_name';
                $sql_select .= $inc_imageset ? ', i.*' : ', i.imageset_name';
                $sql_from = STYLES_TABLE . ' s, ' . STYLES_TEMPLATE_TABLE . ' t, ' . STYLES_THEME_TABLE . ' c, ' . STYLES_IMAGESET_TABLE . ' i';
                $sql_where = "s.style_id = {$style_id} AND t.template_id = s.template_id AND c.theme_id = s.theme_id AND i.imageset_id = s.imageset_id";
                $l_prefix = 'STYLE';
                break;
            case 'template':
                $name = 'template_name';
                $sql_select = '*';
                $sql_from = STYLES_TEMPLATE_TABLE;
                $sql_where = "template_id = {$style_id}";
                $l_prefix = 'TEMPLATE';
                break;
            case 'theme':
                $name = 'theme_name';
                $sql_select = '*';
                $sql_from = STYLES_THEME_TABLE;
                $sql_where = "theme_id = {$style_id}";
                $l_prefix = 'THEME';
                break;
            case 'imageset':
                $name = 'imageset_name';
                $sql_select = '*';
                $sql_from = STYLES_IMAGESET_TABLE;
                $sql_where = "imageset_id = {$style_id}";
                $l_prefix = 'IMAGESET';
                break;
        }
        if ($update && !sizeof($error)) {
            $sql = "SELECT {$sql_select}\n\t\t\t\tFROM {$sql_from}\n\t\t\t\tWHERE {$sql_where}";
            $result = $db->sql_query($sql);
            $style_row = $db->sql_fetchrow($result);
            $db->sql_freeresult($result);
            if (!$style_row) {
                trigger_error($user->lang['NO_' . $l_prefix] . adm_back_link($this->u_action), E_USER_WARNING);
            }
            $var_ary = array('style_id', 'style_name', 'style_copyright', 'template_id', 'template_name', 'template_path', 'template_copyright', 'template_storedb', 'template_inherits_id', 'bbcode_bitfield', 'theme_id', 'theme_name', 'theme_path', 'theme_copyright', 'theme_storedb', 'theme_mtime', 'theme_data', 'imageset_id', 'imageset_name', 'imageset_path', 'imageset_copyright');
            foreach ($var_ary as $var) {
                if (!isset($style_row[$var])) {
                    $style_row[$var] = '';
                }
            }
            $files = $data = array();
            if ($mode == 'style') {
                $style_cfg = str_replace(array('{MODE}', '{NAME}', '{COPYRIGHT}', '{VERSION}'), array($mode, $style_row['style_name'], $style_row['style_copyright'], $config['version']), $this->style_cfg);
                $style_cfg .= !$inc_template ? "\nrequired_template = {$style_row['template_name']}" : '';
                $style_cfg .= !$inc_theme ? "\nrequired_theme = {$style_row['theme_name']}" : '';
                $style_cfg .= !$inc_imageset ? "\nrequired_imageset = {$style_row['imageset_name']}" : '';
                $data[] = array('src' => $style_cfg, 'prefix' => 'style.cfg');
                unset($style_cfg);
            }
            // Export template core code
            if ($mode == 'template' || $inc_template) {
                $template_cfg = str_replace(array('{MODE}', '{NAME}', '{COPYRIGHT}', '{VERSION}'), array($mode, $style_row['template_name'], $style_row['template_copyright'], $config['version']), $this->template_cfg);
                $use_template_name = '';
                // Add the inherit from variable, depending on it's use...
                if ($style_row['template_inherits_id']) {
                    // Get the template name
                    $sql = 'SELECT template_name
						FROM ' . STYLES_TEMPLATE_TABLE . '
						WHERE template_id = ' . (int) $style_row['template_inherits_id'];
                    $result = $db->sql_query($sql);
                    $use_template_name = (string) $db->sql_fetchfield('template_name');
                    $db->sql_freeresult($result);
                }
                $template_cfg .= $use_template_name ? "\ninherit_from = {$use_template_name}" : "\n#inherit_from = ";
                $template_cfg .= "\n\nbbcode_bitfield = {$style_row['bbcode_bitfield']}";
                $data[] = array('src' => $template_cfg, 'prefix' => 'template/template.cfg');
                // This is potentially nasty memory-wise ...
                if (!$style_row['template_storedb']) {
                    $files[] = array('src' => "styles/{$style_row['template_path']}/template/", 'prefix-' => "styles/{$style_row['template_path']}/", 'prefix+' => false, 'exclude' => 'template.cfg');
                } else {
                    $sql = 'SELECT template_filename, template_data
						FROM ' . STYLES_TEMPLATE_DATA_TABLE . "\n\t\t\t\t\t\tWHERE template_id = {$style_row['template_id']}";
                    $result = $db->sql_query($sql);
                    while ($row = $db->sql_fetchrow($result)) {
                        $data[] = array('src' => $row['template_data'], 'prefix' => 'template/' . $row['template_filename']);
                    }
                    $db->sql_freeresult($result);
                }
                unset($template_cfg);
            }
            // Export theme core code
            if ($mode == 'theme' || $inc_theme) {
                $theme_cfg = str_replace(array('{MODE}', '{NAME}', '{COPYRIGHT}', '{VERSION}'), array($mode, $style_row['theme_name'], $style_row['theme_copyright'], $config['version']), $this->theme_cfg);
                // Read old cfg file
                $items = $cache->obtain_cfg_items($style_row);
                $items = $items['theme'];
                if (!isset($items['parse_css_file'])) {
                    $items['parse_css_file'] = 'off';
                }
                $theme_cfg = str_replace(array('{PARSE_CSS_FILE}'), array($items['parse_css_file']), $theme_cfg);
                $files[] = array('src' => "styles/{$style_row['theme_path']}/theme/", 'prefix-' => "styles/{$style_row['theme_path']}/", 'prefix+' => false, 'exclude' => $style_row['theme_storedb'] ? 'stylesheet.css,theme.cfg' : 'theme.cfg');
                $data[] = array('src' => $theme_cfg, 'prefix' => 'theme/theme.cfg');
                if ($style_row['theme_storedb']) {
                    $data[] = array('src' => $style_row['theme_data'], 'prefix' => 'theme/stylesheet.css');
                }
                unset($items, $theme_cfg);
            }
            // Export imageset core code
            if ($mode == 'imageset' || $inc_imageset) {
                $imageset_cfg = str_replace(array('{MODE}', '{NAME}', '{COPYRIGHT}', '{VERSION}'), array($mode, $style_row['imageset_name'], $style_row['imageset_copyright'], $config['version']), $this->imageset_cfg);
                $imageset_main = array();
                $sql = 'SELECT image_filename, image_name, image_height, image_width
					FROM ' . STYLES_IMAGESET_DATA_TABLE . "\n\t\t\t\t\tWHERE imageset_id = {$style_id}\n\t\t\t\t\t\tAND image_lang = ''";
                $result = $db->sql_query($sql);
                while ($row = $db->sql_fetchrow($result)) {
                    $imageset_main[$row['image_name']] = $row['image_filename'] . ($row['image_height'] ? '*' . $row['image_height'] : '') . ($row['image_width'] ? '*' . $row['image_width'] : '');
                }
                $db->sql_freeresult($result);
                foreach ($this->imageset_keys as $topic => $key_array) {
                    foreach ($key_array as $key) {
                        if (isset($imageset_main[$key])) {
                            $imageset_cfg .= "\nimg_" . $key . ' = ' . str_replace("styles/{$style_row['imageset_path']}/imageset/", '{PATH}', $imageset_main[$key]);
                        }
                    }
                }
                $files[] = array('src' => "styles/{$style_row['imageset_path']}/imageset/", 'prefix-' => "styles/{$style_row['imageset_path']}/", 'prefix+' => false, 'exclude' => 'imageset.cfg');
                $data[] = array('src' => trim($imageset_cfg), 'prefix' => 'imageset/imageset.cfg');
                end($data);
                $imageset_root = "{$phpbb_root_path}styles/{$style_row['imageset_path']}/imageset/";
                if ($dh = @opendir($imageset_root)) {
                    while (($fname = readdir($dh)) !== false) {
                        if ($fname[0] != '.' && $fname != 'CVS' && is_dir("{$imageset_root}{$fname}")) {
                            $files[key($files)]['exclude'] .= ',' . $fname . '/imageset.cfg';
                        }
                    }
                    closedir($dh);
                }
                $imageset_lang = array();
                $sql = 'SELECT image_filename, image_name, image_height, image_width, image_lang
					FROM ' . STYLES_IMAGESET_DATA_TABLE . "\n\t\t\t\t\tWHERE imageset_id = {$style_id}\n\t\t\t\t\t\tAND image_lang <> ''";
                $result = $db->sql_query($sql);
                while ($row = $db->sql_fetchrow($result)) {
                    $imageset_lang[$row['image_lang']][$row['image_name']] = $row['image_filename'] . ($row['image_height'] ? '*' . $row['image_height'] : '') . ($row['image_width'] ? '*' . $row['image_width'] : '');
                }
                $db->sql_freeresult($result);
                foreach ($imageset_lang as $lang => $imageset_localized) {
                    $imageset_cfg = str_replace(array('{MODE}', '{NAME}', '{COPYRIGHT}', '{VERSION}'), array($mode, $style_row['imageset_name'], $style_row['imageset_copyright'], $config['version']), $this->imageset_cfg);
                    foreach ($this->imageset_keys as $topic => $key_array) {
                        foreach ($key_array as $key) {
                            if (isset($imageset_localized[$key])) {
                                $imageset_cfg .= "\nimg_" . $key . ' = ' . str_replace("styles/{$style_row['imageset_path']}/imageset/", '{PATH}', $imageset_localized[$key]);
                            }
                        }
                    }
                    $data[] = array('src' => trim($imageset_cfg), 'prefix' => 'imageset/' . $lang . '/imageset.cfg');
                }
                unset($imageset_cfg);
            }
            switch ($format) {
                case 'tar':
                    $ext = '.tar';
                    $mimetype = 'x-tar';
                    $compress = 'compress_tar';
                    break;
                case 'zip':
                    $ext = '.zip';
                    $mimetype = 'zip';
                    break;
                case 'tar.gz':
                    $ext = '.tar.gz';
                    $mimetype = 'x-gzip';
                    break;
                case 'tar.bz2':
                    $ext = '.tar.bz2';
                    $mimetype = 'x-bzip2';
                    break;
                default:
                    $error[] = $user->lang[$l_prefix . '_ERR_ARCHIVE'];
            }
            if (!sizeof($error)) {
                include $phpbb_root_path . 'includes/functions_compress.' . $phpEx;
                if ($mode == 'style') {
                    $path = preg_replace('#[^\\w-]+#', '_', $style_row['style_name']);
                } else {
                    $path = $style_row[$mode . '_path'];
                }
                if ($format == 'zip') {
                    $compress = new compress_zip('w', $phpbb_root_path . "store/{$path}{$ext}");
                } else {
                    $compress = new compress_tar('w', $phpbb_root_path . "store/{$path}{$ext}", $ext);
                }
                if (sizeof($files)) {
                    foreach ($files as $file_ary) {
                        $compress->add_file($file_ary['src'], $file_ary['prefix-'], $file_ary['prefix+'], $file_ary['exclude']);
                    }
                }
                if (sizeof($data)) {
                    foreach ($data as $data_ary) {
                        $compress->add_data($data_ary['src'], $data_ary['prefix']);
                    }
                }
                $compress->close();
                add_log('admin', 'LOG_' . $l_prefix . '_EXPORT', $style_row[$mode . '_name']);
                if (!$store) {
                    $compress->download($path);
                    @unlink("{$phpbb_root_path}store/{$path}{$ext}");
                    exit;
                }
                trigger_error(sprintf($user->lang[$l_prefix . '_EXPORTED'], "store/{$path}{$ext}") . adm_back_link($this->u_action));
            }
        }
        $sql = "SELECT {$mode}_id, {$mode}_name\n\t\t\tFROM " . ($mode == 'style' ? STYLES_TABLE : $sql_from) . "\n\t\t\tWHERE {$mode}_id = {$style_id}";
        $result = $db->sql_query($sql);
        $style_row = $db->sql_fetchrow($result);
        $db->sql_freeresult($result);
        if (!$style_row) {
            trigger_error($user->lang['NO_' . $l_prefix] . adm_back_link($this->u_action), E_USER_WARNING);
        }
        $this->page_title = $l_prefix . '_EXPORT';
        $format_buttons = '';
        foreach ($methods as $method) {
            $format_buttons .= '<label><input type="radio"' . (!$format_buttons ? ' id="format"' : '') . ' class="radio" value="' . $method . '" name="format"' . ($method == $format ? ' checked="checked"' : '') . ' /> ' . $method . '</label>';
        }
        $template->assign_vars(array('S_EXPORT' => true, 'S_ERROR_MSG' => sizeof($error) ? true : false, 'S_STYLE' => $mode == 'style' ? true : false, 'L_TITLE' => $user->lang[$this->page_title], 'L_EXPLAIN' => $user->lang[$this->page_title . '_EXPLAIN'], 'L_NAME' => $user->lang[$l_prefix . '_NAME'], 'U_ACTION' => $this->u_action . '&amp;action=export&amp;id=' . $style_id, 'U_BACK' => $this->u_action, 'ERROR_MSG' => sizeof($error) ? implode('<br />', $error) : '', 'NAME' => $style_row[$mode . '_name'], 'FORMAT_BUTTONS' => $format_buttons));
    }