<?php define('IMAGES_INDEX_TARGETS_DIR', ''); ?>
<?php define('IMAGES_INDEX_TARGETS_FILE', '**/*.{jpg,jpeg,png}'); ?>
<?php define('IMAGES_INDEX', '1'); ?>
<?php define('TOP_DUMMY_PAGE_SLUG', 'home'); ?>
<?php define('STORAGE_PATH', dirname(__FILE__) . '/storage'); ?>
<?php

// Make it a xml file
header('content-type: application/xml; charset=UTF-8');

if (!defined('SITE_URL')) {
    define('SITE_URL', (empty($_SERVER['HTTPS']) ? 'http://' : 'https://') . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['REQUEST_URI']), '/') . '/');
}

/**
 *  サイト一覧を取得
 *
 *  @param  $dir サイトマップを作成する対象ディレクトリ
 *  @return array
 */
function get_sitemap_lists()
{
    $sitemap = array();
    $pages_file = file_get_contents(STORAGE_PATH . '/pages.json');
    $pages = json_decode($pages_file, true);

    foreach ($pages as $key => $page) {
        if ($key === TOP_DUMMY_PAGE_SLUG) {
            $key = '';
        }
        $hierarchy = substr_count($key, '/');
        $sitemap[$key] = array(
            'loc' => rtrim(SITE_URL, '/') . '/' . ($key ? $key . '/' : ''),
            'lastmod' => date('c', $page['date']),
            'changefreq' => ($hierarchy === 0 ? 'daily' : 'weekly'),
            'priority' => ($hierarchy === 0 ? '1.0' : '1.0' - ($hierarchy * 0.2))
        );

        $path = dirname(__FILE__) . '/' . $key;
        // 画像の登録
        if ((defined('IMAGES_INDEX') && IMAGES_INDEX == 1) && (defined('IMAGES_INDEX_TARGETS_FILE') && IMAGES_INDEX_TARGETS_FILE)) {
            foreach (glob($path . '/' . ltrim(IMAGES_INDEX_TARGETS_FILE, '/'), GLOB_BRACE) as $image_file) {
                $sitemap[$key]['image'][] = str_replace(dirname(__FILE__) . '/', SITE_URL, $image_file);
            }
        }
    }
    return $sitemap;
}

/**
 *  検索エンジン用のxmlサイトマップ
 *
 *  @param  $dir サイトマップを作成する対象ディレクトリ
 */
function put_sitemap_xml()
{
    $dom = new DOMDocument('1.0', 'UTF-8');
    $dom->preserveWhiteSpace = false;
    $dom->formatOutput = true;

    $urlset = $dom->createElement('urlset');
    $urlset->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
    $urlset->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
    $urlset->setAttribute('xmlns:mobile', 'http://www.google.com/schemas/sitemap-mobile/1.0');
    $urlset->setAttribute('xmlns:image', 'http://www.google.com/schemas/sitemap-image/1.1');
    $urlset->setAttribute('xsi:schemalocation', 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd');

    // 配列を[loc]キーで再配列
    $arr = get_sitemap_lists();
    $sort = array();
    foreach ((array) $arr as $key => $value) {
        $sort[$key] = $value['loc'];
    }
    array_multisort($sort, SORT_ASC, $arr);

    foreach ((array) $arr as $key) {
        $url = $urlset->appendChild($dom->createElement('url'));
        $url->appendChild($dom->createElement('loc', $key['loc']));
        $url->appendChild($dom->createElement('lastmod', $key['lastmod']));
        $url->appendChild($dom->createElement('changefreq', $key['changefreq']));
        $url->appendChild($dom->createElement('priority', $key['priority']));
        if (isset($key['image'])) {
            foreach ($key['image'] as $image_file) :
                $image = $url->appendChild($dom->createElement('image:image'));
                $image->appendChild($dom->createElement('image:loc', $image_file));
            endforeach;
        }
    }
    $dom->appendChild($urlset);

    // 書き出し
    echo $dom->saveXML();
}

put_sitemap_xml();
