PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / 3.1.9.2
JetBackup – Backup, Restore & Migrate v3.1.9.2
3.1.22.3 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.8.1 1.4.9 1.5.0 1.5.1 1.5.1.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.10 1.6.11 1.6.12 1.6.13 1.6.15 1.6.5.1 1.6.8.8 1.6.9 1.6.9.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7.5 2.0.8.7 2.0.9.11 2.0.9.14 2.0.9.15 2.0.9.6 2.0.9.7 2.0.9.9 3.1.10.7 3.1.11.1 3.1.12.3 3.1.13.4 3.1.14.17 3.1.15.4 3.1.16.1 3.1.17.5 3.1.18.10 3.1.18.8 3.1.18.9 3.1.19.8 3.1.20.3 3.1.21.3 3.1.7.9 3.1.9.2 trunk 1.1.90 1.1.91 1.2.0 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2
backup / src / JetBackup / Web / File / FileDownload.php
backup / src / JetBackup / Web / File Last commit date
.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 }