/**
 * Calculate the document root where comics are stored.
 */
function cpm_calculate_document_root()
{
    global $wpmu_version;
    $document_root = "";
    if (cpm_option('cpm-use-old-subdirectory-method')) {
        $parsed_url = parse_url(get_option('home'));
        $translated_script_filename = str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME']);
        foreach (array('SCRIPT_NAME', 'SCRIPT_URL') as $var_to_try) {
            $root_to_try = substr($translated_script_filename, 0, -strlen($_SERVER[$var_to_try])) . $parsed_url['path'];
            if (file_exists($root_to_try . '/index.php')) {
                $document_root = $root_to_try;
                break;
            }
        }
        if (is_null($document_root)) {
            $document_root = $_SERVER['DOCUMENT_ROOT'] . $parsed_url['path'];
        }
    } else {
        // a base document root to try and use
        if (isset($_SERVER['SCRIPT_FILENAME'])) {
            $document_root = dirname($_SERVER['SCRIPT_FILENAME']);
        }
        $cwd = getcwd();
        if ($cwd !== false) {
            // Strip the wp-admin part and just get to the root.
            $document_root = preg_replace('#[\\\\/]wp-(admin|content).*#', '', $cwd);
        }
        if (isset($wpmu_version)) {
            $document_root = cpm_wpmu_modify_path($document_root);
        }
    }
    return untrailingslashit($document_root);
}
function comicpress_notice_debug()
{
    global $current_user, $comiccat, $blogcat, $comic_folder, $wpdb, $category_tree, $non_comic_categories, $comicpress_options;
    if (substr($_SERVER['PHP_SELF'], -19) != '/wp-admin/index.php' || !$comicpress_options['enable_comicpress_debug']) {
        return;
    }
    $comicpress_options = comicpress_load_options();
    $error = array();
    if (floatval(phpversion()) < 5.0) {
        $phpversion = phpversion();
        $error[] = array('header', __('You are using PHP version: ', 'comicpress') . $phpversion);
        $error[] = __('Version 5 or higher is required for this theme to work correctly.  Please check with your host about upgrading to a newer version.', 'comicpress');
    }
    $post_cat_link = get_bloginfo('wpurl') . '/wp-admin/categories.php';
    // Check Categories
    if ($comiccat == $blogcat) {
        $error[] = array('header', __('Primary Comic and Blog categories are not configured properly.', 'comicpress'));
        $error[] = __('ComicPress requires 2 categories to be added to the ', 'comicpress') . '<a href="' . $post_cat_link . '">' . __('post categories.', 'comicpress') . '</a>' . __(' It is necessary to have 2 more categories in addition to the uncategorized category, a Blog and Comic primary categories.  These two additional categories will be the root categories that seperate the difference between the comic and blog posts.  When you post a new comic you will be posting it into the comic category or heirarchal children of the comic category.   When posting a new blog post you need to set it into the blog category or child of the blog category.   Uncategorized will act as a blog post category (do not rename uncategorized).  You can configure the categories to set as the primary blog and comic category from within the ComicPress Manager plugin.', 'comicpress');
    }
    if (empty($error)) {
        // Check Comics Folder
        $document_root = ABSPATH;
        if (function_exists('cpm_wpmu_modify_path')) {
            $document_root = cpm_wpmu_modify_path($document_root);
        }
        if (!is_dir($document_root . '/' . $comic_folder)) {
            $error[] = array('header', __('Comics Folder is not configured and is unable to be found.', 'comicpress'));
            $error[] = __('ComicPress stores the files it uses inside a specific directory and that directory is set from within ComicPress Manager.  When this error is present it means that the theme is unable to find the appropriate directory to read the comics from.', 'comicpress');
            $error[] = __('ComicPress Manager - Manager Config will allow you to create the directories needed.');
        }
    }
    if (empty($error)) {
        // Make sure the ComicPress theme is installed in themes/comicpress
        if (ABSPATH . 'wp-content/themes/comicpress' != get_template_directory()) {
            $error[] = array('header', __('ComicPress theme is not installed into the correct folder.', 'comicpress'));
            $error[] = __('As of version 2.9, the ComicPress main core theme is required to be installed into the wp-content/themes/comicpress directory.  It is currently not installed into that directory.', 'comicpress');
        }
    }
    if (empty($error) && $comicpress_options['enable_full_post_check']) {
        // Check to make sure posts are not in blogcat and comiccat both
        $founderror = false;
        $non_comic_categories = str_replace(' and ', ',', $non_comic_categories);
        $blog_query = '&show_posts=-1&posts_per_page=-1&cat=' . $non_comic_categories;
        query_posts($blog_query);
        if (have_posts()) {
            while (have_posts()) {
                the_post();
                if (in_comic_category()) {
                    $founderrorpostlist .= '<a href="' . get_bloginfo('wpurl') . '/wp-admin/post.php?action=edit&post=' . get_the_ID() . '">' . get_the_title() . '</a> - Error: Category Crossover<br />';
                    $founderror = true;
                }
            }
        }
        if ($founderror) {
            $error[] = array('header', __('Post\'s are in both a comic category and blog category.', 'comicpress'));
            $error[] = __('The following posts are set both in a comic category and a blog category, with ComicPress the designations of categories is very important.  The rule of thumb is to make sure that all posts are only in a single category.   If a post is in both the comic category and blog category there will be issues with both navigation and execution of the ComicPress code.', 'comicpress');
            $error[] = $founderrorpostlist;
        }
    }
    if (empty($error) && $comicpress_options['enable_full_post_check']) {
        $founderror = false;
        $blog_query = '&show_posts=-1&posts_per_page=-1';
        $posts = query_posts($blog_query);
        foreach ($posts as $testpost) {
            $post_title_slug = $testpost->post_name;
            if (is_numeric($post_title_slug)) {
                $founderror = true;
                $founderrorpostname .= '<a href="' . get_bloginfo('wpurl') . '/wp-admin/post.php?action=edit&post=' . $testpost->ID . '">' . get_the_title($testpost->ID) . '</a> - Error: Post Slug (Permalink) is Numeric<br />';
            }
        }
        if ($founderror) {
            $error[] = array('header', __('Post\'s slug is a numeric.', 'comicpress'));
            $error[] = __('The following posts have a post slug (permalink) that is numeric.  This will cause problems with permalinks.   Post slugs must have at least one alphabetic character in them for Wordpress to handle correctly.', 'comicpress');
            $error[] = $founderrorpostname;
        }
    }
    if (!empty($error)) {
        ?>
	<div class="error">
		<h2>ComicPress Debug</h2>
		ComicPress doesn't seem to be fully installed at this time, check out these messages.<br />
		<br />
		<?php 
        foreach ($error as $info) {
            unset($text);
            if (is_array($info)) {
                list($type, $text) = $info;
            } else {
                if (is_string($info)) {
                    $text = $info;
                    $type = 'paragraph';
                }
            }
            if (!empty($text) && !empty($type)) {
                switch ($type) {
                    case 'header':
                        echo "<h3>{$text}</h3>";
                        break;
                    case 'raw':
                        echo $text;
                        break;
                    default:
                        echo "<p>{$text}</p>";
                        break;
                }
            }
        }
        ?>
		<br />
		<br />
	</div>
<?php 
    }
}
 /**
  * Calculate the document root where comics are stored.
  * @param array $override_server_info If set, override $_SERVER with these values.
  * @return string|boolean The document root, or false if there was an error.
  */
 function calculate_document_root($override_server_info = null)
 {
     global $cpm_attempted_document_roots, $wpmu_version;
     $cpm_attempted_document_roots = array();
     $server_info = !is_null($override_server_info) ? $override_server_info : $_SERVER;
     // we need something to work with
     $any_possible_data = false;
     foreach (array('SCRIPT_FILENAME', 'DOCUMENT_ROOT') as $field) {
         if (isset($server_info[$field])) {
             $any_possible_data = true;
             break;
         }
     }
     if (!$any_possible_data) {
         return false;
     }
     $document_root = null;
     // first try getting path info straight from server info
     $translated_script_filename = str_replace('\\', '/', $server_info['SCRIPT_FILENAME']);
     foreach (array('SCRIPT_NAME', 'SCRIPT_URL') as $var_to_try) {
         if (isset($server_info[$var_to_try])) {
             $root_to_try = substr($translated_script_filename, 0, -strlen($server_info[$var_to_try]));
             $cpm_attempted_document_roots[] = $root_to_try;
             if ($this->_f->file_exists($root_to_try . '/index.php')) {
                 $document_root = $root_to_try;
                 break;
             }
         }
     }
     // then use the URL if necessary
     if (is_null($document_root) && isset($server_info['DOCUMENT_ROOT'])) {
         $parsed_url = @parse_url(get_option('home'));
         if ($parsed_url === false) {
             return false;
         }
         $document_root = untrailingslashit($server_info['DOCUMENT_ROOT']) . $parsed_url['path'];
     }
     // still nothing found?
     if (is_null($document_root)) {
         return false;
     }
     // WPMU
     if (!empty($wpmu_version) && function_exists('cpm_wpmu_modify_path')) {
         $document_root = cpm_wpmu_modify_path($document_root);
     }
     return untrailingslashit($document_root);
 }