| 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_Worker_Configuration |
| 12 |
{ |
| 13 |
|
| 14 |
private $context; |
| 15 |
|
| 16 |
public function __construct(MWP_WordPress_Context $context) |
| 17 |
{ |
| 18 |
$this->context = $context; |
| 19 |
} |
| 20 |
|
| 21 |
public function getPublicKey() |
| 22 |
{ |
| 23 |
return base64_decode($this->context->optionGet('_worker_public_key')); |
| 24 |
} |
| 25 |
|
| 26 |
public function setPublicKey($publicKey) |
| 27 |
{ |
| 28 |
$this->context->optionSet('_worker_public_key', base64_encode($publicKey), true); |
| 29 |
} |
| 30 |
|
| 31 |
public function deletePublicKey() |
| 32 |
{ |
| 33 |
$this->context->optionDelete('_worker_public_key'); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* @return string |
| 38 |
* |
| 39 |
* @deprecated Use public key instead. |
| 40 |
*/ |
| 41 |
public function getSecureKey() |
| 42 |
{ |
| 43 |
return base64_decode($this->context->optionGet('_worker_nossl_key')); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* @param $secureKey |
| 48 |
* |
| 49 |
* @deprecated Use public key instead. |
| 50 |
*/ |
| 51 |
public function setSecureKey($secureKey) |
| 52 |
{ |
| 53 |
$this->context->optionSet('_worker_nossl_key', base64_encode($secureKey)); |
| 54 |
} |
| 55 |
|
| 56 |
public function deleteSecureKey() |
| 57 |
{ |
| 58 |
$this->context->optionSet('_worker_nossl_key', ''); |
| 59 |
} |
| 60 |
|
| 61 |
public function getLivePublicKey($keyName, $prefix = false) |
| 62 |
{ |
| 63 |
$keyData = $this->findKeyData($keyName, $prefix); |
| 64 |
return $keyData !== null ? $keyData['publicKey'] : null; |
| 65 |
} |
| 66 |
|
| 67 |
public function isCommunicationKey($keyName) |
| 68 |
{ |
| 69 |
$keyData = $this->findKeyData($keyName); |
| 70 |
return ($keyData === null || empty($keyData['useServiceKey'])); |
| 71 |
} |
| 72 |
|
| 73 |
public function getCommunicationStringByKeyName($keyName) |
| 74 |
{ |
| 75 |
return $this->isCommunicationKey($keyName) ? mwp_get_communication_key() : mwp_get_service_key(); |
| 76 |
} |
| 77 |
|
| 78 |
public function acceptCommunicationKeyIfEmpty($keyName, $communicationKey) |
| 79 |
{ |
| 80 |
if (!$this->isCommunicationKey($keyName)) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
mwp_add_as_site_communication_key($communicationKey); |
| 85 |
} |
| 86 |
|
| 87 |
protected function findKeyData($keyName, $prefix = false) |
| 88 |
{ |
| 89 |
$key = $this->findKey($keyName, $prefix); |
| 90 |
|
| 91 |
if (!empty($key)) { |
| 92 |
return $key; |
| 93 |
} |
| 94 |
|
| 95 |
$time = time(); |
| 96 |
$keys = $this->context->optionGet('mwp_public_keys', null); |
| 97 |
$refresh_time = $this->context->optionGet('mwp_public_keys_refresh_time', $time - 100000); |
| 98 |
|
| 99 |
// if the keys were refreshed recently, give up |
| 100 |
if (!empty($keys) && $time - $refresh_time < 86400) { |
| 101 |
return null; |
| 102 |
} |
| 103 |
|
| 104 |
mwp_refresh_live_public_keys(array()); |
| 105 |
|
| 106 |
return $this->findKey($keyName, $prefix); |
| 107 |
} |
| 108 |
|
| 109 |
private function findKey($keyName, $prefix = false) |
| 110 |
{ |
| 111 |
$keys = $this->context->optionGet('mwp_public_keys', null); |
| 112 |
|
| 113 |
if (empty($keys) || !is_array($keys)) { |
| 114 |
return null; |
| 115 |
} |
| 116 |
|
| 117 |
foreach ($keys as $key) { |
| 118 |
if (empty($key['id']) || ($key['id'] !== $keyName && !$prefix) || ($key['service'] !== $keyName && $prefix)) { |
| 119 |
continue; |
| 120 |
} |
| 121 |
|
| 122 |
if (empty($key['validFrom']) || empty($key['validTo']) || empty($key['publicKey']) || empty($key['service'])) { |
| 123 |
continue; |
| 124 |
} |
| 125 |
|
| 126 |
$timeNow = new DateTime(); |
| 127 |
|
| 128 |
if ($timeNow < new DateTime($key['validFrom']) || $timeNow > new DateTime($key['validTo'])) { |
| 129 |
continue; |
| 130 |
} |
| 131 |
|
| 132 |
return $key; |
| 133 |
} |
| 134 |
|
| 135 |
return null; |
| 136 |
} |
| 137 |
} |
| 138 |
|