function write_archive()
{
    $header = shell_exec('php -q ' . ROOT_DIR . '/' . LAYOUT_DIR . '/header.php');
    $footer = shell_exec('php -q ' . ROOT_DIR . '/' . LAYOUT_DIR . '/footer.php');
    $sidebar = shell_exec('php -q ' . ROOT_DIR . '/' . LAYOUT_DIR . '/sidebar.php');
    $content = "{$header}\n\n";
    $content .= "<h3>Archives</h3>\n";
    $content .= "<br/><br/>\n";
    $content .= "<hr/>\n\n";
    $archive = create_data_info(NULL);
    $year = array_reverse(array_keys($archive));
    $size = sizeOf($year);
    for ($y = 0; $y < $size; $y++) {
        $content .= "<h2 class='archives'><a href='" . URL . "/{$year[$y]}'>{$year[$y]}</a></h2>\n\n";
        $archive[$year[$y]] = $archive[$year[$y]];
        $month = array_reverse(array_keys($archive[$year[$y]]));
        $size_month = sizeOf($month);
        for ($m = 0; $m < $size_month; $m++) {
            $date = @date('F', @mktime(0, 0, 0, $month[$m]));
            $content .= "<h3 class='archives'><a href='" . URL . '/' . $year[$y] . '/' . $month[$m] . "'>{$date}</a></h3>\n\n";
            $content .= "<ul class='archive-page'>\n";
            $day = array_reverse(array_keys($archive[$year[$y]][$month[$m]]));
            $size_day = sizeOf($day);
            for ($d = 0; $d < $size_day; $d++) {
                $day_posts = array_reverse(array_keys($archive[$year[$y]][$month[$m]][$day[$d]]));
                $size_day_posts = sizeOf($day_posts);
                for ($p = 0; $p < $size_day_posts; $p++) {
                    $post = $archive[$year[$y]][$month[$m]][$day[$d]][$day_posts[$p]];
                    if (empty($post)) {
                        continue;
                    }
                    $filetitle = urlencode($post[0]);
                    $title = $post[3];
                    $filetitle = str_replace('+', '_', $filetitle);
                    $content .= "<li><a href='" . URL . "/{$filetitle}' title='{$title}'>{$title}</a></li>\n";
                }
            }
            $content .= "</ul>\n\n";
        }
    }
    unset($archive);
    $content .= "{$sidebar}\n\n";
    $content .= "{$footer}\n\n";
    $archive_file = ROOT_DIR . '/' . CACHE_DIR . '/archive.html';
    $archive_fd = fopen($archive_file, 'w') or die("can't open file {$archive_file}");
    fwrite($archive_fd, $content);
    fclose($archive_fd);
    chmod($archive_file, 0755);
}
function write_cache()
{
    date_default_timezone_set(TIMEZONE);
    // Create cache for each year
    $flat_year = create_data_info("year");
    foreach ($flat_year as $year => $data) {
        create_pages($data, $year);
    }
    unset($flat_year);
    // Create cache for each year/month (the key of the array is yearmonth so we can split it.)
    $flat_month = create_data_info("month");
    foreach ($flat_month as $yearmonth => $data) {
        $month = substr($yearmonth, -2);
        $year = substr($yearmonth, 0, 4);
        create_pages($data, $year . '/' . $month);
    }
    unset($flat_month);
    // Create the cache for each day.
    $flat_day = create_data_info("day");
    foreach ($flat_day as $yearmonthday => $data) {
        $day = substr($yearmonthday, -2);
        $month = substr($yearmonthday, 4, 2);
        $year = substr($yearmonthday, 0, 4);
        create_pages($data, $year . '/' . $month . '/' . $day);
    }
    unset($flat_day);
    // Create cache per post
    $flat_post = create_data_info("post");
    // Create the index page that will create the "main" pages of the blog
    create_pages($flat_post, "");
    foreach ($flat_post as $post) {
        $title = urlencode($post[0]);
        $title = strtolower(str_replace('+', '_', $title));
        $filepath = ROOT_DIR . '/' . CACHE_DIR . '/' . $title . "_0.html";
        create_page_content($filepath, $post, $GLOBALS["footer"], true);
    }
    unset($flat_post);
}
function write_feed()
{
    date_default_timezone_set(TIMEZONE);
    $xml_feed = ROOT_DIR . '/_atom.xml';
    $flat_posts = create_data_info("post");
    //Check if we need to create the feed or not
    $last_mtime = 0;
    $size = sizeOf($flat_posts) - 1;
    $bound = $size - POST_RSS;
    for ($i = $size; $i > $bound; $i--) {
        if (empty($flat_posts[$i])) {
            continue;
        }
        $last_mtime = $flat_posts[$i][4] > $last_mtime ? $flat_posts[$i][4] : $last_mtime;
    }
    if (file_exists($xml_feed) && $last_mtime < filemtime($xml_feed)) {
        unset($flat_posts);
        return NULL;
    }
    $now = date('c');
    $content = "<?xml version='1.0' encoding='utf-8'?>\n";
    $content .= "<feed xmlns='http://www.w3.org/2005/Atom'>\n";
    $content .= "<title>" . FEED_TITLE . "</title>\n";
    $content .= "<link href='" . URL . "' />\n";
    $content .= "<link href='" . URL . "/feed/' rel='self' />\n";
    $base_url = str_replace(array('http://', 'https://'), '', URL);
    $base_url = str_replace("#", "/", $base_url);
    $content .= "<id>tag:{$base_url}," . date("Y-m-d") . ":/</id>\n";
    $content .= "<updated>{$now}</updated>\n";
    $content .= "<author>\n";
    $content .= "<name>" . AUTHOR_NAME . "</name>\n";
    $content .= "<email>" . AUTHOR_MAIL . "</email>\n";
    $content .= "</author>\n";
    for ($i = $size; $i > $bound; $i--) {
        if (empty($flat_posts[$i])) {
            continue;
        }
        $filename = $flat_posts[$i][2];
        $url_name = URL . '/' . $flat_posts[$i][0];
        $title = $flat_posts[$i][3];
        $date = date("d F Y H:i:s", $flat_posts[$i][4]);
        $tag_date = $flat_posts[$i][5] . '-' . $flat_posts[$i][6] . '-' . $flat_posts[$i][7];
        $content .= "<entry>" . "\n";
        $content .= "<title>{$title}</title>" . "\n";
        $content .= "<link rel='alternate' type='text/html' href='{$url_name}'/>" . "\n";
        $content .= "<id>tag:{$base_url}," . $tag_date . ":/" . $flat_posts[$i][0] . "</id>\n";
        $content .= "<updated>";
        $date_rfc3339 = date('c', $flat_posts[$i][4]);
        $content .= "{$date_rfc3339}";
        $content .= "</updated>" . "\n";
        $post_html = post_to_html($flat_posts[$i], False, False);
        $content .= "<content type='html'>\n<![CDATA[{$post_html}]]>\n</content>" . "\n";
        unset($post_html);
        $content .= "</entry>" . "\n\n";
    }
    $content .= "</feed>\n";
    $xml_fd = fopen($xml_feed, 'w') or die("can't open {$xml_feed}\n");
    fwrite($xml_fd, $content);
    fclose($xml_fd);
    chmod($xml_feed, 0755);
}