123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- // +----------------------------------------------------------------------
- // | Yzncms [ 御宅男工作室 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2018 http://yzncms.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: 御宅男 <530765310@qq.com>
- // +----------------------------------------------------------------------
-
- // +----------------------------------------------------------------------
- // | sitemap类
- // +----------------------------------------------------------------------
- namespace addons\sitemap\library;
-
- class Sitemap
- {
- //类定义开始
-
- private $config = array(
- 'encoding' => 'UTF-8',
- 'ver' => '1.0',
- );
- private $content = '';
- // Items部分
- private $items = array();
-
- public function __get($name)
- {
- if (isset($this->config[$name])) {
- return $this->config[$name];
- }
- return null;
- }
-
- public function __set($name, $value)
- {
- if (isset($this->config[$name])) {
- $this->config[$name] = $value;
- }
- }
-
- public function __isset($name)
- {
- return isset($this->config[$name]);
- }
-
- public function content($name)
- {
- if (empty($this->content)) {
- $this->Build();
- }
-
- $this->content;
- }
-
- /**
- * 架构函数
- * @access public
- * @param array $config 上传参数
- */
- public function __construct()
- {
-
- }
-
- /* * *********************************************************************** */
-
- // 函数名: AddItem
- // 功能: 添加一个节点
- //$changefreq | always 经常,hourly 每小时,daily 每天,weekly 每周,monthly 每月,yearly 每年,never 从不
- //$mobile | mobile 跳转适配, htmladapt 代码适应, pc,mobile 自适应
- /* * *********************************************************************** */
-
- public function AddItem($loc, $priority, $changefreq = 'Always', $time = 0, $mobile = "pc,mobile")
- {
- $arr = array(
- '1.0',
- '0.9',
- '0.8',
- '0.7',
- '0.6',
- '0.5',
- );
- $this->items[] = array(
- 'loc' => $loc,
- 'priority' => $arr[$priority],
- 'lastmod' => $time ? (is_numeric($time) ? date('Y-m-d H:i:s', $time) : $time) : date('Y-m-d H:i:s', time()),
- 'changefreq' => $changefreq,
- 'mobile' => $mobile,
- );
- }
-
- /* * *********************************************************************** */
-
- // 函数名: Build
- // 功能: 生成sitemap xml文件内容
- /* * *********************************************************************** */
- public function Build()
- {
- $s = "<?xml version='1.0' encoding='{$this->encoding}'?>\r\n";
- /* $s .= "<?xml-stylesheet type='text/xsl' href='sitemap.xsl'?>\r\n";*/
- $s .= "\t<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>\r\n";
- // items
- for ($i = 0; $i < count($this->items); $i++) {
- $s .= "\t\t<url>\n";
- $s .= "\t\t\t<loc>{$this->items[$i]['loc']}</loc>\r\n";
- $s .= "\t\t\t<priority>{$this->items[$i]['priority']}</priority>\r\n";
- $s .= "\t\t\t<lastmod>{$this->items[$i]['lastmod']}</lastmod>\r\n";
- $s .= "\t\t\t<changefreq>{$this->items[$i]['changefreq']}</changefreq>\r\n";
- /*$s .= "\t\t\t<mobile:mobile type=\"{$this->items[$i]['mobile']}\"/>\r\n";*/
- $s .= "\t\t</url>\n";
- }
- // close
- $s .= "\t</urlset>";
- $this->content = $s;
- }
-
- /* * *********************************************************************** */
-
- // 函数名: Show
- // 功能: 将产生的sitemap内容直接打印输出
- /* * *********************************************************************** */
- public function Show()
- {
- if (empty($this->content)) {
- $this->Build();
- }
-
- header("Content-Type: text/xml; charset=utf-8");
- echo ($this->content);
- }
-
- /* * *********************************************************************** */
-
- // 函数名: SaveToFile
- // 功能: 将产生的sitemap 内容保存到文件
- // 参数: $fname 要保存的文件名
- /* * *********************************************************************** */
- public function SaveToFile($fname)
- {
- if (empty($this->content)) {
- $this->Build();
- }
-
- $handle = fopen($fname, 'w+');
- if ($handle === false) {
- return false;
- }
-
- fwrite($handle, $this->content);
- fclose($handle);
- }
-
- /* * *********************************************************************** */
-
- // 函数名: getFile
- // 功能: 从文件中获取输出
- // 参数: $fname 文件名
- /* * *********************************************************************** */
- public function getFile($fname)
- {
- $handle = fopen($fname, 'r');
- if ($handle === false) {
- return false;
- }
-
- while (!feof($handle)) {
- echo fgets($handle);
- }
- fclose($handle);
- }
-
- }
|