From 802054d5016bb78b09438b703d006a67721f4fe9 Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Fri, 1 Jun 2018 13:05:36 -0700 Subject: [PATCH] CRAFT_LICENSE_KEY constant --- CHANGELOG-v3.md | 1 + bootstrap/bootstrap.php | 47 +++++++++++++------------ src/controllers/DashboardController.php | 5 ++- src/helpers/App.php | 19 +++++----- src/helpers/Cp.php | 30 ++++++++++------ src/services/Api.php | 2 +- 6 files changed, 58 insertions(+), 46 deletions(-) diff --git a/CHANGELOG-v3.md b/CHANGELOG-v3.md index bcafc4128c5..3c1604c091c 100644 --- a/CHANGELOG-v3.md +++ b/CHANGELOG-v3.md @@ -3,6 +3,7 @@ ## Unreleased ### Added +- Added support for a `CRAFT_LICENSE_KEY` PHP constant, which can be set to the project’s license key, taking precedence over the `license.key` file. - Added `craft\helpers\Stringy::getLangSpecificCharsArray()`. - Added `craft\web\View::setRegisteredAssetBundles()`. - Added `craft\web\View::setRegisteredJsFiles()`. diff --git a/bootstrap/bootstrap.php b/bootstrap/bootstrap.php index 7dca9818b30..a3ac72ac6ef 100644 --- a/bootstrap/bootstrap.php +++ b/bootstrap/bootstrap.php @@ -110,37 +110,40 @@ // Validate the paths // ----------------------------------------------------------------------------- -// Validate permissions on the license key file path (default config/) and storage/ -if (defined('CRAFT_LICENSE_KEY_PATH')) { - $licensePath = dirname(CRAFT_LICENSE_KEY_PATH); - $licenseKeyName = basename(CRAFT_LICENSE_KEY_PATH); -} else { - $licensePath = $configPath; - $licenseKeyName = 'license.key'; -} +if (!defined('CRAFT_LICENSE_KEY')) { + // Validate permissions on the license key file path (default config/) and storage/ + if (defined('CRAFT_LICENSE_KEY_PATH')) { + $licensePath = dirname(CRAFT_LICENSE_KEY_PATH); + $licenseKeyName = basename(CRAFT_LICENSE_KEY_PATH); + } else { + $licensePath = $configPath; + $licenseKeyName = 'license.key'; + } -// Make sure the license folder exists. -if (!is_dir($licensePath) && !file_exists($licensePath)) { - $createFolder($licensePath); -} + // Make sure the license folder exists. + if (!is_dir($licensePath) && !file_exists($licensePath)) { + $createFolder($licensePath); + } -$ensureFolderIsReadable($licensePath); + $ensureFolderIsReadable($licensePath); -if ($appType === 'web') { - $licenseFullPath = $licensePath.'/'.$licenseKeyName; + if ($appType === 'web') { + $licenseFullPath = $licensePath.'/'.$licenseKeyName; - // If the license key doesn't exist yet, make sure the folder is readable and we can write a temp one. - if (!file_exists($licenseFullPath)) { - // Try and write out a temp license key file. - @file_put_contents($licenseFullPath, 'temp'); + // If the license key doesn't exist yet, make sure the folder is readable and we can write a temp one. + if (!file_exists($licenseFullPath)) { + // Try and write out a temp license key file. + @file_put_contents($licenseFullPath, 'temp'); - // See if it worked. - if (!file_exists($licenseFullPath) || (file_exists($licenseFullPath) && file_get_contents($licenseFullPath) !== 'temp')) { - exit($licensePath.' isn\'t writable by PHP. Please fix that.'); + // See if it worked. + if (!file_exists($licenseFullPath) || (file_exists($licenseFullPath) && file_get_contents($licenseFullPath) !== 'temp')) { + exit($licensePath.' isn\'t writable by PHP. Please fix that.'); + } } } } + $ensureFolderIsReadable($storagePath, true); // Create the storage/runtime/ folder if it doesn't already exist diff --git a/src/controllers/DashboardController.php b/src/controllers/DashboardController.php index 6276d32f40d..df5a63163dd 100644 --- a/src/controllers/DashboardController.php +++ b/src/controllers/DashboardController.php @@ -361,9 +361,8 @@ public function actionSendSupportRequest(): Response } // License key - $licenseKeyPath = Craft::$app->getPath()->getLicenseKeyPath(); - if (is_file($licenseKeyPath)) { - $zip->addFile($licenseKeyPath, 'license.key'); + if (($licenseKey = App::licenseKey()) !== null) { + $zip->addFromString('license.key', $licenseKey); } // Logs diff --git a/src/helpers/App.php b/src/helpers/App.php index 89ab2368dd9..549e20e98e1 100644 --- a/src/helpers/App.php +++ b/src/helpers/App.php @@ -168,19 +168,20 @@ public static function maxPowerCaptain() */ public static function licenseKey() { - $path = Craft::$app->getPath()->getLicenseKeyPath(); + if (defined('CRAFT_LICENSE_KEY')) { + $licenseKey = CRAFT_LICENSE_KEY; + } else { + $path = Craft::$app->getPath()->getLicenseKeyPath(); - // Check to see if the key exists and it's not a temp one. - if (!is_file($path)) { - return null; - } + // Check to see if the key exists + if (!is_file($path)) { + return null; + } - $contents = file_get_contents($path); - if (empty($contents) || $contents === 'temp') { - return null; + $licenseKey = file_get_contents($path); } - $licenseKey = trim(preg_replace('/[\r\n]+/', '', $contents)); + $licenseKey = trim(preg_replace('/[\r\n]+/', '', $licenseKey)); if (strlen($licenseKey) !== 250) { return null; diff --git a/src/helpers/Cp.php b/src/helpers/Cp.php index 484c4b6f982..2435ace1a6c 100644 --- a/src/helpers/Cp.php +++ b/src/helpers/Cp.php @@ -81,20 +81,28 @@ public static function alerts(string $path = null, bool $fetch = false): array // Domain mismatch? if ($licenseKeyStatus === LicenseKeyStatus::Mismatched) { $licensedDomain = Craft::$app->getCache()->get('licensedDomain'); + $domainLink = ''.$licensedDomain.''; + + if (defined('CRAFT_LICENSE_KEY')) { + $message = Craft::t('app', 'The license key in use belongs to {domain}', [ + 'domain' => $domainLink + ]); + } else { + $keyPath = Craft::$app->getPath()->getLicenseKeyPath(); + + // If the license key path starts with the root project path, trim the project path off + $rootPath = Craft::getAlias('@root'); + if (strpos($keyPath, $rootPath.'/') === 0) { + $keyPath = substr($keyPath, strlen($rootPath) + 1); + } - $keyPath = Craft::$app->getPath()->getLicenseKeyPath(); - - // If the license key path starts with the root project path, trim the project path off - $rootPath = Craft::getAlias('@root'); - if (strpos($keyPath, $rootPath.'/') === 0) { - $keyPath = substr($keyPath, strlen($rootPath) + 1); + $message = Craft::t('app', 'The license located at {file} belongs to {domain}.', [ + 'file' => $keyPath, + 'domain' => $domainLink + ]); } - $alerts[] = Craft::t('app', 'The license located at {file} belongs to {domain}.', [ - 'file' => $keyPath, - 'domain' => ''.$licensedDomain.'' - ]). - ' '.Craft::t('app', 'Learn more').''; + $alerts[] = $message.' '.Craft::t('app', 'Learn more').''; } // Any plugin issues? diff --git a/src/services/Api.php b/src/services/Api.php index a4d941c7ba4..5d17ae6f204 100644 --- a/src/services/Api.php +++ b/src/services/Api.php @@ -450,7 +450,7 @@ protected function headers(): array } // Craft license - $headers['X-Craft-License'] = App::licenseKey() ?? '🙏'; + $headers['X-Craft-License'] = App::licenseKey() ?? (defined('CRAFT_LICENSE_KEY') ? '😱' : '🙏'); // plugin info $pluginLicenses = [];