Skip to content

Commit

Permalink
Merge branch '4.0' into 'main'
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Nov 14, 2024
2 parents 680dde0 + 46436ed commit 3c52f43
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
13 changes: 5 additions & 8 deletions phpmyfaq/src/phpMyFAQ/Export/Pdf/Wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ public function Image(// phpcs:ignore
$file = parse_url($file, PHP_URL_PATH);

$type = pathinfo($file, PATHINFO_EXTENSION);
$data = file_get_contents($this->concatenatePaths($file));
$data = file_get_contents(PMF_ROOT_DIR, $this->concatenatePaths($file));

if ($this->checkBase64Image($data)) {
$file = '@' . $data;
Expand Down Expand Up @@ -584,16 +584,13 @@ private function checkBase64Image(string $base64): bool
return $info && $info[0] > 0 && $info[1] > 0 && isset($info['mime']);
}

private function concatenatePaths(string $file): string
public function concatenatePaths(string $path, string $file): string
{
$trimmedPath = rtrim(PMF_ROOT_DIR, '/');
$trimmedPath = rtrim(str_replace('\\', '/', $path), '/');
$trimmedFile = ltrim($file, '/');

if (str_starts_with($trimmedFile, basename($trimmedPath))) {
$relativePath = substr($trimmedFile, strlen(basename($trimmedPath)));
} else {
$relativePath = $trimmedFile;
}
$pos = strpos($trimmedFile, 'content');
$relativePath = substr($trimmedFile, $pos);

return $trimmedPath . DIRECTORY_SEPARATOR . $relativePath;
}
Expand Down
59 changes: 59 additions & 0 deletions tests/phpMyFAQ/Export/Pdf/WrapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace phpMyFAQ\Export\Pdf;
use phpMyFAQ\Translation;
use PHPUnit\Framework\TestCase;

class WrapperTest extends TestCase
{
private Wrapper $wrapper;

protected function setUp(): void
{
parent::setUp();

Translation::create()
->setLanguagesDir(PMF_TRANSLATION_DIR)
->setDefaultLanguage('en')
->setCurrentLanguage('en')
->setMultiByteLanguage();

$this->wrapper = new Wrapper();
}

public function testConcatenatePathsWithUnixPaths(): void
{
$path = '/var/www/phpmyfaq';

$file = '/content/user/images/test.jpg';
$expected = '/var/www/phpmyfaq/content/user/images/test.jpg';
$this->assertEquals($expected, $this->wrapper->concatenatePaths($path, $file));
}

public function testConcatenatePathsWithWindowsPaths(): void
{
$path = 'C:\\xampp\\htdocs\\phpmyfaq';

$file = '/content/user/images/test.jpg';
$expected = 'C:/xampp/htdocs/phpmyfaq/content/user/images/test.jpg';
$this->assertEquals($expected, $this->wrapper->concatenatePaths($path, $file));
}

public function testConcatenatePathsWithMixedPaths(): void
{
$path = 'C:\\xampp\\htdocs\\phpmyfaq';

$file = '/content/user/images/test.jpg';
$expected = 'C:/xampp/htdocs/phpmyfaq/content/user/images/test.jpg';
$this->assertEquals($expected, $this->wrapper->concatenatePaths($path, $file));
}

public function testConcatenatePathsWithDuplicateRoot(): void
{
$path = 'C:\\xampp\\htdocs\\phpmyfaq';

$file = '/phpmyfaq/content/user/images/test.jpg';
$expected = 'C:/xampp/htdocs/phpmyfaq/content/user/images/test.jpg';
$this->assertEquals($expected, $this->wrapper->concatenatePaths($path, $file));
}
}

0 comments on commit 3c52f43

Please sign in to comment.