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

326 lines 10.1 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 /* translators: 1: Opening anchor tag, 2: Closing anchor tag */
226 $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://community.mainwp.com/" target="_blank">', '</a>' );
227 throw new MainWP_Exception( $error ); //phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Escaped.
228 }
229
230 return wp_remote_retrieve_body( $request );
231 }
232
233 /**
234 * Get purchased software.
235 *
236 * This function grabs a list of purchased MainWP Extensions that are available for download.
237 *
238 * @param array $args Software Arguments.
239 *
240 * @return array Request response.
241 *
242 * @uses \MainWP\Dashboard\MainWP_Api_Manager::get_upgrade_url()
243 */
244 public function get_purchased_software( $args ) {
245
246 $defaults = array(
247 'request' => 'getpurchasedsoftware',
248 );
249
250 $args = wp_parse_args( $defaults, $args );
251
252 $request = wp_remote_post(
253 MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api',
254 array(
255 'body' => $args,
256 'timeout' => 200,
257 'sslverify' => static::$apisslverify,
258 )
259 );
260
261 MainWP_Logger::instance()->debug( 'Get purchased softwares: ' . MainWP_Utility::value_to_string( $request ) );
262
263 if ( is_wp_error( $request ) || 200 !== wp_remote_retrieve_response_code( $request ) ) {
264 // Request failed.
265 return false;
266 }
267
268 return wp_remote_retrieve_body( $request );
269 }
270
271 /**
272 * Purchase software.
273 *
274 * @param array $args Software arguments.
275 *
276 * @return array Request response.
277 *
278 * @uses \MainWP\Dashboard\MainWP_Api_Manager::get_upgrade_url()
279 */
280 public function purchase_software( $args ) {
281 $defaults = array(
282 'request' => 'purchasesoftware',
283 );
284 $args = wp_parse_args( $defaults, $args );
285
286 $request = wp_remote_post(
287 MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api',
288 array(
289 'body' => $args,
290 'timeout' => 50,
291 'sslverify' => static::$apisslverify,
292 )
293 );
294
295 if ( is_wp_error( $request ) || 200 !== wp_remote_retrieve_response_code( $request ) ) {
296 // Request failed.
297 return false;
298 }
299
300 return wp_remote_retrieve_body( $request );
301 }
302
303 /**
304 * Get decrypt master api key.
305 *
306 * @return array Request response.
307 */
308 public function get_decrypt_master_api_key() {
309 $decryp_api_key = '';
310
311 // to check old value.
312 $old_enscrypt = get_option( 'mainwp_extensions_master_api_key', false );
313 if ( false !== $old_enscrypt && is_string( $old_enscrypt ) && ! empty( $old_enscrypt ) ) { // to compatible.
314 // old encrypt.
315 $decryp_api_key = ! empty( $old_enscrypt ) ? MainWP_Api_Manager_Password_Management::decrypt_string( $old_enscrypt ) : '';
316 // convert to new encrypt: to compatible.
317 if ( ! empty( $decryp_api_key ) && is_string( $decryp_api_key ) ) {
318 MainWP_Keys_Manager::instance()->update_key_value( 'mainwp_extensions_master_api_key', $decryp_api_key );
319 }
320 }
321 return MainWP_Keys_Manager::instance()->get_keys_value( 'mainwp_extensions_master_api_key' );
322 }
323 }
324
325 // Class is instantiated as an object by other classes on-demand.
326