PluginProbe
UpdraftPlus: WP Backup & Migration Plugin / 1.16.5
UpdraftPlus: WP Backup & Migration Plugin v1.16.5
1.26.7 1.26.6 1.26.5 1.26.4 1.26.3 1.9.19 1.9.25 1.9.26 1.9.30 1.9.31 1.9.32 1.9.4 1.9.40 1.9.41 1.9.42 1.9.43 1.9.44 1.9.45 1.9.46 1.9.5 1.9.50 1.9.51 1.9.60 1.9.62 1.9.63 All 371 releases
updraftplus / includes / Dropbox2 / OAuth / Storage / WordPress.php

WordPress.php in UpdraftPlus: WP Backup & Migration Plugin 1.16.5, at includes/Dropbox2/OAuth/Storage/WordPress.php

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