Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
Added Settings class
Browse files Browse the repository at this point in the history
- Future-friendly settings organisation
  • Loading branch information
S1SYPHOS committed Nov 17, 2017
1 parent bbbca2f commit 35d64b3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
4 changes: 2 additions & 2 deletions core/css.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Asset;
use f;
use c;
use settings;
use html;

class CSS extends \Kirby\Component\CSS {
Expand Down Expand Up @@ -50,7 +50,7 @@ public function tag($url, $media = null) {
// build an array of SRI-related attributes
$cssOptions = array(
'integrity' => $cssIntegrity, // generated SRI hash
'crossorigin' => c::get('plugin.kirby-sri.use-credentials') ? 'use-credentials' : 'anonymous' // user-defined 'crossorigin' attribute
'crossorigin' => settings::crossorigin(), // user-defined 'crossorigin' attribute
);
}

Expand Down
3 changes: 1 addition & 2 deletions core/js.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Asset;
use f;
use c;
use html;

class JS extends \Kirby\Component\JS {
Expand Down Expand Up @@ -50,7 +49,7 @@ public function tag($src, $async = false) {
// build an array of SRI-related attributes
$jsOptions = array(
'integrity' => $jsIntegrity, // generated SRI hash
'crossorigin' => c::get('plugin.kirby-sri.use-credentials') ? 'use-credentials' : 'anonymous' // user-defined 'crossorigin' attribute
'crossorigin' => settings::crossorigin(), // user-defined 'crossorigin' attribute
);
}

Expand Down
25 changes: 24 additions & 1 deletion kirby-sri.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,32 @@

if(!c::get('plugin.kirby-sri')) return;

class Settings {

/**
* Returns the default options for `kirby-sri`
*
* @return array
*/

public static function __callStatic($name, $args) {
// Set prefix
$prefix = 'plugin.kirby-sri.';
// Set config names and fallbacks as settings
$settings = [
'algorithm' => 'sha512', // Cryptographic hash algorithm
'crossorigin' => 'anonymous', // CORS settings attribute
];
// If config settings exist, return the config with fallback
if(isset($settings) && array_key_exists($name, $settings)) {
return c::get($prefix . $name, $settings[$name]);
}
}
}

// Helper function generating base64-encoded SRI hashes
function sri_checksum($input) {
$algorithm = c::get('plugin.kirby-sri.algorithm') ? c::get('plugin.kirby-sri.algorithm') : 'sha512';
$algorithm = settings::algorithm();
$hash = hash($algorithm, $input, true);
$hash_base64 = base64_encode($hash);

Expand Down

0 comments on commit 35d64b3

Please sign in to comment.