Skip to content

Commit

Permalink
CRAFT_LICENSE_KEY constant
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Jun 1, 2018
1 parent 5ba5bf8 commit 802054d
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 46 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`.
Expand Down
47 changes: 25 additions & 22 deletions bootstrap/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions src/controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 10 additions & 9 deletions src/helpers/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 19 additions & 11 deletions src/helpers/Cp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<a href="http://'.$licensedDomain.'" target="_blank">'.$licensedDomain.'</a>';

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' => '<a href="http://'.$licensedDomain.'" target="_blank">'.$licensedDomain.'</a>'
]).
' <a class="go" href="https://craftcms.com/support/resolving-mismatched-licenses">'.Craft::t('app', 'Learn more').'</a>';
$alerts[] = $message.' <a class="go" href="https://craftcms.com/support/resolving-mismatched-licenses">'.Craft::t('app', 'Learn more').'</a>';
}

// Any plugin issues?
Expand Down
2 changes: 1 addition & 1 deletion src/services/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down

0 comments on commit 802054d

Please sign in to comment.