PluginProbe
UpdraftPlus: WP Backup & Migration Plugin / 1.8.13
UpdraftPlus: WP Backup & Migration Plugin v1.8.13
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 / Dropbox / OAuth / Storage / WordPress.php

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

127 lines 4.0 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@wordshell.net>
8 * @link http://wordshell.net
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 * Encyption object
23 * @var Encrypter|null
24 */
25 protected $encrypter = null;
26
27 /**
28 * Check if an instance of the encrypter is passed, set the encryption object
29 * @return void
30 */
31 public function __construct(Dropbox_Encrypter $encrypter = null, $option_name_prefix = 'dropbox_token')
32 {
33 if ($encrypter instanceof Dropbox_Encrypter) {
34 $this->encrypter = $encrypter;
35 }
36
37 $this->option_name_prefix = $option_name_prefix;
38
39 }
40
41 /**
42 * Get an OAuth token from the database
43 * If the encryption object is set then decrypt the token before returning
44 * @param string $type Token type to retrieve
45 * @return array|bool
46 */
47 public function get($type)
48 {
49 if ($type != 'request_token' && $type != 'access_token') {
50 throw new Dropbox_Exception("Expected a type of either 'request_token' or 'access_token', got '$type'");
51 } else {
52 if (false !== ($gettoken = UpdraftPlus_Options::get_updraft_option($this->option_name_prefix.$type))) {
53 $token = $this->decrypt($gettoken);
54 return $token;
55 }
56 return false;
57 }
58 }
59
60 /**
61 * Set an OAuth token in the database by type
62 * If the encryption object is set then encrypt the token before storing
63 * @param \stdClass Token object to set
64 * @param string $type Token type
65 * @return void
66 */
67 public function set($token, $type)
68 {
69 if ($type != 'request_token' && $type != 'access_token') {
70 throw new Dropbox_Exception("Expected a type of either 'request_token' or 'access_token', got '$type'");
71 } else {
72 $token = $this->encrypt($token);
73 UpdraftPlus_Options::update_updraft_option($this->option_name_prefix.$type, $token);
74 }
75 }
76
77 /**
78 * Delete the request and access tokens currently stored in the database
79 * @return bool
80 */
81 public function delete()
82 {
83 UpdraftPlus_Options::delete_updraft_option($this->option_name_prefix.'request_token');
84 UpdraftPlus_Options::delete_updraft_option($this->option_name_prefix.'access_token');
85 return true;
86 }
87
88 /**
89 * Use the Encrypter to encrypt a token and return it
90 * If there is not encrypter object, return just the
91 * serialized token object for storage
92 * @param stdClass $token OAuth token to encrypt
93 * @return stdClass|string
94 */
95 protected function encrypt($token)
96 {
97 // Serialize the token object
98 $token = serialize($token);
99
100 // Encrypt the token if there is an Encrypter instance
101 if ($this->encrypter instanceof Dropbox_Encrypter) {
102 $token = $this->encrypter->encrypt($token);
103 }
104
105 // Return the token
106 return $token;
107 }
108
109 /**
110 * Decrypt a token using the Encrypter object and return it
111 * If there is no Encrypter object, assume the token was stored
112 * serialized and return the unserialized token object
113 * @param stdClass $token OAuth token to encrypt
114 * @return stdClass|string
115 */
116 protected function decrypt($token)
117 {
118 // Decrypt the token if there is an Encrypter instance
119 if ($this->encrypter instanceof Dropbox_Encrypter) {
120 $token = $this->encrypter->decrypt($token);
121 }
122
123 // Return the unserialized token
124 return @unserialize($token);
125 }
126 }
127