Session class

This commit is contained in:
Tobias Reich 2014-04-04 21:10:32 +02:00
parent 7f8503aa15
commit 43df706e66
5 changed files with 125 additions and 78 deletions

View File

@ -108,17 +108,21 @@ switch ($_POST['function']) {
// Session Function
case 'init': echo json_encode(init('admin', $_POST['version']));
case 'init': if (!isset($_POST['version'])) exit();
$session = new Session($plugins, $settings);
echo json_encode($session->init(false, $_POST['version']));
break;
case 'login': if (isset($_POST['user'], $_POST['password']))
echo login($_POST['user'], $_POST['password']);
case 'login': if (!isset($_POST['user'], $_POST['password'])) exit();
$session = new Session($plugins, $settings);
echo $session->login($_POST['user'], $_POST['password']);
break;
case 'logout': logout();
case 'logout': $session = new Session($plugins, $settings);
echo $session->logout();
break;
// Settings
// Settings Function
case 'setLogin': if (isset($_POST['username'], $_POST['password']))
if (!isset($_POST['oldPassword'])) $_POST['oldPassword'] = '';

View File

@ -53,11 +53,13 @@ switch ($_POST['function']) {
// Session Functions
case 'init': echo json_encode(init('public', $_POST['version']));
case 'init': $session = new Session($plugins, $settings);
echo json_encode($session->init(true, $_POST['version']));
break;
case 'login': if (isset($_POST['user'], $_POST['password']))
echo login($_POST['user'], $_POST['password']);
case 'login': if (!isset($_POST['user'], $_POST['password'])) exit();
$session = new Session($plugins, $settings);
echo $session->login($_POST['user'], $_POST['password']);
break;
// Miscellaneous

View File

@ -25,7 +25,7 @@ if (!empty($_POST['function'])||!empty($_GET['function'])) {
require('modules/db.php');
require('modules/misc.php');
require('modules/photo.php');
require('modules/session.php');
require('modules/_session.php');
require('modules/settings.php');
require('modules/upload.php');

110
php/modules/_session.php Executable file
View File

@ -0,0 +1,110 @@
<?php
###
# @name Session Module
# @author Tobias Reich
# @copyright 2014 by Tobias Reich
###
if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
class Session {
private $plugins = null;
private $settings = null;
public function __construct($plugins, $settings) {
# Init vars
$this->plugins = $plugins;
$this->settings = $settings;
return true;
}
private function plugins($name, $location, $args) {
if (!isset($this->plugins, $name, $location, $args)) return false;
# Parse
$location = ($location===0 ? 'before' : 'after');
# Call plugins
$this->plugins->activate($name . ":" . $location, $args);
return true;
}
public function init($public, $version) {
if (!isset($this->settings, $public, $version)) return false;
global $configVersion;
# Update
if ($configVersion!==$version)
if (!update($version)) exit('Error: Updating the database failed!');
# Return settings
$return['config'] = $this->settings;
unset($return['config']['password']);
# No login
if ($this->settings['username']===''&&$this->settings['password']==='') $return['config']['login'] = false;
else $return['config']['login'] = true;
if ($public===false) {
# Logged in
$return['loggedIn'] = true;
} else {
# Unset unused vars
unset($return['config']['username']);
unset($return['config']['thumbQuality']);
unset($return['config']['sorting']);
unset($return['config']['dropboxKey']);
unset($return['config']['login']);
# Logged out
$return['loggedIn'] = false;
}
return $return;
}
public function login($username, $password) {
if (!isset($this->settings, $username, $password)) return false;
# Check login
if ($username===$this->settings['username']&&$password===$this->settings['password']) {
$_SESSION['login'] = true;
return true;
}
# No login
if ($this->settings['username']===''&&$this->settings['password']==='') {
$_SESSION['login'] = true;
return true;
}
return false;
}
public function logout() {
session_destroy();
return true;
}
}
?>

View File

@ -1,69 +0,0 @@
<?php
/**
* @name Session Module
* @author Philipp Maurer
* @author Tobias Reich
* @copyright 2014 by Philipp Maurer, Tobias Reich
*/
if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
function init($mode, $version) {
global $settings, $configVersion;
// Update
if ($configVersion!==$version)
if (!update($version)) exit('Error: Updating the database failed!');
$return['config'] = $settings;
unset($return['config']['password']);
// No login
if ($settings['username']===''&&$settings['password']==='') $return['config']['login'] = false;
else $return['config']['login'] = true;
if ($mode==='admin') {
$return['loggedIn'] = true;
} else {
unset($return['config']['username']);
unset($return['config']['thumbQuality']);
unset($return['config']['sorting']);
unset($return['config']['dropboxKey']);
unset($return['config']['login']);
$return['loggedIn'] = false;
}
return $return;
}
function login($username, $password) {
global $database, $settings;
// Check login
if ($username===$settings['username']&&$password===$settings['password']) {
$_SESSION['login'] = true;
return true;
}
// No login
if ($settings['username']===''&&$settings['password']==='') {
$_SESSION['login'] = true;
return true;
}
return false;
}
function logout() {
session_destroy();
return true;
}
?>