public function init()
{
try {
$this->header('Content-Type: text/plain');
//$this->header('Content-Type: application/x-json');
} catch (\Exception $e) {
}
if (!$this->importCmdArgs()) {
return;
}
$this->sleep(5, true);
// setting timeout 5 seconds */
$this->onSessionStart(function () {
$this->wakeup();
if ($this->cmd === 'LOGIN') {
if (sizeof($this->args) !== 2) {
$this->result = ['$err' => 'You must pass exactly 2 arguments.'];
$this->wakeup();
return;
}
$c1 = \PHPDaemon\Utils\Crypt::compareStrings($this->appInstance->config->username->value, $this->args[0]) ? 0 : 1;
$c2 = \PHPDaemon\Utils\Crypt::compareStrings($this->appInstance->config->password->value, $this->args[1]) ? 0 : 1;
if ($c1 + $c2 > 0) {
$this->result = ['$err' => 'Wrong username and/or password.'];
return;
}
$this->attrs->session['logged'] = $this->appInstance->config->credver;
$this->result = ['$ok' => 1];
$this->wakeup();
return;
} elseif ($this->cmd === 'LOGOUT') {
unset($this->attrs->session['logged']);
$this->result = ['$ok' => 1];
$this->wakeup();
return;
}
if (!isset($this->attrs->session['logged']) || $this->attrs->session['logged'] < $this->appInstance->config->credver) {
$this->result = ['$err' => 'You must be authenticated.'];
$this->wakeup();
return;
}
});
}