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_consumers" table
20 if (!defined('TR_INCLUDE_PATH')) exit;
22 require_once(TR_INCLUDE_PATH. 'classes/DAO/DAO.class.php');
23 require_once(TR_INCLUDE_PATH. 'classes/Utility.class.php');
25 class OAuthServerConsumersDAO extends DAO {
28 * Create a new consumer record
31 * @return consumer id, if successful
32 * false and add error into global var $msg, if unsuccessful
35 public function Create($consumer, $expire_threshold)
37 global $addslashes, $msg;
39 $missing_fields = array();
42 $consumer = $addslashes(trim($consumer));
44 /* login name check */
47 $missing_fields[] = _AT('consumer');
52 $missing_fields = implode(', ', $missing_fields);
53 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
56 if (!$msg->containsErrors())
58 /* insert into the db */
59 $consumer_key = Utility::getRandomStr(16);
60 $consumer_secret = Utility::getRandomStr(16);
62 $sql = "INSERT INTO ".TABLE_PREFIX."oauth_server_consumers
69 VALUES ('".$consumer."',
71 '".$consumer_secret."',
72 ".$expire_threshold.",
76 if (!$this->execute($sql))
78 $msg->addError('DB_NOT_UPDATED');
83 return mysql_insert_id();
93 * Update expire threshold
95 * @param consumer, expire threshold
96 * @return true, if successful
97 * false and add error into global var $msg, if unsuccessful
100 public function updateExpireThreshold($consumer, $expire_threshold)
102 global $addslashes, $msg;
104 $missing_fields = array();
107 $consumer = $addslashes(trim($consumer));
109 /* login name check */
112 $missing_fields[] = _AT('consumer');
117 $missing_fields = implode(', ', $missing_fields);
118 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
121 if (!$msg->containsErrors())
124 $sql = "UPDATE ".TABLE_PREFIX."oauth_server_consumers
125 SET expire_threshold = ".$expire_threshold."
126 WHERE consumer = '".$consumer."'";
128 if (!$this->execute($sql))
130 $msg->addError('DB_NOT_UPDATED');
145 * Return row by consumer ID
147 * @param $consumer_id
149 * @author Cindy Qi Li
151 function get($consumer_id)
153 $sql = "SELECT * FROM ".TABLE_PREFIX."oauth_server_consumers WHERE consumer_id='".$consumer_id."'";
154 $rows = $this->execute($sql);
159 * Return row by consumer
163 * @author Cindy Qi Li
165 function getByConsumer($consumer)
168 $sql = "SELECT * FROM ".TABLE_PREFIX."oauth_server_consumers WHERE consumer='".$addslashes($consumer)."'";
169 return $this->execute($sql);
173 * Return row by consumer key
175 * @param $consumer_key
177 * @author Cindy Qi Li
179 function getByConsumerKey($consumer_key)
181 $sql = "SELECT * FROM ".TABLE_PREFIX."oauth_server_consumers
182 WHERE consumer_key = '".$consumer_key."'";
183 return $this->execute($sql);
187 * Return row by consumer key and secret
189 * @param $consumer_key, $consumer_secret
191 * @author Cindy Qi Li
193 function getByConsumerKeyAndSecret($consumer_key, $consumer_secret)
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);