-
Notifications
You must be signed in to change notification settings - Fork 9
/
cdn.php
104 lines (89 loc) · 3.69 KB
/
cdn.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
class CdnPlugin extends Plugin
{
/** @var Config $config */
protected $config;
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
/**
* Initialize configuration
*/
public function onPluginsInitialized()
{
if ($this->isAdmin()) {
$this->active = false;
return;
}
$this->enable([
'onOutputGenerated' => ['onOutputGenerated', 0]
]);
}
public function onOutputGenerated()
{
$config = $this->grav['config']->get('plugins.cdn');
$format = $this->grav['uri']->extension() ?: 'html';
// only process for HTML pages
if (!in_array($format, (array) $config['valid_formats'])) {
return;
}
// set the protocol to HTTPS if you access that way
if ( (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') ||
( isset($config['forcehttps']) && $config['forcehttps'] == true))
{
$protocol = 'https://';
$pullzone = isset($config['pullzone_ssl']) ? $config['pullzone_ssl'] : $config['pullzone'];
} else {
$protocol = 'http://';
$pullzone = $config['pullzone'];
}
$pullzone = $protocol . $pullzone;
$base = str_replace('/', '\/', $this->grav['base_url_relative']);
$extensions = $config['extensions'];
$tag_attributes = $config['tag_attributes'];
$tags = $config['tags'];
// match all pre/code blocks
preg_match_all("/<(pre|code)((?:(?!<\/\\1).)*?)<\/\\1>/uis", $this->grav->output, $blocks);
// https://regex101.com/r/pI3tF7/5 -> (<(?:a|img|link|script)[^>]+(?:href|src)=\"(?:(?!(?:[a-z-+]{1,}?:)?\/{2})))([^\"]+(?:)(\.(?:jpe?g|png|gif|ttf|otf|svg|woff|xml|js|css)(?:(?!(?:\?|&)nocdn).*?))(?<!(\?|&)nocdn))\"
$regex = "/(<(?:" . $tags . ")[^>]+(?:" . $tag_attributes . ")=\"(?:(?!(?:[a-z-+]{1,}?:)?\/{2})))([^\"]+(\.(?:" . $extensions . ")(?:(?!(?:\?|&)nocdn).*?))(?<!(\?|&)nocdn))\"/i";
$this->grav->output = preg_replace_callback(
$regex,
function ($matches) use ($blocks, $pullzone) {
$isBlock = $this->array_search_partial($blocks[0], $matches[0]);
return $isBlock ? $matches[0] : $matches[1] . $pullzone . $matches[2] . '"';
},
$this->grav->output
);
// replacements for inline CSS url() style references
if ($config['inline_css_replace']) {
// https://regex101.com/r/8zAnec/2 -> (url\([\'\"])(?:)(.*?\.(?:jpe?g|png|gif|ttf|otf|svg|woff|xml|js|css))(.*?\);)/i
// or with $base
// https://regex101.com/r/g0R6sj/2 -> (url\([\'\"])(?:http:\/\/github\.com)(.*?\.(?:jpe?g|png|gif|ttf|otf|svg|woff|xml|js|css))(.*?\);)/i
$regex = "/(url\([\'\"]?)(?:" . $base . ")(.*?\.(?:" . $extensions . "))(.*?\);)/i";
$this->grav->output = preg_replace_callback(
$regex,
function ($matches) use ($blocks, $pullzone) {
$isBlock = $this->array_search_partial($blocks[0], $matches[0]);
return $isBlock ? $matches[0] : $matches[1] . $pullzone . $matches[2] . $matches[3];
},
$this->grav->output
);
}
}
private function array_search_partial($arr, $keyword)
{
foreach ($arr as $index => $string) {
if (strpos($string, $keyword) !== false) {
return $index;
}
}
}
}