diff options
author | Andreas Gohr <gohr@cosmocode.de> | 2019-10-10 09:55:14 +0200 |
---|---|---|
committer | Andreas Gohr <gohr@cosmocode.de> | 2019-10-10 09:55:14 +0200 |
commit | 31a58aba4c24b34c34ad5764d1a35b7c398c3a2c (patch) | |
tree | 7f4d1546fbb69863a7d366fc1ff647f784853b68 /lib/plugins/config/core/Writer.php | |
parent | af7ba5aa0bd10fc0ad9ef983006305b4c5a8ed42 (diff) | |
parent | c0c77cd20b23921c9e893bb70b99f38be153875a (diff) | |
download | dokuwiki-31a58aba4c24b34c34ad5764d1a35b7c398c3a2c.tar.gz dokuwiki-31a58aba4c24b34c34ad5764d1a35b7c398c3a2c.zip |
Merge branch 'psr2'
* psr2: (160 commits)
fixed merge error
Moved parts of the Asian word handling to its own class
ignore snake_case error of substr_replace
fixed some line length errors
ignore PSR2 in the old form class
fix PSR2 error in switch statement
replaced deprecated utf8 functions
formatting cleanup
mark old utf8 functions deprecated
some more PSR2 cleanup
Some cleanup for the UTF-8 stuff
Moved all utf8 methods to their own namespaced classes
Create separate table files for UTF-8 handling
Ignore mixed concerns in loader
Use type safe comparisons in loader
Remove obsolete include
adjust phpcs exclude patterns for new plugin classes
🚚 Move Subscription class to deprecated.php
♻️ Split up ChangesSubscriptionSender into multiple classes
Minor optimizations in PluginController
...
Diffstat (limited to 'lib/plugins/config/core/Writer.php')
-rw-r--r-- | lib/plugins/config/core/Writer.php | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/lib/plugins/config/core/Writer.php b/lib/plugins/config/core/Writer.php new file mode 100644 index 000000000..6dee223ac --- /dev/null +++ b/lib/plugins/config/core/Writer.php @@ -0,0 +1,116 @@ +<?php + +namespace dokuwiki\plugin\config\core; +use dokuwiki\plugin\config\core\Setting\Setting; + +/** + * Writes the settings to the correct local file + */ +class Writer { + /** @var string header info */ + protected $header = 'Dokuwiki\'s Main Configuration File - Local Settings'; + + /** @var string the file where the config will be saved to */ + protected $savefile; + + /** + * Writer constructor. + */ + public function __construct() { + global $config_cascade; + $this->savefile = end($config_cascade['main']['local']); + } + + /** + * Save the given settings + * + * @param Setting[] $settings + * @throws \Exception + */ + public function save($settings) { + global $conf; + if($this->isLocked()) throw new \Exception('no save'); + + // backup current file (remove any existing backup) + if(file_exists($this->savefile)) { + if(file_exists($this->savefile . '.bak')) @unlink($this->savefile . '.bak'); + if(!io_rename($this->savefile, $this->savefile . '.bak')) throw new \Exception('no backup'); + } + + if(!$fh = @fopen($this->savefile, 'wb')) { + io_rename($this->savefile . '.bak', $this->savefile); // problem opening, restore the backup + throw new \Exception('no save'); + } + + $out = $this->getHeader(); + foreach($settings as $setting) { + if($setting->shouldBeSaved()) { + $out .= $setting->out('conf', 'php'); + } + } + + fwrite($fh, $out); + fclose($fh); + if($conf['fperm']) chmod($this->savefile, $conf['fperm']); + $this->opcacheUpdate($this->savefile); + } + + /** + * Update last modified time stamp of the config file + * + * Will invalidate all DokuWiki caches + * + * @throws \Exception when the config isn't writable + */ + public function touch() { + if($this->isLocked()) throw new \Exception('no save'); + @touch($this->savefile); + $this->opcacheUpdate($this->savefile); + } + + /** + * Invalidate the opcache of the given file + * + * @todo this should probably be moved to core + * @param string $file + */ + protected function opcacheUpdate($file) { + if(!function_exists('opcache_invalidate')) return; + opcache_invalidate($file); + } + + /** + * Configuration is considered locked if there is no local settings filename + * or the directory its in is not writable or the file exists and is not writable + * + * @return bool true: locked, false: writable + */ + public function isLocked() { + if(!$this->savefile) return true; + if(!is_writable(dirname($this->savefile))) return true; + if(file_exists($this->savefile) && !is_writable($this->savefile)) return true; + return false; + } + + /** + * Returns the PHP intro header for the config file + * + * @return string + */ + protected function getHeader() { + return join( + "\n", + array( + '<?php', + '/*', + ' * ' . $this->header, + ' * Auto-generated by config plugin', + ' * Run for user: ' . $_SERVER['REMOTE_USER'], + ' * Date: ' . date('r'), + ' */', + '', + '' + ) + ); + } +} |