Completed
Push — trunk ( 571dd3...e6b3b6 )
by SuperNova.WS
07:55
created

VersionCheckerDeprecated::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by Gorlum 01.10.2017 13:12
4
 */
5
6
namespace Tools;
7
8
use \classConfig;
9
use \classSupernova;
10
11
class VersionCheckerDeprecated {
12
  /**
13
   * @var classConfig $config
14
   */
15
  protected $config;
16
17
  /**
18
   * @var int $versionCheckResult
19
   */
20
  public $versionCheckResult = SNC_VER_ERROR_CONNECT;
21
22
  /**
23
   * @var array $registerResult
24
   */
25
  public $registerResult = [];
26
27
  public static function performCheckVersion() {
28
    $that = new static();
29
    $that->checkVersion(SNC_MODE_VERSION_CHECK);
30
  }
31
32
  public static function handleCall() {
33
    $that = new static();
34
35
    $mode = sys_get_param_int('mode');
36
    $ajax = sys_get_param_int('ajax');
37
38
    $that->preventDualRegistration($mode, $ajax);
39
40
    $that->checkVersion($mode);
41
42
    $that->processRegistration($mode);
43
44
    if ($ajax) {
45
      define('IN_AJAX', true);
46
      print($that->versionCheckResult);
47
    }
48
  }
49
50
  public function __construct() {
51
    $this->config = classSupernova::$config;
52
  }
53
54
  /**
55
   * @param int $mode - SNC_MODE_xxx constants
56
   */
57
  protected function checkVersion($mode) {
58
    $this->processContent(sn_get_url_contents($this->generateUrl($mode)));
59
  }
60
61
  /**
62
   * @param int $mode - SNC_MODE_xxx constants
63
   *
64
   * @return string
65
   */
66
  protected function generateUrl($mode, $debug = false) {
67
    $rootServerUrl = $debug ? 'http://localhost/supernova_site/' : 'http://supernova.ws/';
68
    $thisSiteUrl = $debug ? 'http://supernova.ws/' : SN_ROOT_VIRTUAL;
69
70
    $url =
71
      $rootServerUrl . 'version_check.php'
72
      . '?mode=' . $mode
73
      . '&db=' . DB_VERSION
74
      . '&release=' . SN_RELEASE
75
      . '&version=' . SN_VERSION
76
      . '&key=' . urlencode($this->config->server_updater_key)
0 ignored issues
show
Bug introduced by
The property server_updater_key does not seem to exist. Did you mean server_updater_key'?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
77
      . '&id=' . urlencode($this->config->server_updater_id)
0 ignored issues
show
Bug introduced by
The property server_updater_id does not seem to exist. Did you mean server_updater_id'?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
78
      . ($mode == SNC_MODE_REGISTER ? "&name=" . urlencode($this->config->game_name) . "&url=" . urlencode($thisSiteUrl) : '');
79
80
    return $url;
81
  }
82
83
  /**
84
   * @param $content
85
   */
86
  protected function processContent($content) {
87
    if (!$content) {
88
      $this->versionCheckResult = SNC_VER_ERROR_CONNECT;
89
    } else {
90
      if (($decoded = json_decode($content, true)) !== null) {
91
        $this->versionCheckResult = isset($decoded['version_check']) ? $decoded['version_check'] : SNC_VER_UNKNOWN_RESPONSE;
92
        $this->registerResult = is_array($decoded['site']) ? $decoded['site'] : [];
93
      } else {
94
        $this->versionCheckResult = $content;
95
      }
96
    }
97
98
    $this->config->pass()->server_updater_check_last = SN_TIME_NOW;
0 ignored issues
show
Bug introduced by
The property server_updater_check_last does not seem to exist. Did you mean server_updater_check_last'?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
99
    $this->config->pass()->server_updater_check_result = $this->versionCheckResult;
0 ignored issues
show
Bug introduced by
The property server_updater_check_result does not seem to exist. Did you mean server_updater_check_result'?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
100
  }
101
102
  /**
103
   * @param $mode
104
   * @param $ajax
105
   */
106
  protected function preventDualRegistration($mode, $ajax) {
107
    if ($mode == SNC_MODE_REGISTER && ($this->config->server_updater_key || $this->config->server_updater_id)) {
0 ignored issues
show
Bug introduced by
The property server_updater_key does not seem to exist. Did you mean server_updater_key'?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
The property server_updater_id does not seem to exist. Did you mean server_updater_id'?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
108
      if ($ajax) {
109
        print(SNC_VER_REGISTER_ERROR_REGISTERED);
110
      }
111
      die();
112
    }
113
  }
114
115
  /**
116
   * @param $mode
117
   */
118
  protected function processRegistration($mode) {
119
    if ($mode == SNC_MODE_REGISTER) {
120
      $this->versionCheckResult = isset($this->registerResult['result']) ? $this->registerResult['result'] : SNC_VER_UNKNOWN_RESPONSE;
121
122
      if ($this->registerResult['result'] == SNC_VER_REGISTER_REGISTERED && $this->registerResult['site_key'] && $this->registerResult['site_id']) {
123
        $this->config->pass()->server_updater_key = $this->registerResult['site_key'];
0 ignored issues
show
Bug introduced by
The property server_updater_key does not seem to exist. Did you mean server_updater_key'?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
124
        $this->config->pass()->server_updater_id = $this->registerResult['site_id'];
0 ignored issues
show
Bug introduced by
The property server_updater_id does not seem to exist. Did you mean server_updater_id'?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
125
      }
126
    }
127
  }
128
129
}
130