Skip to content

Commit

Permalink
fix(AssetsManager): detect local assets
Browse files Browse the repository at this point in the history
  • Loading branch information
J9rem committed Sep 15, 2023
1 parent c10d196 commit b419f42
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions includes/services/AssetsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ class AssetsManager
'javascripts/vendor/vue/vue.js' => 'javascripts/vendor/vue/vue.min.js',
];

protected $dataPath;
protected $wiki;

public function __construct(Wiki $wiki)
{
$this->wiki = $wiki;
$this->dataPath = $this->wiki->getDataPath();
}

public function AddCSS($style)
{
if (!isset($GLOBALS['css'])) {
Expand Down Expand Up @@ -72,10 +74,11 @@ public function AddCSSFile($file, $conditionstart = '', $conditionend = '', $att
public function LinkCSSFile($file, $conditionstart = '', $conditionend = '', $attrs = "")
{
$file = $this->mapFilePath($file);
$isUrl = strpos($file, "http://") === 0 || strpos($file, "https://") === 0;

if ($isUrl || !empty($file) && file_exists($file)) {
$href = $isUrl ? $file : "{$this->wiki->getBaseUrl(true)}/{$file}";
$isUrl = $this->isUrl($file);
$isLocal = !$isUrl && $this->isLocalFile($file);

if ($isUrl || $isLocal || (!empty($file) && file_exists($file))) {
$href = $isUrl ? $file : "{$this->wiki->getBaseUrl(!$isLocal)}/{$file}";
$revision = $this->wiki->GetConfigValue('yeswiki_release', null);
return <<<HTML
$conditionstart
Expand Down Expand Up @@ -107,12 +110,14 @@ public function AddJavascriptFile($file, $first = false, $module = false)
$revision = $this->wiki->GetConfigValue('yeswiki_release', null);
$initChar = (strpos($file, '?') !== false) ? '&' : '?';
$rev = ($revision) ? $initChar.'v='.$revision : '';

$file = $this->mapFilePath($file);
$isUrl = $this->isUrl($file);
$isLocal = !$isUrl && $this->isLocalFile($file);

if (!empty($file) && file_exists($file)) {
if (!$isUrl && ($isLocal || (!empty($file) && file_exists($file)))) {
// include local files
$code = "<script src='{$this->wiki->getBaseUrl(true)}/$file$rev'";
$code = "<script src='{$this->wiki->getBaseUrl(!$isLocal)}/$file$rev'";
if (!str_contains($GLOBALS['js'], $code) || $first) {
if (!$first) {
$code .= " defer";
Expand All @@ -127,7 +132,7 @@ public function AddJavascriptFile($file, $first = false, $module = false)
$GLOBALS['js'] .= $code;
}
}
} elseif (strpos($file, "http://") === 0 || strpos($file, "https://") === 0) {
} elseif ($isUrl) {
// include external files
$code = "<script defer src='$file.$rev'></script>";
if (!str_contains($GLOBALS['js'], $code)) {
Expand All @@ -137,6 +142,11 @@ public function AddJavascriptFile($file, $first = false, $module = false)
return;
}

protected function isUrl($file): bool
{
return !empty($file) && (strpos($file, "http://") === 0 || strpos($file, "https://") === 0);
}

private function mapFilePath($file)
{
// Handle backward compatibility
Expand All @@ -150,7 +160,17 @@ private function mapFilePath($file)
$file = self::PRODUCTION_PATH_MAPPING[$file];
}
}

return $file;
}

private function isLocalFile($file): bool
{
if (!empty($file) &&
!empty($this->dataPath) &&
file_exists("{$this->dataPath}$file")) {
return true;
}
return false;
}
}

0 comments on commit b419f42

Please sign in to comment.