| 1 |
<?php |
| 2 |
/* Copyright (C) 2017 Icinga Development Team <[email protected]> */ |
| 3 |
|
| 4 |
namespace Icinga\Module\Toplevelview; |
| 5 |
|
| 6 |
use Icinga\Module\Toplevelview\Model\View; |
| 7 |
use Icinga\Module\Toplevelview\Util\Auth; |
| 8 |
|
| 9 |
use Icinga\Exception\NotWritableError; |
| 10 |
use Icinga\Exception\NotReadableError; |
| 11 |
use Icinga\Util\DirectoryIterator; |
| 12 |
use Icinga\Web\Session; |
| 13 |
|
| 14 |
/** |
| 15 |
* Manages the View's configurations, loads and stores Views. |
| 16 |
*/ |
| 17 |
class ViewConfig |
| 18 |
{ |
| 19 |
use Auth; |
| 20 |
|
| 21 |
const FORMAT_YAML = 'yml'; |
| 22 |
const SESSION_PREFIX = 'toplevelview_view_'; |
| 23 |
|
| 24 |
/** |
| 25 |
* The module's configuration directory |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
protected $config_dir; |
| 29 |
|
| 30 |
/** |
| 31 |
* @param $config_dir_module string Path to the module's configuration |
| 32 |
*/ |
| 33 |
public function __construct($config_dir_module) |
| 34 |
{ |
| 35 |
// Ensure the Views configuration directory exists |
| 36 |
$config_dir = $config_dir_module . DIRECTORY_SEPARATOR . 'views'; |
| 37 |
$this->ensureDirExists($config_dir_module); |
| 38 |
$this->ensureDirExists($config_dir); |
| 39 |
// Set the configuration directory |
| 40 |
$this->config_dir = $config_dir; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* getConfigDir returns the configuration directory |
| 45 |
* |
| 46 |
* @return string |
| 47 |
* @throws ProgrammingError When dir is not yet set |
| 48 |
*/ |
| 49 |
public function getConfigDir(): string |
| 50 |
{ |
| 51 |
if ($this->config_dir === null) { |
| 52 |
throw new ProgrammingError('Configuration directory does not exit'); |
| 53 |
} |
| 54 |
return $this->config_dir; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* ensureDirExists checks if a given path exists and creates the path if it doesn't |
| 59 |
* |
| 60 |
* @param string $path Path to create the directory at |
| 61 |
* @param string $mode Mode to create the directory with |
| 62 |
* @throws NotWritableError if the directory is not writable |
| 63 |
*/ |
| 64 |
protected function ensureDirExists($path, $mode = '2770'): void |
| 65 |
{ |
| 66 |
if (file_exists($path)) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
if (mkdir($path) !== true) { |
| 71 |
throw new NotWritableError( |
| 72 |
'Configuration directory does not exit, and it could not be created: %s', |
| 73 |
$path |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
$octalMode = intval($mode, 8); |
| 78 |
if ($mode !== null && false === @chmod($path, $octalMode)) { |
| 79 |
throw new NotWritableError('Failed to set file mode "%s" on file "%s"', $mode, $path); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* loadFromSession loads a View stored in the user's session |
| 85 |
* |
| 86 |
* @param string $name name of the View |
| 87 |
* @param string $format format of the View |
| 88 |
* @return ?View |
| 89 |
*/ |
| 90 |
protected function loadFromSession($name, $format): ?View |
| 91 |
{ |
| 92 |
// Try to load data from the session |
| 93 |
$sessionConfig = Session::getSession()->get(self::SESSION_PREFIX . $name); |
| 94 |
// If there is none, we return |
| 95 |
if ($sessionConfig === null) { |
| 96 |
return null; |
| 97 |
} |
| 98 |
// If there is data, create the View with the data |
| 99 |
$view = (new View($name, $format))->setText($sessionConfig); |
| 100 |
$view->hasBeenLoadedFromSession = true; |
| 101 |
$view->hasBeenLoaded = true; |
| 102 |
|
| 103 |
return $view; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* loadFromFile loads a View stored in a configuration file |
| 108 |
* |
| 109 |
* @param string $name name of the View |
| 110 |
* @param string $format format of the View |
| 111 |
* @throws NotReadableError if the file cannot be read |
| 112 |
* @return ?View |
| 113 |
*/ |
| 114 |
protected function loadFromFile($name, $format): ?View |
| 115 |
{ |
| 116 |
// Try to load the data from the file |
| 117 |
$file_path = $this->getConfigDir() . DIRECTORY_SEPARATOR . $name . '.' . $format; |
| 118 |
|
| 119 |
if (!is_readable($file_path)) { |
| 120 |
throw new NotReadableError('Could not read file %s', $file_path); |
| 121 |
} |
| 122 |
|
| 123 |
$text = file_get_contents($file_path); |
| 124 |
|
| 125 |
if ($text === false) { |
| 126 |
throw new NotReadableError('Could not read file %s', $file_path); |
| 127 |
} |
| 128 |
// If there is data, create the View with the data |
| 129 |
$view = (new View($name, $format))->setText($text); |
| 130 |
$view->hasBeenLoadedFromSession = false; |
| 131 |
$view->hasBeenLoaded = true; |
| 132 |
|
| 133 |
return $view; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* writeFile writes the given content to a given path. |
| 138 |
* Used to store the View's YAML content. |
| 139 |
* |
| 140 |
* @param $path Path to the file |
| 141 |
* @param $content Content of the file |
| 142 |
* @param $mode Mode of the new file |
| 143 |
* @throws NotWritableError if the file is not writable |
| 144 |
*/ |
| 145 |
protected function writeFile($path, $content, $mode = '0660'): void |
| 146 |
{ |
| 147 |
$existing = file_exists($path); |
| 148 |
if (file_put_contents($path, $content) === false) { |
| 149 |
throw new NotWritableError('Could not save to %s', $path); |
| 150 |
} |
| 151 |
|
| 152 |
if ($existing === false) { |
| 153 |
$octalMode = intval($mode, 8); |
| 154 |
if ($mode !== null && false === @chmod($path, $octalMode)) { |
| 155 |
throw new NotWritableError('Failed to set file mode "%s" on file "%s"', $mode, $path); |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Load a View by its name |
| 162 |
* |
| 163 |
* @param string $name Name of the view to load |
| 164 |
* @param string $format The format of the view |
| 165 |
* @param ipl\Stdlib\Filter $restrictions Filter that represents the restriction |
| 166 |
* |
| 167 |
* @return ?View |
| 168 |
*/ |
| 169 |
public function loadByName($name, $format = self::FORMAT_YAML, $restrictions = null): ?View |
| 170 |
{ |
| 171 |
// If restrictions are set, check if the user has access to view the View |
| 172 |
if (isset($restrictions)) { |
| 173 |
if (!$this->hasAccessToView($restrictions, $name)) { |
| 174 |
return null; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
// Try to load from session |
| 179 |
$view = $this->loadFromSession($name, $format); |
| 180 |
|
| 181 |
if (isset($view)) { |
| 182 |
return $view; |
| 183 |
} |
| 184 |
|
| 185 |
// Try to load the view from the file |
| 186 |
$view = $this->loadFromFile($name, $format); |
| 187 |
|
| 188 |
return $view; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* loadAll loads and returns all available Views. |
| 193 |
* |
| 194 |
* @param string|null $config_dir |
| 195 |
* @param string $format |
| 196 |
* |
| 197 |
* @return View[] |
| 198 |
*/ |
| 199 |
public function loadAll($format = self::FORMAT_YAML): array |
| 200 |
{ |
| 201 |
$suffix = '.' . $format; |
| 202 |
$restrictions = $this->getRestrictions('toplevelview/filter/views'); |
| 203 |
$views = array(); |
| 204 |
|
| 205 |
// Load the YAML files for the Views from the config directory |
| 206 |
$directory = new DirectoryIterator($this->config_dir, $suffix); |
| 207 |
|
| 208 |
foreach ($directory as $name => $path) { |
| 209 |
if (is_dir($path)) { |
| 210 |
// Do not descend and ignore directories |
| 211 |
continue; |
| 212 |
} |
| 213 |
$name = basename($name, $suffix); |
| 214 |
$view = $this->loadByName($name, $format, $restrictions); |
| 215 |
|
| 216 |
if (isset($view)) { |
| 217 |
$views[$name] = $view; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
// Try to load View from the session |
| 222 |
$len = strlen(self::SESSION_PREFIX); |
| 223 |
|
| 224 |
foreach (Session::getSession()->getAll() as $k => $v) { |
| 225 |
if (substr($k, 0, $len) === self::SESSION_PREFIX) { |
| 226 |
$name = substr($k, $len); |
| 227 |
if (! array_key_exists($name, $views)) { |
| 228 |
$view = $this->loadByName($name, $format, $restrictions); |
| 229 |
|
| 230 |
if (isset($view)) { |
| 231 |
$views[$name] = $view; |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
// Sort and return the views |
| 237 |
ksort($views); |
| 238 |
|
| 239 |
return $views; |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* storeToSession stores a View's text to the user's session |
| 244 |
* |
| 245 |
* @throws NotWritableError if the data cannot be stored |
| 246 |
* @throws SecurityException if the user has no access to edit |
| 247 |
* |
| 248 |
* @param $view |
| 249 |
*/ |
| 250 |
public function storeToSession($view): void |
| 251 |
{ |
| 252 |
// Assert the name is valid, to avoid tricky filenames such as '../../view' |
| 253 |
if (!$view->validateName()) { |
| 254 |
throw new NotWritableError('Invalid filename: %s', $view->getName()); |
| 255 |
} |
| 256 |
|
| 257 |
// Assert the user has rights to edit this view |
| 258 |
$restrictions = $this->getRestrictions('toplevelview/filter/edit'); |
| 259 |
$this->assertAccessToView($restrictions, $view->getName()); |
| 260 |
|
| 261 |
Session::getSession()->set(self::SESSION_PREFIX . $view->getName(), $view->getText()); |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* clearSession removes a view from the user's session |
| 266 |
* |
| 267 |
* @param $view |
| 268 |
*/ |
| 269 |
public function clearSession($view): void |
| 270 |
{ |
| 271 |
Session::getSession()->delete(self::SESSION_PREFIX . $view->getName()); |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* storeToFile stores a View to its configuration file |
| 276 |
* |
| 277 |
* @throws NotWritableError if the data cannot be stored |
| 278 |
* @throws SecurityException if the user has no access to edit |
| 279 |
* |
| 280 |
* @param $view |
| 281 |
*/ |
| 282 |
public function storeToFile($view): void |
| 283 |
{ |
| 284 |
// Assert the name is valid, to avoid tricky filenames such as '../../view' |
| 285 |
if (!$view->validateName()) { |
| 286 |
throw new NotWritableError('Invalid filename: %s', $view->getName()); |
| 287 |
} |
| 288 |
|
| 289 |
// Assert the user has rights to edit this file |
| 290 |
$restrictions = $this->getRestrictions('toplevelview/filter/edit'); |
| 291 |
$this->assertAccessToView($restrictions, $view->getName()); |
| 292 |
|
| 293 |
$file_path = $this->getConfigDir() . DIRECTORY_SEPARATOR . $view->getName() . '.' . $view->getFormat(); |
| 294 |
// Store a backup of the existing config |
| 295 |
if (file_exists($file_path)) { |
| 296 |
$this->storeBackup($view); |
| 297 |
} |
| 298 |
// Write the content to the file and clear the session |
| 299 |
$this->writeFile($file_path, $view->getText()); |
| 300 |
$this->clearSession($view); |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* delete removes a Views configuration file |
| 305 |
* |
| 306 |
* @param $view |
| 307 |
*/ |
| 308 |
public function delete($view): void |
| 309 |
{ |
| 310 |
// Assert the user has rights to edit this view |
| 311 |
$restrictions = $this->getRestrictions('toplevelview/filter/edit'); |
| 312 |
$this->assertAccessToView($restrictions, $view->getName()); |
| 313 |
|
| 314 |
$file_path = $this->getConfigDir() . DIRECTORY_SEPARATOR . $view->getName() . '.' . $view->getFormat(); |
| 315 |
|
| 316 |
$this->clearSession($view); |
| 317 |
|
| 318 |
if (file_exists($file_path)) { |
| 319 |
$this->storeBackup($view, true); |
| 320 |
unlink($file_path); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* storeBackup stores a timestamped backup file of a View's file, |
| 326 |
* if the content has changed |
| 327 |
* |
| 328 |
* @param $view The view to store |
| 329 |
* @param $force Stores a backup even if the content hasn't changed |
| 330 |
* @throws NotReadableError if the file cannot be read |
| 331 |
*/ |
| 332 |
protected function storeBackup($view, $force = false): void |
| 333 |
{ |
| 334 |
$backup_dir = $this->getConfigDir() . DIRECTORY_SEPARATOR . $view->getName(); |
| 335 |
$this->ensureDirExists($backup_dir); |
| 336 |
|
| 337 |
$ts = (string) time(); |
| 338 |
$backup = $backup_dir . DIRECTORY_SEPARATOR . $ts . '.' . $view->getFormat(); |
| 339 |
|
| 340 |
$existing_file = $this->getConfigDir() . DIRECTORY_SEPARATOR . $view->getName() . '.' . $view->getFormat(); |
| 341 |
$oldText = file_get_contents($existing_file); |
| 342 |
|
| 343 |
if ($oldText === false) { |
| 344 |
throw new NotReadableError('Could not read file %s', $existing_file); |
| 345 |
} |
| 346 |
|
| 347 |
// Only store a backup if the text changed or forced is set to true |
| 348 |
if ($force || $oldText !== $view->getText()) { |
| 349 |
$this->writeFile($backup, $oldText); |
| 350 |
} |
| 351 |
} |
| 352 |
} |