Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.UserToken.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * UserToken class
8  *
9  * This class provides methods to store and manage authentication
10  * tokens with expiration time
11  *
12  * @author Anakeen
13  * @version $Id: Class.UserToken.php,v 1.6 2009/01/16 13:33:00 jerome Exp $
14  * @package FDL
15  * @subpackage
16  */
17 /**
18  */
19 
20 include_once ('Class.DbObj.php');
21 
22 class UserToken extends DbObj
23 {
24  var $Class = '$Id: Class.UserToken.php,v 1.6 2009/01/16 13:33:00 jerome Exp $';
25 
26  var $fields = array(
27  'token',
28  'type',
29  'cdate',
30  'authorid',
31  'userid',
32  'expire',
33  'expendable',
34  'description',
35  'context'
36  );
37 
38  public $token;
39  public $userid;
40  public $authorid;
41  public $expire;
42  public $expendable;
43  public $context;
44  public $cdate;
45  public $description;
46  public $type = "CORE";
47 
48  var $id_fields = array(
49  'token'
50  );
51 
52  var $dbtable = 'usertoken';
53 
54  var $sqlcreate = "
55  CREATE TABLE usertoken (
56  token text NOT NULL PRIMARY KEY,
57  type text,
58  cdate timestamp without time zone,
59  authorid int,
60  userid INT NOT NULL,
61  expire TIMESTAMP NOT NULL,
62  expendable BOOLEAN DEFAULT FALSE,
63  description text,
64  context text
65  );
66  CREATE INDEX usertoken_idx ON usertoken(token);
67  ";
68 
69  var $tokenByteLength = 20; // Token size: 160 bits (equal to SHA1 digest output length)
70  var $expiration = 86400; // 24 hours
71  const INFINITY = "infinity";
72 
73  public function preInsert()
74  {
75  if (is_array($this->context)) {
76  $this->context = serialize($this->context);
77  }
78  $this->cdate = date("Y-m-d H:i:s");
79  $currentUser = getCurrentUser();
80  $this->authorid = ($currentUser !== null) ? $currentUser->id : null;
81  }
82 
83  public function setExpiration($expiration = "")
84  {
85  if ($expiration == "") {
87  }
88  $this->expire = self::getExpirationDate($expiration);
89 
90  return $this->expire;
91  }
92  public static function getExpirationDate($delayInSeconds)
93  {
94  if (preg_match('/^-?infinity$/', $delayInSeconds)) {
95  $expireDate = $delayInSeconds;
96  } else {
97  if (!is_numeric($delayInSeconds)) {
98  return false;
99  }
100  $expireDate = strftime("%Y-%m-%d %H:%M:%S", time() + $delayInSeconds);
101  }
102 
103  return $expireDate;
104  }
105  public function genToken()
106  {
107  $strong = false;
108  $bytes = openssl_random_pseudo_bytes($this->tokenByteLength, $strong);
109  if ($bytes === false || $strong === false) {
110  throw new \Dcp\Exception(sprintf("Unable to get cryptographically strong random bytes from openssl: your system might be broken or too old."));
111  }
112  return bin2hex($bytes);
113  }
114 
115  public function getToken()
116  {
117  if ($this->token == "") {
118  error_log(__CLASS__ . "::" . __FUNCTION__ . " " . "token is not defined.");
119  }
120  return $this->token;
121  }
122 
123  public static function deleteExpired()
124  {
125  $sql = sprintf("DELETE FROM usertoken WHERE expire < now()");
126  simpleQuery('', $sql);
127  }
128 
129  public function preUpdate()
130  {
131  if ($this->token == "") {
132  return "Error: token not set";
133  }
134  if ($this->userid == "") {
135  return "Error: userid not set";
136  }
137  if ($this->expire == "") {
138  return "Error: expire not set";
139  }
140  return '';
141  }
142 }
static deleteExpired()
static getExpirationDate($delayInSeconds)
setExpiration($expiration="")
getCurrentUser()
Definition: Lib.Common.php:250
simpleQuery($dbaccess, $query, &$result=array(), $singlecolumn=false, $singleresult=false, $useStrict=null)
Definition: Lib.Common.php:484
← centre documentaire © anakeen