Skip to content

CDN - remove filetypes to replace if setting is disabled #800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: dev
Choose a base branch
from
2 changes: 1 addition & 1 deletion data/const.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,6 @@
"inc_js": ["1"],
"inc_css": ["1"],
"inc_img": ["1"],
"filetype": [".aac\n.css\n.eot\n.gif\n.jpeg\n.jpg\n.js\n.less\n.mp3\n.mp4\n.ogg\n.otf\n.pdf\n.png\n.svg\n.ttf\n.webp\n.woff\n.woff2"]
"filetype": [".aac\n.eot\n.less\n.mp3\n.mp4\n.ogg\n.otf\n.pdf\n.ttf\n.webp\n.woff\n.woff2"]
}
}
76 changes: 45 additions & 31 deletions src/admin-settings.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public function save( $raw_data ) {
}
switch ($id) {
case self::O_CRAWLER_ROLES: // Don't allow Editor/admin to be used in crawler role simulator
$data = Utility::sanitize_lines($data);
if ($data) {
$data = Utility::sanitize_lines($data);
if ($data) {
foreach ($data as $k => $v) {
if (user_can($v, 'edit_posts')) {
$msg = sprintf(
Expand All @@ -99,45 +99,59 @@ public function save( $raw_data ) {
}
break;
case self::O_CDN_MAPPING:
/**
* CDN setting
*
* Raw data format:
* cdn-mapping[url][] = 'xxx'
* cdn-mapping[url][2] = 'xxx2'
* cdn-mapping[inc_js][] = 1
*
* Final format:
* cdn-mapping[ 0 ][ url ] = 'xxx'
* cdn-mapping[ 2 ][ url ] = 'xxx2'
*/
if ($data) {
/**
* CDN setting
*
* Raw data format:
* cdn-mapping[url][] = 'xxx'
* cdn-mapping[url][2] = 'xxx2'
* cdn-mapping[inc_js][] = 1
*
* Final format:
* cdn-mapping[ 0 ][ url ] = 'xxx'
* cdn-mapping[ 2 ][ url ] = 'xxx2'
*/
if ($data && is_array($data)) {
foreach ($data as $k => $v) {
if ($child == self::CDN_MAPPING_FILETYPE) {
if ($child == self::CDN_MAPPING_FILETYPE) {
$v = Utility::sanitize_lines($v);
}
if ($child == self::CDN_MAPPING_URL) {
// If not a valid URL, turn off CDN

// Remove from MAPPING FILETYPE extensions for IMAGES, CSS, JS
$remove_type = apply_filters('litespeed_cdn_save_filetypes_remove', array(
'.jpg',
'.jpeg',
'.png',
'.gif',
'.svg',
'.webp',
'.avif',
'.css',
'.js',
));
$v = array_values(array_diff($v, $remove_type));
}
if ($child == self::CDN_MAPPING_URL) {
# If not a valid URL, turn off CDN
if (strpos($v, 'https://') !== 0) {
self::debug('❌ CDN mapping set to OFF due to invalid URL');
$the_matrix[self::O_CDN] = false;
}
self::debug('❌ CDN mapping set to OFF due to invalid URL');
$the_matrix[self::O_CDN] = false;
}
$v = trailingslashit($v);
}
if (in_array($child, array( self::CDN_MAPPING_INC_IMG, self::CDN_MAPPING_INC_CSS, self::CDN_MAPPING_INC_JS ))) {
}
if (in_array($child, array(self::CDN_MAPPING_INC_IMG, self::CDN_MAPPING_INC_CSS, self::CDN_MAPPING_INC_JS))) {
// Because these can't be auto detected in `config->update()`, need to format here
$v = $v === 'false' ? 0 : (bool) $v;
}
}

if (empty($data2[$k])) {
if (empty($data2[$k])) {
$data2[$k] = array();
}

$data2[$k][$child] = $v;
}

$data2[$k][$child] = $v;
}
}

$data = $data2;
$data = $data2;
break;

case self::O_CRAWLER_COOKIES:
Expand Down Expand Up @@ -209,7 +223,7 @@ public function save( $raw_data ) {

$the_matrix[$id] = $data;
}

// Special handler for CDN/Crawler 2d list to drop empty rows
foreach ($the_matrix as $id => $data) {
/**
Expand Down
37 changes: 19 additions & 18 deletions src/cdn.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,24 @@ public function init() {
}
}

// Add IMAGES to rewrite if CDN Mapping setting is enabled
if (!empty($this->_cfg_cdn_mapping[Base::CDN_MAPPING_INC_IMG])) {
$add_images_type = apply_filters('litespeed_cdn_add_filetypes_image', array('.jpg', '.jpeg', '.png', '.gif', '.svg', '.webp', '.avif'));
foreach ($add_images_type as $ext) {
$this->_cfg_cdn_mapping[$ext] = $this->_cfg_cdn_mapping[Base::CDN_MAPPING_INC_IMG];
}
}

// Add CSS to rewrite if CDN Mapping setting is enabled
if (!empty($this->_cfg_cdn_mapping[Base::CDN_MAPPING_INC_CSS])) {
$this->_cfg_cdn_mapping['.css'] = $this->_cfg_cdn_mapping[Base::CDN_MAPPING_INC_CSS];
}

// Add JS to rewrite if CDN Mapping setting is enabled
if (!empty($this->_cfg_cdn_mapping[Base::CDN_MAPPING_INC_JS])) {
$this->_cfg_cdn_mapping['.js'] = $this->_cfg_cdn_mapping[Base::CDN_MAPPING_INC_JS];
}

if (!$this->_cfg_url_ori || !$this->_cfg_cdn_mapping) {
if (!defined(self::BYPASS)) {
define(self::BYPASS, true);
Expand Down Expand Up @@ -152,24 +170,6 @@ private function _append_cdn_mapping( $filetype, $url ) {
}
}

/**
* If include css/js in CDN
*
* @since 1.6.2.1
* @return bool true if included in CDN
*/
public function inc_type( $type ) {
if ($type == 'css' && !empty($this->_cfg_cdn_mapping[Base::CDN_MAPPING_INC_CSS])) {
return true;
}

if ($type == 'js' && !empty($this->_cfg_cdn_mapping[Base::CDN_MAPPING_INC_JS])) {
return true;
}

return false;
}

/**
* Run CDN process
* NOTE: As this is after cache finalized, can NOT set any cache control anymore
Expand All @@ -182,6 +182,7 @@ public function finalize( $content ) {
$this->content = $content;

$this->_finalize();

return $this->content;
}

Expand Down