Update copyright
[friendica.git/.git] / src / Model / OpenWebAuthToken.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Model;
23
24 use Friendica\Database\DBA;
25 use Friendica\Util\DateTimeFormat;
26
27 /**
28  * Methods to deal with entries of the 'openwebauth-token' table.
29  */
30 class OpenWebAuthToken
31 {
32         /**
33          * Create an entry in the 'openwebauth-token' table.
34          *
35          * @param string $type Verify type.
36          * @param int    $uid  The user ID.
37          * @param string $token
38          * @param string $meta
39          *
40          * @return boolean
41          * @throws \Exception
42          */
43         public static function create($type, $uid, $token, $meta)
44         {
45                 $fields = [
46                         "type" => $type,
47                         "uid" => $uid,
48                         "token" => $token,
49                         "meta" => $meta,
50                         "created" => DateTimeFormat::utcNow()
51                 ];
52                 return DBA::insert("openwebauth-token", $fields);
53         }
54
55         /**
56          * Get the "meta" field of an entry in the openwebauth-token table.
57          *
58          * @param string $type Verify type.
59          * @param int    $uid  The user ID.
60          * @param string $token
61          *
62          * @return string|boolean The meta enry or false if not found.
63          * @throws \Exception
64          */
65         public static function getMeta($type, $uid, $token)
66         {
67                 $condition = ["type" => $type, "uid" => $uid, "token" => $token];
68
69                 $entry = DBA::selectFirst("openwebauth-token", ["id", "meta"], $condition);
70                 if (DBA::isResult($entry)) {
71                         DBA::delete("openwebauth-token", ["id" => $entry["id"]]);
72
73                         return $entry["meta"];
74                 }
75                 return false;
76         }
77
78         /**
79          * Purge entries of a verify-type older than interval.
80          *
81          * @param string $type     Verify type.
82          * @param string $interval SQL compatible time interval
83          * @throws \Exception
84          */
85         public static function purge($type, $interval)
86         {
87                 $condition = ["`type` = ? AND `created` < ?", $type, DateTimeFormat::utcNow() . " - INTERVAL " . $interval];
88                 DBA::delete("openwebauth-token", $condition);
89         }
90
91 }