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)
93 $sql = "UPDATE ".TABLE_PREFIX."oauth_server_tokens
94 SET user_id = ".$user_id."
95 WHERE token = '".$addslashes($token)."'";
96 return $this->execute($sql);
100 * Delete token row by token, token_type
102 * @param $token, $token_type
103 * @return true if successful, otherwise, return false
104 * @author Cindy Qi Li
106 function deleteByTokenAndType($token, $token_type)
108 $sql = "DELETE FROM ".TABLE_PREFIX."oauth_server_tokens
109 WHERE token = '".$token."'
110 AND token_type = '".$token_type."'";
111 return $this->execute($sql);
115 * Return row by consumer
117 * @param $consumer_id, $token_type
119 * @author Cindy Qi Li
121 function get($consumer_id, $token_type)
123 $sql = "SELECT * FROM ".TABLE_PREFIX."oauth_server_tokens
124 WHERE consumer_id='".$consumer_id."'
125 AND token_type='".$token_type."'";
126 return $this->execute($sql);
130 * Return token row by consumer key, token type, token
132 * @param $consumer_key, $token_type, $token
133 * @return table rows if successful, otherwise, return false
134 * @author Cindy Qi Li
136 function getByToken($consumer_key, $token)
138 $sql = "SELECT * FROM ".TABLE_PREFIX."oauth_server_consumers c, ".TABLE_PREFIX."oauth_server_tokens t
139 WHERE c.consumer_id = t.consumer_id
140 AND c.consumer_key='".$consumer_key."'
141 AND t.token = '".$token."'";
142 return $this->execute($sql);
146 * Return token row by token, token_type
148 * @param $token, $token_type
149 * @return table rows if successful, otherwise, return false
150 * @author Cindy Qi Li
152 function getByTokenAndType($token, $token_type)
155 $sql = "SELECT * FROM ".TABLE_PREFIX."oauth_server_tokens
156 WHERE token = '".addslashes($token)."'
157 AND token_type = '".addslashes($token_type)."'";
158 return $this->execute($sql);
162 * Return token row by consumer key, token, nounce
164 * @param $consumer_key, $token, $nounce
165 * @return table rows if successful, otherwise, return false
166 * @author Cindy Qi Li
168 function getByTokenAndNounce($consumer_key, $token, $nonce)
170 $sql = "SELECT * FROM ".TABLE_PREFIX."oauth_server_consumers, c".TABLE_PREFIX."oauth_server_tokens t
171 WHERE c.consumer_id = t.consumer_id
172 AND c.consumer_key='".$consumer_key."'
173 AND t.token = '".$token."'
174 AND t.nounce = '".$nonce."'";
175 return $this->execute($sql);
179 * Check whether the given token is expired. If expired, return true, otherwise, return false.
182 * @return true if expired, otherwise, return false
183 * @author Cindy Qi Li
185 function isTokenExpired($token)
187 $sql = "SELECT unix_timestamp(now()) now_timestamp,
188 osc.expire_threshold,
189 unix_timestamp(addtime(ost.assign_date, sec_to_time(osc.expire_threshold))) expire_timestamp
190 FROM ".TABLE_PREFIX."oauth_server_consumers osc, ".TABLE_PREFIX."oauth_server_tokens ost
191 WHERE osc.consumer_id=ost.consumer_id
192 AND ost.token='".$token."'
193 AND ost.token_type='access'
194 ORDER BY ost.assign_date DESC";
195 $row = $this->execute($sql);
197 if ((!is_array($row) || $row['now_timestamp'] > $row['expire_timestamp']) && $row['expire_threshold'] != 0) {