.htaccess
1 year ago
FileChunk.php
1 year ago
FileChunkIterator.php
1 year ago
FileDownload.php
1 year ago
FileException.php
1 year ago
FileStream.php
1 year ago
index.html
1 year ago
web.config
1 year ago
FileDownload.php
116 lines
| 1 | <?php |
| 2 | /* |
| 3 | * |
| 4 | * JetBackup @ package |
| 5 | * Created By Idan Ben-Ezra |
| 6 | * |
| 7 | * Copyrights @ JetApps |
| 8 | * https://www.jetapps.com |
| 9 | * |
| 10 | **/ |
| 11 | namespace JetBackup\Web\File; |
| 12 | |
| 13 | use JetBackup\Exception\IOException; |
| 14 | |
| 15 | defined( '__JETBACKUP__' ) or die( 'Restricted access' ); |
| 16 | |
| 17 | class FileDownload { |
| 18 | |
| 19 | const DELIMITER = "\r\n\r\n"; |
| 20 | |
| 21 | private string $_destination; |
| 22 | private string $_headers = ''; |
| 23 | private string $_buffer = ''; |
| 24 | private $_fd; |
| 25 | |
| 26 | /** |
| 27 | * @param string $destination |
| 28 | */ |
| 29 | public function __construct(string $destination) { |
| 30 | $this->_destination = $destination; |
| 31 | $this->_fd = fopen($this->_destination, 'a'); |
| 32 | if (!$this->_fd) throw new IOException("Failed to open file: {$this->_destination}"); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @return void |
| 37 | */ |
| 38 | public function deleteFile():void { |
| 39 | $this->close(); |
| 40 | if(file_exists($this->_destination)) unlink($this->_destination); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @return string |
| 45 | */ |
| 46 | public function getHeaders():string { return $this->_headers; } |
| 47 | |
| 48 | /** |
| 49 | * @param string $str |
| 50 | * |
| 51 | * @return void |
| 52 | */ |
| 53 | public function read(string $str):void { |
| 54 | $this->writeHeaders($str); |
| 55 | $this->writeFile($str); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @param string $str |
| 60 | * |
| 61 | * @return void |
| 62 | */ |
| 63 | public function writeHeaders(string &$str):void { |
| 64 | |
| 65 | if($this->_headers) return; |
| 66 | $this->_buffer .= $str; |
| 67 | |
| 68 | // must receive more than 4 bytes - wait for more data |
| 69 | if(strlen($this->_buffer) < 5) { |
| 70 | $str = ''; |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | $d = substr($this->_buffer, 0, 4); |
| 75 | $o = 4; |
| 76 | |
| 77 | while(self::DELIMITER != $d) { |
| 78 | $d = substr($this->_buffer, $o++, 4); |
| 79 | if($d === false || (strlen($this->_buffer) < $o+3)) { |
| 80 | $str = ''; |
| 81 | return; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | $this->_headers = substr($this->_buffer, 0, $o-1); |
| 86 | $str = substr($this->_buffer, $o+3); |
| 87 | $this->_buffer = ''; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @param string $str |
| 92 | * |
| 93 | * @return void |
| 94 | */ |
| 95 | public function writeFile(string $str):void { |
| 96 | if(!$this->_fd || $str === '') return; |
| 97 | fwrite($this->_fd, $str); |
| 98 | fflush($this->_fd); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @return void |
| 103 | */ |
| 104 | public function close():void { |
| 105 | if(!$this->_fd) return; |
| 106 | @fclose($this->_fd); |
| 107 | $this->_fd = null; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * |
| 112 | */ |
| 113 | public function __destruct() { |
| 114 | $this->close(); |
| 115 | } |
| 116 | } |