Skip to content
Extraits de code Groupes Projets
Valider 0e03cf55 rédigé par Thomas Steur's avatar Thomas Steur
Parcourir les fichiers

throw an exception in case a plugin contains a malformed json translation file

parent 7090ace9
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -584,6 +584,41 @@ class Common
return json_decode($json, $assoc);
}
/**
* Detects whether an error occurred during the last json encode/decode.
* @return bool
*/
public static function hasJsonErrorOccurred()
{
return json_last_error() != JSON_ERROR_NONE;
}
/**
* Returns a human readable error message in case an error occcurred during the last json encode/decode.
* Returns an empty string in case there was no error.
*
* @return string
*/
public static function getLastJsonError()
{
switch (json_last_error()) {
case JSON_ERROR_NONE:
return '';
case JSON_ERROR_DEPTH:
return 'Maximum stack depth exceeded';
case JSON_ERROR_STATE_MISMATCH:
return 'Underflow or the modes mismatch';
case JSON_ERROR_CTRL_CHAR:
return 'Unexpected control character found';
case JSON_ERROR_SYNTAX:
return 'Syntax error, malformed JSON';
case JSON_ERROR_UTF8:
return 'Malformed UTF-8 characters, possibly incorrectly encoded';
}
return 'Unknown error';
}
/**
* Returns the list of parent classes for the given class.
*
......
......@@ -11,6 +11,7 @@
namespace Piwik\Plugin;
use Piwik\Common;
use Piwik\Config as PiwikConfig;
use Piwik\EventDispatcher;
use Piwik\Filesystem;
......@@ -742,11 +743,9 @@ class Manager extends Singleton
$defaultEnglishLangPath = sprintf($path, 'en');
if (file_exists($defaultLangPath)) {
$data = file_get_contents($defaultLangPath);
$translations = json_decode($data, true);
$translations = $this->getTranslationsFromFile($defaultLangPath);
} elseif (file_exists($defaultEnglishLangPath)) {
$data = file_get_contents($defaultEnglishLangPath);
$translations = json_decode($data, true);
$translations = $this->getTranslationsFromFile($defaultEnglishLangPath);
} else {
return false;
}
......@@ -897,6 +896,27 @@ class Manager extends Singleton
}
}
}
/**
* @param string $pathToTranslationFile
* @throws \Exception
* @return mixed
*/
private function getTranslationsFromFile($pathToTranslationFile)
{
$data = file_get_contents($pathToTranslationFile);
$translations = json_decode($data, true);
if (is_null($translations) && Common::hasJsonErrorOccurred()) {
$jsonError = Common::getLastJsonError();
$message = sprintf('Not able to load translation file %s: %s', $pathToTranslationFile, $jsonError);
throw new \Exception($message);
}
return $translations;
}
}
/**
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter