Skip to content

Commit

Permalink
Merge pull request #15 from paddling/parsenv
Browse files Browse the repository at this point in the history
Fix X-Disqus-Publisher-API-Key header value when environment variable is used
  • Loading branch information
khalwat authored Oct 6, 2024
2 parents c897a5a + cfb15ee commit de65d85
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 65 deletions.
101 changes: 101 additions & 0 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,102 @@ class Settings extends Model
// Public Methods
// =========================================================================

/**
* @return string the parsed secret key (e.g. 'XXXXXXXXXXX')
*/
public function getDisqusSecretKey(): string
{
return Disqus::$craft31 ? Craft::parseEnv($this->disqusSecretKey) : $this->disqusSecretKey;

Check failure on line 102 in src/models/Settings.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to static method parseEnv() on an unknown class nystudio107\disqus\models\Craft.
}

/**
* @return string the parsed public key (e.g. 'XXXXXXXXXXX')
*/
public function getDisqusPublicKey(): string
{
return Disqus::$craft31 ? Craft::parseEnv($this->disqusPublicKey) : $this->disqusPublicKey;

Check failure on line 110 in src/models/Settings.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to static method parseEnv() on an unknown class nystudio107\disqus\models\Craft.
}

/**
* @return string
*/
public function getDisqusShortname(): string
{
return $this->disqusShortname;
}

/**
* @return bool
*/
public function getUseSSO(): bool
{
return $this->useSSO;
}

/**
* @return bool
*/
public function getCustomLogin(): bool
{
return $this->customLogin;
}

/**
* @return string
*/
public function getLoginName(): string
{
return Disqus::$craft31 ? Craft::parseEnv($this->loginName) : $this->loginName;

Check failure on line 142 in src/models/Settings.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to static method parseEnv() on an unknown class nystudio107\disqus\models\Craft.
}

/**
* @return string
*/
public function getLoginButton(): string
{
return Disqus::$craft31 ? Craft::parseEnv($this->loginButton) : $this->loginButton;

Check failure on line 150 in src/models/Settings.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to static method parseEnv() on an unknown class nystudio107\disqus\models\Craft.
}

/**
* @return string
*/
public function getLoginIcon(): string
{
return Disqus::$craft31 ? Craft::parseEnv($this->loginIcon) : $this->loginIcon;

Check failure on line 158 in src/models/Settings.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to static method parseEnv() on an unknown class nystudio107\disqus\models\Craft.
}

/**
* @return string
*/
public function getLoginUrl(): string
{
return Disqus::$craft31 ? Craft::parseEnv($this->loginUrl) : $this->loginUrl;

Check failure on line 166 in src/models/Settings.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to static method parseEnv() on an unknown class nystudio107\disqus\models\Craft.
}

/**
* @return string
*/
public function getLoginLogoutUrl(): string
{
return Disqus::$craft31 ? Craft::parseEnv($this->loginLogoutUrl) : $this->loginLogoutUrl;

Check failure on line 174 in src/models/Settings.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to static method parseEnv() on an unknown class nystudio107\disqus\models\Craft.
}

/**
* @return int
*/
public function getLoginWidth(): int
{
return $this->loginWidth;
}

/**
* @return int
*/
public function getLoginHeight(): int
{
return $this->loginHeight;
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -141,6 +237,11 @@ public function behaviors()
'parser' => [
'class' => EnvAttributeParserBehavior::class,
'attributes' => [
'loginName',
'loginButton',
'loginIcon',
'loginUrl',
'loginLogoutUrl',
'disqusPublicKey',
'disqusSecretKey',
],
Expand Down
46 changes: 21 additions & 25 deletions src/services/DisqusService.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function outputEmbedTag(
) {
/** @var Settings $settings */
$settings = Disqus::$plugin->getSettings();
$disqusShortname = $settings->disqusShortname;
$disqusShortname = $settings->getDisqusShortName();

$vars = [
'disqusShortname' => $disqusShortname,
Expand Down Expand Up @@ -87,13 +87,9 @@ public function getCommentsCount(
) {
/** @var Settings $settings */
$settings = Disqus::$plugin->getSettings();
if (Disqus::$craft31) {
$settings['disqusPublicKey'] = Craft::parseEnv($settings['disqusPublicKey']);
$settings['disqusSecretKey'] = Craft::parseEnv($settings['disqusSecretKey']);
}
if (!empty($settings['disqusPublicKey'])) {
$disqusShortname = $settings['disqusShortname'];
$apiKey = $settings["disqusPublicKey"];
if (!empty($settings->getDisqusPublicKey())) {
$disqusShortname = $settings->getDisqusShortname();
$apiKey = $settings->getDisqusPublicKey();

$url = "https://disqus.com/api/3.0/threads/details.json?api_key="
. $apiKey
Expand Down Expand Up @@ -139,7 +135,7 @@ protected function getSSOVars(): array
'useSSO' => false,
'useCustomLogin' => false,
];
if ($settings['useSSO']) {
if ($settings->getUseSSO()) {
$data = [];

// Set the data array
Expand All @@ -165,31 +161,31 @@ protected function getSSOVars(): array
$timestamp = time();
$hMac = $this->disqusHmacSha1(
$message
. ' '
. $timestamp,
$settings['disqusSecretKey']
.' '
.$timestamp,
$settings->getDisqusSecretKey()
);

// Set the vars for the template
$vars = array_merge($vars, [
'useSSO' => true,
'message' => $message,
'hmac' => $hMac,
'timestamp' => $timestamp,
'disqusPublicKey' => $settings['disqusPublicKey'],
'useSSO' => true,
'message' => $message,
'hmac' => $hMac,
'timestamp' => $timestamp,
'disqusPublicKey' => $settings->getDisqusPublicKey(),
]);

// Set the vars for the custom login
if ($settings['customLogin']) {
if ($settings->getCustomLogin()) {
$vars = array_merge($vars, [
'useCustomLogin' => true,
'loginName' => $settings['loginName'],
'loginButton' => $settings['loginButton'],
'loginIcon' => $settings['loginIcon'],
'loginUrl' => $settings['loginUrl'],
'loginLogoutUrl' => $settings['loginLogoutUrl'],
'loginWidth' => $settings['loginWidth'],
'loginHeight' => $settings['loginHeight'],
'loginName' => $settings->getLoginName(),
'loginButton' => $settings->getLoginButton(),
'loginIcon' => $settings->getLoginIcon(),
'loginUrl' => $settings->getLoginUrl(),
'loginLogoutUrl' => $settings->getLoginLogoutUrl(),
'loginWidth' => $settings->getLoginWidth(),
'loginHeight' => $settings->getLoginHeight(),
]);
}
}
Expand Down
132 changes: 92 additions & 40 deletions src/templates/settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -96,50 +96,102 @@
'warning': configWarning('customLogin')
}) }}

{{ forms.textField({
'label': 'name',
'instructions': 'Your site name. We will display it in the Post As window.',
'id': 'loginName',
'name': 'loginName',
'value': settings['loginName'],
'warning': configWarning('loginName')
}) }}
{% if craft.disqus.craft31 %}
{{ forms.autosuggestField({
'label': 'name',
'instructions': 'Your site name. We will display it in the Post As window.',
'id': 'loginName',
'name': 'loginName',
'value': settings['loginName'],
'warning': configWarning('loginName'),
suggestEnvVars: true,
}) }}
{{ forms.autosuggestField({
'label': 'button',
'instructions': 'Address of the image that acts as a button. Disqus 2012 users, see style guide below.',
'id': 'loginButton',
'name': 'loginButton',
'value': settings['loginButton'],
'warning': configWarning('loginButton'),
suggestEnvVars: true,
suggestAliases: true
}) }}
{{ forms.autosuggestField({
'label': 'icon',
'instructions': 'Address of the image that appears on the login modal SSO tab. Favicons work well here. (Not required in Disqus 2012.)',
'id': 'loginIcon',
'name': 'loginIcon',
'value': settings['loginIcon'],
'warning': configWarning('loginIcon'),
suggestEnvVars: true,
suggestAliases: true
}) }}
{{ forms.autosuggestField({
'label': 'url',
'instructions': 'Address of your login page. The page will be opened in a new window and it must close itself after authentication is done. That is how we know when it is done and reload the page.',
'id': 'loginUrl',
'name': 'loginUrl',
'value': settings['loginUrl'],
'warning': configWarning('loginUrl'),
suggestEnvVars: true,
suggestAliases: true
}) }}
{{ forms.autosuggestField({
'label': 'logout',
'instructions': 'Address of your logout page. This page must redirect user back to the original page after logout.',
'id': 'loginLogoutUrl',
'name': 'loginLogoutUrl',
'value': settings['loginLogoutUrl'],
'warning': configWarning('loginLogoutUrl'),
suggestEnvVars: true,
suggestAliases: true
}) }}
{% else %}
{{ forms.textField({
'label': 'name',
'instructions': 'Your site name. We will display it in the Post As window.',
'id': 'loginName',
'name': 'loginName',
'value': settings['loginName'],
'warning': configWarning('loginName')
}) }}

{{ forms.textField({
'label': 'button',
'instructions': 'Address of the image that acts as a button. Disqus 2012 users, see style guide below.',
'id': 'loginButton',
'name': 'loginButton',
'value': settings['loginButton'],
'warning': configWarning('loginButton')
}) }}
{{ forms.textField({
'label': 'button',
'instructions': 'Address of the image that acts as a button. Disqus 2012 users, see style guide below.',
'id': 'loginButton',
'name': 'loginButton',
'value': settings['loginButton'],
'warning': configWarning('loginButton')
}) }}

{{ forms.textField({
'label': 'icon',
'instructions': 'Address of the image that appears on the login modal SSO tab. Favicons work well here. (Not required in Disqus 2012.)',
'id': 'loginIcon',
'name': 'loginIcon',
'value': settings['loginIcon'],
'warning': configWarning('loginIcon')
}) }}
{{ forms.textField({
'label': 'icon',
'instructions': 'Address of the image that appears on the login modal SSO tab. Favicons work well here. (Not required in Disqus 2012.)',
'id': 'loginIcon',
'name': 'loginIcon',
'value': settings['loginIcon'],
'warning': configWarning('loginIcon')
}) }}

{{ forms.textField({
'label': 'url',
'instructions': 'Address of your login page. The page will be opened in a new window and it must close itself after authentication is done. That is how we know when it is done and reload the page.',
'id': 'loginUrl',
'name': 'loginUrl',
'value': settings['loginUrl'],
'warning': configWarning('loginUrl')
}) }}
{{ forms.textField({
'label': 'url',
'instructions': 'Address of your login page. The page will be opened in a new window and it must close itself after authentication is done. That is how we know when it is done and reload the page.',
'id': 'loginUrl',
'name': 'loginUrl',
'value': settings['loginUrl'],
'warning': configWarning('loginUrl')
}) }}

{{ forms.textField({
'label': 'logout',
'instructions': 'Address of your logout page. This page must redirect user back to the original page after logout.',
'id': 'loginLogoutUrl',
'name': 'loginLogoutUrl',
'value': settings['loginLogoutUrl'],
'warning': configWarning('loginLogoutUrl')
}) }}
{{ forms.textField({
'label': 'logout',
'instructions': 'Address of your logout page. This page must redirect user back to the original page after logout.',
'id': 'loginLogoutUrl',
'name': 'loginLogoutUrl',
'value': settings['loginLogoutUrl'],
'warning': configWarning('loginLogoutUrl')
}) }}
{% endif %}

{{ forms.textField({
'label': 'width',
Expand Down

0 comments on commit de65d85

Please sign in to comment.