function exit_with_mojo_flush_response($req, $response) { if (is_valid_response($response)) { exit_with($response); } exit_with(glue_response($req, $response)); }
function exit_with_302_plain($url) { exit_with(STATUS_FOUND, array('location' => $url, 'content-type' => 'text/plain'), '$url'); }
function exit_with_500($body) { exit_with($body, 500); }
if (!($uid > 0) || !$code) { exit_with('error', 'invalid user or code'); } $data = C::t(TB)->fetch_all($uid)[$uid]; if (!$data || !$data['key']) { exit_with('error', 'invalid key'); } $fail_count = $data['fail_count']; $ban_until = $data['fail_ban_until']; if (time() < $ban_until) { exit_with('error', 'failed too many times'); } $key = $data['key']; $tick = floor(time() / CODE_INTERVAL); for ($i = 0; $i < CODE_LIFE; $i++) { if (make_code($key, $tick - $i + 1) === $code) { if ($fail_count > 0) { C::t(TB)->update($uid, array('fail_count' => 0)); } C::t(LOG)->insert(array('uid' => $uid, 'action' => 'login', 'result' => 'ok')); exit_with('ok', user_login($uid)); } } if (++$fail_count > MAX_FAIL_TIMES) { $ban_until = time() + FAIL_BAN_TIME; $fail_count = 0; } C::t(TB)->update($uid, array('fail_count' => $fail_count, 'fail_ban_until' => $ban_until)); C::t(LOG)->insert(array('uid' => $uid, 'action' => 'login', 'result' => 'fail * ' . $fail_count)); exit_with('error', 'login failed');
} $data['key'] = bin2hex(openssl_random_pseudo_bytes(256)); $data['request_code'] = $request_code; $data['request_time'] = time(); C::t(TB)->update($uid, $data); exit_with('ok', $request_code); } else { if ($ac === 'download') { $data = C::t(TB)->fetch_all($uid)[$uid]; if (!$data['request_code'] || $data['request_code'] !== $_GET['code'] || time() - $data['request_time'] > REQUEST_EXPIRE) { exit_with('error', 'invalid download link'); } $data['request_code'] = ''; C::t(TB)->update($uid, $data); C::t(LOG)->insert(array('uid' => $uid, 'action' => 'download key', 'result' => 'ok')); exit_with('ok', $data['key']); } else { if ($ac === 'check') { $data = C::t(TB)->fetch_all($uid)[$uid]; exit_with($data ? 'ok' : 'error'); } else { if ($ac === 'cancel') { C::t(TB)->delete($uid); C::t(LOG)->insert(array('uid' => $uid, 'action' => 'remove key', 'result' => 'ok')); exit_with('ok'); } } } } exit_with('error', 'invalid access');