streams/Code/Lib/Verify.php

80 lines
2 KiB
PHP
Raw Normal View History

<?php
2022-02-16 04:08:28 +00:00
namespace Code\Lib;
2021-12-02 23:02:31 +00:00
class Verify
{
2022-08-13 10:57:14 +00:00
/**
* @param $type
* @param $channel_id
* @param $token
* @param $meta
* @return bool
2022-08-13 10:57:14 +00:00
*/
public static function create($type, $channel_id, $token, $meta): bool
2021-12-02 23:02:31 +00:00
{
return (bool) q(
2021-12-03 03:01:39 +00:00
"insert into verify ( vtype, channel, token, meta, created ) values ( '%s', %d, '%s', '%s', '%s' )",
2021-12-02 23:02:31 +00:00
dbesc($type),
intval($channel_id),
dbesc($token),
dbesc($meta),
dbesc(datetime_convert())
);
}
2022-08-13 10:57:14 +00:00
public static function match($type, $channel_id, $token, $meta): bool
2021-12-02 23:02:31 +00:00
{
2021-12-03 03:01:39 +00:00
$r = q(
"select id from verify where vtype = '%s' and channel = %d and token = '%s' and meta = '%s' limit 1",
2021-12-02 23:02:31 +00:00
dbesc($type),
intval($channel_id),
dbesc($token),
dbesc($meta)
);
if ($r) {
2021-12-03 03:01:39 +00:00
q(
"delete from verify where id = %d",
2021-12-02 23:02:31 +00:00
intval($r[0]['id'])
);
return true;
}
return false;
}
public static function get_meta($type, $channel_id, $token)
{
2021-12-03 03:01:39 +00:00
$r = q(
"select id, meta from verify where vtype = '%s' and channel = %d and token = '%s' limit 1",
2021-12-02 23:02:31 +00:00
dbesc($type),
intval($channel_id),
dbesc($token)
);
if ($r) {
2021-12-03 03:01:39 +00:00
q(
"delete from verify where id = %d",
2021-12-02 23:02:31 +00:00
intval($r[0]['id'])
);
return $r[0]['meta'];
}
return false;
}
/**
* @brief Purge entries of a verify-type older than interval.
*
* @param string $type Verify type
* @param string $interval SQL compatible time interval
*/
2022-08-13 10:57:14 +00:00
public static function purge(string $type, string $interval): void
2021-12-02 23:02:31 +00:00
{
2021-12-03 03:01:39 +00:00
q(
"delete from verify where vtype = '%s' and created < ( %s - INTERVAL %s )",
2021-12-02 23:02:31 +00:00
dbesc($type),
db_utcnow(),
db_quoteinterval($interval)
);
}
2021-12-03 03:01:39 +00:00
}