控制台应用,yzncms本身基于tp5.1框架,里面的队列用不了,bug,坑
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Sitemap.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | sitemap类
  13. // +----------------------------------------------------------------------
  14. namespace addons\sitemap\library;
  15. class Sitemap
  16. {
  17. //类定义开始
  18. private $config = array(
  19. 'encoding' => 'UTF-8',
  20. 'ver' => '1.0',
  21. );
  22. private $content = '';
  23. // Items部分
  24. private $items = array();
  25. public function __get($name)
  26. {
  27. if (isset($this->config[$name])) {
  28. return $this->config[$name];
  29. }
  30. return null;
  31. }
  32. public function __set($name, $value)
  33. {
  34. if (isset($this->config[$name])) {
  35. $this->config[$name] = $value;
  36. }
  37. }
  38. public function __isset($name)
  39. {
  40. return isset($this->config[$name]);
  41. }
  42. public function content($name)
  43. {
  44. if (empty($this->content)) {
  45. $this->Build();
  46. }
  47. $this->content;
  48. }
  49. /**
  50. * 架构函数
  51. * @access public
  52. * @param array $config 上传参数
  53. */
  54. public function __construct()
  55. {
  56. }
  57. /* * *********************************************************************** */
  58. // 函数名: AddItem
  59. // 功能: 添加一个节点
  60. //$changefreq | always 经常,hourly 每小时,daily 每天,weekly 每周,monthly 每月,yearly 每年,never 从不
  61. //$mobile | mobile 跳转适配, htmladapt 代码适应, pc,mobile 自适应
  62. /* * *********************************************************************** */
  63. public function AddItem($loc, $priority, $changefreq = 'Always', $time = 0, $mobile = "pc,mobile")
  64. {
  65. $arr = array(
  66. '1.0',
  67. '0.9',
  68. '0.8',
  69. '0.7',
  70. '0.6',
  71. '0.5',
  72. );
  73. $this->items[] = array(
  74. 'loc' => $loc,
  75. 'priority' => $arr[$priority],
  76. 'lastmod' => $time ? (is_numeric($time) ? date('Y-m-d H:i:s', $time) : $time) : date('Y-m-d H:i:s', time()),
  77. 'changefreq' => $changefreq,
  78. 'mobile' => $mobile,
  79. );
  80. }
  81. /* * *********************************************************************** */
  82. // 函数名: Build
  83. // 功能: 生成sitemap xml文件内容
  84. /* * *********************************************************************** */
  85. public function Build()
  86. {
  87. $s = "<?xml version='1.0' encoding='{$this->encoding}'?>\r\n";
  88. /* $s .= "<?xml-stylesheet type='text/xsl' href='sitemap.xsl'?>\r\n";*/
  89. $s .= "\t<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>\r\n";
  90. // items
  91. for ($i = 0; $i < count($this->items); $i++) {
  92. $s .= "\t\t<url>\n";
  93. $s .= "\t\t\t<loc>{$this->items[$i]['loc']}</loc>\r\n";
  94. $s .= "\t\t\t<priority>{$this->items[$i]['priority']}</priority>\r\n";
  95. $s .= "\t\t\t<lastmod>{$this->items[$i]['lastmod']}</lastmod>\r\n";
  96. $s .= "\t\t\t<changefreq>{$this->items[$i]['changefreq']}</changefreq>\r\n";
  97. /*$s .= "\t\t\t<mobile:mobile type=\"{$this->items[$i]['mobile']}\"/>\r\n";*/
  98. $s .= "\t\t</url>\n";
  99. }
  100. // close
  101. $s .= "\t</urlset>";
  102. $this->content = $s;
  103. }
  104. /* * *********************************************************************** */
  105. // 函数名: Show
  106. // 功能: 将产生的sitemap内容直接打印输出
  107. /* * *********************************************************************** */
  108. public function Show()
  109. {
  110. if (empty($this->content)) {
  111. $this->Build();
  112. }
  113. header("Content-Type: text/xml; charset=utf-8");
  114. echo ($this->content);
  115. }
  116. /* * *********************************************************************** */
  117. // 函数名: SaveToFile
  118. // 功能: 将产生的sitemap 内容保存到文件
  119. // 参数: $fname 要保存的文件名
  120. /* * *********************************************************************** */
  121. public function SaveToFile($fname)
  122. {
  123. if (empty($this->content)) {
  124. $this->Build();
  125. }
  126. $handle = fopen($fname, 'w+');
  127. if ($handle === false) {
  128. return false;
  129. }
  130. fwrite($handle, $this->content);
  131. fclose($handle);
  132. }
  133. /* * *********************************************************************** */
  134. // 函数名: getFile
  135. // 功能: 从文件中获取输出
  136. // 参数: $fname 文件名
  137. /* * *********************************************************************** */
  138. public function getFile($fname)
  139. {
  140. $handle = fopen($fname, 'r');
  141. if ($handle === false) {
  142. return false;
  143. }
  144. while (!feof($handle)) {
  145. echo fgets($handle);
  146. }
  147. fclose($handle);
  148. }
  149. }