PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.0
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.0
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / class / class-mainwp-api-manager-key.php

class-mainwp-api-manager-key.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.0, at class/class-mainwp-api-manager-key.php

337 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP API Manager Key handler.
4 *
5 * This class handles user authentication with MainWP.com License Servers and provides the ability to grab license keys automatically.
6 *
7 * @package MainWP/Dashboard
8 */
9
10 namespace MainWP\Dashboard;
11
12 // Exit if accessed directly.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * Class MainWP_Api_Manager_Key
19 *
20 * MainWP Api Manager Key handler
21 * This class handles user authentication with MainWP.com License Servers
22 * and provides the ability to grab License Keys automatically.
23 *
24 * @package MainWP API Manager/Key Handler
25 * @author Todd Lahman LLC
26 * @copyright Copyright (c) Todd Lahman LLC
27 * @since 1.3.0
28 */
29 class MainWP_Api_Manager_Key {
30
31 /**
32 * Set initial $instance value.
33 *
34 * @var null
35 */
36 protected static $instance = null;
37
38 /**
39 * Set initial $apisslverify value.
40 *
41 * @var int
42 */
43 protected static $apisslverify = 1;
44
45 /**
46 * Create a new Self Instance.
47 *
48 * @return mixed self::$instance
49 */
50 public static function instance() {
51
52 if ( is_null( self::$instance ) ) {
53 self::$instance = new self();
54 }
55
56 return self::$instance;
57 }
58
59 /**
60 * MainWP_Api_Manager_Key constructor.
61 *
62 * Run any time class is called.
63 * Validate SSL certificate.
64 */
65 public function __construct() {
66 self::$apisslverify = ( ( get_option( 'mainwp_api_sslVerifyCertificate' ) === false ) || ( 1 === (int) get_option( 'mainwp_api_sslVerifyCertificate' ) ) ) ? 1 : 0;
67 }
68
69 /**
70 * Activate extension.
71 *
72 * This function checks the users login information & grabs the update URL for the specific extension & activates it.
73 *
74 * @param array $args Extension arguments.
75 *
76 * @return array Request response.
77 *
78 * @uses \MainWP\Dashboard\MainWP_Api_Manager::get_upgrade_url()
79 */
80 public function activate( $args ) {
81
82 $defaults = array(
83 'request' => 'softwareactivation',
84 'dashboard_version' => MainWP_System::instance()->get_dashboard_version(),
85 );
86
87 $args = wp_parse_args( $defaults, $args );
88
89 $request = wp_remote_post(
90 MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api',
91 array(
92 'body' => $args,
93 'timeout' => 50,
94 'sslverify' => self::$apisslverify,
95 )
96 );
97
98 if ( is_wp_error( $request ) || 200 !== wp_remote_retrieve_response_code( $request ) ) {
99
100 return false;
101 }
102
103 $response = wp_remote_retrieve_body( $request );
104
105 return $response;
106 }
107
108 /**
109 * Deactivate extension .
110 *
111 * This function checks the users login information & grabs the update URL for the specific extension & deactivates it.
112 *
113 * @param array $args Extension arguments.
114 *
115 * @return array Request response.
116 *
117 * @uses \MainWP\Dashboard\MainWP_Api_Manager::get_upgrade_url()
118 */
119 public function deactivate( $args ) {
120
121 $defaults = array(
122 'request' => 'deactivate', // old: wc-api/deactivation.
123 'dashboard_version' => MainWP_System::instance()->get_dashboard_version(),
124 );
125
126 $args = wp_parse_args( $defaults, $args );
127
128 $request = wp_remote_post(
129 MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api', // old: wc-api/deactivate.
130 array(
131 'body' => $args,
132 'timeout' => 50,
133 'sslverify' => self::$apisslverify,
134 )
135 );
136
137 if ( is_wp_error( $request ) || 200 !== wp_remote_retrieve_response_code( $request ) ) {
138 // Request failed.
139 return false;
140 }
141
142 $response = wp_remote_retrieve_body( $request );
143
144 return $response;
145 }
146
147 /**
148 * Grab extension API Key.
149 *
150 * This function checks the users login information & grabs the update URL for the specific extension & returns the API Key.
151 *
152 * @param array $args Extension arguments.
153 *
154 * @return array Request response.
155 *
156 * @uses \MainWP\Dashboard\MainWP_Api_Manager::get_upgrade_url()
157 */
158 public function grab_api_key( $args ) {
159
160 $defaults = array(
161 'request' => 'grabapikey',
162 'dashboard_version' => MainWP_System::instance()->get_dashboard_version(),
163 );
164
165 $args = wp_parse_args( $defaults, $args );
166
167 $request = wp_remote_post(
168 MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api',
169 array(
170 'body' => $args,
171 'timeout' => 50,
172 'sslverify' => self::$apisslverify,
173 )
174 );
175
176 if ( is_wp_error( $request ) || 200 !== wp_remote_retrieve_response_code( $request ) ) {
177 // Request failed.
178 return false;
179 }
180
181 $response = wp_remote_retrieve_body( $request );
182
183 return $response;
184 }
185
186 /**
187 * Test login API.
188 *
189 * This function checks the users login information & Tests it against the MainWP.com Login Credentials stored on the license server.
190 *
191 * @param aray $args Login arguments.
192 *
193 * @return array Request response.
194 *
195 * @throws \Exception Request error codes.
196 *
197 * @uses \MainWP\Dashboard\MainWP_Api_Manager::get_upgrade_url()
198 * @uses \MainWP\Dashboard\MainWP_Logger::debug()
199 * @uses \MainWP\Dashboard\MainWP_Utility::value_to_string()
200 * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
201 */
202 public function verify_api_key( $args ) {
203
204 $defaults = array(
205 'request' => 'testloginapi',
206 );
207
208 $args = wp_parse_args( $defaults, $args );
209
210 $request = wp_remote_post(
211 MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api',
212 array(
213 'body' => $args,
214 'timeout' => 50,
215 'sslverify' => self::$apisslverify,
216 )
217 );
218
219 if ( is_wp_error( $request ) ) {
220 if ( 1 === (int) self::$apisslverify ) {
221 MainWP_Utility::update_option( 'mainwp_api_sslVerifyCertificate', 0 );
222
223 return array( 'retry_action' => 1 );
224 }
225
226 throw new \Exception( $request->get_error_message() ); //phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
227 }
228
229 $code = wp_remote_retrieve_response_code( $request );
230 if ( 200 !== $code ) {
231 $error = sprintf( esc_html__( 'Login verification could not be completed. Please contact %1$sMainWP Support%2$s so we can assist.', 'mainwp' ), '<a href="https://managers.mainwp.com/" target="_blank">', '</a>' );
232 throw new \Exception( $error ); //phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Escaped.
233 }
234
235 $response = wp_remote_retrieve_body( $request );
236
237 return $response;
238 }
239
240 /**
241 * Get purchased software.
242 *
243 * This function grabs a list of purchased MainWP Extensions that are available for download.
244 *
245 * @param array $args Software Arguments.
246 *
247 * @return array Request response.
248 *
249 * @uses \MainWP\Dashboard\MainWP_Api_Manager::get_upgrade_url()
250 */
251 public function get_purchased_software( $args ) {
252
253 $defaults = array(
254 'request' => 'getpurchasedsoftware',
255 );
256
257 $args = wp_parse_args( $defaults, $args );
258
259 $request = wp_remote_post(
260 MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api',
261 array(
262 'body' => $args,
263 'timeout' => 200,
264 'sslverify' => self::$apisslverify,
265 )
266 );
267
268 MainWP_Logger::instance()->debug( 'Get purchased softwares: ' . MainWP_Utility::value_to_string( $request ) );
269
270 if ( is_wp_error( $request ) || 200 !== wp_remote_retrieve_response_code( $request ) ) {
271 // Request failed.
272 return false;
273 }
274
275 $response = wp_remote_retrieve_body( $request );
276
277 return $response;
278 }
279
280 /**
281 * Purchase software.
282 *
283 * @param array $args Software arguments.
284 *
285 * @return array Request response.
286 *
287 * @uses \MainWP\Dashboard\MainWP_Api_Manager::get_upgrade_url()
288 */
289 public function purchase_software( $args ) {
290 $defaults = array(
291 'request' => 'purchasesoftware',
292 );
293 $args = wp_parse_args( $defaults, $args );
294
295 $request = wp_remote_post(
296 MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api',
297 array(
298 'body' => $args,
299 'timeout' => 50,
300 'sslverify' => self::$apisslverify,
301 )
302 );
303
304 if ( is_wp_error( $request ) || 200 !== wp_remote_retrieve_response_code( $request ) ) {
305 // Request failed.
306 return false;
307 }
308
309 $response = wp_remote_retrieve_body( $request );
310 return $response;
311 }
312
313 /**
314 * Get decrypt master api key.
315 *
316 * @return array Request response.
317 */
318 public function get_decrypt_master_api_key() {
319 $decryp_api_key = '';
320
321 // to check old value.
322 $old_enscrypt = get_option( 'mainwp_extensions_master_api_key', false );
323 if ( false !== $old_enscrypt && is_string( $old_enscrypt ) && ! empty( $old_enscrypt ) ) { // to compatible.
324 // old encrypt.
325 $decryp_api_key = ! empty( $old_enscrypt ) ? MainWP_Api_Manager_Password_Management::decrypt_string( $old_enscrypt ) : '';
326 // convert to new encrypt: to compatible.
327 if ( ! empty( $decryp_api_key ) && is_string( $decryp_api_key ) ) {
328 MainWP_Keys_Manager::instance()->update_key_value( 'mainwp_extensions_master_api_key', $decryp_api_key );
329 }
330 }
331 $decryp_api_key = MainWP_Keys_Manager::instance()->get_keys_value( 'mainwp_extensions_master_api_key' );
332 return $decryp_api_key;
333 }
334 }
335
336 // Class is instantiated as an object by other classes on-demand.
337