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);
    }
}
/**
 * Show the comic in the Post editor.
 */
function cpm_show_comic()
{
    global $post, $cpm_config;
    $form_target = plugin_basename(realpath(dirname(__FILE__) . "/../comicpress_manager_admin.php"));
    read_current_theme_comicpress_config();
    $has_comic_file = false;
    $in_comics_category = false;
    $thumbnails_to_generate = cpm_get_thumbnails_to_generate();
    $post_categories = array();
    $comic_categories = array();
    extract(cpm_normalize_storyline_structure());
    foreach ($category_tree as $node) {
        $comic_categories[] = end(explode("/", $node));
    }
    if ($post->ID !== 0) {
        $post_time = time();
        foreach (array('post_date', 'post_modified', 'post_date_gmt', 'post_modified_gmt') as $time_value) {
            if (($result = strtotime($post->{$time_value})) !== false) {
                $post_time = $result;
                break;
            }
        }
        $post_categories = wp_get_post_categories($post->ID);
        if (isset($cpm_config->properties['comiccat'])) {
            $in_comics_category = count(array_intersect($comic_categories, $post_categories)) > 0;
        }
        $ok = true;
        if (cpm_get_subcomic_directory() !== false) {
            $ok = in_array(get_option('comicpress-manager-manage-subcomic'), wp_get_post_categories($post->ID));
        }
        if ($ok) {
            if (($comic = find_comic_by_date($post_time)) !== false) {
                $comic_uri = cpm_build_comic_uri($comic);
                $comic_filename = preg_replace('#^.*/([^\\/]*)$#', '\\1', $comic_uri);
                $link = "<strong><a target=\"comic_window\" href=\"{$comic_uri}\">{$comic_filename}</a></strong>";
                $date_root = substr($comic_filename, 0, strlen(date(CPM_DATE_FORMAT)));
                $thumbnails_found = cpm_find_thumbnails_by_filename($comic);
                $icon_file_to_use = $comic;
                foreach (array('rss', 'archive') as $type) {
                    if (isset($thumbnails_found[$type])) {
                        $icon_file_to_use = $thumbnails_found[$type];
                    }
                }
                $icon_uri = cpm_build_comic_uri($icon_file_to_use);
                $has_comic_file = true;
            }
        }
    }
    ?>

  <script type="text/javascript">
    function show_comic() {
      if ($('comic-icon').offsetWidth > $('comic-icon').offsetHeight) {
        $('preview-comic').width = 400;
      } else {
        $('preview-comic').height = 400;
      }
      Element.clonePosition('comic-hover', 'comic-icon', { setWidth: false, setHeight: false, offsetTop: -((Element.getDimensions('comic-hover')['height'] - Element.getDimensions('comic-icon')['height'])/2) });
      $('comic-hover').show();
    }

    function hide_comic() { $('comic-hover').hide(); }

    var all_comic_categories = [ <?php 
    echo implode(",", $comic_categories);
    ?>
 ];
    var storyline_enabled = <?php 
    echo get_option('comicpress-enable-storyline-support') == 1 ? 'true' : 'false';
    ?>
;

    Event.observe(window, 'load', function() {
      $('post').encoding = "multipart/form-data";
    });
  </script>

  <?php 
    if (count($cpm_config->comic_files) == 0) {
        ?>
    <div style="border: solid #daa 1px; background-color: #ffe7e7; padding: 5px">
      <strong>It looks like this is a new ComicPress install.</strong> You should test to make
      sure uploading works correctly by visiting <a href="admin.php?page=<?php 
        echo plugin_basename(realpath(dirname(__FILE__) . '/../comicpress_manager_admin.php'));
        ?>
">ComicPress -> Upload</a>.
    </div>
  <?php 
    }
    ?>
  <?php 
    if ($has_comic_file) {
        ?>
    <div id="comic-hover" style="border: solid black 1px; position: absolute; display: none" onmouseout="hide_comic()">
      <img id="preview-comic" src="<?php 
        echo $comic_uri;
        ?>
" />
    </div>
    <a href="#" onclick="return false" onmouseover="show_comic()"><img id="comic-icon" src="<?php 
        echo $icon_uri;
        ?>
" height="100" align="right" /></a>
    <p>
      <?php 
        printf(__("The comic that will be shown with this post is %s.", 'comicpress-manager'), $link);
        ?>
      <?php 
        _e("Mouse over the icon to the right to see a larger version of the image.", 'comicpress-manager');
        ?>
    </p>

    <?php 
        if (cpm_get_subcomic_directory() !== false) {
            printf(__("Comic files will be uploaded to the <strong>%s</strong> comic subdirectory.", 'comicpress-manager'), get_cat_name(get_option('comicpress-manager-manage-subcomic')));
        }
        ?>

    <?php 
        if (count($thumbnails_found) > 0) {
            ?>
      <p><?php 
            _e("The following thumbnails for this comic were also found:", 'comicpress-manager');
            ?>
        <?php 
            foreach ($thumbnails_found as $type => $file) {
                ?>
          <a target="comic_window" href="<?php 
                echo cpm_build_comic_uri(CPM_DOCUMENT_ROOT . '/' . $file);
                ?>
"><?php 
                echo $type;
                ?>
</a>
        <?php 
            }
            ?>
      </p>
    <?php 
        }
        ?>
  <?php 
    }
    ?>

  <?php 
    if (cpm_option("cpm-edit-post-integrate") == 1) {
        ?>
    <p><em><strong>ComicPress Manager Edit Post file management is enabled.</strong></em> Any changes to post date, or deleting this post, will affect any associated comic files.</p>
  <?php 
    }
    ?>

  <p><strong>NOTE: Upload errors will not be reported.</strong> If you are having trouble uploading files, use the <a href="admin.php?page=<?php 
    echo plugin_basename(realpath(dirname(__FILE__) . '/../comicpress_manager_admin.php'));
    ?>
">ComicPress -> Upload</a> screen.</p>

  <table class="form-table">
    <tr>
      <th scope="row">
        <?php 
    if ($has_comic_file) {
        ?>
          <?php 
        _e("Replace This Image", 'comicpress-manager');
        ?>
        <?php 
    } else {
        ?>
          <?php 
        _e("Upload a New Single Image", 'comicpress-manager');
        ?>
        <?php 
    }
    ?>
      </th>
      <td>
        <input type="hidden" name="MAX_FILE_SIZE" value="<?php 
    echo cpm_short_size_string_to_bytes(ini_get('upload_max_filesize'));
    ?>
" />
        <input type="file" id="comicpress-replace-image" name="comicpress-replace-image" class="button" /> <?php 
    echo empty($thumbnails_to_generate) ? "" : __("<em>(thumbnails will be generated)</em>", 'comicpress-manager');
    ?>
<br />
        <?php 
    if ($has_comic_file) {
        ?>
          <input type="hidden" name="overwrite-existing-file-choice" value="<?php 
        echo $comic_filename;
        ?>
" />
        <?php 
    }
    ?>
        <input type="hidden" name="upload-destination" value="comic" />
        <input type="hidden" name="thumbnails" value="yes" />
      </td>
      <script type="text/javascript">
        var handle_click = function() {
          var any_checked = false
          $$('input[name="post_category[]"]').each(function(i) {
            if (i.type == "radio") {
              if (i.checked) {
                any_checked = true;
              }
            }
          });

          if (!any_checked) {
            var has_checked = false;
            $$('input[name="post_category[]"]').each(function(i) {
              if (!has_checked) {
                if (i.type == "radio") {
                  i.checked = true;
                  has_checked = true;
                }
              }
            });
          }
        };

        [ 'click', 'blur' ].each(function(w) { $('comicpress-replace-image').observe(w, handle_click); });
      </script>
    </tr>
    <?php 
    if (cpm_option('cpm-skip-checks') != 1) {
        if (!function_exists('get_comic_path')) {
            ?>
          <tr>
            <td colspan="2" style="background-color: #fee; border: solid #daa 1px">
              <?php 
            _e('<strong>It looks like you\'re running an older version of ComicPress.</strong> Storyline, hovertext, and transcript are fully supported in <a href="http://comicpress.org/">ComicPress 2.7</a>. You can use hovertext and transcripts in earlier themes by using <tt>get_post_meta($post->ID, "hovertext", true)</tt> and <tt>get_post_meta($post->ID, "transcript", true)</tt>.', 'comicpress-manager');
            ?>
            </td>
          </tr>
        <?php 
        }
    }
    ?>
    <?php 
    if (get_option('comicpress-enable-storyline-support') == 1) {
        ?>
      <tr>
        <th scope="row">
          <?php 
        if (count($category_tree) > 1) {
            _e("Storyline", 'comicpress-manager');
        } else {
            _e("Category", 'comicpress-manager');
        }
        ?>
        </th>
        <td>
          <?php 
        cpm_display_storyline_checkboxes($category_tree, $post_categories, null, "post_category");
        ?>
        </td>
      </tr>
    <?php 
    }
    ?>
    <tr>
      <th scope="row"><?php 
    _e('&lt;img title&gt;/hover text', 'comicpress-manager');
    ?>
</th>
      <td><input type="text" name="comicpress-img-title" style="width:99%" value="<?php 
    echo get_post_meta($post->ID, 'hovertext', true);
    ?>
" /></td>
    </tr>
    <tr>
      <th scope="row"><?php 
    _e("Transcript", 'comicpress-manager');
    ?>
</th>
      <td>
        <textarea name="comicpress-transcript" rows="8" style="width:99%"><?php 
    echo get_post_meta($post->ID, 'transcript', true);
    ?>
</textarea>
        <?php 
    if (!is_plugin_active('what-did-they-say/what-did-they-say.php')) {
        ?>
          <p>
            <?php 
        _e('Want even better control over your transcripts? Try <a href="http://wordpress.org/extend/plugins/what-did-they-say/" target="wdts">What Did They Say?!?</a>', 'comicpress-manager');
        ?>
          </p>
        <?php 
    }
    ?>
      </td>
    </tr>
  </table>
  
  <?php 
}
/**
 * Read information about the current installation.
 */
