7 protected $description;
8 protected $consumer_key;
9 protected $consumer_secret;
10 protected $request_token_url;
11 protected $authorize_url;
12 protected $access_token_url;
14 function __construct($name, $description, $consumer_key, $consumer_secret, $request_token_url, $authorize_url, $access_token_url, $id = 0) {
17 $this->description = $description;
18 $this->consumer_key = $consumer_key;
19 $this->consumer_secret = $consumer_secret;
20 $this->request_token_url = $request_token_url;
21 $this->authorize_url = $authorize_url;
22 $this->access_token_url = $access_token_url;
25 public function get_id() {
29 protected function set_id($id) {
33 public function get_name() {
37 public function get_description() {
38 return $this->description;
41 public function get_consumer_key() {
42 return $this->consumer_key;
45 public function get_consumer_secret() {
46 return $this->consumer_secret;
49 public function get_request_token_url() {
50 return $this->request_token_url;
53 public function get_authorize_url() {
54 return $this->authorize_url;
57 public function get_access_token_url() {
58 return $this->access_token_url;
61 public function set_name($name) {
65 public function set_description($description) {
66 $this->description = $description;
69 public function set_consumer_key($consumer_key) {
70 $this->consumer_key = $consumer_key;
73 public function set_consumer_secret($consumer_secret) {
74 $this->consumer_secret = $consumer_secret;
77 public function set_request_token_url($request_token_url) {
78 $this->request_token_url = $request_token_url;
81 public function set_authorize_url($authorize_url) {
82 $this->authorize_url = $authorize_url;
85 public function set_access_token_url($access_token_url) {
86 $this->access_token_url = $access_token_url;
89 static function convert_row_to_object($row) {
91 $provider = new OAuthProvider($row['name'], $row['description'], $row['consumer_key'], $row['consumer_secret'], $row['request_token_url'], $row['authorize_url'], $row['access_token_url'], $row['id']);
98 static function get_provider($id) {
99 $conn = FFOAuthDataStore::singleton();
100 $row = $conn->find_provider_from_id($id);
101 $provider = self::convert_row_to_object($row);
105 static function get_provider_by_name($name) {
106 $conn = FFOAuthDataStore::singleton();
107 $row = $conn->find_provider_from_name($name);
108 $provider = self::convert_row_to_object($row);
112 static function get_all_oauthproviders() {
113 $conn = FFOAuthDataStore::singleton();
114 $rows = $conn->find_all_providers();
115 $providers = array();
116 foreach ($rows as $row) {
117 $provider = OAuthProvider::convert_row_to_object($row);
118 $providers[] = $provider;
123 static function check_provider_values($new=TRUE, $name, $description, $consumer_key, $consumer_secret, $request_token_url, $authorize_url, $access_token_url) {
124 if ((!trim($name))) {
125 return "The field 'Name' is empty! ";
127 elseif ((!trim($description))) {
128 return "The field 'Description' is empty! ";
130 elseif ((!trim($consumer_key))) {
131 return "The field 'Consumer Key' is empty! ";
133 elseif ((!trim($consumer_secret))) {
134 return "The field 'Consumer Secret' is empty! ";
136 elseif(strlen($name)<5) {
137 return "The field 'Name' cannot be less than 5 characters!";
139 elseif(strlen($name)>15) {
140 return "The field 'Name' cannot be more than 15 characters!";
142 elseif(is_numeric(substr($name, 0, 1))) {
143 return "The field 'Name' cannot begin with a numeral!";
145 elseif((substr($name, 0, 1))=="_") {
146 return "The field 'Name' cannot begin with an underscore!";
148 elseif(preg_match('/^[A-z][A-z_0-9]{4,}/', $name)==0) {
149 return "The field 'Name' can only contain alphabets (a-z,A-Z), numbers (0-9) and underscores (_). Please choose a Name accordingly!";
151 elseif($new && self::provider_exists($name)) {
152 return "The name '".$name."' has already been taken. Please choose another!";
154 elseif((trim($request_token_url))&&(!preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $request_token_url))) {
155 return "The Request Token URL is not valid.";
157 elseif((trim($authorize_url))&&(!preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $authorize_url))) {
158 return "The Authorization URL is not valid.";
160 elseif((trim($access_token_url))&&(!preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $access_token_url))) {
161 return "The Access Token URL is not valid.";
168 static function provider_exists($name) {
169 $conn = FFOAuthDataStore::singleton();
170 $row = $conn->find_provider_from_name($name);
179 function write_to_db() {
180 if ( strlen(trim( $this->name ))==0 || strlen(trim( $this->description ))==0 || strlen(trim( $this->consumer_key ))==0 || strlen(trim( $this->consumer_secret ))==0 ) {
181 exit_error( "Error trying to add the oauth provider. Please try again.", 'oauthconsumer' );
183 $conn = FFOAuthDataStore::singleton();
184 $id = $conn->save_provider($this);
186 exit_error("Error trying to add new oauth provider to DB", 'oauthconsumer');
193 $conn = FFOAuthDataStore::singleton();
194 $id = $this->get_id();
196 if(!($conn->delete_provider($id))) {
197 exit_error("Error trying to delete provider from DB", 'oauthconsumer');
200 exit_error("Trying to delete non-existent provider from DB", 'oauthconsumer');