Completed
Push — work-fleets ( 9cd586...f0ff6c )
by SuperNova.WS
04:56
created

classSupernova::db_transaction_clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
class classSupernova {
4
  /**
5
   * ex $sn_mvc
6
   *
7
   * @var array
8
   */
9
  public static $sn_mvc = array();
10
11
  /**
12
   * ex $functions
13
   *
14
   * @var array
15
   */
16
  public static $functions = array();
17
18
  /**
19
   * @var array[] $design
20
   */
21
  public $design = array(
22
    'bbcodes' => array(),
23
    'smiles'  => array(),
24
  );
25
26
  /**
27
   * Основная БД для доступа к данным
28
   *
29
   * @var db_mysql $db
30
   */
31
  public static $db;
32
  public static $db_name = '';
33
34
  /**
35
   * Настройки из файла конфигурации
36
   *
37
   * @var string
38
   */
39
  public static $cache_prefix = '';
40
  // public static $db_prefix = '';
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
41
  public static $sn_secret_word = '';
42
43
  /**
44
   * Конфигурация игры
45
   *
46
   * @var classConfig $config
47
   */
48
  public static $config;
49
50
51
  /**
52
   * Кэш игры
53
   *
54
   * @var classCache $cache
55
   */
56
  public static $cache;
57
58
59
  /**
60
   * @var core_auth $auth
61
   */
62
  public static $auth = null;
63
64
65
  public static $db_in_transaction = false;
66
  public static $db_records_locked = false;
67
  public static $transaction_id = 0;
68
  public static $user = array();
69
  /**
70
   * @var userOptions
71
   */
72
  public static $user_options;
73
74
  /**
75
   * @var debug $debug
76
   */
77
  public static $debug = null;
78
79
  public $options = array();
80
81
  /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
82
  // protected static $user = null;
83
  // protected static $planet = null;
84
  // protected static $ally = null;
85
  */
86
87
  public static $data = array(); // Кэш данных - юзера, планеты, юниты, очередь, альянсы итд
88
  public static $locks = array(); // Информация о блокировках
89
  public static $queries = array(); // Кэш запросов
90
91
  // Массив $locator - хранит отношения между записями для быстрого доступа по тип_записи:тип_локации:ид_локации:внутренний_ид_записи=>информация
92
  // Для LOC_UNIT внутренний ИД - это SNID, а информация - это ссылка на запись `unit`
93
  // Для LOC_QUE внутренний ИД - это тип очереди, а информация - массив ссылок на `que`
94
  public static $locator = array(); // Кэширует соответствия между расположением объектов - в частности юнитов и очередей
95
96
  public static $delayed_changset = array(); // Накопительный массив изменений
97
98
  // Кэш индексов - ключ MD5-строка от суммы ключевых строк через | - менять | на что-то другое перед поиском и назад - после поиска
99
  // Так же в индексах могут быть двойные вхождения - например, названия планет да и вообще
100
  // Придумать спецсимвол для NULL
101
  // protected static $indexes = array();
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
102
103
  /*
104
  TODO Кэш:
105
  1. Всегда дешевле использовать процессор, чем локальную память
106
  2. Всегда дешевле использовать локальную память, чем общую память всех процессов
107
  3. Всегда дешевле использовать общую память всех процессов, чем обращаться к БД
108
109
  Кэш - многоуровневый: локальная память-общая память-БД
110
  БД может быть сверхкэширующей - см. HyperNova. Это реализуется на уровне СН-драйвера БД
111
  Предусмотреть вариант, когда уровни кэширования совпадают, например когда нет xcache и используется общая память
112
  */
113
  //public static $cache; // Объект-кэшер - либо встроенная память, либо мемкэш с блокировками - находится внутри $db!!!!
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
114
  //public static $db; // Объект-БД - либок кэшер с блокировками, либо БД
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
115
116
  // protected static $info = array(); // Кэш информации - инфо о юнитах, инфо о группах итд
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
117
118
  // TODO Автоматически заполнять эту таблицу. В случае кэша в памяти - делать show table при обращении к таблице
119
  public static $location_info = array(
120
    LOC_USER => array(
121
      P_TABLE_NAME => 'users',
122
      P_ID         => 'id',
123
      P_OWNER_INFO => array(),
124
    ),
125
126
    LOC_PLANET => array(
127
      P_TABLE_NAME => 'planets',
128
      P_ID         => 'id',
129
      P_OWNER_INFO => array(
130
        LOC_USER => array(
131
          P_LOCATION    => LOC_USER,
132
          P_OWNER_FIELD => 'id_owner',
133
        ),
134
      ),
135
    ),
136
137
    LOC_UNIT => array(
138
      P_TABLE_NAME => 'unit',
139
      P_ID         => 'unit_id',
140
      P_OWNER_INFO => array(
141
        LOC_USER => array(
142
          P_LOCATION    => LOC_USER,
143
          P_OWNER_FIELD => 'unit_player_id',
144
        ),
145
      ),
146
    ),
147
148
    LOC_QUE => array(
149
      P_TABLE_NAME => 'que',
150
      P_ID         => 'que_id',
151
      P_OWNER_INFO => array(
152
        array(
153
          P_LOCATION    => LOC_USER,
154
          P_OWNER_FIELD => 'que_player_id',
155
        ),
156
157
        array(
158
          P_LOCATION    => LOC_PLANET,
159
          P_OWNER_FIELD => 'que_planet_id_origin',
160
        ),
161
162
        array(
163
          P_LOCATION    => LOC_PLANET,
164
          P_OWNER_FIELD => 'que_planet_id',
165
        ),
166
      ),
167
    ),
168
169
    LOC_FLEET => array(
170
      P_TABLE_NAME => 'fleets',
171
      P_ID         => 'fleet_id',
172
      P_OWNER_INFO => array(
173
        array(
174
          P_LOCATION    => LOC_USER,
175
          P_OWNER_FIELD => 'fleet_owner',
176
        ),
177
178
        array(
179
          P_LOCATION    => LOC_USER,
180
          P_OWNER_FIELD => 'fleet_target_owner',
181
        ),
182
183
        array(
184
          P_LOCATION    => LOC_PLANET,
185
          P_OWNER_FIELD => 'fleet_start_planet_id',
186
        ),
187
188
        array(
189
          P_LOCATION    => LOC_PLANET,
190
          P_OWNER_FIELD => 'fleet_end_planet_id',
191
        ),
192
      ),
193
    ),
194
  );
195
196
  /**
197
   * @param $db db_mysql
198
   */
199
  public static function init_main_db($db) {
200
    self::$db = $db;
201
    self::$db->sn_db_connect();
202
  }
203
204
205
  public static function log_file($message, $spaces = 0) {
206
    if(self::$debug) {
207
      self::$debug->log_file($message, $spaces);
208
    }
209
  }
210
211
  public static function debug_set_handler(&$debug) {
212
    self::$debug = $debug;
213
  }
214
215
  // Перепаковывает массив на заданную глубину, убирая поля с null
216
  public static function array_repack(&$array, $level = 0) {
217
    // TODO $lock_table не нужна тут
218
    if(!is_array($array)) {
219
      return;
220
    }
221
222
    foreach($array as $key => &$value) {
223
      if($value === null) {
224
        unset($array[$key]);
225
      } elseif($level > 0 && is_array($value)) {
226
        static::array_repack($value, $level - 1);
227
        if(empty($value)) {
228
          unset($array[$key]);
229
        }
230
      }
231
    }
232
  }
233
234
235
  // TODO Вынести в отдельный объект
236
  public static function cache_repack($location_type, $record_id = 0) {
237
    // Если есть $user_id - проверяем, а надо ли перепаковывать?
238
    if($record_id && isset(static::$data[$location_type][$record_id]) && static::$data[$location_type][$record_id] !== null) {
239
      return;
240
    }
241
242
    static::array_repack(static::$data[$location_type]);
243
    static::array_repack(static::$locator[$location_type], 3); // TODO У каждого типа локации - своя глубина!!!! Но можно и глубже ???
244
    static::array_repack(static::$queries[$location_type], 1);
245
  }
246
247
  public static function cache_clear($location_type, $hard = true) {
248
    //print("<br />CACHE CLEAR {$cache_id} " . ($hard ? 'HARD' : 'SOFT') . "<br />");
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
249
    if($hard && !empty(static::$data[$location_type])) {
250
      // Здесь нельзя делать unset - надо записывать NULL, что бы это отразилось на зависимых записях
251
      array_walk(static::$data[$location_type], function (&$item) { $item = null; });
252
    }
253
    static::$locator[$location_type] = array();
254
    static::$queries[$location_type] = array();
255
    static::cache_repack($location_type); // Перепаковываем внутренние структуры, если нужно
256
  }
257
258
  public static function cache_clear_all($hard = true) {
259
    //print('<br />CACHE CLEAR ALL<br />');
260
    if($hard) {
261
      static::$data = array();
262
      static::cache_lock_unset_all();
263
    }
264
    static::$locator = array();
265
    static::$queries = array();
266
  }
267
268
  public static function cache_get($location_type, $record_id) {
269
    return isset(static::$data[$location_type][$record_id]) ? static::$data[$location_type][$record_id] : null;
270
  }
271
272
  public static function cache_isset($location_type, $record_id) {
273
    return isset(static::$data[$location_type][$record_id]) && static::$data[$location_type][$record_id] !== null;
274
  }
275
276
  /* Кэшируем запись в соответствующий кэш
277
278
  Писать в кэш:
279
  1. Если записи не существует в кэше
280
  2. Если стоит $force_overwrite
281
  3. Если во время транзакции существующая запись не заблокирована
282
283
  Блокировать запись:
284
  1. Если идет транзакция и запись не заблокирована
285
  2. Если не стоит скип-лок
286
  */
287
  public static function cache_set($location_type, $record_id, $record, $force_overwrite = false, $skip_lock = false) {
0 ignored issues
show
Unused Code introduced by
The parameter $record_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
288
    // нет идентификатора - выход
289
    if(!($record_id = $record[static::$location_info[$location_type][P_ID]])) {
290
      return;
291
    }
292
293
    $in_transaction = static::db_transaction_check(false);
294
    if(
295
      $force_overwrite
296
      ||
297
      // Не заменяются заблокированные записи во время транзакции
298
      ($in_transaction && !static::cache_lock_get($location_type, $record_id))
299
      ||
300
      // !isset(static::$data[$location_type][$record_id]) || static::$data[$location_type][$record_id] === null
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
301
      !static::cache_isset($location_type, $record_id)
302
    ) {
303
      static::$data[$location_type][$record_id] = $record;
304
      if($in_transaction && !$skip_lock) {
305
        static::cache_lock_set($location_type, $record_id);
306
      }
307
    }
308
  }
309
310
  public static function cache_unset($cache_id, $safe_record_id) {
311
    // $record_id должен быть проверен заранее !
312
    if(isset(static::$data[$cache_id][$safe_record_id]) && static::$data[$cache_id][$safe_record_id] !== null) {
313
      // Выставляем запись в null
314
      static::$data[$cache_id][$safe_record_id] = null;
315
      // Очищаем кэш мягко - что бы удалить очистить связанные данные - кэш локаций и кэш запоросов и всё, что потребуется впредь
316
      static::cache_clear($cache_id, false);
317
    }
318
  }
319
320
  public static function cache_lock_get($location_type, $record_id) {
321
    return isset(static::$locks[$location_type][$record_id]);
322
  }
323
324
  public static function cache_lock_set($location_type, $record_id) {
325
    return static::$locks[$location_type][$record_id] = true; // Не всегда - от результата
326
  }
327
328
  public static function cache_lock_unset($location_type, $record_id) {
329
    if(isset(static::$locks[$location_type][$record_id])) {
330
      unset(static::$locks[$location_type][$record_id]);
331
    }
332
333
    return true; // Не всегда - от результата
334
  }
335
336
  public static function cache_lock_unset_all() {
337
    // Когда будем работать с xcache - это понадобиться, что бы снимать в xcache блокировки с записей
338
    // Пройти по массиву - снять блокировки для кэшера в памяти
339
    static::$locks = array();
340
341
    return true; // Не всегда - от результата
342
  }
343
344
345
346
347
348
  // TODO Вынести в отдельный объект
349
  /**
350
   * Эта функция проверяет статус транзакции
351
   *
352
   * Это - низкоуровневая функция. В нормальном состоянии движка её сообщения никогда не будут видны
353
   *
354
   * @param null|true|false $status Должна ли быть запущена транзакция в момент проверки
355
   *   <p>null - транзакция НЕ должна быть запущена</p>
356
   *   <p>true - транзакция должна быть запущена - для совместимости с $for_update</p>
357
   *   <p>false - всё равно - для совместимости с $for_update</p>
358
   *
359
   * @return bool Текущий статус транзакции
360
   */
361
  public static function db_transaction_check($status = null) {
362
    $error_msg = false;
363
    if($status && !static::$db_in_transaction) {
364
      $error_msg = 'No transaction started for current operation';
365
    } elseif($status === null && static::$db_in_transaction) {
366
      $error_msg = 'Transaction is already started';
367
    }
368
369
    if($error_msg) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $error_msg of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
370
      // TODO - Убрать позже
371
      print('<h1>СООБЩИТЕ ЭТО АДМИНУ: sn_db_transaction_check() - ' . $error_msg . '</h1>');
372
      $backtrace = debug_backtrace();
373
      array_shift($backtrace);
374
      pdump($backtrace);
375
      die($error_msg);
376
    }
377
378
    return static::$db_in_transaction;
379
  }
