AC_4897, AC_4898, AC_4899: Multifile uploader fixes.
[acontent.git] / docs / include / classes / DAO / OAuthServerConsumersDAO.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_consumers" 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 require_once(TR_INCLUDE_PATH. 'classes/Utility.class.php');
24
25 class OAuthServerConsumersDAO extends DAO {
26
27         /**
28          * Create a new consumer record
29          * @access  public
30          * @param   consumer
31          * @return  consumer id, if successful
32          *          false and add error into global var $msg, if unsuccessful
33          * @author  Cindy Qi Li
34          */
35         public function Create($consumer, $expire_threshold)
36         {
37                 global $addslashes, $msg;
38
39                 $missing_fields = array();
40
41                 /* email check */
42                 $consumer = $addslashes(trim($consumer));
43
44                 /* login name check */
45                 if ($consumer == '')
46                 {
47                         $missing_fields[] = _AT('consumer');
48                 }
49
50                 if ($missing_fields)
51                 {
52                         $missing_fields = implode(', ', $missing_fields);
53                         $msg->addError(array('EMPTY_FIELDS', $missing_fields));
54                 }
55
56                 if (!$msg->containsErrors())
57                 {
58                         /* insert into the db */
59                         $consumer_key = Utility::getRandomStr(16);
60                         $consumer_secret = Utility::getRandomStr(16);
61                         
62                         $sql = "INSERT INTO ".TABLE_PREFIX."oauth_server_consumers
63                                       (consumer,
64                                        consumer_key,
65                                        consumer_secret,
66                                        expire_threshold,
67                                        create_date
68                                        )
69                                VALUES ('".$consumer."',
70                                        '".$consumer_key."',
71                                        '".$consumer_secret."',
72                                        ".$expire_threshold.",
73                                        now()
74                                       )";
75
76                         if (!$this->execute($sql))
77                         {
78                                 $msg->addError('DB_NOT_UPDATED');
79                                 return false;
80                         }
81                         else
82                         {
83                                 return mysql_insert_id();
84                         }
85                 }
86                 else
87                 {
88                         return false;
89                 }
90         }
91
92         /**
93          * Update expire threshold
94          * @access  public
95          * @param   consumer, expire threshold
96          * @return  true, if successful
97          *          false and add error into global var $msg, if unsuccessful
98          * @author  Cindy Qi Li
99          */
100         public function updateExpireThreshold($consumer, $expire_threshold)
101         {
102                 global $addslashes, $msg;
103
104                 $missing_fields = array();
105
106                 /* email check */
107                 $consumer = $addslashes(trim($consumer));
108
109                 /* login name check */
110                 if ($consumer == '')
111                 {
112                         $missing_fields[] = _AT('consumer');
113                 }
114
115                 if ($missing_fields)
116                 {
117                         $missing_fields = implode(', ', $missing_fields);
118                         $msg->addError(array('EMPTY_FIELDS', $missing_fields));
119                 }
120
121                 if (!$msg->containsErrors())
122                 {
123                         /* update db */
124                         $sql = "UPDATE ".TABLE_PREFIX."oauth_server_consumers
125                                    SET expire_threshold = ".$expire_threshold."
126                                  WHERE consumer = '".$consumer."'";
127
128                         if (!$this->execute($sql))
129                         {
130                                 $msg->addError('DB_NOT_UPDATED');
131                                 return false;
132                         }
133                         else
134                         {
135                                 return true;
136                         }
137                 }
138                 else
139                 {
140                         return false;
141                 }
142         }
143         
144         /**
145         * Return row by consumer ID
146         * @access  public
147         * @param   $consumer_id
148         * @return  table row
149         * @author  Cindy Qi Li
150         */
151         function get($consumer_id)
152         {
153             $sql = "SELECT * FROM ".TABLE_PREFIX."oauth_server_consumers WHERE consumer_id='".$consumer_id."'";
154             $rows = $this->execute($sql);
155             return $rows[0];
156         }
157
158         /**
159         * Return row by consumer
160         * @access  public
161         * @param   $consumer
162         * @return  table row
163         * @author  Cindy Qi Li
164         */
165         function getByConsumer($consumer)
166         {
167             global $addslashes;
168             $sql = "SELECT * FROM ".TABLE_PREFIX."oauth_server_consumers WHERE consumer='".$addslashes($consumer)."'";
169             return $this->execute($sql);
170         }
171
172         /**
173         * Return row by consumer key
174         * @access  public
175         * @param   $consumer_key
176         * @return  table row
177         * @author  Cindy Qi Li
178         */
179         function getByConsumerKey($consumer_key)
180         {
181             $sql = "SELECT * FROM ".TABLE_PREFIX."oauth_server_consumers 
182                      WHERE consumer_key = '".$consumer_key."'";
183             return $this->execute($sql);
184         }
185
186         /**
187         * Return row by consumer key and secret
188         * @access  public
189         * @param   $consumer_key, $consumer_secret
190         * @return  table row
191         * @author  Cindy Qi Li
192         */
193         function getByConsumerKeyAndSecret($consumer_key, $consumer_secret)
194         {
195             $sql = "SELECT * FROM ".TABLE_PREFIX."oauth_server_consumers 
196                      WHERE consumer_key = '".$consumer_key."'
197                        AND consumer_secret = '".$consumer_secret."'";
198             return $this->execute($sql);
199         }
200
201 }
202 ?>