Issues (1369)

classes/Player/playerTimeDiff.php (3 issues)

1
<?php
2
3
namespace Player;
4
/**
5
 * User: Gorlum
6
 * Date: 14.10.2015
7
 * Time: 0:28
8
 */
9
class playerTimeDiff {
10
  const TIME_DIFF = 0; // Чистая разница в ходе часов в секундах
11
  const TIME_DIFF_UTC_OFFSET = 1; // Разница между часовыми поясами в секундах
12
  const TIME_DIFF_FORCED = 2;
13
  const TIME_DIFF_MEASURE_TIME = 3; // Время когда происходил замер
14
15
  /**
16
   * Разница в ходе часов в секундах. Т.е. разница между GMT-временами браузера и сервера
17
   * @var int
18
   */
19
  public $gmt_diff = 0;
20
  /**
21
   * Разница в секундах между часовыми поясами браузера и сервера
22
   * @var int
23
   */
24
  public $zone_offset = 0;
25
  /**
26
   * Форсированный пересчёт времени
27
   *
28
   * @var int
29
   */
30
  public $force_measure = 0;
31
  /**
32
   * Метка времени прошлой синхронизации часов
33
   *
34
   * @var string
35
   */
36
  public $last_measure_time = '2000-01-01';
37
38
39
  static protected function user_time_diff_probe() {
40
    // Определяем время в браузере
41
    $client_time = strtotime(sys_get_param('client_gmt')); // Попытка определить по GMT-времени браузера. В нём будет часовой пояс (GMT), поэтому время будет автоматически преобразовано в часовой пояс сервера
0 ignored issues
show
It seems like sys_get_param('client_gmt') can also be of type array; however, parameter $datetime of strtotime() 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

41
    $client_time = strtotime(/** @scrutinizer ignore-type */ sys_get_param('client_gmt')); // Попытка определить по GMT-времени браузера. В нём будет часовой пояс (GMT), поэтому время будет автоматически преобразовано в часовой пояс сервера
Loading history...
42
    !$client_time ? $client_time = round(sys_get_param_float('timeBrowser') / 1000) : false; // Попытка определить по Date.valueOf() - миллисекунды с начала эпохи UNIX_TIME
43
    !$client_time ? $client_time = SN_TIME_NOW : false; // Если все попытки провалились - тупо берем время сервера
44
45
    $result = array(
46
      self::TIME_DIFF              => $client_time - SN_TIME_NOW,
47
      self::TIME_DIFF_UTC_OFFSET   => ($browser_utc_offset = sys_get_param_int('utc_offset')) ? $browser_utc_offset - date('Z') : 0,
48
      self::TIME_DIFF_FORCED       => sys_get_param_int('PLAYER_OPTION_TIME_DIFF_FORCED'),
49
      self::TIME_DIFF_MEASURE_TIME => SN_TIME_SQL,
50
    );
51
52
    return $result;
53
  }
54
55
  static protected function user_time_diff_set($user_time_diff) {
56
    // Переопределяем массив, что бы элементы были в правильном порядке
57
    !is_array($user_time_diff) ? $user_time_diff = [] : false;
58
    $user_time_diff = self::sortDiffArray($user_time_diff);
59
    $user_time_diff[self::TIME_DIFF_MEASURE_TIME] = SN_TIME_SQL;
60
61
    $user_time_diff_str = implode(';', $user_time_diff);
62
    sn_setcookie(SN_COOKIE_T, $user_time_diff_str, SN_TIME_NOW + PERIOD_MONTH);
63
  }
64
65
  static protected function user_time_diff_get() {
66
    $user_time_diff = !empty($_COOKIE[SN_COOKIE_T]) ? explode(';', $_COOKIE[SN_COOKIE_T]) : null;
67
    !is_array($user_time_diff) ? $user_time_diff = [] : false;
68
    $user_time_diff = self::sortDiffArray($user_time_diff);
0 ignored issues
show
It seems like $user_time_diff can also be of type null; however, parameter $user_time_diff of Player\playerTimeDiff::sortDiffArray() does only seem to accept 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

68
    $user_time_diff = self::sortDiffArray(/** @scrutinizer ignore-type */ $user_time_diff);
Loading history...
69
70
    return $user_time_diff;
71
  }
72
73
  static public function sn_options_timediff($timeDiff, $force, $clear) {
74
    $user_time_diff = playerTimeDiff::user_time_diff_get();
75
    if ($force) {
76
      playerTimeDiff::user_time_diff_set(array(
77
        self::TIME_DIFF              => $timeDiff,
78
        self::TIME_DIFF_UTC_OFFSET   => 0,
79
        self::TIME_DIFF_FORCED       => 1,
80
        self::TIME_DIFF_MEASURE_TIME => SN_TIME_SQL,
81
      ));
82
    } elseif ($clear || $user_time_diff[self::TIME_DIFF_FORCED]) {
83
      playerTimeDiff::user_time_diff_set(array(
84
        self::TIME_DIFF              => '',
85
        self::TIME_DIFF_UTC_OFFSET   => 0,
86
        self::TIME_DIFF_FORCED       => 0,
87
        self::TIME_DIFF_MEASURE_TIME => SN_TIME_SQL,
88
      ));
89
    }
90
  }
91
92
  /**
93
   * @return int
94
   */
95
  static public function timeProbeAjax() {
96
    $userTimeDiff = playerTimeDiff::user_time_diff_get();
97
    if ($userTimeDiff[self::TIME_DIFF_FORCED]) {
98
      $time_diff = intval($userTimeDiff[self::TIME_DIFF]);
99
    } else {
100
      $userTimeDiff = playerTimeDiff::user_time_diff_probe();
101
      playerTimeDiff::user_time_diff_set($userTimeDiff);
102
      $time_diff = $userTimeDiff[self::TIME_DIFF] + $userTimeDiff[self::TIME_DIFF_UTC_OFFSET];
103
    }
104
105
    return $time_diff;
106
  }
107
108
  /**
109
   * @return mixed
110
   */
111
  static public function defineTimeDiff() {
112
    $user_time_diff = playerTimeDiff::user_time_diff_get();
113
114
    $time_diff = (float)$user_time_diff[self::TIME_DIFF] + $user_time_diff[self::TIME_DIFF_UTC_OFFSET];
115
116
    define('SN_CLIENT_TIME_DIFF', $time_diff);
117
    define('SN_CLIENT_TIME_LOCAL', SN_TIME_NOW + SN_CLIENT_TIME_DIFF);
118
    define('SN_CLIENT_TIME_DIFF_GMT', $user_time_diff[self::TIME_DIFF]);
119
120
    return $time_diff; // Разница в GMT-времени между клиентом и сервером. Реальная разница в ходе часов
121
  }
122
123
  /**
124
   * @return int
125
   */
126
  static public function timeDiffTemplate() {
127
    $user_time_diff          = playerTimeDiff::user_time_diff_get();
128
    $user_time_measured_unix = intval(isset($user_time_diff[self::TIME_DIFF_MEASURE_TIME]) ? strtotime($user_time_diff[self::TIME_DIFF_MEASURE_TIME]) : 0);
129
    $measureTimeDiff         = intval(
130
      empty($user_time_diff[self::TIME_DIFF_FORCED])
131
      &&
132
      (SN_TIME_NOW - $user_time_measured_unix > PERIOD_HOUR || $user_time_diff[self::TIME_DIFF] == '')
133
    );
134
135
    return $measureTimeDiff;
136
  }
137
138
  /**
139
   * @return int
140
   */
141
  static public function getTimeDiffForced() {
142
    $user_time_diff        = playerTimeDiff::user_time_diff_get();
143
    $user_time_diff_forced = $user_time_diff[self::TIME_DIFF_FORCED];
144
145
    return $user_time_diff_forced;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $user_time_diff_forced also could return the type string which is incompatible with the documented return type integer.
Loading history...
146
  }
147
148
  /**
149
   * @param array $user_time_diff
150
   *
151
   * @return array
152
   */
153
  protected static function sortDiffArray(array $user_time_diff) {
154
    $user_time_diff = [
155
      self::TIME_DIFF              => isset($user_time_diff[self::TIME_DIFF]) ? $user_time_diff[self::TIME_DIFF] : '',
156
      self::TIME_DIFF_UTC_OFFSET   => isset($user_time_diff[self::TIME_DIFF_UTC_OFFSET]) ? $user_time_diff[self::TIME_DIFF_UTC_OFFSET] : 0,
157
      self::TIME_DIFF_FORCED       => isset($user_time_diff[self::TIME_DIFF_FORCED]) ? $user_time_diff[self::TIME_DIFF_FORCED] : 0,
158
      self::TIME_DIFF_MEASURE_TIME => isset($user_time_diff[self::TIME_DIFF_MEASURE_TIME]) ? $user_time_diff[self::TIME_DIFF_MEASURE_TIME] : '2000-01-01',
159
    ];
160
161
    return $user_time_diff;
162
  }
163
164
}
165