380
381
  public static function db_transaction_start($level = '') {
382
    static::db_transaction_check(null);
383
384
    $level ? doquery('SET TRANSACTION ISOLATION LEVEL ' . $level) : false;
385
386
    static::$transaction_id++;
387
    doquery('START TRANSACTION');
388
389
    if(classSupernova::$config->db_manual_lock_enabled) {
390
      classSupernova::$config->db_loadItem('var_db_manually_locked');
391
      classSupernova::$config->db_saveItem('var_db_manually_locked', SN_TIME_SQL);
392
    }
393
394
    static::$db_in_transaction = true;
395
    static::$locator = array();
396
    static::$queries = array();
397
398
    return static::$transaction_id;
399
  }
400
401
  public static function db_transaction_commit() {
402
    static::db_transaction_check(true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a null|object<true>|false.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
403
404
    if(!empty(static::$delayed_changset)) {
405
      static::db_changeset_apply(static::$delayed_changset, true);
406
      // pdump(static::$delayed_changset);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
407
    }
408
    doquery('COMMIT');
409
410
    return static::db_transaction_clear();
411
  }
412
413
  public static function db_transaction_rollback() {
414
    // static::db_transaction_check(true); // TODO - вообще-то тут тоже надо проверять есть ли транзакция
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
415
416
    if(!empty(static::$delayed_changset)) {
417
      static::db_changeset_revert();
418
    }
419
    doquery('ROLLBACK');
420
421
    return static::db_transaction_clear();
422
  }
423
424
  protected static function db_transaction_clear() {
425
    static::$delayed_changset = array();
426
    static::cache_lock_unset_all();
427
428
    static::$db_in_transaction = false;
429
    static::$db_records_locked = false;
430
    static::$transaction_id++;
431
432
    return static::$transaction_id;
433
  }
434
435
  /**
436
   * Блокирует указанные таблицу/список таблиц
437
   *
438
   * @param string|array $tables Таблица/список таблиц для блокировки. Названия таблиц - без префиксов
439
   * <p>string - название таблицы для блокировки</p>
440
   * <p>array - массив, где ключ - имя таблицы, а значение - условия блокировки элементов</p>
441
   */
442
  public static function db_lock_tables($tables) {
443
    $tables = is_array($tables) ? $tables : array($tables => '');
444
    foreach($tables as $table_name => $condition) {
445
      self::$db->doquery("SELECT 1 FROM {{{$table_name}}}" . ($condition ? ' WHERE ' . $condition : ''));
446
    }
447
  }
448
449
  public static function db_query($query, $fetch = false, $skip_lock = false) {
450
    $select = strpos(strtoupper($query), 'SELECT') !== false;
451
452
    $query .= $select && $fetch ? ' LIMIT 1' : '';
453
    $query .= $select && !$skip_lock && static::db_transaction_check(false) ? ' FOR UPDATE' : '';
454
455
    $result = self::$db->doquery($query, $fetch);
0 ignored issues
show
Documentation introduced by
$fetch is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
456
457
    return $result;
458
  }
459
460
  /**
461
   * Возвращает информацию о записи по её ID
462
   *
463
   * @param int       $location_type
464
   * @param int|array $record_id_unsafe
465
   *    <p>int - ID записи</p>
466
   *    <p>array - запись пользователя с установленным полем P_ID</p>
467
   * @param bool      $for_update @deprecated
468
   * @param string    $fields @deprecated список полей или '*'/'' для всех полей
469
   * @param bool      $skip_lock Указывает на то, что не нужно блокировать запись //TODO и не нужно сохранять в кэше
470
   *
471
   * @return array|false
472
   *    <p>false - Нет записи с указанным ID</p>
473
   *    <p>array - запись</p>
474
   */
475
  public static function db_get_record_by_id($location_type, $record_id_unsafe, $for_update = false, $fields = '*', $skip_lock = false) {
0 ignored issues
show
Unused Code introduced by
The parameter $for_update is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $fields is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $skip_lock is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
476
    $id_field = static::$location_info[$location_type][P_ID];
477
    // $record_id = intval(is_array($record_id) && isset($record_id[$id_field]) ? $record_id[$id_field] : $record_id);
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
478
    $record_id_safe = idval(is_array($record_id_unsafe) && isset($record_id_unsafe[$id_field]) ? $record_id_unsafe[$id_field] : $record_id_unsafe);
479
480
    return static::db_get_record_list($location_type, "`{$id_field}` = {$record_id_safe}", true, false);
481
  }
482
483
  public static function db_get_record_list($location_type, $filter = '', $fetch = false, $no_return = false) {
484
    //pdump($filter, 'Выбираем ' . $location_type);
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
485
    $query_cache = &static::$queries[$location_type][$filter];
486
487
    if(!isset($query_cache) || $query_cache === null) {
488
      // pdump($filter, 'Кэш пустой, начинаем возню');
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
489
      $location_info = &static::$location_info[$location_type];
490
      $id_field = $location_info[P_ID];
491
      $query_cache = array();
492
493
      if(static::db_transaction_check(false)) {
494
        //pdump($filter, 'Транзакция - блокируем ' . $location_type);
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
495
        // Проходим по всем родителям данной записи
496
        // foreach($location_info[P_OWNER_INFO] as $owner_location_type => $owner_data)
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
497
        foreach($location_info[P_OWNER_INFO] as $owner_data) {
498
          $owner_location_type = $owner_data[P_LOCATION];
499
          //pdump($filter, 'Транзакция - блокируем родителя ' . $owner_location_type);
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
500
          $parent_id_list = array();
501
          // Выбираем родителей данного типа и соответствующие ИД текущего типа
502
          $query = static::db_query(
503
            "SELECT
504
              distinct({{{$location_info[P_TABLE_NAME]}}}.{$owner_data[P_OWNER_FIELD]}) AS parent_id
505
            FROM {{{$location_info[P_TABLE_NAME]}}}" .
506
            ($filter ? ' WHERE ' . $filter : '') .
507
            ($fetch ? ' LIMIT 1' : ''), false, true);
508
509
          //pdump($q, 'Запрос блокировки');
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
510
          while($row = db_fetch($query)) {
511
            // Исключаем из списка родительских ИД уже заблокированные записи
512
            if(!static::cache_lock_get($owner_location_type, $row['parent_id'])) {
513
              $parent_id_list[$row['parent_id']] = $row['parent_id'];
514
            }
515
          }
516
          //pdump($parent_id_list, 'Выбраны родители');
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
517
          // Если все-таки какие-то записи еще не заблокированы - вынимаем текущие версии из базы
518
          if($indexes_str = implode(',', $parent_id_list)) {
519
            //pdump($indexes_str, '$indexes_str');
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
520
            $parent_id_field = static::$location_info[$owner_location_type][P_ID];
521
            static::db_get_record_list($owner_location_type,
522
              $parent_id_field . (count($parent_id_list) > 1 ? " IN ({$indexes_str})" : " = {$indexes_str}"), $fetch, true);
523
          }
524
          //pdump($filter, 'Транзакция - родители заблокированы ' . $owner_location_type);
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
525
        }
526
      }
527
528
      //pdump($filter, 'Выбираем записи и заносим их в кыш-память ' . $owner_location_type);
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
529
      $query = static::db_query(
530
        "SELECT * FROM {{{$location_info[P_TABLE_NAME]}}}" .
531
        (($filter = trim($filter)) ? " WHERE {$filter}" : '')
532
      );
533
      while($row = db_fetch($query)) {
534
        // static::db_get_record_by_id($location_type, $row[$id_field]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
79% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
535
        static::cache_set($location_type, $row[$id_field], $row);
536
        $query_cache[$row[$id_field]] = &static::$data[$location_type][$row[$id_field]];
537
        //pdump(static::$data[$location_type][$row[$id_field]]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
94% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
538
      }
539
    }
540
541
    if($no_return) {
542
      return true;
543
    } else {
544
      $result = false;
545
      if(is_array($query_cache)) {
546
        foreach($query_cache as $key => $value) {
547
          $result[$key] = $value;
548
          if($fetch) {
549
            break;
550
          }
551
        }
552
      }
553
554
      return $fetch ? (is_array($result) ? reset($result) : false) : $result;
555
    }
556
  }
557
558
  /**
559
   * @param int    $location_type
560
   * @param int    $record_id
561
   * @param string $set - SQL SET structure
562
   *
563
   * @return array|bool|mysqli_result|null
564
   */
565
  public static function db_upd_record_by_id($location_type, $record_id, $set) {
566
    //if(!($record_id = intval($record_id)) || !($set = trim($set))) return false;
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
567
    if(!($record_id = idval($record_id)) || !($set = trim($set))) {
568
      return false;
569
    }
570
571
    $location_info = &static::$location_info[$location_type];
572
    $id_field = $location_info[P_ID];
573
    $table_name = $location_info[P_TABLE_NAME];
574
    if($result = static::db_query($q = "UPDATE {{{$table_name}}} SET {$set} WHERE `{$id_field}` = {$record_id}")) // TODO Как-то вернуть может быть LIMIT 1 ?
575
    {
576
      if(static::$db->db_affected_rows()) {
577
        // Обновляем данные только если ряд был затронут
578
        // TODO - переделать под работу со структурированными $set
579
580
        // Тут именно так, а не cache_unset - что бы в кэшах автоматически обновилась запись. Будет нужно на будущее
581
        static::$data[$location_type][$record_id] = null;
582
        // Вытаскиваем обновленную запись
583
        static::db_get_record_by_id($location_type, $record_id);
584
        static::cache_clear($location_type, false); // Мягкий сброс - только $queries
585
      }
586
    }
587
588
    return $result;
589
  }
590
591
  public static function db_upd_record_list($location_type, $condition, $set) {
592
    if(!($set = trim($set))) {
593
      return false;
594
    }
595
596
    $condition = trim($condition);
597
    $table_name = static::$location_info[$location_type][P_TABLE_NAME];
598
599
//static::db_get_record_list($location_type, $condition, false, true);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
600
601
    if($result = static::db_query("UPDATE {{{$table_name}}} SET " . $set . ($condition ? ' WHERE ' . $condition : ''))) {
602
603
      if(static::$db->db_affected_rows()) { // Обновляем данные только если ряд был затронут
604
        // Поскольку нам неизвестно, что и как обновилось - сбрасываем кэш этого типа полностью
605
        // TODO - когда будет структурированный $condition и $set - перепаковывать данные
606
        static::cache_clear($location_type, true);
607
      }
608
    }
609
610
    return $result;
611
  }
612
613
  /**
614
   * @param int    $location_type
615
   * @param string $set
616
   *
617
   * @return array|bool|false|mysqli_result|null
618
   */
619
  public static function db_ins_record($location_type, $set) {
620
    $set = trim($set);
621
    $table_name = static::$location_info[$location_type][P_TABLE_NAME];
622 View Code Duplication
    if($result = static::db_query("INSERT INTO `{{{$table_name}}}` SET {$set}")) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
623
      if(static::$db->db_affected_rows()) // Обновляем данные только если ряд был затронут
624
      {
625
        $record_id = db_insert_id();
626
        // Вытаскиваем запись целиком, потому что в $set могли быть "данные по умолчанию"
627
        $result = static::db_get_record_by_id($location_type, $record_id);
628
        // Очищаем второстепенные кэши - потому что вставленная запись могла повлиять на результаты запросов или локация или еще чего
629
        // TODO - когда будет поддержка изменения индексов и локаций - можно будет вызывать её
630
        static::cache_clear($location_type, false); // Мягкий сброс - только $queries
631
      }
632
    }
633
634
    return $result;
635
  }
