function main($id, $mode)
    {
        global $db, $user, $auth, $cache, $template;
        phpbb_gallery::init();
        $user->add_lang(array('mods/gallery_acp', 'mods/gallery'));
        $submit = isset($_POST['submit']) ? true : false;
        $form_key = 'acp_time';
        add_form_key($form_key);
        switch ($mode) {
            case 'main':
                // Disable some Options if they can not be used
                if (!function_exists('exif_read_data')) {
                    $this->display_vars['vars']['exif_data']['type'] = 'custom';
                    $this->display_vars['vars']['exif_data']['explain'] = true;
                    $this->display_vars['vars']['exif_data']['method'] = 'disabled_boolean';
                }
                if (!function_exists('imagerotate')) {
                    $this->display_vars['vars']['allow_rotate_images']['type'] = 'custom';
                    $this->display_vars['vars']['allow_rotate_images']['explain'] = true;
                    $this->display_vars['vars']['allow_rotate_images']['method'] = 'disabled_boolean';
                }
                break;
            default:
                trigger_error('NO_MODE', E_USER_ERROR);
                break;
        }
        phpbb_gallery_config::load(true);
        $this->new_config = phpbb_gallery_config::get_array();
        $cfg_array = isset($_REQUEST['config']) ? utf8_normalize_nfc(request_var('config', array('' => ''), true)) : $this->new_config;
        $error = array();
        // We validate the complete config if whished
        validate_config_vars($this->display_vars['vars'], $cfg_array, $error);
        if ($submit && !check_form_key($form_key)) {
            $error[] = $user->lang['FORM_INVALID'];
        }
        // Do not write values if there is an error
        if (sizeof($error)) {
            $submit = false;
        }
        // We go through the display_vars to make sure no one is trying to set variables he/she is not allowed to...
        foreach ($this->display_vars['vars'] as $config_name => $null) {
            if (!isset($cfg_array[$config_name]) || strpos($config_name, 'legend') !== false) {
                continue;
            }
            $this->new_config[$config_name] = $config_value = $cfg_array[$config_name];
            if ($submit) {
                // Check for RRC-display-options
                if (isset($null['method']) && ($null['method'] == 'rrc_display' || $null['method'] == 'rrc_modes')) {
                    // Changing the value, casted by int to not mess up anything
                    $config_value = (int) array_sum(request_var($config_name, array(0)));
                }
                // Recalculate the Watermark-position
                if (isset($null['method']) && $null['method'] == 'watermark_position') {
                    // Changing the value, casted by int to not mess up anything
                    $config_value = request_var('watermark_position_x', 0) + request_var('watermark_position_y', 0);
                }
                if ($config_name == 'link_thumbnail') {
                    $update_bbcode = request_var('update_bbcode', '');
                    // Update the BBCode
                    if ($update_bbcode) {
                        if (!class_exists('acp_bbcodes')) {
                            phpbb_gallery_url::_include('acp/acp_bbcodes', 'phpbb');
                        }
                        $acp_bbcodes = new acp_bbcodes();
                        $bbcode_match = '[album]{NUMBER}[/album]';
                        $bbcode_tpl = $this->bbcode_tpl($config_value);
                        $sql_ary = $acp_bbcodes->build_regexp($bbcode_match, $bbcode_tpl);
                        $sql_ary = array_merge($sql_ary, array('bbcode_match' => $bbcode_match, 'bbcode_tpl' => $bbcode_tpl, 'display_on_posting' => true, 'bbcode_helpline' => 'GALLERY_HELPLINE_ALBUM'));
                        $sql = 'UPDATE ' . BBCODES_TABLE . '
							SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "\n\t\t\t\t\t\t\tWHERE bbcode_tag = '" . $sql_ary['bbcode_tag'] . "'";
                        $db->sql_query($sql);
                        $cache->destroy('sql', BBCODES_TABLE);
                    }
                }
                phpbb_gallery_config::set($config_name, $config_value);
            }
        }
        if ($submit) {
            $cache->destroy('sql', CONFIG_TABLE);
            trigger_error($user->lang['GALLERY_CONFIG_UPDATED'] . adm_back_link($this->u_action));
        }
        $this->tpl_name = 'acp_board';
        $this->page_title = $this->display_vars['title'];
        $template->assign_vars(array('L_TITLE' => $user->lang[$this->display_vars['title']], 'L_TITLE_EXPLAIN' => $user->lang[$this->display_vars['title'] . '_EXPLAIN'], 'S_ERROR' => sizeof($error) ? true : false, 'ERROR_MSG' => implode('<br />', $error), 'U_ACTION' => $this->u_action));
        // Output relevant page
        foreach ($this->display_vars['vars'] as $config_key => $vars) {
            if (!is_array($vars) && strpos($config_key, 'legend') === false) {
                continue;
            }
            if (strpos($config_key, 'legend') !== false) {
                $template->assign_block_vars('options', array('S_LEGEND' => true, 'LEGEND' => isset($user->lang[$vars]) ? $user->lang[$vars] : $vars));
                continue;
            }
            $this->new_config[$config_key] = phpbb_gallery_config::get($config_key);
            $type = explode(':', $vars['type']);
            $l_explain = '';
            if ($vars['explain']) {
                $l_explain = isset($user->lang[$vars['lang'] . '_EXP']) ? $user->lang[$vars['lang'] . '_EXP'] : '';
            }
            $content = build_cfg_template($type, $config_key, $this->new_config, $config_key, $vars);
            if (empty($content)) {
                continue;
            }
            $template->assign_block_vars('options', array('KEY' => $config_key, 'TITLE' => isset($user->lang[$vars['lang']]) ? $user->lang[$vars['lang']] : $vars['lang'], 'S_EXPLAIN' => $vars['explain'], 'TITLE_EXPLAIN' => $l_explain, 'CONTENT' => $content));
            unset($this->display_vars['vars'][$config_key]);
        }
    }