You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
information-system/application/controllers/cache.php

189 lines
6.6 KiB
PHTML

8 years ago
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Cache extends CI_Controller
{
//缓存文件绝对路径
private $dir = '';
//存放缓存文件的数组
private $file = array();
//存放缓存文件夹的数组
private $path = array();
//缓存更新接口
private $cache_api = '';
//缓存更新参数
private $post_para = '';
//当前站点域名
private $current_domain = '';
//文件更新日期
private $file_time = array();
8 years ago
function __construct()
{
parent::__construct();
$this->permission->is_admin();
$this->load->model('Area_model');
$this->load->model('InfoStructures_model');
$this->load->model('Information_model');
4 years ago
$this->site_code = $this->input->get('site_code') || $this->config->item('site_code');
8 years ago
}
8 years ago
/**
*
* 公有函数:扫描已有缓存文件并更新。
*
* 必要参数:
* @param String $site_code
*
*/
8 years ago
public function update()
{
//设置缓存文件文件夹
$cache_config = $this->config->item('cache');
$current_cache_config = $cache_config[$this->site_code];
$this->dir = $current_cache_config['cache_path'];
8 years ago
//设置当前站点
$this->current_domain = $this->config->item('site_url');
//echo $this->config->item('site_url');
//设置缓存更新接口及POST参数
$this->cache_api = $current_cache_config['cache_api'];
$this->post_para = $current_cache_config['cache_api_para'];
//遍历缓存文件夹
$this->tree($this->dir, $this->file, $this->path, $this->file_time);
5 years ago
//print_r($this->file);
//print_r($this->path);
//print_r($this->file_time);
//die();
8 years ago
//按目录筛选结果
$this->filter($this->file, $this->path);
8 years ago
//整理需要传递到视图的数据
$data['file'] = $this->file;
$data['path'] = $this->path;
$data['file_time'] = $this->file_time;
$data['cache_api'] = $this->cache_api;
$data['post_para'] = $this->post_para;
8 years ago
//视图
$this->load->view('cache/update', $data);
}
8 years ago
/**
*
* 公有函数:扫描已有缓存文件并更新。
*
* 必要参数:
* @param String $site_code
*
*/
8 years ago
public function sitemap()
{
header("Content-type:text/xml");
//设置缓存文件文件夹
$cache_config = $this->config->item('cache');
$current_cache_config = $cache_config[$this->site_code];
$this->dir = $current_cache_config['cache_path'];
8 years ago
//设置当前站点
$this->current_domain = $this->config->item('site_url');
//设置缓存更新接口及POST参数
$this->cache_api = $current_cache_config['cache_api'];
$this->post_para = $current_cache_config['cache_api_para'];
//遍历缓存文件夹
$this->tree($this->dir, $this->file, $this->path, $this->file_time);
//按目录筛选结果
$this->filter($this->file, $this->path);
8 years ago
//整理需要传递到视图的数据
$data['file'] = $this->file;
$data['path'] = $this->path;
$data['file_time'] = $this->file_time;
$data['cache_api'] = $this->cache_api;
$data['post_para'] = $this->post_para;
//排序file数组
sort($data['file']);
8 years ago
//生成sitemap
$dom = new DomDocument('1.0', 'utf-8');
$urlset = $dom->createElement('urlset');
$urlset->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
$dom->appendchild($urlset);
foreach ($data['file'] as $f) {
8 years ago
$url = $dom->createElement('url');
$loc = $dom->createElement('loc');
$text = $dom->createTextNode($f);
$loc->appendchild($text);
$url->appendchild($loc);
$urlset->appendchild($url);
}
8 years ago
echo ($dom->saveXML());
8 years ago
}
/**
*
* 私有函数:递归遍历缓存文件夹,将目录存放到$path文件存放到$file。
*
* 必要参数:
* @param String $dir - 需要遍历的目录
* @param Array $file - 存放文件结果的数组引用
* @param Array $path - 存放路径结果的数组引用
* @param Array $file_time - 存放文件更新日期的数组引用
*
*/
8 years ago
private function tree($dir, &$file, &$path, &$file_time)
{
$mydir = dir($dir);
while ($f = $mydir->read()) {
if (is_dir("$dir/$f") && $f != "." && $f != ".." && (strpos($dir, '/cn/') === false) && (strpos($dir, '/amp/') === false) && (strpos($dir, '/js/') === false)) {
8 years ago
$path[] = "$dir/$f";
$this->tree("$dir/$f", $file, $path, $file_time);
} else {
if ($f != '.' && $f != '..' && (strpos($f, '/cn/') === false) && (strpos($f, '/amp/') === false) && (strpos($f, '.pdf') === false) && (strpos($f, '.mobile.htm') === false) && (strpos($f, '/js/') === false) && (strpos($f, '.amp') === false)) {
$file_temp = str_replace('/index.htm###', '', $f . '###');
5 years ago
$file_temp = str_replace('###', '', $file_temp);
5 years ago
$path_temp = str_replace($this->dir, '', $dir);
$url_temp = $this->current_domain . $path_temp . '/' . $file_temp;
5 years ago
$url_temp = str_replace('index.htm', '', $url_temp);
8 years ago
$file_time[$url_temp] = date("F d Y H:i:s", filemtime("$dir/$f"));
$file[] = $url_temp;
}
}
}
$mydir->close();
}
8 years ago
/**
*
* 私有函数:筛选目录和文件。
*
* 必要参数:
* @param Array $file - 存放文件结果的数组引用
* @param Array $path - 存放路径结果的数组引用
*
* 可选参数POST参数 - $_POST['p']
*
*/
private function filter(&$file, &$path)
{
if (isset($_POST['p']) && !empty($_POST['p'])) {
8 years ago
//删选文件
foreach ($file as &$f) {
(stripos($f, $_POST['p']) !== false) or $f = false;
8 years ago
}
$file = array_filter($file);
//删选目录
foreach ($path as &$p) {
(stripos($p, $_POST['p']) !== false) or $p = false;
8 years ago
}
$path = array_filter($path);
}
}
}
//end of Cache