636
637
  public static function db_ins_field_set($location_type, $field_set, $serialize = false) {
638
    // TODO multiinsert
639
    !sn_db_field_set_is_safe($field_set) ? $field_set = sn_db_field_set_make_safe($field_set, $serialize) : false;
640
    sn_db_field_set_safe_flag_clear($field_set);
641
    $values = implode(',', $field_set);
642
    $fields = implode(',', array_keys($field_set));
643
644
    $table_name = static::$location_info[$location_type][P_TABLE_NAME];
645 View Code Duplication
    if($result = static::db_query("INSERT INTO `{{{$table_name}}}` ($fields) VALUES ($values);")) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
646
      if(static::$db->db_affected_rows()) {
647
        // Обновляем данные только если ряд был затронут
648
        $record_id = db_insert_id();
649
        // Вытаскиваем запись целиком, потому что в $set могли быть "данные по умолчанию"
650
        $result = static::db_get_record_by_id($location_type, $record_id);
651
        // Очищаем второстепенные кэши - потому что вставленная запись могла повлиять на результаты запросов или локация или еще чего
652
        // TODO - когда будет поддержка изменения индексов и локаций - можно будет вызывать её
653
        static::cache_clear($location_type, false); // Мягкий сброс - только $queries
654
      }
655
    }
