forked from Sanix-Darker/SAVEME
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassSAVEME.php
210 lines (176 loc) · 5.24 KB
/
classSAVEME.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
* SAVEME ().
*
* @author Sanix Darker (https://github.com/Sanix-Darker)
* @version 1.0
*/
class SAVEME
{
const MAX_SQL_SIZE = 1e6;
const NONE = 0;
const DROP = 1;
const CREATE = 2;
const DATA = 4;
const TRIGGERS = 8;
const ALL = 15; // DROP | CREATE | DATA | TRIGGERS
/** @var array */
public $tables = array(
'*' => self::ALL,
);
/** @var mysqli */
private $connection;
/**
* Connects to database.
* @param mysqli connection
*/
public function __construct(mysqli $connection, $charset = 'utf8')
{
$this->connection = $connection;
if ($connection->connect_errno) {
throw new Exception($connection->connect_error);
} elseif (!$connection->set_charset($charset)) { // was added in MySQL 5.0.7 and PHP 5.0.5, fixed in PHP 5.1.5)
throw new Exception($connection->error);
}
}
/**
* Dumps table to logical file.
* @param resource
* @return void
*/
public function dumpTable_to_logicalfile($handle, $table)
{
$delTable = $this->delimite($table);
$res = $this->connection->query("SHOW CREATE TABLE $delTable");
$row = $res->fetch_assoc();
$res->close();
fwrite($handle, "-- --------------------------------------------------------\n\n");
$mode = isset($this->tables[$table]) ? $this->tables[$table] : $this->tables['*'];
$view = isset($row['Create View']);
if ($mode & self::DROP) {
fwrite($handle, 'DROP ' . ($view ? 'VIEW' : 'TABLE') . " IF EXISTS $delTable;\n\n");
}
if ($mode & self::CREATE) {
fwrite($handle, $row[$view ? 'Create View' : 'Create Table'] . ";\n\n");
}
if (!$view && ($mode & self::DATA)) {
$numeric = array();
$res = $this->connection->query("SHOW COLUMNS FROM $delTable");
$cols = array();
while ($row = $res->fetch_assoc()) {
$col = $row['Field'];
$cols[] = $this->delimite($col);
$numeric[$col] = (bool) preg_match('#^[^(]*(BYTE|COUNTER|SERIAL|INT|LONG$|CURRENCY|REAL|MONEY|FLOAT|DOUBLE|DECIMAL|NUMERIC|NUMBER)#i', $row['Type']);
}
$cols = '(' . implode(', ', $cols) . ')';
$res->close();
$size = 0;
$res = $this->connection->query("SELECT * FROM $delTable", MYSQLI_USE_RESULT);
while ($row = $res->fetch_assoc()) {
$s = '(';
foreach ($row as $key => $value) {
if ($value === null) {
$s .= "NULL,\t";
} elseif ($numeric[$key]) {
$s .= $value . ",\t";
} else {
$s .= "'" . $this->connection->real_escape_string($value) . "',\t";
}
}
if ($size == 0) {
$s = "INSERT INTO $delTable $cols VALUES\n$s";
} else {
$s = ",\n$s";
}
$len = strlen($s) - 1;
$s[$len - 1] = ')';
fwrite($handle, $s, $len);
$size += $len;
if ($size > self::MAX_SQL_SIZE) {
fwrite($handle, ";\n");
$size = 0;
}
}
$res->close();
if ($size) {
fwrite($handle, ";\n");
}
fwrite($handle, "\n");
}
if ($mode & self::TRIGGERS) {
$res = $this->connection->query("SHOW TRIGGERS LIKE '" . $this->connection->real_escape_string($table) . "'");
if ($res->num_rows) {
fwrite($handle, "DELIMITER ;;\n\n");
while ($row = $res->fetch_assoc()) {
fwrite($handle, "CREATE TRIGGER {$this->delimite($row['Trigger'])} $row[Timing] $row[Event] ON $delTable FOR EACH ROW\n$row[Statement];;\n\n");
}
fwrite($handle, "DELIMITER ;\n\n");
}
$res->close();
}
fwrite($handle, "\n");
}
/**
* Saves dump to the file.
* @param string filename
* @return void
*/
public function save($file)
{
$handle = strcasecmp(substr($file, -3), '.gz') ? fopen($file, 'wb') : gzopen($file, 'wb');
if (!$handle) {
throw new Exception("ERROR: Cannot write file '$file'.");
}
$this->write($handle);
}
/**
* Writes dump to logical file.
* @param resource
* @return void
*/
public function write($handle = null)
{
if ($handle === null) {
$handle = fopen('php://output', 'wb');
} elseif (!is_resource($handle) || get_resource_type($handle) !== 'stream') {
throw new Exception('Argument must be stream resource.');
}
$tables = $views = array();
$res = $this->connection->query('SHOW FULL TABLES');
while ($row = $res->fetch_row()) {
if ($row[1] === 'VIEW') {
$views[] = $row[0];
} else {
$tables[] = $row[0];
}
}
$res->close();
$tables = array_merge($tables, $views); // views must be last
$this->connection->query('LOCK TABLES `' . implode('` READ, `', $tables) . '` READ');
$db = $this->connection->query('SELECT DATABASE()')->fetch_row();
fwrite($handle, '-- Created at ' . date('j.n.Y G:i') . " using David Grudl MySQL Dump Utility\n"
. (isset($_SERVER['HTTP_HOST']) ? "-- Host: $_SERVER[HTTP_HOST]\n" : '')
. '-- MySQL Server: ' . $this->connection->server_info . "\n"
. '-- Database: ' . $db[0] . "\n"
. "\n"
. "SET NAMES utf8;\n"
. "SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO';\n"
. "SET FOREIGN_KEY_CHECKS=0;\n"
);
foreach ($tables as $table) {
$this->dumpTable_to_logicalfile($handle, $table);
}
fwrite($handle, "-- THE END\n");
$this->connection->query('UNLOCK TABLES');
}
public function saveNewDate($date){
$myFile2 = "date_NEVER_DELETE_THIS_FILE.txt";
$myFileLink2 = fopen($myFile2, 'w+') or die("Can't open file.");
fwrite($myFileLink2, $date);
fclose($myFileLink2);
}
private function delimite($s)
{
return '`' . str_replace('`', '``', $s) . '`';
}
}