PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.1
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.1, at class/class-mainwp-api-manager-key.php

325 lines 10.0 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 { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
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 static::$instance
49 */
50 public static function instance() {
51
52 if ( is_null( static::$instance ) ) {
53 static::$instance = new self();
54 }
55
56 return static::$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 static::$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' => static::$apisslverify,
95 )
96 );
97
98 if ( is_wp_error( $request ) || 200 !== wp_remote_retrieve_response_code( $request ) ) {
99
100 return false;
101 }
102
103 return wp_remote_retrieve_body( $request );
104 }
105
106 /**
107 * Deactivate extension .
108 *
109 * This function checks the users login information & grabs the update URL for the specific extension & deactivates it.
110 *
111 * @param array $args Extension arguments.
112 *
113 * @return array Request response.
114 *
115 * @uses \MainWP\Dashboard\MainWP_Api_Manager::get_upgrade_url()
116 */
117 public function deactivate( $args ) {
118
119 $defaults = array(
120 'request' => 'deactivate', // old: wc-api/deactivation.
121 'dashboard_version' => MainWP_System::instance()->get_dashboard_version(),
122 );
123
124 $args = wp_parse_args( $defaults, $args );
125
126 $request = wp_remote_post(
127 MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api', // old: wc-api/deactivate.
128 array(
129 'body' => $args,
130 'timeout' => 50,
131 'sslverify' => static::$apisslverify,
132 )
133 );
134
135 if ( is_wp_error( $request ) || 200 !== wp_remote_retrieve_response_code( $request ) ) {
136 // Request failed.
137 return false;
138 }
139
140 return wp_remote_retrieve_body( $request );
141 }
142
143 /**
144 * Grab extension API Key.
145 *
146 * This function checks the users login information & grabs the update URL for the specific extension & returns the API Key.
147 *
148 * @param array $args Extension arguments.
149 *
150 * @return array Request response.
151 *
152 * @uses \MainWP\Dashboard\MainWP_Api_Manager::get_upgrade_url()
153 */
154 public function grab_api_key( $args ) {
155
156 $defaults = array(
157 'request' => 'grabapikey',
158 'dashboard_version' => MainWP_System::instance()->get_dashboard_version(),
159 );
160
161 $args = wp_parse_args( $defaults, $args );
162
163 $request = wp_remote_post(
164 MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api',
165 array(
166 'body' => $args,
167 'timeout' => 50,
168 'sslverify' => static::$apisslverify,
169 )
170 );
171
172 if ( is_wp_error( $request ) || 200 !== wp_remote_retrieve_response_code( $request ) ) {
173 // Request failed.
174 return false;
175 }
176
177 return wp_remote_retrieve_body( $request );
178 }
179
180 /**
181 * Test login API.
182 *
183 * This function checks the users login information & Tests it against the MainWP.com Login Credentials stored on the license server.
184 *
185 * @param aray $args Login arguments.
186 *
187 * @return array Request response.
188 *
189 * @throws \MainWP_Exception Request error codes.
190 *
191 * @uses \MainWP\Dashboard\MainWP_Api_Manager::get_upgrade_url()
192 * @uses \MainWP\Dashboard\MainWP_Logger::debug()
193 * @uses \MainWP\Dashboard\MainWP_Utility::value_to_string()
194 * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
195 */
196 public function verify_api_key( $args ) {
197
198 $defaults = array(
199 'request' => 'testloginapi',
200 );
201
202 $args = wp_parse_args( $defaults, $args );
203
204 $request = wp_remote_post(
205 MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api',
206 array(
207 'body' => $args,
208 'timeout' => 50,
209 'sslverify' => static::$apisslverify,
210 )
211 );
212
213 if ( is_wp_error( $request ) ) {
214 if ( 1 === (int) static::$apisslverify ) {
215 MainWP_Utility::update_option( 'mainwp_api_sslVerifyCertificate', 0 );
216
217 return array( 'retry_action' => 1 );
218 }
219
220 throw new MainWP_Exception( $request->get_error_message() ); //phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
221 }
222
223 $code = wp_remote_retrieve_response_code( $request );
224 if ( 200 !== $code ) {
225 $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>' );
226 throw new MainWP_Exception( $error ); //phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Escaped.
227 }
228
229 return wp_remote_retrieve_body( $request );
230 }
231
232 /**
233 * Get purchased software.
234 *
235 * This function grabs a list of purchased MainWP Extensions that are available for download.
236 *
237 * @param array $args Software Arguments.
238 *
239 * @return array Request response.
240 *
241 * @uses \MainWP\Dashboard\MainWP_Api_Manager::get_upgrade_url()
242 */
243 public function get_purchased_software( $args ) {
244
245 $defaults = array(
246 'request' => 'getpurchasedsoftware',
247 );
248
249 $args = wp_parse_args( $defaults, $args );
250
251 $request = wp_remote_post(
252 MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api',
253 array(
254 'body' => $args,
255 'timeout' => 200,
256 'sslverify' => static::$apisslverify,
257 )
258 );
259
260 MainWP_Logger::instance()->debug( 'Get purchased softwares: ' . MainWP_Utility::value_to_string( $request ) );
261
262 if ( is_wp_error( $request ) || 200 !== wp_remote_retrieve_response_code( $request ) ) {
263 // Request failed.
264 return false;
265 }
266
267 return wp_remote_retrieve_body( $request );
268 }
269
270 /**
271 * Purchase software.
272 *
273 * @param array $args Software arguments.
274 *
275 * @return array Request response.
276 *
277 * @uses \MainWP\Dashboard\MainWP_Api_Manager::get_upgrade_url()
278 */
279 public function purchase_software( $args ) {
280 $defaults = array(
281 'request' => 'purchasesoftware',
282 );
283 $args = wp_parse_args( $defaults, $args );
284
285 $request = wp_remote_post(
286 MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api',
287 array(
288 'body' => $args,
289 'timeout' => 50,
290 'sslverify' => static::$apisslverify,
291 )
292 );
293
294 if ( is_wp_error( $request ) || 200 !== wp_remote_retrieve_response_code( $request ) ) {
295 // Request failed.
296 return false;
297 }
298
299 return wp_remote_retrieve_body( $request );
300 }
301
302 /**
303 * Get decrypt master api key.
304 *
305 * @return array Request response.
306 */
307 public function get_decrypt_master_api_key() {
308 $decryp_api_key = '';
309
310 // to check old value.
311 $old_enscrypt = get_option( 'mainwp_extensions_master_api_key', false );
312 if ( false !== $old_enscrypt && is_string( $old_enscrypt ) && ! empty( $old_enscrypt ) ) { // to compatible.
313 // old encrypt.
314 $decryp_api_key = ! empty( $old_enscrypt ) ? MainWP_Api_Manager_Password_Management::decrypt_string( $old_enscrypt ) : '';
315 // convert to new encrypt: to compatible.
316 if ( ! empty( $decryp_api_key ) && is_string( $decryp_api_key ) ) {
317 MainWP_Keys_Manager::instance()->update_key_value( 'mainwp_extensions_master_api_key', $decryp_api_key );
318 }
319 }
320 return MainWP_Keys_Manager::instance()->get_keys_value( 'mainwp_extensions_master_api_key' );
321 }
322 }
323
324 // Class is instantiated as an object by other classes on-demand.
325