Refactor lang to class

This commit is contained in:
Bartek Fabiszewski 2019-02-25 10:04:09 +01:00
parent 6f9ae994a3
commit 694c4d2b08
12 changed files with 192 additions and 81 deletions

View File

@ -3,7 +3,7 @@
require_once(__DIR__ . "/../lib/UloggerAPITestCase.php");
if (!defined("ROOT_DIR")) { define("ROOT_DIR", __DIR__ . "/../.."); }
require_once(ROOT_DIR . "/helpers/config.php");
require_once(ROOT_DIR . "/lang.php");
require_once(ROOT_DIR . "/helpers/lang.php");
class ImportTest extends UloggerAPITestCase {
@ -609,7 +609,7 @@ class ImportTest extends UloggerAPITestCase {
}
public function testImportNoLongitude() {
global $lang;
$lang = (new uLang("en"))->getStrings();
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(0, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
@ -656,7 +656,7 @@ class ImportTest extends UloggerAPITestCase {
}
public function testImportNoLatitude() {
global $lang;
$lang = (new uLang("en"))->getStrings();
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(0, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
@ -703,7 +703,7 @@ class ImportTest extends UloggerAPITestCase {
}
public function testImportNoGPX() {
global $lang;
$lang = (new uLang("en"))->getStrings();
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(0, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
@ -744,7 +744,7 @@ class ImportTest extends UloggerAPITestCase {
}
public function testImportCorrupt() {
global $lang;
$lang = (new uLang("en"))->getStrings();
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(0, $this->getConnection()->getRowCount("tracks"), "Wrong row count");

View File

@ -3,7 +3,7 @@
require_once(__DIR__ . "/../lib/UloggerAPITestCase.php");
if (!defined("ROOT_DIR")) { define("ROOT_DIR", __DIR__ . "/../.."); }
require_once(ROOT_DIR . "/helpers/config.php");
require_once(ROOT_DIR . "/lang.php");
require_once(ROOT_DIR . "/helpers/lang.php");
class InternalAPITest extends UloggerAPITestCase {
@ -527,7 +527,6 @@ class InternalAPITest extends UloggerAPITestCase {
/* handletrack.php */
public function testHandleTrackDeleteAdmin() {
global $lang;
$this->assertTrue($this->authenticate(), "Authentication failed");
$userId = $this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
@ -551,7 +550,6 @@ class InternalAPITest extends UloggerAPITestCase {
}
public function testHandleTrackDeleteSelf() {
global $lang;
$userId = $this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
$this->assertTrue($this->authenticate($this->testUser, $this->testPass), "Authentication failed");
@ -575,7 +573,7 @@ class InternalAPITest extends UloggerAPITestCase {
}
public function testHandleTrackDeleteOtherUser() {
global $lang;
$lang = (new uLang("en"))->getStrings();
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
$this->assertTrue($this->authenticate($this->testUser, $this->testPass), "Authentication failed");
@ -597,7 +595,6 @@ class InternalAPITest extends UloggerAPITestCase {
}
public function testHandleTrackUpdate() {
global $lang;
$newName = "New name";
$this->assertTrue($this->authenticate(), "Authentication failed");
$userId = $this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
@ -639,7 +636,7 @@ class InternalAPITest extends UloggerAPITestCase {
}
public function testHandleTrackUpdateEmptyName() {
global $lang;
$lang = (new uLang("en"))->getStrings();
$this->assertTrue($this->authenticate(), "Authentication failed");
$userId = $this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
@ -663,7 +660,7 @@ class InternalAPITest extends UloggerAPITestCase {
}
public function testHandleTrackUpdateNonexistantTrack() {
global $lang;
$lang = (new uLang("en"))->getStrings();
$newName = "New name";
$this->assertTrue($this->authenticate(), "Authentication failed");
$userId = $this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
@ -687,8 +684,7 @@ class InternalAPITest extends UloggerAPITestCase {
}
public function testHandleTrackMissingAction() {
global $lang;
$lang = (new uLang("en"))->getStrings();
$this->assertTrue($this->authenticate(), "Authentication failed");
$options = [
@ -706,8 +702,7 @@ class InternalAPITest extends UloggerAPITestCase {
/* handleuser.php */
public function testHandleUserMissingAction() {
global $lang;
$lang = (new uLang("en"))->getStrings();
$this->assertTrue($this->authenticate(), "Authentication failed");
$options = [
@ -722,8 +717,7 @@ class InternalAPITest extends UloggerAPITestCase {
}
public function testHandleUserNonAdmin() {
global $lang;
$lang = (new uLang("en"))->getStrings();
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
$this->assertTrue($this->authenticate($this->testUser, $this->testPass), "Authentication failed");
@ -743,8 +737,7 @@ class InternalAPITest extends UloggerAPITestCase {
}
public function testHandleUserSelf() {
global $lang;
$lang = (new uLang("en"))->getStrings();
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(1, $this->getConnection()->getRowCount("users"), "Wrong row count");
@ -762,8 +755,7 @@ class InternalAPITest extends UloggerAPITestCase {
}
public function testHandleUserEmptyLogin() {
global $lang;
$lang = (new uLang("en"))->getStrings();
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(1, $this->getConnection()->getRowCount("users"), "Wrong row count");
@ -781,7 +773,7 @@ class InternalAPITest extends UloggerAPITestCase {
}
public function testHandleUserNoAuth() {
global $lang;
$lang = (new uLang("en"))->getStrings();
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
@ -825,8 +817,7 @@ class InternalAPITest extends UloggerAPITestCase {
}
public function testHandleUserAddSameLogin() {
global $lang;
$lang = (new uLang("en"))->getStrings();
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
@ -864,7 +855,7 @@ class InternalAPITest extends UloggerAPITestCase {
}
public function testHandleUserUpdateEmptyPass() {
global $lang;
$lang = (new uLang("en"))->getStrings();
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");

32
.tests/tests/LangTest.php Normal file
View File

@ -0,0 +1,32 @@
<?php
use PHPUnit\Framework\TestCase;
require_once(__DIR__ . "/../../helpers/config.php");
require_once(__DIR__ . "/../../helpers/lang.php");
class LangTest extends TestCase {
public function testGetLanguages() {
$languages = uLang::getLanguages();
$this->assertNotEmpty($languages);
$this->assertArrayHasKey("en", $languages);
$this->assertArrayHasKey("pl", $languages);
$this->assertEquals("English", $languages["en"]);
$this->assertEquals("Polski", $languages["pl"]);
}
public function testGetStrings() {
$lang = new uLang("en");
$this->assertEquals("User", $lang->getStrings()["user"]);
$lang = new uLang("pl");
$this->assertEquals("Użytkownik", $lang->getStrings()["user"]);
}
public function testGetSetupStrings() {
$lang = new uLang("en");
$this->assertEquals("Congratulations!", $lang->getSetupStrings()["congratulations"]);
$lang = new uLang("pl");
$this->assertEquals("Gratulacje!", $lang->getSetupStrings()["congratulations"]);
}
}
?>

115
helpers/lang.php Normal file
View File

@ -0,0 +1,115 @@
<?php
/* μlogger
*
* Copyright(C) 2019 Bartek Fabiszewski (www.fabiszewski.net)
*
* This is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
require_once(ROOT_DIR . "/helpers/config.php");
/**
* Localization
*/
class uLang {
/**
* Available languages
*
* @var array
*/
private static $languages = [
"en" => "English",
"pl" => "Polski",
"de" => "Deutsch",
"hu" => "Magyar",
"fr" => "Français",
"it" => "Italiano",
"es" => "Español",
"nl" => "Nederlands",
"zh" => "中文"
];
/**
* Application strings
* Array of key => translation pairs
*
* @var array
*/
private $strings = [];
/**
* Setup script strings
* Array of key => translation pairs
*
* @var array
*/
private $setupStrings = [];
/**
* Constructor
*
* @param string $language Language code (IANA)
*/
public function __construct($language = "en") {
$lang = [];
$langSetup = [];
// always load en base
require(ROOT_DIR . "/lang/en.php");
// override with translated strings if needed
// missing strings will be displayed in English
if ($language != "en" && array_key_exists($language, self::$languages)) {
require(ROOT_DIR . "/lang/$language.php");
}
// choose password messages based on config
$lang['passrules'] = isset($lang["passrules_" . uConfig::$pass_strength]) ? $lang["passrules_" . uConfig::$pass_strength] : "";
$lang['passlenmin'] = sprintf($lang["passlenmin"], uConfig::$pass_lenmin);
$this->strings = $lang;
$this->setupStrings = $langSetup;
}
/**
* Get supported languages array
* Language code => Native language name
*
* @return array
*/
public static function getLanguages() {
return self::$languages;
}
/**
* Get translated strings array
* Key => translation string
*
* @return array
*/
public function getStrings() {
return $this->strings;
}
/**
* Get translated strings array for setup script
* Key => translation string
*
* @return array
*/
public function getSetupStrings() {
return $this->setupStrings;
}
}
?>

View File

@ -22,12 +22,16 @@
require_once(ROOT_DIR . "/helpers/position.php");
require_once(ROOT_DIR . "/helpers/track.php");
require_once(ROOT_DIR . "/helpers/utils.php");
require_once(ROOT_DIR . "/lang.php");
require_once(ROOT_DIR . "/helpers/lang.php");
$login = uUtils::postString('user');
$pass = uUtils::postPass('pass');
$action = uUtils::postString('action');
$lang = (new uLang(uConfig::$lang))->getStrings();
$langsArr = uLang::getLanguages();
asort($langsArr);
$auth = new uAuth();
if ($action == "auth") {
$auth->checkLogin($login, $pass);
@ -169,7 +173,6 @@
<div class="menutitle"><?= $lang["language"] ?></div>
<form>
<select name="units" onchange="setLang(this.options[this.selectedIndex].value);">
<?php asort($langsArr); ?>
<?php foreach ($langsArr as $langCode => $langName): ?>
<option value="<?= $langCode ?>"<?= (uConfig::$lang == $langCode) ? " selected" : "" ?>><?= $langName ?></option>
<?php endforeach; ?>

View File

@ -1,46 +0,0 @@
<?php
/* μlogger
*
* Copyright(C) 2017 Bartek Fabiszewski (www.fabiszewski.net)
*
* This is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
// available languages
$langsArr = [
"en" => "English",
"pl" => "Polski",
"de" => "Deutsch",
"hu" => "Magyar",
"fr" => "Français",
"it" => "Italiano",
"es" => "Español",
"nl" => "Nederlands",
"zh" => "中文"
];
// always load en base
require_once(ROOT_DIR . "/lang/en.php");
// override with translated strings if needed
// missing strings will be displayed in English
if (uConfig::$lang != "en" && array_key_exists(uConfig::$lang, $langsArr)) {
require_once(ROOT_DIR . "/lang/" . uConfig::$lang . ".php");
}
// choose password messages based on config
$lang['passrules'] = isset($lang["passrules_" . uConfig::$pass_strength]) ? $lang["passrules_" . uConfig::$pass_strength] : "";
$lang['passlenmin'] = sprintf($lang["passlenmin"], uConfig::$pass_lenmin);
?>

View File

@ -18,11 +18,13 @@
*/
require_once("helpers/auth.php");
require_once(ROOT_DIR . "/lang.php");
require_once(ROOT_DIR . "/helpers/lang.php");
require_once(ROOT_DIR . "/helpers/config.php");
$auth_error = uUtils::getBool('auth_error', false);
$lang = (new uLang(uConfig::$lang))->getStrings();
?>
<!DOCTYPE html>
<html>

View File

@ -32,10 +32,13 @@ define("ROOT_DIR", dirname(__DIR__));
require_once(ROOT_DIR . "/helpers/user.php");
require_once(ROOT_DIR . "/helpers/config.php");
require_once(ROOT_DIR . "/helpers/utils.php");
require_once(ROOT_DIR . "/lang.php");
require_once(ROOT_DIR . "/helpers/lang.php");
$command = uUtils::postString('command');
$lang = (new uLang(uConfig::$lang))->getStrings();
$langSetup = (new uLang(uConfig::$lang))->getSetupStrings();
$prefix = preg_replace('/[^a-z0-9_]/i', '', uConfig::$dbprefix);
$tPositions = $prefix . "positions";
$tTracks = $prefix . "tracks";

View File

@ -19,10 +19,11 @@
require_once(dirname(__DIR__) . "/helpers/auth.php");
require_once(ROOT_DIR . "/helpers/position.php");
require_once(ROOT_DIR . "/lang.php");
require_once(ROOT_DIR . "/helpers/lang.php");
require_once(ROOT_DIR . "/helpers/config.php");
$auth = new uAuth();
$lang = (new uLang(uConfig::$lang))->getStrings();
/**
* Add kml marker style element

View File

@ -18,9 +18,10 @@
*/
require_once(dirname(__DIR__) . "/helpers/auth.php");
require_once(ROOT_DIR . "/lang.php");
require_once(ROOT_DIR . "/helpers/lang.php");
require_once(ROOT_DIR . "/helpers/track.php");
require_once(ROOT_DIR . "/helpers/utils.php");
require_once(ROOT_DIR . "/helpers/config.php");
$auth = new uAuth();
@ -28,6 +29,8 @@
$trackId = uUtils::postInt('trackid');
$trackName = uUtils::postString('trackname');
$lang = (new uLang(uConfig::$lang))->getStrings();
if (empty($action) || empty($trackId)) {
uUtils::exitWithError($lang["servererror"]);
}

View File

@ -18,7 +18,8 @@
*/
require_once(dirname(__DIR__) . "/helpers/auth.php");
require_once(ROOT_DIR . "/lang.php");
require_once(ROOT_DIR . "/helpers/lang.php");
require_once(ROOT_DIR . "/helpers/config.php");
require_once(ROOT_DIR . "/helpers/utils.php");
$auth = new uAuth();
@ -26,6 +27,9 @@
$action = uUtils::postString('action');
$login = uUtils::postString('login');
$pass = uUtils::postPass('pass');
$lang = (new uLang(uConfig::$lang))->getStrings();
if (!$auth->isAuthenticated() || !$auth->isAdmin() || $auth->user->login == $login || empty($action) || empty($login)) {
uUtils::exitWithError($lang["servererror"]);
}

View File

@ -21,10 +21,13 @@ require_once(dirname(__DIR__) . "/helpers/auth.php");
require_once(ROOT_DIR . "/helpers/track.php");
require_once(ROOT_DIR . "/helpers/position.php");
require_once(ROOT_DIR . "/helpers/utils.php");
require_once(ROOT_DIR . "/lang.php");
require_once(ROOT_DIR . "/helpers/config.php");
require_once(ROOT_DIR . "/helpers/lang.php");
$auth = new uAuth();
$lang = (new uLang(uConfig::$lang))->getStrings();
$uploadErrors = [];
$uploadErrors[UPLOAD_ERR_INI_SIZE] = "The uploaded file exceeds the upload_max_filesize directive in php.ini";
$uploadErrors[UPLOAD_ERR_FORM_SIZE] = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form";