Alternc  latest
Alternc logiel libre pour l'hébergement
Token.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * Standard Token object for the AlternC API
5  *
6  */
8 
9  const ERR_DATABASE_ERROR = 112001;
10  const ERR_INVALID_ARGUMENT = 112002;
11  const ERR_MISSING_ARGUMENT = 112003;
12  const ERR_INVALID_TOKEN = 112004;
13 
14  /**
15  * AlternC User-Id
16  *
17  * @var int
18  */
19  public $uid;
20 
21  /**
22  * Is this an admin account ?
23  *
24  * @var boolean
25  */
26  public $isAdmin;
27 
28  /**
29  * The Token itself
30  *
31  * @var string
32  */
33  public $token;
34 
35  /**
36  * how long (seconds) is a token valid
37  *
38  * @var int
39  */
40  public $tokenDuration = 2678400; // default is a month
41 
42  /**
43  * initialize a token object
44  * @param options any of the public above
45  * may contain a dbAdapter, in that case create() will be available
46  */
47 
48  public function __construct($options = array()) {
49 
50  if (isset($options["uid"]) && is_int($options["uid"]))
51  $this->uid = $options["uid"];
52 
53  if (isset($options["isAdmin"]) && is_bool($options["isAdmin"]))
54  $this->isAdmin = $options["isAdmin"];
55  }
56 
57  /**
58  * Formats response to json
59  *
60  * @return string
61  */
62  public function toJson() {
63  return json_encode(
64  array("uid" => $this->uid,
65  "isAdmin" => $this->isAdmin,
66  "token" => $this->token)
67  );
68  }
69 
70  /**
71  * Create a new token in the DB for the associated user/admin
72  *
73  * @return string the token (32 chars)
74  */
75  public static function tokenGenerate($options, $db) {
76  if (!($db instanceof PDO)) {
77  throw new \Exception("No DB Object, can't create", self::ERR_DATABASE_ERROR);
78  }
79  if (!isset($options["uid"]) || !isset($options["isAdmin"])) {
80  throw new \Exception("Missing Arguments (uid,isAdmin)", self::ERR_MISSING_ARGUMENT);
81  }
82 
83  $token = new Alternc_Api_Token($options);
84 
85  do {
86  $token->token = $token->tokenRandom();
87  $stmt = $db->prepare("INSERT IGNORE INTO token SET token=?, expire=DATE_ADD(NOW(), INTERVAL ? SECOND), data=?");
88  $stmt->execute(array($token->token, $token->tokenDuration, $token->toJson()));
89  $rows = $stmt->rowCount();
90  } while ($rows == 0); // prevent collisions
91 
92  return $token;
93  }
94 
95  /**
96  * Check and return a token
97  * @param $token string a 32-chars token
98  * @param $db PDO a PDO object for token table access
99  *
100  * @return Alternc_Api_Token object or NULL
101  */
102  public static function tokenGet($token, $db) {
103  if (!($db instanceof PDO)) {
104  throw new \Exception("No DB Object, can't create", self::ERR_DATABASE_ERROR);
105  }
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"));
108  }
109  $stmt = $db->prepare("SELECT * FROM token WHERE token=?");
110  $stmt->execute(array($token));
111  if ($tok = $stmt->fetch(PDO::FETCH_OBJ)) {
112  return new Alternc_Api_Token(json_decode($tok->data, true));
113  }
114  return new Alternc_Api_Response(array("code" => self::ERR_INVALID_TOKEN, "message" => "Invalid token"));
115  }
116 
117  /**
118  * Generate a new random token
119  * @return string
120  */
121  public function tokenRandom() {
122  $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
123  $s = "";
124  for ($i = 0; $i < 32; $i++)
125  $s.=substr($chars, mt_rand(0, 61), 1);
126  return $s;
127  }
128 
129 }
130 
131 // class Alternc_Api_Response
132 
global $db
Definition: bootstrap.php:26
Standard Response object for the AlternC API.
Definition: Response.php:7
Standard Token object for the AlternC API.
Definition: Token.php:7
tokenRandom()
Generate a new random token.
Definition: Token.php:121
static tokenGet($token, $db)
Check and return a token.
Definition: Token.php:102
static tokenGenerate($options, $db)
Create a new token in the DB for the associated user/admin.
Definition: Token.php:75
const ERR_MISSING_ARGUMENT
Definition: Token.php:11
toJson()
Formats response to json.
Definition: Token.php:62
const ERR_DATABASE_ERROR
Definition: Token.php:9
const ERR_INVALID_ARGUMENT
Definition: Token.php:10
const ERR_INVALID_TOKEN
Definition: Token.php:12
__construct($options=array())
initialize a token object
Definition: Token.php:48
$i