| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPIDE\App\Services\Tmpfs\Adapters; |
| 4 |
|
| 5 |
use WPIDE\App\Services\Service; |
| 6 |
use WPIDE\App\Services\Tmpfs\TmpfsInterface; |
| 7 |
|
| 8 |
class Tmpfs implements Service, TmpfsInterface |
| 9 |
{ |
| 10 |
protected $path; |
| 11 |
|
| 12 |
public function init(array $config = []) |
| 13 |
{ |
| 14 |
$this->path = $config['path']; |
| 15 |
|
| 16 |
if (! is_dir($this->path)) { |
| 17 |
mkdir($this->path); |
| 18 |
} |
| 19 |
|
| 20 |
if (mt_rand(0, 99) < $config['gc_probability_perc']) { |
| 21 |
$this->clean($config['gc_older_than']); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
public function write(string $filename, $data, $append = false) |
| 26 |
{ |
| 27 |
$filename = $this->sanitizeFilename($filename); |
| 28 |
|
| 29 |
$flags = 0; |
| 30 |
|
| 31 |
if ($append) { |
| 32 |
$flags = FILE_APPEND; |
| 33 |
} |
| 34 |
|
| 35 |
return file_put_contents($this->getPath().$filename, $data, $flags) !== false; |
| 36 |
} |
| 37 |
|
| 38 |
public function getFileLocation(string $filename): string |
| 39 |
{ |
| 40 |
$filename = $this->sanitizeFilename($filename); |
| 41 |
|
| 42 |
return $this->getPath().$filename; |
| 43 |
} |
| 44 |
|
| 45 |
public function read(string $filename): string |
| 46 |
{ |
| 47 |
$filename = $this->sanitizeFilename($filename); |
| 48 |
|
| 49 |
return (string) file_get_contents($this->getPath().$filename); |
| 50 |
} |
| 51 |
|
| 52 |
public function readStream(string $filename): array |
| 53 |
{ |
| 54 |
$filename = $this->sanitizeFilename($filename); |
| 55 |
|
| 56 |
$stream = fopen($this->getPath().$filename, 'r'); |
| 57 |
$filesize = filesize($this->getPath().$filename); |
| 58 |
|
| 59 |
return [ |
| 60 |
'filename' => $filename, |
| 61 |
'stream' => $stream, |
| 62 |
'filesize' => $filesize, |
| 63 |
]; |
| 64 |
} |
| 65 |
|
| 66 |
public function exists(string $filename): bool |
| 67 |
{ |
| 68 |
$filename = $this->sanitizeFilename($filename); |
| 69 |
|
| 70 |
return file_exists($this->getPath().$filename); |
| 71 |
} |
| 72 |
|
| 73 |
public function findAll($pattern): array |
| 74 |
{ |
| 75 |
$files = []; |
| 76 |
$matches = glob($this->getPath().$pattern); |
| 77 |
if (! empty($matches)) { |
| 78 |
foreach ($matches as $filename) { |
| 79 |
if (is_file($filename)) { |
| 80 |
$files[] = [ |
| 81 |
'name' => basename($filename), |
| 82 |
'size' => filesize($filename), |
| 83 |
'time' => filemtime($filename), |
| 84 |
]; |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return $files; |
| 90 |
} |
| 91 |
|
| 92 |
public function remove(string $filename): bool |
| 93 |
{ |
| 94 |
$filename = $this->sanitizeFilename($filename); |
| 95 |
|
| 96 |
return unlink($this->getPath().$filename); |
| 97 |
} |
| 98 |
|
| 99 |
public function clean(int $older_than) |
| 100 |
{ |
| 101 |
$files = $this->findAll('*'); |
| 102 |
foreach ($files as $file) { |
| 103 |
if (time() - $file['time'] >= $older_than) { |
| 104 |
$this->remove($file['name']); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
private function getPath(): string |
| 110 |
{ |
| 111 |
return $this->path; |
| 112 |
} |
| 113 |
|
| 114 |
private function sanitizeFilename($filename): string |
| 115 |
{ |
| 116 |
$filename = (string) preg_replace( |
| 117 |
'~ |
| 118 |
[<>:"/\\|?*]| # file system reserved https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words |
| 119 |
[\x00-\x1F]| # control characters http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx |
| 120 |
[\x7F\xA0\xAD]| # non-printing characters DEL, NO-BREAK SPACE, SOFT HYPHEN |
| 121 |
[;\\\{}^\~`] # other non-safe |
| 122 |
~xu', |
| 123 |
'-', |
| 124 |
(string) $filename |
| 125 |
); |
| 126 |
|
| 127 |
// maximize filename length to 255 bytes http://serverfault.com/a/9548/44086 |
| 128 |
return mb_substr($filename, 0, 255); |
| 129 |
} |
| 130 |
} |
| 131 |
|