Skip to content

Commit 95bf927

Browse files
committed
Make the helper function usable for another use
1 parent 4ceefa6 commit 95bf927

File tree

8 files changed

+132
-160
lines changed

8 files changed

+132
-160
lines changed

apache/config.inc.php

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
'PMA_UPLOADDIR',
3030
'PMA_SAVEDIR',
3131
'PMA_SSL',
32+
'PMA_SSL_DIR',
3233
'PMA_SSL_VERIFY',
3334
'PMA_SSL_CA',
3435
'PMA_SSL_KEY',
@@ -38,7 +39,6 @@
3839
'PMA_SSL_CAS',
3940
'PMA_SSL_KEYS',
4041
'PMA_SSL_CERTS',
41-
'PMA_PMA_SSL_DIR'
4242
];
4343

4444
foreach ($vars as $var) {
@@ -47,6 +47,11 @@
4747
$_ENV[$var] = $env;
4848
}
4949
}
50+
51+
if (! defined('PMA_SSL_DIR')) {
52+
define('PMA_SSL_DIR', $_ENV['PMA_SSL_DIR'] ?? '/etc/phpmyadmin/ssl');
53+
}
54+
5055
if (isset($_ENV['PMA_QUERYHISTORYDB'])) {
5156
$cfg['QueryHistoryDB'] = (bool) $_ENV['PMA_QUERYHISTORYDB'];
5257
}
@@ -66,44 +71,32 @@
6671
}
6772

6873
if (isset($_ENV['PMA_SSL_CA_BASE64'])) {
69-
if (!is_dir(PMA_SSL_DIR)) {
70-
mkdir(PMA_SSL_DIR, 0755, true);
71-
}
72-
file_put_contents(PMA_SSL_DIR . '/pma-ssl-ca.pem', base64_decode($_ENV['PMA_SSL_CA_BASE64']));
73-
$_ENV['PMA_SSL_CA'] = PMA_SSL_DIR . '/pma-ssl-ca.pem';
74+
$_ENV['PMA_SSL_CA'] = decodeBase64AndSaveFiles($_ENV['PMA_SSL_CA_BASE64'], 'phpmyadmin-ssl-CA', 'pem', PMA_SSL_DIR);
7475
}
7576

7677
/* Decode and save the SSL key from base64 */
7778
if (isset($_ENV['PMA_SSL_KEY_BASE64'])) {
78-
if (!is_dir(PMA_SSL_DIR)) {
79-
mkdir(PMA_SSL_DIR, 0755, true);
80-
}
81-
file_put_contents(PMA_SSL_DIR . '/pma-ssl-key.key', base64_decode($_ENV['PMA_SSL_KEY_BASE64']));
82-
$_ENV['PMA_SSL_KEY'] = PMA_SSL_DIR . '/pma-ssl-key.key';
79+
$_ENV['PMA_SSL_KEY'] = decodeBase64AndSaveFiles($_ENV['PMA_SSL_KEY_BASE64'], 'phpmyadmin-ssl-CERT', 'cert', PMA_SSL_DIR);
8380
}
8481

8582
/* Decode and save the SSL certificate from base64 */
8683
if (isset($_ENV['PMA_SSL_CERT_BASE64'])) {
87-
if (!is_dir(PMA_SSL_DIR)) {
88-
mkdir(PMA_SSL_DIR, 0755, true);
89-
}
90-
file_put_contents(PMA_SSL_DIR . '/pma-ssl-cert.pem', base64_decode($_ENV['PMA_SSL_CERT_BASE64']));
91-
$_ENV['PMA_SSL_CERT'] = PMA_SSL_DIR . '/pma-ssl-cert.pem';
84+
$_ENV['PMA_SSL_CERT'] = decodeBase64AndSaveFiles($_ENV['PMA_SSL_CERT_BASE64'], 'phpmyadmin-ssl-CERT', 'cert', PMA_SSL_DIR);
9285
}
9386

9487
/* Decode and save multiple SSL CA certificates from base64 */
9588
if (isset($_ENV['PMA_SSL_CAS_BASE64'])) {
96-
$_ENV['PMA_SSL_CAS'] = decodeAndSaveSslFiles($_ENV['PMA_SSL_CAS_BASE64'], 'CA', 'pem');
89+
$_ENV['PMA_SSL_CAS'] = decodeBase64AndSaveFiles($_ENV['PMA_SSL_CAS_BASE64'], 'phpmyadmin-ssl-CA', 'pem', PMA_SSL_DIR);
9790
}
9891

9992
/* Decode and save multiple SSL keys from base64 */
10093
if (isset($_ENV['PMA_SSL_KEYS_BASE64'])) {
101-
$_ENV['PMA_SSL_KEYS'] = decodeAndSaveSslFiles($_ENV['PMA_SSL_KEYS_BASE64'], 'CERT', 'cert');
94+
$_ENV['PMA_SSL_KEYS'] = decodeBase64AndSaveFiles($_ENV['PMA_SSL_KEYS_BASE64'], 'phpmyadmin-ssl-CERT', 'cert', PMA_SSL_DIR);
10295
}
10396

