|
1 | 1 | <?php |
2 | 2 |
|
3 | | - |
4 | | -define('PMA_SSL_DIR', $_ENV['PMA_SSL_DIR'] ?? '/etc/phpmyadmin/ssl'); |
| 3 | +declare(strict_types=1); |
5 | 4 |
|
6 | 5 | /** |
7 | 6 | * Helper function to decode and save multiple SSL files from base64. |
8 | 7 | * |
9 | | - * @param string $base64_string The base64 encoded string containing multiple SSL files separated by commas. |
10 | | - * If no commas are present, the entire string is treated as a single file. |
11 | | - * @param string $prefix The prefix to use for the generated SSL file names. |
12 | | - * @param string $extension The file extension to use for the generated SSL files. |
| 8 | + * @param string $base64FilesContents The base64 encoded string containing multiple files separated by commas. |
| 9 | + * If no commas are present, the entire string is treated as a single file. |
| 10 | + * @param string $prefix The prefix to use for the generated file names. |
| 11 | + * @param string $extension The file extension to use for the generated files. |
| 12 | + * @param string $storageFolder The folder where to store the generated files. |
13 | 13 | * |
14 | | - * @return string A comma-separated list of paths to the generated SSL files. |
| 14 | + * @return string A comma-separated list of paths to the generated files. |
15 | 15 | */ |
16 | | -function decodeAndSaveSslFiles(string $base64_string, string $prefix, string $extension): string |
| 16 | +function decodeBase64AndSaveFiles(string $base64FilesContents, string $prefix, string $extension, string $storageFolder): string |
17 | 17 | { |
18 | 18 | // Ensure the output directory exists |
19 | | - if (!is_dir(PMA_SSL_DIR)) { |
20 | | - mkdir(PMA_SSL_DIR, 0755, true); |
| 19 | + if (! is_dir($storageFolder)) { |
| 20 | + mkdir($storageFolder, 0755, true); |
21 | 21 | } |
22 | 22 |
|
23 | 23 | // Split the base64 string into an array of files |
24 | | - $files = strpos($base64_string, ',') !== false ? explode(',', $base64_string) : [$base64_string]; |
| 24 | + $base64FilesContents = explode(',', trim($base64FilesContents)); |
25 | 25 | $counter = 1; |
26 | | - $ssl_files = []; |
| 26 | + $outputFiles = []; |
27 | 27 |
|
28 | 28 | // Process each file |
29 | | - foreach ($files as $file) { |
30 | | - $output_file = PMA_SSL_DIR . "/pma-ssl-$prefix-$counter.$extension"; |
| 29 | + foreach ($base64FilesContents as $base64FileContent) { |
| 30 | + $outputFile = $storageFolder . '/' . $prefix . '-' . $counter . '.' . $extension; |
31 | 31 |
|
32 | | - $file_contents = base64_decode($file, true); |
33 | | - if ($file_contents === false) { |
34 | | - echo 'Failed to decode: ' . $file; |
| 32 | + $fileContent = base64_decode($base64FileContent, true); |
| 33 | + if ($fileContent === false) { |
| 34 | + echo 'Failed to decode: ' . $base64FileContent; |
35 | 35 | exit(1); |
36 | 36 | } |
37 | 37 |
|
38 | 38 | // Write the decoded file to the output directory |
39 | | - if (file_put_contents($output_file, $file_contents) === false) { |
40 | | - echo 'Failed to write to ' . $output_file; |
| 39 | + if (file_put_contents($outputFile, $fileContent) === false) { |
| 40 | + echo 'Failed to write to ' . $outputFile; |
41 | 41 | exit(1); |
42 | 42 | } |
43 | 43 |
|
44 | 44 | // Add the output file path to the list |
45 | | - $ssl_files[] = $output_file; |
| 45 | + $outputFiles[] = $outputFile; |
46 | 46 | $counter++; |
47 | 47 | } |
48 | 48 |
|
49 | 49 | // Return a comma-separated list of the generated file paths |
50 | | - return implode(',', $ssl_files); |
| 50 | + return implode(',', $outputFiles); |
51 | 51 | } |
0 commit comments