2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2010 */
6 /* Inclusive Design Institute */
8 /* This program is free software. You can redistribute it and/or */
9 /* modify it under the terms of the GNU General Public License */
10 /* as published by the Free Software Foundation. */
11 /************************************************************************/
14 * DAO for "oauth_server_tokens" table
20 if (!defined('TR_INCLUDE_PATH')) exit;
22 require_once(TR_INCLUDE_PATH. 'classes/DAO/DAO.class.php');
24 class OAuthServerTokensDAO extends DAO {
32 * @return token id, if successful
33 * false and add error into global var $msg, if unsuccessful
36 public function Create($consumer_id, $token, $token_type, $token_secret, $user_id)
38 global $addslashes, $msg;
40 $missing_fields = array();
42 /* token type check */
43 if ($token_type <> 'request' && $token_type <> 'access')
45 $msg->addError('INVALID_TOKEN_TYPE');
48 if (!$msg->containsErrors())
50 /* insert into the db */
51 $sql = "INSERT INTO ".TABLE_PREFIX."oauth_server_tokens
59 VALUES (".$consumer_id.",
67 if (!$this->execute($sql))
69 $msg->addError('DB_NOT_UPDATED');
84 * Update user_id by token
86 * @param $token, $user_id
87 * @return true if successful, otherwise, return false
90 function updateUserIDByToken($token, $user_id)
92 $sql = "UPDATE ".TABLE_PREFIX."oauth_server_tokens
93 SET user_id = ".$user_id."
94 WHERE token = '".$token."'";
95 return $this->execute($sql);
99 * Delete token row by token, token_type
101 * @param $token, $token_type
102 * @return true if successful, otherwise, return false
103 * @author Cindy Qi Li
105 function deleteByTokenAndType($token, $token_type)
107 $sql = "DELETE FROM ".TABLE_PREFIX."oauth_server_tokens
108 WHERE token = '".$token."'
109 AND token_type = '".$token_type."'";
110 return $this->execute($sql);
114 * Return row by consumer
116 * @param $consumer_id, $token_type
118 * @author Cindy Qi Li
120 function get($consumer_id, $token_type)
122 $sql = "SELECT * FROM ".TABLE_PREFIX."oauth_server_tokens
123 WHERE consumer_id='".$consumer_id."'
124 AND token_type='".$token_type."'";
125 return $this->execute($sql);
129 * Return token row by consumer key, token type, token
131 * @param $consumer_key, $token_type, $token
132 * @return table rows if successful, otherwise, return false
133 * @author Cindy Qi Li
135 function getByToken($consumer_key, $token)
137 $sql = "SELECT * FROM ".TABLE_PREFIX."oauth_server_consumers c, ".TABLE_PREFIX."oauth_server_tokens t
138 WHERE c.consumer_id = t.consumer_id
139 AND c.consumer_key='".$consumer_key."'
140 AND t.token = '".$token."'";
141 return $this->execute($sql);
145 * Return token row by token, token_type
147 * @param $token, $token_type
148 * @return table rows if successful, otherwise, return false
149 * @author Cindy Qi Li
151 function getByTokenAndType($token, $token_type)
153 $sql = "SELECT * FROM ".TABLE_PREFIX."oauth_server_tokens
154 WHERE token = '".$token."'
155 AND token_type = '".$token_type."'";
156 return $this->execute($sql);
160 * Return token row by consumer key, token, nounce
162 * @param $consumer_key, $token, $nounce
163 * @return table rows if successful, otherwise, return false
164 * @author Cindy Qi Li
166 function getByTokenAndNounce($consumer_key, $token, $nonce)
168 $sql = "SELECT * FROM ".TABLE_PREFIX."oauth_server_consumers, c".TABLE_PREFIX."oauth_server_tokens t
169 WHERE c.consumer_id = t.consumer_id
170 AND c.consumer_key='".$consumer_key."'
171 AND t.token = '".$token."'
172 AND t.nounce = '".$nonce."'";
173 return $this->execute($sql);
177 * Check whether the given token is expired. If expired, return true, otherwise, return false.
180 * @return true if expired, otherwise, return false
181 * @author Cindy Qi Li
183 function isTokenExpired($token)
185 $sql = "SELECT unix_timestamp(now()) now_timestamp,
186 osc.expire_threshold,
187 unix_timestamp(addtime(ost.assign_date, sec_to_time(osc.expire_threshold))) expire_timestamp
188 FROM ".TABLE_PREFIX."oauth_server_consumers osc, ".TABLE_PREFIX."oauth_server_tokens ost
189 WHERE osc.consumer_id=ost.consumer_id
190 AND ost.token='".$token."'
191 AND ost.token_type='access'
192 ORDER BY ost.assign_date DESC";
193 $row = $this->execute($sql);
195 if ((!is_array($row) || $row['now_timestamp'] > $row['expire_timestamp']) && $row['expire_threshold'] != 0) {