2015-11-20 12:44:21 +00:00
|
|
|
<?php
|
2016-02-03 22:22:56 +00:00
|
|
|
/**
|
|
|
|
* Holds the action code handling class.
|
|
|
|
*
|
|
|
|
* @package Sakura
|
|
|
|
*/
|
|
|
|
|
2015-11-20 12:44:21 +00:00
|
|
|
namespace Sakura;
|
|
|
|
|
|
|
|
/**
|
2016-02-02 21:04:15 +00:00
|
|
|
* Used to generate one-time keys for user automatic user actions e.g. account activation.
|
|
|
|
*
|
2015-11-20 12:44:21 +00:00
|
|
|
* @package Sakura
|
2016-02-02 21:04:15 +00:00
|
|
|
* @author Julian van de Groep <me@flash.moe>
|
2015-11-20 12:44:21 +00:00
|
|
|
*/
|
2015-12-09 20:21:08 +00:00
|
|
|
class ActionCode
|
2015-11-20 12:44:21 +00:00
|
|
|
{
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Generate an Action Code.
|
|
|
|
*
|
|
|
|
* @param string $action The identifier of the action.
|
|
|
|
* @param int $user The user this action code is intended for (leave 0 for universal).
|
|
|
|
*
|
|
|
|
* @return string The action code given to the user.
|
|
|
|
*/
|
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
|
2016-02-18 23:28:44 +00:00
|
|
|
DB::prepare('INSERT INTO `{prefix}actioncodes` (`code_action`, `user_id`, `action_code`) VALUES (:action, :id, :code)')
|
|
|
|
->execute([
|
|
|
|
'action' => $action,
|
|
|
|
'id' => $user,
|
|
|
|
'code' => $code,
|
2016-01-09 21:57:54 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Return the code
|
|
|
|
return $code;
|
2015-11-20 12:44:21 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Validate an action code.
|
|
|
|
*
|
|
|
|
* @param string $action The action identifier.
|
|
|
|
* @param string $code The action code.
|
|
|
|
* @param int $user The id of the user validating this code.
|
|
|
|
* @param bool $invalidate Set if the code should be invalidated once validated.
|
|
|
|
*
|
|
|
|
* @return bool Boolean indicating success.
|
|
|
|
*/
|
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
|
2016-02-18 23:28:44 +00:00
|
|
|
$get = DB::prepare('SELECT * FROM `{prefix}actioncodes` WHERE `code_action` = :code AND `action_code` = :action AND `user_id` = :id');
|
|
|
|
$get->execute([
|
|
|
|
'code' => $action,
|
|
|
|
'action' => $code,
|
|
|
|
'id' => $user,
|
2016-01-09 21:57:54 +00:00
|
|
|
]);
|
2016-02-18 23:28:44 +00:00
|
|
|
$get = $get->rowCount();
|
2016-01-09 21:57:54 +00:00
|
|
|
|
|
|
|
// Invalidate the code if requested
|
|
|
|
if ($invalidate) {
|
|
|
|
self::invalidate($code);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the result
|
2016-02-18 23:28:44 +00:00
|
|
|
return $get > 0;
|
2015-11-20 12:44:21 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Make a code invalid.
|
|
|
|
*
|
|
|
|
* @param string $code The code that is being invalidated.
|
|
|
|
*/
|
2016-01-09 21:57:54 +00:00
|
|
|
public static function invalidate($code)
|
2015-11-20 12:44:21 +00:00
|
|
|
{
|
2016-02-18 23:28:44 +00:00
|
|
|
DB::prepare('DELETE FROM `{prefix}actioncodes` WHERE `code_action` = :code')
|
|
|
|
->execute([
|
|
|
|
'code' => $code,
|
2016-01-09 21:57:54 +00:00
|
|
|
]);
|
2015-11-20 12:44:21 +00:00
|
|
|
}
|
|
|
|
}
|