1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-11-15 03:17:38 +01:00
vichan/inc/mod/auth.php

170 lines
4.6 KiB
PHP
Raw Normal View History

2012-04-12 18:11:41 +02:00
<?php
/*
2013-01-20 11:23:46 +01:00
* Copyright (c) 2010-2013 Tinyboard Development Group
2012-04-12 18:11:41 +02:00
*/
if (realpath($_SERVER['SCRIPT_FILENAME']) == str_replace('\\', '/', __FILE__)) {
// You cannot request this file directly.
exit;
}
// create a hash/salt pair for validate logins
function mkhash($username, $password, $salt = false) {
global $config;
if (!$salt) {
// create some sort of salt for the hash
$salt = substr(base64_encode(sha1(rand() . time(), true) . $config['cookies']['salt']), 0, 15);
$generated_salt = true;
}
// generate hash (method is not important as long as it's strong)
$hash = substr(base64_encode(md5($username . $config['cookies']['salt'] . sha1($username . $password . $salt . ($config['mod']['lock_ip'] ? $_SERVER['REMOTE_ADDR'] : ''), true), true)), 0, 20);
2012-04-12 18:11:41 +02:00
if (isset($generated_salt))
2012-08-27 13:50:15 +02:00
return array($hash, $salt);
2012-04-12 18:11:41 +02:00
else
return $hash;
}
2013-07-24 17:15:55 +02:00
function generate_salt() {
mt_srand(microtime(true) * 100000 + memory_get_usage(true));
return md5(uniqid(mt_rand(), true));
}
2012-04-12 18:11:41 +02:00
function login($username, $password, $makehash=true) {
global $mod;
// SHA1 password
if ($makehash) {
$password = sha1($password);
}
2013-07-24 17:15:55 +02:00
$query = prepare("SELECT `id`, `type`, `boards`, `password`, `salt` FROM `mods` WHERE `username` = :username");
2012-04-12 18:11:41 +02:00
$query->bindValue(':username', $username);
$query->execute() or error(db_error($query));
2013-07-24 17:15:55 +02:00
if ($user = $query->fetch(PDO::FETCH_ASSOC)) {
if ($user['password'] === hash('sha256', $user['salt'] . $password)) {
return $mod = array(
'id' => $user['id'],
'type' => $user['type'],
'username' => $username,
'hash' => mkhash($username, $user['password']),
'boards' => explode(',', $user['boards'])
);
}
}
return false;
2012-04-12 18:11:41 +02:00
}
function setCookies() {
global $mod, $config;
if (!$mod)
error('setCookies() was called for a non-moderator!');
setcookie($config['cookies']['mod'],
$mod['username'] . // username
':' .
$mod['hash'][0] . // password
':' .
$mod['hash'][1], // salt
time() + $config['cookies']['expire'], $config['cookies']['jail'] ? $config['cookies']['path'] : '/', null, false, true);
}
function destroyCookies() {
global $config;
// Delete the cookies
setcookie($config['cookies']['mod'], 'deleted', time() - $config['cookies']['expire'], $config['cookies']['jail']?$config['cookies']['path'] : '/', null, false, true);
}
function modLog($action, $_board=null) {
global $mod, $board, $config;
$query = prepare("INSERT INTO `modlogs` VALUES (:id, :ip, :board, :time, :text)");
$query->bindValue(':id', $mod['id'], PDO::PARAM_INT);
$query->bindValue(':ip', $_SERVER['REMOTE_ADDR']);
$query->bindValue(':time', time(), PDO::PARAM_INT);
$query->bindValue(':text', $action);
if (isset($_board))
$query->bindValue(':board', $_board);
elseif (isset($board))
$query->bindValue(':board', $board['uri']);
else
$query->bindValue(':board', null, PDO::PARAM_NULL);
$query->execute() or error(db_error($query));
if ($config['syslog'])
_syslog(LOG_INFO, '[mod/' . $mod['username'] . ']: ' . $action);
}
// Validate session
if (isset($_COOKIE[$config['cookies']['mod']])) {
// Should be username:hash:salt
$cookie = explode(':', $_COOKIE[$config['cookies']['mod']]);
if (count($cookie) != 3) {
// Malformed cookies
2012-04-12 18:11:41 +02:00
destroyCookies();
mod_login();
2013-01-29 19:07:09 +01:00
exit;
2012-04-12 18:11:41 +02:00
}
2013-07-24 17:15:55 +02:00
$query = prepare("SELECT `id`, `type`, `boards`, `password` FROM `mods` WHERE `username` = :username");
2012-04-12 18:11:41 +02:00
$query->bindValue(':username', $cookie[0]);
$query->execute() or error(db_error($query));
2013-07-24 17:15:55 +02:00
$user = $query->fetch(PDO::FETCH_ASSOC);
2012-04-12 18:11:41 +02:00
// validate password hash
2013-01-29 12:13:35 +01:00
if ($cookie[1] !== mkhash($cookie[0], $user['password'], $cookie[2])) {
2012-04-12 18:11:41 +02:00
// Malformed cookies
destroyCookies();
mod_login();
2013-01-29 19:07:09 +01:00
exit;
2012-04-12 18:11:41 +02:00
}
2012-08-27 13:50:15 +02:00
$mod = array(
2012-04-12 18:11:41 +02:00
'id' => $user['id'],
'type' => $user['type'],
'username' => $cookie[0],
'boards' => explode(',', $user['boards'])
);
}
2012-04-13 14:00:40 +02:00
function create_pm_header() {
2012-05-20 11:06:27 +02:00
global $mod, $config;
if ($config['cache']['enabled'] && ($header = cache::get('pm_unread_' . $mod['id'])) != false) {
2012-05-20 11:06:27 +02:00
if ($header === true)
return false;
return $header;
}
2012-04-13 14:00:40 +02:00
$query = prepare("SELECT `id` FROM `pms` WHERE `to` = :id AND `unread` = 1");
$query->bindValue(':id', $mod['id'], PDO::PARAM_INT);
$query->execute() or error(db_error($query));
2013-08-01 02:51:43 +02:00
if ($pm = $query->fetch(PDO::FETCH_ASSOC))
2012-08-27 13:50:15 +02:00
$header = array('id' => $pm['id'], 'waiting' => $query->rowCount() - 1);
2012-05-20 11:06:27 +02:00
else
$header = true;
if ($config['cache']['enabled'])
cache::set('pm_unread_' . $mod['id'], $header);
if ($header === true)
return false;
2012-04-13 14:00:40 +02:00
2012-05-20 11:06:27 +02:00
return $header;
2012-04-13 14:00:40 +02:00
}
2012-08-27 07:19:05 +02:00
function make_secure_link_token($uri) {
global $mod, $config;
return substr(sha1($config['cookies']['salt'] . '-' . $uri . '-' . $mod['id']), 0, 8);
}