3
* Gallery - a web based photo album viewer and editor
4
* Copyright (C) 2000-2008 Bharat Mediratta
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or (at
9
* your option) any later version.
11
* This program is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
20
* $Id: Album.php 17321 2007-12-29 07:17:50Z JensT $
24
* Class to log, manipulate, check etc. login attempts.
30
var $attempts; // array('number', 'lasttry')
31
var $maxtries; // Number of login tries after which an account is locked.
32
var $expireTime; // Time in seconds when a lock is removed
33
var $filename; // File where the logins are protocolled
36
* Constructor with defaults.
44
$dir = $gallery->app->albumDir;
46
$this->attempts = array();
48
$this->expireTime = 1 * 60 * 60; // Default 1 hour.
49
$this->filename = "$dir/logins.dat";
53
* Add a login try for a username.
55
* @param string $username
58
function addLoginTry($username) {
59
if(!isset($this->attempts[$username])) {
60
$this->attempts[$username] = array(
66
$this->attempts[$username]['tries']++;
67
$this->attempts[$username]['lasttry'] = time();
72
* Remove all login attemps for a username (or array of usernames)
74
* @param mixed $username
77
function reset($username) {
78
if(is_array($username)) {
79
foreach ($username as $uname) {
85
if(empty($this->attempts[$username])) {
89
unset($this->attempts[$username]);
94
* A username is locked when the number of login attempts is greater
95
* than number of maxtries set in the constructore
97
* @param string $username
101
function userIslocked($username) {
102
if(!isset($this->attempts[$username])) {
105
elseif ($this->attempts[$username]['tries'] < $this->maxtries) {
114
* The lock for a username expires when the expireTime from constructor has went by.
115
* than number of maxtries set in the constructore
117
* @param string $username
121
function lockIsExpired($username) {
122
if (time() - $this->attempts[$username]['lasttry'] > $this->expireTime) {
131
* Checks for every username if the username is locked and expired.
132
* If so, the login attempts are resetted.
134
* After this procedure, the list is saved.
139
if(empty($this->attempts)) {
143
foreach($this->attempts as $username => $values) {
144
if($this->userIslocked($username) && $this->lockIsExpired($username)) {
145
$this->reset($username);
153
* Load the attempts from the disk
158
$tmp = fs_file_get_contents($this->filename);
161
$this->attempts = unserialize($tmp);
163
if (empty($this->attempts)) {
164
$this->attempts = array();
170
* Save the attempts to the disk
172
* @return boolean True on success, false otherwise.
176
$ret = unsafe_serialize($this->attempts, $this->filename);
b'\\ No newline at end of file'