656
657
    return $result;
658
  }
659
660
  public static function db_del_record_by_id($location_type, $safe_record_id) {
661
    // if(!($safe_record_id = intval($safe_record_id))) return false;
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
662
    if(!($safe_record_id = idval($safe_record_id))) {
663
      return false;
664
    }
665
666
    $location_info = &static::$location_info[$location_type];
667
    $id_field = $location_info[P_ID];
668
    $table_name = $location_info[P_TABLE_NAME];
669
    if($result = static::db_query("DELETE FROM `{{{$table_name}}}` WHERE `{$id_field}` = {$safe_record_id}")) {
670
      if(static::$db->db_affected_rows()) // Обновляем данные только если ряд был затронут
671
      {
672
        static::cache_unset($location_type, $safe_record_id);
673
      }
674
    }
675
676
    return $result;
677
  }
678
679
  public static function db_del_record_list($location_type, $condition) {
680
    if(!($condition = trim($condition))) {
681
      return false;
682
    }
683
684
    $location_info = &static::$location_info[$location_type];
685
    $table_name = $location_info[P_TABLE_NAME];
686
687
//static::db_get_record_list($location_type, $condition, false, true);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
688
689
    if($result = static::db_query("DELETE FROM `{{{$table_name}}}` WHERE {$condition}")) {
690
      if(static::$db->db_affected_rows()) // Обновляем данные только если ряд был затронут
691
      {
692
        // Обнуление кэша, потому что непонятно, что поменялось
693
        // TODO - когда будет структурированный $condition можно будет делать только cache_unset по нужным записям
694
        static::cache_clear($location_type);
695
      }
696
    }
697
698
    return $result;
699
  }
700
701
702
703
  // Работа с пользователями
704
  /**
705
   * Возвращает информацию о пользователе по его ID
706
   *
707
   * @param int|array $user_id_unsafe
708
   *    <p>int - ID пользователя</p>
709
   *    <p>array - запись пользователя с установленным полем ['id']</p>
710
   * @param bool      $for_update @deprecated
711
   * @param string    $fields @deprecated список полей или '*'/'' для всех полей
712
   * @param null      $player
713
   * @param bool|null $player Признак выбора записи пользователь типа "игрок"
714
   *    <p>null - Можно выбрать запись любого типа</p>
715
   *    <p>true - Выбирается только запись типа "игрок"</p>
716
   *    <p>false - Выбирается только запись типа "альянс"</p>
717
   *
718
   * @return array|false
719
   *    <p>false - Нет записи с указанным ID и $player</p>
720
   *    <p>array - запись типа $user</p>
721
   */
