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

Commit

Permalink
Releasing v0.6.0 🍾
Browse files Browse the repository at this point in the history
Removing Settings class & replacing it with standard Kirby `c::get`
Changing namespace from S1SYPHOS\SRI to Kirby\Plugins\SRI
  • Loading branch information
S1SYPHOS committed Mar 3, 2018
1 parent d92a67d commit 8bdd25c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 39 deletions.
8 changes: 4 additions & 4 deletions core/css.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace S1SYPHOS\SRI;
namespace Kirby\Plugins\SRI;

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

class CSS extends \Kirby\Component\CSS {
Expand Down Expand Up @@ -41,7 +41,7 @@ public function tag($url, $media = null) {
$cssInput = (new Asset($url))->content();
$cssIntegrity = sri_checksum($cssInput);

if(settings::fingerprinting()) {
if(c::get('fingerprinting')) {
// add timestamp for cache-busting
$modified = filemtime($url);
$filename = f::name($url) . '.' . $modified . '.' . f::extension($url);
Expand All @@ -52,7 +52,7 @@ public function tag($url, $media = null) {
// build an array of SRI-related attributes
$cssOptions = array(
'integrity' => $cssIntegrity, // generated SRI hash
'crossorigin' => settings::crossorigin(), // user-defined 'crossorigin' attribute
'crossorigin' => c::get('plugin.kirby-sri.crossorigin', 'anonymous'), // user-defined 'crossorigin' attribute
);
}

Expand Down
8 changes: 4 additions & 4 deletions core/js.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace S1SYPHOS\SRI;
namespace Kirby\Plugins\SRI;

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

class JS extends \Kirby\Component\JS {
Expand Down Expand Up @@ -41,7 +41,7 @@ public function tag($src, $async = false) {
$jsInput = (new Asset($src))->content();
$jsIntegrity = sri_checksum($jsInput);

if(settings::fingerprinting()) {
if(c::get('fingerprinting')) {
// add timestamp for cache-busting
$modified = filemtime($src);
$filename = f::name($src) . '.' . $modified . '.' . f::extension($src);
Expand All @@ -52,7 +52,7 @@ public function tag($src, $async = false) {
// build an array of SRI-related attributes
$jsOptions = array(
'integrity' => $jsIntegrity, // generated SRI hash
'crossorigin' => settings::crossorigin(), // user-defined 'crossorigin' attribute
'crossorigin' => c::get('plugin.kirby-sri.crossorigin', 'anonymous'), // user-defined 'crossorigin' attribute
);
}

Expand Down
36 changes: 6 additions & 30 deletions kirby-sri.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,15 @@
* @package Kirby CMS
* @author S1SYPHOS <[email protected]>
* @link http://twobrain.io
* @version 0.5.1
* @version 0.6.0
* @license MIT
*/

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
'fingerprinting' => true, // Enables / disables fingerprinting
];
// 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 = settings::algorithm();
$algorithm = c::get('plugin.kirby-sri.algorithm', 'sha512');
$hash = hash($algorithm, $input, true);
$hash_base64 = base64_encode($hash);

Expand All @@ -47,10 +23,10 @@ function sri_checksum($input) {

// Loading core
load([
's1syphos\\sri\\css' => __DIR__ . DS . 'core' . DS . 'css.php',
's1syphos\\sri\\js' => __DIR__ . DS . 'core' . DS . 'js.php'
'kirby\\plugins\\sri\\css' => __DIR__ . DS . 'core' . DS . 'css.php',
'kirby\\plugins\\sri\\js' => __DIR__ . DS . 'core' . DS . 'js.php'
]);

// Registering with Kirby's extension registry
kirby()->set('component', 'css', 'S1SYPHOS\\SRI\\CSS');
kirby()->set('component', 'js', 'S1SYPHOS\\SRI\\JS');
kirby()->set('component', 'css', 'Kirby\Plugins\\SRI\\CSS');
kirby()->set('component', 'js', 'Kirby\Plugins\\SRI\\JS');
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "kirby-sri",
"description": "Subresource integrity hashing & cache-busting static assets for Kirby",
"author": "S1SYPHOS <[email protected]>",
"version": "0.5.1",
"version": "0.6.0",
"type": "kirby-plugin",
"license": "MIT"
}

0 comments on commit 8bdd25c

Please sign in to comment.