From ffef321be97b08a52addf6a60bbf6a632be8f6a8 Mon Sep 17 00:00:00 2001 From: Bartek Fabiszewski Date: Tue, 18 Apr 2017 09:25:21 +0200 Subject: [PATCH] Use static initializer in uConfig class --- auth.php | 6 ++---- helpers/config.php | 32 ++++++++++++++++++++++++-------- helpers/db.php | 5 ++--- helpers/user.php | 6 ++---- index.php | 42 +++++++++++++++++++++--------------------- lang.php | 8 ++++---- scripts/setup.php | 14 ++++++-------- utils/download.php | 6 +++--- utils/getpositions.php | 4 ++-- utils/gettracks.php | 4 ++-- 10 files changed, 68 insertions(+), 59 deletions(-) diff --git a/auth.php b/auth.php index ef7a39a..2359717 100755 --- a/auth.php +++ b/auth.php @@ -19,8 +19,6 @@ define("ROOT_DIR", __DIR__); require_once(ROOT_DIR . "/helpers/config.php"); -$config = new uConfig(); - require_once(ROOT_DIR . "/lang.php"); require_once(ROOT_DIR . "/helpers/user.php"); @@ -31,12 +29,12 @@ $sid = session_id(); // check for forced login to authorize admin in case of public access $force_login = (isset($_REQUEST['force_login']) ? $_REQUEST['force_login'] : false); if ($force_login) { - $config::$require_authentication = true; + uConfig::$require_authentication = true; } $user = new uUser(); $user->getFromSession(); -if (!$user->isValid && ($config::$require_authentication || defined('headless'))) { +if (!$user->isValid && (uConfig::$require_authentication || defined('headless'))) { /* authentication */ $login = (isset($_REQUEST['user']) ? $_REQUEST['user'] : NULL); $pass = (isset($_REQUEST['pass']) ? $_REQUEST['pass'] : NULL); diff --git a/helpers/config.php b/helpers/config.php index fec4073..fe81286 100644 --- a/helpers/config.php +++ b/helpers/config.php @@ -17,6 +17,12 @@ * along with this program; if not, see . */ + + /** + * Initialize on file include + */ + uConfig::init(); + /** * Handles config values */ @@ -82,18 +88,23 @@ private static $fileLoaded = false; + private static $initialized = false; + /** - * Constructor + * Static initializer */ - public function __construct() { - $this->setFromFile(); - $this->setFromCookies(); + static public function init() { + if (!self::$initialized) { + self::setFromFile(); + self::setFromCookies(); + self::$initialized = true; + } } /** * Read config values from "/config.php" file */ - private function setFromFile() { + private static function setFromFile() { $configFile = ROOT_DIR . "/config.php"; if (self::$fileLoaded || !file_exists($configFile)) { return; @@ -132,14 +143,19 @@ /** * Read config values stored in cookies */ - private function setFromCookies() { + private static function setFromCookies() { if (isset($_COOKIE["ulogger_api"])) { self::$mapapi = $_COOKIE["ulogger_api"]; } if (isset($_COOKIE["ulogger_lang"])) { self::$lang = $_COOKIE["ulogger_lang"]; } if (isset($_COOKIE["ulogger_units"])) { self::$units = $_COOKIE["ulogger_units"]; } if (isset($_COOKIE["ulogger_interval"])) { self::$interval = $_COOKIE["ulogger_interval"]; } } - public function isFileLoaded() { + /** + * Is config loaded from file? + * + * @return True if loaded, false otherwise + */ + public static function isFileLoaded() { return self::$fileLoaded; } @@ -147,7 +163,7 @@ * Regex to test if password matches strength and length requirements. * Valid for both php and javascript */ - public function passRegex() { + public static function passRegex() { static $regex = ""; if (self::$pass_strength > 0) { // lower and upper case diff --git a/helpers/db.php b/helpers/db.php index b0625fe..cae9717 100644 --- a/helpers/db.php +++ b/helpers/db.php @@ -62,10 +62,9 @@ */ public static function getInstance() { if (!self::$instance) { - $config = new uConfig(); - self::$instance = new self($config::$dbhost, $config::$dbuser, $config::$dbpass, $config::$dbname); + self::$instance = new self(uConfig::$dbhost, uConfig::$dbuser, uConfig::$dbpass, uConfig::$dbname); self::$tables = []; - $prefix = preg_replace('/[^a-z0-9_]/i', '', $config::$dbprefix); + $prefix = preg_replace('/[^a-z0-9_]/i', '', uConfig::$dbprefix); self::$tables['positions'] = $prefix . "positions"; self::$tables['tracks'] = $prefix . "tracks"; self::$tables['users'] = $prefix . "users"; diff --git a/helpers/user.php b/helpers/user.php index 97e2395..e144890 100644 --- a/helpers/user.php +++ b/helpers/user.php @@ -156,8 +156,7 @@ * @return bool True if matches, false otherwise */ private function validPassStrength($password) { - $config = new uConfig(); - return preg_match($config->passRegex(), $password); + return preg_match(uConfig::passRegex(), $password); } /** @@ -225,8 +224,7 @@ * @return bool True if admin, false otherwise */ private function isAdmin($login) { - $config = new uConfig(); - return (!empty($config::$admin_user) && $config::$admin_user == $login); + return (!empty(uConfig::$admin_user) && uConfig::$admin_user == $login); } } ?> diff --git a/index.php b/index.php index d4e9115..cb5aa18 100755 --- a/index.php +++ b/index.php @@ -23,7 +23,7 @@ $displayUserId = NULL; $usersArr = []; - if ($user->isAdmin || $config::$public_tracks) { + if ($user->isAdmin || uConfig::$public_tracks) { // public access or admin user // get last position user $lastPosition = new uPosition(); @@ -67,27 +67,27 @@ - - + + @@ -145,7 +145,7 @@
- ( s)
+ ( s)

