11<?php
2- /**
3- * Pterodactyl - Panel
4- * Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5- *
6- * This software is licensed under the terms of the MIT license.
7- * https://opensource.org/licenses/MIT
8- */
92
103namespace Pterodactyl \Traits \Commands ;
114
125use Pterodactyl \Exceptions \PterodactylException ;
136
147trait EnvironmentWriterTrait
158{
9+ /**
10+ * Escapes an environment value by looking for any characters that could
11+ * reasonablly cause environment parsing issues. Those values are then wrapped
12+ * in quotes before being returned.
13+ */
14+ public function escapeEnvironmentValue (string $ value ): string
15+ {
16+ if (!preg_match ('/^\"(.*)\"$/ ' , $ value ) && preg_match ('/([^\w.\-+\/])+/ ' , $ value )) {
17+ return sprintf ('"%s" ' , addslashes ($ value ));
18+ }
19+
20+ return $ value ;
21+ }
22+
1623 /**
1724 * Update the .env file for the application using the passed in values.
1825 *
@@ -28,14 +35,7 @@ public function writeToEnvironment(array $values = [])
2835 $ saveContents = file_get_contents ($ path );
2936 collect ($ values )->each (function ($ value , $ key ) use (&$ saveContents ) {
3037 $ key = strtoupper ($ key );
31- // If the key value is not sorrounded by quotation marks, and contains anything that could reasonably
32- // cause environment parsing issues, wrap it in quotes before writing it. This also adds slashes to the
33- // value to ensure quotes within it don't cause us issues.
34- if (!preg_match ('/^\"(.*)\"$/ ' , $ value ) && preg_match ('/([^\w.\-+\/])+/ ' , $ value )) {
35- $ value = sprintf ('"%s" ' , addslashes ($ value ));
36- }
37-
38- $ saveValue = sprintf ('%s=%s ' , $ key , $ value );
38+ $ saveValue = sprintf ('%s=%s ' , $ key , $ this ->escapeEnvironmentValue ($ value ));
3939
4040 if (preg_match_all ('/^ ' . $ key . '=(.*)$/m ' , $ saveContents ) < 1 ) {
4141 $ saveContents = $ saveContents . PHP_EOL . $ saveValue ;
0 commit comments