PageRenderTime 43ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/backup/class/functions.php

https://github.com/westeast/xwdede
PHP | 1246 lines | 1112 code | 48 blank | 86 comment | 82 complexity | 53bc28fc689dbb320b66d35a889cabbe MD5 | raw file
  1. <?php
  2. //-------------- 用户相关函数 ----------------
  3. //是否登陆
  4. function islogin($uname='',$urnd=''){
  5. global $set_username,$set_outtime;
  6. $username=$uname?$uname:getcvar('bakusername');
  7. $rnd=$urnd?$urnd:getcvar('bakrnd');
  8. if(empty($username)||empty($rnd))
  9. {
  10. printerror("NotLogin","index.php");
  11. }
  12. if($username<>$set_username)
  13. {
  14. printerror("NotLogin","index.php");
  15. }
  16. Ebak_CHCookieRnd($username,$rnd);
  17. $time=time();
  18. if($time-getcvar('baklogintime')>$set_outtime*60)
  19. {
  20. printerror("OutLogintime","index.php");
  21. }
  22. esetcookie("baklogintime",$time,0);
  23. $lr['username']=$username;
  24. $lr['rnd']=$rnd;
  25. return $lr;
  26. }
  27. //设置COOKIE认证
  28. function Ebak_SCookieRnd($username,$rnd){
  29. global $set_loginrnd;
  30. $ckpass=md5(md5($rnd.$set_loginrnd).'-'.$rnd.'-'.$username.'-');
  31. esetcookie("loginebakckpass",$ckpass,0);
  32. }
  33. //验证COOKIE认证
  34. function Ebak_CHCookieRnd($username,$rnd){
  35. global $set_loginrnd;
  36. $ckpass=md5(md5($rnd.$set_loginrnd).'-'.$rnd.'-'.$username.'-');
  37. if($ckpass<>getcvar('loginebakckpass'))
  38. {
  39. printerror("NotLogin","index.php");
  40. }
  41. }
  42. //退出系统
  43. function LoginOut(){
  44. esetcookie("bakusername","",0);
  45. esetcookie("bakrnd","",0);
  46. esetcookie("loginebakckpass","",0);
  47. esetcookie("baklogintime","",0);
  48. printerror("ExitSuccess","index.php");
  49. }
  50. //登陆系统
  51. function login($lusername,$lpassword,$key,$lifetime=0){
  52. global $set_username,$set_password,$set_loginauth,$set_loginkey;
  53. if(empty($lusername)||empty($lpassword))
  54. {
  55. printerror("EmptyLoginUser","index.php");
  56. }
  57. //验证码
  58. if(!$set_loginkey)
  59. {
  60. if($key<>getcvar('checkkey')||empty($key))
  61. {
  62. printerror("FailLoginKey","index.php");
  63. }
  64. }
  65. if(md5($lusername)<>md5($set_username)||md5($lpassword)<>$set_password)
  66. {
  67. printerror("ErrorUser","index.php");
  68. }
  69. //认证码
  70. if($set_loginauth&&$set_loginauth!=$_POST['loginauth'])
  71. {
  72. printerror("ErrorLoginAuth","index.php");
  73. }
  74. $logintime=time();
  75. $rnd=make_password(12);
  76. $s1=esetcookie("bakusername",$lusername,0);
  77. $s2=esetcookie("bakrnd",$rnd,0);
  78. $s3=esetcookie("baklogintime",$logintime,0);
  79. Ebak_SCookieRnd($lusername,$rnd);
  80. if(!$s1||!$s2)
  81. {
  82. printerror("NotOpenCookie","index.php");
  83. }
  84. printerror("LoginSuccess","admin.php");
  85. }
  86. //-------------- 公用函数 ----------------
  87. //错误提示
  88. function printerror($error="",$gotourl="",$ecms=0){
  89. global $empire,$public_r,$editor;
  90. if($editor==1){$a="../";}
  91. elseif($editor==2){$a="../../";}
  92. elseif($editor==3){$a="../../../";}
  93. else{$a="";}
  94. if(strstr($gotourl,"(")||empty($gotourl))
  95. {
  96. $gotourl_js="history.go(-1)";
  97. $gotourl="javascript:history.go(-1)";
  98. }
  99. else
  100. {$gotourl_js="self.location.href='$gotourl';";}
  101. if(empty($error))
  102. {$error="DbError";}
  103. if($ecms==0)
  104. {
  105. @include $a.LoadLang("m.php");
  106. $error=$message_r[$error];
  107. @include $a.LoadAdminTemp('message.php');
  108. }
  109. elseif($ecms==9)//弹出对话框
  110. {
  111. @include $a.LoadLang("m.php");
  112. $error=$message_r[$error];
  113. echo"<script>alert('".$error."');".$gotourl_js."</script>";
  114. }
  115. exit();
  116. }
  117. //字符截取
  118. function sub_str($title,$lengh){
  119. if(strlen($title)>$lengh)
  120. {
  121. $pp=2;
  122. $len=strlen($title);
  123. if($len%2<>0)
  124. {$pp=1;}
  125. $title=substr($title,0,$lengh-$pp);
  126. $title=$title.' …';
  127. }
  128. return $title;
  129. }
  130. //取得文件扩展名
  131. function GetFiletype($filename){
  132. $filer=explode(".",$filename);
  133. $count=count($filer)-1;
  134. return strtolower(".".$filer[$count]);
  135. }
  136. //字符截取函数2
  137. function sub($Modi_Str,$start,$length,$mode = false){
  138. $n = 0;
  139. for($i=0;$i<$start;$i++){
  140. if(ord(substr($Modi_Str,$i,1))>0xa0){
  141. if($mode){
  142. $start++;
  143. $i++;
  144. }
  145. $n++;
  146. }
  147. }
  148. if(!$mode)$start = $start + $n%2;
  149. $The_length = $start+$length;
  150. for($i=$start;$i<$The_length;$i++){
  151. if(ord(substr($Modi_Str,$i,1))>0xa0){
  152. $The_Str.=substr($Modi_Str,$i,2);
  153. $i++;
  154. if($mode) $The_length++;
  155. }else{
  156. $The_Str.=substr($Modi_Str,$i,1);
  157. }
  158. }
  159. return $The_Str;
  160. }
  161. //取得随机数
  162. function make_password($pw_length){
  163. $low_ascii_bound=50;
  164. $upper_ascii_bound=122;
  165. $notuse=array(58,59,60,61,62,63,64,73,79,91,92,93,94,95,96,108,111);
  166. while($i<$pw_length)
  167. {
  168. mt_srand((double)microtime()*1000000);
  169. $randnum=mt_rand($low_ascii_bound,$upper_ascii_bound);
  170. if(!in_array($randnum,$notuse))
  171. {
  172. $password1=$password1.chr($randnum);
  173. $i++;
  174. }
  175. }
  176. return $password1;
  177. }
  178. //删除文件
  179. function DelFiletext($filename){
  180. @unlink($filename);
  181. }
  182. //取得文件内容
  183. function ReadFiletext($filepath){
  184. $htmlfp=@fopen($filepath,"r");
  185. while($data=@fread($htmlfp,1000))
  186. {
  187. $string.=$data;
  188. }
  189. @fclose($htmlfp);
  190. return $string;
  191. }
  192. //写文件
  193. function WriteFiletext($filepath,$string){
  194. global $filechmod;
  195. $string=stripSlashes($string);
  196. $fp=@fopen($filepath,"w");
  197. @fputs($fp,$string);
  198. @fclose($fp);
  199. if(empty($filechmod))
  200. {
  201. @chmod($filepath,0777);
  202. }
  203. }
  204. //写文件
  205. function WriteFiletext_n($filepath,$string){
  206. global $filechmod;
  207. $fp=@fopen($filepath,"w");
  208. @fputs($fp,$string);
  209. @fclose($fp);
  210. if(empty($filechmod))
  211. {
  212. @chmod($filepath,0777);
  213. }
  214. }
  215. //建立目录函数
  216. function DoMkdir($path){
  217. global $public_r;
  218. //不存在则建立
  219. if(!file_exists($path))
  220. {
  221. //安全模式
  222. if($public_r[phpmode])
  223. {
  224. $pr[0]=$path;
  225. FtpMkdir($ftpid,$pr);
  226. $mk=1;
  227. }
  228. else
  229. {
  230. $mk=@mkdir($path,0777);
  231. }
  232. @chmod($path,0777);
  233. if(empty($mk))
  234. {
  235. printerror("NotMkdir","history.go(-1)");
  236. }
  237. }
  238. return true;
  239. }
  240. //替换目录值
  241. function RepPathStr($path){
  242. $path=str_replace("\\","",$path);
  243. $path=str_replace("/","",$path);
  244. return $path;
  245. }
  246. //时间转换
  247. function ToChangeUseTime($time){
  248. global $fun_r;
  249. $usetime=time()-$time;
  250. if($usetime<60)
  251. {
  252. $tstr=$usetime.$fun_r['TimeSecond'];
  253. }
  254. else
  255. {
  256. $usetime=round($usetime/60);
  257. $tstr=$usetime.$fun_r['TimeMinute'];
  258. }
  259. return $tstr;
  260. }
  261. //转换大小
  262. function Ebak_ChangeSize($size){
  263. if($size<1024)
  264. {
  265. $str=$size." B";
  266. }
  267. elseif($size<1024*1024)
  268. {
  269. $str=round($size/1024,2)." KB";
  270. }
  271. elseif($size<1024*1024*1024)
  272. {
  273. $str=round($size/(1024*1024),2)." MB";
  274. }
  275. else
  276. {
  277. $str=round($size/(1024*1024*1024),2)." GB";
  278. }
  279. return $str;
  280. }
  281. //删除目录函数
  282. function DelPath($DelPath){
  283. include("class/delpath.php");
  284. $wm_chief=new del_path();
  285. $wm_chief_ok=$wm_chief->wm_chief_delpath($DelPath);
  286. return $wm_chief_ok;
  287. }
  288. //打包目录
  289. function ZipFile($path,$zipname){
  290. global $bakpath,$bakzippath;
  291. @include("class/phpzip.inc.php");
  292. $z=new PHPZip(); //新建立一个zip的类
  293. $z->Zip($bakpath."/".$path,$bakzippath."/".$zipname); //添加指定目录
  294. }
  295. //压缩目录
  296. function Ebak_Dozip($path){
  297. global $bakpath,$bakzippath;
  298. if(strstr($path,".."))
  299. {printerror("DelPathNotExists","history.go(-1)",9);}
  300. if(empty($path))
  301. {
  302. printerror("DelPathNotExists","history.go(-1)",9);
  303. }
  304. $mypath=$bakpath."/".$path;
  305. if(!file_exists($mypath))
  306. {
  307. printerror("DelPathNotExists","history.go(-1)",9);
  308. }
  309. $zipname=$path.".zip";
  310. ZipFile($path,$zipname);
  311. echo"<script>self.location.href='DownZip.php?f=$zipname&p=$path';</script>";
  312. }
  313. //去除adds
  314. function Ebak_ClearAddsData($data){
  315. $magic_quotes_gpc=@get_magic_quotes_gpc();
  316. if($magic_quotes_gpc)
  317. {
  318. $data=stripSlashes($data);
  319. }
  320. return $data;
  321. }
  322. //-------------- 数据库函数 ----------------
  323. //--------------备份
  324. //初使化备份
  325. function Ebak_DoEbak($add){
  326. global $empire,$bakpath,$fun_r,$phome_db_ver;
  327. $dbname=RepPostVar($add['mydbname']);
  328. if(empty($dbname))
  329. {
  330. printerror("NotChangeDb","history.go(-1)");
  331. }
  332. $tablename=$add['tablename'];
  333. $count=count($tablename);
  334. if(empty($count))
  335. {
  336. printerror("EmptyChangeTb","history.go(-1)");
  337. }
  338. $add['baktype']=(int)$add['baktype'];
  339. $add['filesize']=(int)$add['filesize'];
  340. $add['bakline']=(int)$add['bakline'];
  341. $add['autoauf']=(int)$add['autoauf'];
  342. if((!$add['filesize']&&!$add['baktype'])||(!$add['bakline']&&$add['baktype']))
  343. {
  344. printerror("EmptyBakFilesize","history.go(-1)");
  345. }
  346. //目录名
  347. if(empty($add['mypath']))
  348. {
  349. $add['mypath']=$dbname."_".date("YmdHis");
  350. }
  351. DoMkdir($bakpath."/".$add['mypath']);
  352. //生成说明文件
  353. $readme=$add['readme'];
  354. $rfile=$bakpath."/".$add['mypath']."/readme.txt";
  355. $readme.="\r\n\r\nBaktime: ".date("Y-m-d H:i:s");
  356. WriteFiletext_n($rfile,$readme);
  357. $b_table="";
  358. $d_table="";
  359. for($i=0;$i<$count;$i++)
  360. {
  361. $b_table.=$tablename[$i].",";
  362. $d_table.="\$tb[".$tablename[$i]."]=0;\r\n";
  363. }
  364. //去掉最后一个,
  365. $b_table=substr($b_table,0,strlen($b_table)-1);
  366. $bakstru=(int)$add['bakstru'];
  367. $bakstrufour=(int)$add['bakstrufour'];
  368. $beover=(int)$add['beover'];
  369. $waitbaktime=(int)$add['waitbaktime'];
  370. $bakdatatype=(int)$add['bakdatatype'];
  371. if($add['insertf']=='insert')
  372. {
  373. $insertf='insert';
  374. }
  375. else
  376. {
  377. $insertf='replace';
  378. }
  379. if($phome_db_ver=='4.0'&&$add['dbchar']=='auto')
  380. {
  381. $add['dbchar']='';
  382. }
  383. $string="<?php
  384. \$b_table=\"".$b_table."\";
  385. ".$d_table."
  386. \$b_baktype=".$add['baktype'].";
  387. \$b_filesize=".$add['filesize'].";
  388. \$b_bakline=".$add['bakline'].";
  389. \$b_autoauf=".$add['autoauf'].";
  390. \$b_dbname=\"".$dbname."\";
  391. \$b_stru=".$bakstru.";
  392. \$b_strufour=".$bakstrufour.";
  393. \$b_dbchar=\"".addslashes($add['dbchar'])."\";
  394. \$b_beover=".$beover.";
  395. \$b_insertf=\"".addslashes($insertf)."\";
  396. \$b_autofield=\",".addslashes($add['autofield']).",\";
  397. \$b_bakdatatype=".$bakdatatype.";
  398. ?>";
  399. $cfile=$bakpath."/".$add['mypath']."/config.php";
  400. WriteFiletext_n($cfile,$string);
  401. if($add['baktype'])
  402. {
  403. $phome='BakExeT';
  404. }
  405. else
  406. {
  407. $phome='BakExe';
  408. }
  409. echo $fun_r['StartToBak']."<script>self.location.href='phomebak.php?phome=$phome&t=0&s=0&p=0&mypath=$add[mypath]&waitbaktime=$waitbaktime';</script>";
  410. exit();
  411. }
  412. //执行备份(按文件大小)
  413. function Ebak_BakExe($t,$s,$p,$mypath,$alltotal,$thenof,$fnum,$stime=0){
  414. global $empire,$bakpath,$limittype,$fun_r;
  415. if(empty($mypath))
  416. {
  417. printerror("ErrorUrl","history.go(-1)");
  418. }
  419. $path=$bakpath."/".$mypath;
  420. @include($path."/config.php");
  421. if(empty($b_table))
  422. {
  423. printerror("ErrorUrl","history.go(-1)");
  424. }
  425. $waitbaktime=(int)$_GET['waitbaktime'];
  426. if(empty($stime))
  427. {
  428. $stime=time();
  429. }
  430. $header="<?php
  431. require(\"../../inc/header.php\");
  432. ";
  433. $footer="
  434. require(\"../../inc/footer.php\");
  435. ?>";
  436. $btb=explode(",",$b_table);
  437. $count=count($btb);
  438. $t=(int)$t;
  439. $s=(int)$s;
  440. $p=(int)$p;
  441. //备份完毕
  442. if($t>=$count)
  443. {
  444. echo"<script>alert('".$fun_r['BakSuccess']."\\n\\n".$fun_r['TotalUseTime'].ToChangeUseTime($stime)."');self.location.href='ChangeDb.php';</script>";
  445. exit();
  446. }
  447. $dumpsql=Ebak_ReturnVer();
  448. //选择数据库
  449. $u=$empire->query("use `$b_dbname`");
  450. //编码
  451. if($b_dbchar=='auto')
  452. {
  453. if(empty($s))
  454. {
  455. $status_r=Ebak_GetTotal($b_dbname,$btb[$t]);
  456. $collation=Ebak_GetSetChar($status_r['Collation']);
  457. DoSetDbChar($collation);
  458. //总记录数
  459. $num=$limittype?-1:$status_r['Rows'];
  460. }
  461. else
  462. {
  463. $collation=$_GET['collation'];
  464. DoSetDbChar($collation);
  465. $num=(int)$alltotal;
  466. }
  467. $dumpsql.=Ebak_ReturnSetNames($collation);
  468. }
  469. else
  470. {
  471. DoSetDbChar($b_dbchar);
  472. if(empty($s))
  473. {
  474. //总记录数
  475. if($limittype)
  476. {
  477. $num=-1;
  478. }
  479. else
  480. {
  481. $status_r=Ebak_GetTotal($b_dbname,$btb[$t]);
  482. $num=$status_r['Rows'];
  483. }
  484. }
  485. else
  486. {
  487. $num=(int)$alltotal;
  488. }
  489. }
  490. //备份数据库结构
  491. if($b_stru&&empty($s))
  492. {
  493. $dumpsql.=Ebak_Returnstru($btb[$t],$b_strufour);
  494. }
  495. $sql=$empire->query("select * from `".$btb[$t]."` limit $s,$num");
  496. //取得字段数
  497. if(empty($fnum))
  498. {
  499. $return_fr=Ebak_ReturnTbfield($b_dbname,$btb[$t],$b_autofield);
  500. $fieldnum=$return_fr['num'];
  501. $noautof=$return_fr['autof'];
  502. }
  503. else
  504. {
  505. $fieldnum=$fnum;
  506. $noautof=$thenof;
  507. }
  508. //完整插入
  509. $inf='';
  510. if($b_beover==1)
  511. {
  512. $inf='('.Ebak_ReturnInTbfield($b_dbname,$btb[$t]).')';
  513. }
  514. //十六进制
  515. $hexf='';
  516. if($b_bakdatatype==1)
  517. {
  518. $hexf=Ebak_ReturnInStrTbfield($b_dbname,$btb[$t]);
  519. }
  520. $b=0;
  521. while($r=$empire->fetch($sql))
  522. {
  523. $b=1;
  524. $s++;
  525. $dumpsql.="E_D(\"".$b_insertf." into `".$btb[$t]."`".$inf." values(";
  526. $first=1;
  527. for($i=0;$i<$fieldnum;$i++)
  528. {
  529. //首字段
  530. if(empty($first))
  531. {
  532. $dumpsql.=',';
  533. }
  534. else
  535. {
  536. $first=0;
  537. }
  538. $myi=$i+1;
  539. if(!isset($r[$i])||strstr($noautof,','.$myi.','))
  540. {
  541. $dumpsql.='NULL';
  542. }
  543. else
  544. {
  545. $dumpsql.=Ebak_ReSqlFtext($r[$i],$b_bakdatatype,$myi,$hexf);
  546. }
  547. }
  548. $dumpsql.=");\");\r\n";
  549. //是否超过限制
  550. if(strlen($dumpsql)>=$b_filesize*1024)
  551. {
  552. $p++;
  553. $sfile=$path."/".$btb[$t]."_".$p.".php";
  554. $dumpsql=$header.$dumpsql.$footer;
  555. WriteFiletext_n($sfile,$dumpsql);
  556. $empire->free($sql);
  557. //echo $fun_r['BakOneDataSuccess'].Ebak_EchoBakSt($btb[$t],$count,$t,$num,$s)."<script>self.location.href='phomebak.php?phome=BakExe&s=$s&p=$p&t=$t&mypath=$mypath&alltotal=$num&thenof=$noautof&fieldnum=$fieldnum&stime=$stime';</script>";
  558. echo"<meta http-equiv=\"refresh\" content=\"".$waitbaktime.";url=phomebak.php?phome=BakExe&s=$s&p=$p&t=$t&mypath=$mypath&alltotal=$num&thenof=$noautof&fieldnum=$fieldnum&stime=$stime&waitbaktime=$waitbaktime&collation=$collation\">".$fun_r['BakOneDataSuccess'].Ebak_EchoBakSt($btb[$t],$count,$t,$num,$s);
  559. exit();
  560. }
  561. }
  562. //最后一个备份
  563. if(empty($p)||$b==1)
  564. {
  565. $p++;
  566. $sfile=$path."/".$btb[$t]."_".$p.".php";
  567. $dumpsql=$header.$dumpsql.$footer;
  568. WriteFiletext_n($sfile,$dumpsql);
  569. }
  570. Ebak_RepFilenum($p,$btb[$t],$path);
  571. $t++;
  572. $empire->free($sql);
  573. //进入下一个表
  574. //echo $fun_r['OneTableBakSuccOne'].$btb[$t].$fun_r['OneTableBakSuccTwo']."<script>self.location.href='phomebak.php?phome=BakExe&s=0&p=0&t=$t&mypath=$mypath&stime=$stime';</script>";
  575. echo"<meta http-equiv=\"refresh\" content=\"".$waitbaktime.";url=phomebak.php?phome=BakExe&s=0&p=0&t=$t&mypath=$mypath&stime=$stime&waitbaktime=$waitbaktime\">".$fun_r['OneTableBakSuccOne'].$btb[$t-1].$fun_r['OneTableBakSuccTwo'];
  576. exit();
  577. }
  578. //执行备份(按记录)
  579. function Ebak_BakExeT($t,$s,$p,$mypath,$alltotal,$thenof,$fnum,$auf='',$aufval=0,$stime=0){
  580. global $empire,$bakpath,$limittype,$fun_r;
  581. if(empty($mypath))
  582. {
  583. printerror("ErrorUrl","history.go(-1)");
  584. }
  585. $path=$bakpath."/".$mypath;
  586. @include($path."/config.php");
  587. if(empty($b_table))
  588. {
  589. printerror("ErrorUrl","history.go(-1)");
  590. }
  591. $waitbaktime=(int)$_GET['waitbaktime'];
  592. if(empty($stime))
  593. {
  594. $stime=time();
  595. }
  596. $header="<?php
  597. require(\"../../inc/header.php\");
  598. ";
  599. $footer="
  600. require(\"../../inc/footer.php\");
  601. ?>";
  602. $btb=explode(",",$b_table);
  603. $count=count($btb);
  604. $t=(int)$t;
  605. $s=(int)$s;
  606. $p=(int)$p;
  607. //备份完毕
  608. if($t>=$count)
  609. {
  610. echo"<script>alert('".$fun_r['BakSuccess']."\\n\\n".$fun_r['TotalUseTime'].ToChangeUseTime($stime)."');self.location.href='ChangeDb.php';</script>";
  611. exit();
  612. }
  613. $dumpsql=Ebak_ReturnVer();
  614. //选择数据库
  615. $u=$empire->query("use `$b_dbname`");
  616. //编码
  617. if($b_dbchar=='auto')
  618. {
  619. if(empty($s))
  620. {
  621. $status_r=Ebak_GetTotal($b_dbname,$btb[$t]);
  622. $collation=Ebak_GetSetChar($status_r['Collation']);
  623. DoSetDbChar($collation);
  624. //总记录数
  625. $num=$limittype?-1:$status_r['Rows'];
  626. }
  627. else
  628. {
  629. $collation=$_GET['collation'];
  630. DoSetDbChar($collation);
  631. $num=(int)$alltotal;
  632. }
  633. $dumpsql.=Ebak_ReturnSetNames($collation);
  634. }
  635. else
  636. {
  637. DoSetDbChar($b_dbchar);
  638. if(empty($s))
  639. {
  640. //总记录数
  641. if($limittype)
  642. {
  643. $num=-1;
  644. }
  645. else
  646. {
  647. $status_r=Ebak_GetTotal($b_dbname,$btb[$t]);
  648. $num=$status_r['Rows'];
  649. }
  650. }
  651. else
  652. {
  653. $num=(int)$alltotal;
  654. }
  655. }
  656. //备份数据库结构
  657. if($b_stru&&empty($s))
  658. {
  659. $dumpsql.=Ebak_Returnstru($btb[$t],$b_strufour);
  660. }
  661. //取得字段数
  662. if(empty($fnum))
  663. {
  664. $return_fr=Ebak_ReturnTbfield($b_dbname,$btb[$t],$b_autofield);
  665. $fieldnum=$return_fr['num'];
  666. $noautof=$return_fr['autof'];
  667. $auf=$return_fr['auf'];
  668. }
  669. else
  670. {
  671. $fieldnum=$fnum;
  672. $noautof=$thenof;
  673. }
  674. //自动识别自增项
  675. $aufval=(int)$aufval;
  676. if($b_autoauf==1&&$auf)
  677. {
  678. $sql=$empire->query("select * from `".$btb[$t]."` where ".$auf.">".$aufval." order by ".$auf." limit $b_bakline");
  679. }
  680. else
  681. {
  682. $sql=$empire->query("select * from `".$btb[$t]."` limit $s,$b_bakline");
  683. }
  684. //完整插入
  685. $inf='';
  686. if($b_beover==1)
  687. {
  688. $inf='('.Ebak_ReturnInTbfield($b_dbname,$btb[$t]).')';
  689. }
  690. //十六进制
  691. $hexf='';
  692. if($b_bakdatatype==1)
  693. {
  694. $hexf=Ebak_ReturnInStrTbfield($b_dbname,$btb[$t]);
  695. }
  696. $b=0;
  697. while($r=$empire->fetch($sql))
  698. {
  699. if($auf)
  700. {
  701. $lastaufval=$r[$auf];
  702. }
  703. $b=1;
  704. $s++;
  705. $dumpsql.="E_D(\"".$b_insertf." into `".$btb[$t]."`".$inf." values(";
  706. $first=1;
  707. for($i=0;$i<$fieldnum;$i++)
  708. {
  709. //首字段
  710. if(empty($first))
  711. {
  712. $dumpsql.=',';
  713. }
  714. else
  715. {
  716. $first=0;
  717. }
  718. $myi=$i+1;
  719. if(!isset($r[$i])||strstr($noautof,','.$myi.','))
  720. {
  721. $dumpsql.='NULL';
  722. }
  723. else
  724. {
  725. $dumpsql.=Ebak_ReSqlFtext($r[$i],$b_bakdatatype,$myi,$hexf);
  726. }
  727. }
  728. $dumpsql.=");\");\r\n";
  729. }
  730. if(empty($b))
  731. {
  732. //最后一个备份
  733. if(empty($p))
  734. {
  735. $p++;
  736. $sfile=$path."/".$btb[$t]."_".$p.".php";
  737. $dumpsql=$header.$dumpsql.$footer;
  738. WriteFiletext_n($sfile,$dumpsql);
  739. }
  740. Ebak_RepFilenum($p,$btb[$t],$path);
  741. $t++;
  742. $empire->free($sql);
  743. //进入下一个表
  744. //echo $fun_r['OneTableBakSuccOne'].$btb[$t].$fun_r['OneTableBakSuccTwo']."<script>self.location.href='phomebak.php?phome=BakExeT&s=0&p=0&t=$t&mypath=$mypath&stime=$stime';</script>";
  745. echo"<meta http-equiv=\"refresh\" content=\"".$waitbaktime.";url=phomebak.php?phome=BakExeT&s=0&p=0&t=$t&mypath=$mypath&stime=$stime&waitbaktime=$waitbaktime\">".$fun_r['OneTableBakSuccOne'].$btb[$t-1].$fun_r['OneTableBakSuccTwo'];
  746. exit();
  747. }
  748. //进入下一组
  749. $p++;
  750. $sfile=$path."/".$btb[$t]."_".$p.".php";
  751. $dumpsql=$header.$dumpsql.$footer;
  752. WriteFiletext_n($sfile,$dumpsql);
  753. $empire->free($sql);
  754. //echo $fun_r['BakOneDataSuccess'].Ebak_EchoBakSt($btb[$t],$count,$t,$num,$s)."<script>self.location.href='phomebak.php?phome=BakExeT&s=$s&p=$p&t=$t&mypath=$mypath&alltotal=$num&thenof=$noautof&fieldnum=$fieldnum&auf=$auf&aufval=$lastaufval&stime=$stime';</script>";
  755. echo"<meta http-equiv=\"refresh\" content=\"".$waitbaktime.";url=phomebak.php?phome=BakExeT&s=$s&p=$p&t=$t&mypath=$mypath&alltotal=$num&thenof=$noautof&fieldnum=$fieldnum&auf=$auf&aufval=$lastaufval&stime=$stime&waitbaktime=$waitbaktime&collation=$collation\">".$fun_r['BakOneDataSuccess'].Ebak_EchoBakSt($btb[$t],$count,$t,$num,$s);
  756. exit();
  757. }
  758. //输出备份进度条
  759. function Ebak_EchoBakSt($tbname,$tbnum,$tb,$rnum,$r){
  760. $table=($tb+1).'/'.$tbnum;
  761. $record=$r;
  762. if($rnum!=-1)
  763. {
  764. $record=$r.'/'.$rnum;
  765. }
  766. ?>
  767. <br><br>
  768. <table width="90%" border="0" align="center" cellpadding="3" cellspacing="1">
  769. <tr><td height="25">Table Name&nbsp;:&nbsp;<b><?=$tbname?></b></td></tr>
  770. <tr><td height="25">Table&nbsp;:&nbsp;<b><?=$table?></b></td></tr>
  771. <tr><td height="25">Record&nbsp;:&nbsp;<b><?=$record?></b></td></tr>
  772. </table><br><br>
  773. <?
  774. }
  775. //输出恢复进度条
  776. function Ebak_EchoReDataSt($tbname,$tbnum,$tb,$pnum,$p){
  777. $table=($tb+1).'/'.$tbnum;
  778. $record=$p.'/'.$pnum;
  779. ?>
  780. <br><br>
  781. <table width="90%" border="0" align="center" cellpadding="3" cellspacing="1">
  782. <tr><td height="25">Table Name&nbsp;:&nbsp;<b><?=$tbname?></b></td></tr>
  783. <tr><td height="25">Table&nbsp;:&nbsp;<b><?=$table?></b></td></tr>
  784. <tr><td height="25">File&nbsp;:&nbsp;<b><?=$record?></b></td></tr>
  785. </table><br><br>
  786. <?
  787. }
  788. //取得表记录数
  789. function Ebak_GetTotal($dbname,$tbname){
  790. global $empire;
  791. /*
  792. $tr=$empire->fetch1("select count(*) AS total from ".$btb[$t]);
  793. $num=$tr[total];
  794. */
  795. $tr=$empire->fetch1("SHOW TABLE STATUS LIKE '".$tbname."';");
  796. return $tr;
  797. }
  798. //返回字符集set
  799. function Ebak_GetSetChar($char){
  800. global $empire;
  801. if(empty($char))
  802. {
  803. return '';
  804. }
  805. $r=$empire->fetch1("SHOW COLLATION LIKE '".$char."';");
  806. return $r['Charset'];
  807. }
  808. //返回表字段信息
  809. function Ebak_ReturnTbfield($dbname,$tbname,$autofield){
  810. global $empire;
  811. $sql=$empire->query("SHOW FIELDS FROM `".$tbname."`");
  812. $i=0;//字段数
  813. $autof=",";//去除自增字段列表
  814. $f='';//自增字段名
  815. while($r=$empire->fetch($sql))
  816. {
  817. $i++;
  818. if(strstr($autofield,",".$tbname.".".$r[Field].","))
  819. {
  820. $autof.=$i.",";
  821. }
  822. if($r['Extra']=='auto_increment')
  823. {
  824. $f=$r['Field'];
  825. }
  826. }
  827. $return_r['num']=$i;
  828. $return_r['autof']=$autof;
  829. $return_r['auf']=$f;
  830. return $return_r;
  831. }
  832. //返回插入字段
  833. function Ebak_ReturnInTbfield($dbname,$tbname){
  834. global $empire;
  835. $sql=$empire->query("SHOW FIELDS FROM `".$tbname."`");
  836. $f='';
  837. $dh='';
  838. while($r=$empire->fetch($sql))
  839. {
  840. $f.=$dh.'`'.$r['Field'].'`';
  841. $dh=',';
  842. }
  843. return $f;
  844. }
  845. //返回字符字段
  846. function Ebak_ReturnInStrTbfield($dbname,$tbname){
  847. global $empire;
  848. $sql=$empire->query("SHOW FIELDS FROM `".$tbname."`");
  849. $i=0;
  850. $f='';
  851. $dh='';
  852. while($r=$empire->fetch($sql))
  853. {
  854. $i++;
  855. if(!(stristr($r[Type],'char')||stristr($r[Type],'text')))
  856. {
  857. continue;
  858. }
  859. $f.=$dh.$i;
  860. $dh=',';
  861. }
  862. if($f)
  863. {
  864. $f=','.$f.',';
  865. }
  866. return $f;
  867. }
  868. //字符过虑
  869. function escape_str($str){
  870. $str=mysql_escape_string($str);
  871. $str=str_replace('\\\'','\'\'',$str);
  872. $str=str_replace("\\\\","\\\\\\\\",$str);
  873. $str=str_replace('$','\$',$str);
  874. return $str;
  875. }
  876. //返回字段内容
  877. function Ebak_ReSqlFtext($str,$bakdatatype,$i,$tbstrf){
  878. if($bakdatatype==1&&!empty($str)&&strstr($tbstrf,','.$i.','))
  879. {
  880. $restr='0x'.bin2hex($str);
  881. }
  882. else
  883. {
  884. $restr='\''.escape_str($str).'\'';
  885. }
  886. return $restr;
  887. }
  888. //替换文件数
  889. function Ebak_RepFilenum($p,$table,$path){
  890. if(empty($p))
  891. {$p=0;}
  892. $file=$path."/config.php";
  893. $text=ReadFiletext($file);
  894. $rep1="\$tb[".$table."]=0;";
  895. $rep2="\$tb[".$table."]=".$p.";";
  896. $text=str_replace($rep1,$rep2,$text);
  897. WriteFiletext_n($file,$text);
  898. }
  899. //执行SQL
  900. function E_D($sql){
  901. global $empire;
  902. $empire->query($sql);
  903. }
  904. //建立表
  905. function E_C($sql){
  906. global $empire;
  907. $empire->query(Ebak_AddDbchar($sql));
  908. }
  909. //转为Mysql4.0格式
  910. function Ebak_ToMysqlFour($query){
  911. $exp="ENGINE=";
  912. if(!strstr($query,$exp))
  913. {
  914. return $query;
  915. }
  916. $exp1=" ";
  917. $r=explode($exp,$query);
  918. //取得表类型
  919. $r1=explode($exp1,$r[1]);
  920. $returnquery=$r[0]."TYPE=".$r1[0];
  921. return $returnquery;
  922. }
  923. //返回数据库结构
  924. function Ebak_Returnstru($table,$strufour){
  925. global $empire;
  926. $dumpsql.="E_D(\"DROP TABLE IF EXISTS `".$table."`;\");\r\n";
  927. //设置引号
  928. $usql=$empire->query("SET SQL_QUOTE_SHOW_CREATE=1;");
  929. //数据表结构
  930. $r=$empire->fetch1("SHOW CREATE TABLE `$table`;");
  931. $create=str_replace("\"","\\\"",$r[1]);
  932. //转为4.0格式
  933. if($strufour)
  934. {
  935. $create=Ebak_ToMysqlFour($create);
  936. }
  937. $dumpsql.="E_C(\"".$create."\");\r\n";
  938. return $dumpsql;
  939. }
  940. //返回设置编码
  941. function Ebak_ReturnSetNames($char){
  942. if(empty($char))
  943. {
  944. return '';
  945. }
  946. $dumpsql="DoSetDbChar('".$char."');\r\n";
  947. return $dumpsql;
  948. }
  949. //去除字段中的编码
  950. function Ebak_ReplaceFieldChar($sql){
  951. global $phome_db_ver;
  952. if($phome_db_ver=='4.0'&&strstr($sql,' character set '))
  953. {
  954. $preg_str="/ character set (.+?) collate (.+?) /is";
  955. $sql=preg_replace($preg_str,' ',$sql);
  956. }
  957. return $sql;
  958. }
  959. //加编码
  960. function Ebak_AddDbchar($sql){
  961. global $phome_db_ver,$phome_db_char,$b_dbchar;
  962. //加编码
  963. if($phome_db_ver>='4.1'&&!strstr($sql,'ENGINE=')&&($phome_db_char||$b_dbchar)&&$b_dbchar!='auto')
  964. {
  965. $dbcharset=$b_dbchar?$b_dbchar:$phome_db_char;
  966. $sql=Ebak_DoCreateTable($sql,$phome_db_ver,$dbcharset);
  967. }
  968. elseif($phome_db_ver=='4.0'&&strstr($sql,'ENGINE='))
  969. {
  970. $sql=Ebak_ToMysqlFour($sql);
  971. }
  972. //去除字段中的编码
  973. $sql=Ebak_ReplaceFieldChar($sql);
  974. return $sql;
  975. }
  976. //建表
  977. function Ebak_DoCreateTable($sql,$mysqlver,$dbcharset){
  978. $type=strtoupper(preg_replace("/^\s*CREATE TABLE\s+.+\s+\(.+?\).*(ENGINE|TYPE)\s*=\s*([a-z]+?).*$/isU","\\2",$sql));
  979. $type=in_array($type,array('MYISAM','HEAP'))?$type:'MYISAM';
  980. return preg_replace("/^\s*(CREATE TABLE\s+.+\s+\(.+?\)).*$/isU","\\1",$sql).
  981. ($mysqlver>='4.1'?" ENGINE=$type DEFAULT CHARSET=$dbcharset":" TYPE=$type");
  982. }
  983. //返回版权信息
  984. function Ebak_ReturnVer(){
  985. $string="
  986. /*
  987. SoftName : EmpireBak Version 2010
  988. Author : wm_chief
  989. Copyright: Powered by www.phome.net
  990. */
  991. ";
  992. return $string;
  993. }
  994. //导入数据
  995. function Ebak_ReData($add,$mypath){
  996. global $empire,$bakpath;
  997. if(empty($mypath)||empty($add[mydbname]))
  998. {
  999. printerror("EmptyReData","history.go(-1)");
  1000. }
  1001. $path=$bakpath."/".$mypath;
  1002. if(!file_exists($path))
  1003. {
  1004. printerror("PathNotExists","history.go(-1)");
  1005. }
  1006. @include($path."/config.php");
  1007. if(empty($b_table))
  1008. {
  1009. printerror("FailBakVar","history.go(-1)");
  1010. }
  1011. $waitbaktime=(int)$add['waitbaktime'];
  1012. $btb=explode(",",$b_table);
  1013. $nfile=$path."/".$btb[0]."_1.php?t=0&p=0&mydbname=$add[mydbname]&mypath=$mypath&waitbaktime=$waitbaktime";
  1014. Header("Location:$nfile");
  1015. exit();
  1016. }
  1017. //运行SQL
  1018. function Ebak_DoRunQuery($sql,$mydbchar,$mydbver){
  1019. $sql=str_replace("\r","\n",$sql);
  1020. $ret=array();
  1021. $num=0;
  1022. foreach(explode(";\n",trim($sql)) as $query)
  1023. {
  1024. $queries=explode("\n",trim($query));
  1025. foreach($queries as $query)
  1026. {
  1027. $ret[$num].=$query[0]=='#'||$query[0].$query[1]=='--'?'':$query;
  1028. }
  1029. $num++;
  1030. }
  1031. unset($sql);
  1032. foreach($ret as $query)
  1033. {
  1034. $query=trim($query);
  1035. if($query)
  1036. {
  1037. if(substr($query,0,12)=='CREATE TABLE')
  1038. {
  1039. mysql_query(Ebak_DoCreateTable($query,$mydbver,$mydbchar)) or die(mysql_error()."<br>".$query);
  1040. }
  1041. else
  1042. {
  1043. mysql_query($query) or die(mysql_error()."<br>".$query);
  1044. }
  1045. }
  1046. }
  1047. }
  1048. //上传文件
  1049. function Ebak_DoTranFile($file,$newfile){
  1050. $cp=@move_uploaded_file($file,$newfile);
  1051. return $cp;
  1052. }
  1053. //函数是否存在
  1054. function Ebak_HaveFun($fun){
  1055. if(function_exists($fun))
  1056. {
  1057. $word=1;
  1058. }
  1059. else
  1060. {
  1061. $word=0;
  1062. }
  1063. return $word;
  1064. }
  1065. //是否支持ICONV库
  1066. function Ebak_GetIconv(){
  1067. $can=Ebak_HaveFun("iconv");
  1068. return $can;
  1069. }
  1070. //编码转换
  1071. function Ebak_ChangeChar($str,$oldchar,$newchar){
  1072. //是否支持iconv
  1073. if(!Ebak_HaveFun("iconv"))
  1074. {
  1075. return $str;
  1076. }
  1077. if(!empty($newchar))
  1078. {
  1079. $str=iconv($oldchar,$newchar,$str);
  1080. }
  1081. return $str;
  1082. }
  1083. //返回语言目录
  1084. function Ebak_ReturnLang(){
  1085. global $ebaklang,$langcharr;
  1086. $count=count($langcharr);
  1087. $l='';
  1088. for($i=0;$i<$count;$i++)
  1089. {
  1090. $f=explode(',',$langcharr[$i]);
  1091. if(!file_exists('lang/'.$f[0]))
  1092. {
  1093. continue;
  1094. }
  1095. $select='';
  1096. if($f[0]==$ebaklang)
  1097. {
  1098. $select=' selected';
  1099. }
  1100. $l.="<option value='".$i."'".$select.">".$f[2]." (".$f[1].")</option>";
  1101. }
  1102. return $l;
  1103. }
  1104. //选择语言
  1105. function Ebak_ChangeLanguage($add){
  1106. global $langcharr;
  1107. $l=(int)$add['l'];
  1108. if($langcharr[$l])
  1109. {
  1110. $lifetime=time()+365*24*3600;
  1111. esetcookie('loginlangid',$l,$lifetime);
  1112. }
  1113. if(!$add['from'])
  1114. {
  1115. $add['from']='index.php';
  1116. }
  1117. echo"<script>parent.location.href='$add[from]';</script>";
  1118. exit();
  1119. }
  1120. //返回数据库编码列表
  1121. function Ebak_ReturnDbCharList($dbchar){
  1122. global $dbcharr;
  1123. $count=count($dbcharr);
  1124. $c='';
  1125. for($i=0;$i<$count;$i++)
  1126. {
  1127. $select='';
  1128. if($dbcharr[$i]==$dbchar)
  1129. {
  1130. $select=' selected';
  1131. }
  1132. $c.="<option value='".$dbcharr[$i]."'".$select.">".$dbcharr[$i]."</option>";
  1133. }
  1134. return $c;
  1135. }
  1136. //设置转向备份页面
  1137. function Ebak_SetGotoBak($file){
  1138. if(strstr($file,'.')||strstr($file,'/')||strstr($file,"\\"))
  1139. {
  1140. printerror("FailSetSavename","history.go(-1)");
  1141. }
  1142. @include('setsave/'.$file);
  1143. Header("Location:ChangeTable.php?mydbname=$dmydbname&savefilename=$file");
  1144. exit();
  1145. }
  1146. //转向恢复页面
  1147. function Ebak_PathGotoRedata($path){
  1148. global $bakpath;
  1149. if(strstr($path,".."))
  1150. {printerror("NotChangeDelPath","history.go(-1)");}
  1151. if(!trim($path))
  1152. {printerror("NotChangeDelPath","history.go(-1)");}
  1153. $repath=$bakpath."/".$path;
  1154. if(!file_exists($repath))
  1155. {
  1156. printerror("DelPathNotExists","history.go(-1)");
  1157. }
  1158. @include $repath.'/config.php';
  1159. Header("Location:ReData.php?mydbname=$b_dbname&mypath=$path");
  1160. exit();
  1161. }
  1162. //替换字符
  1163. function Ebak_RepInfoZZ($text,$exp,$enews=0){
  1164. $text=str_replace("*","(.*?)",$text);
  1165. $text=str_replace("[!--".$exp."--]","(.*?)",$text);
  1166. //$text=str_replace("\\","\\\\",$text);
  1167. //$text=str_replace("^","\^",$text);
  1168. //$text=str_replace("\"","\"",$text);
  1169. $text=str_replace("/","\/",$text);
  1170. $text="/".$text."/is";
  1171. return $text;
  1172. }
  1173. //正则替换信息
  1174. function Ebak_DoRepFiletextZz($oldword,$newword,$text){
  1175. $zztext=Ebak_RepInfoZZ($oldword,"empire-bak-wm.chief-phome",0);
  1176. $text=preg_replace($zztext,$newword,$text);
  1177. return $text;
  1178. }
  1179. ?>