sitemap.php 3.72 KB
<?
define('IS_AJAX', true);
define('STOP_STATISTICS', true);
define('NO_AGENT_CHECK', true);
define('DisableEventsCheck', true);
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");

$basePath = $_SERVER['DOCUMENT_ROOT'];
$indexSiteMapPath = $basePath . '/sitemap.xml';

header ("Content-Type:text/xml");
try {
    main($indexSiteMapPath, $basePath);
} catch (Exception $e) {
    echo file_get_contents($basePath . $indexSiteMapPath);
}

function main($indexSiteMapPath, $basePath)
{
    $arSiteMapsPath = getSiteMapsPaths($indexSiteMapPath);

    $arLoc = array();
    foreach ($arSiteMapsPath as $filename) {
        $arLoc[] = getLinksFromFilename($filename, $basePath);
    }
    $arLoc = joinUniqueLoc($arLoc);

    $arLoc = addImagesInfo($arLoc);

    $xml = createXml($arLoc);
    showXml($xml);
}

function getSiteMapsPaths($indexSiteMapPath)
{
    $result = array();

    $ob = simplexml_load_file($indexSiteMapPath);
    if(!$ob) throw new Exception();
    if(empty($ob->sitemap)) throw new Exception();
    foreach ($ob->sitemap as $item) {
        $result[] = removeDomainFromUrl((String)$item->loc);
    }

    return $result;
}

function removeDomainFromUrl($url)
{
    $arPath = parse_url($url);
    return $arPath['path'];
}

function getLinksFromFilename($filename, $basePath)
{
    $result = array();

    $ob = simplexml_load_file($basePath . $filename);
    if(!$ob) throw new Exception();
    foreach ($ob->url as $item) {
        $result[] = array('loc' => (String)$item->loc, 'lastmod' => (String)$item->lastmod);
    }

    return $result;
}

function joinUniqueLoc($arLoc)
{
    $result = array();
    $links = array();

    foreach($arLoc as $locs) {
        foreach($locs as $loc) {
            $url = $loc['loc'];
            if(isset($links[$url])) continue;

            $links[$url] = true;
            $result[] = $loc;
        }
    }

    return $result;
}

function createXml($arLoc)
{
    $urlset = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><urlset/>');
    $urlset->addAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
    $urlset->addAttribute('xmlns-image', 'http://www.google.com/schemas/sitemap-image/1.1');

    foreach($arLoc as $loc) {
        $url = $urlset->addChild('url');
        $url->addChild('loc', $loc['loc']);
        $url->addChild('lastmod', $loc['lastmod']);
        if($loc['images']) {
            foreach($loc['images'] as $img) {
                $image = $url->addChild('image-image');
                $image->addChild('image-loc', $img['src']);
                if($img['alt']) {
                    $image->addChild('image-caption', $img['alt']);
                }
            }
        }
    }

    return $urlset;
}

function showXml($xml)
{
    echo str_replace(
        array('image-image>', 'image-loc', 'image-caption', 'xmlns-image'),
        array('image:image>', 'image:loc', 'image:caption', 'xmlns:image'),
        $xml->asXml()
    );
}

function addImagesInfo($arLoc)
{
    $sitePrefix = 'http://' . SITE_SERVER_NAME;
    foreach($arLoc as $i => $loc) {
        $url = str_replace($sitePrefix, '', $loc['loc']);

        $pathCurImageInfo = getImagesInfoPathByUrl($url);
        if(file_exists($pathCurImageInfo)) {
            $images = json_decode(file_get_contents($pathCurImageInfo), true);

            if(is_array($images) && $images) {
                foreach($images as $image) {
                    if(!$image['src']) continue;

                    $image['src'] = $sitePrefix . $image['src'];
                    $arLoc[$i]['images'][] = $image;
                }
            }
        }
    }

    return $arLoc;
}