1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SET\Http\Controllers\Installation; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Illuminate\Support\Facades\Artisan; |
8
|
|
|
use RachidLaasri\LaravelInstaller\Helpers\EnvironmentManager; |
9
|
|
|
use SET\Http\Controllers\Controller; |
10
|
|
|
|
11
|
|
|
class EnvironmentController extends Controller |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var EnvironmentManager |
15
|
|
|
*/ |
16
|
|
|
protected $EnvironmentManager; |
17
|
|
|
protected $envPath; |
18
|
|
|
protected $envSetup = [ |
19
|
|
|
'APP_ENV' => FILTER_SANITIZE_ENCODED, |
20
|
|
|
'APP_DEBUG' => FILTER_SANITIZE_ENCODED, |
21
|
|
|
'APP_KEY' => FILTER_DEFAULT, |
22
|
|
|
'APP_URL' => FILTER_VALIDATE_URL, |
23
|
|
|
'DB_CONNECTION' => FILTER_SANITIZE_ENCODED, |
24
|
|
|
'DB_HOST' => FILTER_SANITIZE_ENCODED, |
25
|
|
|
'DB_DATABASE' => FILTER_SANITIZE_ENCODED, |
26
|
|
|
'DB_USERNAME' => FILTER_SANITIZE_ENCODED, |
27
|
|
|
'DB_PASSWORD' => FILTER_DEFAULT, |
28
|
|
|
'MAIL_DRIVER' => FILTER_SANITIZE_ENCODED, |
29
|
|
|
'MAIL_HOST' => FILTER_SANITIZE_ENCODED, |
30
|
|
|
'MAIL_PORT' => FILTER_VALIDATE_INT, |
31
|
|
|
'MAIL_USERNAME' => FILTER_SANITIZE_ENCODED, |
32
|
|
|
'MAIL_PASSWORD' => FILTER_DEFAULT, |
33
|
|
|
'MAIL_ENCRYPTION' => FILTER_SANITIZE_ENCODED, |
34
|
|
|
'MAIL_FROM_ADDRESS' => FILTER_VALIDATE_EMAIL, |
35
|
|
|
'MAIL_FROM_NAME' => FILTER_DEFAULT, |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param EnvironmentManager $environmentManager |
40
|
|
|
*/ |
41
|
|
|
public function __construct(EnvironmentManager $environmentManager) |
42
|
|
|
{ |
43
|
|
|
$this->EnvironmentManager = $environmentManager; |
44
|
|
|
$this->envPath = base_path('.env'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Display the Environment page. |
49
|
|
|
* |
50
|
|
|
* @return \Illuminate\View\View |
51
|
|
|
*/ |
52
|
|
|
public function index() |
53
|
|
|
{ |
54
|
|
|
$envConfig = $this->EnvironmentManager->getEnvContent(); |
55
|
|
|
$fields = $this->breakApartEnv($envConfig); |
56
|
|
|
|
57
|
|
|
return view('vendor.installer.environment', compact('fields')); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Processes the newly saved environment configuration and redirects back. |
62
|
|
|
* |
63
|
|
|
* @param Request $input |
64
|
|
|
* |
65
|
|
|
* @return \Illuminate\Http\RedirectResponse |
66
|
|
|
*/ |
67
|
|
|
public function store(Request $input) |
68
|
|
|
{ |
69
|
|
|
$env = $this->flattenRequest($input); |
70
|
|
|
$message = $this->saveFile($env); |
71
|
|
|
|
72
|
|
|
Artisan::call('key:generate'); |
73
|
|
|
|
74
|
|
|
return redirect()->route('LaravelInstaller::requirements') |
75
|
|
|
->with(['message' => $message]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Key should be before the first = symbol and value should be after. |
80
|
|
|
* Each new line (\r\n) should be a new array entry. |
81
|
|
|
* |
82
|
|
|
* @param $string |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
private function breakApartEnv($string) |
87
|
|
|
{ |
88
|
|
|
preg_match_all('/([^=]*?)=([^\r\n]*?)[\r\n]+/', $string, $matches); |
89
|
|
|
|
90
|
|
|
$array = array_combine($matches[1], $matches[2]); |
91
|
|
|
$array['MAIL_FROM_NAME'] = $this->removeQuotes($array['MAIL_FROM_NAME']); |
92
|
|
|
|
93
|
|
|
return $array; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private function flattenRequest(Request $input) |
97
|
|
|
{ |
98
|
|
|
$fields = $this->breakApartEnv($this->EnvironmentManager->getEnvContent()); |
99
|
|
|
$results = $this->scrubInput(array_merge($fields, $input->toArray())); |
100
|
|
|
$results['MAIL_FROM_NAME'] = '"'.$this->removeQuotes($results['MAIL_FROM_NAME']).'"'; |
101
|
|
|
|
102
|
|
|
$env = ''; |
103
|
|
|
foreach ($results as $key => $value) { |
104
|
|
|
$env .= $key.'='.$value."\r\n"; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $env; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Save the edited content to the file. |
112
|
|
|
* |
113
|
|
|
* @param $string |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function saveFile($string) |
118
|
|
|
{ |
119
|
|
|
$message = trans('messages.environment.success'); |
120
|
|
|
|
121
|
|
|
try { |
122
|
|
|
file_put_contents($this->envPath, $string); |
123
|
|
|
} catch (Exception $e) { |
124
|
|
|
$message = trans('messages.environment.errors'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $message; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
private function removeQuotes($string) |
131
|
|
|
{ |
132
|
|
|
$string = str_replace('"', '', $string); |
133
|
|
|
|
134
|
|
|
return str_replace("'", '', $string); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
private function scrubInput($array) |
138
|
|
|
{ |
139
|
|
|
$array = filter_var_array($array, $this->envSetup); |
140
|
|
|
foreach ($array as $key => $value) { |
141
|
|
|
if (!in_array($key, array_keys($this->envSetup))) { |
142
|
|
|
unset($array[$key]); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $array; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|