-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathajaximportfile.php
168 lines (122 loc) · 4.25 KB
/
ajaximportfile.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
<?php
/*
SQL Buddy - Web based MySQL administration
http://interruptorgeek.com/sql-buddy-ig-review/
ajaximportfile.php
- import from file - called from import.php
MIT license
Original : 2008 Calvin Lough <http://calv.in>
Reviewed : 2016 Carlos Martín Arnillas <https://interruptorgeek.com>
*/
include "functions.php";
loginCheck();
if (isset($db))
$conn->selectDB($db);
function stripCommentLines($in) {
if (substr($in, 0, 2) == "--")
$in = '';
return $in;
}
if (isset($_POST) || isset($_FILES)) {
if (isset($_FILES['INPUTFILE']['tmp_name']))
$file = $_FILES['INPUTFILE']['tmp_name'];
if (isset($_POST['FORMAT']))
$format = $_POST['FORMAT'];
if (!(isset($format) && $format == "CSV"))
$format = "SQL";
if (isset($_POST['IGNOREFIRST']))
$ignoreFirst = $_POST['IGNOREFIRST'];
$first = true;
// for csv
if (isset($format) && $format == "CSV" && isset($table)) {
$columnCount = 0;
$structureSQL = $conn->describeTable($table);
if ($conn->isResultSet($structureSQL)) {
while ($structureRow = $conn->fetchAssoc($structureSQL)) {
$columnCount++;
}
}
}
$insertCount = 0;
$skipCount = 0;
if (isset($file) && is_uploaded_file($file)) {
if (isset($format) && $format == "SQL") {
$lines = file($file);
// the file() function doesn't handle mac line endings correctly
if (sizeof($lines) == 1 && strpos($lines[0], "\r") > 0) {
$lines = explode("\r", $lines[0]);
}
$commentFree = array_map("stripCommentLines", $lines);
$contents = trim(implode('', $commentFree));
$statements = splitQueryText($contents);
} else {
$statements = file($file);
// see previous comment
if (sizeof($statements) == 1 && strpos($statements[0], "\r") > 0) {
$statements = explode("\r", $statements[0]);
}
}
foreach ($statements as $statement) {
$statement = trim($statement);
if ($statement) {
if (isset($format) && $format == "SQL") {
$importQuery = $conn->query($statement) or ($dbErrors[] = $conn->error());
$affected = (int)($conn->affectedRows($importQuery));
$insertCount += $affected;
} else if (isset($format) && $format == "CSV" && isset($table)) {
if (!(isset($ignoreFirst) && $first)) {
preg_match_all('/"(([^"]|"")*)"/i', $statement, $matches);
$rawValues = $matches[1];
for ($i=0; $i<sizeof($rawValues); $i++) {
$rawValues[$i] = str_replace('""', '"', $rawValues[$i]);
$rawValues[$i] = $conn->escapeString($rawValues[$i]);
}
$values = implode("','", $rawValues);
// make sure that the counts match up
if (sizeof($rawValues) == $columnCount) {
$importQuery = $conn->query("INSERT INTO `$table` VALUES ('$values')") or ($dbErrors[] = $conn->error());
$affected = (int)($conn->affectedRows($importQuery));
$insertCount += $affected;
} else {
$skipCount++;
}
}
$first = false;
}
}
}
}
$message = "";
if (!isset($statements)) {
$message .= __("Either the file could not be read or it was empty") . "<br />";
} else if ($format == "SQL") {
$message .= sprintf(__p("%d statement was executed from the file", "%d statements were executed from the file", $insertCount), $insertCount) . ".<br />";
} else if ($format == "CSV") {
if (isset($insertCount) && $insertCount > 0) {
$message .= sprintf(__p("%d row was inserted into the database from the file", "%d rows were inserted into the database from the file", $insertCount), $insertCount) . ".<br />";
}
if (isset($skipCount) && $skipCount > 0) {
$message .= sprintf(__p("%d row had to be skipped because the number of values was incorrect", "%d rows had to be skipped because the number of values was incorrect", $skipCount), $skipCount) . ".<br />";
}
}
if (isset($dbErrors)) {
$message .= __("The following errors were reported") . ":<br />";
foreach ($dbErrors as $merr) {
$message .= " - " . $merr . "<br />";
}
}
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/REC-html40/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" version="-//W3C//DTD XHTML 1.1//EN" xml:lang="en">
<head>
</head>
<body>
<script type="text/javascript">
parent.updateAfterImport("<?php echo trim(addslashes(nl2br($message))); ?>");
parent.refreshRowCount();
</script>
</body>
</html>
<?php
}
?>