/**
 * Handle uploading a set of files.
 * @param array $files A list of valid $_FILES keys to process.
 */
function cpm_handle_file_uploads($files)
{
    global $cpm_config;
    $posts_created = array();
    $duplicate_posts = array();
    $files_uploaded = array();
    $thumbnails_written = array();
    $invalid_filenames = array();
    $thumbnails_not_written = array();
    $files_not_uploaded = array();
    $invalid_image_types = array();
    $gd_rename_file = array();
    $did_convert_cmyk_jpeg = array();
    $target_root = CPM_DOCUMENT_ROOT . '/' . $cpm_config->properties[$_POST['upload-destination'] . "_folder"];
    if (($subdir = cpm_get_subcomic_directory()) !== false) {
        $target_root .= '/' . $subdir;
    }
    $write_thumbnails = isset($_POST['thumbnails']) && $_POST['upload-destination'] == "comic";
    $new_post = isset($_POST['new_post']) && $_POST['upload-destination'] == "comic";
    $ok_to_keep_uploading = true;
    $files_created_in_operation = array();
    $filename_original_titles = array();
    foreach ($files as $key) {
        if (is_uploaded_file($_FILES[$key]['tmp_name'])) {
            if ($_FILES[$key]['error'] != 0) {
                switch ($_FILES[$key]['error']) {
                    case UPLOAD_ERR_INI_SIZE:
                    case UPLOAD_ERR_FORM_SIZE:
                        $cpm_config->warnings[] = sprintf(__("<strong>The file you uploaded was too large.</strong>  The max allowed filesize for uploads to your server is %s.", 'comicpress-manager'), ini_get('upload_max_filesize'));
                        break;
                    case UPLOAD_ERR_NO_FILE:
                        break;
                    default:
                        $cpm_config->warnings[] = sprintf(__("<strong>There was an error in uploading.</strong>  The <a href='http://php.net/manual/en/features.file-upload.errors.php'>PHP upload error code</a> was %s.", 'comicpress-manager'), $_FILES[$key]['error']);
                        break;
                }
            } else {
                if (strpos($_FILES[$key]['name'], ".zip") !== false) {
                    $invalid_files = array();
                    //harmonious zip_open zip_entry_name zip_read zip_entry_read zip_entry_open zip_entry_filesize zip_entry_close zip_close
                    if (extension_loaded("zip")) {
                        if (is_resource($zip = zip_open($_FILES[$key]['tmp_name']))) {
                            while ($zip_entry = zip_read($zip)) {
                                if (zip_entry_open($zip, $zip_entry, "r")) {
                                    $temp_path = $target_root . '/' . md5(rand());
                                    file_write_contents($temp_path, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)));
                                    $comic_file = zip_entry_name($zip_entry);
                                    $target_filename = pathinfo(zip_entry_name($zip_entry), PATHINFO_BASENAME);
                                    $result = cpm_breakdown_comic_filename($target_filename, true);
                                    if (file_exists($temp_path)) {
                                        extract(cpm_do_gd_file_check_on_upload($temp_path, $target_filename));
                                        if ($result !== false) {
                                            extract($result, EXTR_PREFIX_ALL, "filename");
                                            if ($file_ok) {
                                                if (($obfuscated_filename = cpm_obfuscate_filename($target_filename)) !== $target_filename) {
                                                    $cpm_config->messages[] = sprintf(__('Uploaded file %1$s renamed to %2$s.', 'comicpress-manager'), $target_filename, $obfuscated_filename);
                                                    $filename_original_titles[$obfuscated_filename] = $result['converted_title'];
                                                    $target_filename = $obfuscated_filename;
                                                }
                                                @rename($temp_path, $target_root . '/' . $target_filename);
                                                $files_created_in_operation[] = $target_root . '/' . $target_filename;
                                                $files_uploaded[] = $target_filename;
                                                if ($gd_did_rename) {
                                                    $gd_rename_file[] = $comic_file;
                                                }
                                                if ($did_filecheck) {
                                                    if ($is_cmyk) {
                                                        $did_convert_cmyk_jpeg[] = $comic_file;
                                                    }
                                                }
                                            } else {
                                                if ($did_filecheck) {
                                                    $invalid_image_types[] = $comic_file;
                                                } else {
                                                    $invalid_filenames[] = $comic_file;
                                                }
                                            }
                                        } else {
                                            $files_not_uploaded[] = $comic_file;
                                        }
                                    } else {
                                        $invalid_filenames[] = $comic_file;
                                    }
                                    @unlink($temp_path);
                                    zip_entry_close($zip_entry);
                                }
                                if (($result = cpm_breakdown_comic_filename($target_filename, true)) !== false) {
                                    extract($result, EXTR_PREFIX_ALL, 'filename');
                                    $target_path = $target_root . '/' . $target_filename;
                                    if (isset($_POST['upload-date-format']) && !empty($_POST['upload-date-format'])) {
                                        $target_filename = date(CPM_DATE_FORMAT, strtotime($result['date'])) . $result['title'] . '.' . pathinfo($_FILES[$key]['name'], PATHINFO_EXTENSION);
                                    }
                                } else {
                                    $invalid_filenames[] = $comic_file;
                                }
                            }
                            zip_close($zip);
                        }
                    } else {
                        $cpm_config->warnings[] = sprintf(__("The Zip extension is not installed. %s was not processed.", 'comicpress-manager'), $_FILES[$key]['name']);
                    }
                    //harmonious_end
                } else {
                    $target_filename = $_FILES[$key]['name'];
                    if (get_magic_quotes_gpc()) {
                        $target_filename = stripslashes($target_filename);
                    }
                    $tried_replace = false;
                    if (!empty($_POST['overwrite-existing-file-choice'])) {
                        $tried_replace = true;
                        $original_filename = $target_filename;
                        $target_filename = $_POST['overwrite-existing-file-choice'];
                        if (get_magic_quotes_gpc()) {
                            $target_filename = stripslashes($target_filename);
                        }
                        $new_post = false;
                        if (pathinfo($original_filename, PATHINFO_EXTENSION) != pathinfo($target_filename, PATHINFO_EXTENSION)) {
                            if (@unlink($target_root . '/' . $target_filename)) {
                                foreach (cpm_get_thumbnails_to_generate() as $type) {
                                    $path = CPM_DOCUMENT_ROOT . '/' . $cpm_config->properties[$type . "_comic_folder"];
                                    if (($subdir = cpm_get_thumbnails_to_generate()) !== false) {
                                        $path .= '/' . $subdir;
                                    }
                                    @unlink($path . '/' . $target_filename);
                                }
                            }
                            $target_filename = preg_replace('#\\.[^\\.]+$#', '', $target_filename) . '.' . pathinfo($original_filename, PATHINFO_EXTENSION);
                        }
                        $result = cpm_breakdown_comic_filename($target_filename);
                        $cpm_config->messages[] = sprintf(__('Uploaded file <strong>%1$s</strong> renamed to <strong>%2$s</strong>.', 'comicpress-manager'), $original_filename, $target_filename);
                    } else {
                        if (count($files) == 1) {
                            if (!empty($_POST['override-date'])) {
                                $date = strtotime($_POST['override-date']);
                                if ($date !== false && $date !== -1) {
                                    $new_date = date(CPM_DATE_FORMAT, $date);
                                    $old_filename = $target_filename;
                                    if (($target_result = cpm_breakdown_comic_filename($target_filename, true)) !== false) {
                                        $target_filename = $new_date . $target_result['title'] . '.' . pathinfo($target_filename, PATHINFO_EXTENSION);
                                    } else {
                                        $target_filename = $new_date . '-' . $target_filename;
                                    }
                                    if ($old_filename !== $target_filename) {
                                        $cpm_config->messages[] = sprintf(__('Uploaded file %1$s renamed to %2$s.', 'comicpress-manager'), $_FILES[$key]['name'], $target_filename);
                                    }
                                    $result = cpm_breakdown_comic_filename($target_filename);
                                } else {
                                    if (preg_match('/\\S/', $_POST['override-date']) > 0) {
                                        $cpm_config->warnings[] = sprintf(__("Provided override date %s is not parseable by strtotime().", 'comicpress-manager'), $_POST['override-date']);
                                    }
                                }
                            }
                        }
                        $result = cpm_breakdown_comic_filename($target_filename, true);
                        if ($result !== false) {
                            // bad file, can we get a date attached?
                            if (isset($_POST['upload-date-format']) && !empty($_POST['upload-date-format'])) {
                                $target_filename = date(CPM_DATE_FORMAT, strtotime($result['date'])) . $result['title'] . '.' . pathinfo($_FILES[$key]['name'], PATHINFO_EXTENSION);
                            }
                        }
                    }
                    $comic_file = $_FILES[$key]['name'];
                    extract(cpm_do_gd_file_check_on_upload($_FILES[$key]['tmp_name'], $target_filename));
                    $output_file = null;
                    if ($result !== false) {
                        extract($result, EXTR_PREFIX_ALL, "filename");
                        if ($file_ok) {
                            if (($obfuscated_filename = cpm_obfuscate_filename($target_filename)) !== $target_filename) {
                                $cpm_config->messages[] = sprintf(__('Uploaded file %1$s renamed to %2$s.', 'comicpress-manager'), $target_filename, $obfuscated_filename);
                                $filename_original_titles[$obfuscated_filename] = $result['converted_title'];
                                $target_filename = $obfuscated_filename;
                            }
                            $output_file = $target_root . '/' . $target_filename;
                            @move_uploaded_file($_FILES[$key]['tmp_name'], $output_file);
                            $files_created_in_operation[] = $output_file;
                            if (file_exists($output_file)) {
                                $files_uploaded[] = $target_filename;
                                if ($gd_did_rename) {
                                    $gd_rename_file[] = $comic_file;
                                }
                            } else {
                                $files_not_uploaded[] = $target_filename;
                            }
                            if ($did_filecheck) {
                                if ($is_cmyk) {
                                    $did_convert_cmyk_jpeg[] = $comic_file;
                                }
                            }
                        } else {
                            if ($did_filecheck) {
                                $invalid_image_types[] = $comic_file;
                            } else {
                                $invalid_filenames[] = $comic_file;
                            }
                        }
                    } else {
                        if (!$tried_replace) {
                            $invalid_filenames[] = $comic_file;
                        }
                    }
                    if ($cpm_config->scale_method_cache == CPM_SCALE_IMAGEMAGICK && cpm_option('cpm-strip-icc-profiles') == "1" && !empty($output_file)) {
                        $temp_output_file = $output_file . '.' . md5(rand());
                        $command = array("convert", "\"{$output_file}\"", "-strip", "\"{$temp_output_file}\"");
                        $strip_profiles = escapeshellcmd(implode(" ", $command));
                        exec($strip_profiles);
                        if (file_exists($temp_output_file)) {
                            @unlink($output_file);
                            @rename($temp_output_file, $output_file);
                        }
                    }
                }
            }
        }
        if ($wpmu_version) {
            if (cpm_wpmu_is_over_storage_limit()) {
                $ok_to_keep_uploading = false;
                break;
            }
        }
    }
    if ($ok_to_keep_uploading) {
        foreach ($files_uploaded as $target_filename) {
            $target_path = $target_root . '/' . $target_filename;
            @chmod($target_path, CPM_FILE_UPLOAD_CHMOD);
            if ($write_thumbnails) {
                $wrote_thumbnail = cpm_write_thumbnail($target_path, $target_filename);
            }
            if (!is_null($wrote_thumbnail)) {
                if (is_array($wrote_thumbnail)) {
                    $thumbnails_written[] = $target_filename;
                    $files_created_in_operation = array_merge($files_created_in_operation, $wrote_thumbnail);
                } else {
                    $thumbnails_not_written[] = $target_filename;
                }
            }
        }
        if ($wpmu_version) {
            if (cpm_wpmu_is_over_storage_limit()) {
                $ok_to_keep_uploading = false;
            }
        }
    }
    if ($ok_to_keep_uploading) {
        foreach ($files_uploaded as $target_filename) {
            if ($new_post) {
                extract(cpm_breakdown_comic_filename($target_filename), EXTR_PREFIX_ALL, "filename");
                if (isset($filename_original_titles[$target_filename])) {
                    $filename_converted_title = $filename_original_titles[$target_filename];
                }
                if (($post_hash = generate_post_hash($filename_date, $filename_converted_title)) !== false) {
                    extract($post_hash);
                    $ok_to_create_post = true;
                    if (isset($_POST['duplicate_check'])) {
                        $ok_to_create_post = ($post_id = post_exists($post_title, $post_content, $post_date)) == 0;
                    }
                    if ($ok_to_create_post) {
                        if (!is_null($post_id = wp_insert_post($post_hash))) {
                            $posts_created[] = get_post($post_id, ARRAY_A);
                            foreach (array('hovertext', 'transcript') as $field) {
                                if (!empty($_POST["{$field}-to-use"])) {
                                    update_post_meta($post_id, $field, $_POST["{$field}-to-use"]);
                                }
                            }
                        }
                    } else {
                        $duplicate_posts[] = array(get_post($post_id, ARRAY_A), $target_filename);
                    }
                } else {
                    $invalid_filenames[] = $target_filename;
                }
            }
        }
        cpm_display_operation_messages(compact('invalid_filenames', 'files_uploaded', 'files_not_uploaded', 'thumbnails_written', 'thumbnails_not_written', 'posts_created', 'duplicate_posts', 'invalid_image_types', 'gd_rename_file', 'did_convert_cmyk_jpeg'));
    } else {
        $cpm_config->messages = array();
        $cpm_config->warnings = array($cpm_config->wpmu_disk_space_message);
        foreach ($files_created_in_operation as $file) {
            @unlink($file);
        }
    }
    return array($posts_created, $duplicate_posts);
}
function cpm_action_create_missing_posts()
{
    global $cpm_config;
    $all_post_dates = array();
    foreach (cpm_query_posts() as $comic_post) {
        $all_post_dates[] = date(CPM_DATE_FORMAT, strtotime($comic_post->post_date));
    }
    $all_post_dates = array_unique($all_post_dates);
    $duplicate_posts_within_creation = array();
    $posts_created = array();
    $thumbnails_written = array();
    $thumbnails_not_written = array();
    $invalid_filenames = array();
    $duplicate_posts = array();
    $new_thumbnails_not_needed = array();
    $execution_time = ini_get("max_execution_time");
    $max_posts_imported = (int) ($execution_time / 2);
    $imported_post_count = 0;
    $safe_exit = false;
    if (strtotime($_POST['time']) === false) {
        $cpm_config->warnings[] = sprintf(__('<strong>There was an error in the post time (%1$s)</strong>.  The time is not parseable by strtotime().', 'comicpress-manager'), $_POST['time']);
    } else {
        foreach ($cpm_config->comic_files as $comic_file) {
            $comic_file = pathinfo($comic_file, PATHINFO_BASENAME);
            if (($result = cpm_breakdown_comic_filename($comic_file)) !== false) {
                extract($result, EXTR_PREFIX_ALL, 'filename');
                $ok_to_create_post = !in_array($result['date'], $all_post_dates);
                $show_duplicate_post_message = false;
                $post_id = null;
                if (isset($duplicate_posts_within_creation[$result['date']])) {
                    $ok_to_create_post = false;
                    $show_duplicate_post_message = true;
                    $post_id = $duplicate_posts_within_creation[$result['date']];
                }
                if ($ok_to_create_post) {
                    if (isset($_POST['duplicate_check'])) {
                        $ok_to_create_post = ($post_id = post_exists($post_title, $post_content, $post_date)) == 0;
                    }
                } else {
                    if (!isset($_POST['duplicate_check'])) {
                        $ok_to_create_post = true;
                    }
                }
                if ($ok_to_create_post) {
                    if (($post_hash = generate_post_hash($filename_date, $filename_converted_title)) !== false) {
                        if (!is_null($post_id = wp_insert_post($post_hash))) {
                            $imported_post_count++;
                            $posts_created[] = get_post($post_id, ARRAY_A);
                            $date = date(CPM_DATE_FORMAT, strtotime($filename_date));
                            $all_post_dates[] = $date;
                            $duplicate_posts_within_creation[$date] = $post_id;
                            foreach (array('hovertext', 'transcript') as $field) {
                                if (!empty($_POST["{$field}-to-use"])) {
                                    update_post_meta($post_id, $field, $_POST["{$field}-to-use"]);
                                }
                            }
                            if (isset($_POST['thumbnails'])) {
                                $wrote_thumbnail = cpm_write_thumbnail($cpm_config->path . '/' . $comic_file, $comic_file);
                                if (!is_null($wrote_thumbnail)) {
                                    if ($wrote_thumbnail) {
                                        $thumbnails_written[] = $comic_file;
                                    } else {
                                        $thumbnails_not_written[] = $comic_file;
                                    }
                                } else {
                                    $new_thumbnails_not_needed[] = $comic_file;
                                }
                            }
                        }
                    } else {
                        $invalid_filenames[] = $comic_file;
                    }
                } else {
                    if ($show_duplicate_post_message) {
                        $duplicate_posts[] = array(get_post($post_id, ARRAY_A), $comic_file);
                    }
                }
            }
            if ($imported_post_count >= $max_posts_imported) {
                $safe_exit = true;
                break;
            }
        }
    }
    $cpm_config->import_safe_exit = $safe_exit;
    if ($safe_exit) {
        $cpm_config->messages[] = __("<strong>Import safely exited before you ran out of execution time.</strong> Scroll down to continue creating missing posts.", 'comicpress-manager');
    }
    if (count($posts_created) > 0) {
        cpm_display_operation_messages(compact('invalid_filenames', 'thumbnails_written', 'thumbnails_not_written', 'posts_created', 'duplicate_posts', 'new_thumbnails_not_needed'));
    } else {
        $cpm_config->messages[] = __("<strong>No new posts needed to be created.</strong>", 'comicpress-manager');
    }
}
    $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) {
            if (!in_array($result['date'], $all_post_dates)) {
                if (($post_hash = generate_post_hash($result['date'], $result['converted_title'])) !== false) {
                    $missing_comic_count++;
                }
            }
        }
    }
    header("X-JSON: {missing_posts: {$missing_comic_count}}");
    ob_end_flush();
}
function cpm_action_batch_processing()
{
    global $cpm_config;
    $files_to_delete = array();
    $posts_to_delete = array();
    $thumbnails_to_regenerate = array();
    $files_to_redate = array();
    $posts_to_redate = array();
    $posts_to_generate = array();
    $posts_that_exist = array();
    $posts_to_recategorize = array();
    extract(cpm_normalize_storyline_structure());
    $comic_categories = array();
    foreach ($category_tree as $node) {
        $comic_categories[] = end(explode("/", $node));
    }
    $cpm_config->is_cpm_managing_posts = true;
    foreach ($_POST as $field => $value) {
        if ($_POST['bulk-action'] != "-1" && $_POST['bulk-action'] != "individual") {
            $bulk_posts_updated = array();
            if (preg_match("#^(file|post),([^\\,]*),(.*)\$#", $field, $matches) > 0) {
                list($all, $type, $date, $id) = $matches;
                if (isset($_POST["batch-{$date}"])) {
                    switch ($_POST['bulk-action']) {
                        case "delete":
                            switch ($type) {
                                case "file":
                                    if (($result = cpm_match_id_to_file($id)) !== false) {
                                        $files_to_delete[] = $result;
                                    }
                                    break;
                                case "post":
                                    $posts_to_delete[] = $id;
                                    break;
                            }
                            break;
                        case "regen-thumbs":
                            if ($type == "file") {
                                if (($result = cpm_match_id_to_file($id)) !== false) {
                                    $thumbnails_to_regenerate[] = $result;
                                }
                            }
                            break;
                        case "edit":
                            if ($type == "post") {
                                foreach (array('hovertext' => 'bulk-hovertext', 'transcript' => 'bulk-transcript') as $meta_name => $post_name) {
                                    if (isset($_POST[$post_name])) {
                                        update_post_meta($id, $meta_name, $_POST[$post_name]);
                                    }
                                }
                                $post_categories = wp_get_post_categories($id);
                                $did_change = false;
                                if (isset($_POST['bulk-storyline-in-comic-category'])) {
                                    foreach ($comic_categories as $category_id) {
                                        if (in_array($category_id, $_POST['bulk-storyline-in-comic-category'])) {
                                            if (!in_array($category_id, $post_categories)) {
                                                $did_change = true;
                                                $post_categories[] = $category_id;
                                            }
                                        } else {
                                            if (($index = array_search($category_id, $post_categories)) !== false) {
                                                $did_change = true;
                                                array_splice($post_categories, $index, 1);
                                            }
                                        }
                                    }
                                }
                                if ($did_change) {
                                    wp_set_post_categories($id, $post_categories);
                                }
                                $bulk_posts_updates[] = $id;
                            }
                            break;
                        case "import":
                            switch ($type) {
                                case "file":
                                    if (($result = cpm_match_id_to_file($id)) !== false) {
                                        $posts_to_generate[] = $result;
                                    }
                                    break;
                                case "post":
                                    $posts_that_exist[] = $date;
                                    break;
                            }
                            break;
                    }
                }
            }
        } else {
            if (preg_match('#^([0-9]+)-in-comic-category#', $field, $matches) > 0) {
                if (get_post($matches[1])) {
                    $posts_to_recategorize[$matches[1]] = $value;
                }
            }
            if (preg_match("#^delete-file-(.*)\$#", $field, $matches) > 0) {
                if (($result = cpm_match_id_to_file($matches[1])) !== false) {
                    $files_to_delete[] = $result;
                }
            }
            if (preg_match("#^delete-post-(.*)\$#", $field, $matches) > 0) {
                if (get_post($matches[1])) {
                    $posts_to_delete[] = $matches[1];
                }
            }
            if (preg_match('#^regen-(.*)$#', $field, $matches) > 0) {
                if (($result = cpm_match_id_to_file($matches[1])) !== false) {
                    $thumbnails_to_regenerate[] = $result;
                }
            }
            if (preg_match("#^do-redate-file-(.*)\$#", $field, $matches) > 0) {
                if (($result = cpm_match_id_to_file($matches[1])) !== false) {
                    $files_to_redate[$result] = $value;
                }
            }
            if (preg_match("#^generate-post-(.*)\$#", $field, $matches) > 0) {
                if (($result = cpm_match_id_to_file($matches[1])) !== false) {
                    $posts_to_generate[] = $result;
                }
            }
            if (preg_match("#^delete-post-(.*)\$#", $field, $matches) > 0) {
                if (get_post($matches[1])) {
                    $posts_to_redate[$matches[1]] = $value;
                }
            }
        }
    }
    $did_generate_thumbs = array();
    $ok_to_keep_uploading = true;
    $files_created_in_operation = array();
    if (count($thumbnails_to_regenerate) > 0) {
        $thumbnails_written = array();
        $thumbnails_not_written = array();
        foreach ($thumbnails_to_regenerate as $file) {
            $comic_file = pathinfo($file, PATHINFO_BASENAME);
            $wrote_thumbnail = cpm_write_thumbnail($file, $comic_file, true);
            if (!is_null($wrote_thumbnail)) {
                if (is_array($wrote_thumbnail)) {
                    $files_created_in_operation = array_merge($files_created_in_operation, $wrote_thumbnail);
                    $thumbnails_written[] = $comic_file;
                } else {
                    $thumbnails_not_written[] = $comic_file;
                }
            }
            if (function_exists('cpm_wpmu_is_over_storage_limit')) {
                if (cpm_wpmu_is_over_storage_limit()) {
                    $ok_to_keep_uploading = false;
                    break;
                }
            }
        }
        if (count($thumbnails_written) > 0) {
            $cpm_config->messages[] = sprintf(__("<strong>The following thumbnails were written:</strong> %s", 'comicpress-manager'), implode(", ", $thumbnails_written));
        }
        if (count($thumbnails_not_written) > 0) {
            $cpm_config->warnings[] = sprintf(__("<strong>The following thumbnails were not written:</strong> %s", 'comicpress-manager'), implode(", ", $thumbnails_not_written));
        }
    }
    if (count($bulk_posts_updates) > 0) {
        $cpm_config->messages[] = sprintf(__("<strong>The following posts were updated:</strong> %s", 'comicpress-manager'), implode(", ", $bulk_posts_updates));
    }
    if (count($files_to_delete) > 0) {
        $comic_files_deleted = array();
        foreach ($files_to_delete as $file) {
            $comic_file = pathinfo($file, PATHINFO_BASENAME);
            $delete_targets = array($file);
            foreach ($cpm_config->thumbs_folder_writable as $type => $value) {
                $path = CPM_DOCUMENT_ROOT . '/' . $cpm_config->properties[$type . "_comic_folder"];
                if (($subdir = cpm_get_subcomic_directory()) !== false) {
                    $path .= '/' . $subdir;
                }
                $path .= '/' . $comic_file;
                $delete_targets[] = $path;
            }
            foreach ($delete_targets as $target) {
                if (file_exists($target)) {
                    @unlink($target);
                }
            }
            $comic_files_deleted[] = $comic_file;
        }
        $cpm_config->messages[] = sprintf(__("<strong>The following comic files and their associated thumbnails were deleted:</strong> %s", 'comicpress-manager'), implode(", ", $comic_files_deleted));
    }
    if (count($posts_to_delete) > 0) {
        foreach ($posts_to_delete as $post) {
            wp_delete_post($post);
        }
        $cpm_config->messages[] = sprintf(__("<strong>The following posts were deleted:</strong> %s", 'comicpress-manager'), implode(", ", $posts_to_delete));
    }
    $master_category = end(explode("/", reset($category_tree)));
    foreach ($posts_to_generate as $file) {
        $ok = false;
        $comic_file = pathinfo($file, PATHINFO_BASENAME);
        if (($result = cpm_breakdown_comic_filename($comic_file)) !== false) {
            if (!in_array(date("Y-m-d", strtotime($result['date'])), $posts_that_exist)) {
                if (($post_hash = generate_post_hash($result['date'], $result['converted_title'])) !== false) {
                    $post_hash['post_category'] = array($master_category);
                    $ok = !is_null($post_id = wp_insert_post($post_hash));
                }
            }
        }
        if ($ok) {
            $cpm_config->messages[] = sprintf(__('<strong>Created post %1$s for %2$s.</strong>', 'comicpress-manager'), $post_id, $comic_file);
        } else {
            $cpm_config->warnings[] = sprintf(__("<strong>Could not create post for %s.</strong>", 'comicpress-manager'), $comic_file);
        }
    }
    foreach ($posts_to_recategorize as $id => $requested_comic_categories) {
        if (!in_array($id, $posts_to_delete)) {
            $post_categories = wp_get_post_categories($id);
            $did_change = false;
            foreach ($comic_categories as $category_id) {
                if (in_array($category_id, $requested_comic_categories)) {
                    if (!in_array($category_id, $post_categories)) {
                        $did_change = true;
                        $post_categories[] = $category_id;
                    }
                } else {
                    if (($index = array_search($category_id, $post_categories)) !== false) {
                        $did_change = true;
                        array_splice($post_categories, $index, 1);
                    }
                }
            }
            if ($did_change) {
                wp_set_post_categories($id, $post_categories);
                $cpm_config->messages[] = sprintf(__("<strong>Storyline for post %s updated.</strong>", 'comicpress-manager'), $id);
            }
        }
    }
    if (!$ok_to_keep_uploading) {
        $cpm_config->warnings = array($cpm_config->wpmu_disk_space_message);
        foreach ($files_created_in_operation as $file) {
            @unlink($file);
        }
    }
    $cpm_config->comic_files = cpm_read_comics_folder();
}