3 * Reader for XMP data containing properties relevant to images.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 use Psr\Log\LoggerAwareInterface
;
25 use Psr\Log\LoggerInterface
;
26 use Psr\Log\NullLogger
;
27 use Wikimedia\ScopedCallback
;
30 * Class for reading xmp data containing properties relevant to
31 * images, and spitting out an array that FormatMetadata accepts.
33 * Note, this is not meant to recognize every possible thing you can
34 * encode in XMP. It should recognize all the properties we want.
35 * For example it doesn't have support for structures with multiple
36 * nesting levels, as none of the properties we're supporting use that
37 * feature. If it comes across properties it doesn't recognize, it should
40 * The public methods one would call in this class are
42 * Reads in xmp content.
43 * Can potentially be called multiple times with partial data each time.
44 * - parseExtended( $content )
45 * Reads XMPExtended blocks (jpeg files only).
47 * Outputs a results array.
49 * Note XMP kind of looks like rdf. They are not the same thing - XMP is
50 * encoded as a specific subset of rdf. This class can read XMP. It cannot
53 class XMPReader
implements LoggerAwareInterface
{
54 /** @var array XMP item configuration array */
57 /** @var array Array to hold the current element (and previous element, and so on) */
58 private $curItem = [];
60 /** @var bool|string The structure name when processing nested structures. */
61 private $ancestorStruct = false;
63 /** @var bool|string Temporary holder for character data that appears in xmp doc. */
64 private $charContent = false;
66 /** @var array Stores the state the xmpreader is in (see MODE_FOO constants) */
69 /** @var array Array to hold results */
70 private $results = [];
72 /** @var bool If we're doing a seq or bag. */
73 private $processingArray = false;
75 /** @var bool|string Used for lang alts only */
76 private $itemLang = false;
78 /** @var resource A resource handle for the XML parser */
81 /** @var bool|string Character set like 'UTF-8' */
82 private $charset = false;
85 private $extendedXMPOffset = 0;
87 /** @var int Flag determining if the XMP is safe to parse **/
88 private $parsable = 0;
90 /** @var string Buffer of XML to parse **/
91 private $xmlParsableBuffer = '';
94 * These are various mode constants.
95 * they are used to figure out what to do
96 * with an element when its encountered.
98 * For example, MODE_IGNORE is used when processing
99 * a property we're not interested in. So if a new
100 * element pops up when we're in that mode, we ignore it.
102 const MODE_INITIAL
= 0;
103 const MODE_IGNORE
= 1;
105 const MODE_LI_LANG
= 3;
106 const MODE_QDESC
= 4;
108 // The following MODE constants are also used in the
109 // $items array to denote what type of property the item is.
110 const MODE_SIMPLE
= 10;
111 const MODE_STRUCT
= 11; // structure (associative array)
112 const MODE_SEQ
= 12; // ordered list
113 const MODE_BAG
= 13; // unordered list
114 const MODE_LANG
= 14;
115 const MODE_ALT
= 15; // non-language alt. Currently not implemented, and not needed atm.
116 const MODE_BAGSTRUCT
= 16; // A BAG of Structs.
118 const NS_RDF
= 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
119 const NS_XML
= 'http://www.w3.org/XML/1998/namespace';
121 // States used while determining if XML is safe to parse
122 const PARSABLE_UNKNOWN
= 0;
123 const PARSABLE_OK
= 1;
124 const PARSABLE_BUFFERING
= 2;
125 const PARSABLE_NO
= 3;
128 * @var LoggerInterface
138 * Primary job is to initialize the XMLParser
139 * @param LoggerInterface|null $logger
140 * @param string $filename
142 function __construct( LoggerInterface
$logger = null, $filename = 'unknown' ) {
143 if ( !function_exists( 'xml_parser_create_ns' ) ) {
144 // this should already be checked by this point
145 throw new RuntimeException( 'XMP support requires XML Parser' );
148 $this->setLogger( $logger );
150 $this->setLogger( new NullLogger() );
152 $this->filename
= $filename;
154 $this->items
= XMPInfo
::getItems();
156 $this->resetXMLParser();
159 public function setLogger( LoggerInterface
$logger ) {
160 $this->logger
= $logger;
164 * free the XML parser.
166 * @note It is unclear to me if we really need to do this ourselves
167 * or if php garbage collection will automatically free the xmlParser
168 * when it is no longer needed.
170 private function destroyXMLParser() {
171 if ( $this->xmlParser
) {
172 xml_parser_free( $this->xmlParser
);
173 $this->xmlParser
= null;
178 * Main use is if a single item has multiple xmp documents describing it.
179 * For example in jpeg's with extendedXMP
181 private function resetXMLParser() {
182 $this->destroyXMLParser();
184 $this->xmlParser
= xml_parser_create_ns( 'UTF-8', ' ' );
185 xml_parser_set_option( $this->xmlParser
, XML_OPTION_CASE_FOLDING
, 0 );
186 xml_parser_set_option( $this->xmlParser
, XML_OPTION_SKIP_WHITE
, 1 );
188 xml_set_element_handler( $this->xmlParser
,
189 [ $this, 'startElement' ],
190 [ $this, 'endElement' ] );
192 xml_set_character_data_handler( $this->xmlParser
, [ $this, 'char' ] );
194 $this->parsable
= self
::PARSABLE_UNKNOWN
;
195 $this->xmlParsableBuffer
= '';
199 * Check if this instance supports using this class
202 public static function isSupported() {
203 return function_exists( 'xml_parser_create_ns' ) && class_exists( 'XMLReader' );
206 /** Get the result array. Do some post-processing before returning
207 * the array, and transform any metadata that is special-cased.
209 * @return array Array of results as an array of arrays suitable for
210 * FormatMetadata::getFormattedData().
212 public function getResults() {
213 // xmp-special is for metadata that affects how stuff
214 // is extracted. For example xmpNote:HasExtendedXMP.
216 // It is also used to handle photoshop:AuthorsPosition
217 // which is weird and really part of another property,
218 // see 2:85 in IPTC. See also pg 21 of IPTC4XMP standard.
219 // The location fields also use it.
221 $data = $this->results
;
223 if ( isset( $data['xmp-special']['AuthorsPosition'] )
224 && is_string( $data['xmp-special']['AuthorsPosition'] )
225 && isset( $data['xmp-general']['Artist'][0] )
227 // Note, if there is more than one creator,
228 // this only applies to first. This also will
229 // only apply to the dc:Creator prop, not the
232 $data['xmp-general']['Artist'][0] =
233 $data['xmp-special']['AuthorsPosition'] . ', '
234 . $data['xmp-general']['Artist'][0];
237 // Go through the LocationShown and LocationCreated
238 // changing it to the non-hierarchal form used by
239 // the other location fields.
241 if ( isset( $data['xmp-special']['LocationShown'][0] )
242 && is_array( $data['xmp-special']['LocationShown'][0] )
244 // the is_array is just paranoia. It should always
246 foreach ( $data['xmp-special']['LocationShown'] as $loc ) {
247 if ( !is_array( $loc ) ) {
248 // To avoid copying over the _type meta-fields.
251 foreach ( $loc as $field => $val ) {
252 $data['xmp-general'][$field . 'Dest'][] = $val;
256 if ( isset( $data['xmp-special']['LocationCreated'][0] )
257 && is_array( $data['xmp-special']['LocationCreated'][0] )
259 // the is_array is just paranoia. It should always
261 foreach ( $data['xmp-special']['LocationCreated'] as $loc ) {
262 if ( !is_array( $loc ) ) {
263 // To avoid copying over the _type meta-fields.
266 foreach ( $loc as $field => $val ) {
267 $data['xmp-general'][$field . 'Created'][] = $val;
272 // We don't want to return the special values, since they're
273 // special and not info to be stored about the file.
274 unset( $data['xmp-special'] );
276 // Convert GPSAltitude to negative if below sea level.
277 if ( isset( $data['xmp-exif']['GPSAltitudeRef'] )
278 && isset( $data['xmp-exif']['GPSAltitude'] )
280 // Must convert to a real before multiplying by -1
281 // XMPValidate guarantees there will always be a '/' in this value.
282 list( $nom, $denom ) = explode( '/', $data['xmp-exif']['GPSAltitude'] );
283 $data['xmp-exif']['GPSAltitude'] = $nom / $denom;
285 if ( $data['xmp-exif']['GPSAltitudeRef'] == '1' ) {
286 $data['xmp-exif']['GPSAltitude'] *= -1;
288 unset( $data['xmp-exif']['GPSAltitudeRef'] );
295 * Main function to call to parse XMP. Use getResults to
298 * Also catches any errors during processing, writes them to
299 * debug log, blanks result array and returns false.
301 * @param string $content XMP data
302 * @param bool $allOfIt If this is all the data (true) or if its split up (false). Default true
303 * @throws RuntimeException
304 * @return bool Success.
306 public function parse( $content, $allOfIt = true ) {
307 if ( !$this->xmlParser
) {
308 $this->resetXMLParser();
312 // detect encoding by looking for BOM which is supposed to be in processing instruction.
313 // see page 12 of http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart3.pdf
314 if ( !$this->charset
) {
316 if ( preg_match( '/\xEF\xBB\xBF|\xFE\xFF|\x00\x00\xFE\xFF|\xFF\xFE\x00\x00|\xFF\xFE/',
321 $this->charset
= 'UTF-16BE';
324 $this->charset
= 'UTF-16LE';
326 case "\x00\x00\xFE\xFF":
327 $this->charset
= 'UTF-32BE';
329 case "\xFF\xFE\x00\x00":
330 $this->charset
= 'UTF-32LE';
333 $this->charset
= 'UTF-8';
336 // this should be impossible to get to
337 throw new RuntimeException( "Invalid BOM" );
340 // standard specifically says, if no bom assume utf-8
341 $this->charset
= 'UTF-8';
344 if ( $this->charset
!== 'UTF-8' ) {
345 // don't convert if already utf-8
346 MediaWiki\
suppressWarnings();
347 $content = iconv( $this->charset
, 'UTF-8//IGNORE', $content );
348 MediaWiki\restoreWarnings
();
351 // Ensure the XMP block does not have an xml doctype declaration, which
352 // could declare entities unsafe to parse with xml_parse (T85848/T71210).
353 if ( $this->parsable
!== self
::PARSABLE_OK
) {
354 if ( $this->parsable
=== self
::PARSABLE_NO
) {
355 throw new RuntimeException( 'Unsafe doctype declaration in XML.' );
358 $content = $this->xmlParsableBuffer
. $content;
359 if ( !$this->checkParseSafety( $content ) ) {
360 if ( !$allOfIt && $this->parsable
!== self
::PARSABLE_NO
) {
361 // parse wasn't Unsuccessful yet, so return true
365 $msg = ( $this->parsable
=== self
::PARSABLE_NO
) ?
366 'Unsafe doctype declaration in XML.' :
367 'No root element found in XML.';
368 throw new RuntimeException( $msg );
372 $ok = xml_parse( $this->xmlParser
, $content, $allOfIt );
374 $code = xml_get_error_code( $this->xmlParser
);
375 $error = xml_error_string( $code );
376 $line = xml_get_current_line_number( $this->xmlParser
);
377 $col = xml_get_current_column_number( $this->xmlParser
);
378 $offset = xml_get_current_byte_index( $this->xmlParser
);
381 '{method} : Error reading XMP content: {error} ' .
382 '(file: {file}, line: {line} column: {column} ' .
383 'byte offset: {offset})',
385 'method' => __METHOD__
,
386 'error_code' => $code,
388 'file' => $this->filename
,
392 'content' => $content,
394 $this->results
= []; // blank if error.
395 $this->destroyXMLParser();
398 } catch ( Exception
$e ) {
399 $this->logger
->warning(
400 '{method} {exception}',
402 'method' => __METHOD__
,
404 'file' => $this->filename
,
405 'content' => $content,
412 $this->destroyXMLParser();
418 /** Entry point for XMPExtended blocks in jpeg files
420 * @todo In serious need of testing
421 * @see http://www.adobe.ge/devnet/xmp/pdfs/XMPSpecificationPart3.pdf XMP spec part 3 page 20
422 * @param string $content XMPExtended block minus the namespace signature
423 * @return bool If it succeeded.
425 public function parseExtended( $content ) {
426 // @todo FIXME: This is untested. Hard to find example files
427 // or programs that make such files..
428 $guid = substr( $content, 0, 32 );
429 if ( !isset( $this->results
['xmp-special']['HasExtendedXMP'] )
430 ||
$this->results
['xmp-special']['HasExtendedXMP'] !== $guid
432 $this->logger
->info( __METHOD__
.
433 " Ignoring XMPExtended block due to wrong guid (guid= '{guid}')",
436 'file' => $this->filename
,
442 $len = unpack( 'Nlength/Noffset', substr( $content, 32, 8 ) );
445 $len['length'] < 4 ||
446 $len['offset'] < 0 ||
447 $len['offset'] > $len['length']
450 __METHOD__
. 'Error reading extended XMP block, invalid length or offset.',
451 [ 'file' => $this->filename
]
457 // we're not very robust here. we should accept it in the wrong order.
458 // To quote the XMP standard:
459 // "A JPEG writer should write the ExtendedXMP marker segments in order,
460 // immediately following the StandardXMP. However, the JPEG standard
461 // does not require preservation of marker segment order. A robust JPEG
462 // reader should tolerate the marker segments in any order."
463 // On the other hand, the probability that an image will have more than
464 // 128k of metadata is rather low... so the probability that it will have
465 // > 128k, and be in the wrong order is very low...
467 if ( $len['offset'] !== $this->extendedXMPOffset
) {
468 $this->logger
->info( __METHOD__
. 'Ignoring XMPExtended block due to wrong order. (Offset was '
469 . $len['offset'] . ' but expected ' . $this->extendedXMPOffset
. ')',
470 [ 'file' => $this->filename
]
476 if ( $len['offset'] === 0 ) {
477 // if we're starting the extended block, we've probably already
478 // done the XMPStandard block, so reset.
479 $this->resetXMLParser();
482 $this->extendedXMPOffset +
= $len['length'];
484 $actualContent = substr( $content, 40 );
486 if ( $this->extendedXMPOffset
=== strlen( $actualContent ) ) {
492 $this->logger
->debug(
493 __METHOD__
. 'Parsing a XMPExtended block',
494 [ 'file' => $this->filename
]
497 return $this->parse( $actualContent, $atEnd );
501 * Character data handler
502 * Called whenever character data is found in the xmp document.
504 * does nothing if we're in MODE_IGNORE or if the data is whitespace
505 * throws an error if we're not in MODE_SIMPLE (as we're not allowed to have character
506 * data in the other modes).
508 * As an example, this happens when we encounter XMP like:
509 * <exif:DigitalZoomRatio>0/10</exif:DigitalZoomRatio>
510 * and are processing the 0/10 bit.
512 * @param resource $parser XMLParser reference to the xml parser
513 * @param string $data Character data
514 * @throws RuntimeException On invalid data
516 function char( $parser, $data ) {
517 $data = trim( $data );
518 if ( trim( $data ) === "" ) {
522 if ( !isset( $this->mode
[0] ) ) {
523 throw new RuntimeException( 'Unexpected character data before first rdf:Description element' );
526 if ( $this->mode
[0] === self
::MODE_IGNORE
) {
530 if ( $this->mode
[0] !== self
::MODE_SIMPLE
531 && $this->mode
[0] !== self
::MODE_QDESC
533 throw new RuntimeException( 'character data where not expected. (mode ' . $this->mode
[0] . ')' );
536 // to check, how does this handle w.s.
537 if ( $this->charContent
=== false ) {
538 $this->charContent
= $data;
540 $this->charContent
.= $data;
545 * Check if a block of XML is safe to pass to xml_parse, i.e. doesn't
546 * contain a doctype declaration which could contain a dos attack if we
547 * parse it and expand internal entities (T85848).
549 * @param string $content xml string to check for parse safety
550 * @return bool true if the xml is safe to parse, false otherwise
552 private function checkParseSafety( $content ) {
553 $reader = new XMLReader();
556 // For XMLReader to parse incomplete/invalid XML, it has to be open()'ed
557 // instead of using XML().
559 'data://text/plain,' . urlencode( $content ),
561 LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_NONET
564 $oldDisable = libxml_disable_entity_loader( true );
565 /** @noinspection PhpUnusedLocalVariableInspection */
566 $reset = new ScopedCallback(
567 'libxml_disable_entity_loader',
570 $reader->setParserProperty( XMLReader
::SUBST_ENTITIES
, false );
572 // Even with LIBXML_NOWARNING set, XMLReader::read gives a warning
573 // when parsing truncated XML, which causes unit tests to fail.
574 MediaWiki\
suppressWarnings();
575 while ( $reader->read() ) {
576 if ( $reader->nodeType
=== XMLReader
::ELEMENT
) {
577 // Reached the first element without hitting a doctype declaration
578 $this->parsable
= self
::PARSABLE_OK
;
582 if ( $reader->nodeType
=== XMLReader
::DOC_TYPE
) {
583 $this->parsable
= self
::PARSABLE_NO
;
588 MediaWiki\restoreWarnings
();
590 if ( !is_null( $result ) ) {
594 // Reached the end of the parsable xml without finding an element
595 // or doctype. Buffer and try again.
596 $this->parsable
= self
::PARSABLE_BUFFERING
;
597 $this->xmlParsableBuffer
= $content;
601 /** When we hit a closing element in MODE_IGNORE
602 * Check to see if this is the element we started to ignore,
603 * in which case we get out of MODE_IGNORE
605 * @param string $elm Namespace of element followed by a space and then tag name of element.
607 private function endElementModeIgnore( $elm ) {
608 if ( $this->curItem
[0] === $elm ) {
609 array_shift( $this->curItem
);
610 array_shift( $this->mode
);
615 * Hit a closing element when in MODE_SIMPLE.
616 * This generally means that we finished processing a
617 * property value, and now have to save the result to the
620 * For example, when processing:
621 * <exif:DigitalZoomRatio>0/10</exif:DigitalZoomRatio>
622 * this deals with when we hit </exif:DigitalZoomRatio>.
624 * Or it could be if we hit the end element of a property
625 * of a compound data structure (like a member of an array).
627 * @param string $elm Namespace, space, and tag name.
629 private function endElementModeSimple( $elm ) {
630 if ( $this->charContent
!== false ) {
631 if ( $this->processingArray
) {
632 // if we're processing an array, use the original element
633 // name instead of rdf:li.
634 list( $ns, $tag ) = explode( ' ', $this->curItem
[0], 2 );
636 list( $ns, $tag ) = explode( ' ', $elm, 2 );
638 $this->saveValue( $ns, $tag, $this->charContent
);
640 $this->charContent
= false; // reset
642 array_shift( $this->curItem
);
643 array_shift( $this->mode
);
647 * Hit a closing element in MODE_STRUCT, MODE_SEQ, MODE_BAG
648 * generally means we've finished processing a nested structure.
649 * resets some internal variables to indicate that.
651 * Note this means we hit the closing element not the "</rdf:Seq>".
653 * @par For example, when processing:
655 * <exif:ISOSpeedRatings> <rdf:Seq> <rdf:li>64</rdf:li>
656 * </rdf:Seq> </exif:ISOSpeedRatings>
659 * This method is called when we hit the "</exif:ISOSpeedRatings>" tag.
661 * @param string $elm Namespace . space . tag name.
662 * @throws RuntimeException
664 private function endElementNested( $elm ) {
665 /* cur item must be the same as $elm, unless if in MODE_STRUCT
666 * in which case it could also be rdf:Description */
667 if ( $this->curItem
[0] !== $elm
668 && !( $elm === self
::NS_RDF
. ' Description'
669 && $this->mode
[0] === self
::MODE_STRUCT
)
671 throw new RuntimeException( "nesting mismatch. got a </$elm> but expected a </" .
672 $this->curItem
[0] . '>' );
675 // Validate structures.
676 list( $ns, $tag ) = explode( ' ', $elm, 2 );
677 if ( isset( $this->items
[$ns][$tag]['validate'] ) ) {
678 $info =& $this->items
[$ns][$tag];
679 $finalName = isset( $info['map_name'] )
680 ?
$info['map_name'] : $tag;
682 if ( is_array( $info['validate'] ) ) {
683 $validate = $info['validate'];
685 $validator = new XMPValidate( $this->logger
);
686 $validate = [ $validator, $info['validate'] ];
689 if ( !isset( $this->results
['xmp-' . $info['map_group']][$finalName] ) ) {
690 // This can happen if all the members of the struct failed validation.
691 $this->logger
->debug(
692 __METHOD__
. " <$ns:$tag> has no valid members.",
693 [ 'file' => $this->filename
]
695 } elseif ( is_callable( $validate ) ) {
696 $val =& $this->results
['xmp-' . $info['map_group']][$finalName];
697 call_user_func_array( $validate, [ $info, &$val, false ] );
698 if ( is_null( $val ) ) {
699 // the idea being the validation function will unset the variable if
702 __METHOD__
. " <$ns:$tag> failed validation.",
703 [ 'file' => $this->filename
]
705 unset( $this->results
['xmp-' . $info['map_group']][$finalName] );
708 $this->logger
->warning(
709 __METHOD__
. " Validation function for $finalName (" .
710 $validate[0] . '::' . $validate[1] . '()) is not callable.',
711 [ 'file' => $this->filename
]
716 array_shift( $this->curItem
);
717 array_shift( $this->mode
);
718 $this->ancestorStruct
= false;
719 $this->processingArray
= false;
720 $this->itemLang
= false;
724 * Hit a closing element in MODE_LI (either rdf:Seq, or rdf:Bag )
725 * Add information about what type of element this is.
727 * Note we still have to hit the outer "</property>"
729 * @par For example, when processing:
731 * <exif:ISOSpeedRatings> <rdf:Seq> <rdf:li>64</rdf:li>
732 * </rdf:Seq> </exif:ISOSpeedRatings>
735 * This method is called when we hit the "</rdf:Seq>".
736 * (For comparison, we call endElementModeSimple when we
737 * hit the "</rdf:li>")
739 * @param string $elm Namespace . ' ' . element name
740 * @throws RuntimeException
742 private function endElementModeLi( $elm ) {
743 list( $ns, $tag ) = explode( ' ', $this->curItem
[0], 2 );
744 $info = $this->items
[$ns][$tag];
745 $finalName = isset( $info['map_name'] )
746 ?
$info['map_name'] : $tag;
748 array_shift( $this->mode
);
750 if ( !isset( $this->results
['xmp-' . $info['map_group']][$finalName] ) ) {
751 $this->logger
->debug(
752 __METHOD__
. " Empty compund element $finalName.",
753 [ 'file' => $this->filename
]
759 if ( $elm === self
::NS_RDF
. ' Seq' ) {
760 $this->results
['xmp-' . $info['map_group']][$finalName]['_type'] = 'ol';
761 } elseif ( $elm === self
::NS_RDF
. ' Bag' ) {
762 $this->results
['xmp-' . $info['map_group']][$finalName]['_type'] = 'ul';
763 } elseif ( $elm === self
::NS_RDF
. ' Alt' ) {
764 // extra if needed as you could theoretically have a non-language alt.
765 if ( $info['mode'] === self
::MODE_LANG
) {
766 $this->results
['xmp-' . $info['map_group']][$finalName]['_type'] = 'lang';
769 throw new RuntimeException(
770 __METHOD__
. " expected </rdf:seq> or </rdf:bag> but instead got $elm."
776 * End element while in MODE_QDESC
777 * mostly when ending an element when we have a simple value
778 * that has qualifiers.
780 * Qualifiers aren't all that common, and we don't do anything
783 * @param string $elm Namespace and element
785 private function endElementModeQDesc( $elm ) {
786 if ( $elm === self
::NS_RDF
. ' value' ) {
787 list( $ns, $tag ) = explode( ' ', $this->curItem
[0], 2 );
788 $this->saveValue( $ns, $tag, $this->charContent
);
792 array_shift( $this->mode
);
793 array_shift( $this->curItem
);
798 * Handler for hitting a closing element.
800 * generally just calls a helper function depending on what
803 * Ignores the outer wrapping elements that are optional in
804 * xmp and have no meaning.
806 * @param resource $parser
807 * @param string $elm Namespace . ' ' . element name
808 * @throws RuntimeException
810 function endElement( $parser, $elm ) {
811 if ( $elm === ( self
::NS_RDF
. ' RDF' )
812 ||
$elm === 'adobe:ns:meta/ xmpmeta'
813 ||
$elm === 'adobe:ns:meta/ xapmeta'
819 if ( $elm === self
::NS_RDF
. ' type' ) {
820 // these aren't really supported properly yet.
821 // However, it appears they almost never used.
823 __METHOD__
. ' encountered <rdf:type>',
824 [ 'file' => $this->filename
]
828 if ( strpos( $elm, ' ' ) === false ) {
829 // This probably shouldn't happen.
830 // However, there is a bug in an adobe product
831 // that forgets the namespace on some things.
832 // (Luckily they are unimportant things).
834 __METHOD__
. " Encountered </$elm> which has no namespace. Skipping.",
835 [ 'file' => $this->filename
]
841 if ( count( $this->mode
[0] ) === 0 ) {
842 // This should never ever happen and means
843 // there is a pretty major bug in this class.
844 throw new RuntimeException( 'Encountered end element with no mode' );
847 if ( count( $this->curItem
) == 0 && $this->mode
[0] !== self
::MODE_INITIAL
) {
848 // just to be paranoid. Should always have a curItem, except for initially
849 // (aka during MODE_INITAL).
850 throw new RuntimeException( "Hit end element </$elm> but no curItem" );
853 switch ( $this->mode
[0] ) {
854 case self
::MODE_IGNORE
:
855 $this->endElementModeIgnore( $elm );
857 case self
::MODE_SIMPLE
:
858 $this->endElementModeSimple( $elm );
860 case self
::MODE_STRUCT
:
863 case self
::MODE_LANG
:
864 case self
::MODE_BAGSTRUCT
:
865 $this->endElementNested( $elm );
867 case self
::MODE_INITIAL
:
868 if ( $elm === self
::NS_RDF
. ' Description' ) {
869 array_shift( $this->mode
);
871 throw new RuntimeException( 'Element ended unexpectedly while in MODE_INITIAL' );
875 case self
::MODE_LI_LANG
:
876 $this->endElementModeLi( $elm );
878 case self
::MODE_QDESC
:
879 $this->endElementModeQDesc( $elm );
883 __METHOD__
. " no mode (elm = $elm)",
884 [ 'file' => $this->filename
]
891 * Hit an opening element while in MODE_IGNORE
893 * XMP is extensible, so ignore any tag we don't understand.
895 * Mostly ignores, unless we encounter the element that we are ignoring.
896 * in which case we add it to the item stack, so we can ignore things
897 * that are nested, correctly.
899 * @param string $elm Namespace . ' ' . tag name
901 private function startElementModeIgnore( $elm ) {
902 if ( $elm === $this->curItem
[0] ) {
903 array_unshift( $this->curItem
, $elm );
904 array_unshift( $this->mode
, self
::MODE_IGNORE
);
909 * Start element in MODE_BAG (unordered array)
910 * this should always be <rdf:Bag>
912 * @param string $elm Namespace . ' ' . tag
913 * @throws RuntimeException If we have an element that's not <rdf:Bag>
915 private function startElementModeBag( $elm ) {
916 if ( $elm === self
::NS_RDF
. ' Bag' ) {
917 array_unshift( $this->mode
, self
::MODE_LI
);
919 throw new RuntimeException( "Expected <rdf:Bag> but got $elm." );
924 * Start element in MODE_SEQ (ordered array)
925 * this should always be <rdf:Seq>
927 * @param string $elm Namespace . ' ' . tag
928 * @throws RuntimeException If we have an element that's not <rdf:Seq>
930 private function startElementModeSeq( $elm ) {
931 if ( $elm === self
::NS_RDF
. ' Seq' ) {
932 array_unshift( $this->mode
, self
::MODE_LI
);
933 } elseif ( $elm === self
::NS_RDF
. ' Bag' ) {
936 __METHOD__
. ' Expected an rdf:Seq, but got an rdf:Bag. Pretending' .
937 ' it is a Seq, since some buggy software is known to screw this up.',
938 [ 'file' => $this->filename
]
940 array_unshift( $this->mode
, self
::MODE_LI
);
942 throw new RuntimeException( "Expected <rdf:Seq> but got $elm." );
947 * Start element in MODE_LANG (language alternative)
948 * this should always be <rdf:Alt>
950 * This tag tends to be used for metadata like describe this
951 * picture, which can be translated into multiple languages.
953 * XMP supports non-linguistic alternative selections,
954 * which are really only used for thumbnails, which
955 * we don't care about.
957 * @param string $elm Namespace . ' ' . tag
958 * @throws RuntimeException If we have an element that's not <rdf:Alt>
960 private function startElementModeLang( $elm ) {
961 if ( $elm === self
::NS_RDF
. ' Alt' ) {
962 array_unshift( $this->mode
, self
::MODE_LI_LANG
);
964 throw new RuntimeException( "Expected <rdf:Seq> but got $elm." );
969 * Handle an opening element when in MODE_SIMPLE
971 * This should not happen often. This is for if a simple element
972 * already opened has a child element. Could happen for a
976 * <exif:DigitalZoomRatio><rdf:Description><rdf:value>0/10</rdf:value>
977 * <foo:someQualifier>Bar</foo:someQualifier> </rdf:Description>
978 * </exif:DigitalZoomRatio>
980 * This method is called when processing the <rdf:Description> element
982 * @param string $elm Namespace and tag names separated by space.
983 * @param array $attribs Attributes of the element.
984 * @throws RuntimeException
986 private function startElementModeSimple( $elm, $attribs ) {
987 if ( $elm === self
::NS_RDF
. ' Description' ) {
988 // If this value has qualifiers
989 array_unshift( $this->mode
, self
::MODE_QDESC
);
990 array_unshift( $this->curItem
, $this->curItem
[0] );
992 if ( isset( $attribs[self
::NS_RDF
. ' value'] ) ) {
993 list( $ns, $tag ) = explode( ' ', $this->curItem
[0], 2 );
994 $this->saveValue( $ns, $tag, $attribs[self
::NS_RDF
. ' value'] );
996 } elseif ( $elm === self
::NS_RDF
. ' value' ) {
997 // This should not be here.
998 throw new RuntimeException( __METHOD__
. ' Encountered <rdf:value> where it was unexpected.' );
1000 // something else we don't recognize, like a qualifier maybe.
1001 $this->logger
->info( __METHOD__
.
1002 " Encountered element <{element}> where only expecting character data as value of {curitem}",
1005 'curitem' => $this->curItem
[0],
1006 'file' => $this->filename
,
1009 array_unshift( $this->mode
, self
::MODE_IGNORE
);
1010 array_unshift( $this->curItem
, $elm );
1015 * Start an element when in MODE_QDESC.
1016 * This generally happens when a simple element has an inner
1017 * rdf:Description to hold qualifier elements.
1020 * <exif:DigitalZoomRatio><rdf:Description><rdf:value>0/10</rdf:value>
1021 * <foo:someQualifier>Bar</foo:someQualifier> </rdf:Description>
1022 * </exif:DigitalZoomRatio>
1023 * Called when processing the <rdf:value> or <foo:someQualifier>.
1025 * @param string $elm Namespace and tag name separated by a space.
1027 private function startElementModeQDesc( $elm ) {
1028 if ( $elm === self
::NS_RDF
. ' value' ) {
1029 return; // do nothing
1031 // otherwise its a qualifier, which we ignore
1032 array_unshift( $this->mode
, self
::MODE_IGNORE
);
1033 array_unshift( $this->curItem
, $elm );
1038 * Starting an element when in MODE_INITIAL
1039 * This usually happens when we hit an element inside
1040 * the outer rdf:Description
1042 * This is generally where most properties start.
1044 * @param string $ns Namespace
1045 * @param string $tag Tag name (without namespace prefix)
1046 * @param array $attribs Array of attributes
1047 * @throws RuntimeException
1049 private function startElementModeInitial( $ns, $tag, $attribs ) {
1050 if ( $ns !== self
::NS_RDF
) {
1051 if ( isset( $this->items
[$ns][$tag] ) ) {
1052 if ( isset( $this->items
[$ns][$tag]['structPart'] ) ) {
1053 // If this element is supposed to appear only as
1054 // a child of a structure, but appears here (not as
1055 // a child of a struct), then something weird is
1056 // happening, so ignore this element and its children.
1058 $this->logger
->info(
1059 'Encountered <{element}> outside of its expected parent. Ignoring.',
1060 [ 'element' => "$ns:$tag", 'file' => $this->filename
]
1063 array_unshift( $this->mode
, self
::MODE_IGNORE
);
1064 array_unshift( $this->curItem
, $ns . ' ' . $tag );
1068 $mode = $this->items
[$ns][$tag]['mode'];
1069 array_unshift( $this->mode
, $mode );
1070 array_unshift( $this->curItem
, $ns . ' ' . $tag );
1071 if ( $mode === self
::MODE_STRUCT
) {
1072 $this->ancestorStruct
= isset( $this->items
[$ns][$tag]['map_name'] )
1073 ?
$this->items
[$ns][$tag]['map_name'] : $tag;
1075 if ( $this->charContent
!== false ) {
1077 // Should not happen in valid XMP.
1078 throw new RuntimeException( 'tag nested in non-whitespace characters.' );
1081 // This element is not on our list of allowed elements so ignore.
1082 $this->logger
->debug( __METHOD__
. ' Ignoring unrecognized element <{element}>.',
1083 [ 'element' => "$ns:$tag", 'file' => $this->filename
] );
1084 array_unshift( $this->mode
, self
::MODE_IGNORE
);
1085 array_unshift( $this->curItem
, $ns . ' ' . $tag );
1090 // process attributes
1091 $this->doAttribs( $attribs );
1095 * Hit an opening element when in a Struct (MODE_STRUCT)
1096 * This is generally for fields of a compound property.
1098 * Example of a struct (abbreviated; flash has more properties):
1100 * <exif:Flash> <rdf:Description> <exif:Fired>True</exif:Fired>
1101 * <exif:Mode>1</exif:Mode></rdf:Description></exif:Flash>
1105 * <exif:Flash rdf:parseType='Resource'> <exif:Fired>True</exif:Fired>
1106 * <exif:Mode>1</exif:Mode></exif:Flash>
1108 * @param string $ns Namespace
1109 * @param string $tag Tag name (no ns)
1110 * @param array $attribs Array of attribs w/ values.
1111 * @throws RuntimeException
1113 private function startElementModeStruct( $ns, $tag, $attribs ) {
1114 if ( $ns !== self
::NS_RDF
) {
1115 if ( isset( $this->items
[$ns][$tag] ) ) {
1116 if ( isset( $this->items
[$ns][$this->ancestorStruct
]['children'] )
1117 && !isset( $this->items
[$ns][$this->ancestorStruct
]['children'][$tag] )
1119 // This assumes that we don't have inter-namespace nesting
1120 // which we don't in all the properties we're interested in.
1121 throw new RuntimeException( " <$tag> appeared nested in <" . $this->ancestorStruct
1122 . "> where it is not allowed." );
1124 array_unshift( $this->mode
, $this->items
[$ns][$tag]['mode'] );
1125 array_unshift( $this->curItem
, $ns . ' ' . $tag );
1126 if ( $this->charContent
!== false ) {
1128 // Should not happen in valid XMP.
1129 throw new RuntimeException( "tag <$tag> nested in non-whitespace characters (" .
1130 $this->charContent
. ")." );
1133 array_unshift( $this->mode
, self
::MODE_IGNORE
);
1134 array_unshift( $this->curItem
, $ns . ' ' . $tag );
1140 if ( $ns === self
::NS_RDF
&& $tag === 'Description' ) {
1141 $this->doAttribs( $attribs );
1142 array_unshift( $this->mode
, self
::MODE_STRUCT
);
1143 array_unshift( $this->curItem
, $this->curItem
[0] );
1148 * opening element in MODE_LI
1149 * process elements of arrays.
1152 * <exif:ISOSpeedRatings> <rdf:Seq> <rdf:li>64</rdf:li>
1153 * </rdf:Seq> </exif:ISOSpeedRatings>
1154 * This method is called when we hit the <rdf:li> element.
1156 * @param string $elm Namespace . ' ' . tagname
1157 * @param array $attribs Attributes. (needed for BAGSTRUCTS)
1158 * @throws RuntimeException If gets a tag other than <rdf:li>
1160 private function startElementModeLi( $elm, $attribs ) {
1161 if ( ( $elm ) !== self
::NS_RDF
. ' li' ) {
1162 throw new RuntimeException( "<rdf:li> expected but got $elm." );
1165 if ( !isset( $this->mode
[1] ) ) {
1166 // This should never ever ever happen. Checking for it
1168 throw new RuntimeException( 'In mode Li, but no 2xPrevious mode!' );
1171 if ( $this->mode
[1] === self
::MODE_BAGSTRUCT
) {
1172 // This list item contains a compound (STRUCT) value.
1173 array_unshift( $this->mode
, self
::MODE_STRUCT
);
1174 array_unshift( $this->curItem
, $elm );
1175 $this->processingArray
= true;
1177 if ( !isset( $this->curItem
[1] ) ) {
1179 throw new RuntimeException( 'Can not find parent of BAGSTRUCT.' );
1181 list( $curNS, $curTag ) = explode( ' ', $this->curItem
[1] );
1182 $this->ancestorStruct
= isset( $this->items
[$curNS][$curTag]['map_name'] )
1183 ?
$this->items
[$curNS][$curTag]['map_name'] : $curTag;
1185 $this->doAttribs( $attribs );
1187 // Normal BAG or SEQ containing simple values.
1188 array_unshift( $this->mode
, self
::MODE_SIMPLE
);
1189 // need to add curItem[0] on again since one is for the specific item
1190 // and one is for the entire group.
1191 array_unshift( $this->curItem
, $this->curItem
[0] );
1192 $this->processingArray
= true;
1197 * Opening element in MODE_LI_LANG.
1198 * process elements of language alternatives
1201 * <dc:title> <rdf:Alt> <rdf:li xml:lang="x-default">My house
1202 * </rdf:li> </rdf:Alt> </dc:title>
1204 * This method is called when we hit the <rdf:li> element.
1206 * @param string $elm Namespace . ' ' . tag
1207 * @param array $attribs Array of elements (most importantly xml:lang)
1208 * @throws RuntimeException If gets a tag other than <rdf:li> or if no xml:lang
1210 private function startElementModeLiLang( $elm, $attribs ) {
1211 if ( $elm !== self
::NS_RDF
. ' li' ) {
1212 throw new RuntimeException( __METHOD__
. " <rdf:li> expected but got $elm." );
1214 if ( !isset( $attribs[self
::NS_XML
. ' lang'] )
1215 ||
!preg_match( '/^[-A-Za-z0-9]{2,}$/D', $attribs[self
::NS_XML
. ' lang'] )
1217 throw new RuntimeException( __METHOD__
1218 . " <rdf:li> did not contain, or has invalid xml:lang attribute in lang alternative" );
1221 // Lang is case-insensitive.
1222 $this->itemLang
= strtolower( $attribs[self
::NS_XML
. ' lang'] );
1224 // need to add curItem[0] on again since one is for the specific item
1225 // and one is for the entire group.
1226 array_unshift( $this->curItem
, $this->curItem
[0] );
1227 array_unshift( $this->mode
, self
::MODE_SIMPLE
);
1228 $this->processingArray
= true;
1232 * Hits an opening element.
1233 * Generally just calls a helper based on what MODE we're in.
1234 * Also does some initial set up for the wrapper element
1236 * @param resource $parser
1237 * @param string $elm Namespace "<space>" element
1238 * @param array $attribs Attribute name => value
1239 * @throws RuntimeException
1241 function startElement( $parser, $elm, $attribs ) {
1242 if ( $elm === self
::NS_RDF
. ' RDF'
1243 ||
$elm === 'adobe:ns:meta/ xmpmeta'
1244 ||
$elm === 'adobe:ns:meta/ xapmeta'
1248 } elseif ( $elm === self
::NS_RDF
. ' Description' ) {
1249 if ( count( $this->mode
) === 0 ) {
1251 array_unshift( $this->mode
, self
::MODE_INITIAL
);
1253 } elseif ( $elm === self
::NS_RDF
. ' type' ) {
1254 // This doesn't support rdf:type properly.
1255 // In practise I have yet to see a file that
1256 // uses this element, however it is mentioned
1257 // on page 25 of part 1 of the xmp standard.
1258 // Also it seems as if exiv2 and exiftool do not support
1259 // this either (That or I misunderstand the standard)
1260 $this->logger
->info(
1261 __METHOD__
. ' Encountered <rdf:type> which isn\'t currently supported',
1262 [ 'file' => $this->filename
]
1266 if ( strpos( $elm, ' ' ) === false ) {
1267 // This probably shouldn't happen.
1268 $this->logger
->info(
1269 __METHOD__
. " Encountered <$elm> which has no namespace. Skipping.",
1270 [ 'file' => $this->filename
]
1276 list( $ns, $tag ) = explode( ' ', $elm, 2 );
1278 if ( count( $this->mode
) === 0 ) {
1279 // This should not happen.
1280 throw new RuntimeException( 'Error extracting XMP, '
1281 . "encountered <$elm> with no mode" );
1284 switch ( $this->mode
[0] ) {
1285 case self
::MODE_IGNORE
:
1286 $this->startElementModeIgnore( $elm );
1288 case self
::MODE_SIMPLE
:
1289 $this->startElementModeSimple( $elm, $attribs );
1291 case self
::MODE_INITIAL
:
1292 $this->startElementModeInitial( $ns, $tag, $attribs );
1294 case self
::MODE_STRUCT
:
1295 $this->startElementModeStruct( $ns, $tag, $attribs );
1297 case self
::MODE_BAG
:
1298 case self
::MODE_BAGSTRUCT
:
1299 $this->startElementModeBag( $elm );
1301 case self
::MODE_SEQ
:
1302 $this->startElementModeSeq( $elm );
1304 case self
::MODE_LANG
:
1305 $this->startElementModeLang( $elm );
1307 case self
::MODE_LI_LANG
:
1308 $this->startElementModeLiLang( $elm, $attribs );
1311 $this->startElementModeLi( $elm, $attribs );
1313 case self
::MODE_QDESC
:
1314 $this->startElementModeQDesc( $elm );
1317 throw new RuntimeException( 'StartElement in unknown mode: ' . $this->mode
[0] );
1321 // phpcs:disable Generic.Files.LineLength
1323 * Process attributes.
1324 * Simple values can be stored as either a tag or attribute
1326 * Often the initial "<rdf:Description>" tag just has all the simple
1327 * properties as attributes.
1331 * <rdf:Description rdf:about="" xmlns:exif="http://ns.adobe.com/exif/1.0/" exif:DigitalZoomRatio="0/10">
1334 * @param array $attribs Array attribute=>value
1335 * @throws RuntimeException
1338 private function doAttribs( $attribs ) {
1339 // first check for rdf:parseType attribute, as that can change
1340 // how the attributes are interperted.
1342 if ( isset( $attribs[self
::NS_RDF
. ' parseType'] )
1343 && $attribs[self
::NS_RDF
. ' parseType'] === 'Resource'
1344 && $this->mode
[0] === self
::MODE_SIMPLE
1346 // this is equivalent to having an inner rdf:Description
1347 $this->mode
[0] = self
::MODE_QDESC
;
1349 foreach ( $attribs as $name => $val ) {
1350 if ( strpos( $name, ' ' ) === false ) {
1351 // This shouldn't happen, but so far some old software forgets namespace
1353 $this->logger
->info(
1354 __METHOD__
. ' Encountered non-namespaced attribute: ' .
1355 " $name=\"$val\". Skipping. ",
1356 [ 'file' => $this->filename
]
1360 list( $ns, $tag ) = explode( ' ', $name, 2 );
1361 if ( $ns === self
::NS_RDF
) {
1362 if ( $tag === 'value' ||
$tag === 'resource' ) {
1363 // resource is for url.
1364 // value attribute is a weird way of just putting the contents.
1365 $this->char( $this->xmlParser
, $val );
1367 } elseif ( isset( $this->items
[$ns][$tag] ) ) {
1368 if ( $this->mode
[0] === self
::MODE_SIMPLE
) {
1369 throw new RuntimeException( __METHOD__
1370 . " $ns:$tag found as attribute where not allowed" );
1372 $this->saveValue( $ns, $tag, $val );
1374 $this->logger
->debug(
1375 __METHOD__
. " Ignoring unrecognized element <$ns:$tag>.",
1376 [ 'file' => $this->filename
]
1383 * Given an extracted value, save it to results array
1385 * note also uses $this->ancestorStruct and
1386 * $this->processingArray to determine what name to
1387 * save the value under. (in addition to $tag).
1389 * @param string $ns Namespace of tag this is for
1390 * @param string $tag Tag name
1391 * @param string $val Value to save
1393 private function saveValue( $ns, $tag, $val ) {
1394 $info =& $this->items
[$ns][$tag];
1395 $finalName = isset( $info['map_name'] )
1396 ?
$info['map_name'] : $tag;
1397 if ( isset( $info['validate'] ) ) {
1398 if ( is_array( $info['validate'] ) ) {
1399 $validate = $info['validate'];
1401 $validator = new XMPValidate( $this->logger
);
1402 $validate = [ $validator, $info['validate'] ];
1405 if ( is_callable( $validate ) ) {
1406 call_user_func_array( $validate, [ $info, &$val, true ] );
1407 // the reasoning behind using &$val instead of using the return value
1408 // is to be consistent between here and validating structures.
1409 if ( is_null( $val ) ) {
1410 $this->logger
->info(
1411 __METHOD__
. " <$ns:$tag> failed validation.",
1412 [ 'file' => $this->filename
]
1418 $this->logger
->warning(
1419 __METHOD__
. " Validation function for $finalName (" .
1420 $validate[0] . '::' . $validate[1] . '()) is not callable.',
1421 [ 'file' => $this->filename
]
1426 if ( $this->ancestorStruct
&& $this->processingArray
) {
1427 // Aka both an array and a struct. ( self::MODE_BAGSTRUCT )
1428 $this->results
['xmp-' . $info['map_group']][$this->ancestorStruct
][][$finalName] = $val;
1429 } elseif ( $this->ancestorStruct
) {
1430 $this->results
['xmp-' . $info['map_group']][$this->ancestorStruct
][$finalName] = $val;
1431 } elseif ( $this->processingArray
) {
1432 if ( $this->itemLang
=== false ) {
1434 $this->results
['xmp-' . $info['map_group']][$finalName][] = $val;
1437 $this->results
['xmp-' . $info['map_group']][$finalName][$this->itemLang
] = $val;
1440 $this->results
['xmp-' . $info['map_group']][$finalName] = $val;