2018-06-18 17:05:44 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2018-06-20 13:24:02 -04:00
|
|
|
* @file src/Model/OpenWebAuthToken.php
|
2018-06-18 17:05:44 -04:00
|
|
|
*/
|
|
|
|
namespace Friendica\Model;
|
|
|
|
|
2018-07-20 08:19:26 -04:00
|
|
|
use Friendica\Database\DBA;
|
2018-06-18 17:05:44 -04:00
|
|
|
use Friendica\Util\DateTimeFormat;
|
|
|
|
|
|
|
|
/**
|
2018-06-20 14:11:26 -04:00
|
|
|
* Methods to deal with entries of the 'openwebauth-token' table.
|
2018-06-18 17:05:44 -04:00
|
|
|
*/
|
2018-06-20 13:24:02 -04:00
|
|
|
class OpenWebAuthToken
|
2018-06-18 17:05:44 -04:00
|
|
|
{
|
|
|
|
/**
|
2018-06-20 14:11:26 -04:00
|
|
|
* Create an entry in the 'openwebauth-token' table.
|
2018-07-19 22:15:21 -04:00
|
|
|
*
|
2018-06-18 17:05:44 -04:00
|
|
|
* @param string $type Verify type.
|
|
|
|
* @param int $uid The user ID.
|
|
|
|
* @param string $token
|
|
|
|
* @param string $meta
|
2018-07-19 22:15:21 -04:00
|
|
|
*
|
2018-06-18 17:05:44 -04: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 08:19:26 -04:00
|
|
|
return DBA::insert("openwebauth-token", $fields);
|
2018-06-18 17:05:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-20 14:11:26 -04:00
|
|
|
* Get the "meta" field of an entry in the openwebauth-token table.
|
2018-07-19 22:15:21 -04:00
|
|
|
*
|
2018-06-18 17:05:44 -04:00
|
|
|
* @param string $type Verify type.
|
|
|
|
* @param int $uid The user ID.
|
|
|
|
* @param string $token
|
2018-07-19 22:15:21 -04:00
|
|
|
*
|
2018-06-18 17:05:44 -04: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 08:19:26 -04:00
|
|
|
$entry = DBA::selectFirst("openwebauth-token", ["id", "meta"], $condition);
|
2018-07-21 08:40:21 -04:00
|
|
|
if (DBA::is_result($entry)) {
|
2018-07-20 08:19:26 -04:00
|
|
|
DBA::delete("openwebauth-token", ["id" => $entry["id"]]);
|
2018-06-18 17:05:44 -04:00
|
|
|
|
|
|
|
return $entry["meta"];
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Purge entries of a verify-type older than interval.
|
2018-07-19 22:15:21 -04:00
|
|
|
*
|
2018-06-18 17:05:44 -04: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 08:19:26 -04:00
|
|
|
DBA::delete("openwebauth-token", $condition);
|
2018-06-18 17:05:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|