Módulo VPSManager para WHMCS con configuración externa, acceso seguro al panel y métricas Graphite para LX.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

37 rindas
1.6 KiB

  1. <?php
  2. namespace VPSManager;
  3. final class BackupCache
  4. {
  5. private $directory;
  6. public function __construct(string $directory)
  7. {
  8. $real = realpath($directory);
  9. if (!$real || !is_dir($real) || is_link($directory) || (fileperms($real) & 0077) !== 0
  10. || !is_writable($real)) throw new \RuntimeException('CACHE_CONFIG');
  11. foreach ([dirname(__DIR__), $_SERVER['DOCUMENT_ROOT'] ?? ''] as $public) {
  12. $root = $public === '' ? false : realpath($public);
  13. if ($root && ($real === $root || strpos($real . '/', $root . '/') === 0)) throw new \RuntimeException('CACHE_PUBLIC');
  14. }
  15. // Administrator must choose a directory outside every web alias as well.
  16. $this->directory = $real;
  17. }
  18. public function read(string $key): ?array
  19. {
  20. $file = $this->directory . '/' . $key . '.json';
  21. if (!is_file($file) || is_link($file) || filesize($file) > 2097152) return null;
  22. $value = json_decode(file_get_contents($file), true, 32);
  23. return is_array($value) ? $value : null;
  24. }
  25. public function write(string $key, array $data): void
  26. {
  27. $file = tempnam($this->directory, '.new-');
  28. if ($file === false) throw new \RuntimeException('CACHE_WRITE');
  29. try {
  30. chmod($file, 0600);
  31. if (file_put_contents($file, json_encode($data, JSON_THROW_ON_ERROR), LOCK_EX) === false
  32. || !rename($file, $this->directory . '/' . $key . '.json')) throw new \RuntimeException('CACHE_WRITE');
  33. } finally { if (is_file($file)) unlink($file); }
  34. }
  35. }