722
  public static function db_get_user_by_id($user_id_unsafe, $for_update = false, $fields = '*', $player = null) {
723
    $user = static::db_get_record_by_id(LOC_USER, $user_id_unsafe, $for_update, $fields);
724
725
    return (is_array($user) &&
726
      (
727
        $player === null
728
        ||
729
        ($player === true && !$user['user_as_ally'])
730
        ||
731
        ($player === false && $user['user_as_ally'])
732
      )) ? $user : false;
733
  }
734
735
  public static function db_get_user_by_username($username_unsafe, $for_update = false, $fields = '*', $player = null, $like = false) {
0 ignored issues
show
Unused Code introduced by
The parameter $for_update is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $fields is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
736
    // TODO Проверить, кстати - а везде ли нужно выбирать юзеров или где-то все-таки ищутся Альянсы ?
737
    if(!($username_unsafe = trim($username_unsafe))) {
738
      return false;
739
    }
740
741
    $user = null;
742
    if(is_array(static::$data[LOC_USER])) {
743
      foreach(static::$data[LOC_USER] as $user_id => $user_data) {
744
        if(is_array($user_data) && isset($user_data['username'])) {
745
          // проверяем поле
746
          // TODO Возможно есть смысл всегда искать по strtolower - но может игрок захочет переименоваться с другим регистром? Проверить!
747
          if((!$like && $user_data['username'] == $username_unsafe) || ($like && strtolower($user_data['username']) == strtolower($username_unsafe))) {
748
            // $user_as_ally = intval($user_data['user_as_ally']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
749
            $user_as_ally = idval($user_data['user_as_ally']);
750
            if($player === null || ($player === true && !$user_as_ally) || ($player === false && $user_as_ally)) {
751
              $user = $user_data;
752
              break;
753
            }
754
          }
755
        }
756
      }
757
    }
758
759 View Code Duplication
    if($user === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
760
      // Вытаскиваем запись
761
      $username_safe = db_escape($like ? strtolower($username_unsafe) : $username_unsafe); // тут на самом деле strtolower() лишняя, но пусть будет
762
763
      // TODO переписать
764
      // self::db_get_record_list(LOC_USER, "`username` " . ($like ? 'LIKE' : '='). " '{$username_safe}'");
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
765
766
      $user = static::db_query(
767
        "SELECT * FROM {{users}} WHERE `username` " . ($like ? 'LIKE' : '=') . " '{$username_safe}'"
768
        , true);
769
      static::cache_set(LOC_USER, $user['id'], $user); // В кэш-юзер так же заполнять индексы
770
    }
771
772
    return $user;
773
  }
774
775
  // UNUSED
776
  public static function db_get_user_by_email($email_unsafe, $use_both = false, $for_update = false, $fields = '*') {
0 ignored issues
show
Unused Code introduced by
The parameter $for_update is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $fields is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
777
    if(!($email_unsafe = strtolower(trim($email_unsafe)))) {
778
      return false;
779
    }
780
781
    $user = null;
782
    // TODO переделать на индексы
783
    if(is_array(static::$data[LOC_USER])) {
784
      foreach(static::$data[LOC_USER] as $user_id => $user_data) {
785
        if(is_array($user_data) && isset($user_data['email_2'])) {
786
          // проверяем поле
787
          if(strtolower($user_data['email_2']) == $email_unsafe || ($use_both && strtolower($user_data['email']) == $email_unsafe)) {
788
            $user = $user_data;
789
            break;
790
          }
791
        }
792
      }
793
    }
794
795 View Code Duplication
    if($user === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
796
      // Вытаскиваем запись
797
      $email_safe = db_escape($email_unsafe);
798
      $user = static::db_query(
799
        "SELECT * FROM {{users}} WHERE LOWER(`email_2`) = '{$email_safe}'" .
800
        ($use_both ? " OR LOWER(`email`) = '{$email_safe}'" : '')
801
        , true);
802
803
      static::cache_set(LOC_USER, $user['id'], $user); // В кэш-юзер так же заполнять индексы
804
    }
805
806
    return $user;
807
  }
808
809
  public static function db_get_user_by_where($where_safe, $for_update = false, $fields = '*') {
0 ignored issues
show
Unused Code introduced by
The parameter $for_update is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $fields is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
810
//    if(!($email_unsafe = strtolower(trim($email_unsafe)))) return false;
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
811
    $user = null;
812
    // TODO переделать на индексы
813
814
    if($user === null && !empty($where_safe)) {
815
      // Вытаскиваем запись
816
      $user = static::db_query("SELECT * FROM {{users}} WHERE {$where_safe}", true);
817
818
      static::cache_set(LOC_USER, $user['id'], $user); // В кэш-юзер так же заполнять индексы
819
    }
820
821
    return $user;
822
  }
823
824
825 View Code Duplication
  public static function db_unit_time_restrictions($date = SN_TIME_NOW) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
826
    $date = is_numeric($date) ? "FROM_UNIXTIME({$date})" : "'{$date}'";
827
828
    return
829
      "(unit_time_start IS NULL OR unit_time_start <= {$date}) AND
830
    (unit_time_finish IS NULL OR unit_time_finish = '1970-01-01 03:00:00' OR unit_time_finish >= {$date})";
831
  }
832
833
  public static function db_get_unit_by_id($unit_id, $for_update = false, $fields = '*') {
834
    // TODO запихивать в $data[LOC_LOCATION][$location_type][$location_id]
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
835
    $unit = static::db_get_record_by_id(LOC_UNIT, $unit_id, $for_update, $fields);
836
    if(is_array($unit)) {
837
      static::$locator[LOC_UNIT][$unit['unit_location_type']][$unit['unit_location_id']][$unit['unit_snid']] = &static::$data[LOC_UNIT][$unit_id];
838
    }
839
840
    return $unit;
841
  }
842
843
  /**
844
   * @param int $user_id
845
   * @param     $location_type
846
   * @param     $location_id
847
   *
848
   * @return array|bool
849
   */
850
  public static function db_get_unit_list_by_location($user_id = 0, $location_type, $location_id) {
0 ignored issues
show
Unused Code introduced by
The parameter $user_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
851
    //if(!($location_type = intval($location_type)) || !($location_id = intval($location_id))) return false;
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
852
    if(!($location_type = idval($location_type)) || !($location_id = idval($location_id))) {
853
      return false;
854
    }
855
856
    $query_cache = &static::$locator[LOC_UNIT][$location_type][$location_id];
857
    if(!isset($query_cache)) {
858
      $got_data = static::db_get_record_list(LOC_UNIT, "unit_location_type = {$location_type} AND unit_location_id = {$location_id} AND " . static::db_unit_time_restrictions());
859
      if(is_array($got_data)) {
860
        foreach($got_data as $unit_id => $unit_data) {
861
          // static::$data[LOC_LOCATION][$location_type][$location_id][$unit_data['unit_snid']] = &static::$data[LOC_UNIT][$unit_id];
0 ignored issues
show
Unused Code Comprehensibility introduced by
82% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
862
          $query_cache[$unit_data['unit_snid']] = &static::$data[LOC_UNIT][$unit_id];
863
        }
864
      }
865
    }
866
867
    $result = false;
868
    if(is_array($query_cache)) {
869
      foreach($query_cache as $key => $value) {
870
        $result[$key] = $value;
871
      }
872
    }
873
874
    return $result;
875
  }
