| 36 | | abstract class AbstractFile extends AbstractEntry |
| 37 | | { |
| 38 | | public const string MODE_READ = 'rb'; |
| 39 | | |
| 40 | | public const string MODE_APPEND = 'ab'; |
| 41 | | |
| 42 | | public const string MODE_WRITE = 'wb'; |
| 43 | | |
| 44 | | |
| 45 | | |
| 46 | | |
| 47 | | |
| 48 | | |
| 49 | | |
| 50 | | |
| 51 | | public function __construct( |
| 52 | | string $filepath, |
| 53 | | protected string $mode = self::MODE_READ, |
| 54 | | ) { |
| 55 | | $this->path = $filepath; |
| 56 | | |
| 57 | | $this->handle = fopen($this->path, $this->mode); |
| 58 | | |
| 59 | | if (!is_resource($this->handle)) { |
| 60 | | throw new FileException('Could not open file: ' . $this->path); |
| 61 | | } |
| 62 | | } |
| 63 | | |
| 64 | | |
| 65 | | |
| 66 | | |
| 67 | | public function __destruct() |
| 68 | | { |
| 69 | | if (is_resource($this->handle)) { |
| 70 | | fclose($this->handle); |
| 71 | | } |
| 72 | | } |
| 73 | | |
| 74 | | |
| 75 | | |
| 76 | | |
| 77 | | public function eof(): bool |
| 78 | | { |
| 79 | | return feof($this->handle); |
| 80 | | } |
| 81 | | |
| 82 | | |
| 83 | | |
| 84 | | |
| 85 | | abstract public function getChunk(): string; |
| 86 | | |
| 87 | | |
| 88 | | |
| 89 | | |
| 90 | | |
| 91 | | |
| 92 | | |
| 93 | | abstract public function putChunk(string $chunk): int|bool; |
| 94 | | |
| 95 | | |
| 96 | | |
| 97 | | |
| 98 | | |
| 99 | | |
| 100 | | public function delete(): bool |
| 101 | | { |
| 102 | | if ($this->handle) { |
| 103 | | fclose($this->handle); |
| 104 | | } |
| 105 | | |
| 106 | | |
| 107 | | |
| 108 | | |
| 109 | | |
| 110 | | |
| 111 | | $uploadedTmpName = $_FILES['userfile']['tmp_name'] ?? null; |
| 112 | | if (is_string($uploadedTmpName) && $this->path !== $uploadedTmpName && file_exists($this->path)) { |
| 113 | | return $this->deleteDir(dirname($this->path)); |
| 114 | | } |
| 115 | | |
| 116 | | return true; |
| 117 | | } |
| 118 | | |
| 119 | | |
| 120 | | |
| 121 | | |
| 122 | | public function getMode(): string |
| 123 | | { |
| 124 | | return $this->mode; |
| 125 | | } |
| 126 | | |
| 127 | | |
| 128 | | |
| 129 | | |
| 130 | | |
| 131 | | |
| 132 | | public function setMode($mode): bool |
| 133 | | { |
| 134 | | $retval = false; |
| 135 | | |
| 136 | | if (in_array($mode, [self::MODE_WRITE, self::MODE_READ, self::MODE_APPEND], strict: true)) { |
| 137 | | fclose($this->handle); |
| 138 | | $this->handle = fopen($this->path, $mode); |
| 139 | | |
| 140 | | $retval = is_resource($this->handle); |
| 141 | | } |
| 142 | | |
| 143 | | return $retval; |
| 144 | | } |
| 145 | | |
| 146 | | |
| 147 | | |
| 148 | | |
| 149 | | |
| 150 | | |
| 151 | | public function copyToSimple($target): bool |
| 152 | | { |
| 153 | | if (is_uploaded_file($this->path)) { |
| 154 | | return move_uploaded_file($this->path, $target); |
| 155 | | } |
| 156 | | |
| 157 | | return copy($this->path, $target); |
| 158 | | } |
| 159 | | |
| 160 | | |
| 161 | | |
| 162 | | |
| 163 | | public function isOk(): bool |
| 164 | | { |
| 165 | | return is_resource($this->handle); |
| 166 | | } |
| 167 | | |
| 168 | | |
| 169 | | |
| 170 | | |
| 171 | | |
| 172 | | |
| 173 | | |
| 174 | | |
| 175 | | public function deleteDir($path): bool |
| 176 | | { |
| 177 | | if (!file_exists($path)) { |
| 178 | | throw new FileException(sprintf("Directory %s doesn't exist.", $path)); |
| 179 | | } |
| 180 | | |
| 181 | | $it = new RecursiveIteratorIterator( |
| 182 | | new RecursiveDirectoryIterator($path), |
| 183 | | RecursiveIteratorIterator::CHILD_FIRST, |
| 184 | | ); |
| 185 | | |
| 186 | | foreach ($it as $file) { |
| 187 | | if (!$file instanceof \SplFileInfo || in_array($file->getBasename(), ['.', '..'], strict: true)) { |
| 188 | | continue; |
| 189 | | } |
| 190 | | |
| 191 | | if ($file->isDir()) { |
| 192 | | rmdir($file->getPathname()); |
| 193 | | } |
| 194 | | |
| 195 | | if ($file->isFile() || $file->isLink()) { |
| 196 | | if (!is_writable($file->getPathname())) { |
| 197 | | throw new FileException("File can't be deleted."); |
| 198 | | } |
| 199 | | |
| 200 | | unlink($file->getPathname()); |
| 201 | | } |
| 202 | | } |
| 203 | | |
| 204 | | return rmdir($path); |
| 205 | | } |
| 206 | | } |