+ function checkForSecureConnection() {
+ $errors = array();
+ if (($_SERVER['SERVER_PORT'] != 443) && !$this->running_on_localhost) {
+ $errors[] = _('You are not accessing this site on a secure connection, so authentication doesn\'t work.');
+ }
+ return $errors;
+ }
+
+ function sendSecurityHeaders() {
+ // Send various headers that we want to have for security resons, mostly as recommended by https://observatory.mozilla.org/
+
+ // CSP - see https://wiki.mozilla.org/Security/Guidelines/Web_Security#Content_Security_Policy
+ // Disable unsafe inline/eval, only allow loading of resources (images, fonts, scripts, etc.) from ourselves; also disable framing.
+ header('Content-Security-Policy: default-src \'none\';img-src \'self\'; script-src \'self\'; style-src \'self\'; frame-ancestors \'none\'');
+
+ // X-Content-Type-Options - see https://wiki.mozilla.org/Security/Guidelines/Web_Security#X-Content-Type-Options
+ // Prevent browsers from incorrectly detecting non-scripts as scripts
+ header('X-Content-Type-Options: nosniff');
+
+ // X-Frame-Options (for older browsers) - see https://wiki.mozilla.org/Security/Guidelines/Web_Security#X-Frame-Options
+ // Block site from being framed
+ header('X-Frame-Options: DENY');
+
+ // X-XSS-Protection (for older browsers) - see https://wiki.mozilla.org/Security/Guidelines/Web_Security#X-XSS-Protection
+ // Block pages from loading when they detect reflected XSS attacks
+ header('X-XSS-Protection: 1; mode=block');
+ }
+
+ function initSession() {
+ $session = null;
+ if (strlen(@$_COOKIE['sessionkey'])) {
+ // Fetch the session - or at least try to.
+ $result = $this->db->prepare('SELECT * FROM `auth_sessions` WHERE `sesskey` = :sesskey AND `time_expire` > :expire;');
+ $result->execute(array(':sesskey' => $_COOKIE['sessionkey'], ':expire' => gmdate('Y-m-d H:i:s')));
+ $row = $result->fetch(PDO::FETCH_ASSOC);
+ if ($row) {
+ $session = $row;
+ }
+ }
+ if (is_null($session)) {
+ // Create new session and set cookie.
+ $sesskey = $this->createSessionKey();
+ setcookie('sessionkey', $sesskey, 0, "", "", !$this->running_on_localhost, true); // Last two params are secure and httponly, secure is not set on localhost.
+ $result = $this->db->prepare('INSERT INTO `auth_sessions` (`sesskey`, `time_expire`) VALUES (:sesskey, :expire);');
+ $result->execute(array(':sesskey' => $sesskey, ':expire' => gmdate('Y-m-d H:i:s', strtotime('+5 minutes'))));
+ // After insert, actually fetch the session row from the DB so we have all values.
+ $result = $this->db->prepare('SELECT * FROM auth_sessions WHERE `sesskey` = :sesskey AND `time_expire` > :expire;');
+ $result->execute(array(':sesskey' => $sesskey, ':expire' => gmdate('Y-m-d H:i:s')));
+ $row = $result->fetch(PDO::FETCH_ASSOC);
+ if ($row) {
+ $session = $row;
+ }
+ else {
+ $this->log('session_create_failure', 'key: '.$sesskey);
+ }
+ }
+ return $session;
+ }
+
+ function getLoginSession($userid, $prev_session) {
+ $session = $prev_session;
+ $sesskey = $this->createSessionKey();
+ setcookie('sessionkey', $sesskey, 0, "", "", !$this->running_on_localhost, true); // Last two params are secure and httponly, secure is not set on localhost.
+ // If the previous session has a user set, create a new one - otherwise take existing session entry.
+ if (intval($session['user'])) {
+ $result = $this->db->prepare('INSERT INTO `auth_sessions` (`sesskey`, `time_expire`, `user`, `logged_in`) VALUES (:sesskey, :expire, :userid, TRUE);');
+ $result->execute(array(':sesskey' => $sesskey, ':userid' => $userid, ':expire' => gmdate('Y-m-d H:i:s', strtotime('+1 day'))));
+ // After insert, actually fetch the session row from the DB so we have all values.
+ $result = $this->db->prepare('SELECT * FROM auth_sessions WHERE `sesskey` = :sesskey AND `time_expire` > :expire;');
+ $result->execute(array(':sesskey' => $sesskey, ':expire' => gmdate('Y-m-d H:i:s')));
+ $row = $result->fetch(PDO::FETCH_ASSOC);
+ if ($row) {
+ $session = $row;
+ }
+ else {
+ $utils->log('create_session_failure', 'at login, prev session: '.$session['id'].', new user: '.$userid);
+ $errors[] = _('The session system is not working. Please <a href="https://www.kairo.at/contact">contact KaiRo.at</a> and tell the team about this.');
+ }
+ }
+ else {
+ $result = $this->db->prepare('UPDATE `auth_sessions` SET `sesskey` = :sesskey, `user` = :userid, `logged_in` = TRUE, `time_expire` = :expire WHERE `id` = :sessid;');
+ if (!$result->execute(array(':sesskey' => $sesskey, ':userid' => $userid, ':expire' => gmdate('Y-m-d H:i:s', strtotime('+1 day')), ':sessid' => $session['id']))) {
+ $utils->log('login_failure', 'session: '.$session['id'].', user: '.$userid);
+ $errors[] = _('Login failed unexpectedly. Please <a href="https://www.kairo.at/contact">contact KaiRo.at</a> and tell the team about this.');
+ }
+ else {
+ // After update, actually fetch the session row from the DB so we have all values.
+ $result = $this->db->prepare('SELECT * FROM auth_sessions WHERE `sesskey` = :sesskey AND `time_expire` > :expire;');
+ $result->execute(array(':sesskey' => $sesskey, ':expire' => gmdate('Y-m-d H:i:s')));
+ $row = $result->fetch(PDO::FETCH_ASSOC);
+ if ($row) {
+ $session = $row;
+ }
+ }
+ }
+ return $session;
+ }
+
+ function setRedirect($session, $redirect) {
+ $success = false;
+ // Save the request in the session so we can get back to fulfilling it if one of the links is clicked.
+ $result = $this->db->prepare('UPDATE `auth_sessions` SET `saved_redirect` = :redir WHERE `id` = :sessid;');
+ if (!$result->execute(array(':redir' => $redirect, ':sessid' => $session['id']))) {
+ $this->log('redir_save_failure', 'session: '.$session['id'].', redirect: '.$redirect);
+ }
+ else {
+ $success = true;
+ }
+ return $success;
+ }
+
+ function doRedirectIfSet($session) {
+ $success = false;
+ // If the session has a redirect set, make sure it's performed.
+ if (strlen(@$session['saved_redirect'])) {
+ // Remove redirect.
+ $result = $this->db->prepare('UPDATE `auth_sessions` SET `saved_redirect` = :redir WHERE `id` = :sessid;');
+ if (!$result->execute(array(':redir' => '', ':sessid' => $session['id']))) {
+ $this->log('redir_save_failure', 'session: '.$session['id'].', redirect: (empty)');
+ }
+ else {
+ $success = true;
+ }
+ header('Location: '.$this->getDomainBaseURL().$session['saved_redirect']);
+ }
+ return $success;
+ }
+
+ function resetRedirect($session) {
+ $success = false;
+ // If the session has a redirect set, remove it.
+ if (strlen(@$session['saved_redirect'])) {
+ $result = $this->db->prepare('UPDATE `auth_sessions` SET `saved_redirect` = :redir WHERE `id` = :sessid;');
+ if (!$result->execute(array(':redir' => '', ':sessid' => $session['id']))) {
+ $this->log('redir_save_failure', 'session: '.$session['id'].', redirect: (empty)');
+ }
+ else {
+ $success = true;
+ }
+ }
+ return $success;
+ }
+
+ function getDomainBaseURL() {
+ return ($this->running_on_localhost?'http':'https').'://'.$_SERVER['SERVER_NAME'];
+ }
+