PageRenderTime 69ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/backup/mysql_backup/libs/backup.class.php

https://github.com/sad1990/--
PHP | 935 lines | 640 code | 133 blank | 162 comment | 83 complexity | c80813461e2ceb5841dc0a33d78a02af MD5 | raw file
Possible License(s): CC-BY-3.0, GPL-2.0, Apache-2.0
  1. <?php
  2. define ('_CONNECTION_ERROR_',"DB Connection error, please <a href=\"?option=databaseAdmin&go=config\">configure</a> your application.");
  3. /**
  4. * backup class : Backup class
  5. *
  6. * @package
  7. * @author Ben Yacoub Hatem <hatem@php.net>
  8. * @website http://www.phptunisie.net/
  9. * @copyright Copyright (c) 2004
  10. * @version $Id$ - 10/03/2004 15:22:00 - backup.class.php
  11. * @access public
  12. **/
  13. class backup extends db2{
  14. var $backupdir = "./backup";
  15. var $compression = "bz2";
  16. var $color1 = "#F57E1D";
  17. var $color2 = "#F5F6C4";
  18. var $color3 = "#FFFF40";
  19. var $mysqldump = true;
  20. var $default_sort_order = "DateDesc";
  21. var $file_mod = "0777";
  22. var $file_name = "";
  23. /**
  24. * Constructor
  25. * @access protected
  26. */
  27. function backup(){
  28. global $GonxAdmin;
  29. $this->compression = $GonxAdmin["compression_default"];
  30. }
  31. /**
  32. * backup::version() Return class version
  33. *
  34. * @return
  35. **/
  36. function version(){
  37. return "1.0.8-PRE2";
  38. }
  39. /**
  40. *
  41. * @access public
  42. * @return void
  43. **/
  44. function tables_menu(){
  45. global $GONX;
  46. $color3 = $this->color3;
  47. $color2 = $this->color2;
  48. $color1 = $this->color1;
  49. $res = "\n<form action='?go=backuptables' method='post'>
  50. <table border=0 cellpadding=5>
  51. <tr bgcolor=\"$color1\">
  52. \t<th>Table</th>
  53. \t<th>Rows</th>
  54. \t<th>Create_time</th>
  55. \t<th>Update_time</th>
  56. \t<th>Check_time</th>
  57. </tr>\n\n";
  58. $result = $this->query('SHOW TABLE STATUS');
  59. $i = 0; $bgcolor = $color3;
  60. while ($table = @$this->fetch_array($result)) {
  61. if ($table["Update_time"]!=$table["Create_time"]) {
  62. $l1 = "<label for=\"tables$i\">";
  63. $l2 = "</label>";
  64. } else {
  65. $l1 = $l2 = "";
  66. };
  67. $res .= "<tr bgcolor=\"$bgcolor\">
  68. \t<th align=left><input type='checkbox' id='tables$i' name='tables[]' value='".$table["Name"]."' /> ".$table["Name"]."</th>
  69. \t<td>".$table["Rows"]."</td>
  70. \t<td>$l1".$table["Create_time"]."$l2</td>
  71. \t<td>$l1".$table["Update_time"]."$l2</td>
  72. \t<td>".$table["Check_time"]."</td>
  73. </tr>
  74. ";
  75. if ($bgcolor==$color3) {
  76. $bgcolor = $color2;
  77. } else $bgcolor = $color3;
  78. $i++;
  79. }
  80. $res .= "</table>
  81. <input type='checkbox' name='structonly' value='Yes'> ".$GONX["structonly"]."<br><br>
  82. <input type='radio' name='chg' onclick='for(i=0;i<$i;i++){document.getElementById(\"tables\"+i).checked=true;}'> ".$GONX["checkall"]." &nbsp;&nbsp;
  83. <input type='radio' name='chg' onclick='for(i=0;i<$i;i++){document.getElementById(\"tables\"+i).checked=false;}'> ".$GONX["uncheckall"]." &nbsp;&nbsp;
  84. <p align=right><input type='submit' class='button'></p>
  85. ".$GONX["tablesmenuhelp"]."
  86. </form>\n";
  87. return $res;
  88. }
  89. /**
  90. *
  91. * @access public
  92. * @return void
  93. **/
  94. function tables_backup($tables,$structonly){
  95. global $GONX;
  96. foreach($tables as $v){
  97. $res = $this-> query("SHOW CREATE TABLE " . $this -> dbName . "." .$v);
  98. while ($resu[] = $this -> get_data()) {
  99. }
  100. }
  101. foreach($resu as $key => $val) {
  102. $tbl_name_status = $this->valid_table_name($val[0]);
  103. if (trim($val[0]) !== "" and $tbl_name_status) {
  104. $Dx_Create_Tables .= "
  105. # Drop table '" . $val[0] . "' if exist
  106. DROP TABLE IF EXISTS " . $val[0] . ";
  107. # create table '" . $val[0] . "'
  108. " . $val[1] . " ;
  109. \r\n";
  110. if ($structonly!="Yes") {
  111. $query = "Insert into `$val[0]` (";
  112. $this -> query("LOCK TABLES $val[0] WRITE");
  113. $qresult = $this -> query("Select * from $val[0]");
  114. while ($line = $this -> fetch_array($qresult)) {
  115. unset($fields, $values);
  116. $j = 0;
  117. while (list($col_name, $col_value) = each($line)) {
  118. if (!is_int($col_name)) {
  119. $fields .= "`$col_name`,";
  120. $values .= "'" . $this->escape_string($col_value) . "',";
  121. }
  122. }
  123. $fields = substr($fields, 0, strlen($fields)-1);
  124. $values = substr($values, 0, strlen($values)-1);
  125. $myquery = $query . $fields . ") values (" . $values . ");";
  126. $Dx_Create_Tables .= "\r\n<xquery>
  127. " . $myquery . "
  128. </xquery>\r\n" ;
  129. }
  130. $this -> query("UNLOCK TABLES;");
  131. }
  132. } elseif (!$tbl_name_status){
  133. $err_msg .= "<font color=red>".$GONX["ignoredtables"]." ".$val[0]." - ".$GONX["reservedwords"].".</font><br>\n";
  134. }
  135. }
  136. if (!is_dir($this->backupdir)) {
  137. @mkdir($this->backupdir,0755);
  138. }
  139. if (sizeof($tables)==1) {
  140. $prefix = "[".$tables[0]."]";
  141. } else $prefix = "[".sizeof($tables)."tables]";
  142. switch($this->compression){
  143. case "bz2":
  144. // Random 5 characters to append to file names to prevent name guessing
  145. $rand = substr(md5(microtime()),rand(0,26),5);
  146. $fname = $this->dbName.date("Y-m-d H-i-s")."_".$rand.".bz2";
  147. touch($this->backupdir."/".$fname);
  148. $fp = bzopen($this->backupdir."/".$fname, "w");
  149. bzwrite($fp, $Dx_Create_Tables);
  150. bzclose($fp);
  151. break;
  152. case "zlib":
  153. // Random 5 characters to append to file names to prevent name guessing
  154. $rand = substr(md5(microtime()),rand(0,26),5);
  155. $fname = $this->dbName."_".date("Y-m-d H-i-s")."_".$rand.".gz";
  156. touch($this->backupdir."/".$fname);
  157. $fp = gzopen($this->backupdir."/".$fname, "w");
  158. gzwrite($fp, $Dx_Create_Tables);
  159. gzclose($fp);
  160. break;
  161. default:
  162. // Random 5 characters to append to file names to prevent name guessing
  163. $rand = substr(md5(microtime()),rand(0,26),5);
  164. $fname = $this->dbName.date("Y-m-d_H-i-s")."_".$rand.".sql";
  165. touch($this->backupdir."/".$fname);
  166. $fp = fopen($this->backupdir."/".$fname, "w");
  167. fwrite($fp, $Dx_Create_Tables);
  168. fclose($fp);
  169. break;
  170. } // switch
  171. return "$err_msg<font color=green>".$GONX["backup"]." ".$this->dbName." ".$GONX["iscorrectcreat"]." : ".$this->backupdir."/$fname</font>";
  172. }
  173. /**
  174. * backup::generate() Create a backup file of the DB
  175. *
  176. * @return
  177. **/
  178. function generate(){
  179. global $GONX, $GonxAdmin;
  180. $result = @$this->list_tables($this->dbName);
  181. while ($table = @$this->fetch_row($result)) {
  182. $res = $this-> query("SHOW CREATE TABLE " . $this->dbName . "." . $table[0]);
  183. while ($resu[] = $this -> get_data()) {
  184. }
  185. }
  186. foreach($resu as $key => $val) {
  187. $tbl_name_status = $this->valid_table_name($val[0]);
  188. if (trim($val[0]) !== "" and $tbl_name_status) {
  189. $Dx_Create_Tables .= "";
  190. $query = "Insert into `$val[0]` (";
  191. $this -> query("LOCK TABLES $val[0] WRITE");
  192. $qresult = $this -> query("Select * from $val[0]");
  193. while ($line = $this -> fetch_array($qresult)) {
  194. unset($fields, $values);
  195. $j = 0;
  196. while (list($col_name, $col_value) = each($line)) {
  197. if (!is_int($col_name)) {
  198. $fields .= "`$col_name`,";
  199. $values .= "'" . $this->escape_string($col_value) . "',";
  200. }
  201. }
  202. $fields = substr($fields, 0, strlen($fields)-1);
  203. $values = substr($values, 0, strlen($values)-1);
  204. $myquery = $query . $fields . ") values (" . $values . ");";
  205. $Dx_Create_Tables .= $myquery . "\r\n" ;
  206. }
  207. $this -> query("UNLOCK TABLES;");
  208. } elseif (!$tbl_name_status){
  209. $err_msg .= "<font color=red>Ignored table ".$val[0]." - Reserved SQL word.</font><br>\n";
  210. }
  211. }
  212. if (!is_dir($this->backupdir)) {
  213. @mkdir($this->backupdir,octdec($this->file_mod));
  214. }
  215. switch($this->compression){
  216. case "bz2":
  217. $fname = "MySQL-Data-Backup-" . $this->dbName."-".date("Y-m-d H-i-s").".bz2";
  218. touch($this->backupdir."/".$fname);
  219. $fp = bzopen($this->backupdir."/".$fname, "w");
  220. bzwrite($fp, $Dx_Create_Tables);
  221. bzclose($fp);
  222. break;
  223. case "zlib":
  224. $fname = "MySQL-Data-Backup-" . $this->dbName."-".date("Y-m-d H-i-s").".gz";
  225. touch($this->backupdir."/".$fname);
  226. $fp = gzopen($this->backupdir."/".$fname, "w");
  227. gzwrite($fp, $Dx_Create_Tables);
  228. gzclose($fp);
  229. break;
  230. default:
  231. // Random 5 characters to append to file names to prevent name guessing
  232. $rand = substr(md5(microtime()),rand(0,26),5);
  233. $fname = "MySQL_Data_".date("Y-m-d_H-i-s")."_".$rand.".sql";
  234. touch($this->backupdir."/".$fname);
  235. $fp = fopen($this->backupdir."/".$fname, "w");
  236. fwrite($fp, $Dx_Create_Tables);
  237. fclose($fp);
  238. break;
  239. } // switch
  240. // Random 5 characters to append to file names to prevent name guessing
  241. $rand = substr(md5(microtime()),rand(0,26),5);
  242. $this->filename = date("Y-m-d_H-i-s")."_".$rand.".sql";
  243. return "$err_msg<font color=green>".$GONX["backup"]." ".$this->dbName." ".$GONX["iscorrectcreat"]." : ".$this->backupdir."/$fname</font>";
  244. }
  245. /**
  246. * backup::import() Import a Backup file to DB
  247. *
  248. * @param string $bfile
  249. * @return
  250. **/
  251. function import($bfile = ""){
  252. global $GONX,$GonxAdmin;
  253. set_time_limit(0);
  254. if (isset($_GET["importdump"])) {
  255. if (is_file($this->backupdir."/".$bfile)) {
  256. switch($GonxAdmin["compression_default"]){
  257. case "bz2":
  258. $bz = bzopen($this->backupdir."/".$bfile, "r");
  259. while (!feof($bz)) {
  260. $contents .= bzread($bz, 4096);
  261. }
  262. // $contents = bzread($bz, filesize($this->backupdir."/".$bfile)*1000); // just a hack coz feof doesn't wrk for me
  263. bzclose($bz);
  264. break;
  265. case "zlib":
  266. $bz = gzopen($this->backupdir."/".$bfile, "r");
  267. $contents = gzread($bz, filesize($this->backupdir."/".$bfile)*1000); // just a hack coz feof doesn't wrk for me
  268. gzclose($bz);
  269. break;
  270. default:
  271. $bz = fopen($this->backupdir."/".$bfile, "r");
  272. $contents = fread($bz, filesize($this->backupdir."/".$bfile)*1000); // just a hack coz feof doesn't wrk for me
  273. fclose($bz);
  274. break;
  275. } // switch
  276. // Convert to mysql dump format
  277. $contents = str_replace("<xquery>", "", $contents);
  278. $contents = str_replace("</xquery>", "", $contents);
  279. // Create temporary file
  280. touch($this->backupdir."/temp.sql");
  281. $fp = fopen($this->backupdir."/temp.sql", "w");
  282. fwrite($fp, $contents);
  283. fclose($fp); unset($contents);
  284. // Execute MySQL Dump
  285. @shell_exec($GonxAdmin["mysqldump"]." --host ".$GonxAdmin["dbhost"]." --user=".$GonxAdmin["dbuser"]." --pass=".$GonxAdmin["dbpass"]." --databases ".$GonxAdmin["dbname"]." < ".$this->backupdir."/temp.sql");
  286. // Remove the temporary file.
  287. @unlink($this->backupdir."/temp.sql");
  288. return "<font color=green> $bfile ".$GONX["iscorrectimport"]." </font>";
  289. } else return FALSE;
  290. }
  291. if (is_file($this->backupdir."/".$bfile)) { // File existe, import it
  292. switch($GonxAdmin["compression_default"]){
  293. case "bz2":
  294. $bz = bzopen($this->backupdir."/".$bfile, "r");
  295. while (!feof($bz)) {
  296. $contents .= bzread($bz, 4096);
  297. }
  298. // $contents = bzread($bz, filesize($this->backupdir."/".$bfile)*1000); // just a hack coz feof doesn't wrk for me
  299. bzclose($bz);
  300. break;
  301. case "zlib":
  302. $bz = gzopen($this->backupdir."/".$bfile, "r");
  303. $contents = gzread($bz, filesize($this->backupdir."/".$bfile)*1000); // just a hack coz feof doesn't wrk for me
  304. gzclose($bz);
  305. break;
  306. default:
  307. $bz = fopen($this->backupdir."/".$bfile, "r");
  308. $contents = fread($bz, filesize($this->backupdir."/".$bfile)*1000); // just a hack coz feof doesn't wrk for me
  309. fclose($bz);
  310. break;
  311. } // switch
  312. preg_match_all("'<xquery[?>]*?>(.*?)</xquery>'si" , $contents, $requetes);
  313. //<?
  314. unset($contents);
  315. foreach($requetes[1] as $key => $val) {
  316. $this -> query(trim($val));
  317. }
  318. return "<font color=green> $bfile ".$GONX["iscorrectimport"]." </font>";
  319. } else { // Erronous file, read dir, and list available backup file.
  320. return false;
  321. }
  322. }
  323. /**
  324. * backup::importfromfile() Download and Import a Backup file to DB
  325. *
  326. * @param string $bfile
  327. * @return
  328. **/
  329. function importfromfile(){
  330. global $GONX,$HTTP_POST_FILES;
  331. @set_time_limit(0);
  332. $bfile = $HTTP_POST_FILES["backupfile"];
  333. $pathinfo = pathinfo($bfile["name"]);
  334. $compression = $pathinfo["extension"];
  335. if ($bfile["error"]==0) { // File existe, import it
  336. switch($compression){
  337. case "bz2":
  338. $bz = bzopen($bfile["tmp_name"], "r");
  339. $contents = bzread($bz, $bfile["size"]); // just a hack coz feof doesn't wrk for me
  340. bzclose($bz);
  341. break;
  342. case "gz":
  343. $gz = gzopen($bfile["tmp_name"], "r");
  344. $contents = gzread($gz, $bfile["size"]); // just a hack coz feof doesn't wrk for me
  345. bzclose($gz);
  346. break;
  347. default:
  348. $f = fopen($bfile["tmp_name"], "r");
  349. $contents = fread($f, $bfile["size"]); // just a hack coz feof doesn't wrk for me
  350. fclose($f);
  351. break;
  352. } // switch
  353. preg_match_all("'<xquery[?>]*?>(.*?)</xquery>'si" , $contents, $requetes);
  354. //<?
  355. foreach($requetes[1] as $key => $val) {
  356. $this -> query(trim($val));
  357. }
  358. return "<font color=green> ".$bfile["name"]." ".$GONX["iscorrectimport"]." </font>";
  359. } else { // Erronous file, read dir, and list available backup file.
  360. return $this->listbackups();
  361. }
  362. }
  363. /**
  364. * backup::listbackups() List available backup
  365. *
  366. * @return
  367. **/
  368. function listbackups(){
  369. global $GONX,$GonxAdmin,$page,$orderby;
  370. $pagesize = $GonxAdmin["pagedisplay"];
  371. $GonxOrder = array("DateAsc","NameAsc","NameDesc","SizeAsc","SizeDesc","DateDesc");
  372. if ($orderby=="" or !in_array($orderby,$GonxOrder )) {
  373. $orderby = $this->default_sort_order;
  374. }
  375. if( !isset( $page ) or ($page<=0) ){
  376. $page = 1;
  377. $from = $page-1;
  378. $to = ($pagesize*$page);
  379. } elseif ($page ==1){
  380. $from = 0;
  381. $to = ($pagesize*($page+1)-$pagesize);
  382. } else {
  383. $from = $pagesize * ($page-1);
  384. $to = ($pagesize*($page+1)-$page *$pagesize);
  385. }
  386. $res = "
  387. <script language=\"JavaScript\" type=\"text/javascript\">
  388. <!--
  389. //author Philippe BENVENISTE <info@pommef.com>
  390. //copyright Pommef - IdŽes et Changements (c) 2004
  391. function IECColor1(el) {
  392. IEC_obj2.IECColor(el);
  393. IEC_obj1.IECColor(el);
  394. }
  395. function IECColor2(el){
  396. IEC_obj1.IECColor(el);
  397. IEC_obj2.IECColor(el);
  398. if(ConfirmDelete()){
  399. return true;
  400. } else {
  401. document.getElementById(el).style.background = IEC_obj1.BG2;
  402. document.forms[\"bform\"].reset();
  403. return false;
  404. }
  405. }
  406. function IECColorClass(BG1, BG2){
  407. this.gvar = 10000;
  408. this.BG1 = BG1;
  409. this.BG2 = BG2;
  410. this.IECColor=IECColor;
  411. }
  412. function IECColor(el) {
  413. document.getElementById(el).style.background = this.BG1;
  414. if(this.gvar < 9000 && this.gvar != el)
  415. document.getElementById(this.gvar).style.background = this.BG2;
  416. this.gvar = el;
  417. }
  418. IEC_obj1 = new IECColorClass('khaki','#F6F6F6');
  419. IEC_obj2 = new IECColorClass('#CF2B5A','#F6F6F6');
  420. //-->
  421. </script>
  422. <form method=get action=\"?\" name=bform><b>".$GONX["selectbackupfile"]." :</b><br/><br/>\n";
  423. if (!is_dir($this->backupdir)) {
  424. @mkdir($this->backupdir,octdec($this->file_mod));
  425. }
  426. $d = dir($this->backupdir);
  427. $i = $BackupSize = 0;
  428. while (false !== ($entry = $d->read())) {
  429. if ($entry!="." and $entry!=".." and preg_match('/.(bz2|gz|sql)$/',$entry)) {
  430. $mtime = date ("F d Y H:i:s.", filemtime($this->backupdir."/".$entry));
  431. $time = filemtime($this->backupdir."/".$entry);
  432. $size = filesize($this->backupdir."/".$entry);
  433. $fsize = round($size/1024);
  434. $GonxBackups[$i]["fname"] = $entry;
  435. $GonxBackups[$i]["mtime"] = $mtime;
  436. $GonxBackups[$i]["time"] = $time;
  437. $GonxBackups[$i]["fsize"] = $fsize;
  438. $GonxBackups[$i]["size"] = $size;
  439. $BackupSize += $fsize;
  440. $i++;
  441. }
  442. }
  443. if ($i==0) {
  444. $res .= "<ul><li>".$GONX["nobckupfile"]."</li></ul>";
  445. } else {
  446. /**
  447. * Pagination
  448. */
  449. $allpages = round(sizeof($GonxBackups)/$pagesize);
  450. $all_rest = $allpages - $allpages*$pagesize;
  451. if ($all_rest > 0) {$allpages++; }
  452. if ($page<$allpages)
  453. {
  454. $next = "<a href=\"?option=databaseAdmin&go=list&amp;page=".($page+1)."&orderby=$orderby\" class=tab-s>".$GONX["next"]."</a>";
  455. } else $next="";
  456. if ($page>1)
  457. {
  458. $prev = "<a href=\"?option=databaseAdmin&go=list&amp;page=".($page-1)."&orderby=$orderby\" class=tab-s>".$GONX["prev"]."</a>";
  459. } else $prev ="";
  460. $links = "";
  461. for ($i=0; $i<$allpages; $i++)
  462. {
  463. if (($i+1) == $page)
  464. {
  465. $links .= "<span class=tab-s> ".($i+1)." </span>";
  466. } else {
  467. $links .= "<a href=\"?option=databaseAdmin&go=list&amp;page=".($i+1)."&orderby=$orderby\" class=tab-g> ".($i+1)." </a>";
  468. }
  469. }
  470. /**
  471. * Order by
  472. */
  473. $OrderMenu = "<select class=button OnChange=\"location.href='?option=databaseAdmin&go=list&page=$page&orderby='+ChgOrder.options[selectedIndex].value\" name=\"ChgOrder\">\n";
  474. foreach($GonxOrder as $v){
  475. if ($v==$orderby) {
  476. $sel = " selected";
  477. } else $sel ="";
  478. $OrderMenu .= "<option$sel value=\"$v\">$v</option>\n";
  479. }
  480. $OrderMenu .= "</select>\n";
  481. switch($orderby){
  482. case "DateAsc":
  483. usort($GonxBackups, array("backup","DateSortAsc"));
  484. break;
  485. case "NameAsc":
  486. usort($GonxBackups, array("backup","NameSortAsc"));
  487. break;
  488. case "NameDesc":
  489. usort($GonxBackups, array("backup","NameSortDesc"));
  490. break;
  491. case "SizeAsc":
  492. usort($GonxBackups, array("backup","SizeSortAsc"));
  493. break;
  494. case "SizeDesc":
  495. usort($GonxBackups, array("backup","SizeSortDesc"));
  496. break;
  497. default:
  498. usort($GonxBackups, array("backup","DateSortDesc"));
  499. break;
  500. } // switch
  501. if (is_array($GonxBackups)) {
  502. $GonxBackups = array_slice($GonxBackups, $from,$to);
  503. }
  504. $res .= "\n<table align=\"center\" width=90%>
  505. <tr>
  506. <td width=10%>$prev</td>
  507. <td width=80% align=\"center\"> $links</td>
  508. <td width=10% align=\"right\">$next</td>
  509. </tr></table>\n<br/>
  510. <table align=\"center\" width=100% cellspacing=0>
  511. \t<tr>
  512. <th></th>
  513. <th>DB</th>
  514. <th>Download</th>
  515. <th>Size</th>
  516. <th>Date</th>
  517. <th>Delete<th>
  518. </tr>\n";
  519. foreach($GonxBackups as $k=>$v){
  520. $db = explode("-",$v['fname'] );
  521. $db = $db[0];
  522. $res .= "\t<tr id=$k>
  523. <td><input type=\"radio\" name=\"bfile\" value=\"".$v['fname']."\" onclick=\"IECColor1($k);\"></td>
  524. <th>$db</th>
  525. <td align=center><font size=2px> <a href=\"?option=databaseAdmin&go=getbackup&bfile=".urlencode($v['fname'])."\" target=\"_new\" title=\"Download ".$v['fname']."\">".$v['fname']."</a></td>
  526. <td align=center><em>size ".$v['fsize']." Ko</em></td>
  527. <td align=center>".$v['mtime']."</td>
  528. <td align=center><a href=\"?option=databaseAdmin&go=delete&fname=".$v['fname']."\" title=\"".$GONX["delete"]." ".$v['fname']."\" onclick=\"return IECColor2($k);\"><img src=\"image.php?img=delete_gif\" border=0></a></font><td>
  529. </tr>\n\n";
  530. }
  531. $BackupSize = number_format(($BackupSize/1024),3 );
  532. $res .= "</table><br/><br/><em>".$GONX["totalbackupsize"]." : $BackupSize Mo</em> - ".$GONX["chgdisplayorder"]." : $OrderMenu
  533. <br/><br/><input type=hidden name=go value=import>
  534. <p align=right>";
  535. if($this->mysqldump)
  536. $res .="<input type=submit name=importdump value=\"".$GONX["importbackupdump"]."\" class=button>";
  537. $res .=" <input type=submit name=import value=\"".$GONX["importbackup"]."\" class=button></p></form>\n";
  538. $res .= "<form method=post action=\"?\" enctype=\"multipart/form-data\"><b>".$GONX["importbackupfile"]." :</b><br/><br/>
  539. <input type=file name=backupfile class=button>
  540. <br/><br/><input type=hidden name=go value=importfromfile>
  541. <p align=right><input type=submit name=import value=\"".$GONX["importbackup"]."\" class=button></p></form>\n";
  542. }
  543. $d->close();
  544. return $res;
  545. }
  546. /**
  547. * backup::NameSortAsc()
  548. *
  549. * @param $a
  550. * @param $b
  551. * @return
  552. **/
  553. function NameSortAsc($a, $b) {
  554. return strcmp($a["fname"], $b["fname"]);
  555. }
  556. /**
  557. * backup::NameSortDesc()
  558. *
  559. * @param $a
  560. * @param $b
  561. * @return
  562. **/
  563. function NameSortDesc($a, $b) {
  564. return !strcmp($a["fname"], $b["fname"]);
  565. }
  566. /**
  567. * backup::SizeSortAsc()
  568. *
  569. * @param $a
  570. * @param $b
  571. * @return
  572. **/
  573. function SizeSortAsc($a, $b) {
  574. return ($a["size"]>$b["size"])?1:-1;
  575. }
  576. /**
  577. * backup::SizeSortDesc()
  578. *
  579. * @param $a
  580. * @param $b
  581. * @return
  582. **/
  583. function SizeSortDesc($a, $b) {
  584. return ($a["size"]<$b["size"])?1:-1;
  585. }
  586. /**
  587. * backup::DateSortAsc()
  588. *
  589. * @param $a
  590. * @param $b
  591. * @return
  592. **/
  593. function DateSortAsc($a, $b) {
  594. return ($a["time"]>$b["time"])?1:-1;
  595. }
  596. /**
  597. * backup::DateSortDesc()
  598. *
  599. * @param $a
  600. * @param $b
  601. * @return
  602. **/
  603. function DateSortDesc($a, $b) {
  604. return ($a["time"]<$b["time"])?1:-1;
  605. }
  606. /**
  607. * backup::delete() delete a backup file based on its name
  608. *
  609. * @param $_fname
  610. * @return
  611. **/
  612. function delete($_fname){
  613. if (is_file($this->backupdir."/".$_fname)) {
  614. unlink($this->backupdir."/".$_fname);
  615. return "<font color=green> Backup file $_fname is correctly removed </font>";
  616. } else return "<font color=red> Error while removing backup file $_fname</font>";
  617. }
  618. /**
  619. * backup::keep() Keep backup files for a limited period of days and remove all others
  620. *
  621. * @param integer $days
  622. * @return
  623. **/
  624. function keep($days = 4){
  625. if (is_dir($this->backupdir)) {
  626. $d = dir($this->backupdir);
  627. while (false !== ($entry = $d->read())) {
  628. if ($entry!="." and $entry!=".." and preg_match('/.(bz2|gz|sql)$/',$entry)) {
  629. if ((filemtime($this->backupdir."/".$entry)) < (strtotime('-'.$days.' days'))) {
  630. $this->delete($entry);
  631. }
  632. }
  633. }
  634. }
  635. }
  636. /**
  637. * method optimize : execute an operation in all db table
  638. *
  639. * @param $operation operation to execute on DB
  640. *
  641. * @access public
  642. * @return void
  643. **/
  644. function optimize($operation = "OPTIMIZE"){
  645. global $GonxAdmin;
  646. if ($GonxAdmin["dbtype"]!="mysql") {
  647. return "Sorry this feature is available for only MySQL Databases.";
  648. }
  649. if ($operation!= "OPTIMIZE" or $operation!= "ANALYZE" or $operation!= "REPAIR") {
  650. $operation = "OPTIMIZE";
  651. }
  652. $color3 = $this->color3;
  653. $color2 = $this->color2;
  654. $color1 = $this->color1;
  655. $Tables = $this->get_db_tables();
  656. $query = "$operation TABLE ";
  657. foreach($Tables as $k=>$v){
  658. $query .= " `$v`,";
  659. }
  660. $query = substr($query,0,strlen($query)-1);
  661. $result = $this->query($query);
  662. $res = "<table border=\"0\" cellpadding=\"5\" align=center>\n";
  663. $res .= "<tr>
  664. <th bgcolor=\"$color1\">Table</font></th>
  665. <th bgcolor=\"$color1\">Op</font></th>
  666. <th bgcolor=\"$color1\">Msg_type</font></th>
  667. <th bgcolor=\"$color1\">Msg_text</font></th>
  668. </tr>";
  669. $bgcolor = $color3;
  670. while ($line = $this->fetch_array($result)) {
  671. $res .= "\t<tr>\n";
  672. foreach ($line as $k=>$col_value) {
  673. if (!is_numeric($k)) {
  674. if ($col_value == "OK") {
  675. $optimize = " <a href=\"?option=databaseAdmin&go=optimize&op=OPTIMIZE\" class=tab-s>Optimize DB</a>";
  676. } else $optimize="";
  677. $res .= "\t\t<td valign=\"top\" bgcolor=\"$bgcolor\">$col_value $optimize</td>\n";
  678. }
  679. }
  680. $res .= "\t</tr>\n";
  681. if ($bgcolor==$color3) {
  682. $bgcolor = $color2;
  683. } else $bgcolor = $color3;
  684. }
  685. $res .= "</table>\n";
  686. return $res;
  687. }
  688. /**
  689. * backup::configure() Configuration menu for the application
  690. *
  691. * @return
  692. **/
  693. function configure(){
  694. global $GONX;
  695. $filename = "init.php";
  696. // $perms = fileperms ($filename);
  697. $contents = implode('',file($filename));
  698. preg_match_all("|(.*)=(.*);|U",$contents ,$matches );
  699. $initform = "<form action=\"?\" method=post>
  700. <table class=config cellspacing=0 cellpadding=4>
  701. <tr><td colspan=2><b>".$GONX["configureapp"]."</b></td></tr>\n\n
  702. <tr align=center><th width=50%><i>Variable</i></th><th width=50%><i>Value</i></th></tr>\n\n";
  703. foreach($matches[1] as $k=>$v){
  704. if (strstr($v,"<?php")) {
  705. $v = str_replace("<?php","" , $v);
  706. }
  707. $v = trim($v);
  708. $value = trim(htmlentities($matches[2][$k]));
  709. $initform .= " <tr><td>$v</td><td><input type=text size=40 name=vars[$k] value=\"$value\"></td></tr>\n\n";
  710. }
  711. $initform .= "
  712. </table>
  713. <p align=right><input type=submit style=\"width:200\" class=button></p>
  714. <input type=hidden name=go value=saveconfig>
  715. </form>\n\n";
  716. return $initform ;
  717. }
  718. /**
  719. * backup::saveconfig() Save configuration to init.php
  720. *
  721. * @return
  722. **/
  723. function saveconfig(){
  724. global $vars;
  725. $filename = "init.php";
  726. $contents = implode('',file($filename));
  727. preg_match_all("|(.*)=(.*);|U",$contents ,$matches );
  728. foreach($matches[1] as $k=>$v){
  729. $contents = str_replace($matches[1][$k]."=".$matches[2][$k].";",$matches[1][$k]."= ".stripcslashes($vars[$k]).";", $contents);
  730. }
  731. // Assurons nous que le fichier est accessible en Žcriture
  732. if (is_writable($filename)) {
  733. // Dans notre exemple, nous ouvrons le fichier $filename en mode d'ajout
  734. // Le pointeur de fichier est placŽ ˆ la fin du fichier
  735. // c'est lˆ que $somecontent sera placŽ
  736. if (!$handle = fopen($filename, 'w')) {
  737. echo "Impossible d'ouvrir le fichier ($filename)";
  738. exit;
  739. }
  740. // Ecrivons quelque chose dans notre fichier.
  741. if (fwrite($handle, $contents) === FALSE) {
  742. echo "Impossible d'Žcrire dans le fichier ($filename)";
  743. exit;
  744. }
  745. fclose($handle);
  746. return TRUE;
  747. } else {
  748. return FALSE;
  749. }
  750. }
  751. /**
  752. * backup::monitor() Return tables status
  753. *
  754. * @return
  755. **/
  756. function monitor(){
  757. $color3 = $this->color3;
  758. $color2 = $this->color2;
  759. $color1 = $this->color1;
  760. $res = '<table border=0 cellpadding=5>';
  761. $result = $this->query('SHOW TABLE STATUS');
  762. $i = 0; $bgcolor = $color3; $l1 = $l2 = "";
  763. while ($table = @$this->fetch_array($result)) {
  764. foreach($table as $k=>$v){
  765. if (!is_integer($k)) {
  766. $l1 .= "<th bgcolor=\"$color1\"><font size=1>$k</font></th>\n";
  767. $l2 .= "<td bgcolor=\"$bgcolor\"><font size=1>$v</font></td>\n";
  768. }
  769. }
  770. if ($i==0) {
  771. $res .= "<tr>\n$l1</tr>\n";
  772. }
  773. $res .= "<tr>\n$l2</tr>\n";
  774. if ($bgcolor==$color3) {
  775. $bgcolor = $color2;
  776. } else $bgcolor = $color3;
  777. $l1 = $l2 = "";
  778. $i++;
  779. }
  780. $res .= '</table>';
  781. return $res;
  782. }
  783. /**
  784. * backup::getbackup()
  785. *
  786. * @return
  787. **/
  788. function getbackup($bfile){
  789. if (is_file($this->backupdir."/".$bfile) and !strstr($bfile,"../")) {
  790. header("Pragma: public");
  791. header("Expires: 0");
  792. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  793. header("Content-Type: application/force-download");
  794. header("Content-Disposition: attachment; filename=".basename($this->backupdir."/".$bfile));
  795. header("Content-Description: File Transfer");
  796. @readfile($this->backupdir."/".$bfile);
  797. exit;
  798. }
  799. }
  800. }
  801. ?>