10497
/* Decode and save multiple SSL certificates from base64 */
10598
if (isset($_ENV['PMA_SSL_CERTS_BASE64'])) {
106-
$_ENV['PMA_SSL_CERTS'] = decodeAndSaveSslFiles($_ENV['PMA_SSL_CERTS_BASE64'], 'KEY', 'key');
99+
$_ENV['PMA_SSL_CERTS'] = decodeBase64AndSaveFiles($_ENV['PMA_SSL_CERTS_BASE64'], 'phpmyadmin-ssl-KEY', 'key', PMA_SSL_DIR);
107100
}
108101

109102
/* Figure out hosts */

apache/helpers.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
11
<?php
22

3-
4-
define('PMA_SSL_DIR', $_ENV['PMA_SSL_DIR'] ?? '/etc/phpmyadmin/ssl');
3+
declare(strict_types=1);
54

65
/**
76
* Helper function to decode and save multiple SSL files from base64.
87
*
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.
1313
*
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.
1515
*/
16-
function decodeAndSaveSslFiles(string $base64_string, string $prefix, string $extension): string
16+
function decodeBase64AndSaveFiles(string $base64FilesContents, string $prefix, string $extension, string $storageFolder): string
1717
{
1818
// 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);
2121
}
2222

2323
// 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));
2525
$counter = 1;
26-
$ssl_files = [];
26+
$outputFiles = [];
2727

2828
// 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;
3131

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;
3535
exit(1);
3636
}
3737

3838
// 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;
4141
exit(1);
4242
}
4343

4444
// Add the output file path to the list
45-
$ssl_files[] = $output_file;
45+
$outputFiles[] = $outputFile;
4646
$counter++;
4747
}
4848

4949
// Return a comma-separated list of the generated file paths
50-
return implode(',', $ssl_files);
50+
return implode(',', $outputFiles);
5151
}

config.inc.php

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
'PMA_UPLOADDIR',
3030
'PMA_SAVEDIR',
3131
'PMA_SSL',
32+
'PMA_SSL_DIR',
3233
'PMA_SSL_VERIFY',
3334
'PMA_SSL_CA',
3435
'PMA_SSL_KEY',
@@ -38,7 +39,6 @@
3839
'PMA_SSL_CAS',
3940
'PMA_SSL_KEYS',
4041
'PMA_SSL_CERTS',
41-
'PMA_PMA_SSL_DIR'
4242
];
4343

4444
foreach ($vars as $var) {
@@ -47,6 +47,11 @@
4747
$_ENV[$var] = $env;
4848
}
4949
}
50+
51+
if (! defined('PMA_SSL_DIR')) {
52+
define('PMA_SSL_DIR', $_ENV['PMA_SSL_DIR'] ?? '/etc/phpmyadmin/ssl');
53+
}
54+
5055
if (isset($_ENV['PMA_QUERYHISTORYDB'])) {
5156
$cfg['QueryHistoryDB'] = (bool) $_ENV['PMA_QUERYHISTORYDB'];
5257
}
@@ -66,44 +71,32 @@
6671
}
6772

6873
if (isset($_ENV['PMA_SSL_CA_BASE64'])) {
69-
if (!is_dir(PMA_SSL_DIR)) {
70-
mkdir(PMA_SSL_DIR, 0755, true);
71-
}
72-
file_put_contents(PMA_SSL_DIR . '/pma-ssl-ca.pem', base64_decode($_ENV['PMA_SSL_CA_BASE64']));
73-
$_ENV['PMA_SSL_CA'] = PMA_SSL_DIR . '/pma-ssl-ca.pem';
74+
$_ENV['PMA_SSL_CA'] = decodeBase64AndSaveFiles($_ENV['PMA_SSL_CA_BASE64'], 'phpmyadmin-ssl-CA', 'pem', PMA_SSL_DIR);
7475
}
7576

7677
/* Decode and save the SSL key from base64 */
7778
if (isset($_ENV['PMA_SSL_KEY_BASE64'])) {
78-
if (!is_dir(PMA_SSL_DIR)) {
79-
mkdir(PMA_SSL_DIR, 0755, true);
80-
}
81-
file_put_contents(PMA_SSL_DIR . '/pma-ssl-key.key', base64_decode($_ENV['PMA_SSL_KEY_BASE64']));
82-
$_ENV['PMA_SSL_KEY'] = PMA_SSL_DIR . '/pma-ssl-key.key';
79+
$_ENV['PMA_SSL_KEY'] = decodeBase64AndSaveFiles($_ENV['PMA_SSL_KEY_BASE64'], 'phpmyadmin-ssl-CERT', 'cert', PMA_SSL_DIR);
8380
}
8481