876
877
  public static function db_get_unit_by_location($user_id = 0, $location_type, $location_id, $unit_snid = 0, $for_update = false, $fields = '*') {
0 ignored issues
show
Unused Code introduced by
The parameter $for_update is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $fields is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
878
    static::db_get_unit_list_by_location($user_id, $location_type, $location_id);
879
880
    return $unit_snid ? static::$locator[LOC_UNIT][$location_type][$location_id][$unit_snid] : static::$locator[LOC_UNIT][$location_type][$location_id];
881
  }
882
883
884
  /*
885
   * С $for_update === true эта функция должна вызываться только из транзакции! Все соответствующие записи в users и planets должны быть уже блокированы!
886
   *
887
   * $que_type
888
   *   !$que_type - все очереди
889
   *   QUE_XXXXXX - конкретная очередь по планете
890
   * $user_id - ID пользователя
891
   * $planet_id
892
   *   $que_type == QUE_RESEARCH - игнорируется
893
   *   null - обработка очередей планет не производится
894
   *   false/0 - обрабатываются очереди всех планет по $user_id
895
   *   (integer) - обрабатываются локальные очереди для планеты. Нужно, например, в обработчике флотов
896
   *   иначе - $que_type для указанной планеты
897
   * $for_update - true == нужно блокировать записи
898
   *
899
   * TODO Работа при !$user_id
900
   * TODO Переформатировать вывод данных, что бы можно было возвращать данные по всем планетам и юзерам в одном запросе: добавить подмассивы 'que', 'planets', 'players'
901
   *
902
   */
903
  public static function db_que_list_by_type_location($user_id, $planet_id = null, $que_type = false, $for_update = false) {
0 ignored issues
show
Unused Code introduced by
The parameter $for_update is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
904
    if(!$user_id) {
905
      pdump(debug_backtrace());
906
      die('No user_id for que_get_que()');
907
    }
908
909
    $ques = array();
910
911
    $query = array();
912
913
    // if($user_id = intval($user_id))
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
914
    if($user_id = idval($user_id)) {
915
      $query[] = "`que_player_id` = {$user_id}";
916
    }
917
918
    if($que_type == QUE_RESEARCH || $planet_id === null) {
919
      $query[] = "`que_planet_id` IS NULL";
920
    } elseif($planet_id) {
921
      $query[] = "(`que_planet_id` = {$planet_id}" . ($que_type ? '' : ' OR que_planet_id IS NULL') . ")";
922
    }
923
    if($que_type) {
924
      $query[] = "`que_type` = {$que_type}";
925
    }
926
927
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
51% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
928
    $sql = '';
929
    $sql .= $user_id ? " AND `que_player_id` = {$user_id}" : '';
930
    $sql .= $que_type == QUE_RESEARCH || $planet_id === null ? " AND `que_planet_id` IS NULL" :
931
      ($planet_id ? " AND (`que_planet_id` = {$planet_id}" . ($que_type ? '' : ' OR que_planet_id IS NULL') . ")" : '');
932
    $sql .= $que_type ? " AND `que_type` = {$que_type}" : '';
933
    pdump($sql);
934
    pdump(implode(' AND ', $query));
935
936
    $que_query = ($sql = implode(' AND ', $query))
937
      ? doquery("SELECT * FROM {{que}} WHERE {$sql} ORDER BY que_id" . ($for_update ? ' FOR UPDATE' : ''))
938
      : false;
939
940
    if($que_query)
941
    {
942
      while($row = db_fetch($que_query))
943
      {
944
        $ques['items'][] = $row;
945
      }
946
    }
947
    */
948
949
    $ques['items'] = static::db_get_record_list(LOC_QUE, implode(' AND ', $query));
950
951
    return que_recalculate($ques);
952
  }
953
954
955 View Code Duplication
  public static function db_changeset_prepare_unit($unit_id, $unit_value, $user, $planet_id = null) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
956
    if(!is_array($user)) {
957
      // TODO - remove later
958
      print('<h1>СООБЩИТЕ ЭТО АДМИНУ: sn_db_unit_changeset_prepare() - USER is not ARRAY</h1>');
959
      pdump(debug_backtrace());
960
      die('USER is not ARRAY');
961
    }
962
    if(!isset($user['id']) || !$user['id']) {
963
      // TODO - remove later
964
      print('<h1>СООБЩИТЕ ЭТО АДМИНУ: sn_db_unit_changeset_prepare() - USER[id] пустой</h1>');
965
      pdump($user);
966
      pdump(debug_backtrace());
967
      die('USER[id] пустой');
968
    }
969
    $planet_id = is_array($planet_id) && isset($planet_id['id']) ? $planet_id['id'] : $planet_id;
970
971
    $unit_location = sys_get_unit_location($user, array(), $unit_id);
972
    $location_id = $unit_location == LOC_USER ? $user['id'] : $planet_id;
973
    $location_id = $location_id ? $location_id : 'NULL';
974
975
    $db_changeset = array();
0 ignored issues
show
Unused Code introduced by
$db_changeset is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
976
    $temp = db_unit_by_location($user['id'], $unit_location, $location_id, $unit_id, true, 'unit_id');
977
    if($temp['unit_id']) {
978
      $db_changeset = array(
979
        'action'  => SQL_OP_UPDATE,
980
        P_VERSION => 1,
981
        'where'   => array(
982
          "unit_id" => $temp['unit_id'],
983
        ),
984
        'fields'  => array(
985
          'unit_level' => array(
986
            'delta' => $unit_value
987
          ),
988
        ),
989
      );
990
    } else {
991
      $db_changeset = array(
992
        'action' => SQL_OP_INSERT,
993
        'fields' => array(
994
          'unit_player_id'     => array(
995
            'set' => $user['id'],
996
          ),
997
          'unit_location_type' => array(
998
            'set' => $unit_location,
999
          ),
1000
          'unit_location_id'   => array(
1001
            'set' => $unit_location == LOC_USER ? $user['id'] : $planet_id,
1002
          ),
1003
          'unit_type'          => array(
1004
            'set' => get_unit_param($unit_id, P_UNIT_TYPE),
1005
          ),
1006
          'unit_snid'          => array(
1007
            'set' => $unit_id,
1008
          ),
1009
          'unit_level'         => array(
1010
            'set' => $unit_value,
1011
          ),
1012
        ),
1013
      );
1014
    }
1015
1016
    return $db_changeset;
1017
  }
1018
1019
1020
  public function db_changeset_delay($table_name, $table_data) {
1021
    // TODO Применять ченджсет к записям
1022
    static::$delayed_changset[$table_name] = is_array(static::$delayed_changset[$table_name]) ? static::$delayed_changset[$table_name] : array();
1023
    // TODO - На самом деле дурацкая оптимизация, если честно - может быть идентичные записи с идентичными дельтами - и привет. Но не должны, конечно
1024
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1025
    foreach($table_data as $key => $value)
1026
    {
1027
      if(array_search($value, static::$delayed_changset[$table_name]) !== false)
1028
      {
1029
        unset($table_data[$key]);
1030
      }
1031
    }
1032
    */
1033
    static::$delayed_changset[$table_name] = array_merge(static::$delayed_changset[$table_name], $table_data);
1034
    // pdump(static::$delayed_changset);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1035
    // die();
1036
  }
