| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use MediaWiki\Libs\UnpackFailedException; |
| 25 |
use Wikimedia\AtEase\AtEase; |
| 26 |
use Wikimedia\XMPReader\Reader as XMPReader; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class JpegMetadataExtractor { |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
private const MAX_JPEG_SEGMENTS = 200; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public static function segmentSplitter( $filename ) { |
| 55 |
$showXMP = XMPReader::isSupported(); |
| 56 |
|
| 57 |
$segmentCount = 0; |
| 58 |
|
| 59 |
$segments = [ |
| 60 |
'XMP_ext' => [], |
| 61 |
'COM' => [], |
| 62 |
'PSIR' => [], |
| 63 |
]; |
| 64 |
|
| 65 |
if ( !$filename ) { |
| 66 |
throw new InvalidJpegException( "No filename specified for " . __METHOD__ ); |
| 67 |
} |
| 68 |
if ( !file_exists( $filename ) || is_dir( $filename ) ) { |
| 69 |
throw new InvalidJpegException( "Invalid file $filename passed to " . __METHOD__ ); |
| 70 |
} |
| 71 |
|
| 72 |
$fh = fopen( $filename, "rb" ); |
| 73 |
|
| 74 |
if ( !$fh ) { |
| 75 |
throw new InvalidJpegException( "Could not open file $filename" ); |
| 76 |
} |
| 77 |
|
| 78 |
$buffer = fread( $fh, 2 ); |
| 79 |
if ( $buffer !== "\xFF\xD8" ) { |
| 80 |
throw new InvalidJpegException( "Not a jpeg, no SOI" ); |
| 81 |
} |
| 82 |
while ( !feof( $fh ) ) { |
| 83 |
$buffer = fread( $fh, 1 ); |
| 84 |
$segmentCount++; |
| 85 |
if ( $segmentCount > self::MAX_JPEG_SEGMENTS ) { |
| 86 |
throw new InvalidJpegException( 'Too many jpeg segments. Aborting' ); |
| 87 |
} |
| 88 |
while ( $buffer !== "\xFF" && !feof( $fh ) ) { |
| 89 |
|
| 90 |
|
| 91 |
$buffer = fread( $fh, 1 ); |
| 92 |
} |
| 93 |
|
| 94 |
$buffer = fread( $fh, 1 ); |
| 95 |
while ( $buffer === "\xFF" && !feof( $fh ) ) { |
| 96 |
|
| 97 |
$buffer = fread( $fh, 1 ); |
| 98 |
} |
| 99 |
if ( $buffer === "\xFE" ) { |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
$com = $oldCom = trim( self::jpegExtractMarker( $fh ) ); |
| 104 |
UtfNormal\Validator::quickIsNFCVerify( $com ); |
| 105 |
|
| 106 |
|
| 107 |
if ( $com !== $oldCom ) { |
| 108 |
AtEase::suppressWarnings(); |
| 109 |
$com = $oldCom = iconv( 'windows-1252', 'UTF-8//IGNORE', $oldCom ); |
| 110 |
AtEase::restoreWarnings(); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
UtfNormal\Validator::quickIsNFCVerify( $com ); |
| 115 |
if ( $com === $oldCom ) { |
| 116 |
$segments["COM"][] = $oldCom; |
| 117 |
} else { |
| 118 |
wfDebug( __METHOD__ . " Ignoring JPEG comment as is garbage." ); |
| 119 |
} |
| 120 |
} elseif ( $buffer === "\xE1" ) { |
| 121 |
|
| 122 |
|
| 123 |
$temp = self::jpegExtractMarker( $fh ); |
| 124 |
|
| 125 |
if ( substr( $temp, 0, 29 ) === "http://ns.adobe.com/xap/1.0/\x00" && $showXMP ) { |
| 126 |
|
| 127 |
$segments["XMP"] = trim( substr( $temp, 29 ) ); |
| 128 |
} elseif ( substr( $temp, 0, 35 ) === "http://ns.adobe.com/xmp/extension/\x00" && $showXMP ) { |
| 129 |
|
| 130 |
$segments["XMP_ext"][] = trim( substr( $temp, 35 ) ); |
| 131 |
} elseif ( substr( $temp, 0, 29 ) === "XMP\x00://ns.adobe.com/xap/1.0/\x00" && $showXMP ) { |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
$segments["XMP"] = trim( substr( $temp, 29 ) ); |
| 137 |
wfDebug( __METHOD__ . ' Found XMP section with wrong app identifier ' |
| 138 |
. "Using anyways." ); |
| 139 |
} elseif ( substr( $temp, 0, 6 ) === "Exif\0\0" ) { |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
$byteOrderMarker = substr( $temp, 6, 2 ); |
| 144 |
if ( $byteOrderMarker === 'MM' ) { |
| 145 |
$segments['byteOrder'] = 'BE'; |
| 146 |
} elseif ( $byteOrderMarker === 'II' ) { |
| 147 |
$segments['byteOrder'] = 'LE'; |
| 148 |
} else { |
| 149 |
wfDebug( __METHOD__ . " Invalid byte ordering?!" ); |
| 150 |
} |
| 151 |
} |
| 152 |
} elseif ( $buffer === "\xED" ) { |
| 153 |
|
| 154 |
$temp = self::jpegExtractMarker( $fh ); |
| 155 |
if ( substr( $temp, 0, 14 ) === "Photoshop 3.0\x00" ) { |
| 156 |
$segments["PSIR"][] = $temp; |
| 157 |
} |
| 158 |
} elseif ( $buffer === "\xD9" || $buffer === "\xDA" ) { |
| 159 |
|
| 160 |
return $segments; |
| 161 |
} elseif ( in_array( $buffer, [ |
| 162 |
"\xC0", "\xC1", "\xC2", "\xC3", "\xC5", "\xC6", "\xC7", |
| 163 |
"\xC9", "\xCA", "\xCB", "\xCD", "\xCE", "\xCF" ] ) |
| 164 |
) { |
| 165 |
|
| 166 |
$temp = self::jpegExtractMarker( $fh ); |
| 167 |
try { |
| 168 |
$segments["SOF"] = StringUtils::unpack( 'Cbits/nheight/nwidth/Ccomponents', $temp ); |
| 169 |
} catch ( UnpackFailedException $e ) { |
| 170 |
throw new InvalidJpegException( $e->getMessage() ); |
| 171 |
} |
| 172 |
} else { |
| 173 |
|
| 174 |
try { |
| 175 |
$size = StringUtils::unpack( "nint", fread( $fh, 2 ), 2 ); |
| 176 |
} catch ( UnpackFailedException $e ) { |
| 177 |
throw new InvalidJpegException( $e->getMessage() ); |
| 178 |
} |
| 179 |
if ( $size['int'] < 2 ) { |
| 180 |
throw new InvalidJpegException( "invalid marker size in jpeg" ); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
fseek( $fh, $size['int'] - 2, SEEK_CUR ); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
throw new InvalidJpegException( "Reached end of jpeg file unexpectedly" ); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
private static function jpegExtractMarker( &$fh ) { |
| 198 |
try { |
| 199 |
$size = StringUtils::unpack( "nint", fread( $fh, 2 ), 2 ); |
| 200 |
} catch ( UnpackFailedException $e ) { |
| 201 |
throw new InvalidJpegException( $e->getMessage() ); |
| 202 |
} |
| 203 |
if ( $size['int'] < 2 ) { |
| 204 |
throw new InvalidJpegException( "invalid marker size in jpeg" ); |
| 205 |
} |
| 206 |
if ( $size['int'] === 2 ) { |
| 207 |
|
| 208 |
return ''; |
| 209 |
} |
| 210 |
$segment = fread( $fh, $size['int'] - 2 ); |
| 211 |
if ( strlen( $segment ) !== $size['int'] - 2 ) { |
| 212 |
throw new InvalidJpegException( "Segment shorter than expected" ); |
| 213 |
} |
| 214 |
|
| 215 |
return $segment; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
public static function doPSIR( $app13 ) { |
| 233 |
if ( !$app13 ) { |
| 234 |
throw new InvalidPSIRException( "No App13 segment given" ); |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
$offset = 14; |
| 243 |
$appLen = strlen( $app13 ); |
| 244 |
$realHash = ""; |
| 245 |
$recordedHash = ""; |
| 246 |
|
| 247 |
|
| 248 |
while ( $offset + 12 <= $appLen ) { |
| 249 |
$valid = true; |
| 250 |
if ( substr( $app13, $offset, 4 ) !== '8BIM' ) { |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
$valid = false; |
| 255 |
} |
| 256 |
$offset += 4; |
| 257 |
$id = substr( $app13, $offset, 2 ); |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
$offset += 2; |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
$lenName = ord( substr( $app13, $offset, 1 ) ) + 1; |
| 269 |
|
| 270 |
if ( $lenName % 2 === 1 ) { |
| 271 |
$lenName++; |
| 272 |
} |
| 273 |
$offset += $lenName; |
| 274 |
|
| 275 |
|
| 276 |
try { |
| 277 |
$lenData = StringUtils::unpack( 'Nlen', substr( $app13, $offset, 4 ), 4 ); |
| 278 |
} catch ( UnpackFailedException $e ) { |
| 279 |
throw new InvalidPSIRException( $e->getMessage() ); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
if ( $lenData['len'] < 0 ) { |
| 285 |
throw new InvalidPSIRException( "Too big PSIR (" . $lenData['len'] . ')' ); |
| 286 |
} |
| 287 |
|
| 288 |
$offset += 4; |
| 289 |
|
| 290 |
|
| 291 |
if ( $lenData['len'] + $offset > $appLen ) { |
| 292 |
throw new InvalidPSIRException( "PSIR data too long. (item length=" . $lenData['len'] |
| 293 |
. "; offset=$offset; total length=$appLen)" ); |
| 294 |
} |
| 295 |
|
| 296 |
if ( $valid ) { |
| 297 |
switch ( $id ) { |
| 298 |
case "\x04\x04": |
| 299 |
|
| 300 |
$realHash = md5( substr( $app13, $offset, $lenData['len'] ), true ); |
| 301 |
break; |
| 302 |
case "\x04\x25": |
| 303 |
$recordedHash = substr( $app13, $offset, $lenData['len'] ); |
| 304 |
break; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
if ( $lenData['len'] % 2 === 1 ) { |
| 311 |
$lenData['len']++; |
| 312 |
} |
| 313 |
$offset += $lenData['len']; |
| 314 |
} |
| 315 |
|
| 316 |
if ( !$realHash || !$recordedHash ) { |
| 317 |
return 'iptc-no-hash'; |
| 318 |
} |
| 319 |
if ( $realHash === $recordedHash ) { |
| 320 |
return 'iptc-good-hash'; |
| 321 |
} |
| 322 |
|
| 323 |
return 'iptc-bad-hash'; |
| 324 |
} |
| 325 |
} |