Use PHP 7 '??' operator instead of '?:' with 'isset()' where convenient
[lhc/web/wiklou.git] / includes / libs / xmp / XMP.php
1 <?php
2 /**
3 * Reader for XMP data containing properties relevant to images.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Media
22 */
23
24 use Psr\Log\LoggerAwareInterface;
25 use Psr\Log\LoggerInterface;
26 use Psr\Log\NullLogger;
27 use Wikimedia\ScopedCallback;
28
29 /**
30 * Class for reading xmp data containing properties relevant to
31 * images, and spitting out an array that FormatMetadata accepts.
32 *
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
38 * ignore them.
39 *
40 * The public methods one would call in this class are
41 * - parse( $content )
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).
46 * - getResults
47 * Outputs a results array.
48 *
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
51 * read rdf.
52 */
53 class XMPReader implements LoggerAwareInterface {
54 /** @var array XMP item configuration array */
55 protected $items;
56
57 /** @var array Array to hold the current element (and previous element, and so on) */
58 private $curItem = [];
59
60 /** @var bool|string The structure name when processing nested structures. */
61 private $ancestorStruct = false;
62
63 /** @var bool|string Temporary holder for character data that appears in xmp doc. */
64 private $charContent = false;
65
66 /** @var array Stores the state the xmpreader is in (see MODE_FOO constants) */
67 private $mode = [];
68
69 /** @var array Array to hold results */
70 private $results = [];
71
72 /** @var bool If we're doing a seq or bag. */
73 private $processingArray = false;
74
75 /** @var bool|string Used for lang alts only */
76 private $itemLang = false;
77
78 /** @var resource A resource handle for the XML parser */
79 private $xmlParser;
80
81 /** @var bool|string Character set like 'UTF-8' */
82 private $charset = false;
83
84 /** @var int */
85 private $extendedXMPOffset = 0;
86
87 /** @var int Flag determining if the XMP is safe to parse **/
88 private $parsable = 0;
89
90 /** @var string Buffer of XML to parse **/
91 private $xmlParsableBuffer = '';
92
93 /**
94 * These are various mode constants.
95 * they are used to figure out what to do
96 * with an element when its encountered.
97 *
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.
101 */
102 const MODE_INITIAL = 0;
103 const MODE_IGNORE = 1;
104 const MODE_LI = 2;
105 const MODE_LI_LANG = 3;
106 const MODE_QDESC = 4;
107
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.
117
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';
120
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;
126
127 /**
128 * @var LoggerInterface
129 */
130 private $logger;
131
132 /**
133 * @var string
134 */
135 private $filename;
136
137 /**
138 * Primary job is to initialize the XMLParser
139 * @param LoggerInterface|null $logger
140 * @param string $filename
141 */
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' );
146 }
147 if ( $logger ) {
148 $this->setLogger( $logger );
149 } else {
150 $this->setLogger( new NullLogger() );
151 }
152 $this->filename = $filename;
153
154 $this->items = XMPInfo::getItems();
155
156 $this->resetXMLParser();
157 }
158
159 public function setLogger( LoggerInterface $logger ) {
160 $this->logger = $logger;
161 }
162
163 /**
164 * free the XML parser.
165 *
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.
169 */
170 private function destroyXMLParser() {
171 if ( $this->xmlParser ) {
172 xml_parser_free( $this->xmlParser );
173 $this->xmlParser = null;
174 }
175 }
176
177 /**
178 * Main use is if a single item has multiple xmp documents describing it.
179 * For example in jpeg's with extendedXMP
180 */
181 private function resetXMLParser() {
182 $this->destroyXMLParser();
183
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 );
187
188 xml_set_element_handler( $this->xmlParser,
189 [ $this, 'startElement' ],
190 [ $this, 'endElement' ] );
191
192 xml_set_character_data_handler( $this->xmlParser, [ $this, 'char' ] );
193
194 $this->parsable = self::PARSABLE_UNKNOWN;
195 $this->xmlParsableBuffer = '';
196 }
197
198 /**
199 * Check if this instance supports using this class
200 * @return bool
201 */
202 public static function isSupported() {
203 return function_exists( 'xml_parser_create_ns' ) && class_exists( 'XMLReader' );
204 }
205
206 /** Get the result array. Do some post-processing before returning
207 * the array, and transform any metadata that is special-cased.
208 *
209 * @return array Array of results as an array of arrays suitable for
210 * FormatMetadata::getFormattedData().
211 */
212 public function getResults() {
213 // xmp-special is for metadata that affects how stuff
214 // is extracted. For example xmpNote:HasExtendedXMP.
215
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.
220
221 $data = $this->results;
222
223 if ( isset( $data['xmp-special']['AuthorsPosition'] )
224 && is_string( $data['xmp-special']['AuthorsPosition'] )
225 && isset( $data['xmp-general']['Artist'][0] )
226 ) {
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
230 // exif:Artist prop.
231
232 $data['xmp-general']['Artist'][0] =
233 $data['xmp-special']['AuthorsPosition'] . ', '
234 . $data['xmp-general']['Artist'][0];
235 }
236
237 // Go through the LocationShown and LocationCreated
238 // changing it to the non-hierarchal form used by
239 // the other location fields.
240
241 if ( isset( $data['xmp-special']['LocationShown'][0] )
242 && is_array( $data['xmp-special']['LocationShown'][0] )
243 ) {
244 // the is_array is just paranoia. It should always
245 // be an array.
246 foreach ( $data['xmp-special']['LocationShown'] as $loc ) {
247 if ( !is_array( $loc ) ) {
248 // To avoid copying over the _type meta-fields.
249 continue;
250 }
251 foreach ( $loc as $field => $val ) {
252 $data['xmp-general'][$field . 'Dest'][] = $val;
253 }
254 }
255 }
256 if ( isset( $data['xmp-special']['LocationCreated'][0] )
257 && is_array( $data['xmp-special']['LocationCreated'][0] )
258 ) {
259 // the is_array is just paranoia. It should always
260 // be an array.
261 foreach ( $data['xmp-special']['LocationCreated'] as $loc ) {
262 if ( !is_array( $loc ) ) {
263 // To avoid copying over the _type meta-fields.
264 continue;
265 }
266 foreach ( $loc as $field => $val ) {
267 $data['xmp-general'][$field . 'Created'][] = $val;
268 }
269 }
270 }
271
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'] );
275
276 // Convert GPSAltitude to negative if below sea level.
277 if ( isset( $data['xmp-exif']['GPSAltitudeRef'] )
278 && isset( $data['xmp-exif']['GPSAltitude'] )
279 ) {
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;
284
285 if ( $data['xmp-exif']['GPSAltitudeRef'] == '1' ) {
286 $data['xmp-exif']['GPSAltitude'] *= -1;
287 }
288 unset( $data['xmp-exif']['GPSAltitudeRef'] );
289 }
290
291 return $data;
292 }
293
294 /**
295 * Main function to call to parse XMP. Use getResults to
296 * get results.
297 *
298 * Also catches any errors during processing, writes them to
299 * debug log, blanks result array and returns false.
300 *
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.
305 */
306 public function parse( $content, $allOfIt = true ) {
307 if ( !$this->xmlParser ) {
308 $this->resetXMLParser();
309 }
310 try {
311
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 ) {
315 $bom = [];
316 if ( preg_match( '/\xEF\xBB\xBF|\xFE\xFF|\x00\x00\xFE\xFF|\xFF\xFE\x00\x00|\xFF\xFE/',
317 $content, $bom )
318 ) {
319 switch ( $bom[0] ) {
320 case "\xFE\xFF":
321 $this->charset = 'UTF-16BE';
322 break;
323 case "\xFF\xFE":
324 $this->charset = 'UTF-16LE';
325 break;
326 case "\x00\x00\xFE\xFF":
327 $this->charset = 'UTF-32BE';
328 break;
329 case "\xFF\xFE\x00\x00":
330 $this->charset = 'UTF-32LE';
331 break;
332 case "\xEF\xBB\xBF":
333 $this->charset = 'UTF-8';
334 break;
335 default:
336 // this should be impossible to get to
337 throw new RuntimeException( "Invalid BOM" );
338 }
339 } else {
340 // standard specifically says, if no bom assume utf-8
341 $this->charset = 'UTF-8';
342 }
343 }
344 if ( $this->charset !== 'UTF-8' ) {
345 // don't convert if already utf-8
346 Wikimedia\suppressWarnings();
347 $content = iconv( $this->charset, 'UTF-8//IGNORE', $content );
348 Wikimedia\restoreWarnings();
349 }
350
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.' );
356 }
357
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
362 // in this case.
363 return true;
364 }
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 );
369 }
370 }
371
372 $ok = xml_parse( $this->xmlParser, $content, $allOfIt );
373 if ( !$ok ) {
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 );
379
380 $this->logger->info(
381 '{method} : Error reading XMP content: {error} ' .
382 '(file: {file}, line: {line} column: {column} ' .
383 'byte offset: {offset})',
384 [
385 'method' => __METHOD__,
386 'error_code' => $code,
387 'error' => $error,
388 'file' => $this->filename,
389 'line' => $line,
390 'column' => $col,
391 'offset' => $offset,
392 'content' => $content,
393 ] );
394 $this->results = []; // blank if error.
395 $this->destroyXMLParser();
396 return false;
397 }
398 } catch ( Exception $e ) {
399 $this->logger->warning(
400 '{method} {exception}',
401 [
402 'method' => __METHOD__,
403 'exception' => $e,
404 'file' => $this->filename,
405 'content' => $content,
406 ]
407 );
408 $this->results = [];
409 return false;
410 }
411 if ( $allOfIt ) {
412 $this->destroyXMLParser();
413 }
414
415 return true;
416 }
417
418 /** Entry point for XMPExtended blocks in jpeg files
419 *
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.
424 */
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
431 ) {
432 $this->logger->info( __METHOD__ .
433 " Ignoring XMPExtended block due to wrong guid (guid= '{guid}')",
434 [
435 'guid' => $guid,
436 'file' => $this->filename,
437 ]
438 );
439
440 return false;
441 }
442 $len = unpack( 'Nlength/Noffset', substr( $content, 32, 8 ) );
443
444 if ( !$len ||
445 $len['length'] < 4 ||
446 $len['offset'] < 0 ||
447 $len['offset'] > $len['length']
448 ) {
449 $this->logger->info(
450 __METHOD__ . 'Error reading extended XMP block, invalid length or offset.',
451 [ 'file' => $this->filename ]
452 );
453
454 return false;
455 }
456
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...
466
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 ]
471 );
472
473 return false;
474 }
475
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();
480 }
481
482 $this->extendedXMPOffset += $len['length'];
483
484 $actualContent = substr( $content, 40 );
485
486 if ( $this->extendedXMPOffset === strlen( $actualContent ) ) {
487 $atEnd = true;
488 } else {
489 $atEnd = false;
490 }
491
492 $this->logger->debug(
493 __METHOD__ . 'Parsing a XMPExtended block',
494 [ 'file' => $this->filename ]
495 );
496
497 return $this->parse( $actualContent, $atEnd );
498 }
499
500 /**
501 * Character data handler
502 * Called whenever character data is found in the xmp document.
503 *
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).
507 *
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.
511 *
512 * @param resource $parser XMLParser reference to the xml parser
513 * @param string $data Character data
514 * @throws RuntimeException On invalid data
515 */
516 function char( $parser, $data ) {
517 $data = trim( $data );
518 if ( trim( $data ) === "" ) {
519 return;
520 }
521
522 if ( !isset( $this->mode[0] ) ) {
523 throw new RuntimeException( 'Unexpected character data before first rdf:Description element' );
524 }
525
526 if ( $this->mode[0] === self::MODE_IGNORE ) {
527 return;
528 }
529
530 if ( $this->mode[0] !== self::MODE_SIMPLE
531 && $this->mode[0] !== self::MODE_QDESC
532 ) {
533 throw new RuntimeException( 'character data where not expected. (mode ' . $this->mode[0] . ')' );
534 }
535
536 // to check, how does this handle w.s.
537 if ( $this->charContent === false ) {
538 $this->charContent = $data;
539 } else {
540 $this->charContent .= $data;
541 }
542 }
543
544 /**
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).
548 *
549 * @param string $content xml string to check for parse safety
550 * @return bool true if the xml is safe to parse, false otherwise
551 */
552 private function checkParseSafety( $content ) {
553 $reader = new XMLReader();
554 $result = null;
555
556 // For XMLReader to parse incomplete/invalid XML, it has to be open()'ed
557 // instead of using XML().
558 $reader->open(
559 'data://text/plain,' . urlencode( $content ),
560 null,
561 LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_NONET
562 );
563
564 $oldDisable = libxml_disable_entity_loader( true );
565 /** @noinspection PhpUnusedLocalVariableInspection */
566 $reset = new ScopedCallback(
567 'libxml_disable_entity_loader',
568 [ $oldDisable ]
569 );
570 $reader->setParserProperty( XMLReader::SUBST_ENTITIES, false );
571
572 // Even with LIBXML_NOWARNING set, XMLReader::read gives a warning
573 // when parsing truncated XML, which causes unit tests to fail.
574 Wikimedia\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;
579 $result = true;
580 break;
581 }
582 if ( $reader->nodeType === XMLReader::DOC_TYPE ) {
583 $this->parsable = self::PARSABLE_NO;
584 $result = false;
585 break;
586 }
587 }
588 Wikimedia\restoreWarnings();
589
590 if ( !is_null( $result ) ) {
591 return $result;
592 }
593
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;
598 return false;
599 }
600
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
604 *
605 * @param string $elm Namespace of element followed by a space and then tag name of element.
606 */
607 private function endElementModeIgnore( $elm ) {
608 if ( $this->curItem[0] === $elm ) {
609 array_shift( $this->curItem );
610 array_shift( $this->mode );
611 }
612 }
613
614 /**
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
618 * results array
619 *
620 * For example, when processing:
621 * <exif:DigitalZoomRatio>0/10</exif:DigitalZoomRatio>
622 * this deals with when we hit </exif:DigitalZoomRatio>.
623 *
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).
626 *
627 * @param string $elm Namespace, space, and tag name.
628 */
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 );
635 } else {
636 list( $ns, $tag ) = explode( ' ', $elm, 2 );
637 }
638 $this->saveValue( $ns, $tag, $this->charContent );
639
640 $this->charContent = false; // reset
641 }
642 array_shift( $this->curItem );
643 array_shift( $this->mode );
644 }
645
646 /**
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.
650 *
651 * Note this means we hit the closing element not the "</rdf:Seq>".
652 *
653 * @par For example, when processing:
654 * @code{,xml}
655 * <exif:ISOSpeedRatings> <rdf:Seq> <rdf:li>64</rdf:li>
656 * </rdf:Seq> </exif:ISOSpeedRatings>
657 * @endcode
658 *
659 * This method is called when we hit the "</exif:ISOSpeedRatings>" tag.
660 *
661 * @param string $elm Namespace . space . tag name.
662 * @throws RuntimeException
663 */
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 )
670 ) {
671 throw new RuntimeException( "nesting mismatch. got a </$elm> but expected a </" .
672 $this->curItem[0] . '>' );
673 }
674
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 = $info['map_name'] ?? $tag;
680
681 if ( is_array( $info['validate'] ) ) {
682 $validate = $info['validate'];
683 } else {
684 $validator = new XMPValidate( $this->logger );
685 $validate = [ $validator, $info['validate'] ];
686 }
687
688 if ( !isset( $this->results['xmp-' . $info['map_group']][$finalName] ) ) {
689 // This can happen if all the members of the struct failed validation.
690 $this->logger->debug(
691 __METHOD__ . " <$ns:$tag> has no valid members.",
692 [ 'file' => $this->filename ]
693 );
694 } elseif ( is_callable( $validate ) ) {
695 $val =& $this->results['xmp-' . $info['map_group']][$finalName];
696 call_user_func_array( $validate, [ $info, &$val, false ] );
697 if ( is_null( $val ) ) {
698 // the idea being the validation function will unset the variable if
699 // its invalid.
700 $this->logger->info(
701 __METHOD__ . " <$ns:$tag> failed validation.",
702 [ 'file' => $this->filename ]
703 );
704 unset( $this->results['xmp-' . $info['map_group']][$finalName] );
705 }
706 } else {
707 $this->logger->warning(
708 __METHOD__ . " Validation function for $finalName (" .
709 $validate[0] . '::' . $validate[1] . '()) is not callable.',
710 [ 'file' => $this->filename ]
711 );
712 }
713 }
714
715 array_shift( $this->curItem );
716 array_shift( $this->mode );
717 $this->ancestorStruct = false;
718 $this->processingArray = false;
719 $this->itemLang = false;
720 }
721
722 /**
723 * Hit a closing element in MODE_LI (either rdf:Seq, or rdf:Bag )
724 * Add information about what type of element this is.
725 *
726 * Note we still have to hit the outer "</property>"
727 *
728 * @par For example, when processing:
729 * @code{,xml}
730 * <exif:ISOSpeedRatings> <rdf:Seq> <rdf:li>64</rdf:li>
731 * </rdf:Seq> </exif:ISOSpeedRatings>
732 * @endcode
733 *
734 * This method is called when we hit the "</rdf:Seq>".
735 * (For comparison, we call endElementModeSimple when we
736 * hit the "</rdf:li>")
737 *
738 * @param string $elm Namespace . ' ' . element name
739 * @throws RuntimeException
740 */
741 private function endElementModeLi( $elm ) {
742 list( $ns, $tag ) = explode( ' ', $this->curItem[0], 2 );
743 $info = $this->items[$ns][$tag];
744 $finalName = $info['map_name'] ?? $tag;
745
746 array_shift( $this->mode );
747
748 if ( !isset( $this->results['xmp-' . $info['map_group']][$finalName] ) ) {
749 $this->logger->debug(
750 __METHOD__ . " Empty compund element $finalName.",
751 [ 'file' => $this->filename ]
752 );
753
754 return;
755 }
756
757 if ( $elm === self::NS_RDF . ' Seq' ) {
758 $this->results['xmp-' . $info['map_group']][$finalName]['_type'] = 'ol';
759 } elseif ( $elm === self::NS_RDF . ' Bag' ) {
760 $this->results['xmp-' . $info['map_group']][$finalName]['_type'] = 'ul';
761 } elseif ( $elm === self::NS_RDF . ' Alt' ) {
762 // extra if needed as you could theoretically have a non-language alt.
763 if ( $info['mode'] === self::MODE_LANG ) {
764 $this->results['xmp-' . $info['map_group']][$finalName]['_type'] = 'lang';
765 }
766 } else {
767 throw new RuntimeException(
768 __METHOD__ . " expected </rdf:seq> or </rdf:bag> but instead got $elm."
769 );
770 }
771 }
772
773 /**
774 * End element while in MODE_QDESC
775 * mostly when ending an element when we have a simple value
776 * that has qualifiers.
777 *
778 * Qualifiers aren't all that common, and we don't do anything
779 * with them.
780 *
781 * @param string $elm Namespace and element
782 */
783 private function endElementModeQDesc( $elm ) {
784 if ( $elm === self::NS_RDF . ' value' ) {
785 list( $ns, $tag ) = explode( ' ', $this->curItem[0], 2 );
786 $this->saveValue( $ns, $tag, $this->charContent );
787
788 return;
789 } else {
790 array_shift( $this->mode );
791 array_shift( $this->curItem );
792 }
793 }
794
795 /**
796 * Handler for hitting a closing element.
797 *
798 * generally just calls a helper function depending on what
799 * mode we're in.
800 *
801 * Ignores the outer wrapping elements that are optional in
802 * xmp and have no meaning.
803 *
804 * @param resource $parser
805 * @param string $elm Namespace . ' ' . element name
806 * @throws RuntimeException
807 */
808 function endElement( $parser, $elm ) {
809 if ( $elm === ( self::NS_RDF . ' RDF' )
810 || $elm === 'adobe:ns:meta/ xmpmeta'
811 || $elm === 'adobe:ns:meta/ xapmeta'
812 ) {
813 // ignore these.
814 return;
815 }
816
817 if ( $elm === self::NS_RDF . ' type' ) {
818 // these aren't really supported properly yet.
819 // However, it appears they almost never used.
820 $this->logger->info(
821 __METHOD__ . ' encountered <rdf:type>',
822 [ 'file' => $this->filename ]
823 );
824 }
825
826 if ( strpos( $elm, ' ' ) === false ) {
827 // This probably shouldn't happen.
828 // However, there is a bug in an adobe product
829 // that forgets the namespace on some things.
830 // (Luckily they are unimportant things).
831 $this->logger->info(
832 __METHOD__ . " Encountered </$elm> which has no namespace. Skipping.",
833 [ 'file' => $this->filename ]
834 );
835
836 return;
837 }
838
839 if ( count( $this->mode ) === 0 ) {
840 // This should never ever happen and means
841 // there is a pretty major bug in this class.
842 throw new RuntimeException( 'Encountered end element with no mode' );
843 }
844
845 if ( count( $this->curItem ) == 0 && $this->mode[0] !== self::MODE_INITIAL ) {
846 // just to be paranoid. Should always have a curItem, except for initially
847 // (aka during MODE_INITAL).
848 throw new RuntimeException( "Hit end element </$elm> but no curItem" );
849 }
850
851 switch ( $this->mode[0] ) {
852 case self::MODE_IGNORE:
853 $this->endElementModeIgnore( $elm );
854 break;
855 case self::MODE_SIMPLE:
856 $this->endElementModeSimple( $elm );
857 break;
858 case self::MODE_STRUCT:
859 case self::MODE_SEQ:
860 case self::MODE_BAG:
861 case self::MODE_LANG:
862 case self::MODE_BAGSTRUCT:
863 $this->endElementNested( $elm );
864 break;
865 case self::MODE_INITIAL:
866 if ( $elm === self::NS_RDF . ' Description' ) {
867 array_shift( $this->mode );
868 } else {
869 throw new RuntimeException( 'Element ended unexpectedly while in MODE_INITIAL' );
870 }
871 break;
872 case self::MODE_LI:
873 case self::MODE_LI_LANG:
874 $this->endElementModeLi( $elm );
875 break;
876 case self::MODE_QDESC:
877 $this->endElementModeQDesc( $elm );
878 break;
879 default:
880 $this->logger->info(
881 __METHOD__ . " no mode (elm = $elm)",
882 [ 'file' => $this->filename ]
883 );
884 break;
885 }
886 }
887
888 /**
889 * Hit an opening element while in MODE_IGNORE
890 *
891 * XMP is extensible, so ignore any tag we don't understand.
892 *
893 * Mostly ignores, unless we encounter the element that we are ignoring.
894 * in which case we add it to the item stack, so we can ignore things
895 * that are nested, correctly.
896 *
897 * @param string $elm Namespace . ' ' . tag name
898 */
899 private function startElementModeIgnore( $elm ) {
900 if ( $elm === $this->curItem[0] ) {
901 array_unshift( $this->curItem, $elm );
902 array_unshift( $this->mode, self::MODE_IGNORE );
903 }
904 }
905
906 /**
907 * Start element in MODE_BAG (unordered array)
908 * this should always be <rdf:Bag>
909 *
910 * @param string $elm Namespace . ' ' . tag
911 * @throws RuntimeException If we have an element that's not <rdf:Bag>
912 */
913 private function startElementModeBag( $elm ) {
914 if ( $elm === self::NS_RDF . ' Bag' ) {
915 array_unshift( $this->mode, self::MODE_LI );
916 } else {
917 throw new RuntimeException( "Expected <rdf:Bag> but got $elm." );
918 }
919 }
920
921 /**
922 * Start element in MODE_SEQ (ordered array)
923 * this should always be <rdf:Seq>
924 *
925 * @param string $elm Namespace . ' ' . tag
926 * @throws RuntimeException If we have an element that's not <rdf:Seq>
927 */
928 private function startElementModeSeq( $elm ) {
929 if ( $elm === self::NS_RDF . ' Seq' ) {
930 array_unshift( $this->mode, self::MODE_LI );
931 } elseif ( $elm === self::NS_RDF . ' Bag' ) {
932 # T29105
933 $this->logger->info(
934 __METHOD__ . ' Expected an rdf:Seq, but got an rdf:Bag. Pretending' .
935 ' it is a Seq, since some buggy software is known to screw this up.',
936 [ 'file' => $this->filename ]
937 );
938 array_unshift( $this->mode, self::MODE_LI );
939 } else {
940 throw new RuntimeException( "Expected <rdf:Seq> but got $elm." );
941 }
942 }
943
944 /**
945 * Start element in MODE_LANG (language alternative)
946 * this should always be <rdf:Alt>
947 *
948 * This tag tends to be used for metadata like describe this
949 * picture, which can be translated into multiple languages.
950 *
951 * XMP supports non-linguistic alternative selections,
952 * which are really only used for thumbnails, which
953 * we don't care about.
954 *
955 * @param string $elm Namespace . ' ' . tag
956 * @throws RuntimeException If we have an element that's not <rdf:Alt>
957 */
958 private function startElementModeLang( $elm ) {
959 if ( $elm === self::NS_RDF . ' Alt' ) {
960 array_unshift( $this->mode, self::MODE_LI_LANG );
961 } else {
962 throw new RuntimeException( "Expected <rdf:Seq> but got $elm." );
963 }
964 }
965
966 /**
967 * Handle an opening element when in MODE_SIMPLE
968 *
969 * This should not happen often. This is for if a simple element
970 * already opened has a child element. Could happen for a
971 * qualified element.
972 *
973 * For example:
974 * <exif:DigitalZoomRatio><rdf:Description><rdf:value>0/10</rdf:value>
975 * <foo:someQualifier>Bar</foo:someQualifier> </rdf:Description>
976 * </exif:DigitalZoomRatio>
977 *
978 * This method is called when processing the <rdf:Description> element
979 *
980 * @param string $elm Namespace and tag names separated by space.
981 * @param array $attribs Attributes of the element.
982 * @throws RuntimeException
983 */
984 private function startElementModeSimple( $elm, $attribs ) {
985 if ( $elm === self::NS_RDF . ' Description' ) {
986 // If this value has qualifiers
987 array_unshift( $this->mode, self::MODE_QDESC );
988 array_unshift( $this->curItem, $this->curItem[0] );
989
990 if ( isset( $attribs[self::NS_RDF . ' value'] ) ) {
991 list( $ns, $tag ) = explode( ' ', $this->curItem[0], 2 );
992 $this->saveValue( $ns, $tag, $attribs[self::NS_RDF . ' value'] );
993 }
994 } elseif ( $elm === self::NS_RDF . ' value' ) {
995 // This should not be here.
996 throw new RuntimeException( __METHOD__ . ' Encountered <rdf:value> where it was unexpected.' );
997 } else {
998 // something else we don't recognize, like a qualifier maybe.
999 $this->logger->info( __METHOD__ .
1000 " Encountered element <{element}> where only expecting character data as value of {curitem}",
1001 [
1002 'element' => $elm,
1003 'curitem' => $this->curItem[0],
1004 'file' => $this->filename,
1005 ]
1006 );
1007 array_unshift( $this->mode, self::MODE_IGNORE );
1008 array_unshift( $this->curItem, $elm );
1009 }
1010 }
1011
1012 /**
1013 * Start an element when in MODE_QDESC.
1014 * This generally happens when a simple element has an inner
1015 * rdf:Description to hold qualifier elements.
1016 *
1017 * For example in:
1018 * <exif:DigitalZoomRatio><rdf:Description><rdf:value>0/10</rdf:value>
1019 * <foo:someQualifier>Bar</foo:someQualifier> </rdf:Description>
1020 * </exif:DigitalZoomRatio>
1021 * Called when processing the <rdf:value> or <foo:someQualifier>.
1022 *
1023 * @param string $elm Namespace and tag name separated by a space.
1024 */
1025 private function startElementModeQDesc( $elm ) {
1026 if ( $elm === self::NS_RDF . ' value' ) {
1027 return; // do nothing
1028 } else {
1029 // otherwise its a qualifier, which we ignore
1030 array_unshift( $this->mode, self::MODE_IGNORE );
1031 array_unshift( $this->curItem, $elm );
1032 }
1033 }
1034
1035 /**
1036 * Starting an element when in MODE_INITIAL
1037 * This usually happens when we hit an element inside
1038 * the outer rdf:Description
1039 *
1040 * This is generally where most properties start.
1041 *
1042 * @param string $ns Namespace
1043 * @param string $tag Tag name (without namespace prefix)
1044 * @param array $attribs Array of attributes
1045 * @throws RuntimeException
1046 */
1047 private function startElementModeInitial( $ns, $tag, $attribs ) {
1048 if ( $ns !== self::NS_RDF ) {
1049 if ( isset( $this->items[$ns][$tag] ) ) {
1050 if ( isset( $this->items[$ns][$tag]['structPart'] ) ) {
1051 // If this element is supposed to appear only as
1052 // a child of a structure, but appears here (not as
1053 // a child of a struct), then something weird is
1054 // happening, so ignore this element and its children.
1055
1056 $this->logger->info(
1057 'Encountered <{element}> outside of its expected parent. Ignoring.',
1058 [ 'element' => "$ns:$tag", 'file' => $this->filename ]
1059 );
1060
1061 array_unshift( $this->mode, self::MODE_IGNORE );
1062 array_unshift( $this->curItem, $ns . ' ' . $tag );
1063
1064 return;
1065 }
1066 $mode = $this->items[$ns][$tag]['mode'];
1067 array_unshift( $this->mode, $mode );
1068 array_unshift( $this->curItem, $ns . ' ' . $tag );
1069 if ( $mode === self::MODE_STRUCT ) {
1070 $this->ancestorStruct = $this->items[$ns][$tag]['map_name'] ?? $tag;
1071 }
1072 if ( $this->charContent !== false ) {
1073 // Something weird.
1074 // Should not happen in valid XMP.
1075 throw new RuntimeException( 'tag nested in non-whitespace characters.' );
1076 }
1077 } else {
1078 // This element is not on our list of allowed elements so ignore.
1079 $this->logger->debug( __METHOD__ . ' Ignoring unrecognized element <{element}>.',
1080 [ 'element' => "$ns:$tag", 'file' => $this->filename ] );
1081 array_unshift( $this->mode, self::MODE_IGNORE );
1082 array_unshift( $this->curItem, $ns . ' ' . $tag );
1083
1084 return;
1085 }
1086 }
1087 // process attributes
1088 $this->doAttribs( $attribs );
1089 }
1090
1091 /**
1092 * Hit an opening element when in a Struct (MODE_STRUCT)
1093 * This is generally for fields of a compound property.
1094 *
1095 * Example of a struct (abbreviated; flash has more properties):
1096 *
1097 * <exif:Flash> <rdf:Description> <exif:Fired>True</exif:Fired>
1098 * <exif:Mode>1</exif:Mode></rdf:Description></exif:Flash>
1099 *
1100 * or:
1101 *
1102 * <exif:Flash rdf:parseType='Resource'> <exif:Fired>True</exif:Fired>
1103 * <exif:Mode>1</exif:Mode></exif:Flash>
1104 *
1105 * @param string $ns Namespace
1106 * @param string $tag Tag name (no ns)
1107 * @param array $attribs Array of attribs w/ values.
1108 * @throws RuntimeException
1109 */
1110 private function startElementModeStruct( $ns, $tag, $attribs ) {
1111 if ( $ns !== self::NS_RDF ) {
1112 if ( isset( $this->items[$ns][$tag] ) ) {
1113 if ( isset( $this->items[$ns][$this->ancestorStruct]['children'] )
1114 && !isset( $this->items[$ns][$this->ancestorStruct]['children'][$tag] )
1115 ) {
1116 // This assumes that we don't have inter-namespace nesting
1117 // which we don't in all the properties we're interested in.
1118 throw new RuntimeException( " <$tag> appeared nested in <" . $this->ancestorStruct
1119 . "> where it is not allowed." );
1120 }
1121 array_unshift( $this->mode, $this->items[$ns][$tag]['mode'] );
1122 array_unshift( $this->curItem, $ns . ' ' . $tag );
1123 if ( $this->charContent !== false ) {
1124 // Something weird.
1125 // Should not happen in valid XMP.
1126 throw new RuntimeException( "tag <$tag> nested in non-whitespace characters (" .
1127 $this->charContent . ")." );
1128 }
1129 } else {
1130 array_unshift( $this->mode, self::MODE_IGNORE );
1131 array_unshift( $this->curItem, $ns . ' ' . $tag );
1132
1133 return;
1134 }
1135 }
1136
1137 if ( $ns === self::NS_RDF && $tag === 'Description' ) {
1138 $this->doAttribs( $attribs );
1139 array_unshift( $this->mode, self::MODE_STRUCT );
1140 array_unshift( $this->curItem, $this->curItem[0] );
1141 }
1142 }
1143
1144 /**
1145 * opening element in MODE_LI
1146 * process elements of arrays.
1147 *
1148 * Example:
1149 * <exif:ISOSpeedRatings> <rdf:Seq> <rdf:li>64</rdf:li>
1150 * </rdf:Seq> </exif:ISOSpeedRatings>
1151 * This method is called when we hit the <rdf:li> element.
1152 *
1153 * @param string $elm Namespace . ' ' . tagname
1154 * @param array $attribs Attributes. (needed for BAGSTRUCTS)
1155 * @throws RuntimeException If gets a tag other than <rdf:li>
1156 */
1157 private function startElementModeLi( $elm, $attribs ) {
1158 if ( ( $elm ) !== self::NS_RDF . ' li' ) {
1159 throw new RuntimeException( "<rdf:li> expected but got $elm." );
1160 }
1161
1162 if ( !isset( $this->mode[1] ) ) {
1163 // This should never ever ever happen. Checking for it
1164 // to be paranoid.
1165 throw new RuntimeException( 'In mode Li, but no 2xPrevious mode!' );
1166 }
1167
1168 if ( $this->mode[1] === self::MODE_BAGSTRUCT ) {
1169 // This list item contains a compound (STRUCT) value.
1170 array_unshift( $this->mode, self::MODE_STRUCT );
1171 array_unshift( $this->curItem, $elm );
1172 $this->processingArray = true;
1173
1174 if ( !isset( $this->curItem[1] ) ) {
1175 // be paranoid.
1176 throw new RuntimeException( 'Can not find parent of BAGSTRUCT.' );
1177 }
1178 list( $curNS, $curTag ) = explode( ' ', $this->curItem[1] );
1179 $this->ancestorStruct = $this->items[$curNS][$curTag]['map_name'] ?? $curTag;
1180
1181 $this->doAttribs( $attribs );
1182 } else {
1183 // Normal BAG or SEQ containing simple values.
1184 array_unshift( $this->mode, self::MODE_SIMPLE );
1185 // need to add curItem[0] on again since one is for the specific item
1186 // and one is for the entire group.
1187 array_unshift( $this->curItem, $this->curItem[0] );
1188 $this->processingArray = true;
1189 }
1190 }
1191
1192 /**
1193 * Opening element in MODE_LI_LANG.
1194 * process elements of language alternatives
1195 *
1196 * Example:
1197 * <dc:title> <rdf:Alt> <rdf:li xml:lang="x-default">My house
1198 * </rdf:li> </rdf:Alt> </dc:title>
1199 *
1200 * This method is called when we hit the <rdf:li> element.
1201 *
1202 * @param string $elm Namespace . ' ' . tag
1203 * @param array $attribs Array of elements (most importantly xml:lang)
1204 * @throws RuntimeException If gets a tag other than <rdf:li> or if no xml:lang
1205 */
1206 private function startElementModeLiLang( $elm, $attribs ) {
1207 if ( $elm !== self::NS_RDF . ' li' ) {
1208 throw new RuntimeException( __METHOD__ . " <rdf:li> expected but got $elm." );
1209 }
1210 if ( !isset( $attribs[self::NS_XML . ' lang'] )
1211 || !preg_match( '/^[-A-Za-z0-9]{2,}$/D', $attribs[self::NS_XML . ' lang'] )
1212 ) {
1213 throw new RuntimeException( __METHOD__
1214 . " <rdf:li> did not contain, or has invalid xml:lang attribute in lang alternative" );
1215 }
1216
1217 // Lang is case-insensitive.
1218 $this->itemLang = strtolower( $attribs[self::NS_XML . ' lang'] );
1219
1220 // need to add curItem[0] on again since one is for the specific item
1221 // and one is for the entire group.
1222 array_unshift( $this->curItem, $this->curItem[0] );
1223 array_unshift( $this->mode, self::MODE_SIMPLE );
1224 $this->processingArray = true;
1225 }
1226
1227 /**
1228 * Hits an opening element.
1229 * Generally just calls a helper based on what MODE we're in.
1230 * Also does some initial set up for the wrapper element
1231 *
1232 * @param resource $parser
1233 * @param string $elm Namespace "<space>" element
1234 * @param array $attribs Attribute name => value
1235 * @throws RuntimeException
1236 */
1237 function startElement( $parser, $elm, $attribs ) {
1238 if ( $elm === self::NS_RDF . ' RDF'
1239 || $elm === 'adobe:ns:meta/ xmpmeta'
1240 || $elm === 'adobe:ns:meta/ xapmeta'
1241 ) {
1242 /* ignore. */
1243 return;
1244 } elseif ( $elm === self::NS_RDF . ' Description' ) {
1245 if ( count( $this->mode ) === 0 ) {
1246 // outer rdf:desc
1247 array_unshift( $this->mode, self::MODE_INITIAL );
1248 }
1249 } elseif ( $elm === self::NS_RDF . ' type' ) {
1250 // This doesn't support rdf:type properly.
1251 // In practise I have yet to see a file that
1252 // uses this element, however it is mentioned
1253 // on page 25 of part 1 of the xmp standard.
1254 // Also it seems as if exiv2 and exiftool do not support
1255 // this either (That or I misunderstand the standard)
1256 $this->logger->info(
1257 __METHOD__ . ' Encountered <rdf:type> which isn\'t currently supported',
1258 [ 'file' => $this->filename ]
1259 );
1260 }
1261
1262 if ( strpos( $elm, ' ' ) === false ) {
1263 // This probably shouldn't happen.
1264 $this->logger->info(
1265 __METHOD__ . " Encountered <$elm> which has no namespace. Skipping.",
1266 [ 'file' => $this->filename ]
1267 );
1268
1269 return;
1270 }
1271
1272 list( $ns, $tag ) = explode( ' ', $elm, 2 );
1273
1274 if ( count( $this->mode ) === 0 ) {
1275 // This should not happen.
1276 throw new RuntimeException( 'Error extracting XMP, '
1277 . "encountered <$elm> with no mode" );
1278 }
1279
1280 switch ( $this->mode[0] ) {
1281 case self::MODE_IGNORE:
1282 $this->startElementModeIgnore( $elm );
1283 break;
1284 case self::MODE_SIMPLE:
1285 $this->startElementModeSimple( $elm, $attribs );
1286 break;
1287 case self::MODE_INITIAL:
1288 $this->startElementModeInitial( $ns, $tag, $attribs );
1289 break;
1290 case self::MODE_STRUCT:
1291 $this->startElementModeStruct( $ns, $tag, $attribs );
1292 break;
1293 case self::MODE_BAG:
1294 case self::MODE_BAGSTRUCT:
1295 $this->startElementModeBag( $elm );
1296 break;
1297 case self::MODE_SEQ:
1298 $this->startElementModeSeq( $elm );
1299 break;
1300 case self::MODE_LANG:
1301 $this->startElementModeLang( $elm );
1302 break;
1303 case self::MODE_LI_LANG:
1304 $this->startElementModeLiLang( $elm, $attribs );
1305 break;
1306 case self::MODE_LI:
1307 $this->startElementModeLi( $elm, $attribs );
1308 break;
1309 case self::MODE_QDESC:
1310 $this->startElementModeQDesc( $elm );
1311 break;
1312 default:
1313 throw new RuntimeException( 'StartElement in unknown mode: ' . $this->mode[0] );
1314 }
1315 }
1316
1317 // phpcs:disable Generic.Files.LineLength
1318 /**
1319 * Process attributes.
1320 * Simple values can be stored as either a tag or attribute
1321 *
1322 * Often the initial "<rdf:Description>" tag just has all the simple
1323 * properties as attributes.
1324 *
1325 * @par Example:
1326 * @code
1327 * <rdf:Description rdf:about="" xmlns:exif="http://ns.adobe.com/exif/1.0/" exif:DigitalZoomRatio="0/10">
1328 * @endcode
1329 *
1330 * @param array $attribs Array attribute=>value
1331 * @throws RuntimeException
1332 */
1333 // phpcs:enable
1334 private function doAttribs( $attribs ) {
1335 // first check for rdf:parseType attribute, as that can change
1336 // how the attributes are interperted.
1337
1338 if ( isset( $attribs[self::NS_RDF . ' parseType'] )
1339 && $attribs[self::NS_RDF . ' parseType'] === 'Resource'
1340 && $this->mode[0] === self::MODE_SIMPLE
1341 ) {
1342 // this is equivalent to having an inner rdf:Description
1343 $this->mode[0] = self::MODE_QDESC;
1344 }
1345 foreach ( $attribs as $name => $val ) {
1346 if ( strpos( $name, ' ' ) === false ) {
1347 // This shouldn't happen, but so far some old software forgets namespace
1348 // on rdf:about.
1349 $this->logger->info(
1350 __METHOD__ . ' Encountered non-namespaced attribute: ' .
1351 " $name=\"$val\". Skipping. ",
1352 [ 'file' => $this->filename ]
1353 );
1354 continue;
1355 }
1356 list( $ns, $tag ) = explode( ' ', $name, 2 );
1357 if ( $ns === self::NS_RDF ) {
1358 if ( $tag === 'value' || $tag === 'resource' ) {
1359 // resource is for url.
1360 // value attribute is a weird way of just putting the contents.
1361 $this->char( $this->xmlParser, $val );
1362 }
1363 } elseif ( isset( $this->items[$ns][$tag] ) ) {
1364 if ( $this->mode[0] === self::MODE_SIMPLE ) {
1365 throw new RuntimeException( __METHOD__
1366 . " $ns:$tag found as attribute where not allowed" );
1367 }
1368 $this->saveValue( $ns, $tag, $val );
1369 } else {
1370 $this->logger->debug(
1371 __METHOD__ . " Ignoring unrecognized element <$ns:$tag>.",
1372 [ 'file' => $this->filename ]
1373 );
1374 }
1375 }
1376 }
1377
1378 /**
1379 * Given an extracted value, save it to results array
1380 *
1381 * note also uses $this->ancestorStruct and
1382 * $this->processingArray to determine what name to
1383 * save the value under. (in addition to $tag).
1384 *
1385 * @param string $ns Namespace of tag this is for
1386 * @param string $tag Tag name
1387 * @param string $val Value to save
1388 */
1389 private function saveValue( $ns, $tag, $val ) {
1390 $info =& $this->items[$ns][$tag];
1391 $finalName = $info['map_name'] ?? $tag;
1392 if ( isset( $info['validate'] ) ) {
1393 if ( is_array( $info['validate'] ) ) {
1394 $validate = $info['validate'];
1395 } else {
1396 $validator = new XMPValidate( $this->logger );
1397 $validate = [ $validator, $info['validate'] ];
1398 }
1399
1400 if ( is_callable( $validate ) ) {
1401 call_user_func_array( $validate, [ $info, &$val, true ] );
1402 // the reasoning behind using &$val instead of using the return value
1403 // is to be consistent between here and validating structures.
1404 if ( is_null( $val ) ) {
1405 $this->logger->info(
1406 __METHOD__ . " <$ns:$tag> failed validation.",
1407 [ 'file' => $this->filename ]
1408 );
1409
1410 return;
1411 }
1412 } else {
1413 $this->logger->warning(
1414 __METHOD__ . " Validation function for $finalName (" .
1415 $validate[0] . '::' . $validate[1] . '()) is not callable.',
1416 [ 'file' => $this->filename ]
1417 );
1418 }
1419 }
1420
1421 if ( $this->ancestorStruct && $this->processingArray ) {
1422 // Aka both an array and a struct. ( self::MODE_BAGSTRUCT )
1423 $this->results['xmp-' . $info['map_group']][$this->ancestorStruct][][$finalName] = $val;
1424 } elseif ( $this->ancestorStruct ) {
1425 $this->results['xmp-' . $info['map_group']][$this->ancestorStruct][$finalName] = $val;
1426 } elseif ( $this->processingArray ) {
1427 if ( $this->itemLang === false ) {
1428 // normal array
1429 $this->results['xmp-' . $info['map_group']][$finalName][] = $val;
1430 } else {
1431 // lang array.
1432 $this->results['xmp-' . $info['map_group']][$finalName][$this->itemLang] = $val;
1433 }
1434 } else {
1435 $this->results['xmp-' . $info['map_group']][$finalName] = $val;
1436 }
1437 }
1438 }