47bf1a70adbd1e1f58ed900a3cbe81fa9d40d8ec
[acontent.git] / docs / include / classes / DAO / OAuthServerTokensDAO.class.php
1 <?php
2 /************************************************************************/
3 /* AContent                                                             */
4 /************************************************************************/
5 /* Copyright (c) 2010                                                   */
6 /* Inclusive Design Institute                                           */
7 /*                                                                      */
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 /************************************************************************/
12
13 /**
14 * DAO for "oauth_server_tokens" table
15 * @access       public
16 * @author       Cindy Qi Li
17 * @package      DAO
18 */
19
20 if (!defined('TR_INCLUDE_PATH')) exit;
21
22 require_once(TR_INCLUDE_PATH. 'classes/DAO/DAO.class.php');
23
24 class OAuthServerTokensDAO extends DAO {
25
26         /**
27          * Create a new token
28          * @access  public
29          * @param   token type
30          *          token
31          *          token secret
32          * @return  token id, if successful
33          *          false and add error into global var $msg, if unsuccessful
34          * @author  Cindy Qi Li
35          */
36         public function Create($consumer_id, $token, $token_type, $token_secret, $user_id)
37         {
38                 global $addslashes, $msg;
39
40                 $missing_fields = array();
41
42                 /* token type check */
43                 if ($token_type <> 'request' && $token_type <> 'access')
44                 {
45                         $msg->addError('INVALID_TOKEN_TYPE');
46                 }
47
48                 if (!$msg->containsErrors())
49                 {
50                         /* insert into the db */
51                         $sql = "INSERT INTO ".TABLE_PREFIX."oauth_server_tokens
52                                       (consumer_id,
53                                        token,
54                                        token_type,
55                                        token_secret,
56                                        user_id,
57                                        assign_date
58                                        )
59                                VALUES (".$consumer_id.",
60                                        '".$token."',
61                                        '".$token_type."',
62                                        '".$token_secret."',
63                                        ".$user_id.",
64                                        now()
65                                       )";
66
67                         if (!$this->execute($sql))
68                         {
69                                 $msg->addError('DB_NOT_UPDATED');
70                                 return false;
71                         }
72                         else
73                         {
74                                 return true;
75                         }
76                 }
77                 else
78                 {
79                         return false;
80                 }
81         }
82
83         /**
84         * Update user_id by token
85         * @access  public
86         * @param   $token, $user_id
87         * @return  true if successful, otherwise, return false
88         * @author  Cindy Qi Li
89         */
90         function updateUserIDByToken($token, $user_id)
91         {
92             global $addslashes;
93             $sql = "UPDATE ".TABLE_PREFIX."oauth_server_tokens 
94                        SET user_id = ".$user_id."
95                      WHERE token = '".$addslashes($token)."'";
96             return $this->execute($sql);
97         }
98
99         /**
100         * Delete token row by token, token_type
101         * @access  public
102         * @param   $token, $token_type
103         * @return  true if successful, otherwise, return false
104         * @author  Cindy Qi Li
105         */
106         function deleteByTokenAndType($token, $token_type)
107         {
108             $sql = "DELETE FROM ".TABLE_PREFIX."oauth_server_tokens 
109                      WHERE token = '".$token."'
110                        AND token_type = '".$token_type."'";
111             return $this->execute($sql);
112         }
113
114         /**
115         * Return row by consumer
116         * @access  public
117         * @param   $consumer_id, $token_type
118         * @return  table rows
119         * @author  Cindy Qi Li
120         */
121         function get($consumer_id, $token_type)
122         {
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);
127         }
128
129         /**
130         * Return token row by consumer key, token type, token
131         * @access  public
132         * @param   $consumer_key, $token_type, $token
133         * @return  table rows if successful, otherwise, return false
134         * @author  Cindy Qi Li
135         */
136         function getByToken($consumer_key, $token)
137         {
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);
143         }
144
145         /**
146         * Return token row by token, token_type
147         * @access  public
148         * @param   $token, $token_type
149         * @return  table rows if successful, otherwise, return false
150         * @author  Cindy Qi Li
151         */
152         function getByTokenAndType($token, $token_type)
153         {
154
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);
159         }
160
161         /**
162         * Return token row by consumer key, token, nounce
163         * @access  public
164         * @param   $consumer_key, $token, $nounce
165         * @return  table rows if successful, otherwise, return false
166         * @author  Cindy Qi Li
167         */
168         function getByTokenAndNounce($consumer_key, $token, $nonce)
169         {
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);
176         }
177
178         /**
179         * Check whether the given token is expired. If expired, return true, otherwise, return false.
180         * @access  public
181         * @param   $token
182         * @return  true if expired, otherwise, return false
183         * @author  Cindy Qi Li
184         */
185         function isTokenExpired($token)
186         {
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);
196
197                 if ((!is_array($row) || $row['now_timestamp'] > $row['expire_timestamp']) && $row['expire_threshold'] != 0) {
198                         return true;
199                 } else {
200                         return false;
201                 }
202         }
203 }
204 ?>