| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the ManageWP Worker plugin. |
| 4 |
* |
| 5 |
* (c) ManageWP LLC <contact@managewp.com> |
| 6 |
* |
| 7 |
* For the full copyright and license information, please view the LICENSE |
| 8 |
* file that was distributed with this source code. |
| 9 |
*/ |
| 10 |
|
| 11 |
class MWP_Action_ConnectWebsite extends MWP_Action_Abstract |
| 12 |
{ |
| 13 |
|
| 14 |
public function execute(array $params = array(), MWP_Worker_Request $request) |
| 15 |
{ |
| 16 |
if (empty($params['public_key'])) { |
| 17 |
throw new MWP_Worker_Exception(MWP_Worker_Exception::CONNECTION_PUBLIC_KEY_NOT_PROVIDED); |
| 18 |
} |
| 19 |
|
| 20 |
$siteId = $request->getSiteId(); |
| 21 |
$communicationKey = mwp_get_communication_key(); |
| 22 |
$publicKey = base64_decode($params['public_key']); |
| 23 |
$configuration = $this->container->getConfiguration(); |
| 24 |
$existingPublicKey = $configuration->getPublicKey(); |
| 25 |
|
| 26 |
if (empty($siteId) && ((!empty($existingPublicKey) && $publicKey !== $existingPublicKey) || !empty($communicationKey))) { |
| 27 |
throw new MWP_Worker_Exception(MWP_Worker_Exception::CONNECTION_PUBLIC_KEY_EXISTS, "Sorry, the site appears to be already added to a ManageWP account. Please deactivate, then activate ManageWP Worker plugin on your website and try again or contact our support."); |
| 28 |
} |
| 29 |
|
| 30 |
if (!empty($params['skipVerificationTest'])) { |
| 31 |
// Legacy support for worker key. |
| 32 |
$signer = $this->container->getSigner(); |
| 33 |
$messageId = $request->getAction().$request->getNonce(); |
| 34 |
$verify = $signer->verify($messageId, $request->getSignature(), $publicKey); |
| 35 |
|
| 36 |
if (!$verify) { |
| 37 |
throw new MWP_Worker_Exception(MWP_Worker_Exception::CONNECTION_VERIFICATION_TEST_FAILED, "Unable to verify security signature. Contact your hosting support to check the OpenSSL configuration."); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
if (empty($existingPublicKey)) { |
| 42 |
$configuration->setPublicKey($publicKey); |
| 43 |
} |
| 44 |
|
| 45 |
mwp_accept_potential_key($request->getCommunicationKey()); |
| 46 |
|
| 47 |
$this->setBrand($params); |
| 48 |
|
| 49 |
return array(); |
| 50 |
} |
| 51 |
|
| 52 |
private function setBrand(array $params) |
| 53 |
{ |
| 54 |
if (!empty($params['brand']) && is_array($params['brand'])) { |
| 55 |
$this->container->getBrand()->update($params['brand']); |
| 56 |
|
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
$this->container->getBrand()->delete(); |
| 61 |
} |
| 62 |
} |
| 63 |
|