Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / user / authentication / failure / UserAuthenticationFailure.class.php
1 <?php
2 namespace wcf\data\user\authentication\failure;
3 use wcf\data\DatabaseObject;
4 use wcf\util\UserUtil;
5 use wcf\system\WCF;
6
7 /**
8 * Represents a user authentication failure.
9 *
10 * @author Marcel Werk
11 * @copyright 2001-2014 WoltLab GmbH
12 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
13 * @package com.woltlab.wcf
14 * @subpackage data.user.authentication.failure
15 * @category Community Framework
16 */
17 class UserAuthenticationFailure extends DatabaseObject {
18 /**
19 * @see \wcf\data\DatabaseObject::$databaseTableName
20 */
21 protected static $databaseTableName = 'user_authentication_failure';
22
23 /**
24 * @see \wcf\data\DatabaseObject::$databaseTableIndexName
25 */
26 protected static $databaseTableIndexName = 'failureID';
27
28 /**
29 * Returns the ip address and attempts to convert into IPv4.
30 *
31 * @return string
32 */
33 public function getIpAddress() {
34 return UserUtil::convertIPv6To4($this->ipAddress);
35 }
36
37 /**
38 * Returns the number of authentication failures caused by given ip address.
39 *
40 * @param string $ipAddress
41 * @return boolean
42 */
43 public static function countIPFailures($ipAddress) {
44 $sql = "SELECT COUNT(*) AS count
45 FROM wcf".WCF_N."_user_authentication_failure
46 WHERE ipAddress = ?
47 AND time > ?";
48 $statement = WCF::getDB()->prepareStatement($sql);
49 $statement->execute(array($ipAddress, TIME_NOW - USER_AUTHENTICATION_FAILURE_TIMEOUT));
50 return $statement->fetchColumn();
51 }
52
53 /**
54 * Returns the number of authentication failures for given user account.
55 *
56 * @param integer $userID
57 * @return boolean
58 */
59 public static function countUserFailures($userID) {
60 $sql = "SELECT COUNT(*) AS count
61 FROM wcf".WCF_N."_user_authentication_failure
62 WHERE userID = ?
63 AND time > ?";
64 $statement = WCF::getDB()->prepareStatement($sql);
65 $statement->execute(array($userID, TIME_NOW - USER_AUTHENTICATION_FAILURE_TIMEOUT));
66 return $statement->fetchColumn();
67 }
68 }