function cpm_action_update_config()
{
    global $cpm_config, $wpmu_version;
    $cpm_config->is_cpm_managing_posts = true;
    $do_write = false;
    $use_default_file = false;
    if ($cpm_config->config_method == "comicpress-config.php") {
        $do_write = !isset($_POST['just-show-config']);
    } else {
        $use_default_file = true;
    }
    include realpath(dirname(__FILE__)) . '/../cp_configuration_options.php';
    $original_properties = $cpm_config->properties;
    foreach ($comicpress_configuration_options as $field_info) {
        extract($field_info);
        $config_id = isset($field_info['variable_name']) ? $field_info['variable_name'] : $field_info['id'];
        switch ($type) {
            case "folder":
                $cpm_config->properties[$config_id] = $_POST[$_POST["folder-{$config_id}"] . "-" . $config_id];
                break;
            default:
                if (isset($_POST[$config_id])) {
                    $cpm_config->properties[$config_id] = $_POST[$config_id];
                }
                break;
        }
    }
    if ($wpmu_version) {
        cpm_wpmu_save_options();
        $cpm_config->is_wp_options = true;
    }
    if (!$cpm_config->is_wp_options) {
        if (!$do_write) {
            $file_output = write_comicpress_config_functions_php($cpm_config->config_filepath, true, $use_default_file);
            $cpm_config->properties = $original_properties;
            if ($use_default_file) {
                $cpm_config->messages[] = __("<strong>No comicpress-config.php file was found in your theme folder.</strong> Using default configuration file.", 'comicpress-manager');
            }
            $cpm_config->messages[] = __("<strong>Your configuration:</strong>", 'comicpress-manager') . "<pre class=\"code-block\">" . htmlentities($file_output) . "</pre>";
        } else {
            if (!is_null($cpm_config->config_filepath)) {
                if (is_array($file_output = write_comicpress_config_functions_php($cpm_config->config_filepath))) {
                    $cpm_config->config_method = read_current_theme_comicpress_config();
                    $cpm_config->path = get_comic_folder_path();
                    $cpm_config->plugin_path = PLUGINDIR . '/' . plugin_basename(__FILE__);
                    cpm_read_information_and_check_config();
                    $backup_file = pathinfo($file_output[0], PATHINFO_BASENAME);
                    $cpm_config->messages[] = sprintf(__("<strong>Configuration updated and original config backed up to %s.</strong> Rename this file to comicpress-config.php if you are having problems.", 'comicpress-manager'), $backup_file);
                } else {
                    $relative_path = substr(realpath($cpm_config->config_filepath), CPM_STRLEN_REALPATH_DOCUMENT_ROOT);
                    $cpm_config->warnings[] = sprintf(__("<strong>Configuration not updated</strong>, check the permissions of %s and the theme folder.  They should be writable by the Webserver process. Alternatively, copy and paste the following code into your comicpress-config.php file:", 'comicpress-manager'), $relative_path) . "<pre class=\"code-block\">" . htmlentities($file_output) . "</pre>";
                    $cpm_config->properties = $original_properties;
                }
            }
        }
    } else {
        cpm_read_information_and_check_config();
        $cpm_config->messages[] = sprintf(__("<strong>Configuration updated in database.</strong>", 'comicpress-manager'), $backup_file);
    }
}
function cpm_action_skip_first_run()
{
    global $cpm_config;
    $cpm_config->messages[] = __("<strong>No directories were created.</strong> You'll need to create directories on your own.", 'comicpress-manager');
    $cpm_config->did_first_run = true;
    cpm_read_information_and_check_config();
}
function cpm_action_update_cpm_config()
{
    global $cpm_config;
    include realpath(dirname(__FILE__)) . '/../cpm_configuration_options.php';
    $all_valid = true;
    $target_update_options = array();
    foreach ($configuration_options as $option_info) {
        $target_key = 'comicpress-manager-' . $option_info['id'];
        switch ($option_info['type']) {
            case "text":
            case "textarea":
            case "dropdown":
                if (isset($_POST[$option_info['id']])) {
                    $validate_function_name = "cpm_validate_cpm_option_" . str_replace("-", "_", $option_info['id']);
                    $ok = true;
                    if (function_exists($validate_function_name)) {
                        $ok = call_user_func($validate_function_name, stripslashes($_POST[$option_info['id']]));
                    }
                    if ($ok) {
                        $target_update_options[$target_key] = stripslashes($_POST[$option_info['id']]);
                    } else {
                        $target_update_options[$target_key] = $option_info['default'];
                        update_option($target_key, $option_info['default']);
                        $all_valid = false;
                        break 2;
                    }
                }
                break;
            case "checkbox":
                $target_update_options[$target_key] = isset($_POST[$option_info['id']]) ? "1" : "0";
                break;
            case "categories":
                if (isset($_POST[$option_info['id']])) {
                    $all_categories = implode(",", $_POST[$option_info['id']]);
                    $target_update_options[$target_key] = $all_categories;
                } else {
                    $target_update_options[$target_key] = "";
                }
                break;
        }
    }
    if ($all_valid) {
        cpm_read_information_and_check_config();
        foreach ($target_update_options as $option => $value) {
            update_option($option, $value);
        }
        $cpm_config->messages[] = __("<strong>ComicPress Manager configuration updated.</strong>", 'comicpress-manager');
    } else {
        $cpm_config->warnings[] = __("<strong>You entered invalid data into your configuration.</strong> Configuration  not updated.", 'comicpress-manager');
    }
}
function cpm_action_restore_backup()
{
    global $cpm_config;
    $config_dirname = dirname($cpm_config->config_filepath);
    if (is_numeric($_POST['backup-file-time'])) {
        if (file_exists($config_dirname . '/comicpress-config.php.' . $_POST['backup-file-time'])) {
            if ($cpm_config->can_write_config) {
                if (@copy($config_dirname . '/comicpress-config.php.' . $_POST['backup-file-time'], $config_dirname . '/comicpress-config.php') !== false) {
                    cpm_read_information_and_check_config();
                    $cpm_config->messages[] = sprintf(__("<strong>Restored %s</strong>.  Check to make sure your site is functioning correctly.", 'comicpress-manager'), 'comicpress-config.php.' . $_POST['backup-file-time']);
                } else {
                    $cpm_config->warnings[] = sprintf(__("<strong>Could not restore %s</strong>.  Check the permissions of your theme folder and try again.", 'comicpress-manager'), 'comicpress-config.php.' . $_POST['backup-file-time']);
                }
            }
        }
    }
}
function cpm_action_do_first_run()
{
    global $cpm_config, $blog_id, $wpmu_version;
    $dir_list = array(CPM_DOCUMENT_ROOT, CPM_DOCUMENT_ROOT . '/comics', CPM_DOCUMENT_ROOT . '/comics-rss', CPM_DOCUMENT_ROOT . '/comics-archive', CPM_DOCUMENT_ROOT . '/comics-mini');
    $is_wpmu = $wpmu_version;
    if ($is_wpmu) {
        $dir_list = cpm_wpmu_first_run_dir_list();
    }
    $any_made = false;
    $all_made = true;
    foreach ($dir_list as $dir_to_make) {
        if (!file_exists($dir_to_make)) {
            $any_made = true;
            if (@mkdir($dir_to_make)) {
                if (!$is_wpmu) {
                    $cpm_config->messages[] = sprintf(__("<strong>Directory created:</strong> %s", 'comicpress-manager'), $dir_to_make);
                }
            } else {
                $all_made = false;
                if (!$is_wpmu) {
                    $cpm_config->warnings[] = sprintf(__("<strong>Unable to create directory:</strong> %s", 'comicpress-manager'), $dir_to_make);
                }
            }
        }
    }
    if (!$any_made) {
        $cpm_config->messages[] = __("<strong>All the directories were already found, nothing to do!</strong>", "comicpress-manager");
    }
    if ($is_wpmu) {
        if ($all_made) {
            $cpm_config->messages[] = sprintf(__("<strong>All directories created!</strong>", 'comicpress-manager'), $dir_to_make);
            cpm_wpmu_complete_first_run();
        } else {
            $cpm_config->warnings[] = sprintf(__("<strong>Unable to create directories!</strong> Contact your administrator.", 'comicpress-manager'), $dir_to_make);
        }
        update_option("comicpress-manager-cpm-did-first-run", 1);
    }
    $cpm_config->did_first_run = true;
    cpm_read_information_and_check_config();
}
function cpm_action_manage_subcomic()
{
    global $cpm_config;
    $target_category_id = (int) $_POST['comic'];
    extract(cpm_normalize_storyline_structure());
    $final_category_id = false;
    $first = true;
    foreach ($category_tree as $node) {
        $category = get_category(end(explode("/", $node)));
        if ($first) {
            $final_category_id = $category->term_id;
        }
        if ($target_category_id == $category->term_id) {
            if (is_dir(CPM_DOCUMENT_ROOT . '/' . $cpm_config->properties['comic_folder'] . '/' . $category->slug)) {
                $final_category_id = $category->term_id;
                break;
            }
        }
        $first = false;
    }
    update_option('comicpress-manager-manage-subcomic', $final_category_id);
    cpm_read_information_and_check_config();
    $cpm_config->messages[] = sprintf(__("Now managing <strong>%s</strong>.", 'comicpress-manager'), get_cat_name($final_category_id));
}
/**
 * If a category is added, deleted, or edited, and it's not done through
 * the Storyline Structure page, normalize the Storyline Structure
 * so that it includes/removes the affected categories.
 */
