public function send(ResponseEvent $event) { /** @var HttpResponseEx $response */ $response = $event->getResponse(); if ($response->getStatusCode() === 401) { $user_id = $this->session->getLoggedInUserId(); $reason = $user_id > 0 ? $this->lang->getText('Your account does not have the required authorization to view this page') : $this->lang->getText('You must be logged in to view this page'); if ($event->isAjaxRequest()) { $response->setContent($reason); } else { $url = $this->response->getLoginRedirect($reason, true); $response->redirect($url, 302); } } if (!headers_sent()) { foreach ($response->getHeaders() as $header) { header($header, false); } } echo $response->getContent() ?? sprintf("Error code: %d", $response->getStatusCode()); }
public function setup(HttpRequestEx $request) { $params = $request->getParameters(); try { if (!empty($params['db']['database']) && !empty($params['db']['username']) && !empty($params['db']['password'])) { try { $conn = $this->database->connect($params['db']); if ($pdo = $conn->getPdo()) { $conf = sprintf('%s/app/Config/db-config', $this->bootLoader->getBaseDir()); if (file_put_contents($conf, sprintf('mysql://%s:%s@%s/%s', $params['db']['username'], $params['db']['password'], $params['db']['host'], $params['db']['database']))) { if ($this->installer->install(['minutephp/site'], 'require', true)) { $sth = $pdo->prepare('REPLACE INTO users SET email = :email, password = :password, ip_addr = :ip, created_at = NOW(), updated_at = NOW(), first_name = "Admin", verified = "true"'); $sth->execute(['email' => sprintf('admin@%s', $params['site']['domain'] ?? 'localhost'), 'password' => password_hash(Str::random(), PASSWORD_DEFAULT), 'ip' => $this->sniffer->getUserIP()]); if ($admin_id = $pdo->lastInsertId()) { $sth = $pdo->prepare('REPLACE INTO m_user_groups set user_id = :user_id, group_name = "admin", created_at = NOW(), updated_at = NOW(), expires_at = "20200101", credits = 999, comments = "First run"'); $sth->execute(['user_id' => $admin_id]); $types = ['public' => $params['site'] ?? [], 'private' => []]; foreach ($types as $type => $data) { $sth = $pdo->prepare('REPLACE INTO m_configs set type = :type, data_json = :data'); $sth->execute(['type' => $type, 'data' => json_encode($data)]); } $this->session->startSession($admin_id); return 'pass'; } } else { throw new FirstRunError($this->lang->getText("Unable to run composer")); } } } } catch (\Throwable $e) { throw new FirstRunError($this->lang->getText("Unable to connect to database.\n") . $e->getMessage()); } } throw new FirstRunError($this->lang->getText('All connection parameters are required. Please check connection details')); } catch (\Throwable $e) { if (!empty($conf) && file_exists($conf)) { @unlink($conf); } throw new FirstRunError("Error: " . $e->getMessage()); } }