| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
om> |
| 22 |
rber |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
use MediaWiki\Config\ConfigException; |
| 29 |
use Wikimedia\AtEase\AtEase; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class Exif { |
| 36 |
|
| 37 |
private const BYTE = 1; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
private const ASCII = 2; |
| 43 |
|
| 44 |
|
| 45 |
private const SHORT = 3; |
| 46 |
|
| 47 |
|
| 48 |
private const LONG = 4; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
private const RATIONAL = 5; |
| 54 |
|
| 55 |
|
| 56 |
private const SHORT_OR_LONG = 6; |
| 57 |
|
| 58 |
|
| 59 |
private const UNDEFINED = 7; |
| 60 |
|
| 61 |
|
| 62 |
private const SLONG = 9; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
private const SRATIONAL = 10; |
| 68 |
|
| 69 |
|
| 70 |
private const IGNORE = -1; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
private $mExifTags; |
| 77 |
|
| 78 |
|
| 79 |
private $mRawExifData; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
private $mFilteredExifData; |
| 86 |
|
| 87 |
|
| 88 |
private $file; |
| 89 |
|
| 90 |
|
| 91 |
private $basename; |
| 92 |
|
| 93 |
|
| 94 |
private $log = false; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
private $byteOrder; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public function __construct( $file, $byteOrder = '' ) { |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
$this->mExifTags = [ |
| 120 |
|
| 121 |
'IFD0' => [ |
| 122 |
|
| 123 |
|
| 124 |
'ImageWidth' => self::SHORT_OR_LONG, |
| 125 |
|
| 126 |
'ImageLength' => self::SHORT_OR_LONG, |
| 127 |
|
| 128 |
'BitsPerSample' => [ self::SHORT, 3 ], |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
'Compression' => self::SHORT, |
| 134 |
|
| 135 |
'PhotometricInterpretation' => self::SHORT, |
| 136 |
|
| 137 |
'Orientation' => self::SHORT, |
| 138 |
|
| 139 |
'SamplesPerPixel' => self::SHORT, |
| 140 |
|
| 141 |
'PlanarConfiguration' => self::SHORT, |
| 142 |
|
| 143 |
'YCbCrSubSampling' => [ self::SHORT, 2 ], |
| 144 |
|
| 145 |
'YCbCrPositioning' => self::SHORT, |
| 146 |
|
| 147 |
'XResolution' => self::RATIONAL, |
| 148 |
|
| 149 |
'YResolution' => self::RATIONAL, |
| 150 |
|
| 151 |
'ResolutionUnit' => self::SHORT, |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
'StripOffsets' => self::SHORT_OR_LONG, |
| 156 |
|
| 157 |
'RowsPerStrip' => self::SHORT_OR_LONG, |
| 158 |
|
| 159 |
'StripByteCounts' => self::SHORT_OR_LONG, |
| 160 |
|
| 161 |
'JPEGInterchangeFormat' => self::SHORT_OR_LONG, |
| 162 |
|
| 163 |
'JPEGInterchangeFormatLength' => self::SHORT_OR_LONG, |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
'TransferFunction' => self::IGNORE, |
| 168 |
|
| 169 |
'WhitePoint' => [ self::RATIONAL, 2 ], |
| 170 |
|
| 171 |
'PrimaryChromaticities' => [ self::RATIONAL, 6 ], |
| 172 |
|
| 173 |
'YCbCrCoefficients' => [ self::RATIONAL, 3 ], |
| 174 |
|
| 175 |
'ReferenceBlackWhite' => [ self::RATIONAL, 6 ], |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
'DateTime' => self::ASCII, |
| 180 |
|
| 181 |
'ImageDescription' => self::ASCII, |
| 182 |
|
| 183 |
'Make' => self::ASCII, |
| 184 |
|
| 185 |
'Model' => self::ASCII, |
| 186 |
|
| 187 |
'Software' => self::ASCII, |
| 188 |
|
| 189 |
'Artist' => self::ASCII, |
| 190 |
|
| 191 |
'Copyright' => self::ASCII, |
| 192 |
], |
| 193 |
|
| 194 |
|
| 195 |
'EXIF' => [ |
| 196 |
|
| 197 |
|
| 198 |
'ExifVersion' => self::UNDEFINED, |
| 199 |
|
| 200 |
'FlashPixVersion' => self::UNDEFINED, |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
'ColorSpace' => self::SHORT, |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
'ComponentsConfiguration' => self::UNDEFINED, |
| 209 |
|
| 210 |
'CompressedBitsPerPixel' => self::RATIONAL, |
| 211 |
|
| 212 |
'PixelYDimension' => self::SHORT_OR_LONG, |
| 213 |
|
| 214 |
'PixelXDimension' => self::SHORT_OR_LONG, |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
'MakerNote' => self::IGNORE, |
| 219 |
|
| 220 |
'UserComment' => self::UNDEFINED, |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
'RelatedSoundFile' => self::ASCII, |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
'DateTimeOriginal' => self::ASCII, |
| 229 |
|
| 230 |
'DateTimeDigitized' => self::ASCII, |
| 231 |
|
| 232 |
'SubSecTime' => self::ASCII, |
| 233 |
|
| 234 |
'SubSecTimeOriginal' => self::ASCII, |
| 235 |
|
| 236 |
'SubSecTimeDigitized' => self::ASCII, |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
'ExposureTime' => self::RATIONAL, |
| 241 |
|
| 242 |
'FNumber' => self::RATIONAL, |
| 243 |
|
| 244 |
'ExposureProgram' => self::SHORT, |
| 245 |
|
| 246 |
'SpectralSensitivity' => self::ASCII, |
| 247 |
|
| 248 |
'ISOSpeedRatings' => self::SHORT, |
| 249 |
|
| 250 |
|
| 251 |
'OECF' => self::IGNORE, |
| 252 |
|
| 253 |
|
| 254 |
'ShutterSpeedValue' => self::SRATIONAL, |
| 255 |
|
| 256 |
'ApertureValue' => self::RATIONAL, |
| 257 |
|
| 258 |
'BrightnessValue' => self::SRATIONAL, |
| 259 |
|
| 260 |
'ExposureBiasValue' => self::SRATIONAL, |
| 261 |
|
| 262 |
'MaxApertureValue' => self::RATIONAL, |
| 263 |
|
| 264 |
'SubjectDistance' => self::RATIONAL, |
| 265 |
|
| 266 |
'MeteringMode' => self::SHORT, |
| 267 |
|
| 268 |
'LightSource' => self::SHORT, |
| 269 |
|
| 270 |
'Flash' => self::SHORT, |
| 271 |
|
| 272 |
'FocalLength' => self::RATIONAL, |
| 273 |
|
| 274 |
'SubjectArea' => [ self::SHORT, 4 ], |
| 275 |
|
| 276 |
'FlashEnergy' => self::RATIONAL, |
| 277 |
|
| 278 |
'SpatialFrequencyResponse' => self::IGNORE, |
| 279 |
|
| 280 |
'FocalPlaneXResolution' => self::RATIONAL, |
| 281 |
|
| 282 |
'FocalPlaneYResolution' => self::RATIONAL, |
| 283 |
|
| 284 |
'FocalPlaneResolutionUnit' => self::SHORT, |
| 285 |
|
| 286 |
'SubjectLocation' => [ self::SHORT, 2 ], |
| 287 |
|
| 288 |
'ExposureIndex' => self::RATIONAL, |
| 289 |
|
| 290 |
'SensingMethod' => self::SHORT, |
| 291 |
|
| 292 |
'FileSource' => self::UNDEFINED, |
| 293 |
|
| 294 |
'SceneType' => self::UNDEFINED, |
| 295 |
|
| 296 |
'CFAPattern' => self::IGNORE, |
| 297 |
|
| 298 |
'CustomRendered' => self::SHORT, |
| 299 |
|
| 300 |
'ExposureMode' => self::SHORT, |
| 301 |
|
| 302 |
'WhiteBalance' => self::SHORT, |
| 303 |
|
| 304 |
'DigitalZoomRatio' => self::RATIONAL, |
| 305 |
|
| 306 |
'FocalLengthIn35mmFilm' => self::SHORT, |
| 307 |
|
| 308 |
'SceneCaptureType' => self::SHORT, |
| 309 |
|
| 310 |
'GainControl' => self::SHORT, |
| 311 |
|
| 312 |
'Contrast' => self::SHORT, |
| 313 |
|
| 314 |
'Saturation' => self::SHORT, |
| 315 |
|
| 316 |
'Sharpness' => self::SHORT, |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
'DeviceSettingDescription' => self::IGNORE, |
| 321 |
|
| 322 |
|
| 323 |
'SubjectDistanceRange' => self::SHORT, |
| 324 |
|
| 325 |
|
| 326 |
'ImageUniqueID' => self::ASCII, |
| 327 |
], |
| 328 |
|
| 329 |
|
| 330 |
'GPS' => [ |
| 331 |
'GPSVersion' => self::UNDEFINED, |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
'GPSLatitudeRef' => self::ASCII, |
| 336 |
|
| 337 |
'GPSLatitude' => [ self::RATIONAL, 3 ], |
| 338 |
|
| 339 |
'GPSLongitudeRef' => self::ASCII, |
| 340 |
|
| 341 |
'GPSLongitude' => [ self::RATIONAL, 3 ], |
| 342 |
'GPSAltitudeRef' => self::UNDEFINED, |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
'GPSAltitude' => self::RATIONAL, |
| 348 |
|
| 349 |
'GPSTimeStamp' => [ self::RATIONAL, 3 ], |
| 350 |
|
| 351 |
'GPSSatellites' => self::ASCII, |
| 352 |
|
| 353 |
'GPSStatus' => self::ASCII, |
| 354 |
|
| 355 |
'GPSMeasureMode' => self::ASCII, |
| 356 |
|
| 357 |
'GPSDOP' => self::RATIONAL, |
| 358 |
|
| 359 |
'GPSSpeedRef' => self::ASCII, |
| 360 |
|
| 361 |
'GPSSpeed' => self::RATIONAL, |
| 362 |
|
| 363 |
'GPSTrackRef' => self::ASCII, |
| 364 |
|
| 365 |
'GPSTrack' => self::RATIONAL, |
| 366 |
|
| 367 |
'GPSImgDirectionRef' => self::ASCII, |
| 368 |
|
| 369 |
'GPSImgDirection' => self::RATIONAL, |
| 370 |
|
| 371 |
'GPSMapDatum' => self::ASCII, |
| 372 |
|
| 373 |
'GPSDestLatitudeRef' => self::ASCII, |
| 374 |
|
| 375 |
'GPSDestLatitude' => [ self::RATIONAL, 3 ], |
| 376 |
|
| 377 |
'GPSDestLongitudeRef' => self::ASCII, |
| 378 |
|
| 379 |
'GPSDestLongitude' => [ self::RATIONAL, 3 ], |
| 380 |
|
| 381 |
'GPSDestBearingRef' => self::ASCII, |
| 382 |
|
| 383 |
'GPSDestBearing' => self::RATIONAL, |
| 384 |
|
| 385 |
'GPSDestDistanceRef' => self::ASCII, |
| 386 |
|
| 387 |
'GPSDestDistance' => self::RATIONAL, |
| 388 |
|
| 389 |
'GPSProcessingMethod' => self::UNDEFINED, |
| 390 |
|
| 391 |
'GPSAreaInformation' => self::UNDEFINED, |
| 392 |
|
| 393 |
'GPSDateStamp' => self::ASCII, |
| 394 |
|
| 395 |
'GPSDifferential' => self::SHORT, |
| 396 |
], |
| 397 |
]; |
| 398 |
|
| 399 |
$this->file = $file; |
| 400 |
$this->basename = wfBaseName( $this->file ); |
| 401 |
if ( $byteOrder === 'BE' || $byteOrder === 'LE' ) { |
| 402 |
$this->byteOrder = $byteOrder; |
| 403 |
} else { |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
wfWarn( 'Exif class did not have byte order specified. ' . |
| 408 |
'Some properties may be decoded incorrectly.' ); |
| 409 |
|
| 410 |
$this->byteOrder = 'BE'; |
| 411 |
} |
| 412 |
|
| 413 |
$this->debugFile( __FUNCTION__, true ); |
| 414 |
if ( function_exists( 'exif_read_data' ) ) { |
| 415 |
AtEase::suppressWarnings(); |
| 416 |
$data = exif_read_data( $this->file, '', true ); |
| 417 |
AtEase::restoreWarnings(); |
| 418 |
} else { |
| 419 |
throw new ConfigException( "Internal error: exif_read_data not present. " . |
| 420 |
"\$wgShowEXIF may be incorrectly set or not checked by an extension." ); |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
$this->mRawExifData = $data ?: []; |
| 428 |
$this->makeFilteredData(); |
| 429 |
$this->collapseData(); |
| 430 |
$this->debugFile( __FUNCTION__, false ); |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
private function makeFilteredData() { |
| 437 |
$this->mFilteredExifData = []; |
| 438 |
|
| 439 |
foreach ( $this->mRawExifData as $section => $data ) { |
| 440 |
if ( !array_key_exists( $section, $this->mExifTags ) ) { |
| 441 |
$this->debug( $section, __FUNCTION__, "'$section' is not a valid Exif section" ); |
| 442 |
continue; |
| 443 |
} |
| 444 |
|
| 445 |
foreach ( $data as $tag => $value ) { |
| 446 |
if ( !array_key_exists( $tag, $this->mExifTags[$section] ) ) { |
| 447 |
$this->debug( $tag, __FUNCTION__, "'$tag' is not a valid tag in '$section'" ); |
| 448 |
continue; |
| 449 |
} |
| 450 |
|
| 451 |
if ( $this->validate( $section, $tag, $value ) ) { |
| 452 |
|
| 453 |
|
| 454 |
$this->mFilteredExifData[$tag] = $value; |
| 455 |
} else { |
| 456 |
$this->debug( $value, __FUNCTION__, "'$tag' contained invalid data" ); |
| 457 |
} |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
private function collapseData() { |
| 481 |
$this->exifGPStoNumber( 'GPSLatitude' ); |
| 482 |
$this->exifGPStoNumber( 'GPSDestLatitude' ); |
| 483 |
$this->exifGPStoNumber( 'GPSLongitude' ); |
| 484 |
$this->exifGPStoNumber( 'GPSDestLongitude' ); |
| 485 |
|
| 486 |
if ( isset( $this->mFilteredExifData['GPSAltitude'] ) ) { |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
[ $num, $denom ] = explode( '/', $this->mFilteredExifData['GPSAltitude'], 2 ); |
| 491 |
$this->mFilteredExifData['GPSAltitude'] = (int)$num / (int)$denom; |
| 492 |
|
| 493 |
if ( isset( $this->mFilteredExifData['GPSAltitudeRef'] ) ) { |
| 494 |
switch ( $this->mFilteredExifData['GPSAltitudeRef'] ) { |
| 495 |
case "\0": |
| 496 |
|
| 497 |
break; |
| 498 |
case "\1": |
| 499 |
|
| 500 |
$this->mFilteredExifData['GPSAltitude'] *= -1; |
| 501 |
break; |
| 502 |
default: |
| 503 |
|
| 504 |
unset( $this->mFilteredExifData['GPSAltitude'] ); |
| 505 |
break; |
| 506 |
} |
| 507 |
} |
| 508 |
} |
| 509 |
unset( $this->mFilteredExifData['GPSAltitudeRef'] ); |
| 510 |
|
| 511 |
$this->exifPropToOrd( 'FileSource' ); |
| 512 |
$this->exifPropToOrd( 'SceneType' ); |
| 513 |
|
| 514 |
$this->charCodeString( 'UserComment' ); |
| 515 |
$this->charCodeString( 'GPSProcessingMethod' ); |
| 516 |
$this->charCodeString( 'GPSAreaInformation' ); |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
if ( isset( $this->mFilteredExifData['ComponentsConfiguration'] ) ) { |
| 522 |
$val = $this->mFilteredExifData['ComponentsConfiguration']; |
| 523 |
$ccVals = []; |
| 524 |
|
| 525 |
$strLen = strlen( $val ); |
| 526 |
for ( $i = 0; $i < $strLen; $i++ ) { |
| 527 |
$ccVals[$i] = ord( substr( $val, $i, 1 ) ); |
| 528 |
} |
| 529 |
|
| 530 |
$ccVals['_type'] = 'ol'; |
| 531 |
$this->mFilteredExifData['ComponentsConfiguration'] = $ccVals; |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
if ( isset( $this->mFilteredExifData['GPSVersion'] ) ) { |
| 542 |
$val = $this->mFilteredExifData['GPSVersion']; |
| 543 |
$newVal = ''; |
| 544 |
|
| 545 |
$strLen = strlen( $val ); |
| 546 |
for ( $i = 0; $i < $strLen; $i++ ) { |
| 547 |
if ( $i !== 0 ) { |
| 548 |
$newVal .= '.'; |
| 549 |
} |
| 550 |
$newVal .= ord( substr( $val, $i, 1 ) ); |
| 551 |
} |
| 552 |
|
| 553 |
if ( $this->byteOrder === 'LE' ) { |
| 554 |
|
| 555 |
$newVal2 = ''; |
| 556 |
for ( $i = strlen( $newVal ) - 1; $i >= 0; $i-- ) { |
| 557 |
$newVal2 .= substr( $newVal, $i, 1 ); |
| 558 |
} |
| 559 |
$this->mFilteredExifData['GPSVersionID'] = $newVal2; |
| 560 |
} else { |
| 561 |
$this->mFilteredExifData['GPSVersionID'] = $newVal; |
| 562 |
} |
| 563 |
unset( $this->mFilteredExifData['GPSVersion'] ); |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
private function charCodeString( $prop ) { |
| 574 |
if ( isset( $this->mFilteredExifData[$prop] ) ) { |
| 575 |
if ( strlen( $this->mFilteredExifData[$prop] ) <= 8 ) { |
| 576 |
|
| 577 |
|
| 578 |
$this->debug( $this->mFilteredExifData[$prop], __FUNCTION__, false ); |
| 579 |
unset( $this->mFilteredExifData[$prop] ); |
| 580 |
|
| 581 |
return; |
| 582 |
} |
| 583 |
$charCode = substr( $this->mFilteredExifData[$prop], 0, 8 ); |
| 584 |
$val = substr( $this->mFilteredExifData[$prop], 8 ); |
| 585 |
|
| 586 |
switch ( $charCode ) { |
| 587 |
case "JIS\x00\x00\x00\x00\x00": |
| 588 |
$charset = "Shift-JIS"; |
| 589 |
break; |
| 590 |
case "UNICODE\x00": |
| 591 |
$charset = "UTF-16" . $this->byteOrder; |
| 592 |
break; |
| 593 |
default: |
| 594 |
|
| 595 |
$charset = ""; |
| 596 |
break; |
| 597 |
} |
| 598 |
if ( $charset ) { |
| 599 |
AtEase::suppressWarnings(); |
| 600 |
$val = iconv( $charset, 'UTF-8//IGNORE', $val ); |
| 601 |
AtEase::restoreWarnings(); |
| 602 |
} else { |
| 603 |
|
| 604 |
$valCopy = $val; |
| 605 |
UtfNormal\Validator::quickIsNFCVerify( $valCopy ); |
| 606 |
if ( $valCopy !== $val ) { |
| 607 |
AtEase::suppressWarnings(); |
| 608 |
$val = iconv( 'Windows-1252', 'UTF-8//IGNORE', $val ); |
| 609 |
AtEase::restoreWarnings(); |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
|
| 614 |
$val = trim( $val ); |
| 615 |
if ( strlen( $val ) === 0 ) { |
| 616 |
|
| 617 |
$this->debug( $this->mFilteredExifData[$prop], __FUNCTION__, "$prop: Is only whitespace" ); |
| 618 |
unset( $this->mFilteredExifData[$prop] ); |
| 619 |
|
| 620 |
return; |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
$this->mFilteredExifData[$prop] = $val; |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
private function exifPropToOrd( $prop ) { |
| 635 |
if ( isset( $this->mFilteredExifData[$prop] ) ) { |
| 636 |
$this->mFilteredExifData[$prop] = ord( $this->mFilteredExifData[$prop] ); |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
private function exifGPStoNumber( $prop ) { |
| 646 |
$loc = $this->mFilteredExifData[$prop] ?? null; |
| 647 |
$dir = $this->mFilteredExifData[$prop . 'Ref'] ?? null; |
| 648 |
$res = false; |
| 649 |
|
| 650 |
if ( $loc !== null && in_array( $dir, [ 'N', 'S', 'E', 'W' ] ) ) { |
| 651 |
if ( is_array( $loc ) && count( $loc ) === 3 ) { |
| 652 |
[ $num, $denom ] = explode( '/', $loc[0], 2 ); |
| 653 |
$res = (int)$num / (int)$denom; |
| 654 |
[ $num, $denom ] = explode( '/', $loc[1], 2 ); |
| 655 |
$res += ( (int)$num / (int)$denom ) * ( 1 / 60 ); |
| 656 |
[ $num, $denom ] = explode( '/', $loc[2], 2 ); |
| 657 |
$res += ( (int)$num / (int)$denom ) * ( 1 / 3600 ); |
| 658 |
} elseif ( is_string( $loc ) ) { |
| 659 |
|
| 660 |
[ $num, $denom ] = explode( '/', $loc, 2 ); |
| 661 |
$res = (int)$num / (int)$denom; |
| 662 |
} |
| 663 |
|
| 664 |
if ( $res && ( $dir === 'S' || $dir === 'W' ) ) { |
| 665 |
|
| 666 |
$res *= -1; |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
if ( $res !== false ) { |
| 674 |
$this->mFilteredExifData[$prop] = $res; |
| 675 |
} else { |
| 676 |
|
| 677 |
unset( $this->mFilteredExifData[$prop] ); |
| 678 |
} |
| 679 |
unset( $this->mFilteredExifData[$prop . 'Ref'] ); |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
public function getData() { |
| 687 |
return $this->mRawExifData; |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
public function getFilteredData() { |
| 695 |
return $this->mFilteredExifData; |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
public static function version() { |
| 711 |
return 2; |
| 712 |
} |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
private function isByte( $in ) { |
| 721 |
if ( !is_array( $in ) && sprintf( '%d', $in ) == $in && $in >= 0 && $in <= 255 ) { |
| 722 |
$this->debug( $in, __FUNCTION__, true ); |
| 723 |
|
| 724 |
return true; |
| 725 |
} |
| 726 |
|
| 727 |
$this->debug( $in, __FUNCTION__, false ); |
| 728 |
|
| 729 |
return false; |
| 730 |
} |
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
private function isASCII( $in ) { |
| 737 |
if ( is_array( $in ) ) { |
| 738 |
return false; |
| 739 |
} |
| 740 |
|
| 741 |
if ( preg_match( "/[^\x0a\x20-\x7e]/", $in ) ) { |
| 742 |
$this->debug( $in, __FUNCTION__, 'found a character that is not allowed' ); |
| 743 |
|
| 744 |
return false; |
| 745 |
} |
| 746 |
|
| 747 |
if ( preg_match( '/^\s*$/', $in ) ) { |
| 748 |
$this->debug( $in, __FUNCTION__, 'input consisted solely of whitespace' ); |
| 749 |
|
| 750 |
return false; |
| 751 |
} |
| 752 |
|
| 753 |
return true; |
| 754 |
} |
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
private function isShort( $in ) { |
| 761 |
if ( !is_array( $in ) && sprintf( '%d', $in ) == $in && $in >= 0 && $in <= 65536 ) { |
| 762 |
$this->debug( $in, __FUNCTION__, true ); |
| 763 |
|
| 764 |
return true; |
| 765 |
} |
| 766 |
|
| 767 |
$this->debug( $in, __FUNCTION__, false ); |
| 768 |
|
| 769 |
return false; |
| 770 |
} |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
private function isLong( $in ) { |
| 777 |
if ( !is_array( $in ) && sprintf( '%d', $in ) == $in && $in >= 0 && $in <= 4_294_967_296 ) { |
| 778 |
$this->debug( $in, __FUNCTION__, true ); |
| 779 |
|
| 780 |
return true; |
| 781 |
} |
| 782 |
|
| 783 |
$this->debug( $in, __FUNCTION__, false ); |
| 784 |
|
| 785 |
return false; |
| 786 |
} |
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
private function isRational( $in ) { |
| 793 |
$m = []; |
| 794 |
|
| 795 |
|
| 796 |
if ( !is_array( $in ) |
| 797 |
&& preg_match( '/^(\d+)\/(\d+[1-9]|[1-9]\d*)$/', $in, $m ) |
| 798 |
) { |
| 799 |
return $this->isLong( $m[1] ) && $this->isLong( $m[2] ); |
| 800 |
} |
| 801 |
|
| 802 |
$this->debug( $in, __FUNCTION__, 'fed a non-fraction value' ); |
| 803 |
|
| 804 |
return false; |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
private function isUndefined( $in ) { |
| 812 |
$this->debug( $in, __FUNCTION__, true ); |
| 813 |
|
| 814 |
return true; |
| 815 |
} |
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
private function isSlong( $in ) { |
| 822 |
if ( $this->isLong( abs( (float)$in ) ) ) { |
| 823 |
$this->debug( $in, __FUNCTION__, true ); |
| 824 |
|
| 825 |
return true; |
| 826 |
} |
| 827 |
|
| 828 |
$this->debug( $in, __FUNCTION__, false ); |
| 829 |
|
| 830 |
return false; |
| 831 |
} |
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
private function isSrational( $in ) { |
| 838 |
$m = []; |
| 839 |
|
| 840 |
|
| 841 |
if ( !is_array( $in ) && |
| 842 |
preg_match( '/^(-?\d+)\/(\d+[1-9]|[1-9]\d*)$/', $in, $m ) |
| 843 |
) { |
| 844 |
return $this->isSlong( $m[0] ) && $this->isSlong( $m[1] ); |
| 845 |
} |
| 846 |
|
| 847 |
$this->debug( $in, __FUNCTION__, 'fed a non-fraction value' ); |
| 848 |
|
| 849 |
return false; |
| 850 |
} |
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
private function validate( $section, $tag, $val, $recursive = false ): bool { |
| 862 |
$debug = "tag is '$tag'"; |
| 863 |
$etype = $this->mExifTags[$section][$tag]; |
| 864 |
$ecount = 1; |
| 865 |
if ( is_array( $etype ) ) { |
| 866 |
[ $etype, $ecount ] = $etype; |
| 867 |
if ( $recursive ) { |
| 868 |
|
| 869 |
$ecount = 1; |
| 870 |
} |
| 871 |
} |
| 872 |
|
| 873 |
$count = 1; |
| 874 |
if ( is_array( $val ) ) { |
| 875 |
$count = count( $val ); |
| 876 |
if ( $ecount !== $count ) { |
| 877 |
$this->debug( $val, __FUNCTION__, "Expected $ecount elements for $tag but got $count" ); |
| 878 |
return false; |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
if ( $count > 1 ) { |
| 883 |
foreach ( $val as $v ) { |
| 884 |
if ( !$this->validate( $section, $tag, $v, true ) ) { |
| 885 |
return false; |
| 886 |
} |
| 887 |
} |
| 888 |
|
| 889 |
return true; |
| 890 |
} |
| 891 |
|
| 892 |
|
| 893 |
if ( $val === null ) { |
| 894 |
return true; |
| 895 |
} |
| 896 |
|
| 897 |
|
| 898 |
switch ( (string)$etype ) { |
| 899 |
case (string)self::BYTE: |
| 900 |
$this->debug( $val, __FUNCTION__, $debug ); |
| 901 |
|
| 902 |
return $this->isByte( $val ); |
| 903 |
case (string)self::ASCII: |
| 904 |
$this->debug( $val, __FUNCTION__, $debug ); |
| 905 |
|
| 906 |
return $this->isASCII( $val ); |
| 907 |
case (string)self::SHORT: |
| 908 |
$this->debug( $val, __FUNCTION__, $debug ); |
| 909 |
|
| 910 |
return $this->isShort( $val ); |
| 911 |
case (string)self::LONG: |
| 912 |
$this->debug( $val, __FUNCTION__, $debug ); |
| 913 |
|
| 914 |
return $this->isLong( $val ); |
| 915 |
case (string)self::RATIONAL: |
| 916 |
$this->debug( $val, __FUNCTION__, $debug ); |
| 917 |
|
| 918 |
return $this->isRational( $val ); |
| 919 |
case (string)self::SHORT_OR_LONG: |
| 920 |
$this->debug( $val, __FUNCTION__, $debug ); |
| 921 |
|
| 922 |
return $this->isShort( $val ) || $this->isLong( $val ); |
| 923 |
case (string)self::UNDEFINED: |
| 924 |
$this->debug( $val, __FUNCTION__, $debug ); |
| 925 |
|
| 926 |
return $this->isUndefined( $val ); |
| 927 |
case (string)self::SLONG: |
| 928 |
$this->debug( $val, __FUNCTION__, $debug ); |
| 929 |
|
| 930 |
return $this->isSlong( $val ); |
| 931 |
case (string)self::SRATIONAL: |
| 932 |
$this->debug( $val, __FUNCTION__, $debug ); |
| 933 |
|
| 934 |
return $this->isSrational( $val ); |
| 935 |
case (string)self::IGNORE: |
| 936 |
$this->debug( $val, __FUNCTION__, $debug ); |
| 937 |
|
| 938 |
return false; |
| 939 |
default: |
| 940 |
$this->debug( $val, __FUNCTION__, "The tag '$tag' is unknown" ); |
| 941 |
|
| 942 |
return false; |
| 943 |
} |
| 944 |
} |
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
private function debug( $in, $fname, $action = null ) { |
| 954 |
if ( !$this->log ) { |
| 955 |
return; |
| 956 |
} |
| 957 |
$type = get_debug_type( $in ); |
| 958 |
$class = ucfirst( __CLASS__ ); |
| 959 |
if ( is_array( $in ) ) { |
| 960 |
$in = print_r( $in, true ); |
| 961 |
} |
| 962 |
|
| 963 |
if ( $action === true ) { |
| 964 |
wfDebugLog( $this->log, "$class::$fname: accepted: '$in' (type: $type)" ); |
| 965 |
} elseif ( $action === false ) { |
| 966 |
wfDebugLog( $this->log, "$class::$fname: rejected: '$in' (type: $type)" ); |
| 967 |
} elseif ( $action === null ) { |
| 968 |
wfDebugLog( $this->log, "$class::$fname: input was: '$in' (type: $type)" ); |
| 969 |
} else { |
| 970 |
wfDebugLog( $this->log, "$class::$fname: $action (type: $type; content: '$in')" ); |
| 971 |
} |
| 972 |
} |
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
private function debugFile( $fname, $io ) { |
| 981 |
if ( !$this->log ) { |
| 982 |
return; |
| 983 |
} |
| 984 |
$class = ucfirst( __CLASS__ ); |
| 985 |
if ( $io ) { |
| 986 |
wfDebugLog( $this->log, "$class::$fname: begin processing: '{$this->basename}'" ); |
| 987 |
} else { |
| 988 |
wfDebugLog( $this->log, "$class::$fname: end processing: '{$this->basename}'" ); |
| 989 |
} |
| 990 |
} |
| 991 |
} |