function cpm_read_information_and_check_config()
{
    global $cpm_config, $cpm_attempted_document_roots, $blog_id, $wpmu_version;
    $cpm_config->config_method = read_current_theme_comicpress_config();
    $cpm_config->config_filepath = get_functions_php_filepath();
    $cpm_config->can_write_config = can_write_comicpress_config($cpm_config->config_filepath);
    $cpm_config->path = get_comic_folder_path();
    $cpm_config->plugin_path = PLUGINDIR . '/' . plugin_basename(__FILE__);
    foreach (array_keys($cpm_config->separate_thumbs_folder_defined) as $type) {
        $cpm_config->separate_thumbs_folder_defined[$type] = $cpm_config->properties['comic_folder'] != $cpm_config->properties[$type . '_comic_folder'];
    }
    $cpm_config->errors = array();
    $cpm_config->warnings = array();
    $cpm_config->detailed_warnings = array();
    $cpm_config->messages = array();
    $cpm_config->show_config_editor = true;
    $folders = array(array('comic folder', 'comic_folder', true, ""), array('RSS feed folder', 'rss_comic_folder', false, 'rss'), array('archive folder', 'archive_comic_folder', false, 'archive'), array('mini thumb folder', 'mini_comic_folder', false, 'mini'));
    foreach ($folders as $folder_info) {
        list($name, $property, $is_fatal, $thumb_type) = $folder_info;
        if ($thumb_type != "") {
            $cpm_config->thumbs_folder_writable[$thumb_type] = null;
        }
    }
    if (cpm_option("cpm-skip-checks") == 1) {
        // if the user knows what they're doing, disabling all of the checks improves performance
        foreach ($folders as $folder_info) {
            list($name, $property, $is_fatal, $thumb_type) = $folder_info;
            $path = CPM_DOCUMENT_ROOT . '/' . $cpm_config->properties[$property];
            if ($thumb_type != "") {
                $cpm_config->thumbs_folder_writable[$thumb_type] = true;
            }
        }
        foreach (array('comic', 'blog') as $type) {
            $result = (object) get_category($cpm_config->properties["{$type}cat"]);
            if (!is_wp_error($result)) {
                $cpm_config->{"{$type}_category_info"} = get_object_vars($result);
            }
        }
        $cpm_config->comic_files = cpm_read_comics_folder();
    } else {
        // quick check to see if the theme is ComicPress.
        // this needs to be made more robust.
        if (preg_match('/ComicPress/', get_current_theme()) == 0) {
            $cpm_config->detailed_warnings[] = __("The current theme isn't the ComicPress theme.  If you've renamed the theme, ignore this warning.", 'comicpress-manager');
        }
        if (!extension_loaded('zip')) {
            $cpm_config->detailed_warnings[] = __("You do not have the Zip extension installed. Uploading a Zip file will not work.", 'comicpress-manager');
        }
        $any_cpm_document_root_failures = false;
        if (!$wpmu_version) {
            // is the site root configured properly?
            if (!file_exists(CPM_DOCUMENT_ROOT)) {
                $cpm_config->warnings[] = sprintf(__('The comics site root <strong>%s</strong> does not exist. Check your <a href="options-general.php">WordPress address and address settings</a>.', 'comicpress-manager'), CPM_DOCUMENT_ROOT);
                $any_cpm_document_root_failures = true;
            }
            if (!file_exists(CPM_DOCUMENT_ROOT . '/index.php')) {
                $cpm_config->warnings[] = sprintf(__('The comics site root <strong>%s</strong> does not contain a WordPress index.php file. Check your <a href="options-general.php">WordPress address and address settings</a>.', 'comicpress-manager'), CPM_DOCUMENT_ROOT);
                $any_cpm_document_root_failures = true;
            }
        }
        if ($any_cpm_document_root_failures) {
            $cpm_config->warnings[] = print_r($cpm_attempted_document_roots, true);
        }
        // folders that are the same as the comics folder won't be written to
        $all_the_same = array();
        foreach ($cpm_config->separate_thumbs_folder_defined as $type => $value) {
            if (!$value) {
                $all_the_same[] = $type;
            }
        }
        if (count($all_the_same) > 0) {
            $cpm_config->detailed_warnings[] = sprintf(__("The <strong>%s</strong> folders and the comics folder are the same.  You won't be able to generate thumbnails until you change these folders.", 'comicpress-manager'), implode(", ", $all_the_same));
        }
        if (cpm_option('cpm-did-first-run') == 1) {
            // check the existence and writability of all image folders
            foreach ($folders as $folder_info) {
                list($name, $property, $is_fatal, $thumb_type) = $folder_info;
                if ($thumb_type == "" || $cpm_config->separate_thumbs_folder_defined[$thumb_type] == true) {
                    $path = CPM_DOCUMENT_ROOT . '/' . $cpm_config->properties[$property];
                    if (!file_exists($path)) {
                        $cpm_config->errors[] = sprintf(__('The %1$s <strong>%2$s</strong> does not exist.  Did you create it within the <strong>%3$s</strong> folder?', 'comicpress-manager'), $name, $cpm_config->properties[$property], CPM_DOCUMENT_ROOT);
                    } else {
                        do {
                            $tmp_filename = "test-" . md5(rand());
                        } while (file_exists($path . '/' . $tmp_filename));
                        $ok_to_warn = true;
                        if ($thumb_type != "") {
                            $ok_to_warn = cpm_option("cpm-{$thumb_type}-generate-thumbnails") == 1;
                        }
                        if ($ok_to_warn) {
                            if (!@touch($path . '/' . $tmp_filename)) {
                                $message = sprintf(__('The %1$s <strong>%2$s</strong> is not writable by the Webserver.', 'comicpress-manager'), $name, $cpm_config->properties[$property]);
                                if ($is_fatal) {
                                    $cpm_config->errors[] = $message;
                                } else {
                                    $cpm_config->warnings[] = $message;
                                }
                                if ($thumb_type != "") {
                                    $cpm_config->thumbs_folder_writable[$thumb_type] = false;
                                }
                            } else {
                                if (@stat($path . '/' . $tmp_filename) === false) {
                                    $cpm_config->errors[] = __('<strong>Files written to the %s directory by the Webserver cannot be read again!</strong>  Are you using IIS7 with FastCGI?', $cpm_config->properties[$property]);
                                    if ($thumb_type != "") {
                                        $cpm_config->thumbs_folder_writable[$thumb_type] = false;
                                    }
                                }
                            }
                        }
                        if (is_null($cpm_config->thumbs_folder_writable[$thumb_type])) {
                            @unlink($path . '/' . $tmp_filename);
                            if ($thumb_type != "") {
                                $cpm_config->thumbs_folder_writable[$thumb_type] = true;
                            }
                        }
                    }
                }
            }
        }
        // to generate thumbnails, a supported image processor is needed
        if ($cpm_config->get_scale_method() == CPM_SCALE_NONE) {
            $cpm_config->detailed_warnings[] = __("No image resize methods are installed (GD or ImageMagick).  You are unable to generate thumbnails automatically.", 'comicpress-manager');
        }
        // are there enough categories created?
        if (count(get_all_category_ids()) < 2) {
            $cpm_config->errors[] = __("You need to define at least two categories, a blog category and a comics category, to use ComicPress.  Visit <a href=\"categories.php\">Manage -> Categories</a> and create at least two categories, then return here to continue your configuration.", 'comicpress-manager');
            $cpm_config->show_config_editor = false;
        } else {
            // ensure the defined comic category exists
            if (is_null($cpm_config->properties['comiccat'])) {
                // all non-blog categories are comic categories
                $cpm_config->comic_category_info = array('name' => __("All other categories", 'comicpress-manager'));
                $cpm_config->properties['comiccat'] = array_diff(get_all_category_ids(), array($cpm_config->properties['blogcat']));
                if (count($cpm_config->properties['comiccat']) == 1) {
                    $cpm_config->properties['comiccat'] = $cpm_config->properties['comiccat'][0];
                    $cpm_config->comic_category_info = get_object_vars(get_category($cpm_config->properties['comiccat']));
                }
            } else {
                if (!is_numeric($cpm_config->properties['comiccat'])) {
                    // the property is non-numeric
                    $cpm_config->errors[] = __("The comic category needs to be defined as a number, not an alphanumeric string.", 'comicpress-manager');
                } else {
                    // one comic category is specified
                    if (is_null($cpm_config->comic_category_info = get_category($cpm_config->properties['comiccat']))) {
                        $cpm_config->errors[] = sprintf(__("The requested category ID for your comic, <strong>%s</strong>, doesn't exist!", 'comicpress-manager'), $cpm_config->properties['comiccat']);
                    } else {
                        $cpm_config->comic_category_info = get_object_vars($cpm_config->comic_category_info);
                    }
                }
            }
            // ensure the defined blog category exists
            // TODO: multiple blog categories
            if (!is_numeric($cpm_config->properties['blogcat'])) {
                // the property is non-numeric
                $cpm_config->errors[] = __("The blog category needs to be defined as a number, not an alphanumeric string.", 'comicpress-manager');
            } else {
                if (is_null($cpm_config->blog_category_info = get_category($cpm_config->properties['blogcat']))) {
                    $cpm_config->errors[] = sprintf(__("The requested category ID for your blog, <strong>%s</strong>, doesn't exist!", 'comicpress-manager'), $cpm_config->properties['blogcat']);
                } else {
                    $cpm_config->blog_category_info = get_object_vars($cpm_config->blog_category_info);
                }
                if (!is_array($cpm_config->properties['blogcat']) && !is_array($cpm_config->properties['comiccat'])) {
                    if ($cpm_config->properties['blogcat'] == $cpm_config->properties['comiccat']) {
                        $cpm_config->warnings[] = __("Your comic and blog categories are the same.  This will cause browsing problems for visitors to your site.", 'comicpress-manager');
                    }
                }
            }
        }
        // a quick note if you have no comics uploaded.
        // could be a sign of something more serious.
        if (count($cpm_config->comic_files = cpm_read_comics_folder()) == 0) {
            $cpm_config->detailed_warnings[] = __("Your comics folder is empty!", 'comicpress-manager');
        }
    }
}