1b9ae968fe498839fdccb8737d0671204864f89f
[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             $sql = "UPDATE ".TABLE_PREFIX."oauth_server_tokens 
93                        SET user_id = ".$user_id."
94                      WHERE token = '".$token."'";
95             return $this->execute($sql);
96         }
97
98         /**
99         * Delete token row by token, token_type
100         * @access  public
101         * @param   $token, $token_type
102         * @return  true if successful, otherwise, return false
103         * @author  Cindy Qi Li
104         */
105         function deleteByTokenAndType($token, $token_type)
106         {
107             $sql = "DELETE FROM ".TABLE_PREFIX."oauth_server_tokens 
108                      WHERE token = '".$token."'
109                        AND token_type = '".$token_type."'";
110             return $this->execute($sql);
111         }
112
113         /**
114         * Return row by consumer
115         * @access  public
116         * @param   $consumer_id, $token_type
117         * @return  table rows
118         * @author  Cindy Qi Li
119         */
120         function get($consumer_id, $token_type)
121         {
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);
126         }
127
128         /**
129         * Return token row by consumer key, token type, token
130         * @access  public
131         * @param   $consumer_key, $token_type, $token
132         * @return  table rows if successful, otherwise, return false
133         * @author  Cindy Qi Li
134         */
135         function getByToken($consumer_key, $token)
136         {
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);
142         }
143
144         /**
145         * Return token row by token, token_type
146         * @access  public
147         * @param   $token, $token_type
148         * @return  table rows if successful, otherwise, return false
149         * @author  Cindy Qi Li
150         */
151         function getByTokenAndType($token, $token_type)
152         {
153             $sql = "SELECT * FROM ".TABLE_PREFIX."oauth_server_tokens 
154                      WHERE token = '".$token."'
155                        AND token_type = '".$token_type."'";
156             return $this->execute($sql);
157         }
158
159         /**
160         * Return token row by consumer key, token, nounce
161         * @access  public
162         * @param   $consumer_key, $token, $nounce
163         * @return  table rows if successful, otherwise, return false
164         * @author  Cindy Qi Li
165         */
166         function getByTokenAndNounce($consumer_key, $token, $nonce)
167         {
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);
174         }
175
176         /**
177         * Check whether the given token is expired. If expired, return true, otherwise, return false.
178         * @access  public
179         * @param   $token
180         * @return  true if expired, otherwise, return false
181         * @author  Cindy Qi Li
182         */
183         function isTokenExpired($token)
184         {
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);
194
195                 if ((!is_array($row) || $row['now_timestamp'] > $row['expire_timestamp']) && $row['expire_threshold'] != 0) {
196                         return true;
197                 } else {
198                         return false;
199                 }
200         }
201 }
202 ?>