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
133 * Primary job is to initialize the XMLParser
135 function __construct( LoggerInterface
$logger = null ) {
136 if ( !function_exists( 'xml_parser_create_ns' ) ) {
137 // this should already be checked by this point
138 throw new RuntimeException( 'XMP support requires XML Parser' );
141 $this->setLogger( $logger );
143 $this->setLogger( new NullLogger() );
146 $this->items
= XMPInfo
::getItems();
148 $this->resetXMLParser();
151 public function setLogger( LoggerInterface
$logger ) {
152 $this->logger
= $logger;
156 * free the XML parser.
158 * @note It is unclear to me if we really need to do this ourselves
159 * or if php garbage collection will automatically free the xmlParser
160 * when it is no longer needed.
162 private function destroyXMLParser() {
163 if ( $this->xmlParser
) {
164 xml_parser_free( $this->xmlParser
);
165 $this->xmlParser
= null;
170 * Main use is if a single item has multiple xmp documents describing it.
171 * For example in jpeg's with extendedXMP
173 private function resetXMLParser() {
174 $this->destroyXMLParser();
176 $this->xmlParser
= xml_parser_create_ns( 'UTF-8', ' ' );
177 xml_parser_set_option( $this->xmlParser
, XML_OPTION_CASE_FOLDING
, 0 );
178 xml_parser_set_option( $this->xmlParser
, XML_OPTION_SKIP_WHITE
, 1 );
180 xml_set_element_handler( $this->xmlParser
,
181 [ $this, 'startElement' ],
182 [ $this, 'endElement' ] );
184 xml_set_character_data_handler( $this->xmlParser
, [ $this, 'char' ] );
186 $this->parsable
= self
::PARSABLE_UNKNOWN
;
187 $this->xmlParsableBuffer
= '';
191 * Check if this instance supports using this class
193 public static function isSupported() {
194 return function_exists( 'xml_parser_create_ns' ) && class_exists( 'XMLReader' );
197 /** Get the result array. Do some post-processing before returning
198 * the array, and transform any metadata that is special-cased.
200 * @return array Array of results as an array of arrays suitable for
201 * FormatMetadata::getFormattedData().
203 public function getResults() {
204 // xmp-special is for metadata that affects how stuff
205 // is extracted. For example xmpNote:HasExtendedXMP.
207 // It is also used to handle photoshop:AuthorsPosition
208 // which is weird and really part of another property,
209 // see 2:85 in IPTC. See also pg 21 of IPTC4XMP standard.
210 // The location fields also use it.
212 $data = $this->results
;
214 if ( isset( $data['xmp-special']['AuthorsPosition'] )
215 && is_string( $data['xmp-special']['AuthorsPosition'] )
216 && isset( $data['xmp-general']['Artist'][0] )
218 // Note, if there is more than one creator,
219 // this only applies to first. This also will
220 // only apply to the dc:Creator prop, not the
223 $data['xmp-general']['Artist'][0] =
224 $data['xmp-special']['AuthorsPosition'] . ', '
225 . $data['xmp-general']['Artist'][0];
228 // Go through the LocationShown and LocationCreated
229 // changing it to the non-hierarchal form used by
230 // the other location fields.
232 if ( isset( $data['xmp-special']['LocationShown'][0] )
233 && is_array( $data['xmp-special']['LocationShown'][0] )
235 // the is_array is just paranoia. It should always
237 foreach ( $data['xmp-special']['LocationShown'] as $loc ) {
238 if ( !is_array( $loc ) ) {
239 // To avoid copying over the _type meta-fields.
242 foreach ( $loc as $field => $val ) {
243 $data['xmp-general'][$field . 'Dest'][] = $val;
247 if ( isset( $data['xmp-special']['LocationCreated'][0] )
248 && is_array( $data['xmp-special']['LocationCreated'][0] )
250 // the is_array is just paranoia. It should always
252 foreach ( $data['xmp-special']['LocationCreated'] as $loc ) {
253 if ( !is_array( $loc ) ) {
254 // To avoid copying over the _type meta-fields.
257 foreach ( $loc as $field => $val ) {
258 $data['xmp-general'][$field . 'Created'][] = $val;
263 // We don't want to return the special values, since they're
264 // special and not info to be stored about the file.
265 unset( $data['xmp-special'] );
267 // Convert GPSAltitude to negative if below sea level.
268 if ( isset( $data['xmp-exif']['GPSAltitudeRef'] )
269 && isset( $data['xmp-exif']['GPSAltitude'] )
271 // Must convert to a real before multiplying by -1
272 // XMPValidate guarantees there will always be a '/' in this value.
273 list( $nom, $denom ) = explode( '/', $data['xmp-exif']['GPSAltitude'] );
274 $data['xmp-exif']['GPSAltitude'] = $nom / $denom;
276 if ( $data['xmp-exif']['GPSAltitudeRef'] == '1' ) {
277 $data['xmp-exif']['GPSAltitude'] *= -1;
279 unset( $data['xmp-exif']['GPSAltitudeRef'] );
286 * Main function to call to parse XMP. Use getResults to
289 * Also catches any errors during processing, writes them to
290 * debug log, blanks result array and returns false.
292 * @param string $content XMP data
293 * @param bool $allOfIt If this is all the data (true) or if its split up (false). Default true
294 * @throws RuntimeException
295 * @return bool Success.
297 public function parse( $content, $allOfIt = true ) {
298 if ( !$this->xmlParser
) {
299 $this->resetXMLParser();
303 // detect encoding by looking for BOM which is supposed to be in processing instruction.
304 // see page 12 of http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart3.pdf
305 if ( !$this->charset
) {
307 if ( preg_match( '/\xEF\xBB\xBF|\xFE\xFF|\x00\x00\xFE\xFF|\xFF\xFE\x00\x00|\xFF\xFE/',
312 $this->charset
= 'UTF-16BE';
315 $this->charset
= 'UTF-16LE';
317 case "\x00\x00\xFE\xFF":
318 $this->charset
= 'UTF-32BE';
320 case "\xFF\xFE\x00\x00":
321 $this->charset
= 'UTF-32LE';
324 $this->charset
= 'UTF-8';
327 // this should be impossible to get to
328 throw new RuntimeException( "Invalid BOM" );
331 // standard specifically says, if no bom assume utf-8
332 $this->charset
= 'UTF-8';
335 if ( $this->charset
!== 'UTF-8' ) {
336 // don't convert if already utf-8
337 MediaWiki\
suppressWarnings();
338 $content = iconv( $this->charset
, 'UTF-8//IGNORE', $content );
339 MediaWiki\restoreWarnings
();
342 // Ensure the XMP block does not have an xml doctype declaration, which
343 // could declare entities unsafe to parse with xml_parse (T85848/T71210).
344 if ( $this->parsable
!== self
::PARSABLE_OK
) {
345 if ( $this->parsable
=== self
::PARSABLE_NO
) {
346 throw new RuntimeException( 'Unsafe doctype declaration in XML.' );
349 $content = $this->xmlParsableBuffer
. $content;
350 if ( !$this->checkParseSafety( $content ) ) {
351 if ( !$allOfIt && $this->parsable
!== self
::PARSABLE_NO
) {
352 // parse wasn't Unsuccessful yet, so return true
356 $msg = ( $this->parsable
=== self
::PARSABLE_NO
) ?
357 'Unsafe doctype declaration in XML.' :
358 'No root element found in XML.';
359 throw new RuntimeException( $msg );
363 $ok = xml_parse( $this->xmlParser
, $content, $allOfIt );
365 $code = xml_get_error_code( $this->xmlParser
);
366 $error = xml_error_string( $code );
367 $line = xml_get_current_line_number( $this->xmlParser
);
368 $col = xml_get_current_column_number( $this->xmlParser
);
369 $offset = xml_get_current_byte_index( $this->xmlParser
);
371 $this->logger
->warning(
372 '{method} : Error reading XMP content: {error} ' .
373 '(line: {line} column: {column} byte offset: {offset})',
375 'method' => __METHOD__
,
376 'error_code' => $code,
381 'content' => $content,
383 $this->results
= []; // blank if error.
384 $this->destroyXMLParser();
387 } catch ( Exception
$e ) {
388 $this->logger
->warning(
389 '{method} Exception caught while parsing: ' . $e->getMessage(),
391 'method' => __METHOD__
,
393 'content' => $content,
400 $this->destroyXMLParser();
406 /** Entry point for XMPExtended blocks in jpeg files
408 * @todo In serious need of testing
409 * @see http://www.adobe.ge/devnet/xmp/pdfs/XMPSpecificationPart3.pdf XMP spec part 3 page 20
410 * @param string $content XMPExtended block minus the namespace signature
411 * @return bool If it succeeded.
413 public function parseExtended( $content ) {
414 // @todo FIXME: This is untested. Hard to find example files
415 // or programs that make such files..
416 $guid = substr( $content, 0, 32 );
417 if ( !isset( $this->results
['xmp-special']['HasExtendedXMP'] )
418 ||
$this->results
['xmp-special']['HasExtendedXMP'] !== $guid
420 $this->logger
->info( __METHOD__
.
421 " Ignoring XMPExtended block due to wrong guid (guid= '$guid')" );
425 $len = unpack( 'Nlength/Noffset', substr( $content, 32, 8 ) );
428 $len['length'] < 4 ||
429 $len['offset'] < 0 ||
430 $len['offset'] > $len['length']
433 __METHOD__
. 'Error reading extended XMP block, invalid length or offset.'
439 // we're not very robust here. we should accept it in the wrong order.
440 // To quote the XMP standard:
441 // "A JPEG writer should write the ExtendedXMP marker segments in order,
442 // immediately following the StandardXMP. However, the JPEG standard
443 // does not require preservation of marker segment order. A robust JPEG
444 // reader should tolerate the marker segments in any order."
445 // On the other hand, the probability that an image will have more than
446 // 128k of metadata is rather low... so the probability that it will have
447 // > 128k, and be in the wrong order is very low...
449 if ( $len['offset'] !== $this->extendedXMPOffset
) {
450 $this->logger
->info( __METHOD__
. 'Ignoring XMPExtended block due to wrong order. (Offset was '
451 . $len['offset'] . ' but expected ' . $this->extendedXMPOffset
. ')' );
456 if ( $len['offset'] === 0 ) {
457 // if we're starting the extended block, we've probably already
458 // done the XMPStandard block, so reset.
459 $this->resetXMLParser();
462 $this->extendedXMPOffset +
= $len['length'];
464 $actualContent = substr( $content, 40 );
466 if ( $this->extendedXMPOffset
=== strlen( $actualContent ) ) {
472 $this->logger
->debug( __METHOD__
. 'Parsing a XMPExtended block' );
474 return $this->parse( $actualContent, $atEnd );
478 * Character data handler
479 * Called whenever character data is found in the xmp document.
481 * does nothing if we're in MODE_IGNORE or if the data is whitespace
482 * throws an error if we're not in MODE_SIMPLE (as we're not allowed to have character
483 * data in the other modes).
485 * As an example, this happens when we encounter XMP like:
486 * <exif:DigitalZoomRatio>0/10</exif:DigitalZoomRatio>
487 * and are processing the 0/10 bit.
489 * @param resource $parser XMLParser reference to the xml parser
490 * @param string $data Character data
491 * @throws RuntimeException On invalid data
493 function char( $parser, $data ) {
494 $data = trim( $data );
495 if ( trim( $data ) === "" ) {
499 if ( !isset( $this->mode
[0] ) ) {
500 throw new RuntimeException( 'Unexpected character data before first rdf:Description element' );
503 if ( $this->mode
[0] === self
::MODE_IGNORE
) {
507 if ( $this->mode
[0] !== self
::MODE_SIMPLE
508 && $this->mode
[0] !== self
::MODE_QDESC
510 throw new RuntimeException( 'character data where not expected. (mode ' . $this->mode
[0] . ')' );
513 // to check, how does this handle w.s.
514 if ( $this->charContent
=== false ) {
515 $this->charContent
= $data;
517 $this->charContent
.= $data;
522 * Check if a block of XML is safe to pass to xml_parse, i.e. doesn't
523 * contain a doctype declaration which could contain a dos attack if we
524 * parse it and expand internal entities (T85848).
526 * @param string $content xml string to check for parse safety
527 * @return bool true if the xml is safe to parse, false otherwise
529 private function checkParseSafety( $content ) {
530 $reader = new XMLReader();
533 // For XMLReader to parse incomplete/invalid XML, it has to be open()'ed
534 // instead of using XML().
536 'data://text/plain,' . urlencode( $content ),
538 LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_NONET
541 $oldDisable = libxml_disable_entity_loader( true );
542 /** @noinspection PhpUnusedLocalVariableInspection */
543 $reset = new ScopedCallback(
544 'libxml_disable_entity_loader',
547 $reader->setParserProperty( XMLReader
::SUBST_ENTITIES
, false );
549 // Even with LIBXML_NOWARNING set, XMLReader::read gives a warning
550 // when parsing truncated XML, which causes unit tests to fail.
551 MediaWiki\
suppressWarnings();
552 while ( $reader->read() ) {
553 if ( $reader->nodeType
=== XMLReader
::ELEMENT
) {
554 // Reached the first element without hitting a doctype declaration
555 $this->parsable
= self
::PARSABLE_OK
;
559 if ( $reader->nodeType
=== XMLReader
::DOC_TYPE
) {
560 $this->parsable
= self
::PARSABLE_NO
;
565 MediaWiki\restoreWarnings
();
567 if ( !is_null( $result ) ) {
571 // Reached the end of the parsable xml without finding an element
572 // or doctype. Buffer and try again.
573 $this->parsable
= self
::PARSABLE_BUFFERING
;
574 $this->xmlParsableBuffer
= $content;
578 /** When we hit a closing element in MODE_IGNORE
579 * Check to see if this is the element we started to ignore,
580 * in which case we get out of MODE_IGNORE
582 * @param string $elm Namespace of element followed by a space and then tag name of element.
584 private function endElementModeIgnore( $elm ) {
585 if ( $this->curItem
[0] === $elm ) {
586 array_shift( $this->curItem
);
587 array_shift( $this->mode
);
592 * Hit a closing element when in MODE_SIMPLE.
593 * This generally means that we finished processing a
594 * property value, and now have to save the result to the
597 * For example, when processing:
598 * <exif:DigitalZoomRatio>0/10</exif:DigitalZoomRatio>
599 * this deals with when we hit </exif:DigitalZoomRatio>.
601 * Or it could be if we hit the end element of a property
602 * of a compound data structure (like a member of an array).
604 * @param string $elm Namespace, space, and tag name.
606 private function endElementModeSimple( $elm ) {
607 if ( $this->charContent
!== false ) {
608 if ( $this->processingArray
) {
609 // if we're processing an array, use the original element
610 // name instead of rdf:li.
611 list( $ns, $tag ) = explode( ' ', $this->curItem
[0], 2 );
613 list( $ns, $tag ) = explode( ' ', $elm, 2 );
615 $this->saveValue( $ns, $tag, $this->charContent
);
617 $this->charContent
= false; // reset
619 array_shift( $this->curItem
);
620 array_shift( $this->mode
);
624 * Hit a closing element in MODE_STRUCT, MODE_SEQ, MODE_BAG
625 * generally means we've finished processing a nested structure.
626 * resets some internal variables to indicate that.
628 * Note this means we hit the closing element not the "</rdf:Seq>".
630 * @par For example, when processing:
632 * <exif:ISOSpeedRatings> <rdf:Seq> <rdf:li>64</rdf:li>
633 * </rdf:Seq> </exif:ISOSpeedRatings>
636 * This method is called when we hit the "</exif:ISOSpeedRatings>" tag.
638 * @param string $elm Namespace . space . tag name.
639 * @throws RuntimeException
641 private function endElementNested( $elm ) {
642 /* cur item must be the same as $elm, unless if in MODE_STRUCT
643 * in which case it could also be rdf:Description */
644 if ( $this->curItem
[0] !== $elm
645 && !( $elm === self
::NS_RDF
. ' Description'
646 && $this->mode
[0] === self
::MODE_STRUCT
)
648 throw new RuntimeException( "nesting mismatch. got a </$elm> but expected a </" .
649 $this->curItem
[0] . '>' );
652 // Validate structures.
653 list( $ns, $tag ) = explode( ' ', $elm, 2 );
654 if ( isset( $this->items
[$ns][$tag]['validate'] ) ) {
655 $info =& $this->items
[$ns][$tag];
656 $finalName = isset( $info['map_name'] )
657 ?
$info['map_name'] : $tag;
659 if ( is_array( $info['validate'] ) ) {
660 $validate = $info['validate'];
662 $validator = new XMPValidate( $this->logger
);
663 $validate = [ $validator, $info['validate'] ];
666 if ( !isset( $this->results
['xmp-' . $info['map_group']][$finalName] ) ) {
667 // This can happen if all the members of the struct failed validation.
668 $this->logger
->debug( __METHOD__
. " <$ns:$tag> has no valid members." );
669 } elseif ( is_callable( $validate ) ) {
670 $val =& $this->results
['xmp-' . $info['map_group']][$finalName];
671 call_user_func_array( $validate, [ $info, &$val, false ] );
672 if ( is_null( $val ) ) {
673 // the idea being the validation function will unset the variable if
675 $this->logger
->info( __METHOD__
. " <$ns:$tag> failed validation." );
676 unset( $this->results
['xmp-' . $info['map_group']][$finalName] );
679 $this->logger
->warning( __METHOD__
. " Validation function for $finalName ("
680 . $validate[0] . '::' . $validate[1] . '()) is not callable.' );
684 array_shift( $this->curItem
);
685 array_shift( $this->mode
);
686 $this->ancestorStruct
= false;
687 $this->processingArray
= false;
688 $this->itemLang
= false;
692 * Hit a closing element in MODE_LI (either rdf:Seq, or rdf:Bag )
693 * Add information about what type of element this is.
695 * Note we still have to hit the outer "</property>"
697 * @par For example, when processing:
699 * <exif:ISOSpeedRatings> <rdf:Seq> <rdf:li>64</rdf:li>
700 * </rdf:Seq> </exif:ISOSpeedRatings>
703 * This method is called when we hit the "</rdf:Seq>".
704 * (For comparison, we call endElementModeSimple when we
705 * hit the "</rdf:li>")
707 * @param string $elm Namespace . ' ' . element name
708 * @throws RuntimeException
710 private function endElementModeLi( $elm ) {
711 list( $ns, $tag ) = explode( ' ', $this->curItem
[0], 2 );
712 $info = $this->items
[$ns][$tag];
713 $finalName = isset( $info['map_name'] )
714 ?
$info['map_name'] : $tag;
716 array_shift( $this->mode
);
718 if ( !isset( $this->results
['xmp-' . $info['map_group']][$finalName] ) ) {
719 $this->logger
->debug( __METHOD__
. " Empty compund element $finalName." );
724 if ( $elm === self
::NS_RDF
. ' Seq' ) {
725 $this->results
['xmp-' . $info['map_group']][$finalName]['_type'] = 'ol';
726 } elseif ( $elm === self
::NS_RDF
. ' Bag' ) {
727 $this->results
['xmp-' . $info['map_group']][$finalName]['_type'] = 'ul';
728 } elseif ( $elm === self
::NS_RDF
. ' Alt' ) {
729 // extra if needed as you could theoretically have a non-language alt.
730 if ( $info['mode'] === self
::MODE_LANG
) {
731 $this->results
['xmp-' . $info['map_group']][$finalName]['_type'] = 'lang';
734 throw new RuntimeException(
735 __METHOD__
. " expected </rdf:seq> or </rdf:bag> but instead got $elm."
741 * End element while in MODE_QDESC
742 * mostly when ending an element when we have a simple value
743 * that has qualifiers.
745 * Qualifiers aren't all that common, and we don't do anything
748 * @param string $elm Namespace and element
750 private function endElementModeQDesc( $elm ) {
751 if ( $elm === self
::NS_RDF
. ' value' ) {
752 list( $ns, $tag ) = explode( ' ', $this->curItem
[0], 2 );
753 $this->saveValue( $ns, $tag, $this->charContent
);
757 array_shift( $this->mode
);
758 array_shift( $this->curItem
);
763 * Handler for hitting a closing element.
765 * generally just calls a helper function depending on what
768 * Ignores the outer wrapping elements that are optional in
769 * xmp and have no meaning.
771 * @param resource $parser
772 * @param string $elm Namespace . ' ' . element name
773 * @throws RuntimeException
775 function endElement( $parser, $elm ) {
776 if ( $elm === ( self
::NS_RDF
. ' RDF' )
777 ||
$elm === 'adobe:ns:meta/ xmpmeta'
778 ||
$elm === 'adobe:ns:meta/ xapmeta'
784 if ( $elm === self
::NS_RDF
. ' type' ) {
785 // these aren't really supported properly yet.
786 // However, it appears they almost never used.
787 $this->logger
->info( __METHOD__
. ' encountered <rdf:type>' );
790 if ( strpos( $elm, ' ' ) === false ) {
791 // This probably shouldn't happen.
792 // However, there is a bug in an adobe product
793 // that forgets the namespace on some things.
794 // (Luckily they are unimportant things).
795 $this->logger
->info( __METHOD__
. " Encountered </$elm> which has no namespace. Skipping." );
800 if ( count( $this->mode
[0] ) === 0 ) {
801 // This should never ever happen and means
802 // there is a pretty major bug in this class.
803 throw new RuntimeException( 'Encountered end element with no mode' );
806 if ( count( $this->curItem
) == 0 && $this->mode
[0] !== self
::MODE_INITIAL
) {
807 // just to be paranoid. Should always have a curItem, except for initially
808 // (aka during MODE_INITAL).
809 throw new RuntimeException( "Hit end element </$elm> but no curItem" );
812 switch ( $this->mode
[0] ) {
813 case self
::MODE_IGNORE
:
814 $this->endElementModeIgnore( $elm );
816 case self
::MODE_SIMPLE
:
817 $this->endElementModeSimple( $elm );
819 case self
::MODE_STRUCT
:
822 case self
::MODE_LANG
:
823 case self
::MODE_BAGSTRUCT
:
824 $this->endElementNested( $elm );
826 case self
::MODE_INITIAL
:
827 if ( $elm === self
::NS_RDF
. ' Description' ) {
828 array_shift( $this->mode
);
830 throw new RuntimeException( 'Element ended unexpectedly while in MODE_INITIAL' );
834 case self
::MODE_LI_LANG
:
835 $this->endElementModeLi( $elm );
837 case self
::MODE_QDESC
:
838 $this->endElementModeQDesc( $elm );
841 $this->logger
->warning( __METHOD__
. " no mode (elm = $elm)" );
847 * Hit an opening element while in MODE_IGNORE
849 * XMP is extensible, so ignore any tag we don't understand.
851 * Mostly ignores, unless we encounter the element that we are ignoring.
852 * in which case we add it to the item stack, so we can ignore things
853 * that are nested, correctly.
855 * @param string $elm Namespace . ' ' . tag name
857 private function startElementModeIgnore( $elm ) {
858 if ( $elm === $this->curItem
[0] ) {
859 array_unshift( $this->curItem
, $elm );
860 array_unshift( $this->mode
, self
::MODE_IGNORE
);
865 * Start element in MODE_BAG (unordered array)
866 * this should always be <rdf:Bag>
868 * @param string $elm Namespace . ' ' . tag
869 * @throws RuntimeException If we have an element that's not <rdf:Bag>
871 private function startElementModeBag( $elm ) {
872 if ( $elm === self
::NS_RDF
. ' Bag' ) {
873 array_unshift( $this->mode
, self
::MODE_LI
);
875 throw new RuntimeException( "Expected <rdf:Bag> but got $elm." );
880 * Start element in MODE_SEQ (ordered array)
881 * this should always be <rdf:Seq>
883 * @param string $elm Namespace . ' ' . tag
884 * @throws RuntimeException If we have an element that's not <rdf:Seq>
886 private function startElementModeSeq( $elm ) {
887 if ( $elm === self
::NS_RDF
. ' Seq' ) {
888 array_unshift( $this->mode
, self
::MODE_LI
);
889 } elseif ( $elm === self
::NS_RDF
. ' Bag' ) {
891 $this->logger
->info( __METHOD__
. ' Expected an rdf:Seq, but got an rdf:Bag. Pretending'
892 . ' it is a Seq, since some buggy software is known to screw this up.' );
893 array_unshift( $this->mode
, self
::MODE_LI
);
895 throw new RuntimeException( "Expected <rdf:Seq> but got $elm." );
900 * Start element in MODE_LANG (language alternative)
901 * this should always be <rdf:Alt>
903 * This tag tends to be used for metadata like describe this
904 * picture, which can be translated into multiple languages.
906 * XMP supports non-linguistic alternative selections,
907 * which are really only used for thumbnails, which
908 * we don't care about.
910 * @param string $elm Namespace . ' ' . tag
911 * @throws RuntimeException If we have an element that's not <rdf:Alt>
913 private function startElementModeLang( $elm ) {
914 if ( $elm === self
::NS_RDF
. ' Alt' ) {
915 array_unshift( $this->mode
, self
::MODE_LI_LANG
);
917 throw new RuntimeException( "Expected <rdf:Seq> but got $elm." );
922 * Handle an opening element when in MODE_SIMPLE
924 * This should not happen often. This is for if a simple element
925 * already opened has a child element. Could happen for a
929 * <exif:DigitalZoomRatio><rdf:Description><rdf:value>0/10</rdf:value>
930 * <foo:someQualifier>Bar</foo:someQualifier> </rdf:Description>
931 * </exif:DigitalZoomRatio>
933 * This method is called when processing the <rdf:Description> element
935 * @param string $elm Namespace and tag names separated by space.
936 * @param array $attribs Attributes of the element.
937 * @throws RuntimeException
939 private function startElementModeSimple( $elm, $attribs ) {
940 if ( $elm === self
::NS_RDF
. ' Description' ) {
941 // If this value has qualifiers
942 array_unshift( $this->mode
, self
::MODE_QDESC
);
943 array_unshift( $this->curItem
, $this->curItem
[0] );
945 if ( isset( $attribs[self
::NS_RDF
. ' value'] ) ) {
946 list( $ns, $tag ) = explode( ' ', $this->curItem
[0], 2 );
947 $this->saveValue( $ns, $tag, $attribs[self
::NS_RDF
. ' value'] );
949 } elseif ( $elm === self
::NS_RDF
. ' value' ) {
950 // This should not be here.
951 throw new RuntimeException( __METHOD__
. ' Encountered <rdf:value> where it was unexpected.' );
953 // something else we don't recognize, like a qualifier maybe.
954 $this->logger
->info( __METHOD__
.
955 " Encountered element <$elm> where only expecting character data as value of " .
957 array_unshift( $this->mode
, self
::MODE_IGNORE
);
958 array_unshift( $this->curItem
, $elm );
963 * Start an element when in MODE_QDESC.
964 * This generally happens when a simple element has an inner
965 * rdf:Description to hold qualifier elements.
968 * <exif:DigitalZoomRatio><rdf:Description><rdf:value>0/10</rdf:value>
969 * <foo:someQualifier>Bar</foo:someQualifier> </rdf:Description>
970 * </exif:DigitalZoomRatio>
971 * Called when processing the <rdf:value> or <foo:someQualifier>.
973 * @param string $elm Namespace and tag name separated by a space.
975 private function startElementModeQDesc( $elm ) {
976 if ( $elm === self
::NS_RDF
. ' value' ) {
977 return; // do nothing
979 // otherwise its a qualifier, which we ignore
980 array_unshift( $this->mode
, self
::MODE_IGNORE
);
981 array_unshift( $this->curItem
, $elm );
986 * Starting an element when in MODE_INITIAL
987 * This usually happens when we hit an element inside
988 * the outer rdf:Description
990 * This is generally where most properties start.
992 * @param string $ns Namespace
993 * @param string $tag Tag name (without namespace prefix)
994 * @param array $attribs Array of attributes
995 * @throws RuntimeException
997 private function startElementModeInitial( $ns, $tag, $attribs ) {
998 if ( $ns !== self
::NS_RDF
) {
999 if ( isset( $this->items
[$ns][$tag] ) ) {
1000 if ( isset( $this->items
[$ns][$tag]['structPart'] ) ) {
1001 // If this element is supposed to appear only as
1002 // a child of a structure, but appears here (not as
1003 // a child of a struct), then something weird is
1004 // happening, so ignore this element and its children.
1006 $this->logger
->warning( "Encountered <$ns:$tag> outside"
1007 . " of its expected parent. Ignoring." );
1009 array_unshift( $this->mode
, self
::MODE_IGNORE
);
1010 array_unshift( $this->curItem
, $ns . ' ' . $tag );
1014 $mode = $this->items
[$ns][$tag]['mode'];
1015 array_unshift( $this->mode
, $mode );
1016 array_unshift( $this->curItem
, $ns . ' ' . $tag );
1017 if ( $mode === self
::MODE_STRUCT
) {
1018 $this->ancestorStruct
= isset( $this->items
[$ns][$tag]['map_name'] )
1019 ?
$this->items
[$ns][$tag]['map_name'] : $tag;
1021 if ( $this->charContent
!== false ) {
1023 // Should not happen in valid XMP.
1024 throw new RuntimeException( 'tag nested in non-whitespace characters.' );
1027 // This element is not on our list of allowed elements so ignore.
1028 $this->logger
->debug( __METHOD__
. " Ignoring unrecognized element <$ns:$tag>." );
1029 array_unshift( $this->mode
, self
::MODE_IGNORE
);
1030 array_unshift( $this->curItem
, $ns . ' ' . $tag );
1035 // process attributes
1036 $this->doAttribs( $attribs );
1040 * Hit an opening element when in a Struct (MODE_STRUCT)
1041 * This is generally for fields of a compound property.
1043 * Example of a struct (abbreviated; flash has more properties):
1045 * <exif:Flash> <rdf:Description> <exif:Fired>True</exif:Fired>
1046 * <exif:Mode>1</exif:Mode></rdf:Description></exif:Flash>
1050 * <exif:Flash rdf:parseType='Resource'> <exif:Fired>True</exif:Fired>
1051 * <exif:Mode>1</exif:Mode></exif:Flash>
1053 * @param string $ns Namespace
1054 * @param string $tag Tag name (no ns)
1055 * @param array $attribs Array of attribs w/ values.
1056 * @throws RuntimeException
1058 private function startElementModeStruct( $ns, $tag, $attribs ) {
1059 if ( $ns !== self
::NS_RDF
) {
1060 if ( isset( $this->items
[$ns][$tag] ) ) {
1061 if ( isset( $this->items
[$ns][$this->ancestorStruct
]['children'] )
1062 && !isset( $this->items
[$ns][$this->ancestorStruct
]['children'][$tag] )
1064 // This assumes that we don't have inter-namespace nesting
1065 // which we don't in all the properties we're interested in.
1066 throw new RuntimeException( " <$tag> appeared nested in <" . $this->ancestorStruct
1067 . "> where it is not allowed." );
1069 array_unshift( $this->mode
, $this->items
[$ns][$tag]['mode'] );
1070 array_unshift( $this->curItem
, $ns . ' ' . $tag );
1071 if ( $this->charContent
!== false ) {
1073 // Should not happen in valid XMP.
1074 throw new RuntimeException( "tag <$tag> nested in non-whitespace characters (" .
1075 $this->charContent
. ")." );
1078 array_unshift( $this->mode
, self
::MODE_IGNORE
);
1079 array_unshift( $this->curItem
, $ns . ' ' . $tag );
1085 if ( $ns === self
::NS_RDF
&& $tag === 'Description' ) {
1086 $this->doAttribs( $attribs );
1087 array_unshift( $this->mode
, self
::MODE_STRUCT
);
1088 array_unshift( $this->curItem
, $this->curItem
[0] );
1093 * opening element in MODE_LI
1094 * process elements of arrays.
1097 * <exif:ISOSpeedRatings> <rdf:Seq> <rdf:li>64</rdf:li>
1098 * </rdf:Seq> </exif:ISOSpeedRatings>
1099 * This method is called when we hit the <rdf:li> element.
1101 * @param string $elm Namespace . ' ' . tagname
1102 * @param array $attribs Attributes. (needed for BAGSTRUCTS)
1103 * @throws RuntimeException If gets a tag other than <rdf:li>
1105 private function startElementModeLi( $elm, $attribs ) {
1106 if ( ( $elm ) !== self
::NS_RDF
. ' li' ) {
1107 throw new RuntimeException( "<rdf:li> expected but got $elm." );
1110 if ( !isset( $this->mode
[1] ) ) {
1111 // This should never ever ever happen. Checking for it
1113 throw new RuntimeException( 'In mode Li, but no 2xPrevious mode!' );
1116 if ( $this->mode
[1] === self
::MODE_BAGSTRUCT
) {
1117 // This list item contains a compound (STRUCT) value.
1118 array_unshift( $this->mode
, self
::MODE_STRUCT
);
1119 array_unshift( $this->curItem
, $elm );
1120 $this->processingArray
= true;
1122 if ( !isset( $this->curItem
[1] ) ) {
1124 throw new RuntimeException( 'Can not find parent of BAGSTRUCT.' );
1126 list( $curNS, $curTag ) = explode( ' ', $this->curItem
[1] );
1127 $this->ancestorStruct
= isset( $this->items
[$curNS][$curTag]['map_name'] )
1128 ?
$this->items
[$curNS][$curTag]['map_name'] : $curTag;
1130 $this->doAttribs( $attribs );
1132 // Normal BAG or SEQ containing simple values.
1133 array_unshift( $this->mode
, self
::MODE_SIMPLE
);
1134 // need to add curItem[0] on again since one is for the specific item
1135 // and one is for the entire group.
1136 array_unshift( $this->curItem
, $this->curItem
[0] );
1137 $this->processingArray
= true;
1142 * Opening element in MODE_LI_LANG.
1143 * process elements of language alternatives
1146 * <dc:title> <rdf:Alt> <rdf:li xml:lang="x-default">My house
1147 * </rdf:li> </rdf:Alt> </dc:title>
1149 * This method is called when we hit the <rdf:li> element.
1151 * @param string $elm Namespace . ' ' . tag
1152 * @param array $attribs Array of elements (most importantly xml:lang)
1153 * @throws RuntimeException If gets a tag other than <rdf:li> or if no xml:lang
1155 private function startElementModeLiLang( $elm, $attribs ) {
1156 if ( $elm !== self
::NS_RDF
. ' li' ) {
1157 throw new RuntimeException( __METHOD__
. " <rdf:li> expected but got $elm." );
1159 if ( !isset( $attribs[self
::NS_XML
. ' lang'] )
1160 ||
!preg_match( '/^[-A-Za-z0-9]{2,}$/D', $attribs[self
::NS_XML
. ' lang'] )
1162 throw new RuntimeException( __METHOD__
1163 . " <rdf:li> did not contain, or has invalid xml:lang attribute in lang alternative" );
1166 // Lang is case-insensitive.
1167 $this->itemLang
= strtolower( $attribs[self
::NS_XML
. ' lang'] );
1169 // need to add curItem[0] on again since one is for the specific item
1170 // and one is for the entire group.
1171 array_unshift( $this->curItem
, $this->curItem
[0] );
1172 array_unshift( $this->mode
, self
::MODE_SIMPLE
);
1173 $this->processingArray
= true;
1177 * Hits an opening element.
1178 * Generally just calls a helper based on what MODE we're in.
1179 * Also does some initial set up for the wrapper element
1181 * @param resource $parser
1182 * @param string $elm Namespace "<space>" element
1183 * @param array $attribs Attribute name => value
1184 * @throws RuntimeException
1186 function startElement( $parser, $elm, $attribs ) {
1187 if ( $elm === self
::NS_RDF
. ' RDF'
1188 ||
$elm === 'adobe:ns:meta/ xmpmeta'
1189 ||
$elm === 'adobe:ns:meta/ xapmeta'
1193 } elseif ( $elm === self
::NS_RDF
. ' Description' ) {
1194 if ( count( $this->mode
) === 0 ) {
1196 array_unshift( $this->mode
, self
::MODE_INITIAL
);
1198 } elseif ( $elm === self
::NS_RDF
. ' type' ) {
1199 // This doesn't support rdf:type properly.
1200 // In practise I have yet to see a file that
1201 // uses this element, however it is mentioned
1202 // on page 25 of part 1 of the xmp standard.
1203 // Also it seems as if exiv2 and exiftool do not support
1204 // this either (That or I misunderstand the standard)
1205 $this->logger
->info( __METHOD__
. ' Encountered <rdf:type> which isn\'t currently supported' );
1208 if ( strpos( $elm, ' ' ) === false ) {
1209 // This probably shouldn't happen.
1210 $this->logger
->info( __METHOD__
. " Encountered <$elm> which has no namespace. Skipping." );
1215 list( $ns, $tag ) = explode( ' ', $elm, 2 );
1217 if ( count( $this->mode
) === 0 ) {
1218 // This should not happen.
1219 throw new RuntimeException( 'Error extracting XMP, '
1220 . "encountered <$elm> with no mode" );
1223 switch ( $this->mode
[0] ) {
1224 case self
::MODE_IGNORE
:
1225 $this->startElementModeIgnore( $elm );
1227 case self
::MODE_SIMPLE
:
1228 $this->startElementModeSimple( $elm, $attribs );
1230 case self
::MODE_INITIAL
:
1231 $this->startElementModeInitial( $ns, $tag, $attribs );
1233 case self
::MODE_STRUCT
:
1234 $this->startElementModeStruct( $ns, $tag, $attribs );
1236 case self
::MODE_BAG
:
1237 case self
::MODE_BAGSTRUCT
:
1238 $this->startElementModeBag( $elm );
1240 case self
::MODE_SEQ
:
1241 $this->startElementModeSeq( $elm );
1243 case self
::MODE_LANG
:
1244 $this->startElementModeLang( $elm );
1246 case self
::MODE_LI_LANG
:
1247 $this->startElementModeLiLang( $elm, $attribs );
1250 $this->startElementModeLi( $elm, $attribs );
1252 case self
::MODE_QDESC
:
1253 $this->startElementModeQDesc( $elm );
1256 throw new RuntimeException( 'StartElement in unknown mode: ' . $this->mode
[0] );
1260 // @codingStandardsIgnoreStart Generic.Files.LineLength
1262 * Process attributes.
1263 * Simple values can be stored as either a tag or attribute
1265 * Often the initial "<rdf:Description>" tag just has all the simple
1266 * properties as attributes.
1270 * <rdf:Description rdf:about="" xmlns:exif="http://ns.adobe.com/exif/1.0/" exif:DigitalZoomRatio="0/10">
1273 * @param array $attribs Array attribute=>value
1274 * @throws RuntimeException
1276 // @codingStandardsIgnoreEnd
1277 private function doAttribs( $attribs ) {
1278 // first check for rdf:parseType attribute, as that can change
1279 // how the attributes are interperted.
1281 if ( isset( $attribs[self
::NS_RDF
. ' parseType'] )
1282 && $attribs[self
::NS_RDF
. ' parseType'] === 'Resource'
1283 && $this->mode
[0] === self
::MODE_SIMPLE
1285 // this is equivalent to having an inner rdf:Description
1286 $this->mode
[0] = self
::MODE_QDESC
;
1288 foreach ( $attribs as $name => $val ) {
1289 if ( strpos( $name, ' ' ) === false ) {
1290 // This shouldn't happen, but so far some old software forgets namespace
1292 $this->logger
->info( __METHOD__
. ' Encountered non-namespaced attribute: '
1293 . " $name=\"$val\". Skipping. " );
1296 list( $ns, $tag ) = explode( ' ', $name, 2 );
1297 if ( $ns === self
::NS_RDF
) {
1298 if ( $tag === 'value' ||
$tag === 'resource' ) {
1299 // resource is for url.
1300 // value attribute is a weird way of just putting the contents.
1301 $this->char( $this->xmlParser
, $val );
1303 } elseif ( isset( $this->items
[$ns][$tag] ) ) {
1304 if ( $this->mode
[0] === self
::MODE_SIMPLE
) {
1305 throw new RuntimeException( __METHOD__
1306 . " $ns:$tag found as attribute where not allowed" );
1308 $this->saveValue( $ns, $tag, $val );
1310 $this->logger
->debug( __METHOD__
. " Ignoring unrecognized element <$ns:$tag>." );
1316 * Given an extracted value, save it to results array
1318 * note also uses $this->ancestorStruct and
1319 * $this->processingArray to determine what name to
1320 * save the value under. (in addition to $tag).
1322 * @param string $ns Namespace of tag this is for
1323 * @param string $tag Tag name
1324 * @param string $val Value to save
1326 private function saveValue( $ns, $tag, $val ) {
1327 $info =& $this->items
[$ns][$tag];
1328 $finalName = isset( $info['map_name'] )
1329 ?
$info['map_name'] : $tag;
1330 if ( isset( $info['validate'] ) ) {
1331 if ( is_array( $info['validate'] ) ) {
1332 $validate = $info['validate'];
1334 $validator = new XMPValidate( $this->logger
);
1335 $validate = [ $validator, $info['validate'] ];
1338 if ( is_callable( $validate ) ) {
1339 call_user_func_array( $validate, [ $info, &$val, true ] );
1340 // the reasoning behind using &$val instead of using the return value
1341 // is to be consistent between here and validating structures.
1342 if ( is_null( $val ) ) {
1343 $this->logger
->info( __METHOD__
. " <$ns:$tag> failed validation." );
1348 $this->logger
->warning( __METHOD__
. " Validation function for $finalName ("
1349 . $validate[0] . '::' . $validate[1] . '()) is not callable.' );
1353 if ( $this->ancestorStruct
&& $this->processingArray
) {
1354 // Aka both an array and a struct. ( self::MODE_BAGSTRUCT )
1355 $this->results
['xmp-' . $info['map_group']][$this->ancestorStruct
][][$finalName] = $val;
1356 } elseif ( $this->ancestorStruct
) {
1357 $this->results
['xmp-' . $info['map_group']][$this->ancestorStruct
][$finalName] = $val;
1358 } elseif ( $this->processingArray
) {
1359 if ( $this->itemLang
=== false ) {
1361 $this->results
['xmp-' . $info['map_group']][$finalName][] = $val;
1364 $this->results
['xmp-' . $info['map_group']][$finalName][$this->itemLang
] = $val;
1367 $this->results
['xmp-' . $info['map_group']][$finalName] = $val;