o2system /
filesystem
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * This file is part of the O2System Framework package. |
||
| 4 | * |
||
| 5 | * For the full copyright and license information, please view the LICENSE |
||
| 6 | * file that was distributed with this source code. |
||
| 7 | * |
||
| 8 | * @author Steeve Andrian Salim |
||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
| 10 | */ |
||
| 11 | |||
| 12 | // ------------------------------------------------------------------------ |
||
| 13 | |||
| 14 | namespace O2System\Filesystem; |
||
| 15 | |||
| 16 | // ------------------------------------------------------------------------ |
||
| 17 | |||
| 18 | use O2System\Spl\Info\SplFileInfo; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Class File |
||
| 22 | * |
||
| 23 | * @package O2System\Filesystem |
||
| 24 | */ |
||
| 25 | class File extends SplFileInfo |
||
| 26 | {
|
||
| 27 | /** |
||
| 28 | * File::$filePath |
||
| 29 | * |
||
| 30 | * Path of File |
||
| 31 | * @var String |
||
| 32 | */ |
||
| 33 | private $filePath; |
||
| 34 | |||
| 35 | // ------------------------------------------------------------------------ |
||
| 36 | |||
| 37 | /** |
||
| 38 | * File::__construct |
||
| 39 | * |
||
| 40 | * @param string|null $filePath |
||
| 41 | */ |
||
| 42 | public function __construct($filePath = null) |
||
| 43 | {
|
||
| 44 | if (isset($filePath)) {
|
||
| 45 | $this->filePath = $filePath; |
||
| 46 | parent::__construct($filePath); |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | // ------------------------------------------------------------------------ |
||
| 51 | |||
| 52 | /** |
||
| 53 | * File::setGroup |
||
| 54 | * |
||
| 55 | * Attempts to change the group of the file filename to group. |
||
| 56 | * |
||
| 57 | * Only the superuser may change the group of a file arbitrarily; other users may change the group of a file to any |
||
| 58 | * group of which that user is a member. |
||
| 59 | * |
||
| 60 | * @param mixed $group A group name or number. |
||
| 61 | * |
||
| 62 | * @return bool Returns TRUE on success or FALSE on failure. |
||
| 63 | */ |
||
| 64 | public function setGroup($group) |
||
| 65 | {
|
||
| 66 | $params[] = $this->getRealPath(); |
||
| 67 | $params[] = $group; |
||
| 68 | |||
| 69 | return call_user_func_array('chgrp', $params);
|
||
| 70 | } |
||
| 71 | |||
| 72 | // ------------------------------------------------------------------------ |
||
| 73 | |||
| 74 | /** |
||
| 75 | * File::setMode |
||
| 76 | * |
||
| 77 | * Attempts to change the mode of the specified file to that given in mode. |
||
| 78 | * |
||
| 79 | * @param int $mode The mode parameter consists of three octal number components specifying access restrictions for |
||
| 80 | * the owner, the user group in which the owner is in, and to everybody else in this order. One |
||
| 81 | * component can be computed by adding up the needed permissions for that target user base. Number |
||
| 82 | * 1 means that you grant execute rights, number 2 means that you make the file writable, number |
||
| 83 | * 4 means that you make the file readable. Add up these numbers to specify needed rights. You can |
||
| 84 | * also read more about modes on Unix systems with 'man 1 chmod' and 'man 2 chmod'. |
||
| 85 | * |
||
| 86 | * @return bool Returns TRUE on success or FALSE on failure. |
||
| 87 | */ |
||
| 88 | public function setMode($mode) |
||
| 89 | {
|
||
| 90 | $params[] = $this->getRealPath(); |
||
| 91 | $params[] = $mode; |
||
| 92 | |||
| 93 | return call_user_func_array('chmod', $params);
|
||
| 94 | } |
||
| 95 | |||
| 96 | // ------------------------------------------------------------------------ |
||
| 97 | |||
| 98 | /** |
||
| 99 | * File::setOwner |
||
| 100 | * |
||
| 101 | * Attempts to change the owner of the file filename to user user. |
||
| 102 | * Only the superuser may change the owner of a file. |
||
| 103 | * |
||
| 104 | * @param mixed $user A user name or number. |
||
| 105 | * |
||
| 106 | * @return bool Returns TRUE on success or FALSE on failure. |
||
| 107 | */ |
||
| 108 | public function setOwner($user) |
||
| 109 | {
|
||
| 110 | $params[] = $this->getRealPath(); |
||
| 111 | $params[] = $user; |
||
| 112 | |||
| 113 | return call_user_func_array('chown', $params);
|
||
| 114 | } |
||
| 115 | |||
| 116 | // ------------------------------------------------------------------------ |
||
| 117 | |||
| 118 | /** |
||
| 119 | * File::setLink |
||
| 120 | * |
||
| 121 | * Creates a symbolic link to the file with the specified name link. |
||
| 122 | * |
||
| 123 | * @param string $link The link name. |
||
| 124 | * |
||
| 125 | * @return bool Returns TRUE on success or FALSE on failure. |
||
| 126 | */ |
||
| 127 | public function setLink($link) |
||
| 128 | {
|
||
| 129 | $params[] = $this->getRealPath(); |
||
| 130 | $params[] = $link; |
||
| 131 | |||
| 132 | return call_user_func_array('symlink', $params);
|
||
| 133 | } |
||
| 134 | |||
| 135 | // ------------------------------------------------------------------------ |
||
| 136 | |||
| 137 | /** |
||
| 138 | * File::getContents |
||
| 139 | * |
||
| 140 | * Reads entire file into a string. |
||
| 141 | * |
||
| 142 | * @param bool $useIncludePath As of PHP 5 the FILE_USE_INCLUDE_PATH constant can be used to trigger include |
||
| 143 | * path search. |
||
| 144 | * |
||
| 145 | * @param resource $context A valid context resource created with stream_context_create(). If you don't need |
||
| 146 | * to use a custom context, you can skip this parameter by NULL. |
||
| 147 | * |
||
| 148 | * @param int $offset The offset where the reading starts on the original stream. Negative offsets |
||
| 149 | * count from the end of the stream. Seeking (offset) is not supported with remote |
||
| 150 | * files. Attempting to seek on non-local files may work with small offsets, but |
||
| 151 | * this is unpredictable because it works on the buffered stream. |
||
| 152 | * @param int $maxlen Maximum length of data read. The default is to read until end of file is |
||
| 153 | * reached. Note that this parameter is applied to the stream processed by the |
||
| 154 | * filters. |
||
| 155 | * |
||
| 156 | * @return string The function returns the read data or FALSE on failure. |
||
| 157 | */ |
||
| 158 | public function getContents($useIncludePath = false, $context = null, $offset = 0, $maxlen = 0) |
||
| 159 | {
|
||
| 160 | $params[] = $this->getRealPath(); |
||
| 161 | $params[] = $useIncludePath; |
||
| 162 | $params[] = $context; |
||
| 163 | $params[] = $offset; |
||
| 164 | |||
| 165 | if ($maxlen > 0) {
|
||
| 166 | $params[] = $maxlen; |
||
| 167 | } |
||
| 168 | |||
| 169 | return call_user_func_array('file_get_contents', $params);
|
||
| 170 | } |
||
| 171 | |||
| 172 | // ------------------------------------------------------------------------ |
||
| 173 | |||
| 174 | /** |
||
| 175 | * File::touch |
||
| 176 | * |
||
| 177 | * @param int $time The touch time. If time is not supplied, the current system time is used. |
||
| 178 | * @param int $atime If present, the access time of the given filename is set to the value of atime. Otherwise, it |
||
| 179 | * is set to the value passed to the time parameter. If neither are present, the current system |
||
| 180 | * time is used. |
||
| 181 | * |
||
| 182 | * @return bool Returns TRUE on success or FALSE on failure. |
||
| 183 | */ |
||
| 184 | public function touch($time = null, $atime = null) |
||
| 185 | {
|
||
| 186 | $params[] = $this->getRealPath(); |
||
| 187 | $params[] = (isset($time) ? $time : time()); |
||
| 188 | $params[] = (isset($atime) ? $atime : time()); |
||
| 189 | |||
| 190 | return call_user_func_array('touch', $params);
|
||
| 191 | } |
||
| 192 | |||
| 193 | // ------------------------------------------------------------------------ |
||
| 194 | |||
| 195 | /** |
||
| 196 | * File::show |
||
| 197 | * |
||
| 198 | * Show file with header. |
||
| 199 | * |
||
| 200 | * @return void |
||
| 201 | */ |
||
| 202 | public function show() |
||
| 203 | {
|
||
| 204 | if ($mime = $this->getMime()) {
|
||
| 205 | $mime = is_array($mime) ? $mime[ 0 ] : $mime; |
||
| 206 | } elseif (is_file($this->getRealPath())) {
|
||
| 207 | $mime = 'application/octet-stream'; |
||
| 208 | } |
||
| 209 | |||
| 210 | $fileSize = filesize($this->getRealPath()); |
||
| 211 | $filename = pathinfo($this->getRealPath(), PATHINFO_BASENAME); |
||
| 212 | |||
| 213 | // Common headers |
||
| 214 | $expires = 604800; // (60*60*24*7) |
||
| 215 | header('Expires:' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT');
|
||
| 216 | |||
| 217 | header('Accept-Ranges: bytes', true);
|
||
| 218 | header("Cache-control: private", true);
|
||
| 219 | header('Pragma: private', true);
|
||
| 220 | |||
| 221 | header('Content-Type: ' . $mime);
|
||
| 222 | header('Content-Disposition: inline; filename="' . $filename . '"');
|
||
| 223 | header('Content-Transfer-Encoding: binary');
|
||
| 224 | header('Accept-Ranges: bytes');
|
||
| 225 | |||
| 226 | $ETag = '"' . md5($filename) . '"'; |
||
| 227 | |||
| 228 | if ( ! empty($_SERVER[ 'HTTP_IF_NONE_MATCH' ]) |
||
| 229 | && $_SERVER[ 'HTTP_IF_NONE_MATCH' ] == $ETag |
||
| 230 | ) {
|
||
| 231 | header('HTTP/1.1 304 Not Modified');
|
||
| 232 | header('Content-Length: ' . $fileSize);
|
||
| 233 | exit; |
||
| 234 | } |
||
| 235 | |||
| 236 | $expires = 604800; // (60*60*24*7) |
||
| 237 | header('ETag: ' . $ETag);
|
||
| 238 | header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
|
||
| 239 | header('Expires:' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT');
|
||
| 240 | |||
| 241 | // Open file |
||
| 242 | // @readfile($file_path); |
||
| 243 | $file = @fopen($filename, "rb"); |
||
| 244 | if ($file) {
|
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 245 | while ( ! feof($file)) {
|
||
| 246 | print(fread($file, 1024 * 8)); |
||
| 247 | flush(); |
||
| 248 | if (connection_status() != 0) {
|
||
| 249 | @fclose($file); |
||
| 250 | die(); |
||
| 251 | } |
||
| 252 | } |
||
| 253 | @fclose($file); |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | // ------------------------------------------------------------------------ |
||
| 258 | |||
| 259 | /** |
||
| 260 | * File::getMime |
||
| 261 | * |
||
| 262 | * Get mime info based on MIME config. |
||
| 263 | * |
||
| 264 | * @return bool|string|array |
||
| 265 | */ |
||
| 266 | public function getMime() |
||
| 267 | {
|
||
| 268 | $mimes = $this->getMimes(); |
||
| 269 | $ext = strtolower($this->getExtension()); |
||
| 270 | |||
| 271 | if (isset($mimes[ $ext ])) {
|
||
| 272 | return $mimes[ $ext ]; |
||
| 273 | } |
||
| 274 | |||
| 275 | return false; |
||
| 276 | } |
||
| 277 | |||
| 278 | // ------------------------------------------------------------------------ |
||
| 279 | |||
| 280 | /** |
||
| 281 | * File::getMimes |
||
| 282 | * |
||
| 283 | * Get config mimes. |
||
| 284 | * |
||
| 285 | * @static |
||
| 286 | * @return array Returns the MIME types array from config/mimes.php |
||
| 287 | */ |
||
| 288 | public static function getMimes() |
||
| 289 | {
|
||
| 290 | static $mimes; |
||
| 291 | |||
| 292 | if (empty($mimes)) {
|
||
| 293 | if (file_exists(__DIR__ . '/Config/Mimes.php')) {
|
||
| 294 | $mimes = require(__DIR__ . '/Config/Mimes.php'); |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | return $mimes; |
||
| 299 | } |
||
| 300 | |||
| 301 | // ------------------------------------------------------------------------ |
||
| 302 | |||
| 303 | /** |
||
| 304 | * File::read |
||
| 305 | * |
||
| 306 | * Outputs a file. |
||
| 307 | * |
||
| 308 | * @param bool $useIncludePath You can use the optional second parameter and set it to TRUE, if you want to |
||
| 309 | * search for the file in the include_path, too. |
||
| 310 | * @param resource $context A context stream resource. |
||
| 311 | * |
||
| 312 | * @return int Returns the number of bytes read from the file. If an error occurs, FALSE is returned and unless |
||
| 313 | * the function was called as @readfile(), an error message is printed. |
||
| 314 | */ |
||
| 315 | public function read($useIncludePath = false, $context = null) |
||
| 316 | {
|
||
| 317 | $params[] = $this->getRealPath(); |
||
| 318 | $params[] = $useIncludePath; |
||
| 319 | $params[] = $context; |
||
| 320 | |||
| 321 | return call_user_func_array('readfile', $params);
|
||
| 322 | } |
||
| 323 | |||
| 324 | // ------------------------------------------------------------------------ |
||
| 325 | |||
| 326 | /** |
||
| 327 | * File::write |
||
| 328 | * |
||
| 329 | * Writes a file. |
||
| 330 | * |
||
| 331 | * @param string $contents File contents to write. |
||
| 332 | * @param string $mode File handle mode. |
||
| 333 | * |
||
| 334 | * @return bool |
||
| 335 | */ |
||
| 336 | public function write($filePath, $contents, $mode = 'wb') |
||
| 337 | {
|
||
| 338 | if (false !== ($fp = $this->create($filePath, $mode))) {
|
||
|
0 ignored issues
–
show
|
|||
| 339 | flock($fp, LOCK_EX); |
||
| 340 | |||
| 341 | for ($result = $written = 0, $length = strlen($contents); $written < $length; $written += $result) {
|
||
| 342 | if (($result = fwrite($fp, substr($contents, $written))) === false) {
|
||
| 343 | break; |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | flock($fp, LOCK_UN); |
||
| 348 | fclose($fp); |
||
| 349 | |||
| 350 | return is_int($result); |
||
| 351 | } |
||
| 352 | |||
| 353 | return false; |
||
| 354 | } |
||
| 355 | |||
| 356 | // ------------------------------------------------------------------------ |
||
| 357 | |||
| 358 | /** |
||
| 359 | * File::create |
||
| 360 | * |
||
| 361 | * Create a File |
||
| 362 | * |
||
| 363 | * @param string|null $filePath |
||
| 364 | * @param string $mode |
||
| 365 | * |
||
| 366 | * @return resource |
||
| 367 | */ |
||
| 368 | public function create($filePath = null, $mode = 'wb') |
||
| 369 | {
|
||
| 370 | $filePath = isset($filePath) ? $filePath : $this->filePath; |
||
| 371 | $dir = dirname($filePath); |
||
| 372 | |||
| 373 | if ( ! is_writable($dir)) {
|
||
| 374 | if ( ! file_exists($dir)) {
|
||
| 375 | mkdir($dir, 0777, true); |
||
| 376 | } |
||
| 377 | } |
||
| 378 | |||
| 379 | if ( ! $fp = @fopen($filePath, $mode)) {
|
||
| 380 | return false; |
||
| 381 | } |
||
| 382 | |||
| 383 | parent::__construct($filePath); |
||
| 384 | |||
| 385 | return $fp; |
||
| 386 | } |
||
| 387 | |||
| 388 | // ------------------------------------------------------------------------ |
||
| 389 | |||
| 390 | /** |
||
| 391 | * File::rename |
||
| 392 | * |
||
| 393 | * Renames a file. |
||
| 394 | * |
||
| 395 | * @param string $newFilename The new filename. |
||
| 396 | * @param null $context Context support was added with PHP 5.0.0. For a description of contexts, refer to |
||
| 397 | * Streams. |
||
| 398 | * |
||
| 399 | * @return bool Returns TRUE on success or FALSE on failure. |
||
| 400 | */ |
||
| 401 | public function rename($newFilename, $context = null) |
||
| 402 | {
|
||
| 403 | $params[] = $this->getRealPath(); |
||
| 404 | $params[] = dirname($this->getRealPath()) . DIRECTORY_SEPARATOR . $newFilename; |
||
| 405 | $params[] = $context; |
||
| 406 | |||
| 407 | return call_user_func_array('rename', $params);
|
||
| 408 | } |
||
| 409 | |||
| 410 | // ------------------------------------------------------------------------ |
||
| 411 | |||
| 412 | /** |
||
| 413 | * File::copy |
||
| 414 | * |
||
| 415 | * Makes a copy of the file source to destination. |
||
| 416 | * |
||
| 417 | * @param string $destination The destination path. If destination is a URL, the copy operation may fail if the |
||
| 418 | * wrapper does not support overwriting of existing files. |
||
| 419 | * @param resource $context A valid context resource created with stream_context_create(). |
||
| 420 | * |
||
| 421 | * @return bool Returns TRUE on success or FALSE on failure. |
||
| 422 | */ |
||
| 423 | public function copy($destination, $context = null) |
||
| 424 | {
|
||
| 425 | $params[] = $this->getRealPath(); |
||
| 426 | $params[] = $destination; |
||
| 427 | $params[] = $context; |
||
| 428 | |||
| 429 | return call_user_func_array('copy', $params);
|
||
| 430 | } |
||
| 431 | |||
| 432 | // ------------------------------------------------------------------------ |
||
| 433 | |||
| 434 | /** |
||
| 435 | * File::delete |
||
| 436 | * |
||
| 437 | * Deletes a file. |
||
| 438 | * |
||
| 439 | * @param resource $context Context support was added with PHP 5.0.0. For a description of contexts, refer to |
||
| 440 | * Streams. |
||
| 441 | * |
||
| 442 | * @return bool Returns TRUE on success or FALSE on failure. |
||
| 443 | */ |
||
| 444 | public function delete($context = null) |
||
| 445 | {
|
||
| 446 | $params[] = $this->getRealPath(); |
||
| 447 | $params[] = $context; |
||
| 448 | |||
| 449 | return call_user_func_array('unlink', $params);
|
||
| 450 | } |
||
| 451 | } |