| Conditions | 37 |
| Paths | > 20000 |
| Total Lines | 216 |
| Code Lines | 138 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 55 | function tpCreateDatabaseBackup(array $SETTINGS, string $encryptionKey = '', array $options = []): array |
||
| 56 | { |
||
| 57 | // Ensure required dependencies are loaded |
||
| 58 | $mainFunctionsPath = __DIR__ . '/main.functions.php'; |
||
| 59 | if (!function_exists('GenerateCryptKey') && is_file($mainFunctionsPath)) { |
||
| 60 | require_once $mainFunctionsPath; |
||
| 61 | } |
||
| 62 | if (function_exists('loadClasses') && !class_exists('DB')) { |
||
| 63 | loadClasses('DB'); |
||
| 64 | } |
||
| 65 | |||
| 66 | $outputDir = $options['output_dir'] ?? ($SETTINGS['path_to_files_folder'] ?? ''); |
||
| 67 | $prefix = (string)($options['filename_prefix'] ?? ''); |
||
| 68 | $chunkRows = (int)($options['chunk_rows'] ?? 1000); |
||
| 69 | $flushEvery = (int)($options['flush_every_inserts'] ?? 200); |
||
| 70 | $includeTables = $options['include_tables'] ?? []; |
||
| 71 | $excludeTables = $options['exclude_tables'] ?? []; |
||
| 72 | |||
| 73 | if ($outputDir === '' || !is_dir($outputDir) || !is_writable($outputDir)) { |
||
| 74 | return [ |
||
| 75 | 'success' => false, |
||
| 76 | 'filename' => '', |
||
| 77 | 'filepath' => '', |
||
| 78 | 'encrypted' => false, |
||
| 79 | 'size_bytes' => 0, |
||
| 80 | 'message' => 'Backup folder is not writable or not found: ' . $outputDir, |
||
| 81 | ]; |
||
| 82 | } |
||
| 83 | |||
| 84 | // Generate filename |
||
| 85 | $token = function_exists('GenerateCryptKey') |
||
| 86 | ? GenerateCryptKey(20, false, true, true, false, true) |
||
| 87 | : bin2hex(random_bytes(10)); |
||
| 88 | |||
| 89 | $filename = $prefix . time() . '-' . $token . '.sql'; |
||
| 90 | $filepath = rtrim($outputDir, '/') . '/' . $filename; |
||
| 91 | |||
| 92 | $handle = @fopen($filepath, 'w+'); |
||
| 93 | if ($handle === false) { |
||
| 94 | return [ |
||
| 95 | 'success' => false, |
||
| 96 | 'filename' => $filename, |
||
| 97 | 'filepath' => $filepath, |
||
| 98 | 'encrypted' => false, |
||
| 99 | 'size_bytes' => 0, |
||
| 100 | 'message' => 'Could not create backup file: ' . $filepath, |
||
| 101 | ]; |
||
| 102 | } |
||
| 103 | |||
| 104 | $insertCount = 0; |
||
| 105 | |||
| 106 | try { |
||
| 107 | // Get all tables |
||
| 108 | $tables = []; |
||
| 109 | $result = DB::query('SHOW TABLES'); |
||
| 110 | foreach ($result as $row) { |
||
| 111 | // SHOW TABLES returns key like 'Tables_in_<DB_NAME>' |
||
| 112 | foreach ($row as $v) { |
||
| 113 | $tables[] = (string) $v; |
||
| 114 | break; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | // Filter tables if requested |
||
| 119 | if (!empty($includeTables) && is_array($includeTables)) { |
||
| 120 | $tables = array_values(array_intersect($tables, $includeTables)); |
||
| 121 | } |
||
| 122 | if (!empty($excludeTables) && is_array($excludeTables)) { |
||
| 123 | $tables = array_values(array_diff($tables, $excludeTables)); |
||
| 124 | } |
||
| 125 | |||
| 126 | foreach ($tables as $tableName) { |
||
| 127 | // Safety: only allow typical MySQL table identifiers |
||
| 128 | if (!preg_match('/^[a-zA-Z0-9_]+$/', $tableName)) { |
||
| 129 | continue; |
||
| 130 | } |
||
| 131 | |||
| 132 | // Write drop and creation |
||
| 133 | fwrite($handle, 'DROP TABLE IF EXISTS `' . $tableName . "`;\n"); |
||
| 134 | |||
| 135 | $row2 = DB::queryFirstRow('SHOW CREATE TABLE `' . $tableName . '`'); |
||
| 136 | if (!is_array($row2) || empty($row2['Create Table'])) { |
||
| 137 | // Skip table if structure cannot be fetched |
||
| 138 | fwrite($handle, "\n"); |
||
| 139 | continue; |
||
| 140 | } |
||
| 141 | |||
| 142 | fwrite($handle, $row2['Create Table'] . ";\n\n"); |
||
| 143 | |||
| 144 | // Process table data in chunks to reduce memory usage |
||
| 145 | $offset = 0; |
||
| 146 | while (true) { |
||
| 147 | $rows = DB::query( |
||
| 148 | 'SELECT * FROM `' . $tableName . '` LIMIT %i OFFSET %i', |
||
| 149 | $chunkRows, |
||
| 150 | $offset |
||
| 151 | ); |
||
| 152 | |||
| 153 | if (empty($rows)) { |
||
| 154 | break; |
||
| 155 | } |
||
| 156 | |||
| 157 | foreach ($rows as $record) { |
||
| 158 | $values = []; |
||
| 159 | foreach ($record as $value) { |
||
| 160 | if ($value === null) { |
||
| 161 | $values[] = 'NULL'; |
||
| 162 | continue; |
||
| 163 | } |
||
| 164 | |||
| 165 | // Force scalar/string |
||
| 166 | if (is_bool($value)) { |
||
| 167 | $value = $value ? '1' : '0'; |
||
| 168 | } elseif (is_numeric($value)) { |
||
| 169 | // keep numeric as string but quoted (safe & consistent) |
||
| 170 | $value = (string) $value; |
||
| 171 | } else { |
||
| 172 | $value = (string) $value; |
||
| 173 | } |
||
| 174 | |||
| 175 | // Escape and keep newlines |
||
| 176 | $value = addslashes(preg_replace("/\n/", '\\n', $value)); |
||
| 177 | $values[] = '"' . $value . '"'; |
||
| 178 | } |
||
| 179 | |||
| 180 | $insertQuery = 'INSERT INTO `' . $tableName . '` VALUES(' . implode(',', $values) . ");\n"; |
||
| 181 | fwrite($handle, $insertQuery); |
||
| 182 | |||
| 183 | $insertCount++; |
||
| 184 | if ($flushEvery > 0 && ($insertCount % $flushEvery) === 0) { |
||
| 185 | fflush($handle); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | $offset += $chunkRows; |
||
| 190 | fflush($handle); |
||
| 191 | } |
||
| 192 | |||
| 193 | fwrite($handle, "\n\n"); |
||
| 194 | fflush($handle); |
||
| 195 | } |
||
| 196 | } catch (Throwable $e) { |
||
| 197 | fclose($handle); |
||
| 198 | @unlink($filepath); |
||
| 199 | |||
| 200 | return [ |
||
| 201 | 'success' => false, |
||
| 202 | 'filename' => $filename, |
||
| 203 | 'filepath' => $filepath, |
||
| 204 | 'encrypted' => false, |
||
| 205 | 'size_bytes' => 0, |
||
| 206 | 'message' => 'Backup failed: ' . $e->getMessage(), |
||
| 207 | ]; |
||
| 208 | } |
||
| 209 | |||
| 210 | fclose($handle); |
||
| 211 | |||
| 212 | // Encrypt the file if key provided |
||
| 213 | $encrypted = false; |
||
| 214 | if ($encryptionKey !== '') { |
||
| 215 | $tmpPath = rtrim($outputDir, '/') . '/defuse_temp_' . $filename; |
||
| 216 | |||
| 217 | if (!function_exists('prepareFileWithDefuse')) { |
||
| 218 | @unlink($filepath); |
||
| 219 | return [ |
||
| 220 | 'success' => false, |
||
| 221 | 'filename' => $filename, |
||
| 222 | 'filepath' => $filepath, |
||
| 223 | 'encrypted' => false, |
||
| 224 | 'size_bytes' => 0, |
||
| 225 | 'message' => 'Missing prepareFileWithDefuse() dependency (main.functions.php not loaded?)', |
||
| 226 | ]; |
||
| 227 | } |
||
| 228 | |||
| 229 | $ret = prepareFileWithDefuse('encrypt', $filepath, $tmpPath, $encryptionKey); |
||
| 230 | |||
| 231 | // prepareFileWithDefuse usually returns true on success, otherwise message/false |
||
| 232 | if ($ret !== true) { |
||
| 233 | @unlink($filepath); |
||
| 234 | @unlink($tmpPath); |
||
| 235 | return [ |
||
| 236 | 'success' => false, |
||
| 237 | 'filename' => $filename, |
||
| 238 | 'filepath' => $filepath, |
||
| 239 | 'encrypted' => false, |
||
| 240 | 'size_bytes' => 0, |
||
| 241 | 'message' => 'Encryption failed: ' . (is_string($ret) ? $ret : 'unknown error'), |
||
| 242 | ]; |
||
| 243 | } |
||
| 244 | |||
| 245 | // Replace original with encrypted version |
||
| 246 | @unlink($filepath); |
||
| 247 | if (!@rename($tmpPath, $filepath)) { |
||
| 248 | @unlink($tmpPath); |
||
| 249 | return [ |
||
| 250 | 'success' => false, |
||
| 251 | 'filename' => $filename, |
||
| 252 | 'filepath' => $filepath, |
||
| 253 | 'encrypted' => false, |
||
| 254 | 'size_bytes' => 0, |
||
| 255 | 'message' => 'Encryption succeeded but could not finalize file (rename failed)', |
||
| 256 | ]; |
||
| 257 | } |
||
| 258 | |||
| 259 | $encrypted = true; |
||
| 260 | } |
||
| 261 | |||
| 262 | $size = (int) (@filesize($filepath) ?: 0); |
||
| 263 | |||
| 264 | return [ |
||
| 265 | 'success' => true, |
||
| 266 | 'filename' => $filename, |
||
| 267 | 'filepath' => $filepath, |
||
| 268 | 'encrypted' => $encrypted, |
||
| 269 | 'size_bytes' => $size, |
||
| 270 | 'message' => '', |
||
| 271 | ]; |
||
| 399 |