diff options
-rw-r--r-- | config.php | 2 | ||||
-rw-r--r-- | inc/dictionary.inc.php | 82 | ||||
-rw-r--r-- | index.php | 18 | ||||
-rw-r--r-- | lang/i18n.php | 52 | ||||
-rw-r--r-- | modules/admin.inc.php | 299 | ||||
-rw-r--r-- | templates/main-menu-login.html | 6 | ||||
-rw-r--r-- | templates/main-menu-logout.html | 6 |
7 files changed, 254 insertions, 211 deletions
@@ -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..58127c18 --- /dev/null +++ b/inc/dictionary.inc.php @@ -0,0 +1,82 @@ +<?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); + return array_merge($language, json_decode($content, true)); + } + + 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(); @@ -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/i18n.php b/lang/i18n.php deleted file mode 100644 index 1f7731f2..00000000 --- a/lang/i18n.php +++ /dev/null @@ -1,52 +0,0 @@ -<?php - -class Dictionary{ - private static $messageArray; - - function build(){ - self::$messageArray = json_decode(file_get_contents("lang/" . LANG . "/messages.json"),true); - } - - public static function getArrayTemplate($template,$lang = false){ - $language = array('lang'=>LANG); - if(!$lang) - return array_merge($language,json_decode(file_get_contents("lang/" . LANG . "/" . $template . ".json"),true)); - return array_merge($language,json_decode(file_get_contents("lang/" . $lang . "/" . $template . ".json"),true)); - } - - 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; - } - -} - //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/admin.inc.php b/modules/admin.inc.php index 2941e21d..97a47819 100644 --- a/modules/admin.inc.php +++ b/modules/admin.inc.php @@ -13,145 +13,152 @@ class Page_Admin extends Page private $tags = false; private $unusedTags = false; private $message = 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() { - if($this->update) $this->updateJson(); - if($this->delete && $this->template) $this->deleteTag($this->template,$this->delete); - - switch($this->page){ - case 'messages': - Render::addTemplate('administration/messages', array( + if ($this->update) + $this->updateJson(); + if ($this->delete && $this->template) + $this->deleteTag($this->template, $this->delete); + + switch ($this->page) { + case 'messages': + Render::addTemplate('administration/messages', array( 'token' => Session::get('token'), 'msgs' => $this->initMsg(false), 'msgsHC' => $this->initMsg(true) )); - break; - case 'templates': - if($this->templateAnalysis($this->template)){ - Render::addTemplate('administration/template', array( + break; + case 'templates': + if ($this->templateAnalysis($this->template)) { + Render::addTemplate('administration/template', array( + 'token' => Session::get('token'), + 'template' => $this->template, + 'path' => $this->path, + 'tags' => $this->tags + )); + break; + } + default: + $this->initTable(); + Render::addTemplate('administration/_page', array( 'token' => Session::get('token'), - 'template' => $this->template, - 'path' => $this->path, - 'tags' => $this->tags + 'adminMessage' => $this->message, + 'table' => $this->table )); - break; - } - default: - $this->initTable(); - Render::addTemplate('administration/_page', array( - 'token' => Session::get('token'), - 'adminMessage' => $this->message, - 'table' => $this->table - )); } - - } - - private function initTable(){ + + private function initTable() + { $this->listTemplates(); $de = $this->listJson('de/'); $en = $this->listJson('en/'); $pt = $this->listJson('pt/'); - - foreach($this->files as $key => $value){ - + + foreach ($this->files as $key => $value) { + $this->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($this->table); } - - private function listTemplates(){ + + private function listTemplates() + { $this->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); - $this->files[$key] = $name; - } + $this->files[$key] = $name; + } } } - - 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; } - - private function checkJson($path,$lang){ - if(!$path){ + + private function checkJson($path, $lang) + { + if (!$path) { return "JSON file is missing"; - }else{ + } else { $htmlTemplate = file_get_contents('templates/' . $path . '.html'); - $json = Dictionary::getArrayTemplate($path,$lang); + $json = Dictionary::getArrayTemplate($path, $lang); preg_match_all('/{{lang_(.*?)}}/s', $htmlTemplate, $matches); $htmlCount = count(array_unique($matches[1])); $matchCount = 0; - - foreach($json as $key => $value){ - if($key != 'lang'){ - if(!in_array(preg_replace('/^lang_/', '', $key), $matches[1])){ + + foreach ($json as $key => $value) { + if ($key != 'lang') { + if (!in_array(preg_replace('/^lang_/', '', $key), $matches[1])) { $matchCount++; - }else if($value != ''){ + } else if ($value != '') { $matchCount++; } } } - + $diff = $htmlCount - $matchCount; - if($diff == 0) return "OK"; - if($diff > 0) return $diff . " JSON tag(s) are missing"; - if($diff < 0) return ($diff * -1) . " JSON tag(s) are not being used"; + if ($diff == 0) + return "OK"; + if ($diff > 0) + return $diff . " JSON tag(s) are missing"; + if ($diff < 0) + return ($diff * -1) . " JSON tag(s) are not being used"; } } - - private function templateAnalysis($path){ - if(!file_exists('templates/' . $path . '.html')){ + + private function templateAnalysis($path) + { + if (!file_exists('templates/' . $path . '.html')) { Message::addError('invalid-template'); return false; } @@ -159,124 +166,130 @@ class Page_Admin extends Page 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]); } - + $langArray = array('en'); $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); + $json = array_merge($json, $jsonTags); } - + unset($json['lang']); - $test = array_merge($json,$tags); - - foreach($test as $tag=>$value){ + $test = array_merge($json, $tags); + + 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) ); } - + $this->path = $path; - + return true; } - - private function checkJsonTag($path,$tag,$lang){ - if($json = Dictionary::getArrayTemplate($path,$lang)){ + + private function checkJsonTag($path, $tag, $lang) + { + if ($json = Dictionary::getArrayTemplate($path, $lang)) { return $json[$tag]; } - return ''; + return ''; } - - private function checkJsonTags($path,$tag){ + + private function checkJsonTags($path, $tag) + { $htmlTemplate = file_get_contents('templates/' . $path . '.html'); $htmlCount = substr_count($htmlTemplate, $tag); - if($htmlCount < 1) return "danger"; - - $langArray = array('de/','en/','pt/'); - foreach($langArray as $lang){ - if($json = Dictionary::getArrayTemplate($path,$lang)){ - if(!isset($json[$tag]) || $json[$tag] == '') return 'warning'; + if ($htmlCount < 1) + return "danger"; + + $langArray = array('de/', 'en/', 'pt/'); + foreach ($langArray as $lang) { + if ($json = Dictionary::getArrayTemplate($path, $lang)) { + if (!isset($json[$tag]) || $json[$tag] == '') + return 'warning'; } } - return ''; + return ''; } - - private function updateJson(){ - $langArray = unserialize(SITE_LANGUAGES); - $json = array( - 'de' => array(), - 'en' => array(), - 'pt' => array() - ); - - foreach($_REQUEST as $key => $value){ - $str = explode('#',$key); + + private function updateJson() + { + $langArray = Dictionary::getLanguages(); + $json = array(); + foreach ($langArray as $lang) { + $json[$lang] = array(); + } + + 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') + if ($pre === 'lang') { + if (in_array($lang, $langArray)) { + if ($tag !== 'newtag') $json[$lang][$tag] = $value; - else{ + else { $json[$lang][$_REQUEST['newtag']] = $value; } } } - } - - foreach($json as $key => $array){ - $path = 'lang/' . $key . '/' . $_POST['path'] . '.json'; - $json = json_encode($array,true); - if(!file_put_contents($path,$json)){ + + foreach ($json as $key => $array) { + $path = 'lang/' . $key . '/' . $_POST['path'] . '.json'; // TODO: Wtf? Unvalidated user input -> filesystem access! + $json = json_encode($array, JSON_PRETTY_PRINT); + if (@file_put_contents($path, $json) === false) { Message::addError('invalid-template'); return false; } } Message::addSuccess('updated-tags'); } - - private function initMsg($isHardcoded){ + + private function initMsg($isHardcoded) + { $msgs = array(); $path = 'messages'; - if($isHardcoded){ + if ($isHardcoded) { $path = 'messages-hardcoded'; } - $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; } - - private function deleteTag($path,$tag){ + + private function deleteTag($path, $tag) + { $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']); //file_put_contents('test.txt','lang/' . $lang . '/' . $path . '.json'); - file_put_contents('lang/' . $lang . '/' . $path . '.json', json_encode($json)); + file_put_contents('lang/' . $lang . '/' . $path . '.json', json_encode($json)); // TODO: Check return code, show error if not writable } Message::addSuccess('deleted-tag'); } - + } diff --git a/templates/main-menu-login.html b/templates/main-menu-login.html index e2edf7da..71e10b0e 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&url={{url}}"><img src="lang/de/flag.png"> Deutsch</a></li> + <li><a href="?lang=en&url={{url}}"><img src="lang/en/flag.png"> English</a></li> + <li><a href="?lang=pt&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 2b90de5c..1721161e 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&url={{url}}"><img src="lang/de/flag.png"> Deutsch</a></li> + <li><a href="?lang=en&url={{url}}"><img src="lang/en/flag.png"> English</a></li> + <li><a href="?lang=pt&url={{url}}"><img src="lang/pt/flag.png"> Português</a></li> </ul> </li> </ul> |