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.
25 require_once 'OAuth.php';
28 * OAuth Consumer class stored in DB
30 * @author Olivier Berger
32 class OauthAuthzConsumer extends OAuthConsumer {
33 protected $id; // ID in the DB
34 protected $name; // admin provided user-friendly name for the consumer
40 function __construct( $p_name, $p_key, $p_secret, $p_url, $p_desc, $p_email) {
41 parent::__construct($p_key, $p_secret);
43 $this->name = $p_name;
45 $this->desc = $p_desc;
46 $this->email = $p_email;
49 public function setId($p_id) {
53 public function setName($p_name) {
54 $this->name = $p_name;
57 public function setURL($p_url) {
61 public function setDesc($p_desc) {
62 $this->desc = $p_desc;
65 public function setEmail($p_email) {
66 $this->email = $p_email;
69 public function getId() {
73 public function getName() {
77 public function getUrl() {
81 public function getDesc() {
85 public function getEmail() {
89 static function check_consumer_values($p_consumer_name, $p_consumer_url, $p_consumer_desc, $p_consumer_email) {
90 if ((!trim($p_consumer_name))) {
91 //$missing_params[] = _('"Consumer Name"');
92 return "The field 'Consumer Name' is empty! ";
93 //exit_missing_param('', $missing_params,'oauthprovider');
95 elseif ((!trim($p_consumer_url))) {
96 return "The field 'Consumer URL' is empty! ";
98 elseif ((!trim($p_consumer_desc))) {
99 return "The field 'Consumer Description' is empty! ";
101 elseif ((!trim($p_consumer_email))) {
102 return "The field 'Consumer Email' is empty! ";
104 elseif(strlen($p_consumer_name)<5) {
105 return "The Consumer Name cannot be less than 5 characters!";
107 elseif(strlen($p_consumer_name)>15) {
108 return "The Consumer Name cannot be more than 15 characters!";
110 elseif(is_numeric(substr($p_consumer_name, 0, 1))) {
111 return "The Consumer Name cannot begin with a numeral!";
113 elseif((substr($p_consumer_name, 0, 1))=="_") {
114 return "The Consumer Name cannot begin with an underscore!";
116 elseif(preg_match('/^[A-z][A-z_0-9]{4,}/', $p_consumer_name)==0) {
117 return "The Consumer Name can only contain alphabets (a-z,A-Z), numbers (0-9) and underscores (_). Please choose a Consumer Name accordingly!";
119 elseif(OauthAuthzConsumer::consumer_exists($p_consumer_name)) {
120 return "The name '".$p_consumer_name."' has already been taken. Please choose another!";
123 elseif(!preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $p_consumer_url)) {
124 return "The Consumer URL is not valid.";
132 * Converts a row returned by select * into an object
134 * @param array $t_row
135 * @return OauthAuthzRequestToken
137 static function row_to_new_consumer ($t_row) {
139 $t_consumer = new OauthAuthzConsumer( $t_row['name'], $t_row['consumer_key'], $t_row['consumer_secret'], $t_row['consumer_url'], $t_row['consumer_desc'], $t_row['consumer_email'] );
140 $t_consumer->setId($t_row['id']);
149 * returns a newly generated consumer key and secret
153 static function new_consumer_keys_generate () {
154 $DBSTORE = FFDbOAuthDataStore::singleton();
155 return $DBSTORE->new_consumer_keys();
159 * Loads an OauthAuthzConsumer from its ID in the DB
161 * @param int $p_id ID in the DB
162 * @return OauthAuthzConsumer
164 static function load( $p_id ) {
165 $DBSTORE = FFDbOAuthDataStore::singleton();
166 $t_row = $DBSTORE->find_consumer_from_id($p_id);
167 $t_consumer = OauthAuthzConsumer::row_to_new_consumer($t_row);
168 $t_consumer->setId($t_row['id']);
173 * Loads all OauthAuthzConsumer from the DB
175 * @return array of OauthAuthzConsumer
177 static function load_all() {
178 $DBSTORE = FFDbOAuthDataStore::singleton();
179 $t_rows = $DBSTORE->find_all_consumers();
181 $t_consumers = array();
183 foreach ($t_rows as $t_row) {
184 $t_consumer = OauthAuthzConsumer::row_to_new_consumer($t_row);
186 $t_consumers[] = $t_consumer;
193 * Loads an OauthAuthzConsumer from the DB provided its key
195 * @param string $p_consumer_key
196 * @return OauthAuthzConsumer
198 static function load_by_key( $p_consumer_key ) {
199 $DBSTORE = FFDbOAuthDataStore::singleton();
200 $t_row = $DBSTORE->find_consumer_from_key($p_consumer_key);
205 $t_consumer = OauthAuthzConsumer::row_to_new_consumer($t_row);
211 * Loads an OauthAuthzConsumer from the DB provided its name
213 * @param string $p_consumer_name
216 static function consumer_exists( $p_consumer_name ) {
217 $DBSTORE = FFDbOAuthDataStore::singleton();
218 $t_row = $DBSTORE->find_consumer_from_name($p_consumer_name);
229 * Saves an OauthAuthzConsumer to the DB
232 if ( strlen(trim( $this->name ))==0 || strlen(trim( $this->key ))==0 || strlen(trim( $this->secret ))==0 ) {
233 exit_error( "Error trying to save consumer. Please try again.", 'oauthprovider' );
235 $DBSTORE = FFDbOAuthDataStore::singleton();
236 $id=$DBSTORE->save_consumer($this);
238 exit_error("Error trying to create new consumer in DB", 'oauthprovider');
247 * Deletes an OauthAuthzConsumer from the DB
250 $DBSTORE = FFDbOAuthDataStore::singleton();
251 if(!($DBSTORE->delete_consumer($this->id))) {
252 exit_error("Error trying to delete consumer from DB", 'oauthprovider');