summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--config.php2
-rw-r--r--inc/dictionary.inc.php85
-rw-r--r--inc/up_json_encode.php170
-rw-r--r--index.php18
-rw-r--r--lang/de/sysconfig/branding-start.json2
-rw-r--r--lang/i18n.php57
-rw-r--r--modules/translation.inc.php345
-rw-r--r--templates/main-menu-login.html6
-rw-r--r--templates/main-menu-logout.html6
9 files changed, 453 insertions, 238 deletions
diff --git a/config.php b/config.php
index cd271e0c..43cc140c 100644
--- a/config.php
+++ b/config.php
@@ -14,8 +14,6 @@ define('CONFIG_SQL_PASS', 'geheim');
define('CONFIG_SQL_FORCE_UTF8', false);
//define('CONFIG_SQL_DB', 'openslx');
-define ("SITE_LANGUAGES", serialize (array ("de", "en", "pt")));
-
define('CONFIG_TGZ_LIST_DIR', '/opt/openslx/configs');
define('CONFIG_REMOTE_ML', 'http://mltk.boot.openslx.org/update/new');
diff --git a/inc/dictionary.inc.php b/inc/dictionary.inc.php
new file mode 100644
index 00000000..b8a888d5
--- /dev/null
+++ b/inc/dictionary.inc.php
@@ -0,0 +1,85 @@
+<?php
+
+class Dictionary
+{
+
+ private static $messageArray;
+ private static $languages;
+
+ public static function init()
+ {
+ self::$languages = array();
+ foreach (glob('lang/??', GLOB_ONLYDIR) as $lang) {
+ $lang = basename($lang);
+ if ($lang === '..')
+ continue;
+ self::$languages[] = $lang;
+ }
+
+ //Changes the language in case there is a request to
+ $lang = Request::get('lang');
+ if ($lang !== false && in_array($lang, self::$languages)) {
+ setcookie('lang', $lang, time() + 60 * 60 * 24 * 30 * 12);
+ $url = Request::get('url');
+ if ($url === false && isset($_SERVER['HTTP_REFERER']))
+ $url = $_SERVER['HTTP_REFERER'];
+ if ($url === false)
+ $url = '?do=Main';
+ Util::redirect($url);
+ }
+
+ //Default language
+ $language = 'en';
+
+ if (isset($_COOKIE['lang']) && in_array($_COOKIE['lang'], self::$languages)) {
+ // Did user override language?
+ $language = $_COOKIE['lang'];
+ } else if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
+ $langs = preg_split('/[,\s]+/', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
+ foreach ($langs as $lang) {
+ $lang = substr($lang, 0, 2);
+ if (in_array($lang, self::$languages)) {
+ $language = $lang;
+ break;
+ }
+ }
+ }
+
+ define('LANG', $language);
+ self::$messageArray = json_decode(file_get_contents("lang/" . LANG . "/messages.json"), true);
+ }
+
+ public static function getArrayTemplate($template, $lang = false)
+ {
+ if ($lang === false)
+ $lang = LANG;
+ $file = "lang/" . $lang . "/" . $template . ".json";
+ $content = @file_get_contents($file);
+ if ($content === false)
+ Util::traceError("Could not find language file $template for language $lang");
+ $language = array('lang' => $lang);
+ $json = json_decode($content, true);
+ if (!is_array($json))
+ return $language;
+ return array_merge($language, $json);
+ }
+
+ public static function translate($string)
+ {
+ $hardcoded = json_decode(file_get_contents("lang/" . LANG . "/messages-hardcoded.json"), true);
+ return $hardcoded[$string];
+ }
+
+ public static function getMessages()
+ {
+ return self::$messageArray;
+ }
+
+ public static function getLanguages()
+ {
+ return self::$languages;
+ }
+
+}
+
+Dictionary::init();
diff --git a/inc/up_json_encode.php b/inc/up_json_encode.php
new file mode 100644
index 00000000..b3b843c1
--- /dev/null
+++ b/inc/up_json_encode.php
@@ -0,0 +1,170 @@
+<?php
+
+/**
+ * api: php
+ * title: upgrade.php
+ * description: Emulates functions from new PHP versions on older interpreters.
+ * version: 19
+ * license: Public Domain
+ * url: http://freshmeat.net/projects/upgradephp
+ * type: functions
+ * category: library
+ * priority: auto
+ * load_if: (PHP_VERSION<5.2)
+ * sort: -255
+ * provides: upgrade-php, api:php5, json
+ *
+ *
+ * By loading this library you get PHP version independence. It provides
+ * downwards compatibility to older PHP interpreters by emulating missing
+ * functions or constants using IDENTICAL NAMES. So this doesn't slow down
+ * script execution on setups where the native functions already exist. It
+ * is meant as quick drop-in solution. It spares you from rewriting code or
+ * using cumbersome workarounds instead of the more powerful v5 functions.
+ *
+ * It cannot mirror PHP5s extended OO-semantics and functionality into PHP4
+ * however. A few features are added here that weren't part of PHP yet. And
+ * some other function collections are separated out into the ext/ directory.
+ * It doesn't produce many custom error messages (YAGNI), and instead leaves
+ * reporting to invoked functions or for native PHP execution.
+ *
+ * And further this is PUBLIC DOMAIN (no copyright, no license, no warranty)
+ * so therefore compatible to ALL open source licenses. You could rip this
+ * paragraph out to republish this instead only under more restrictive terms
+ * or your favorite license (GNU LGPL/GPL, BSDL, MPL/CDDL, Artistic/PHPL, ..)
+ *
+ * Any contribution is appreciated. <milky*users#sf#net>
+ *
+ */
+/**
+ * -------------------------- FUTURE ---
+ * @group SVN
+ * @since future
+ *
+ * Following functions aren't implemented in current PHP versions, but
+ * might already be in CVS/SVN.
+ *
+ * @removed
+ * setcookie2
+ *
+ */
+/**
+ * Converts PHP variable or array into a "JSON" (JavaScript value expression
+ * or "object notation") string.
+ *
+ * @compat
+ * Output seems identical to PECL versions. "Only" 20x slower than PECL version.
+ * @bugs
+ * Doesn't take care with unicode too much - leaves UTF-8 sequences alone.
+ *
+ * @param $var mixed PHP variable/array/object
+ * @return string transformed into JSON equivalent
+ */
+if (!defined("JSON_HEX_TAG")) {
+ define("JSON_HEX_TAG", 1);
+ define("JSON_HEX_AMP", 2);
+ define("JSON_HEX_APOS", 4);
+ define("JSON_HEX_QUOT", 8);
+ define("JSON_FORCE_OBJECT", 16);
+}
+if (!defined("JSON_NUMERIC_CHECK")) {
+ define("JSON_NUMERIC_CHECK", 32); // 5.3.3
+}
+if (!defined("JSON_UNESCAPED_SLASHES")) {
+ define("JSON_UNESCAPED_SLASHES", 64); // 5.4.0
+ define("JSON_PRETTY_PRINT", 128); // 5.4.0
+ define("JSON_UNESCAPED_UNICODE", 256); // 5.4.0
+}
+
+function up_json_encode($var, $options = 0, $_indent = "")
+{
+ global ${'.json_last_error'};
+ ${'.json_last_error'} = JSON_ERROR_NONE;
+
+ #-- prepare JSON string
+ $obj = ($options & JSON_FORCE_OBJECT);
+ list($_space, $_tab, $_nl) = ($options & JSON_PRETTY_PRINT) ? array(" ", " $_indent", "\n") : array("", "", "");
+ $json = "$_indent";
+
+ if ($options & JSON_NUMERIC_CHECK and is_string($var) and is_numeric($var)) {
+ $var = (strpos($var, ".") || strpos($var, "e")) ? floatval($var) : intval($var);
+ }
+
+ #-- add array entries
+ if (is_array($var) || ($obj = is_object($var))) {
+
+ #-- check if array is associative
+ if (!$obj) {
+ $keys = array_keys((array) $var);
+ $obj = !($keys == array_keys($keys)); // keys must be in 0,1,2,3, ordering, but PHP treats integers==strings otherwise
+ }
+
+ #-- concat individual entries
+ $empty = 0;
+ $json = "";
+ foreach ((array) $var as $i => $v) {
+ $json .= ($empty++ ? ",$_nl" : "") // comma separators
+ . $_tab . ($obj ? (up_json_encode($i, $options & ~JSON_NUMERIC_CHECK, $_tab) . ":$_space") : "") // assoc prefix
+ . (up_json_encode($v, $options, $_tab)); // value
+ }
+
+ #-- enclose into braces or brackets
+ $json = $obj ? "{" . "$_nl$json$_nl$_indent}" : "[$_nl$json$_nl$_indent]";
+ }
+
+ #-- strings need some care
+ elseif (is_string($var)) {
+
+ if (!empty($var) && mb_detect_encoding($var, 'UTF-8', true) === false) {
+ trigger_error("up_json_encode: invalid UTF-8 encoding in string '$var', cannot proceed.", E_USER_WARNING);
+ $var = NULL;
+ }
+ $rewrite = array(
+ "\\" => "\\\\",
+ "\"" => "\\\"",
+ "\010" => "\\b",
+ "\f" => "\\f",
+ "\n" => "\\n",
+ "\r" => "\\r",
+ "\t" => "\\t",
+ "/" => $options & JSON_UNESCAPED_SLASHES ? "/" : "\\/",
+ "<" => $options & JSON_HEX_TAG ? "\\u003C" : "<",
+ ">" => $options & JSON_HEX_TAG ? "\\u003E" : ">",
+ "'" => $options & JSON_HEX_APOS ? "\\u0027" : "'",
+ "\"" => $options & JSON_HEX_QUOT ? "\\u0022" : "\"",
+ "&" => $options & JSON_HEX_AMP ? "\\u0026" : "&",
+ );
+ $var = strtr($var, $rewrite);
+ //@COMPAT control chars should probably be stripped beforehand, not escaped as here
+ if (function_exists("iconv") && ($options & JSON_UNESCAPED_UNICODE) == 0) {
+ $var = preg_replace("/[^\\x{0020}-\\x{007F}]/ue", "'\\u'.current(unpack('H*', iconv('UTF-8', 'UCS-2BE', '$0')))", $var);
+ }
+ $json = '"' . $var . '"';
+ }
+
+ #-- basic types
+ elseif (is_bool($var)) {
+ $json = $var ? "true" : "false";
+ } elseif ($var === NULL) {
+ $json = "null";
+ } elseif (is_int($var)) {
+ $json = "$var";
+ } elseif (is_float($var)) {
+ if (is_nan($var) || is_infinite($var)) {
+ ${'.json_last_error'} = JSON_ERROR_INF_OR_NAN;
+ return;
+ } else {
+ $json = "$var";
+ }
+ }
+
+ #-- something went wrong
+ else {
+ trigger_error("up_json_encode: don't know what a '" . gettype($var) . "' is.", E_USER_WARNING);
+ ${'.json_last_error'} = JSON_ERROR_UNSUPPORTED_TYPE;
+ return;
+ }
+
+ #-- done
+ return($json);
+}
diff --git a/index.php b/index.php
index 1a48ad6f..8efb21ed 100644
--- a/index.php
+++ b/index.php
@@ -2,10 +2,6 @@
require_once 'config.php';
-require_once 'lang/i18n.php';
-
-Dictionary::build();
-
require_once('inc/user.inc.php');
/**
@@ -119,10 +115,16 @@ Page::preprocess();
// Generate Main menu
//$menu = new Menu;
-if(User::getName() === false) Render::addTemplate('main-menu-login');
-else Render::addTemplate('main-menu-logout',array(
- 'user' => User::getName()
- ));
+if (User::getName() === false) {
+ Render::addTemplate('main-menu-login', array(
+ 'url' => urlencode($_SERVER['REQUEST_URI'])
+ ));
+} else {
+ Render::addTemplate('main-menu-logout', array(
+ 'url' => urlencode($_SERVER['REQUEST_URI']),
+ 'user' => User::getName()
+ ));
+}
Message::renderList();
diff --git a/lang/de/sysconfig/branding-start.json b/lang/de/sysconfig/branding-start.json
index c32a137b..c93704a7 100644
--- a/lang/de/sysconfig/branding-start.json
+++ b/lang/de/sysconfig/branding-start.json
@@ -1,5 +1,5 @@
{
- "lang_branding":"Für beste Ergebnisse sollten Sie ihr Einrichtungslogo im SVG Format hochladen. das SVG-Format ist ein Vektorgrafikformat, was zum Skalieren vorteilhaft ist. Eine Gute Quelle für SVG-Logos von Unis und Hochschulen ist ihr jeweiliger Wikipedia-Artikel.",
+ "lang_branding":"Für beste Ergebnisse sollten Sie ihr Einrichtungslogo im SVG Format hochladen. Das SVG-Format ist ein Vektorgrafikformat, was zum Skalieren vorteilhaft ist. Eine Gute Quelle für SVG-Logos von Unis und Hochschulen ist ihr jeweiliger Wikipedia-Artikel.",
"lang_or":"oder",
"lang_upload":"Hochladen",
"lang_urlLoad":"Bild von URL laden",
diff --git a/lang/i18n.php b/lang/i18n.php
deleted file mode 100644
index a77efdcb..00000000
--- a/lang/i18n.php
+++ /dev/null
@@ -1,57 +0,0 @@
-<?php
-
-class Dictionary{
- private static $messageArray;
-
- //loads the messages and store them in the class
- function build(){
- self::$messageArray = json_decode(file_get_contents("lang/" . LANG . "/messages.json"),true);
- }
-
- //return an array with the JSON tags from the user's language or a specified language
- public static function getArrayTemplate($template,$lang = false){
- $language = array('lang'=>LANG);
- if(!$lang) $lang = LANG;
- return array_merge($language,json_decode(file_get_contents("lang/" . $lang . "/" . $template . ".json"),true));
- }
-
- //translate a hardcoded message
- public static function translate($string){
- $hardcoded = json_decode(file_get_contents("lang/" . LANG . "/messages-hardcoded.json"),true);
- return $hardcoded[$string];
- }
-
- //returns an array with the JSON message tags
- public static function getMessages(){
- return self::$messageArray;
- }
-
-}
-
-
- //Array containing the allowed languages for the website
- $langArray = unserialize(SITE_LANGUAGES);
-
-
- //Changes the language in case there is a request to
- if(isset($_GET['lang']))
- if(in_array($_GET['lang'],$langArray)){
- setcookie('lang',$_GET['lang'],time()+60*60*24*30*12);
- header('Location: ' . $_SERVER['HTTP_REFERER']);
- }
-
- //Default language
- $language = 'en';
-
- //Language from the browser
- $langBrowser = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
-
- //User language
- if(isset($_COOKIE['lang']) && in_array($_COOKIE['lang'],$langArray)){
- $language = $_COOKIE['lang'];
- }else if(in_array($langBrowser,$langArray)){
- $language = $langBrowser;
- }
-
- define('LANG', $language);
-?>
diff --git a/modules/translation.inc.php b/modules/translation.inc.php
index f563e8b0..fccde7cf 100644
--- a/modules/translation.inc.php
+++ b/modules/translation.inc.php
@@ -2,6 +2,7 @@
class Page_Translation extends Page
{
+
/**
* The pages where you can administrate the website translations
* @var string|boolean holds the target template
@@ -15,231 +16,237 @@ class Page_Translation extends Page
private $update = false;
private $delete = false;
private $tags = false;
-
+
protected function doPreprocess()
{
User::load();
-
+
if (!User::hasPermission('superadmin')) {
Message::addError('no-permission');
Util::redirect('?do=Main');
}
-
- if(Request::get('template')){
+
+ if (Request::get('template')) {
$this->template = Request::get('template');
}
-
- if(Request::get('page')){
+
+ if (Request::get('page')) {
$this->page = Request::get('page');
}
-
- if(Request::get('delete')){
+
+ if (Request::get('delete')) {
$this->delete = Request::get('delete');
}
-
- if(Request::post('update')){
+
+ if (Request::post('update')) {
$this->update = Request::post('update');
}
-
}
protected function doRender()
{
//calls the update function
- if($this->update) $this->updateJson();
+ if ($this->update)
+ $this->updateJson();
//calls the tag deletion function
- if($this->delete && $this->template) $this->deleteTag($this->template,$this->delete);
-
+ if ($this->delete && $this->template)
+ $this->deleteTag($this->template, $this->delete);
+
//load the page accordingly to the link
- switch($this->page){
- case 'messages':
- //renders the message edition page
- Render::addTemplate('translation/messages', array(
+ switch ($this->page) {
+ case 'messages':
+ //renders the message edition page
+ Render::addTemplate('translation/messages', array(
'token' => Session::get('token'),
'msgs' => $this->initMsg(false),
'msgsHC' => $this->initMsg(true)
));
- break;
- case 'templates':
- //renders the tag edition page
- if($this->templateAnalysis($this->template)){
- Render::addTemplate('translation/template', array(
+ break;
+ case 'templates':
+ //renders the tag edition page
+ if ($this->templateAnalysis($this->template)) {
+ Render::addTemplate('translation/template', array(
+ 'token' => Session::get('token'),
+ 'template' => $this->template,
+ 'tags' => $this->tags
+ ));
+ break;
+ }
+ default:
+ //renders the template selection page
+ Render::addTemplate('translation/_page', array(
'token' => Session::get('token'),
- 'template' => $this->template,
- 'tags' => $this->tags
+ 'table' => $this->initTable(),
));
- break;
- }
- default:
- //renders the template selection page
- Render::addTemplate('translation/_page', array(
- 'token' => Session::get('token'),
- 'adminMessage' => $this->message,
- 'table' => $this->initTable(),
- ));
}
-
-
}
-
+
/**
* Load the main table with all the website's templates and it's informations
* @return array with the templates' information
*/
- private function initTable(){
+ private function initTable()
+ {
$table = array();
-
+
//loads every template
$files = $this->listTemplates();
//loads every json from each language
$de = $this->listJson('de/');
$en = $this->listJson('en/');
$pt = $this->listJson('pt/');
-
+
//checks the JSON tags from every language
- foreach($files as $key => $value){
+ foreach ($files as $key => $value) {
$table[] = array(
'template' => $value,
'link' => $key,
- 'de' => $this->checkJson($de[$key],'de'),
- 'en' => $this->checkJson($en[$key],'en'),
- 'pt' => $this->checkJson($pt[$key],'pt')
+ 'de' => $this->checkJson($de[$key], 'de'),
+ 'en' => $this->checkJson($en[$key], 'en'),
+ 'pt' => $this->checkJson($pt[$key], 'pt')
);
}
sort($table);
return $table;
}
-
+
/**
* Finds and returns all the website's templates
* @return array
*/
- private function listTemplates(){
+ private function listTemplates()
+ {
$files = array();
$dir = 'templates/';
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
- foreach($objects as $name => $object){
- if(array_pop(explode('.',$name)) === 'html'){
+ foreach ($objects as $name => $object) {
+ if (array_pop(explode('.', $name)) === 'html') {
$key = str_replace($dir, '', $name);
$key = str_replace('.html', '', $key);
- $files[$key] = $name;
- }
+ $files[$key] = $name;
+ }
}
return $files;
}
-
+
/**
* Finds and returns all the JSON files from a selected language
* @param string the selected language (abbreviated)
* @return array all the JSON files from the language
*/
- private function listJson($lang){
+ private function listJson($lang)
+ {
$json = array();
$dir = 'lang/' . $lang;
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
- foreach($objects as $name => $object){
- if(array_pop(explode('.',$name)) === 'json'){
+ foreach ($objects as $name => $object) {
+ if (array_pop(explode('.', $name)) === 'json') {
$key = str_replace($dir, '', $name);
$key = str_replace('.json', '', $key);
- $json[$key] = $key;
- }
+ $json[$key] = $key;
+ }
}
return $json;
}
-
+
/**
* Checks the JSON tags from a template
* @param string the template's path
* @param string the selected language
* @return string the information about the JSON tags
*/
- private function checkJson($path,$lang){
+ private function checkJson($path, $lang)
+ {
//if there was not a valid template's path
- if(!$path){
+ if (!$path) {
return "JSON file is missing";
- }else{
- //loads a template and find all its tags
- $htmlTemplate = file_get_contents('templates/' . $path . '.html');
- preg_match_all('/{{lang_(.*?)}}/s', $htmlTemplate, $matches);
- $htmlCount = count(array_unique($matches[1]));
-
- //initialize the count variables
- $matchCount = 0;
- $unusedCount = 0;
-
- //loads the JSON tags and count the matches
- $json = Dictionary::getArrayTemplate($path,$lang);
- foreach($json as $key => $value){
- if($key != 'lang'){
- if(!in_array(preg_replace('/^lang_/', '', $key), $matches[1])){
- $unusedCount++;
- }else if($value != ''){
- $matchCount++;
- }
+ }
+ //loads a template and find all its tags
+ $htmlTemplate = file_get_contents('templates/' . $path . '.html');
+ preg_match_all('/{{lang_(.*?)}}/s', $htmlTemplate, $matches);
+ $htmlCount = count(array_unique($matches[1]));
+
+ //initialize the count variables
+ $matchCount = 0;
+ $unusedCount = 0;
+
+ //loads the JSON tags and count the matches
+ $json = Dictionary::getArrayTemplate($path, $lang);
+ foreach ($json as $key => $value) {
+ if ($key != 'lang') {
+ if (!in_array(preg_replace('/^lang_/', '', $key), $matches[1])) {
+ $unusedCount++;
+ } else if ($value != '') {
+ $matchCount++;
}
}
- $diff = $htmlCount - $matchCount;
-
- //build the return string
- $str = "";
- if($diff == 0 && $unusedCount == 0) $str .= "OK";
- if($diff > 0) $str .= $diff . " JSON tag(s) are missing";
- if($diff > 0 && $unusedCount > 0) $str .= "<br>";
- if($unusedCount > 0) $str .= $unusedCount . " JSON tag(s) are not being used";
- return $str;
}
+ $diff = $htmlCount - $matchCount;
+
+ //build the return string
+ $str = "";
+ if ($diff == 0 && $unusedCount == 0)
+ $str .= "OK";
+ if ($diff > 0)
+ $str .= $diff . " JSON tag(s) are missing";
+ if ($diff > 0 && $unusedCount > 0)
+ $str .= "<br>";
+ if ($unusedCount > 0)
+ $str .= $unusedCount . " JSON tag(s) are not being used";
+ return $str;
}
-
+
/**
* Builds the template page with the tags from its template and its JSON file
* @param string the template's path
* @param string the selected language
* @return string the information about the JSON tags
*/
- private function templateAnalysis($path){
+ private function templateAnalysis($path)
+ {
//checks if the template is valid
- if(!file_exists('templates/' . $path . '.html')){
+ if (!file_exists('templates/' . $path . '.html')) {
Message::addError('invalid-template');
return false;
}
-
+
//finds every mustache tag within the html template
$htmlTemplate = file_get_contents('templates/' . $path . '.html');
preg_match_all('/{{lang_(.*?)}}/s', $htmlTemplate, $matches);
$tags = $matches[1];
$tags = array_flip($tags);
- foreach ($tags as $key => $value)
- {
- $tags['lang_'.$key] = $value;
+ foreach ($tags as $key => $value) {
+ $tags['lang_' . $key] = $value;
unset($tags[$key]);
}
-
+
//finds every JSON tag withing the JSON language files
- $langArray = array('de','en','pt');
+ $langArray = Dictionary::getLanguages();
$json = array();
- foreach($langArray as $lang){
- $jsonTags = Dictionary::getArrayTemplate($path,$lang);
- $json = array_merge($json,$jsonTags);
+ foreach ($langArray as $lang) {
+ $jsonTags = Dictionary::getArrayTemplate($path, $lang);
+ if (is_array($jsonTags))
+ $json = array_merge($json, $jsonTags);
}
//remove unused tag
unset($json['lang']);
//merges the arrays to keep the unique tags
- $test = array_merge($json,$tags);
-
+ $test = array_merge($json, $tags);
+
//loads the content of every JSON tag from the specified language
- foreach($test as $tag=>$value){
+ foreach ($test as $tag => $value) {
$this->tags[] = array(
'tag' => $tag,
- 'de' => $this->checkJsonTag($path,$tag,'de/'),
- 'en' => $this->checkJsonTag($path,$tag,'en/'),
- 'pt' => $this->checkJsonTag($path,$tag,'pt/'),
- 'class' => $this->checkJsonTags($path,$tag)
+ 'de' => $this->checkJsonTag($path, $tag, 'de/'),
+ 'en' => $this->checkJsonTag($path, $tag, 'en/'),
+ 'pt' => $this->checkJsonTag($path, $tag, 'pt/'),
+ 'class' => $this->checkJsonTags($path, $tag)
);
}
-
+
return true;
}
-
+
/**
* Loads the content of a JSON tag
* @param string the JSON's path
@@ -247,126 +254,136 @@ class Page_Translation extends Page
* @param string the specified language
* @return string the tag's content
*/
- private function checkJsonTag($path,$tag,$lang){
- if($json = Dictionary::getArrayTemplate($path,$lang)){
+ private function checkJsonTag($path, $tag, $lang)
+ {
+ $json = Dictionary::getArrayTemplate($path, $lang);
+ if (is_array($json) && isset($json[$tag])) {
return $json[$tag];
}
- return '';
+ return '';
}
-
+
/**
* Change the color of the table line according to the tag status
* @param string the JSON's path
* @param string the selected tag
* @return string the css class of the line
*/
- private function checkJsonTags($path,$tag){
+ private function checkJsonTags($path, $tag)
+ {
//return danger in case the tag is not found in the template
$htmlTemplate = file_get_contents('templates/' . $path . '.html');
$htmlCount = substr_count($htmlTemplate, $tag);
- if($htmlCount < 1) return "danger";
-
+ if ($htmlCount < 1)
+ return "danger";
+
//return warning in case at least one of the tag's values is empty
- $langArray = array('de/','en/','pt/');
- foreach($langArray as $lang){
- if($json = Dictionary::getArrayTemplate($path,$lang)){
- if(!isset($json[$tag]) || $json[$tag] == '') return 'warning';
+ $langArray = array('de/', 'en/', 'pt/');
+ foreach ($langArray as $lang) {
+ if (($json = Dictionary::getArrayTemplate($path, $lang))) {
+ if (!isset($json[$tag]) || $json[$tag] == '')
+ return 'warning';
}
}
//if it's ok don't change the class
- return '';
+ return '';
}
-
+
/**
* Updates a JSON file with it's new tags or/and tags values
- * @return boolean if the action was not successful
*/
- private function updateJson(){
- $langArray = unserialize(SITE_LANGUAGES);
- $json = array(
- 'de' => array(),
- 'en' => array(),
- 'pt' => array()
- );
-
+ private function updateJson()
+ {
+ $langArray = Dictionary::getLanguages();
+ foreach ($langArray as $lang) {
+ $json[$lang] = array();
+ }
+
//find the tag requests to change the file
- foreach($_REQUEST as $key => $value){
- $str = explode('#',$key);
+ foreach ($_REQUEST as $key => $value) {
+ $str = explode('#', $key, 3);
+ if (count($str) !== 3)
+ continue;
$pre = $str[0];
$lang = $str[1];
$tag = $str[2];
- if($pre == 'lang'){
- if(in_array($lang,$langArray)){
- if($tag != 'newtag')
- $json[$lang][$tag] = $value;
- else{
- $json[$lang][$_REQUEST['newtag']] = $value;
- }
+ if ($pre !== 'lang')
+ continue;
+ if (in_array($lang, $langArray)) {
+ if ($tag !== 'newtag')
+ $json[$lang][$tag] = $value;
+ else {
+ $json[$lang][$_REQUEST['newtag']] = $value;
}
}
-
}
-
+
+ // JSON_PRETTY_PRINT is only available starting with php 5.4.0.... Use upgradephp's json_encode
+ require_once('inc/up_json_encode.php');
+
//saves the new values on the file
- foreach($json as $key => $array){
- $path = 'lang/' . $key . '/' . $_POST['path'] . '.json';
- $json = json_encode($array,true);
+ foreach ($json as $key => $array) {
+ $path = 'lang/' . $key . '/' . $_POST['path'] . '.json'; // TODO: Wtf? Unvalidated user input -> filesystem access!
+ ksort($array); // Sort by key, so the diff on the output is cleaner
+ $json = up_json_encode($array, JSON_PRETTY_PRINT); // Also for better diffability of the json files, we pretty print
//exits the function in case the action was unsuccessful
- if(!file_put_contents($path,$json)){
+ if (@file_put_contents($path, $json) === false) {
Message::addError('invalid-template');
- return false;
+ return;
}
}
Message::addSuccess('updated-tags');
}
-
+
/**
* Load the main table with all the website's messages or hardcoded messages
* @var boolean choose between hardcoded and non-hardcoded messages
* @return array with the selected messages
*/
- private function initMsg($isHardcoded){
+ private function initMsg($isHardcoded)
+ {
$msgs = array();
//chooses the path
$path = 'messages';
- if($isHardcoded){
+ if ($isHardcoded) {
$path = 'messages-hardcoded';
}
//loads the content of every JSON tag from the message file
- $json = Dictionary::getArrayTemplate($path,$lang);
- foreach($json as $key => $array){
- if($key != 'lang')
- $msgs[] = array(
- 'tag' => $key,
- 'de' => $this->checkJsonTag($path,$key,'de/'),
- 'en' => $this->checkJsonTag($path,$key,'en/'),
- 'pt' => $this->checkJsonTag($path,$key,'pt/')
- );
+ $json = Dictionary::getArrayTemplate($path, LANG);
+ foreach ($json as $key => $array) {
+ if ($key != 'lang')
+ $msgs[] = array(
+ 'tag' => $key,
+ 'de' => $this->checkJsonTag($path, $key, 'de/'), // TODO: Hardcoded language list, use Dictionary::getLanguages()
+ 'en' => $this->checkJsonTag($path, $key, 'en/'),
+ 'pt' => $this->checkJsonTag($path, $key, 'pt/')
+ );
}
return $msgs;
}
-
+
/**
* Delete a specific JSON tag from a JSON files
* @var string the JSON's file path
* @var the JSON tag to be deleted
* @return boolean if the action was not successful
*/
- private function deleteTag($path,$tag){
+ private function deleteTag($path, $tag)
+ {
//delete the tag from every language file
$langArray = unserialize(SITE_LANGUAGES);
- foreach($langArray as $lang){
- $json = Dictionary::getArrayTemplate($path,$lang);
+ foreach ($langArray as $lang) {
+ $json = Dictionary::getArrayTemplate($path, $lang);
unset($json[$tag]);
unset($json['lang']);
$result = file_put_contents('lang/' . $lang . '/' . $path . '.json', json_encode($json));
//add warning and exit in case the action was unsuccessful
- if($result === false){
+ if ($result === false) {
Message::addWarning('unsuccessful-action');
return false;
}
}
Message::addSuccess('deleted-tag');
}
-
+
}
diff --git a/templates/main-menu-login.html b/templates/main-menu-login.html
index e90fe63a..81aef288 100644
--- a/templates/main-menu-login.html
+++ b/templates/main-menu-login.html
@@ -32,9 +32,9 @@
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><img src="lang/{{lang}}/flag.png"><b class="caret"></b></a>
<ul class="dropdown-menu">
<li class="dropdown-header">{{lang_language}}</li>
- <li><a href="index.php?lang=de"><img src="lang/de/flag.png"> Deutsch</a></li>
- <li><a href="index.php?lang=en"><img src="lang/en/flag.png"> English</a></li>
- <li><a href="index.php?lang=pt"><img src="lang/pt/flag.png"> Português</a></li>
+ <li><a href="?lang=de&amp;url={{url}}"><img src="lang/de/flag.png"> Deutsch</a></li>
+ <li><a href="?lang=en&amp;url={{url}}"><img src="lang/en/flag.png"> English</a></li>
+ <li><a href="?lang=pt&amp;url={{url}}"><img src="lang/pt/flag.png"> Português</a></li>
</ul>
</li>
</ul>
diff --git a/templates/main-menu-logout.html b/templates/main-menu-logout.html
index aaa8f0f8..d5671fef 100644
--- a/templates/main-menu-logout.html
+++ b/templates/main-menu-logout.html
@@ -32,9 +32,9 @@
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><img src="lang/{{lang}}/flag.png"><b class="caret"></b></a>
<ul class="dropdown-menu">
<li class="dropdown-header">{{lang_language}}</li>
- <li><a href="index.php?lang=de"><img src="lang/de/flag.png"> Deutsch</a></li>
- <li><a href="index.php?lang=en"><img src="lang/en/flag.png"> English</a></li>
- <li><a href="index.php?lang=pt"><img src="lang/pt/flag.png"> Português</a></li>
+ <li><a href="?lang=de&amp;url={{url}}"><img src="lang/de/flag.png"> Deutsch</a></li>
+ <li><a href="?lang=en&amp;url={{url}}"><img src="lang/en/flag.png"> English</a></li>
+ <li><a href="?lang=pt&amp;url={{url}}"><img src="lang/pt/flag.png"> Português</a></li>
</ul>
</li>
</ul>