function cpm_rebuild_storyline_structure($term_id)
{
    global $cpm_config;
    if (empty($cpm_config->is_cpm_modifying_categories)) {
        cpm_read_information_and_check_config();
        cpm_normalize_storyline_structure();
    }
}
<?php

global $blog_id;
if (!function_exists('add_action')) {
    require_once "../../../wp-config.php";
}
if (WP_ADMIN) {
    require_once 'comicpress_manager_config.php';
    require_once 'comicpress_manager_library.php';
    cpm_get_cpm_document_root();
    $cpm_config = new ComicPressConfig();
    cpm_read_information_and_check_config();
    if (isset($_REQUEST['blog_id']) && function_exists('switch_to_blog')) {
        switch_to_blog((int) $_REQUEST['blog_id']);
    }
    // TODO: handle different comic categories differently, this is still too geared
    // toward one blog/one comic...
    $all_post_dates = array();
    $format = CPM_DATE_FORMAT;
    if (isset($_POST['format'])) {
        $format = $_POST['format'];
    }
    foreach (cpm_query_posts() as $comic_post) {
        $all_post_dates[] = date($format, strtotime($comic_post->post_date));
    }
    $all_post_dates = array_unique($all_post_dates);
    ob_start();
    $missing_comic_count = 0;
    foreach (cpm_read_comics_folder() as $comic_file) {
        $comic_file = pathinfo($comic_file, PATHINFO_BASENAME);
        if (($result = cpm_breakdown_comic_filename($comic_file, $format)) !== false) {