Skip to content

Commit 8d347b0

Browse files
Add files via upload
1 parent 4573a42 commit 8d347b0

File tree

1 file changed

+11
-123
lines changed

1 file changed

+11
-123
lines changed

src/Db.php

Lines changed: 11 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ class Db {
1919
/**
2020
* global param db
2121
*/
22-
private $key = null; // Передаем ключ шифрования файлов
23-
private $crypt = null; // true|false Шифруем или нет
24-
private $temp = null; // Очередь на запись. true|false
25-
private $api = null; // true|false Если установить false база будет работать как основное хранилище
26-
private $cached = null; // Кеширование. true|false
27-
private $cache_lifetime = 30; // Min
22+
private $key = '0'; // Передаем ключ шифрования файлов
23+
private $crypt = '0'; // true|false Шифруем или нет
24+
private $temp = '0'; // Очередь на запись. true|false
25+
private $api = '0'; // true|false Если установить false база будет работать как основное хранилище
26+
private $cached = '0'; // Кеширование. true|false
27+
private $cache_lifetime = '30'; // Min
2828
private $export = 'false';
29-
private $size = 50000;
30-
private $max_size = 1000000;
29+
private $size = '50000';
30+
private $max_size = '1000000';
3131
private $dir_core = 'db.core';
3232
private $dir_log = 'db.log';
3333
private $dir_cached = 'db.cached';
34-
private $structure = null;
34+
private $structure = '0';
3535

3636
public function __construct($db_path)
3737
{
@@ -61,86 +61,7 @@ public function run()
6161

6262
// Проверяем наличие каталога базы данных, если нет создаем
6363
if (!file_exists($this->db_path)){mkdir($this->db_path);}
64-
65-
if (file_exists($this->db_path.'db.data.json')) {
66-
// Резервная копия основной таблицы
67-
$db_data = json_decode(file_get_contents($this->db_path.'db.data.json'), true);
68-
if (isset($db_data["0"]["id"])) {
69-
if ($db_data["0"]["id"] == 1) {
70-
file_put_contents(JSON_DB_DIR_CACHED.'/db.json', json_encode($db_data));
71-
}
72-
} else {
73-
if (file_exists(JSON_DB_DIR_CACHED.'/db.json')) {
74-
// Если таблица повреждена востанавливаем из резервной копии
75-
$db_data = json_decode(file_get_contents(JSON_DB_DIR_CACHED.'/db.json'), true);
76-
if (isset($db_data["0"]["id"])) {
77-
if ($db_data["0"]["id"] == 1) {
78-
file_put_contents($this->db_path.'db.data.json', json_encode($db_data));
79-
}
80-
}
81-
} else {
82-
// Если резервного файла нет
83-
// Удаляем оба файла, для стандартного востановления
84-
unlink($this->db_path.'db.config.json');
85-
unlink($this->db_path.'db.data.json');
86-
}
87-
}
88-
}
89-
90-
// Проверяем наличие главной таблицы если нет создаем
91-
try {
92-
\jsonDB\Validate::table('db')->exists();
93-
94-
// Обновляем таблицу конфигурации db из параметров
95-
$update = jsonDb::table('db')->find(1); // Edit with ID 1
96-
$update->db_path = $this->db_path;
97-
$update->cached = $this->cached;
98-
$update->temp = $this->temp;
99-
$update->api = $this->api;
100-
$update->cache_lifetime = $this->cache_lifetime;
101-
$update->export = $this->export;
102-
$update->size = $this->size;
103-
$update->max_size = $this->max_size;
104-
$update->dir_core = $this->dir_core;
105-
$update->dir_log = $this->dir_log;
106-
$update->dir_cached = $this->dir_cached;
107-
$update->save();
108-
109-
} catch(dbException $e) {
110-
111-
try {
112-
// Создаем главную таблицу конфигурации db
113-
jsonDb::create('db', array(
114-
'db_id' => 'integer',
115-
'type' => 'string',
116-
'table' => 'string',
117-
'version' => 'string',
118-
'time' => 'string',
119-
'user_key' => 'string',
120-
'password' => 'string',
121-
'public_key' => 'string',
122-
'template' => 'string',
123-
'temp' => 'integer',
124-
'api' => 'integer',
125-
'cached' => 'integer',
126-
'cache_lifetime' => 'integer',
127-
'export' => 'string',
128-
'size' => 'integer',
129-
'max_size' => 'integer',
130-
'db_path' => 'string',
131-
'dir_core' => 'string',
132-
'dir_log' => 'string',
133-
'dir_cached' => 'string'
134-
));
135-
136-
} catch(dbException $e) {
137-
// Возникла ошибка при создании таблицы
138-
// Это означает что балица обнулена
139-
140-
}
141-
142-
}
143-
64+
14465
// Проверяем наличие таблицы cached
14566
try {
14667
Validate::table('cached')->exists();} catch(dbException $e){
@@ -169,39 +90,6 @@ public function run()
16990
));
17091

17192
}
172-
173-
try {
174-
jsonDb::table('db')->find(1);
175-
} catch(dbException $e){
176-
177-
// Создаем основную запись в главной таблице
178-
$row = jsonDb::table('db');
179-
$row->type = 'root';
180-
$row->table = 'root';
181-
$row->version = '1.0.1';
182-
$row->time = $date;
183-
$row->user_key = $this->randomUid();
184-
$row->password = $this->randomUid();
185-
$row->temp = $this->temp;
186-
$row->api = $this->api;
187-
$row->cached = $this->cached;
188-
$row->cache_lifetime = $this->cache_lifetime;
189-
$row->export = $this->export;
190-
$row->size = $this->size;
191-
$row->max_size = $this->max_size;
192-
$row->db_path = $this->db_path;
193-
$row->dir_core = $this->dir_core;
194-
$row->dir_log = $this->dir_log;
195-
$row->dir_cached = $this->dir_cached;
196-
$row->save();
197-
198-
199-
}
200-
201-
// Читаем главную таблицу
202-
$table = jsonDb::table('db')->find(1);
203-
define('JSON_DB_USER_KEY', $table->user_key);
204-
define('JSON_DB_PASSWORD', $table->password);
20593

20694
// Проверяем существуют ли необходимые каталоги, если нет создаем
20795
if (!file_exists(JSON_DB_DB_PATH)){mkdir(JSON_DB_DB_PATH);}
@@ -309,7 +197,7 @@ public function run()
309197

310198
public static function cacheReader($uri) // Читает кеш или удаляет кеш если время жизни просрочено
311199
{
312-
if (JSON_DB_CACHED === 1) {
200+
if (JSON_DB_CACHED == '1') {
313201

314202
$row = jsonDb::table('cached')->where('cached_uri', '=', $uri)->find();
315203

0 commit comments

Comments
 (0)