lychee/php/modules/Session.php

148 lines
3.1 KiB
PHP
Raw Normal View History

2014-04-04 19:10:32 +00:00
<?php
###
2014-10-21 11:45:11 +00:00
# @name Session Module
2015-02-01 21:08:37 +00:00
# @copyright 2015 by Tobias Reich
2014-04-04 19:10:32 +00:00
###
if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
2014-04-04 19:27:10 +00:00
class Session extends Module {
2014-04-04 19:10:32 +00:00
2014-04-04 19:27:10 +00:00
private $settings = null;
2014-04-04 19:10:32 +00:00
public function __construct($plugins, $settings) {
# Init vars
$this->plugins = $plugins;
$this->settings = $settings;
return true;
}
public function init($database, $dbName, $public) {
2014-04-04 19:10:32 +00:00
# Check dependencies
self::dependencies(isset($this->settings, $public));
2014-04-04 19:10:32 +00:00
2014-04-04 19:12:49 +00:00
# Call plugins
$this->plugins(__METHOD__, 0, func_get_args());
2014-04-04 19:10:32 +00:00
# Return settings
$return['config'] = $this->settings;
2015-04-17 20:50:35 +00:00
2016-01-19 10:03:28 +00:00
# Path to Lychee for the server-import dialog
$return['config']['location'] = LYCHEE;
2015-04-17 20:50:35 +00:00
# Remove username and password from response
unset($return['config']['username']);
2014-04-04 19:10:32 +00:00
unset($return['config']['password']);
# Remove identifier from response
unset($return['config']['identifier']);
2015-01-23 20:00:27 +00:00
# Check if login credentials exist and login if they don't
if ($this->noLogin()===true) {
$public = false;
$return['config']['login'] = false;
} else {
$return['config']['login'] = true;
}
2014-04-04 19:10:32 +00:00
if ($public===false) {
# Logged in
2015-03-06 22:29:55 +00:00
$return['status'] = LYCHEE_STATUS_LOGGEDIN;
2014-04-04 19:10:32 +00:00
} else {
2015-03-06 22:29:55 +00:00
# Logged out
$return['status'] = LYCHEE_STATUS_LOGGEDOUT;
2014-04-04 19:10:32 +00:00
# Unset unused vars
unset($return['config']['thumbQuality']);
2015-05-14 15:20:33 +00:00
unset($return['config']['sortingAlbums']);
unset($return['config']['sortingPhotos']);
2014-04-04 19:10:32 +00:00
unset($return['config']['dropboxKey']);
unset($return['config']['login']);
unset($return['config']['location']);
2015-05-14 15:20:33 +00:00
unset($return['config']['imagick']);
unset($return['config']['medium']);
unset($return['config']['plugins']);
2014-04-04 19:10:32 +00:00
}
2014-04-04 19:12:49 +00:00
# Call plugins
$this->plugins(__METHOD__, 1, func_get_args());
2014-04-04 19:10:32 +00:00
return $return;
}
public function login($username, $password) {
# Check dependencies
2014-06-25 12:50:49 +00:00
self::dependencies(isset($this->settings, $username, $password));
2014-04-04 19:10:32 +00:00
2014-04-04 19:12:49 +00:00
# Call plugins
$this->plugins(__METHOD__, 0, func_get_args());
$username = crypt($username, $this->settings['username']);
$password = crypt($password, $this->settings['password']);
2014-04-04 19:10:32 +00:00
# Check login with crypted hash
if ($this->settings['username']===$username&&
$this->settings['password']===$password) {
$_SESSION['login'] = true;
$_SESSION['identifier'] = $this->settings['identifier'];
return true;
}
2014-04-04 19:10:32 +00:00
# No login
if ($this->noLogin()===true) return true;
2014-04-04 19:10:32 +00:00
2014-04-04 19:12:49 +00:00
# Call plugins
$this->plugins(__METHOD__, 1, func_get_args());
2014-04-04 19:10:32 +00:00
return false;
}
2015-01-23 20:00:27 +00:00
private function noLogin() {
# Check dependencies
self::dependencies(isset($this->settings));
# Check if login credentials exist and login if they don't
if ($this->settings['username']===''&&
$this->settings['password']==='') {
$_SESSION['login'] = true;
$_SESSION['identifier'] = $this->settings['identifier'];
return true;
2015-01-23 20:00:27 +00:00
}
return false;
}
2014-04-04 19:10:32 +00:00
public function logout() {
2014-04-04 19:12:49 +00:00
# Call plugins
$this->plugins(__METHOD__, 0, func_get_args());
$_SESSION['login'] = null;
$_SESSION['identifier'] = null;
2014-04-04 19:10:32 +00:00
session_destroy();
2014-04-04 19:12:49 +00:00
# Call plugins
$this->plugins(__METHOD__, 1, func_get_args());
2014-04-04 19:10:32 +00:00
return true;
}
}
?>