Sitemap.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage Helper
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @see Zend_View_Helper_Navigation_HelperAbstract
  23. */
  24. require_once 'Zend/View/Helper/Navigation/HelperAbstract.php';
  25. /**
  26. * Helper for printing sitemaps
  27. *
  28. * @link http://www.sitemaps.org/protocol.php
  29. *
  30. * @category Zend
  31. * @package Zend_View
  32. * @subpackage Helper
  33. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_View_Helper_Navigation_Sitemap
  37. extends Zend_View_Helper_Navigation_HelperAbstract
  38. {
  39. /**
  40. * Namespace for the <urlset> tag
  41. *
  42. * @var string
  43. */
  44. const SITEMAP_NS = 'http://www.sitemaps.org/schemas/sitemap/0.9';
  45. /**
  46. * Schema URL
  47. *
  48. * @var string
  49. */
  50. const SITEMAP_XSD = 'http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd';
  51. /**
  52. * Whether XML output should be formatted
  53. *
  54. * @var bool
  55. */
  56. protected $_formatOutput = false;
  57. /**
  58. * Whether the XML declaration should be included in XML output
  59. *
  60. * @var bool
  61. */
  62. protected $_useXmlDeclaration = true;
  63. /**
  64. * Whether sitemap should be validated using Zend_Validate_Sitemap_*
  65. *
  66. * @var bool
  67. */
  68. protected $_useSitemapValidators = true;
  69. /**
  70. * Whether sitemap should be schema validated when generated
  71. *
  72. * @var bool
  73. */
  74. protected $_useSchemaValidation = false;
  75. /**
  76. * Server url
  77. *
  78. * @var string
  79. */
  80. protected $_serverUrl;
  81. /**
  82. * View helper entry point:
  83. * Retrieves helper and optionally sets container to operate on
  84. *
  85. * @param Zend_Navigation_Container $container [optional] container to
  86. * operate on
  87. * @return Zend_View_Helper_Navigation_Sitemap fluent interface, returns
  88. * self
  89. */
  90. public function sitemap(Zend_Navigation_Container $container = null)
  91. {
  92. if (null !== $container) {
  93. $this->setContainer($container);
  94. }
  95. return $this;
  96. }
  97. // Accessors:
  98. /**
  99. * Sets whether XML output should be formatted
  100. *
  101. * @param bool $formatOutput [optional] whether output
  102. * should be formatted. Default
  103. * is true.
  104. * @return Zend_View_Helper_Navigation_Sitemap fluent interface, returns
  105. * self
  106. */
  107. public function setFormatOutput($formatOutput = true)
  108. {
  109. $this->_formatOutput = (bool) $formatOutput;
  110. return $this;
  111. }
  112. /**
  113. * Returns whether XML output should be formatted
  114. *
  115. * @return bool whether XML output should be formatted
  116. */
  117. public function getFormatOutput()
  118. {
  119. return $this->_formatOutput;
  120. }
  121. /**
  122. * Sets whether the XML declaration should be used in output
  123. *
  124. * @param bool $useXmlDecl whether XML delcaration
  125. * should be rendered
  126. * @return Zend_View_Helper_Navigation_Sitemap fluent interface, returns
  127. * self
  128. */
  129. public function setUseXmlDeclaration($useXmlDecl)
  130. {
  131. $this->_useXmlDeclaration = (bool) $useXmlDecl;
  132. return $this;
  133. }
  134. /**
  135. * Returns whether the XML declaration should be used in output
  136. *
  137. * @return bool whether the XML declaration should be used in output
  138. */
  139. public function getUseXmlDeclaration()
  140. {
  141. return $this->_useXmlDeclaration;
  142. }
  143. /**
  144. * Sets whether sitemap should be validated using Zend_Validate_Sitemap_*
  145. *
  146. * @param bool $useSitemapValidators whether sitemap validators
  147. * should be used
  148. * @return Zend_View_Helper_Navigation_Sitemap fluent interface, returns
  149. * self
  150. */
  151. public function setUseSitemapValidators($useSitemapValidators)
  152. {
  153. $this->_useSitemapValidators = (bool) $useSitemapValidators;
  154. return $this;
  155. }
  156. /**
  157. * Returns whether sitemap should be validated using Zend_Validate_Sitemap_*
  158. *
  159. * @return bool whether sitemap should be validated using validators
  160. */
  161. public function getUseSitemapValidators()
  162. {
  163. return $this->_useSitemapValidators;
  164. }
  165. /**
  166. * Sets whether sitemap should be schema validated when generated
  167. *
  168. * @param bool $schemaValidation whether sitemap should
  169. * validated using XSD Schema
  170. * @return Zend_View_Helper_Navigation_Sitemap fluent interface, returns
  171. * self
  172. */
  173. public function setUseSchemaValidation($schemaValidation)
  174. {
  175. $this->_useSchemaValidation = (bool) $schemaValidation;
  176. return $this;
  177. }
  178. /**
  179. * Returns true if sitemap should be schema validated when generated
  180. *
  181. * @return bool
  182. */
  183. public function getUseSchemaValidation()
  184. {
  185. return $this->_useSchemaValidation;
  186. }
  187. /**
  188. * Sets server url (scheme and host-related stuff without request URI)
  189. *
  190. * E.g. http://www.example.com
  191. *
  192. * @param string $serverUrl server URL to set (only
  193. * scheme and host)
  194. * @throws Zend_Uri_Exception if invalid server URL
  195. * @return Zend_View_Helper_Navigation_Sitemap fluent interface, returns
  196. * self
  197. */
  198. public function setServerUrl($serverUrl)
  199. {
  200. require_once 'Zend/Uri.php';
  201. $uri = Zend_Uri::factory($serverUrl);
  202. $uri->setFragment('');
  203. $uri->setPath('');
  204. $uri->setQuery('');
  205. if ($uri->valid()) {
  206. $this->_serverUrl = $uri->getUri();
  207. } else {
  208. require_once 'Zend/Uri/Exception.php';
  209. throw new Zend_Uri_Exception(sprintf(
  210. 'Invalid server URL: "%s"',
  211. $serverUrl));
  212. }
  213. return $this;
  214. }
  215. /**
  216. * Returns server URL
  217. *
  218. * @return string server URL
  219. */
  220. public function getServerUrl()
  221. {
  222. if (!isset($this->_serverUrl)) {
  223. $this->_serverUrl = $this->view->serverUrl();
  224. }
  225. return $this->_serverUrl;
  226. }
  227. // Helper methods:
  228. /**
  229. * Escapes string for XML usage
  230. *
  231. * @param string $string string to escape
  232. * @return string escaped string
  233. */
  234. protected function _xmlEscape($string)
  235. {
  236. // TODO: remove check when minimum PHP version is >= 5.2.3
  237. if (version_compare(PHP_VERSION, '5.2.3', '>=')) {
  238. // do not encode existing HTML entities
  239. return htmlspecialchars($string, ENT_QUOTES, 'UTF-8', false);
  240. } else {
  241. $string = preg_replace('/&(?!(?:#\d++|[a-z]++);)/ui', '&amp;', $string);
  242. $string = str_replace(array('<', '>', '\'', '"'), array('&lt;', '&gt;', '&#39;', '&quot;'), $string);
  243. return $string;
  244. }
  245. }
  246. // Public methods:
  247. /**
  248. * Returns an escaped absolute URL for the given page
  249. *
  250. * @param Zend_Navigation_Page $page page to get URL from
  251. * @return string
  252. */
  253. public function url(Zend_Navigation_Page $page)
  254. {
  255. $href = $page->getHref();
  256. if (!isset($href{0})) {
  257. // no href
  258. return '';
  259. } elseif ($href{0} == '/') {
  260. // href is relative to root; use serverUrl helper
  261. $url = $this->getServerUrl() . $href;
  262. } elseif (preg_match('/^[a-z]+:/im', (string) $href)) {
  263. // scheme is given in href; assume absolute URL already
  264. $url = (string) $href;
  265. } else {
  266. // href is relative to current document; use url helpers
  267. $url = $this->getServerUrl()
  268. . rtrim($this->view->url(), '/') . '/'
  269. . $href;
  270. }
  271. return $this->_xmlEscape($url);
  272. }
  273. /**
  274. * Returns a DOMDocument containing the Sitemap XML for the given container
  275. *
  276. * @param Zend_Navigation_Container $container [optional] container to get
  277. * breadcrumbs from, defaults
  278. * to what is registered in the
  279. * helper
  280. * @return DOMDocument DOM representation of the
  281. * container
  282. * @throws Zend_View_Exception if schema validation is on
  283. * and the sitemap is invalid
  284. * according to the sitemap
  285. * schema, or if sitemap
  286. * validators are used and the
  287. * loc element fails validation
  288. */
  289. public function getDomSitemap(Zend_Navigation_Container $container = null)
  290. {
  291. if (null === $container) {
  292. $container = $this->getContainer();
  293. }
  294. // check if we should validate using our own validators
  295. if ($this->getUseSitemapValidators()) {
  296. require_once 'Zend/Validate/Sitemap/Changefreq.php';
  297. require_once 'Zend/Validate/Sitemap/Lastmod.php';
  298. require_once 'Zend/Validate/Sitemap/Loc.php';
  299. require_once 'Zend/Validate/Sitemap/Priority.php';
  300. // create validators
  301. $locValidator = new Zend_Validate_Sitemap_Loc();
  302. $lastmodValidator = new Zend_Validate_Sitemap_Lastmod();
  303. $changefreqValidator = new Zend_Validate_Sitemap_Changefreq();
  304. $priorityValidator = new Zend_Validate_Sitemap_Priority();
  305. }
  306. // create document
  307. $dom = new DOMDocument('1.0', 'UTF-8');
  308. $dom->formatOutput = $this->getFormatOutput();
  309. // ...and urlset (root) element
  310. $urlSet = $dom->createElementNS(self::SITEMAP_NS, 'urlset');
  311. $dom->appendChild($urlSet);
  312. // create iterator
  313. $iterator = new RecursiveIteratorIterator($container,
  314. RecursiveIteratorIterator::SELF_FIRST);
  315. $maxDepth = $this->getMaxDepth();
  316. if (is_int($maxDepth)) {
  317. $iterator->setMaxDepth($maxDepth);
  318. }
  319. $minDepth = $this->getMinDepth();
  320. if (!is_int($minDepth) || $minDepth < 0) {
  321. $minDepth = 0;
  322. }
  323. // iterate container
  324. foreach ($iterator as $page) {
  325. if ($iterator->getDepth() < $minDepth || !$this->accept($page)) {
  326. // page should not be included
  327. continue;
  328. }
  329. // get absolute url from page
  330. if (!$url = $this->url($page)) {
  331. // skip page if it has no url (rare case)
  332. continue;
  333. }
  334. // create url node for this page
  335. $urlNode = $dom->createElementNS(self::SITEMAP_NS, 'url');
  336. $urlSet->appendChild($urlNode);
  337. if ($this->getUseSitemapValidators() &&
  338. !$locValidator->isValid($url)) {
  339. require_once 'Zend/View/Exception.php';
  340. throw new Zend_View_Exception(sprintf(
  341. 'Encountered an invalid URL for Sitemap XML: "%s"',
  342. $url));
  343. }
  344. // put url in 'loc' element
  345. $urlNode->appendChild($dom->createElementNS(self::SITEMAP_NS,
  346. 'loc', $url));
  347. // add 'lastmod' element if a valid lastmod is set in page
  348. if (isset($page->lastmod)) {
  349. $lastmod = strtotime((string) $page->lastmod);
  350. // prevent 1970-01-01...
  351. if ($lastmod !== false) {
  352. $lastmod = date('c', $lastmod);
  353. }
  354. if (!$this->getUseSitemapValidators() ||
  355. $lastmodValidator->isValid($lastmod)) {
  356. $urlNode->appendChild(
  357. $dom->createElementNS(self::SITEMAP_NS, 'lastmod',
  358. $lastmod)
  359. );
  360. }
  361. }
  362. // add 'changefreq' element if a valid changefreq is set in page
  363. if (isset($page->changefreq)) {
  364. $changefreq = $page->changefreq;
  365. if (!$this->getUseSitemapValidators() ||
  366. $changefreqValidator->isValid($changefreq)) {
  367. $urlNode->appendChild(
  368. $dom->createElementNS(self::SITEMAP_NS, 'changefreq',
  369. $changefreq)
  370. );
  371. }
  372. }
  373. // add 'priority' element if a valid priority is set in page
  374. if (isset($page->priority)) {
  375. $priority = $page->priority;
  376. if (!$this->getUseSitemapValidators() ||
  377. $priorityValidator->isValid($priority)) {
  378. $urlNode->appendChild(
  379. $dom->createElementNS(self::SITEMAP_NS, 'priority',
  380. $priority)
  381. );
  382. }
  383. }
  384. }
  385. // validate using schema if specified
  386. if ($this->getUseSchemaValidation()) {
  387. if (!@$dom->schemaValidate(self::SITEMAP_XSD)) {
  388. require_once 'Zend/View/Exception.php';
  389. throw new Zend_View_Exception(sprintf(
  390. 'Sitemap is invalid according to XML Schema at "%s"',
  391. self::SITEMAP_XSD));
  392. }
  393. }
  394. return $dom;
  395. }
  396. // Zend_View_Helper_Navigation_Helper:
  397. /**
  398. * Renders helper
  399. *
  400. * Implements {@link Zend_View_Helper_Navigation_Helper::render()}.
  401. *
  402. * @param Zend_Navigation_Container $container [optional] container to
  403. * render. Default is to
  404. * render the container
  405. * registered in the helper.
  406. * @return string helper output
  407. */
  408. public function render(Zend_Navigation_Container $container = null)
  409. {
  410. $dom = $this->getDomSitemap($container);
  411. $xml = $this->getUseXmlDeclaration() ?
  412. $dom->saveXML() :
  413. $dom->saveXML($dom->documentElement);
  414. return rtrim($xml, PHP_EOL);
  415. }
  416. }