-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.php
More file actions
103 lines (87 loc) · 3.14 KB
/
export.php
File metadata and controls
103 lines (87 loc) · 3.14 KB
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
<?php
require 'auth.php';
require 'vendor/autoload.php';
use Shuchkin\SimpleXLSXGen;
if (!$pdo) {
header("Location: index.php");
exit;
}
$dbName = $_SESSION['db_name'];
$exportAll = $_GET['export_all'] ?? false;
$table = $_GET['table'] ?? '';
$search = $_GET['search'] ?? '';
// Validate request
if (!$exportAll && (!$table || !preg_match('/^[a-zA-Z0-9_]+$/', $table))) {
die("Invalid request.");
}
$tablesToExport = [];
if ($exportAll) {
$stmt = $pdo->query("SHOW TABLES");
$tablesToExport = $stmt->fetchAll(PDO::FETCH_COLUMN);
} else {
$tablesToExport = [$table];
}
$xlsx = null;
foreach ($tablesToExport as $currentTable) {
$whereSql = '';
$params = [];
$stmtCols = $pdo->query("SHOW COLUMNS FROM `$currentTable`");
$columns = $stmtCols->fetchAll(PDO::FETCH_ASSOC);
$headerFields = [];
// Applying the exact style requested by user: black background, yellow text, bold, centered
foreach ($columns as $col) {
$headerFields[] = "<center><style fill=\"#000000\" font-weight=\"bold\" color=\"#FFFF00\">" . htmlspecialchars($col['Field']) . "</style></center>";
}
$sheetData = [];
$sheetData[] = $headerFields;
// If exporting a single table with search string
if (!$exportAll && $search !== '') {
$whereClauses = [];
foreach ($columns as $col) {
$whereClauses[] = "`{$col['Field']}` LIKE ?";
$params[] = '%' . $search . '%';
}
if (!empty($whereClauses)) {
$whereSql = "WHERE " . implode(' OR ', $whereClauses);
}
}
$dataQuery = "SELECT * FROM `$currentTable` $whereSql";
// We maintain buffered because we are fetching into an array.
// For exceedingly large database tables >100M rows, PHP memory limit might hit.
// However, generating XLSX requires memory anyway in SimpleXLSXGen unless streamed.
$dataStmt = $pdo->prepare($dataQuery);
$dataStmt->execute($params);
while ($row = $dataStmt->fetch(PDO::FETCH_ASSOC)) {
// Replace NULL with explicitly empty or string version if needed
$cleanRow = [];
foreach($row as $val) {
$cleanRow[] = $val === null ? '' : (string)$val;
}
$sheetData[] = $cleanRow;
}
// User requested sheet name to be the database name if possible.
// This is possible if we are only exporting a single specific table.
// Excel cannot have multiple duplicate sheet names.
$rawSheetName = $exportAll ? $currentTable : $dbName;
$sheetName = substr(str_replace(['[', ']', '*', '/', '\\', '?', ':'], '', $rawSheetName), 0, 31);
// Fallback if sheetname becomes empty
if (empty($sheetName)) $sheetName = "Sheet";
if ($xlsx === null) {
$xlsx = SimpleXLSXGen::fromArray($sheetData, $sheetName);
} else {
$xlsx->addSheet($sheetData, $sheetName);
}
}
// Ensure safe filename
$dateStr = date('Y-m-d_His');
if ($exportAll) {
$filename = "{$dbName}_FullDB_GoogleSheets_{$dateStr}.xlsx";
} else {
$filename = "{$dbName}_{$table}_GoogleSheets_{$dateStr}.xlsx";
}
if ($xlsx !== null) {
$xlsx->downloadAs($filename);
} else {
echo "No data to export.";
}
exit;