Skip to content

Instantly share code, notes, and snippets.

@Acen
Last active Dec 7, 2020
Embed
What would you like to do?
Facebook PHP Manual Unshim
<?php
function manualUnshim(string $url): string
{
if(!str_contains($url, 'l.facebook.com')){
return $url;
}
$urlArray = explode('u=', $url);
$decodedUrl = urldecode($urlArray[1]);
$parsed_url = parse_url($decodedUrl);
unset($parsed_url['query']);
$urlClass = new UnparsedURL($parsed_url);
return $urlClass->url();
}
<?php
/**
* Sourced from https://gist.github.com/Ellrion/f51ba0d40ae1d62eeae44fd1adf7b704
*/
class UnparsedURL
{
private $parts;
public function __construct(array $parts){
$this->parts = $parts;
}
/**
* @return string
*/
public function url(): string
{
$scheme = isset($this->parts['scheme']) ? $this->parts['scheme'] . '://' : '';
$host = $this->parts['host'] ?? '';
$port = isset($this->parts['port']) ? ':' . $this->parts['port'] : '';
$user = $this->parts['user'] ?? '';
$pass = isset($this->parts['pass']) ? ':' . $this->parts['pass'] : '';
$pass = $user || $pass ? "$pass@" : '';
$path = $this->parts['path'] ?? '';
$query = isset($this->parts['query']) ? '?' . $this->parts['query'] : '';
$fragment = isset($this->parts['fragment']) ? '#' . $this->parts['fragment'] : '';
return implode('', [
$scheme,
$user,
$pass,
$host,
$port,
$path,
$query,
$fragment,
]);
}
/**
* @return string
*/
public function __toString(): string
{
return $this->url();
}
}
<?php
use PHPUnit\Framework\TestCase;
class UnparsedURLTest extends TestCase
{
public function UrlDataProvider(): array
{
return [
[parse_url('http://www.google.com/'), 'http://www.google.com/'],
[parse_url('http://@foo:1/path/path?#'), 'http://foo:1/path/path'],
[
parse_url('http://u:p@foo:1/path/path?q#frag'),
'http://u:p@foo:1/path/path?q#frag',
],
[
[
'scheme' => 'https',
'host' => 'domain.co.nz',
'path' =>
'/blahblah-blah',
],
'https://domain.co.nz/blahblah-blah'
],
];
}
/**
* @dataProvider UrlDataProvider
* @param array $input
* @param $expected
*/
public function testUrl(array $input, $expected): void
{
$unparsedUrl = new UnparsedURL($input);
self::assertSame($expected, $unparsedUrl->url());
}
/**
* @dataProvider UrlDataProvider
* @param array $input
* @param $expected
*/
public function test__toString(array $input, $expected): void
{
$this->expectOutputString($expected);
$unparsedUrl = new UnparsedURL($input);
echo $unparsedUrl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment