moved code up one level to eliminate the docs subdirectory
[acontent.git] / include / classes / DAO / OAuthClientTokensDAO.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_client_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 OAuthClientTokensDAO 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($oauth_server_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_client_tokens
52                                       (oauth_server_id,
53                                        token,
54                                        token_type,
55                                        token_secret,
56                                        user_id,
57                                        assign_date
58                                        )
59                                VALUES (".$oauth_server_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         * Delete token row by token, token_type
85         * @access  public
86         * @param   $token, $token_type
87         * @return  true if successful, otherwise, return false
88         * @author  Cindy Qi Li
89         */
90         function deleteByTokenAndType($token, $token_type)
91         {
92             $sql = "DELETE FROM ".TABLE_PREFIX."oauth_client_tokens 
93                      WHERE token = '".$token."'
94                        AND token_type = '".$token_type."'";
95             return $this->execute($sql);
96         }
97
98         /**
99         * Return row by consumer
100         * @access  public
101         * @param   $oauth_server_id, $token_type
102         * @return  table rows
103         * @author  Cindy Qi Li
104         */
105         function get($oauth_server_id, $token_type)
106         {
107             $sql = "SELECT * FROM ".TABLE_PREFIX."oauth_client_tokens 
108                      WHERE oauth_server_id='".$oauth_server_id."'
109                        AND token_type='".$token_type."'";
110             return $this->execute($sql);
111         }
112
113         /**
114         * Return token row by consumer key, token type, token
115         * @access  public
116         * @param   $consumer_key, $token_type, $token
117         * @return  table rows if successful, otherwise, return false
118         * @author  Cindy Qi Li
119         */
120         function getByToken($consumer_key, $token)
121         {
122             $sql = "SELECT * FROM ".TABLE_PREFIX."oauth_client_servers c, ".TABLE_PREFIX."oauth_client_tokens t 
123                      WHERE c.oauth_server_id = t.oauth_server_id
124                        AND c.consumer_key='".$consumer_key."'
125                        AND t.token = '".$token."'";
126             return $this->execute($sql);
127         }
128
129         /**
130         * Return token row by token, token_type
131         * @access  public
132         * @param   $token, $token_type
133         * @return  table rows if successful, otherwise, return false
134         * @author  Cindy Qi Li
135         */
136         function getByTokenAndType($token, $token_type)
137         {
138             $sql = "SELECT * FROM ".TABLE_PREFIX."oauth_client_tokens 
139                      WHERE token = '".$token."'
140                        AND token_type = '".$token_type."'";
141             return $this->execute($sql);
142         }
143
144         /**
145         * Return token row by consumer key, token, nounce
146         * @access  public
147         * @param   $consumer_key, $token, $nounce
148         * @return  table rows if successful, otherwise, return false
149         * @author  Cindy Qi Li
150         */
151         function getByTokenAndNounce($consumer_key, $token, $nonce)
152         {
153             $sql = "SELECT * FROM ".TABLE_PREFIX."oauth_client_servers, c".TABLE_PREFIX."oauth_client_tokens t 
154                      WHERE c.oauth_server_id = t.oauth_server_id
155                        AND c.consumer_key='".$consumer_key."'
156                        AND t.token = '".$token."'
157                        AND t.nounce = '".$nonce."'";
158             return $this->execute($sql);
159         }
160
161 }
162 ?>