50 if (isset($options[
"uid"]) && is_int($options[
"uid"]))
51 $this->uid = $options[
"uid"];
53 if (isset($options[
"isAdmin"]) && is_bool($options[
"isAdmin"]))
54 $this->isAdmin = $options[
"isAdmin"];
64 array(
"uid" => $this->uid,
65 "isAdmin" => $this->isAdmin,
66 "token" => $this->token)
76 if (!(
$db instanceof PDO)) {
77 throw new \Exception(
"No DB Object, can't create", self::ERR_DATABASE_ERROR);
79 if (!isset($options[
"uid"]) || !isset($options[
"isAdmin"])) {
80 throw new \Exception(
"Missing Arguments (uid,isAdmin)", self::ERR_MISSING_ARGUMENT);
87 $stmt =
$db->prepare(
"INSERT IGNORE INTO token SET token=?, expire=DATE_ADD(NOW(), INTERVAL ? SECOND), data=?");
89 $rows = $stmt->rowCount();
103 if (!(
$db instanceof PDO)) {
104 throw new \Exception(
"No DB Object, can't create", self::ERR_DATABASE_ERROR);
106 if (!is_string(
$token) || !preg_match(
"#^[a-zA-Z0-9]{32}$#",
$token)) {
107 return new Alternc_Api_Response(array(
"code" => self::ERR_INVALID_TOKEN,
"message" =>
"Invalid token"));
109 $stmt =
$db->prepare(
"SELECT * FROM token WHERE token=?");
110 $stmt->execute(array(
$token));
111 if ($tok = $stmt->fetch(PDO::FETCH_OBJ)) {
114 return new Alternc_Api_Response(array(
"code" => self::ERR_INVALID_TOKEN,
"message" =>
"Invalid token"));
122 $chars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
124 for (
$i = 0;
$i < 32;
$i++)
125 $s.=substr($chars, mt_rand(0, 61), 1);
Standard Response object for the AlternC API.
Standard Token object for the AlternC API.
tokenRandom()
Generate a new random token.
static tokenGet($token, $db)
Check and return a token.
static tokenGenerate($options, $db)
Create a new token in the DB for the associated user/admin.
const ERR_MISSING_ARGUMENT
toJson()
Formats response to json.
const ERR_INVALID_ARGUMENT
__construct($options=array())
initialize a token object