Files
librenms-librenms/LibreNMS/Util/EnvHelper.php

159 lines
4.6 KiB
PHP
Raw Normal View History

2020-06-08 02:21:03 -05:00
<?php
/**
* EnvHelper.php
*
* -Description-
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2020 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Util;
2020-06-19 00:17:36 -05:00
use LibreNMS\Exceptions\FileWriteFailedException;
2020-06-08 02:21:03 -05:00
class EnvHelper
{
/**
2020-06-19 00:17:36 -05:00
* Set a setting in .env file.
* Will only set non-empty unset variables
*
* @param array $settings KEY => value list of settings
* @param array $unset Remove the given KEYS from the config
* @param string $file
* @return string
2020-06-08 02:21:03 -05:00
*/
2020-06-19 00:17:36 -05:00
public static function writeEnv($settings, $unset = [], $file = '.env')
2020-06-08 02:21:03 -05:00
{
2020-06-19 00:17:36 -05:00
$original_content = file_get_contents($file);
2020-06-08 02:21:03 -05:00
2020-06-19 00:17:36 -05:00
$new_content = self::setEnv($original_content, $settings, $unset);
// only write if the content has changed
if ($new_content !== $original_content) {
file_put_contents($file, $new_content);
}
return $new_content;
2020-06-08 02:21:03 -05:00
}
/**
* Set a setting in .env file.
* Will only set non-empty unset variables
*
* @param array $settings KEY => value list of settings
2020-06-09 23:24:43 -05:00
* @param array $unset Remove the given KEYS from the config
2020-06-08 02:21:03 -05:00
* @param string $file
* @return string
2020-06-19 00:17:36 -05:00
* @throws \LibreNMS\Exceptions\FileWriteFailedException
2020-06-08 02:21:03 -05:00
*/
2020-06-19 00:17:36 -05:00
public static function tryWriteEnv($settings, $unset = [], $file = '.env')
2020-06-08 02:21:03 -05:00
{
2020-06-19 00:17:36 -05:00
$original_content = file_get_contents($file);
$new_content = self::setEnv($original_content, $settings, $unset);
// only write if the content has changed
if ($new_content !== $original_content) {
if(!file_put_contents($file, $new_content)) {
throw new FileWriteFailedException($file);
}
}
return $new_content;
}
2020-06-08 02:21:03 -05:00
2020-06-19 00:17:36 -05:00
/**
* Set a setting in .env file.
* Will only set non-empty unset variables
*
* @param string $content
* @param array $settings KEY => value list of settings
* @param array $unset Remove the given KEYS from the config
* @return string
*/
public static function setEnv($content, $settings, $unset = [])
{
2020-06-08 02:21:03 -05:00
// ensure trailing line return
if (substr($content, -1) !== PHP_EOL) {
$content .= PHP_EOL;
}
2020-06-09 23:24:43 -05:00
// unset the given keys
if (!empty($unset)) {
$regex = '/^(' . implode('|', $unset) . ')=.*$\n/m';
$content = preg_replace($regex, '', $content);
}
2020-06-08 02:21:03 -05:00
foreach ($settings as $key => $value) {
// only add non-empty settings
if (empty($value)) {
continue;
}
2020-06-19 00:17:36 -05:00
$value = self::escapeValue($value);
2020-06-08 02:21:03 -05:00
if (strpos($content, "$key=") !== false) {
// only replace ones that aren't already set for safety and uncomment
// escape $ in the replacement
$content = preg_replace("/#?$key=\n/", addcslashes("$key=$value\n", '$'), $content);
} else {
$content .= "$key=$value\n";
}
}
2020-06-19 00:17:36 -05:00
return self::fixComments($content);
}
2020-06-08 02:21:03 -05:00
2020-06-19 00:17:36 -05:00
/**
* Fix .env with # in them without a space before it
*
* @param string $dotenv
* @return string
*/
private static function fixComments($dotenv)
{
return implode(PHP_EOL, array_map(function ($line) {
$parts = explode('=', $line, 2);
if (isset($parts[1])
&& preg_match('/(?<!\s)#/', $parts[1]) // number symbol without a space before it
&& !preg_match('/^(".*"|\'.*\')$/', $parts[1]) // not already quoted
) {
return trim($parts[0]) . '="' . trim($parts[1]) . '"';
}
return $line;
}, explode(PHP_EOL, $dotenv)));
}
/**
* quote strings with spaces
*
* @param $value
* @return string
*/
private static function escapeValue($value)
{
if (strpos($value, ' ') !== false) {
return "\"$value\"";
2020-06-08 02:21:03 -05:00
}
2020-06-19 00:17:36 -05:00
return $value;
2020-06-08 02:21:03 -05:00
}
}