| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* OAuth storage handler using WordPress options |
| 5 |
* This can only be used if you have a WordPress environment loaded, such that the (get|update|delete)_option functions are available |
| 6 |
* @package Dropbox\Oauth |
| 7 |
* @subpackage Storage |
| 8 |
*/ |
| 9 |
|
| 10 |
class Dropbox_WordPress implements Dropbox_StorageInterface |
| 11 |
{ |
| 12 |
/** |
| 13 |
* Option name |
| 14 |
* @var string |
| 15 |
*/ |
| 16 |
protected $option_name_prefix = 'dropbox_token'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Option name (array storage) |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
protected $option_array = ''; |
| 23 |
|
| 24 |
/** |
| 25 |
* Encyption object |
| 26 |
* @var Encrypter|null |
| 27 |
*/ |
| 28 |
protected $encrypter = null; |
| 29 |
|
| 30 |
/** |
| 31 |
* Backup module object |
| 32 |
* @var Backup_module_object|null |
| 33 |
*/ |
| 34 |
protected $backup_module_object = null; |
| 35 |
|
| 36 |
/** |
| 37 |
* Check if an instance of the encrypter is passed, set the encryption object |
| 38 |
* @return void |
| 39 |
*/ |
| 40 |
public function __construct(Dropbox_Encrypter $encrypter = null, $option_name_prefix = 'dropbox_token', $option_array = 'dropbox', $backup_module_object = null) |
| 41 |
{ |
| 42 |
if ($encrypter instanceof Dropbox_Encrypter) { |
| 43 |
$this->encrypter = $encrypter; |
| 44 |
} |
| 45 |
|
| 46 |
$this->backup_module_object = $backup_module_object; |
| 47 |
|
| 48 |
$this->option_name_prefix = $option_name_prefix; |
| 49 |
$this->option_array = $option_array; |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Get an entry from the Dropbox options in the database |
| 55 |
* If the encryption object is set then decrypt the token before returning |
| 56 |
* @param string $type is the key to retrieve |
| 57 |
* @return array|bool |
| 58 |
*/ |
| 59 |
public function get($type) |
| 60 |
{ |
| 61 |
if ($type != 'request_token' && $type != 'access_token' && $type != 'appkey' && $type != 'CSRF' && $type != 'code') { |
| 62 |
throw new Dropbox_Exception("Expected a type of either 'request_token', 'access_token', 'CSRF' or 'code', got '$type'"); |
| 63 |
} else { |
| 64 |
if (false !== ($opts = $this->backup_module_object->get_options())) { |
| 65 |
if ($type == 'request_token' || $type == 'access_token'){ |
| 66 |
if (!empty($opts[$this->option_name_prefix.$type])) { |
| 67 |
$gettoken = $opts[$this->option_name_prefix.$type]; |
| 68 |
if (!empty($opts['email'])) { |
| 69 |
$token = $this->decrypt($gettoken); |
| 70 |
}else{ |
| 71 |
$token = $gettoken; |
| 72 |
} |
| 73 |
return $token; |
| 74 |
} |
| 75 |
} else { |
| 76 |
if (!empty($opts[$type])) { |
| 77 |
return $opts[$type]; |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
return false; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Set a value in the database by type |
| 87 |
* If the value is a token and the encryption object is set then encrypt the token before storing |
| 88 |
* @param \stdClass Token object to set |
| 89 |
* @param string $type Token type |
| 90 |
* @return void |
| 91 |
*/ |
| 92 |
public function set($token, $type) |
| 93 |
{ |
| 94 |
if ($type != 'request_token' && $type != 'access_token' && $type != 'upgraded' && $type != 'CSRF' && $type != 'code') { |
| 95 |
throw new Dropbox_Exception("Expected a type of either 'request_token', 'access_token', 'CSRF', 'upgraded' or 'code', got '$type'"); |
| 96 |
} else { |
| 97 |
|
| 98 |
$opts = $this->backup_module_object->get_options(); |
| 99 |
|
| 100 |
if ($type == 'access_token'){ |
| 101 |
if (!empty($opts['email'])) { |
| 102 |
$token = $this->encrypt($token); |
| 103 |
} |
| 104 |
$opts[$this->option_name_prefix.$type] = $token; |
| 105 |
} else if ($type == 'request_token' ) { |
| 106 |
$opts[$this->option_name_prefix.$type] = $token; |
| 107 |
} else { |
| 108 |
$opts[$type] = $token; |
| 109 |
} |
| 110 |
|
| 111 |
$this->backup_module_object->set_options($opts, true); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Remove a value in the database by type rather than setting to null / empty |
| 117 |
* set the value to null here so that when it gets to the options filter it will |
| 118 |
* unset the value there, this avoids a bug where if the value is not set then |
| 119 |
* the option filter will take the value from the database and save that version back. |
| 120 |
* |
| 121 |
* N.B. Before PHP 7.0, you can't call a method name unset() |
| 122 |
* |
| 123 |
* @param string $type Token type |
| 124 |
* @return void |
| 125 |
*/ |
| 126 |
public function do_unset($type) |
| 127 |
{ |
| 128 |
if ($type != 'request_token' && $type != 'access_token' && $type != 'upgraded' && $type != 'CSRF' && $type != 'code') { |
| 129 |
throw new Dropbox_Exception("Expected a type of either 'request_token', 'access_token', 'CSRF', 'upgraded' or 'code', got '$type'"); |
| 130 |
} else { |
| 131 |
|
| 132 |
$opts = $this->backup_module_object->get_options(); |
| 133 |
|
| 134 |
if ($type == 'access_token' || $type == 'request_token'){ |
| 135 |
$opts[$this->option_name_prefix.$type] = null; |
| 136 |
} else { |
| 137 |
$opts[$type] = null; |
| 138 |
} |
| 139 |
$this->backup_module_object->set_options($opts, true); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Delete the request and access tokens currently stored in the database |
| 145 |
* @return bool |
| 146 |
*/ |
| 147 |
public function delete() |
| 148 |
{ |
| 149 |
$opts = $this->backup_module_object->get_options(); |
| 150 |
$opts[$this->option_name_prefix.'request_token'] = null; |
| 151 |
$opts[$this->option_name_prefix.'access_token'] = null; |
| 152 |
unset($opts['ownername']); |
| 153 |
unset($opts['upgraded']); |
| 154 |
$this->backup_module_object->set_options($opts, true); |
| 155 |
return true; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Use the Encrypter to encrypt a token and return it |
| 160 |
* If there is not encrypter object, return just the |
| 161 |
* serialized token object for storage |
| 162 |
* @param stdClass $token OAuth token to encrypt |
| 163 |
* @return stdClass|string |
| 164 |
*/ |
| 165 |
protected function encrypt($token) |
| 166 |
{ |
| 167 |
// Serialize the token object |
| 168 |
$token = serialize($token); |
| 169 |
|
| 170 |
// Encrypt the token if there is an Encrypter instance |
| 171 |
if ($this->encrypter instanceof Dropbox_Encrypter) { |
| 172 |
$token = $this->encrypter->encrypt($token); |
| 173 |
} |
| 174 |
|
| 175 |
// Return the token |
| 176 |
return $token; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Decrypt a token using the Encrypter object and return it |
| 181 |
* If there is no Encrypter object, assume the token was stored |
| 182 |
* serialized and return the unserialized token object |
| 183 |
* @param stdClass $token OAuth token to encrypt |
| 184 |
* @return stdClass|string |
| 185 |
*/ |
| 186 |
protected function decrypt($token) |
| 187 |
{ |
| 188 |
// Decrypt the token if there is an Encrypter instance |
| 189 |
if ($this->encrypter instanceof Dropbox_Encrypter) { |
| 190 |
$token = $this->encrypter->decrypt($token); |
| 191 |
} |
| 192 |
|
| 193 |
// Return the unserialized token |
| 194 |
return @unserialize($token); |
| 195 |
} |
| 196 |
} |
| 197 |
|