diff options
author | sgiehl <stefan@piwik.org> | 2013-08-30 20:46:42 +0400 |
---|---|---|
committer | sgiehl <stefan@piwik.org> | 2013-09-04 02:15:15 +0400 |
commit | 236f0379ba878604441d24ba2ea03cc324260919 (patch) | |
tree | 90d7f5a5c2c4dcd623d9be78507b3ae5e6fcc288 /core | |
parent | 8b01230fc5f0424e2dc8721c29737f7f4b25498c (diff) |
added translation validators
Diffstat (limited to 'core')
-rw-r--r-- | core/Translate/Validate/BaseTranslations.php | 88 | ||||
-rw-r--r-- | core/Translate/Validate/NoScripts.php | 46 | ||||
-rw-r--r-- | core/Translate/Validate/ValidateAbstract.php | 52 |
3 files changed, 186 insertions, 0 deletions
diff --git a/core/Translate/Validate/BaseTranslations.php b/core/Translate/Validate/BaseTranslations.php new file mode 100644 index 0000000000..ceae7e3811 --- /dev/null +++ b/core/Translate/Validate/BaseTranslations.php @@ -0,0 +1,88 @@ +<?php +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + * @category Piwik + * @package Piwik + */ + +namespace Piwik\Translate\Validate; + +use Piwik\Translate\Validate\ValidateAbstract; +use Piwik\Common; + +/** + * @package Piwik + * @subpackage Piwik_Translate + */ +class BaseTranslations extends ValidateAbstract +{ + /** + * Error States + */ + const __ERRORSTATE_MINIMUMTRANSLATIONS__ = 'At least 250 translations required'; + const __ERRORSTATE_LOCALEREQUIRED__ = 'Locale required'; + const __ERRORSTATE_TRANSLATORINFOREQUIRED__ = 'Translator info required'; + const __ERRORSTATE_TRANSLATOREMAILREQUIRED__ = 'Translator email required'; + const __ERRORSTATE_LAYOUTDIRECTIONINVALID__ = 'Layout direction must be rtl or ltr'; + const __ERRORSTATE_LOCALEINVALID__ = 'Locale is invalid'; + const __ERRORSTATE_LOCALEINVALIDLANGUAGE__ = 'Locale is invalid - invalid language code'; + const __ERRORSTATE_LOCALEINVALIDCOUNTRY__ = 'Locale is invalid - invalid country code'; + + /** + * Validates the given translations + * + * @param array $translations + * + * @return boolean + * + */ + public function isValid($translations) + { + if (250 > count($translations, COUNT_RECURSIVE)) { + $this->_error = self::__ERRORSTATE_MINIMUMTRANSLATIONS__; + return false; + } + + if (empty($translations['General']['Locale'])) { + $this->_error = self::__ERRORSTATE_LOCALEREQUIRED__; + return false; + } + + if (empty($translations['General']['TranslatorName'])) { + $this->_error = self::__ERRORSTATE_TRANSLATORINFOREQUIRED__; + return false; + } + + if (empty($translations['General']['TranslatorEmail'])) { + $this->_error = self::__ERRORSTATE_TRANSLATOREMAILREQUIRED__; + return false; + } + + if (!empty($translations['General']['LayoutDirection']) && + !in_array($translations['General']['LayoutDirection'], array('ltr', 'rtl')) + ) { + $this->_error = self::__ERRORSTATE_LAYOUTDIRECTIONINVALID__; + return false; + } + + $allLanguages = Common::getLanguagesList(); + $allCountries = Common::getCountriesList(); + + if (!preg_match('/^([a-z]{2})_([A-Z]{2})\.UTF-8$/', $translations['General']['Locale'], $matches)) { + $this->_error = self::__ERRORSTATE_LOCALEINVALID__; + return false; + } else if (!array_key_exists($matches[1], $allLanguages)) { + $this->_error = self::__ERRORSTATE_LOCALEINVALIDLANGUAGE__; + return false; + } else if (!array_key_exists(strtolower($matches[2]), $allCountries)) { + $this->_error = self::__ERRORSTATE_LOCALEINVALIDCOUNTRY__; + return false; + } + + return true; + } +}
\ No newline at end of file diff --git a/core/Translate/Validate/NoScripts.php b/core/Translate/Validate/NoScripts.php new file mode 100644 index 0000000000..b3017b840c --- /dev/null +++ b/core/Translate/Validate/NoScripts.php @@ -0,0 +1,46 @@ +<?php +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + * @category Piwik + * @package Piwik + */ + +namespace Piwik\Translate\Validate; + +use Piwik\Translate\Validate\ValidateAbstract; +use Piwik\Common; + +/** + * @package Piwik + * @subpackage Piwik_Translate + */ +class NoScripts extends ValidateAbstract +{ + /** + * Validates the given translations + * + * @param array $translations + * + * @return boolean + * + */ + public function isValid($translations) + { + $this->_error = null; + + // check if any translation contains restricted script tags + $serializedStrings = serialize($translations); + $invalids = array("<script", 'document.', 'javascript:', 'src=', 'background=', 'onload='); + foreach ($invalids as $invalid) { + if (stripos($serializedStrings, $invalid) !== false) { + $this->_error = 'script tags restricted for language files'; + return false; + } + } + return true; + } +}
\ No newline at end of file diff --git a/core/Translate/Validate/ValidateAbstract.php b/core/Translate/Validate/ValidateAbstract.php new file mode 100644 index 0000000000..6fdee45ed2 --- /dev/null +++ b/core/Translate/Validate/ValidateAbstract.php @@ -0,0 +1,52 @@ +<?php +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + * @category Piwik + * @package Piwik + */ + +namespace Piwik\Translate\Validate; + +/** + * @package Piwik + * @subpackage Piwik_Db + */ +abstract class ValidateAbstract +{ + protected $_baseTranslations = array(); + + protected $_error = null; + + /** + * Sets base translations + * + * @param array $baseTranslations + */ + public function __construct($baseTranslations=array()) + { + $this->_baseTranslations = $baseTranslations; + } + + /** + * Returns if the given translations are valid + * + * @param array $translations + * + * @return boolean + * + */ + abstract public function isValid($translations); + + /** + * Returns the error occured while validating + * @return mixed + */ + public function getError() + { + return $this->_error; + } +}
\ No newline at end of file |