1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187:
<?php
namespace MvcCore\Ext\Forms\Validators\Files\Validations\BombScanners;
class ZipArchive implements \MvcCore\Ext\Forms\Validators\Files\Validations\IBombScanner {
const ERR_NO_ZIP_EXT = "System extension to detect dangerous ZIP archives is not installed.";
protected static $zipErrors = [
\ZipArchive::ER_INCONS => "Uploaded file is inconsistent ZIP archive (`{1}`).",
\ZipArchive::ER_MEMORY => "Uploaded file caused memory allocation failure (`{1}`).",
\ZipArchive::ER_NOZIP => "Uploaded file has archive header but it's not an archive (`{1}`).",
\ZipArchive::ER_OPEN => "Uploaded file archive is not possible to open (`{1}`).",
\ZipArchive::ER_READ => "Uploaded file archive is not possible to read (`{1}`).",
\ZipArchive::ER_SEEK => "Uploaded file archive is not possible to seek (`{1}`).",
];
protected $validator = NULL;
protected $spl = NULL;
protected $fullPath = NULL;
protected $zip = NULL;
protected $openResult = NULL;
protected $index = 0;
protected $entry = NULL;
public static function MatchMagicBytes ($firstFourBytes) {
return $firstFourBytes === "PK\3\4";
}
public static function IsArchive () {
return TRUE;
}
public static function IsSupported () {
return extension_loaded('zip');
}
public static function GetNotSupportedError () {
return static::ERR_NO_ZIP_EXT;
}
public function __construct (\MvcCore\Ext\Forms\Validators\IFiles $validator, \SplFileObject $spl) {
$this->validator = $validator;
$this->spl = $spl;
$this->fullPath = str_replace('\\', '/', $this->spl->getRealPath());
$this->zip = new \ZipArchive();
}
public function Open () {
$this->openResult = $this->zip->open(
$this->fullPath, \ZipArchive::CHECKCONS
);
$this->index = 0;
return $this->openResult === TRUE;
}
public function GetError () {
if ($this->openResult === TRUE) return 0;
return static::$zipErrors[$this->openResult];
}
public function GetCompressedSize () {
return $this->spl->getSize();
}
public function Close() {
$this->zip->close();
}
public function Move() {
$moved = ($entry = $this->zip->statIndex($this->index++));
if ($moved) {
$this->entry = $entry;
return TRUE;
}
return FALSE;
}
public function GetEntrySize () {
return $this->entry['size'];
}
public function GetEntryName () {
return $this->entry['name'];
}
public function ExtractEntry ($destinationFullPath) {
$entryName = $this->entry['name'];
$readPointer = $this->zip->getStream($entryName);
if (!$readPointer)
return NULL;
$writePointer = fopen($destinationFullPath, 'w');
while (!feof($readPointer))
fwrite($writePointer, fread($readPointer, 131072));
fclose($readPointer);
fclose($writePointer);
return $destinationFullPath;
}
}