3 namespace Friendica\Util\Config;
6 * The ConfigCacheSaver saves specific variables back from the ConfigCache (@see ConfigCache )
7 * into the config-files
9 * It is capable of loading the following config files:
10 * - *.config.php (current)
11 * - *.ini.php (deprecated)
12 * - *.htconfig.php (deprecated)
14 class ConfigCacheSaver extends ConfigCacheManager
17 * The standard indentation for config files
23 * Saves a given value to the config file
24 * Either it replaces the current value or it will get added
26 * @param string $cat The configuration category
27 * @param string $key The configuration key
28 * @param string $value The new value
30 public function saveToConfigFile($cat, $key, $value)
32 $this->saveToLegacyConfig('htpreconfig', $cat, $key, $value);
33 $this->saveToLegacyConfig('htconfig', $cat, $key, $value);
34 $this->saveToCoreConfig('local', $cat, $key, $value);
38 * Saves a value to either an config or an ini file
40 * @param string $name The configuration file name ('local', 'addon', ..)
41 * @param string $cat The configuration category
42 * @param string $key The configuration key
43 * @param string $value The new value
45 private function saveToCoreConfig($name, $cat, $key, $value)
47 if (!empty($this->getConfigFullName($name))) {
48 $this->saveConfigFile($this->getConfigFullName($name), $cat, $key, $value);
49 } elseif (!empty($this->getIniFullName($name))) {
50 $this->saveINIConfigFile($this->getIniFullName($name), $cat, $key, $value);
57 * Saves a value to a config file
59 * @param string $fullName The configuration full name (including the path)
60 * @param string $cat The configuration category
61 * @param string $key The configuration key
62 * @param string $value The new value
64 * @throws \Exception In case a file operation doesn't work
66 private function saveConfigFile($fullName, $cat, $key, $value)
68 $reading = fopen($fullName, 'r');
70 throw new \Exception('Cannot open config file \'' . $fullName . '\'.');
72 $writing = fopen($fullName . '.tmp', 'w');
74 throw new \Exception('Cannot create temporary config file \'' . $fullName . '.tmp\'.');
76 $categoryFound = false;
77 $categoryBracketFound = false;
79 $lineArrowFound = false;
80 while (!feof($reading)) {
81 $line = fgets($reading);
82 // find the first line like "'system' =>"
83 if (!$categoryFound && stristr($line, sprintf('\'%s\'', $cat))) {
84 $categoryFound = true;
86 // find the first line with a starting bracket ( "[" )
87 if ($categoryFound && !$categoryBracketFound && stristr($line, '[')) {
88 $categoryBracketFound = true;
90 // find the first line with the key like "'value'"
91 if ($categoryBracketFound && !$lineFound && stristr($line, sprintf('\'%s\'', $key))) {
94 // find the first line with an arrow ("=>") after finding the key
95 if ($lineFound && !$lineArrowFound && stristr($line, '=>')) {
96 $lineArrowFound = true;
98 // find the current value and replace it
99 if ($lineArrowFound && preg_match_all('/\'(.*?)\'/', $line, $matches, PREG_SET_ORDER)) {
100 $lineVal = end($matches)[0];
101 $writeLine = str_replace($lineVal, '\'' . $value . '\'', $line);
102 $categoryFound = false;
103 $categoryBracketFound = false;
105 $lineArrowFound = false;
106 // if a line contains a closing bracket for the category ( "]" ) and we didn't find the key/value pair,
107 // add it as a new line before the closing bracket
108 } elseif ($categoryBracketFound && !$lineArrowFound && stristr($line, ']')) {
109 $categoryFound = false;
110 $categoryBracketFound = false;
112 $lineArrowFound = false;
113 $writeLine = sprintf(self::INDENT . self::INDENT .'\'%s\' => \'%s\',' . PHP_EOL, $key, $value);
118 fputs($writing, $writeLine);
120 if (!fclose($reading)) {
121 throw new \Exception('Cannot close config file \'' . $fullName . '\'.');
123 if (!fclose($writing)) {
124 throw new \Exception('Cannot close temporary config file \'' . $fullName . '.tmp\'.');
126 if (!rename($fullName, $fullName . '.old')) {
127 throw new \Exception('Cannot backup current config file \'' . $fullName . '\'.');
129 if (!rename($fullName . '.tmp', $fullName)) {
130 throw new \Exception('Cannot move temporary config file \'' . $fullName . '.tmp\' to current.');
135 * Saves a value to a ini file
137 * @param string $fullName The configuration full name (including the path)
138 * @param string $cat The configuration category
139 * @param string $key The configuration key
140 * @param string $value The new value
142 private function saveINIConfigFile($fullName, $cat, $key, $value)
144 $reading = fopen($fullName, 'r');
145 $writing = fopen($fullName . '.tmp', 'w');
146 $categoryFound = false;
147 while (!feof($reading)) {
148 $line = fgets($reading);
149 if (!$categoryFound && stristr($line, sprintf('[%s]', $cat))) {
150 $categoryFound = true;
152 } elseif ($categoryFound && preg_match_all('/^' . $key . '\s*=\s*(.*?)$/', $line, $matches, PREG_SET_ORDER)) {
153 $writeLine = $key . ' = ' . $value . PHP_EOL;
154 $categoryFound = false;
155 } elseif ($categoryFound && (preg_match_all('/^\[.*?\]$/', $line) || preg_match_all('/^INI;.*$/', $line))) {
156 $categoryFound = false;
157 $writeLine = $key . ' = ' . $value . PHP_EOL;
162 fputs($writing, $writeLine);
166 rename($fullName, $fullName . '.old');
167 rename($fullName . '.tmp', $fullName);
170 private function saveToLegacyConfig($name, $cat, $key, $value)
172 if (empty($this->getHtConfigFullName($name))) {
175 $fullName = $this->getHtConfigFullName($name);
176 $reading = fopen($fullName, 'r');
177 $writing = fopen($fullName . '.tmp', 'w');
179 while (!feof($reading)) {
180 $line = fgets($reading);
181 if (preg_match_all('/^\$a\-\>config\[\'' . $cat . '\',\'' . $key . '\'\]\s*=\s\'*(.*?)\'$/', $line, $matches, PREG_SET_ORDER)) {
182 $writeLine = $key . ' = ' . $value . PHP_EOL;
187 fputs($writing, $writeLine);
190 $writeLine = $key . ' = ' . $value . PHP_EOL;
191 fputs($writing, $writeLine);
195 rename($fullName, $fullName . '.old');
196 rename($fullName . '.tmp', $fullName);