| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace Liuch\DmarcSrg\ReportFile; |
| 24 |
|
| 25 |
use Liuch\DmarcSrg\Exception\SoftException; |
| 26 |
|
| 27 |
class ReportFile |
| 28 |
{ |
| 29 |
private $fd = null; |
| 30 |
private $zip = null; |
| 31 |
private $type = null; |
| 32 |
private $remove = false; |
| 33 |
private $filename = null; |
| 34 |
private $filepath = null; |
| 35 |
private $gzcutfilter = null; |
| 36 |
private $gzinflatefilter = null; |
| 37 |
private static $filters = []; |
| 38 |
private static $ext_mime_map = [ |
| 39 |
'xml' => 'text/xml', |
| 40 |
'gz' => 'application/gzip', |
| 41 |
'zip' => 'application/zip' |
| 42 |
]; |
| 43 |
|
| 44 |
private function __construct($filename, $type = null, $fd = null, $remove = false, $filepath = null) |
| 45 |
{ |
| 46 |
$this->filename = $filename; |
| 47 |
$this->type = $type ?? self::getMimeType($filename, $fd, $filepath); |
| 48 |
$this->remove = $remove; |
| 49 |
$this->filepath = $filepath; |
| 50 |
|
| 51 |
switch ($this->type) { |
| 52 |
case 'text/xml': |
| 53 |
case 'application/gzip': |
| 54 |
case 'application/x-gzip': |
| 55 |
$this->fd = $fd; |
| 56 |
break; |
| 57 |
case 'application/zip': |
| 58 |
if ($fd) { |
| 59 |
$tmpfname = tempnam(sys_get_temp_dir(), 'dmarc_'); |
| 60 |
if ($tmpfname === false) { |
| 61 |
throw new SoftException('Failed to create a temporary file'); |
| 62 |
} |
| 63 |
if (stream_get_meta_data($fd)['seekable'] ?? false) { |
| 64 |
rewind($fd); |
| 65 |
} |
| 66 |
if (file_put_contents($tmpfname, $fd) === false) { |
| 67 |
throw new SoftException('Failed to copy data to a temporary file'); |
| 68 |
} |
| 69 |
$this->filepath = $tmpfname; |
| 70 |
$this->remove = true; |
| 71 |
} |
| 72 |
break; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
public function __destruct() |
| 77 |
{ |
| 78 |
if ($this->fd) { |
| 79 |
if ($this->isGzipStream()) { |
| 80 |
$this->enableGzFilter(false); |
| 81 |
} |
| 82 |
gzclose($this->fd); |
| 83 |
} |
| 84 |
if ($this->zip) { |
| 85 |
$this->zip->close(); |
| 86 |
} |
| 87 |
if ($this->remove && $this->filepath) { |
| 88 |
unlink($this->filepath); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
public static function getMimeType($filename, $fd = null, $filepath = null): string |
| 93 |
{ |
| 94 |
if (function_exists('mime_content_type')) { |
| 95 |
if ($fd && (stream_get_meta_data($fd)['seekable'] ?? false)) { |
| 96 |
if ($res = mime_content_type($fd)) { |
| 97 |
return $res; |
| 98 |
} |
| 99 |
} |
| 100 |
if ($filepath && ($res = mime_content_type($filepath))) { |
| 101 |
return $res; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
$ext = pathinfo(basename($filename), PATHINFO_EXTENSION); |
| 106 |
return self::$ext_mime_map[$ext] ?? 'application/octet-stream'; |
| 107 |
} |
| 108 |
|
| 109 |
public static function fromFile($filepath, $filename = null, $remove = false) |
| 110 |
{ |
| 111 |
if (!is_file($filepath)) { |
| 112 |
throw new SoftException('ReportFile: it is not a file'); |
| 113 |
} |
| 114 |
|
| 115 |
return new ReportFile( |
| 116 |
$filename ? basename($filename) : basename($filepath), |
| 117 |
null, |
| 118 |
null, |
| 119 |
$remove, |
| 120 |
$filepath |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
public static function fromStream($fd, $filename, $type) |
| 125 |
{ |
| 126 |
return new ReportFile($filename, $type, $fd); |
| 127 |
} |
| 128 |
|
| 129 |
public function filename() |
| 130 |
{ |
| 131 |
return $this->filename; |
| 132 |
} |
| 133 |
|
| 134 |
public function datastream() |
| 135 |
{ |
| 136 |
if (!$this->fd) { |
| 137 |
$fd = null; |
| 138 |
switch ($this->type) { |
| 139 |
case 'application/zip': |
| 140 |
$this->zip = new \ZipArchive(); |
| 141 |
$this->zip->open($this->filepath); |
| 142 |
if ($this->zip->count() !== 1) { |
| 143 |
throw new SoftException('The archive must have only one file in it'); |
| 144 |
} |
| 145 |
$zfn = $this->zip->getNameIndex(0); |
| 146 |
if ($zfn !== pathinfo($zfn, PATHINFO_BASENAME)) { |
| 147 |
throw new SoftException('There must not be any directories in the archive'); |
| 148 |
} |
| 149 |
$fd = $this->zip->getStream($zfn); |
| 150 |
break; |
| 151 |
default: |
| 152 |
|
| 153 |
|
| 154 |
$fd = gzopen($this->filepath, 'r'); |
| 155 |
break; |
| 156 |
} |
| 157 |
if (!$fd) { |
| 158 |
throw new SoftException('Failed to open a report file'); |
| 159 |
} |
| 160 |
$this->fd = $fd; |
| 161 |
} |
| 162 |
if ($this->isGzipStream()) { |
| 163 |
ReportFile::ensureRegisterFilter( |
| 164 |
'report_gzfile_cut_filter', |
| 165 |
'Liuch\DmarcSrg\ReportFile\ReportGZFileCutFilter' |
| 166 |
); |
| 167 |
$this->enableGzFilter(true); |
| 168 |
} |
| 169 |
return $this->fd; |
| 170 |
} |
| 171 |
|
| 172 |
private static function ensureRegisterFilter($filtername, $classname) |
| 173 |
{ |
| 174 |
if (!isset(ReportFile::$filters[$filtername])) { |
| 175 |
stream_filter_register($filtername, $classname); |
| 176 |
ReportFile::$filters[$filtername] = true; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
private function enableGzFilter($enable) |
| 181 |
{ |
| 182 |
if ($enable) { |
| 183 |
if (!$this->gzcutfilter) { |
| 184 |
$this->gzcutfilter = stream_filter_append($this->fd, 'report_gzfile_cut_filter', STREAM_FILTER_READ); |
| 185 |
$this->gzinflatefilter = stream_filter_append($this->fd, 'zlib.inflate', STREAM_FILTER_READ); |
| 186 |
} |
| 187 |
} else { |
| 188 |
if ($this->gzcutfilter) { |
| 189 |
stream_filter_remove($this->gzinflatefilter); |
| 190 |
stream_filter_remove($this->gzcutfilter); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
private function isGzipStream(): bool |
| 196 |
{ |
| 197 |
return (in_array($this->type, [ 'application/gzip', 'application/x-gzip' ]) && !$this->filepath); |
| 198 |
} |
| 199 |
} |