8582
/* Decode and save the SSL certificate from base64 */
8683
if (isset($_ENV['PMA_SSL_CERT_BASE64'])) {
87-
if (!is_dir(PMA_SSL_DIR)) {
88-
mkdir(PMA_SSL_DIR, 0755, true);
89-
}
90-
file_put_contents(PMA_SSL_DIR . '/pma-ssl-cert.pem', base64_decode($_ENV['PMA_SSL_CERT_BASE64']));
91-
$_ENV['PMA_SSL_CERT'] = PMA_SSL_DIR . '/pma-ssl-cert.pem';
84+
$_ENV['PMA_SSL_CERT'] = decodeBase64AndSaveFiles($_ENV['PMA_SSL_CERT_BASE64'], 'phpmyadmin-ssl-CERT', 'cert', PMA_SSL_DIR);
9285
}
9386

9487
/* Decode and save multiple SSL CA certificates from base64 */
9588
if (isset($_ENV['PMA_SSL_CAS_BASE64'])) {
96-
$_ENV['PMA_SSL_CAS'] = decodeAndSaveSslFiles($_ENV['PMA_SSL_CAS_BASE64'], 'CA', 'pem');
89+
$_ENV['PMA_SSL_CAS'] = decodeBase64AndSaveFiles($_ENV['PMA_SSL_CAS_BASE64'], 'phpmyadmin-ssl-CA', 'pem', PMA_SSL_DIR);
9790
}
9891

9992
/* Decode and save multiple SSL keys from base64 */
10093
if (isset($_ENV['PMA_SSL_KEYS_BASE64'])) {
101-
$_ENV['PMA_SSL_KEYS'] = decodeAndSaveSslFiles($_ENV['PMA_SSL_KEYS_BASE64'], 'CERT', 'cert');
94+
$_ENV['PMA_SSL_KEYS'] = decodeBase64AndSaveFiles($_ENV['PMA_SSL_KEYS_BASE64'], 'phpmyadmin-ssl-CERT', 'cert', PMA_SSL_DIR);
10295
}
10396

10497
/* Decode and save multiple SSL certificates from base64 */
10598
if (isset($_ENV['PMA_SSL_CERTS_BASE64'])) {
106-
$_ENV['PMA_SSL_CERTS'] = decodeAndSaveSslFiles($_ENV['PMA_SSL_CERTS_BASE64'], 'KEY', 'key');
99+
$_ENV['PMA_SSL_CERTS'] = decodeBase64AndSaveFiles($_ENV['PMA_SSL_CERTS_BASE64'], 'phpmyadmin-ssl-KEY', 'key', PMA_SSL_DIR);
107100
}
108101

109102
/* Figure out hosts */

fpm-alpine/config.inc.php

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
'PMA_UPLOADDIR',
3030
'PMA_SAVEDIR',
3131
'PMA_SSL',
32+
'PMA_SSL_DIR',
3233
'PMA_SSL_VERIFY',
3334
'PMA_SSL_CA',
3435
'PMA_SSL_KEY',
@@ -38,7 +39,6 @@
3839
'PMA_SSL_CAS',
3940
'PMA_SSL_KEYS',
4041
'PMA_SSL_CERTS',
41-
'PMA_PMA_SSL_DIR'
4242
];
4343

4444
foreach ($vars as $var) {
@@ -47,6 +47,11 @@
4747
$_ENV[$var] = $env;
4848
}
4949
}
50+
51+
if (! defined('PMA_SSL_DIR')) {
52+
define('PMA_SSL_DIR', $_ENV['PMA_SSL_DIR'] ?? '/etc/phpmyadmin/ssl');
53+
}
54+
5055
if (isset($_ENV['PMA_QUERYHISTORYDB'])) {
5156
$cfg['QueryHistoryDB'] = (bool) $_ENV['PMA_QUERYHISTORYDB'];
5257
}
@@ -66,44 +71,32 @@
6671
}
6772

6873
if (isset($_ENV['PMA_SSL_CA_BASE64'])) {
69-
if (!is_dir(PMA_SSL_DIR)) {
70-
mkdir(PMA_SSL_DIR, 0755, true);
71-
}
72-
file_put_contents(PMA_SSL_DIR . '/pma-ssl-ca.pem', base64_decode($_ENV['PMA_SSL_CA_BASE64']));
73-
$_ENV['PMA_SSL_CA'] = PMA_SSL_DIR . '/pma-ssl-ca.pem';
74+
$_ENV['PMA_SSL_CA'] = decodeBase64AndSaveFiles($_ENV['PMA_SSL_CA_BASE64'], 'phpmyadmin-ssl-CA', 'pem', PMA_SSL_DIR);
7475
}
7576

