123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- <?php
- class phpoksql_c extends Control
- {
- function __construct()
- {
- parent::Control();
- $this->load_model("sql");
- }
- function phpoksql_c()
- {
- $this->__construct();
- }
- function index_f()
- {
- sys_popedom("phpoksql:list","tpl");
- $ifact = sys_popedom("phpoksql:set");
- $this->tpl->assign("set_popedom",$ifact);
- $rslist = $this->sql_m->get_all();
- $this->tpl->assign("rslist",$rslist);
- $this->tpl->display("sql.html");
- }
- function optimize_f()
- {
- sys_popedom("phpoksql:set","tpl");
- $id = $this->trans_lib->safe("id");
- if(!$id)
- {
- error("没有指定要优化的数据表!",site_url("phpoksql"));
- }
- $idlist = sys_id_list($id);
- foreach($idlist AS $key=>$value)
- {
- $this->sql_m->optimize($value);
- }
- error("指定数据表信息已优化完成!",site_url("phpoksql"));
- }
- function repair_f()
- {
- sys_popedom("phpoksql:set","tpl");
- $id = $this->trans_lib->safe("id");
- if(!$id)
- {
- error("没有指定要修复的数据表!",site_url("phpoksql"));
- }
- $idlist = sys_id_list($id);
- foreach($idlist AS $key=>$value)
- {
- $this->sql_m->repair($value);
- }
- error("指定数据表信息已修复完成!",site_url("phpoksql"));
- }
- function backup_f()
- {
- sys_popedom("phpoksql:set","tpl");
- $id = $this->trans_lib->safe("id");
- if(!$id || $id == "all")
- {
- $tbl_list = $this->sql_m->get_all();
- $idlist = array();
- foreach($tbl_list AS $key=>$value)
- {
- $idlist[] = $value["Name"];
- }
- $tourl = site_url("phpoksql,backup")."id=all";
- }
- else
- {
- $idlist = sys_id_list($id);
- $tourl = site_url("phpoksql,backup")."id=".rawurlencode($id);
- }
-
- $backfilename = $this->trans_lib->safe("backfilename");
- if(!$backfilename)
- {
- $sql_prefix = $this->sql_m->sql_prefix();
-
- $backfilename = "sql_".date("YmdHis",$this->system_time)."_".$_SESSION["admin_id"];
-
- $html = "";
- foreach($idlist AS $key=>$value)
- {
-
- if($value != $sql_prefix."session")
- {
- $html .= "DROP TABLE IF EXISTS ".$value.";\n";
- $html .= $this->sql_m->show_create_table($value);
- $html .= ";\n\n";
- }
- }
-
- if(in_array($sql_prefix."admin",$idlist))
- {
- $rslist = $this->sql_m->getsql($sql_prefix."admin",0,"all");
- if($rslist)
- {
- foreach($rslist AS $key=>$value)
- {
- $html .= "INSERT INTO ".$sql_prefix."admin VALUES('".implode("','",$value)."');\n";
- }
- }
- }
- $this->file_lib->vi($html,ROOT_DATA.$backfilename.".php");
- $tourl .= "&backfilename=".rawurlencode($backfilename);
-
- $this->file_lib->vi("#PHPOK Full 数据备份\n\n",ROOT_DATA.$backfilename."_tmpdata.php");
- error("表结构信息备份完毕,请稍候,正在执行下一步!",$tourl);
- }
- $tourl .= "&backfilename=".rawurlencode($backfilename);
- $startid = $this->trans_lib->int("startid");
-
- if(($startid + 1)> count($idlist))
- {
- error("数据备份完毕!系统将返回已备份列表中",site_url('phpoksql,baklist'));
- }
- $table = $idlist[$startid];
-
- if($table == $sql_prefix."admin")
- {
- $pageid = $this->trans_lib->int("pageid");
- $dataid = $this->trans_lib->int("dataid");
- $tourl .= "&startid=".($startid+1)."&pageid=".$pageid."&dataid=".$dataid;
- error("数据表 ".$table." 已备份完成!正在进行下一步操作,请稍候!",$tourl);
- }
-
- if($table == $sql_prefix."session")
- {
- $pageid = $this->trans_lib->int("pageid");
- $dataid = $this->trans_lib->int("dataid");
- $tourl .= "&startid=".($startid+1)."&pageid=".$pageid."&dataid=".$dataid;
- error("数据表 ".$table." 无需备份数据!正在进行下一步操作,请稍候!",$tourl);
- }
-
- $msg = "";
- $oldmsg = "";
- if(file_exists(ROOT_DATA.$backfilename."_tmpdata.php"))
- {
- $oldmsg = $this->file_lib->cat(ROOT_DATA.$backfilename."_tmpdata.php");
- }
- $psize = 100;
- $total = $this->sql_m->table_count($table);
- if($psize >= $total)
- {
- $rslist = $this->sql_m->getsql($table,0,"all");
- if($rslist)
- {
- $msg .= "\n#table : ".$table." , backup time ".date("Y-m-d H:i:s",$this->system_time)."\n";
- foreach($rslist AS $key=>$value)
- {
- $tmp_value = array();
- foreach($value AS $k=>$v)
- {
- $v = $this->sql_m->escape_string($v);
- $tmp_value[$k] = $v;
- }
- $msg .= "INSERT INTO ".$table." VALUES('".implode("','",$tmp_value)."');\n";
- }
- }
- $new_startid = $startid + 1;
- $pageid = 0;
- }
- else
- {
- $pageid = $this->trans_lib->int("pageid");
- if($pageid<1) $pageid = 1;
- if($pageid<2)
- {
- $msg .= "\n#table : ".$table." , backup time ".date("Y-m-d H:i:s",$this->system_time)."\n";
- }
- $offset = ($pageid-1)*$psize;
- if($offset < $total)
- {
- $rslist = $this->sql_m->getsql($table,$offset,$psize);
- if($rslist)
- {
- foreach($rslist AS $key=>$value)
- {
- $tmp_value = array();
- foreach($value AS $k=>$v)
- {
- $v = $this->sql_m->escape_string($v);
- $tmp_value[$k] = $v;
- }
- $msg .= "INSERT INTO ".$table." VALUES('".implode("','",$tmp_value)."');\n";
- }
- $new_startid = $startid;
- $pageid = $pageid + 1;
- }
- else
- {
- $new_startid = $startid + 1;
- $pageid = 0;
- }
- }
- else
- {
- $new_startid = $startid + 1;
- $pageid = 0;
- }
- }
- $tourl .= "&startid=".$new_startid."&pageid=".$pageid;
-
- $dataid = $this->trans_lib->int("dataid");
-
-
-
- $msg = addslashes($oldmsg . $msg);
- if(strlen($msg)>=(1024*1000))
- {
-
- if(file_exists(ROOT_DATA.$backfilename."_data_".$dataid.".php"))
- {
- $dataid++;
- }
- $this->file_lib->vi($msg,ROOT_DATA.$backfilename."_data_".$dataid.".php");
- unset($msg,$oldmsg);
-
- $new_dataid = $dataid+1;
-
- if($idlist[$new_startid])
- {
- $this->file_lib->vi("#PHPOK Full 数据备份\n\n",ROOT_DATA.$backfilename."_tmpdata.php");
- error("正在备份数据,当前第 ".($dataid+1)." 个文件!",$tourl."&dataid=".$new_dataid);
- }
- else
- {
- $this->file_lib->rm(ROOT_DATA.$backfilename."_tmpdata.php");
- error("数据表备份操作成功,请稍候,正在进入下一步!",site_url("phpoksql,baklist"));
- }
- }
- else
- {
- if(!$idlist[$new_startid])
- {
- if(file_exists(ROOT_DATA.$backfilename."_data_".$dataid.".php"))
- {
- $dataid++;
- }
- $this->file_lib->vi($msg,ROOT_DATA.$backfilename."_data_".$dataid.".php");
- $this->file_lib->rm(ROOT_DATA.$backfilename."_tmpdata.php");
- error("数据表备份操作成功,请稍候,正在进入下一步!",site_url("phpoksql,baklist"));
- }
- else
- {
-
- $this->file_lib->vi($msg,ROOT_DATA.$backfilename."_tmpdata.php");
- $new_dataid = $dataid;
- error("正在备份数据,当前第 ".($dataid+1)." 个文件!",$tourl."&dataid=".$new_dataid);
- }
- }
- }
- function baklist_f()
- {
- sys_popedom("phpoksql:list","tpl");
- $ifact = sys_popedom("phpoksql:set");
- $this->tpl->assign("set_popedom",$ifact);
- $this->load_model("admin");
- $this->admin_m->psize = 999;
- $admin_tmplist = $this->admin_m->get_list(0);
- $adminlist = array();
- foreach($admin_tmplist AS $key=>$value)
- {
- $adminlist[$value["id"]] = $value["name"];
- }
- unset($admin_tmplist);
- $filelist = $this->file_lib->ls(ROOT_DATA);
- if(!$filelist)
- {
- error("没有取得相应数据!",site_url("phpoksql"));
- }
- $tmplist = array();
- $i=0;
- foreach($filelist AS $key=>$value)
- {
- $bv = basename($value);
- if(substr($bv,0,4) == "sql_")
- {
- $tmp = explode("_",substr($bv,0,-4));
- $tmplist[$i] = array();
- $tmplist[$i]["filename"] = $value;
- $tmplist[$i]["basename"] = substr($bv,0,-4);
- $tmplist[$i]["tmptime"] = $tmp[1];
- $tmplist[$i]["postdate"] = substr($tmp[1],0,4)."-".substr($tmp[1],4,2)."-".substr($tmp[1],6,2)." ".substr($tmp[1],8,2).":".substr($tmp[1],10,2).":".substr($tmp[1],12,2);
- $tmplist[$i]["admin"] = $adminlist[$tmp[2]];
- $tmplist[$i]["type"] = $tmp[3] ? "data" : "sql";
- if($tmp[3] == "tmpdata")
- {
- $tmplist[$i]["type"] = "tmpdata";
- }
- $i++;
- }
- }
- if(!$tmplist || count($tmplist)<1)
- {
- error("没有检测到备份文件!",site_url("phpoksql"));
- }
- $yclist = $rslist = array();
- foreach($tmplist AS $key=>$value)
- {
- if($value["type"] == "sql")
- {
- $filesize = filesize($value["filename"]);
- foreach($tmplist AS $k=>$v)
- {
- if($v["type"] == "data" && $v["tmptime"] == $value["tmptime"] && $v["admin"] == $value["admin"])
- {
- $filesize += filesize($v["filename"]);
- }
- }
- $value["psize"] = $filesize;
- $rslist[] = $value;
- }
- elseif($value["type"] == "tmpdata")
- {
- $value["psize"] = filesize($value["filename"]);
- $yclist[] = $value;
- }
- }
- $this->tpl->assign("rslist",$rslist);
- $this->tpl->display("sql_list.html");
- }
- function del_f()
- {
- sys_popedom("phpoksql:set","tpl");
- $id = $this->trans_lib->safe("id");
- if(!$id)
- {
- error("没有指定备份文件!",site_url("phpoksql,baklist"));
- }
- $filelist = $this->file_lib->ls(ROOT_DATA);
- if(!$filelist)
- {
- error("没有取得相应数据!",site_url("phpoksql,baklist"));
- }
- $idlen = strlen($id);
- foreach($filelist AS $key=>$value)
- {
- $bv = basename($value);
- if(substr($bv,0,$idlen) == $id)
- {
- $this->file_lib->rm($value);
- }
- }
- error("备份文件 ".$id." 删除操作成功!",site_url("phpoksql,baklist"));
- }
- function recover_f()
- {
- sys_popedom("phpoksql:set","tpl");
- $id = $this->trans_lib->safe("id");
- if(!$id)
- {
- error("没有指定备份文件!",site_url("phpoksql,baklist"));
- }
- if(!file_exists(ROOT_DATA.$id.".php"))
- {
- error("备份文件丢失,请检查!",site_url("phpoksql,baklist"));
- }
-
- $msg = $this->file_lib->cat(ROOT_DATA.$id.".php");
- $this->format_sql($msg);
- error("表结构数据已经修复,正在恢复数据!请稍候!",site_url("phpoksql,recover_data")."id=".rawurlencode($id)."&startid=0");
- }
- function recover_session_f()
- {
- sys_popedom("phpoksql:set","tpl");
- $this->sql_m->recover_session();
- error("SESSION表已还原!",site_url("phpoksql,baklist"));
- }
- function recover_data_f()
- {
- sys_popedom("phpoksql:set","tpl");
- $id = $this->trans_lib->safe("id");
- if(!$id)
- {
- error("没有指定备份文件!",site_url("phpoksql,baklist"));
- }
- $filelist = $this->file_lib->ls(ROOT_DATA);
- if(!$filelist)
- {
- error("没有取得相应数据!",site_url("phpoksql,baklist"));
- }
- $idlen = strlen($id."_data_");
- $rslist = array();
- foreach($filelist AS $key=>$value)
- {
- $bv = basename($value);
- if(substr($bv,0,$idlen) == $id."_data_")
- {
- $rslist[] = $value;
- }
- }
- if(!$rslist || count($rslist)<1)
- {
- error("数据文件丢失,请检查!",site_url("phpoksql,baklist"));
- }
- $startid = $this->trans_lib->int("startid");
- if(!$rslist[$startid])
- {
- error("数据信息已恢复完成!建议您清空缓存后退出再重新登录!",site_url("phpoksql,baklist"));
- }
- $file = $rslist[$startid];
-
- $msg = $this->file_lib->cat($file);
- $this->format_sql($msg);
- $new_startid = $startid + 1;
- if(!$rslist[$new_startid])
- {
- error("数据信息已恢复完成!建议您清空缓存后退出再重新登录!",site_url("phpoksql,baklist"));
- }
- error("正在恢复数据,请稍候!",site_url("phpoksql,recover_data")."id=".rawurlencode($id)."&startid=".$new_startid);
- }
- function format_sql($sql)
- {
- $sql = str_replace("\r","\n",$sql);
- $ret = array();
- $num = 0;
- foreach(explode(";\n", trim($sql)) as $query)
- {
- $queries = explode("\n", trim($query));
- foreach($queries as $query)
- {
- $ret[$num] .= $query[0] == '#' || $query[0].$query[1] == '--' ? '' : $query;
- }
- $num++;
- }
- unset($sql);
- foreach($ret as $query)
- {
- $query = trim($query);
- if($query)
- {
- if(substr($query, 0, 12) == 'CREATE TABLE')
- {
- $this->sql_m->query_create($query);
- }
- else
- {
- $this->sql_m->query($query);
- }
- }
- }
- }
- }
- ?>
|