4 * This file is (c) Copyright 2010 by Olivier BERGER, Madhumita DHAR, Institut TELECOM
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * This program has been developed in the frame of the COCLICO
21 * project with financial support of its funders.
26 // Inspired from examples described in "Creating a OAuth Service
27 // Provider in PHP" by Morten Fangel
28 // (http://sevengoslings.net/~fangel/oauthprovider-sp-guide.html)
30 require_once 'OAuth.php';
33 * OAuthDataStore singleton class to manage tokens, consumers and providers in FusionForge DB
35 * Everything specific to the DB model is handled in this class : no other SQL request should exist outside it
36 * It should be reimplemented for other apps, the rest of the classes being untouched
38 * It will assume that OAuthProvider, OauthAuthzConsumer, OauthAuthzToken and its sub-classes are used
40 * @author Olivier Berger
44 class FFOAuthDataStore extends OAuthDataStore {
46 // Hold an instance of the class
47 private static $instance;
50 * Singleton pattern's method to retrieve the instance
52 public static function singleton()
54 if (!isset(self::$instance)) {
56 self::$instance = new $c;
59 return self::$instance;
63 * Prevent users to clone the instance
65 public function __clone()
67 exit_error('Clone is not allowed.', 'oauthconsumer');
71 * Retrieve values of columns for a provider in the DB provided its id
73 * @param int $p_id ID in the DB
74 * @return array of column values
76 function find_provider_from_id( $p_id ) {
77 $t_provider_table = "plugin_oauthconsumer_provider";
79 $t_result = db_query_params ("SELECT * FROM $t_provider_table WHERE id=$1",
80 array ( (int) $p_id )) ;
81 if (!$t_result || ( db_numrows( $t_result ) < 1 )) {
82 exit_error( "provider not found!", 'oauthconsumer' );
85 $t_row = db_fetch_array( $t_result );
91 * Retrieve a table of columns values for all providers
93 * @return array of arrays of column values
95 function find_all_providers() {
96 $t_provider_table = "plugin_oauthconsumer_provider";
97 $t_result = db_query_params("SELECT * FROM $t_provider_table ORDER BY name ASC", array());
101 while ( $t_row = db_fetch_array( $t_result ) ) {
109 * Retrieve values of columns for a provider in the DB provided its name
111 * @param string $p_provider_name
112 * @return array of column values
114 function find_provider_from_name( $p_provider_name ) {
115 $t_provider_table = "plugin_oauthconsumer_provider";
117 $t_query = "SELECT * FROM $t_provider_table WHERE name = $1";
118 $t_result = db_query_params( $t_query, array( $p_provider_name ) );
120 if ( db_numrows( $t_result ) < 1 ) {
124 $t_row = db_fetch_array( $t_result );
130 * Saves an OauthAuthzprovider to the DB
132 * @param OauthAuthzprovider $provider
133 * @return int the provider ID in the DB
135 public function save_provider($provider) {
136 $t_provider_table = "plugin_oauthconsumer_provider";
138 $provider_id = $provider->get_id();
139 if ( 0 == $provider_id ) { # create
142 $result = db_query_params ("INSERT INTO $t_provider_table".' ( name, description, consumer_key, consumer_secret, request_token_url, authorize_url, access_token_url) VALUES ($1,$2,$3,$4,$5,$6,$7)',
143 array ( $provider->get_name(), $provider->get_description(), $provider->get_consumer_key(), $provider->get_consumer_secret(), $provider->get_request_token_url(), $provider->get_authorize_url(), $provider->get_access_token_url())) ;
145 //$this->setError('Error Adding provider: '.db_error());
149 $provider_id = db_insertid($result, $t_provider_table, 'id' );
154 $t_query = "UPDATE $t_provider_table SET name=$1, description=$2, consumer_key=$3, consumer_secret=$4, request_token_url=$5, authorize_url=$6, access_token_url=$7 WHERE id=$8";
155 db_query_params( $t_query, array ( $provider->get_name(), $provider->get_description(), $provider->get_consumer_key(), $provider->get_consumer_secret(), $provider->get_request_token_url(), $provider->get_authorize_url(), $provider->get_access_token_url(), $provider->get_id()) );
162 * Deletes a provider from the DB
164 * @param int $provider_id
166 public function delete_provider( $provider_id ) {
168 $t_provider_table = "plugin_oauthconsumer_provider";
170 $t_query = "DELETE FROM $t_provider_table WHERE id=$1";
171 $t_result = db_query_params( $t_query, array( (int) $provider_id ) );
183 * Saves an OAuthAccessToken to the DB
185 * @param OAuthAccessToken $token
186 * @return int the token ID in the DB
188 public function save_access_token($token) {
190 $t_token_table = "plugin_oauthconsumer_access_token";
191 $time_stamp = time();
192 $token_id = $token->get_id();
193 if ( 0 == $token_id ) {
195 $t_query = "INSERT INTO $t_token_table ( provider_id, token_key, token_secret, user_id, time_stamp ) VALUES ($1, $2, $3, $4, $5)";
196 $t_result = db_query_params( $t_query, array( $token->get_provider_id(), $token->get_token_key(), $token->get_token_secret(), $token->get_user_id(), $time_stamp ) );
198 $token_id = db_insertid($t_result, $t_token_table, 'id');
201 else { # TODO feature to be added later, with lifetime/limited access feature support
202 //$t_query = "UPDATE $t_token_table SET provider_id=$1, token_key=$2, token_secret=$3, user_id=$4, time_stamp=$4 WHERE id=$5";
203 //db_query_params( $t_query, array( $token->getproviderId(), $token->key, $token->secret, $token->getUserId(), $token->gettime_stamp(), $token->getId() ) );
204 exit_error("The access token already exists and cannot be modified.", 'oauthconsumer');
210 * Retrieve a table of columns values for all access tokens (of a user)
212 * @param int $user_id
213 * @return array of arrays of column values
215 public function find_all_access_tokens($user_id) {
216 $t_token_table = "plugin_oauthconsumer_access_token";
217 if(isset($user_id)||($user_id)) {
218 $t_query = "SELECT * FROM $t_token_table WHERE user_id = $1";
219 $t_result = db_query_params( $t_query, array( (int) $user_id ) );
224 while ( $t_row = db_fetch_array( $t_result ) ) {
232 * Retrieve values of columns for a token in the DB provided its key
234 * @param string $token_key
235 * @return array of column values
237 public function find_access_token_from_key($token_key) {
238 $t_token_table = "plugin_oauthconsumer_access_token";
240 $t_query = "SELECT * FROM $t_token_table WHERE token_key = $1";
241 $t_result = db_query_params( $t_query, array( $token_key ) );
243 if ( db_numrows( $t_result ) < 1 ) {
247 $t_row = db_fetch_array( $t_result );
252 * Retrieve values of columns for a token in the DB provided its id
254 * @param int $token_id
255 * @return array of column values
257 public function find_token_from_id($token_id) {
258 $t_token_table = "plugin_oauthconsumer_access_token";
260 $t_query = "SELECT * FROM $t_token_table WHERE id = $1";
261 $t_result = db_query_params( $t_query, array( (int) $token_id ) );
263 if ( db_numrows( $t_result ) < 1 ) {
267 $t_row = db_fetch_array( $t_result );
272 * Retrieve a table of columns values for all tokens issued for a provider (and a user)
274 * @param int $provider_id
275 * @param int $user_id
276 * @return array of arrays of column values
278 public function find_access_tokens_by_provider($provider_id, $user_id) {
279 $t_token_table = "plugin_oauthconsumer_access_token";
281 if(isset($user_id)) {
282 $t_query = "SELECT * FROM $t_token_table WHERE provider_id = $1 AND user_id = $2";
283 $t_result = db_query_params( $t_query, array( (int) $provider_id, (int) $user_id ) );
288 while ( $t_row = db_fetch_array( $t_result ) ) {
296 * Deletes an access token from the DB
298 * @param string $token_type
299 * @param int $token_id
301 function delete_access_token($token_id) {
302 $t_token_table = "plugin_oauthconsumer_access_token";
304 $t_query = "DELETE FROM $t_token_table WHERE id=$1";
305 $t_result = db_query_params( $t_query, array( (int) $token_id ) );
317 * Saves an OAuthResource to the DB
319 * @param OAuthResource $resource
320 * @return int the resource ID in the DB
322 public function save_oauth_resource($resource) {
324 $t_resource_table = "plugin_oauthconsumer_resource";
325 $id = $resource->get_id();
326 if ( 0 == $id ) { # create
329 $result = db_query_params ("INSERT INTO $t_resource_table".' ( url, provider_id, http_method) VALUES ($1,$2,$3)',
330 array ( $resource->get_url(), $resource->get_provider_id(), $resource->get_http_method())) ;
335 $resource_id = db_insertid($result, $t_resource_table, 'id' );
340 $t_query = "UPDATE $t_resource_table SET url=$1, provider_id=$2, http_method=$3 WHERE id=$4";
341 db_query_params( $t_query, array ($resource->get_url(), $resource->get_provider_id(), $resource->get_http_method(), $resource->get_id()) );