sitemap.php 1.21 KB
Newer Older
Bostjan Skufca committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
<?php



/**
 * Import classes
 */
//use Symfony\Component\Console\Application;
use Teon\Sitemap\Generator\BruteForce\Generator;



/**
 * Bootstrap first
 */
require __DIR__.'/../app/bootstrap.php';



/**
 * Decide which format to throw out
 */
$requestUri = $_SERVER['REQUEST_URI'];

// Detect format
if (!preg_match('#/sitemap\.([^.]+)(\.gz)?$#', $requestUri, $m)) {
    throw new Exception("Unsupported sitemap format: $requestUri");
}
$format = $m[1];

// Detect compression
$compressed = false;
if (preg_match('#\.gz$#', $requestUri)) {
    $compressed = true;
}



/*
 * Generate in appropriate format
 */
$Generator = new Generator();
switch ($format) {
    case "txt":
        $contentType = "application/xml";
        $sitemapData = $Generator->generateTxt();
        break;
    case "xml":
        $contentType = "text/plain";
        $sitemapData = $Generator->generateXml();
        break;
    default:
        throw new Exception("Unsupported sitemap format: $format");
}



/**
 * Manage output
 */
if ($compressed) {
    header('Content-Type: application/gzip');
    $sitemapDataCompressed = gzencode($sitemapData);
    echo $sitemapDataCompressed;
} else {
    header("Content-Type: $contentType");
    echo $sitemapData;
}