Re: Прехвърляне на масиви от файлове към база данни
Posted: Fri Aug 21, 2020 6:29 pm
Html, php, mysql, perl, javascript, SEO, CMS и CSS форуми.
https://web-tourist.net/forum/
Code: Select all
<?php
class Lang {
public $current;
public $default = 'us';
public $map = [
'us' => 'us', //United States - English
'gb' => 'gb', //United Kingdom - English
'de' => 'de', //Deutsch
'es' => 'es', //Español
'it' => 'it', //Italiano
'fr' => 'fr', //Français
];
public $data = [];
private $dir;
public function __construct() {
$this->dir = ENGINE_DIR . '/data/lang/';
$this->current = $this->default;
if ( isset($_REQUEST['lang']) && in_array($_REQUEST['lang'], $this->map) ) {
$this->set($_REQUEST['lang']);
}
// Determine the current language
if ( isset($_COOKIE['lng']) && is_string($_COOKIE['lng']) && array_key_exists($_COOKIE['lng'], $this->map) ) {
if ( file_exists($this->dir . $this->map[$_COOKIE['lng']] . '.php') ) {
$this->current = $this->map[$_COOKIE['lng']];
}
} elseif ( isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ) {
$lang = substr(strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']), 0, 2);
if ( $lang && array_key_exists($lang, $this->map) ) {
$this->current = $this->map[$lang];
}
}
}
public function get() {
global $db;
if ( file_exists($this->dir . $this->current . '.php') ) {
$SArray = $db->query( "SELECT `pld_id`, `pld_key`, `pld_data` FROM `promo_lang_data` WHERE `lang_id` = '" . (string)$this->current . "';" );
if(mysqli_num_rows($SArray) > 0) {
while ($obj = $SArray->fetch_object()) {
$rows[$obj->pld_key] = $obj->pld_data;
}
$this->data = $rows;
$this->merge();
return $this->data;
} else {
die('Language file not found, contact by administrator.');
}
} else {
die('Language file not found, contact by administrator.');
}
}
public function set($lang) {
$expire = 365 * 86400 + time(); // 1 год
$domain = str_replace(['http://', 'https://'], '', mb_strtolower($_SERVER['HTTP_HOST'], 'utf-8'));
if ( substr( $domain, 0, 4 ) == 'www.' ) $domain = substr( $domain, 4 );
setcookie( 'lng', $lang, $expire, '/', $domain, NULL, TRUE );
$_COOKIE['lng'] = $lang;
}
private function merge() {
global $db;
if ( $this->current != $this->default ) {
if ( file_exists($this->dir . $this->default . '.php') ) {
$SArray = $db->query( "SELECT `pld_id`, `pld_key`, `pld_data` FROM `promo_lang_data` WHERE `lang_id` = '" . (string)$this->default . "';" );
if(mysqli_num_rows($SArray) > 0) {
while ($obj = $SArray->fetch_object()) {
$rows[$obj->pld_key] = $obj->pld_data;
}
$this->data = array_merge($rows, $this->data);
} else {
die('Language file not found, contact by administrator.');
}
} else {
die('Language file not found, contact by administrator.');
}
}
}
}