sitemap.class.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. class Sitemap
  3. {
  4. public $modx;
  5. public $collected = [];
  6. public $ignore = [
  7. '.css',
  8. '.js',
  9. '.ico',
  10. '.jpg',
  11. '.png',
  12. '.jpeg',
  13. '.swf',
  14. '.gif',
  15. '.svg',
  16. '#',
  17. 'mailto:',
  18. 'tel:',
  19. 'skype:',
  20. 'javascript:',
  21. 'whatsapp:'
  22. ];
  23. public $url = '';
  24. public $asFile = false;
  25. public $saveFilePath = '';
  26. public function __construct(modX & $modx, $config = [])
  27. {
  28. $this->modx = &$modx;
  29. if (!$this->modx->loadClass('phpquery.phpQuery', MODX_CORE_PATH . 'components/googlesitemap/model/', true, true)) {
  30. return false;
  31. }
  32. $basePath = $this->modx->getOption('core_path') . 'components/googlesitemap/';
  33. $assetsUrl = $this->modx->getOption('assets_url') . 'components/googlesitemap/';
  34. $this->config = array_merge([
  35. 'basePath' => $basePath,
  36. 'corePath' => $basePath,
  37. 'modelPath' => $basePath . 'model/',
  38. 'processorsPath' => $basePath . 'processors/',
  39. 'templatesPath' => $basePath . 'templates/',
  40. 'chunksPath' => $basePath . 'elements/chunks/',
  41. 'jsUrl' => $assetsUrl . 'js/',
  42. 'cssUrl' => $assetsUrl . 'css/',
  43. 'assetsUrl' => $assetsUrl,
  44. 'connectorUrl' => $assetsUrl . 'connector.php',
  45. ], $config);
  46. $this->url = MODX_SITE_URL;
  47. }
  48. public function start()
  49. {
  50. $this->collected[] = $this->url;
  51. $this->load($this->url);
  52. }
  53. private function get($page)
  54. {
  55. $prepare = [];
  56. $document = phpQuery::newDocument($page);
  57. foreach ($document->find('a') as $node) {
  58. $node = pq($node);
  59. $href = $node->attr('href');
  60. if ($href and $href != '/') {
  61. if ($href[0] == '/') {
  62. $href = mb_substr($href, 1);
  63. }
  64. if (!in_array($this->url . $href, $this->collected) and !$this->validateUrl($href)) {
  65. if (strpos($href, 'https://') !== false or strpos($href, 'http://') !== false) {
  66. $prepare[] = $href;
  67. } else {
  68. $prepare[] = $this->url . $href;
  69. }
  70. }
  71. }
  72. }
  73. $document->unloadDocument();
  74. $this->collected = array_merge($this->collected, $prepare);
  75. foreach ($prepare as $link) {
  76. $this->load($link);
  77. }
  78. return true;
  79. }
  80. private function load($path)
  81. {
  82. $curl = curl_init($path);
  83. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  84. if (strpos($path, 'https://') !== false) {
  85. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  86. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
  87. }
  88. $page = curl_exec($curl);
  89. if (curl_errno($curl)) {
  90. exit('Load error: ' . curl_error($curl));
  91. }
  92. curl_close($curl);
  93. $this->get($page);
  94. }
  95. private function validateUrl($link)
  96. {
  97. if (strpos($link, 'https://') !== false or strpos($link, 'http://') !== false) {
  98. if (strpos($link, $this->url) === false) {
  99. return true;
  100. }
  101. }
  102. foreach ($this->ignore as $value) {
  103. if (strpos($link, $value) !== false) {
  104. return true;
  105. }
  106. }
  107. }
  108. public function save()
  109. {
  110. $xml = new DomDocument('1.0', 'utf-8');
  111. $urlset = $xml->appendChild($xml->createElement('urlset'));
  112. $urlset->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
  113. foreach ($this->collected as $link) {
  114. $url = $urlset->appendChild($xml->createElement('url'));
  115. $loc = $url->appendChild($xml->createElement('loc'));
  116. $lastmod = $url->appendChild($xml->createElement('lastmod'));
  117. $loc->appendChild($xml->createTextNode(htmlentities($link)));
  118. $lastmod->appendChild($xml->createTextNode(date('Y-m-d')));
  119. }
  120. $xml->formatOutput = true;
  121. if ($this->asFile == true and $this->saveFilePath) {
  122. $xml->save($this->saveFilePath);
  123. return true;
  124. }
  125. return $xml->saveXML();
  126. }
  127. }