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

319 lines 7.5 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 ) || ( get_option( 'mainwp_api_sslVerifyCertificate' ) == 1 ) ) ? 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 public function activate( $args ) {
79
80 $defaults = array(
81 'request' => 'softwareactivation',
82 );
83
84 $args = wp_parse_args( $defaults, $args );
85
86 if ( isset( $args['password'] ) ) {
87 $args['password'] = stripslashes( $args['password'] );
88 }
89
90 $request = wp_remote_post(
91 MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api',
92 array(
93 'body' => $args,
94 'timeout' => 50,
95 'sslverify' => self::$apisslverify,
96 )
97 );
98
99 if ( is_wp_error( $request ) || 200 != wp_remote_retrieve_response_code( $request ) ) {
100
101 return false;
102 }
103
104 $response = wp_remote_retrieve_body( $request );
105
106 return $response;
107 }
108
109 /**
110 * Deactivate extension .
111 *
112 * This function checks the users login information & grabs the update URL for the specific extension & deactivates it.
113 *
114 * @param array $args Extension arguments.
115 *
116 * @return array Request response.
117 */
118 public function deactivate( $args ) {
119
120 $defaults = array(
121 'request' => 'deactivation',
122 );
123
124 $args = wp_parse_args( $defaults, $args );
125
126 if ( isset( $args['password'] ) ) {
127 $args['password'] = stripslashes( $args['password'] );
128 }
129 $request = wp_remote_post(
130 MainWP_Api_Manager::instance()->get_upgrade_url() . '?wc-api=am-software-api',
131 array(
132 'body' => $args,
133 'timeout' => 50,
134 'sslverify' => self::$apisslverify,
135 )
136 );
137
138 if ( is_wp_error( $request ) || 200 != wp_remote_retrieve_response_code( $request ) ) {
139 // Request failed.
140 return false;
141 }
142
143 $response = wp_remote_retrieve_body( $request );
144
145 return $response;
146 }
147
148 /**
149 * Grab extension API Key.
150 *
151 * This function checks the users login information & grabs the update URL for the specific extension & returns the API Key.
152 *
153 * @param array $args Extension arguments.
154 *
155 * @return array Request response.
156 */
157 public function grab_api_key( $args ) {
158
159 $defaults = array(
160 'request' => 'grabapikey',
161 );
162
163 $args = wp_parse_args( $defaults, $args );
164
165 if ( isset( $args['password'] ) ) {
166 $args['password'] = stripslashes( $args['password'] );
167 }
168 $request = wp_remote_post(
169 MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api',
170 array(
171 'body' => $args,
172 'timeout' => 50,
173 'sslverify' => self::$apisslverify,
174 )
175 );
176
177 if ( is_wp_error( $request ) || 200 != wp_remote_retrieve_response_code( $request ) ) {
178 // Request failed.
179 return false;
180 }
181
182 $response = wp_remote_retrieve_body( $request );
183
184 return $response;
185 }
186
187 /**
188 * Test login API.
189 *
190 * This function checks the users login information & Tests it against the MainWP.com Login Credentials stored on the license server.
191 *
192 * @param aray $args Login arguments.
193 *
194 * @throws \Exception Request error codes.
195 *
196 * @return array Request response.
197 */
198 public function test_login_api( $args ) {
199
200 $defaults = array(
201 'request' => 'testloginapi',
202 );
203
204 $args = wp_parse_args( $defaults, $args );
205 $args['password'] = stripslashes( $args['password'] );
206
207 $request = wp_remote_post(
208 MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api',
209 array(
210 'body' => $args,
211 'timeout' => 50,
212 'sslverify' => self::$apisslverify,
213 )
214 );
215
216 $log = $request;
217 if ( is_array( $log ) && isset( $log['http_response'] ) ) {
218 unset( $log['http_response'] );
219 }
220
221 MainWP_Logger::instance()->debug( 'test_login_api:: RESULT :: ' . MainWP_Utility::value_to_string( $log, true ) );
222
223 if ( is_wp_error( $request ) ) {
224 if ( 1 == self::$apisslverify ) {
225 MainWP_Utility::update_option( 'mainwp_api_sslVerifyCertificate', 0 );
226
227 return array( 'retry_action' => 1 );
228 }
229
230 throw new \Exception( $request->get_error_message() );
231 }
232
233 $code = wp_remote_retrieve_response_code( $request );
234 if ( 200 != $code ) {
235 $error = sprintf( __( 'Login verification could not be completed. Please contact %1$sMainWP Support%2$s so we can assist.', 'mainwp' ), '<a href="https://mainwp.com/my-account/get-support/" target="_blank">', '</a>' );
236 throw new \Exception( $error );
237 }
238
239 $response = wp_remote_retrieve_body( $request );
240
241 return $response;
242 }
243
244 /**
245 * Get purchased software.
246 *
247 * This function grabs a list of purchased MainWP Extensions that are available for download.
248 *
249 * @param array $args Software Arguments.
250 *
251 * @return array Request response.
252 */
253 public function get_purchased_software( $args ) {
254
255 $defaults = array(
256 'request' => 'getpurchasedsoftware',
257 );
258
259 $args = wp_parse_args( $defaults, $args );
260
261 if ( isset( $args['password'] ) ) {
262 $args['password'] = stripslashes( $args['password'] );
263 }
264 $request = wp_remote_post(
265 MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api',
266 array(
267 'body' => $args,
268 'timeout' => 50,
269 'sslverify' => self::$apisslverify,
270 )
271 );
272
273 if ( is_wp_error( $request ) || 200 != wp_remote_retrieve_response_code( $request ) ) {
274 // Request failed.
275 return false;
276 }
277
278 $response = wp_remote_retrieve_body( $request );
279
280 return $response;
281 }
282
283 /**
284 * Purchase software.
285 *
286 * @param array $args Software arguments.
287 *
288 * @return array Request response.
289 */
290 public function purchase_software( $args ) {
291 $defaults = array(
292 'request' => 'purchasesoftware',
293 );
294 $args = wp_parse_args( $defaults, $args );
295
296 if ( isset( $args['password'] ) ) {
297 $args['password'] = stripslashes( $args['password'] );
298 }
299 $request = wp_remote_post(
300 MainWP_Api_Manager::instance()->get_upgrade_url() . '?mainwp-api=am-software-api',
301 array(
302 'body' => $args,
303 'timeout' => 50,
304 'sslverify' => self::$apisslverify,
305 )
306 );
307
308 if ( is_wp_error( $request ) || 200 != wp_remote_retrieve_response_code( $request ) ) {
309 // Request failed.
310 return false;
311 }
312
313 $response = wp_remote_retrieve_body( $request );
314 return $response;
315 }
316 }
317
318 // Class is instantiated as an object by other classes on-demand.
319