7677
/* Decode and save the SSL key from base64 */
7778
if (isset($_ENV['PMA_SSL_KEY_BASE64'])) {
78-
if (!is_dir(PMA_SSL_DIR)) {
79-
mkdir(PMA_SSL_DIR, 0755, true);
80-
}
81-
file_put_contents(PMA_SSL_DIR . '/pma-ssl-key.key', base64_decode($_ENV['PMA_SSL_KEY_BASE64']));
82-
$_ENV['PMA_SSL_KEY'] = PMA_SSL_DIR . '/pma-ssl-key.key';
79+
$_ENV['PMA_SSL_KEY'] = decodeBase64AndSaveFiles($_ENV['PMA_SSL_KEY_BASE64'], 'phpmyadmin-ssl-CERT', 'cert', PMA_SSL_DIR);
8380
}
8481

8582
/* Decode and save the SSL certificate from base64 */
8683
if (isset($_ENV['PMA_SSL_CERT_BASE64'])) {
87-
if (!is_dir(PMA_SSL_DIR)) {
88-
mkdir(PMA_SSL_DIR, 0755, true);
89-
}
90-
file_put_contents(PMA_SSL_DIR . '/pma-ssl-cert.pem', base64_decode($_ENV['PMA_SSL_CERT_BASE64']));
91-
$_ENV['PMA_SSL_CERT'] = PMA_SSL_DIR . '/pma-ssl-cert.pem';
84+
$_ENV['PMA_SSL_CERT'] = decodeBase64AndSaveFiles($_ENV['PMA_SSL_CERT_BASE64'], 'phpmyadmin-ssl-CERT', 'cert', PMA_SSL_DIR);
9285
}
9386

9487
/* Decode and save multiple SSL CA certificates from base64 */
9588
if (isset($_ENV['PMA_SSL_CAS_BASE64'])) {
96-
$_ENV['PMA_SSL_CAS'] = decodeAndSaveSslFiles($_ENV['PMA_SSL_CAS_BASE64'], 'CA', 'pem');
89+
$_ENV['PMA_SSL_CAS'] = decodeBase64AndSaveFiles($_ENV['PMA_SSL_CAS_BASE64'], 'phpmyadmin-ssl-CA', 'pem', PMA_SSL_DIR);
9790
}
9891

9992
/* Decode and save multiple SSL keys from base64 */
10093
if (isset($_ENV['PMA_SSL_KEYS_BASE64'])) {
101-
$_ENV['PMA_SSL_KEYS'] = decodeAndSaveSslFiles($_ENV['PMA_SSL_KEYS_BASE64'], 'CERT', 'cert');
94+
$_ENV['PMA_SSL_KEYS'] = decodeBase64AndSaveFiles($_ENV['PMA_SSL_KEYS_BASE64'], 'phpmyadmin-ssl-CERT', 'cert', PMA_SSL_DIR);
10295
}
10396

10497
/* Decode and save multiple SSL certificates from base64 */
10598
if (isset($_ENV['PMA_SSL_CERTS_BASE64'])) {
106-
$_ENV['PMA_SSL_CERTS'] = decodeAndSaveSslFiles($_ENV['PMA_SSL_CERTS_BASE64'], 'KEY', 'key');
99+
$_ENV['PMA_SSL_CERTS'] = decodeBase64AndSaveFiles($_ENV['PMA_SSL_CERTS_BASE64'], 'phpmyadmin-ssl-KEY', 'key', PMA_SSL_DIR);
107100
}
108101

109102
/* Figure out hosts */

fpm-alpine/helpers.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
11
<?php
22

3-
4-
define('PMA_SSL_DIR', $_ENV['PMA_SSL_DIR'] ?? '/etc/phpmyadmin/ssl');
3+
declare(strict_types=1);
54

65
/**
76
* Helper function to decode and save multiple SSL files from base64.
87
*
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.
1313
*
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.
1515
*/
16-
function decodeAndSaveSslFiles(string $base64_string, string $prefix, string $extension): string
16+
function decodeBase64AndSaveFiles(string $base64FilesContents, string $prefix, string $extension, string $storageFolder): string
1717
{
1818
// 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);
2121
}
2222

2323
// 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));
2525
$counter = 1;
26-
$ssl_files = [];
26+
$outputFiles = [];
2727

2828
// 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;
3131

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;
3535
exit(1);
3636
}
3737

3838
// 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;
4141
exit(1);
4242
}
4343

4444
// Add the output file path to the list
45-
$ssl_files[] = $output_file;
45+
$outputFiles[] = $outputFile;
4646
$counter++;
4747
}
4848

4949
// Return a comma-separated list of the generated file paths
50-
return implode(',', $ssl_files);
50+
return implode(',', $outputFiles);
5151
}

0 commit comments

Comments
 (0)