sn_options_admin_protection()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 11
rs 10
c 1
b 0
f 0
1
<?php
2
3
use DBAL\db_mysql;
4
use DBAL\DbQuery;
5
use Fleet\DbFleetStatic;
6
use Old\Avatar;
7
use Planet\DBStaticPlanet;
8
use Player\playerTimeDiff;
9
10
/**
11
 * options.php
12
 *
13
 * @copyright (c) 2010-2017 by Gorlum for http://supernova.ws
14
 */
15
16
function sn_options_model() {
17
  global $user, $template_result;
18
19
  $language_new = sys_get_param_str('langer', $user['lang']);
20
  if ($language_new != $user['lang']) {
21
    SN::$lang->lng_switch($language_new);
22
  }
23
24
  lng_include('options');
25
  lng_include('messages');
26
27
  sys_user_options_unpack($user);
28
29
  $savedOk = false;
30
//  if (sys_get_param_str('mode') == 'change') {
31
  if (sys_get_param_str('save_settings')) {
32
    if (!is_array($template_result['.']['result'])) {
33
      $template_result['.']['result'] = [];
34
    }
35
36
    $user = sn_options_admin_protection($user);
37
    $user = sn_options_vacation($user);
38
    $user = sn_options_gender($user);
39
    $user = sn_options_change_birthday($user);
40
    $user = sn_options_deprecated($user);
0 ignored issues
show
Deprecated Code introduced by
The function sn_options_deprecated() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

40
    $user = /** @scrutinizer ignore-deprecated */ sn_options_deprecated($user);
Loading history...
41
    sn_options_player_standard();
42
43
    $template_result['.']['result'][] = sn_options_change_password();
44
    list($user, $usernameResult) = sn_options_change_username($user);
45
    $template_result['.']['result'] = array_merge($template_result['.']['result'], $usernameResult);
46
47
    playerTimeDiff::sn_options_timediff(
48
      sys_get_param_int('PLAYER_OPTION_TIME_DIFF'),
49
      sys_get_param_int('PLAYER_OPTION_TIME_DIFF_FORCED'),
50
      sys_get_param_int('opt_time_diff_clear')
51
    );
52
53
    $avatar_upload_result = Avatar::sys_avatar_upload($user['id'], $user['avatar']);
54
    $template_result['.']['result'][] = $avatar_upload_result;
55
56
    $user['email'] = sys_get_param_str('db_email');
57
    SN::$gc->theUser->setSkinName(sys_get_param_str('skin_name'));
58
    $user['lang'] = sys_get_param_str('langer', $user['lang']);
59
    $user['design'] = sys_get_param_int('design');
60
    $user['noipcheck'] = sys_get_param_int('noipcheck');
61
    $user['deltime'] = !sys_get_param_int('deltime') ? 0 : ($user['deltime'] ? $user['deltime'] : SN_TIME_NOW + SN::$config->player_delete_time);
62
63
    DbQuery::build(SN::$db)
64
      ->setTable('users')
65
      ->setValues([
66
        'email'                    => $user['email'],
67
        'lang'                     => $user['lang'],
68
        'avatar'                   => $user['avatar'],
69
        'design'                   => $user['design'],
70
        'noipcheck'                => $user['noipcheck'],
71
        'deltime'                  => $user['deltime'],
72
        'vacation'                 => $user['vacation'],
73
        'gender'                   => $user['gender'],
74
        'skin'                     => SN::$gc->theUser->getSkinName(),
75
        'user_birthday'            => $user['user_birthday'],
76
        'user_birthday_celebrated' => $user['user_birthday_celebrated'],
77
        'options'                  => $user['options'],
78
      ])
79
      ->setWhereArray(['id' => $user['id']])
80
      ->doUpdate();
81
82
    $savedOk = true;
83
  } elseif (sys_get_param_str('result') == 'ok') {
84
    $savedOk = true;
85
  }
86
87
  if ($savedOk) {
88
    $template_result['.']['result'][] = array(
89
      'STATUS'  => ERR_NONE,
90
      'MESSAGE' => SN::$lang['opt_msg_saved']
91
    );
92
  }
93
}
94
95
//-------------------------------
96
97
function sn_options_view($template = null) {
98
  global $lang, $template_result, $user, $planetrow, $user_option_list, $user_option_types, $sn_message_class_list, $config;
99
100
  sys_user_vacation($user);
101
102
  $FMT_DATE = preg_replace(array('/d/', '/m/', '/Y/'), array('DD', 'MM', 'YYYY'), FMT_DATE);
103
104
  $template = SnTemplate::gettemplate('options', $template);
105
106
  $dir = dir(SN_ROOT_PHYSICAL . 'skins');
107
  while (($entry = $dir->read()) !== false) {
108
    if (is_dir("skins/{$entry}") && $entry[0] != '.') {
109
      $template_result['.']['skin_list'][] = array(
110
        'VALUE'    => $entry,
111
        'NAME'     => $entry,
112
        'SELECTED' => SN::$gc->theUser->getSkinName() == $entry,
113
      );
114
    }
115
  }
116
  $dir->close();
117
118
  $ignores = SN::$gc->ignores->getIgnores($user['id'], true);
119
  $template_result['.']['ignores'] = $ignores;
120
121
  foreach ($lang['opt_planet_sort_options'] as $key => &$value) {
122
    $template_result['.']['planet_sort_options'][] = array(
123
      'VALUE'    => $key,
124
      'NAME'     => $value,
125
      'SELECTED' => SN::$user_options[PLAYER_OPTION_PLANET_SORT] == $key,
126
    );
127
  }
128
129
  foreach ($lang['sys_gender_list'] as $key => $value) {
130
    $template_result['.']['gender_list'][] = array(
131
      'VALUE'    => $key,
132
      'NAME'     => $value,
133
      'SELECTED' => $user['gender'] == $key,
134
    );
135
  }
136
137
  $lang_list = lng_get_list();
138
  foreach ($lang_list as $lang_id => $lang_data) {
139
    $template_result['.']['languages'][] = array(
140
      'VALUE'    => $lang_id,
141
      'NAME'     => $lang_data['LANG_NAME_NATIVE'],
142
      'SELECTED' => $lang_id == $user['lang'],
143
    );
144
  }
145
146
147
  if (isset($lang['menu_customize_show_hide_button_state'])) {
148
    foreach ($lang['menu_customize_show_hide_button_state'] as $key => $value) {
149
      $template->assign_block_vars('menu_customize_show_hide_button_state', array(
150
        'ID'   => $key,
151
        'NAME' => $value,
152
      ));
153
    }
154
  }
155
156
  $str_date_format = "%3$02d %2$0s %1$04d {$lang['top_of_year']} %4$02d:%5$02d:%6$02d";
157
  $time_now_parsed = getdate($user['deltime']);
158
159
  sn_options_add_standard($template);
160
161
  $template->assign_vars([
162
    'USER_ID' => $user['id'],
163
164
    'ACCOUNT_NAME' => sys_safe_output(SN::$auth->account->account_name),
165
166
    'USER_AUTHLEVEL' => $user['authlevel'],
167
168
    'menu_customize_show_hide_button'     => SN::$user_options[PLAYER_OPTION_MENU_HIDE_SHOW_BUTTON],
169
    'PLAYER_OPTION_MENU_SHOW_ON_BUTTON'   => SN::$user_options[PLAYER_OPTION_MENU_SHOW_ON_BUTTON],
170
    'PLAYER_OPTION_MENU_HIDE_ON_BUTTON'   => SN::$user_options[PLAYER_OPTION_MENU_HIDE_ON_BUTTON],
171
    'PLAYER_OPTION_MENU_HIDE_ON_LEAVE'    => SN::$user_options[PLAYER_OPTION_MENU_HIDE_ON_LEAVE],
172
    'PLAYER_OPTION_MENU_UNPIN_ABSOLUTE'   => SN::$user_options[PLAYER_OPTION_MENU_UNPIN_ABSOLUTE],
173
    'PLAYER_OPTION_MENU_ITEMS_AS_BUTTONS' => SN::$user_options[PLAYER_OPTION_MENU_ITEMS_AS_BUTTONS],
174
    'PLAYER_OPTION_MENU_WHITE_TEXT'       => SN::$user_options[PLAYER_OPTION_MENU_WHITE_TEXT],
175
    'PLAYER_OPTION_MENU_OLD'              => SN::$user_options[PLAYER_OPTION_MENU_OLD],
176
177
    'PLAYER_OPTION_TUTORIAL_CURRENT_ID' => PLAYER_OPTION_TUTORIAL_CURRENT,
178
179
    'ADM_PROTECT_PLANETS' => $user['authlevel'] >= 3,
180
    'opt_usern_data'      => htmlspecialchars($user['username']),
181
    'opt_mail1_data'      => $user['email'],
182
    'opt_mail2_data'      => sys_safe_output(SN::$auth->account->account_email),
183
184
    'PLAYER_OPTION_PLANET_SORT_INVERSE'    => SN::$user_options[PLAYER_OPTION_PLANET_SORT_INVERSE],
185
    'PLAYER_OPTION_FLEET_SPY_DEFAULT'      => SN::$user_options[PLAYER_OPTION_FLEET_SPY_DEFAULT],
186
    'PLAYER_OPTION_TOOLTIP_DELAY'          => SN::$user_options[PLAYER_OPTION_TOOLTIP_DELAY],
187
    'PLAYER_OPTION_BUILD_AUTOCONVERT_HIDE' => SN::$user_options[PLAYER_OPTION_BUILD_AUTOCONVERT_HIDE],
188
189
    'opt_sskin_data' => ($user['design'] == 1) ? " checked='checked'" : '',
190
    'opt_noipc_data' => ($user['noipcheck'] == 1) ? " checked='checked'" : '',
191
    'deltime'        => $user['deltime'],
192
    'deltime_text'   => sprintf($str_date_format, $time_now_parsed['year'], $lang['months'][$time_now_parsed['mon']], $time_now_parsed['mday'],
193
      $time_now_parsed['hours'], $time_now_parsed['minutes'], $time_now_parsed['seconds']
194
    ),
195
196
    'opt_avatar' => $user['avatar'],
197
198
    'config_game_email_pm' => $config->game_email_pm,
199
200
    'user_settings_esp'        => SN::$user_options[PLAYER_OPTION_UNIVERSE_ICON_SPYING],
201
    'user_settings_mis'        => SN::$user_options[PLAYER_OPTION_UNIVERSE_ICON_MISSILE],
202
    'user_settings_wri'        => SN::$user_options[PLAYER_OPTION_UNIVERSE_ICON_PM],
203
    'user_settings_statistics' => SN::$user_options[PLAYER_OPTION_UNIVERSE_ICON_STATS],
204
    'user_settings_info'       => SN::$user_options[PLAYER_OPTION_UNIVERSE_ICON_PROFILE],
205
    'user_settings_bud'        => SN::$user_options[PLAYER_OPTION_UNIVERSE_ICON_BUDDY],
206
207
    'user_time_diff_forced' => playerTimeDiff::getTimeDiffForced(),
208
209
    'adm_pl_prot' => $user['admin_protection'],
210
211
    'user_birthday' => $user['user_birthday'],
212
    'GENDER'        => $user['gender'],
213
    'GENDER_TEXT'   => $lang['sys_gender_list'][$user['gender']],
214
    'FMT_DATE'      => $FMT_DATE,
215
    'JS_FMT_DATE'   => js_safe_string($FMT_DATE),
216
217
    'USER_VACATION_DISABLE' => $config->user_vacation_disable,
218
    'VACATION_NEXT'         => $user['vacation_next'],
219
    'VACATION_NEXT_TEXT'    => date(FMT_DATE_TIME, $user['vacation_next']),
220
    'VACATION_TIMEOUT'      => $user['vacation_next'] - SN_TIME_NOW > 0 ? $user['vacation_next'] - SN_TIME_NOW : 0,
221
    'SN_TIME_NOW'           => SN_TIME_NOW,
222
223
    'SERVER_SEND_EMAIL' => $config->game_email_pm,
224
225
    'SERVER_NAME_CHANGE'         => $config->game_user_changename != SERVER_PLAYER_NAME_CHANGE_NONE,
226
    'SERVER_NAME_CHANGE_PAY'     => $config->game_user_changename == SERVER_PLAYER_NAME_CHANGE_PAY,
227
    'SERVER_NAME_CHANGE_ENABLED' => $config->game_user_changename == SERVER_PLAYER_NAME_CHANGE_FREE || ($config->game_user_changename == SERVER_PLAYER_NAME_CHANGE_PAY && mrc_get_level($user, $planetrow, RES_DARK_MATTER) >= $config->game_user_changename_cost),
228
229
    'DARK_MATTER' => prettyNumberStyledCompare($config->game_user_changename_cost, mrc_get_level($user, $planetrow, RES_DARK_MATTER)),
0 ignored issues
show
Bug introduced by
It seems like mrc_get_level($user, $planetrow, RES_DARK_MATTER) can also be of type boolean; however, parameter $compareTo of prettyNumberStyledCompare() does only seem to accept double|integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

229
    'DARK_MATTER' => prettyNumberStyledCompare($config->game_user_changename_cost, /** @scrutinizer ignore-type */ mrc_get_level($user, $planetrow, RES_DARK_MATTER)),
Loading history...
230
231
    'GROUP_DESIGN_BLOCK_TUTORIAL'      => GROUP_DESIGN_BLOCK_TUTORIAL,
232
    'GROUP_DESIGN_BLOCK_FLEET_COMPOSE' => GROUP_DESIGN_BLOCK_FLEET_COMPOSE,
233
    'GROUP_DESIGN_BLOCK_UNIVERSE'      => GROUP_DESIGN_BLOCK_UNIVERSE,
234
    'GROUP_DESIGN_BLOCK_NAVBAR'        => GROUP_DESIGN_BLOCK_NAVBAR,
235
    'GROUP_DESIGN_BLOCK_RESOURCEBAR'   => GROUP_DESIGN_BLOCK_RESOURCEBAR,
236
    'GROUP_DESIGN_BLOCK_PLANET_SORT'   => GROUP_DESIGN_BLOCK_PLANET_SORT,
237
    'GROUP_DESIGN_BLOCK_COMMON_ONE'    => GROUP_DESIGN_BLOCK_COMMON_ONE,
238
    'GROUP_DESIGN_BLOCK_COMMON_TWO'    => GROUP_DESIGN_BLOCK_COMMON_TWO,
239
240
    'PAGE_HEADER' => $lang['opt_header'],
241
  ]);
242
243
  foreach ($user_option_list as $option_group_id => $option_group) {
244
    if ($option_group_id == OPT_MESSAGE) {
245
      foreach ($sn_message_class_list as $message_class_id => $message_class_data) {
246
        if ($message_class_data['switchable'] || ($message_class_data['email'] && $config->game_email_pm)) {
247
          $option_name = $message_class_data['name'];
248
249
          $template->assign_block_vars("options_{$option_group_id}", array(
250
            'NAME'  => $message_class_data['name'],
251
            'TEXT'  => $lang['msg_class'][$message_class_id], // $lang['opt_custom'][$option_name],
252
            'PM'    => $message_class_data['switchable'] ? $user["opt_{$option_name}"] : -1,
253
            'EMAIL' => $message_class_data['email'] && $config->game_email_pm ? $user["opt_email_{$option_name}"] : -1,
254
          ));
255
        }
256
      }
257
    } else {
258
      foreach ($option_group as $option_name => $option_value) {
259
        if (array_key_exists($option_name, $user_option_types)) {
260
          $option_type = $user_option_types[$option_name];
261
        } else {
262
          $option_type = 'switch';
263
        }
264
265
        $template->assign_block_vars("options_{$option_group_id}", array(
266
          'NAME'  => $option_name,
267
          'TYPE'  => $option_type,
268
          'TEXT'  => $lang['opt_custom'][$option_name],
269
          'HINT'  => $lang['opt_custom']["{$option_name}_hint"],
270
          'VALUE' => $user[$option_name],
271
        ));
272
      }
273
    }
274
  }
275
276
//  var_dump($template_result['.']['result']);
277
//  var_dump($template->_tpldata);
278
//
279
  return $template;
280
}
281
282
//-------------------------------
283
284
/**
285
 * @param $user
286
 *
287
 * @return array
288
 */
289
function sn_options_gender($user) {
290
  $gender = sys_get_param_int('gender', $user['gender']);
291
  !isset(SN::$lang['sys_gender_list'][$gender]) ? $gender = $user['gender'] : false;
292
  $user['gender'] = $user['gender'] == GENDER_UNKNOWN ? $gender : $user['gender'];
293
294
  return $user;
295
}
296
297
/**
298
 * @param array $user
299
 *
300
 * @return array
301
 */
302
function sn_options_change_birthday($user) {
303
  $user_birthday = sys_get_param_str_unsafe('user_birthday');
304
  $FMT_DATE = preg_replace(array('/d/', '/m/', '/Y/'), array('DD', 'MM', 'YYYY'), FMT_DATE);
305
306
  if ($user['birthday'] || empty($user_birthday) || $user_birthday == $FMT_DATE) {
307
    return $user;
308
  }
309
310
  try {
311
    // Some black magic to parse any valid date format - those that contains all three "d", "m" and "Y" and any of the delimeters "\", "/", ".", "-"
312
    $pos['d'] = strpos(FMT_DATE, 'd');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$pos was never initialized. Although not strictly required by PHP, it is generally a good practice to add $pos = array(); before regardless.
Loading history...
313
    $pos['m'] = strpos(FMT_DATE, 'm');
314
    $pos['Y'] = strpos(FMT_DATE, 'Y');
315
    asort($pos);
316
    $i = 0;
317
    foreach ($pos as &$position) {
318
      $position = ++$i;
319
    }
320
321
    $regexp = "/" . preg_replace(array('/\\\\/', '/\//', '/\./', '/\-/', '/d/', '/m/', '/Y/'), array('\\\\\\', '\/', '\.', '\-', '(\d?\d)', '(\d?\d)', '(\d{4})'), FMT_DATE) . "/";
322
    if (!preg_match($regexp, $user_birthday, $match)) {
323
      throw new Exception();
324
    }
325
326
    if (!checkdate($match[$pos['m']], $match[$pos['d']], $match[$pos['Y']])) {
327
      throw new Exception();
328
    }
329
330
    $user_birthday_new_unescaped = "{$match[$pos['Y']]}-{$match[$pos['m']]}-{$match[$pos['d']]}";
331
    $user['user_birthday'] = $user_birthday_new_unescaped;
332
    // EOF black magic! Now we have valid SQL date in $user['user_birthday'] - independent of date format
333
334
    $year = date('Y', SN_TIME_NOW);
335
    if (mktime(0, 0, 0, $match[$pos['m']], $match[$pos['d']], $year) > SN_TIME_NOW) {
0 ignored issues
show
Bug introduced by
$year of type string is incompatible with the type integer expected by parameter $year of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

335
    if (mktime(0, 0, 0, $match[$pos['m']], $match[$pos['d']], /** @scrutinizer ignore-type */ $year) > SN_TIME_NOW) {
Loading history...
336
      $year--;
337
    }
338
    $user['user_birthday_celebrated'] = "{$year}-{$match[$pos['m']]}-{$match[$pos['d']]}";
339
  } catch (exception $e) {
340
    $user['user_birthday'] = null;
341
    $user['user_birthday_celebrated'] = null;
342
  }
343
344
  return $user;
345
}
346
347
/**
348
 * @return array
349
 */
350
function sn_options_change_password() {
351
  $result = [];
352
  if (!($new_password = sys_get_param('newpass1'))) {
353
    return $result;
354
  }
355
356
  try {
357
    if ($new_password != sys_get_param('newpass2')) {
358
      throw new Exception('opt_err_pass_unmatched', ERR_WARNING);
359
    }
360
361
    if (!SN::$auth->password_change(sys_get_param('db_password'), $new_password)) {
362
      throw new Exception('opt_err_pass_wrong', ERR_WARNING);
363
    }
364
365
    throw new Exception('opt_msg_pass_changed', ERR_NONE);
366
  } catch (Exception $e) {
367
    $result = [
368
      'STATUS'  => in_array($e->getCode(), [ERR_NONE, ERR_WARNING, ERR_ERROR]) ? $e->getCode() : ERR_ERROR,
369
      'MESSAGE' => SN::$lang[$e->getMessage()],
370
    ];
371
  }
372
373
  return $result;
374
}
375
376
function sn_options_player_standard() {
377
  $player_options = sys_get_param('options');
378
  if (empty($player_options)) {
379
    return;
380
  }
381
382
  if ($player_options[PLAYER_OPTION_TUTORIAL_CURRENT]) {
383
    $player_options[PLAYER_OPTION_TUTORIAL_CURRENT] = SN::$config->tutorial_first_item;
384
    $player_options[PLAYER_OPTION_TUTORIAL_FINISHED] = 0;
385
  } else {
386
    unset($player_options[PLAYER_OPTION_TUTORIAL_CURRENT]);
387
  }
388
389
  array_walk($player_options, function (&$value) {
0 ignored issues
show
Bug introduced by
It seems like $player_options can also be of type string; however, parameter $array of array_walk() does only seem to accept array|object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

389
  array_walk(/** @scrutinizer ignore-type */ $player_options, function (&$value) {
Loading history...
390
    // TODO - Когда будет больше параметров - сделать больше проверок
391
    $value = intval($value);
392
  });
393
  SN::$user_options->offsetSet($player_options);
394
}
395
396
/**
397
 * @param array $user
398
 *
399
 * @return array
400
 */
401
function sn_options_change_username($user) {
402
  $config = SN::$config;
403
  $lang = SN::$lang;
404
405
  $result = [];
406
407
  $username = substr(sys_get_param_str_unsafe('username'), 0, 32);
408
  if (
409
    empty($username)
410
    || $user['username'] == $username
411
    || $config->game_user_changename == SERVER_PLAYER_NAME_CHANGE_NONE
412
    || !sys_get_param_int('username_confirm')
413
    || strpbrk($username, LOGIN_REGISTER_CHARACTERS_PROHIBITED)
414
  ) {
415
    return [$user, $result];
416
  }
417
418
  // проверка на корректность
419
  db_mysql::db_transaction_start();
420
  $username_safe = SN::$db->db_escape($username);
421
  /** @noinspection SqlResolve */
422
  $name_check = doquery("SELECT * FROM `{{player_name_history}}` WHERE `player_name` LIKE \"{$username_safe}\" LIMIT 1 FOR UPDATE;", true);
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

422
  $name_check = /** @scrutinizer ignore-deprecated */ doquery("SELECT * FROM `{{player_name_history}}` WHERE `player_name` LIKE \"{$username_safe}\" LIMIT 1 FOR UPDATE;", true);
Loading history...
423
  if (empty($name_check['player_id']) || $name_check['player_id'] == $user['id']) {
424
    $user = db_user_by_id($user['id'], true);
0 ignored issues
show
Deprecated Code introduced by
The function db_user_by_id() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

424
    $user = /** @scrutinizer ignore-deprecated */ db_user_by_id($user['id'], true);
Loading history...
425
    switch ($config->game_user_changename) {
426
      /** @noinspection PhpMissingBreakStatementInspection */
427
      case SERVER_PLAYER_NAME_CHANGE_PAY:
428
        if (mrc_get_level($user, [], RES_DARK_MATTER) < $config->game_user_changename_cost) {
429
          $result[] = [
430
            'STATUS'  => ERR_ERROR,
431
            'MESSAGE' => $lang['opt_msg_name_change_err_no_dm'],
432
          ];
433
          break;
434
        }
435
        rpg_points_change(
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
436
          $user['id'],
437
          RPG_NAME_CHANGE,
438
          -$config->game_user_changename_cost,
439
          vsprintf('Пользователь ID %1$d сменил имя с "%2$s" на "%3$s"', [$user['id'], $user['username'], $username,])
440
        );
441
442
      case SERVER_PLAYER_NAME_CHANGE_FREE:
443
        db_user_set_by_id($user['id'], "`username` = '{$username_safe}'");
0 ignored issues
show
Deprecated Code introduced by
The function db_user_set_by_id() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

443
        /** @scrutinizer ignore-deprecated */ db_user_set_by_id($user['id'], "`username` = '{$username_safe}'");
Loading history...
444
        /** @noinspection SqlResolve */
445
        doquery("REPLACE INTO `{{player_name_history}}` SET `player_id` = {$user['id']}, `player_name` = '{$username_safe}'");
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

445
        /** @scrutinizer ignore-deprecated */ doquery("REPLACE INTO `{{player_name_history}}` SET `player_id` = {$user['id']}, `player_name` = '{$username_safe}'");
Loading history...
446
        // TODO: Change cookie to not force user relogin
447
        // sn_setcookie(SN_COOKIE, '', time() - PERIOD_WEEK, SN_ROOT_RELATIVE);
448
        $result[] = [
449
          'STATUS'  => ERR_NONE,
450
          'MESSAGE' => $lang['opt_msg_name_changed']
451
        ];
452
        $user['username'] = $username;
453
      break;
454
    }
455
  } else {
456
    $result[] = [
457
      'STATUS'  => ERR_ERROR,
458
      'MESSAGE' => $lang['opt_msg_name_change_err_used_name'],
459
    ];
460
  }
461
  db_mysql::db_transaction_commit();
462
463
  return [$user, $result];
464
}
465
466
/**
467
 * Set old options
468
 *
469
 * @param array $user
470
 *
471
 * @return array
472
 * @deprecated
473
 */
474
function sn_options_deprecated($user) {
475
  global $user_option_list;
476
477
  foreach ($user_option_list as $option_group_id => $option_group) {
478
    foreach ($option_group as $option_name => $option_value) {
479
      if ($user[$option_name] !== null) {
480
        $user[$option_name] = sys_get_param_str($option_name);
481
      } else {
482
        $user[$option_name] = $option_value;
483
      }
484
    }
485
  }
486
487
  sys_user_options_pack($user);
488
489
  return $user;
490
}
491
492
/**
493
 * @param array $user
494
 *
495
 * @return array
496
 */
497
function sn_options_admin_protection($user) {
498
  if ($user['authlevel'] <= AUTH_LEVEL_REGISTERED) {
499
    return $user;
500
  }
501
502
  $planet_protection = sys_get_param_int('adm_pl_prot') ? $user['authlevel'] : 0;
503
  DBStaticPlanet::db_planet_set_by_owner($user['id'], "`id_level` = '{$planet_protection}'");
504
  db_user_set_by_id($user['id'], "`admin_protection` = '{$planet_protection}'");
0 ignored issues
show
Deprecated Code introduced by
The function db_user_set_by_id() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

504
  /** @scrutinizer ignore-deprecated */ db_user_set_by_id($user['id'], "`admin_protection` = '{$planet_protection}'");
Loading history...
505
  $user['admin_protection'] = $planet_protection;
506
507
  return $user;
508
}
509
510
/**
511
 * @param array $user
512
 *
513
 * @return array
514
 */
515
function sn_options_vacation($user) {
516
  $config = SN::$config;
517
  $lang = SN::$lang;
518
519
  if (!sys_get_param_int('vacation') || $config->user_vacation_disable) {
520
    return $user;
521
  }
522
523
  db_mysql::db_transaction_start();
524
  if ($user['authlevel'] < AUTH_LEVEL_ADMINISTRATOR) {
525
    if ($user['vacation_next'] > SN_TIME_NOW) {
526
      SnTemplate::messageBox($lang['opt_vacation_err_timeout'], $lang['Error'], 'index.php?page=options', 5);
527
      die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
528
    }
529
530
    if (DbFleetStatic::fleet_count_flying($user['id'])) {
531
      SnTemplate::messageBox($lang['opt_vacation_err_your_fleet'], $lang['Error'], 'index.php?page=options', 5);
532
      die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
533
    }
534
535
    $que = que_get($user['id'], false);
536
    if (!empty($que)) {
537
      SnTemplate::messageBox($lang['opt_vacation_err_que'], $lang['Error'], 'index.php?page=options', 5);
538
      die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
539
    }
540
541
    $query = SN::db_get_record_list(LOC_PLANET, "`id_owner` = {$user['id']}");
542
    foreach ($query as $planet) {
543
      DBStaticPlanet::db_planet_set_by_id($planet['id'],
544
        "last_update = " . SN_TIME_NOW . ", energy_used = '0', energy_max = '0',
545
        metal_perhour = '{$config->metal_basic_income}', crystal_perhour = '{$config->crystal_basic_income}', deuterium_perhour = '{$config->deuterium_basic_income}',
546
        metal_mine_porcent = '0', crystal_mine_porcent = '0', deuterium_sintetizer_porcent = '0', solar_plant_porcent = '0',
547
        fusion_plant_porcent = '0', solar_satelit_porcent = '0', ship_sattelite_sloth_porcent = 0"
548
      );
549
    }
550
    $user['vacation'] = SN_TIME_NOW + $config->player_vacation_time;
551
  } else {
552
    $user['vacation'] = SN_TIME_NOW;
553
  }
554
  db_mysql::db_transaction_commit();
555
556
  return $user;
557
}
558
559
560
/**
561
 * @param template $template
562
 * @param string   $blockName
563
 * @param int      $blockId
564
 * @param int[]    $optionsNavBar
565
 * @param array    $options
566
 */
567
function sn_options_render_block($template, $blockName, $blockId, $optionsNavBar, $options = []) {
568
  $template->assign_block_vars('player_options', [
569
    'ID'   => $blockId,
570
    'NAME' => $blockName,
571
  ]);
572
573
  foreach ($optionsNavBar as $optionId) {
574
    $template->assign_block_vars('player_options.option', [
575
      'ID'         => $optionId,
576
      'VALUE'      => SN::$user_options[$optionId],
577
      'NAME'       => SN::$lang['opt_player_options'][$optionId],
578
      'ALWAYS_OFF' => !empty($options[$optionId]['always_off']),
579
      'CLASS'      => !empty($options[$optionId]['class']) ? $options[$optionId]['class'] : 'cell',
580
    ]);
581
  }
582
}
583
584
/**
585
 * @param $template
586
 */
587
function sn_options_add_standard($template) {
588
  sn_options_render_block($template, '', 5, [
589
  ]);
590
591
592
  // 8
593
  sn_options_render_block($template, '', GROUP_DESIGN_BLOCK_COMMON_TWO,
594
    [
595
      PLAYER_OPTION_SOUND_ENABLED,
596
      PLAYER_OPTION_ANIMATION_DISABLED,
597
      PLAYER_OPTION_PROGRESS_BARS_DISABLED,
598
    ],
599
    [
600
      PLAYER_OPTION_SOUND_ENABLED          => ['class' => 'header'],
601
      PLAYER_OPTION_ANIMATION_DISABLED     => ['class' => 'header'],
602
      PLAYER_OPTION_PROGRESS_BARS_DISABLED => ['class' => 'header'],
603
    ]
604
  );
605
  // 7
606
  sn_options_render_block($template, '', GROUP_DESIGN_BLOCK_COMMON_ONE, [
607
    PLAYER_OPTION_BUILD_AUTOCONVERT_HIDE,
608
    PLAYER_OPTION_DESIGN_DISABLE_BORDERS,
609
    PLAYER_OPTION_TECH_TREE_TABLE,
610
  ]);
611
  // 6
612
  sn_options_render_block($template, '', GROUP_DESIGN_BLOCK_PLANET_SORT, [
613
    PLAYER_OPTION_PLANET_SORT_INVERSE,
614
  ]);
615
  // 4
616
  sn_options_render_block($template, SN::$lang['opt_navbar_resourcebar_description'], GROUP_DESIGN_BLOCK_RESOURCEBAR, [
617
    PLAYER_OPTION_NAVBAR_PLANET_VERTICAL,
618
    PLAYER_OPTION_NAVBAR_PLANET_DISABLE_STORAGE,
619
    PLAYER_OPTION_NAVBAR_PLANET_OLD,
620
  ]);
621
  // 3
622
  sn_options_render_block($template, SN::$lang['opt_navbar_buttons_title'], GROUP_DESIGN_BLOCK_NAVBAR, [
623
    PLAYER_OPTION_NAVBAR_RESEARCH_WIDE,
624
    PLAYER_OPTION_NAVBAR_DISABLE_RESEARCH,
625
    PLAYER_OPTION_NAVBAR_DISABLE_PLANET,
626
    PLAYER_OPTION_NAVBAR_DISABLE_HANGAR,
627
    PLAYER_OPTION_NAVBAR_DISABLE_DEFENSE,
628
    PLAYER_OPTION_NAVBAR_DISABLE_EXPEDITIONS,
629
    PLAYER_OPTION_NAVBAR_DISABLE_FLYING_FLEETS,
630
    PLAYER_OPTION_NAVBAR_DISABLE_QUESTS,
631
    PLAYER_OPTION_NAVBAR_DISABLE_META_MATTER,
632
  ]);
633
  // 2
634
  sn_options_render_block($template, SN::$lang['galaxyvision_options'], GROUP_DESIGN_BLOCK_UNIVERSE, [
635
    PLAYER_OPTION_UNIVERSE_OLD,
636
    PLAYER_OPTION_UNIVERSE_DISABLE_COLONIZE,
637
  ]);
638
  // 1
639
  sn_options_render_block($template, SN::$lang['option_fleet_send'], GROUP_DESIGN_BLOCK_FLEET_COMPOSE, [
640
    PLAYER_OPTION_FLEET_SHIP_SELECT_OLD,
641
    PLAYER_OPTION_FLEET_SHIP_HIDE_CONSUMPTION,
642
    PLAYER_OPTION_FLEET_SHIP_HIDE_SPEED,
643
    PLAYER_OPTION_FLEET_SHIP_HIDE_CAPACITY,
644
  ]);
645
  // 0
646
  sn_options_render_block($template, SN::$lang['opt_tutorial'], GROUP_DESIGN_BLOCK_TUTORIAL, [
647
    PLAYER_OPTION_TUTORIAL_DISABLED,
648
    // PLAYER_OPTION_TUTORIAL_WINDOWED,
649
    PLAYER_OPTION_TUTORIAL_CURRENT,
650
  ], [PLAYER_OPTION_TUTORIAL_CURRENT => ['always_off' => true]]);
651
}
652