Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tltneon committed Jul 25, 2024
1 parent daf537f commit 1698088
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
20 changes: 14 additions & 6 deletions src/lgsl_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@ public function queryCached($request = 'seph') {
public function queryLive($request = 'seph') {
if ($request === "h") return;
$this->queried();
$this->removeOption("_error");
$protocol = new Protocol($this, $request);
$protocol->query();

Expand Down Expand Up @@ -1044,12 +1045,22 @@ public function checkTimestamps($request = "") {
public function getZone() {
return isset($this->_other['zone']) ? $this->_other['zone'] : "";
}
public function setExtraValue($name, $value) {
public function addOption($name, $value) {
if (isset($this->_extra[$name])) {
$this->_extra[$name] .= "\n{$value}";
} else {
$this->setOption($name, $value);
}
}
public function setOption($name, $value) {
$this->_extra[$name] = $value;
}
public function getE($name) {
public function getOption($name) {
return $this->_extra[$name] ?? LGSL::NONE;
}
public function removeOption($name) {
unset($this->_extra['_error']);
}
public function setStatus($status) {
$this->_base['status'] = (int) $status;
}
Expand All @@ -1060,7 +1071,7 @@ public function getStatus() {
if ($this->_server['password']) {
return self::PASSWORDED;
}
if (isset($this->_extra['_error'])) {
if ($this->_base['status'] === Query::WITH_ERROR && isset($this->_extra['_error'])) {
return self::ERROR;
}
if ($this->_base['status']) {
Expand All @@ -1072,9 +1083,6 @@ public function isOnline() {
$s = $this->getStatus();
return $s === self::PASSWORDED || $s === self::ONLINE;
}
public function trimError() {
unset($this->_extra['_error']);
}
public function queryLocation() {
return LGSL::locationCode($this->getIp());
}
Expand Down
4 changes: 2 additions & 2 deletions src/lgsl_feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@
//------------------------------------------------------------------------------------------------------------+
// ADD THE FEED PROVIDER

$server->setExtraValue('_feed_', "http://{$_SERVER['HTTP_HOST']}");
$server->setExtraValue('_lgsl_', LGSL::VERSION);
$server->setOption('_feed_', "http://{$_SERVER['HTTP_HOST']}");
$server->setOption('_lgsl_', LGSL::VERSION);

//------------------------------------------------------------------------------------------------------------+
// FEED USAGE LOGGING - 'logs' FOLDER MUST BE MANUALLY CREATED AND SET AS WRITABLE
Expand Down
28 changes: 15 additions & 13 deletions src/lgsl_protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,11 +448,11 @@ public function query() {
if (class_exists($query)) {
$status = (new $query($this->_server, $this->_lgsl_fp, $this->_lgsl_need))->execute();
} else {
$this->_server->setExtraValue('_error', "LGSL PROBLEM: FUNCTION DOES NOT EXIST FOR TYPE {$this->_server->getType()}");
$this->_server->addOption('_error', "LGSL PROBLEM: FUNCTION DOES NOT EXIST FOR TYPE {$this->_server->getType()}");
$this->updateTimestamps();
}
} else {
$this->_server->setExtraValue('_error', 'Can\'t establish the connection to server.');
$this->_server->addOption('_error', 'Can\'t establish the connection to server.');
$this->updateTimestamps();
}
$this->_server->setStatus($status);
Expand All @@ -463,9 +463,9 @@ public function query() {
//------------------------------------------------------------------------------------------------------------+
//------------------------------------------------------------------------------------------------------------+
abstract class Query {
protected const NO_RESPOND = 0;
protected const SUCCESS = 1;
protected const WITH_ERROR = 2;
const NO_RESPOND = 0;
const SUCCESS = 1;
const WITH_ERROR = 2;
protected $_server, $_fp, $_need;
protected $_data = ['s' => [], 'e' => [], 'p' => []];
protected $separatedPackets = false;
Expand All @@ -487,17 +487,17 @@ abstract function process();
public function execute(): int {
$time = microtime(true);
$status = $this->process();
if ($this->separatedPackets) {
if ($status === $this::SUCCESS && $this->separatedPackets) {
if ($this->_need['e']) $status |= $this->process();
if ($this->_need['p']) $status |= $this->process();
}
$this->_data['o']['time_execution'] = $time - microtime(true);
if ($status !== $this::NO_RESPOND) {
if ($status === $this::SUCCESS) {
$this->_server->trimError();
$this->_server->removeOption("_error");
}
if ($status === $this::WITH_ERROR) {
$this->_data['e']['_error'] = $this->_data['e']['_error'] ?? "Probably protocol mistake";
$this->_server->addOption("_error", "Probably protocol mistake: " . static::class);
}
$this->_server->updateValues($this->_data);
}
Expand Down Expand Up @@ -2438,9 +2438,11 @@ public function process() {
if (isset($buffer['inviter'])) {
$this->_data['e']['inviter'] = "{$buffer['inviter']['username']}#{$buffer['inviter']['discriminator']}";
}
$this->need('s');
$this->need('e');

if ($this->need('p')) {
$buffer = $this->fetch("https://discordapp.com/api/guilds/{$this->_server->getE('id')}/widget.json");
$buffer = $this->fetch("https://discordapp.com/api/guilds/{$this->_server->getOption('id')}/widget.json");

if (isset($buffer['code']) and $buffer['code'] == 0) {
$this->_data['e']['_error_fetching_users'] = $buffer['message'];
Expand Down Expand Up @@ -3093,8 +3095,8 @@ public function open(&$server = null) {
} else {
$this->_stream = @fsockopen("{$this->_protocol}://{$server->getIp()}", $server->getQueryPort(), $errno, $errstr, 1);
if (!$this->_stream) {
$server->setExtraValue('_error', $errstr);
$server->setStatus(false);
$server->addOption('_error', $errstr);
$server->setStatus(Server::OFFLINE);
return false;
}
stream_set_timeout($this->_stream, $lgsl_config['timeout'], $lgsl_config['timeout'] ? 0 : 500000);
Expand All @@ -3112,8 +3114,8 @@ public function readRaw($length = 4096) {
if (curl_errno($this->_stream)) {
$msg = "{$err}<br>" . curl_error($this->_stream);
if ($this->_server) {
$this->_server->setExtraValue('_error', $msg);
$this->_server->setStatus(false);
$this->_server->addOption('_error', $msg);
$this->_server->setStatus(Server::OFFLINE);
return false;
} else {
return $msg;
Expand Down

0 comments on commit 1698088

Please sign in to comment.