Skip to content

Remove errors when open_basedir restriction is on #820

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 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions lib/css_js_min/minify/minify.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,43 @@ protected function restoreExtractedData($content)
return $content;
}

/**
* Check if the input is a file path.
*
* @param string $path
*
* @return bool
*/
protected function detectUrlType(string $input) {
// Is CSS
if (strpos($input, '{') !== false && strpos($input, '}') !== false && strpos($input, ';') !== false) {
return false;
}

// Is JS
if (strpos($input, 'function') !== false ||
strpos($input, 'var ') !== false ||
strpos($input, 'let ') !== false ||
strpos($input, 'const ') !== false ||
strpos($input, '=>') !== false ||
strpos($input, ';') !== false ||
strpos($input, '(') !== false && strpos($input, ')') !== false ||
strpos($input, '{') !== false && strpos($input, '}') !== false) {
return false;
}

// Is file path
if (preg_match('/^(https?:\/\/|www\.|[a-z0-9]+\.[a-z]+)/i', $input) ||
strpos($input, '/') === 0 ||
strpos($input, './') === 0 ||
strpos($input, '../') === 0 ||
strpos($input, 'data:') === 0) {
return true;
}

return false;
}

/**
* Check if the path is a regular file and can be read.
*
Expand All @@ -466,6 +503,16 @@ protected function restoreExtractedData($content)
*/
protected function canImportFile($path)
{
// Not file if path is empty
if(empty($path)){
return false;
}

// Not file? Return false
if (!$this->detectUrlType($path)) {
return false;
}

$parsed = parse_url($path);
if (
// file is elsewhere
Expand Down
40 changes: 23 additions & 17 deletions lib/css_js_min/pathconverter/converter.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,27 @@ class Converter implements ConverterInterface
*/
public function __construct($from, $to, $root = '')
{
$shared = $this->shared($from, $to);
if ($shared === '') {
// when both paths have nothing in common, one of them is probably
// absolute while the other is relative
$root = $root ?: getcwd();
$from = strpos($from, $root) === 0 ? $from : preg_replace('/\/+/', '/', $root.'/'.$from);
$to = strpos($to, $root) === 0 ? $to : preg_replace('/\/+/', '/', $root.'/'.$to);

// or traveling the tree via `..`
// attempt to resolve path, or assume it's fine if it doesn't exist
$from = @realpath($from) ?: $from;
$to = @realpath($to) ?: $to;
}
if(!empty($from)){
$shared = $this->shared($from, $to);
if ($shared === '') {
// when both paths have nothing in common, one of them is probably
// absolute while the other is relative
$root = $root ?: getcwd();
$from = strpos($from, $root) === 0 ? $from : preg_replace('/\/+/', '/', $root.'/'.$from);
$to = strpos($to, $root) === 0 ? $to : preg_replace('/\/+/', '/', $root.'/'.$to);

// or traveling the tree via `..`
// attempt to resolve path, or assume it's fine if it doesn't exist
$from = @realpath($from) ?: $from;
$to = @realpath($to) ?: $to;
}

$from = $this->dirname($from);
$to = $this->dirname($to);
$from = $this->dirname($from);
$to = $this->dirname($to);

$from = $this->normalize($from);
$to = $this->normalize($to);
$from = $this->normalize($from);
$to = $this->normalize($to);
}

$this->from = $from;
$this->to = $to;
Expand Down Expand Up @@ -191,6 +193,10 @@ public function convert($path)
*/
protected function dirname($path)
{
if(empty($path)){
return $path;
}

if (@is_file($path)) {
return dirname($path);
}
Expand Down