| Conditions | 45 |
| Paths | > 20000 |
| Total Lines | 307 |
| Code Lines | 185 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 54 | function tpCreateDatabaseBackup(array $SETTINGS, string $encryptionKey = '', array $options = []): array |
||
| 55 | { |
||
| 56 | // Ensure required dependencies are loaded |
||
| 57 | $mainFunctionsPath = __DIR__ . '/main.functions.php'; |
||
| 58 | if ((!function_exists('GenerateCryptKey') || !function_exists('prefixTable')) && is_file($mainFunctionsPath)) { |
||
| 59 | require_once $mainFunctionsPath; |
||
| 60 | } |
||
| 61 | if (function_exists('loadClasses') && !class_exists('DB')) { |
||
| 62 | loadClasses('DB'); |
||
| 63 | } |
||
| 64 | |||
| 65 | // Enable maintenance mode for the whole backup operation, then restore previous value at the end. |
||
| 66 | // This is best-effort: a failure to toggle maintenance must not break the backup itself. |
||
| 67 | /** @scrutinizer ignore-unused */ |
||
| 68 | $__tpMaintenanceGuard = new class() { |
||
|
|
|||
| 69 | private $prev = null; |
||
| 70 | private $changed = false; |
||
| 71 | |||
| 72 | public function __construct() |
||
| 73 | { |
||
| 74 | try { |
||
| 75 | $row = DB::queryFirstRow( |
||
| 76 | 'SELECT valeur FROM ' . prefixTable('misc') . ' WHERE intitule=%s AND type=%s', |
||
| 77 | 'maintenance_mode', |
||
| 78 | 'admin' |
||
| 79 | ); |
||
| 80 | |||
| 81 | if (is_array($row) && array_key_exists('valeur', $row)) { |
||
| 82 | $this->prev = (string) $row['valeur']; |
||
| 83 | } |
||
| 84 | |||
| 85 | // Only toggle if it was not already enabled |
||
| 86 | if ($this->prev !== '1') { |
||
| 87 | DB::update( |
||
| 88 | prefixTable('misc'), |
||
| 89 | array( |
||
| 90 | 'valeur' => '1', |
||
| 91 | 'updated_at' => time(), |
||
| 92 | ), |
||
| 93 | 'intitule = %s AND type= %s', |
||
| 94 | 'maintenance_mode', |
||
| 95 | 'admin' |
||
| 96 | ); |
||
| 97 | $this->changed = true; |
||
| 98 | } |
||
| 99 | } catch (Throwable $ignored) { |
||
| 100 | // ignore |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | public function __destruct() |
||
| 105 | { |
||
| 106 | if ($this->changed !== true) { |
||
| 107 | return; |
||
| 108 | } |
||
| 109 | |||
| 110 | try { |
||
| 111 | DB::update( |
||
| 112 | prefixTable('misc'), |
||
| 113 | array( |
||
| 114 | 'valeur' => (string) ($this->prev ?? '0'), |
||
| 115 | 'updated_at' => time(), |
||
| 116 | ), |
||
| 117 | 'intitule = %s AND type= %s', |
||
| 118 | 'maintenance_mode', |
||
| 119 | 'admin' |
||
| 120 | ); |
||
| 121 | } catch (Throwable $ignored) { |
||
| 122 | // ignore |
||
| 123 | } |
||
| 124 | } |
||
| 125 | }; |
||
| 126 | |||
| 127 | $outputDir = $options['output_dir'] ?? ($SETTINGS['path_to_files_folder'] ?? ''); |
||
| 128 | $prefix = (string)($options['filename_prefix'] ?? ''); |
||
| 129 | $chunkRows = (int)($options['chunk_rows'] ?? 1000); |
||
| 130 | $flushEvery = (int)($options['flush_every_inserts'] ?? 200); |
||
| 131 | $includeTables = $options['include_tables'] ?? []; |
||
| 132 | $excludeTables = $options['exclude_tables'] ?? []; |
||
| 133 | |||
| 134 | if ($outputDir === '' || !is_dir($outputDir) || !is_writable($outputDir)) { |
||
| 135 | return [ |
||
| 136 | 'success' => false, |
||
| 137 | 'filename' => '', |
||
| 138 | 'filepath' => '', |
||
| 139 | 'encrypted' => false, |
||
| 140 | 'size_bytes' => 0, |
||
| 141 | 'message' => 'Backup folder is not writable or not found: ' . $outputDir, |
||
| 142 | ]; |
||
| 143 | } |
||
| 144 | |||
| 145 | // Generate filename |
||
| 146 | $token = function_exists('GenerateCryptKey') |
||
| 147 | ? GenerateCryptKey(20, false, true, true, false, true) |
||
| 148 | : bin2hex(random_bytes(10)); |
||
| 149 | |||
| 150 | $filename = $prefix . time() . '-' . $token . '.sql'; |
||
| 151 | $filepath = rtrim($outputDir, '/') . '/' . $filename; |
||
| 152 | |||
| 153 | $handle = @fopen($filepath, 'w+'); |
||
| 154 | if ($handle === false) { |
||
| 155 | return [ |
||
| 156 | 'success' => false, |
||
| 157 | 'filename' => $filename, |
||
| 158 | 'filepath' => $filepath, |
||
| 159 | 'encrypted' => false, |
||
| 160 | 'size_bytes' => 0, |
||
| 161 | 'message' => 'Could not create backup file: ' . $filepath, |
||
| 162 | ]; |
||
| 163 | } |
||
| 164 | |||
| 165 | $insertCount = 0; |
||
| 166 | |||
| 167 | try { |
||
| 168 | // Get all tables |
||
| 169 | $tables = []; |
||
| 170 | $result = DB::query('SHOW TABLES'); |
||
| 171 | foreach ($result as $row) { |
||
| 172 | // SHOW TABLES returns key like 'Tables_in_<DB_NAME>' |
||
| 173 | foreach ($row as $v) { |
||
| 174 | $tables[] = (string) $v; |
||
| 175 | break; |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | // Filter tables if requested |
||
| 180 | if (!empty($includeTables) && is_array($includeTables)) { |
||
| 181 | $tables = array_values(array_intersect($tables, $includeTables)); |
||
| 182 | } |
||
| 183 | if (!empty($excludeTables) && is_array($excludeTables)) { |
||
| 184 | $tables = array_values(array_diff($tables, $excludeTables)); |
||
| 185 | } |
||
| 186 | |||
| 187 | foreach ($tables as $tableName) { |
||
| 188 | // Safety: only allow typical MySQL table identifiers |
||
| 189 | if (!preg_match('/^[a-zA-Z0-9_]+$/', $tableName)) { |
||
| 190 | continue; |
||
| 191 | } |
||
| 192 | |||
| 193 | // Write drop and creation |
||
| 194 | fwrite($handle, 'DROP TABLE IF EXISTS `' . $tableName . "`;\n"); |
||
| 195 | |||
| 196 | $row2 = DB::queryFirstRow('SHOW CREATE TABLE `' . $tableName . '`'); |
||
| 197 | if (!is_array($row2) || empty($row2['Create Table'])) { |
||
| 198 | // Skip table if structure cannot be fetched |
||
| 199 | fwrite($handle, "\n"); |
||
| 200 | continue; |
||
| 201 | } |
||
| 202 | |||
| 203 | fwrite($handle, $row2['Create Table'] . ";\n\n"); |
||
| 204 | |||
| 205 | // Process table data in chunks to reduce memory usage |
||
| 206 | $offset = 0; |
||
| 207 | while (true) { |
||
| 208 | $rows = DB::query( |
||
| 209 | 'SELECT * FROM `' . $tableName . '` LIMIT %i OFFSET %i', |
||
| 210 | $chunkRows, |
||
| 211 | $offset |
||
| 212 | ); |
||
| 213 | |||
| 214 | if (empty($rows)) { |
||
| 215 | break; |
||
| 216 | } |
||
| 217 | |||
| 218 | foreach ($rows as $record) { |
||
| 219 | $values = []; |
||
| 220 | foreach ($record as $value) { |
||
| 221 | if ($value === null) { |
||
| 222 | $values[] = 'NULL'; |
||
| 223 | continue; |
||
| 224 | } |
||
| 225 | |||
| 226 | // Force scalar/string |
||
| 227 | if (is_bool($value)) { |
||
| 228 | $value = $value ? '1' : '0'; |
||
| 229 | } elseif (is_numeric($value)) { |
||
| 230 | // keep numeric as string but quoted (safe & consistent) |
||
| 231 | $value = (string) $value; |
||
| 232 | } else { |
||
| 233 | $value = (string) $value; |
||
| 234 | } |
||
| 235 | |||
| 236 | // Escape and keep newlines |
||
| 237 | $value = addslashes(preg_replace("/\n/", '\\n', $value)); |
||
| 238 | $values[] = '"' . $value . '"'; |
||
| 239 | } |
||
| 240 | |||
| 241 | $insertQuery = 'INSERT INTO `' . $tableName . '` VALUES(' . implode(',', $values) . ");\n"; |
||
| 242 | fwrite($handle, $insertQuery); |
||
| 243 | |||
| 244 | $insertCount++; |
||
| 245 | if ($flushEvery > 0 && ($insertCount % $flushEvery) === 0) { |
||
| 246 | fflush($handle); |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | $offset += $chunkRows; |
||
| 251 | fflush($handle); |
||
| 252 | } |
||
| 253 | |||
| 254 | fwrite($handle, "\n\n"); |
||
| 255 | fflush($handle); |
||
| 256 | } |
||
| 257 | } catch (Throwable $e) { |
||
| 258 | if (is_resource($handle)) { |
||
| 259 | fclose($handle); |
||
| 260 | } |
||
| 261 | |||
| 262 | $errorMessage = 'Backup failed: ' . $e->getMessage(); |
||
| 263 | |||
| 264 | // Suppression sécurisée sans @ |
||
| 265 | if (file_exists($filepath)) { |
||
| 266 | $deleted = unlink($filepath); |
||
| 267 | if ($deleted === false) { |
||
| 268 | $errorMessage .= ' (Note: Temporary backup file could not be deleted from disk)'; |
||
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 272 | return [ |
||
| 273 | 'success' => false, |
||
| 274 | 'filename' => $filename, |
||
| 275 | 'filepath' => $filepath, |
||
| 276 | 'encrypted' => false, |
||
| 277 | 'size_bytes' => 0, |
||
| 278 | 'message' => $errorMessage, |
||
| 279 | ]; |
||
| 280 | } |
||
| 281 | |||
| 282 | fclose($handle); |
||
| 283 | |||
| 284 | // Encrypt the file if key provided |
||
| 285 | $encrypted = false; |
||
| 286 | if ($encryptionKey !== '') { |
||
| 287 | $tmpPath = rtrim($outputDir, '/') . '/defuse_temp_' . $filename; |
||
| 288 | |||
| 289 | if (!function_exists('prepareFileWithDefuse')) { |
||
| 290 | if (file_exists($filepath)) { |
||
| 291 | unlink($filepath); |
||
| 292 | } |
||
| 293 | return [ |
||
| 294 | 'success' => false, |
||
| 295 | 'filename' => $filename, |
||
| 296 | 'filepath' => $filepath, |
||
| 297 | 'encrypted' => false, |
||
| 298 | 'size_bytes' => 0, |
||
| 299 | 'message' => 'Missing prepareFileWithDefuse() dependency (main.functions.php not loaded?)', |
||
| 300 | ]; |
||
| 301 | } |
||
| 302 | |||
| 303 | $ret = prepareFileWithDefuse('encrypt', $filepath, $tmpPath, $encryptionKey); |
||
| 304 | |||
| 305 | if ($ret !== true) { |
||
| 306 | if (file_exists($filepath)) { |
||
| 307 | unlink($filepath); |
||
| 308 | } |
||
| 309 | if (file_exists($tmpPath)) { |
||
| 310 | unlink($tmpPath); |
||
| 311 | } |
||
| 312 | return [ |
||
| 313 | 'success' => false, |
||
| 314 | 'filename' => $filename, |
||
| 315 | 'filepath' => $filepath, |
||
| 316 | 'encrypted' => false, |
||
| 317 | 'size_bytes' => 0, |
||
| 318 | 'message' => 'Encryption failed: ' . (is_string($ret) ? $ret : 'unknown error'), |
||
| 319 | ]; |
||
| 320 | } |
||
| 321 | |||
| 322 | // Replace original with encrypted version |
||
| 323 | if (file_exists($filepath)) { |
||
| 324 | unlink($filepath); |
||
| 325 | } |
||
| 326 | |||
| 327 | // On vérifie le succès de rename() sans @ |
||
| 328 | if (is_file($tmpPath) && !rename($tmpPath, $filepath)) { |
||
| 329 | if (file_exists($tmpPath)) { |
||
| 330 | unlink($tmpPath); |
||
| 331 | } |
||
| 332 | return [ |
||
| 333 | 'success' => false, |
||
| 334 | 'filename' => $filename, |
||
| 335 | 'filepath' => $filepath, |
||
| 336 | 'encrypted' => false, |
||
| 337 | 'size_bytes' => 0, |
||
| 338 | 'message' => 'Encryption succeeded but could not finalize file (rename failed)', |
||
| 339 | ]; |
||
| 340 | } |
||
| 341 | |||
| 342 | $encrypted = true; |
||
| 343 | } |
||
| 344 | |||
| 345 | // Gestion de filesize sans @ |
||
| 346 | $size = 0; |
||
| 347 | if (is_file($filepath)) { |
||
| 348 | $size = filesize($filepath); |
||
| 349 | if ($size === false) { |
||
| 350 | $size = 0; |
||
| 351 | } |
||
| 352 | } |
||
| 353 | |||
| 354 | return [ |
||
| 355 | 'success' => true, |
||
| 356 | 'filename' => $filename, |
||
| 357 | 'filepath' => $filepath, |
||
| 358 | 'encrypted' => $encrypted, |
||
| 359 | 'size_bytes' => (int) $size, |
||
| 360 | 'message' => '', |
||
| 361 | ]; |
||
| 477 |