dzadsod/admin/backups.class.php

196 lines
5.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
defined('IN_MET') or exit ('No permission');
load::own_class('appadmin');
load::own_class('../traits/tdata');
load::own_class('../traits/tlist');
load::own_class('../traits/tfield');
//表格
class backups extends appadmin {
//备份字段
private $bsign = true;
//列表
private $list = [];
//当前执行表名
private $sqlk;
//form数组
private $form = [];
//info_on 快速URL
private $own_name_info;
private $bacname = ''; //默认开头
private $csvname = ''; //文件名
private $title = []; //表格标头
private $keys = []; //表格标头对应的键值
private $bactable = ''; //数据库名
private $bacconds = ''; //查询项以及排序
private $bacfield = ''; //查询字段
private $bacpages = 500; //每页查询条数
public function __construct() {
global $_M,$_YW;
parent::__construct();
$this->form = $_M['form'];
if($this->form['tname']){
$this->tname = $this->form['tname'];
$this->sqlk = $_YW['k'][$this->tname];
}
$this->own_name_info = $_M['url']['own_name'].'c=info_on&a=do';
}
use tdata;
use tfield;
use tlist;
//表格数据
public function doindex() {
global $_M;
self::csvtitle();
$this->bacname = '广告位';
self::csvname();
//输出头部
header("Content-type:text/csv");
header("Content-Disposition:attachment;filename=" . $this->csvname);
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
header('Expires:0');
header('Pragma:public');
//刷新buffer
ob_flush();
flush();
$file = fopen('php://output', 'a');
//先将标题写入文件
$title = self::arrzbm($this->title, 'utf-8', 'GBK');
fputcsv($file, $title);
ob_flush();
flush();
# 初始化mysql需要的参数
self::bactable();
# 获取数据总条数
$total = self::tabletotal();
$pages = ceil($total/$this->bacpages);
for ($i = 0; $i < $pages; $i++){
$data = self::tabledata($i);
$y = 0;
$listnum = 1000;
foreach ($data as $val) {
//返回数据
$y++;
# 每隔$listnum行刷新一下输出buffer,大数据量时处理
# 刷新一下输出buffer防止由于数据过多造成问题
if ($listnum == $y) {
ob_flush();
# 刷新buffer
flush();
$y = 0;
}
$arr = self::arrzbm($val,'utf-8' , 'GBK');
fputcsv($file, $arr);
}
}
fclose($file);
}
//判断语句组合
protected function bactable() {
global $_M;
$this->bactable = self::td_sqlk();
$this->bacfield = self::td_field();
$where = self::td_where();
parent::where_id($where);
if($where){
$where = trim($where);
if (strtolower(substr($where, 0, 5)) != 'where' && $where) $this->bacconds = " WHERE {$where} ";
}
//排序
if($this->bacconds){
$order = self::td_order();
$this->bacconds .= " ORDER BY {$order} ";
}
return $this;
}
//计算总数
protected function tabletotal() {
global $_M;
$countsql = " SELECT COUNT(*) FROM {$this->bactable} {$this->bacconds} ";
if($this->multi_table) $countsql = " SELECT count(*) FROM (SELECT {$this->bacfield} FROM {$this->bactable} {$this->bacconds}) num ";
$result = DB::query($countsql);
$fetch_row = DB::fetch_row($result);
return $fetch_row[0];
}
//获取获取
protected function tabledata($pages) {
global $_M;
$cursize = $pages* $this->bacpages;
//SQL查询
$result = DB::query("SELECT {$this->bacfield} FROM {$this->bactable} {$this->bacconds} LIMIT {$cursize},{$this->bacpages}");
while($val = DB::fetch_array($result)){
$data[] = $val;
}
//处理数组结果
foreach ($data as $val) {
$lists = self::{$this->tname}($val);
$baclist[] = array_intersect_key(self::striptags($lists),$this->title);
}
return $baclist;
}
/* * *******************************************************************************
*
* 导入导出公用函数
*
* ****************************************************************************** */
# 导入导出数组(保持和导入格式一样)
protected function csvtitle() {
global $_M, $_YW;
$this->title = self::{'tf_'.$this->tname}();
$this->keys = array_keys($this->title);
return $this;
}
//生成文件名
public function csvname() {
global $_M, $_YW;
$date = date('Ymd', time());
$filename = iconv("utf-8", "GBK", $this->bacname . $_M['lang'] . $date);
$this->csvname = $filename . '.csv';
return $this;
}
//对数组的值进行转码并返回(一维数组)
public function arrzbm($arr, $u, $g) {
global $_M, $_YW;
foreach ($arr as $k => $v) {
$newarr[$k] = iconv($u, $g, $v);
}
return $newarr;
}
//删除html元素
public function striptags($array = []) {
global $_M, $_YW;
$ret = [];
foreach ($array as $key => $str) {
$ret[$key] = strip_tags($str);
}
return $ret;
}
}
?>