| @@ -1,0 +1,232 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace AcyMailing\Controllers\Configuration; | |
| 4 | + | |
| 5 | +use AcyMailing\Helpers\UpdatemeHelper; | |
| 6 | + | |
| 7 | +trait License | |
| 8 | +{ | |
| 9 | + public function unlinkLicense(): void | |
| 10 | + { | |
| 11 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 12 | + | |
| 13 | + $config = acym_getVar('array', 'config', []); | |
| 14 | + $licenseKey = empty($config['license_key']) ? $this->config->get('license_key') : $config['license_key']; | |
| 15 | + | |
| 16 | + $resultUnlinkLicenseOnUpdateMe = $this->unlinkLicenseOnUpdateMe($licenseKey); | |
| 17 | + | |
| 18 | + if ($resultUnlinkLicenseOnUpdateMe['success'] === true) { | |
| 19 | + $this->config->saveConfig(['license_key' => '']); | |
| 20 | + UpdatemeHelper::getLicenseInfo(); | |
| 21 | + } | |
| 22 | + | |
| 23 | + if (!empty($resultUnlinkLicenseOnUpdateMe['message'])) { | |
| 24 | + $this->displayMessage($resultUnlinkLicenseOnUpdateMe['message']); | |
| 25 | + } | |
| 26 | + | |
| 27 | + //Display the configuration | |
| 28 | + $this->listing(); | |
| 29 | + } | |
| 30 | + | |
| 31 | + public function attachLicense(): void | |
| 32 | + { | |
| 33 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 34 | + | |
| 35 | + | |
| 36 | + $config = acym_getVar('array', 'config', []); | |
| 37 | + if (empty($config['license_key'])) { | |
| 38 | + $this->displayMessage(acym_translation('ACYM_PLEASE_SET_A_LICENSE_KEY')); | |
| 39 | + $this->listing(); | |
| 40 | + | |
| 41 | + return; | |
| 42 | + } | |
| 43 | + | |
| 44 | + //We save the license key | |
| 45 | + $this->config->saveConfig(['license_key' => $config['license_key']]); | |
| 46 | + | |
| 47 | + //We call updateme to attach the website to the license | |
| 48 | + $resultAttachLicenseOnUpdateMe = $this->attachLicenseOnUpdateMe(); | |
| 49 | + | |
| 50 | + if ($resultAttachLicenseOnUpdateMe['success'] === false) { | |
| 51 | + $this->config->saveConfig(['license_key' => '']); | |
| 52 | + } else { | |
| 53 | + UpdatemeHelper::getLicenseInfo(); | |
| 54 | + } | |
| 55 | + | |
| 56 | + if (!empty($resultAttachLicenseOnUpdateMe['message'])) { | |
| 57 | + $this->displayMessage($resultAttachLicenseOnUpdateMe['message']); | |
| 58 | + } | |
| 59 | + | |
| 60 | + $this->listing(); | |
| 61 | + } | |
| 62 | + | |
| 63 | + public function attachLicenseOnUpdateMe(?string $licenseKey = null): array | |
| 64 | + { | |
| 65 | + | |
| 66 | + //We get the license key saved | |
| 67 | + if (is_null($licenseKey)) { | |
| 68 | + $licenseKey = $this->config->get('license_key', ''); | |
| 69 | + } | |
| 70 | + | |
| 71 | + $return = [ | |
| 72 | + 'message' => '', | |
| 73 | + 'success' => false, | |
| 74 | + ]; | |
| 75 | + | |
| 76 | + if (empty($licenseKey)) { | |
| 77 | + $return['message'] = 'LICENSE_NOT_FOUND'; | |
| 78 | + | |
| 79 | + return $return; | |
| 80 | + } | |
| 81 | + | |
| 82 | + $data = [ | |
| 83 | + 'domain' => ACYM_LIVE, | |
| 84 | + 'cms' => ACYM_CMS, | |
| 85 | + 'version' => $this->config->get('version'), | |
| 86 | + ]; | |
| 87 | + | |
| 88 | + $resultAttach = UpdatemeHelper::call('api/websites/attach', 'POST', $data); | |
| 89 | + | |
| 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']) { | |
| 93 | + return $return; | |
| 94 | + } | |
| 95 | + | |
| 96 | + acym_trigger('onAcymAttachLicense', [&$licenseKey]); | |
| 97 | + | |
| 98 | + return $resultAttach; | |
| 99 | + } | |
| 100 | + | |
| 101 | + private function unlinkLicenseOnUpdateMe(?string $licenseKey = null): array | |
| 102 | + { | |
| 103 | + //We get the license key saved | |
| 104 | + if (is_null($licenseKey)) { | |
| 105 | + $licenseKey = $this->config->get('license_key'); | |
| 106 | + } | |
| 107 | + | |
| 108 | + $return = [ | |
| 109 | + 'message' => '', | |
| 110 | + 'success' => false, | |
| 111 | + ]; | |
| 112 | + | |
| 113 | + if (empty($licenseKey)) { | |
| 114 | + $return['message'] = 'LICENSE_NOT_FOUND'; | |
| 115 | + | |
| 116 | + return $return; | |
| 117 | + } | |
| 118 | + | |
| 119 | + //First let's deactivate the cron | |
| 120 | + $this->deactivateCron(false, $licenseKey); | |
| 121 | + | |
| 122 | + $data = [ | |
| 123 | + 'domain' => ACYM_LIVE, | |
| 124 | + ]; | |
| 125 | + | |
| 126 | + //Call updateme to unlink the license from this website | |
| 127 | + $resultUnlink = UpdatemeHelper::call('api/websites/unlink', 'POST', $data); | |
| 128 | + | |
| 129 | + //If it's not the result well formated => out | |
| 130 | + if (empty($resultUnlink) || !$resultUnlink['success']) { | |
| 131 | + return $return; | |
| 132 | + } | |
| 133 | + | |
| 134 | + acym_trigger('onAcymDetachLicense'); | |
| 135 | + | |
| 136 | + return $resultUnlink; | |
| 137 | + } | |
| 138 | + | |
| 139 | + public function activateCron(): void | |
| 140 | + { | |
| 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 | + | |
| 150 | + $this->listing(); | |
| 151 | + } | |
| 152 | + | |
| 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 | |
| 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 | + | |
| 162 | + $result = $this->modifyCron('deactivateCron', $licenseKey); | |
| 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 | + } | |
| 167 | + | |
| 168 | + if ($listing) { | |
| 169 | + $this->listing(); | |
| 170 | + } | |
| 171 | + } | |
| 172 | + | |
| 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 | |
| 175 | + { | |
| 176 | + if (is_null($licenseKey)) { | |
| 177 | + $config = acym_getVar('array', 'config', []); | |
| 178 | + $licenseKey = empty($config['license_key']) ? '' : $config['license_key']; | |
| 179 | + } | |
| 180 | + | |
| 181 | + //If the license is not set => out | |
| 182 | + if (empty($licenseKey)) { | |
| 183 | + $this->displayMessage('LICENSE_NOT_FOUND'); | |
| 184 | + | |
| 185 | + return []; | |
| 186 | + } | |
| 187 | + | |
| 188 | + $data = [ | |
| 189 | + 'domain' => ACYM_LIVE, | |
| 190 | + 'cms' => ACYM_CMS, | |
| 191 | + 'version' => $this->config->get('version'), | |
| 192 | + 'level' => $this->config->get('level'), | |
| 193 | + 'activate' => $functionToCall === 'activateCron', | |
| 194 | + 'security_key' => $this->config->get('cron_key'), | |
| 195 | + ]; | |
| 196 | + | |
| 197 | + //We call updateme to activate/deactivate the cron | |
| 198 | + $result = UpdatemeHelper::call('api/crons/modify', 'POST', $data); | |
| 199 | + | |
| 200 | + //If it's not the result well formated => out | |
| 201 | + if (empty($result['success'])) { | |
| 202 | + $this->displayMessage(empty($result['message']) ? 'CRON_NOT_SAVED' : $result['message']); | |
| 203 | + | |
| 204 | + return []; | |
| 205 | + } | |
| 206 | + | |
| 207 | + return $result; | |
| 208 | + } | |
| 209 | + | |
| 210 | + public function attachLicenseAcymailer(): void | |
| 211 | + { | |
| 212 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 213 | + | |
| 214 | + $acyMailerLicenseKey = $this->config->get('acymailer_apikey'); | |
| 215 | + $acyMailingKey = $this->config->get('license_key'); | |
| 216 | + if (empty($acyMailerLicenseKey) && !empty($acyMailingKey)) { | |
| 217 | + acym_trigger('onAcymAttachLicense', [&$acyMailingKey]); | |
| 218 | + } | |
| 219 | + | |
| 220 | + $this->config->load(); | |
| 221 | + $acyMailerLicenseKey = $this->config->get('acymailer_apikey'); | |
| 222 | + | |
| 223 | + if (empty($acyMailerLicenseKey)) { | |
| 224 | + acym_enqueueMessage(acym_translation('ACYM_LICENCE_NO_SENDING_SERVICE'), 'error'); | |
| 225 | + } else { | |
| 226 | + $this->config->saveConfig(['mailer_method' => 'acymailer']); | |
| 227 | + acym_enqueueMessage(acym_translation('ACYM_SENDING_SERVICE_ACTIVATED'), 'success', false); | |
| 228 | + } | |
| 229 | + | |
| 230 | + $this->listing(); | |
| 231 | + } | |
| 232 | +} | |