Automad
 All Classes Functions Variables Pages
sitemap.php
1 <?php
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  * AUTOMAD
29  *
30  * Copyright (c) 2014 by Marc Anton Dahmen
31  * http://marcdahmen.de
32  *
33  * Licensed under the MIT license.
34  * http://automad.org/license
35  */
36 
37 
38 namespace Automad\Core;
39 
40 
41 defined('AUTOMAD') or die('Direct access not permitted!');
42 
43 
52 class Sitemap {
53 
54 
61  public function __construct($collection) {
62 
63  // Skip sitemap for Proxies.
64  if (!isset($_SERVER['HTTP_X_FORWARDED_HOST']) && !isset($_SERVER['HTTP_X_FORWARDED_SERVER'])) {
65 
66  $sitemap = AM_BASE_DIR . '/sitemap.xml';
67 
68  // If the base dir is writable without having a sitemap.xml or if sitemap.xml exists and is writable itself.
69  if ((is_writable(AM_BASE_DIR) && !file_exists($sitemap)) || is_writable($sitemap)) {
70  $this->generate($collection, $sitemap);
71  } else {
72  Debug::log('Permissions denied!');
73  }
74 
75  } else {
76  Debug::log('Skipped generating sitemap.xml! (Proxy)');
77  }
78 
79  }
80 
81 
89  private function generate($collection, $sitemap) {
90 
91  $xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
92  '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
93 
94  foreach ($collection as $Page) {
95  // Only include "real" URLs and not aliases.
96  if (strpos($Page->url, '/') === 0) {
97  $xml .= '<url><loc>http://' . $_SERVER['SERVER_NAME'] . AM_BASE_URL . AM_INDEX . $Page->url . '</loc></url>' . "\n";
98  }
99  }
100 
101  $xml .= '</urlset>';
102 
103  if (@file_put_contents($sitemap, $xml)) {
104  Debug::log('Successfully generated "' . $sitemap . '"');
105  }
106 
107  }
108 
109 
110 }
111 
112 ?>
static log($element, $description= '')
Definition: debug.php:113
generate($collection, $sitemap)
Definition: sitemap.php:89
__construct($collection)
Definition: sitemap.php:61