2018-06-18 21:05:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2018-06-20 17:24:02 +00:00
|
|
|
* @file src/Model/OpenWebAuthToken.php
|
2018-06-18 21:05:44 +00:00
|
|
|
*/
|
|
|
|
namespace Friendica\Model;
|
|
|
|
|
2018-07-20 12:19:26 +00:00
|
|
|
use Friendica\Database\DBA;
|
2018-06-18 21:05:44 +00:00
|
|
|
use Friendica\Util\DateTimeFormat;
|
|
|
|
|
|
|
|
/**
|
2018-06-20 18:11:26 +00:00
|
|
|
* Methods to deal with entries of the 'openwebauth-token' table.
|
2018-06-18 21:05:44 +00:00
|
|
|
*/
|
2018-06-20 17:24:02 +00:00
|
|
|
class OpenWebAuthToken
|
2018-06-18 21:05:44 +00:00
|
|
|
{
|
|
|
|
/**
|
2018-06-20 18:11:26 +00:00
|
|
|
* Create an entry in the 'openwebauth-token' table.
|
2018-07-20 02:15:21 +00:00
|
|
|
*
|
2018-06-18 21:05:44 +00:00
|
|
|
* @param string $type Verify type.
|
|
|
|
* @param int $uid The user ID.
|
|
|
|
* @param string $token
|
|
|
|
* @param string $meta
|
2018-07-20 02:15:21 +00:00
|
|
|
*
|
2018-06-18 21:05:44 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static function create($type, $uid, $token, $meta)
|
|
|
|
{
|
|
|
|
$fields = [
|
|
|
|
"type" => $type,
|
|
|
|
"uid" => $uid,
|
|
|
|
"token" => $token,
|
|
|
|
"meta" => $meta,
|
|
|
|
"created" => DateTimeFormat::utcNow()
|
|
|
|
];
|
2018-07-20 12:19:26 +00:00
|
|
|
return DBA::insert("openwebauth-token", $fields);
|
2018-06-18 21:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-20 18:11:26 +00:00
|
|
|
* Get the "meta" field of an entry in the openwebauth-token table.
|
2018-07-20 02:15:21 +00:00
|
|
|
*
|
2018-06-18 21:05:44 +00:00
|
|
|
* @param string $type Verify type.
|
|
|
|
* @param int $uid The user ID.
|
|
|
|
* @param string $token
|
2018-07-20 02:15:21 +00:00
|
|
|
*
|
2018-06-18 21:05:44 +00:00
|
|
|
* @return string|boolean The meta enry or false if not found.
|
|
|
|
*/
|
|
|
|
public static function getMeta($type, $uid, $token)
|
|
|
|
{
|
|
|
|
$condition = ["type" => $type, "uid" => $uid, "token" => $token];
|
|
|
|
|
2018-07-20 12:19:26 +00:00
|
|
|
$entry = DBA::selectFirst("openwebauth-token", ["id", "meta"], $condition);
|
2018-07-21 12:40:21 +00:00
|
|
|
if (DBA::is_result($entry)) {
|
2018-07-20 12:19:26 +00:00
|
|
|
DBA::delete("openwebauth-token", ["id" => $entry["id"]]);
|
2018-06-18 21:05:44 +00:00
|
|
|
|
|
|
|
return $entry["meta"];
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Purge entries of a verify-type older than interval.
|
2018-07-20 02:15:21 +00:00
|
|
|
*
|
2018-06-18 21:05:44 +00:00
|
|
|
* @param string $type Verify type.
|
|
|
|
* @param string $interval SQL compatible time interval
|
|
|
|
*/
|
|
|
|
public static function purge($type, $interval)
|
|
|
|
{
|
|
|
|
$condition = ["`type` = ? AND `created` < ?", $type, DateTimeFormat::utcNow() . " - INTERVAL " . $interval];
|
2018-07-20 12:19:26 +00:00
|
|
|
DBA::delete("openwebauth-token", $condition);
|
2018-06-18 21:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|