1037
1038
  public function db_changeset_revert() {
1039
    // TODO Для этапа 1 - достаточно чистить только те таблицы, что были затронуты
1040
    // Для этапа 2 - чистить только записи
1041
    // Для этапа 3 - возвращать всё
1042
    static::cache_clear_all(true);
1043
  }
1044
1045
  public function db_changeset_condition_compile(&$conditions, &$table_name = '') {
1046
    if(!$conditions[P_LOCATION] || $conditions[P_LOCATION] == LOC_NONE) {
1047
      $conditions[P_LOCATION] = LOC_NONE;
1048
      switch($table_name) {
1049
        case 'users':
1050
        case LOC_USER:
1051
          $conditions[P_TABLE_NAME] = $table_name = 'users';
1052
          $conditions[P_LOCATION] = LOC_USER;
1053
        break;
1054
1055
        case 'planets':
1056
        case LOC_PLANET:
1057
          $conditions[P_TABLE_NAME] = $table_name = 'planets';
1058
          $conditions[P_LOCATION] = LOC_PLANET;
1059
        break;
1060
1061
        case 'unit':
1062
        case LOC_UNIT:
1063
          $conditions[P_TABLE_NAME] = $table_name = 'unit';
1064
          $conditions[P_LOCATION] = LOC_UNIT;
1065
        break;
1066
      }
1067
    }
1068
1069
    $conditions[P_FIELDS_STR] = '';
1070
    if($conditions['fields']) {
1071
      $fields = array();
1072 View Code Duplication
      foreach($conditions['fields'] as $field_name => $field_data) {
0 ignored issues
show
Bug introduced by
The expression $conditions['fields'] of type string is not traversable.
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1073
        $condition = "`{$field_name}` = ";
1074
        $value = '';
1075
        if($field_data['delta']) {
1076
          $value = "`{$field_name}`" . ($field_data['delta'] >= 0 ? '+' : '') . $field_data['delta'];
1077
        } elseif($field_data['set']) {
1078
          $value = (is_string($field_data['set']) ? "'{$field_data['set']}'" : $field_data['set']);
1079
        }
1080
1081
        if($value) {
1082
          $fields[] = $condition . $value;
1083
        }
1084
      }
1085
      $conditions[P_FIELDS_STR] = implode(',', $fields);
1086
    }
1087
1088
    $conditions[P_WHERE_STR] = '';
1089
    if(!empty($conditions['where'])) {
1090
      if($conditions[P_VERSION] == 1) {
1091
        $the_conditions = array();
1092
        foreach($conditions['where'] as $field_id => $field_value) {
0 ignored issues
show
Bug introduced by
The expression $conditions['where'] of type string is not traversable.
Loading history...
1093
          // Простое условие - $field_id = $field_value
1094
          if(is_string($field_id)) {
1095
            $field_value =
1096
              $field_value === null ? 'NULL' :
1097
                (is_string($field_value) ? "'" . db_escape($field_value) . "'" :
1098
                  (is_bool($field_value) ? intval($field_value) : $field_value));
1099
            $the_conditions[] = "`{$field_id}` = {$field_value}";
1100
          } else {
1101
            die('Неподдерживаемый тип условия');
1102
          }
1103
        }
1104
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1105
        print('<h1>НЕ СООБЩАТЬ! НЕ ОШИБКА!</h1>');
1106
        pdump($conditions['where']);
1107
        pdump($the_conditions);
1108
        */
1109
      } else {
1110
        $the_conditions = &$conditions['where'];
1111
      }
1112
      $conditions[P_WHERE_STR] = implode(' AND ', $the_conditions);
1113
    }
1114
1115
    switch($conditions['action']) {
1116
      case SQL_OP_DELETE:
1117
        $conditions[P_ACTION_STR] = ("DELETE FROM {{{$table_name}}}");
1118
      break;
1119
      case SQL_OP_UPDATE:
1120
        $conditions[P_ACTION_STR] = ("UPDATE {{{$table_name}}} SET");
1121
      break;
1122
      case SQL_OP_INSERT:
1123
        $conditions[P_ACTION_STR] = ("INSERT INTO {{{$table_name}}} SET");
1124
      break;
1125
      // case SQL_OP_REPLACE: $result = doquery("REPLACE INTO {{{$table_name}}} SET {$fields}") && $result; break;
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1126
      default:
1127
        die('Неподдерживаемая операция в classSupernova::db_changeset_condition_compile');
1128
    }
1129
1130
    $conditions[P_QUERY_STR] = $conditions[P_ACTION_STR] . ' ' . $conditions[P_FIELDS_STR] . (' WHERE ' . $conditions[P_WHERE_STR]);
1131
  }
1132
1133
  public static function db_changeset_apply($db_changeset, $flush_delayed = false) {
0 ignored issues
show
Unused Code introduced by
The parameter $flush_delayed is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1134
    $result = true;
1135
    if(!is_array($db_changeset) || empty($db_changeset)) {
1136
      return $result;
1137
    }
1138
1139
    foreach($db_changeset as $table_name => &$table_data) {
1140
      // TODO - delayed changeset
1141
      /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1142
      if(static::db_transaction_check(false) && !$flush_delayed && ($table_name == 'users' || $table_name == 'planets' || $table_name == 'unit'))
1143
      {
1144
        static::db_changeset_delay($table_name, $table_data);
1145
        continue;
1146
      }
1147
      */
1148
      foreach($table_data as $record_id => &$conditions) {
1149
        static::db_changeset_condition_compile($conditions, $table_name);
1150
1151
        if($conditions['action'] != SQL_OP_DELETE && !$conditions[P_FIELDS_STR]) {
1152
          continue;
1153
        }
1154
        if($conditions['action'] == SQL_OP_DELETE && !$conditions[P_WHERE_STR]) {
1155
          continue;
1156
        } // Защита от случайного удаления всех данных в таблице
1157
1158
        if($conditions[P_LOCATION] != LOC_NONE) {
1159
          //die('spec ops supernova.php line 928 Добавить работу с кэшем юнитов итд');
1160
          switch($conditions['action']) {
1161
            case SQL_OP_DELETE:
1162
              $result = self::db_del_record_list($conditions[P_LOCATION], $conditions[P_WHERE_STR]) && $result;
1163
            break;
1164
            case SQL_OP_UPDATE:
1165
              $result = self::db_upd_record_list($conditions[P_LOCATION], $conditions[P_WHERE_STR], $conditions[P_FIELDS_STR]) && $result;
1166
            break;
1167
            case SQL_OP_INSERT:
1168
              $result = self::db_ins_record($conditions[P_LOCATION], $conditions[P_FIELDS_STR]) && $result;
1169
            break;
1170
            default:
1171
              die('Неподдерживаемая операция в classSupernova::db_changeset_apply');
1172
            // case SQL_OP_REPLACE: $result = $result && doquery("REPLACE INTO {{{$table_name}}} SET {$fields}"); break;
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1173
          }
1174
        } else {
1175
          $result = doquery($conditions[P_QUERY_STR]) && $result;
1176
        }
1177
      }
1178
    }
1179
1180
    return $result;
1181
  }
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
  // que_process не всегда должна работать в режиме прямой работы с БД !! Она может работать и в режиме эмуляции
