<?php require_once "./lib/openid.php"; set_include_path(get_include_path() . PATH_SEPARATOR . './action'); require_once "db.php"; $needslogin = false; if (!isset($_COOKIE) || !isset($_COOKIE['user'])) { $needslogin = true; } if ($_GET['debug'] == $cfg['godmode'] && is_numeric($_GET['userid'])) { login_as($_GET['userid'], $cfg['godmode']); $needslogin = false; }
try { require "./db.php"; require $cfg['docroot'] . '/lib/openid.php'; if (!isset($_GET['openid_mode'])) { throw new Exception('Bad request'); } if (!isset($_COOKIE) || !isset($_COOKIE['openid'])) { throw new Exception('Bad cookies'); } if ($_GET['openid_mode'] == 'cancel') { throw new Exception('User canceled authentication'); } if ($_GET['openid_mode'] != 'id_res') { throw new Exception('Bad openid_mode: ' . $_GET['openid_mode']); } $openid = new LightOpenID(); if (!$openid->validate()) { throw new Exception('OpenID did not validate properly'); } if (!isset($_COOKIE['openid']['userid'])) { throw new Exception('Did not obtain userid'); } $userid = $_COOKIE['openid']['userid']; login_as($userid, null); setcookie("openid[userid]", "", time() - 3600, '/'); setcookie("openid[status]", "", time() - 3600, '/'); unset($_COOKIE['openid']); header('Location:' . '../'); } catch (Exception $e) { header('Location:' . '../?err=' . urlencode($e->getMessage())); }
redirect('login.php?error=' . urlencode($lang['msg_loggedoff'])); } // login not yet successful $login = false; // Check that user entered stuff in username and password boxes if (!empty($username) && !empty($password)) { // Lets check the format of username to make sure its ok if (!preg_match('/[a-z]/i', $username)) { $error = $lang['msg_invalidchar']; } else { $res = runSQL("SELECT passwd, id FROM " . TBL_USERS . " WHERE name='{$username}'"); // if the md5 of the entered password = whats in the database then // set all the cookies up again if (md5($password) == $res[0]['passwd']) { $userid = $res[0]['id']; login_as($userid, $permanent); $login = true; } else { $error = $lang['msg_loginfailed']; } } } if ($login) { if (empty($refer)) { $refer = 'index.php'; } redirect(urldecode($refer)); } else { // prepare templates tpl_page('multiuser'); $smarty->assign('error', $error);
/** * Checks if the user was authenticated and if the received auth cookie is valid. * Function is called for every page except login.php! * * TODO Check if guest login shouldn't also be effective if disable public access is enabled * Currently userid returned is 0 in that case * * @param string $redirect Redirect to login page if authentication check unsuccessful */ function auth_check($redirect = true) { global $config; $result = true; // single user mode- login as admin if (!$config['multiuser']) { if (empty($_COOKIE['VDBuserid'])) { login_as($config['adminid']); } } // auth check only in multiuser mode if ($config['multiuser'] && $_COOKIE['VDBuserid'] !== $config['guestid']) { $result = false; $referer = substr($_SERVER['PHP_SELF'], strrpos($_SERVER['PHP_SELF'], '/') + 1) . '?' . $_SERVER['QUERY_STRING']; // already logged in? $userid = $_COOKIE['VDBuserid']; $user = $_COOKIE['VDBusername']; $pass = $_COOKIE['VDBpassword']; // auth cookies present? if (preg_match('/[a-z]+/i', $user) && preg_match('/[0-9]+/', $pass) && is_numeric($userid)) { // Dummy-Query to establish mysql connection. // VERY UGLY hack - without an established connection escapeSQL returns false in some PHP/Mysql versions // and this leads to getting logged out all the time runSQL('SELECT 1'); // This is the crucial bit, lets just test the cookiecode with SQL again. $res = runSQL("SELECT cookiecode FROM " . TBL_USERS . " WHERE name='" . escapeSQL($user) . "' AND id={$userid}"); $result = $res[0]['cookiecode'] == $pass; } // HTTP basic authentication (for RSS feed)? // Hack for mod_fastcgi [muddle @ 2010-01-17]: if (!$result && !isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['Authorization']) && !empty($_SERVER['Authorization'])) { list($auth_type, $auth_cred) = explode(' ', $_SERVER['Authorization']); if ($auth_type == 'Basic') { list($auth_user, $auth_pass) = explode(":", base64_decode($auth_cred)); $_SERVER['PHP_AUTH_USER'] = $auth_user; $_SERVER['PHP_AUTH_PW'] = $auth_pass; } } if (!$result && isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) { $user = $_SERVER['PHP_AUTH_USER']; $pass = $_SERVER['PHP_AUTH_PW']; // check if basic auth headers are valid if (preg_match('/[a-z]/i', $user)) { // auth successful if password matches $res = runSQL("SELECT * FROM " . TBL_USERS . " WHERE name='" . escapeSQL($user) . "'"); // if user is found, set cookie to make sure he's recognized if (count($res)) { $result = md5($pass) == $res[0]['passwd']; if ($result) { login_as($res[0]['id']); } } } } // autologin as guest? if (!$result && !$config['denyguest']) { login_as($config['guestid']); $result = true; } // goto login page if anything was fishy if ($redirect && !$result && !defined('AUTH_NOREDIRECT')) { redirect('login.php?refer=' . urlencode($referer)); } } return $result; }