function bbpress_hl_add_buttons()
{
    global $wp_sh_allowed_str, $wp_sh_setting_opt;
    if (bbp_is_single_forum() || bbp_is_single_topic() || bbp_is_topic_edit() || bbp_is_reply_edit()) {
        if (bbp_is_single_forum()) {
            $textarea_id = "bbp_topic_content";
        } elseif (bbp_is_topic_edit()) {
            $textarea_id = "bbp_topic_content";
        } elseif (bbp_is_reply_edit()) {
            $textarea_id = "bbp_reply_content";
        } elseif (bbp_is_single_topic()) {
            $textarea_id = "bbp_reply_content";
        }
        echo "<div class=\"bbpress_highlight\">";
        if ($wp_sh_setting_opt['bbpress_hl_description_before_enable'] == 1) {
            $wp_sh_bbpress_hl_description_before = wp_sh_valid_text(get_option('wp_sh_bbpress_hl_description_before'), $wp_sh_allowed_str);
            if ($wp_sh_bbpress_hl_description_before == "invalid") {
                $wp_sh_bbpress_hl_description_before = wp_sh_default_setting_value('bbp_desc');
            }
            echo "<p>" . str_replace("<pre>", "&lt;pre&gt;", $wp_sh_bbpress_hl_description_before) . "</p>";
        }
        if ($wp_sh_setting_opt['bbpress_hl_bt_tag'] == "shortcode") {
            $tag = "shorcode";
        } else {
            $tag = "pre";
        }
        echo "<div class=\"bbpress_highlight_button\">";
        if ($wp_sh_setting_opt['lib_version'] == '3.0') {
            $languages = get_option('wp_sh_language3');
        } elseif ($wp_sh_setting_opt['lib_version'] == '2.1') {
            $languages = get_option('wp_sh_language2');
        }
        $gutter = $wp_sh_setting_opt['gutter'];
        if (is_array($languages)) {
            asort($languages);
            foreach ($languages as $key => $val) {
                if ($val[1] == 'true' || $val[1] == 'added') {
                    echo "<a href=\"javascript:void(0);\" onclick=\"surroundHTML('" . $key . "','" . $textarea_id . "','" . $gutter . "','" . $wp_sh_setting_opt['first_line'] . "','" . $tag . "','0');\">" . $val[0] . "</a> | ";
                }
            }
            unset($val);
        }
        echo "</div>";
        echo "</div>";
    }
}
function wp_sh_check_db_ver()
{
    global $wp_sh_db_ver, $wp_sh_setting_opt;
    $current_checkver_stamp = get_option('wp_sh_checkver_stamp');
    if (!$current_checkver_stamp || version_compare($current_checkver_stamp, $wp_sh_db_ver, "!=")) {
        $updated_count = 0;
        $migration_count = 0;
        // Register languages when new installation, updated from ver 1.2.3 or older
        if (!$current_checkver_stamp || version_compare($current_checkver_stamp, "1.3", "<")) {
            wp_sh_language_array();
            $updated_count = $updated_count + 1;
        }
        // Register settings when new installation, updated from ver 1.5 or older
        if (!is_array($wp_sh_setting_opt)) {
            wp_sh_setting_array();
            $updated_count = $updated_count + 1;
        }
        // Setting data migration when updated from ver.1.5 or older
        if (!$current_checkver_stamp || version_compare($current_checkver_stamp, "1.5", "<=")) {
            if (get_option('wp_sh_theme')) {
                include_once 'data-migration.php';
                $migration_count = $updated_count + 1;
                // Delete un-used options before ver.1.5
                include_once 'del-old-options.php';
            }
        }
        // If possible, update to current default values
        if (!$current_checkver_stamp || version_compare($current_checkver_stamp, "1.3.9", "<")) {
            // This option is available since ver 1.0 and updated in ver. 1.3.9
            if (get_option('wp_sh_class_name') == '') {
                update_option('wp_sh_class_name', wp_sh_default_setting_value('class'));
                $updated_count = $updated_count + 1;
            }
        }
        if (version_compare($current_checkver_stamp, "1.3", ">=") && version_compare($current_checkver_stamp, "1.5", "<")) {
            // This option is available since ver. 1.3.5 and updated in ver. 1.5
            if ($wp_sh_setting_opt['addl_style_enable'] == 0) {
                update_option('wp_sh_addl_style', wp_sh_default_setting_value('style'));
                $updated_count = $updated_count + 1;
            }
        }
        if (version_compare($current_checkver_stamp, "1.5", "==")) {
            // This option is available since ver. 1.5 and updated in ver. 1.5.5
            if ($wp_sh_setting_opt['comment_hl_description_before_enable'] == 0) {
                update_option('wp_sh_comment_hl_description_before', wp_sh_default_setting_value('comment_desc'));
                $updated_count = $updated_count + 1;
            }
            if ($wp_sh_setting_opt['comment_hl_stylesheet_enable'] == 0) {
                update_option('wp_sh_comment_hl_stylesheet', wp_sh_default_setting_value('comment_style'));
                $updated_count = $updated_count + 1;
            }
        }
        // Add new setting options when updated from ver.1.5.8 or older
        if ($current_checkver_stamp && version_compare($current_checkver_stamp, "1.5.5", "<=")) {
            if (version_compare(get_bloginfo('version'), "3.0.6", "<=")) {
                $wp_sh_setting_opt['quicktag'] = 0;
            } else {
                $wp_sh_setting_opt['quicktag'] = 1;
            }
            update_option('wp_sh_setting_opt', $wp_sh_setting_opt);
            $updated_count = $updated_count + 1;
        }
        // Add new setting options when updated from ver.1.6 or older
        if ($current_checkver_stamp && version_compare($current_checkver_stamp, "1.6", "<=")) {
            $wp_sh_setting_opt['quicktag_jquery'] = 0;
            // Correct a typo
            if ($wp_sh_setting_opt['theme'] == "Randam") {
                $wp_sh_setting_opt['theme'] = "Random";
            }
            update_option('wp_sh_setting_opt', $wp_sh_setting_opt);
            $updated_count = $updated_count + 1;
        }
        // Add new setting options when updated from ver.1.6.5 or older
        if ($current_checkver_stamp && version_compare($current_checkver_stamp, "1.6.5", "<=")) {
            $wp_sh_setting_opt['quick_code'] = "true";
            update_option('wp_sh_setting_opt', $wp_sh_setting_opt);
            $updated_count = $updated_count + 1;
        }
        // Add new setting options when updated from ver.1.6.7 or older
        if ($current_checkver_stamp && version_compare($current_checkver_stamp, "1.6.7", "<=")) {
            $wp_sh_setting_opt['editor_shorcode'] = 0;
            $wp_sh_setting_opt['editor_no_unfiltered_html'] = 0;
            $wp_sh_setting_opt['comment_hl_bt_tag'] = "pre";
            $wp_sh_setting_opt['comment_hl_bt_shorcode'] = 0;
            if (version_compare(get_bloginfo('version'), "3.2.1", "<=")) {
                $wp_sh_setting_opt['comment_quicktag'] = 0;
            } else {
                $wp_sh_setting_opt['comment_quicktag'] = 1;
            }
            $wp_sh_setting_opt['comment_jquery'] = 0;
            $wp_sh_setting_opt['wiget_tag'] = "pre";
            $wp_sh_setting_opt['wiget_shorcode'] = 0;
            $wp_sh_setting_opt['highlight_bbpress'] = 0;
            $wp_sh_setting_opt['bbpress_hl_bt_enable'] = 0;
            $wp_sh_setting_opt['bbpress_hl_bt_tag'] = "pre";
            $wp_sh_setting_opt['bbpress_hl_bt_shorcode'] = 0;
            $wp_sh_setting_opt['bbpress_hl_bt_guest'] = 0;
            $wp_sh_setting_opt['bbpress_hl_description_before_enable'] = 0;
            $wp_sh_setting_opt['bbpress_hl_stylesheet_enable'] = 0;
            update_option('wp_sh_setting_opt', $wp_sh_setting_opt);
            add_option('wp_sh_bbpress_hl_description_before', wp_sh_default_setting_value('bbp_desc'));
            add_option('wp_sh_bbpress_hl_stylesheet', wp_sh_default_setting_value('bbp_style'));
            $updated_count = $updated_count + 1;
        }
        update_option('wp_sh_checkver_stamp', $wp_sh_db_ver);
        // Stamp for showing messages
        if ($updated_count != 0 && $migration_count == 0) {
            update_option('wp_sh_updated', 'true');
        } elseif ($migration_count != 0) {
            update_option('wp_sh_updated', 'migration');
        }
    }
}
function wp_sh_update_setting()
{
    global $wp_sh_allowed_str, $wp_sh_setting_opt;
    $wp_sh_brushes = get_option('wp_sh_brush_files');
    // Get updated settings
    $wp_sh_code_title = stripslashes($_POST['wp_sh_code_title']);
    $wp_sh_class_name = stripslashes($_POST['wp_sh_class_name']);
    $wp_sh_addl_style = stripslashes($_POST['wp_sh_addl_style']);
    $wp_sh_collapse_lable_text = stripslashes($_POST['wp_sh_collapse_lable_text']);
    if (version_compare(get_bloginfo('version'), "3.0", ">=")) {
        $wp_sh_comment_hl_description_before = stripslashes($_POST['wp_sh_comment_hl_description_before']);
        $wp_sh_comment_hl_stylesheet = stripslashes($_POST['wp_sh_comment_hl_stylesheet']);
    } else {
        $wp_sh_comment_hl_description_before = wp_sh_default_setting_value('comment_desc');
        $wp_sh_comment_hl_stylesheet = wp_sh_default_setting_value('comment_style');
    }
    $wp_sh_bbpress_hl_description_before = stripslashes($_POST['wp_sh_bbpress_hl_description_before']);
    $wp_sh_bbpress_hl_stylesheet = stripslashes($_POST['wp_sh_bbpress_hl_stylesheet']);
    // Transforming before validation
    $wp_sh_addl_style = strip_tags($wp_sh_addl_style);
    $wp_sh_comment_hl_description_before = str_replace(array("\r\n", "\r", "\n"), "<br />", $wp_sh_comment_hl_description_before);
    $wp_sh_comment_hl_stylesheet = strip_tags($wp_sh_comment_hl_stylesheet);
    $wp_sh_bbpress_hl_description_before = str_replace(array("\r\n", "\r", "\n"), "<br />", $wp_sh_bbpress_hl_description_before);
    $wp_sh_bbpress_hl_stylesheet = strip_tags($wp_sh_bbpress_hl_stylesheet);
    // Validate values
    if (!preg_match("/^[0-9]+\$/", $wp_sh_setting_opt['first_line'])) {
        wp_die(__("Invalid value. Settings could not be saved.<br />Your \"Line number(Gutter)\" must be entered in numbers.", 'wp_sh'));
    }
    if (!preg_match("/^[0-9]+\$/", $wp_sh_setting_opt['tab_size'])) {
        wp_die(__("Invalid value. Settings could not be saved.<br />Your \"Tab size\" must be entered in numbers.", 'wp_sh'));
    }
    if (strpos($wp_sh_class_name, "\"") || strpos($wp_sh_class_name, "'")) {
        wp_die(__("Invalid value. Settings could not be saved.<br />Your \"Class attribute\" contains \" or ' that are not allowed to use.", 'wp_sh'));
    }
    if (wp_sh_valid_css($wp_sh_addl_style) == "invalid") {
        wp_die(__('Invalid value. Settings could not be saved.<br />Your "Stylesheet" contains some character strings that are not allowed to use.', 'wp_sh'));
    } else {
        $wp_sh_addl_style = wp_sh_valid_css($wp_sh_addl_style);
    }
    if (wp_sh_valid_text($wp_sh_comment_hl_description_before, $wp_sh_allowed_str) == "invalid") {
        wp_die(__('Invalid value. Settings could not be saved.<br />Your "Description" for "Comment Highlighter Button" contains some character strings that are not allowed to use.', 'wp_sh'));
    } else {
        $wp_sh_comment_hl_description_before = wp_sh_valid_text($wp_sh_comment_hl_description_before, $wp_sh_allowed_str);
    }
    if (wp_sh_valid_css($wp_sh_comment_hl_stylesheet) == "invalid") {
        wp_die(__('Invalid value. Settings could not be saved.<br />Your "Stylesheet" for "Comment Highlighter Button" contains some character strings that are not allowed to use.', 'wp_sh'));
    } else {
        $wp_sh_comment_hl_stylesheet = wp_sh_valid_css($wp_sh_comment_hl_stylesheet);
    }
    if (wp_sh_valid_text($wp_sh_bbpress_hl_description_before, $wp_sh_allowed_str) == "invalid") {
        wp_die(__('Invalid value. Settings could not be saved.<br />Your "Description" for "bbpress Highlighter Button" contains some character strings that are not allowed to use.', 'wp_sh'));
    } else {
        $wp_sh_bbpress_hl_description_before = wp_sh_valid_text($wp_sh_bbpress_hl_description_before, $wp_sh_allowed_str);
    }
    if (wp_sh_valid_css($wp_sh_bbpress_hl_stylesheet) == "invalid") {
        wp_die(__('Invalid value. Settings could not be saved.<br />Your "Stylesheet" for "bbPress Highlighter Button" contains some character strings that are not allowed to use.', 'wp_sh'));
    } else {
        $wp_sh_bbpress_hl_stylesheet = wp_sh_valid_css($wp_sh_bbpress_hl_stylesheet);
    }
    // For backward compatibility
    $wp_sh_version = $_POST['lib_version'];
    if ($_POST['gutter'] == "true") {
        $wp_sh_gutter = 1;
    } else {
        $wp_sh_gutter = 0;
    }
    $wp_sh_first_line = $_POST['first_line'];
    // Update languages
    foreach ($wp_sh_brushes as $lang => $val) {
        $brush_file = $val[0];
        $brush_alias = $val[1];
        $brush_ver = $val[2];
        $wp_sh_brush_files[$lang] = array($brush_file, $brush_alias, $brush_ver, $_POST[$lang]);
    }
    // Restore in DB
    update_option('wp_sh_version', $wp_sh_version);
    // For backward compatibility
    update_option('wp_sh_gutter', $wp_sh_gutter);
    // For backward compatibility
    update_option('wp_sh_first_line', $wp_sh_first_line);
    // For backward compatibility
    update_option('wp_sh_setting_opt', $wp_sh_setting_opt);
    update_option('wp_sh_code_title', $wp_sh_code_title);
    update_option('wp_sh_class_name', $wp_sh_class_name);
    update_option('wp_sh_addl_style', $wp_sh_addl_style);
    update_option('wp_sh_collapse_lable_text', $wp_sh_collapse_lable_text);
    update_option('wp_sh_comment_hl_description_before', $wp_sh_comment_hl_description_before);
    update_option('wp_sh_comment_hl_stylesheet', $wp_sh_comment_hl_stylesheet);
    update_option('wp_sh_bbpress_hl_description_before', $wp_sh_bbpress_hl_description_before);
    update_option('wp_sh_bbpress_hl_stylesheet', $wp_sh_bbpress_hl_stylesheet);
    update_option('wp_sh_brush_files', $wp_sh_brush_files);
    update_option('wp_sh_updated', 'false');
    // Rebuild language list
    wp_sh_rebuild_lang_list();
    // Message for admin
    echo "<div id='setting-error-settings_updated' class='updated fade'><p><strong>" . __("Settings saved.", "wp_sh") . "</strong></p></div>";
}