1218
  // !!!!!!!! После que_get брать не [0] элемент, а first() - тогда можно в индекс элемента засовывать que_id из таблицы
1219
1220
1221
  // TODO - это вообще-то надо хранить в конфигурации
1222
  public static function db_get_user_player_username_last_registered() {
1223
    $user = static::db_query('SELECT * FROM {{users}} WHERE `user_as_ally` IS NULL ORDER BY `id` DESC', true);
1224
    static::cache_set(LOC_USER, $user['id'], $user);
1225
1226
    return isset($user['username']) ? $user['username'] : '';
1227
  }
1228
1229
  // Это для поиска по кэшу
1230
  protected static function db_get_record_by_field($location_type) {
0 ignored issues
show
Unused Code introduced by
The parameter $location_type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1231
  }
1232
1233
  // Для модулей - регистрация юнитов
1234
  public static function unit_register() {
1235
1236
  }
1237
1238
1239
  public static function init_0_prepare() {
1240
    // Отключаем magic_quotes
1241
    ini_get('magic_quotes_sybase') ? die('SN is incompatible with \'magic_quotes_sybase\' turned on. Disable it in php.ini or .htaccess...') : false;
1242
    if(@get_magic_quotes_gpc()) {
1243
      $gpcr = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
1244
      array_walk_recursive($gpcr, function (&$value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1245
        $value = stripslashes($value);
1246
      });
1247
    }
1248
    if(function_exists('set_magic_quotes_runtime')) {
1249
      @set_magic_quotes_runtime(0);
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
1250
      @ini_set('magic_quotes_runtime', 0);
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
1251
      @ini_set('magic_quotes_sybase', 0);
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
1252
    }
1253
  }
1254
1255
  public static function init_1_constants() {
1256
//    global $phpEx, $phpbb_root_path; // Это нужно для работы PTL
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1257
1258
//    define('SN_TIME_NOW', intval(SN_TIME_MICRO));
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1259
//    define('SN_TIME_ZONE_OFFSET', date('Z'));
1260
//
1261
//    define('FMT_DATE_TIME_SQL', 'Y-m-d H:i:s');
1262
//    define('SN_TIME_SQL', date(FMT_DATE_TIME_SQL, SN_TIME_NOW));
1263
1264
//    if(strpos(strtolower($_SERVER['SERVER_NAME']), 'google.') !== false) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1265
//      define('SN_GOOGLE', true);
1266
//    }
1267
1268
//    $phpEx = strpos($phpEx = substr(strrchr(__FILE__, '.'), 1), '/') === false ? $phpEx : '';
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1269
//    define('PHP_EX', $phpEx); // PHP extension on this server
1270
//    define('DOT_PHP_EX', '.' . PHP_EX); // PHP extension on this server
1271
1272
//    $sn_root_relative = str_replace('\\', '/', getcwd());
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1273
//    $sn_root_relative .= $sn_root_relative[strlen($sn_root_relative) - 1] == '/' ? '' : '/';
1274
//    $sn_root_relative = str_replace(SN_ROOT_PHYSICAL, '', $sn_root_relative);
1275
//    $sn_root_relative .= basename($_SERVER['SCRIPT_NAME']);
1276
//    $sn_root_relative = str_replace($sn_root_relative, '', $_SERVER['SCRIPT_NAME']);
1277
//    define('SN_ROOT_RELATIVE', $sn_root_relative);
1278
1279
//    define('SN_ROOT_VIRTUAL' , 'http' . (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . SN_ROOT_RELATIVE);
0 ignored issues
show
Unused Code Comprehensibility introduced by
51% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1280
//    define('SN_ROOT_VIRTUAL_PARENT' , str_replace('//google.', '//', SN_ROOT_VIRTUAL));
1281
//
1282
//    $phpbb_root_path = SN_ROOT_PHYSICAL; // Это нужно для работы PTL
1283
  }
1284
1285
  public static function init_3_load_config_file() {
1286
    $dbsettings = array();
1287
1288
    require(SN_ROOT_PHYSICAL . "config" . DOT_PHP_EX);
1289
    //self::$db_prefix = $dbsettings['prefix'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1290
    self::$cache_prefix = !empty($dbsettings['cache_prefix']) ? $dbsettings['cache_prefix'] : $dbsettings['prefix'];
1291
    self::$db_name = $dbsettings['name'];
1292
    self::$sn_secret_word = $dbsettings['secretword'];
1293
    unset($dbsettings);
1294
  }
1295
1296
  public static function init_global_objects() {
1297
    /**
1298
     * @var classSupernova $supernova
1299
     */
1300
    global $supernova, $sn_cache;
1301
1302
    self::$user_options = new userOptions(0);
1303
1304
    /**
1305
     * @var classSupernova $supernova
1306
     */
1307
    $supernova = new classSupernova();
1308
1309
    // Initializing global 'cacher' object
1310
    static::$cache = new classCache(classSupernova::$cache_prefix);
1311
    $sn_cache = static::$cache;
1312
    empty($sn_cache->tables) ? sys_refresh_tablelist() : false;
1313
    empty($sn_cache->tables) ? die('DB error - cannot find any table. Halting...') : false;
1314
1315
    // Initializing global "config" object
1316
    classSupernova::$config = static::$config = new classConfig(classSupernova::$cache_prefix);
1317
  }
1318
1319
  public static function init_debug_state() {
1320
    if($_SERVER['SERVER_NAME'] == 'localhost' && !defined('BE_DEBUG')) {
1321
      define('BE_DEBUG', true);
1322
    }
1323
// define('DEBUG_SQL_ONLINE', true); // Полный дамп запросов в рил-тайме. Подойдет любое значение
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1324
    define('DEBUG_SQL_ERROR', true); // Выводить в сообщении об ошибке так же полный дамп запросов за сессию. Подойдет любое значение
1325
    define('DEBUG_SQL_COMMENT_LONG', true); // Добавлять SQL запрос длинные комментарии. Не зависим от всех остальных параметров. Подойдет любое значение
1326
    define('DEBUG_SQL_COMMENT', true); // Добавлять комментарии прямо в SQL запрос. Подойдет любое значение
1327
// Включаем нужные настройки
1328
    defined('DEBUG_SQL_ONLINE') && !defined('DEBUG_SQL_ERROR') ? define('DEBUG_SQL_ERROR', true) : false;
1329
    defined('DEBUG_SQL_ERROR') && !defined('DEBUG_SQL_COMMENT') ? define('DEBUG_SQL_COMMENT', true) : false;
1330
    defined('DEBUG_SQL_COMMENT_LONG') && !defined('DEBUG_SQL_COMMENT') ? define('DEBUG_SQL_COMMENT', true) : false;
1331
1332
    if(defined('BE_DEBUG') || static::$config->debug) {
1333
      @define('BE_DEBUG', true);
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
1334
      @ini_set('display_errors', 1);
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
1335
      @error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED);
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
1336
    } else {
1337
      @define('BE_DEBUG', false);
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
1338
      @ini_set('display_errors', 0);
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
1339
    }
1340
1341
  }
1342
1343
}
1344