2
0

Sitemap.php 15 KB

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