phpoksql.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <?php
  2. class phpoksql_c extends Control
  3. {
  4. function __construct()
  5. {
  6. parent::Control();
  7. $this->load_model("sql");
  8. }
  9. function phpoksql_c()
  10. {
  11. $this->__construct();
  12. }
  13. function index_f()
  14. {
  15. sys_popedom("phpoksql:list","tpl");
  16. $ifact = sys_popedom("phpoksql:set");
  17. $this->tpl->assign("set_popedom",$ifact);//执行操作
  18. $rslist = $this->sql_m->get_all();
  19. $this->tpl->assign("rslist",$rslist);
  20. $this->tpl->display("sql.html");
  21. }
  22. function optimize_f()
  23. {
  24. sys_popedom("phpoksql:set","tpl");
  25. $id = $this->trans_lib->safe("id");
  26. if(!$id)
  27. {
  28. error("没有指定要优化的数据表!",site_url("phpoksql"));
  29. }
  30. $idlist = sys_id_list($id);
  31. foreach($idlist AS $key=>$value)
  32. {
  33. $this->sql_m->optimize($value);
  34. }
  35. error("指定数据表信息已优化完成!",site_url("phpoksql"));
  36. }
  37. function repair_f()
  38. {
  39. sys_popedom("phpoksql:set","tpl");
  40. $id = $this->trans_lib->safe("id");
  41. if(!$id)
  42. {
  43. error("没有指定要修复的数据表!",site_url("phpoksql"));
  44. }
  45. $idlist = sys_id_list($id);
  46. foreach($idlist AS $key=>$value)
  47. {
  48. $this->sql_m->repair($value);
  49. }
  50. error("指定数据表信息已修复完成!",site_url("phpoksql"));
  51. }
  52. function backup_f()
  53. {
  54. sys_popedom("phpoksql:set","tpl");
  55. $id = $this->trans_lib->safe("id");
  56. if(!$id || $id == "all")
  57. {
  58. $tbl_list = $this->sql_m->get_all();
  59. $idlist = array();
  60. foreach($tbl_list AS $key=>$value)
  61. {
  62. $idlist[] = $value["Name"];
  63. }
  64. $tourl = site_url("phpoksql,backup")."id=all";
  65. }
  66. else
  67. {
  68. $idlist = sys_id_list($id);
  69. $tourl = site_url("phpoksql,backup")."id=".rawurlencode($id);
  70. }
  71. //备份的文件名,如果为空,则表示未创建表结构
  72. $backfilename = $this->trans_lib->safe("backfilename");
  73. if(!$backfilename)
  74. {
  75. $sql_prefix = $this->sql_m->sql_prefix();//数据表前缀
  76. //创建备份文件名称
  77. $backfilename = "sql_".date("YmdHis",$this->system_time)."_".$_SESSION["admin_id"];
  78. //生成表名,获取表结构
  79. $html = "";
  80. foreach($idlist AS $key=>$value)
  81. {
  82. //禁止session表恢复操作
  83. if($value != $sql_prefix."session")
  84. {
  85. $html .= "DROP TABLE IF EXISTS ".$value.";\n";
  86. $html .= $this->sql_m->show_create_table($value);
  87. $html .= ";\n\n";
  88. }
  89. }
  90. //判断是否包含 管理员表,如果包含,则同时更新管理员数据
  91. if(in_array($sql_prefix."admin",$idlist))
  92. {
  93. $rslist = $this->sql_m->getsql($sql_prefix."admin",0,"all");
  94. if($rslist)
  95. {
  96. foreach($rslist AS $key=>$value)
  97. {
  98. $html .= "INSERT INTO ".$sql_prefix."admin VALUES('".implode("','",$value)."');\n";
  99. }
  100. }
  101. }
  102. $this->file_lib->vi($html,ROOT_DATA.$backfilename.".php");//存储数据
  103. $tourl .= "&backfilename=".rawurlencode($backfilename);//已创建表
  104. //初始化临昨表数据
  105. $this->file_lib->vi("#PHPOK Full 数据备份\n\n",ROOT_DATA.$backfilename."_tmpdata.php");
  106. error("表结构信息备份完毕,请稍候,正在执行下一步!",$tourl);
  107. }
  108. $tourl .= "&backfilename=".rawurlencode($backfilename);
  109. $startid = $this->trans_lib->int("startid");
  110. //判断startid是否存在
  111. if(($startid + 1)> count($idlist))
  112. {
  113. error("数据备份完毕!系统将返回已备份列表中",site_url('phpoksql,baklist'));
  114. }
  115. $table = $idlist[$startid];//指定表
  116. //判断如果是管理员表,则跳到下一步
  117. if($table == $sql_prefix."admin")
  118. {
  119. $pageid = $this->trans_lib->int("pageid");
  120. $dataid = $this->trans_lib->int("dataid");
  121. $tourl .= "&startid=".($startid+1)."&pageid=".$pageid."&dataid=".$dataid;
  122. error("数据表 ".$table." 已备份完成!正在进行下一步操作,请稍候!",$tourl);
  123. }
  124. //如果是session表,自动进入下一步操作
  125. if($table == $sql_prefix."session")
  126. {
  127. $pageid = $this->trans_lib->int("pageid");
  128. $dataid = $this->trans_lib->int("dataid");
  129. $tourl .= "&startid=".($startid+1)."&pageid=".$pageid."&dataid=".$dataid;
  130. error("数据表 ".$table." 无需备份数据!正在进行下一步操作,请稍候!",$tourl);
  131. }
  132. //每次只备份1000条数据
  133. $msg = "";
  134. $oldmsg = "";
  135. if(file_exists(ROOT_DATA.$backfilename."_tmpdata.php"))
  136. {
  137. $oldmsg = $this->file_lib->cat(ROOT_DATA.$backfilename."_tmpdata.php");
  138. }
  139. $psize = 100;//每次查询最多读取次数
  140. $total = $this->sql_m->table_count($table);//取得当前表的总记录数
  141. if($psize >= $total)
  142. {
  143. $rslist = $this->sql_m->getsql($table,0,"all");
  144. if($rslist)
  145. {
  146. $msg .= "\n#table : ".$table." , backup time ".date("Y-m-d H:i:s",$this->system_time)."\n";
  147. foreach($rslist AS $key=>$value)
  148. {
  149. $tmp_value = array();
  150. foreach($value AS $k=>$v)
  151. {
  152. $v = $this->sql_m->escape_string($v);
  153. $tmp_value[$k] = $v;
  154. }
  155. $msg .= "INSERT INTO ".$table." VALUES('".implode("','",$tmp_value)."');\n";
  156. }
  157. }
  158. $new_startid = $startid + 1;
  159. $pageid = 0;
  160. }
  161. else
  162. {
  163. $pageid = $this->trans_lib->int("pageid");
  164. if($pageid<1) $pageid = 1;
  165. if($pageid<2)
  166. {
  167. $msg .= "\n#table : ".$table." , backup time ".date("Y-m-d H:i:s",$this->system_time)."\n";
  168. }
  169. $offset = ($pageid-1)*$psize;
  170. if($offset < $total)
  171. {
  172. $rslist = $this->sql_m->getsql($table,$offset,$psize);
  173. if($rslist)
  174. {
  175. foreach($rslist AS $key=>$value)
  176. {
  177. $tmp_value = array();
  178. foreach($value AS $k=>$v)
  179. {
  180. $v = $this->sql_m->escape_string($v);
  181. $tmp_value[$k] = $v;
  182. }
  183. $msg .= "INSERT INTO ".$table." VALUES('".implode("','",$tmp_value)."');\n";
  184. }
  185. $new_startid = $startid;
  186. $pageid = $pageid + 1;
  187. }
  188. else
  189. {
  190. $new_startid = $startid + 1;
  191. $pageid = 0;
  192. }
  193. }
  194. else
  195. {
  196. $new_startid = $startid + 1;
  197. $pageid = 0;
  198. }
  199. }
  200. $tourl .= "&startid=".$new_startid."&pageid=".$pageid;
  201. //存储数据的文件Id
  202. $dataid = $this->trans_lib->int("dataid");
  203. //计算数据长度
  204. //$msg = $oldmsg.$msg;
  205. //$msg = $oldmsg . addslashes($msg);
  206. $msg = addslashes($oldmsg . $msg);
  207. if(strlen($msg)>=(1024*1000))
  208. {
  209. //如果文件存在,则自动加一
  210. if(file_exists(ROOT_DATA.$backfilename."_data_".$dataid.".php"))
  211. {
  212. $dataid++;
  213. }
  214. $this->file_lib->vi($msg,ROOT_DATA.$backfilename."_data_".$dataid.".php");//存储数据
  215. unset($msg,$oldmsg);
  216. //再重新创建临时文件
  217. $new_dataid = $dataid+1;
  218. //判断是否已经结束了
  219. if($idlist[$new_startid])
  220. {
  221. $this->file_lib->vi("#PHPOK Full 数据备份\n\n",ROOT_DATA.$backfilename."_tmpdata.php");
  222. error("正在备份数据,当前第 ".($dataid+1)." 个文件!",$tourl."&dataid=".$new_dataid);
  223. }
  224. else
  225. {
  226. $this->file_lib->rm(ROOT_DATA.$backfilename."_tmpdata.php");
  227. error("数据表备份操作成功,请稍候,正在进入下一步!",site_url("phpoksql,baklist"));
  228. }
  229. }
  230. else
  231. {
  232. if(!$idlist[$new_startid])
  233. {
  234. if(file_exists(ROOT_DATA.$backfilename."_data_".$dataid.".php"))
  235. {
  236. $dataid++;
  237. }
  238. $this->file_lib->vi($msg,ROOT_DATA.$backfilename."_data_".$dataid.".php");//存储数据
  239. $this->file_lib->rm(ROOT_DATA.$backfilename."_tmpdata.php");//删除临时文件
  240. error("数据表备份操作成功,请稍候,正在进入下一步!",site_url("phpoksql,baklist"));
  241. }
  242. else
  243. {
  244. //如果数据没有超过系统限制,则
  245. $this->file_lib->vi($msg,ROOT_DATA.$backfilename."_tmpdata.php");
  246. $new_dataid = $dataid;
  247. error("正在备份数据,当前第 ".($dataid+1)." 个文件!",$tourl."&dataid=".$new_dataid);
  248. }
  249. }
  250. }
  251. function baklist_f()
  252. {
  253. sys_popedom("phpoksql:list","tpl");
  254. $ifact = sys_popedom("phpoksql:set");
  255. $this->tpl->assign("set_popedom",$ifact);//执行操作
  256. $this->load_model("admin");
  257. $this->admin_m->psize = 999;//设置管理员999个
  258. $admin_tmplist = $this->admin_m->get_list(0);
  259. $adminlist = array();
  260. foreach($admin_tmplist AS $key=>$value)
  261. {
  262. $adminlist[$value["id"]] = $value["name"];
  263. }
  264. unset($admin_tmplist);
  265. $filelist = $this->file_lib->ls(ROOT_DATA);
  266. if(!$filelist)
  267. {
  268. error("没有取得相应数据!",site_url("phpoksql"));
  269. }
  270. $tmplist = array();
  271. $i=0;
  272. foreach($filelist AS $key=>$value)
  273. {
  274. $bv = basename($value);
  275. if(substr($bv,0,4) == "sql_")
  276. {
  277. $tmp = explode("_",substr($bv,0,-4));
  278. $tmplist[$i] = array();
  279. $tmplist[$i]["filename"] = $value;
  280. $tmplist[$i]["basename"] = substr($bv,0,-4);
  281. $tmplist[$i]["tmptime"] = $tmp[1];
  282. $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);
  283. $tmplist[$i]["admin"] = $adminlist[$tmp[2]];
  284. $tmplist[$i]["type"] = $tmp[3] ? "data" : "sql";
  285. if($tmp[3] == "tmpdata")
  286. {
  287. $tmplist[$i]["type"] = "tmpdata";
  288. }
  289. $i++;
  290. }
  291. }
  292. if(!$tmplist || count($tmplist)<1)
  293. {
  294. error("没有检测到备份文件!",site_url("phpoksql"));
  295. }
  296. $yclist = $rslist = array();
  297. foreach($tmplist AS $key=>$value)
  298. {
  299. if($value["type"] == "sql")
  300. {
  301. $filesize = filesize($value["filename"]);
  302. foreach($tmplist AS $k=>$v)
  303. {
  304. if($v["type"] == "data" && $v["tmptime"] == $value["tmptime"] && $v["admin"] == $value["admin"])
  305. {
  306. $filesize += filesize($v["filename"]);
  307. }
  308. }
  309. $value["psize"] = $filesize;
  310. $rslist[] = $value;
  311. }
  312. elseif($value["type"] == "tmpdata")
  313. {
  314. $value["psize"] = filesize($value["filename"]);
  315. $yclist[] = $value;
  316. }
  317. }
  318. $this->tpl->assign("rslist",$rslist);
  319. $this->tpl->display("sql_list.html");
  320. }
  321. function del_f()
  322. {
  323. sys_popedom("phpoksql:set","tpl");
  324. $id = $this->trans_lib->safe("id");
  325. if(!$id)
  326. {
  327. error("没有指定备份文件!",site_url("phpoksql,baklist"));
  328. }
  329. $filelist = $this->file_lib->ls(ROOT_DATA);
  330. if(!$filelist)
  331. {
  332. error("没有取得相应数据!",site_url("phpoksql,baklist"));
  333. }
  334. $idlen = strlen($id);
  335. foreach($filelist AS $key=>$value)
  336. {
  337. $bv = basename($value);
  338. if(substr($bv,0,$idlen) == $id)
  339. {
  340. $this->file_lib->rm($value);
  341. }
  342. }
  343. error("备份文件 ".$id." 删除操作成功!",site_url("phpoksql,baklist"));
  344. }
  345. function recover_f()
  346. {
  347. sys_popedom("phpoksql:set","tpl");
  348. $id = $this->trans_lib->safe("id");
  349. if(!$id)
  350. {
  351. error("没有指定备份文件!",site_url("phpoksql,baklist"));
  352. }
  353. if(!file_exists(ROOT_DATA.$id.".php"))
  354. {
  355. error("备份文件丢失,请检查!",site_url("phpoksql,baklist"));
  356. }
  357. //恢复表结构数据
  358. $msg = $this->file_lib->cat(ROOT_DATA.$id.".php");
  359. $this->format_sql($msg);
  360. error("表结构数据已经修复,正在恢复数据!请稍候!",site_url("phpoksql,recover_data")."id=".rawurlencode($id)."&startid=0");
  361. }
  362. function recover_session_f()
  363. {
  364. sys_popedom("phpoksql:set","tpl");
  365. $this->sql_m->recover_session();
  366. error("SESSION表已还原!",site_url("phpoksql,baklist"));
  367. }
  368. function recover_data_f()
  369. {
  370. sys_popedom("phpoksql:set","tpl");
  371. $id = $this->trans_lib->safe("id");
  372. if(!$id)
  373. {
  374. error("没有指定备份文件!",site_url("phpoksql,baklist"));
  375. }
  376. $filelist = $this->file_lib->ls(ROOT_DATA);
  377. if(!$filelist)
  378. {
  379. error("没有取得相应数据!",site_url("phpoksql,baklist"));
  380. }
  381. $idlen = strlen($id."_data_");
  382. $rslist = array();
  383. foreach($filelist AS $key=>$value)
  384. {
  385. $bv = basename($value);
  386. if(substr($bv,0,$idlen) == $id."_data_")
  387. {
  388. $rslist[] = $value;
  389. }
  390. }
  391. if(!$rslist || count($rslist)<1)
  392. {
  393. error("数据文件丢失,请检查!",site_url("phpoksql,baklist"));
  394. }
  395. $startid = $this->trans_lib->int("startid");
  396. if(!$rslist[$startid])
  397. {
  398. error("数据信息已恢复完成!建议您清空缓存后退出再重新登录!",site_url("phpoksql,baklist"));
  399. }
  400. $file = $rslist[$startid];
  401. //恢复表结构数据
  402. $msg = $this->file_lib->cat($file);
  403. $this->format_sql($msg);
  404. $new_startid = $startid + 1;
  405. if(!$rslist[$new_startid])
  406. {
  407. error("数据信息已恢复完成!建议您清空缓存后退出再重新登录!",site_url("phpoksql,baklist"));
  408. }
  409. error("正在恢复数据,请稍候!",site_url("phpoksql,recover_data")."id=".rawurlencode($id)."&startid=".$new_startid);
  410. }
  411. function format_sql($sql)
  412. {
  413. $sql = str_replace("\r","\n",$sql);
  414. $ret = array();
  415. $num = 0;
  416. foreach(explode(";\n", trim($sql)) as $query)
  417. {
  418. $queries = explode("\n", trim($query));
  419. foreach($queries as $query)
  420. {
  421. $ret[$num] .= $query[0] == '#' || $query[0].$query[1] == '--' ? '' : $query;
  422. }
  423. $num++;
  424. }
  425. unset($sql);
  426. foreach($ret as $query)
  427. {
  428. $query = trim($query);
  429. if($query)
  430. {
  431. if(substr($query, 0, 12) == 'CREATE TABLE')
  432. {
  433. $this->sql_m->query_create($query);
  434. }
  435. else
  436. {
  437. $this->sql_m->query($query);
  438. }
  439. }
  440. }
  441. }
  442. }
  443. ?>