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:
<?php
/**
* MvcCore
*
* This source file is subject to the BSD 3 License
* For the full copyright and license information, please view
* the LICENSE.md file that are distributed with this source code.
*
* @copyright Copyright (c) 2016 Tom Flidr (https://github.com/mvccore)
* @license https://mvccore.github.io/docs/mvccore/5.0.0/LICENCE.md
*/
namespace MvcCore\Ext\Forms\Validators\Files\Validations\BombScanners;
class PngImage implements \MvcCore\Ext\Forms\Validators\Files\Validations\IBombScanner {
const ERR_INVALID_PNG_FILE = "Uploaded file is inconsistent PNG image (`{1}`).";
const ERR_INVALID_SIZES = "Uploaded file has invalid sizes (`{1}`).";
/**
* @var \MvcCore\Ext\Forms\Validators\Files
*/
protected $validator = NULL;
/**
* @var \SplFileObject
*/
protected $spl = NULL;
/**
* @var bool
*/
protected $ihdrFound = FALSE;
/**
* @var bool
*/
protected $invalidSizes = FALSE;
/**
* @param string $firstFourBytes
* @return bool
*/
public static function MatchMagicBytes ($firstFourBytes) {
return $firstFourBytes === chr(0x89) . "PNG";
}
/**
* @return bool
*/
public static function IsArchive () {
return FALSE;
}
/**
* @return bool
*/
public static function IsSupported () {
return TRUE;
}
/**
* @return string
*/
public static function GetNotSupportedError () {
return '';
}
/**
* @param \MvcCore\Ext\Forms\Validators\Files $validator
* @param \SplFileObject $spl
* @return void
*/
public function __construct (\MvcCore\Ext\Forms\Validators\IFiles $validator, \SplFileObject $spl) {
$this->validator = $validator;
$this->spl = $spl;
}
/**
* @return bool
*/
public function Open () {
$this->spl->rewind();
$this->spl->fread(12);
$ihdrBytes = (string) $this->spl->fread(4);
$this->ihdrFound = $ihdrBytes === 'IHDR';
if (!$this->ihdrFound)
return FALSE;
// PNG stores width and height integers in big-endian
$widthBinnary = (string) $this->spl->fread(4);
$heightBinnary = (string) $this->spl->fread(4);
$widthUnpacked = unpack('N', $widthBinnary);
$heightUnpacked = unpack('N', $heightBinnary);
$width = isset($widthUnpacked[1])
? $widthUnpacked[1]
: 0;
$height = isset($heightUnpacked[1])
? $heightUnpacked[1]
: 0;
$maxPngSize = $this->validator->GetPngImageMaxWidthHeight();
$this->invalidSizes = ($width > $maxPngSize || $height > $maxPngSize);
if ($this->invalidSizes)
return FALSE;
return TRUE;
}
/**
* @return int
*/
public function GetCompressedSize () {
return $this->spl->getSize();
}
/**
* @return string
*/
public function GetError () {
if (!$this->ihdrFound)
return static::ERR_INVALID_PNG_FILE;
if ($this->invalidSizes)
return static::ERR_INVALID_SIZES;
return '';
}
/**
*
* @return void
*/
public function Close() {
unset($this->spl);
}
/**
*
* @return boolean
*/
public function Move() {
return FALSE;
}
/**
* @return int
*/
public function GetEntrySize () {
return 0;
}
/**
* @return string
*/
public function GetEntryName () {
return '';
}
/**
* @param string $destinationFullPath
* @return string|NULL
*/
public function ExtractEntry ($destinationFullPath) {
return NULL;
}
}