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; } }); }
public function checkCode($code, $cb) { if ($this->cond === null) { $this->extractCondFrom($this->obj); } $this->orm->messages->findAndModify(['query' => $this->cond + ['tries' => ['$gt' => 0], 'ts' => ['$gt' => microtime(true) - 5 * 60], 'success' => null], 'update' => ['$inc' => ['tries' => -11]], 'new' => true], function ($lastError) use($cb, $code) { if (!isset($lastError['value']['code'])) { call_user_func($cb, $this, false, 0); return; } if (!Crypt::compareStrings($lastError['value']['code'], trim($code))) { call_user_func($cb, $this, false, $lastError['value']['tries']); } $this->set('success', true); $this->save(function () use($cb) { if ($this->lastError(true)) { call_user_func($cb, $this, true); } else { call_user_func($cb, $this, false, 0); } }); }); }
/** * @param string $password * @return bool */ public function checkPassword($password) { return !isset($this->obj['password']) ? false : Crypt::compareStrings($this->obj['password'], Crypt::hash($password, $this->obj['salt'] . $this->appInstance->config->cryptsaltextra->value)); }