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