enwikipedia-acc /
waca
| 1 | <?php |
||
| 2 | /****************************************************************************** |
||
| 3 | * Wikipedia Account Creation Assistance tool * |
||
| 4 | * ACC Development Team. Please see team.json for a list of contributors. * |
||
| 5 | * * |
||
| 6 | * This is free and unencumbered software released into the public domain. * |
||
| 7 | * Please see LICENSE.md for the full licencing statement. * |
||
| 8 | ******************************************************************************/ |
||
| 9 | |||
| 10 | namespace Waca\DataObjects; |
||
| 11 | |||
| 12 | use DateTimeImmutable; |
||
| 13 | use Exception; |
||
| 14 | use Waca\DataObject; |
||
| 15 | use Waca\PdoDatabase; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * AntiSpoofCache data object |
||
| 19 | */ |
||
| 20 | class AntiSpoofCache extends DataObject |
||
| 21 | { |
||
| 22 | /** @var string */ |
||
| 23 | protected $username; |
||
| 24 | /** @var string */ |
||
| 25 | protected $data; |
||
| 26 | /** @var string */ |
||
| 27 | protected $timestamp; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @param string $username |
||
| 31 | * @param PdoDatabase $database |
||
| 32 | * |
||
| 33 | * @return AntiSpoofCache|false |
||
| 34 | */ |
||
| 35 | public static function getByUsername($username, PdoDatabase $database) |
||
| 36 | { |
||
| 37 | $statement = $database->prepare(<<<SQL |
||
| 38 | SELECT * |
||
| 39 | FROM antispoofcache |
||
| 40 | WHERE username = :id AND timestamp > date_sub(now(), INTERVAL 3 HOUR) |
||
| 41 | LIMIT 1 |
||
| 42 | SQL |
||
| 43 | ); |
||
| 44 | $statement->bindValue(":id", $username); |
||
| 45 | |||
| 46 | $statement->execute(); |
||
| 47 | |||
| 48 | $resultObject = $statement->fetchObject(get_called_class()); |
||
| 49 | |||
| 50 | if ($resultObject != false) { |
||
| 51 | $resultObject->setDatabase($database); |
||
| 52 | } |
||
| 53 | |||
| 54 | return $resultObject; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @return string |
||
| 59 | */ |
||
| 60 | public function getUsername() |
||
| 61 | { |
||
| 62 | return $this->username; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param string $username |
||
| 67 | */ |
||
| 68 | public function setUsername($username) |
||
| 69 | { |
||
| 70 | $this->username = $username; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @return string |
||
| 75 | */ |
||
| 76 | public function getData() |
||
| 77 | { |
||
| 78 | return $this->data; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param string $data |
||
| 83 | */ |
||
| 84 | public function setData($data) |
||
| 85 | { |
||
| 86 | $this->data = $data; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return DateTimeImmutable |
||
| 91 | */ |
||
| 92 | public function getTimestamp() |
||
| 93 | { |
||
| 94 | return new DateTimeImmutable($this->timestamp); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @throws Exception |
||
| 99 | */ |
||
| 100 | public function save() |
||
| 101 | { |
||
| 102 | if ($this->isNew()) { |
||
| 103 | // insert |
||
| 104 | // clear old data first |
||
| 105 | $this->dbObject->exec("DELETE FROM antispoofcache WHERE timestamp < date_sub(now(), INTERVAL 3 HOUR);"); |
||
| 106 | |||
| 107 | $statement = $this->dbObject->prepare("INSERT INTO antispoofcache (username, data) VALUES (:username, :data);"); |
||
| 108 | $statement->bindValue(":username", $this->username); |
||
| 109 | $statement->bindValue(":data", $this->data); |
||
| 110 | |||
| 111 | if ($statement->execute()) { |
||
| 112 | $this->id = (int)$this->dbObject->lastInsertId(); |
||
| 113 | } |
||
| 114 | else { |
||
| 115 | throw new Exception($statement->errorInfo()); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 116 | } |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 |