PluginProbe
JetBackup – Backup, Restore & Migrate / 3.1.21.3
JetBackup – Backup, Restore & Migrate v3.1.21.3
3.1.23.6 3.1.23.5 3.1.23.3 3.1.22.4 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 All 82 releases
backup / src / JetBackup / Web / File / FileDownload.php
FileDownload.php
116 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }