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