]> git.mxchange.org Git - friendica.git/blob - src/Model/OpenWebAuthToken.php
Merge pull request #4836 from Angristan/patch-1
[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\DBM;
9 use Friendica\Util\DateTimeFormat;
10 use dba;
11
12 /**
13  * Methods to deal with entries of the 'openwebauth-token' table.
14  */
15 class OpenWebAuthToken
16 {
17         /**
18          * Create an entry in the 'openwebauth-token' table.
19          * 
20          * @param string $type   Verify type.
21          * @param int    $uid    The user ID.
22          * @param string $token
23          * @param string $meta
24          * 
25          * @return boolean
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          */
48         public static function getMeta($type, $uid, $token)
49         {
50                 $condition = ["type" => $type, "uid" => $uid, "token" => $token];
51
52                 $entry = dba::selectFirst("openwebauth-token", ["id", "meta"], $condition);
53                 if (DBM::is_result($entry)) {
54                         dba::delete("openwebauth-token", ["id" => $entry["id"]]);
55
56                         return $entry["meta"];
57                 }
58                 return false;
59         }
60
61         /**
62          * Purge entries of a verify-type older than interval.
63          * 
64          * @param string $type     Verify type.
65          * @param string $interval SQL compatible time interval
66          */
67         public static function purge($type, $interval)
68         {
69                 $condition = ["`type` = ? AND `created` < ?", $type, DateTimeFormat::utcNow() . " - INTERVAL " . $interval];
70                 dba::delete("openwebauth-token", $condition);
71         }
72
73 }