Skip to content

Commit

Permalink
[5.x] Add url friendly base64 en-/decoding for Glide (#11299)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcorieser authored Jan 6, 2025
1 parent e8349f5 commit c2ed1ce
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/Http/Controllers/GlideController.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function generateByUrl($url)
{
$this->validateSignature();

$url = base64_decode($url);
$url = Str::fromBase64Url($url);

return $this->createResponse($this->generateBy('url', $url));
}
Expand All @@ -90,7 +90,7 @@ public function generateByAsset($encoded)
{
$this->validateSignature();

$decoded = base64_decode($encoded);
$decoded = Str::fromBase64Url($encoded);

// The string before the first slash is the container
[$container, $path] = explode('/', $decoded, 2);
Expand Down
8 changes: 4 additions & 4 deletions src/Imaging/GlideUrlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ public function build($item, $params)

switch ($this->itemType()) {
case 'url':
$path = 'http/'.base64_encode($item);
$path = 'http/'.Str::toBase64Url($item);
$filename = Str::afterLast($item, '/');
break;
case 'asset':
$path = 'asset/'.base64_encode($this->item->containerId().'/'.$this->item->path());
$path = 'asset/'.Str::toBase64Url($this->item->containerId().'/'.$this->item->path());
$filename = Str::afterLast($this->item->path(), '/');
break;
case 'id':
$path = 'asset/'.base64_encode(str_replace('::', '/', $this->item));
$path = 'asset/'.Str::toBase64Url(str_replace('::', '/', $this->item));
break;
case 'path':
$path = URL::encode($this->item);
Expand All @@ -61,7 +61,7 @@ public function build($item, $params)

if (isset($params['mark']) && $params['mark'] instanceof Asset) {
$asset = $params['mark'];
$params['mark'] = 'asset::'.base64_encode($asset->containerId().'/'.$asset->path());
$params['mark'] = 'asset::'.Str::toBase64Url($asset->containerId().'/'.$asset->path());
}

return URL::prependSiteRoot($builder->getUrl($path, $params));
Expand Down
2 changes: 1 addition & 1 deletion src/Imaging/ImageGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private function setUpWatermark($watermark): string
private function getWatermarkFilesystemAndParam($item)
{
if (is_string($item) && Str::startsWith($item, 'asset::')) {
$decoded = base64_decode(Str::after($item, 'asset::'));
$decoded = Str::fromBase64Url(Str::after($item, 'asset::'));
[$container, $path] = explode('/', $decoded, 2);
$item = Assets::find($container.'::'.$path);
}
Expand Down
10 changes: 10 additions & 0 deletions src/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,16 @@ public static function ensureRight($string, $cap)
return IlluminateStr::finish($string, $cap);
}

public static function toBase64Url($url): string
{
return rtrim(strtr(base64_encode($url), '+/', '-_'), '=');
}

public static function fromBase64Url($url, $strict = false)
{
return base64_decode(strtr($url, '-_', '+/'), $strict);
}

/**
* Implicitly defer all other method calls to either \Stringy\StaticStringy or \Illuminate\Support\Str.
*
Expand Down
9 changes: 5 additions & 4 deletions tests/Imaging/GlideUrlBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Statamic\Assets\Asset;
use Statamic\Assets\AssetContainer;
use Statamic\Imaging\GlideUrlBuilder;
use Statamic\Support\Str;
use Tests\TestCase;

class GlideUrlBuilderTest extends TestCase
Expand Down Expand Up @@ -54,7 +55,7 @@ public function testAsset()
$asset->container((new AssetContainer)->handle('main'));
$asset->path('img/foo.jpg');

$encoded = base64_encode('main/img/foo.jpg');
$encoded = Str::toBase64Url('main/img/foo.jpg');

$this->assertEquals(
"/img/asset/$encoded/foo.jpg?w=100",
Expand All @@ -64,7 +65,7 @@ public function testAsset()

public function testId()
{
$encoded = base64_encode('main/img/foo.jpg');
$encoded = Str::toBase64Url('main/img/foo.jpg');

$this->assertEquals(
"/img/asset/$encoded?w=100",
Expand All @@ -78,7 +79,7 @@ public function testConfigAddsFilename()
$asset->container((new AssetContainer)->handle('main'));
$asset->path('img/foo.jpg');

$encoded = base64_encode('main/img/foo.jpg');
$encoded = Str::toBase64Url('main/img/foo.jpg');

$this->assertEquals(
"/img/asset/$encoded/foo.jpg?w=100",
Expand All @@ -92,7 +93,7 @@ public function testMarkWithAsset()
$asset->container((new AssetContainer)->handle('main'));
$asset->path('img/foo.jpg');

$encoded = rawurlencode(base64_encode('main/img/foo.jpg'));
$encoded = rawurlencode(Str::toBase64Url('main/img/foo.jpg'));

$this->assertEquals(
"/img/foo.jpg?w=100&mark=asset%3A%3A$encoded",
Expand Down

0 comments on commit c2ed1ce

Please sign in to comment.