| @@ -5,10 +5,12 @@ | ||
| 5 | 5 | use AcyMailing\Helpers\UpdatemeHelper; |
| 6 | 6 | |
| 7 | 7 | trait License |
| 8 | 8 | { |
| 9 | - public function unlinkLicense() | |
| 9 | + public function unlinkLicense(): void | |
| 10 | 10 | { |
| 11 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 12 | + | |
| 11 | 13 | $config = acym_getVar('array', 'config', []); |
| 12 | 14 | $licenseKey = empty($config['license_key']) ? $this->config->get('license_key') : $config['license_key']; |
| 13 | 15 | |
| 14 | 16 | $resultUnlinkLicenseOnUpdateMe = $this->unlinkLicenseOnUpdateMe($licenseKey); |
| @@ -13,9 +15,10 @@ | ||
| 13 | 15 | |
| 14 | 16 | $resultUnlinkLicenseOnUpdateMe = $this->unlinkLicenseOnUpdateMe($licenseKey); |
| 15 | 17 | |
| 16 | 18 | if ($resultUnlinkLicenseOnUpdateMe['success'] === true) { |
| 17 | - $this->config->save(['license_key' => '']); | |
| 19 | + $this->config->saveConfig(['license_key' => '']); | |
| 20 | + UpdatemeHelper::getLicenseInfo(); | |
| 18 | 21 | } |
| 19 | 22 | |
| 20 | 23 | if (!empty($resultUnlinkLicenseOnUpdateMe['message'])) { |
| 21 | 24 | $this->displayMessage($resultUnlinkLicenseOnUpdateMe['message']); |
| @@ -20,31 +23,35 @@ | ||
| 20 | 23 | if (!empty($resultUnlinkLicenseOnUpdateMe['message'])) { |
| 21 | 24 | $this->displayMessage($resultUnlinkLicenseOnUpdateMe['message']); |
| 22 | 25 | } |
| 23 | 26 | |
| 27 | + //Display the configuration | |
| 24 | 28 | $this->listing(); |
| 25 | - | |
| 26 | - return true; | |
| 27 | 29 | } |
| 28 | 30 | |
| 29 | - public function attachLicense() | |
| 31 | + public function attachLicense(): void | |
| 30 | 32 | { |
| 33 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 34 | + | |
| 35 | + | |
| 31 | 36 | $config = acym_getVar('array', 'config', []); |
| 32 | - $licenseKey = $config['license_key']; | |
| 33 | - | |
| 34 | - if (empty($licenseKey)) { | |
| 37 | + if (empty($config['license_key'])) { | |
| 35 | 38 | $this->displayMessage(acym_translation('ACYM_PLEASE_SET_A_LICENSE_KEY')); |
| 36 | 39 | $this->listing(); |
| 37 | 40 | |
| 38 | - return true; | |
| 41 | + return; | |
| 39 | 42 | } |
| 40 | 43 | |
| 41 | - $this->config->save(['license_key' => $licenseKey]); | |
| 44 | + //We save the license key | |
| 45 | + $this->config->saveConfig(['license_key' => $config['license_key']]); | |
| 42 | 46 | |
| 47 | + //We call updateme to attach the website to the license | |
| 43 | 48 | $resultAttachLicenseOnUpdateMe = $this->attachLicenseOnUpdateMe(); |
| 44 | 49 | |
| 45 | 50 | if ($resultAttachLicenseOnUpdateMe['success'] === false) { |
| 46 | - $this->config->save(['license_key' => '']); | |
| 51 | + $this->config->saveConfig(['license_key' => '']); | |
| 52 | + } else { | |
| 53 | + UpdatemeHelper::getLicenseInfo(); | |
| 47 | 54 | } |
| 48 | 55 | |
| 49 | 56 | if (!empty($resultAttachLicenseOnUpdateMe['message'])) { |
| 50 | 57 | $this->displayMessage($resultAttachLicenseOnUpdateMe['message']); |
| @@ -50,15 +57,14 @@ | ||
| 50 | 57 | $this->displayMessage($resultAttachLicenseOnUpdateMe['message']); |
| 51 | 58 | } |
| 52 | 59 | |
| 53 | 60 | $this->listing(); |
| 54 | - | |
| 55 | - return true; | |
| 56 | 61 | } |
| 57 | 62 | |
| 58 | - public function attachLicenseOnUpdateMe($licenseKey = null) | |
| 63 | + public function attachLicenseOnUpdateMe(?string $licenseKey = null): array | |
| 59 | 64 | { |
| 60 | 65 | |
| 66 | + //We get the license key saved | |
| 61 | 67 | if (is_null($licenseKey)) { |
| 62 | 68 | $licenseKey = $this->config->get('license_key', ''); |
| 63 | 69 | } |
| 64 | 70 | |
| @@ -80,29 +86,24 @@ | ||
| 80 | 86 | ]; |
| 81 | 87 | |
| 82 | 88 | $resultAttach = UpdatemeHelper::call('api/websites/attach', 'POST', $data); |
| 83 | 89 | |
| 84 | - acym_checkVersion(); | |
| 85 | - | |
| 86 | - if (empty($resultAttach)) { | |
| 87 | - $return['message'] = empty($resultAttach['message']) ? '' : $resultAttach['message']; | |
| 88 | - | |
| 90 | + // If it's not the result well formatted => don't save the license key and out | |
| 91 | + // If there is an error when the website has been attached => don't save the license key in the configuration | |
| 92 | + if (empty($resultAttach) || !$resultAttach['success']) { | |
| 89 | 93 | return $return; |
| 90 | 94 | } |
| 91 | 95 | |
| 92 | - if (!$resultAttach['success']) { | |
| 93 | - return $resultAttach; | |
| 94 | - } | |
| 95 | - | |
| 96 | 96 | acym_trigger('onAcymAttachLicense', [&$licenseKey]); |
| 97 | 97 | |
| 98 | 98 | return $resultAttach; |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | - private function unlinkLicenseOnUpdateMe($licenseKey = null) | |
| 101 | + private function unlinkLicenseOnUpdateMe(?string $licenseKey = null): array | |
| 102 | 102 | { |
| 103 | + //We get the license key saved | |
| 103 | 104 | if (is_null($licenseKey)) { |
| 104 | - $licenseKey = $this->config->get('license_key', ''); | |
| 105 | + $licenseKey = $this->config->get('license_key'); | |
| 105 | 106 | } |
| 106 | 107 | |
| 107 | 108 | $return = [ |
| 108 | 109 | 'message' => '', |
| @@ -114,8 +115,9 @@ | ||
| 114 | 115 | |
| 115 | 116 | return $return; |
| 116 | 117 | } |
| 117 | 118 | |
| 119 | + //First let's deactivate the cron | |
| 118 | 120 | $this->deactivateCron(false, $licenseKey); |
| 119 | 121 | |
| 120 | 122 | $data = [ |
| 121 | 123 | 'domain' => ACYM_LIVE, |
| @@ -120,46 +122,57 @@ | ||
| 120 | 122 | $data = [ |
| 121 | 123 | 'domain' => ACYM_LIVE, |
| 122 | 124 | ]; |
| 123 | 125 | |
| 126 | + //Call updateme to unlink the license from this website | |
| 124 | 127 | $resultUnlink = UpdatemeHelper::call('api/websites/unlink', 'POST', $data); |
| 125 | 128 | |
| 126 | - acym_checkVersion(); | |
| 127 | - | |
| 128 | - if (empty($resultUnlink)) { | |
| 129 | - $return['message'] = empty($resultUnlink['message']) ? '' : $resultUnlink['message']; | |
| 130 | - | |
| 129 | + //If it's not the result well formated => out | |
| 130 | + if (empty($resultUnlink) || !$resultUnlink['success']) { | |
| 131 | 131 | return $return; |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | - if (!$resultUnlink['success']) { | |
| 135 | - return $resultUnlink; | |
| 136 | - } | |
| 137 | - | |
| 138 | 134 | acym_trigger('onAcymDetachLicense'); |
| 139 | 135 | |
| 140 | 136 | return $resultUnlink; |
| 141 | 137 | } |
| 142 | 138 | |
| 143 | - public function activateCron($licenseKey = null) | |
| 139 | + public function activateCron(): void | |
| 144 | 140 | { |
| 145 | - $result = $this->modifyCron('activateCron', $licenseKey); | |
| 146 | - if ($result !== false && $this->displayMessage($result['message'])) $this->config->save(['active_cron' => 1]); | |
| 141 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 142 | + | |
| 143 | + | |
| 144 | + $result = $this->modifyCron('activateCron'); | |
| 145 | + //If everything went ok we save config with an active_cron to true | |
| 146 | + if (!empty($result) && !empty($this->displayMessage($result['message']))) { | |
| 147 | + $this->config->saveConfig(['active_cron' => 1]); | |
| 148 | + } | |
| 149 | + | |
| 147 | 150 | $this->listing(); |
| 148 | - | |
| 149 | - return true; | |
| 150 | 151 | } |
| 151 | 152 | |
| 152 | - public function deactivateCron($listing = true, $licenseKey = null) | |
| 153 | + //The listing parameter allows us to know if we need to display the listing or not | |
| 154 | + public function deactivateCron(bool $listing = true, ?string $licenseKey = null): void | |
| 153 | 155 | { |
| 156 | + // Called as a task (no argument) → require a CSRF token. Internal calls (e.g. unlinkLicense) are already token-checked. | |
| 157 | + if (func_num_args() === 0) { | |
| 158 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 159 | + } | |
| 160 | + | |
| 161 | + | |
| 154 | 162 | $result = $this->modifyCron('deactivateCron', $licenseKey); |
| 155 | - if ($result !== false && $this->displayMessage($result['message'])) $this->config->save(['active_cron' => 0]); | |
| 156 | - if ($listing) $this->listing(); | |
| 163 | + //If everything went ok we save config with an active_cron to false | |
| 164 | + if (!empty($result) && !empty($this->displayMessage($result['message']))) { | |
| 165 | + $this->config->saveConfig(['active_cron' => 0]); | |
| 166 | + } | |
| 157 | 167 | |
| 158 | - return true; | |
| 168 | + if ($listing) { | |
| 169 | + $this->listing(); | |
| 170 | + } | |
| 159 | 171 | } |
| 160 | 172 | |
| 161 | - public function modifyCron(string $functionToCall, ?string $licenseKey = null) | |
| 173 | + //The listing parameter allows us to know if we need to display the listing or not | |
| 174 | + public function modifyCron(string $functionToCall, ?string $licenseKey = null): array | |
| 162 | 175 | { |
| 163 | 176 | if (is_null($licenseKey)) { |
| 164 | 177 | $config = acym_getVar('array', 'config', []); |
| 165 | 178 | $licenseKey = empty($config['license_key']) ? '' : $config['license_key']; |
| @@ -164,12 +177,13 @@ | ||
| 164 | 177 | $config = acym_getVar('array', 'config', []); |
| 165 | 178 | $licenseKey = empty($config['license_key']) ? '' : $config['license_key']; |
| 166 | 179 | } |
| 167 | 180 | |
| 181 | + //If the license is not set => out | |
| 168 | 182 | if (empty($licenseKey)) { |
| 169 | 183 | $this->displayMessage('LICENSE_NOT_FOUND'); |
| 170 | 184 | |
| 171 | - return false; | |
| 185 | + return []; | |
| 172 | 186 | } |
| 173 | 187 | |
| 174 | 188 | $data = [ |
| 175 | 189 | 'domain' => ACYM_LIVE, |
| @@ -179,43 +193,40 @@ | ||
| 179 | 193 | 'activate' => $functionToCall === 'activateCron', |
| 180 | 194 | 'security_key' => $this->config->get('cron_key'), |
| 181 | 195 | ]; |
| 182 | 196 | |
| 197 | + //We call updateme to activate/deactivate the cron | |
| 183 | 198 | $result = UpdatemeHelper::call('api/crons/modify', 'POST', $data); |
| 184 | 199 | |
| 200 | + //If it's not the result well formated => out | |
| 185 | 201 | if (empty($result['success'])) { |
| 186 | 202 | $this->displayMessage(empty($result['message']) ? 'CRON_NOT_SAVED' : $result['message']); |
| 187 | 203 | |
| 188 | - return false; | |
| 204 | + return []; | |
| 189 | 205 | } |
| 190 | 206 | |
| 191 | 207 | return $result; |
| 192 | 208 | } |
| 193 | 209 | |
| 194 | - public function call($task, $allowedTasks = []) | |
| 210 | + public function attachLicenseAcymailer(): void | |
| 195 | 211 | { |
| 196 | - $allowedTasks[] = 'markNotificationRead'; | |
| 197 | - $allowedTasks[] = 'removeNotification'; | |
| 198 | - $allowedTasks[] = 'getAjax'; | |
| 199 | - $allowedTasks[] = 'addNotification'; | |
| 212 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 200 | 213 | |
| 201 | - parent::call($task, $allowedTasks); | |
| 202 | - } | |
| 203 | - | |
| 204 | - public function attachLicenseAcymailer() | |
| 205 | - { | |
| 206 | - $acyMailerLicenseKey = $this->config->get('acymailer_apikey', ''); | |
| 207 | - $acyMailingKey = $this->config->get('license_key', ''); | |
| 214 | + $acyMailerLicenseKey = $this->config->get('acymailer_apikey'); | |
| 215 | + $acyMailingKey = $this->config->get('license_key'); | |
| 208 | 216 | if (empty($acyMailerLicenseKey) && !empty($acyMailingKey)) { |
| 209 | 217 | acym_trigger('onAcymAttachLicense', [&$acyMailingKey]); |
| 210 | 218 | } |
| 219 | + | |
| 211 | 220 | $this->config->load(); |
| 212 | - $acyMailerLicenseKey = $this->config->get('acymailer_apikey', ''); | |
| 221 | + $acyMailerLicenseKey = $this->config->get('acymailer_apikey'); | |
| 222 | + | |
| 213 | 223 | if (empty($acyMailerLicenseKey)) { |
| 214 | 224 | acym_enqueueMessage(acym_translation('ACYM_LICENCE_NO_SENDING_SERVICE'), 'error'); |
| 215 | 225 | } else { |
| 216 | - $this->config->save(['mailer_method' => 'acymailer']); | |
| 226 | + $this->config->saveConfig(['mailer_method' => 'acymailer']); | |
| 217 | 227 | acym_enqueueMessage(acym_translation('ACYM_SENDING_SERVICE_ACTIVATED'), 'success', false); |
| 218 | 228 | } |
| 229 | + | |
| 219 | 230 | $this->listing(); |
| 220 | 231 | } |
| 221 | 232 | } |