]> git.mxchange.org Git - friendica.git/blob - src/Model/OpenWebAuthToken.php
Rename DBM method calls to DBA method calls
[friendica.git] / src / Model / OpenWebAuthToken.php
1 <?php
2
3 /**
4  * @file src/Model/OpenWebAuthToken.php
5  */
6 namespace Friendica\Model;
7
8 use Friendica\Database\DBA;
9 use Friendica\Util\DateTimeFormat;
10
11 /**
12  * Methods to deal with entries of the 'openwebauth-token' table.
13  */
14 class OpenWebAuthToken
15 {
16         /**
17          * Create an entry in the 'openwebauth-token' table.
18          *
19          * @param string $type   Verify type.
20          * @param int    $uid    The user ID.
21          * @param string $token
22          * @param string $meta
23          *
24          * @return boolean
25          */
26         public static function create($type, $uid, $token, $meta)
27         {
28                 $fields = [
29                         "type" => $type,
30                         "uid" => $uid,
31                         "token" => $token,
32                         "meta" => $meta,
33                         "created" => DateTimeFormat::utcNow()
34                 ];
35                 return DBA::insert("openwebauth-token", $fields);
36         }
37
38         /**
39          * Get the "meta" field of an entry in the openwebauth-token table.
40          *
41          * @param string $type   Verify type.
42          * @param int    $uid    The user ID.
43          * @param string $token
44          *
45          * @return string|boolean The meta enry or false if not found.
46          */
47         public static function getMeta($type, $uid, $token)
48         {
49                 $condition = ["type" => $type, "uid" => $uid, "token" => $token];
50
51                 $entry = DBA::selectFirst("openwebauth-token", ["id", "meta"], $condition);
52                 if (DBA::is_result($entry)) {
53                         DBA::delete("openwebauth-token", ["id" => $entry["id"]]);
54
55                         return $entry["meta"];
56                 }
57                 return false;
58         }
59
60         /**
61          * Purge entries of a verify-type older than interval.
62          *
63          * @param string $type     Verify type.
64          * @param string $interval SQL compatible time interval
65          */
66         public static function purge($type, $interval)
67         {
68                 $condition = ["`type` = ? AND `created` < ?", $type, DateTimeFormat::utcNow() . " - INTERVAL " . $interval];
69                 DBA::delete("openwebauth-token", $condition);
70         }
71
72 }