sitemap.php
3.72 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?
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;
}