Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 61 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| EnvService | |
0.00% |
0 / 61 |
|
0.00% |
0 / 6 |
342 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| write | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
12 | |||
| update | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 | |||
| read | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
30 | |||
| detectBaseUrl | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| generateEncryptionKey | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Modules\Installer\Core; |
| 4 | |
| 5 | use App\Modules\Installer\Core\Contracts\EnvServiceInterface; |
| 6 | |
| 7 | class EnvService implements EnvServiceInterface |
| 8 | { |
| 9 | private string $path; |
| 10 | |
| 11 | public function __construct() |
| 12 | { |
| 13 | $this->path = ROOTPATH . '.env'; |
| 14 | } |
| 15 | |
| 16 | public function write(array $data): bool |
| 17 | { |
| 18 | try { |
| 19 | $baseUrl = $this->detectBaseUrl(); |
| 20 | |
| 21 | $env = [ |
| 22 | // APP |
| 23 | 'CI_ENVIRONMENT' => 'production', |
| 24 | 'app.baseURL' => $baseUrl, |
| 25 | 'app.forceGlobalSecureRequests' => 'false', |
| 26 | |
| 27 | // DB |
| 28 | 'database.default.hostname' => $data['host'] ?? '', |
| 29 | 'database.default.database' => $data['database'] ?? '', |
| 30 | 'database.default.username' => $data['username'] ?? '', |
| 31 | 'database.default.password' => $data['password'] ?? '', |
| 32 | 'database.default.DBDriver' => 'MySQLi', |
| 33 | 'database.default.port' => $data['port'] ?? '3306', |
| 34 | |
| 35 | // ENCRYPTION |
| 36 | 'encryption.key' => $data['encryption_key'] ?? $this->generateEncryptionKey(), |
| 37 | |
| 38 | // SESSION |
| 39 | 'session.driver' => 'CodeIgniter\Session\Handlers\FileHandler', |
| 40 | 'session.cookieName' => 'ci_session', |
| 41 | 'session.savePath' => 'writable/session', |
| 42 | |
| 43 | // LICENCE & UPDATER |
| 44 | 'LICENSE_API_URL' => 'https://apicloudlicensing.methodeamani.com', |
| 45 | 'UPDATER_REGISTRY_URL' => 'https://apicloudlicensing.methodeamani.com', |
| 46 | 'SUPPORT_API_KEY' => $data['api_key'] ?? '', |
| 47 | |
| 48 | // SECURITY / CRON |
| 49 | 'CRON_SECRET' => '', |
| 50 | ]; |
| 51 | |
| 52 | $content = "# AUTO GENERATED ENV\n\n"; |
| 53 | |
| 54 | foreach ($env as $key => $value) { |
| 55 | $content .= "{$key} = {$value}\n"; |
| 56 | } |
| 57 | |
| 58 | file_put_contents($this->path, $content, LOCK_EX); |
| 59 | |
| 60 | return true; |
| 61 | |
| 62 | } catch (\Throwable) { |
| 63 | return false; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | public function update(string $key, string $value): bool |
| 68 | { |
| 69 | try { |
| 70 | if (!file_exists($this->path)) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | $content = file_get_contents($this->path); |
| 75 | |
| 76 | // Remplace si la clé existe déjà |
| 77 | if (preg_match('/^' . preg_quote($key, '/') . '\s*=.*/m', $content)) { |
| 78 | $content = preg_replace( |
| 79 | '/^' . preg_quote($key, '/') . '\s*=.*/m', |
| 80 | $key . ' = ' . $value, |
| 81 | $content |
| 82 | ); |
| 83 | } else { |
| 84 | // Ajoute à la fin |
| 85 | $content .= "\n" . $key . ' = ' . $value . "\n"; |
| 86 | } |
| 87 | |
| 88 | file_put_contents($this->path, $content, LOCK_EX); |
| 89 | |
| 90 | return true; |
| 91 | |
| 92 | } catch (\Throwable) { |
| 93 | return false; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | |
| 98 | public function read(string $key, mixed $default = null): mixed |
| 99 | { |
| 100 | if (! file_exists($this->path)) { |
| 101 | return $default; |
| 102 | } |
| 103 | |
| 104 | $lines = file($this->path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
| 105 | |
| 106 | foreach ($lines as $line) { |
| 107 | |
| 108 | if (str_starts_with(trim($line), '#')) { |
| 109 | continue; |
| 110 | } |
| 111 | |
| 112 | [$k, $v] = array_pad(explode('=', $line, 2), 2, null); |
| 113 | |
| 114 | if (trim($k) === $key) { |
| 115 | return trim($v ?? '', " \t\"'"); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return $default; |
| 120 | } |
| 121 | |
| 122 | private function detectBaseUrl(): string |
| 123 | { |
| 124 | $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') |
| 125 | ? 'https' |
| 126 | : 'http'; |
| 127 | |
| 128 | $host = $_SERVER['HTTP_HOST'] ?? $_SERVER['SERVER_NAME'] ?? 'localhost'; |
| 129 | |
| 130 | $scriptPath = $_SERVER['SCRIPT_NAME'] ?? ''; |
| 131 | $basePath = rtrim(dirname($scriptPath), '/\\'); |
| 132 | |
| 133 | // Supprimer /public du chemin si présent |
| 134 | $basePath = preg_replace('#/public$#', '', $basePath); |
| 135 | |
| 136 | return $protocol . '://' . $host . ($basePath ?: '') . '/'; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | private function generateEncryptionKey(): string |
| 141 | { |
| 142 | // nouveau standard |
| 143 | return 'hex2bin:' . bin2hex(random_bytes(32)); |
| 144 | |
| 145 | // LEGACY |
| 146 | // return 'hex:' . bin2hex(random_bytes(32)); |
| 147 | } |
| 148 | |
| 149 | } |