📄 vodcmsmodel.php
字号:
<?php
class VodcmsModel extends DBModel {
public $database = 'config/database';
public $filename = null;
public $letter;
public $path;
public $dir;
public $pagesize = 2;
public $ext = '.sql';
public $charset = 'GBK';
public $_name;
public function Init(){
$this->_IO = new Easy_Filesystem();
$this->ext = '.sql';
$this->dir = ROOT.$this->database;
$this->_name = VODCMS_FILE;
if (!defined('SUCCEED_VODCMS')){
exit();
}
}
/*
将数据库导出
*/
public function view($dir=null){
if (is_null($dir)){
$dir = ROOT.$this->database;
$this->path = '';
}elseif (basename(base64_decode($dir)) == 'Config'){
$dir = ROOT.$this->database;
$this->path = '';
}else {
$dir = base64_decode($dir);
$this->path = $dir;
}
$data = $this->_IO->scandir($dir);
$this->_Page->set(count($data), 10);
$this->printpage = $this->_Page->printpage();
$this->_Page->limit();
//print_r($array);
return @array_slice($data, $this->_Page->pagesize, $this->_Page->maxpage, true);
}
public function export(){
@$this->dir = ROOT.$this->database;
ini_set('memory_limit', '128M');
$this->letter = 65;
$this->ext = $this->_Response->getPost('ext');
if ($this->_Response->getPost('charset')){
$this->charset = strtoupper($this->_Response->getPost('charset'));
}
$uniqid = date("Ymdhis");
mkdir($this->dir.'/'.$uniqid, 0755);
$this->filename = $this->dir.'/'.$uniqid.'/'.chr($this->letter).$this->ext;
$this->pagesize = $this->_Response->getPost('pagesize'); //分页大小
foreach($this->_DB->showTable() as $table){
@set_time_limit(0);
$this->exportData($table);
}
}
/*
导出SQL数据以及表结构
*/
public function exportData($table){
if (is_null($this->filename)){
$uniqid = date("Ymdhis");
@mkdir($this->dir.'/'.$uniqid, 0755);
$this->filename = $this->dir.'/'.$uniqid.'/'.$table.$this->ext;
}
$data = $this->_DB->fetRow('SHOW CREATE TABLE '.$table);
$maxsize = $this->pagesize *1024*1000;
$sqlData = "\nDROP TABLE IF EXISTS `{$table}`;\n".preg_replace('/CHARSET\=(\w+)/is', 'CHARSET='.$this->charset, end($data)).";";
$this->_IO->wfile($this->filename, $sqlData, 'a');
unset($sqlData);
$i=0;
$total = $this->_DB->getCount('select * from '.$table);
$maxpage = 300;
$pagesize = ceil($total / $maxpage);
$page = 1;
while($page <= $pagesize){
$offset = ($page-1) * $maxpage;
$sql = 'select * from '.$table.' LIMIT '.$offset.','.$maxpage;
foreach ($this->_DB->fetAll($sql) as $row){
@set_time_limit(999);
clearstatcache();
$i++;
if ($i % 20 == 0){
$sqlData.= "('".implode("','", array_map('addslashes', array_values($row)))."');";
if ((int)filesize($this->filename) > $maxsize){
$this->letter = $this->letter+1;
$this->filename = preg_replace('/\/([A-Z])\./is', '/'.chr($this->letter).'.', $this->filename);
//echo $this->filename;
}
$this->_IO->wfile($this->filename, $insert.$sqlData, 'a');
unset($sqlData, $insert);
}else{
$insert = "\nINSERT INTO ".$table."(`".implode("`,`", array_keys($row))."`) VALUES";
$sqlData.= "\n('".implode("','", array_map('addslashes', array_values($row)))."'),";
}
}
$page ++;
}
$sqlData = preg_replace('/,$/is', ';', $sqlData);
$this->_IO->wfile($this->filename, $insert.$sqlData, 'a');
unset($sqlData, $insert);
}
/*
删除文件
*/
public function delete($filename){
if (is_dir($filename)){
$this->_IO->delete($filename);
}else{
$this->_IO->delete($filename, array('sql','gz','gzip'));
}
return $this->_IO->delrow;
//chdir($root);
//$this->_IO->delete($fllename)
}
/*
导入数据库文件
*/
public function import($filename){
if (is_dir($filename)){
$files = array_values($this->_IO->scandir($filename));
}else{
$files = array($filename);
}
$num = (int)$_GET['num'];
$row = $files[$num];
if (in_array(substr(strrchr($row, '.'), 1), array('gzip','gz'))){
$zd = gzopen($row, "r");
$contents = gzread($zd, 10000);
gzclose($zd);
}else {
$contents = @file_get_contents($row);
}
$data = explode(";\n", $contents);
foreach ($data as $sql){
if($sql) {
@set_time_limit(300);
$this->_DB->exec($sql);
}
}
$num = $num+1;
if (count($files)>$num){
$this->sqlfile = $row;
return $num;
}else{
return false; //导入结束来
}
}
/*
获取表状态信息
*/
public function getStat(){
return $this->_DB->fetAll('SHOW TABLE STATUS');
}
/*
优化表
*/
public function optimize($tablename){
foreach ($tablename as $table){
$this->_DB->exec('OPTIMIZE TABLE '. $table);
}
}
public function repair($tablename){
foreach ($tablename as $table){
$this->_DB->exec('REPAIR TABLE '. $table);
}
}
/*
获取表信息
*/
public function getPic(){
$select = $this->_DB->select();
$sql = $select->from($this->_name)->order('id DESC')->toString();
$total = $this->_DB->getCount($sql);
$this->_Page->set($total);
return $this->_DB->fetAll($sql.$this->_Page->limit());
}
/*SQL命令执行*/
public function exec($sql){
if ($sql){
$func = array('truncate', 'select', 'delete', 'alter', 'update');
$off = false;
foreach($func as $fun){
if (stripos($sql, $fun) !==false){
$off = true;
break;
}
}
if ($off === true){
if (stripos($sql, VODCMS_SYSTEMLOG) !== false){
return true;
}
$sql = str_replace('{tblpre}', TBLPRE, $sql);
$sql = str_replace('{time}', time(), $sql);
systemlog::set('执行SQL命令'.$sql);
$result = @mysql_query($sql);
if ($result === false){
$this->error = _('SQL命令错误描述'.mysql_error());
return false;
}else{
return true;
}
}else{
$this->error = _('不是允许的SQL命令');
return false;
}
}else{
$this->error = _('SQL语句不能为空!');
return false;
}
}
/*
清理SQL语句
*/
public function clean($action = array()){
$IO = new Easy_Filesystem();
if (empty($action)){
$action = $this->_Response->getPost('checkbox');
}
if (is_array($action) == false){
$action = array(1,2,3);
}
if (in_array(1, $action)){
$IO->delete(ROOT.'cache', array('php'), false);
}
if (in_array(2, $action)){
$IO->delete(ROOT.'cache/tmp', array('html'), false);
}
if (in_array(3, $action)){
$files = scandir(ROOT.'cache/session');
foreach($files as $file){
if ($file != '.' && $file !='..' && $file!='.svn'){
if (time()-@filemtime(ROOT.'cache/session/'.$file)>3600 || @filesize(ROOT.'cache/session/'.$file)==0){
@unlink(ROOT.'cache/session/'.$file);
}
}
}
}
if (in_array(4, $action)){
$movie = new MovieModel();
$movie->setMidTable();
$movie->initmovie();
$category = new CategoryModel();
$category->initLable();
$category->updateNodes();
}
if (in_array(5, $action)){
$category = new CategoryModel();
$category->initLable();
$movie = new MovieModel();
$movie->initsCategory();
}
/*重组影片评论数*/
$sql = 'select count(a.movid) FROM '.VODCMS_COMMENT.' AS a,'.VODCMS_MOVIE.' as b
where a.movid=b.movid';
$this->_DB->getCount($sql);
/*end*/
$config = $GLOBALS['config'];
if ( $config['memcached']===true ){
if ( $this->_DB->flush_memcache()===false){
echo '清理memcache服务器缓存失败!';
}
}
}
/*
*/
public function suuperReplace(){
}
}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -