Completed
Push — trunk ( b5d7ea...8dcff2 )
by SuperNova.WS
08:14
created

PageAdminUserView   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 302
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 302
rs 10
ccs 0
cts 90
cp 0
wmc 22

4 Methods

Rating   Name   Duplication   Size   Complexity  
C viewStatic() 0 63 10
A renderExtra() 0 10 3
A userBlockAssign() 0 15 3
B modelStatic() 0 21 6
1
<?php
2
/**
3
 * Created by Gorlum 05.03.2018 12:57
4
 */
5
6
namespace Pages\Deprecated;
7
8
use Account;
9
use Modules\sn_module;
10
use SN;
11
use template;
12
13
class PageAdminUserView extends PageDeprecated {
14
  private static $formats = [
15
    'sys_time_human_system'     => [
16
      'register_time',
17
      'onlinetime',
18
      'ally_register_time',
19
      'news_lastread',
20
      'banaday',
21
      'vacation',
22
      'vacation_next',
23
      'deltime',
24
      'que_processed',
25
    ],
26
    'prettyNumberStyledDefault' => [
27
      'account_metamatter',
28
      'account_metamatter_total',
29
30
      'metal',
31
      'crystal',
32
      'deuterium',
33
      'dark_matter',
34
      'dark_matter_total',
35
      'metamatter',
36
      'metamatter_total',
37
38
      'player_rpg_explore_xp',
39
      'player_rpg_explore_level',
40
      'lvl_minier',
41
      'xpminier',
42
      'player_rpg_tech_xp',
43
      'player_rpg_tech_level',
44
      'lvl_raid',
45
      'xpraid',
46
      'raids',
47
      'raidsloose',
48
      'raidswin',
49
      'total_rank',
50
      'total_points',
51
52
      // Message counts
53
      'new_message',
54
      'mnl_alliance',
55
      'mnl_joueur',
56
      'mnl_attaque',
57
      'mnl_spy',
58
      'mnl_exploit',
59
      'mnl_transport',
60
      'mnl_expedition',
61
      'mnl_buildlist',
62
      'msg_admin',
63
    ],
64
  ];
65
66
  private static $blocks = [
67
    'Аккаунт'                       => [
68
//      'account_id',
69
      'account_name',
70
      'account_password',
71
      'account_salt',
72
      'account_email',
73
      'account_email_verified',
74
      'account_register_time',
75
      'account_language',
76
      'account_metamatter',
77
      'account_metamatter_total',
78
    ],
79
    'Основная информация'           => [
80
      'id',
81
      'username',
82
      'password',
83
      'salt',
84
      'user_last_browser_id',
85
      'browser_user_agent',
86
      'user_lastip',
87
      'user_last_proxy',
88
      'register_time',
89
      'onlinetime',
90
      'authlevel',
91
      'admin_protection',
92
      'id_planet',
93
      'galaxy',
94
      'system',
95
      'planet',
96
      'current_planet',
97
      'server_name',
98
    ],
99
    'Профиль пользователя'          => [
100
      'email',
101
      'email_2',
102
      'gender',
103
      'avatar',
104
      'sign',
105
      'user_birthday',
106
    ],
107
    'Ресурсы'                       => [
108
      'metal',
109
      'crystal',
110
      'deuterium',
111
      'dark_matter',
112
      'dark_matter_total',
113
      'metamatter',
114
      'metamatter_total',
115
      'player_race',
116
    ],
117
    'Альянс'                        => [
118
      'ally_id',
119
      'ally_tag',
120
      'ally_name',
121
      'ally_register_time',
122
      'ally_rank_id',
123
      'user_as_ally',
124
    ],
125
    'Очки'                          => [
126
      'player_rpg_explore_xp',
127
      'player_rpg_explore_level',
128
      'lvl_minier',
129
      'xpminier',
130
      'player_rpg_tech_xp',
131
      'player_rpg_tech_level',
132
      'lvl_raid',
133
      'xpraid',
134
      'raids',
135
      'raidsloose',
136
      'raidswin',
137
      'total_rank',
138
      'total_points',
139
    ],
140
    'Блокировка, отпуск и удаление' => [
141
      'immortal',
142
      'bana',
143
      'banaday',
144
      'vacation',
145
      'vacation_next',
146
      'deltime',
147
    ],
148
    'Основные настройки интерфейса' => [
149
      'lang',
150
      'template',
151
      'skin',
152
      'design',
153
    ],
154
    'Новости и сообщения'           => [
155
      'news_lastread',
156
      'new_message',
157
      'mnl_alliance',
158
      'mnl_joueur',
159
      'mnl_attaque',
160
      'mnl_spy',
161
      'mnl_exploit',
162
      'mnl_transport',
163
      'mnl_expedition',
164
      'mnl_buildlist',
165
      'msg_admin',
166
    ],
167
    'Прочие настройки'              => [
168
      'noipcheck',
169
      'options',
170
      'user_time_measured',
171
    ],
172
    'Системные поля'                => [
173
      'que_processed',
174
      'user_birthday_celebrated',
175
      'user_bot',
176
      'parent_account_id',
177
      'parent_account_global',
178
    ],
179
  ];
180
181
  private static $extras = [
182
    'account_password' => [
183
      'TYPE' => 'password',
184
    ],
185
  ];
186
187
  /**
188
   * @param null|template $template
189
   *
190
   * @return null|template
191
   */
192
  public static function modelStatic($template = null) {
193
    global $user;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
194
195
    $user_id = sys_get_param_id('uid');
196
    if (!($user_row = db_user_by_id($user_id))) {
0 ignored issues
show
Bug introduced by
It seems like $user_id can also be of type string; however, parameter $user_id_unsafe of db_user_by_id() does only seem to accept integer|array, 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

196
    if (!($user_row = db_user_by_id(/** @scrutinizer ignore-type */ $user_id))) {
Loading history...
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

196
    if (!($user_row = /** @scrutinizer ignore-deprecated */ db_user_by_id($user_id))) {
Loading history...
197
      return $template;
198
    }
199
200
    if (empty($user['authlevel']) || $user['authlevel'] < $user_row['authlevel']) {
201
      messageBoxAdmin(SN::$lang['admin_title_access_denied']);
202
    }
203
204
205
    if (!empty(sys_get_param('account_password_change')) && ($password = trim(sys_get_param('account_password')))) {
0 ignored issues
show
Bug introduced by
It seems like sys_get_param('account_password') can also be of type array; however, parameter $str of trim() does only seem to accept string, 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

205
    if (!empty(sys_get_param('account_password_change')) && ($password = trim(/** @scrutinizer ignore-type */ sys_get_param('account_password')))) {
Loading history...
206
      $account = new Account();
207
      $account->dbGetByPlayerId($user_id);
208
209
      $account->db_set_password($password, '');
210
    }
211
212
    return $template;
213
  }
214
215
  public static function viewStatic($template = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $template is not used and could be removed. ( Ignorable by Annotation )

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

215
  public static function viewStatic(/** @scrutinizer ignore-unused */ $template = null) {

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

Loading history...
216
    global $user;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
217
218
    define('IN_ADMIN', true);
219
220
    messageBoxAdminAccessDenied(AUTH_LEVEL_ADMINISTRATOR);
221
222
    $user_id = sys_get_param_id('uid');
223
    if (!($user_row = db_user_by_id($user_id))) {
0 ignored issues
show
Bug introduced by
It seems like $user_id can also be of type string; however, parameter $user_id_unsafe of db_user_by_id() does only seem to accept integer|array, 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

223
    if (!($user_row = db_user_by_id(/** @scrutinizer ignore-type */ $user_id))) {
Loading history...
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

223
    if (!($user_row = /** @scrutinizer ignore-deprecated */ db_user_by_id($user_id))) {
Loading history...
224
      messageBoxAdmin(sprintf(SN::$lang['adm_dm_user_none'], $user_id));
225
    }
226
227
    if (empty($user['authlevel']) || $user['authlevel'] <= $user_row['authlevel']) {
228
      messageBoxAdmin(SN::$lang['admin_title_access_denied']);
229
    }
230
231
    $account = new \Account();
232
    $account->dbGetByPlayerId($user_id);
233
    foreach ([
234
      'account_id',
235
      'account_name',
236
      'account_password',
237
      'account_salt',
238
      'account_email',
239
      'account_email_verified',
240
      'account_register_time',
241
      'account_language',
242
      'account_metamatter',
243
      'account_metamatter_total',
244
    ] as $accountField) {
245
      $user_row[$accountField] = $account->$accountField;
246
    }
247
248
    if (!empty($user_row['user_last_browser_id'])) {
249
      $temp = doquery("SELECT `browser_user_agent` FROM `{{security_browser}}` WHERE `browser_id` = {$user_row['user_last_browser_id']}", true);
250
      $user_row['browser_user_agent'] = $temp['browser_user_agent'];
251
    }
252
253
    foreach (self::$formats as $callable => $field_list) {
254
      foreach ($field_list as $field_name) {
255
        $user_row[$field_name] = call_user_func($callable, $user_row[$field_name]);
256
      }
257
    }
258
259
    $template = gettemplate('admin/admin_user', true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type null|template expected by parameter $template of gettemplate(). ( Ignorable by Annotation )

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

259
    $template = gettemplate('admin/admin_user', /** @scrutinizer ignore-type */ true);
Loading history...
260
261
    $result = [
262
      'PAGE_HEADER' => "[{$user_row['id']}] {$user_row['username']}",
263
      'USER_ID'     => $user_row['id'],
264
    ];
265
    $exclude = $user_row;
266
    foreach (self::$blocks as $title => $fields) {
267
//      $template->assign_recursive(['.' => ['block' => [self::userBlockAssign($exclude, $title, $fields)]]]);
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...
268
      $result['.']['block'][] = self::userBlockAssign($exclude, $title, $fields);
269
    }
270
271
    if (!empty($exclude)) {
272
      $result['.']['block'][] = self::userBlockAssign($exclude, '!!! НЕИЗВЕСТНЫЕ ПАРАМЕТРЫ !!!', array_keys($exclude));
273
    }
274
275
    $template->assign_recursive($result);
276
277
    return $template;
278
  }
279
280
  /**
281
   * @param array      $exclude
282
   * @param int|string $title
283
   * @param array      $fields
284
   *
285
   * @return array
286
   */
287
  private static function userBlockAssign(&$exclude, $title, $fields) {
288
    $block = [
289
      'TITLE' => $title,
290
    ];
291
    foreach ($fields as $field) {
292
      $fieldBlock = [
293
        'NAME'  => $field,
294
        'VALUE' => isset($exclude[$field]) ? $exclude[$field] : 'N/A',
295
      ];
296
      $fieldBlock += self::renderExtra($field);
297
      $block['.']['field'][] = $fieldBlock;
298
      unset($exclude[$field]);
299
    }
300
301
    return $block;
302
303
  }
304
305
  private static function renderExtra($field) {
306
    $result = [];
307
308
    if (isset(self::$extras[$field])) {
309
      foreach (self::$extras[$field] as $extraName => $extraContent) {
310
        $result[$extraName] = $extraContent;
311
      }
312
    }
313
314
    return $result;
315
  }
316
317
}
318