@@ -160,8 +160,8 @@
@@ -172,7 +172,7 @@ @@ -182,8 +182,8 @@
@@ -207,7 +207,7 @@ - +
diff --git a/lang.php b/lang.php index a2fe7be..76d25ed 100755 --- a/lang.php +++ b/lang.php @@ -33,12 +33,12 @@ // override with translated strings if needed // missing strings will be displayed in English - if ($config::$lang != "en" && array_key_exists($config::$lang, $langsArr)) { - require_once(ROOT_DIR . "/lang/{$config::$lang}.php"); + 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"][$config::$pass_strength]) ? $lang["passrules"][$config::$pass_strength] : ""; - $lang['passlenmin'] = sprintf($lang["passlenmin"], $config::$pass_lenmin); + $lang['passrules'] = isset($lang["passrules"][uConfig::$pass_strength]) ? $lang["passrules"][uConfig::$pass_strength] : ""; + $lang['passlenmin'] = sprintf($lang["passlenmin"], uConfig::$pass_lenmin); ?> \ No newline at end of file diff --git a/scripts/setup.php b/scripts/setup.php index 4805353..41f2238 100644 --- a/scripts/setup.php +++ b/scripts/setup.php @@ -27,13 +27,11 @@ $enabled = false; define("ROOT_DIR", dirname(__DIR__)); require_once(ROOT_DIR . "/helpers/user.php"); require_once(ROOT_DIR . "/helpers/config.php"); -$config = new uConfig(); - require_once(ROOT_DIR . "/lang.php"); $command = isset($_REQUEST['command']) ? $_REQUEST['command'] : NULL; -$prefix = preg_replace('/[^a-z0-9_]/i', '', $config::$dbprefix); +$prefix = preg_replace('/[^a-z0-9_]/i', '', uConfig::$dbprefix); $tPositions = $prefix . "positions"; $tTracks = $prefix . "tracks"; $tUsers = $prefix . "users"; @@ -87,7 +85,7 @@ switch ($command) { $error = false; mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); try { - $mysqli = new mysqli($config::$dbhost, $config::$dbuser, $config::$dbpass, $config::$dbname); + $mysqli = new mysqli(uConfig::$dbhost, uConfig::$dbuser, uConfig::$dbpass, uConfig::$dbname); } catch (mysqli_sql_exception $e ) { $messages[] = "{$langSetup["dbconnectfailed"]}"; $messages[] = sprintf($langSetup["serversaid"], "" . $e->getMessage() . ""); @@ -153,19 +151,19 @@ switch ($command) { $messages[] = "
"; break; } - if (!$config->isFileLoaded()) { + if (!uConfig::isFileLoaded()) { $messages[] = $langSetup["createconfig"]; $messages[] = $langSetup["dorestart"]; $messages[] = "
"; break; } - if (empty($config::$dbname) || empty($config::$dbhost) || empty($config::$dbuser)) { + if (empty(uConfig::$dbname) || empty(uConfig::$dbhost) || empty(uConfig::$dbuser)) { $messages[] = sprintf($langSetup["nodbsettings"], "\$dbname, \$dbhost, \$dbuser, \$dbpass"); $messages[] = $langSetup["dorestart"]; $messages[] = "
"; break; } - $messages[] = sprintf($langSetup["scriptdesc"], "'$tPositions', '$tTracks', '$tUsers'", "{$config::$dbname}"); + $messages[] = sprintf($langSetup["scriptdesc"], "'$tPositions', '$tTracks', '$tUsers'", "" . uConfig::$dbname . ""); $messages[] = $langSetup["scriptdesc2"]; $messages[] = "
"; break; @@ -211,7 +209,7 @@ switch ($command) {