Passed
Push — master ( 1c7137...b5ef5e )
by Pieter van der
03:26 queued 14s
created

Tiqr_UserStorage_Pdo::isBlocked()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 10
c 2
b 0
f 0
dl 0
loc 14
rs 9.2222
cc 6
nc 3
nop 2
1
<?php
2
/**
3
 * This file is part of the tiqr project.
4
 * 
5
 * The tiqr project aims to provide an open implementation for 
6
 * authentication using mobile devices. It was initiated by 
7
 * SURFnet and developed by Egeniq.
8
 *
9
 * More information: http://www.tiqr.org
10
 *
11
 * @author Patrick Honing <[email protected]>
12
 * 
13
 * @package tiqr
14
 *
15
 * @license New BSD License - See LICENSE file for details.
16
 *
17
 * @copyright (C) 2010-2012 SURFnet BV
18
 * 
19
 * Create SQL table (MySQL):
20
 * CREATE TABLE `tiqruser` (`userid` varchar(10) PRIMARY KEY, `displayname` varchar(45),`blocked` int,`loginattempts` int,
21
 * `tmpblockattempts` int,`tmpblocktimestamp` varchar(45) default NULL,`notificationtype` varchar(10),`notificationaddress` varchar(45))
22
 * 
23
 */
24
25
26
/**
27
 * This user storage implementation implements a user storage using PDO.
28
 * It is usable for any database with a PDO driver
29
 * 
30
 * @author Patrick Honing <[email protected]>
31
 */
32
class Tiqr_UserStorage_Pdo extends Tiqr_UserStorage_Abstract
33
{
34
    protected $handle = null;
35
    protected $tablename;
36
    
37
    /**
38
     * Create an instance
39
     * @param array $config
40
     * @param array $secretconfig
41
     */
42
    public function __construct($config, $secretconfig = array())
43
    {
44
        parent::__construct($config, $secretconfig);
45
        $this->tablename = isset($config['table']) ? $config['table'] : 'tiqruser';
46
        try {
47
            $this->handle = new PDO($config['dsn'],$config['username'],$config['password']);
48
        } catch (PDOException $e) {
49
            return false;
50
        }
51
    }
52
53
    public function createUser($userId, $displayName)
54
    {
55
        if ($this->userExists($userId)) {
56
            $sth = $this->handle->prepare("UPDATE ".$this->tablename." SET displayname = ? WHERE userid = ?");
57
        } else {
58
            $sth = $this->handle->prepare("INSERT INTO ".$this->tablename." (displayname,userid) VALUES (?,?)");
59
        }
60
        $sth->execute(array($displayName,$userId));
61
        return $this->userExists($userId);
62
    }
63
    
64
    public function userExists($userId)
65
    {
66
        $sth = $this->handle->prepare("SELECT userid FROM ".$this->tablename." WHERE userid = ?");
67
        $sth->execute(array($userId));
68
        return $sth->fetchColumn();
69
    }
70
    
71
    public function getDisplayName($userId)
72
    {
73
        $sth = $this->handle->prepare("SELECT displayname FROM ".$this->tablename." WHERE userid = ?");
74
        $sth->execute(array($userId));
75
        return $sth->fetchColumn();
76
    }
77
78
    public function getNotificationType($userId)
79
    {
80
        $sth = $this->handle->prepare("SELECT notificationtype FROM ".$this->tablename." WHERE userid = ?");
81
        $sth->execute(array($userId));
82
        return $sth->fetchColumn();
83
    }
84
    
85
    public function setNotificationType($userId, $type)
86
    {
87
        $sth = $this->handle->prepare("UPDATE ".$this->tablename." SET notificationtype = ? WHERE userid = ?");
88
        $sth->execute(array($type,$userId));
89
    }
90
    
91
    public function getNotificationAddress($userId)
92
    {
93
        $sth = $this->handle->prepare("SELECT notificationaddress FROM ".$this->tablename." WHERE userid = ?");
94
        $sth->execute(array($userId));
95
        return $sth->fetchColumn();
96
    }
97
    
98
    public function setNotificationAddress($userId, $address)
99
    {
100
        $sth = $this->handle->prepare("UPDATE ".$this->tablename." SET notificationaddress = ?  WHERE userid = ?");
101
        $sth->execute(array($address,$userId));
102
    }
103
    
104
    public function getLoginAttempts($userId)
105
    {
106
        $sth = $this->handle->prepare("SELECT loginattempts FROM ".$this->tablename." WHERE userid = ?");
107
        $sth->execute(array($userId));
108
        return $sth->fetchColumn();
109
    }
110
    
111
    public function setLoginAttempts($userId, $amount)
112
    {
113
        $sth = $this->handle->prepare("UPDATE ".$this->tablename." SET loginattempts = ? WHERE userid = ?");
114
        $sth->execute(array($amount,$userId));
115
    }
116
    
117
    public function isBlocked($userId, $duration)
118
    {
119
        if ($this->userExists($userId)) {
120
            $sth = $this->handle->prepare("SELECT blocked FROM ".$this->tablename." WHERE userid = ?");
121
            $sth->execute(array($userId));
122
            $blocked = ($sth->fetchColumn() == 1);
123
            $timestamp = $this->getTemporaryBlockTimestamp($userId);
124
            // if not blocked or block is expired, return false
125
            if (!$blocked || (false !== $timestamp && false != $duration && (strtotime($timestamp) + duration * 60) < time())) {
0 ignored issues
show
Bug introduced by
The constant duration was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
126
                return false;
127
            }
128
            return true;
129
        } else {
130
            return false;
131
        }
132
    }
133
    
134
    public function setBlocked($userId, $blocked)
135
    {
136
        $sth = $this->handle->prepare("UPDATE ".$this->tablename." SET blocked = ? WHERE userid = ?");
137
        $sth->execute(array(
138
                ($blocked) ? "1" : "0",
139
                $userId
140
        ));
141
    }
142
    
143
    public function setTemporaryBlockAttempts($userId, $amount) {
144
        $sth = $this->handle->prepare("UPDATE ".$this->tablename." SET tmpblockattempts = ? WHERE userid = ?");
145
        $sth->execute(array($amount,$userId));
146
    }
147
    
148
    public function getTemporaryBlockAttempts($userId) {
149
        if ($this->userExists($userId)) {
150
            $sth = $this->handle->prepare("SELECT tmpblockattempts FROM ".$this->tablename." WHERE userid = ?");
151
            $sth->execute(array($userId));
152
            return $sth->fetchColumn();
153
        }
154
        return 0;
155
    }
156
    
157
    public function setTemporaryBlockTimestamp($userId, $timestamp)
158
    {
159
        $sth = $this->handle->prepare("UPDATE ".$this->tablename." SET tmpblocktimestamp = ? WHERE userid = ?");
160
        $sth->execute(array($timestamp,$userId));
161
    }
162
            
163
    public function getTemporaryBlockTimestamp($userId)
164
    {
165
        if ($this->userExists($userId)) {
166
            $sth = $this->handle->prepare("SELECT tmpblocktimestamp FROM ".$this->tablename." WHERE userid = ?");
167
            $sth->execute(array($userId));
168
            $timestamp = $sth->fetchColumn(); 
169
            if (null !== $timestamp) {
170
                return $timestamp;
171
            }
172
        }
173
        return false;
174
    }
175
    
176
}
177