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