Cheetah
ChWsbDatabaseBackup.php
Go to the documentation of this file.
1 <?php
2 
8 define('CHEETAH_VERSION', $GLOBALS['site']['ver'] . '.' . $GLOBALS['site']['build']);
9 
11 {
12 
14  var $sInputs;
15 
16  function __construct($sCharset = 'utf8', $sCollate = 'utf8_unicode_ci')
17  {
18  $this -> sCharset = $sCharset;
19  $this -> sCollate = $sCollate;
20  $this -> sInputs =
21 '--
22 -- Database Dump For Cheetah: ' . CHEETAH_VERSION . '
23 --
24 
25 ';
26 
27  }
28 
29  function _getTableStruct($name, $data = 0)
30  {
31  if ($data != 1) { ## 1 only data
32  ##Read table structure
33  $Query = "SHOW CREATE TABLE {$name}";
34  $Result = db_res($Query);
35 
36  $this -> sInputs .=
37 "
38 --
39 -- Table structure for table `{$name}`
40 --
41 
42 DROP TABLE IF EXISTS `{$name}`;
43 ";
44 
45  while($Row = $Result->fetch(PDO::FETCH_NUM))
46  $this -> sInputs .= preg_replace("/ENGINE=.*/", "ENGINE=MyISAM DEFAULT CHARSET={$this -> sCharset};\n", $Row[1]);
47  }
48 
49  ### Read data from table
50  if ($data != 0) { ##Only strucure
51 
52  $this -> sInputs .=
53 "
54 --
55 -- Dumping data for table `{$name}`
56 --
57 
58 ";
59 
60  $Query = "SELECT * FROM {$name} ";
61  $Result = db_res($Query);
62 
63  while($Row = $Result->fetch(PDO::FETCH_NUM)) {
64  $this -> sInputs .= "INSERT INTO `{$name}` VALUES (";
65 
66  for ($j = 0; $j < count($Row); $j++ ) {
67  if( is_null( $Row[$j] ) )
68  $this -> sInputs .= "NULL, ";
69  else //string or numeric
70  $this -> sInputs .= "'" . my_escape_string($Row[$j]) . "', ";
71  }
72 
73  $this -> sInputs = substr ($this -> sInputs, 0, strrpos($this -> sInputs, ',')); //delete last ,
74  $this -> sInputs .= ");\n";
75  }
76  $this -> sInputs .= "\n-- --------------------------------------------------------\n";
77  }
78  }
79 
80  function _getAllTables($data = false)
81  {
82  $Query = "SHOW TABLES";
83  $Result = db_res($Query);
84  while($Row = $Result->fetch(PDO::FETCH_NUM))
85  $this -> _getTableStruct($Row[0], $data);
86  }
87 
88  function _restoreFromDumpFile($file)
89  {
90  return execSqlFile( $file );
91  }
92 
93 }
94 
95 function my_escape_string( $text )
96 {
97  $text = str_replace( '\\', '\\\\', $text );
98  $text = str_replace( '\'', '\'\'', $text );
99  $text = str_replace( "\n", '\\n', $text );
100  $text = str_replace( "\r", '\\r', $text );
101  return $text;
102 }
CHEETAH_VERSION
const CHEETAH_VERSION
Definition: ChWsbDatabaseBackup.php:8
ChWsbDatabaseBackup\_restoreFromDumpFile
_restoreFromDumpFile($file)
Definition: ChWsbDatabaseBackup.php:88
ChWsbDatabaseBackup\$sCollate
$sCollate
Definition: ChWsbDatabaseBackup.php:13
ChWsbDatabaseBackup\__construct
__construct($sCharset='utf8', $sCollate='utf8_unicode_ci')
Definition: ChWsbDatabaseBackup.php:16
php
ChWsbDatabaseBackup\$sInputs
$sInputs
Definition: ChWsbDatabaseBackup.php:14
execSqlFile
execSqlFile($sFileName)
Definition: utils.inc.php:843
ChWsbDatabaseBackup
Definition: ChWsbDatabaseBackup.php:11
ChWsbDatabaseBackup\_getAllTables
_getAllTables($data=false)
Definition: ChWsbDatabaseBackup.php:80
ChWsbDatabaseBackup\$sCharset
$sCharset
Definition: ChWsbDatabaseBackup.php:13
db_res
db_res($query, $bindings=[])
Definition: db.inc.php:39
my_escape_string
my_escape_string( $text)
Definition: ChWsbDatabaseBackup.php:95
ChWsbDb
Definition: ChWsbDb.php:13
$GLOBALS
$GLOBALS['iAdminPage']
Definition: advanced_settings.php:10
ChWsbDatabaseBackup\_getTableStruct
_getTableStruct($name, $data=0)
Definition: ChWsbDatabaseBackup.php:29