supernova-ws /
SuperNova
| 1 | <?php |
||||
| 2 | |||||
| 3 | /** |
||||
| 4 | * User: Gorlum |
||||
| 5 | * Date: 29.08.2015 |
||||
| 6 | * Time: 16:49 |
||||
| 7 | */ |
||||
| 8 | |||||
| 9 | use DBAL\db_mysql; |
||||
| 10 | |||||
| 11 | /** |
||||
| 12 | * Подробности о запросе |
||||
| 13 | */ |
||||
| 14 | class RequestInfo { |
||||
| 15 | /** |
||||
| 16 | * Идентификационная строка устройства |
||||
| 17 | * |
||||
| 18 | * @var string |
||||
| 19 | */ |
||||
| 20 | protected $device_cypher = ''; |
||||
| 21 | /** |
||||
| 22 | * Идентификатор устройства |
||||
| 23 | * |
||||
| 24 | * @var string |
||||
| 25 | */ |
||||
| 26 | public $device_id = 0; |
||||
| 27 | |||||
| 28 | /** |
||||
| 29 | * Строка User-agent пользовательского браузера |
||||
| 30 | * |
||||
| 31 | * @var string |
||||
| 32 | */ |
||||
| 33 | protected $user_agent = ''; |
||||
| 34 | /** |
||||
| 35 | * Внутренний идентификатор строки браузера |
||||
| 36 | * |
||||
| 37 | * @var int |
||||
| 38 | */ |
||||
| 39 | public $browser_id = 0; |
||||
| 40 | |||||
| 41 | /** |
||||
| 42 | * Полный URL строки запроса |
||||
| 43 | * |
||||
| 44 | * @var string |
||||
| 45 | */ |
||||
| 46 | protected $page_address = ''; |
||||
| 47 | /** |
||||
| 48 | * ID запроса в таблице УРЛов |
||||
| 49 | * |
||||
| 50 | * @var int |
||||
| 51 | */ |
||||
| 52 | public $page_address_id = 0; |
||||
| 53 | |||||
| 54 | /** |
||||
| 55 | * Query param |
||||
| 56 | * |
||||
| 57 | * @var string $queryString |
||||
| 58 | */ |
||||
| 59 | protected $queryString = ''; |
||||
| 60 | /** |
||||
| 61 | * Query param ID |
||||
| 62 | * |
||||
| 63 | * @var int |
||||
| 64 | */ |
||||
| 65 | public $queryStringId = 0; |
||||
| 66 | |||||
| 67 | /** |
||||
| 68 | * Player entry ID - pointer to combination of player ID, device ID, browser ID, user IP, user proxy |
||||
| 69 | * |
||||
| 70 | * @var int $playerEntryId |
||||
| 71 | */ |
||||
| 72 | protected $playerEntryId = 0; |
||||
| 73 | |||||
| 74 | /** |
||||
| 75 | * Адрес IPv4 в виде строки |
||||
| 76 | * |
||||
| 77 | * @var string |
||||
| 78 | */ |
||||
| 79 | public $ip_v4_string = ''; |
||||
| 80 | /** |
||||
| 81 | * Адрес IPv4 в виде целого |
||||
| 82 | * |
||||
| 83 | * @var int |
||||
| 84 | */ |
||||
| 85 | public $ip_v4_int = 0; |
||||
| 86 | /** |
||||
| 87 | * Цепочка прокси IPv4 |
||||
| 88 | * |
||||
| 89 | * @var string |
||||
| 90 | */ |
||||
| 91 | public $ip_v4_proxy_chain = ''; |
||||
| 92 | |||||
| 93 | protected $write_full_url = false; |
||||
| 94 | |||||
| 95 | public function __construct() { |
||||
| 96 | // TODO - CHANGE!!!! |
||||
| 97 | global $skip_log_query; |
||||
| 98 | |||||
| 99 | $this->write_full_url = !SN::$config->security_write_full_url_disabled; |
||||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||||
| 100 | |||||
| 101 | // Инфа об устройстве и браузере - общая для всех |
||||
| 102 | db_mysql::db_transaction_start(); |
||||
| 103 | $this->device_cypher = $_COOKIE[SN_COOKIE_D]; |
||||
| 104 | if ($this->device_cypher) { |
||||
| 105 | $cypher_safe = SN::$db->db_escape($this->device_cypher); |
||||
| 106 | /** @noinspection SqlResolve */ |
||||
| 107 | $device_id = doquery("SELECT `device_id` FROM `{{security_device}}` WHERE `device_cypher` = '{$cypher_safe}' LIMIT 1 FOR UPDATE", true); |
||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||
| 108 | if (!empty($device_id['device_id'])) { |
||||
| 109 | $this->device_id = $device_id['device_id']; |
||||
| 110 | } |
||||
| 111 | } |
||||
| 112 | |||||
| 113 | if ($this->device_id <= 0) { |
||||
| 114 | do { |
||||
| 115 | $cypher_safe = SN::$db->db_escape($this->device_cypher = sys_random_string()); |
||||
| 116 | |||||
| 117 | /** @noinspection SqlResolve */ |
||||
| 118 | $row = doquery("SELECT `device_id` FROM `{{security_device}}` WHERE `device_cypher` = '{$cypher_safe}' LIMIT 1 FOR UPDATE", true); |
||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||
| 119 | } while (!empty($row)); |
||||
| 120 | doquery("INSERT INTO {{security_device}} (`device_cypher`) VALUES ('{$cypher_safe}');"); |
||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||
| 121 | $this->device_id = SN::$db->db_insert_id(); |
||||
| 122 | sn_setcookie(SN_COOKIE_D, $this->device_cypher, PERIOD_FOREVER, SN_ROOT_RELATIVE); |
||||
| 123 | } |
||||
| 124 | db_mysql::db_transaction_commit(); |
||||
| 125 | |||||
| 126 | $this->user_agent = !empty($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; |
||||
| 127 | $this->browser_id = db_get_set_unique_id_value('security_browser', 'browser_id', ['browser_user_agent' => $this->user_agent,]); |
||||
| 128 | |||||
| 129 | $this->page_address = substr($_SERVER['PHP_SELF'], strlen(SN_ROOT_RELATIVE)); |
||||
| 130 | $this->page_address_id = db_get_set_unique_id_value('security_url', 'url_id', ['url_string' => $this->page_address,]); |
||||
| 131 | |||||
| 132 | // Not a simulator - because it can have loooooong string |
||||
| 133 | if (strpos($_SERVER['REQUEST_URI'], '/simulator.php') !== 0 && !$skip_log_query) { |
||||
| 134 | $this->queryString = !empty($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : ''; |
||||
| 135 | $this->queryStringId = db_get_set_unique_id_value('security_query_strings', 'id', ['query_string' => $this->queryString,]); |
||||
| 136 | } |
||||
| 137 | |||||
| 138 | $ip = sec_player_ip(); |
||||
| 139 | $this->ip_v4_string = $ip['ip']; |
||||
| 140 | $this->ip_v4_int = ip2longu($this->ip_v4_string); |
||||
| 141 | $this->ip_v4_proxy_chain = $ip['proxy_chain']; |
||||
| 142 | |||||
| 143 | $this->playerEntryId = db_get_set_unique_id_value( |
||||
| 144 | 'security_player_entry', |
||||
| 145 | 'id', |
||||
| 146 | [ |
||||
| 147 | 'device_id' => $this->device_id, |
||||
| 148 | 'browser_id' => $this->browser_id, |
||||
| 149 | 'user_ip' => $this->ip_v4_int, |
||||
| 150 | 'user_proxy' => $this->ip_v4_proxy_chain, |
||||
| 151 | ] |
||||
| 152 | ); |
||||
| 153 | } |
||||
| 154 | |||||
| 155 | /** |
||||
| 156 | * Вставляет запись системы безопасности |
||||
| 157 | * |
||||
| 158 | * @param $userId |
||||
| 159 | * |
||||
| 160 | * @return int |
||||
| 161 | * @deprecated |
||||
| 162 | */ |
||||
| 163 | // TODO - remove |
||||
| 164 | public function db_security_entry_insert($userId) { |
||||
| 165 | // TODO $user_id = !empty(self::$user['id']) ? self::$user['id'] : 'NULL'; |
||||
| 166 | if (empty($userId)) { |
||||
| 167 | // self::flog('Нет ИД пользователя'); |
||||
| 168 | return true; |
||||
|
0 ignored issues
–
show
|
|||||
| 169 | } |
||||
| 170 | |||||
| 171 | $pEntry = db_get_set_unique_id_value( |
||||
| 172 | 'security_player_entry', |
||||
| 173 | 'id', |
||||
| 174 | [ |
||||
| 175 | // 'player_id' => $userId, |
||||
| 176 | 'device_id' => $this->device_id, |
||||
| 177 | 'browser_id' => $this->browser_id, |
||||
| 178 | 'user_ip' => $this->ip_v4_int, |
||||
| 179 | 'user_proxy' => $this->ip_v4_proxy_chain, |
||||
| 180 | ] |
||||
| 181 | ); |
||||
| 182 | |||||
| 183 | return $pEntry; |
||||
| 184 | |||||
| 185 | |||||
| 186 | // self::flog('Вставляем запись системы безопасности'); |
||||
| 187 | } |
||||
| 188 | |||||
| 189 | /** |
||||
| 190 | * Вставляет данные в счётчик |
||||
| 191 | * |
||||
| 192 | * @param $user_id_unsafe |
||||
| 193 | */ |
||||
| 194 | public function db_counter_insert($user_id_unsafe) { |
||||
| 195 | global $config, $sys_stop_log_hit, $is_watching; |
||||
| 196 | |||||
| 197 | if ($sys_stop_log_hit || !$config->game_counter) { |
||||
| 198 | return; |
||||
| 199 | } |
||||
| 200 | |||||
| 201 | $user_id_safe = SN::$db->db_escape($user_id_unsafe); |
||||
| 202 | $proxy_safe = SN::$db->db_escape($this->ip_v4_proxy_chain); |
||||
|
0 ignored issues
–
show
|
|||||
| 203 | |||||
| 204 | $is_watching = true; |
||||
| 205 | doquery( |
||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||
| 206 | "INSERT INTO {{counter}} SET |
||||
| 207 | `visit_time` = '" . SN_TIME_SQL . "', |
||||
| 208 | `user_id` = {$user_id_safe}, |
||||
| 209 | `player_entry_id` = {$this->playerEntryId}, |
||||
| 210 | `page_url_id` = {$this->page_address_id}, |
||||
| 211 | `query_string_id` = {$this->queryStringId}" . |
||||
| 212 | ";"); |
||||
| 213 | |||||
| 214 | // `device_id` = {$this->device_id}, |
||||
| 215 | // `browser_id` = {$this->browser_id}, |
||||
| 216 | // `user_ip` = {$this->ip_v4_int}, |
||||
| 217 | // `user_proxy` = '{$proxy_safe}', |
||||
| 218 | |||||
| 219 | $is_watching = false; |
||||
| 220 | } |
||||
| 221 | |||||
| 222 | } |
||||
| 223 |