Merge pull request #7207 from nupplaphil/bug/6917-php_warnings
[friendica.git/.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          * @throws \Exception
26          */
27         public static function create($type, $uid, $token, $meta)
28         {
29                 $fields = [
30                         "type" => $type,
31                         "uid" => $uid,
32                         "token" => $token,
33                         "meta" => $meta,
34                         "created" => DateTimeFormat::utcNow()
35                 ];
36                 return DBA::insert("openwebauth-token", $fields);
37         }
38
39         /**
40          * Get the "meta" field of an entry in the openwebauth-token table.
41          *
42          * @param string $type Verify type.
43          * @param int    $uid  The user ID.
44          * @param string $token
45          *
46          * @return string|boolean The meta enry or false if not found.
47          * @throws \Exception
48          */
49         public static function getMeta($type, $uid, $token)
50         {
51                 $condition = ["type" => $type, "uid" => $uid, "token" => $token];
52
53                 $entry = DBA::selectFirst("openwebauth-token", ["id", "meta"], $condition);
54                 if (DBA::isResult($entry)) {
55                         DBA::delete("openwebauth-token", ["id" => $entry["id"]]);
56
57                         return $entry["meta"];
58                 }
59                 return false;
60         }
61
62         /**
63          * Purge entries of a verify-type older than interval.
64          *
65          * @param string $type     Verify type.
66          * @param string $interval SQL compatible time interval
67          * @throws \Exception
68          */
69         public static function purge($type, $interval)
70         {
71                 $condition = ["`type` = ? AND `created` < ?", $type, DateTimeFormat::utcNow() . " - INTERVAL " . $interval];
72                 DBA::delete("openwebauth-token", $condition);
73         }
74
75 }