2015-11-20 12:44:21 +00:00
|
|
|
<?php
|
|
|
|
namespace Sakura;
|
|
|
|
|
|
|
|
/**
|
2015-12-09 20:21:08 +00:00
|
|
|
* Class ActionCode
|
2015-11-20 12:44:21 +00:00
|
|
|
* @package Sakura
|
|
|
|
*/
|
2015-12-09 20:21:08 +00:00
|
|
|
class ActionCode
|
2015-11-20 12:44:21 +00:00
|
|
|
{
|
|
|
|
// Generating an action code
|
2016-01-09 21:57:54 +00:00
|
|
|
public static function generate($action, $user = 0)
|
2015-11-20 12:44:21 +00:00
|
|
|
{
|
2016-01-09 21:57:54 +00:00
|
|
|
// Generate a code
|
|
|
|
$code = uniqid();
|
|
|
|
|
|
|
|
// Insert it
|
|
|
|
Database::insert('actioncodes', [
|
|
|
|
'code_action' => $action,
|
|
|
|
'user_id' => $user,
|
|
|
|
'action_code' => $code,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Return the code
|
|
|
|
return $code;
|
2015-11-20 12:44:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Checking if a code is still valid
|
2016-01-09 21:57:54 +00:00
|
|
|
public static function validate($action, $code, $user = 0, $invalidate = true)
|
2015-11-20 12:44:21 +00:00
|
|
|
{
|
2016-01-09 21:57:54 +00:00
|
|
|
// Fetch the code from the db
|
|
|
|
$get = Database::count('actioncodes', [
|
|
|
|
'code_action' => [$action, '='],
|
|
|
|
'action_code' => [$code, '='],
|
|
|
|
'user_id' => [$user, '='],
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Invalidate the code if requested
|
|
|
|
if ($invalidate) {
|
|
|
|
self::invalidate($code);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the result
|
|
|
|
return $get[0] > 0;
|
2015-11-20 12:44:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make a code invalid
|
2016-01-09 21:57:54 +00:00
|
|
|
public static function invalidate($code)
|
2015-11-20 12:44:21 +00:00
|
|
|
{
|
2016-01-09 21:57:54 +00:00
|
|
|
Database::delete('actioncodes', [
|
2016-01-10 18:24:47 +00:00
|
|
|
'code_action' => [$code, '='],
|
2016-01-09 21:57:54 +00:00
|
|
|
]);
|
2015-11-20 12:44:21 +00:00
|
|
|
}
|
|
|
|
}
|