[BUGFIX] Fatal error if xlf file has wrong format
authorJigal van Hemert <jigal@xs4all.nl>
Sat, 10 Mar 2012 09:25:51 +0000 (10:25 +0100)
committerSteffen Ritter <info@rs-websystems.de>
Sun, 11 Mar 2012 11:49:04 +0000 (12:49 +0100)
Check if there is a file->body element before trying to parse it
further.

Change-Id: I07c4f88199c7c8102ad5c02f19675a06f6c67b32
Fixes: #34473
Releases: 4.8, 4.7, 4.6
Reviewed-on: http://review.typo3.org/9530
Reviewed-by: Xavier Perseguers
Tested-by: Xavier Perseguers
Reviewed-by: Steffen Ritter
Tested-by: Steffen Ritter
t3lib/l10n/parser/class.t3lib_l10n_parser_xliff.php

index 30259d8..d6fc051 100644 (file)
@@ -44,61 +44,62 @@ class t3lib_l10n_parser_Xliff extends t3lib_l10n_parser_AbstractXml {
                $parsedData = array();
                $bodyOfFileTag = $root->file->body;
 
-               foreach ($bodyOfFileTag->children() as $translationElement) {
-                       if ($translationElement->getName() === 'trans-unit' && !isset($translationElement['restype'])) {
-                                       // If restype would be set, it could be metadata from Gettext to XLIFF conversion (and we don't need this data)
+               if ($bodyOfFileTag instanceof SimpleXMLElement) {
+                       foreach ($bodyOfFileTag->children() as $translationElement) {
+                               if ($translationElement->getName() === 'trans-unit' && !isset($translationElement['restype'])) {
+                                               // If restype would be set, it could be metadata from Gettext to XLIFF conversion (and we don't need this data)
 
-                               if ($this->languageKey === 'default') {
-                                               // Default language coming from an XLIFF template (no target element)
-                                       $parsedData[(string)$translationElement['id']][0] = array(
-                                               'source' => (string)$translationElement->source,
-                                               'target' => (string)$translationElement->source,
-                                       );
-                               } else {
-                                               // @todo Support "approved" attribute
-                                       $parsedData[(string)$translationElement['id']][0] = array(
-                                               'source' => (string)$translationElement->source,
-                                               'target' => (string)$translationElement->target,
-                                       );
-                               }
-                       } elseif ($translationElement->getName() === 'group' && isset($translationElement['restype']) && (string)$translationElement['restype'] === 'x-gettext-plurals') {
-                                       // This is a translation with plural forms
-                               $parsedTranslationElement = array();
+                                       if ($this->languageKey === 'default') {
+                                                       // Default language coming from an XLIFF template (no target element)
+                                               $parsedData[(string)$translationElement['id']][0] = array(
+                                                       'source' => (string)$translationElement->source,
+                                                       'target' => (string)$translationElement->source,
+                                               );
+                                       } else {
+                                                       // @todo Support "approved" attribute
+                                               $parsedData[(string)$translationElement['id']][0] = array(
+                                                       'source' => (string)$translationElement->source,
+                                                       'target' => (string)$translationElement->target,
+                                               );
+                                       }
+                               } elseif ($translationElement->getName() === 'group' && isset($translationElement['restype']) && (string)$translationElement['restype'] === 'x-gettext-plurals') {
+                                               // This is a translation with plural forms
+                                       $parsedTranslationElement = array();
 
-                               foreach ($translationElement->children() as $translationPluralForm) {
-                                       if ($translationPluralForm->getName() === 'trans-unit') {
-                                                       // When using plural forms, ID looks like this: 1[0], 1[1] etc
-                                               $formIndex = substr((string)$translationPluralForm['id'], strpos((string)$translationPluralForm['id'], '[') + 1, -1);
+                                       foreach ($translationElement->children() as $translationPluralForm) {
+                                               if ($translationPluralForm->getName() === 'trans-unit') {
+                                                               // When using plural forms, ID looks like this: 1[0], 1[1] etc
+                                                       $formIndex = substr((string)$translationPluralForm['id'], strpos((string)$translationPluralForm['id'], '[') + 1, -1);
 
-                                               if ($this->languageKey === 'default') {
-                                                               // Default language come from XLIFF template (no target element)
-                                                       $parsedTranslationElement[(int)$formIndex] = array(
-                                                               'source' => (string)$translationPluralForm->source,
-                                                               'target' => (string)$translationPluralForm->source,
-                                                       );
-                                               } else {
-                                                               // @todo Support "approved" attribute
-                                                       $parsedTranslationElement[(int)$formIndex] = array(
-                                                               'source' => (string)$translationPluralForm->source,
-                                                               'target' => (string)$translationPluralForm->target,
-                                                       );
+                                                       if ($this->languageKey === 'default') {
+                                                                       // Default language come from XLIFF template (no target element)
+                                                               $parsedTranslationElement[(int)$formIndex] = array(
+                                                                       'source' => (string)$translationPluralForm->source,
+                                                                       'target' => (string)$translationPluralForm->source,
+                                                               );
+                                                       } else {
+                                                                       // @todo Support "approved" attribute
+                                                               $parsedTranslationElement[(int)$formIndex] = array(
+                                                                       'source' => (string)$translationPluralForm->source,
+                                                                       'target' => (string)$translationPluralForm->target,
+                                                               );
+                                                       }
                                                }
                                        }
-                               }
 
-                               if (!empty($parsedTranslationElement)) {
-                                       if (isset($translationElement['id'])) {
-                                               $id = (string)$translationElement['id'];
-                                       } else {
-                                               $id = (string)($translationElement->{'trans-unit'}[0]['id']);
-                                               $id = substr($id, 0, strpos($id, '['));
-                                       }
+                                       if (!empty($parsedTranslationElement)) {
+                                               if (isset($translationElement['id'])) {
+                                                       $id = (string)$translationElement['id'];
+                                               } else {
+                                                       $id = (string)($translationElement->{'trans-unit'}[0]['id']);
+                                                       $id = substr($id, 0, strpos($id, '['));
+                                               }
 
-                                       $parsedData[$id] = $parsedTranslationElement;
+                                               $parsedData[$id] = $parsedTranslationElement;
+                                       }
                                }
                        }
                }
-
                return $parsedData;
        }
 }